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.

120 lines
2.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. // Author: Matthew D. Campbell ([email protected]), 2003
  8. #include <vgui/KeyCode.h>
  9. #include "CvarTextEntry.h"
  10. #include "TextEntryBox.h"
  11. #include <vgui_controls/TextEntry.h>
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include <tier0/memdbgon.h>
  14. #ifndef max
  15. #define max(a,b) (((a) > (b)) ? (a) : (b))
  16. #endif
  17. using namespace vgui;
  18. //--------------------------------------------------------------------------------------------------------------
  19. CTextEntryBox::CTextEntryBox(const char *title, const char *queryText, const char *entryText, bool isCvar, vgui::Panel *parent) : QueryBox(title, queryText,parent)
  20. {
  21. if (isCvar)
  22. {
  23. m_pEntry = m_pCvarEntry = new CCvarTextEntry( this, "TextEntry", entryText );
  24. }
  25. else
  26. {
  27. m_pEntry = new TextEntry( this, "TextEntry" );
  28. m_pCvarEntry = NULL;
  29. }
  30. m_pEntry->SetTabPosition(3);
  31. m_pEntry->RequestFocus();
  32. m_pEntry->GotoTextEnd();
  33. }
  34. //--------------------------------------------------------------------------------------------------------------
  35. CTextEntryBox::~CTextEntryBox()
  36. {
  37. delete m_pEntry;
  38. }
  39. //--------------------------------------------------------------------------------------------------------------
  40. void CTextEntryBox::ShowWindow(Frame *pFrameOver)
  41. {
  42. BaseClass::ShowWindow( pFrameOver );
  43. m_pEntry->RequestFocus();
  44. InvalidateLayout();
  45. }
  46. //--------------------------------------------------------------------------------------------------------------
  47. void CTextEntryBox::PerformLayout()
  48. {
  49. BaseClass::PerformLayout();
  50. int x, y, wide, tall;
  51. GetClientArea(x, y, wide, tall);
  52. wide += x;
  53. tall += y;
  54. const int borderW = 10;
  55. int labelW, labelH;
  56. int entryW, entryH;
  57. m_pMessageLabel->GetSize( labelW, labelH );
  58. entryW = max(120, wide - borderW - borderW - borderW - labelW);
  59. entryH = max(24, labelH);
  60. m_pEntry->SetSize( entryW, entryH );
  61. int boxWidth, boxTall;
  62. GetSize(boxWidth, boxTall);
  63. if (boxWidth < labelW + entryW + borderW*3)
  64. SetSize( labelW + entryW + borderW*3, boxTall );
  65. m_pMessageLabel->GetPos( x, y );
  66. m_pMessageLabel->SetPos( borderW, y - (entryH - labelH)/2 );
  67. m_pEntry->SetPos( borderW + m_pMessageLabel->GetWide() + borderW, y - (entryH - labelH) );
  68. }
  69. //--------------------------------------------------------------------------------------------------------------
  70. void CTextEntryBox::OnCommand(const char *command)
  71. {
  72. if (!stricmp(command, "Ok"))
  73. {
  74. if (m_pCvarEntry)
  75. {
  76. m_pCvarEntry->ApplyChanges( true );
  77. }
  78. }
  79. BaseClass::OnCommand(command);
  80. }
  81. //--------------------------------------------------------------------------------------------------------------
  82. void CTextEntryBox::OnKeyCodeTyped(KeyCode code)
  83. {
  84. if (code == KEY_ESCAPE)
  85. {
  86. OnCommand("Cancel");
  87. }
  88. if (code == KEY_ENTER)
  89. {
  90. OnCommand("Ok");
  91. }
  92. else
  93. {
  94. BaseClass::OnKeyCodeTyped(code);
  95. }
  96. }