popupMenu.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #ifndef _POPUPMENU_H_
  23. #define _POPUPMENU_H_
  24. #include "console/simBase.h"
  25. #include "core/util/tVector.h"
  26. #include "util/messaging/dispatcher.h"
  27. #include "gui/core/guiCanvas.h"
  28. class PopupMenu;
  29. class GuiMenuBar;
  30. class GuiPopupMenuTextListCtrl;
  31. class GuiPopupMenuBackgroundCtrl;
  32. struct MenuItem // an individual item in a pull-down menu
  33. {
  34. String mText; // the text of the menu item
  35. U32 mID; // a script-assigned identifier
  36. char *mAccelerator; // the keyboard accelerator shortcut for the menu item
  37. U32 mAcceleratorIndex; // index of this accelerator
  38. bool mEnabled; // true if the menu item is selectable
  39. bool mVisible; // true if the menu item is visible
  40. S32 mBitmapIndex; // index of the bitmap in the bitmap array
  41. S32 mCheckGroup; // the group index of the item visa vi check marks -
  42. // only one item in the group can be checked.
  43. bool mIsSubmenu; // This menu item has a submenu that will be displayed
  44. bool mIsChecked;
  45. bool mIsSpacer;
  46. bool mIsMenubarEntry;
  47. PopupMenu* mSubMenuParentMenu; // For a submenu, this is the parent menu
  48. PopupMenu* mSubMenu;
  49. String mCMD;
  50. };
  51. // PopupMenu represents a menu.
  52. // You can add menu items to the menu, but there is no torque object associated
  53. // with these menu items, they exist only in a platform specific manner.
  54. class PopupMenu : public SimObject, public virtual Dispatcher::IMessageListener
  55. {
  56. typedef SimObject Parent;
  57. friend class GuiMenuBar;
  58. friend class GuiPopupMenuTextListCtrl;
  59. friend class GuiPopupMenuBackgroundCtrl;
  60. protected:
  61. Vector<MenuItem> mMenuItems;
  62. GuiMenuBar* mMenuBarCtrl;
  63. StringTableEntry mBarTitle;
  64. RectI mBounds;
  65. bool mVisible;
  66. S32 mBitmapIndex; // Index of the bitmap in the bitmap array (-1 = no bitmap)
  67. bool mDrawBitmapOnly; // Draw only the bitmap and not the text
  68. bool mDrawBorder; // Should a border be drawn around this menu (usually if we only have a bitmap, we don't want a border)
  69. bool mIsSubmenu;
  70. bool mRadioSelection; ///If true, we treat all the items in the same check group as a radio item, so we uncheck everything else in the group if an item is checked
  71. ///If false, then we don't clear other selections
  72. //This is the gui control that renders our popup
  73. GuiPopupMenuTextListCtrl *mTextList;
  74. public:
  75. PopupMenu();
  76. virtual ~PopupMenu();
  77. DECLARE_CONOBJECT(PopupMenu);
  78. static void initPersistFields();
  79. virtual bool onAdd();
  80. virtual void onRemove();
  81. static PopupMenuEvent smPopupMenuEvent;
  82. static bool smSelectionEventHandled; /// Set to true if any menu or submenu handles a selection event
  83. /// pass NULL for @p title to insert a separator
  84. /// returns the menu item's ID, or -1 on failure.
  85. /// implementd on a per-platform basis.
  86. /// TODO: factor out common code
  87. S32 insertItem(S32 pos, const char *title, const char* accelerator, const char* cmd, S32 bitmapIndex = -1);
  88. /// Sets the name title and accelerator for
  89. /// an existing item.
  90. bool setItem(S32 pos, const char *title, const char* accelerator, const char* cmd);
  91. /// pass NULL for @p title to insert a separator
  92. /// returns the menu item's ID, or -1 on failure.
  93. /// adds the submenu to the mSubmenus vector.
  94. /// implemented on a per-platform basis.
  95. /// TODO: factor out common code
  96. S32 insertSubMenu(S32 pos, const char *title, PopupMenu *submenu);
  97. /// remove the menu item at @p itemPos
  98. /// if the item has a submenu, it is removed from the mSubmenus list.
  99. /// implemented on a per-platform basis.
  100. /// TODO: factor out common code
  101. void removeItem(S32 itemPos);
  102. /// implemented on a per-platform basis.
  103. void enableItem(S32 pos, bool enable);
  104. /// implemented on a per-platform basis.
  105. void checkItem(S32 pos, bool checked);
  106. /// All items at positions firstPos through lastPos are unchecked, and the
  107. /// item at checkPos is checked.
  108. /// implemented on a per-platform basis.
  109. void checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos);
  110. bool isItemChecked(S32 pos);
  111. /// Returns the number of items in the menu.
  112. U32 getItemCount();
  113. ///Clears all items
  114. void clearItems();
  115. ///Gets the text of a given item
  116. String getItemText(S32 pos);
  117. //-----------------------------------------------------------------------------
  118. /// Displays this menu as a popup menu and blocks until the user has selected
  119. /// an item.
  120. /// @param canvas the owner to show this popup associated with
  121. /// @param x window local x coordinate at which to display the popup menu
  122. /// @param y window local y coordinate at which to display the popup menu
  123. /// implemented on a per-platform basis.
  124. void showPopup(GuiCanvas *owner, S32 x = -1, S32 y = -1);
  125. void hidePopup();
  126. void hidePopupSubmenus();
  127. /// Returns true iff this menu contains an item that matches @p iD.
  128. /// implemented on a per-platform basis.
  129. /// TODO: factor out common code
  130. bool canHandleID(U32 iD);
  131. /// A menu item in this menu has been selected by id.
  132. /// Submenus are given a chance to respond to the command first.
  133. /// If no submenu can handle the command id, this menu handles it.
  134. /// The script callback this::onSelectItem( position, text) is called.
  135. /// If @p text is null, then the text arg passed to script is the text of
  136. /// the selected menu item.
  137. /// implemented on a per-platform basis.
  138. /// TODO: factor out common code
  139. bool handleSelect(U32 command, const char *text = NULL);
  140. void onMenuSelect();
  141. /// Helper function to allow menu selections from signal events.
  142. /// Wraps canHandleID() and handleSelect() in one function
  143. /// without changing their internal functionality, so
  144. /// it should work regardless of platform.
  145. void handleSelectEvent(U32 popID, U32 command);
  146. virtual bool onMessageReceived(StringTableEntry queue, const char* event, const char* data );
  147. virtual bool onMessageObjectReceived(StringTableEntry queue, Message *msg );
  148. bool isVisible() { return mVisible; }
  149. void setVisible(bool isVis) { mVisible = isVis; }
  150. GuiMenuBar* getMenuBarCtrl();
  151. };
  152. #endif // _POPUPMENU_H_