home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Pascal / Libraries / Mercutio 1.1.5f / APIs / Mercutio API.c next >
Encoding:
C/C++ Source or Header  |  1993-01-20  |  5.0 KB  |  149 lines  |  [TEXT/KAHL]

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