home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / p / popups / Docs / ExampleSrc / c / Template < prev   
Encoding:
Text File  |  1993-05-25  |  4.2 KB  |  127 lines

  1. /*
  2.     THIS CODE WAS SOURCED (and modified to work in the RMA - erg!) FROM:
  3.  
  4.  
  5.     ####             #    #     # #
  6.     #   #            #    #       #          The FreeWare C library for 
  7.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  8.     #   # #  # #     # #  #     # #  #   ___________________________________
  9.     #   # ####  ###  ##   #     # #  #                                      
  10.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  11.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  12.     ________________________________________________________________________
  13.  
  14.     File:    Template.Clone.c
  15.     Author:  Copyright © 1992 Jason Williams
  16.              Thanks to John Winters for supplying the code that I hacked
  17.              changed, hacked, rewrote, and then wrote again from scratch!
  18.     Version: 1.10 (29 Mar 1992)
  19.     Purpose: Loading, cacheing, and retrieval of window templates
  20. */
  21.  
  22.  
  23. #include "TempDefs.h"
  24. #include "DeskLib:SWI.h"
  25.  
  26. /*  Hack - Mr. Wimp very kindly assumes that any window in a menu structure
  27.  *  is owned by the menu's owner - but it ONLY assumes this when looking for
  28.  *  the window's indirected data (i.e. it works fine, except that it pages
  29.  *  in the wrong application to find the indirected data)
  30.  *
  31.  *  The only solution for this is to allocate the indirected data needed in
  32.  *  the RMA while it is needed. This will only be done for MENU LEAF PopUps-
  33.  *  all others work just fine.
  34.  *
  35.  *  To save fragmeting the RMA, I grab 1kB of RMA space at startup, and
  36.  *  hope that it is enough! If your PopUp uses more than this, then
  37.  *  shame on it! (and bump that number up...)
  38.  *  This also saves each individual PopUp handler having to make sure that
  39.  *  it releases any RMA space it is using...
  40.  */
  41.  
  42. #define MEMCLAIM 1024
  43. static void *memclaim  = NULL;
  44. static void *windowmem = NULL;
  45.  
  46.  
  47. extern window_block *Template_RMAFind(char *name)
  48. /*  The same as Template_Find, but clones the indirected data into the RMA,
  49.  *  and the rest of the window creation block into a static memory area,
  50.  *  because of our wee probbie wih the (W)IMPs.
  51.  *  This is 'safe' because only one menu-leaf window should ever exist at
  52.  *  any moment in time, and YOU WON'T EVER call this function for NON LEAF
  53.  *  PopUps, WILL YOU?!!!
  54.  */
  55. {
  56.   template_record *t;
  57.   window_block    *window;
  58.   int             icon;
  59.   icon_block      *icons;
  60.   char            *i;
  61.  
  62.   t = Template__FindTemplate(name);
  63.   if (t == NULL)
  64.     return(NULL);
  65.  
  66.   if (windowmem != NULL) free(windowmem);
  67.   windowmem = malloc(sizeof(window_block) +
  68.                      (sizeof(icon_block) * t->windowdef->numicons));
  69.   if (windowmem == NULL) return(NULL);
  70.  
  71.   window = windowmem;
  72.   memcpy(window, t->windowdef, sizeof(window_block) +
  73.                                 (sizeof(icon_block) * t->windowdef->numicons));
  74.   i = memclaim;
  75.   if (i == NULL || MEMCLAIM < t->indirectsize)
  76.     Error_ReportFatalInternal(1, "Unable to claim enough RMA memory to run!");
  77.  
  78.   if (window->titleflags.data.indirected)
  79.   {
  80.     window->title.indirecttext.buffer  = i;
  81.     strncpy(i, t->windowdef->title.indirecttext.buffer,
  82.             window->title.indirecttext.bufflen);
  83.     i += window->title.indirecttext.bufflen;
  84.   }
  85.  
  86.   icons = (icon_block *) ((int)window + sizeof(window_block));
  87.   for (icon = 0; icon < t->windowdef->numicons; icon++)
  88.   {
  89.     if (icons[icon].flags.data.indirected && icons[icon].flags.data.text)
  90.     {
  91.       char *oldtext = icons[icon].data.indirecttext.buffer;
  92.  
  93.       icons[icon].data.indirecttext.buffer = i;
  94.       strncpy(i, oldtext, icons[icon].data.indirecttext.bufflen);
  95.       i += icons[icon].data.indirecttext.bufflen;
  96.  
  97.       oldtext = icons[icon].data.indirecttext.validstring;
  98.       if ((int)oldtext > 0)
  99.       {
  100.         int size;
  101.         size = strlencr(oldtext) + 1;
  102.  
  103.         icons[icon].data.indirecttext.validstring = i;
  104.         strncpy(i, oldtext, size);
  105.         i += size;
  106.       }
  107.     }
  108.   }
  109.  
  110.   return(window);
  111. }
  112.  
  113.  
  114. extern void Template_RMAInit(void)
  115.   /* OS Module 6 - claim memory */
  116. {
  117.   if (memclaim == NULL)
  118.     SWI(4,3, 0x2001e, 6, 0, 0, MEMCLAIM, /*TO*/ NULL, NULL, (int *) &memclaim);
  119. }
  120.  
  121.  
  122. extern void Template_RMAFree(void)
  123. {
  124.   /* OS_Module 7 - release memory */
  125.   SWI(4,0, 0x2001e, 7, 0, (int)memclaim, 0);
  126. }
  127.