guiMenuBar.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 _GUIMENUBAR_H_
  23. #define _GUIMENUBAR_H_
  24. #ifndef _GUITEXTLISTCTRL_H_
  25. #include "gui/controls/guiTextListCtrl.h"
  26. #endif
  27. #ifndef _GUITICKCTRL_H_
  28. #include "gui/shiny/guiTickCtrl.h"
  29. #endif
  30. class GuiMenuBar;
  31. class GuiMenuTextListCtrl;
  32. class WindowInputGenerator;
  33. class GuiMenuBackgroundCtrl : public GuiControl
  34. {
  35. typedef GuiControl Parent;
  36. protected:
  37. GuiMenuBar *mMenuBarCtrl;
  38. GuiMenuTextListCtrl *mTextList;
  39. public:
  40. GuiMenuBackgroundCtrl(GuiMenuBar *ctrl, GuiMenuTextListCtrl* textList);
  41. void onMouseDown(const GuiEvent &event);
  42. void onMouseMove(const GuiEvent &event);
  43. void onMouseDragged(const GuiEvent &event);
  44. };
  45. class GuiSubmenuBackgroundCtrl : public GuiMenuBackgroundCtrl
  46. {
  47. typedef GuiMenuBackgroundCtrl Parent;
  48. public:
  49. GuiSubmenuBackgroundCtrl(GuiMenuBar *ctrl, GuiMenuTextListCtrl* textList);
  50. bool pointInControl(const Point2I & parentCoordPoint);
  51. void onMouseDown(const GuiEvent &event);
  52. };
  53. //------------------------------------------------------------------------------
  54. class GuiMenuTextListCtrl : public GuiTextListCtrl
  55. {
  56. private:
  57. typedef GuiTextListCtrl Parent;
  58. protected:
  59. GuiMenuBar *mMenuBarCtrl;
  60. public:
  61. bool isSubMenu; // Indicates that this text list is in a submenu
  62. GuiMenuTextListCtrl(); // for inheritance
  63. GuiMenuTextListCtrl(GuiMenuBar *ctrl);
  64. // GuiControl overloads:
  65. bool onKeyDown(const GuiEvent &event);
  66. void onMouseDown(const GuiEvent &event);
  67. void onMouseUp(const GuiEvent &event);
  68. void onRenderCell(Point2I offset, Point2I cell, bool selected, bool mouseOver);
  69. virtual void onCellHighlighted(Point2I cell); // Added
  70. };
  71. //------------------------------------------------------------------------------
  72. class GuiMenuBar : public GuiTickCtrl // Was: GuiControl
  73. {
  74. typedef GuiTickCtrl Parent; // Was: GuiControl Parent;
  75. public:
  76. struct Menu;
  77. struct MenuItem // an individual item in a pull-down menu
  78. {
  79. char *text; // the text of the menu item
  80. U32 id; // a script-assigned identifier
  81. char *accelerator; // the keyboard accelerator shortcut for the menu item
  82. U32 acceleratorIndex; // index of this accelerator
  83. bool enabled; // true if the menu item is selectable
  84. bool visible; // true if the menu item is visible
  85. S32 bitmapIndex; // index of the bitmap in the bitmap array
  86. S32 checkGroup; // the group index of the item visa vi check marks -
  87. // only one item in the group can be checked.
  88. MenuItem *nextMenuItem; // next menu item in the linked list
  89. bool isSubmenu; // This menu item has a submenu that will be displayed
  90. Menu* submenuParentMenu; // For a submenu, this is the parent menu
  91. Menu* submenu;
  92. String cmd;
  93. };
  94. struct Menu
  95. {
  96. char *text;
  97. U32 id;
  98. RectI bounds;
  99. bool visible;
  100. S32 bitmapIndex; // Index of the bitmap in the bitmap array (-1 = no bitmap)
  101. bool drawBitmapOnly; // Draw only the bitmap and not the text
  102. bool drawBorder; // Should a border be drawn around this menu (usually if we only have a bitmap, we don't want a border)
  103. Menu *nextMenu;
  104. MenuItem *firstMenuItem;
  105. };
  106. GuiMenuBackgroundCtrl *mBackground;
  107. GuiMenuTextListCtrl *mTextList;
  108. GuiSubmenuBackgroundCtrl *mSubmenuBackground; // Background for a submenu
  109. GuiMenuTextListCtrl *mSubmenuTextList; // Text list for a submenu
  110. Vector<Menu*> mMenuList;
  111. Menu *mouseDownMenu;
  112. Menu *mouseOverMenu;
  113. MenuItem* mouseDownSubmenu; // Stores the menu item that is a submenu that has been selected
  114. MenuItem* mouseOverSubmenu; // Stores the menu item that is a submenu that has been highlighted
  115. bool menuBarDirty;
  116. U32 mCurAcceleratorIndex;
  117. Point2I maxBitmapSize;
  118. S32 mCheckmarkBitmapIndex; // Index in the bitmap array to use for the check mark image
  119. S32 mPadding;
  120. S32 mHorizontalMargin; // Left and right margin around the text of each menu
  121. S32 mVerticalMargin; // Top and bottom margin around the text of each menu
  122. S32 mBitmapMargin; // Margin between a menu's bitmap and text
  123. // Used to keep track of the amount of ticks that the mouse is hovering
  124. // over a menu.
  125. S32 mMouseOverCounter;
  126. bool mCountMouseOver;
  127. S32 mMouseHoverAmount;
  128. GuiMenuBar();
  129. bool onWake();
  130. void onSleep();
  131. // internal menu handling functions
  132. // these are used by the script manipulation functions to add/remove/change menu items
  133. static Menu* sCreateMenu(const char *menuText, U32 menuId);
  134. void addMenu(Menu *menu, S32 pos = -1);
  135. void addMenu(const char *menuText, U32 menuId);
  136. Menu *findMenu(const char *menu); // takes either a menu text or a string id
  137. static MenuItem *findMenuItem(Menu *menu, const char *menuItem); // takes either a menu text or a string id
  138. void removeMenu(Menu *menu);
  139. static void removeMenuItem(Menu *menu, MenuItem *menuItem);
  140. static MenuItem* addMenuItem(Menu *menu, const char *text, U32 id, const char *accelerator, S32 checkGroup, const char *cmd);
  141. static MenuItem* addMenuItem(Menu *menu, MenuItem *menuItem);
  142. static void clearMenuItems(Menu *menu);
  143. void clearMenus();
  144. void attachToMenuBar(Menu* menu, S32 pos = -1);
  145. void removeFromMenuBar(Menu* menu);
  146. // Methods to deal with submenus
  147. static MenuItem* findSubmenuItem(Menu *menu, const char *menuItem, const char *submenuItem);
  148. static MenuItem* findSubmenuItem(MenuItem *menuItem, const char *submenuItem);
  149. static void addSubmenuItem(Menu *menu, MenuItem *submenu, const char *text, U32 id, const char *accelerator, S32 checkGroup);
  150. static void addSubmenuItem(Menu *menu, MenuItem *submenu, MenuItem *newMenuItem );
  151. static void removeSubmenuItem(MenuItem *menuItem, MenuItem *submenuItem);
  152. static void clearSubmenuItems(MenuItem *menuitem);
  153. void onSubmenuAction(S32 selectionIndex, const RectI& bounds, Point2I cellSize);
  154. void closeSubmenu();
  155. void checkSubmenuMouseMove(const GuiEvent &event);
  156. MenuItem *findHitMenuItem(Point2I mousePoint);
  157. void highlightedMenuItem(S32 selectionIndex, const RectI& bounds, Point2I cellSize); // Called whenever a menu item is highlighted by the mouse
  158. // display/mouse functions
  159. Menu *findHitMenu(Point2I mousePoint);
  160. // Called when the GUI theme changes and a bitmap arrary may need updating
  161. // void onThemeChange();
  162. void onPreRender();
  163. void onRender(Point2I offset, const RectI &updateRect);
  164. void checkMenuMouseMove(const GuiEvent &event);
  165. void onMouseMove(const GuiEvent &event);
  166. void onMouseLeave(const GuiEvent &event);
  167. void onMouseDown(const GuiEvent &event);
  168. void onMouseDragged(const GuiEvent &event);
  169. void onMouseUp(const GuiEvent &event);
  170. void onAction();
  171. void closeMenu();
  172. void buildWindowAcceleratorMap( WindowInputGenerator &inputGenerator );
  173. void removeWindowAcceleratorMap( WindowInputGenerator &inputGenerator );
  174. void acceleratorKeyPress(U32 index);
  175. virtual void menuItemSelected(Menu *menu, MenuItem *item);
  176. // Added to support 'ticks'
  177. void processTick();
  178. static void initPersistFields();
  179. DECLARE_CONOBJECT(GuiMenuBar);
  180. DECLARE_CALLBACK( void, onMouseInMenu, ( bool hasLeftMenu ));
  181. DECLARE_CALLBACK( void, onMenuSelect, ( S32 menuId, const char* menuText ));
  182. DECLARE_CALLBACK( void, onMenuItemSelect, ( S32 menuId, const char* menuText, S32 menuItemId, const char* menuItemText ));
  183. DECLARE_CALLBACK( void, onSubmenuSelect, ( S32 submenuId, const char* submenuText ));
  184. };
  185. #endif