/* * template.c * * Loading and manipulation of window templates * * © 1994-1998 Straylight */ /*----- Licensing note ----------------------------------------------------* * * This file is part of Straylight's Steel library. * * Steel is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2, or (at your option) * any later version. * * Steel is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Steel. If not, write to the Free Software Foundation, * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include "os.h" #include "wimp.h" #include "utils.h" #include "template.h" #include "dll.h" #include "mem.h" #include "font.h" #include "msgs.h" #include "werr.h" #include "res.h" #include "resspr.h" /*----- Note to the reader ------------------------------------------------* * * This code makes no use of Wimp_LoadTemplate at all. It's modified from * the Glass template loading code, which as we all know is no slouch. */ /*----- Variables ---------------------------------------------------------*/ static template *template__all; static char template__fonts[256]; static BOOL template__usingFonts; /*----- Main code ---------------------------------------------------------*/ #ifndef _dll_NODLL extern void _dllEntry(template__exit)(void); #endif /* * int template__cmp(char *a,char *b) * * Use * Compares two strings of length 12 or less. */ static int template__cmp(char *a,char *b) { int i; for (i=0;i<12;i++) { if (*a<32 && *b<32) return (0); if (*a!=*b) return (*a-*b); a++; b++; } return (0); } /* * template *template_copy(template *from) * * Use * Copies a template field-for-field and fixes up new indirected data for * it. */ template *template_copy(template *from) { template *t; wimp_icon *ic; int i; int size=sizeof(template)+from->window.nicons*sizeof(wimp_icon); /* --- Allocate a new template block and copy --- */ t=mem_alloc(size); if (!t) return (0); memcpy(t,from,size); /* --- Allocate a new indirected space block and copy that too --- */ if (!from->workspace) return(t); t->workspace=mem_alloc(t->workspacesize); if (!t->workspace) { mem_free(t); return (0); } memcpy(t->workspace,from->workspace,t->workspacesize); /* --- Fix up all the indirected pointers --- */ if (t->window.titleflags & wimp_INDIRECT) { t->window.title.indirecttext.buffer+=t->workspace-from->workspace; if (t->window.titleflags & wimp_ITEXT && t->window.title.indirecttext.validstring!=(char *)-1) t->window.title.indirecttext.validstring+=t->workspace-from->workspace; } /* --- And for the icons --- */ ic=(wimp_icon *)(t+1); for (i=0;iwindow.nicons;i++) { if (ic->flags & wimp_INDIRECT) { ic->data.indirecttext.buffer+=t->workspace-from->workspace; if (ic->flags & wimp_ITEXT && ic->data.indirecttext.validstring!=(char *)-1) ic->data.indirecttext.validstring+=t->workspace-from->workspace; } ic++; } return (t); } /* * void template__exit(void) * * Use * Closes down all the fonts loaded with templates */ _dll_static void template__exit(void) { int i,j; for (i=0;i<256;i++) { for (j=0;jwindow, _ptr(void,filebuf,objoff), sizeof(wimp_wind)+numicons*sizeof(wimp_icon)); /* --- Set up the window's sprite area --- */ w->window.spritearea=resspr_area(); /* --- Now perform the two passes on the window data --- */ datasize=0; for (pass=1;pass<=2;pass++) { icf=w->window.titleflags; /* --- Handle title indirected data --- */ if (icf & wimp_INDIRECT) { if (pass==1) { /* --- Pass 1 just counts the size we need --- */ q=(int)w->window.title.indirecttext.buffer+filebuf+objoff; utils_ctermToNterm(q); datasize+=w->window.title.indirecttext.bufflen; if (icf & wimp_ITEXT && w->window.title.indirecttext.validstring!=(char *)-1) { q=(int)w->window.title.indirecttext.validstring+ filebuf+ objoff; utils_ctermToNterm(q); datasize+=strlen(q)+1; } } else { q=(int)w->window.title.indirecttext.buffer+filebuf+objoff; memcpy(p,q,w->window.title.indirecttext.bufflen); w->window.title.indirecttext.buffer=p; p+=w->window.title.indirecttext.bufflen; if (icf & wimp_ITEXT && w->window.title.indirecttext.validstring!=(char *)-1) { q=(int)w->window.title.indirecttext.validstring+ filebuf+ objoff; strcpy(p,q); w->window.title.indirecttext.validstring=p; p+=strlen(q)+1; } } } /* --- Cope with antialiasing on the second pass --- */ if ((icf & wimp_IFONT) && pass==2) { /* --- Get the font handle --- */ fhandle=(icf & 0xff000000) >> 24; /* --- Find information about the font --- */ fptr=fontData+(fhandle-1)*48; memcpy(&fwidth,intptr(filebuf,fptr)+0,sizeof(int)); memcpy(&fheight,intptr(filebuf,fptr)+1,sizeof(int)); utils_ctermToNterm(charptr(filebuf,fptr+8)); /* --- Get a real font like this --- */ if (font_find(charptr(filebuf,fptr+8), fwidth, fheight, 0,0, &fhandle)) { if (!fquiet) { werr(FALSE,msgs_lookup("templateCFF:Some fonts used in this " "template file could not be found. " "Icons with such fonts will not be " "anti-aliased.")); fquiet=TRUE; } icf&=~wimp_IFONT; } else { icf=(icf & 0x00ffffff) | (fhandle << 24); template__fonts[fhandle]++; if (!template__usingFonts) { atexit(_dllEntry(template__exit)); template__usingFonts=TRUE; } } w->window.titleflags=icf; } /* --- Now handle each icon in turn like this --- */ iconptr=(wimp_icon *)(w+1); for (i=0;iflags; /* --- Handle title indirected data --- */ if (icf & wimp_INDIRECT) { if (pass==1) { /* --- Pass 1 just counts the size we need --- */ q=(int)iconptr->data.indirecttext.buffer+filebuf+objoff; utils_ctermToNterm(q); datasize+=iconptr->data.indirecttext.bufflen; if (icf & wimp_ITEXT && iconptr->data.indirecttext.validstring!=(char *)-1) { q=(int)iconptr->data.indirecttext.validstring+ filebuf+ objoff; utils_ctermToNterm(q); datasize+=strlen(q)+1; } } else { q=(int)iconptr->data.indirecttext.buffer+filebuf+objoff; memcpy(p,q,iconptr->data.indirecttext.bufflen); iconptr->data.indirecttext.buffer=p; p+=iconptr->data.indirecttext.bufflen; if (icf & wimp_ITEXT && iconptr->data.indirecttext.validstring!=(char *)-1) { q=(int)iconptr->data.indirecttext.validstring+ filebuf+ objoff; strcpy(p,q); iconptr->data.indirecttext.validstring=p; p+=strlen(q)+1; } } } /* --- Cope with antialiasing on the second pass --- */ if ((icf & wimp_IFONT) && pass==2) { /* --- Get the font handle --- */ fhandle=(icf & 0xff000000) >> 24; /* --- Find information about the font --- */ fptr=fontData+(fhandle-1)*48; memcpy(&fwidth,intptr(filebuf,fptr)+0,sizeof(int)); memcpy(&fheight,intptr(filebuf,fptr)+1,sizeof(int)); utils_ctermToNterm(charptr(filebuf,fptr+8)); /* --- Get a real font like this --- */ if (font_find(charptr(filebuf,fptr+8), fwidth, fheight, 0,0, &fhandle)) { if (!fquiet) { werr(FALSE,msgs_lookup("templateCFF:Some fonts used in " "this template file could not be " "found. Icons with such fonts " "will not be anti-aliased.")); fquiet=TRUE; } icf&=~wimp_IFONT; } else { icf=(icf & 0x00ffffff) | (fhandle << 24); template__fonts[fhandle]++; if (!template__usingFonts) { atexit(_dllEntry(template__exit)); template__usingFonts=TRUE; } } iconptr->flags=icf; } iconptr++; } /* --- Finish off the pass properly now --- */ if (pass==1) { /* --- Allocate the right amount of indirected space --- */ w->workspacesize=datasize; if (datasize) { w->workspace=mem_alloc(datasize); if (!w->workspace) { werr(FALSE,msgs_lookup("templateNEM:Not enough memory " "to load template file.")); exit(1); } } else w->workspace=0; p=w->workspace; } else { /* --- Link the filled in structure into the list --- */ w->next=template__all; template__all=w; /* --- Fill in the name of the template --- */ memcpy(w->name,name,12); } } break; } } /* --- Finished -- return to the user --- */ mem_free(filebuf); return (TRUE); /* It can't do any harm, now, can it? */ } /* * void template_init(void) * * Use * Loads the application's `Templates' file. */ void template_init(void) { template_readfile(res_name("Templates")); } /* * void template_use_fancyfonts(void) * * Use * Does absolutely nothing at all. Fancy font support happens anyway. */ void template_use_fancyfonts(void) { /* Do absolutely nothing at all :-) */ } /* * BOOL template_exists(char *name) * * Use * Returns TRUE if the named template is known at the moment, or FALSE * otherwise */ BOOL template_exists(char *name) { template *t=template__all; while (t) { if (!template__cmp(name,t->name)) return (TRUE); t=t->next; } return (FALSE); } /* * template *template_find(char *name) * * Use * Locates a named template and returns a pointer to it */ template *template_find(char *name) { template *t=template__all; while (t) { if (!template__cmp(name,t->name)) return (t); t=t->next; } werr(TRUE,msgs_lookup("templateNF:Template '%s' not found"),name); return (0); /* FWIW */ } /* * wimp_wind *template_syshandle(char *name) * * Use * Not very much, if the truth be known. It returns a pointer to a named * window definition. */ wimp_wind *template_syshandle(char *name) { return (&template_find(name)->window); } /* * BOOL template_loaded(void) * * Use * Returns TRUE if we have templates on board. */ BOOL template_loaded(void) { return (template__all ? TRUE : FALSE); }