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.

118 lines
3.4 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 <Keyvalues.h>
  11. #include <vgui_controls/Button.h>
  12. #include <vgui_controls/MessageBox.h>
  13. using namespace vgui;
  14. // Message boxes are windows that pop up in response to events.
  15. // They are particularly useful for error messages.
  16. // In this example we will trigger the opening of a message box when
  17. // a button is pressed.
  18. class MessageBoxDemo: public DemoPage
  19. {
  20. public:
  21. MessageBoxDemo(Panel *parent, const char *name);
  22. ~MessageBoxDemo();
  23. void OnButtonClicked();
  24. void ShowMessageBox();
  25. private:
  26. Button *m_pButton;
  27. DECLARE_PANELMAP();
  28. };
  29. //-----------------------------------------------------------------------------
  30. // Purpose: Constructor
  31. //-----------------------------------------------------------------------------
  32. MessageBoxDemo::MessageBoxDemo(Panel *parent, const char *name) : DemoPage(parent, name)
  33. {
  34. // Create a button to trigger the message box.
  35. m_pButton = new Button(this, "AButton", "Click Me For A Message");
  36. // Size the button to its text.
  37. int wide, tall;
  38. m_pButton->GetContentSize(wide, tall);
  39. m_pButton->SetSize(wide + Label::Content, tall + Label::Content);
  40. // Set its position.
  41. m_pButton->SetPos(100, 100);
  42. // Install a command that will be executed when the button is pressed
  43. // Here we use a KeyValues command, this is mapped using the Message map
  44. // below to a function.
  45. m_pButton->SetCommand(new KeyValues ("ButtonClicked"));
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: Destructor
  49. //-----------------------------------------------------------------------------
  50. MessageBoxDemo::~MessageBoxDemo()
  51. {
  52. }
  53. //-----------------------------------------------------------------------------
  54. // Purpose: Respond to a message based action signal
  55. //
  56. //-----------------------------------------------------------------------------
  57. void MessageBoxDemo::OnButtonClicked()
  58. {
  59. ivgui()->DPrintf("Button was clicked.\n");
  60. // When the button is clicked we open the message box in response.
  61. ShowMessageBox();
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Purpose: Display a message box
  65. //-----------------------------------------------------------------------------
  66. void MessageBoxDemo::ShowMessageBox()
  67. {
  68. // create a new message box.
  69. // The first arg is the name of the window and will be across the top.
  70. // The second arg is the text that will appear in the message box.
  71. // The third arg is if the box starts minimized (yes since we want the button to open it)
  72. // The fourth arg is a parent window arg.
  73. MessageBox *pMessage = new MessageBox ("Message Window", "Here is some message box text\n You must click OK to continue.", NULL);
  74. // This command will pop up the message box and hold it there until we click
  75. // the OK button. When the OK button is clicked the message box object is destroyed.
  76. pMessage->DoModal();
  77. }
  78. MessageMapItem_t MessageBoxDemo::m_MessageMap[] =
  79. {
  80. MAP_MESSAGE( MessageBoxDemo, "ButtonClicked", OnButtonClicked ),
  81. };
  82. IMPLEMENT_PANELMAP(MessageBoxDemo, DemoPage);
  83. Panel* MessageBoxDemo_Create(Panel *parent)
  84. {
  85. return new MessageBoxDemo(parent, "MessageBoxDemo");
  86. }