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.

306 lines
8.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <assert.h>
  8. #include <vgui_controls/ScrollBar.h>
  9. #include <vgui_controls/Label.h>
  10. #include <vgui_controls/Button.h>
  11. #include <KeyValues.h>
  12. #include <vgui/MouseCode.h>
  13. #include <vgui/KeyCode.h>
  14. #include <vgui/IInput.h>
  15. #include <vgui/IScheme.h>
  16. #include <vgui/ISurface.h>
  17. #include "PanelListPanel.h"
  18. // memdbgon must be the last include file in a .cpp file!!!
  19. #include "tier0/memdbgon.h"
  20. using namespace vgui;
  21. class VScrollBarReversedButtons : public ScrollBar
  22. {
  23. public:
  24. VScrollBarReversedButtons( Panel *parent, const char *panelName, bool vertical );
  25. virtual void ApplySchemeSettings( IScheme *pScheme );
  26. };
  27. VScrollBarReversedButtons::VScrollBarReversedButtons( Panel *parent, const char *panelName, bool vertical ) : ScrollBar( parent, panelName, vertical )
  28. {
  29. }
  30. void VScrollBarReversedButtons::ApplySchemeSettings( IScheme *pScheme )
  31. {
  32. ScrollBar::ApplySchemeSettings( pScheme );
  33. Button *pButton;
  34. pButton = GetButton( 0 );
  35. pButton->SetArmedColor( pButton->GetSchemeColor("DimBaseText", pScheme), pButton->GetBgColor());
  36. pButton->SetDepressedColor( pButton->GetSchemeColor("DimBaseText", pScheme), pButton->GetBgColor());
  37. pButton->SetDefaultColor( pButton->GetFgColor(), pButton->GetBgColor());
  38. pButton = GetButton( 1 );
  39. pButton->SetArmedColor( pButton->GetSchemeColor("DimBaseText", pScheme), pButton->GetBgColor());
  40. pButton->SetDepressedColor( pButton->GetSchemeColor("DimBaseText", pScheme), pButton->GetBgColor());
  41. pButton->SetDefaultColor( pButton->GetFgColor(), pButton->GetBgColor());
  42. }
  43. //-----------------------------------------------------------------------------
  44. // Purpose:
  45. // Input : x -
  46. // y -
  47. // wide -
  48. // tall -
  49. // Output :
  50. //-----------------------------------------------------------------------------
  51. CPanelListPanel::CPanelListPanel( vgui::Panel *parent, char const *panelName, bool inverseButtons ) : Panel( parent, panelName )
  52. {
  53. SetBounds( 0, 0, 100, 100 );
  54. _sliderYOffset = 0;
  55. if (inverseButtons)
  56. {
  57. _vbar = new VScrollBarReversedButtons(this, "CPanelListPanelVScroll", true );
  58. }
  59. else
  60. {
  61. _vbar = new ScrollBar(this, "CPanelListPanelVScroll", true );
  62. }
  63. _vbar->SetBounds( 0, 0, 20, 20 );
  64. _vbar->SetVisible(false);
  65. _vbar->AddActionSignalTarget( this );
  66. _embedded = new Panel( this, "PanelListEmbedded" );
  67. _embedded->SetBounds( 0, 0, 20, 20 );
  68. _embedded->SetPaintBackgroundEnabled( false );
  69. _embedded->SetPaintBorderEnabled( false );
  70. }
  71. //-----------------------------------------------------------------------------
  72. // Purpose:
  73. //-----------------------------------------------------------------------------
  74. CPanelListPanel::~CPanelListPanel()
  75. {
  76. // free data from table
  77. DeleteAllItems();
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Purpose:
  81. //-----------------------------------------------------------------------------
  82. int CPanelListPanel::computeVPixelsNeeded( void )
  83. {
  84. int pixels =0;
  85. DATAITEM *item;
  86. Panel *panel;
  87. for ( int i = 0; i < _dataItems.GetCount(); i++ )
  88. {
  89. item = _dataItems[ i ];
  90. if ( !item )
  91. continue;
  92. panel = item->panel;
  93. if ( !panel )
  94. continue;
  95. int w, h;
  96. panel->GetSize( w, h );
  97. pixels += h;
  98. }
  99. pixels+=5; // add a buffer after the last item
  100. return pixels;
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Purpose: Returns the panel to use to render a cell
  104. // Input : column -
  105. // row -
  106. // Output : Panel
  107. //-----------------------------------------------------------------------------
  108. Panel *CPanelListPanel::GetCellRenderer( int row )
  109. {
  110. DATAITEM *item = _dataItems[ row ];
  111. if ( item )
  112. {
  113. Panel *panel = item->panel;
  114. return panel;
  115. }
  116. return NULL;
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose: adds an item to the view
  120. // data->GetName() is used to uniquely identify an item
  121. // data sub items are matched against column header name to be used in the table
  122. // Input : *item -
  123. //-----------------------------------------------------------------------------
  124. int CPanelListPanel::AddItem( Panel *panel )
  125. {
  126. InvalidateLayout();
  127. DATAITEM *newitem = new DATAITEM;
  128. newitem->panel = panel;
  129. panel->SetParent( _embedded );
  130. return _dataItems.PutElement( newitem );
  131. }
  132. //-----------------------------------------------------------------------------
  133. // Purpose:
  134. // Output :
  135. //-----------------------------------------------------------------------------
  136. int CPanelListPanel::GetItemCount( void )
  137. {
  138. return _dataItems.GetCount();
  139. }
  140. //-----------------------------------------------------------------------------
  141. // Purpose: returns pointer to data the row holds
  142. // Input : itemIndex -
  143. // Output : KeyValues
  144. //-----------------------------------------------------------------------------
  145. Panel *CPanelListPanel::GetItem(int itemIndex)
  146. {
  147. if ( itemIndex < 0 || itemIndex >= _dataItems.GetCount() )
  148. return NULL;
  149. return _dataItems[itemIndex]->panel;
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Purpose:
  153. // Input : itemIndex -
  154. // Output : DATAITEM
  155. //-----------------------------------------------------------------------------
  156. CPanelListPanel::DATAITEM *CPanelListPanel::GetDataItem( int itemIndex )
  157. {
  158. if ( itemIndex < 0 || itemIndex >= _dataItems.GetCount() )
  159. return NULL;
  160. return _dataItems[ itemIndex ];
  161. }
  162. //-----------------------------------------------------------------------------
  163. // Purpose:
  164. // Input : index -
  165. //-----------------------------------------------------------------------------
  166. void CPanelListPanel::RemoveItem(int itemIndex)
  167. {
  168. DATAITEM *item = _dataItems[ itemIndex ];
  169. delete item->panel;
  170. delete item;
  171. _dataItems.RemoveElementAt(itemIndex);
  172. InvalidateLayout();
  173. }
  174. //-----------------------------------------------------------------------------
  175. // Purpose: clears and deletes all the memory used by the data items
  176. //-----------------------------------------------------------------------------
  177. void CPanelListPanel::DeleteAllItems()
  178. {
  179. for (int i = 0; i < _dataItems.GetCount(); i++)
  180. {
  181. if ( _dataItems[i] )
  182. {
  183. delete _dataItems[i]->panel;
  184. }
  185. delete _dataItems[i];
  186. }
  187. _dataItems.RemoveAll();
  188. InvalidateLayout();
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Purpose:
  192. //-----------------------------------------------------------------------------
  193. void CPanelListPanel::OnMouseWheeled(int delta)
  194. {
  195. int val = _vbar->GetValue();
  196. val -= (delta * 3 * 5);
  197. _vbar->SetValue(val);
  198. }
  199. //-----------------------------------------------------------------------------
  200. // Purpose: relayouts out the panel after any internal changes
  201. //-----------------------------------------------------------------------------
  202. void CPanelListPanel::PerformLayout()
  203. {
  204. int wide, tall;
  205. GetSize( wide, tall );
  206. int vpixels = computeVPixelsNeeded();
  207. //!! need to make it recalculate scroll positions
  208. _vbar->SetVisible(true);
  209. _vbar->SetEnabled(false);
  210. _vbar->SetRange( 0, vpixels - tall + 24);
  211. _vbar->SetRangeWindow( 24 /*vpixels / 10*/ );
  212. _vbar->SetButtonPressedScrollValue( 24 );
  213. _vbar->SetPos(wide - 20, _sliderYOffset);
  214. _vbar->SetSize(18, tall - 2 - _sliderYOffset);
  215. _vbar->InvalidateLayout();
  216. int top = _vbar->GetValue();
  217. _embedded->SetPos( 0, -top );
  218. _embedded->SetSize( wide-20, vpixels );
  219. // Now lay out the controls on the embedded panel
  220. int y = 0;
  221. int h = 0;
  222. for ( int i = 0; i < _dataItems.GetCount(); i++, y += h )
  223. {
  224. DATAITEM *item = _dataItems[ i ];
  225. if ( !item || !item->panel )
  226. continue;
  227. h = item->panel->GetTall();
  228. item->panel->SetBounds( 8, y, wide-36, h );
  229. }
  230. }
  231. //-----------------------------------------------------------------------------
  232. // Purpose:
  233. //-----------------------------------------------------------------------------
  234. void CPanelListPanel::PaintBackground()
  235. {
  236. Panel::PaintBackground();
  237. }
  238. //-----------------------------------------------------------------------------
  239. // Purpose:
  240. // Input : *inResourceData -
  241. //-----------------------------------------------------------------------------
  242. void CPanelListPanel::ApplySchemeSettings(IScheme *pScheme)
  243. {
  244. Panel::ApplySchemeSettings(pScheme);
  245. SetBorder(pScheme->GetBorder("ButtonDepressedBorder"));
  246. SetBgColor(GetSchemeColor("Label.BgColor", GetBgColor(), pScheme));
  247. // _labelFgColor = GetSchemeColor("WindowFgColor");
  248. // _selectionFgColor = GetSchemeColor("ListSelectionFgColor", _labelFgColor);
  249. }
  250. void CPanelListPanel::OnSliderMoved( int position )
  251. {
  252. InvalidateLayout();
  253. Repaint();
  254. }
  255. //-----------------------------------------------------------------------------
  256. //-----------------------------------------------------------------------------
  257. void CPanelListPanel::SetSliderYOffset( int pixels )
  258. {
  259. _sliderYOffset = pixels;
  260. }