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.

222 lines
6.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. // This class is a message box that has two buttons, ok and cancel instead of
  5. // just the ok button of a message box. We use a message box class for the ok button
  6. // and implement another button here.
  7. //
  8. // $NoKeywords: $
  9. //=============================================================================//
  10. #include <vgui/KeyCode.h>
  11. #include <vgui_controls/QueryBox.h>
  12. #include <vgui_controls/TextImage.h>
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include <tier0/memdbgon.h>
  15. #ifndef max
  16. #define max(a,b) (((a) > (b)) ? (a) : (b))
  17. #endif
  18. using namespace vgui;
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Constructor
  21. //-----------------------------------------------------------------------------
  22. QueryBox::QueryBox(const char *title, const char *queryText, vgui::Panel *parent) : MessageBox(title, queryText,parent)
  23. {
  24. SetDeleteSelfOnClose(true);
  25. m_pCancelButton = new Button(this, "CancelButton", "#QueryBox_Cancel");
  26. m_pCancelButton->SetCommand("Cancel");
  27. m_pOkButton->SetCommand("OK");
  28. m_pCancelCommand = NULL;
  29. m_pOkCommand = NULL;
  30. m_pOkButton->SetTabPosition(1);
  31. m_pCancelButton->SetTabPosition(2);
  32. }
  33. //-----------------------------------------------------------------------------
  34. // Purpose: Constructor
  35. //-----------------------------------------------------------------------------
  36. QueryBox::QueryBox(const wchar_t *wszTitle, const wchar_t *wszQueryText,vgui::Panel *parent) : MessageBox(wszTitle, wszQueryText,parent)
  37. {
  38. SetDeleteSelfOnClose(true);
  39. m_pCancelButton = new Button(this, "CancelButton", "#QueryBox_Cancel");
  40. m_pCancelButton->SetCommand("Cancel");
  41. m_pOkButton->SetCommand("OK");
  42. m_pCancelCommand = NULL;
  43. m_pOkCommand = NULL;
  44. m_pOkButton->SetTabPosition(1);
  45. m_pCancelButton->SetTabPosition(2);
  46. }
  47. //-----------------------------------------------------------------------------
  48. // Purpose: Destructor
  49. //-----------------------------------------------------------------------------
  50. QueryBox::~QueryBox()
  51. {
  52. delete m_pCancelButton;
  53. if ( m_pOkCommand )
  54. {
  55. m_pOkCommand->deleteThis();
  56. }
  57. if ( m_pCancelCommand )
  58. {
  59. m_pCancelCommand->deleteThis();
  60. }
  61. }
  62. //-----------------------------------------------------------------------------
  63. // Purpose: Layout the window for drawing
  64. //-----------------------------------------------------------------------------
  65. void QueryBox::PerformLayout()
  66. {
  67. BaseClass::PerformLayout();
  68. int boxWidth, boxTall;
  69. GetSize(boxWidth, boxTall);
  70. int x, y, wide, tall;
  71. GetClientArea(x, y, wide, tall);
  72. wide += x;
  73. tall += y;
  74. int oldWide, oldTall;
  75. m_pCancelButton->GetSize(oldWide, oldTall);
  76. int btnWide, btnTall;
  77. m_pCancelButton->GetContentSize(btnWide, btnTall);
  78. btnWide = max(oldWide, btnWide + 10);
  79. btnTall = max(oldTall, btnTall + 10);
  80. m_pCancelButton->SetSize(btnWide, btnTall);
  81. //nt boxWidth, boxTall;
  82. GetSize(boxWidth, boxTall);
  83. // wide = max(wide, btnWide * 2 + 100);
  84. // SetSize(wide, tall);
  85. m_pOkButton->SetPos((wide/2)-(m_pOkButton->GetWide())-1 + x, tall - m_pOkButton->GetTall() - 15);
  86. m_pCancelButton->SetPos((wide/2) + x+16, tall - m_pCancelButton->GetTall() - 15);
  87. }
  88. //-----------------------------------------------------------------------------
  89. // Purpose: Handles command text from the buttons
  90. // Deletes self when closed
  91. //-----------------------------------------------------------------------------
  92. void QueryBox::OnCommand(const char *command)
  93. {
  94. if (!stricmp(command, "OK"))
  95. {
  96. OnCommand("Close");
  97. if ( m_pOkCommand )
  98. {
  99. PostActionSignal(m_pOkCommand->MakeCopy());
  100. }
  101. }
  102. else if (!stricmp(command, "Cancel"))
  103. {
  104. OnCommand("Close");
  105. if (m_pCancelCommand)
  106. {
  107. PostActionSignal(m_pCancelCommand->MakeCopy());
  108. }
  109. }
  110. BaseClass::OnCommand(command);
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Purpose: Set the keyvalues to send when ok button is hit
  114. //-----------------------------------------------------------------------------
  115. void QueryBox::SetOKCommand(KeyValues *keyValues)
  116. {
  117. if ( m_pOkCommand )
  118. {
  119. m_pOkCommand->deleteThis();
  120. }
  121. m_pOkCommand = keyValues;
  122. }
  123. //-----------------------------------------------------------------------------
  124. // Purpose: Set a value of the ok command
  125. //-----------------------------------------------------------------------------
  126. void QueryBox::SetOKCommandValue(const char *keyName, int value)
  127. {
  128. if ( !m_pOkCommand )
  129. {
  130. m_pOkCommand = new KeyValues("Command");
  131. }
  132. m_pOkCommand->SetInt(keyName, value);
  133. }
  134. //-----------------------------------------------------------------------------
  135. // Purpose: Set the keyvalues to send when the cancel button is hit
  136. //-----------------------------------------------------------------------------
  137. void QueryBox::SetCancelCommand(KeyValues *keyValues)
  138. {
  139. if ( m_pCancelCommand )
  140. {
  141. m_pCancelCommand->deleteThis();
  142. }
  143. m_pCancelCommand = keyValues;
  144. }
  145. //-----------------------------------------------------------------------------
  146. // Purpose: Sets the cancel button text
  147. //-----------------------------------------------------------------------------
  148. void QueryBox::SetCancelButtonText(const char* buttonText)
  149. {
  150. m_pCancelButton->SetText(buttonText);
  151. InvalidateLayout();
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose: Sets the cancel button text
  155. //-----------------------------------------------------------------------------
  156. void QueryBox::SetCancelButtonText(const wchar_t* wszButtonText)
  157. {
  158. m_pCancelButton->SetText(wszButtonText);
  159. InvalidateLayout();
  160. }
  161. void QueryBox::OnKeyCodeTyped( KeyCode code )
  162. {
  163. if ( code == KEY_ESCAPE )
  164. {
  165. OnCommand("Cancel");
  166. }
  167. else
  168. {
  169. Frame::OnKeyCodeTyped(code);
  170. }
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Purpose:
  174. //-----------------------------------------------------------------------------
  175. void QueryBox::OnKeyCodePressed( KeyCode code )
  176. {
  177. if ( code == KEY_XBUTTON_B )
  178. {
  179. OnCommand("Cancel");
  180. }
  181. else
  182. {
  183. Frame::OnKeyCodePressed(code);
  184. }
  185. }