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.

236 lines
6.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include <vgui_controls/InputDialog.h>
  7. #include <vgui_controls/Label.h>
  8. #include <vgui_controls/Button.h>
  9. #include <vgui_controls/TextEntry.h>
  10. #include "tier1/KeyValues.h"
  11. #include "vgui/IInput.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include <tier0/memdbgon.h>
  14. using namespace vgui;
  15. //-----------------------------------------------------------------------------
  16. // Purpose: Constructor
  17. //-----------------------------------------------------------------------------
  18. BaseInputDialog::BaseInputDialog( vgui::Panel *parent, const char *title ) :
  19. BaseClass( parent, NULL )
  20. {
  21. m_pContextKeyValues = NULL;
  22. SetDeleteSelfOnClose( true );
  23. SetTitle(title, true);
  24. SetSize(320, 180);
  25. SetSizeable( false );
  26. m_pCancelButton = new Button(this, "CancelButton", "#VGui_Cancel");
  27. m_pOKButton = new Button(this, "OKButton", "#VGui_OK");
  28. m_pCancelButton->SetCommand("Cancel");
  29. m_pOKButton->SetCommand("OK");
  30. m_pOKButton->SetAsDefaultButton( true );
  31. if ( parent )
  32. {
  33. AddActionSignalTarget( parent );
  34. }
  35. }
  36. BaseInputDialog::~BaseInputDialog()
  37. {
  38. CleanUpContextKeyValues();
  39. }
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Cleans up the keyvalues
  42. //-----------------------------------------------------------------------------
  43. void BaseInputDialog::CleanUpContextKeyValues()
  44. {
  45. if ( m_pContextKeyValues )
  46. {
  47. m_pContextKeyValues->deleteThis();
  48. m_pContextKeyValues = NULL;
  49. }
  50. }
  51. //-----------------------------------------------------------------------------
  52. // Purpose:
  53. //-----------------------------------------------------------------------------
  54. void BaseInputDialog::DoModal( KeyValues *pContextKeyValues )
  55. {
  56. CleanUpContextKeyValues();
  57. m_pContextKeyValues = pContextKeyValues;
  58. BaseClass::DoModal();
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Purpose: lays out controls
  62. //-----------------------------------------------------------------------------
  63. void BaseInputDialog::PerformLayout()
  64. {
  65. BaseClass::PerformLayout();
  66. int w, h;
  67. GetSize( w, h );
  68. // lay out all the controls
  69. int topy = IsSmallCaption() ? 15 : 30;
  70. int halfw = w / 2;
  71. PerformLayout( 12, topy, w - 24, h - 100 );
  72. m_pOKButton->SetBounds( halfw - 84, h - 30, 72, 24 );
  73. m_pCancelButton->SetBounds( halfw + 12, h - 30, 72, 24 );
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Purpose: handles button commands
  77. //-----------------------------------------------------------------------------
  78. void BaseInputDialog::OnCommand(const char *command)
  79. {
  80. KeyValues *kv = NULL;
  81. if ( !stricmp( command, "OK" ) )
  82. {
  83. kv = new KeyValues( "InputCompleted" );
  84. kv->SetPtr( "dialog", this );
  85. }
  86. else if ( !stricmp( command, "Cancel" ) )
  87. {
  88. kv = new KeyValues( "InputCanceled" );
  89. }
  90. else
  91. {
  92. BaseClass::OnCommand( command );
  93. return;
  94. }
  95. if ( m_pContextKeyValues )
  96. {
  97. kv->AddSubKey( m_pContextKeyValues );
  98. m_pContextKeyValues = NULL;
  99. }
  100. PostActionSignal( kv );
  101. CloseModal();
  102. }
  103. //-----------------------------------------------------------------------------
  104. // Purpose: Utility dialog, used to ask yes/no questions of the user
  105. //-----------------------------------------------------------------------------
  106. InputMessageBox::InputMessageBox( vgui::Panel *parent, const char *title, char const *prompt )
  107. : BaseClass( parent, title )
  108. {
  109. SetSize( 320, 120 );
  110. m_pPrompt = new Label( this, "Prompt", prompt );
  111. }
  112. InputMessageBox::~InputMessageBox()
  113. {
  114. }
  115. void InputMessageBox::PerformLayout( int x, int y, int w, int h )
  116. {
  117. m_pPrompt->SetBounds( x, y, w, 24 );
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose: Constructor
  121. //-----------------------------------------------------------------------------
  122. InputDialog::InputDialog(vgui::Panel *parent, const char *title, char const *prompt, char const *defaultValue /*=""*/ ) :
  123. BaseClass(parent, title)
  124. {
  125. SetSize( 320, 120 );
  126. m_pPrompt = new Label( this, "Prompt", prompt );
  127. m_pInput = new TextEntry( this, "Text" );
  128. m_pInput->SetText( defaultValue );
  129. m_pInput->SelectAllText( true );
  130. m_pInput->RequestFocus();
  131. }
  132. InputDialog::~InputDialog()
  133. {
  134. }
  135. //-----------------------------------------------------------------------------
  136. // Sets the dialog to be multiline
  137. //-----------------------------------------------------------------------------
  138. void InputDialog::SetMultiline( bool state )
  139. {
  140. m_pInput->SetMultiline( state );
  141. m_pInput->SetCatchEnterKey( state );
  142. }
  143. //-----------------------------------------------------------------------------
  144. // Allow numeric input only
  145. //-----------------------------------------------------------------------------
  146. void InputDialog::AllowNumericInputOnly( bool bOnlyNumeric )
  147. {
  148. if ( m_pInput )
  149. {
  150. m_pInput->SetAllowNumericInputOnly( bOnlyNumeric );
  151. }
  152. }
  153. //-----------------------------------------------------------------------------
  154. // Purpose: lays out controls
  155. //-----------------------------------------------------------------------------
  156. void InputDialog::PerformLayout( int x, int y, int w, int h )
  157. {
  158. m_pPrompt->SetBounds( x, y, w, 24 );
  159. m_pInput ->SetBounds( x, y + 30, w, m_pInput->IsMultiline() ? h - 30 : 24 );
  160. }
  161. //-----------------------------------------------------------------------------
  162. // Purpose: handles button commands
  163. //-----------------------------------------------------------------------------
  164. void InputDialog::OnCommand(const char *command)
  165. {
  166. // overriding OnCommand for backwards compatability
  167. // it'd be nice at some point to find all uses of InputDialog and just use BaseInputDialog's OnCommand
  168. if (!stricmp(command, "OK"))
  169. {
  170. int nTextLength = m_pInput->GetTextLength() + 1;
  171. char* txt = (char*)_alloca( nTextLength * sizeof(char) );
  172. m_pInput->GetText( txt, nTextLength );
  173. KeyValues *kv = new KeyValues( "InputCompleted", "text", txt );
  174. if ( m_pContextKeyValues )
  175. {
  176. kv->AddSubKey( m_pContextKeyValues );
  177. m_pContextKeyValues = NULL;
  178. }
  179. PostActionSignal( kv );
  180. CloseModal();
  181. }
  182. else if (!stricmp(command, "Cancel"))
  183. {
  184. KeyValues *kv = new KeyValues( "InputCanceled" );
  185. if ( m_pContextKeyValues )
  186. {
  187. kv->AddSubKey( m_pContextKeyValues );
  188. m_pContextKeyValues = NULL;
  189. }
  190. PostActionSignal( kv );
  191. CloseModal();
  192. }
  193. else
  194. {
  195. BaseClass::OnCommand(command);
  196. }
  197. }