menuBarWin32.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "platformWin32/platformWin32.h"
  23. #include "platform/menus/menuBar.h"
  24. #include "platform/menus/popupMenu.h"
  25. #include "gui/core/guiCanvas.h"
  26. #include "windowManager/platformWindowMgr.h"
  27. #include "windowManager/win32/win32Window.h"
  28. #include "core/util/safeDelete.h"
  29. //-----------------------------------------------------------------------------
  30. // Platform Data
  31. //-----------------------------------------------------------------------------
  32. // class PlatformMenuBarData
  33. // {
  34. //
  35. // };
  36. //-----------------------------------------------------------------------------
  37. // MenuBar Methods
  38. //-----------------------------------------------------------------------------
  39. #ifndef TORQUE_SDL
  40. void MenuBar::createPlatformPopupMenuData()
  41. {
  42. // mData = new PlatformMenuBarData;
  43. // [tom, 6/4/2007] Nothing currently needed for win32
  44. mData = NULL;
  45. }
  46. void MenuBar::deletePlatformPopupMenuData()
  47. {
  48. // SAFE_DELETE(mData);
  49. }
  50. //-----------------------------------------------------------------------------
  51. void MenuBar::updateMenuBar(PopupMenu *menu /* = NULL */)
  52. {
  53. if(! isAttachedToCanvas())
  54. return;
  55. if(menu == NULL)
  56. {
  57. // [tom, 6/4/2007] Kludgetastic
  58. GuiCanvas *oldCanvas = mCanvas;
  59. S32 pos = -1;
  60. PopupMenu *mnu = dynamic_cast<PopupMenu *>(at(0));
  61. if(mnu)
  62. pos = mnu->getPosOnMenuBar();
  63. removeFromCanvas();
  64. attachToCanvas(oldCanvas, pos);
  65. return;
  66. }
  67. menu->removeFromMenuBar();
  68. SimSet::iterator itr = find(begin(), end(), menu);
  69. if(itr == end())
  70. return;
  71. menu->attachToMenuBar(mCanvas, itr - begin());
  72. Win32Window *pWindow = dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow());
  73. if(pWindow == NULL)
  74. return;
  75. HWND hWindow = pWindow->getHWND();
  76. DrawMenuBar(hWindow);
  77. }
  78. //-----------------------------------------------------------------------------
  79. void MenuBar::attachToCanvas(GuiCanvas *owner, S32 pos)
  80. {
  81. if(owner == NULL || isAttachedToCanvas())
  82. return;
  83. // This is set for popup menus in the onAttachToMenuBar() callback
  84. mCanvas = owner;
  85. Win32Window *pWindow = dynamic_cast<Win32Window*>(owner->getPlatformWindow());
  86. if(pWindow == NULL)
  87. return;
  88. // Setup the native menu bar
  89. HMENU hWindowMenu = pWindow->getMenuHandle();
  90. if(hWindowMenu == NULL && !Journal::IsPlaying())
  91. {
  92. hWindowMenu = CreateMenu();
  93. if(hWindowMenu)
  94. {
  95. pWindow->setMenuHandle( hWindowMenu);
  96. }
  97. }
  98. // Add the items
  99. for(S32 i = 0;i < size();++i)
  100. {
  101. PopupMenu *mnu = dynamic_cast<PopupMenu *>(at(i));
  102. if(mnu == NULL)
  103. {
  104. Con::warnf("MenuBar::attachToMenuBar - Non-PopupMenu object in set");
  105. continue;
  106. }
  107. if(mnu->isAttachedToMenuBar())
  108. mnu->removeFromMenuBar();
  109. mnu->attachToMenuBar(owner, pos + i);
  110. }
  111. HWND hWindow = pWindow->getHWND();
  112. SetMenu(hWindow, hWindowMenu);
  113. DrawMenuBar(hWindow);
  114. }
  115. void MenuBar::removeFromCanvas()
  116. {
  117. if(mCanvas == NULL || ! isAttachedToCanvas())
  118. return;
  119. Win32Window *pWindow = dynamic_cast<Win32Window*>(mCanvas->getPlatformWindow());
  120. if(pWindow == NULL)
  121. return;
  122. // Setup the native menu bar
  123. HMENU hWindowMenu = pWindow->getMenuHandle();
  124. if(hWindowMenu == NULL)
  125. return;
  126. // Add the items
  127. for(S32 i = 0;i < size();++i)
  128. {
  129. PopupMenu *mnu = dynamic_cast<PopupMenu *>(at(i));
  130. if(mnu == NULL)
  131. {
  132. Con::warnf("MenuBar::removeFromMenuBar - Non-PopupMenu object in set");
  133. continue;
  134. }
  135. mnu->removeFromMenuBar();
  136. }
  137. HWND hWindow = pWindow->getHWND();
  138. SetMenu(hWindow, NULL);
  139. DrawMenuBar(hWindow);
  140. mCanvas = NULL;
  141. }
  142. #endif