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.

136 lines
3.8 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 "tier1/KeyValues.h"
  11. #include <vgui_controls/Button.h>
  12. #include <vgui_controls/ComboBox.h>
  13. using namespace vgui;
  14. // Combo boxes are boxes that display text and have a menu attached.
  15. // Selecting an item from the menu changes the displayed text in the box.
  16. class ComboBox2Demo: public DemoPage
  17. {
  18. DECLARE_CLASS_SIMPLE( ComboBox2Demo, DemoPage );
  19. public:
  20. ComboBox2Demo(Panel *parent, const char *name);
  21. ~ComboBox2Demo();
  22. void OnButtonClicked();
  23. void AddItemToComboBoxMenu();
  24. private:
  25. Button *m_pButton;
  26. ComboBox *m_pComboBox;
  27. DECLARE_PANELMAP();
  28. };
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Constructor
  31. //-----------------------------------------------------------------------------
  32. ComboBox2Demo::ComboBox2Demo(Panel *parent, const char *name) : DemoPage(parent, name)
  33. {
  34. // Create a new combo box.
  35. // The first arg is the parent, the second the name
  36. // The third arg is the number of items that will be in the menu
  37. // In settin this arg to 4, if we add more than 4 items to the menu
  38. // a scroll bar will be enabled.
  39. // The fourth arg is if the box is editable or not, this time we set it to true.
  40. m_pComboBox = new ComboBox(this, "Directions", 6, true);
  41. // Position the box.
  42. m_pComboBox->SetPos(100, 100);
  43. // Set the width of the Combo box so any element selected will display nicely.
  44. m_pComboBox->SetWide(150);
  45. // Add some text selections to the menu list
  46. m_pComboBox->AddItem("Right", NULL );
  47. m_pComboBox->AddItem("Left", NULL );
  48. m_pComboBox->AddItem("Up", NULL );
  49. m_pComboBox->AddItem("Down", NULL );
  50. m_pComboBox->AddItem("Forward", NULL );
  51. m_pComboBox->AddItem("Backward", NULL );
  52. m_pComboBox->AddItem("Backward and really long", NULL );
  53. // Create a button. Clicking this button will send a command back to us.
  54. // In response to this command we will get the contents of the combo box
  55. // and add it to the list of items in the list.
  56. m_pButton = new Button (this, "AButton", "Click to add an item name to the menu");
  57. m_pButton->SizeToContents();
  58. m_pButton->SetPos(270, 100);
  59. // Install a command that will be executed when the button is pressed
  60. m_pButton->SetCommand(new KeyValues ("ButtonClicked"));
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose: Destructor
  64. //-----------------------------------------------------------------------------
  65. ComboBox2Demo::~ComboBox2Demo()
  66. {
  67. }
  68. //-----------------------------------------------------------------------------
  69. // Purpose: Respond to a message based action signal
  70. //-----------------------------------------------------------------------------
  71. void ComboBox2Demo::OnButtonClicked()
  72. {
  73. ivgui()->DPrintf("Button was clicked.\n");
  74. // Lets check out what's in the combo box and add it to the menu.
  75. AddItemToComboBoxMenu();
  76. }
  77. void ComboBox2Demo::AddItemToComboBoxMenu()
  78. {
  79. char boxText[128];
  80. // Get the text from the combo box
  81. m_pComboBox->GetText(boxText, sizeof( boxText ) );
  82. // We won't add empty text strings to the menu.
  83. if (!strcmp(boxText, ""))
  84. return;
  85. // Add this item to the combo box menu
  86. // If you wanted to check for uniqueness you would have to keep a
  87. // list of the items in the menu and check it.
  88. m_pComboBox->AddItem(boxText, NULL);
  89. // Reset the combo box to empty
  90. m_pComboBox->SetText("");
  91. }
  92. MessageMapItem_t ComboBox2Demo::m_MessageMap[] =
  93. {
  94. MAP_MESSAGE( ComboBox2Demo, "ButtonClicked", OnButtonClicked ),
  95. };
  96. IMPLEMENT_PANELMAP(ComboBox2Demo, BaseClass);
  97. Panel* ComboBox2Demo_Create(Panel *parent)
  98. {
  99. return new ComboBox2Demo(parent, "ComboBox2Demo");
  100. }