home *** CD-ROM | disk | FTP | other *** search
- /* > $.CLIB.C.templates
- *
- * 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.
- *
- * Routines to deal with templates. Since the WIMP allows only one
- * template to be open at a time we implement a stack.
- */
- #include "includes.h"
-
- /*
- * we can stack open template files up to HASWIN_MAXTEMPLATES levels
- */
- #define HASWIN_MAXTEMPLATES 16
- static char (haswin_templatestack[256])[HASWIN_MAXTEMPLATES];
- static int haswin_templatestackptr = 0;
-
- void haswin_closetemplate() {
-
- if (haswin_flags & HASWIN_FLAGS_TEMPLATE) {
- if (haswin_templatewspace) {
- if (!haswin_free(haswin_templatewspace))
- haswin_errorbox("haswin_closetemplate: cannot free template workspace");
- haswin_templatewspace = 0;
- }
- if (haswin_templateptr) {
- if (!haswin_free(haswin_templateptr))
- haswin_errorbox("haswin_closetemplate: cannot free template indirect space");
- haswin_templateptr = 0;
- }
- if (!haswin_swi(HASWIN_Close_template, 0))
- return;
- haswin_flags &= ~HASWIN_FLAGS_TEMPLATE;
- }
- }
-
- /*
- * open a template file.
- */
- int haswin_opentemplate(char *filename) {
-
- int fsize;
- _kernel_swi_regs regs;
-
- if (haswin_flags & HASWIN_FLAGS_TEMPLATE)
- return(HASWIN_FALSE);
- if ((!filename) || (filename[0] == '\0'))
- return(HASWIN_TRUE);
- regs.r[0] = 5; /* get file catalogue */
- regs.r[1] = (int)filename;
- if (!haswin_swi(OS_File, ®s))
- regs.r[0] = 0;
- switch (regs.r[0]) {
- case 0:
- haswin_interrorprintf("Cannot find template file %s", filename);
- return(HASWIN_FALSE);
- case 2:
- haswin_interrorprintf("Template file %s is a directory, not a file", filename);
- return(HASWIN_FALSE);
- }
- if ((regs.r[5] & 0x01) != 0x01) {
- haswin_interrorprintf("No read access for template file %s",filename);
- return(HASWIN_FALSE);
- }
- /* create a buffer for the templates. If it is the size of the
- file then it MUST be enough !! */
- fsize = regs.r[4];
- haswin_templatewspace = (char *)haswin_malloc(fsize, "haswin_opentemplate", "haswin_templatewspace");
- /* create a buffer for the indirected data in the templates. If it
- is the size of the file then it MUST be enough !! */
- haswin_templateptr = (char *)haswin_malloc(fsize, "haswin_opentemplate", "haswin_templateptr");
- haswin_templateend = haswin_templateptr+fsize-1;
- regs.r[1] = (int)filename;
- if (!haswin_swi(HASWIN_Open_template, ®s))
- return(HASWIN_FALSE);
- haswin_flags |= HASWIN_FLAGS_TEMPLATE;
- return(HASWIN_TRUE);
- }
-
- int haswin_pushtemplate(char *fname) {
-
- if (haswin_templatestackptr >= HASWIN_MAXTEMPLATES) {
- haswin_interrorprintf("haswin_pushtemplate: %s: too many templates on stack (max = %d)", fname, HASWIN_MAXTEMPLATES);
- return(HASWIN_FALSE);
- }
- /* close existing template */
- haswin_closetemplate();
- /* open the new template */
- if (haswin_opentemplate(fname)) {
- strncpy(haswin_templatestack[haswin_templatestackptr++], fname, 256);
- return(HASWIN_TRUE);
- }
- return(HASWIN_FALSE);
- }
-
- int haswin_poptemplate(void) {
-
- if (haswin_templatestackptr == 1) {
- haswin_closetemplate();
- haswin_templatestackptr = 0;
- return(HASWIN_TRUE);
- } else if (haswin_templatestackptr == 0) {
- return(HASWIN_FALSE);
- } else if (haswin_templatestackptr < 0) {
- haswin_interrorprintf("haswin_poptemplate: template stack empty");
- return(HASWIN_FALSE);
- }
- /* close existing template */
- haswin_closetemplate();
- /*
- * NB: we move the stack pointer even if the open fails
- * to stop infinite loops.
- */
- if (!haswin_opentemplate(haswin_templatestack[--haswin_templatestackptr]))
- return(HASWIN_FALSE);
- return(HASWIN_TRUE);
- }
-
- /*
- * get the window ident "title" from the template file.
- * "flags" are the USER part of the window flags.
- * Create all the icons required.
- */
- window *haswin_loadwindow(char *title, int flags) {
-
- _kernel_swi_regs regs;
-
- icon *ic;
- window *wptr = 0;
- int i, numicons, *thisicon, *blk, sarea;
- char fonts[256], realtitle[256];
-
- if (!(haswin_flags & HASWIN_FLAGS_TEMPLATE)) {
- haswin_internalerror("A Template file is not open");
- return((window *)0);
- }
- /* clear the fonts array for this window */
- for(i=0; i<256; fonts[i++]='\0')
- ;
- regs.r[1] = (int)haswin_templatewspace;
- regs.r[2] = (int)haswin_templateptr;
- regs.r[3] = (int)haswin_templateend;
- regs.r[4] = (int)fonts;
-
- strcpy(realtitle, title);
- regs.r[5] = (int)realtitle;
- regs.r[6] = 0;
- if (!haswin_swi(HASWIN_Load_template, ®s))
- regs.r[6] = 0;
- if (regs.r[6] == 0) {
- /* template window not found in file */
- haswin_interrorprintf("Window %s not found in Template file", title);
- return((window *)0);
- }
- for(i=0; i<256; i++)
- haswin_fontarray[i] += fonts[i];
-
- wptr=(window *)haswin_malloc(sizeof(window), "haswin_loadwindow", "wptr");
- wptr->win = haswin_malloc(88, "haswin_loadwindow", "window win block");
- ((int *)wptr->win)[ 0] = ((int *)haswin_templatewspace)[ 0];
- ((int *)wptr->win)[ 1] = ((int *)haswin_templatewspace)[ 1];
- ((int *)wptr->win)[ 2] = ((int *)haswin_templatewspace)[ 2];
- ((int *)wptr->win)[ 3] = ((int *)haswin_templatewspace)[ 3];
- ((int *)wptr->win)[ 4] = ((int *)haswin_templatewspace)[ 4];
- ((int *)wptr->win)[ 5] = ((int *)haswin_templatewspace)[ 5];
- ((int *)wptr->win)[ 6] = ((int *)haswin_templatewspace)[ 6];
- ((int *)wptr->win)[ 7] = ((((int *)haswin_templatewspace)[ 7]&WINDOW_WIMPFLAGS) | WINDOW_FLAGS_OR) & ~WINDOW_FLAGS_AND;
- wptr->flags = flags & WINDOW_HASFLAGS;
- wptr->win[32] = haswin_templatewspace[32];
- wptr->win[33] = haswin_templatewspace[33];
- wptr->win[34] = haswin_templatewspace[34];
- wptr->win[35] = haswin_templatewspace[35];
- wptr->win[36] = haswin_templatewspace[36];
- wptr->win[37] = haswin_templatewspace[37];
- wptr->win[38] = haswin_templatewspace[38];
- wptr->win[39] = 0;
- ((int *)wptr->win)[10] = ((int *)haswin_templatewspace)[10];
- ((int *)wptr->win)[11] = ((int *)haswin_templatewspace)[11];
- ((int *)wptr->win)[12] = ((int *)haswin_templatewspace)[12];
- ((int *)wptr->win)[13] = ((int *)haswin_templatewspace)[13];
- ((int *)wptr->win)[14] = ((int *)haswin_templatewspace)[14] | 0x00000100;
- ((int *)wptr->win)[15] = ((int *)haswin_templatewspace)[15];
- wptr->button = ((((int *)haswin_templatewspace)[15])>>12) & 0x0F;
- if (haswin_canbuttondrag(wptr->button))
- wptr->flags |= WINDOW_DRAGSEL|WINDOW_DRAGADJ;
- /* choose a sprite area control block. */
- /* if a specific area is not chosen, then choose the WIMP +1 area */
- switch (flags & SPRITE_AREA_MASK) {
- case SPRITE_SYSTEM_AREA:
- ((int *)wptr->win)[16] = 0;
- break;
- case SPRITE_WIMP_AREA:
- case SPRITE_RISCOS_AREA:
- ((int *)wptr->win)[16] = 1;
- break;
- case SPRITE_HASWIN_AREA:
- ((int *)wptr->win)[16] = (int)haswin_haswinsprites;
- break;
- case SPRITE_USER_AREA:
- ((int *)wptr->win)[16] = (int)haswin_usersprites;
- break;
- default:
- haswin_interrorprintf("haswin_loadwindow: unknown sprite area %8.8X", flags & SPRITE_AREA_MASK);
- /* FALL THRU */
- case 0:
- ((int *)wptr->win)[16] = +1;
- break;
- }
- ((int *)wptr->win)[17] = ((int *)haswin_templatewspace)[17];
- if ((((int *)haswin_templatewspace)[14]) & 0x00000100) {
- ((int *)wptr->win)[18] = (int)haswin_malloc(((int *)haswin_templatewspace)[20], "haswin_loadwindow:", "indirected window title");
- strncpy((char *)((int *)wptr->win)[18], (char *)(((int *)haswin_templatewspace)[18]), ((int *)haswin_templatewspace)[20]);
- if ((((int *)haswin_templatewspace)[19] > 0) && (((int *)haswin_templatewspace)[14] & 0x00000002)) {
- ((int *)wptr->win)[19] = (int)haswin_malloc(asciilen((char *)(((int *)haswin_templatewspace)[19])), "haswin_loadwindow", "window title validation");
- strcpy((char *)(((int *)wptr->win)[19]), (char *)(((int *)haswin_templatewspace)[19]));
- } else
- ((int *)wptr->win)[19] = ((int *)haswin_templatewspace)[19];
- ((int *)wptr->win)[20] = ((int *)haswin_templatewspace)[20];
- } else {
- ((int *)wptr->win)[18] = (int)haswin_malloc(13, "haswin_loadwindow:", "window title");
- strncpy((char *)((int *)wptr->win)[18], haswin_templatewspace + 72, 12);
- ((char *)((int *)wptr->win)[18])[12] = 0;
- ((int *)wptr->win)[19] = -1;
- ((int *)wptr->win)[20] = asciilen((char *)((int *)wptr->win)[18]);
- }
- numicons = ((int *)haswin_templatewspace)[21];
- ((int *)wptr->win)[21] = 0;
-
- regs.r[1] = (int)wptr->win;
- if ((!haswin_swi(HASWIN_Create_window, ®s)) || (regs.r[0] <= 0)) {
- haswin_free(wptr);
- return((window *)0);
- }
- wptr->handle = regs.r[0];
- /*
- * we have now created a window from the template. There are no icons
- * in this window, as yet.
- */
- wptr->name = haswin_malloc(asciilen(realtitle)+1, "haswin_loadwindow", "window name");
- strcpy(wptr->name, realtitle);
- wptr->numicons = 0;
- wptr->icons = (icon *)0;
- wptr->orgx = wptr->orgy = 0;
- wptr->mousebutton = (int (*)())0;
- wptr->drawroutine = (int (*)())0;
- wptr->dragroutine = (int (*)())0;
- wptr->openroutine = (int (*)())0;
- wptr->keyroutine = (int (*)())0;
- wptr->pointer = (pointer *)0;
- wptr->menu = (menu *)0;
- wptr->scrollx = wptr->scrolly = 0;
- wptr->pagex = wptr->pagey = 0;
- wptr->picture = (char *)0;
- wptr->text = (text *)0;
- wptr->pane = wptr->slide = (window *)0;
- wptr->caret = (caret *)0;
- wptr->help = (window *)0;
- wptr->helpmsg = (char *)0;
- wptr->master = wptr; /* window is its own master */
- wptr->next = haswin_topwindow;
- haswin_topwindow = wptr;
- /*
- * create the icons
- */
- for (i=0; i<numicons; i++) {
- thisicon = &(((int *)haswin_templatewspace)[22+i*8]);
- blk = (int *)haswin_malloc(36, "haswin_loadwindow", "icon SWI block");
- ic = (icon *)haswin_malloc(sizeof(struct icon), "haswin_loadwindow", "icon");
- blk[0] = ic->whandle = wptr->handle;
- blk[1] = thisicon[0];
- blk[2] = thisicon[1];
- blk[3] = thisicon[2];
- blk[4] = thisicon[3];
- /* our icons are ALWAYS indirected */
- blk[5] = thisicon[4] | 0x00000100;
- if (thisicon[4] & 0x00000100) {
- /* data is indirected */
- blk[6] = (int)haswin_malloc(thisicon[7]+1, "haswin_loadwindow", "indirected icon data");
- strncpy((char *)(blk[6]), (char *)(thisicon[5]), thisicon[7]);
- if ((thisicon[4] & 1) && (thisicon[6] > 0)) {
- /* validation string */
- blk[7] = (int)haswin_malloc(asciilen((char *)(thisicon[6]))+1, "haswin_loadwindow", "icon validation string");
- strcpy((char *)(blk[7]), (char *)(thisicon[6]));
- } else
- blk[7] = thisicon[6];
- } else {
- blk[6] = (int)haswin_malloc(13, "haswin_loadwindow", "icon data");
- strncpy((char *)(blk[6]), ((char *)thisicon)+20, 12);
- blk[7] = thisicon[6];
- }
- blk[8] = asciilen((char *)(blk[6]));
- if ((thisicon[4] & 0x03) == 0x02) {
- /* sprite only icon */
- sarea=haswin_findsprite((char *)(blk[6]));
- if (sarea == HASWIN_UNKNOWN) {
- haswin_errorprintf("haswin_loadwindow: cannot find sprite area for sprite only icon '%s' in window '%s'", (char *)(blk[6]), wptr->name);
- } else
- blk[7] = sarea;
- }
- ic->name = haswin_malloc(asciilen((char *)blk[6])+1, "haswin_createicon", "icon name buffer");
- strcpy(ic->name, (char *)blk[6]);
- ic->button = (blk[5]>>12) & 0x0F;
- ic->window = (window *)0;
- ic->menu = (menu *)0;
- ic->help = (window *)0;
- ic->helpmsg = (char *)0;
- if (haswin_canbuttondrag(ic->button))
- ic->flags = ICON_DRAGSEL|ICON_DRAGADJ;
- else
- ic->flags = 0;
- ic->mousebutton = 0;
- ic->dragroutine = 0;
- regs.r[1] = (int)blk;
- if (haswin_swi(HASWIN_Create_icon, ®s)) {
- ic->ihandle = regs.r[0];
- blk[0] = blk[1];
- blk[1] = blk[2];
- blk[2] = blk[3];
- blk[3] = blk[4];
- blk[4] = blk[5];
- blk[5] = blk[6];
- blk[6] = blk[7];
- blk[7] = blk[8];
- blk[8] = 0;
- ic->ic = blk;
- ic->next = wptr->icons;
- wptr->icons = ic;
- wptr->numicons++;
- } else {
- haswin_errorprintf("haswin_loadtemplate: failed to create icon '%s' in window '%s'", haswin_geticontitle(ic), haswin_getwindowtitle(wptr));
- haswin_free((char *)(blk[6]));
- haswin_free(blk);
- haswin_free(ic->name);
- haswin_free(ic);
- }
- }
- return(wptr);
- }
-
- window *haswin_loadpanewindow(window *top, char *title, int flags) {
-
- window *win;
-
- win = haswin_loadwindow(title, flags|WINDOW_TRESPASS);
- win->master = top;
- while (top->pane)
- top=top->pane;
- top->pane = win;
- return(win);
- }
-
- window *haswin_loadslidewindow(window *top, char *title, int flags) {
-
- window *win;
-
- win = haswin_loadwindow(title, flags|WINDOW_TRESPASS);
- win->master = top;
- while (top->slide)
- top=top->slide;
- top->slide = win;
- return(win);
- }
-
-