menuBarSDL.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. AssertFatal( dStrcmp( popupMenu->mData->mMenuGui->text, popupMenu->getBarTitle() ) == 0, "");
  90. GuiMenuBar::Menu* menuGui = menuBarGui->findMenu( popupMenu->getBarTitle() );
  91. if(!menuGui)
  92. {
  93. menuBarGui->addMenu( popupMenu->mData->mMenuGui );
  94. menuGui = menuBarGui->findMenu( popupMenu->getBarTitle() );
  95. }
  96. PlatformPopupMenuData::mMenuMap[ menuGui ] = popupMenu;
  97. }
  98. //-----------------------------------------------------------------------------
  99. void MenuBar::attachToCanvas(GuiCanvas *owner, S32 pos)
  100. {
  101. if(owner == NULL || isAttachedToCanvas())
  102. return;
  103. // This is set for popup menus in the onAttachToMenuBar() callback
  104. mCanvas = owner;
  105. PlatformWindowSDL *pWindow = dynamic_cast<PlatformWindowSDL*>(owner->getPlatformWindow());
  106. if(pWindow == NULL)
  107. return;
  108. // Setup the native menu bar
  109. GuiMenuBar *hWindowMenu = static_cast<GuiMenuBar*>( pWindow->getMenuHandle() );
  110. if( hWindowMenu == NULL && !Journal::IsPlaying() )
  111. hWindowMenu = _FindMenuBarCtrl();
  112. if(hWindowMenu)
  113. {
  114. pWindow->setMenuHandle( hWindowMenu );
  115. GuiControl *base = hWindowMenu->getParent();
  116. while( base->getParent() )
  117. {
  118. base = base->getParent();
  119. }
  120. mCanvas->setMenuBar( base );
  121. }
  122. }
  123. void MenuBar::removeFromCanvas()
  124. {
  125. _FindMenuBarCtrl()->clearMenus();
  126. mCanvas->setMenuBar(NULL);
  127. if(mCanvas == NULL || !isAttachedToCanvas())
  128. return;
  129. }
  130. #endif