popupMenuSDL.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. #ifdef TORQUE_SDL
  23. #include "platform/menus/popupMenu.h"
  24. #include "platform/menus//menuBar.h"
  25. #include "console/consoleTypes.h"
  26. #include "gui/core/guiCanvas.h"
  27. #include "core/util/safeDelete.h"
  28. #include "sim/actionMap.h"
  29. #include "platform/platformInput.h"
  30. #include "windowManager/sdl/sdlWindow.h"
  31. #include "gui/editor/guiMenuBar.h"
  32. #include "platformSDL/menus/PlatformSDLPopupMenuData.h"
  33. //////////////////////////////////////////////////////////////////////////
  34. // Platform Menu Data
  35. //////////////////////////////////////////////////////////////////////////
  36. //////////////////////////////////////////////////////////////////////////
  37. void PlatformPopupMenuData::insertAccelerator(EventDescriptor &desc, U32 id)
  38. {
  39. AssertFatal(0, "");
  40. }
  41. void PlatformPopupMenuData::removeAccelerator(U32 id)
  42. {
  43. AssertFatal(0, "");
  44. }
  45. void PlatformPopupMenuData::setAccelleratorEnabled( U32 id, bool enabled )
  46. {
  47. AssertFatal(0, "");
  48. }
  49. //////////////////////////////////////////////////////////////////////////
  50. void PopupMenu::createPlatformPopupMenuData()
  51. {
  52. mData = new PlatformPopupMenuData;
  53. }
  54. void PopupMenu::deletePlatformPopupMenuData()
  55. {
  56. SAFE_DELETE(mData);
  57. }
  58. void PopupMenu::createPlatformMenu()
  59. {
  60. mData->mMenuGui = GuiMenuBar::sCreateMenu( getBarTitle(), getId() );
  61. PlatformPopupMenuData::mMenuMap[ mData->mMenuGui ] = this;
  62. }
  63. //////////////////////////////////////////////////////////////////////////
  64. // Public Methods
  65. //////////////////////////////////////////////////////////////////////////
  66. S32 PopupMenu::insertItem(S32 pos, const char *title, const char* accelerator, const char* cmd)
  67. {
  68. GuiMenuBar::MenuItem *item = GuiMenuBar::findMenuItem( mData->mMenuGui, title );
  69. if(item)
  70. {
  71. setItem( pos, title, accelerator, cmd);
  72. return pos;
  73. }
  74. item = GuiMenuBar::addMenuItem( mData->mMenuGui, title, pos, accelerator, -1, cmd );
  75. item->submenuParentMenu = this->mData->mMenuGui;
  76. return pos;
  77. }
  78. S32 PopupMenu::insertSubMenu(S32 pos, const char *title, PopupMenu *submenu)
  79. {
  80. GuiMenuBar::MenuItem *item = GuiMenuBar::addMenuItem( mData->mMenuGui, title, pos, "", -1, "" );
  81. item->isSubmenu = true;
  82. item->submenu = submenu->mData->mMenuGui;
  83. item->submenuParentMenu = this->mData->mMenuGui;
  84. return pos;
  85. }
  86. bool PopupMenu::setItem(S32 pos, const char *title, const char* accelerator, const char* cmd)
  87. {
  88. GuiMenuBar::MenuItem *item = NULL;
  89. item = GuiMenuBar::findMenuItem( mData->mMenuGui, title );
  90. if(item)
  91. {
  92. item->id = pos;
  93. item->cmd = cmd;
  94. if( accelerator && accelerator[0] )
  95. item->accelerator = dStrdup( accelerator );
  96. else
  97. item->accelerator = NULL;
  98. return true;
  99. }
  100. return false;
  101. }
  102. void PopupMenu::removeItem(S32 itemPos)
  103. {
  104. GuiMenuBar::MenuItem *item = GuiMenuBar::findMenuItem( mData->mMenuGui, String::ToString(itemPos) );
  105. if(item)
  106. {
  107. GuiMenuBar::removeMenuItem( mData->mMenuGui, item);
  108. }
  109. }
  110. //////////////////////////////////////////////////////////////////////////
  111. void PopupMenu::enableItem( S32 pos, bool enable )
  112. {
  113. GuiMenuBar::MenuItem *item = NULL;
  114. for( item = mData->mMenuGui->firstMenuItem; item; item = item->nextMenuItem )
  115. {
  116. if( item->id == pos)
  117. item->enabled = enable;
  118. }
  119. }
  120. void PopupMenu::checkItem(S32 pos, bool checked)
  121. {
  122. GuiMenuBar::MenuItem *item = NULL;
  123. for( item = mData->mMenuGui->firstMenuItem; item; item = item->nextMenuItem )
  124. if(item->id == pos)
  125. break;
  126. if( !item )
  127. return;
  128. if(checked && item->checkGroup != -1)
  129. {
  130. // first, uncheck everything in the group:
  131. for( GuiMenuBar::MenuItem *itemWalk = mData->mMenuGui->firstMenuItem; itemWalk; itemWalk = itemWalk->nextMenuItem )
  132. if( itemWalk->checkGroup == item->checkGroup && itemWalk->bitmapIndex == mData->mCheckedBitmapIdx )
  133. itemWalk->bitmapIndex = -1;
  134. }
  135. item->bitmapIndex = checked ? mData->mCheckedBitmapIdx : -1;
  136. }
  137. void PopupMenu::checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos)
  138. {
  139. GuiMenuBar::MenuItem *item = NULL;
  140. for( item = mData->mMenuGui->firstMenuItem; item; item = item->nextMenuItem )
  141. {
  142. if(item->id >= firstPos && item->id <= lastPos)
  143. {
  144. item->bitmapIndex = (item->id == checkPos) ? mData->mCheckedBitmapIdx : -1;
  145. }
  146. }
  147. }
  148. bool PopupMenu::isItemChecked(S32 pos)
  149. {
  150. GuiMenuBar::MenuItem *item = NULL;
  151. for( item = mData->mMenuGui->firstMenuItem; item; item = item->nextMenuItem )
  152. if(item->id == pos)
  153. return item->bitmapIndex == mData->mCheckedBitmapIdx;
  154. return false;
  155. }
  156. U32 PopupMenu::getItemCount()
  157. {
  158. int count = 0;
  159. for( GuiMenuBar::MenuItem *item = mData->mMenuGui->firstMenuItem; item; item = item->nextMenuItem )
  160. ++count;
  161. return count;
  162. }
  163. //////////////////////////////////////////////////////////////////////////
  164. bool PopupMenu::canHandleID(U32 id)
  165. {
  166. return true;
  167. }
  168. bool PopupMenu::handleSelect(U32 command, const char *text /* = NULL */)
  169. {
  170. return dAtob(Con::executef(this, "onSelectItem", Con::getIntArg(command), text ? text : ""));
  171. }
  172. //////////////////////////////////////////////////////////////////////////
  173. void PopupMenu::showPopup(GuiCanvas *owner, S32 x /* = -1 */, S32 y /* = -1 */)
  174. {
  175. if(owner == NULL || isAttachedToMenuBar())
  176. return;
  177. }
  178. //////////////////////////////////////////////////////////////////////////
  179. void PopupMenu::attachToMenuBar(GuiCanvas *owner, S32 pos, const char *title)
  180. {
  181. if(owner == NULL || isAttachedToMenuBar())
  182. return;
  183. }
  184. // New version of above for use by MenuBar class. Do not use yet.
  185. void PopupMenu::attachToMenuBar(GuiCanvas *owner, S32 pos)
  186. {
  187. if(owner == NULL || isAttachedToMenuBar())
  188. return;
  189. //mData->mMenuBar = owner->setMenuBar();
  190. }
  191. void PopupMenu::removeFromMenuBar()
  192. {
  193. if(isAttachedToMenuBar())
  194. return;
  195. }
  196. S32 PopupMenu::getPosOnMenuBar()
  197. {
  198. return 0;
  199. }
  200. #endif