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.

156 lines
4.2 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 <Keyvalues.h>
  10. #include <vgui_controls/MenuBar.h>
  11. #include <vgui_controls/MenuButton.h>
  12. #include <vgui_controls/Menu.h>
  13. #include "vgui/ISurface.h"
  14. using namespace vgui;
  15. //-----------------------------------------------------------------------------
  16. // A MenuBar
  17. //-----------------------------------------------------------------------------
  18. class MenuBarDemo: public DemoPage
  19. {
  20. public:
  21. MenuBarDemo(Panel *parent, const char *name);
  22. ~MenuBarDemo();
  23. private:
  24. MenuBar *m_pMenuBar;
  25. };
  26. //-----------------------------------------------------------------------------
  27. // Purpose: Constructor
  28. //-----------------------------------------------------------------------------
  29. MenuBarDemo::MenuBarDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  30. {
  31. m_pMenuBar = new MenuBar(this, "AMenuBar");
  32. m_pMenuBar->SetPos(0, 20);
  33. // Make a couple menus and attach them in.
  34. // A menu
  35. MenuButton *pMenuButton = new MenuButton(this, "FileMenuButton", "&File");
  36. Menu *pMenu = new Menu(pMenuButton, "AMenu");
  37. pMenu->AddMenuItem("&New", new KeyValues ("NewFile"), this);
  38. pMenu->AddMenuItem("&Open", new KeyValues ("OpenFile"), this);
  39. pMenu->AddMenuItem("&Save", new KeyValues ("SaveFile"), this);
  40. pMenuButton->SetMenu(pMenu);
  41. m_pMenuBar->AddButton(pMenuButton);
  42. // A menu
  43. pMenuButton = new MenuButton(this, "EditMenuButton", "&Edit");
  44. pMenu = new Menu(pMenuButton, "AMenu");
  45. pMenu->AddMenuItem("&Undo", new KeyValues ("Undo"), this);
  46. pMenu->AddMenuItem("&Find", new KeyValues ("Find"), this);
  47. pMenu->AddMenuItem("Select&All", new KeyValues ("SelectAll"), this);
  48. pMenuButton->SetMenu(pMenu);
  49. m_pMenuBar->AddButton(pMenuButton);
  50. // A menu
  51. pMenuButton = new MenuButton(this, "ViewMenuButton", "&View");
  52. pMenu = new Menu(pMenuButton, "AMenu");
  53. pMenu->AddMenuItem("&FullScreen", new KeyValues ("FullScreen"), this);
  54. pMenu->AddMenuItem("&SplitScreen", new KeyValues ("SplitScreen"), this);
  55. pMenu->AddMenuItem("&Properties", new KeyValues ("Properties"), this);
  56. pMenu->AddMenuItem("&Output", new KeyValues ("Output"), this);
  57. pMenuButton->SetMenu(pMenu);
  58. m_pMenuBar->AddButton(pMenuButton);
  59. // A menu
  60. pMenuButton = new MenuButton(this, "Big", "&HugeMenu");
  61. pMenu = new Menu(pMenuButton, "HugeMenu");
  62. int items = 150;
  63. for ( int i = 0 ; i < items; ++i )
  64. {
  65. char sz[ 32 ];
  66. Q_snprintf( sz, sizeof( sz ), "Item %03d", i + 1 );
  67. int idx = pMenu->AddMenuItem( sz, new KeyValues ( sz ), this);
  68. if ( !(i % 4 ) )
  69. {
  70. char binding[ 256 ];
  71. Q_snprintf( binding, sizeof( binding ), "Ctrl+%c", 'A' + ( rand() % 26 ) );
  72. pMenu->SetCurrentKeyBinding( idx, binding );
  73. }
  74. if ( !(i % 7 ) )
  75. {
  76. pMenu->AddSeparator();
  77. }
  78. }
  79. pMenuButton->SetMenu(pMenu);
  80. m_pMenuBar->AddButton(pMenuButton);
  81. pMenuButton = new MenuButton(this, "Big", "&HalfHuge");
  82. pMenu = new Menu(pMenuButton, "HalfHuge");
  83. int htotal = 0;
  84. int itemHeight = pMenu->GetMenuItemHeight();
  85. int workX, workY, workWide, workTall;
  86. surface()->GetWorkspaceBounds(workX, workY, workWide, workTall);
  87. int i = 0;
  88. while ( htotal < ( workTall / 2 ) )
  89. {
  90. char sz[ 32 ];
  91. Q_snprintf( sz, sizeof( sz ), "Item %03d", i + 1 );
  92. int idx = pMenu->AddMenuItem( sz, new KeyValues ( sz ), this);
  93. if ( !(i % 4 ) )
  94. {
  95. char binding[ 256 ];
  96. Q_snprintf( binding, sizeof( binding ), "Ctrl+%c", 'A' + ( rand() % 26 ) );
  97. pMenu->SetCurrentKeyBinding( idx, binding );
  98. }
  99. if ( !(i % 7 ) )
  100. {
  101. pMenu->AddSeparator();
  102. htotal += 3;
  103. }
  104. ++i;
  105. htotal += itemHeight;
  106. }
  107. pMenuButton->SetMenu(pMenu);
  108. m_pMenuBar->AddButton(pMenuButton);
  109. int bwide, btall;
  110. pMenuButton->GetSize( bwide, btall);
  111. int wide, tall;
  112. GetParent()->GetSize(wide, tall);
  113. m_pMenuBar->SetSize(wide - 2, btall + 8);
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Purpose: Destructor
  117. //-----------------------------------------------------------------------------
  118. MenuBarDemo::~MenuBarDemo()
  119. {
  120. }
  121. Panel* MenuBarDemo_Create(Panel *parent)
  122. {
  123. return new MenuBarDemo(parent, "MenuBarDemo");
  124. }