popupMenu.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 "console/simBase.h"
  23. #include "core/util/tVector.h"
  24. #include "util/messaging/dispatcher.h"
  25. #include "gui/core/guiCanvas.h"
  26. #ifndef _POPUPMENU_H_
  27. #define _POPUPMENU_H_
  28. // Forward ref used by the platform code
  29. struct PlatformPopupMenuData;
  30. class MenuBar;
  31. // PopupMenu represents a menu.
  32. // You can add menu items to the menu, but there is no torque object associated
  33. // with these menu items, they exist only in a platform specific manner.
  34. class PopupMenu : public SimObject, public virtual Dispatcher::IMessageListener
  35. {
  36. typedef SimObject Parent;
  37. friend class MenuBar;
  38. private:
  39. /// Used by MenuBar to attach the menu to the menu bar. Do not use anywhere else.
  40. void attachToMenuBar(GuiCanvas *owner, S32 pos);
  41. protected:
  42. PlatformPopupMenuData *mData;
  43. SimSet *mSubmenus;
  44. SimObjectPtr<GuiCanvas> mCanvas;
  45. StringTableEntry mBarTitle;
  46. U32 mPopupGUID;
  47. bool mIsPopup;
  48. public:
  49. PopupMenu();
  50. virtual ~PopupMenu();
  51. void createPlatformPopupMenuData();
  52. void deletePlatformPopupMenuData();
  53. DECLARE_CONOBJECT(PopupMenu);
  54. static void initPersistFields();
  55. virtual bool onAdd();
  56. virtual void onRemove();
  57. static PopupMenuEvent smPopupMenuEvent;
  58. static bool smSelectionEventHandled; /// Set to true if any menu or submenu handles a selection event
  59. /// Creates the platform specific menu object, a peer to this object.
  60. /// The platform menu *must* exist before calling any method that manipulates
  61. /// menu items or displays the menu.
  62. /// implementd on a per-platform basis.
  63. void createPlatformMenu();
  64. void setBarTitle(const char * val) { mBarTitle = StringTable->insert(val, true); }
  65. StringTableEntry getBarTitle() const { return mBarTitle; }
  66. /// pass NULL for @p title to insert a separator
  67. /// returns the menu item's ID, or -1 on failure.
  68. /// implementd on a per-platform basis.
  69. /// TODO: factor out common code
  70. S32 insertItem(S32 pos, const char *title, const char* accelerator, const char* cmd);
  71. /// Sets the name title and accelerator for
  72. /// an existing item.
  73. bool setItem(S32 pos, const char *title, const char* accelerator, const char* cmd);
  74. /// pass NULL for @p title to insert a separator
  75. /// returns the menu item's ID, or -1 on failure.
  76. /// adds the submenu to the mSubmenus vector.
  77. /// implemented on a per-platform basis.
  78. /// TODO: factor out common code
  79. S32 insertSubMenu(S32 pos, const char *title, PopupMenu *submenu);
  80. /// remove the menu item at @p itemPos
  81. /// if the item has a submenu, it is removed from the mSubmenus list.
  82. /// implemented on a per-platform basis.
  83. /// TODO: factor out common code
  84. void removeItem(S32 itemPos);
  85. /// implemented on a per-platform basis.
  86. void enableItem(S32 pos, bool enable);
  87. /// implemented on a per-platform basis.
  88. void checkItem(S32 pos, bool checked);
  89. /// All items at positions firstPos through lastPos are unchecked, and the
  90. /// item at checkPos is checked.
  91. /// implemented on a per-platform basis.
  92. void checkRadioItem(S32 firstPos, S32 lastPos, S32 checkPos);
  93. bool isItemChecked(S32 pos);
  94. /// Returns the number of items in the menu.
  95. U32 getItemCount();
  96. /// Returns the popup GUID
  97. U32 getPopupGUID() { return mPopupGUID; }
  98. //-----------------------------------------------------------------------------
  99. // New code should not use these methods directly, use the menu bar instead.
  100. //
  101. // They remain for compatibility with old code and will be changing/going away
  102. // once the existing code is moved over to the menu bar.
  103. //-----------------------------------------------------------------------------
  104. /// Places this menu in the menu bar of the application's main window.
  105. /// @param owner The GuiCanvas that owns the PlatformWindow that this call is associated with
  106. /// @param pos The relative position at which to place the menu.
  107. /// @param title The name of the menu
  108. void attachToMenuBar(GuiCanvas *owner, S32 pos, const char *title);
  109. /// Removes this menu from the menu bar.
  110. void removeFromMenuBar();
  111. //-----------------------------------------------------------------------------
  112. /// Called when the menu has been attached to the menu bar
  113. void onAttachToMenuBar(GuiCanvas *canvas, S32 pos, const char *title);
  114. /// Called when the menu has been removed from the menu bar
  115. void onRemoveFromMenuBar(GuiCanvas *canvas);
  116. /// Returns the position index of this menu on the bar.
  117. S32 getPosOnMenuBar();
  118. /// Returns true if this menu is attached to the menu bar
  119. bool isAttachedToMenuBar() { return mCanvas != NULL; }
  120. /// Displays this menu as a popup menu and blocks until the user has selected
  121. /// an item.
  122. /// @param canvas the owner to show this popup associated with
  123. /// @param x window local x coordinate at which to display the popup menu
  124. /// @param y window local y coordinate at which to display the popup menu
  125. /// implemented on a per-platform basis.
  126. void showPopup(GuiCanvas *owner, S32 x = -1, S32 y = -1);
  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. };
  149. #endif // _POPUPMENU_H_