treectrl.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/gtk1/treectrl.h
  3. // Purpose: wxTreeCtrl class
  4. // Author: Denis Pershin
  5. // Modified by:
  6. // Created: 08/08/98
  7. // Copyright: (c) Denis Pershin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_TREECTRL_H_
  11. #define _WX_TREECTRL_H_
  12. #include "wx/textctrl.h"
  13. #include "wx/imaglist.h"
  14. #include <gtk/gtk.h>
  15. // the type for "untyped" data
  16. typedef long wxDataType;
  17. // fwd decl
  18. class WXDLLIMPEXP_CORE wxImageList;
  19. struct wxTreeViewItem;
  20. // a callback function used for sorting tree items, it should return -1 if the
  21. // first item precedes the second, +1 if the second precedes the first or 0 if
  22. // they're equivalent
  23. class WXDLLIMPEXP_FWD_CORE wxTreeItemData;
  24. typedef int (*wxTreeItemCmpFunc)(wxTreeItemData *item1, wxTreeItemData *item2);
  25. // ----------------------------------------------------------------------------
  26. // constants
  27. // ----------------------------------------------------------------------------
  28. // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
  29. // where exactly the specified point is situated:
  30. // above the client area.
  31. static const int wxTREE_HITTEST_ABOVE = 0x0001;
  32. // below the client area.
  33. static const int wxTREE_HITTEST_BELOW = 0x0002;
  34. // in the client area but below the last item.
  35. static const int wxTREE_HITTEST_NOWHERE = 0x0004;
  36. // on the button associated with an item.
  37. static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0010;
  38. // on the bitmap associated with an item.
  39. static const int wxTREE_HITTEST_ONITEMICON = 0x0020;
  40. // in the indentation associated with an item.
  41. static const int wxTREE_HITTEST_ONITEMINDENT = 0x0040;
  42. // on the label (string) associated with an item.
  43. static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080;
  44. // in the area to the right of an item.
  45. static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0100;
  46. // on the state icon for a tree view item that is in a user-defined state.
  47. static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0200;
  48. // to the right of the client area.
  49. static const int wxTREE_HITTEST_TOLEFT = 0x0400;
  50. // to the left of the client area.
  51. static const int wxTREE_HITTEST_TORIGHT = 0x0800;
  52. // anywhere on the item
  53. static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
  54. wxTREE_HITTEST_ONITEMLABEL |
  55. wxTREE_HITTEST_ONITEMSTATEICON;
  56. #if WXWIN_COMPATIBILITY_2_6
  57. // NB: all the following flags are for compatbility only and will be removed in
  58. // next versions
  59. // flags for deprecated InsertItem() variant
  60. #define wxTREE_INSERT_FIRST 0xFFFF0001
  61. #define wxTREE_INSERT_LAST 0xFFFF0002
  62. #endif
  63. // ----------------------------------------------------------------------------
  64. // wxTreeItemId identifies an element of the tree. In this implementation, it's
  65. // just a trivial wrapper around GTK GtkTreeItem *. It's opaque for the
  66. // application.
  67. // ----------------------------------------------------------------------------
  68. class WXDLLIMPEXP_CORE wxTreeItemId {
  69. public:
  70. // ctors
  71. wxTreeItemId() { m_itemId = NULL; }
  72. // default copy ctor/assignment operator are ok for us
  73. // accessors
  74. // is this a valid tree item?
  75. bool IsOk() const { return m_itemId != NULL; }
  76. // conversion to/from either real (system-dependent) tree item id or
  77. // to "long" which used to be the type for tree item ids in previous
  78. // versions of wxWidgets
  79. // for wxTreeCtrl usage only
  80. wxTreeItemId(GtkTreeItem *itemId) { m_itemId = itemId; }
  81. operator GtkTreeItem *() const { return m_itemId; }
  82. void operator =(GtkTreeItem *item) { m_itemId = item; }
  83. protected:
  84. GtkTreeItem *m_itemId;
  85. };
  86. // ----------------------------------------------------------------------------
  87. // wxTreeItemData is some (arbitrary) user class associated with some item. The
  88. // main advantage of having this class (compared to old untyped interface) is
  89. // that wxTreeItemData's are destroyed automatically by the tree and, as this
  90. // class has virtual dtor, it means that the memory will be automatically
  91. // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
  92. // the size of this class is critical: in any real application, each tree leaf
  93. // will have wxTreeItemData associated with it and number of leaves may be
  94. // quite big.
  95. //
  96. // Because the objects of this class are deleted by the tree, they should
  97. // always be allocated on the heap!
  98. // ----------------------------------------------------------------------------
  99. class WXDLLIMPEXP_CORE wxTreeItemData : private wxTreeItemId {
  100. public:
  101. // default ctor/copy ctor/assignment operator are ok
  102. // dtor is virtual and all the items are deleted by the tree control when
  103. // it's deleted, so you normally don't have to care about freeing memory
  104. // allocated in your wxTreeItemData-derived class
  105. virtual ~wxTreeItemData() { }
  106. // accessors: set/get the item associated with this node
  107. void SetId(const wxTreeItemId& id) { m_itemId = id; }
  108. const wxTreeItemId& GetId() const { return (wxTreeItemId&) m_itemId; }
  109. };
  110. class WXDLLIMPEXP_CORE wxTreeCtrl: public wxControl {
  111. public:
  112. // creation
  113. // --------
  114. wxTreeCtrl() { Init(); }
  115. wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
  116. const wxPoint& pos = wxDefaultPosition,
  117. const wxSize& size = wxDefaultSize,
  118. long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
  119. const wxValidator& validator = wxDefaultValidator,
  120. const wxString& name = "wxTreeCtrl") {
  121. Create(parent, id, pos, size, style, validator, name);
  122. }
  123. virtual ~wxTreeCtrl();
  124. bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
  125. const wxPoint& pos = wxDefaultPosition,
  126. const wxSize& size = wxDefaultSize,
  127. long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
  128. const wxValidator& validator = wxDefaultValidator,
  129. const wxString& name = "wxTreeCtrl");
  130. // accessors
  131. // ---------
  132. // get the total number of items in the control
  133. virtual unsigned int GetCount() const;
  134. // indent is the number of pixels the children are indented relative to
  135. // the parents position. SetIndent() also redraws the control
  136. // immediately.
  137. unsigned int GetIndent() const;
  138. void SetIndent(unsigned int indent);
  139. // image list: these functions allow to associate an image list with
  140. // the control and retrieve it. Note that the control does _not_ delete
  141. // the associated image list when it's deleted in order to allow image
  142. // lists to be shared between different controls.
  143. //
  144. // The normal image list is for the icons which correspond to the
  145. // normal tree item state (whether it is selected or not).
  146. // Additionally, the application might choose to show a state icon
  147. // which corresponds to an app-defined item state (for example,
  148. // checked/unchecked) which are taken from the state image list.
  149. wxImageList *GetImageList() const;
  150. wxImageList *GetStateImageList() const;
  151. void SetImageList(wxImageList *imageList);
  152. void SetStateImageList(wxImageList *imageList);
  153. // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
  154. // member functions of wxTreeItem because they must know the tree the item
  155. // belongs to for Windows implementation and storing the pointer to
  156. // wxTreeCtrl in each wxTreeItem is just too much waste.
  157. // accessors
  158. // ---------
  159. // retrieve items label
  160. wxString GetItemText(const wxTreeItemId& item) const;
  161. // get the normal item image
  162. int GetItemImage(const wxTreeItemId& item) const;
  163. // get the data associated with the item
  164. wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
  165. // modifiers
  166. // ---------
  167. // set items label
  168. void SetItemText(const wxTreeItemId& item, const wxString& text);
  169. // set the normal item image
  170. void SetItemImage(const wxTreeItemId& item, int image);
  171. // associate some data with the item
  172. void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
  173. // item status inquiries
  174. // ---------------------
  175. // is the item visible (it might be outside the view or not expanded)?
  176. bool IsVisible(const wxTreeItemId& item) const;
  177. // does the item has any children?
  178. bool ItemHasChildren(const wxTreeItemId& item) const;
  179. // is the item expanded (only makes sense if HasChildren())?
  180. bool IsExpanded(const wxTreeItemId& item) const;
  181. // is this item currently selected (the same as has focus)?
  182. bool IsSelected(const wxTreeItemId& item) const;
  183. // number of children
  184. // ------------------
  185. // if 'recursively' is false, only immediate children count, otherwise
  186. // the returned number is the number of all items in this branch
  187. size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = true);
  188. // navigation
  189. // ----------
  190. // wxTreeItemId.IsOk() will return false if there is no such item
  191. // get the root tree item
  192. wxTreeItemId GetRootItem() const;
  193. // get the item currently selected (may return NULL if no selection)
  194. wxTreeItemId GetSelection() const;
  195. // get the parent of this item (may return NULL if root)
  196. wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
  197. // for this enumeration function you must pass in a "cookie" parameter
  198. // which is opaque for the application but is necessary for the library
  199. // to make these functions reentrant (i.e. allow more than one
  200. // enumeration on one and the same object simultaneously). Of course,
  201. // the "cookie" passed to GetFirstChild() and GetNextChild() should be
  202. // the same!
  203. // get the last child of this item - this method doesn't use cookies
  204. wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
  205. // get the next sibling of this item
  206. wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
  207. // get the previous sibling
  208. wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
  209. // get first visible item
  210. wxTreeItemId GetFirstVisibleItem() const;
  211. // get the next visible item: item must be visible itself!
  212. // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
  213. wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
  214. // get the previous visible item: item must be visible itself!
  215. wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
  216. // operations
  217. // ----------
  218. // add the root node to the tree
  219. wxTreeItemId AddRoot(const wxString& text,
  220. int image = -1, int selectedImage = -1,
  221. wxTreeItemData *data = NULL);
  222. // insert a new item in as the first child of the parent
  223. wxTreeItemId PrependItem(const wxTreeItemId& parent,
  224. const wxString& text,
  225. int image = -1, int selectedImage = -1,
  226. wxTreeItemData *data = NULL);
  227. // insert a new item after a given one
  228. wxTreeItemId InsertItem(const wxTreeItemId& parent,
  229. const wxTreeItemId& idPrevious,
  230. const wxString& text,
  231. int image = -1, int selectedImage = -1,
  232. wxTreeItemData *data = NULL);
  233. // insert a new item in as the last child of the parent
  234. wxTreeItemId AppendItem(const wxTreeItemId& parent,
  235. const wxString& text,
  236. int image = -1, int selectedImage = -1,
  237. wxTreeItemData *data = NULL);
  238. // delete this item and associated data if any
  239. void Delete(const wxTreeItemId& item);
  240. // delete all items from the tree
  241. void DeleteAllItems();
  242. // expand this item
  243. void Expand(const wxTreeItemId& item);
  244. // collapse the item without removing its children
  245. void Collapse(const wxTreeItemId& item);
  246. // collapse the item and remove all children
  247. void CollapseAndReset(const wxTreeItemId& item);
  248. // toggles the current state
  249. void Toggle(const wxTreeItemId& item);
  250. // remove the selection from currently selected item (if any)
  251. void Unselect();
  252. // select this item
  253. void SelectItem(const wxTreeItemId& item);
  254. // make sure this item is visible (expanding the parent item and/or
  255. // scrolling to this item if necessary)
  256. void EnsureVisible(const wxTreeItemId& item);
  257. // scroll to this item (but don't expand its parent)
  258. void ScrollTo(const wxTreeItemId& item);
  259. // start editing the item label: this (temporarily) replaces the item
  260. // with a one line edit control. The item will be selected if it hadn't
  261. // been before. textCtrlClass parameter allows you to create an edit
  262. // control of arbitrary user-defined class deriving from wxTextCtrl.
  263. wxTextCtrl* EditLabel(const wxTreeItemId& item,
  264. wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl));
  265. // returns the same pointer as StartEdit() if the item is being edited,
  266. // NULL otherwise (it's assumed that no more than one item may be
  267. // edited simultaneously)
  268. wxTextCtrl* GetEditControl() const;
  269. // end editing and accept or discard the changes to item label
  270. void EndEditLabel(const wxTreeItemId& item, bool discardChanges = false);
  271. // sort the children of this item using the specified callback function
  272. // (it should return -1, 0 or +1 as usual), if it's not specified
  273. // alphabetical comparaison is performed.
  274. //
  275. // NB: this function is not reentrant!
  276. void SortChildren(const wxTreeItemId& item,
  277. wxTreeItemCmpFunc *cmpFunction = NULL);
  278. // deprecated
  279. // ----------
  280. #if WXWIN_COMPATIBILITY_2_6
  281. // these methods are deprecated and will be removed in future versions of
  282. // wxWidgets, they're here for compatibility only, don't use them in new
  283. // code (the comments indicate why these methods are now useless and how to
  284. // replace them)
  285. // use Expand, Collapse, CollapseAndReset or Toggle
  286. wxDEPRECATED( void ExpandItem(const wxTreeItemId& item, int action) );
  287. // use SetImageList
  288. wxDEPRECATED( void SetImageList(wxImageList *imageList, int) )
  289. { SetImageList(imageList); }
  290. // use Set/GetItemImage directly
  291. wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId& item) const );
  292. wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId& item, int image) );
  293. // get the first child of this item
  294. wxDEPRECATED( wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const );
  295. // get the next child (after GetFirstChild or GetNextChild)
  296. wxDEPRECATED( wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const );
  297. // use AddRoot, PrependItem or AppendItem
  298. wxDEPRECATED( wxTreeItemId InsertItem(const wxTreeItemId& parent,
  299. const wxString& text,
  300. int image = -1, int selImage = -1,
  301. long insertAfter = wxTREE_INSERT_LAST) );
  302. #endif // WXWIN_COMPATIBILITY_2_6
  303. // use Set/GetImageList and Set/GetStateImageList
  304. wxImageList *GetImageList(int) const
  305. { return GetImageList(); }
  306. void SendExpanding(const wxTreeItemId& item);
  307. void SendExpanded(const wxTreeItemId& item);
  308. void SendCollapsing(const wxTreeItemId& item);
  309. void SendCollapsed(const wxTreeItemId& item);
  310. void SendSelChanging(const wxTreeItemId& item);
  311. void SendSelChanged(const wxTreeItemId& item);
  312. protected:
  313. wxTreeItemId m_editItem;
  314. GtkTree *m_tree;
  315. GtkTreeItem *m_anchor;
  316. wxTextCtrl* m_textCtrl;
  317. wxImageList* m_imageListNormal;
  318. wxImageList* m_imageListState;
  319. long m_curitemId;
  320. void SendMessage(wxEventType command, const wxTreeItemId& item);
  321. // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
  322. // the common part of all ctors
  323. void Init();
  324. // insert a new item in as the last child of the parent
  325. wxTreeItemId p_InsertItem(GtkTreeItem *p,
  326. const wxString& text,
  327. int image, int selectedImage,
  328. wxTreeItemData *data);
  329. DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
  330. };
  331. #endif
  332. // _WX_TREECTRL_H_