/* Copyright (C) Acorn Computers Ltd 1994 */

/*
 * Name   : !Hyper
 * Desc   : An example of a C task written using the Toolbox
 *
 *
 * **********************************************
 * * Disclaimer                                 *
 * * ----------                                 *
 * *                                            *
 * * The software is provided "as is" Acorn     *
 * * Computers Limited ("Acorn") makes no       *
 * * warranty, express or implied, of the       *
 * * merchantability of this software or its    *
 * * fitness for any particular purpose. In no  *
 * * circumstances shall Acorn be liable for any*
 * * damage, loss of profits, or any indirect or*
 * * consequential loss arising out of the use  *
 * * of this software or inability to use this  *
 * * software, even if Acorn has been advised of*
 * * the possibility of such loss.              *
 * **********************************************
 */

/* include standard C definitions */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "swis.h"

/* include event lib declarations */

#include "event.h"

/* include renderlib stuff */

#include "drawfile.h"

/* include toolbox lib declarations */

#include "toolbox.h"
#include "window.h"
#include "gadgets.h"
#include "menu.h"
#include "scale.h"
#include "fileinfo.h"
#include "printdbox.h"
#include "proginfo.h"
#include "iconbar.h"
#include "quit.h"
#include "wimplib.h"

/* include application definitions */

#include "hyper.h"
#include "button.h"
#include "draw.h"
#include "events.h"
#include "utils.h"
#include "comps.h"
#include "handler.h"
#include "view.h"
#include "menus.h"
#include "file.h"

/* static/global data */

int quit=0;

/* linked list of all views that we have loaded */

Viewer *views = NULL;

/* the message block and toolbox id block */

static IdBlock idblk;
MessagesFD mbl;

/* if we receive any toolbox errors, then just report them in a dialogue */

static int toolbox_error_handler(int event_code, ToolboxEvent  *event, IdBlock *id_block,void  *handle)
{
   ToolboxErrorEvent *error = (ToolboxErrorEvent *) event;
   _kernel_oserror *err = (_kernel_oserror *) &error->errnum;

   IGNORE(event_code);
   IGNORE(handle);
   IGNORE(id_block);

   wimp_report_error(err,0,0);

   return 1;
}

int default_scale;

/* if we are running in a small screen mode, then the cards may be too big
   to fit. check_mode returns a suitable scale (in percent) for the current mode */

static int check_mode(void)
{
   _kernel_swi_regs regs;
   int width;

   regs.r[0] = -1;
   regs.r[1] = 11;

   _kernel_swi (OS_ReadModeVariable,&regs,&regs);
   width = 1+regs.r[2];

   regs.r[1] = 4;
   _kernel_swi (OS_ReadModeVariable,&regs,&regs);
   width = width << regs.r[2];

   /* Mode 31 ish */
   if (width >= 1600) return 100;

   /* mode 28 ish */
   if (width >= 1280) return 66;

   return 40;
}

/* these are all the Wimp messages and toolbox events that !Hyper is
   interested in */

static int messages [] ={
        Wimp_MQuit,
        Wimp_MDataOpen,
        Wimp_MDataLoad,
        0};

static int tbcodes [] ={
        IbarAboutToBeShown,
        ViewEvent,
        Quit_Quit,
        Scale_ApplyFactor,
        Scale_AboutToBeShown,
        PrintDbox_Print,
        FileInfo_AboutToBeShown,
        ProgInfo_AboutToBeShown,
        StatusEvent,
        ViewMenuShow,
        NextFindEvent,
        HomeButtonEvent,
        BackButtonEvent,
        Toolbox_Error,
        0};
/*
 * Initialise the task (we use toolbox_initialise instead of wimp_initialise)
 * and register any events handlers.
 */

static void app_init(void)
{
     _kernel_oserror *e;

     /* initialise as a toolbox task */

     if ((e=toolbox_initialise(0,310, messages, tbcodes, "<hyper$dir>",&mbl, &idblk,0,0,0)) != NULL) {
        wimp_report_error(e,0,0);
        exit(1);
     }

     /* initialise event lib */

     event_initialise(&idblk);

     /* not interested in nulls or keypresses- the toolbox handles
        all our keyboard shortcuts */

     event_set_mask(1+256);

     /* register events */

     event_register_toolbox_handler(-1,IbarAboutToBeShown,build_views,(void *) &views);
     event_register_toolbox_handler(-1,ViewEvent,alter_views,(void *) &views);

     event_register_message_handler(Wimp_MQuit,quit_handler,0);
     event_register_toolbox_handler(-1,Quit_Quit,tbquit_handler,NULL);
     event_register_toolbox_handler(-1,Toolbox_Error,toolbox_error_handler,NULL);

     event_register_toolbox_handler(-1,Scale_ApplyFactor,scale_view,NULL);
     event_register_toolbox_handler(-1,Scale_AboutToBeShown,scale_show,NULL);

     event_register_message_handler(Wimp_MDataOpen,file_loader,0);
     event_register_message_handler(Wimp_MDataLoad,file_loader,0);

     event_register_toolbox_handler(-1,PrintDbox_Print,print_card,0);
     event_register_toolbox_handler(-1,FileInfo_AboutToBeShown,fileinfo_show,0);
     event_register_toolbox_handler(-1,ProgInfo_AboutToBeShown,proginfo_show,0);
     event_register_toolbox_handler(-1,StatusEvent,toggle_status,0);
     event_register_toolbox_handler(-1,ViewMenuShow,menu_setup,0);
     event_register_toolbox_handler(-1,NextFindEvent,search_handler,0);

     /* register previous/home handler */

     event_register_toolbox_handler(-1,-1,misc_handler,0);
}

char * view_name = "HyperViewer";

int main(int argc, char *argv[])
{
     int i;

     default_scale = check_mode();

     for(i =1; i<argc;i++) {
        /* two command line arguments are supported :-
                left   - makes us use a different viewer template (by default with
                         a toolbar to the left)
                scale  - to set a different default scale
         */

        if (!strcmp(argv[i],"-left")) view_name = "HyperView2";
        if (!strcmp(argv[i],"-scale")) default_scale = atoi (argv[++i]);
     }

     app_init();

     for(i =1; i<argc;i++) {
        /* anything without a '-' in front is interpreted as a command line file */
        if (*argv[i] != '-') new_viewer(argv[i]);
     }

     while (!quit) {
        event_poll(0,0,0);
     }

     exit(0);

}


