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.

203 lines
6.7 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef TREEVIEW_H
  8. #define TREEVIEW_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <utllinkedlist.h>
  13. #include <utlvector.h>
  14. #include <vgui/VGUI.h>
  15. #include <vgui_controls/Panel.h>
  16. class KeyValues;
  17. namespace vgui
  18. {
  19. class ExpandButton;
  20. class TreeNode;
  21. class TreeViewSubPanel;
  22. // sorting function, should return true if node1 should be displayed before node2
  23. typedef bool (*TreeViewSortFunc_t)(KeyValues *node1, KeyValues *node2);
  24. class TreeView : public Panel
  25. {
  26. DECLARE_CLASS_SIMPLE( TreeView, Panel );
  27. public:
  28. TreeView(Panel *parent, const char *panelName);
  29. ~TreeView();
  30. void SetSortFunc(TreeViewSortFunc_t pSortFunc);
  31. virtual int AddItem(KeyValues *data, int parentItemIndex);
  32. virtual int GetRootItemIndex();
  33. virtual int GetNumChildren( int itemIndex );
  34. virtual int GetChild( int iParentItemIndex, int iChild ); // between 0 and GetNumChildren( iParentItemIndex ).
  35. virtual int GetItemCount(void);
  36. virtual KeyValues *GetItemData(int itemIndex);
  37. virtual void RemoveItem(int itemIndex, bool bPromoteChildren, bool bRecursivelyRemove = false );
  38. virtual void RemoveAll();
  39. virtual bool ModifyItem(int itemIndex, KeyValues *data);
  40. virtual int GetItemParent(int itemIndex);
  41. virtual void SetFont(HFont font);
  42. virtual void SetImageList(ImageList *imageList, bool deleteImageListWhenDone);
  43. void SetAllowMultipleSelections( bool state );
  44. bool IsMultipleSelectionAllowed() const;
  45. virtual void ClearSelection();
  46. virtual void AddSelectedItem( int itemIndex, bool clearCurrentSelection, bool requestFocus = true, bool bMakeItemVisible = true );
  47. virtual void RemoveSelectedItem( int itemIndex );
  48. virtual void SelectAll();
  49. virtual bool IsItemSelected( int itemIndex );
  50. virtual void RangeSelectItems( int clickedItem );
  51. virtual void FindNodesInRange( int startItem, int endItem, CUtlVector< int >& itemIndices );
  52. // returns the id of the currently selected item, -1 if nothing is selected
  53. virtual int GetSelectedItemCount() const;
  54. virtual int GetFirstSelectedItem() const;
  55. virtual void GetSelectedItems( CUtlVector< int >& list );
  56. virtual void GetSelectedItemData( CUtlVector< KeyValues * >& list );
  57. // set colors for individual elments
  58. virtual void SetItemFgColor(int itemIndex, const Color& color);
  59. virtual void SetItemBgColor(int itemIndex, const Color& color);
  60. virtual void SetItemSelectionTextColor( int itemIndex, const Color& clr );
  61. virtual void SetItemSelectionBgColor( int itemIndex, const Color& clr );
  62. virtual void SetItemSelectionUnfocusedBgColor( int itemIndex, const Color& clr );
  63. // returns true if the itemID is valid for use
  64. virtual bool IsItemIDValid(int itemIndex);
  65. // item iterators
  66. // iterate from [0..GetHighestItemID()],
  67. // and check each with IsItemIDValid() before using
  68. virtual int GetHighestItemID();
  69. virtual void ExpandItem(int itemIndex, bool bExpand);
  70. virtual bool IsItemExpanded( int itemIndex );
  71. virtual void MakeItemVisible(int itemIndex);
  72. // This tells which of the visible items is the top one.
  73. virtual void GetVBarInfo( int &top, int &nItemsVisible, bool& hbarVisible );
  74. virtual HFont GetFont();
  75. virtual void GenerateDragDataForItem( int itemIndex, KeyValues *msg );
  76. virtual void SetDragEnabledItems( bool state );
  77. virtual void OnLabelChanged( int itemIndex, char const *oldString, char const *newString );
  78. virtual bool IsLabelEditingAllowed() const;
  79. virtual bool IsLabelBeingEdited() const;
  80. virtual void SetAllowLabelEditing( bool state );
  81. /* message sent
  82. "TreeViewItemSelected" int "itemIndex"
  83. called when the selected item changes
  84. "TreeViewItemDeselected" int "itemIndex"
  85. called when item is deselected
  86. */
  87. int GetRowHeight();
  88. int GetVisibleMaxWidth();
  89. virtual void OnMousePressed(MouseCode code);
  90. // By default, the tree view expands nodes on left-click. This enables/disables that feature
  91. void EnableExpandTreeOnLeftClick( bool bEnable );
  92. virtual void SetLabelEditingAllowed( int itemIndex, bool state );
  93. virtual void StartEditingLabel( int itemIndex );
  94. virtual bool IsItemDroppable( int itemIndex, CUtlVector< KeyValues * >& msglist );
  95. virtual void OnItemDropped( int itemIndex, CUtlVector< KeyValues * >& msglist );
  96. virtual bool GetItemDropContextMenu( int itemIndex, Menu *menu, CUtlVector< KeyValues * >& msglist );
  97. virtual HCursor GetItemDropCursor( int itemIndex, CUtlVector< KeyValues * >& msglist );
  98. virtual int GetPrevChildItemIndex( int itemIndex );
  99. virtual int GetNextChildItemIndex( int itemIndex );
  100. virtual void PerformLayout();
  101. // Makes the scrollbar parented to some other panel...
  102. ScrollBar *SetScrollBarExternal( bool vertical, Panel *newParent );
  103. void GetScrollBarSize( bool vertical, int& w, int& h );
  104. void SetMultipleItemDragEnabled( bool state ); // if this is set, then clicking on one row and dragging will select a run or items, etc.
  105. bool IsMultipleItemDragEnabled() const;
  106. int FindItemUnderMouse( int mx, int my );
  107. protected:
  108. // functions to override
  109. // called when a node, marked as "Expand", needs to generate it's child nodes when expanded
  110. virtual void GenerateChildrenOfNode(int itemIndex) {}
  111. // override to open a custom context menu on a node being selected and right-clicked
  112. virtual void GenerateContextMenu( int itemIndex, int x, int y ) {}
  113. // overrides
  114. virtual void OnMouseWheeled(int delta);
  115. virtual void OnSizeChanged(int wide, int tall);
  116. virtual void ApplySchemeSettings(IScheme *pScheme);
  117. MESSAGE_FUNC_INT( OnSliderMoved, "ScrollBarSliderMoved", position );
  118. virtual void SetBgColor( Color color );
  119. private:
  120. friend class TreeNode;
  121. friend class TreeNodeText;
  122. TreeNode* GetItem( int itemIndex );
  123. virtual void RemoveChildrenOfNode( int itemIndex );
  124. void SetLabelBeingEdited( bool state );
  125. // Clean up the image list
  126. void CleanUpImageList( );
  127. // to be accessed by TreeNodes
  128. IImage* GetImage(int index);
  129. // bools
  130. bool m_bAllowLabelEditing : 1;
  131. bool m_bDragEnabledItems : 1;
  132. bool m_bDeleteImageListWhenDone : 1;
  133. bool m_bLeftClickExpandsTree : 1;
  134. bool m_bLabelBeingEdited : 1;
  135. bool m_bMultipleItemDragging : 1;
  136. bool m_bAllowMultipleSelections : 1;
  137. // cross reference - no hierarchy ordering in this list
  138. CUtlLinkedList<TreeNode *, int> m_NodeList;
  139. ScrollBar *m_pHorzScrollBar, *m_pVertScrollBar;
  140. int m_nRowHeight;
  141. ImageList *m_pImageList;
  142. TreeNode *m_pRootNode;
  143. TreeViewSortFunc_t m_pSortFunc;
  144. HFont m_Font;
  145. CUtlVector< TreeNode * > m_SelectedItems;
  146. TreeViewSubPanel *m_pSubPanel;
  147. int m_nMostRecentlySelectedItem;
  148. bool m_bScrollbarExternal[ 2 ]; // 0 = vert, 1 = horz
  149. };
  150. }
  151. #endif // TREEVIEW_H