popupMenuSDL.cpp 7.1 KB

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