home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 …ember: Reference Library / Apple Developer Reference Library (December 1999) (Disk 1).iso / pc / technical documentation / develop / develop issue 26 / develop issue 26 code / truffles - display mgr. / sprocket / interfaces / menubar.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-02  |  4.1 KB  |  175 lines

  1. /*
  2.     File:        MenuBar.h
  3.  
  4.     Contains:    A MenuBar class deliberately designed to resemble
  5.                 ODMenuBar from OpenDoc™. Because it is really just
  6.                 a table of menu commands, we depend on our sorted
  7.                 dynamic array base class to maintain mappings.
  8.  
  9.     Written by: Dave Falkenburg
  10.  
  11.     Copyright:    © 1994-95 by Dave Falkenburg, all rights reserved.
  12.  
  13.     Change History (most recent first):
  14.  
  15.          <3>     1/24/95    DRF        Finished up work on CMNUs for next major release.
  16.          <2>     1/20/95    DRF        Added MenuBarResource type, revised some methods.
  17.          <1>      1/3/95    DRF        First checked in.
  18.  */
  19. #ifndef    _MENUBAR_
  20. #define    _MENUBAR_
  21.  
  22. #include <Menus.h>
  23. #include "SortedDynamicArray.h"
  24.  
  25. //    bare types are dangerous, eh?
  26.  
  27. typedef    SInt16    MenuID;
  28. typedef    UInt16    MenuItemID;
  29. typedef    SInt16    MenuBarTemplateID;
  30. typedef    UInt32    MenuCommandID;
  31.  
  32. //    Some types which should probably be defined in <Menus.h>
  33. //    NOTE: These must be aligned on 2-byte boundaries
  34. #if PRAGMA_ALIGN_SUPPORTED
  35. #pragma options align=mac68k
  36. #endif
  37.  
  38. //    Kinda strange that NONE of the toolbox headers define
  39. //    the ON-DISK representations of their template resources, eh?
  40.  
  41. typedef    struct    MenuBarResource    MenuBarResource;
  42.  
  43. struct    MenuBarResource
  44.     {
  45.     UInt16    numberOfMenus;
  46.     SInt16    menuIDList[1];
  47.     };
  48.  
  49.  
  50. //    CMNU Resources look just like 'MENU' resources, except that
  51. //    each item is appended with an extra (2-byte aligned) longword.
  52. //
  53. //    They are evil because menu items always start out screwed up
  54. //    because some bright individual decided to START the record
  55. //    with the variable-sized field!.
  56. //
  57. //    In the long run this is still OK, because ResEdit and Resourceror
  58. //    already create and edit CMNU resources. All the evil is confined
  59. //    within parts of Sprocket you don’t have to modify.
  60.  
  61. typedef    struct    CMNUResource    CMNUResource;
  62. typedef    struct    CMNUItemData    CMNUItemData;
  63.  
  64.  
  65. //    CW6 didn’t like having this below the CMNUResource definition,
  66. //    even though CW5 didn’t complain:
  67.  
  68. struct    CMNUItemData
  69.     {
  70.     Str255                itemString;    //    don’t you hate structures that start out misalinged!
  71.     Byte                itemIcon;
  72.     Byte                itemCmd;
  73.     Byte                itemMark;
  74.     Style                itemStyle;
  75.     MenuCommandID        itemCommand;
  76.     };
  77.  
  78.  
  79. struct CMNUResource
  80.     {
  81.     short                menuID;
  82.     short                menuWidth;
  83.     short                menuHeight;
  84.     Handle                menuProc;
  85.     long                enableFlags;
  86.     Str255                menuTitle;
  87.     CMNUItemData        menuData[1];    //    here begins evil!
  88.     };
  89.  
  90.  
  91. //    Restore default alignment
  92. #if PRAGMA_ALIGN_SUPPORTED
  93. #pragma options align=reset
  94. #endif
  95.  
  96.  
  97. enum
  98.     {
  99.     kNoMenuID            =    0,
  100.     kNoMenuItemID        =    0,
  101.     kNoMenuCommandID    =    0
  102.     };
  103.  
  104.  
  105. struct    MenuMapping
  106.     {
  107.     MenuID            fMenu;
  108.     MenuItemID        fItem;
  109.     MenuCommandID    fCommand;        //    the command ID, which must be unique
  110.     };
  111.  
  112.  
  113. class    TMenuItemTable : public TSortedDynamicArray
  114.     {
  115.     virtual    CompareResult    Compare(ArrayElementPtr element1,
  116.                                     ArrayElementPtr    element2);
  117.     };
  118.  
  119.  
  120. class    TMenuCommandTable : public TSortedDynamicArray
  121.     {
  122.     virtual    CompareResult    Compare(ArrayElementPtr element1,
  123.                                     ArrayElementPtr    element2);
  124.     };
  125.  
  126.  
  127. class    TMenuBar
  128.     {
  129. public:
  130.     //    Resource ('MBAR' and 'CMNU') Utilities
  131.     
  132.     OSErr                    GetNewMenuBar(short whichMBAR);
  133.     MenuRef                    GetMenuFromCMNU(short whichMenu);
  134.     
  135.     //    Menu command mapping functions
  136.  
  137.     MenuCommandID            GetCommand(MenuID menu, MenuItemID item);
  138.     void                    GetMenuAndItem(MenuCommandID commandNum, MenuID * returnedMenu, MenuItemID * returnedItem);
  139.     
  140.     OSErr                    RegisterCommand(MenuCommandID commandNum, MenuID menu, MenuItemID item);
  141.     OSErr                    UnregisterCommand(MenuCommandID commandNum);
  142.     
  143.     //    Menu enable/disable routines for menu items
  144.  
  145.     void                    EnableCommand(MenuCommandID commandNum, Boolean enable);
  146.     void                    EnableAndCheckCommand(MenuCommandID commandNum, Boolean enable, Boolean check);
  147.  
  148.     void                    GetItemString(MenuCommandID commandNum,StringPtr itemString);
  149.     void                    SetItemString(MenuCommandID commandNum,StringPtr itemString);
  150.  
  151.     //    helpful utility functions
  152.  
  153.     static void                HideMenuBar();
  154.     static void                ShowMenuBar();
  155.  
  156.     static void                EnterModalState();
  157.     static void                ExitModalState();
  158.  
  159. private:
  160.     //    "globals"
  161.     
  162.     static Boolean            fgMenuBarHidden;
  163.  
  164.     //    mapping tables
  165.  
  166.     TMenuCommandTable        fCommandTable;
  167.     TMenuItemTable            fMenuItemTable;
  168.  
  169.     //    internal methods
  170.     
  171.     MenuHandle                GetMenuHandleAndItemFromCommand(MenuCommandID commandNum, MenuID *menu,MenuItemID *item);
  172.     };
  173.  
  174. #endif
  175.