home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / OS / FWMenu / Sources / FWMnuItm.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  28.9 KB  |  916 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWMnuItm.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWMNUITM_H
  13. #include "FWMnuItm.h"
  14. #endif
  15.  
  16. #ifndef FWMENU_K
  17. #include "FWMenu.k"
  18. #endif
  19.  
  20. #ifndef FWPULLDM_H
  21. #include "FWPullDM.h"
  22. #endif
  23.  
  24. #ifndef FWMNUBAR_H
  25. #include "FWMnuBar.h"
  26. #endif
  27.  
  28. #ifndef FWINTSTR_H
  29. #include "FWIntStr.h"
  30. #endif
  31.  
  32. // ----- Foundation Includes -----
  33.  
  34. #ifndef FWSTREAM_H
  35. #include "FWStream.h"
  36. #endif
  37.  
  38. #ifndef FWSTRING_H
  39. #include "FWString.h"
  40. #endif
  41.  
  42. // ----- OpenDoc Includes -----
  43.  
  44. #ifndef SOM_ODMenuBar_xh
  45. #include <MenuBar.xh>
  46. #endif
  47.  
  48. // ----- Macintosh Includes -----
  49.  
  50. #if defined(FW_BUILD_MAC) && !defined(__SCRIPT__)
  51. #include <Script.h>
  52. #endif
  53.  
  54. //========================================================================================
  55. //    RunTime Info
  56. //========================================================================================
  57.  
  58. #if FW_LIB_EXPORT_PRAGMAS
  59. #pragma lib_export on
  60. #endif
  61.  
  62. #ifdef FW_BUILD_MAC
  63. #pragma segment fwmenu
  64. #endif
  65.  
  66. FW_DEFINE_CLASS_M0(FW_CMenuItem)
  67. FW_DEFINE_CLASS_M1(FW_CSeparatorItem, FW_CMenuItem)
  68. FW_DEFINE_CLASS_M1(FW_CTextItem, FW_CMenuItem)
  69. FW_DEFINE_CLASS_M1(FW_CToggleItem, FW_CTextItem)
  70. FW_DEFINE_CLASS_M1(FW_CSubMenuItem, FW_CMenuItem)
  71.  
  72. FW_REGISTER_ARCHIVABLE_CLASS(FW_LMenuItem, FW_CMenuItem, FW_CMenuItem::Read, FW_CMenuItem::Write)
  73. FW_REGISTER_ARCHIVABLE_CLASS(FW_LSeparatorItem, FW_CSeparatorItem, FW_CSeparatorItem::Read, FW_CMenuItem::Write)
  74. FW_REGISTER_ARCHIVABLE_CLASS(FW_LTextItem, FW_CTextItem, FW_CTextItem::Read, FW_CMenuItem::Write)
  75. FW_REGISTER_ARCHIVABLE_CLASS(FW_LToggleItem, FW_CToggleItem, FW_CToggleItem::Read, FW_CMenuItem::Write)
  76. FW_REGISTER_ARCHIVABLE_CLASS(FW_LSubMenuItem, FW_CSubMenuItem, FW_CSubMenuItem::Read, FW_CMenuItem::Write)
  77.  
  78. //========================================================================================
  79. //    class FW_CMenuItem
  80. //========================================================================================
  81.  
  82. /*
  83. //----------------------------------------------------------------------------------------
  84. //    FW_CMenuItem::FW_CMenuItem
  85. //----------------------------------------------------------------------------------------
  86.  
  87. FW_CMenuItem::FW_CMenuItem() :
  88.     fOwnerMenu(NULL),
  89.     fIndex(0)
  90. #ifdef FW_BUILD_MAC
  91.     ,fCommandID(FW_kNoCommand)
  92. #endif
  93. {
  94. }
  95. */
  96.  
  97. //----------------------------------------------------------------------------------------
  98. //    FW_CMenuItem::FW_CMenuItem
  99. //----------------------------------------------------------------------------------------
  100.  
  101. FW_CMenuItem::FW_CMenuItem(Environment* ev, FW_CReadableStream& archive) :
  102.     fOwnerMenu(NULL)
  103. #ifdef FW_BUILD_MAC
  104.     ,fCommandID(FW_kNoCommand)
  105. #endif
  106. {
  107.     FW_READ_DYNAMIC_OBJECT(archive, &fOwnerMenu, FW_CPullDownMenu);
  108.     
  109.     archive >> fIndex;
  110. }
  111.  
  112. //----------------------------------------------------------------------------------------
  113. //    FW_CMenuItem::FW_CMenuItem
  114. //----------------------------------------------------------------------------------------
  115.  
  116. FW_CMenuItem::FW_CMenuItem(Environment* ev, FW_CPullDownMenu* ownerMenu, short index) :
  117.     fOwnerMenu(ownerMenu), 
  118.     fIndex(index)
  119. #ifdef FW_BUILD_MAC
  120.     ,fCommandID(FW_kNoCommand)
  121. #endif
  122. {
  123.     FW_ASSERT(ownerMenu != NULL);
  124.     FW_ASSERT(index > 0);
  125. }
  126.  
  127. //----------------------------------------------------------------------------------------
  128. //    FW_CMenuItem::~FW_CMenuItem
  129. //----------------------------------------------------------------------------------------
  130.  
  131. FW_CMenuItem::~FW_CMenuItem()
  132. {
  133. }
  134.  
  135. //----------------------------------------------------------------------------------------
  136. //    FW_CMenuItem::GetItemText
  137. //----------------------------------------------------------------------------------------
  138.  
  139. void FW_CMenuItem::GetItemText(Environment* ev, FW_CString& itemText) const
  140. {
  141.     itemText = "";    
  142. }
  143.  
  144. //----------------------------------------------------------------------------------------
  145. //    FW_CMenuItem::GetCommandID
  146. //----------------------------------------------------------------------------------------
  147.  
  148. ODCommandID FW_CMenuItem::GetCommandID(Environment* ev) const
  149. {
  150.     FW_ASSERT(fOwnerMenu != NULL);
  151.  
  152. #ifdef FW_BUILD_MAC
  153.     return fCommandID;
  154. #endif
  155.     
  156. #ifdef FW_BUILD_WIN
  157.     return fOwnerMenu->GetCommandID(ev, fIndex);
  158. #endif
  159. }
  160.  
  161. //----------------------------------------------------------------------------------------
  162. //    FW_CMenuItem::PrivGetMenuItem
  163. //----------------------------------------------------------------------------------------
  164.  
  165. FW_CMenuItem* FW_CMenuItem::PrivGetMenuItem(Environment* ev, ODCommandID commandID) const
  166. {
  167.     return NULL;
  168. }
  169.  
  170. //----------------------------------------------------------------------------------------
  171. //    FW_CMenuItem::GetMenuKey
  172. //----------------------------------------------------------------------------------------
  173.  
  174. FW_MenuKey FW_CMenuItem::GetMenuKey(Environment* ev) const
  175. {
  176.     return FW_kNoKeyEquivalent;
  177. }
  178.  
  179. //----------------------------------------------------------------------------------------
  180. //    FW_CMenuItem::PrivFindMenuWithID
  181. //----------------------------------------------------------------------------------------
  182.  
  183. FW_CPullDownMenu* FW_CMenuItem::PrivFindMenuWithID(Environment* ev, ODMenuID menuID) const
  184. {
  185.     return NULL;
  186. }
  187.  
  188. //----------------------------------------------------------------------------------------
  189. //    FW_CMenuItem::PrivDisableAll
  190. //----------------------------------------------------------------------------------------
  191.  
  192. void FW_CMenuItem::PrivDisableAll(Environment* ev)
  193. {
  194.     // Nothing to do on the Mac because FW_CPullDownMenu::DisableAll disable all its items
  195.     // at once
  196.     
  197. #ifdef FW_BUILD_WIN
  198.     HMENU hMenu = fOwnerMenu->GetPlatformMenu(ev).menu;
  199.     ::EnableMenuItem(hMenu, fIndex - 1, MF_BYPOSITION | MF_DISABLED | MF_GRAYED);
  200. #endif
  201. }
  202.  
  203. //----------------------------------------------------------------------------------------
  204. //    FW_CMenuItem::PrivEnableAll
  205. //----------------------------------------------------------------------------------------
  206.  
  207. void FW_CMenuItem::PrivEnableAll(Environment* ev)
  208. {
  209.     // Nothing to do on the Mac because FW_CPullDownMenu::DisableAll disable all its items
  210.     // at once
  211.     
  212. #ifdef FW_BUILD_WIN
  213.     HMENU hMenu = fOwnerMenu->GetPlatformMenu(ev).menu;
  214.     ::EnableMenuItem(hMenu, fIndex - 1, MF_BYPOSITION | MF_ENABLED);
  215. #endif
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. //    FW_CMenuItem::PrivAttach
  220. //----------------------------------------------------------------------------------------
  221.  
  222. void FW_CMenuItem::PrivAttach(Environment* ev, FW_CMenuBar* menuBar)
  223. {
  224.     // Nothing to do 
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    FW_CMenuItem::PrivDetach
  229. //----------------------------------------------------------------------------------------
  230.  
  231. void FW_CMenuItem::PrivDetach(Environment* ev, FW_CMenuBar* menuBar)
  232. {
  233.     // Nothing to do 
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. //    FW_CMenuItem::Flatten
  238. //----------------------------------------------------------------------------------------
  239.  
  240. void FW_CMenuItem::Flatten(Environment* ev, FW_CWritableStream& archive)
  241. {
  242.     FW_WRITE_DYNAMIC_OBJECT(archive, fOwnerMenu, FW_CPullDownMenu);
  243.     
  244.     archive << fIndex;
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. //    FW_CMenuItem::Read
  249. //----------------------------------------------------------------------------------------
  250.  
  251. void* FW_CMenuItem::Read(FW_CReadableStream& archive)
  252. {
  253. FW_UNUSED(archive);
  254.  
  255.     FW_DEBUG_MESSAGE("FW_CMenuItem::Read should never have been called");
  256.     return NULL;
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. //    FW_CMenuItem::Write
  261. //----------------------------------------------------------------------------------------
  262.  
  263. void FW_CMenuItem::Write(FW_CWritableStream& archive, const void* theMenuItem)
  264. {
  265.     Environment* ev = somGetGlobalEnvironment();
  266.     
  267.     FW_CMenuItem* menuItem = (FW_CMenuItem*)theMenuItem;
  268.     menuItem->Flatten(ev, archive);
  269. }
  270.  
  271. //========================================================================================
  272. //    class FW_CSeparatorItem
  273. //========================================================================================
  274.  
  275. /*
  276. //----------------------------------------------------------------------------------------
  277. //    FW_CSeparatorItem::FW_CSeparatorItem
  278. //----------------------------------------------------------------------------------------
  279.  
  280. FW_CSeparatorItem::FW_CSeparatorItem() :
  281.     FW_CMenuItem()
  282. {
  283. }
  284. */
  285.  
  286. //----------------------------------------------------------------------------------------
  287. //    FW_CSeparatorItem::FW_CSeparatorItem
  288. //----------------------------------------------------------------------------------------
  289.  
  290. FW_CSeparatorItem::FW_CSeparatorItem(Environment* ev, FW_CReadableStream& archive) :
  291.     FW_CMenuItem(ev, archive)
  292. {    
  293.     InitSeparator(ev, fOwnerMenu->GetPlatformMenu(ev));
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    FW_CSeparatorItem::FW_CSeparatorItem
  298. //----------------------------------------------------------------------------------------
  299.  
  300. FW_CSeparatorItem::FW_CSeparatorItem(Environment* ev, FW_CPullDownMenu* ownerMenu, short index) :
  301.     FW_CMenuItem(ev, ownerMenu, index)
  302. {
  303.     InitSeparator(ev, ownerMenu->GetPlatformMenu(ev));
  304. }
  305.  
  306. //----------------------------------------------------------------------------------------
  307. //    FW_CSeparatorItem::~FW_CSeparatorItem
  308. //----------------------------------------------------------------------------------------
  309.  
  310. FW_CSeparatorItem::~FW_CSeparatorItem()
  311. {
  312. }
  313.  
  314. //----------------------------------------------------------------------------------------
  315. //    FW_CSeparatorItem::InitSeparator
  316. //----------------------------------------------------------------------------------------
  317.  
  318. void FW_CSeparatorItem::InitSeparator(Environment* ev, const ODPlatformMenu& platformMenu)
  319. {
  320. #ifdef FW_BUILD_MAC
  321.     fCommandID = FW_kSeparatorCommand;
  322.     ::InsertMenuItem(platformMenu, "\p-", fIndex - 1);
  323. #endif
  324.  
  325. #ifdef FW_BUILD_WIN
  326.     ::InsertMenu(platformMenu.menu, fIndex, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
  327. #endif
  328. }
  329.  
  330. //----------------------------------------------------------------------------------------
  331. //    FW_CSeparatorItem::PrivDisableAll
  332. //----------------------------------------------------------------------------------------
  333.  
  334. void FW_CSeparatorItem::PrivDisableAll(Environment* ev)
  335. {
  336.     // Nothing to do. Already disable
  337. }
  338.  
  339. //----------------------------------------------------------------------------------------
  340. //    FW_CSeparatorItem::PrivEnableAll
  341. //----------------------------------------------------------------------------------------
  342.  
  343. void FW_CSeparatorItem::PrivEnableAll(Environment* ev)
  344. {
  345.     // Should never enable it
  346. }
  347.  
  348. //----------------------------------------------------------------------------------------
  349. //    FW_CSeparatorItem::Read
  350. //----------------------------------------------------------------------------------------
  351.  
  352. void* FW_CSeparatorItem::Read(FW_CReadableStream& archive)
  353. {
  354.     return new FW_CSeparatorItem(somGetGlobalEnvironment(), archive);
  355. }
  356.  
  357. //========================================================================================
  358. //    class FW_CTextItem
  359. //========================================================================================
  360.  
  361. /*
  362. //----------------------------------------------------------------------------------------
  363. //    FW_CTextItem::FW_CTextItem
  364. //----------------------------------------------------------------------------------------
  365.  
  366. FW_CTextItem::FW_CTextItem() :
  367.     FW_CMenuItem()
  368. {
  369. }
  370. */
  371.  
  372. //----------------------------------------------------------------------------------------
  373. //    FW_CTextItem::FW_CTextItem
  374. //----------------------------------------------------------------------------------------
  375.  
  376. FW_CTextItem::FW_CTextItem(Environment* ev, FW_CReadableStream& archive) :
  377.     FW_CMenuItem(ev, archive)
  378. {
  379.     ODCommandID commandID;
  380.     archive >> commandID;
  381.     
  382.     FW_CDynamicString text;
  383.     archive >> text;
  384.     
  385.     FW_MenuKey menuKey;
  386.     archive >> menuKey;
  387.     
  388.     PrivInitTextItem(ev, fOwnerMenu->GetPlatformMenu(ev), text, commandID, menuKey);
  389. }
  390.  
  391. //----------------------------------------------------------------------------------------
  392. //    FW_CTextItem::FW_CTextItem
  393. //----------------------------------------------------------------------------------------
  394.  
  395. FW_CTextItem::FW_CTextItem(Environment* ev,
  396.                         FW_CPullDownMenu* ownerMenu, 
  397.                         short index,            
  398.                         const FW_CString& text,
  399.                         ODCommandID commandID,
  400.                         FW_MenuKey menuKey) :
  401.     FW_CMenuItem(ev, ownerMenu, index)
  402. {
  403.     FW_ASSERT(commandID > 0);
  404.     PrivInitTextItem(ev, ownerMenu->GetPlatformMenu(ev), text, commandID, menuKey);
  405. }
  406.  
  407. //----------------------------------------------------------------------------------------
  408. //    FW_CTextItem::~FW_CTextItem
  409. //----------------------------------------------------------------------------------------
  410.  
  411. FW_CTextItem::~FW_CTextItem()
  412. {
  413. }
  414.  
  415. //----------------------------------------------------------------------------------------
  416. //    FW_CTextItem::PrivInitTextItem
  417. //----------------------------------------------------------------------------------------
  418.  
  419. void FW_CTextItem::PrivInitTextItem(Environment* ev,
  420.                               const ODPlatformMenu&    platformMenu,
  421.                               const FW_CString& text,
  422.                               ODCommandID commandID,
  423.                               FW_MenuKey menuKey)
  424. {
  425.     FW_ASSERT(commandID >= FW_kFirstUserCommandID);
  426.     
  427. #ifdef FW_BUILD_MAC
  428.     fCommandID = commandID;
  429.     
  430.     Str255 str;
  431.     text.ExportPascal(str);
  432.     ::InsertMenuItem(platformMenu, "\pabc", fIndex - 1);
  433.     ::SetMenuItemText(platformMenu, fIndex, str);
  434.     
  435.     FW_CIntlString* intlText = FW_DYNAMIC_CAST(FW_CIntlString, &text);
  436.     if (intlText)
  437.     {
  438.         ODScriptCode systemScriptCode = (ODScriptCode) ::GetScriptManagerVariable(smSysScript);
  439.         if (intlText->GetScriptCode() != systemScriptCode)
  440.         {    // set this menu item's script code
  441.             ::SetItemCmd(platformMenu, fIndex, FW_kScriptCodeKeyEquivalent);
  442.             ::SetItemIcon(platformMenu, fIndex, (short) intlText->GetScriptCode());
  443.             menuKey = FW_kNoKeyEquivalent;    // so that code below doesn't mess up the key field we just set
  444.         }
  445.     }
  446.     
  447.     if (menuKey != FW_kNoKeyEquivalent)
  448.         ::SetItemCmd(platformMenu, fIndex, menuKey & FW_kMenuKeyCharMask);
  449. #endif
  450.  
  451. #ifdef FW_BUILD_WIN
  452.     ::InsertMenu(platformMenu.menu, fIndex, MF_BYPOSITION | MF_STRING | MF_DISABLED | MF_UNCHECKED, commandID, text);
  453.     //::AppendMenu(platformMenu.menu, MF_STRING | MF_DISABLED | MF_UNCHECKED, commandID, text);
  454.     
  455.     // [HLX] No way yet to build accelerator table
  456. FW_UNUSED(menuKey);
  457. #endif
  458. }
  459.  
  460. //----------------------------------------------------------------------------------------
  461. //    FW_CTextItem::GetItemText
  462. //----------------------------------------------------------------------------------------
  463.  
  464. void FW_CTextItem::GetItemText(Environment* ev, FW_CString& itemText) const
  465. {
  466.     const ODPlatformMenu& platformMenu = fOwnerMenu->GetPlatformMenu(ev);
  467.  
  468. #ifdef FW_BUILD_MAC
  469.     Str255 str;
  470.     ::GetMenuItemText(platformMenu, fIndex, str);
  471.  
  472.     FW_CIntlString* intlText = FW_DYNAMIC_CAST(FW_CIntlString, &itemText);
  473.     if (intlText)
  474.     {    
  475.         // Retrieve this menu item's script information
  476.         short itemField;
  477.         ODScriptCode scriptCode = (ODScriptCode) ::GetScriptManagerVariable(smSysScript);
  478.         ODLangCode languageCode = (ODLangCode) ::GetScriptVariable(scriptCode, smScriptLang);
  479.         ::GetItemCmd(platformMenu, fIndex, &itemField);
  480.  
  481.         if (itemField == FW_kScriptCodeKeyEquivalent)    // item contains a script code
  482.         {
  483.             ::GetItemIcon(platformMenu, fIndex, &itemField);
  484.             if (itemField != 0x00FF)    /* kludge - $00FF is really 255, system script */
  485.                 scriptCode = (ODScriptCode) itemField;
  486.             languageCode = (ODLangCode) ::GetScriptVariable(scriptCode, smScriptLang);
  487.  
  488.             FW_CIntlString intlString((const FW_Char*) &str[1], 
  489.                                       (FW_CharacterCount) str[0],
  490.                                       scriptCode, languageCode);
  491.             itemText = intlString;
  492.             return;
  493.         }
  494.     }
  495.  
  496.     // default - just copy characters from menu string to itemText
  497.     itemText.ReplaceAll(str);
  498. #endif
  499.  
  500. #ifdef FW_BUILD_WIN
  501.     char str[256];
  502.     ::GetMenuString(platformMenu.menu, fIndex - 1, str, sizeof(str) - 1, MF_BYPOSITION);
  503.     itemText = str;
  504. #endif
  505. }
  506.  
  507. //----------------------------------------------------------------------------------------
  508. //    FW_CTextItem::SetItemText
  509. //----------------------------------------------------------------------------------------
  510.  
  511. void FW_CTextItem::SetItemText(Environment* ev, const FW_CString& itemText) const
  512. {
  513.     const ODPlatformMenu& platformMenu = fOwnerMenu->GetPlatformMenu(ev);
  514.  
  515. #ifdef FW_BUILD_MAC
  516.     Str255 str;
  517.     itemText.ExportPascal(str);
  518.     ::SetMenuItemText(platformMenu, fIndex, str);
  519.  
  520.     FW_CIntlString* intlText = FW_DYNAMIC_CAST(FW_CIntlString, &itemText);
  521.     if (intlText)
  522.     {
  523.         ODScriptCode systemScriptCode = (ODScriptCode) ::GetScriptManagerVariable(smSysScript);
  524.         if (intlText->GetScriptCode() != systemScriptCode)
  525.         {    
  526.             // set this menu item's script code
  527.             ::SetItemCmd(platformMenu, fIndex, FW_kScriptCodeKeyEquivalent);
  528.             ::SetItemIcon(platformMenu, fIndex, (short) intlText->GetScriptCode());
  529.         }
  530.         else    // remove script code leftover from previous tenant
  531.         {
  532.             ::SetItemCmd(platformMenu, fIndex, FW_kNoKeyEquivalent);
  533.             ::SetItemIcon(platformMenu, fIndex, 0);
  534.         }
  535.     }
  536. #endif
  537.  
  538. #ifdef FW_BUILD_WIN
  539.     char str[256];
  540.     itemText.Export(str);
  541.     ::ModifyMenu(platformMenu.menu, fIndex - 1, MF_BYPOSITION | MF_STRING, GetCommandID(ev), str);
  542. #endif
  543. }
  544.  
  545. //----------------------------------------------------------------------------------------
  546. //    FW_CTextItem::GetMenuKey
  547. //----------------------------------------------------------------------------------------
  548.  
  549. FW_MenuKey FW_CTextItem::GetMenuKey(Environment* ev) const
  550. {
  551. #ifdef FW_BUILD_MAC
  552.     FW_MenuKey menuKey;
  553.     ::GetItemCmd(fOwnerMenu->GetPlatformMenu(ev), fIndex, &menuKey);
  554.     return menuKey;
  555. #endif
  556.  
  557. #ifdef FW_BUILD_WIN
  558.     return 0;                    // [HLX] no way yet to parse accelerator table
  559. #endif
  560. }
  561.  
  562. #ifdef FW_BUILD_MAC
  563. //----------------------------------------------------------------------------------------
  564. //    FW_CTextItem::PrivAttach
  565. //----------------------------------------------------------------------------------------
  566.  
  567. void FW_CTextItem::PrivAttach(Environment* ev, FW_CMenuBar* menuBar)
  568. {
  569.     menuBar->PrivMacRegisterCommand(ev, fCommandID, fOwnerMenu->GetMenuID(ev), fIndex);
  570. }
  571. #endif
  572.  
  573. #ifdef FW_BUILD_MAC
  574. //----------------------------------------------------------------------------------------
  575. //    FW_CTextItem::PrivDetach
  576. //----------------------------------------------------------------------------------------
  577.  
  578. void FW_CTextItem::PrivDetach(Environment* ev, FW_CMenuBar* menuBar)
  579. {
  580.     menuBar->PrivMacUnregisterCommand(ev, fCommandID);
  581. }
  582. #endif
  583.  
  584. //----------------------------------------------------------------------------------------
  585. //    FW_CTextItem::Read
  586. //----------------------------------------------------------------------------------------
  587.  
  588. void* FW_CTextItem::Read(FW_CReadableStream& archive)
  589. {
  590. FW_UNUSED(archive);
  591.  
  592.     return new FW_CTextItem(somGetGlobalEnvironment(), archive);
  593. }
  594.  
  595. //----------------------------------------------------------------------------------------
  596. //    FW_CTextItem::Flatten
  597. //----------------------------------------------------------------------------------------
  598.  
  599. void FW_CTextItem::Flatten(Environment* ev, FW_CWritableStream& archive)
  600. {
  601.     FW_CMenuItem::Flatten(ev, archive);
  602.         
  603.     ODCommandID commandID = GetCommandID(ev);
  604.     archive << commandID;
  605.  
  606.     FW_CDynamicString text;
  607.     GetItemText(ev, text);
  608.     archive << text;
  609.         
  610.     FW_MenuKey menuKey = GetMenuKey(ev);
  611.     archive << menuKey;
  612. }
  613.  
  614. //========================================================================================
  615. //    class FW_CToggleItem
  616. //========================================================================================
  617.  
  618. /*
  619. //----------------------------------------------------------------------------------------
  620. //    FW_CToggleItem::FW_CToggleItem
  621. //----------------------------------------------------------------------------------------
  622.  
  623. FW_CToggleItem::FW_CToggleItem() :
  624.     FW_CTextItem(),
  625.     fToggleState(TRUE)
  626. {
  627. }
  628. */
  629.  
  630. //----------------------------------------------------------------------------------------
  631. //    FW_CToggleItem::FW_CToggleItem
  632. //----------------------------------------------------------------------------------------
  633.  
  634. FW_CToggleItem::FW_CToggleItem(Environment* ev, FW_CReadableStream& archive) :
  635.     FW_CTextItem(ev, archive)
  636. {
  637.     archive >> fToggleState;
  638.     archive >> fOtherText;
  639. }
  640.  
  641. //----------------------------------------------------------------------------------------
  642. //    FW_CToggleItem::FW_CToggleItem
  643. //----------------------------------------------------------------------------------------
  644.  
  645. FW_CToggleItem::FW_CToggleItem(Environment* ev,
  646.                         FW_CPullDownMenu* ownerMenu, 
  647.                         short index,            
  648.                         const FW_CString& trueText,
  649.                         const FW_CString& falseText,
  650.                         ODCommandID commandID,
  651.                         FW_MenuKey menuKey) :
  652.     FW_CTextItem(ev, ownerMenu, index, trueText, commandID, menuKey)
  653. {
  654.     fToggleState = TRUE;
  655.     fOtherText = falseText;
  656. }
  657.  
  658. //----------------------------------------------------------------------------------------
  659. //    FW_CToggleItem::~FW_CToggleItem
  660. //----------------------------------------------------------------------------------------
  661.  
  662. FW_CToggleItem::~FW_CToggleItem()
  663. {
  664. }
  665.  
  666. //----------------------------------------------------------------------------------------
  667. //    FW_CToggleItem::GetTrueText
  668. //----------------------------------------------------------------------------------------
  669.  
  670. void FW_CToggleItem::GetTrueText(Environment* ev, FW_CString& trueText) const
  671. {
  672.     if (fToggleState)
  673.         GetItemText(ev, trueText);
  674.     else
  675.         trueText = fOtherText;
  676. }
  677.  
  678. //----------------------------------------------------------------------------------------
  679. //    FW_CToggleItem::GetFalseText
  680. //----------------------------------------------------------------------------------------
  681.  
  682. void FW_CToggleItem::GetFalseText(Environment* ev, FW_CString& falseText) const
  683. {
  684.     if (fToggleState)
  685.         falseText = fOtherText;
  686.     else
  687.         GetItemText(ev, falseText);
  688. }
  689.  
  690. //----------------------------------------------------------------------------------------
  691. //    FW_CToggleItem::ToggleItem
  692. //----------------------------------------------------------------------------------------
  693.  
  694. void FW_CToggleItem::ToggleItem(Environment* ev, FW_Boolean newState)
  695. {
  696.     if (newState == fToggleState)
  697.         return;
  698.         
  699.     FW_CDynamicString currentText;
  700.     GetItemText(ev, currentText);
  701.         
  702.     fToggleState = newState;
  703.     
  704.     SetItemText(ev, fOtherText);
  705.     
  706.     fOtherText = currentText;
  707. }
  708.  
  709. //----------------------------------------------------------------------------------------
  710. //    FW_CToggleItem::Read
  711. //----------------------------------------------------------------------------------------
  712.  
  713. void* FW_CToggleItem::Read(FW_CReadableStream& archive)
  714. {
  715. FW_UNUSED(archive);
  716.  
  717.     return new FW_CToggleItem(somGetGlobalEnvironment(), archive);
  718. }
  719.  
  720. //----------------------------------------------------------------------------------------
  721. //    FW_CToggleItem::Flatten
  722. //----------------------------------------------------------------------------------------
  723.  
  724. void FW_CToggleItem::Flatten(Environment* ev, FW_CWritableStream& archive)
  725. {
  726.     FW_CTextItem::Flatten(ev, archive);
  727.     
  728.     archive << fToggleState;
  729.     archive << fOtherText;
  730. }
  731.  
  732. //========================================================================================
  733. //    class FW_CSubMenuItem
  734. //========================================================================================
  735.  
  736. /*
  737. //----------------------------------------------------------------------------------------
  738. //    FW_CSubMenuItem::FW_CSubMenuItem
  739. //----------------------------------------------------------------------------------------
  740.  
  741. FW_CSubMenuItem::FW_CSubMenuItem() :
  742.     FW_CMenuItem(),
  743.     fSubMenu(NULL)
  744. {
  745. }
  746. */
  747.  
  748. //----------------------------------------------------------------------------------------
  749. //    FW_CSubMenuItem::FW_CSubMenuItem
  750. //----------------------------------------------------------------------------------------
  751.  
  752. FW_CSubMenuItem::FW_CSubMenuItem(Environment* ev, FW_CReadableStream& archive) :
  753.     FW_CMenuItem(ev, archive)
  754. {
  755.     FW_READ_DYNAMIC_OBJECT(archive, &fSubMenu, FW_CPullDownMenu);
  756.     
  757.     PrivInitSubMenu(ev, fOwnerMenu->GetPlatformMenu(ev));
  758. }
  759.  
  760. //----------------------------------------------------------------------------------------
  761. //    FW_CSubMenuItem::FW_CSubMenuItem
  762. //----------------------------------------------------------------------------------------
  763.  
  764. FW_CSubMenuItem::FW_CSubMenuItem(Environment* ev,
  765.                 FW_CPullDownMenu* ownerMenu, 
  766.                 short index,
  767.                 FW_CPullDownMenu* adoptSubMenu):
  768.     FW_CMenuItem(ev, ownerMenu, index),
  769.     fSubMenu(adoptSubMenu)
  770. {
  771.     FW_ASSERT(adoptSubMenu != NULL);
  772.     PrivInitSubMenu(ev, ownerMenu->GetPlatformMenu(ev));
  773. }
  774.  
  775. //----------------------------------------------------------------------------------------
  776. //    FW_CSubMenuItem::~FW_CSubMenuItem
  777. //----------------------------------------------------------------------------------------
  778.  
  779. FW_CSubMenuItem::~FW_CSubMenuItem()
  780. {
  781.     delete fSubMenu;
  782.     fSubMenu = NULL;
  783. }
  784.  
  785. //----------------------------------------------------------------------------------------
  786. //    FW_CSubMenuItem::PrivInitSubMenu
  787. //----------------------------------------------------------------------------------------
  788.  
  789. void FW_CSubMenuItem::PrivInitSubMenu(Environment* ev, const ODPlatformMenu& platformMenu)
  790. {
  791.     const ODPlatformMenu& subPlatformMenu = fSubMenu->GetPlatformMenu(ev);
  792.  
  793. #ifdef FW_BUILD_MAC
  794.     fSubMenu->SetParentMenuItem(ev, this);
  795.     
  796.     // ----- Add it -----
  797.     ::InsertMenuItem(platformMenu, "\pabc", fIndex - 1);
  798.         
  799.     ::HLock((Handle)subPlatformMenu);
  800.     ::SetMenuItemText(platformMenu, fIndex, (*subPlatformMenu)->menuData);
  801.     ::HUnlock((Handle)subPlatformMenu);
  802.     
  803.     // ----- Set the cmd key to hMenuCmd to specify sub menu
  804.     ::SetItemCmd(platformMenu, fIndex, hMenuCmd);        // has sub menu
  805.     
  806.     // ----- The menu id of the submenu is in the mark character
  807.     //     We don't set the item mark because we don't know yet the 
  808.     //    ID of the sub menu. Will be done by PrivAttachedToMenuBar.
  809. #endif
  810.  
  811. #ifdef FW_BUILD_WIN
  812.     ::InsertMenu(platformMenu.menu,
  813.                  fIndex,
  814.                  MF_BYPOSITION | MF_POPUP,
  815.                  (UINT) subPlatformMenu.menu,
  816.                  subPlatformMenu.strMenu);
  817. #endif
  818. }
  819.  
  820. //----------------------------------------------------------------------------------------
  821. //    FW_CSubMenuItem::PrivFindMenuWithID
  822. //----------------------------------------------------------------------------------------
  823.  
  824. FW_CPullDownMenu* FW_CSubMenuItem::PrivFindMenuWithID(Environment* ev, ODMenuID menuID) const
  825. {
  826.     if (fSubMenu->GetMenuID(ev) == menuID)
  827.         return fSubMenu;
  828.     else 
  829.         return fSubMenu->PrivFindMenuWithID(ev, menuID);
  830. }
  831.  
  832. //----------------------------------------------------------------------------------------
  833. //    FW_CSubMenuItem::PrivGetMenuItem
  834. //----------------------------------------------------------------------------------------
  835.  
  836. FW_CMenuItem* FW_CSubMenuItem::PrivGetMenuItem(Environment* ev, ODCommandID commandID) const
  837. {
  838.     return fSubMenu->PrivGetMenuItem(ev, commandID);
  839. }
  840.  
  841. //----------------------------------------------------------------------------------------
  842. //    FW_CSubMenuItem::PrivDisableAll
  843. //----------------------------------------------------------------------------------------
  844.  
  845. void FW_CSubMenuItem::PrivDisableAll(Environment* ev)
  846. {
  847. //    FW_CMenuItem::PrivDisableAll(ev);
  848.     
  849.     fSubMenu->DisableAll(ev);
  850. }
  851.  
  852. //----------------------------------------------------------------------------------------
  853. //    FW_CSubMenuItem::PrivEnableAll
  854. //----------------------------------------------------------------------------------------
  855.  
  856. void FW_CSubMenuItem::PrivEnableAll(Environment* ev)
  857. {
  858.     FW_CMenuItem::PrivEnableAll(ev);
  859.     
  860.     fSubMenu->EnableAll(ev);
  861. }
  862.  
  863. //----------------------------------------------------------------------------------------
  864. //    FW_CSubMenuItem::PrivAttach
  865. //----------------------------------------------------------------------------------------
  866.  
  867. void FW_CSubMenuItem::PrivAttach(Environment* ev, FW_CMenuBar* menuBar)
  868. {
  869.     short subMenuID = fSubMenu->PrivAcquireMenuID(ev, menuBar);
  870. #ifdef FW_BUILD_MAC
  871.     ::SetItemMark(fOwnerMenu->GetPlatformMenu(ev), fIndex, (FW_Char)subMenuID);
  872. #endif
  873.  
  874.     menuBar->PrivAddSubMenu(ev, subMenuID, fSubMenu->GetPlatformMenu(ev));
  875.     
  876.     fSubMenu->PrivAttach(ev, menuBar);
  877. }
  878.  
  879. //----------------------------------------------------------------------------------------
  880. //    FW_CSubMenuItem::PrivDetach
  881. //----------------------------------------------------------------------------------------
  882.  
  883. void FW_CSubMenuItem::PrivDetach(Environment* ev, FW_CMenuBar* menuBar)
  884. {
  885. #ifdef FW_BUILD_MAC
  886.     ::SetItemMark(fOwnerMenu->GetPlatformMenu(ev), fIndex, (FW_Char)0xFF);
  887. #endif
  888.     menuBar->PrivRemoveMenu(ev, fSubMenu->GetMenuID(ev));
  889.  
  890.     fSubMenu->PrivRelinquishMenuID(ev);
  891.         
  892.     fSubMenu->PrivDetach(ev, menuBar);
  893. }
  894.  
  895. //----------------------------------------------------------------------------------------
  896. //    FW_CSubMenuItem::Read
  897. //----------------------------------------------------------------------------------------
  898.  
  899. void* FW_CSubMenuItem::Read(FW_CReadableStream& archive)
  900. {
  901. FW_UNUSED(archive);
  902.  
  903.     return new FW_CMenuItem(somGetGlobalEnvironment(), archive);
  904. }
  905.  
  906. //----------------------------------------------------------------------------------------
  907. //    FW_CSubMenuItem::Flatten
  908. //----------------------------------------------------------------------------------------
  909.  
  910. void FW_CSubMenuItem::Flatten(Environment* ev, FW_CWritableStream& archive)
  911. {
  912.     FW_CMenuItem::Flatten(ev, archive);
  913.     
  914.     FW_WRITE_DYNAMIC_OBJECT(archive, fSubMenu, FW_CPullDownMenu);
  915. }
  916.