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.

351 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef SECTIONEDLISTPANEL_H
  8. #define SECTIONEDLISTPANEL_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <utlvector.h>
  13. #include <utllinkedlist.h>
  14. #include <vgui/VGUI.h>
  15. #include <vgui_controls/Panel.h>
  16. #include <vgui_controls/PHandle.h>
  17. #include <vgui_controls/Label.h>
  18. namespace vgui
  19. {
  20. class SectionedListPanel;
  21. class SectionedListPanelHeader;
  22. class CItemButton;
  23. // sorting function, should return true if itemID1 should be displayed before itemID2
  24. typedef bool (*SectionSortFunc_t)(SectionedListPanel *list, int itemID1, int itemID2);
  25. //-----------------------------------------------------------------------------
  26. // Purpose: List panel control that is divided up into discrete sections
  27. //-----------------------------------------------------------------------------
  28. class SectionedListPanel : public Panel
  29. {
  30. DECLARE_CLASS_SIMPLE( SectionedListPanel, Panel );
  31. public:
  32. SectionedListPanel(vgui::Panel *parent, const char *name);
  33. ~SectionedListPanel();
  34. // adds a new section; returns false if section already exists
  35. virtual void AddSection(int sectionID, const char *name, SectionSortFunc_t sortFunc = NULL);
  36. virtual void AddSection(int sectionID, const wchar_t *name, SectionSortFunc_t sortFunc = NULL);
  37. virtual void AddSection(int sectionID, SectionedListPanelHeader *pHeader, SectionSortFunc_t sortFunc = NULL);
  38. // clears all the sections - leaves the items in place
  39. virtual void RemoveAllSections();
  40. // modifies section info
  41. virtual void SetSectionFgColor(int sectionID, Color color);
  42. virtual void SetSectionDividerColor( int sectionID, Color color);
  43. // forces a section to always be visible
  44. virtual void SetSectionAlwaysVisible(int sectionID, bool visible = true);
  45. virtual void SetSectionMinimumHeight(int sectionID, int iMinimumHeight);
  46. // adds a new column to a section
  47. enum ColumnFlags_e
  48. {
  49. HEADER_IMAGE = 0x01, // set if the header for the column is an image instead of text
  50. COLUMN_IMAGE = 0x02, // set if the column contains an image instead of text (images are looked up by index from the ImageList) (see SetImageList below)
  51. COLUMN_BRIGHT = 0x04, // set if the column text should be the bright color
  52. COLUMN_CENTER = 0x08, // set to center the text/image in the column
  53. COLUMN_RIGHT = 0x10, // set to right-align the text in the column
  54. };
  55. virtual bool AddColumnToSection(int sectionID, const char *columnName, const char *columnText, int columnFlags, int width, HFont fallbackFont = INVALID_FONT );
  56. virtual bool AddColumnToSection(int sectionID, const char *columnName, const wchar_t *columnText, int columnFlags, int width, HFont fallbackFont = INVALID_FONT );
  57. // modifies the text in an existing column
  58. virtual bool ModifyColumn(int sectionID, const char *columnName, const wchar_t *columnText);
  59. // adds an item to the list; returns the itemID of the new item
  60. virtual int AddItem(int sectionID, const KeyValues *data);
  61. // modifies an existing item; returns false if the item does not exist
  62. virtual bool ModifyItem(int itemID, int sectionID, const KeyValues *data);
  63. // removes an item from the list; returns false if the item does not exist or is already removed
  64. virtual bool RemoveItem(int itemID);
  65. // clears the list
  66. virtual void RemoveAll() { DeleteAllItems(); }
  67. // DeleteAllItems() is deprecated, use RemoveAll();
  68. virtual void DeleteAllItems();
  69. // set the text color of an item
  70. virtual void SetItemFgColor(int itemID, Color color);
  71. //=============================================================================
  72. // HPE_BEGIN:
  73. // [menglish] Getters and setters for several item and section objects
  74. //=============================================================================
  75. virtual void SetItemBgColor( int itemID, Color color );
  76. virtual int GetColumnIndexByName(int sectionID, char* name);
  77. virtual int GetLineSpacing() { return m_iLineSpacing; }
  78. //=============================================================================
  79. // HPE_END
  80. //=============================================================================
  81. virtual void SetItemFont( int itemID, HFont font );
  82. virtual void SetItemEnabled( int itemID, bool bEnabled );
  83. /* MESSAGES SENT:
  84. "RowSelected"
  85. "itemID" - the selected item id, -1 if nothing selected
  86. // when an item has been clicked on
  87. "RowContextMenu" "itemID"
  88. "RowLeftClick" "itemID"
  89. "RowDoubleLeftClick" "itemID"
  90. */
  91. // returns the number of columns in a section
  92. virtual int GetColumnCountBySection(int sectionID);
  93. // returns the name of a column by section and column index; returns NULL if there are no more columns
  94. // valid range of columnIndex is [0, GetColumnCountBySection)
  95. virtual const char *GetColumnNameBySection(int sectionID, int columnIndex);
  96. virtual const wchar_t *GetColumnTextBySection(int sectionID, int columnIndex);
  97. virtual int GetColumnFlagsBySection(int sectionID, int columnIndex);
  98. virtual int GetColumnWidthBySection(int sectionID, int columnIndex);
  99. virtual HFont GetColumnFallbackFontBySection( int sectionID, int columnIndex );
  100. // returns the id of the currently selected item, -1 if nothing is selected
  101. virtual int GetSelectedItem();
  102. // sets which item is currently selected
  103. virtual void SetSelectedItem(int itemID);
  104. // remove selection
  105. virtual void ClearSelection( void );
  106. // returns the data of a selected item
  107. // InvalidateItem(itemID) needs to be called if the KeyValues are modified
  108. virtual KeyValues *GetItemData(int itemID);
  109. // returns what section an item is in
  110. virtual int GetItemSection(int itemID);
  111. // forces an item to redraw (use when keyvalues have been modified)
  112. virtual void InvalidateItem(int itemID);
  113. // returns true if the itemID is valid for use
  114. virtual bool IsItemIDValid(int itemID);
  115. virtual int GetHighestItemID();
  116. // returns the number of items (ignoring section dividers)
  117. virtual int GetItemCount();
  118. // returns the item ID from the row, again ignoring section dividers - valid from [0, GetItemCount )
  119. virtual int GetItemIDFromRow(int row);
  120. // returns the row that this itemID occupies. -1 if the itemID is invalid
  121. virtual int GetRowFromItemID(int itemID);
  122. // gets the local coordinates of a cell
  123. virtual bool GetCellBounds(int itemID, int column, int &x, int &y, int &wide, int &tall);
  124. // Gets the coordinates of a section header
  125. virtual bool GetSectionHeaderBounds(int sectionID, int &x, int &y, int &wide, int &tall);
  126. //=============================================================================
  127. // HPE_BEGIN:
  128. // [menglish] Get the bounds of an item or column.
  129. //=============================================================================
  130. // gets the local coordinates of a cell using the max width for every column
  131. virtual bool GetMaxCellBounds(int itemID, int column, int &x, int &y, int &wide, int &tall);
  132. // gets the local coordinates of an item
  133. virtual bool GetItemBounds(int itemID, int &x, int &y, int &wide, int &tall);
  134. // [tj] Accessors for clickability
  135. void SetClickable(bool clickable) { m_clickable = clickable; }
  136. bool IsClickable() { return m_clickable; }
  137. // [tj] Accessors for header drawing
  138. void SetDrawHeaders(bool drawHeaders) { m_bDrawSectionHeaders = drawHeaders; }
  139. bool GetDrawHeaders() { return m_bDrawSectionHeaders; }
  140. //=============================================================================
  141. // HPE_END
  142. //=============================================================================
  143. // set up a field for editing
  144. virtual void EnterEditMode(int itemID, int column, vgui::Panel *editPanel);
  145. // leaves editing mode
  146. virtual void LeaveEditMode();
  147. // returns true if we are currently in inline editing mode
  148. virtual bool IsInEditMode();
  149. // sets whether or not the vertical scrollbar should ever be displayed
  150. virtual void SetVerticalScrollbar(bool state);
  151. // returns the size required to fully draw the contents of the panel
  152. virtual void GetContentSize(int &wide, int &tall);
  153. // image handling
  154. virtual void SetImageList(ImageList *imageList, bool deleteImageListWhenDone);
  155. virtual void ScrollToItem(int iItem);
  156. virtual void SetProportional(bool state);
  157. HFont GetHeaderFont( void ) const;
  158. void SetHeaderFont( HFont hFont );
  159. HFont GetRowFont( void ) const;
  160. void SetRowFont( HFont hFont );
  161. void MoveSelectionDown( void );
  162. void MoveSelectionUp( void );
  163. ScrollBar *GetScrollBar( void ) { return m_pScrollBar; }
  164. void SetColumnWidthBySection(int sectionID, const char *columnName, int iWidth);
  165. protected:
  166. virtual void PerformLayout();
  167. virtual void ApplySchemeSettings(IScheme *pScheme);
  168. virtual void ApplySettings(KeyValues *inResourceData);
  169. virtual void OnSizeChanged(int wide, int tall);
  170. virtual void OnMouseWheeled(int delta);
  171. virtual void OnMousePressed( MouseCode code);
  172. virtual void NavigateTo( void );
  173. virtual void OnKeyCodePressed( KeyCode code );
  174. virtual void OnSetFocus(); // called after the panel receives the keyboard focus
  175. public:
  176. virtual void SetFontSection(int sectionID, HFont font);
  177. virtual void SetItemBgHorizFillInset( int itemID, int nInset );
  178. void SetColorOverrideForCell( int sectionID, int itemID, int columnID, Color clrOverride );
  179. Color *GetColorOverrideForCell( int sectionID, int itemID, int columnID );
  180. void ClearAllColorOverrideForCell(){ m_ColorOverrides.Purge(); }
  181. enum
  182. {
  183. BUTTON_HEIGHT_DEFAULT = 20,
  184. BUTTON_HEIGHT_SPACER = 7,
  185. DEFAULT_LINE_SPACING = 20,
  186. DEFAULT_SECTION_GAP = 8,
  187. COLUMN_DATA_INDENT = 6,
  188. COLUMN_DATA_GAP = 2,
  189. };
  190. virtual void SetSectionDrawDividerBar( int sectionID, bool bDraw );
  191. private:
  192. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" );
  193. int GetSectionTall();
  194. void LayoutPanels(int &contentTall);
  195. // Returns the index of a new item button, reusing an existing item button if possible
  196. int GetNewItemButton();
  197. friend class CItemButton;
  198. void SetSelectedItem(CItemButton *item);
  199. DHANDLE<CItemButton> m_hSelectedItem;
  200. struct color_override_t
  201. {
  202. int m_SectionID;
  203. int m_ItemID;
  204. int m_ColumnID;
  205. Color m_clrOverride;
  206. };
  207. struct column_t
  208. {
  209. char m_szColumnName[32];
  210. wchar_t m_szColumnText[64];
  211. int m_iColumnFlags;
  212. int m_iWidth;
  213. HFont m_hFallbackFont;
  214. };
  215. struct section_t
  216. {
  217. int m_iID;
  218. bool m_bAlwaysVisible;
  219. SectionedListPanelHeader *m_pHeader;
  220. CUtlVector<column_t> m_Columns;
  221. SectionSortFunc_t m_pSortFunc;
  222. int m_iMinimumHeight;
  223. };
  224. CUtlVector<section_t> m_Sections;
  225. CUtlLinkedList<CItemButton *, int> m_Items;
  226. CUtlLinkedList<CItemButton *, int> m_FreeItems;
  227. CUtlVector<CItemButton *> m_SortedItems;
  228. CUtlVector<color_override_t> m_ColorOverrides;
  229. PHandle m_hEditModePanel;
  230. int m_iEditModeItemID;
  231. int m_iEditModeColumn;
  232. int m_iContentHeight;
  233. int m_iLineSpacing; // row height
  234. int m_iLineGap; // gap between rows
  235. int m_iSectionGap;
  236. int FindSectionIndexByID(int sectionID);
  237. void ReSortList();
  238. ScrollBar *m_pScrollBar;
  239. ImageList *m_pImageList;
  240. bool m_bDeleteImageListWhenDone;
  241. bool m_bSortNeeded;
  242. bool m_bVerticalScrollbarEnabled;
  243. HFont m_hHeaderFont;
  244. HFont m_hRowFont;
  245. //=============================================================================
  246. // HPE_BEGIN:
  247. //=============================================================================
  248. // [tj] Whether or not this list should respond to the mouse
  249. bool m_clickable;
  250. // [tj] Whether or not this list should draw the headers for the sections
  251. bool m_bDrawSectionHeaders;
  252. //=============================================================================
  253. // HPE_END
  254. //=============================================================================
  255. CPanelAnimationVar( bool, m_bShowColumns, "show_columns", "false" );
  256. };
  257. class SectionedListPanelHeader : public Label
  258. {
  259. DECLARE_CLASS_SIMPLE( SectionedListPanelHeader, Label );
  260. public:
  261. SectionedListPanelHeader(SectionedListPanel *parent, const char *name, int sectionID);
  262. SectionedListPanelHeader(SectionedListPanel *parent, const wchar_t *name, int sectionID);
  263. virtual void ApplySchemeSettings(IScheme *pScheme) OVERRIDE;
  264. virtual void Paint() OVERRIDE;
  265. virtual void PerformLayout() OVERRIDE;
  266. void SetColor(Color col);
  267. void SetDividerColor(Color col );
  268. void DrawDividerBar(bool bDraw){ m_bDrawDividerBar = bDraw; }
  269. protected:
  270. int m_iSectionID;
  271. Color m_SectionDividerColor;
  272. SectionedListPanel *m_pListPanel;
  273. bool m_bDrawDividerBar;
  274. };
  275. } // namespace vgui
  276. #endif // SECTIONEDLISTPANEL_H