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.

190 lines
4.8 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose: Core Movie Maker UI API
  4. //
  5. //=============================================================================
  6. #include "toolutils/toolmenubutton.h"
  7. #include "toolutils/toolmenubar.h"
  8. #include "toolutils/basetoolsystem.h"
  9. #include "vgui_controls/menu.h"
  10. #include "vgui_controls/KeyBindingMap.h"
  11. #include "vgui/ILocalize.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. using namespace vgui;
  15. //-----------------------------------------------------------------------------
  16. // Constructor
  17. //-----------------------------------------------------------------------------
  18. CToolMenuButton::CToolMenuButton( Panel *parent, const char *panelName, const char *text, Panel *actionTarget ) :
  19. BaseClass( parent, panelName, text ),
  20. m_pActionTarget( actionTarget )
  21. {
  22. m_pMenu = new Menu( this, "Menu" );
  23. }
  24. void CToolMenuButton::Reset()
  25. {
  26. m_Items.RemoveAll();
  27. m_pMenu->DeleteAllItems();
  28. }
  29. int CToolMenuButton::AddMenuItem( char const *itemName, const char *itemText, KeyValues *message, Panel *target, const KeyValues *userData /*= NULL*/, char const *kbcommandname /*= NULL*/ )
  30. {
  31. int id = m_pMenu->AddMenuItem(itemText, message, target, userData);
  32. MenuItem_t item;
  33. item.m_ItemID = id;
  34. if ( kbcommandname )
  35. {
  36. item.m_KeyBinding = kbcommandname;
  37. }
  38. m_Items.Insert( itemName, item );
  39. return id;
  40. }
  41. int CToolMenuButton::AddCheckableMenuItem( char const *itemName, const char *itemText, KeyValues *message, Panel *target, const KeyValues *userData /*= NULL*/, char const *kbcommandname /*= NULL*/ )
  42. {
  43. int id = m_pMenu->AddCheckableMenuItem(itemText, message, target, userData);
  44. MenuItem_t item;
  45. item.m_ItemID = id;
  46. if ( kbcommandname )
  47. {
  48. item.m_KeyBinding = kbcommandname;
  49. }
  50. m_Items.Insert( itemName, item );
  51. return id;
  52. }
  53. int CToolMenuButton::AddMenuItem( char const *itemName, const wchar_t *itemText, KeyValues *message, Panel *target, const KeyValues *userData /*= NULL*/, char const *kbcommandname /*= NULL*/ )
  54. {
  55. int id = m_pMenu->AddMenuItem(itemName, itemText, message, target, userData);
  56. MenuItem_t item;
  57. item.m_ItemID = id;
  58. if ( kbcommandname )
  59. {
  60. item.m_KeyBinding = kbcommandname;
  61. }
  62. m_Items.Insert( itemName, item );
  63. return id;
  64. }
  65. int CToolMenuButton::AddCheckableMenuItem( char const *itemName, const wchar_t *itemText, KeyValues *message, Panel *target, const KeyValues *userData /*= NULL*/, char const *kbcommandname /*= NULL*/ )
  66. {
  67. int id = m_pMenu->AddCheckableMenuItem(itemName, itemText, message, target, userData);
  68. MenuItem_t item;
  69. item.m_ItemID = id;
  70. if ( kbcommandname )
  71. {
  72. item.m_KeyBinding = kbcommandname;
  73. }
  74. m_Items.Insert( itemName, item );
  75. return id;
  76. }
  77. void CToolMenuButton::AddSeparator()
  78. {
  79. m_pMenu->AddSeparator();
  80. }
  81. void CToolMenuButton::SetItemEnabled( int itemID, bool state )
  82. {
  83. m_pMenu->SetItemEnabled( m_Items[ itemID ].m_ItemID, state );
  84. }
  85. int CToolMenuButton::FindMenuItem( char const *itemName )
  86. {
  87. int id = m_Items.Find( itemName );
  88. if ( id == m_Items.InvalidIndex() )
  89. return -1;
  90. return m_Items[ id ].m_ItemID;
  91. }
  92. MenuItem *CToolMenuButton::GetMenuItem( int itemID )
  93. {
  94. return m_pMenu->GetMenuItem( itemID );
  95. }
  96. void CToolMenuButton::AddSeparatorAfterItem( char const *itemName )
  97. {
  98. int id = FindMenuItem( itemName );
  99. if ( id != -1 )
  100. {
  101. m_pMenu->AddSeparatorAfterItem( id );
  102. }
  103. }
  104. void CToolMenuButton::MoveMenuItem( int itemID, int moveBeforeThisItemID )
  105. {
  106. m_pMenu->MoveMenuItem( itemID, moveBeforeThisItemID );
  107. }
  108. void CToolMenuButton::SetCurrentKeyBindingLabel( char const *itemName, char const *binding )
  109. {
  110. int id = FindMenuItem( itemName );
  111. if ( id != -1 )
  112. {
  113. m_pMenu->SetCurrentKeyBinding( id, binding );
  114. }
  115. }
  116. void CToolMenuButton::UpdateMenuItemKeyBindings()
  117. {
  118. if ( !m_pActionTarget )
  119. return;
  120. int c = m_Items.Count();
  121. for ( int i = 0; i < c; ++i )
  122. {
  123. if ( !m_Items[ i ].m_KeyBinding.IsValid() )
  124. continue;
  125. char const *bindingName = m_Items[ i ].m_KeyBinding.String();
  126. CUtlVector< BoundKey_t * > list;
  127. m_pActionTarget->LookupBoundKeys( bindingName, list );
  128. if ( list.Count() <= 0 )
  129. continue;
  130. BoundKey_t *kb = list[ 0 ];
  131. Assert( kb );
  132. // Found it, now convert to binding string
  133. // First do modifiers
  134. wchar_t sz[ 256 ];
  135. wcsncpy( sz, Panel::KeyCodeModifiersToDisplayString( (KeyCode)kb->keycode, kb->modifiers ), 256 );
  136. sz[ 255 ] = L'\0';
  137. char ansi[ 512 ];
  138. g_pVGuiLocalize->ConvertUnicodeToANSI( sz, ansi, sizeof( ansi ) );
  139. m_pMenu->SetCurrentKeyBinding( m_Items[ i ].m_ItemID, ansi );
  140. }
  141. }
  142. void CToolMenuButton::OnShowMenu( Menu *menu )
  143. {
  144. CToolMenuBar *bar = dynamic_cast< CToolMenuBar * >( GetParent() );
  145. if ( bar )
  146. {
  147. CBaseToolSystem *sys = bar->GetToolSystem();
  148. if ( sys )
  149. {
  150. sys->UpdateMenu( menu );
  151. }
  152. }
  153. UpdateMenuItemKeyBindings();
  154. m_pMenu->ForceCalculateWidth();
  155. }
  156. vgui::Menu *CToolMenuButton::GetMenu()
  157. {
  158. return m_pMenu;
  159. }