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.

372 lines
12 KiB

  1. //========= Copyright � 1996-2005, 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. uintp 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. // Makes a copy of the data for use in the table. Returns the index the item is at.
  101. virtual int AddItem(const KeyValues *data, uintp userData, bool bScrollToItem, bool bSortOnAdd);
  102. // Unlike AddItem, this takes ownership of the KeyValues * and stores it in the list. Used when dragging from the table. Only used if the caller enables drag support
  103. void SetItemDragData( int itemID, const KeyValues *data );
  104. virtual int GetItemCount( void ); // returns the number of VISIBLE items
  105. virtual int GetItem(const char *itemName); // gets the row index of an item by name (data->GetName())
  106. virtual KeyValues *GetItem(int itemID); // returns pointer to data the row holds
  107. virtual int GetItemCurrentRow(int itemID); // returns -1 if invalid index or item not visible
  108. virtual int GetItemIDFromRow(int currentRow); // returns -1 if invalid row
  109. virtual uintp GetItemUserData(int itemID);
  110. virtual ListPanelItem *GetItemData(int itemID);
  111. virtual void SetUserData( int itemID, uintp userData );
  112. virtual int GetItemIDFromUserData( uintp userData );
  113. virtual void ApplyItemChanges(int itemID); // applies any changes to the data, performed by modifying the return of GetItem() above
  114. virtual void RemoveItem(int itemID); // removes an item from the table (changing the indices of all following items)
  115. virtual void RereadAllItems(); // updates the view with the new data
  116. virtual void RemoveAll(); // clears and deletes all the memory used by the data items
  117. virtual void DeleteAllItems(); // obselete, use RemoveAll();
  118. virtual void GetCellText(int itemID, int column, OUT_Z_BYTECAP(bufferSize) wchar_t *buffer, int bufferSize); // returns the data held by a specific cell
  119. virtual IImage *GetCellImage(int itemID, int column); //, ImagePanel *&buffer); // returns the image held by a specific cell
  120. // Use these until they return InvalidItemID to iterate all the items.
  121. virtual int FirstItem() const;
  122. virtual int NextItem( int iItem ) const;
  123. virtual int InvalidItemID() const;
  124. virtual bool IsValidItemID(int itemID);
  125. // sets whether the dataitem is visible or not
  126. // it is removed from the row list when it becomes invisible, but stays in the indexes
  127. // this is much faster than a normal remove
  128. virtual void SetItemVisible(int itemID, bool state);
  129. virtual void SetItemDisabled(int itemID, bool state );
  130. bool IsItemVisible( int itemID );
  131. void SetAllVisible( bool state );
  132. virtual void SetFont(HFont font);
  133. // image handling
  134. virtual void SetImageList(ImageList *imageList, bool deleteImageListWhenDone);
  135. // SELECTION
  136. // returns the count of selected items
  137. virtual int GetSelectedItemsCount();
  138. // returns the selected item by selection index, valid in range [0, GetNumSelectedRows)
  139. virtual int GetSelectedItem(int selectionIndex);
  140. // sets no item as selected
  141. virtual void ClearSelectedItems();
  142. virtual bool IsItemSelected( int itemID );
  143. // adds a item to the select list
  144. virtual void AddSelectedItem( int itemID );
  145. // sets this single item as the only selected item
  146. virtual void SetSingleSelectedItem( int itemID );
  147. // returns the selected column, -1 for particular column selected
  148. virtual int GetSelectedColumn();
  149. // whether or not to select specific cells (off by default)
  150. virtual void SetSelectIndividualCells(bool state);
  151. // whether or not multiple cells/rows can be selected
  152. void SetMultiselectEnabled( bool bState );
  153. bool IsMultiselectEnabled() const;
  154. // sets a single cell - all other previous rows are cleared
  155. virtual void SetSelectedCell(int row, int column);
  156. 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
  157. virtual bool GetCellBounds( int row, int column, int& x, int& y, int& wide, int& tall );
  158. // sets the text which is displayed when the list is empty
  159. virtual void SetEmptyListText(const char *text);
  160. virtual void SetEmptyListText(const wchar_t *text);
  161. // Move the scroll bar to a point where this item is visible
  162. void ScrollToItem( int itemID );
  163. // relayout the scroll bar in response to changing the items in the list panel
  164. // do this if you RemoveAll()
  165. void ResetScrollBar();
  166. // Attaches drag data to a particular item
  167. virtual void OnCreateDragData( KeyValues *msg );
  168. void SetIgnoreDoubleClick( bool state );
  169. // set up a field for editing
  170. virtual void EnterEditMode(int itemID, int column, vgui::Panel *editPanel);
  171. // leaves editing mode
  172. virtual void LeaveEditMode();
  173. // returns true if we are currently in inline editing mode
  174. virtual bool IsInEditMode();
  175. MESSAGE_FUNC_INT( ResizeColumnToContents, "ResizeColumnToContents", column );
  176. #ifdef _GAMECONSOLE
  177. virtual void NavigateTo();
  178. #endif
  179. protected:
  180. // PAINTING
  181. virtual Panel *GetCellRenderer(int row, int column);
  182. // overrides
  183. virtual void OnMouseWheeled(int delta);
  184. virtual void OnSizeChanged(int wide, int tall);
  185. virtual void PerformLayout();
  186. virtual void Paint();
  187. virtual void PaintBackground();
  188. virtual void ApplySchemeSettings(IScheme *pScheme);
  189. virtual void OnMousePressed( MouseCode code );
  190. virtual void OnMouseDoublePressed( MouseCode code );
  191. #ifdef _GAMECONSOLE
  192. virtual void OnKeyCodePressed(KeyCode code);
  193. #endif
  194. virtual void OnKeyCodeTyped( KeyCode code );
  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