popupMenu.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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/guiCanvas.h"
  25. #include "memory/safeDelete.h"
  26. //////////////////////////////////////////////////////////////////////////
  27. // Constructor/Destructor
  28. //////////////////////////////////////////////////////////////////////////
  29. PopupMenu::PopupMenu()
  30. {
  31. createPlatformPopupMenuData();
  32. mSubmenus = new SimSet;
  33. mSubmenus->registerObject();
  34. mIsPopup = false;
  35. }
  36. PopupMenu::~PopupMenu()
  37. {
  38. // This searches the menu bar so is safe to call for menus
  39. // that aren't on it, since nothing will happen.
  40. removeFromMenuBar();
  41. SimSet::iterator i;
  42. while((i = mSubmenus->begin()) != mSubmenus->end())
  43. {
  44. (*i)->deleteObject();
  45. }
  46. mSubmenus->deleteObject();
  47. deletePlatformPopupMenuData();
  48. }
  49. IMPLEMENT_CONOBJECT(PopupMenu);
  50. //////////////////////////////////////////////////////////////////////////
  51. void PopupMenu::initPersistFields()
  52. {
  53. Parent::initPersistFields();
  54. // [neo, 5/7/2007 - #2973]
  55. // isPopup field corrected to TypeBool instead of TypeString which caused
  56. // a crash when reading field value e.g. in dump()
  57. addField( "isPopup", TypeBool, Offset(mIsPopup, PopupMenu));
  58. }
  59. //////////////////////////////////////////////////////////////////////////
  60. bool PopupMenu::onAdd()
  61. {
  62. if(! Parent::onAdd())
  63. return false;
  64. createPlatformMenu();
  65. return true;
  66. }
  67. void PopupMenu::onRemove()
  68. {
  69. Parent::onRemove();
  70. }
  71. //////////////////////////////////////////////////////////////////////////
  72. bool PopupMenu::onMessageReceived(StringTableEntry queue, const char* event, const char* data)
  73. {
  74. return Con::executef(this, 4, "onMessageReceived", queue, event, data);
  75. }
  76. bool PopupMenu::onMessageObjectReceived(StringTableEntry queue, Message *msg )
  77. {
  78. return Con::executef(this, 4, "onMessageReceived", queue, Con::getIntArg(msg->getId()));
  79. }
  80. //////////////////////////////////////////////////////////////////////////
  81. // Console Methods
  82. //////////////////////////////////////////////////////////////////////////
  83. ConsoleMethod(PopupMenu, insertItem, S32, 3, 5, "(pos[, title][, accelerator) Insert a new menu item\n"
  84. "@param pos The position of the item\n"
  85. "@param title The title of the item to add (optional)\n"
  86. "@param accelerator A keyboard accelerator for the menu item\n"
  87. "@return Returns the id of the new menu item as a nonnegative integer (-1 on fail)")
  88. {
  89. return object->insertItem(dAtoi(argv[2]), argc < 4 ? NULL : argv[3], argc < 5 ? "" : argv[4]);
  90. }
  91. ConsoleMethod(PopupMenu, removeItem, void, 3, 3, "(pos) Removes the menu item at the given position\n"
  92. "@param pos The position of the desired menu item to obliterate\n"
  93. "@return No return value")
  94. {
  95. object->removeItem(dAtoi(argv[2]));
  96. }
  97. ConsoleMethod(PopupMenu, insertSubMenu, S32, 5, 5, "(pos, title, subMenu) Inserts a menu item into the current object\n"
  98. "@param pos The position of the menu item\n"
  99. "@param The desired title of the sub menu\n"
  100. "@param subMenu The submenu you are inserting.\n"
  101. "@return Returns the ID of the inserted item or -1 on failure")
  102. {
  103. PopupMenu *mnu = dynamic_cast<PopupMenu *>(Sim::findObject(argv[4]));
  104. if(mnu == NULL)
  105. {
  106. Con::errorf("PopupMenu::insertSubMenu - Invalid PopupMenu object specified for submenu");
  107. return -1;
  108. }
  109. return object->insertSubMenu(dAtoi(argv[2]), argv[3], mnu);
  110. }
  111. //////////////////////////////////////////////////////////////////////////
  112. ConsoleMethod(PopupMenu, enableItem, void, 4, 4, "(pos, enabled) Enables the item at the given position\n"
  113. "@param pos The item's position\n"
  114. "@param enabled A boolean value to set its current status\n"
  115. "@return No return value")
  116. {
  117. object->enableItem(dAtoi(argv[2]), dAtob(argv[3]));
  118. }
  119. ConsoleMethod(PopupMenu, checkItem, void, 4, 4, "(pos, checked) Sets the given item's \"checked\" status\n"
  120. "@param pos The item's position\n"
  121. "@return No return value")
  122. {
  123. object->checkItem(dAtoi(argv[2]), dAtob(argv[3]));
  124. }
  125. ConsoleMethod(PopupMenu, checkRadioItem, void, 5, 5, "(firstPos, lastPos, checkPos) Checks the specified menu item and makes it a radio item\n"
  126. "@param firstPos The position of the first item in the group\n"
  127. "@param lastPos The position of the last item in the group\n"
  128. "@param checkPos The position of the item to check\n"
  129. "@return No return value.")
  130. {
  131. object->checkRadioItem(dAtoi(argv[2]), dAtoi(argv[3]), dAtoi(argv[4]));
  132. }
  133. ConsoleMethod(PopupMenu, isItemChecked, bool, 3, 3, "(pos) Checks (no pun intended) the \"checked\" flag of the given menu item\n"
  134. "@param pos The position of the desired item\n"
  135. "@return Returns true if checked, false otherwise")
  136. {
  137. return object->isItemChecked(dAtoi(argv[2]));
  138. }
  139. //////////////////////////////////////////////////////////////////////////
  140. ConsoleMethod(PopupMenu, attachToMenuBar, void, 4, 4, "(pos, title) Attaches given menu item to menu bar\n"
  141. "@param pos The position of the desired menu item\n"
  142. "@param title The menu item title\n"
  143. "@return No return value")
  144. {
  145. object->attachToMenuBar(dAtoi(argv[2]), argv[3]);
  146. }
  147. ConsoleMethod(PopupMenu, removeFromMenuBar, void, 2, 2, "() Removes current item from menu bar"
  148. "@return No return value")
  149. {
  150. object->removeFromMenuBar();
  151. }
  152. //////////////////////////////////////////////////////////////////////////
  153. ConsoleMethod(PopupMenu, showPopup, void, 2, 4, "([x, y]) Show the menu (optional: specify position)\n"
  154. "@param x,y Coodinates to display menu (upper left corner)\n"
  155. "@return No return value.")
  156. {
  157. S32 x = argc >= 3 ? dAtoi(argv[2]) : -1;
  158. S32 y = argc >= 4 ? dAtoi(argv[3]) : -1;
  159. object->showPopup(x, y);
  160. }