Counter Strike : Global Offensive Source Code
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.

131 lines
2.9 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include "labeledcommandcombobox.h"
  8. #include "engineinterface.h"
  9. #include <keyvalues.h>
  10. #include <vgui/ILocalize.h>
  11. // memdbgon must be the last include file in a .cpp file!!!
  12. #include <tier0/memdbgon.h>
  13. using namespace vgui;
  14. CLabeledCommandComboBox::CLabeledCommandComboBox( vgui::Panel *parent, const char *panelName ) : vgui::ComboBox( parent, panelName, 6, false )
  15. {
  16. AddActionSignalTarget(this);
  17. m_iCurrentSelection = -1;
  18. m_iStartSelection = -1;
  19. }
  20. CLabeledCommandComboBox::~CLabeledCommandComboBox( void )
  21. {
  22. }
  23. void CLabeledCommandComboBox::DeleteAllItems()
  24. {
  25. BaseClass::DeleteAllItems();
  26. m_Items.RemoveAll();
  27. }
  28. void CLabeledCommandComboBox::AddItem( char const *text, char const *engineCommand )
  29. {
  30. int idx = m_Items.AddToTail();
  31. COMMANDITEM *item = &m_Items[ idx ];
  32. item->comboBoxID = BaseClass::AddItem( text, NULL );
  33. Q_strncpy( item->name, text, sizeof( item->name ) );
  34. if (text[0] == '#')
  35. {
  36. // need to localize the string
  37. wchar_t *localized = g_pVGuiLocalize->Find(text);
  38. if (localized)
  39. {
  40. g_pVGuiLocalize->ConvertUnicodeToANSI(localized, item->name, sizeof(item->name));
  41. }
  42. }
  43. Q_strncpy( item->command, engineCommand, sizeof( item->command ) );
  44. }
  45. void CLabeledCommandComboBox::ActivateItem(int index)
  46. {
  47. if ( index< m_Items.Count() )
  48. {
  49. int comboBoxID = m_Items[index].comboBoxID;
  50. BaseClass::ActivateItem(comboBoxID);
  51. m_iCurrentSelection = index;
  52. }
  53. }
  54. void CLabeledCommandComboBox::SetInitialItem(int index)
  55. {
  56. if ( index< m_Items.Count() )
  57. {
  58. m_iStartSelection = index;
  59. int comboBoxID = m_Items[index].comboBoxID;
  60. ActivateItem(comboBoxID);
  61. }
  62. }
  63. void CLabeledCommandComboBox::OnTextChanged( char const *text )
  64. {
  65. int i;
  66. for ( i = 0; i < m_Items.Count(); i++ )
  67. {
  68. COMMANDITEM *item = &m_Items[ i ];
  69. if ( !stricmp( item->name, text ) )
  70. {
  71. // engine->pfnClientCmd( item->command );
  72. m_iCurrentSelection = i;
  73. break;
  74. }
  75. }
  76. if (HasBeenModified())
  77. {
  78. PostActionSignal(new KeyValues("ControlModified"));
  79. }
  80. // PostMessage( GetParent()->GetVPanel(), new vgui::KeyValues( "TextChanged", "text", text ) );
  81. }
  82. const char *CLabeledCommandComboBox::GetActiveItemCommand()
  83. {
  84. if (m_iCurrentSelection == -1)
  85. return NULL;
  86. COMMANDITEM *item = &m_Items[ m_iCurrentSelection ];
  87. return item->command;
  88. }
  89. void CLabeledCommandComboBox::ApplyChanges()
  90. {
  91. if (m_iCurrentSelection == -1)
  92. return;
  93. if (m_Items.Count() < 1)
  94. return;
  95. Assert( m_iCurrentSelection < m_Items.Count() );
  96. COMMANDITEM *item = &m_Items[ m_iCurrentSelection ];
  97. engine->ClientCmd_Unrestricted( item->command );
  98. m_iStartSelection = m_iCurrentSelection;
  99. }
  100. bool CLabeledCommandComboBox::HasBeenModified()
  101. {
  102. return m_iStartSelection != m_iCurrentSelection;
  103. }
  104. void CLabeledCommandComboBox::Reset()
  105. {
  106. if (m_iStartSelection != -1)
  107. {
  108. ActivateItem(m_iStartSelection);
  109. }
  110. }