popupMenu.cpp 8.5 KB

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