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.

371 lines
12 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef LISTPANEL_H
  8. #define LISTPANEL_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <utllinkedlist.h>
  13. #include <utlvector.h>
  14. #include <utlrbtree.h>
  15. #include <vgui/VGUI.h>
  16. #include <vgui_controls/Panel.h>
  17. class KeyValues;
  18. namespace vgui
  19. {
  20. class ScrollBar;
  21. class TextImage;
  22. class ImagePanel;
  23. class Label;
  24. class Button;
  25. class IDraggerEvent;
  26. class FastSortListPanelItem;
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Generic class for ListPanel items
  29. //-----------------------------------------------------------------------------
  30. class ListPanelItem
  31. {
  32. public:
  33. ListPanelItem() :
  34. kv( 0 ),
  35. userData( 0 ),
  36. m_pDragData( 0 ),
  37. m_bImage( false ),
  38. m_nImageIndex( -1 ),
  39. m_nImageIndexSelected( -1 ),
  40. m_pIcon( 0 )
  41. {
  42. }
  43. KeyValues *kv;
  44. unsigned int userData;
  45. KeyValues *m_pDragData;
  46. bool m_bImage;
  47. int m_nImageIndex;
  48. int m_nImageIndexSelected;
  49. IImage *m_pIcon;
  50. };
  51. typedef int __cdecl SortFunc(
  52. ListPanel *pPanel,
  53. const ListPanelItem &item1,
  54. const ListPanelItem &item2 );
  55. //-----------------------------------------------------------------------------
  56. // Purpose: A spread-sheet type data view, similar to MFC's
  57. //-----------------------------------------------------------------------------
  58. class ListPanel : public Panel
  59. {
  60. DECLARE_CLASS_SIMPLE( ListPanel, Panel );
  61. public:
  62. ListPanel(Panel *parent, const char *panelName);
  63. ~ListPanel();
  64. // COLUMN HANDLING
  65. // all indices are 0 based, limit of 255 columns
  66. // columns are resizable by default
  67. enum ColumnFlags_e
  68. {
  69. COLUMN_FIXEDSIZE = 0x01, // set to have the column be a fixed size
  70. COLUMN_RESIZEWITHWINDOW = 0x02, // set to have the column grow with the parent dialog growing
  71. COLUMN_IMAGE = 0x04, // set if the column data is not text, but instead the index of the image to display
  72. COLUMN_HIDDEN = 0x08, // column is hidden by default
  73. COLUMN_UNHIDABLE = 0x10, // column is unhidable
  74. };
  75. // adds a column header
  76. virtual void AddColumnHeader(int index, const char *columnName, const char *columnText, int startingWidth, int minWidth, int maxWidth, int columnFlags = 0);
  77. virtual void AddColumnHeader(int index, const char *columnName, const char *columnText, int width, int columnFlags = 0);
  78. virtual void RemoveColumn(int column); // removes a column
  79. virtual int FindColumn(const char *columnName);
  80. virtual void SetColumnHeaderHeight( int height );
  81. virtual void SetColumnHeaderText(int column, const char *text);
  82. virtual void SetColumnHeaderText(int column, wchar_t *text);
  83. virtual void SetColumnHeaderImage(int column, int imageListIndex);
  84. virtual void SetColumnHeaderTooltip(int column, const char *tooltipText);
  85. virtual void SetColumnTextAlignment( int column, int align );
  86. // Get information about the column headers.
  87. virtual int GetNumColumnHeaders() const;
  88. virtual bool GetColumnHeaderText( int index, char *pOut, int maxLen );
  89. virtual void SetSortFunc(int column, SortFunc *func);
  90. virtual void SetSortColumn(int column);
  91. virtual void SortList( void );
  92. virtual void SetColumnSortable(int column, bool sortable);
  93. virtual void SetColumnVisible(int column, bool visible);
  94. int GetSortColumn() const;
  95. // sets whether the user can add/remove columns (defaults to off)
  96. virtual void SetAllowUserModificationOfColumns(bool allowed);
  97. // DATA HANDLING
  98. // data->GetName() is used to uniquely identify an item
  99. // data sub items are matched against column header name to be used in the table
  100. virtual int AddItem(const KeyValues *data, unsigned int userData, bool bScrollToItem, bool bSortOnAdd); // Takes a copy of the data for use in the table. Returns the index the item is at.
  101. void SetItemDragData( int itemID, const KeyValues *data ); // Makes a copy of the keyvalues to store in the table. Used when dragging from the table. Only used if the caller enables drag support
  102. virtual int GetItemCount( void ); // returns the number of VISIBLE items
  103. virtual int GetItem(const char *itemName); // gets the row index of an item by name (data->GetName())
  104. virtual KeyValues *GetItem(int itemID); // returns pointer to data the row holds
  105. virtual int GetItemCurrentRow(int itemID); // returns -1 if invalid index or item not visible
  106. virtual int GetItemIDFromRow(int currentRow); // returns -1 if invalid row
  107. virtual unsigned int GetItemUserData(int itemID);
  108. virtual ListPanelItem *GetItemData(int itemID);
  109. virtual void SetUserData( int itemID, unsigned int userData );
  110. virtual int GetItemIDFromUserData( unsigned int userData );
  111. virtual void ApplyItemChanges(int itemID); // applies any changes to the data, performed by modifying the return of GetItem() above
  112. virtual void RemoveItem(int itemID); // removes an item from the table (changing the indices of all following items)
  113. virtual void RereadAllItems(); // updates the view with the new data
  114. virtual void RemoveAll(); // clears and deletes all the memory used by the data items
  115. virtual void DeleteAllItems(); // obselete, use RemoveAll();
  116. virtual void GetCellText(int itemID, int column, OUT_Z_BYTECAP(bufferSizeInBytes) wchar_t *buffer, int bufferSizeInBytes); // returns the data held by a specific cell
  117. virtual IImage *GetCellImage(int itemID, int column); //, ImagePanel *&buffer); // returns the image held by a specific cell
  118. // Use these until they return InvalidItemID to iterate all the items.
  119. virtual int FirstItem() const;
  120. virtual int NextItem( int iItem ) const;
  121. virtual int InvalidItemID() const;
  122. virtual bool IsValidItemID(int itemID);
  123. // sets whether the dataitem is visible or not
  124. // it is removed from the row list when it becomes invisible, but stays in the indexes
  125. // this is much faster than a normal remove
  126. virtual void SetItemVisible(int itemID, bool state);
  127. virtual void SetItemDisabled(int itemID, bool state );
  128. bool IsItemVisible( int itemID );
  129. virtual void SetFont(HFont font);
  130. // image handling
  131. virtual void SetImageList(ImageList *imageList, bool deleteImageListWhenDone);
  132. // SELECTION
  133. // returns the count of selected items
  134. virtual int GetSelectedItemsCount();
  135. // returns the selected item by selection index, valid in range [0, GetNumSelectedRows)
  136. virtual int GetSelectedItem(int selectionIndex);
  137. // sets no item as selected
  138. virtual void ClearSelectedItems();
  139. virtual bool IsItemSelected( int itemID );
  140. // adds a item to the select list
  141. virtual void AddSelectedItem( int itemID );
  142. // sets this single item as the only selected item
  143. virtual void SetSingleSelectedItem( int itemID );
  144. // returns the selected column, -1 for particular column selected
  145. virtual int GetSelectedColumn();
  146. // whether or not to select specific cells (off by default)
  147. virtual void SetSelectIndividualCells(bool state);
  148. // whether or not multiple cells/rows can be selected
  149. void SetMultiselectEnabled( bool bState );
  150. bool IsMultiselectEnabled() const;
  151. // sets a single cell - all other previous rows are cleared
  152. virtual void SetSelectedCell(int row, int column);
  153. virtual bool GetCellAtPos(int x, int y, int &row, int &column); // returns true if any found, row and column are filled out. x, y are in screen space
  154. virtual bool GetCellBounds( int row, int column, int& x, int& y, int& wide, int& tall );
  155. // sets the text which is displayed when the list is empty
  156. virtual void SetEmptyListText(const char *text);
  157. virtual void SetEmptyListText(const wchar_t *text);
  158. // relayout the scroll bar in response to changing the items in the list panel
  159. // do this if you RemoveAll()
  160. void ResetScrollBar();
  161. // Attaches drag data to a particular item
  162. virtual void OnCreateDragData( KeyValues *msg );
  163. void SetIgnoreDoubleClick( bool state );
  164. // set up a field for editing
  165. virtual void EnterEditMode(int itemID, int column, vgui::Panel *editPanel);
  166. // leaves editing mode
  167. virtual void LeaveEditMode();
  168. // returns true if we are currently in inline editing mode
  169. virtual bool IsInEditMode();
  170. MESSAGE_FUNC_INT( ResizeColumnToContents, "ResizeColumnToContents", column );
  171. #ifdef _X360
  172. virtual void NavigateTo();
  173. #endif
  174. /// Version number for file format of user config. This defaults to 1,
  175. /// and if you rearrange columns you can increment it to cause any old
  176. /// user configs (which will be screwed up) to be discarded.
  177. int m_nUserConfigFileVersion;
  178. protected:
  179. // PAINTING
  180. virtual Panel *GetCellRenderer(int row, int column);
  181. // overrides
  182. virtual void OnMouseWheeled(int delta);
  183. virtual void OnSizeChanged(int wide, int tall);
  184. virtual void PerformLayout();
  185. virtual void Paint();
  186. virtual void PaintBackground();
  187. virtual void ApplySchemeSettings(IScheme *pScheme);
  188. virtual void OnMousePressed( MouseCode code );
  189. virtual void OnMouseDoublePressed( MouseCode code );
  190. #ifdef _X360
  191. virtual void OnKeyCodePressed(KeyCode code);
  192. #else
  193. virtual void OnKeyCodePressed( KeyCode code );
  194. #endif
  195. MESSAGE_FUNC( OnSliderMoved, "ScrollBarSliderMoved" );
  196. MESSAGE_FUNC_INT_INT( OnColumnResized, "ColumnResized", column, delta );
  197. MESSAGE_FUNC_INT( OnSetSortColumn, "SetSortColumn", column );
  198. MESSAGE_FUNC( OpenColumnChoiceMenu, "OpenColumnChoiceMenu" );
  199. MESSAGE_FUNC_INT( OnToggleColumnVisible, "ToggleColumnVisible", col );
  200. virtual float GetRowsPerPage();
  201. virtual int GetStartItem();
  202. // user configuration
  203. virtual void ApplyUserConfigSettings(KeyValues *userConfig);
  204. virtual void GetUserConfigSettings(KeyValues *userConfig);
  205. virtual bool HasUserConfigSettings();
  206. /* MESSAGES SENT
  207. "ItemSelected" - query which items are selected
  208. "ItemDeselected" - query which items are selected
  209. */
  210. public:
  211. virtual void SetSortColumnEx( int iPrimarySortColumn, int iSecondarySortColumn, bool bSortAscending );
  212. void GetSortColumnEx( int &iPrimarySortColumn, int &iSecondarySortColumn, bool &bSortAscending ) const;
  213. private:
  214. // Cleans up allocations associated with a particular item
  215. void CleanupItem( FastSortListPanelItem *data );
  216. // adds the item into the column indexes
  217. void IndexItem(int itemID);
  218. // Purpose:
  219. void UpdateSelection( vgui::MouseCode code, int x, int y, int row, int column );
  220. // Handles multiselect
  221. void HandleMultiSelection( int itemID, int row, int column );
  222. // Handles addselect
  223. void HandleAddSelection( int itemID, int row, int column );
  224. // pre-sorted columns
  225. struct IndexItem_t
  226. {
  227. ListPanelItem *dataItem;
  228. int duplicateIndex;
  229. };
  230. typedef CUtlRBTree<IndexItem_t, int> IndexRBTree_t;
  231. struct column_t
  232. {
  233. Button *m_pHeader;
  234. int m_iMinWidth;
  235. int m_iMaxWidth;
  236. bool m_bResizesWithWindow;
  237. Panel *m_pResizer;
  238. SortFunc *m_pSortFunc;
  239. bool m_bTypeIsText;
  240. bool m_bHidden;
  241. bool m_bUnhidable;
  242. IndexRBTree_t m_SortedTree;
  243. int m_nContentAlignment;
  244. };
  245. // list of the column headers
  246. CUtlLinkedList<column_t, unsigned char> m_ColumnsData;
  247. // persistent list of all columns ever created, indexes into m_ColumnsData - used for matching up DATAITEM m_SortedTreeIndexes
  248. CUtlVector<unsigned char> m_ColumnsHistory;
  249. // current list of columns, indexes into m_ColumnsData
  250. CUtlVector<unsigned char> m_CurrentColumns;
  251. int m_iColumnDraggerMoved; // which column dragger was moved->which header to resize
  252. int m_lastBarWidth;
  253. CUtlLinkedList<FastSortListPanelItem*, int> m_DataItems;
  254. CUtlVector<int> m_VisibleItems;
  255. // set to true if the table needs to be sorted before it's drawn next
  256. int m_iSortColumn;
  257. int m_iSortColumnSecondary;
  258. void ResortColumnRBTree(int col);
  259. static bool RBTreeLessFunc(vgui::ListPanel::IndexItem_t &item1, vgui::ListPanel::IndexItem_t &item2);
  260. TextImage *m_pTextImage; // used in rendering
  261. ImagePanel *m_pImagePanel; // used in rendering
  262. Label *m_pLabel; // used in rendering
  263. ScrollBar *m_hbar;
  264. ScrollBar *m_vbar;
  265. int m_iSelectedColumn;
  266. bool m_bNeedsSort : 1;
  267. bool m_bSortAscending : 1;
  268. bool m_bSortAscendingSecondary : 1;
  269. bool m_bCanSelectIndividualCells : 1;
  270. bool m_bShiftHeldDown : 1;
  271. bool m_bMultiselectEnabled : 1;
  272. bool m_bAllowUserAddDeleteColumns : 1;
  273. bool m_bDeleteImageListWhenDone : 1;
  274. bool m_bIgnoreDoubleClick : 1;
  275. int m_iHeaderHeight;
  276. int m_iRowHeight;
  277. // selection data
  278. CUtlVector<int> m_SelectedItems; // array of selected rows
  279. int m_LastItemSelected; // remember the last row selected for future shift clicks
  280. int m_iTableStartX;
  281. int m_iTableStartY;
  282. Color m_LabelFgColor;
  283. Color m_DisabledColor;
  284. Color m_SelectionFgColor;
  285. Color m_DisabledSelectionFgColor;
  286. ImageList *m_pImageList;
  287. TextImage *m_pEmptyListText;
  288. PHandle m_hEditModePanel;
  289. int m_iEditModeItemID;
  290. int m_iEditModeColumn;
  291. void ResetColumnHeaderCommands();
  292. };
  293. }
  294. #endif // LISTPANEL_H