menuBarWin32.cpp 4.8 KB

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