home *** CD-ROM | disk | FTP | other *** search
- /* > $.CLIB.C.files
- *
- * HASWIN Graphics Library
- * =========================
- *
- * Copyright (C) H.A.Shaw 1990.
- * Howard A. Shaw.
- * The Unit for Space Sciences,
- * Room 165,
- * Physics Building,
- * University of Kent at Canterbury.
- * Canterbury.
- * Kent. CT2 7NJ
- * You may use and distribute this code freely, however please leave
- * it alone. If you find bugs (and there will be many) please contact
- * me and the master source can be modified. If you keep me informed
- * of who you give copies of this to then I can get release upgrades
- * to them.
- *
- */
- #include "includes.h"
-
- icon *haswin_loadiconok=0; /* this must be accessed by menu
- routines to allow "Files.Load"
- selection to work */
- static icon *haswin_loadiconup=0;
- static icon *haswin_loadicondn=0;
- static icon *haswin_loadiconbusy=0;
- icon *haswin_saveiconok=0; /* this must be accessed by menu
- routines to allow "Files.Save"
- selection to work */
- static icon *haswin_saveiconup=0;
- static icon *haswin_saveicondn=0;
- static icon *haswin_saveiconbusy=0;
-
- void haswin_setsavefileroutine(int (*user)(char *, buffer *), char *name, char *type) {
-
- char tmp[9];
-
- if ((haswin_flags & HASWIN_FLAGS_FILEAUTO) == 0) {
- haswin_internalerror("haswin_setsavefileroutine: called before file initialisation, ignored");
- return;
- }
- haswin_savefileroutine = user;
- if (!name) {
- haswin_defaultsavefile[0] = 0;
- haswin_seticondata(haswin_saveiconname, "");
- haswin_flags &= ~HASWIN_FLAGS_FILEMENU;
- } else {
- strcpy(haswin_defaultsavefile, name);
- haswin_seticondata(haswin_saveiconname, name);
- haswin_flags |= HASWIN_FLAGS_FILEMENU;
- haswin_savefiletype = haswin_encodefiletype(type);
- sprintf(tmp, "file_%3.3x", haswin_savefiletype);
- if (haswin_findsprite(tmp) != HASWIN_UNKNOWN);
- haswin_seticondata(haswin_saveicon, tmp);
- haswin_decodefiletype(haswin_savefiletype, tmp);
- haswin_seticondata(haswin_saveicontype, tmp);
- }
- }
-
- void haswin_setloadfileroutine(int (*user)(char *, buffer *), char *name, char *type) {
-
- char tmp[9];
-
- if ((haswin_flags & HASWIN_FLAGS_FILEAUTO) == 0) {
- haswin_internalerror("haswin_setloadfileroutine: called before file initialisation, ignored");
- return;
- }
- haswin_loadfileroutine = user;
- if (!name) {
- haswin_defaultloadfile[0] = 0;
- haswin_seticondata(haswin_loadiconname, "");
- haswin_flags &= ~HASWIN_FLAGS_FILEMENU;
- } else {
- strcpy(haswin_defaultloadfile, name);
- haswin_seticondata(haswin_loadiconname, name);
- haswin_flags |= HASWIN_FLAGS_FILEMENU;
- haswin_loadfiletype = haswin_encodefiletype(type);
- sprintf(tmp, "file_%3.3x", haswin_loadfiletype);
- if (haswin_findsprite(tmp) != HASWIN_UNKNOWN);
- haswin_seticondata(haswin_loadicon, tmp);
- haswin_decodefiletype(haswin_loadfiletype, tmp);
- haswin_seticondata(haswin_loadicontype, tmp);
- }
- }
-
- void haswin_setautoloadroutine(int (*user)(char *, buffer *), char *type) {
-
- if ((haswin_flags & HASWIN_FLAGS_FILEAUTO) == 0) {
- haswin_internalerror("haswin_setautoloadroutine: called before file initialisation, ignored");
- return;
- }
- haswin_autoloadroutine = user;
- haswin_autoloadtype = haswin_encodefiletype(type);
- }
-
- void haswin_loadfilemugwatch(int direction) {
-
- static char spin[] = "rotor_0";
-
- if ((haswin_flags & HASWIN_FLAGS_FILEAUTO) == 0) {
- haswin_internalerror("haswin_loadfilemugwatch: called before file initialisation, ignored");
- return;
- }
- if (direction == 0)
- spin[6] = '0';
- else if (direction > 0) {
- if (++spin[6] >= '9')
- spin[6] = '1';
- } else if (direction < 0) {
- if (--spin[6] <= '0')
- spin[6] = '8';
- }
- haswin_seticondata(haswin_loadiconbusy, spin);
- }
-
- void haswin_savefilemugwatch(int direction) {
-
- static char spin[] = "rotor_0";
-
- if ((haswin_flags & HASWIN_FLAGS_FILEAUTO) == 0) {
- haswin_internalerror("haswin_savefilemugwatch: called before file initialisation, ignored");
- return;
- }
- if (direction == 0)
- spin[6] = '0';
- else if (direction > 0) {
- if (++spin[6] >= '9')
- spin[6] = '1';
- } else if (direction < 0) {
- if (--spin[6] <= '0')
- spin[6] = '8';
- }
- haswin_seticondata(haswin_saveiconbusy, spin);
- }
-
- int filemousebutton(icon *ic, buffer *pbuf) {
-
- char type[9], str[256];
- int i, oldtype = 0, button;
- buffer mybuf;
-
- if ((ic == haswin_saveiconname) || (ic == haswin_saveiconok)) {
- if (haswin_savefileroutine) {
- strcpy(str,haswin_geticontitle(haswin_saveiconname));
- for (i=asciilen(str); i>=0; i--)
- if (str[i] == '.')
- break;
- strcpy(haswin_defaultsavefile, &(str[i+1]));
- haswin_seticondata(haswin_saveiconname,
- haswin_defaultsavefile);
- /* make a Message Data Block as if we called the
- user routine from a WIMP DataSave Message */
- button = pbuf->i[2] & POINTER_MOUSEBUTTONS;
- mybuf.i[ 1] = 0;
- mybuf.i[ 2] = 0;
- mybuf.i[ 3] = 0;
- mybuf.i[ 4] = MESSAGE_DataSave;
- mybuf.i[ 5] = pbuf->i[3];
- mybuf.i[ 6] = pbuf->i[4];
- mybuf.i[ 7] = pbuf->i[0];
- mybuf.i[ 8] = pbuf->i[1];
- mybuf.i[ 9] = 0;
- mybuf.i[10] = haswin_savefiletype;
- strcpy(&mybuf.c[44], haswin_defaultsavefile);
- mybuf.i[ 0] = (48+asciilen(haswin_defaultsavefile))&0xFFFFFFFC;
- if (((*haswin_savefileroutine)(haswin_defaultsavefile, &mybuf)) && (button == POINTER_MOUSE_L)) {
- /*
- * close any open menus cos we probably came
- * from one.
- */
- haswin_closewindow(haswin_saveingwindow);
- haswin_removemenu();
- }
- if ((*haswin_savefileroutine)(&mybuf.c[44], 0))
- haswin_setfiletype(&mybuf.c[44],mybuf.i[10]);
- haswin_flags &= ~HASWIN_FLAGS_SAVING;
- } else {
- haswin_interrorprintf("cannot save '%s', no user routine", haswin_defaultsavefile);
- }
- haswin_closewindow(haswin_saveingwindow);
- } else if ((ic==haswin_loadiconname) || (ic==haswin_loadiconok)) {
- if (haswin_loadfileroutine) {
- strcpy(str,haswin_geticontitle(haswin_loadiconname));
- for (i=asciilen(str); i>=0; i--)
- if (str[i] == '.')
- break;
- strcpy(haswin_defaultloadfile, &(str[i+1]));
- haswin_seticondata(haswin_loadiconname,
- haswin_defaultloadfile);
- /* make a Message Data Block as if we called the
- user routine from a WIMP DataLoad Message */
- button = pbuf->i[2]&POINTER_MOUSEBUTTONS;
- mybuf.i[ 1] = 0;
- mybuf.i[ 2] = 0;
- mybuf.i[ 3] = 0;
- mybuf.i[ 4] = MESSAGE_DataLoad;
- mybuf.i[ 5] = pbuf->i[3];
- mybuf.i[ 6] = pbuf->i[4];
- mybuf.i[ 7] = pbuf->i[0];
- mybuf.i[ 8] = pbuf->i[1];
- mybuf.i[ 9] = 0;
- mybuf.i[10] = haswin_loadfiletype;
- strcpy(&mybuf.c[44], haswin_defaultloadfile);
- mybuf.i[ 0] = (48+asciilen(haswin_defaultloadfile))&0xFFFFFFFC;
- if (((*haswin_loadfileroutine)(haswin_defaultloadfile, &mybuf)) && (button == POINTER_MOUSE_L)) {
- /*
- * close any open menus cos we probably came
- * from one.
- */
- haswin_closewindow(haswin_loadingwindow);
- haswin_removemenu();
- }
- } else {
- haswin_interrorprintf("cannot load '%s', no user routine", haswin_defaultloadfile);
- }
- haswin_closewindow(haswin_loadingwindow);
- } else if (ic == haswin_loadiconup) {
- oldtype = 0;
- do {
- if ((++oldtype & 0x07) == 0)
- haswin_loadfilemugwatch(+1);
- if (++haswin_loadfiletype >= 0x1000)
- haswin_loadfiletype = 0;
- sprintf(type, "file_%3.3x", haswin_loadfiletype);
- } while (haswin_findsprite(type) == HASWIN_UNKNOWN);
- haswin_seticondata(haswin_loadicon, type);
- haswin_decodefiletype(haswin_loadfiletype, type);
- haswin_seticondata(haswin_loadicontype, type);
- haswin_loadfilemugwatch(0);
- } else if (ic == haswin_loadicondn) {
- oldtype = 0;
- do {
- if ((++oldtype & 0x07) == 0)
- haswin_loadfilemugwatch(-1);
- if (--haswin_loadfiletype <= 0)
- haswin_loadfiletype = 0xFFF;
- sprintf(type, "file_%3.3x", haswin_loadfiletype);
- } while (haswin_findsprite(type) == HASWIN_UNKNOWN);
- haswin_seticondata(haswin_loadicon, type);
- haswin_decodefiletype(haswin_loadfiletype, type);
- haswin_seticondata(haswin_loadicontype, type);
- haswin_loadfilemugwatch(0);
- } else if (ic == haswin_saveiconup) {
- oldtype = 0;
- do {
- if ((++oldtype & 0x07) == 0)
- haswin_savefilemugwatch(+1);
- if (++haswin_savefiletype >= 0x1000)
- haswin_savefiletype = 0;
- sprintf(type, "file_%3.3x", haswin_savefiletype);
- } while (haswin_findsprite(type) == HASWIN_UNKNOWN);
- haswin_seticondata(haswin_saveicon, type);
- haswin_decodefiletype(haswin_savefiletype, type);
- haswin_seticondata(haswin_saveicontype, type);
- haswin_savefilemugwatch(0);
- } else if (ic == haswin_saveicondn) {
- oldtype = 0;
- do {
- if ((++oldtype & 0x07) == 0)
- haswin_savefilemugwatch(-1);
- if (--haswin_savefiletype <= 0)
- haswin_savefiletype = 0xFFF;
- sprintf(type, "file_%3.3x", haswin_savefiletype);
- } while (haswin_findsprite(type) == HASWIN_UNKNOWN);
- haswin_seticondata(haswin_saveicon, type);
- haswin_decodefiletype(haswin_savefiletype, type);
- haswin_seticondata(haswin_saveicontype, type);
- haswin_savefilemugwatch(0);
- } else if (ic == haswin_loadicontype) {
- haswin_loadfiletype = haswin_encodefiletype(haswin_geticontitle(ic));
- sprintf(type, "file_%3.3x", haswin_loadfiletype);
- if (haswin_findsprite(type) == HASWIN_UNKNOWN)
- strcpy(type, "file_xxx");
- haswin_seticondata(haswin_loadicon, type);
- haswin_decodefiletype(haswin_loadfiletype, type);
- haswin_seticondata(haswin_loadicontype, type);
- } else if (ic == haswin_saveicontype) {
- haswin_savefiletype = haswin_encodefiletype(haswin_geticontitle(ic));
- sprintf(type, "file_%3.3x", haswin_savefiletype);
- if (haswin_findsprite(type) == HASWIN_UNKNOWN)
- strcpy(type, "file_xxx");
- haswin_seticondata(haswin_saveicon, type);
- haswin_decodefiletype(haswin_savefiletype, type);
- haswin_seticondata(haswin_saveicontype, type);
- }
- return(HASWIN_TRUE);
- }
-
- static int savefiledrag(window *win, icon *ic, pointer *mouse, buffer *pbuf) {
-
- int i, buttons;
- _kernel_swi_regs regs;
- char str[256];
-
- if ((ic != haswin_saveicon) || (win != haswin_saveingwindow))
- return(HASWIN_FALSE);
- if (pbuf == 0)
- /* drag started, let it continue */
- return(HASWIN_TRUE);
- haswin_flags |= HASWIN_FLAGS_SAVING;
- strcpy(str, haswin_geticontitle(haswin_saveiconname));
- for (i=asciilen(str); i>=0; i--)
- if (str[i] == '.')
- break;
- strcpy(haswin_defaultsavefile, &(str[i+1]));
- buttons = mouse->buttons;
- mouse = haswin_getpointerinfo(mouse);
- pbuf->i[ 3] = 0;
- pbuf->i[ 4] = MESSAGE_DataSave;
- pbuf->i[ 5] = mouse->whandle;
- pbuf->i[ 6] = mouse->ihandle;
- pbuf->i[ 7] = mouse->mx;
- pbuf->i[ 8] = mouse->my;
- pbuf->i[ 9] = 0;
- pbuf->i[10] = haswin_savefiletype;
- strcpy(&(pbuf->c[44]), haswin_defaultsavefile);
- pbuf->i[ 0]=(48+asciilen(haswin_defaultsavefile)) & 0xFFFFFFFC;
- regs.r[0] = HASWIN_POLL_Message_Rec;
- regs.r[1] = (int)pbuf;
- regs.r[2] = mouse->whandle;
- regs.r[3] = mouse->ihandle;
- if ((buttons&POINTER_MOUSEBUTTONS) == POINTER_MOUSE_L) {
- /*
- * close any open menus cos we probably came from one.
- */
- haswin_removemenu();
- haswin_closewindow(haswin_saveingwindow);
- }
- return(haswin_swi(HASWIN_Send_message, ®s));
- }
-
- static int loadfiledrag(window *win, icon *ic, pointer *mouse, buffer *pbuf) {
-
- int i, buttons;
- _kernel_swi_regs regs;
- char str[256];
-
- if ((ic != haswin_loadicon) || (win != haswin_loadingwindow))
- return(HASWIN_FALSE);
- if (pbuf == 0)
- /* drag started, let it continue */
- return(HASWIN_TRUE);
- haswin_flags |= HASWIN_FLAGS_LOADING;
- strcpy(str, haswin_geticontitle(haswin_loadiconname));
- for (i=asciilen(str); i>=0; i--)
- if (str[i] == '.')
- break;
- strcpy(haswin_defaultloadfile, &(str[i+1]));
- buttons = mouse->buttons;
- mouse = haswin_getpointerinfo(0);
- pbuf->i[ 3] = 0;
- pbuf->i[ 4] = MESSAGE_DataSave;
- pbuf->i[ 5] = mouse->whandle;
- pbuf->i[ 6] = mouse->ihandle;
- pbuf->i[ 7] = mouse->mx;
- pbuf->i[ 8] = mouse->my;
- pbuf->i[ 9] = 0;
- pbuf->i[10] = haswin_loadfiletype;
- strcpy(&(pbuf->c[44]), haswin_defaultloadfile);
- pbuf->i[ 0]=(48+asciilen(haswin_defaultloadfile)) & 0xFFFFFFFC;
- regs.r[0] = HASWIN_POLL_Message_Rec;
- regs.r[1] = (int)pbuf;
- regs.r[2] = mouse->whandle;
- regs.r[3] = mouse->ihandle;
- if ((buttons&POINTER_MOUSEBUTTONS) == POINTER_MOUSE_L) {
- /*
- * close any open menus cos we probably came from one.
- */
- haswin_removemenu();
- haswin_closewindow(haswin_loadingwindow);
- }
- return(haswin_swi(HASWIN_Send_message, ®s));
- }
-
- /*
- * this routine initialises all of the variables used by the HASWIN
- * automatic file load/save routines.
- */
- int haswin_fileinitialise() {
-
- haswin_saveingwindow = haswin_makewindow("Save as:", 250, 240, 7, 2, 7, 1, WINDOW_TITLE_BAR|WINDOW_MOVEABLE);
- haswin_saveingwindow->name = haswin_realloc(haswin_saveingwindow->name, 10, "haswin_fileinitialise", "save window name");
- strcpy(haswin_saveingwindow->name, "file_save");
- haswin_saveingwindow->pointer = haswin_makepointer("ptr_disc", 5, 0);
- haswin_saveiconup = haswin_createicon(haswin_saveingwindow, "P2 S^:uparrow", 7, 0, 0, 8, -160, 35, -110);
- haswin_saveiconup->mousebutton = filemousebutton;
- haswin_saveicondn = haswin_createicon(haswin_saveingwindow, "P2 S^:downarrow", 7, 0, 0, 215, -160, 242, -110);
- haswin_saveicondn->mousebutton = filemousebutton;
- haswin_saveicon = haswin_createicon(haswin_saveingwindow, "SD^:file_xxx", 7, 0, 0, 85, -90, 160, -25);
- haswin_saveicon->dragroutine = savefiledrag;
- haswin_saveiconname = haswin_createicon(haswin_saveingwindow, "BFT+256 ^:SaveFile", 7, 0, 0, 8, -232, 185, -182);
- haswin_saveiconname->mousebutton = filemousebutton;
- haswin_saveiconok = haswin_createicon(haswin_saveingwindow, "P9 TFB^:OK", 7, 14, 1, 190, -232, 240, -182);
- haswin_saveiconok->mousebutton = filemousebutton;
- haswin_saveicontype = haswin_createicon(haswin_saveingwindow, "P15 BFT+15 ^:file_xxx", 7, 0, 0, 45, -160, 205, -110);
- haswin_saveicontype->mousebutton = filemousebutton;
- haswin_saveiconbusy = haswin_createicon(haswin_saveingwindow, "P0 S^:rotor_0", 7, 0, 0, 10, -73, 60, -23);
-
- haswin_loadingwindow = haswin_makewindow("Load from:", 250, 240, 7, 2, 7, 1, WINDOW_TITLE_BAR|WINDOW_MOVEABLE);
- haswin_loadingwindow->name = haswin_realloc(haswin_loadingwindow->name, 10, "haswin_fileinitialise", "load window name");
- strcpy(haswin_loadingwindow->name, "file_load");
- haswin_loadingwindow->pointer = haswin_makepointer("ptr_disc", 5, 0);
- haswin_loadiconup = haswin_createicon(haswin_loadingwindow, "P2 S^:uparrow", 7, 0, 0, 8, -160, 35, -110);
- haswin_loadiconup->mousebutton = filemousebutton;
- haswin_loadicondn = haswin_createicon(haswin_loadingwindow, "P2 S^:downarrow", 7, 0, 0, 215, -160, 242, -110);
- haswin_loadicondn->mousebutton = filemousebutton;
- haswin_loadicon = haswin_createicon(haswin_loadingwindow, "SD^:file_xxx", 7, 0, 0, 85, -90, 160, -25);
- haswin_loadicon->dragroutine = loadfiledrag;
- haswin_loadiconname = haswin_createicon(haswin_loadingwindow, "BFT+256 ^:LoadFile", 7, 0, 0, 8, -232, 185, -182);
- haswin_loadiconname->mousebutton = filemousebutton;
- haswin_loadiconok = haswin_createicon(haswin_loadingwindow, "P9 TFB^:OK", 7, 14, 1, 190, -232, 240, -182);
- haswin_loadiconok->mousebutton = filemousebutton;
- haswin_loadicontype = haswin_createicon(haswin_loadingwindow, "P15 BFT+15 ^:file_xxx", 7, 0, 0, 45, -160, 205, -110);
- haswin_loadicontype->mousebutton = filemousebutton;
- haswin_loadiconbusy = haswin_createicon(haswin_loadingwindow, "P0 S^:rotor_0", 7, 0, 0, 10, -73, 60, -23);
-
- haswin_flags |= HASWIN_FLAGS_FILEAUTO;
- return(HASWIN_TRUE);
- }
-
- /*
- * routine to convert a filetype from integer into text
- */
- void haswin_decodefiletype(int type, char *str) {
-
- _kernel_swi_regs regs, ans;
- _kernel_oserror *err;
-
- if (type == 0x1000) {
- strcpy(str, "Dir. ");
- return;
- }
- if (type == 0x2000) {
- strcpy(str, "Appl. ");
- return;
- }
- regs.r[0] = 18;
- regs.r[2] = type;
- if ((err = _kernel_swi(OS_FSControl|SWI_X, ®s, &ans)) == 0) {
- ((int *)str)[0] = ans.r[2];
- ((int *)str)[1] = ans.r[3];
- } else {
- haswin_interrorprintf("haswin_decodefiletype, filetype %X, %s", type, err->errmess);
- /* put spaces in str */
- ((int *)str)[0] = 0x20202020;
- ((int *)str)[1] = 0x20202020;
- }
- str[8] = 0;
- return;
- }
-
- /*
- * routine to convert a filetype from text into an integer
- */
- int haswin_encodefiletype(char *str) {
-
- _kernel_swi_regs regs, ans;
- _kernel_oserror *err;
-
- if (!strncmp(str, "Dir.", 4))
- return(0x1000);
- if (!strncmp(str, "Appl.", 5))
- return(0x2000);
- regs.r[0] = 31;
- regs.r[1] = (int)str;
- if ((err = _kernel_swi(OS_FSControl|SWI_X, ®s, &ans)) == 0)
- return(ans.r[2]);
- haswin_interrorprintf("haswin_encodefiletype, filetype '%s', %s",
- str, err->errmess);
- return(0);
- }
-
- /*
- * routine to convert a set a filetype in the filesystem
- */
- int haswin_setfiletype(char *name, int type) {
-
- _kernel_swi_regs regs;
-
- regs.r[0] = 18;
- regs.r[1] = (int)name;
- regs.r[2] = type & 0x00000FFF;
- return(haswin_swi(OS_File, ®s));
- }
-
- int haswin_clearfileoptions(int new) {
-
- haswin_fileoptions &= ~new;
- return(haswin_fileoptions);
- }
-
- int haswin_getfileoptions(void) {
-
- return(haswin_fileoptions);
- }
-
- int haswin_setfileoptions(int new) {
-
- haswin_fileoptions |= new;
- return(haswin_fileoptions);
- }
-
-