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.

212 lines
6.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include <vgui_controls/CheckButtonList.h>
  7. #include <vgui_controls/CheckButton.h>
  8. #include <vgui_controls/ScrollBar.h>
  9. #include <KeyValues.h>
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include <tier0/memdbgon.h>
  12. using namespace vgui;
  13. //-----------------------------------------------------------------------------
  14. // Purpose: Constructor
  15. //-----------------------------------------------------------------------------
  16. CheckButtonList::CheckButtonList(Panel *parent, const char *name) : BaseClass(parent, name)
  17. {
  18. m_pScrollBar = new ScrollBar(this, NULL, true);
  19. }
  20. //-----------------------------------------------------------------------------
  21. // Purpose: Destructor
  22. //-----------------------------------------------------------------------------
  23. CheckButtonList::~CheckButtonList()
  24. {
  25. RemoveAll();
  26. }
  27. //-----------------------------------------------------------------------------
  28. // Purpose: adds a check button to the list
  29. //-----------------------------------------------------------------------------
  30. int CheckButtonList::AddItem(const char *itemText, bool startsSelected, KeyValues *userData)
  31. {
  32. CheckItem_t newItem;
  33. newItem.checkButton = new vgui::CheckButton(this, NULL, itemText);
  34. newItem.checkButton->SetSilentMode( true );
  35. newItem.checkButton->SetSelected(startsSelected);
  36. newItem.checkButton->SetSilentMode( false );
  37. newItem.checkButton->AddActionSignalTarget(this);
  38. newItem.userData = userData;
  39. InvalidateLayout();
  40. return m_CheckItems.AddToTail(newItem);
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: clears the list
  44. //-----------------------------------------------------------------------------
  45. void CheckButtonList::RemoveAll()
  46. {
  47. for (int i = 0; i < m_CheckItems.Count(); i++)
  48. {
  49. m_CheckItems[i].checkButton->MarkForDeletion();
  50. if (m_CheckItems[i].userData)
  51. {
  52. m_CheckItems[i].userData->deleteThis();
  53. }
  54. }
  55. m_CheckItems.RemoveAll();
  56. }
  57. //-----------------------------------------------------------------------------
  58. // Purpose: returns the number of items in list that are checked
  59. //-----------------------------------------------------------------------------
  60. int CheckButtonList::GetCheckedItemCount()
  61. {
  62. int count = 0;
  63. for (int i = 0; i < m_CheckItems.Count(); i++)
  64. {
  65. if (m_CheckItems[i].checkButton->IsSelected())
  66. {
  67. count++;
  68. }
  69. }
  70. return count;
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Purpose: lays out buttons
  74. //-----------------------------------------------------------------------------
  75. void CheckButtonList::PerformLayout()
  76. {
  77. BaseClass::PerformLayout();
  78. // get sizes
  79. int x = 4, y = 4, wide = GetWide() - ((x * 2) + m_pScrollBar->GetWide()), tall = 22;
  80. // set scrollbar
  81. int totalHeight = y + (m_CheckItems.Count() * tall);
  82. if (totalHeight > GetTall())
  83. {
  84. m_pScrollBar->SetRange(0, totalHeight + 1);
  85. m_pScrollBar->SetRangeWindow(GetTall());
  86. m_pScrollBar->SetVisible(true);
  87. m_pScrollBar->SetBounds(GetWide() - 21, 0, 19, GetTall() - 2);
  88. SetPaintBorderEnabled(true);
  89. y -= m_pScrollBar->GetValue();
  90. }
  91. else
  92. {
  93. m_pScrollBar->SetVisible(false);
  94. SetPaintBorderEnabled(false);
  95. }
  96. // position the items
  97. for (int i = 0; i < m_CheckItems.Count(); i++)
  98. {
  99. CheckButton *btn = m_CheckItems[i].checkButton;
  100. btn->SetBounds(x, y, wide, tall);
  101. y += tall;
  102. }
  103. }
  104. //-----------------------------------------------------------------------------
  105. // Purpose: Sets the border on the window
  106. //-----------------------------------------------------------------------------
  107. void CheckButtonList::ApplySchemeSettings(IScheme *pScheme)
  108. {
  109. BaseClass::ApplySchemeSettings(pScheme);
  110. SetBorder(pScheme->GetBorder("ButtonDepressedBorder"));
  111. }
  112. //-----------------------------------------------------------------------------
  113. // Purpose: iteration
  114. //-----------------------------------------------------------------------------
  115. bool CheckButtonList::IsItemIDValid(int itemID)
  116. {
  117. return m_CheckItems.IsValidIndex(itemID);
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Purpose: iteration
  121. //-----------------------------------------------------------------------------
  122. int CheckButtonList::GetHighestItemID()
  123. {
  124. return m_CheckItems.Count() - 1;
  125. }
  126. //-----------------------------------------------------------------------------
  127. // Purpose: iteration
  128. //-----------------------------------------------------------------------------
  129. KeyValues *CheckButtonList::GetItemData(int itemID)
  130. {
  131. return m_CheckItems[itemID].userData;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Purpose: data accessor
  135. //-----------------------------------------------------------------------------
  136. int CheckButtonList::GetItemCount()
  137. {
  138. return m_CheckItems.Count();
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: data accessor
  142. //-----------------------------------------------------------------------------
  143. bool CheckButtonList::IsItemChecked(int itemID)
  144. {
  145. return m_CheckItems[itemID].checkButton->IsSelected();
  146. }
  147. //-----------------------------------------------------------------------------
  148. // Purpose: Sets the state of the check button
  149. //-----------------------------------------------------------------------------
  150. void CheckButtonList::SetItemCheckable(int itemID, bool state)
  151. {
  152. m_CheckItems[itemID].checkButton->SetCheckButtonCheckable(state);
  153. }
  154. //-----------------------------------------------------------------------------
  155. // Purpose: Forwards up check button selected message
  156. //-----------------------------------------------------------------------------
  157. void CheckButtonList::OnCheckButtonChecked( KeyValues *pParams )
  158. {
  159. vgui::Panel *pPanel = (vgui::Panel *)pParams->GetPtr( "panel" );
  160. int c = m_CheckItems.Count();
  161. for ( int i = 0; i < c; ++i )
  162. {
  163. if ( pPanel == m_CheckItems[i].checkButton )
  164. {
  165. KeyValues *kv = new KeyValues( "CheckButtonChecked", "itemid", i );
  166. kv->SetInt( "state", pParams->GetInt( "state" ) );
  167. PostActionSignal( kv );
  168. break;
  169. }
  170. }
  171. }
  172. //-----------------------------------------------------------------------------
  173. // Purpose: updates from scrollbar movement
  174. //-----------------------------------------------------------------------------
  175. void CheckButtonList::OnScrollBarSliderMoved()
  176. {
  177. InvalidateLayout();
  178. Repaint();
  179. }
  180. //-----------------------------------------------------------------------------
  181. // Purpose: Mouse wheeled
  182. //-----------------------------------------------------------------------------
  183. void CheckButtonList::OnMouseWheeled(int delta)
  184. {
  185. int val = m_pScrollBar->GetValue();
  186. val -= (delta * 15);
  187. m_pScrollBar->SetValue(val);
  188. }