menuBarSDL.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/menus/menuBar.h"
  23. #include "platform/menus/popupMenu.h"
  24. #include "gui/core/guiCanvas.h"
  25. #include "windowManager/platformWindowMgr.h"
  26. #include "core/util/safeDelete.h"
  27. #include "windowManager/sdl/sdlWindow.h"
  28. #include "gui/editor/guiMenuBar.h"
  29. #include "platformSDL/menus/PlatformSDLPopupMenuData.h"
  30. #ifdef TORQUE_SDL
  31. //-----------------------------------------------------------------------------
  32. // Platform Data
  33. //-----------------------------------------------------------------------------
  34. // class PlatformMenuBarData
  35. // {
  36. //
  37. // };
  38. Map<GuiMenuBar::Menu*, PopupMenu*> PlatformPopupMenuData::mMenuMap;
  39. class GuiPlatformGenericMenuBar : public GuiMenuBar
  40. {
  41. typedef GuiMenuBar Parent;
  42. public:
  43. DECLARE_CONOBJECT(GuiPlatformGenericMenuBar);
  44. virtual void menuItemSelected(Menu *menu, MenuItem *item)
  45. {
  46. AssertFatal(menu && item, "");
  47. PopupMenu *popupMenu = PlatformPopupMenuData::mMenuMap[ menu ];
  48. AssertFatal(popupMenu, "");
  49. popupMenu->handleSelect( item->id );
  50. Parent::menuItemSelected(menu, item);
  51. }
  52. protected:
  53. /// menu id / item id
  54. Map<CompoundKey<U32, U32>, String> mCmds;
  55. };
  56. IMPLEMENT_CONOBJECT(GuiPlatformGenericMenuBar);
  57. //-----------------------------------------------------------------------------
  58. // MenuBar Methods
  59. //-----------------------------------------------------------------------------
  60. void MenuBar::createPlatformPopupMenuData()
  61. {
  62. mData = NULL;
  63. }
  64. void MenuBar::deletePlatformPopupMenuData()
  65. {
  66. // SAFE_DELETE(mData);
  67. }
  68. //-----------------------------------------------------------------------------
  69. GuiPlatformGenericMenuBar* _FindMenuBarCtrl()
  70. {
  71. GuiControl* control;
  72. Sim::findObject("PlatformGenericMenubar", control);
  73. AssertFatal(control, "");
  74. if( !control )
  75. return NULL;
  76. GuiPlatformGenericMenuBar* menuBar;
  77. menuBar = dynamic_cast<GuiPlatformGenericMenuBar*>( control->findObjectByInternalName( StringTable->insert("menubar"), true) );
  78. AssertFatal(menuBar, "");
  79. return menuBar;
  80. }
  81. void MenuBar::updateMenuBar(PopupMenu *popupMenu /* = NULL */)
  82. {
  83. //if(! isAttachedToCanvas())
  84. // return;
  85. if(!popupMenu)
  86. return;
  87. GuiPlatformGenericMenuBar* menuBarGui = _FindMenuBarCtrl();
  88. popupMenu->mData->mMenuBar = this;
  89. String menuTitle = popupMenu->getBarTitle();
  90. //Next, find out if we're still in the list of entries
  91. SimSet::iterator itr = find(begin(), end(), popupMenu);
  92. GuiMenuBar::Menu* menuGui = menuBarGui->findMenu(menuTitle);
  93. if (!menuGui)
  94. {
  95. //This is our first time setting this particular menu up, so we'll OK it.
  96. if (itr == end())
  97. menuBarGui->attachToMenuBar(popupMenu->mData->mMenuGui);
  98. else
  99. menuBarGui->attachToMenuBar(popupMenu->mData->mMenuGui, itr - begin());
  100. }
  101. else
  102. {
  103. //Not our first time through, so we're really updating it.
  104. //So, first, remove it from the menubar
  105. menuBarGui->removeFromMenuBar(menuGui);
  106. //Next, find out if we're still in the list of entries
  107. SimSet::iterator itr = find(begin(), end(), popupMenu);
  108. //if we're no longer in the list, we're pretty much done here
  109. if (itr == end())
  110. return;
  111. //We're still here, so this is a valid menu for our current bar configuration, so add us back in.
  112. menuBarGui->attachToMenuBar(menuGui, itr - begin());
  113. }
  114. }
  115. //-----------------------------------------------------------------------------
  116. void MenuBar::attachToCanvas(GuiCanvas *owner, S32 pos)
  117. {
  118. if(owner == NULL || isAttachedToCanvas())
  119. return;
  120. // This is set for popup menus in the onAttachToMenuBar() callback
  121. mCanvas = owner;
  122. PlatformWindowSDL *pWindow = dynamic_cast<PlatformWindowSDL*>(owner->getPlatformWindow());
  123. if(pWindow == NULL)
  124. return;
  125. // Setup the native menu bar
  126. GuiMenuBar *hWindowMenu = static_cast<GuiMenuBar*>( pWindow->getMenuHandle() );
  127. if( hWindowMenu == NULL && !Journal::IsPlaying() )
  128. hWindowMenu = _FindMenuBarCtrl();
  129. if(hWindowMenu)
  130. {
  131. pWindow->setMenuHandle( hWindowMenu );
  132. GuiControl *base = hWindowMenu->getParent();
  133. while( base->getParent() )
  134. {
  135. base = base->getParent();
  136. }
  137. mCanvas->setMenuBar( base );
  138. }
  139. for (S32 i = 0; i < size(); ++i)
  140. {
  141. PopupMenu *mnu = dynamic_cast<PopupMenu *>(at(i));
  142. if (mnu == NULL)
  143. {
  144. Con::warnf("MenuBar::attachToMenuBar - Non-PopupMenu object in set");
  145. continue;
  146. }
  147. if (mnu->isAttachedToMenuBar())
  148. mnu->removeFromMenuBar();
  149. mnu->attachToMenuBar(owner, pos + i);
  150. }
  151. }
  152. void MenuBar::removeFromCanvas()
  153. {
  154. if (mCanvas == NULL || !isAttachedToCanvas())
  155. return;
  156. //_FindMenuBarCtrl()->clearMenus();
  157. // Add the items
  158. for (S32 i = 0; i < size(); ++i)
  159. {
  160. PopupMenu *mnu = dynamic_cast<PopupMenu *>(at(i));
  161. if (mnu == NULL)
  162. {
  163. Con::warnf("MenuBar::removeFromMenuBar - Non-PopupMenu object in set");
  164. continue;
  165. }
  166. mnu->removeFromMenuBar();
  167. }
  168. mCanvas->setMenuBar(NULL);
  169. mCanvas = NULL;
  170. }
  171. #endif