Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
14 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef MENU_H
  8. #define MENU_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <vgui_controls/Panel.h>
  13. #include <vgui_controls/Label.h>
  14. #include <utllinkedlist.h>
  15. #include <utlvector.h>
  16. namespace vgui
  17. {
  18. class MenuItem;
  19. class ScrollBar;
  20. class MenuSeparator;
  21. //-----------------------------------------------------------------------------
  22. // Purpose: A menu is a list of items that can be selected with one click, navigated
  23. // with arrow keys and/or hot keys, and have a lit behavior when mouse over.
  24. // It is NOT the button which opens the menu, but only the menu itself.
  25. //
  26. // Behaviour spec:
  27. // Menu navigation can be done in 2 modes, via keyboard keys and via mouse.
  28. // Clicking on menu button opens menu.
  29. // Only one item in a menu is highlighted at a time.
  30. // Only one submenu in a menu is open at a time.
  31. // Disabled menuitems get highlighted via mouse and keys but will not activate.
  32. //
  33. // Mouse:
  34. // Moving mouse into a menuitem highlights it.
  35. // If the menuitem has a cascading menu, the menu opens when the mouse enters
  36. // the menuitem. The cascading menuitem stays highlighted while its menu is open.
  37. // No submenu items are highlighted by default.
  38. // Moving the mouse into another menuitem closes any previously open submenus in the list.
  39. // Clicking once in the menu item activates the menu item and closes all menus.
  40. // Moving the mouse off a menuitem unhighlights it.
  41. // The scroll bar arrows can be used to move up/down the menu one item at a time.
  42. // The clicking and dragging on the scroll bar nob also scrolls the menu items.
  43. // If a highlighed menuitem scrolls off, and the user then begins navigating via keys,
  44. // the menu will snap the scroll bar so the highlighted item is visible.
  45. // If user has been navigating via keys, moving the mouse over a menu item
  46. // highlights it.
  47. // Mousewheel:
  48. // You must have the mouse inside the menu/scroll bar to use the wheel.
  49. // The mouse wheel moves the highlighted menuitem up or down the list.
  50. // If the list has no scroll bar the wheel will cycle from the bottom of the list
  51. // to the top of the list and vice versa.
  52. // If the list has a scrollbar the mouse wheel will stop at the top or bottom
  53. // of the list.
  54. // If the mouse is over the scroll bar no items are highlighted.
  55. // Keyboard:
  56. // When a menu is opened, no items are highlighted.
  57. // If a menuitem has a cascading menu it does not open when the item is highlighted.
  58. // The down arrow selects the next item in the list.
  59. // (first item if none are highlighted and there is a scrollbar).
  60. // The up arrow selects the previous item in the list
  61. // (first item if none are highlighted and there is a scrollbar, last item if none are
  62. // highlighted and there is no scrollbar).
  63. // Selecting a new menuitem closes any previously open submenus in the list.
  64. // The enter key activates the selected item and closes all menus.
  65. // If the selected item has a cascading menu, activating it opens its submenu.
  66. // These may also be activated by pressing the right arrow.
  67. // Pressing the left arrow closes the submenu.
  68. // When the submenu is opened the cascading menuitem stays highlighted.
  69. // No items in the submenu are highlighted when it is opened.
  70. //
  71. // Note: Cascading menuitems in menus with a scrollbar is not supported.
  72. // Its a clunky UI and if we want this we should design a better solution,
  73. // perhaps along the lines of how explorer's bookmarks does it.
  74. // It currently functions, but there are some arm/disarm bugs.
  75. //
  76. //
  77. //-----------------------------------------------------------------------------
  78. class Menu : public Panel
  79. {
  80. DECLARE_CLASS_SIMPLE( Menu, Panel );
  81. friend class MenuItem;
  82. public:
  83. enum MenuDirection_e
  84. {
  85. LEFT,
  86. RIGHT,
  87. UP,
  88. DOWN,
  89. CURSOR, // make the menu appear under the mouse cursor
  90. ALIGN_WITH_PARENT, // make the menu appear under the parent
  91. };
  92. Menu(Panel *parent, const char *panelName);
  93. ~Menu();
  94. static void PlaceContextMenu( Panel *parent, Menu *menu );
  95. static void OnInternalMousePressed( Panel *other, MouseCode code );
  96. virtual void PositionRelativeToPanel( Panel *reference, MenuDirection_e direction, int nAdditionalYOffset = 0, bool showMenu = false );
  97. // the menu. For combo boxes, it's the edit/field, etc. etc.
  98. // Add a simple text item to the menu
  99. virtual int AddMenuItem( const char *itemName, const char *itemText, const char *command, Panel *target, const KeyValues *userData = NULL );
  100. virtual int AddMenuItem( const char *itemName, const wchar_t *wszItemText, const char *command, Panel *target, const KeyValues *userData = NULL );
  101. virtual int AddMenuItem( const char *itemName, const char *itemText, KeyValues *message, Panel *target , const KeyValues *userData = NULL);
  102. virtual int AddMenuItem( const char *itemName, const wchar_t *wszItemText, KeyValues *message, Panel *target , const KeyValues *userData = NULL);
  103. virtual int AddMenuItem( const char *itemText, const char *command, Panel *target , const KeyValues *userData = NULL);
  104. virtual int AddMenuItem( const char *itemText, KeyValues *message, Panel *target, const KeyValues *userData = NULL );
  105. virtual int AddMenuItem( const char *itemText, Panel *target, const KeyValues *userData = NULL );
  106. // Add a checkable item to the menu
  107. virtual int AddCheckableMenuItem( const char *itemName, const char *itemText, const char *command, Panel *target, const KeyValues *userData = NULL );
  108. virtual int AddCheckableMenuItem( const char *itemName, const wchar_t *wszItemText, const char *command, Panel *target, const KeyValues *userData = NULL );
  109. virtual int AddCheckableMenuItem( const char *itemName, const char *itemText, KeyValues *message, Panel *target, const KeyValues *userData = NULL );
  110. virtual int AddCheckableMenuItem( const char *itemName, const wchar_t *wszItemText, KeyValues *message, Panel *target, const KeyValues *userData = NULL );
  111. virtual int AddCheckableMenuItem( const char *itemText, const char *command, Panel *target , const KeyValues *userData = NULL);
  112. virtual int AddCheckableMenuItem( const char *itemText, KeyValues *message, Panel *target, const KeyValues *userData = NULL );
  113. virtual int AddCheckableMenuItem( const char *itemText, Panel *target, const KeyValues *userData = NULL );
  114. // Add a cascading menu item to the menu
  115. virtual int AddCascadingMenuItem( const char *itemName, const char *itemText, const char *command, Panel *target, Menu *cascadeMenu, const KeyValues *userData = NULL );
  116. virtual int AddCascadingMenuItem( const char *itemName, const wchar_t *wszItemText, const char *command, Panel *target, Menu *cascadeMenu, const KeyValues *userData = NULL );
  117. virtual int AddCascadingMenuItem( const char *itemName, const char *itemText, KeyValues *message, Panel *target, Menu *cascadeMenu, const KeyValues *userData = NULL );
  118. virtual int AddCascadingMenuItem( const char *itemName, const wchar_t *wszItemText, KeyValues *message, Panel *target, Menu *cascadeMenu, const KeyValues *userData = NULL );
  119. virtual int AddCascadingMenuItem( const char *itemText, const char *command, Panel *target, Menu *cascadeMenu, const KeyValues *userData = NULL );
  120. virtual int AddCascadingMenuItem( const char *itemText, KeyValues *message, Panel *target, Menu *cascadeMenu, const KeyValues *userData = NULL );
  121. virtual int AddCascadingMenuItem( const char *itemText, Panel *target, Menu *cascadeMenu, const KeyValues *userData = NULL );
  122. // Add a custom panel to the menu
  123. virtual int AddMenuItem( MenuItem *panel );
  124. virtual void AddSeparator();
  125. virtual void AddSeparatorAfterItem( int itemID );
  126. // Sets the values of a menu item at the specified index
  127. virtual void UpdateMenuItem(int itemID, const char *itemText,KeyValues *message, const KeyValues *userData = NULL);
  128. virtual void UpdateMenuItem(int itemID, const wchar_t *wszItemText,KeyValues *message, const KeyValues *userData = NULL);
  129. virtual void MoveMenuItem( int itemID, int moveBeforeThisItemID );
  130. virtual bool IsValidMenuID(int itemID);
  131. virtual int GetInvalidMenuID();
  132. KeyValues *GetItemUserData(int itemID);
  133. void GetItemText(int itemID, wchar_t *text, int bufLenInBytes);
  134. void GetItemText(int itemID, char *text, int bufLenInBytes);
  135. virtual void SetItemEnabled(const char *itemName, bool state);
  136. virtual void SetItemEnabled(int itemID, bool state);
  137. virtual void SetItemVisible(const char *itemName, bool visible);
  138. virtual void SetItemVisible(int itemID, bool visible);
  139. // Remove a single item
  140. void DeleteItem( int itemID );
  141. // Clear the menu, deleting all the menu items within
  142. void DeleteAllItems();
  143. // Override the auto-width setting with a single fixed width
  144. virtual void SetFixedWidth( int width );
  145. // Sets the content alignment of all items in the menu
  146. void SetContentAlignment( Label::Alignment alignment );
  147. // sets the height of each menu item
  148. virtual void SetMenuItemHeight(int itemHeight);
  149. virtual int GetMenuItemHeight() const;
  150. // Set the max number of items visible (scrollbar appears with more)
  151. virtual void SetNumberOfVisibleItems( int numItems );
  152. // Set up the menu items layout
  153. virtual void PerformLayout( void );
  154. virtual void SetBorder(class IBorder *border);
  155. virtual void ApplySchemeSettings(IScheme *pScheme);
  156. // Set type ahead behaviour
  157. enum MenuTypeAheadMode
  158. {
  159. COMPAT_MODE = 0,
  160. HOT_KEY_MODE,
  161. TYPE_AHEAD_MODE,
  162. };
  163. virtual void SetTypeAheadMode(MenuTypeAheadMode mode);
  164. virtual int GetTypeAheadMode();
  165. // Hotkey handling
  166. virtual void OnKeyTyped(wchar_t unichar);
  167. // Menu nagivation etc.
  168. virtual void OnKeyCodeTyped( KeyCode code );
  169. // Visibility
  170. virtual void SetVisible(bool state);
  171. // Activates item in the menu list, as if that menu item had been selected by the user
  172. virtual void ActivateItem(int itemID);
  173. virtual void ActivateItemByRow(int row);
  174. virtual int GetActiveItem(); // returns the itemID (not the row) of the active item
  175. // Return the number of items currently in the menu list
  176. virtual int GetItemCount();
  177. // return the menuID of the n'th item in the menu list, valid from [0, GetItemCount)
  178. virtual int GetMenuID(int index);
  179. // Return the number of items currently visible in the menu list
  180. int GetCurrentlyVisibleItemsCount();
  181. MenuItem *GetMenuItem(int itemID);
  182. void CloseOtherMenus(MenuItem *item);
  183. virtual void OnKillFocus();
  184. int GetMenuMode();
  185. enum MenuMode
  186. {
  187. MOUSE = 0,
  188. KEYBOARD,
  189. };
  190. void SetCurrentlyHighlightedItem(int itemID);
  191. int GetCurrentlyHighlightedItem();
  192. void ClearCurrentlyHighlightedItem();
  193. // Set the checked state of a checkable menuItem
  194. void SetMenuItemChecked(int itemID, bool state);
  195. bool IsChecked(int index); // check if item is checked.
  196. void SetMinimumWidth(int width);
  197. int GetMinimumWidth();
  198. // baseclass overrides to chain colors through to cascade menus
  199. virtual void SetFgColor( Color newColor );
  200. virtual void SetBgColor( Color newColor );
  201. virtual void SetFont( HFont font );
  202. // Pass in NULL hotkey to remove hotkey
  203. void SetCurrentKeyBinding( int itemID, char const *hotkey );
  204. void ForceCalculateWidth();
  205. void SetUseFallbackFont( bool bState, HFont hFallback );
  206. protected:
  207. // helper functions
  208. int AddMenuItemCharCommand(MenuItem *item, const char *command, Panel *target, const KeyValues *userData);
  209. int AddMenuItemKeyValuesCommand(MenuItem *item, KeyValues *message, Panel *target, const KeyValues *userData);
  210. // vgui result reporting
  211. virtual void OnCommand( const char *command );
  212. MESSAGE_FUNC_PTR( OnMenuItemSelected, "MenuItemSelected", panel );
  213. virtual void AddScrollBar();
  214. virtual void RemoveScrollBar();
  215. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" );
  216. virtual void Paint();
  217. virtual void LayoutMenuBorder();
  218. virtual void MakeItemsVisibleInScrollRange( int maxVisibleItems, int nNumPixelsAvailable );
  219. virtual void OnMouseWheeled(int delta);
  220. // Alternate OnKeyTyped behaviors
  221. virtual void OnHotKey(wchar_t unichar);
  222. virtual void OnTypeAhead(wchar_t unichar);
  223. int CountVisibleItems();
  224. void ComputeWorkspaceSize( int& workWide, int& workTall );
  225. int ComputeFullMenuHeightWithInsets();
  226. void CalculateWidth();
  227. void LayoutScrollBar();
  228. void PositionCascadingMenu();
  229. void SizeMenuItems();
  230. void OnCursorMoved(int x, int y);
  231. void OnKeyCodePressed(KeyCode code);
  232. void OnMenuClose();
  233. MESSAGE_FUNC( OnKeyModeSet, "KeyModeSet" );
  234. void SetCurrentlySelectedItem(MenuItem *item);
  235. void SetCurrentlySelectedItem(int itemID);
  236. MESSAGE_FUNC_PTR( OnCursorEnteredMenuItem, "CursorEnteredMenuItem", VPanel);
  237. MESSAGE_FUNC_PTR( OnCursorExitedMenuItem, "CursorExitedMenuItem", VPanel);
  238. void MoveAlongMenuItemList(int direction, int loopCount);
  239. enum
  240. {
  241. DEFAULT_MENU_ITEM_HEIGHT = 22, // height of items in the menu
  242. MENU_UP = -1, // used for moving up/down list of menu items in the menu
  243. MENU_DOWN = 1
  244. };
  245. #ifdef DBGFLAG_VALIDATE
  246. virtual void Validate( CValidator &validator, char *pchName );
  247. #endif // DBGFLAG_VALIDATE
  248. private:
  249. MenuItem *GetParentMenuItem();
  250. int m_iMenuItemHeight;
  251. int m_iFixedWidth;
  252. int m_iMinimumWidth; // a minimum width the menu has to be if it is not fixed width
  253. int m_iNumVisibleLines; // number of items in menu before scroll bar adds on
  254. ScrollBar *m_pScroller;
  255. CUtlLinkedList<MenuItem*, int> m_MenuItems;
  256. CUtlVector<int> m_VisibleSortedItems;
  257. CUtlVector<int> m_SortedItems; // used for visual
  258. CUtlVector<int> m_Separators; // menu item ids after which separators should be shown
  259. CUtlVector<MenuSeparator *> m_SeparatorPanels;
  260. bool _sizedForScrollBar: 1 ; // whether menu has been sized for a scrollbar
  261. bool m_bUseFallbackFont : 1;
  262. bool _recalculateWidth : 1;
  263. int _menuWide;
  264. int m_iCurrentlySelectedItemID;
  265. int m_iInputMode;
  266. int m_iCheckImageWidth; // the size of the check box spot on a checkable menu.
  267. int m_iProportionalScrollBarSize;
  268. Label::Alignment m_Alignment;
  269. Color _borderDark;
  270. int m_iActivatedItem;
  271. HFont m_hItemFont;
  272. HFont m_hFallbackItemFont;
  273. // for managing type ahead
  274. #define TYPEAHEAD_BUFSIZE 256
  275. MenuTypeAheadMode m_eTypeAheadMode;
  276. wchar_t m_szTypeAheadBuf[TYPEAHEAD_BUFSIZE];
  277. int m_iNumTypeAheadChars;
  278. double m_fLastTypeAheadTime;
  279. };
  280. } // namespace vgui
  281. #endif // MENU_H