home *** CD-ROM | disk | FTP | other *** search
- /*
- THIS CODE WAS SOURCED (and modified to work in the RMA - erg!) FROM:
-
-
- #### # # # #
- # # # # # The FreeWare C library for
- # # ## ### # # # # ### RISC OS machines
- # # # # # # # # # # # ___________________________________
- # # #### ### ## # # # #
- # # # # # # # # # # Please refer to the accompanying
- #### ### #### # # ##### # ### documentation for conditions of use
- ________________________________________________________________________
-
- File: Template.Clone.c
- Author: Copyright © 1992 Jason Williams
- Thanks to John Winters for supplying the code that I hacked
- changed, hacked, rewrote, and then wrote again from scratch!
- Version: 1.10 (29 Mar 1992)
- Purpose: Loading, cacheing, and retrieval of window templates
- */
-
-
- #include "TempDefs.h"
- #include "DeskLib:SWI.h"
-
- /* Hack - Mr. Wimp very kindly assumes that any window in a menu structure
- * is owned by the menu's owner - but it ONLY assumes this when looking for
- * the window's indirected data (i.e. it works fine, except that it pages
- * in the wrong application to find the indirected data)
- *
- * The only solution for this is to allocate the indirected data needed in
- * the RMA while it is needed. This will only be done for MENU LEAF PopUps-
- * all others work just fine.
- *
- * To save fragmeting the RMA, I grab 1kB of RMA space at startup, and
- * hope that it is enough! If your PopUp uses more than this, then
- * shame on it! (and bump that number up...)
- * This also saves each individual PopUp handler having to make sure that
- * it releases any RMA space it is using...
- */
-
- #define MEMCLAIM 1024
- static void *memclaim = NULL;
- static void *windowmem = NULL;
-
-
- extern window_block *Template_RMAFind(char *name)
- /* The same as Template_Find, but clones the indirected data into the RMA,
- * and the rest of the window creation block into a static memory area,
- * because of our wee probbie wih the (W)IMPs.
- * This is 'safe' because only one menu-leaf window should ever exist at
- * any moment in time, and YOU WON'T EVER call this function for NON LEAF
- * PopUps, WILL YOU?!!!
- */
- {
- template_record *t;
- window_block *window;
- int icon;
- icon_block *icons;
- char *i;
-
- t = Template__FindTemplate(name);
- if (t == NULL)
- return(NULL);
-
- if (windowmem != NULL) free(windowmem);
- windowmem = malloc(sizeof(window_block) +
- (sizeof(icon_block) * t->windowdef->numicons));
- if (windowmem == NULL) return(NULL);
-
- window = windowmem;
- memcpy(window, t->windowdef, sizeof(window_block) +
- (sizeof(icon_block) * t->windowdef->numicons));
- i = memclaim;
- if (i == NULL || MEMCLAIM < t->indirectsize)
- Error_ReportFatalInternal(1, "Unable to claim enough RMA memory to run!");
-
- if (window->titleflags.data.indirected)
- {
- window->title.indirecttext.buffer = i;
- strncpy(i, t->windowdef->title.indirecttext.buffer,
- window->title.indirecttext.bufflen);
- i += window->title.indirecttext.bufflen;
- }
-
- icons = (icon_block *) ((int)window + sizeof(window_block));
- for (icon = 0; icon < t->windowdef->numicons; icon++)
- {
- if (icons[icon].flags.data.indirected && icons[icon].flags.data.text)
- {
- char *oldtext = icons[icon].data.indirecttext.buffer;
-
- icons[icon].data.indirecttext.buffer = i;
- strncpy(i, oldtext, icons[icon].data.indirecttext.bufflen);
- i += icons[icon].data.indirecttext.bufflen;
-
- oldtext = icons[icon].data.indirecttext.validstring;
- if ((int)oldtext > 0)
- {
- int size;
- size = strlencr(oldtext) + 1;
-
- icons[icon].data.indirecttext.validstring = i;
- strncpy(i, oldtext, size);
- i += size;
- }
- }
- }
-
- return(window);
- }
-
-
- extern void Template_RMAInit(void)
- /* OS Module 6 - claim memory */
- {
- if (memclaim == NULL)
- SWI(4,3, 0x2001e, 6, 0, 0, MEMCLAIM, /*TO*/ NULL, NULL, (int *) &memclaim);
- }
-
-
- extern void Template_RMAFree(void)
- {
- /* OS_Module 7 - release memory */
- SWI(4,0, 0x2001e, 7, 0, (int)memclaim, 0);
- }
-