popupMenu.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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/popupMenu.h"
  23. #include "console/consoleTypes.h"
  24. #include "gui/core/guiCanvas.h"
  25. #include "core/util/safeDelete.h"
  26. static U32 sMaxPopupGUID = 0;
  27. PopupMenuEvent PopupMenu::smPopupMenuEvent;
  28. bool PopupMenu::smSelectionEventHandled = false;
  29. /// Event class used to remove popup menus from the event notification in a safe way
  30. class PopUpNotifyRemoveEvent : public SimEvent
  31. {
  32. public:
  33. void process(SimObject *object)
  34. {
  35. PopupMenu::smPopupMenuEvent.remove((PopupMenu *)object, &PopupMenu::handleSelectEvent);
  36. }
  37. };
  38. //-----------------------------------------------------------------------------
  39. // Constructor/Destructor
  40. //-----------------------------------------------------------------------------
  41. PopupMenu::PopupMenu() : mCanvas(NULL)
  42. {
  43. createPlatformPopupMenuData();
  44. mSubmenus = new SimSet;
  45. mSubmenus->registerObject();
  46. mBarTitle = StringTable->insert("");
  47. mIsPopup = false;
  48. mPopupGUID = sMaxPopupGUID++;
  49. }
  50. PopupMenu::~PopupMenu()
  51. {
  52. // This searches the menu bar so is safe to call for menus
  53. // that aren't on it, since nothing will happen.
  54. removeFromMenuBar();
  55. SimSet::iterator i;
  56. while((i = mSubmenus->begin()) != mSubmenus->end())
  57. {
  58. (*i)->deleteObject();
  59. }
  60. mSubmenus->deleteObject();
  61. deletePlatformPopupMenuData();
  62. PopupMenu::smPopupMenuEvent.remove(this, &PopupMenu::handleSelectEvent);
  63. }
  64. IMPLEMENT_CONOBJECT(PopupMenu);
  65. ConsoleDocClass( PopupMenu,
  66. "@brief PopupMenu represents a system menu.\n\n"
  67. "You can add menu items to the menu, but there is no torque object associated "
  68. "with these menu items, they exist only in a platform specific manner.\n\n"
  69. "@note Internal use only\n\n"
  70. "@internal"
  71. );
  72. //-----------------------------------------------------------------------------
  73. void PopupMenu::initPersistFields()
  74. {
  75. addField("isPopup", TypeBool, Offset(mIsPopup, PopupMenu), "true if this is a pop-up/context menu. defaults to false.");
  76. addField("barTitle", TypeCaseString, Offset(mBarTitle, PopupMenu), "the title of this menu when attached to a menu bar");
  77. Parent::initPersistFields();
  78. }
  79. //-----------------------------------------------------------------------------
  80. bool PopupMenu::onAdd()
  81. {
  82. if(! Parent::onAdd())
  83. return false;
  84. createPlatformMenu();
  85. Con::executef(this, "onAdd");
  86. return true;
  87. }
  88. void PopupMenu::onRemove()
  89. {
  90. Con::executef(this, "onRemove");
  91. Parent::onRemove();
  92. }
  93. //-----------------------------------------------------------------------------
  94. void PopupMenu::onMenuSelect()
  95. {
  96. Con::executef(this, "onMenuSelect");
  97. }
  98. //-----------------------------------------------------------------------------
  99. void PopupMenu::handleSelectEvent(U32 popID, U32 command)
  100. {
  101. if (popID == mPopupGUID && canHandleID(command))
  102. if (handleSelect(command))
  103. smSelectionEventHandled = true;
  104. }
  105. //-----------------------------------------------------------------------------
  106. void PopupMenu::onAttachToMenuBar(GuiCanvas *canvas, S32 pos, const char *title)
  107. {
  108. mCanvas = canvas;
  109. // Attached menus must be notified of menu events
  110. smPopupMenuEvent.notify(this, &PopupMenu::handleSelectEvent);
  111. // Pass on to sub menus
  112. for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i)
  113. {
  114. PopupMenu *mnu = dynamic_cast<PopupMenu *>(*i);
  115. if(mnu == NULL)
  116. continue;
  117. mnu->onAttachToMenuBar(canvas, pos, title);
  118. }
  119. // Call script
  120. if(isProperlyAdded())
  121. Con::executef(this, "onAttachToMenuBar", Con::getIntArg(canvas ? canvas->getId() : 0), Con::getIntArg(pos), title);
  122. }
  123. void PopupMenu::onRemoveFromMenuBar(GuiCanvas *canvas)
  124. {
  125. mCanvas = NULL;
  126. // We are no longer interested in select events, remove ourselves from the notification list in a safe way
  127. Sim::postCurrentEvent(this, new PopUpNotifyRemoveEvent());
  128. // Pass on to sub menus
  129. for(SimSet::iterator i = mSubmenus->begin();i != mSubmenus->end();++i)
  130. {
  131. PopupMenu *mnu = dynamic_cast<PopupMenu *>(*i);
  132. if(mnu == NULL)
  133. continue;
  134. mnu->onRemoveFromMenuBar(canvas);
  135. }
  136. // Call script
  137. if(isProperlyAdded())
  138. Con::executef(this, "onRemoveFromMenuBar", Con::getIntArg(canvas ? canvas->getId() : 0));
  139. }
  140. //-----------------------------------------------------------------------------
  141. bool PopupMenu::onMessageReceived(StringTableEntry queue, const char* event, const char* data)
  142. {
  143. return Con::executef(this, "onMessageReceived", queue, event, data);
  144. }
  145. bool PopupMenu::onMessageObjectReceived(StringTableEntry queue, Message *msg )
  146. {
  147. return Con::executef(this, "onMessageReceived", queue, Con::getIntArg(msg->getId()));
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Console Methods
  151. //-----------------------------------------------------------------------------
  152. ConsoleMethod(PopupMenu, insertItem, S32, 3, 5, "(pos[, title][, accelerator])")
  153. {
  154. return object->insertItem(dAtoi(argv[2]), argc < 4 ? NULL : argv[3], argc < 5 ? "" : argv[4]);
  155. }
  156. ConsoleMethod(PopupMenu, removeItem, void, 3, 3, "(pos)")
  157. {
  158. object->removeItem(dAtoi(argv[2]));
  159. }
  160. ConsoleMethod(PopupMenu, insertSubMenu, S32, 5, 5, "(pos, title, subMenu)")
  161. {
  162. PopupMenu *mnu = dynamic_cast<PopupMenu *>(Sim::findObject(argv[4]));
  163. if(mnu == NULL)
  164. {
  165. Con::errorf("PopupMenu::insertSubMenu - Invalid PopupMenu object specified for submenu");
  166. return -1;
  167. }
  168. return object->insertSubMenu(dAtoi(argv[2]), argv[3], mnu);
  169. }
  170. ConsoleMethod(PopupMenu, setItem, bool, 4, 5, "(pos, title[, accelerator])")
  171. {
  172. return object->setItem(dAtoi(argv[2]), argv[3], argc < 5 ? "" : argv[4]);
  173. }
  174. //-----------------------------------------------------------------------------
  175. ConsoleMethod(PopupMenu, enableItem, void, 4, 4, "(pos, enabled)")
  176. {
  177. object->enableItem(dAtoi(argv[2]), dAtob(argv[3]));
  178. }
  179. ConsoleMethod(PopupMenu, checkItem, void, 4, 4, "(pos, checked)")
  180. {
  181. object->checkItem(dAtoi(argv[2]), dAtob(argv[3]));
  182. }
  183. ConsoleMethod(PopupMenu, checkRadioItem, void, 5, 5, "(firstPos, lastPos, checkPos)")
  184. {
  185. object->checkRadioItem(dAtoi(argv[2]), dAtoi(argv[3]), dAtoi(argv[4]));
  186. }
  187. ConsoleMethod(PopupMenu, isItemChecked, bool, 3, 3, "(pos)")
  188. {
  189. return object->isItemChecked(dAtoi(argv[2]));
  190. }
  191. ConsoleMethod(PopupMenu, getItemCount, S32, 2, 2, "()")
  192. {
  193. return object->getItemCount();
  194. }
  195. //-----------------------------------------------------------------------------
  196. ConsoleMethod(PopupMenu, attachToMenuBar, void, 5, 5, "(GuiCanvas, pos, title)")
  197. {
  198. object->attachToMenuBar(dynamic_cast<GuiCanvas*>(Sim::findObject(argv[2])),dAtoi(argv[3]), argv[4]);
  199. }
  200. ConsoleMethod(PopupMenu, removeFromMenuBar, void, 2, 2, "()")
  201. {
  202. object->removeFromMenuBar();
  203. }
  204. //-----------------------------------------------------------------------------
  205. ConsoleMethod(PopupMenu, showPopup, void, 3, 5, "(Canvas,[x, y])")
  206. {
  207. GuiCanvas *pCanvas = dynamic_cast<GuiCanvas*>(Sim::findObject(argv[2]));
  208. S32 x = argc >= 4 ? dAtoi(argv[3]) : -1;
  209. S32 y = argc >= 5 ? dAtoi(argv[4]) : -1;
  210. object->showPopup(pCanvas, x, y);
  211. }