home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / MacPerl 5.1.3 / Mac_Perl_513_src / MacPerl5 / Mercutio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  5.1 KB  |  152 lines  |  [TEXT/MPS ]

  1. /***********************************************************************************
  2. **
  3. **       Developer's Programming Interface for Mercutio Menu Definition Function
  4. **               © 1992 Ramon M. Felciano, All Rights Reserved
  5. **                       C port -- December 5, 1992
  6. **
  7. ************************************************************************************/
  8.  
  9. #include "Menus.h"
  10. #include "Memory.h"
  11. #include "Events.h"
  12.  
  13. #define        customDefProcSig  'CUST'
  14. #define        areYouCustomMsg  128
  15. #define        getVersionMsg  131
  16. #define        getCopyrightMsg  132
  17. #define        mMenuKeyMsg  262
  18. #define     _Point2Long(pt)    (* (long *) &pt)            // these would have pbs with register vars
  19. #define     _Long2Point(long)    (* (Point *) &long)
  20.  
  21. void InitMercutio(void);
  22. long PowerMenuKey (long theMessage, short theModifiers, MenuHandle hMenu);
  23. Boolean IsCustomMenu (MenuHandle menu);
  24. long    GetMDEFVersion (MenuHandle menu);
  25. StringHandle    GetMDEFCopyright (MenuHandle menu);
  26.  
  27. typedef pascal void (*MDEFProc)(short msg, MenuHandle theMenu, Rect* menuRect,
  28.                 Point hitPt, short *itemID);
  29.  
  30.  
  31. /***********************************************************************************
  32. **
  33. **   GetMDEFVersion returns the MDEF version in long form. This can be typecast
  34. **     to a normal version record if needed.
  35. **
  36. ************************************************************************************/
  37. long    GetMDEFVersion (MenuHandle menu)
  38. {
  39.     SignedByte state;
  40.     Handle    proc;
  41.     Rect    dummyRect;
  42.     short    dummyInt;
  43.     Point     pt;
  44.     
  45.     proc = (*menu)->menuProc;    /* same as **menu.menuProc */
  46.     state = HGetState(proc);
  47.     HLock(proc);
  48.     dummyRect.top = dummyRect.left = dummyRect.bottom = dummyRect.right = 0;
  49.  
  50.     SetPt(&pt,0,0);
  51.     CallMenuDefProc((MenuDefProcPtr) *proc, getVersionMsg, menu, &dummyRect, pt, &dummyInt);
  52.     HSetState(proc, state);
  53.     
  54.     /* the result, a long, is returned in dummyRect.topLeft */
  55.     return _Point2Long(dummyRect);
  56. }
  57.  
  58. /***********************************************************************************
  59. **
  60. **   GetMDEFCopyright returns a stringHandle to the copyright message for the MDEF.
  61. **
  62. **   IMPORTANT: THE CALLER IS RESPONSIBLE FOR DISPOSING OF THIS HANDLE WHEN DONE
  63. **              WITH IT.
  64. **
  65. ************************************************************************************/
  66. StringHandle    GetMDEFCopyright (MenuHandle menu)
  67. {
  68.     SignedByte state;
  69.     Handle    proc;
  70.     Rect    dummyRect;
  71.     short    dummyInt;
  72.     Point     pt;
  73.     
  74.     proc = (*menu)->menuProc;    /* same as **menu.menuProc */
  75.     state = HGetState(proc);
  76.     HLock(proc);
  77.     dummyRect.top = dummyRect.left = dummyRect.bottom = dummyRect.right = 0;
  78.  
  79.     SetPt(&pt,0,0);
  80.     CallMenuDefProc((MenuDefProcPtr) *proc, getCopyrightMsg, menu, &dummyRect, pt, &dummyInt);
  81.     HSetState(proc, state);
  82.     
  83.     /* the result, a stringHandle, is returned in dummyRect.topLeft */
  84.     return *(StringHandle*)(&dummyRect);
  85. }
  86.  
  87. /***********************************************************************************
  88. **
  89. **   IsCustomMenu returns true if hMenu is controlled by a custom MDEF. This relies on my}
  90. **   convention of returning the customDefProcSig constant in the rect parameter: this obtuse}
  91. **   convention should be unique enough that only my custom MDEFs behave this way.}
  92. **
  93. ************************************************************************************/
  94. Boolean IsCustomMenu (MenuHandle menu)
  95. {
  96.     SignedByte state;
  97.     Handle    proc;
  98.     Rect    dummyRect;
  99.     short    dummyInt;
  100.     Point     pt;
  101.     
  102.     proc = (*menu)->menuProc;    /* same as **menu.menuProc */
  103.     state = HGetState(proc);
  104.     HLock(proc);
  105.     dummyRect.top = dummyRect.left = dummyRect.bottom = dummyRect.right = 0;
  106.  
  107.     SetPt(&pt,0,0);
  108.     CallMenuDefProc((MenuDefProcPtr) *proc, areYouCustomMsg, menu, &dummyRect, pt, &dummyInt);
  109.     HSetState(proc, state);
  110.     
  111.     /* the result, a long, is returned in dummyRect.topLeft */
  112.     return (_Point2Long(dummyRect) == (long) (customDefProcSig));
  113. }
  114.  
  115.  
  116. /***********************************************************************************
  117. **
  118. **   PowerMenuKey is a replacement for the standard toolbox call MenuKey for use with the}
  119. **   Mercutio. Given the keypress message and modifiers parameters from a standard event, it }
  120. **   checks to see if the keypress is a key-equivalent for a particular menuitem. If you are currently}
  121. **   using custom menus (i.e. menus using Mercutio), pass the handle to one of these menus in}
  122. **   hMenu. If you are not using custom menus, pass in NIL or another menu, and PowerMenuKey will use the}
  123. **   standard MenuKey function to interpret the keypress.}
  124. **
  125. **   As with MenuKey, PowerMenuKey returns the menu ID in high word of the result, and the menu}
  126. **   item in the low word.}
  127. **
  128. ************************************************************************************/
  129.  
  130. long PowerMenuKey (long theMessage, short theModifiers, MenuHandle hMenu)
  131. {
  132.     if ((hMenu == NULL) || (!IsCustomMenu(hMenu)))
  133.     {
  134.         return(MenuKey((char)(theMessage & charCodeMask)));
  135.     }
  136.     else
  137.     {
  138.         Handle proc = (*hMenu)->menuProc;
  139.         char state = HGetState(proc);
  140.         Rect dummyRect;
  141.         Point pt = _Long2Point(theMessage);
  142.         
  143.         HLock(proc);
  144.         dummyRect.top = dummyRect.left = 0;
  145.         CallMenuDefProc((MenuDefProcPtr) *proc, mMenuKeyMsg, hMenu, &dummyRect, pt, &theModifiers);
  146.         HSetState(proc, state);
  147.         return( _Point2Long(dummyRect));
  148.     }
  149. }
  150.  
  151.  
  152.