home *** CD-ROM | disk | FTP | other *** search
/ Hand Held Organizer Toolkit / walnutcreekcdrom-handheldorganizertoolkit-march1998.iso / PalmPilot / development / pfc.sit / Src / CustomFonts.c next >
Text File  |  1997-04-25  |  6KB  |  292 lines

  1. /***********************************************************************
  2.  *
  3.  *    Copyright ⌐ 1997 Ken Hancock
  4.  *
  5.  * PROJECT: CustomFonts support code
  6.  *
  7.  * FILE: CustomFonts.c
  8.  *
  9.  * DESCRIPTION: Routines and trap patch for handling custom fonts
  10.  *
  11.  * REVISION HISTORY:
  12.  *      4/8/97    KSH        Initial version
  13.  *
  14.  **********************************************************************/
  15. #include <Pilot.h>
  16.  
  17. #include "CustomFonts.h" 
  18.  
  19. #if EMULATION_LEVEL == EMULATION_NONE
  20.     #define    curFontID        ((FontID *) 0x1EE)
  21.     #define    curFontPtr        ((FontPtr *) 0x1CE)
  22.     #define    curFontTable    ((FontPtr *) 0x1D2)
  23. #else
  24.     #define    curFontID        (UICurrentFontID)
  25.     #define    curFontPtr        (UICurrentFontPtr)
  26.     #define    curFontTable    (UIFontTable)
  27. //    #define    curFontID        ((FontID *) &(UICurrentFontID))
  28. //    #define    curFontPtr        ((FontPtr *) &(UICurrentFontPtr))
  29. //    #define    curFontTable    ((FontPtr *) &(UIFontTable))
  30. #endif
  31.  
  32. typedef struct
  33. {
  34.     FontPtr    cfSaveFont;
  35.     Int        cfSwapID;
  36. } CFSwapTable;
  37.  
  38. CFSwapTable cfSwapTable[UINumFonts];
  39.  
  40. /***********************************************************************
  41.  *
  42.  * FUNCTION:     CFInit
  43.  *
  44.  * DESCRIPTION:  Initializes the custom font tables
  45.  *
  46.  * PARAMETERS:   None.
  47.  *
  48.  * RETURNED:     Nothing.
  49.  *
  50.  ***********************************************************************/
  51.  
  52. void CFInit(void)
  53. {
  54.     MemSet(cfSwapTable, sizeof(cfSwapTable), 0);
  55. }
  56.  
  57.  
  58. /***********************************************************************
  59.  *
  60.  * FUNCTION:     CFSwapFont
  61.  *
  62.  * DESCRIPTION:  Swaps a custom font with a FONT of resource id fontResID
  63.  *                            If a second font has already been swapped in, it
  64.  *                            restores the system font before swapping in the new
  65.  *
  66.  * PARAMETERS:   FontID, Int
  67.  *
  68.  * RETURNED:     Err
  69.  *
  70.  ***********************************************************************/
  71.  
  72. Err CFSwapFont(FontID sysFont, Int fontResID)
  73. {
  74.     CFSwapTable    *stp;
  75.     Int            lastSwap;
  76.     VoidHand        fontH;
  77.     Err            err = 0;
  78.     
  79.     if (sysFont < UINumFonts)
  80.     {
  81.         //    If we're not swapping in the same font, unswap the last font first
  82.         
  83.         stp = &(cfSwapTable[sysFont]);
  84.         if ((lastSwap = stp->cfSwapID) != fontResID)
  85.         {
  86.             if (lastSwap != 0)
  87.                 CFRestoreFont(sysFont);
  88.             
  89.             fontH = DmGetResource('FONT', fontResID);
  90.             if (fontH)
  91.             {
  92.                 stp->cfSwapID = fontResID;
  93.                 stp->cfSaveFont = curFontTable[sysFont];
  94.                 curFontTable[sysFont] = MemHandleLock(fontH);
  95.             }
  96.             else
  97.                 err = dmErrResourceNotFound;
  98.         }
  99.     }
  100.     
  101.     return(err);
  102. }
  103.  
  104.  
  105. /***********************************************************************
  106.  *
  107.  * FUNCTION:     CFRestoreFont
  108.  *
  109.  * DESCRIPTION:  Restores the specified system font back to its original
  110.  *
  111.  * PARAMETERS:   FontID
  112.  *
  113.  * RETURNED:     Err
  114.  *
  115.  ***********************************************************************/
  116.  
  117. Err CFRestoreFont(FontID sysFont)
  118. {
  119.     Err            err;
  120.     CFSwapTable    *stp;
  121.     VoidHand        fontH;
  122.  
  123.     if (sysFont < UINumFonts)
  124.     {
  125.         stp = &(cfSwapTable[sysFont]);
  126.         if (stp->cfSwapID)
  127.         {
  128.             fontH = DmGetResource('FONT', stp->cfSwapID);
  129.             if (fontH)
  130.             {
  131.                 curFontTable[sysFont] = stp->cfSaveFont;
  132.                 stp->cfSwapID = 0;
  133.                 MemHandleUnlock(fontH);
  134.             }
  135.             else
  136.                 err = dmErrResourceNotFound;
  137.         }
  138.     }
  139.     
  140.     return(err);
  141. }
  142.  
  143.  
  144. /***********************************************************************
  145.  *
  146.  * FUNCTION:        CFRestoreAllFonts
  147.  *
  148.  * DESCRIPTION:     Restores all fonts back to their defaults
  149.  *
  150.  * PARAMETERS:      
  151.  *
  152.  * RETURNED:     Nothing.
  153.  *
  154.  ***********************************************************************/
  155.  
  156. Err CFRestoreAllFonts(void)
  157. {
  158.     Err    err = 0;
  159.     Err    newerr;
  160.     Word    i;
  161.     
  162.     for (i = 0; i < UINumFonts; i++)
  163.     {
  164.         newerr = CFRestoreFont((FontID) i);
  165.         if (newerr && (err == 0))
  166.             err = newerr;
  167.     }
  168.     return(err);
  169. }
  170.  
  171. /*
  172.  
  173. #if EMULATION_LEVEL == EMULATION_NONE
  174.     #define    curFontID    (*((FontID *) 0x1EE))
  175.     #define    curFontPtr    (*((FontPtr *) 0x1CE))
  176. #else
  177.     Don't even try to run this under emulation!
  178. #endif
  179.  
  180. FontID    CustomFntSetFont (FontID font);
  181.  
  182. typedef FontID (*FntSetFontProc)(FontID font);
  183.  
  184. FntSetFontProc *oldFntSetFont;
  185. FontPtr            *customFontTable;
  186. Word                customFontCount;
  187. */
  188.  
  189. /***********************************************************************
  190.  *
  191.  * FUNCTION:     InstallCustomFonts            OBSOLETE for 2.0 firmware
  192.  *
  193.  * DESCRIPTION:  Routine counts the number of fonts, creats a font table
  194.  *                        and installs the trap patch.
  195.  *
  196.  * PARAMETERS:   None.
  197.  *
  198.  * RETURNED:     Nothing.
  199.  *
  200.  ***********************************************************************/
  201. /*
  202. void InstallCustomFonts()
  203. {
  204.     short        i;
  205.     VoidHand    h;
  206.     
  207.     //    Count how many fonts are installed starting at kCustomFontResBase
  208.     
  209.     i = 0;
  210.     while (DmGet1Resource('FONT', kCustomFontResBase + i) != 0)
  211.         i++;
  212.     
  213.     customFontCount = i;
  214.     //    If any fonts were found, generate a FontPtr table and install trap patch
  215.     
  216.     if (customFontCount)
  217.     {    
  218.         customFontTable = (FontPtr *) MemPtrNew(sizeof(FontPtr) * i);
  219.         
  220.         while (i--)
  221.         {
  222.             h = DmGetResource('FONT', kCustomFontResBase + i);
  223.             customFontTable[i] = (FontPtr) MemHandleLock(h);
  224.         }
  225.  
  226.         oldFntSetFont = SysGetTrapAddress(sysTrapFntSetFont);
  227.         SysSetTrapAddress(sysTrapFntSetFont,CustomFntSetFont);
  228.     }
  229. }
  230. */
  231.  
  232. /***********************************************************************
  233.  *
  234.  * FUNCTION:     RemoveCustomFonts            OBSOLETE for 2.0 firmware
  235.  *
  236.  * DESCRIPTION:  Routine unlocks font resources and 
  237.  *                        deinstalls the trap patch.
  238.  *
  239.  * PARAMETERS:   None.
  240.  *
  241.  * RETURNED:     Nothing.
  242.  *
  243.  ***********************************************************************/
  244. /*
  245. void RemoveCustomFonts()
  246. {
  247.     Word        i;
  248.     VoidHand    h;
  249.  
  250.     SysSetTrapAddress(sysTrapFntSetFont, oldFntSetFont);
  251.     i = 0;
  252.     while ((h = DmGet1Resource('FONT', kCustomFontResBase + i)) != 0)
  253.     {
  254.         MemHandleUnlock(h);
  255.         i++;
  256.     }
  257.     MemPtrFree(customFontTable);
  258.     
  259. }
  260. */
  261.  
  262. /***********************************************************************
  263.  *
  264.  * FUNCTION:     CustomFntSetFont            OBSOLETE for 2.0 firmware
  265.  *
  266.  * DESCRIPTION:  Replacement for FntSetFont trap
  267.  *
  268.  * PARAMETERS:   FontID -- ID of font to set; 100+ for custom fonts
  269.  *
  270.  * RETURNED:     Current FontID
  271.  *
  272.  ***********************************************************************/
  273. /*
  274. FontID CustomFntSetFont (FontID font)
  275. {
  276.     FontID    result;
  277.         
  278.     if (curFontID >= kCustomFontBase)
  279.         result = 0;
  280.     else
  281.         result = ((FntSetFontProc) oldFntSetFont)(font);
  282.         
  283.     if ((font >= kCustomFontBase) && (font < kCustomFontBase + customFontCount))
  284.     {
  285.         curFontID = font;
  286.         curFontPtr = customFontTable[font - kCustomFontBase];
  287.     }
  288.  
  289.     return(result);
  290. }
  291.  
  292. */