treectrl.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/treectrl.h
  3. // Purpose: wxTreeCtrl base header
  4. // Author: Karsten Ballueder
  5. // Modified by:
  6. // Created:
  7. // Copyright: (c) Karsten Ballueder
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_TREECTRL_H_BASE_
  11. #define _WX_TREECTRL_H_BASE_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #include "wx/defs.h"
  16. #if wxUSE_TREECTRL
  17. #include "wx/control.h"
  18. #include "wx/treebase.h"
  19. #include "wx/textctrl.h" // wxTextCtrl::ms_classinfo used through wxCLASSINFO macro
  20. class WXDLLIMPEXP_FWD_CORE wxImageList;
  21. // ----------------------------------------------------------------------------
  22. // wxTreeCtrlBase
  23. // ----------------------------------------------------------------------------
  24. class WXDLLIMPEXP_CORE wxTreeCtrlBase : public wxControl
  25. {
  26. public:
  27. wxTreeCtrlBase();
  28. virtual ~wxTreeCtrlBase();
  29. // accessors
  30. // ---------
  31. // get the total number of items in the control
  32. virtual unsigned int GetCount() const = 0;
  33. // indent is the number of pixels the children are indented relative to
  34. // the parents position. SetIndent() also redraws the control
  35. // immediately.
  36. virtual unsigned int GetIndent() const = 0;
  37. virtual void SetIndent(unsigned int indent) = 0;
  38. // spacing is the number of pixels between the start and the Text
  39. // (has no effect under wxMSW)
  40. unsigned int GetSpacing() const { return m_spacing; }
  41. void SetSpacing(unsigned int spacing) { m_spacing = spacing; }
  42. // image list: these functions allow to associate an image list with
  43. // the control and retrieve it. Note that the control does _not_ delete
  44. // the associated image list when it's deleted in order to allow image
  45. // lists to be shared between different controls.
  46. //
  47. // The normal image list is for the icons which correspond to the
  48. // normal tree item state (whether it is selected or not).
  49. // Additionally, the application might choose to show a state icon
  50. // which corresponds to an app-defined item state (for example,
  51. // checked/unchecked) which are taken from the state image list.
  52. wxImageList *GetImageList() const { return m_imageListNormal; }
  53. wxImageList *GetStateImageList() const { return m_imageListState; }
  54. virtual void SetImageList(wxImageList *imageList) = 0;
  55. virtual void SetStateImageList(wxImageList *imageList) = 0;
  56. void AssignImageList(wxImageList *imageList)
  57. {
  58. SetImageList(imageList);
  59. m_ownsImageListNormal = true;
  60. }
  61. void AssignStateImageList(wxImageList *imageList)
  62. {
  63. SetStateImageList(imageList);
  64. m_ownsImageListState = true;
  65. }
  66. // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
  67. // member functions of wxTreeItem because they must know the tree the item
  68. // belongs to for Windows implementation and storing the pointer to
  69. // wxTreeCtrl in each wxTreeItem is just too much waste.
  70. // accessors
  71. // ---------
  72. // retrieve items label
  73. virtual wxString GetItemText(const wxTreeItemId& item) const = 0;
  74. // get one of the images associated with the item (normal by default)
  75. virtual int GetItemImage(const wxTreeItemId& item,
  76. wxTreeItemIcon which = wxTreeItemIcon_Normal) const = 0;
  77. // get the data associated with the item
  78. virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const = 0;
  79. // get the item's text colour
  80. virtual wxColour GetItemTextColour(const wxTreeItemId& item) const = 0;
  81. // get the item's background colour
  82. virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const = 0;
  83. // get the item's font
  84. virtual wxFont GetItemFont(const wxTreeItemId& item) const = 0;
  85. // get the items state
  86. int GetItemState(const wxTreeItemId& item) const
  87. {
  88. return DoGetItemState(item);
  89. }
  90. // modifiers
  91. // ---------
  92. // set items label
  93. virtual void SetItemText(const wxTreeItemId& item, const wxString& text) = 0;
  94. // set one of the images associated with the item (normal by default)
  95. virtual void SetItemImage(const wxTreeItemId& item,
  96. int image,
  97. wxTreeItemIcon which = wxTreeItemIcon_Normal) = 0;
  98. // associate some data with the item
  99. virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data) = 0;
  100. // force appearance of [+] button near the item. This is useful to
  101. // allow the user to expand the items which don't have any children now
  102. // - but instead add them only when needed, thus minimizing memory
  103. // usage and loading time.
  104. virtual void SetItemHasChildren(const wxTreeItemId& item,
  105. bool has = true) = 0;
  106. // the item will be shown in bold
  107. virtual void SetItemBold(const wxTreeItemId& item, bool bold = true) = 0;
  108. // the item will be shown with a drop highlight
  109. virtual void SetItemDropHighlight(const wxTreeItemId& item,
  110. bool highlight = true) = 0;
  111. // set the items text colour
  112. virtual void SetItemTextColour(const wxTreeItemId& item,
  113. const wxColour& col) = 0;
  114. // set the items background colour
  115. virtual void SetItemBackgroundColour(const wxTreeItemId& item,
  116. const wxColour& col) = 0;
  117. // set the items font (should be of the same height for all items)
  118. virtual void SetItemFont(const wxTreeItemId& item,
  119. const wxFont& font) = 0;
  120. // set the items state (special state values: wxTREE_ITEMSTATE_NONE/NEXT/PREV)
  121. void SetItemState(const wxTreeItemId& item, int state);
  122. // item status inquiries
  123. // ---------------------
  124. // is the item visible (it might be outside the view or not expanded)?
  125. virtual bool IsVisible(const wxTreeItemId& item) const = 0;
  126. // does the item has any children?
  127. virtual bool ItemHasChildren(const wxTreeItemId& item) const = 0;
  128. // same as above
  129. bool HasChildren(const wxTreeItemId& item) const
  130. { return ItemHasChildren(item); }
  131. // is the item expanded (only makes sense if HasChildren())?
  132. virtual bool IsExpanded(const wxTreeItemId& item) const = 0;
  133. // is this item currently selected (the same as has focus)?
  134. virtual bool IsSelected(const wxTreeItemId& item) const = 0;
  135. // is item text in bold font?
  136. virtual bool IsBold(const wxTreeItemId& item) const = 0;
  137. // is the control empty?
  138. bool IsEmpty() const;
  139. // number of children
  140. // ------------------
  141. // if 'recursively' is false, only immediate children count, otherwise
  142. // the returned number is the number of all items in this branch
  143. virtual size_t GetChildrenCount(const wxTreeItemId& item,
  144. bool recursively = true) const = 0;
  145. // navigation
  146. // ----------
  147. // wxTreeItemId.IsOk() will return false if there is no such item
  148. // get the root tree item
  149. virtual wxTreeItemId GetRootItem() const = 0;
  150. // get the item currently selected (may return NULL if no selection)
  151. virtual wxTreeItemId GetSelection() const = 0;
  152. // get the items currently selected, return the number of such item
  153. //
  154. // NB: this operation is expensive and can take a long time for a
  155. // control with a lot of items (~ O(number of items)).
  156. virtual size_t GetSelections(wxArrayTreeItemIds& selections) const = 0;
  157. // get the last item to be clicked when the control has wxTR_MULTIPLE
  158. // equivalent to GetSelection() if not wxTR_MULTIPLE
  159. virtual wxTreeItemId GetFocusedItem() const = 0;
  160. // Clears the currently focused item
  161. virtual void ClearFocusedItem() = 0;
  162. // Sets the currently focused item. Item should be valid
  163. virtual void SetFocusedItem(const wxTreeItemId& item) = 0;
  164. // get the parent of this item (may return NULL if root)
  165. virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const = 0;
  166. // for this enumeration function you must pass in a "cookie" parameter
  167. // which is opaque for the application but is necessary for the library
  168. // to make these functions reentrant (i.e. allow more than one
  169. // enumeration on one and the same object simultaneously). Of course,
  170. // the "cookie" passed to GetFirstChild() and GetNextChild() should be
  171. // the same!
  172. // get the first child of this item
  173. virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
  174. wxTreeItemIdValue& cookie) const = 0;
  175. // get the next child
  176. virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
  177. wxTreeItemIdValue& cookie) const = 0;
  178. // get the last child of this item - this method doesn't use cookies
  179. virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const = 0;
  180. // get the next sibling of this item
  181. virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const = 0;
  182. // get the previous sibling
  183. virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const = 0;
  184. // get first visible item
  185. virtual wxTreeItemId GetFirstVisibleItem() const = 0;
  186. // get the next visible item: item must be visible itself!
  187. // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
  188. virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const = 0;
  189. // get the previous visible item: item must be visible itself!
  190. virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const = 0;
  191. // operations
  192. // ----------
  193. // add the root node to the tree
  194. virtual wxTreeItemId AddRoot(const wxString& text,
  195. int image = -1, int selImage = -1,
  196. wxTreeItemData *data = NULL) = 0;
  197. // insert a new item in as the first child of the parent
  198. wxTreeItemId PrependItem(const wxTreeItemId& parent,
  199. const wxString& text,
  200. int image = -1, int selImage = -1,
  201. wxTreeItemData *data = NULL)
  202. {
  203. return DoInsertItem(parent, 0u, text, image, selImage, data);
  204. }
  205. // insert a new item after a given one
  206. wxTreeItemId InsertItem(const wxTreeItemId& parent,
  207. const wxTreeItemId& idPrevious,
  208. const wxString& text,
  209. int image = -1, int selImage = -1,
  210. wxTreeItemData *data = NULL)
  211. {
  212. return DoInsertAfter(parent, idPrevious, text, image, selImage, data);
  213. }
  214. // insert a new item before the one with the given index
  215. wxTreeItemId InsertItem(const wxTreeItemId& parent,
  216. size_t pos,
  217. const wxString& text,
  218. int image = -1, int selImage = -1,
  219. wxTreeItemData *data = NULL)
  220. {
  221. return DoInsertItem(parent, pos, text, image, selImage, data);
  222. }
  223. // insert a new item in as the last child of the parent
  224. wxTreeItemId AppendItem(const wxTreeItemId& parent,
  225. const wxString& text,
  226. int image = -1, int selImage = -1,
  227. wxTreeItemData *data = NULL)
  228. {
  229. return DoInsertItem(parent, (size_t)-1, text, image, selImage, data);
  230. }
  231. // delete this item and associated data if any
  232. virtual void Delete(const wxTreeItemId& item) = 0;
  233. // delete all children (but don't delete the item itself)
  234. // NB: this won't send wxEVT_TREE_ITEM_DELETED events
  235. virtual void DeleteChildren(const wxTreeItemId& item) = 0;
  236. // delete all items from the tree
  237. // NB: this won't send wxEVT_TREE_ITEM_DELETED events
  238. virtual void DeleteAllItems() = 0;
  239. // expand this item
  240. virtual void Expand(const wxTreeItemId& item) = 0;
  241. // expand the item and all its children recursively
  242. void ExpandAllChildren(const wxTreeItemId& item);
  243. // expand all items
  244. void ExpandAll();
  245. // collapse the item without removing its children
  246. virtual void Collapse(const wxTreeItemId& item) = 0;
  247. // collapse the item and all its children
  248. void CollapseAllChildren(const wxTreeItemId& item);
  249. // collapse all items
  250. void CollapseAll();
  251. // collapse the item and remove all children
  252. virtual void CollapseAndReset(const wxTreeItemId& item) = 0;
  253. // toggles the current state
  254. virtual void Toggle(const wxTreeItemId& item) = 0;
  255. // remove the selection from currently selected item (if any)
  256. virtual void Unselect() = 0;
  257. // unselect all items (only makes sense for multiple selection control)
  258. virtual void UnselectAll() = 0;
  259. // select this item
  260. virtual void SelectItem(const wxTreeItemId& item, bool select = true) = 0;
  261. // selects all (direct) children for given parent (only for
  262. // multiselection controls)
  263. virtual void SelectChildren(const wxTreeItemId& parent) = 0;
  264. // unselect this item
  265. void UnselectItem(const wxTreeItemId& item) { SelectItem(item, false); }
  266. // toggle item selection
  267. void ToggleItemSelection(const wxTreeItemId& item)
  268. {
  269. SelectItem(item, !IsSelected(item));
  270. }
  271. // make sure this item is visible (expanding the parent item and/or
  272. // scrolling to this item if necessary)
  273. virtual void EnsureVisible(const wxTreeItemId& item) = 0;
  274. // scroll to this item (but don't expand its parent)
  275. virtual void ScrollTo(const wxTreeItemId& item) = 0;
  276. // start editing the item label: this (temporarily) replaces the item
  277. // with a one line edit control. The item will be selected if it hadn't
  278. // been before. textCtrlClass parameter allows you to create an edit
  279. // control of arbitrary user-defined class deriving from wxTextCtrl.
  280. virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
  281. wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl)) = 0;
  282. // returns the same pointer as StartEdit() if the item is being edited,
  283. // NULL otherwise (it's assumed that no more than one item may be
  284. // edited simultaneously)
  285. virtual wxTextCtrl *GetEditControl() const = 0;
  286. // end editing and accept or discard the changes to item label
  287. virtual void EndEditLabel(const wxTreeItemId& item,
  288. bool discardChanges = false) = 0;
  289. // Enable or disable beep when incremental match doesn't find any item.
  290. // Only implemented in the generic version currently.
  291. virtual void EnableBellOnNoMatch(bool WXUNUSED(on) = true) { }
  292. // sorting
  293. // -------
  294. // this function is called to compare 2 items and should return -1, 0
  295. // or +1 if the first item is less than, equal to or greater than the
  296. // second one. The base class version performs alphabetic comparaison
  297. // of item labels (GetText)
  298. virtual int OnCompareItems(const wxTreeItemId& item1,
  299. const wxTreeItemId& item2)
  300. {
  301. return wxStrcmp(GetItemText(item1), GetItemText(item2));
  302. }
  303. // sort the children of this item using OnCompareItems
  304. //
  305. // NB: this function is not reentrant and not MT-safe (FIXME)!
  306. virtual void SortChildren(const wxTreeItemId& item) = 0;
  307. // items geometry
  308. // --------------
  309. // determine to which item (if any) belongs the given point (the
  310. // coordinates specified are relative to the client area of tree ctrl)
  311. // and, in the second variant, fill the flags parameter with a bitmask
  312. // of wxTREE_HITTEST_xxx constants.
  313. wxTreeItemId HitTest(const wxPoint& point) const
  314. { int dummy; return DoTreeHitTest(point, dummy); }
  315. wxTreeItemId HitTest(const wxPoint& point, int& flags) const
  316. { return DoTreeHitTest(point, flags); }
  317. // get the bounding rectangle of the item (or of its label only)
  318. virtual bool GetBoundingRect(const wxTreeItemId& item,
  319. wxRect& rect,
  320. bool textOnly = false) const = 0;
  321. // implementation
  322. // --------------
  323. virtual bool ShouldInheritColours() const { return false; }
  324. // hint whether to calculate best size quickly or accurately
  325. void SetQuickBestSize(bool q) { m_quickBestSize = q; }
  326. bool GetQuickBestSize() const { return m_quickBestSize; }
  327. protected:
  328. virtual wxSize DoGetBestSize() const;
  329. // common part of Get/SetItemState()
  330. virtual int DoGetItemState(const wxTreeItemId& item) const = 0;
  331. virtual void DoSetItemState(const wxTreeItemId& item, int state) = 0;
  332. // common part of Append/Prepend/InsertItem()
  333. //
  334. // pos is the position at which to insert the item or (size_t)-1 to append
  335. // it to the end
  336. virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
  337. size_t pos,
  338. const wxString& text,
  339. int image, int selImage,
  340. wxTreeItemData *data) = 0;
  341. // and this function implements overloaded InsertItem() taking wxTreeItemId
  342. // (it can't be called InsertItem() as we'd have virtual function hiding
  343. // problem in derived classes then)
  344. virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
  345. const wxTreeItemId& idPrevious,
  346. const wxString& text,
  347. int image = -1, int selImage = -1,
  348. wxTreeItemData *data = NULL) = 0;
  349. // real HitTest() implementation: again, can't be called just HitTest()
  350. // because it's overloaded and so the non-virtual overload would be hidden
  351. // (and can't be called DoHitTest() because this is already in wxWindow)
  352. virtual wxTreeItemId DoTreeHitTest(const wxPoint& point,
  353. int& flags) const = 0;
  354. wxImageList *m_imageListNormal, // images for tree elements
  355. *m_imageListState; // special images for app defined states
  356. bool m_ownsImageListNormal,
  357. m_ownsImageListState;
  358. // spacing between left border and the text
  359. unsigned int m_spacing;
  360. // whether full or quick calculation is done in DoGetBestSize
  361. bool m_quickBestSize;
  362. private:
  363. // Intercept Escape and Return keys to ensure that our in-place edit
  364. // control always gets them before they're used for dialog navigation or
  365. // anything else.
  366. void OnCharHook(wxKeyEvent& event);
  367. wxDECLARE_NO_COPY_CLASS(wxTreeCtrlBase);
  368. };
  369. // ----------------------------------------------------------------------------
  370. // include the platform-dependent wxTreeCtrl class
  371. // ----------------------------------------------------------------------------
  372. #if defined(__WXUNIVERSAL__)
  373. #include "wx/generic/treectlg.h"
  374. #elif defined(__WXMSW__)
  375. #include "wx/msw/treectrl.h"
  376. #elif defined(__WXMOTIF__)
  377. #include "wx/generic/treectlg.h"
  378. #elif defined(__WXGTK__)
  379. #include "wx/generic/treectlg.h"
  380. #elif defined(__WXMAC__)
  381. #include "wx/generic/treectlg.h"
  382. #elif defined(__WXCOCOA__)
  383. #include "wx/generic/treectlg.h"
  384. #elif defined(__WXPM__)
  385. #include "wx/generic/treectlg.h"
  386. #endif
  387. #endif // wxUSE_TREECTRL
  388. #endif // _WX_TREECTRL_H_BASE_