home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 August: Tool Chest / Apple_Developer_Group_August_1996_Tool_Chest.iso / Sample Code / Snippets / Toolbox / PopUpMenuSelectWithCurFont / PopUpMenuSelectWithCurFont.c next >
Encoding:
Text File  |  1996-03-25  |  5.1 KB  |  226 lines  |  [TEXT/CWIE]

  1.     //
  2.     //    PopUpMenuSelectWithCurFont demonstrates which low memory globals
  3.     //    (and possibly even nastier things) one must twiddle in order to
  4.     //    control the font used by MDEF 0 during PopUpMenuSelect. Note that
  5.     //    aside from the compatibility code associated with HOSTED_BY_FONT_MISCREANT,
  6.     //    this code roughly parallels what the popup menu CDEF (ID 63) does.
  7.     //    If at all possible you should make use of the CDEF, because when
  8.     //    Engineering breaks the low memory trick, we can fix the CDEF, but
  9.     //    we can't fix your code.
  10.     //
  11.     //    Complaints and kudos to:
  12.     //
  13.     //        Pete Gontier
  14.     //        Apple Macintosh Developer Technical Support
  15.     //        <gurgle@apple.com>
  16.     //
  17.     //    Change history:
  18.     //
  19.     //        03/25/96    PG        Was calling DeleteMenu with magic number.
  20.     //
  21.     //        03/25/96    PG        Fixed bizarre menu bar offset problem.
  22.     //                            Thanks to Harold Ekstrom.
  23.     //
  24.  
  25. #define OLDROUTINELOCATIONS        0
  26. #define OLDROUTINENAMES            0
  27. #define SystemSevenOrLater        1
  28.  
  29. #ifndef __FONTS__
  30. #    include <Fonts.h>
  31. #endif
  32.  
  33. #ifndef __DIALOGS__
  34. #    include <Dialogs.h>
  35. #endif
  36.  
  37. #ifndef __RESOURCES__
  38. #    include <Resources.h>
  39. #endif
  40.  
  41. #ifndef __LOWMEM__
  42. #    include <LowMem.h>
  43. #endif
  44.  
  45.     //
  46.     //    HOSTED_BY_FONT_MISCREANT
  47.     //
  48.     //    If you are writing an external code resource (filter,
  49.     //    XCMD, etc.), and this code isn't giving you any joy,
  50.     //    let HOSTED_BY_FONT_MISCREANT be 1 and see if that helps.
  51.     //    Some extension hosts set up a hostile font environment.
  52.     //    Your lack of joy is their fault, and they force you
  53.     //    to assume even more compatibility risk than you would
  54.     //    by using this code without HOSTED_BY_FONT_MISCREANT
  55.     //    defined. On the other hand, we *are* weaving a tangled
  56.     //    web by twiddling low memory, and sometimes one must pay
  57.     //    the price for this sort of behavior.
  58.     //
  59.  
  60. #define HOSTED_BY_FONT_MISCREANT 0
  61.  
  62. #if HOSTED_BY_FONT_MISCREANT
  63.  
  64. static pascal GrafPtr GetSomeWindowManagerPort (void)
  65. {
  66.     //
  67.     //    Produces a pointer to the Window Manager port or
  68.     //    Color Window Manager Port. You can test the returned
  69.     //    GrafPtr to see whether it's colored or not, but
  70.     //    many callers won't care.
  71.     //
  72.  
  73.     GrafPtr result = nil;
  74.  
  75.     SysEnvRec theWorld;
  76.  
  77.     //
  78.     //    Yes, SysEnvirons is deprecated. But it still works,
  79.     //    and it's a cheap way to buy backward compatibility
  80.     //    if you only need to know whether CQD support exists.
  81.     //
  82.  
  83.     if (SysEnvirons (1, &theWorld))
  84.         DebugStr ("\pWhoa! Panic! SysEnvirons failed?!");
  85.     else if (theWorld.hasColorQD)
  86.         GetCWMgrPort ((CGrafPtr *) &result);
  87.     else
  88.         GetWMgrPort (&result);
  89.  
  90.     return result;
  91. }
  92.  
  93. #endif
  94.  
  95. static pascal void PopUpMenuSelectWithCurFont
  96.     (MenuRef popUpMenuRef, Point where, unsigned short prevSelection)
  97. {
  98.     GrafPtr hostPort;
  99.  
  100.     short oldSysFont = LMGetSysFontFam ( );
  101.     short oldSysSize = LMGetSysFontSize ( );
  102.  
  103.     GetPort (&hostPort);
  104.  
  105.     //
  106.     //    Believe it or not, it's important to insert
  107.     //    the menu before diddling the font characteristics.
  108.     //
  109.  
  110.     InsertMenu (popUpMenuRef,hierMenu);
  111.  
  112.     LMSetSysFontFam (hostPort->txFont);
  113.     LMSetSysFontSize (hostPort->txSize);
  114.     LMSetLastSPExtra (-1);
  115.  
  116. #if HOSTED_BY_FONT_MISCREANT
  117.     SetPort (GetSomeWindowManagerPort ( ));
  118.     TextFont (0);
  119.     TextSize (0);
  120.     SetPort (hostPort);
  121. #endif
  122.  
  123.     PopUpMenuSelect (popUpMenuRef, where.v, where.h, prevSelection);
  124.  
  125.     LMSetSysFontFam (oldSysFont);
  126.     LMSetSysFontSize (oldSysSize);
  127.     LMSetLastSPExtra (-1);
  128.  
  129.     DeleteMenu ((**popUpMenuRef).menuID);
  130. }
  131.  
  132. //////////////////////////////////////////////////////////////////////
  133. //
  134. //    Below please find the usual sort of application boilerplate.
  135. //
  136. //////////////////////////////////////////////////////////////////////
  137.  
  138. static pascal OSErr InitMac (void)
  139. {
  140.     MaxApplZone ( );
  141.     InitGraf (&(qd.thePort));
  142.     InitFonts ( );
  143.     InitWindows ( );
  144.     InitMenus ( );
  145.     TEInit ( );
  146.     InitDialogs (nil);
  147.  
  148.     return noErr;
  149. }
  150.  
  151. static pascal Boolean ModalFilterProc (DialogPtr theDialog, EventRecord *theEvent, short *)
  152. {
  153.     Boolean result = false;
  154.  
  155.     if (theEvent->what == mouseDown)
  156.     {
  157.         Point localWhere = theEvent->where;
  158.         GrafPtr savePort;
  159.     
  160.         GetPort (&savePort);
  161.         SetPort (theDialog);
  162.         GlobalToLocal (&localWhere);
  163.     
  164.         if (FindDialogItem (theDialog, localWhere) == 1)
  165.         {
  166.             MenuRef popUpMenuRef = GetMenu (128);
  167.             if (popUpMenuRef)
  168.             {
  169.                 short txFont = theDialog->txFont;
  170.                 short txSize = theDialog->txSize;
  171.                 short iType; Handle iHandle; Rect iRect;
  172.                 Point popWhere;
  173.  
  174.                 GetDialogItem (theDialog,2,&iType,&iHandle,&iRect);
  175.  
  176.                 popWhere.v = iRect.bottom;
  177.                 popWhere.h = iRect.left + 2;
  178.                 LocalToGlobal (&popWhere);
  179.  
  180.                 InvertRect (&iRect);
  181.                     TextFont (geneva);
  182.                         TextSize (9);
  183.                             PopUpMenuSelectWithCurFont (popUpMenuRef,popWhere,0);
  184.                         TextSize (txSize);
  185.                     TextFont (txFont);
  186.                 InvertRect (&iRect);
  187.  
  188.                 ReleaseResource ((Handle) popUpMenuRef);
  189.             }
  190.         }
  191.     
  192.         SetPort (savePort);
  193.     }
  194.  
  195.     return result;
  196. }
  197.  
  198. static pascal Boolean SetUpMenuBar (void)
  199. {
  200.     Boolean result = false;
  201.     Handle mBar = GetNewMBar (128);
  202.     if (!ResError ( ) && mBar)
  203.     {
  204.         SetMenuBar (mBar);
  205.         AppendResMenu (GetMenuHandle (130), 'DRVR');
  206.         DrawMenuBar ( );
  207.         result = true;
  208.         ReleaseResource (mBar);
  209.     }
  210.     return result;
  211. }
  212.  
  213. void main (void)
  214. {
  215.     if (!InitMac ( ) && SetUpMenuBar ( ))
  216.     {
  217.         DialogRef dlgRef = GetNewDialog (128,nil,nil);
  218.         if (dlgRef)
  219.         {
  220.             short itemHit;
  221.             ModalDialog (ModalFilterProc,&itemHit);
  222.             DisposeDialog (dlgRef);
  223.         }
  224.     }
  225. }
  226.