Team Fortress 2 Source Code as on 22/4/2020
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.

397 lines
13 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "DemoPage.h"
  8. #include <VGUI/IVGui.h>
  9. #include <vgui_controls/Controls.h>
  10. #include <vgui_controls/Menu.h>
  11. #include <vgui_controls/MenuItem.h>
  12. #include <vgui_controls/MenuButton.h>
  13. #include <Keyvalues.h>
  14. #include <vgui_controls/Label.h>
  15. using namespace vgui;
  16. class SampleMenus: public DemoPage
  17. {
  18. public:
  19. SampleMenus(Panel *parent, const char *name);
  20. ~SampleMenus();
  21. void InitMenus();
  22. void OnCommand( const char *command );
  23. private:
  24. // Menu that opens when button is pressed
  25. Menu *m_pMenu;
  26. Menu *m_pOuterMenu;
  27. Menu *m_pInnerMenu;
  28. Menu *m_pInnerMenu2;
  29. Menu *m_pScrollMenu;
  30. // Button to trigger the menu
  31. MenuButton *m_pMenuButton;
  32. MenuButton *m_pOuterMenuButton;
  33. MenuButton *m_pScrollMenuButton;
  34. // These are the same menus as above with checkable menu items in them
  35. MenuButton *m_pMenuButton2;
  36. MenuButton *m_pOuterMenuButton2;
  37. MenuButton *m_pScrollMenuButton2;
  38. Menu *m_pMenu2;
  39. Menu *m_pOuterMenu2;
  40. Menu *m_pInnerMenu_2;
  41. Menu *m_pInnerMenu22;
  42. Menu *m_pScrollMenu2;
  43. MenuButton *m_pMenuButton3;
  44. Menu *m_pMenu3;
  45. MenuButton *m_pMenuButton4;
  46. Menu *m_pMenu4;
  47. MenuButton *m_pMenuButton5;
  48. Menu *m_pMenu5;
  49. };
  50. //-----------------------------------------------------------------------------
  51. // Purpose: Constructor
  52. //-----------------------------------------------------------------------------
  53. SampleMenus::SampleMenus(Panel *parent, const char *name) : DemoPage(parent, name)
  54. {
  55. // A fixed width menu
  56. m_pMenuButton = new MenuButton(this, "AMenuButton", "Fixed width");
  57. int wide, tall;
  58. m_pMenuButton->GetContentSize(wide, tall);
  59. m_pMenuButton->SetSize(wide + Label::Content, tall + Label::Content);
  60. // Position the menu button in the window.
  61. m_pMenuButton->SetPos(95, 15);
  62. m_pMenu = new Menu(m_pMenuButton, "AMenu");
  63. m_pMenu->AddMenuItem("Menu Item", "junk", this);
  64. m_pMenu->AddMenuItem("Another Choice", "junk", this);
  65. m_pMenu->AddMenuItem("Menu Item", "junk", this);
  66. m_pMenu->SetVisible(false);
  67. m_pMenuButton->SetMenu(m_pMenu);
  68. m_pMenuButton->GetSize(wide, tall);
  69. m_pMenu->SetFixedWidth(wide);
  70. // Cascading menu
  71. InitMenus();
  72. // Scrolling menu
  73. m_pScrollMenuButton = new MenuButton(this, "AMenuButton", "Scrolling");
  74. m_pScrollMenuButton->GetContentSize(wide, tall);
  75. m_pScrollMenuButton->SetSize(wide + Label::Content, tall + Label::Content);
  76. // Position the menu button in the window.
  77. m_pScrollMenuButton->SetPos(360, 15);
  78. m_pScrollMenu = new Menu(m_pScrollMenuButton, "AScrollingMenu");
  79. m_pScrollMenu->AddMenuItem("Freezes", "junk", this);
  80. m_pScrollMenu->AddMenuItem("Kipup", "junk", this);
  81. m_pScrollMenu->AddMenuItem("Donkey", "junk", this);
  82. m_pScrollMenu->AddMenuItem("Sidewinder", "junk", this);
  83. m_pScrollMenu->AddMenuItem("Handspin", "junk", this);
  84. m_pScrollMenu->AddMenuItem("Coffee Grinder", "junk", this);
  85. m_pScrollMenu->AddMenuItem("Headspin", "junk", this);
  86. m_pScrollMenu->AddMenuItem("The Worm", "junk", this);
  87. m_pScrollMenu->SetNumberOfVisibleItems(5);
  88. m_pScrollMenu->SetVisible(false);
  89. m_pScrollMenuButton->SetMenu(m_pScrollMenu);
  90. // A fixed width menu with checkable menu items and items with names longer than the menu width
  91. m_pMenuButton2 = new MenuButton(this, "AMenuButton", "A Check menu");
  92. m_pMenuButton2->GetContentSize(wide, tall);
  93. m_pMenuButton2->SetSize(wide + Label::Content, tall + Label::Content);
  94. // Position the menu button in the window.
  95. m_pMenuButton2->SetPos(95, 85);
  96. m_pMenu2 = new Menu(m_pMenuButton2, "AMenu");
  97. m_pMenu2->AddCheckableMenuItem("Menu Item Long Long Name", "junk", this );
  98. m_pMenu2->AddCheckableMenuItem("Another Choice", "junk", this );
  99. // last item not checkable for testing
  100. m_pMenu2->AddMenuItem("Menu Item", "junk", this);
  101. m_pMenu2->SetVisible(false);
  102. m_pMenuButton2->SetMenu(m_pMenu2);
  103. m_pMenuButton2->GetSize(wide, tall);
  104. m_pMenu2->SetFixedWidth(wide);
  105. // Cascading menu with checkable menu items and items with names longer than the menu width
  106. InitMenus();
  107. // Scrolling menu with checkable menu items and items with names longer than the menu width
  108. m_pScrollMenuButton2 = new MenuButton(this, "AMenuButton", "Scrolling");
  109. m_pScrollMenuButton2->GetContentSize(wide, tall);
  110. m_pScrollMenuButton2->SetSize(wide + Label::Content, tall + Label::Content);
  111. // Position the menu button in the window.
  112. m_pScrollMenuButton2->SetPos(360, 85);
  113. m_pScrollMenu2 = new Menu(m_pScrollMenuButton2, "AScrollingMenu");
  114. m_pScrollMenu2->AddCheckableMenuItem("Freezes", "junk", this);
  115. m_pScrollMenu2->AddCheckableMenuItem("Kipup", "junk", this);
  116. m_pScrollMenu2->AddCheckableMenuItem("Donkey", "junk", this);
  117. m_pScrollMenu2->AddCheckableMenuItem("Sidewinder", "junk", this);
  118. m_pScrollMenu2->AddCheckableMenuItem("Handspin", "junk", this);
  119. m_pScrollMenu2->AddCheckableMenuItem("Coffee Grinder", "junk", this);
  120. m_pScrollMenu2->AddCheckableMenuItem("Headspin", "junk", this);
  121. // last item not checkable for testing
  122. m_pScrollMenu2->AddMenuItem("The Worm", "junk", this);
  123. m_pScrollMenu2->SetNumberOfVisibleItems(5);
  124. m_pScrollMenu2->SetVisible(false);
  125. m_pScrollMenuButton2->SetMenu(m_pScrollMenu2);
  126. // Lets check off some stuff
  127. m_pMenu2->SetMenuItemChecked( 1, true );
  128. m_pScrollMenu2->SetMenuItemChecked( 0, true );
  129. m_pScrollMenu2->SetMenuItemChecked( 2, true );
  130. m_pScrollMenu2->SetMenuItemChecked( 3, true );
  131. Label *buttonLabel = new Label (this, "buttonLabel", "These are the same menus as above with checkable/elipsis menuitems");
  132. buttonLabel->SetPos(95, 60);
  133. buttonLabel->SizeToContents();
  134. // A button to toggle a check setting in a menu
  135. Button *toggleMenuCheckButton = new Button (this, "Toggle Menu Check", "Toggle Simple Menu Check");
  136. toggleMenuCheckButton->SetCommand("Check");
  137. toggleMenuCheckButton->AddActionSignalTarget(this);
  138. toggleMenuCheckButton->GetContentSize(wide, tall);
  139. toggleMenuCheckButton->SetSize(wide + Label::Content, tall + Label::Content);
  140. toggleMenuCheckButton->SetPos( 95, 220);
  141. // A non fixed width menu with checkable menu items
  142. m_pMenuButton3 = new MenuButton(this, "AMenuButton", "A menu");
  143. m_pMenuButton3->GetContentSize(wide, tall);
  144. m_pMenuButton3->SetSize(wide + Label::Content, tall + Label::Content);
  145. // Position the menu button in the window.
  146. m_pMenuButton3->SetPos(285, 220);
  147. m_pMenu3 = new Menu(m_pMenuButton3, "AMenu");
  148. m_pMenu3->AddCheckableMenuItem("Menu Item", "junk", this);
  149. m_pMenu3->AddCheckableMenuItem("Another Choice", "junk", this);
  150. m_pMenu3->AddCheckableMenuItem("Menu Item", "junk", this);
  151. m_pMenu3->SetVisible(false);
  152. m_pMenuButton3->SetMenu(m_pMenu3);
  153. // A non fixed width menu
  154. m_pMenuButton4 = new MenuButton(this, "AMenuButton", "A menu");
  155. m_pMenuButton4->GetContentSize(wide, tall);
  156. m_pMenuButton4->SetSize(wide + Label::Content, tall + Label::Content);
  157. // Position the menu button in the window.
  158. m_pMenuButton4->SetPos(355, 220);
  159. m_pMenu4 = new Menu(m_pMenuButton4, "AMenu");
  160. m_pMenu4->AddMenuItem("Menu Item", "junk", this);
  161. m_pMenu4->AddMenuItem("Another Choice", "junk", this);
  162. m_pMenu4->AddMenuItem("Menu Item", "junk", this);
  163. m_pMenu4->SetVisible(false);
  164. m_pMenuButton4->SetMenu(m_pMenu4);
  165. // A non fixed width menu with a minimum width
  166. m_pMenuButton5 = new MenuButton(this, "AMenuButton", "A Minimum Width Menu");
  167. m_pMenuButton5->GetContentSize(wide, tall);
  168. m_pMenuButton5->SetSize(wide + Label::Content, tall + Label::Content);
  169. m_pMenuButton5->GetContentSize(wide, tall);
  170. m_pMenuButton5->SetSize(wide + Label::Content, tall + Label::Content);
  171. // Position the menu button in the window.
  172. m_pMenuButton5->SetPos(355, 270);
  173. m_pMenu5 = new Menu(m_pMenuButton5, "AMenu");
  174. m_pMenu5->AddMenuItem("Menu Item", "junk", this);
  175. m_pMenu5->AddMenuItem("Another Choice", "junk", this);
  176. m_pMenu5->AddMenuItem("Menu Item", "junk", this);
  177. m_pMenu5->SetVisible(false);
  178. m_pMenuButton5->GetSize(wide, tall);
  179. m_pMenu5->SetMinimumWidth(wide);
  180. m_pMenuButton5->SetMenu(m_pMenu5);
  181. }
  182. //-----------------------------------------------------------------------------
  183. // Purpose: Create cascading menus.
  184. // We will create a Menu with 2 Menus inside it, and yet another Menu inside one of those!
  185. //-----------------------------------------------------------------------------
  186. void SampleMenus::InitMenus()
  187. {
  188. // A drop down menu button for the top most menu
  189. m_pOuterMenuButton = new MenuButton(this, "OuterMenu", "Cascading");
  190. // Size the Button so we can read its label.
  191. int wide, tall;
  192. m_pOuterMenuButton->GetContentSize(wide, tall);
  193. m_pOuterMenuButton->SetSize(wide + Label::Content, tall + Label::Content);
  194. // Create the Menu to go with it.
  195. m_pOuterMenu = new Menu(m_pOuterMenuButton, "OuterMenu");
  196. // Add the menu items to this menu
  197. m_pOuterMenu->SetVisible(false);
  198. m_pOuterMenu->SetNumberOfVisibleItems(5);
  199. // Attach this menu to the menu button
  200. m_pOuterMenuButton->SetMenu(m_pOuterMenu);
  201. // Position the menu button on screen.
  202. m_pOuterMenuButton->SetPos(220, 15);
  203. // Create cascading menu #1
  204. m_pInnerMenu = new Menu(m_pOuterMenu, "InnerMenu");
  205. // Add menu items to this menu.
  206. m_pInnerMenu->AddMenuItem("Marcia", "junk", this);
  207. m_pInnerMenu->AddMenuItem("Greg", "junk", this);
  208. m_pInnerMenu->AddMenuItem("Peter", "junk", this);
  209. m_pInnerMenu->SetVisible(false);
  210. // Now add the cascading menu to the top menu as a menu item!
  211. m_pOuterMenu->AddCascadingMenuItem("Cascades", this, m_pInnerMenu);
  212. // Create cascading menu #2
  213. m_pInnerMenu2 = new Menu(m_pOuterMenu, "InnerMenu2");
  214. m_pInnerMenu2->AddMenuItem("One Fish", "junk", this);
  215. m_pInnerMenu2->AddMenuItem("Two Fish", "junk", this);
  216. m_pInnerMenu2->AddMenuItem("Red Fish", "junk", this);
  217. m_pInnerMenu2->AddMenuItem("Blue Fish", "junk", this);
  218. m_pInnerMenu2->SetVisible(false);
  219. // Add this cascading menu to the top menu as a manu item.
  220. m_pOuterMenu->AddCascadingMenuItem("Cascading Choice", this, m_pInnerMenu2);
  221. // Make one of the items in the menu disabled.
  222. m_pOuterMenu->AddMenuItem("Normal Menuitem", "junk", this);
  223. m_pOuterMenu->AddMenuItem("Disabled Menuitem", "Disabled", this);
  224. Panel *menuItem = m_pOuterMenu->FindChildByName("Disabled Menuitem");
  225. assert(menuItem);
  226. menuItem->SetEnabled(false);
  227. m_pOuterMenu->AddMenuItem("Normal Menuitem", "junk", this);
  228. // A drop down menu button for the top most menu
  229. m_pOuterMenuButton2 = new MenuButton(this, "OuterMenu", "Cascading");
  230. // Size the Button so we can read its label.
  231. m_pOuterMenuButton2->GetContentSize(wide, tall);
  232. m_pOuterMenuButton2->SetSize(wide + Label::Content, tall + Label::Content);
  233. // Create the Menu to go with it.
  234. m_pOuterMenu2 = new Menu(m_pOuterMenuButton2, "OuterMenu");
  235. // Add the menu items to this menu
  236. m_pOuterMenu2->SetVisible(false);
  237. m_pOuterMenu2->SetNumberOfVisibleItems(7);
  238. // Attach this menu to the menu button
  239. m_pOuterMenuButton2->SetMenu(m_pOuterMenu2);
  240. // Position the menu button on screen.
  241. m_pOuterMenuButton2->SetPos(220, 85);
  242. // Create cascading menu #1
  243. m_pInnerMenu_2 = new Menu(m_pOuterMenu2, "InnerMenu");
  244. // Add menu items to this menu.
  245. m_pInnerMenu_2->AddCheckableMenuItem("Marcia", "junk", this);
  246. m_pInnerMenu_2->AddCheckableMenuItem("Greg", "junk", this);
  247. m_pInnerMenu_2->AddMenuItem("Peter", "junk", this);
  248. m_pInnerMenu_2->SetVisible(false);
  249. // Now add the cascading menu to the top menu as a menu item!
  250. m_pOuterMenu2->AddCascadingMenuItem("Cascades", this, m_pInnerMenu_2);
  251. // Create cascading menu #2
  252. m_pInnerMenu22 = new Menu(m_pOuterMenu2, "InnerMenu2");
  253. m_pInnerMenu22->AddCheckableMenuItem("One Fish", "junk", this);
  254. m_pInnerMenu22->AddCheckableMenuItem("Two Fish", "junk", this);
  255. m_pInnerMenu22->AddCheckableMenuItem("Red Fish", "junk", this);
  256. m_pInnerMenu22->AddCheckableMenuItem("Blue Fish", "junk", this);
  257. m_pInnerMenu22->SetVisible(false);
  258. // Add this cascading menu to the top menu as a manu item.
  259. m_pOuterMenu2->AddCascadingMenuItem("Cascading Choice", this, m_pInnerMenu22);
  260. // Make one of the items in the menu disabled.
  261. m_pOuterMenu2->AddMenuItem("Normal Menuitem", "junk", this);
  262. // checkable so we can see what disabled checkable items look like
  263. m_pOuterMenu2->AddCheckableMenuItem("Disabled Menuitem", "Disabled", this);
  264. menuItem = m_pOuterMenu2->FindChildByName("Disabled Menuitem");
  265. assert(menuItem);
  266. menuItem->SetEnabled(false);
  267. m_pOuterMenu2->AddMenuItem("Normal Menuitem", "junk", this);
  268. m_pOuterMenu2->AddCheckableMenuItem("Normal Checkable", "junk", this);
  269. // Lets check off some stuff for starters
  270. m_pOuterMenu2->SetMenuItemChecked( 3, true );
  271. m_pOuterMenu2->SetMenuItemChecked( 5, true );
  272. //m_pInnerMenu_2->SetMenuItemChecked( 0, true );
  273. m_pInnerMenu22->SetMenuItemChecked( 0, true );
  274. m_pInnerMenu22->SetMenuItemChecked( 1, true );
  275. m_pInnerMenu22->SetMenuItemChecked( 2, true );
  276. // another way of checking a menu item
  277. m_pInnerMenu22->GetMenuItem(3)->SetChecked( true );
  278. }
  279. void SampleMenus::OnCommand( const char *command )
  280. {
  281. // Hitting the button will toggle the checking and unchecking
  282. // of the first item of menu3
  283. if (!stricmp(command, "Check"))
  284. {
  285. if (!m_pMenu3->GetMenuItem(0)->IsChecked())
  286. // check the first menu item in the first menu
  287. m_pMenu3->SetMenuItemChecked( 0, true );
  288. else
  289. // another way of setting the checked state.
  290. m_pMenu3->GetMenuItem(0)->SetChecked(false);
  291. }
  292. Panel::OnCommand(command);
  293. }
  294. //-----------------------------------------------------------------------------
  295. // Purpose: Destructor
  296. //-----------------------------------------------------------------------------
  297. SampleMenus::~SampleMenus()
  298. {
  299. }
  300. Panel* SampleMenus_Create(Panel *parent)
  301. {
  302. return new SampleMenus(parent, "Menus");
  303. }