menuBarSDL.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #include "platformSDL/menus/guiPlatformGenericMenuBar.h"
  31. #ifdef TORQUE_SDL
  32. //-----------------------------------------------------------------------------
  33. // Platform Data
  34. //-----------------------------------------------------------------------------
  35. // class PlatformMenuBarData
  36. // {
  37. //
  38. // };
  39. Map<GuiMenuBar::Menu*, PopupMenu*> PlatformPopupMenuData::mMenuMap;
  40. IMPLEMENT_CONOBJECT(GuiPlatformGenericMenuBar);
  41. //-----------------------------------------------------------------------------
  42. // MenuBar Methods
  43. //-----------------------------------------------------------------------------
  44. void MenuBar::createPlatformPopupMenuData()
  45. {
  46. mData = NULL;
  47. }
  48. void MenuBar::deletePlatformPopupMenuData()
  49. {
  50. // SAFE_DELETE(mData);
  51. }
  52. //-----------------------------------------------------------------------------
  53. GuiPlatformGenericMenuBar* _FindMenuBarCtrl()
  54. {
  55. GuiControl* control;
  56. Sim::findObject("PlatformGenericMenubar", control);
  57. AssertFatal(control, "");
  58. if( !control )
  59. return NULL;
  60. GuiPlatformGenericMenuBar* menuBar;
  61. menuBar = dynamic_cast<GuiPlatformGenericMenuBar*>( control->findObjectByInternalName( StringTable->insert("menubar"), true) );
  62. AssertFatal(menuBar, "");
  63. return menuBar;
  64. }
  65. void MenuBar::updateMenuBar(PopupMenu *popupMenu /* = NULL */)
  66. {
  67. //if(! isAttachedToCanvas())
  68. // return;
  69. if(!popupMenu)
  70. return;
  71. GuiPlatformGenericMenuBar* menuBarGui = _FindMenuBarCtrl();
  72. popupMenu->mData->mMenuBar = this;
  73. String menuTitle = popupMenu->getBarTitle();
  74. //Next, find out if we're still in the list of entries
  75. SimSet::iterator itr = find(begin(), end(), popupMenu);
  76. GuiMenuBar::Menu* menuGui = menuBarGui->findMenu(menuTitle);
  77. if (!menuGui)
  78. {
  79. //This is our first time setting this particular menu up, so we'll OK it.
  80. if (itr == end())
  81. menuBarGui->attachToMenuBar(popupMenu->mData->mMenuGui);
  82. else
  83. menuBarGui->attachToMenuBar(popupMenu->mData->mMenuGui, itr - begin());
  84. }
  85. else
  86. {
  87. //Not our first time through, so we're really updating it.
  88. //So, first, remove it from the menubar
  89. menuBarGui->removeFromMenuBar(menuGui);
  90. //Next, find out if we're still in the list of entries
  91. SimSet::iterator itr = find(begin(), end(), popupMenu);
  92. //if we're no longer in the list, we're pretty much done here
  93. if (itr == end())
  94. return;
  95. //We're still here, so this is a valid menu for our current bar configuration, so add us back in.
  96. menuBarGui->attachToMenuBar(menuGui, itr - begin());
  97. }
  98. }
  99. //-----------------------------------------------------------------------------
  100. void MenuBar::attachToCanvas(GuiCanvas *owner, S32 pos)
  101. {
  102. if(owner == NULL || isAttachedToCanvas())
  103. return;
  104. // This is set for popup menus in the onAttachToMenuBar() callback
  105. mCanvas = owner;
  106. PlatformWindowSDL *pWindow = dynamic_cast<PlatformWindowSDL*>(owner->getPlatformWindow());
  107. if(pWindow == NULL)
  108. return;
  109. // Setup the native menu bar
  110. GuiMenuBar *hWindowMenu = static_cast<GuiMenuBar*>( pWindow->getMenuHandle() );
  111. if( hWindowMenu == NULL && !Journal::IsPlaying() )
  112. hWindowMenu = _FindMenuBarCtrl();
  113. if(hWindowMenu)
  114. {
  115. pWindow->setMenuHandle( hWindowMenu );
  116. GuiControl *base = hWindowMenu->getParent();
  117. while( base->getParent() )
  118. {
  119. base = base->getParent();
  120. }
  121. mCanvas->setMenuBar( base );
  122. }
  123. for (S32 i = 0; i < size(); ++i)
  124. {
  125. PopupMenu *mnu = dynamic_cast<PopupMenu *>(at(i));
  126. if (mnu == NULL)
  127. {
  128. Con::warnf("MenuBar::attachToMenuBar - Non-PopupMenu object in set");
  129. continue;
  130. }
  131. if (mnu->isAttachedToMenuBar())
  132. mnu->removeFromMenuBar();
  133. mnu->attachToMenuBar(owner, pos + i);
  134. }
  135. }
  136. void MenuBar::removeFromCanvas()
  137. {
  138. if (mCanvas == NULL || !isAttachedToCanvas())
  139. return;
  140. //_FindMenuBarCtrl()->clearMenus();
  141. // Add the items
  142. for (S32 i = 0; i < size(); ++i)
  143. {
  144. PopupMenu *mnu = dynamic_cast<PopupMenu *>(at(i));
  145. if (mnu == NULL)
  146. {
  147. Con::warnf("MenuBar::removeFromMenuBar - Non-PopupMenu object in set");
  148. continue;
  149. }
  150. mnu->removeFromMenuBar();
  151. }
  152. mCanvas->setMenuBar(NULL);
  153. mCanvas = NULL;
  154. }
  155. #endif