home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / UI / MenuBar.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  22.3 KB  |  909 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        MenuBar.cpp
  3.  
  4.     Contains:    Implementation of ODMenuBar
  5.  
  6.     Owned by:    Richard Rodseth
  7.  
  8.     Copyright:    © 1994 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>      6/7/96    eeh        1303383: check for smSystemScript in
  13.                                     SetItemString
  14.         <27>    10/23/95    RR        # 1291688 Synch with menu manager when
  15.                                     adding & removing menus, if this menu bar
  16.                                     is the current menu bar
  17.         <26>     10/8/95    TJ        Fixes Recomended by Refball
  18.         <25>     9/12/95    eeh        1283557: use SetErrorCode
  19.         <24>     8/21/95    RR         #1271358 GetMenuAndItem throws
  20.                                     kODErrInvalidCommandID. EnableCommand etc.
  21.                                     suppress this error and fail silently
  22.         <23>     8/16/95    NP        1274946: ErrorDef.idl problems. Add include
  23.                                     file.
  24.         <22>     8/15/95    RR        #1276683 remove extra new ODPrivMenuInfo.
  25.         <21>      8/3/95    RR        #1257260: Collapse B classes. Remove
  26.                                     somInit methods. Don't call IsInitialized
  27.                                     or SubclassResponsibility
  28.         <20>     7/25/95    VL        1270320: Delete foundMenu in RemoveMenu.
  29.                                     Delete elements in _fOtherMenus before
  30.                                     deleting it.
  31.         <19>     7/25/95    eeh        1259218: remove assert in SetItemString
  32.         <18>      6/8/95    RR        #1257260 Collapse base classes. #1214898
  33.                                     StdTypes.r -> ODTypes.r
  34.         <17>     5/26/95    RR        #1251403: Multithreading naming support
  35.         <16>     5/17/95    RR        #1250135/1250137/1250143 Getters increment
  36.                                     refcount
  37.         <15>     4/28/95    RR        1211085 Remove 5$ comments
  38.         <14>     4/13/95    RR        # 1216618 Added ODVolatile, #1220325
  39.                                     Removed private error code
  40.         <13>      4/7/95    RR        #1216618 Added SOM_TRY etc.
  41.         <12>      4/6/95    RR        #1211085 Remove 5$ comments
  42.         <11>      4/4/95    RR        # 1197749 Get/Set ItemString deal with
  43.                                     script code
  44.         <10>     3/22/95    RR        #1197749, 1194296 Set script code in
  45.                                     SetItemString
  46.          <9>     1/23/95    RR        # 1212896 Added EnableAll and DisableAll
  47.          <8>     1/12/95    jpa        Don't use obsolete Toolbox names [1211211]
  48.          <7>     9/29/94    RA        1189812: Mods for 68K build.
  49.          <6>     8/25/94    RR        Implemented IsValid. Added private
  50.                                     SetGeneration/GetGeneration
  51.          <5>     8/15/94    JBS        1181023:  return kODTrue in IsValid
  52.          <4>     8/15/94    CG        #1181023:  Added new IsValid API.  It is
  53.                                     not implemented yet.
  54.          <3>     7/19/94    eeh        fix to ODMenuBarCopy
  55.          <2>      7/6/94    RR        Implemented SetItemString and GetItemString
  56.          <1>     6/23/94    RR        first checked in
  57.  
  58.     To Do:
  59. */
  60.  
  61.  
  62. #define ODMenuBar_Class_Source
  63. #define VARIABLE_MACROS
  64. #include <MenuBar.xih>
  65.  
  66. #ifndef _PLFMDEF_
  67. #include "PlfmDef.h"
  68. #endif
  69.  
  70. #ifndef SOM_ODSession_xh
  71. #include <ODSessn.xh>
  72. #endif
  73.  
  74. #ifndef _ODUTILS_
  75. #include "ODUtils.h"
  76. #endif
  77.  
  78. #ifndef SOM_ODWindowState_xh
  79. #include <WinStat.xh>
  80. #endif
  81.  
  82. #ifndef _ORDCOLL_
  83. #include "OrdColl.h"
  84. #endif
  85.  
  86. #ifndef _CMDTABLE_
  87. #include "CmdTable.h"
  88. #endif
  89.  
  90. #ifndef _EXCEPT_
  91. #include "Except.h"
  92. #endif
  93.  
  94. #ifndef _ITEXT_
  95. #include "IText.h"
  96. #endif
  97.  
  98. #ifndef _ODDEBUG_
  99. #include "ODDebug.h"    // Adkins -- added
  100. #endif
  101.  
  102. #ifndef _TEMPOBJ_
  103. #include "TempObj.h"
  104. #endif
  105.  
  106. #ifndef _UTILERRS_
  107. #include "UtilErrs.h"
  108. #endif
  109.  
  110. #pragma segment ODMenuBar
  111.  
  112. #include "MenuBarB.cpp"    // Platform-independent methods, if any
  113.  
  114. //=====================================================================================
  115. // Constants
  116. //=====================================================================================
  117. #define kSyntheticMask    0x0F000000
  118. #define kMenuIDMask        0x00FFFF00
  119. #define kMenuIDShift    8
  120. #define kMenuItemMask    0x000000FF
  121.  
  122.  
  123. class ODPrivMenuInfo
  124. {
  125. public:
  126.     ODPrivMenuInfo(ODMenuID menuID, ODPlatformMenu menu,  
  127.                 ODPart* part, ODSShort position);
  128.     ~ODPrivMenuInfo();
  129.  
  130.     ODMenuID        fMenuID;
  131.     ODPlatformMenu  fMenu;
  132.     ODPart*            fPart;
  133.     ODSShort        fPosition;
  134. };
  135.  
  136. ODPrivMenuInfo::ODPrivMenuInfo( ODMenuID menuID, ODPlatformMenu menu, 
  137.                          ODPart* part, ODSShort position)
  138. {
  139.     fMenuID = menuID;
  140.     fMenu = menu;
  141.     fPart = part;
  142.     fPosition = position;
  143. }
  144.  
  145. ODPrivMenuInfo::~ODPrivMenuInfo()
  146. {
  147. }
  148.  
  149. //=====================================================================================
  150. // Class ODMenuBar
  151. //=====================================================================================
  152.  
  153.  
  154. SOM_Scope void  SOMLINK ODMenuBarInitMenuBar(ODMenuBar *somSelf, Environment *ev,
  155.         ODSession* session,
  156.         ODPlatformMenuBar menuBar)
  157. {
  158.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  159.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarInitMenuBar");
  160.  
  161.     SOM_TRY
  162.             
  163.         /* Moved from somInit. SOM itself sets fields to zero
  164.         _fSession = kODNULL;
  165.         _fSystemMenus = kODNULL;
  166.         _fCommandTable = kODNULL;
  167.         _fOtherMenus = kODNULL;
  168.         _fGeneration = 0;
  169.         */
  170.         
  171.         somSelf->InitRefCntObject(ev); 
  172.     
  173.         _fSession = session;
  174.         _fSystemMenus = menuBar;
  175.         
  176.     SOM_CATCH_ALL
  177.     SOM_ENDTRY
  178. }
  179.  
  180. SOM_Scope void  SOMLINK ODMenuBarAddSubMenu(ODMenuBar *somSelf, Environment *ev,
  181.         ODMenuID menuID,
  182.         ODPlatformMenu menu,
  183.         ODPart* part)
  184. {
  185.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  186.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarAddSubMenu");
  187.   
  188.       OrderedCollection* otherMenus = kODNULL; ODVolatile(otherMenus);
  189.     ODPrivMenuInfo* info = kODNULL; ODVolatile(info);
  190.       
  191.     SOM_TRY
  192.  
  193.         if (_fOtherMenus == kODNULL)
  194.         {
  195.             otherMenus = new OrderedCollection;
  196.             _fOtherMenus = otherMenus;
  197.         }
  198.         info = new ODPrivMenuInfo(menuID, menu,part, hierMenu);
  199.         _fOtherMenus->AddLast((ElementType) info);
  200.  
  201.         // if this is the current menu bar, we should also tell the Menu Manager
  202.         TempODMenuBar currentMenuBar = _fSession->GetWindowState(ev)->AcquireCurrentMenuBar(ev);
  203.         if (ODObjectsAreEqual(ev, currentMenuBar, somSelf))
  204.         {
  205.             ::InsertMenu(menu, hierMenu);
  206.             ::InvalMenuBar();
  207.         }
  208.  
  209.     SOM_CATCH_ALL
  210.     
  211.         ODDeleteObject(otherMenus);
  212.         ODDeleteObject(info);
  213.         
  214.     SOM_ENDTRY
  215.  
  216. }
  217.  
  218. SOM_Scope void  SOMLINK ODMenuBarRegisterCommand(ODMenuBar *somSelf, Environment *ev,
  219.         ODCommandID command,
  220.         ODMenuID menu,
  221.         ODMenuItemID menuItem)
  222. {
  223.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  224.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarRegisterCommand");
  225.     
  226.     CommandTable* table = kODNULL; ODVolatile(table);
  227.     
  228.     SOM_TRY
  229.  
  230.         if (_fCommandTable == kODNULL)
  231.         {
  232.             table = new CommandTable;
  233.             _fCommandTable = table;
  234.         }
  235.         _fCommandTable->RegisterCommand(command, menu, menuItem);
  236.     
  237.     SOM_CATCH_ALL
  238.     
  239.         ODDeleteObject(table);
  240.         
  241.     SOM_ENDTRY
  242. }
  243.  
  244. SOM_Scope void  SOMLINK ODMenuBarUnregisterCommand(ODMenuBar *somSelf, Environment *ev,
  245.         ODCommandID command)
  246. {
  247.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  248.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarUnregisterCommand");
  249.  
  250.     SOM_TRY
  251.     
  252.         if (_fCommandTable)
  253.             _fCommandTable->UnregisterCommand(command);
  254.         
  255.     SOM_CATCH_ALL
  256.     SOM_ENDTRY
  257. }
  258.  
  259. SOM_Scope void  SOMLINK ODMenuBarUnregisterAll(ODMenuBar *somSelf, Environment *ev)
  260. {
  261.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  262.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarUnregisterAll");
  263.  
  264.     SOM_TRY
  265.     
  266.         if (_fCommandTable)
  267.             _fCommandTable->UnregisterAll();
  268.  
  269.     SOM_CATCH_ALL
  270.     SOM_ENDTRY
  271. }
  272.  
  273.  
  274. SOM_Scope ODBoolean  SOMLINK ODMenuBarIsCommandRegistered(ODMenuBar *somSelf, Environment *ev,
  275.         ODCommandID command)
  276. {
  277.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  278.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarIsCommandRegistered");
  279.     
  280.     ODBoolean isRegistered = kODFalse;
  281.  
  282.     SOM_TRY
  283.     
  284.         if (_fCommandTable)
  285.             isRegistered = _fCommandTable->IsCommandRegistered(command);
  286.  
  287.     SOM_CATCH_ALL
  288.     SOM_ENDTRY
  289.     
  290.     return isRegistered;
  291. }
  292.  
  293. SOM_Scope ODBoolean  SOMLINK ODMenuBarIsCommandSynthetic(ODMenuBar *somSelf, Environment *ev,
  294.         ODCommandID command)
  295. {
  296.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  297.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarIsCommandSynthetic");
  298.  
  299.     return (command&kSyntheticMask) == kSyntheticMask;
  300. }
  301.  
  302. SOM_Scope ODCommandID  SOMLINK ODMenuBarGetCommand(ODMenuBar *somSelf, Environment *ev,
  303.         ODMenuID menu,
  304.         ODMenuItemID menuItem)
  305. {
  306.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  307.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarGetCommand");
  308.  
  309.     ODCommandID command = kNoCommand;
  310.     
  311.     SOM_TRY
  312.     
  313.         if (_fCommandTable)
  314.             command = _fCommandTable->GetCommand(menu, menuItem);
  315.             
  316.         if (command == kNoCommand)
  317.             // No command in table, so synthesize one:
  318.             command = kSyntheticMask | ((long)menu<<kMenuIDShift) | (menuItem & kMenuItemMask);
  319.  
  320.     SOM_CATCH_ALL
  321.     SOM_ENDTRY
  322.         
  323.     return command;
  324. }
  325.  
  326. SOM_Scope void  SOMLINK ODMenuBarGetMenuAndItem(ODMenuBar *somSelf, Environment *ev,
  327.         ODCommandID command,
  328.         ODMenuID* menu,
  329.         ODMenuItemID* menuItem)
  330. {
  331.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  332.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarGetMenuAndItem");
  333.  
  334.     ODBoolean found = kODFalse;
  335.     
  336.     SOM_TRY
  337.     
  338.         if (somSelf->IsCommandSynthetic(ev, command)) 
  339.         {    
  340.             *menu = (ODMenuID) ((command & kMenuIDMask) >>kMenuIDShift);
  341.             *menuItem = (ODMenuItemID) (command & kMenuItemMask);
  342.             found = kODTrue;
  343.         }
  344.         else if (_fCommandTable)
  345.         {
  346.             found = _fCommandTable->GetMenuAndItem(command, *menu, *menuItem);
  347.         }
  348.         if (!found)
  349.             THROW(kODErrInvalidCommandID); 
  350.  
  351.     SOM_CATCH_ALL
  352.     SOM_ENDTRY
  353. }
  354.  
  355. SOM_Scope void  SOMLINK ODMenuBarEnableCommand(ODMenuBar *somSelf, Environment *ev,
  356.         ODCommandID cmdNumber,
  357.         ODBoolean enable)
  358. {
  359.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  360.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarEnableCommand");
  361.  
  362.     SOM_TRY
  363.  
  364.         ODMenuID menuID;
  365.         ODMenuItemID item;
  366.         somSelf->GetMenuAndItem( ev, cmdNumber, &menuID, &item ) ; // THROWS
  367.         ODPlatformMenu macMenu = somSelf->GetMenu( ev, menuID ) ;
  368.         if (macMenu)
  369.         {
  370.             if (enable)
  371.                 EnableItem(macMenu, item) ;
  372.             else
  373.                 DisableItem(macMenu, item) ;
  374.         }
  375.             
  376.     SOM_CATCH_ALL
  377.     
  378.         SetErrorCode(kODNoError);
  379.         
  380.     SOM_ENDTRY
  381. }
  382.  
  383. SOM_Scope void  SOMLINK ODMenuBarCheckCommand(ODMenuBar *somSelf, Environment *ev,
  384.         ODCommandID cmdNumber,
  385.         ODBoolean check)
  386. {
  387.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  388.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarCheckCommand");
  389.     
  390.     SOM_TRY
  391.  
  392.         ODMenuID menuID;
  393.         ODMenuItemID item;
  394.         somSelf->GetMenuAndItem( ev, cmdNumber, &menuID, &item ) ;
  395.         ODPlatformMenu macMenu = somSelf->GetMenu( ev, menuID ) ;
  396.         if (macMenu)
  397.             CheckItem(macMenu, item, check); 
  398.  
  399.     SOM_CATCH_ALL
  400.     
  401.         SetErrorCode(kODNoError);
  402.     
  403.     SOM_ENDTRY
  404.  
  405. }
  406.  
  407. SOM_Scope void  SOMLINK ODMenuBarEnableAndCheckCommand(ODMenuBar *somSelf, Environment *ev,
  408.         ODCommandID cmdNumber,
  409.         ODBoolean enable,
  410.         ODBoolean check)
  411. {
  412.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  413.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarEnableAndCheckCommand");
  414.  
  415.     SOM_TRY
  416.  
  417.         ODMenuID menuID;
  418.         ODMenuItemID item;
  419.         somSelf->GetMenuAndItem( ev, cmdNumber, &menuID, &item ) ;
  420.         ODPlatformMenu macMenu = somSelf->GetMenu( ev, menuID ) ;
  421.         if (macMenu)
  422.         {
  423.         if ( enable )
  424.             EnableItem(macMenu, item) ;
  425.         else
  426.             DisableItem(macMenu, item) ;
  427.         CheckItem(macMenu, item, check); 
  428.         }
  429.  
  430.     SOM_CATCH_ALL
  431.  
  432.         SetErrorCode(kODNoError);
  433.  
  434.     SOM_ENDTRY
  435. }
  436.  
  437. SOM_Scope void  SOMLINK ODMenuBarSetItemString(ODMenuBar *somSelf, Environment *ev,
  438.         ODCommandID cmdNumber,
  439.         ODIText* itemString)
  440. {
  441.     const ODSShort    kUseSpecificScript = 0x1c;
  442.  
  443.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  444.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarSetItemString");
  445.     
  446.     SOM_TRY
  447.  
  448.         ODMenuID menuID;
  449.         ODMenuItemID item;
  450.         somSelf->GetMenuAndItem( ev, cmdNumber, &menuID, &item ) ;
  451.         ODPlatformMenu macMenu = somSelf->GetMenu( ev, menuID ) ;
  452.         if( macMenu != kODNULL ) 
  453.         {
  454.             StringPtr s = GetPStringFromIText(itemString);
  455.             SetMenuItemText(macMenu, item, s); 
  456.             ODDisposePtr(s);
  457.         
  458.             short scriptID = GetITextScriptCode(itemString);
  459.             ODSLong    savedSMFontForceFlag = GetScriptManagerVariable(smFontForce);
  460.             SetScriptManagerVariable(smFontForce, false);
  461.  
  462.             // if the scriptID is smSystemScript (which in our use means whatever
  463.             // the current app script is) then set it to the actual value.  Simply
  464.             // checking that it's not that constant before continuing does the same
  465.             // thing in this case.
  466.             if ( (scriptID != smSystemScript)
  467.                     && (scriptID != FontToScript( GetAppFont() )) )
  468.             {
  469.                 SetItemCmd(macMenu, item, kUseSpecificScript);
  470.                 SetItemIcon(macMenu, item, scriptID);
  471.             }
  472.             
  473.             SetScriptManagerVariable(smFontForce, savedSMFontForceFlag);
  474.         }
  475.  
  476.     SOM_CATCH_ALL
  477.  
  478.         SetErrorCode(kODNoError);
  479.  
  480.     SOM_ENDTRY
  481. }
  482.  
  483. SOM_Scope void  SOMLINK ODMenuBarGetItemString(ODMenuBar *somSelf, Environment *ev,
  484.         ODCommandID cmdNumber,
  485.         ODIText* itemString)
  486. {
  487.     const ODSShort    kUseSpecificScript = 0x1c;
  488.  
  489.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  490.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarGetItemString");
  491.     
  492.     SOM_TRY
  493.  
  494.         ODMenuID menuID;
  495.         ODMenuItemID item;
  496.         somSelf->GetMenuAndItem( ev, cmdNumber, &menuID, &item ) ;
  497.         ODPlatformMenu macMenu = somSelf->GetMenu( ev, menuID ) ;
  498.         if (macMenu)
  499.         {
  500.             if (itemString)    
  501.             {
  502.                 itemString->format = kODTraditionalMacText;
  503.                 itemString->text._buffer = kODNULL;
  504.             }
  505.             
  506.             Str255 s;
  507.             GetMenuItemText(macMenu, item, s); 
  508.             SetITextString(itemString, s);
  509.             
  510.             // Is this correct?
  511.             short cmd;
  512.             short scriptID = smSystemScript;
  513.             ODSLong    savedSMFontForceFlag = GetScriptManagerVariable(smFontForce);
  514.             SetScriptManagerVariable(smFontForce, false);
  515.             GetItemCmd (macMenu, item, &cmd);
  516.             if (cmd == kUseSpecificScript)
  517.             {
  518.                 GetItemIcon (macMenu, item, &scriptID);
  519.             }
  520.             SetITextScriptCode(itemString, scriptID);
  521.             SetITextLangCode(itemString, langEnglish);
  522.             SetScriptManagerVariable(smFontForce, savedSMFontForceFlag);
  523.         }
  524.  
  525.     SOM_CATCH_ALL
  526.     SOM_ENDTRY
  527. }
  528.  
  529. SOM_Scope void  SOMLINK ODMenuBarSetCommandTable(ODMenuBar *somSelf, Environment *ev,
  530.         CommandTable* table)
  531. {
  532.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  533.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarSetCommandTable");
  534.  
  535.     _fCommandTable = table;
  536. }
  537.  
  538. SOM_Scope void  SOMLINK ODMenuBarDisableAll(ODMenuBar *somSelf, Environment *ev)
  539. {
  540.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  541.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarDisableAll");
  542.  
  543.     SOM_TRY
  544.     
  545.         if (_fOtherMenus)
  546.         {
  547.             ODPlatformMenu platformMenu = kODNULL;
  548.             OrderedCollectionIterator iter(_fOtherMenus);
  549.             for (ODPrivMenuInfo* menuInfo = (ODPrivMenuInfo*) iter.First();
  550.                 iter.IsNotComplete();
  551.                 menuInfo = (ODPrivMenuInfo*) iter.Next())
  552.             {
  553.                 platformMenu = menuInfo->fMenu;
  554.                 DisableItem(platformMenu,0);
  555.             }
  556.         }
  557.         
  558.     SOM_CATCH_ALL
  559.     SOM_ENDTRY
  560. }
  561.  
  562. SOM_Scope void  SOMLINK ODMenuBarEnableAll(ODMenuBar *somSelf, Environment *ev)
  563. {
  564.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  565.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarEnableAll");
  566.  
  567.     SOM_TRY
  568.     
  569.         if (_fOtherMenus)
  570.         {
  571.             ODPlatformMenu platformMenu = kODNULL;
  572.             OrderedCollectionIterator iter(_fOtherMenus);
  573.             for (ODPrivMenuInfo* menuInfo = (ODPrivMenuInfo*) iter.First();
  574.                 iter.IsNotComplete();
  575.                 menuInfo = (ODPrivMenuInfo*) iter.Next())
  576.             {
  577.                 platformMenu = menuInfo->fMenu;
  578.                 EnableItem(platformMenu,0);
  579.             }
  580.         }
  581.     SOM_CATCH_ALL
  582.     SOM_ENDTRY
  583. }
  584.  
  585. SOM_Scope void  SOMLINK ODMenuBarsomUninit(ODMenuBar *somSelf)
  586. {
  587.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  588.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarsomUninit");
  589.  
  590.     ODDeleteObject(_fCommandTable);
  591.  
  592.     if (_fOtherMenus)
  593.     {
  594.         TRY
  595.             OrderedCollectionIterator* iter = _fOtherMenus->CreateIterator();
  596.             for (ElementType t = iter->First(); iter->IsNotComplete(); t = iter->Next())
  597.             {
  598.                 ODDeleteObject(t);
  599.             }
  600.             ODDeleteObject(iter);
  601.             ODDeleteObject(_fOtherMenus);
  602.         CATCH_ALL
  603.             // Ignore any exceptions
  604.         ENDTRY
  605.     }
  606. }
  607.  
  608. SOM_Scope ODSize  SOMLINK ODMenuBarPurge(ODMenuBar *somSelf, Environment *ev,
  609.         ODSize size)
  610. {
  611.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  612.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarPurge");
  613.  
  614.     ODSize purgeSize = 0;
  615.     
  616.     SOM_TRY
  617.     
  618.         purgeSize = parent_Purge(somSelf, ev, size);
  619.         
  620.     SOM_CATCH_ALL
  621.         WARN("Error %ld trying to purge in ODMenuBarPurge",ErrorCode());
  622.         SetErrorCode(kODNoError);        // dh - Eat the exception; Purge should not 
  623.                                         // propagate it because clients function
  624.                                         // fine whether memory was purged or not.
  625.     SOM_ENDTRY
  626.     
  627.     return purgeSize;
  628. }
  629.  
  630. SOM_Scope void  SOMLINK ODMenuBarRelease(ODMenuBar *somSelf, Environment *ev)
  631. {
  632.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  633.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarRelease");
  634.     
  635.     SOM_TRY
  636.  
  637.         parent_Release(somSelf,ev);
  638.         if (somSelf->GetRefCount(ev) == 0) 
  639.             somSelf->somFree(); 
  640.         
  641.     SOM_CATCH_ALL
  642.     SOM_ENDTRY    
  643. }
  644.  
  645. SOM_Scope void  SOMLINK ODMenuBarDisplay(ODMenuBar *somSelf, Environment *ev)
  646. {
  647.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  648.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarDisplay");
  649.  
  650.     SOM_TRY
  651.  
  652.         if (_fSystemMenus) 
  653.         {
  654.             ClearMenuBar();
  655.             SetMenuBar(_fSystemMenus);
  656.         }
  657.         if (_fOtherMenus)
  658.         {
  659.             OrderedCollectionIterator iter(_fOtherMenus);
  660.             for (ODPrivMenuInfo* menu = (ODPrivMenuInfo*) iter.First();
  661.                 iter.IsNotComplete();
  662.                 menu = (ODPrivMenuInfo*) iter.Next())
  663.             {
  664.                 InsertMenu(menu->fMenu,menu->fPosition);
  665.             }
  666.         }
  667.         InvalMenuBar();
  668.         
  669.         _fSession->GetWindowState(ev)->SetCurrentMenuBar(ev,somSelf);
  670.         
  671.     SOM_CATCH_ALL
  672.     SOM_ENDTRY    
  673. }
  674.  
  675. SOM_Scope void  SOMLINK ODMenuBarAddMenuLast(ODMenuBar *somSelf, Environment *ev,
  676.         ODMenuID menuID,
  677.         ODPlatformMenu menu,
  678.         ODPart* part)
  679. {
  680.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  681.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarAddMenuLast");
  682.  
  683.       OrderedCollection* otherMenus = kODNULL; ODVolatile(otherMenus);
  684.     ODPrivMenuInfo* info = kODNULL; ODVolatile(info);
  685.     
  686.     SOM_TRY
  687.     
  688.         if (_fOtherMenus == kODNULL)
  689.         {
  690.             otherMenus = new OrderedCollection;
  691.             _fOtherMenus = otherMenus;
  692.         }
  693.         info = new ODPrivMenuInfo(menuID, menu,part, 0);
  694.         _fOtherMenus->AddLast((ElementType) info);
  695.  
  696.         // if this is the current menu bar, we should also tell the Menu Manager
  697.         TempODMenuBar currentMenuBar = _fSession->GetWindowState(ev)->AcquireCurrentMenuBar(ev);
  698.         if (ODObjectsAreEqual(ev, currentMenuBar, somSelf))
  699.         {
  700.             ::InsertMenu(menu, 0);
  701.             ::InvalMenuBar();
  702.         }
  703.  
  704.     SOM_CATCH_ALL
  705.     
  706.         ODDeleteObject(otherMenus);
  707.         ODDeleteObject(info);
  708.         
  709.     SOM_ENDTRY    
  710. }
  711.  
  712. SOM_Scope void  SOMLINK ODMenuBarAddMenuBefore(ODMenuBar *somSelf, Environment *ev,
  713.         ODMenuID menuID,
  714.         ODPlatformMenu menu,
  715.         ODPart* part,
  716.         ODMenuID beforeID)
  717. {
  718.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  719.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarAddMenuBefore");
  720.  
  721.       OrderedCollection* otherMenus = kODNULL; ODVolatile(otherMenus);
  722.     ODPrivMenuInfo* info = kODNULL; ODVolatile(info);
  723.  
  724.     SOM_TRY
  725.     
  726.         if (_fOtherMenus == kODNULL)
  727.         {
  728.             otherMenus = new OrderedCollection;
  729.             _fOtherMenus = otherMenus;
  730.         }
  731.         info = new ODPrivMenuInfo(menuID, menu,part, beforeID);
  732.         _fOtherMenus->AddLast((ElementType) info);
  733.  
  734.         // if this is the current menu bar, we should also tell the Menu Manager
  735.         TempODMenuBar currentMenuBar = _fSession->GetWindowState(ev)->AcquireCurrentMenuBar(ev);
  736.         if (ODObjectsAreEqual(ev, currentMenuBar, somSelf))
  737.         {
  738.             ::InsertMenu(menu, beforeID);
  739.             ::InvalMenuBar();
  740.         }
  741.  
  742.     SOM_CATCH_ALL
  743.  
  744.         ODDeleteObject(otherMenus);
  745.         ODDeleteObject(info);
  746.  
  747.     SOM_ENDTRY    
  748. }
  749.  
  750. SOM_Scope void  SOMLINK ODMenuBarRemoveMenu(ODMenuBar *somSelf, Environment *ev,
  751.         ODMenuID menu)
  752. {
  753.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  754.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarRemoveMenu");
  755.  
  756.     ODPrivMenuInfo* foundMenu = kODNULL;
  757.     
  758.     SOM_TRY
  759.     
  760.         if (_fOtherMenus)
  761.         {
  762.             OrderedCollectionIterator iter(_fOtherMenus);
  763.             for (ODPrivMenuInfo* menuInfo = (ODPrivMenuInfo*) iter.First();
  764.                 iter.IsNotComplete();
  765.                 menuInfo = (ODPrivMenuInfo*) iter.Next())
  766.             {
  767.                 if (menuInfo->fMenuID == menu)
  768.                 {
  769.                     foundMenu = menuInfo;
  770.                     break;
  771.                 }
  772.             }
  773.             if (foundMenu)
  774.             {
  775.                 _fOtherMenus->Remove((ElementType) foundMenu);
  776.                 
  777.                 // if this is the current menu bar, we should also tell the Menu Manager
  778.                 TempODMenuBar currentMenuBar = _fSession->GetWindowState(ev)->AcquireCurrentMenuBar(ev);
  779.                 if (ODObjectsAreEqual(ev, currentMenuBar, somSelf))
  780.                 {
  781.                     ::DeleteMenu(menu);
  782.                     ::InvalMenuBar();
  783.                 }
  784.             }
  785.             ODDeleteObject(foundMenu);
  786.         }
  787.  
  788.     SOM_CATCH_ALL
  789.     SOM_ENDTRY    
  790. }
  791.  
  792. SOM_Scope ODPlatformMenu  SOMLINK ODMenuBarGetMenu(ODMenuBar *somSelf, Environment *ev,
  793.         ODMenuID menu)
  794. {
  795.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  796.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarGetMenu");
  797.  
  798.     ODPlatformMenu platformMenu = kODNULL;
  799.     
  800.     SOM_TRY
  801.     
  802.     if (_fOtherMenus)
  803.     {
  804.         OrderedCollectionIterator iter(_fOtherMenus);
  805.         for (ODPrivMenuInfo* menuInfo = (ODPrivMenuInfo*) iter.First();
  806.             iter.IsNotComplete();
  807.             menuInfo = (ODPrivMenuInfo*) iter.Next())
  808.         {
  809.             if (menuInfo->fMenuID == menu)
  810.             {
  811.                 platformMenu = menuInfo->fMenu;
  812.                 break;
  813.             }
  814.         }
  815.     }
  816.     // In case it's in the system menus:
  817.     if (platformMenu == kODNULL)
  818.         platformMenu = GetMenuHandle(menu);
  819.  
  820.     SOM_CATCH_ALL
  821.     SOM_ENDTRY    
  822.  
  823.     return platformMenu;
  824. }
  825.  
  826. SOM_Scope ODBoolean  SOMLINK ODMenuBarIsValid(ODMenuBar *somSelf, Environment *ev)
  827. {
  828.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  829.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarIsValid");
  830.     
  831.     ODBoolean isValid = kODFalse;
  832.     
  833.     SOM_TRY
  834.  
  835.         ODMenuBar* baseMenuBar = _fSession->GetWindowState(ev)->AcquireBaseMenuBar(ev); 
  836.         if (baseMenuBar)
  837.             isValid = _fGeneration == baseMenuBar->GetGeneration(ev);
  838.         baseMenuBar->Release(ev);
  839.         
  840.     SOM_CATCH_ALL
  841.     SOM_ENDTRY
  842.     
  843.     return isValid;
  844. }
  845.  
  846.  
  847. SOM_Scope void  SOMLINK ODMenuBarSetGeneration(ODMenuBar *somSelf, Environment *ev,
  848.         ODULong generation)
  849. {
  850.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  851.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarSetGeneration");
  852.  
  853.     _fGeneration = generation;
  854. }
  855.  
  856.  
  857. SOM_Scope ODULong  SOMLINK ODMenuBarGetGeneration(ODMenuBar *somSelf, Environment *ev)
  858. {
  859.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  860.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarGetGeneration");
  861.  
  862.     return _fGeneration;
  863. }
  864.  
  865. SOM_Scope ODMenuBar*  SOMLINK ODMenuBarCopy(ODMenuBar *somSelf, Environment *ev)
  866. {
  867.     ODMenuBarData *somThis = ODMenuBarGetData(somSelf);
  868.     ODMenuBarMethodDebug("ODMenuBar","ODMenuBarCopy");
  869.  
  870.     // Needs work, obviously!
  871.  
  872.     ODMenuBar* copy = kODNULL; ODVolatile(copy);
  873.     
  874.     SOM_TRY
  875.     
  876.         copy = new ODMenuBar();
  877.         THROW_IF_NULL(copy); // "new" does not THROW for SOM objects
  878.         copy->InitMenuBar(ev, _fSession,_fSystemMenus);
  879.     
  880.         if (_fOtherMenus)
  881.         {
  882.             OrderedCollectionIterator iter(_fOtherMenus);
  883.             for (ODPrivMenuInfo* menu = (ODPrivMenuInfo*) iter.First();
  884.                 iter.IsNotComplete();
  885.                 menu = (ODPrivMenuInfo*) iter.Next())
  886.             {
  887.                 if (menu->fPosition == 0)
  888.                     copy->AddMenuLast(ev, menu->fMenuID, menu->fMenu, menu->fPart);
  889.                 else if (menu->fPosition == hierMenu)
  890.                     copy->AddSubMenu(ev, menu->fMenuID, menu->fMenu, menu->fPart);
  891.                 else 
  892.                     copy->AddMenuBefore(ev, menu->fMenuID, menu->fMenu, 
  893.                                         menu->fPart, menu->fPosition);
  894.             }
  895.         if (_fCommandTable) //!!! eeewh!!!
  896.             // copy->fCommandTable = _fCommandTable->Copy();
  897.             copy->SetCommandTable(ev, _fCommandTable->Copy() );    // <eeh> was somSelf->SetCommandTable
  898.         }
  899.         copy->SetGeneration(ev, _fGeneration);
  900.  
  901.     SOM_CATCH_ALL
  902.         
  903.         ODDeleteObject(copy);
  904.         
  905.     SOM_ENDTRY
  906.             
  907.     return copy;
  908. }
  909.