home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / desklib / Libraries / Window / c / ModeChange < prev    next >
Encoding:
Text File  |  1993-06-29  |  5.2 KB  |  152 lines

  1. /*
  2.     ####             #    #     # #
  3.     #   #            #    #       #          The FreeWare C library for 
  4.     #   #  ##   ###  #  # #     # ###             RISC OS machines
  5.     #   # #  # #     # #  #     # #  #   ___________________________________
  6.     #   # ####  ###  ##   #     # #  #                                      
  7.     #   # #        # # #  #     # #  #    Please refer to the accompanying
  8.     ####   ### ####  #  # ##### # ###    documentation for conditions of use
  9.     ________________________________________________________________________
  10.  
  11.     File:    Window.ModeChange.c
  12.     Author:  Copyright © 1993 Tim Browse and Jason Williams
  13.              (Original code by Tim, massive changes and rewriting by Jason ;-)
  14.     Version: 1.02 (28 Jun 1993)
  15.     Purpose: High-level window management functions: 
  16.              Fix windows to use new font handles after a mode change.
  17.              (This even works with open windows, so long as they were created
  18.              with Window_ calls)
  19. */
  20.  
  21.  
  22. #include "Desklib:Error.h"
  23. #include "Desklib:Event.h"
  24. #include "Desklib:Font.h"
  25. #include "Desklib:LinkList.h"
  26. #include "Desklib:Screen.h"
  27. #include "Desklib:Template.h"
  28. #include "Desklib:Window.h"
  29. #include "Desklib:WimpSWIs.h"
  30.  
  31. #include "WindowDefs.h"
  32. #include "TempDefs.h"  /*  These have been duplicated from the file
  33.                         *  Template.h.TempDefs into Window.h.TempDefs
  34.                         */
  35. #include <stdlib.h>
  36. #include <string.h>
  37.  
  38.  
  39. extern linklist_header window_listanchor;
  40.  
  41.  
  42. /* This constant is used to mask out all icon flag fields except the font handle */
  43. #define FONT_MASK ((unsigned) 0xFF000000)
  44.  
  45.  
  46. static void MapFontHandles(window_block  *wblock,
  47.                            window_handle window,
  48.                            font_array    *fontarray)
  49. /*  This function goes through a window block, and changes each icon's font
  50.  *  handle (if any) by looking up its current handle in the given fontarray,
  51.  *  and using that entry in the fontarray as the new font handle to use
  52.  */
  53. {
  54.   icon_block    *icons;
  55.   int           i;
  56.  
  57.   icons = (icon_block *) (wblock + 1);    /* pointer arithmetic! */
  58.   for (i = 0; i < wblock->numicons; i++)
  59.   {
  60.     if (icons[i].flags.data.font)
  61.     {
  62.       if (window != 0)    /* The window is open - change all on-screen icons */
  63.       {
  64.         icon_flags flags;
  65.         flags.font.handle = fontarray->fonts[icons[i].flags.font.handle];
  66.         Wimp_SetIconState(window, i, flags.value & FONT_MASK, FONT_MASK);
  67.       }
  68.  
  69.       /* Change icon font handle in the window defn block */
  70.       icons[i].flags.font.handle =
  71.                           fontarray->fonts[icons[i].flags.font.handle];
  72.     }
  73.   }
  74. }
  75.  
  76.  
  77.  
  78. extern void Window_ModeChange(void)
  79. {
  80.   windowrec       *record;
  81.   template_record *tptr;
  82.   font_defn       defn;
  83.   font_array      fontconvert;
  84.   int             i;
  85.  
  86.   if ((int)template_fontarray < 0)
  87.     return;                       /* You aren't using outline fonts, stupid! */
  88.  
  89.   /*  Go through the template's font usage array...
  90.    *  For each font we are using, re-find it at the same point size to get
  91.    *  the font system to cache the font for the current screen mode.
  92.    *  We fill in a conversion table which maps each old font handle to each
  93.    *  new one (fontconvert.fonts[old_handle] = new_handle)
  94.    */
  95.  
  96.   for (i = 0; i < 256; i++)
  97.   {
  98.     fontconvert.fonts[i] = 0;
  99.     if (template_fontarray->fonts[i] > 0)       /* If we are using this font */
  100.     {
  101.       font_handle h = 0;
  102.  
  103.       Font_ReadDefn(i, &defn);             /* Find out its name and size ... */
  104.                                            /* ...and get a new handle for it */
  105.       Font_FindFont(&h, defn.name, defn.xsize, defn.ysize, 0, 0);
  106.       fontconvert.fonts[i] = h;  /* (BTW, it's OK if the handle is the same) */
  107.     }
  108.   }
  109.  
  110.   /*  Replace all of the fonts we are using with the new handles
  111.    *  Firstly, go through all of the Window_Create'd windows (open windows)
  112.    *  and fix them... (Note that if any windows use fonts but were not
  113.    *  Window_Create'd then
  114.    *    a) they won't be fixed, and
  115.    *    b) the font usage is probably now 0, so if the cache fills up,
  116.    *       the font may be lost/replaced, and NASTY things will happen!
  117.    */
  118.  
  119.   record = LinkList_FirstItem(&window_listanchor);
  120.   while (record != NULL)
  121.   {
  122.     MapFontHandles((window_block *) record->memory,
  123.                    record->window, &fontconvert);
  124.  
  125.     record = LinkList_NextItem(&record->header);
  126.   }
  127.  
  128.   /*  Now do the template definitions themselves (otherwise the next time
  129.    *  you open a window all the font handles will be wrong and won't you
  130.    *  look silly?)
  131.    */
  132.   tptr = (template_record *) template_list.next;
  133.   while (tptr != NULL)
  134.   {
  135.     MapFontHandles(tptr->windowdef, (window_handle) 0, &fontconvert);
  136.     tptr = (template_record *) tptr->header.next;
  137.   }
  138.  
  139.   /*  Lose all the old fonts and install new font list into Template module
  140.    *  so that we know which fonts to lose next time, or when we quit.
  141.    *  We Font_FindFont'd each font in the conversion table
  142.    *  Thus, for each font handle in the conversion table, we increment its
  143.    *  usage count. (Note that Font_LoseAllFonts() clears the table out to 0's)
  144.    */
  145.  
  146.   Font_LoseAllFonts(template_fontarray);
  147.  
  148.   for (i = 0; i < 256; i++)
  149.     if (fontconvert.fonts[i] != 0)
  150.       template_fontarray->fonts[fontconvert.fonts[i]]++;
  151. }
  152.