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.

315 lines
6.7 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include <assert.h>
  9. #define PROTECTED_THINGS_DISABLE
  10. #include <vgui/Cursor.h>
  11. #include <vgui/IScheme.h>
  12. #include <vgui/IInput.h>
  13. #include <vgui/IPanel.h>
  14. #include <vgui/ISurface.h>
  15. #include <vgui/KeyCode.h>
  16. #include <keyvalues.h>
  17. #include <vgui/MouseCode.h>
  18. #include <vgui/IBorder.h>
  19. #include <vgui_controls/TreeViewListControl.h>
  20. #include <vgui_controls/ScrollBar.h>
  21. #include <vgui_controls/TextEntry.h>
  22. #include <vgui_controls/TreeView.h>
  23. #include <vgui_controls/Label.h>
  24. #include <vgui_controls/Button.h>
  25. #include <vgui_controls/TextImage.h>
  26. #include <vgui_controls/ImageList.h>
  27. #include <vgui_controls/ImagePanel.h>
  28. // memdbgon must be the last include file in a .cpp file!!!
  29. #include <tier0/memdbgon.h>
  30. using namespace vgui;
  31. DECLARE_BUILD_FACTORY( CTreeViewListControl );
  32. CTreeViewListControl::CTreeViewListControl( vgui::Panel *pParent, const char *pName ) :
  33. BaseClass( pParent, pName )
  34. {
  35. m_pTree = NULL;
  36. m_BorderColor.SetColor( 255, 255, 255, 255 );
  37. m_TitleBarFont = NULL;
  38. m_TitleBarHeight = 20;
  39. SetPostChildPaintEnabled( true );
  40. }
  41. void CTreeViewListControl::SetTreeView( vgui::TreeView *pTree )
  42. {
  43. m_pTree = pTree;
  44. if ( m_pTree )
  45. {
  46. m_pTree->SetParent( this );
  47. m_pTree->SetPaintBackgroundEnabled( false );
  48. }
  49. InvalidateLayout();
  50. }
  51. vgui::TreeView *CTreeViewListControl::GetTree()
  52. {
  53. return m_pTree;
  54. }
  55. int CTreeViewListControl::GetTitleBarHeight()
  56. {
  57. return m_TitleBarHeight;
  58. }
  59. void CTreeViewListControl::SetTitleBarInfo( vgui::HFont hFont, int titleBarHeight )
  60. {
  61. m_TitleBarFont = hFont;
  62. m_TitleBarHeight = titleBarHeight;
  63. InvalidateLayout();
  64. }
  65. void CTreeViewListControl::SetBorderColor( Color clr )
  66. {
  67. m_BorderColor = clr;
  68. }
  69. void CTreeViewListControl::SetNumColumns( int nColumns )
  70. {
  71. m_Columns.Purge();
  72. m_Columns.SetSize( nColumns );
  73. InvalidateLayout();
  74. }
  75. int CTreeViewListControl::GetNumColumns() const
  76. {
  77. return m_Columns.Count();
  78. }
  79. void CTreeViewListControl::SetColumnInfo( int iColumn, const char *pTitle, int width, int ciFlags )
  80. {
  81. if ( iColumn < 0 || iColumn >= m_Columns.Count() )
  82. {
  83. Assert( false );
  84. return;
  85. }
  86. CColumnInfo *pInfo = &m_Columns[iColumn];
  87. pInfo->m_Title = pTitle;
  88. pInfo->m_Width = width;
  89. pInfo->m_ciFlags = ciFlags;
  90. InvalidateLayout();
  91. }
  92. int CTreeViewListControl::GetNumRows()
  93. {
  94. return m_Rows.Count();
  95. }
  96. int CTreeViewListControl::GetTreeItemAtRow( int iRow )
  97. {
  98. if ( iRow < 0 || iRow >= m_Rows.Count() )
  99. return -1;
  100. else
  101. return m_Rows[iRow];
  102. }
  103. void CTreeViewListControl::GetGridElementBounds( int iColumn, int iRow, int &left, int &top, int &right, int &bottom )
  104. {
  105. left = m_Columns[iColumn].m_Left;
  106. right = m_Columns[iColumn].m_Right;
  107. // vgui doesn't seem to be drawing things exactly right. Like it you draw a line at (0,0) to (100,0),
  108. // then a rectangle from (1,1) to (100,100), it'll overwrite the line at the top.
  109. int treeTopBorder = 0;
  110. IBorder *treeBorder = m_pTree->GetBorder();
  111. if ( treeBorder )
  112. {
  113. int l, t, r, b;
  114. treeBorder->GetInset( l, t, r, b );
  115. treeTopBorder = t;
  116. }
  117. if ( iRow == -1 )
  118. {
  119. top = 1;
  120. bottom = m_TitleBarHeight - 2;
  121. }
  122. else if ( m_pTree )
  123. {
  124. int x, y;
  125. m_pTree->GetPos( x, y );
  126. top = treeTopBorder + m_TitleBarHeight + ( iRow * m_pTree->GetRowHeight() );
  127. bottom = top + m_pTree->GetRowHeight();
  128. }
  129. else
  130. {
  131. left = top = right = bottom = 0;
  132. }
  133. }
  134. void CTreeViewListControl::PerformLayout()
  135. {
  136. RecalculateRows();
  137. RecalculateColumns();
  138. // Reposition the tree view.
  139. if ( m_pTree && m_Columns.Count() > 0 )
  140. {
  141. int left, top, right, bottom;
  142. GetGridElementBounds( 0, -1, left, top, right, bottom );
  143. top = m_TitleBarHeight;
  144. m_pTree->SetBounds( left, top, right - left, GetTall() - top );
  145. }
  146. BaseClass::PerformLayout();
  147. }
  148. void CTreeViewListControl::RecalculateRows()
  149. {
  150. m_Rows.Purge();
  151. if ( !m_pTree || m_pTree->GetRootItemIndex() == -1 )
  152. return;
  153. int iRoot = m_pTree->GetRootItemIndex();
  154. RecalculateRows_R( iRoot );
  155. }
  156. void CTreeViewListControl::RecalculateRows_R( int index )
  157. {
  158. m_Rows.AddToTail( index );
  159. if ( !m_pTree->IsItemExpanded( index ) )
  160. return;
  161. int nChildren = m_pTree->GetNumChildren( index );
  162. for ( int i=0; i < nChildren; i++ )
  163. {
  164. int iChild = m_pTree->GetChild( index, i );
  165. RecalculateRows_R( iChild );
  166. }
  167. }
  168. int CTreeViewListControl::GetScrollBarSize()
  169. {
  170. return 0;
  171. }
  172. void CTreeViewListControl::RecalculateColumns()
  173. {
  174. int rightEdge = GetWide()-1 - GetScrollBarSize();
  175. int x = 0;
  176. int c = m_Columns.Count();
  177. for ( int i=0; i < c; i++ )
  178. {
  179. m_Columns[i].m_Left = x + 1;
  180. int cw = m_Columns[i].m_Width;
  181. if ( i == c - 1 )
  182. {
  183. cw = rightEdge - x - 2;
  184. }
  185. m_Columns[i].m_Right = x + cw - 2;
  186. x += cw;
  187. }
  188. }
  189. void CTreeViewListControl::PostChildPaint()
  190. {
  191. BaseClass::PostChildPaint();
  192. // Draw the grid lines.
  193. vgui::surface()->DrawSetColor( m_BorderColor );
  194. if ( m_Columns.Count() <= 0 )
  195. return;
  196. // Draw the horizontal lines.
  197. int endX = 0;
  198. endX = m_Columns[m_Columns.Count()-1].m_Right + 1;
  199. int bottomY = 0;
  200. for ( int i=0; i < m_Rows.Count(); i++ )
  201. {
  202. int left, top, right, bottom;
  203. GetGridElementBounds( 0, i, left, top, right, bottom );
  204. bottomY = bottom;
  205. vgui::surface()->DrawLine( 0, bottomY, endX, bottomY );
  206. }
  207. // Draw the vertical lines.
  208. int curX = 0;
  209. for ( int i=0; i < m_Columns.Count(); i++ )
  210. {
  211. vgui::surface()->DrawLine( curX, 0, curX, bottomY );
  212. curX += m_Columns[i].m_Width;
  213. }
  214. vgui::surface()->DrawLine( curX, 0, curX, bottomY );
  215. }
  216. void CTreeViewListControl::Paint()
  217. {
  218. BaseClass::Paint();
  219. // Draw the title bars.
  220. DrawTitleBars();
  221. }
  222. void CTreeViewListControl::DrawTitleBars()
  223. {
  224. int rightEdge = GetWide();
  225. for ( int i=0; i < m_Columns.Count(); i++ )
  226. {
  227. int left, top, right, bottom;
  228. GetGridElementBounds( i, -1, left, top, right, bottom );
  229. if ( left >= rightEdge )
  230. continue;
  231. vgui::surface()->DrawSetColor( 0, 0, 0, 255 );
  232. vgui::surface()->DrawFilledRect( left, top, right, bottom );
  233. vgui::surface()->DrawSetTextColor( 255, 255, 255, 255 );
  234. const char *pTitleString = m_Columns[i].m_Title.String();
  235. wchar_t unicodeString[1024];
  236. g_pVGuiLocalize->ConvertANSIToUnicode( pTitleString, unicodeString, sizeof(unicodeString) );
  237. int wide, tall;
  238. surface()->GetTextSize( m_TitleBarFont, unicodeString, wide, tall );
  239. surface()->DrawSetTextFont( m_TitleBarFont );
  240. if ( m_Columns[i].m_ciFlags & CTreeViewListControl::CI_HEADER_LEFTALIGN )
  241. {
  242. int midy = (top+bottom)/2;
  243. surface()->DrawSetTextPos( left, midy );
  244. }
  245. else
  246. {
  247. int textRight = MIN( right, rightEdge );
  248. int midx = (left+textRight)/2;
  249. int midy = (top+bottom)/2;
  250. surface()->DrawSetTextPos( midx - wide/2, midy - tall/2 );
  251. }
  252. surface()->DrawPrintText( unicodeString, strlen( pTitleString ) );
  253. }
  254. }