popupMenu.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. //This is the gui control that renders our popup
  71. GuiPopupMenuTextListCtrl *mTextList;
  72. public:
  73. PopupMenu();
  74. virtual ~PopupMenu();
  75. DECLARE_CONOBJECT(PopupMenu);
  76. static void initPersistFields();
  77. virtual bool onAdd();
  78. virtual void onRemove();
  79. static PopupMenuEvent smPopupMenuEvent;
  80. static bool smSelectionEventHandled; /// Set to true if any menu or submenu handles a selection event
  81. /// pass NULL for @p title to insert a separator
  82. /// returns the menu item's ID, or -1 on failure.
  83. /// implementd on a per-platform basis.
  84. /// TODO: factor out common code
  85. S32 insertItem(S32 pos, const char *title, const char* accelerator, const char* cmd);
  86. /// Sets the name title and accelerator for
  87. /// an existing item.
  88. bool setItem(S32 pos, const char *title, const char* accelerator, const char* cmd);
  89. /// pass NULL for @p title to insert a separator
  90. /// returns the menu item's ID, or -1 on failure.
  91. /// adds the submenu to the mSubmenus vector.
  92. /// implemented on a per-platform basis.
  93. /// TODO: factor out common code
  94. S32 insertSubMenu(S32 pos, const char *title, PopupMenu *submenu);
  95. /// remove the menu item at @p itemPos
  96. /// if the item has a submenu, it is removed from the mSubmenus list.
  97. /// implemented on a per-platform basis.
  98. /// TODO: factor out common code
  99. void removeItem(S32 itemPos);
  100. /// implemented on a per-platform basis.
  101. void enableItem(S32 pos, bool enable);
  102. /// implemented on a per-platform basis.
  103. void checkItem(S32 pos, bool checked);
  104. /// All items at positions firstPos through lastPos are unchecked, and the
  105. /// item at checkPos is checked.
  106. /// implemented on a per-platform basis.
  107. void checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos);
  108. bool isItemChecked(S32 pos);
  109. /// Returns the number of items in the menu.
  110. U32 getItemCount();
  111. ///Clears all items
  112. void clearItems();
  113. //-----------------------------------------------------------------------------
  114. /// Displays this menu as a popup menu and blocks until the user has selected
  115. /// an item.
  116. /// @param canvas the owner to show this popup associated with
  117. /// @param x window local x coordinate at which to display the popup menu
  118. /// @param y window local y coordinate at which to display the popup menu
  119. /// implemented on a per-platform basis.
  120. void showPopup(GuiCanvas *owner, S32 x = -1, S32 y = -1);
  121. void hidePopup();
  122. void hidePopupSubmenus();
  123. /// Returns true iff this menu contains an item that matches @p iD.
  124. /// implemented on a per-platform basis.
  125. /// TODO: factor out common code
  126. bool canHandleID(U32 iD);
  127. /// A menu item in this menu has been selected by id.
  128. /// Submenus are given a chance to respond to the command first.
  129. /// If no submenu can handle the command id, this menu handles it.
  130. /// The script callback this::onSelectItem( position, text) is called.
  131. /// If @p text is null, then the text arg passed to script is the text of
  132. /// the selected menu item.
  133. /// implemented on a per-platform basis.
  134. /// TODO: factor out common code
  135. bool handleSelect(U32 command, const char *text = NULL);
  136. void onMenuSelect();
  137. /// Helper function to allow menu selections from signal events.
  138. /// Wraps canHandleID() and handleSelect() in one function
  139. /// without changing their internal functionality, so
  140. /// it should work regardless of platform.
  141. void handleSelectEvent(U32 popID, U32 command);
  142. virtual bool onMessageReceived(StringTableEntry queue, const char* event, const char* data );
  143. virtual bool onMessageObjectReceived(StringTableEntry queue, Message *msg );
  144. bool isVisible() { return mVisible; }
  145. void setVisible(bool isVis) { mVisible = isVis; }
  146. GuiMenuBar* getMenuBarCtrl();
  147. };
  148. #endif // _POPUPMENU_H_