4 * Loading and manipulation of window templates
6 * © 1994-1998 Straylight
9 /*----- Licensing note ----------------------------------------------------*
11 * This file is part of Straylight's Steel library.
13 * Steel is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * Steel is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with Steel. If not, write to the Free Software Foundation,
25 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
44 /*----- Note to the reader ------------------------------------------------*
46 * This code makes no use of Wimp_LoadTemplate at all. It's modified from
47 * the Glass template loading code, which as we all know is no slouch.
50 /*----- Variables ---------------------------------------------------------*/
52 static template *template__all;
53 static char template__fonts[256];
54 static BOOL template__usingFonts;
56 /*----- Main code ---------------------------------------------------------*/
59 extern void _dllEntry(template__exit)(void);
63 * int template__cmp(char *a,char *b)
66 * Compares two strings of length 12 or less.
69 static int template__cmp(char *a,char *b)
85 * template *template_copy(template *from)
88 * Copies a template field-for-field and fixes up new indirected data for
92 template *template_copy(template *from)
97 int size=sizeof(template)+from->window.nicons*sizeof(wimp_icon);
99 /* --- Allocate a new template block and copy --- */
106 /* --- Allocate a new indirected space block and copy that too --- */
108 if (!from->workspace)
111 t->workspace=mem_alloc(t->workspacesize);
117 memcpy(t->workspace,from->workspace,t->workspacesize);
119 /* --- Fix up all the indirected pointers --- */
121 if (t->window.titleflags & wimp_INDIRECT)
123 t->window.title.indirecttext.buffer+=t->workspace-from->workspace;
124 if (t->window.titleflags & wimp_ITEXT &&
125 t->window.title.indirecttext.validstring!=(char *)-1)
126 t->window.title.indirecttext.validstring+=t->workspace-from->workspace;
129 /* --- And for the icons --- */
131 ic=(wimp_icon *)(t+1);
132 for (i=0;i<t->window.nicons;i++)
134 if (ic->flags & wimp_INDIRECT)
136 ic->data.indirecttext.buffer+=t->workspace-from->workspace;
137 if (ic->flags & wimp_ITEXT &&
138 ic->data.indirecttext.validstring!=(char *)-1)
139 ic->data.indirecttext.validstring+=t->workspace-from->workspace;
148 * void template__exit(void)
151 * Closes down all the fonts loaded with templates
154 _dll_static void template__exit(void)
159 for (j=0;j<template__fonts[i];j++)
165 * BOOL template_readfile(char *name)
168 * Loads the template file named into memory, and sorts out all its
171 * Template entries with strange identifiers are ignored. Other entry
172 * types may be supported later.
175 * char *name == the name of the template file to load (a resource file)
178 * FALSE if the file contained no sprite icons. No, I don't understand
179 * the use of this either. It's not my problem though. I just write the
183 /* --- Macros from Glass's gStruct.h to help read the structure --- */
185 #define intptr(flex,offset) \
186 ((int *)(((char *)(flex))+(offset)))
187 #define charptr(flex,offset) \
188 (((char *)(flex))+(offset))
189 #define _ptr(type,flex,offset) \
190 ((type *)(((char *)(flex))+(offset)))
192 BOOL template_readfile(char *file)
194 /* --- Variables for loading the file --- */
199 /* --- Variables for mangling the file in memory --- */
201 int fontData; /* Offset of font ata from start of file */
202 template *w; /* For each window we come across */
203 int datasize; /* Size of indirected data for the window */
204 char *p=0; /* Offset in indirected data for next item*/
205 char *q; /* Pointer to an indirected string */
206 int index; /* Current index entry we're looking at */
207 char name[13]; /* For the name of the template */
208 int objoff; /* Offset of window we're looking at */
209 wimp_icon *iconptr; /* Offset of icon we're looking at */
210 wimp_iconflags icf; /* Icon flags for something or other */
211 int objtype; /* Type of current template entry */
212 int numicons; /* Number of icons in a window */
213 int i; /* A loop counter */
214 int pass; /* We take two passes at each window */
216 /* --- Font handling --- */
218 int fheight; /* Height of a font required */
219 int fwidth; /* Width of a font required */
220 int fhandle; /* Font handle */
221 int fptr; /* Offset of font name */
222 BOOL fquiet=FALSE; /* Shut up about font failures. */
224 /* --- Load the file into memory --- */
228 if (utils_complain(os_file(&f),
229 msgs_lookup("templateELF:Error loading template file: '%s'")))
234 filebuf=mem_alloc(f.start);
237 werr(FALSE,msgs_lookup("templateNEM:Not enough memory "
238 "to load template file."));
243 f.loadaddr=(int)filebuf;
245 if (utils_complain(os_file(&f),
246 msgs_lookup("templateELF:Error loading template file: '%s'")))
251 /* --- Now start parsing up the file structure --- */
253 fontData=*intptr(filebuf,0);
255 /* --- Loop through each index entry in turn --- */
257 for (index=16;*intptr(filebuf,index);index+=24)
259 /* --- Find this entry's location in the file --- */
261 objoff=*intptr(filebuf,index);
262 objtype=*intptr(filebuf,index+8);
264 /* --- Mangle it according to its type --- */
270 /* --- Find the name of the window and its number of icons --- */
272 memcpy(name,charptr(filebuf,index+12),12);
274 utils_ctermToNterm(name);
275 memcpy(&numicons,intptr(filebuf,objoff+84),sizeof(int));
277 /* --- Allocate a template block for it --- */
279 datasize=sizeof(template)+numicons*sizeof(wimp_icon);
280 w=mem_alloc(datasize);
283 werr(FALSE,msgs_lookup("templateNEM:Not enough memory "
284 "to load template file."));
288 /* --- Copy the template data into this block --- */
291 _ptr(void,filebuf,objoff),
292 sizeof(wimp_wind)+numicons*sizeof(wimp_icon));
294 /* --- Set up the window's sprite area --- */
296 w->window.spritearea=resspr_area();
298 /* --- Now perform the two passes on the window data --- */
301 for (pass=1;pass<=2;pass++)
303 icf=w->window.titleflags;
305 /* --- Handle title indirected data --- */
307 if (icf & wimp_INDIRECT)
311 /* --- Pass 1 just counts the size we need --- */
313 q=(int)w->window.title.indirecttext.buffer+filebuf+objoff;
314 utils_ctermToNterm(q);
315 datasize+=w->window.title.indirecttext.bufflen;
316 if (icf & wimp_ITEXT &&
317 w->window.title.indirecttext.validstring!=(char *)-1)
319 q=(int)w->window.title.indirecttext.validstring+
322 utils_ctermToNterm(q);
323 datasize+=strlen(q)+1;
328 q=(int)w->window.title.indirecttext.buffer+filebuf+objoff;
329 memcpy(p,q,w->window.title.indirecttext.bufflen);
330 w->window.title.indirecttext.buffer=p;
331 p+=w->window.title.indirecttext.bufflen;
332 if (icf & wimp_ITEXT &&
333 w->window.title.indirecttext.validstring!=(char *)-1)
335 q=(int)w->window.title.indirecttext.validstring+
339 w->window.title.indirecttext.validstring=p;
345 /* --- Cope with antialiasing on the second pass --- */
347 if ((icf & wimp_IFONT) && pass==2)
349 /* --- Get the font handle --- */
351 fhandle=(icf & 0xff000000) >> 24;
353 /* --- Find information about the font --- */
355 fptr=fontData+(fhandle-1)*48;
356 memcpy(&fwidth,intptr(filebuf,fptr)+0,sizeof(int));
357 memcpy(&fheight,intptr(filebuf,fptr)+1,sizeof(int));
358 utils_ctermToNterm(charptr(filebuf,fptr+8));
360 /* --- Get a real font like this --- */
362 if (font_find(charptr(filebuf,fptr+8),
370 werr(FALSE,msgs_lookup("templateCFF:Some fonts used in this "
371 "template file could not be found. "
372 "Icons with such fonts will not be "
380 icf=(icf & 0x00ffffff) | (fhandle << 24);
381 template__fonts[fhandle]++;
382 if (!template__usingFonts)
384 atexit(_dllEntry(template__exit));
385 template__usingFonts=TRUE;
388 w->window.titleflags=icf;
391 /* --- Now handle each icon in turn like this --- */
393 iconptr=(wimp_icon *)(w+1);
394 for (i=0;i<numicons;i++)
398 /* --- Handle title indirected data --- */
400 if (icf & wimp_INDIRECT)
404 /* --- Pass 1 just counts the size we need --- */
406 q=(int)iconptr->data.indirecttext.buffer+filebuf+objoff;
407 utils_ctermToNterm(q);
408 datasize+=iconptr->data.indirecttext.bufflen;
409 if (icf & wimp_ITEXT &&
410 iconptr->data.indirecttext.validstring!=(char *)-1)
412 q=(int)iconptr->data.indirecttext.validstring+
415 utils_ctermToNterm(q);
416 datasize+=strlen(q)+1;
421 q=(int)iconptr->data.indirecttext.buffer+filebuf+objoff;
422 memcpy(p,q,iconptr->data.indirecttext.bufflen);
423 iconptr->data.indirecttext.buffer=p;
424 p+=iconptr->data.indirecttext.bufflen;
425 if (icf & wimp_ITEXT &&
426 iconptr->data.indirecttext.validstring!=(char *)-1)
428 q=(int)iconptr->data.indirecttext.validstring+
432 iconptr->data.indirecttext.validstring=p;
438 /* --- Cope with antialiasing on the second pass --- */
440 if ((icf & wimp_IFONT) && pass==2)
442 /* --- Get the font handle --- */
444 fhandle=(icf & 0xff000000) >> 24;
446 /* --- Find information about the font --- */
448 fptr=fontData+(fhandle-1)*48;
449 memcpy(&fwidth,intptr(filebuf,fptr)+0,sizeof(int));
450 memcpy(&fheight,intptr(filebuf,fptr)+1,sizeof(int));
451 utils_ctermToNterm(charptr(filebuf,fptr+8));
453 /* --- Get a real font like this --- */
455 if (font_find(charptr(filebuf,fptr+8),
463 werr(FALSE,msgs_lookup("templateCFF:Some fonts used in "
464 "this template file could not be "
465 "found. Icons with such fonts "
466 "will not be anti-aliased."));
473 icf=(icf & 0x00ffffff) | (fhandle << 24);
474 template__fonts[fhandle]++;
475 if (!template__usingFonts)
477 atexit(_dllEntry(template__exit));
478 template__usingFonts=TRUE;
486 /* --- Finish off the pass properly now --- */
490 /* --- Allocate the right amount of indirected space --- */
492 w->workspacesize=datasize;
495 w->workspace=mem_alloc(datasize);
498 werr(FALSE,msgs_lookup("templateNEM:Not enough memory "
499 "to load template file."));
509 /* --- Link the filled in structure into the list --- */
511 w->next=template__all;
514 /* --- Fill in the name of the template --- */
516 memcpy(w->name,name,12);
523 /* --- Finished -- return to the user --- */
526 return (TRUE); /* It can't do any harm, now, can it? */
530 * void template_init(void)
533 * Loads the application's `Templates' file.
536 void template_init(void)
538 template_readfile(res_name("Templates"));
542 * void template_use_fancyfonts(void)
545 * Does absolutely nothing at all. Fancy font support happens anyway.
548 void template_use_fancyfonts(void)
550 /* Do absolutely nothing at all :-) */
554 * BOOL template_exists(char *name)
557 * Returns TRUE if the named template is known at the moment, or FALSE
561 BOOL template_exists(char *name)
563 template *t=template__all;
566 if (!template__cmp(name,t->name))
574 * template *template_find(char *name)
577 * Locates a named template and returns a pointer to it
580 template *template_find(char *name)
582 template *t=template__all;
585 if (!template__cmp(name,t->name))
589 werr(TRUE,msgs_lookup("templateNF:Template '%s' not found"),name);
590 return (0); /* FWIW */
594 * wimp_wind *template_syshandle(char *name)
597 * Not very much, if the truth be known. It returns a pointer to a named
601 wimp_wind *template_syshandle(char *name)
603 return (&template_find(name)->window);
607 * BOOL template_loaded(void)
610 * Returns TRUE if we have templates on board.
613 BOOL template_loaded(void)
615 return (template__all ? TRUE : FALSE);