treectrl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/treectrl.h
  3. // Purpose: wxTreeCtrl class
  4. // Author: Julian Smart
  5. // Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
  6. // Created: 01/02/97
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_MSW_TREECTRL_H_
  11. #define _WX_MSW_TREECTRL_H_
  12. // ----------------------------------------------------------------------------
  13. // headers
  14. // ----------------------------------------------------------------------------
  15. #if wxUSE_TREECTRL
  16. #include "wx/textctrl.h"
  17. #include "wx/dynarray.h"
  18. #include "wx/treebase.h"
  19. #include "wx/hashmap.h"
  20. #ifdef __GNUWIN32__
  21. // Cygwin windows.h defines these identifiers
  22. #undef GetFirstChild
  23. #undef GetNextSibling
  24. #endif // Cygwin
  25. // fwd decl
  26. class WXDLLIMPEXP_FWD_CORE wxImageList;
  27. class WXDLLIMPEXP_FWD_CORE wxDragImage;
  28. struct WXDLLIMPEXP_FWD_CORE wxTreeViewItem;
  29. #if WXWIN_COMPATIBILITY_2_6
  30. // NB: all the following flags are for compatbility only and will be removed in the
  31. // next versions
  32. // flags for deprecated InsertItem() variant (their values are the same as of
  33. // TVI_FIRST and TVI_LAST)
  34. #define wxTREE_INSERT_FIRST 0xFFFF0001
  35. #define wxTREE_INSERT_LAST 0xFFFF0002
  36. #endif
  37. // hash storing attributes for our items
  38. WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr);
  39. // ----------------------------------------------------------------------------
  40. // wxTreeCtrl
  41. // ----------------------------------------------------------------------------
  42. class WXDLLIMPEXP_CORE wxTreeCtrl : public wxTreeCtrlBase
  43. {
  44. public:
  45. // creation
  46. // --------
  47. wxTreeCtrl() { Init(); }
  48. wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
  49. const wxPoint& pos = wxDefaultPosition,
  50. const wxSize& size = wxDefaultSize,
  51. long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
  52. const wxValidator& validator = wxDefaultValidator,
  53. const wxString& name = wxTreeCtrlNameStr)
  54. {
  55. Create(parent, id, pos, size, style, validator, name);
  56. }
  57. virtual ~wxTreeCtrl();
  58. bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
  59. const wxPoint& pos = wxDefaultPosition,
  60. const wxSize& size = wxDefaultSize,
  61. long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
  62. const wxValidator& validator = wxDefaultValidator,
  63. const wxString& name = wxTreeCtrlNameStr);
  64. // implement base class pure virtuals
  65. // ----------------------------------
  66. virtual unsigned int GetCount() const;
  67. virtual unsigned int GetIndent() const;
  68. virtual void SetIndent(unsigned int indent);
  69. virtual void SetImageList(wxImageList *imageList);
  70. virtual void SetStateImageList(wxImageList *imageList);
  71. virtual wxString GetItemText(const wxTreeItemId& item) const;
  72. virtual int GetItemImage(const wxTreeItemId& item,
  73. wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
  74. virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
  75. virtual wxColour GetItemTextColour(const wxTreeItemId& item) const;
  76. virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
  77. virtual wxFont GetItemFont(const wxTreeItemId& item) const;
  78. virtual void SetItemText(const wxTreeItemId& item, const wxString& text);
  79. virtual void SetItemImage(const wxTreeItemId& item, int image,
  80. wxTreeItemIcon which = wxTreeItemIcon_Normal);
  81. virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
  82. virtual void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
  83. virtual void SetItemBold(const wxTreeItemId& item, bool bold = true);
  84. virtual void SetItemDropHighlight(const wxTreeItemId& item,
  85. bool highlight = true);
  86. virtual void SetItemTextColour(const wxTreeItemId& item,
  87. const wxColour& col);
  88. virtual void SetItemBackgroundColour(const wxTreeItemId& item,
  89. const wxColour& col);
  90. virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font);
  91. // item status inquiries
  92. // ---------------------
  93. virtual bool IsVisible(const wxTreeItemId& item) const;
  94. virtual bool ItemHasChildren(const wxTreeItemId& item) const;
  95. virtual bool IsExpanded(const wxTreeItemId& item) const;
  96. virtual bool IsSelected(const wxTreeItemId& item) const;
  97. virtual bool IsBold(const wxTreeItemId& item) const;
  98. virtual size_t GetChildrenCount(const wxTreeItemId& item,
  99. bool recursively = true) const;
  100. // navigation
  101. // ----------
  102. virtual wxTreeItemId GetRootItem() const;
  103. virtual wxTreeItemId GetSelection() const;
  104. virtual size_t GetSelections(wxArrayTreeItemIds& selections) const;
  105. virtual wxTreeItemId GetFocusedItem() const;
  106. virtual void ClearFocusedItem();
  107. virtual void SetFocusedItem(const wxTreeItemId& item);
  108. virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
  109. virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
  110. wxTreeItemIdValue& cookie) const;
  111. virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
  112. wxTreeItemIdValue& cookie) const;
  113. virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
  114. virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
  115. virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
  116. virtual wxTreeItemId GetFirstVisibleItem() const;
  117. virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
  118. virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
  119. // operations
  120. // ----------
  121. virtual wxTreeItemId AddRoot(const wxString& text,
  122. int image = -1, int selectedImage = -1,
  123. wxTreeItemData *data = NULL);
  124. virtual void Delete(const wxTreeItemId& item);
  125. virtual void DeleteChildren(const wxTreeItemId& item);
  126. virtual void DeleteAllItems();
  127. virtual void Expand(const wxTreeItemId& item);
  128. virtual void Collapse(const wxTreeItemId& item);
  129. virtual void CollapseAndReset(const wxTreeItemId& item);
  130. virtual void Toggle(const wxTreeItemId& item);
  131. virtual void Unselect();
  132. virtual void UnselectAll();
  133. virtual void SelectItem(const wxTreeItemId& item, bool select = true);
  134. virtual void SelectChildren(const wxTreeItemId& parent);
  135. virtual void EnsureVisible(const wxTreeItemId& item);
  136. virtual void ScrollTo(const wxTreeItemId& item);
  137. virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
  138. wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl));
  139. virtual wxTextCtrl *GetEditControl() const;
  140. virtual void EndEditLabel(const wxTreeItemId& WXUNUSED(item),
  141. bool discardChanges = false)
  142. {
  143. DoEndEditLabel(discardChanges);
  144. }
  145. virtual void SortChildren(const wxTreeItemId& item);
  146. virtual bool GetBoundingRect(const wxTreeItemId& item,
  147. wxRect& rect,
  148. bool textOnly = false) const;
  149. // implementation
  150. // --------------
  151. virtual wxVisualAttributes GetDefaultAttributes() const
  152. {
  153. return GetClassDefaultAttributes(GetWindowVariant());
  154. }
  155. static wxVisualAttributes
  156. GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
  157. virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
  158. virtual WXLRESULT MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
  159. virtual bool MSWCommand(WXUINT param, WXWORD id);
  160. virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
  161. virtual bool MSWShouldPreProcessMessage(WXMSG* msg);
  162. // override some base class virtuals
  163. virtual bool SetBackgroundColour(const wxColour &colour);
  164. virtual bool SetForegroundColour(const wxColour &colour);
  165. // returns true if the platform should explicitly apply a theme border
  166. virtual bool CanApplyThemeBorder() const { return false; }
  167. protected:
  168. // Implement "update locking" in a custom way for this control.
  169. virtual void DoFreeze();
  170. virtual void DoThaw();
  171. virtual void DoSetSize(int x, int y,
  172. int width, int height,
  173. int sizeFlags = wxSIZE_AUTO);
  174. // SetImageList helper
  175. void SetAnyImageList(wxImageList *imageList, int which);
  176. // refresh a single item
  177. void RefreshItem(const wxTreeItemId& item);
  178. // end edit label
  179. void DoEndEditLabel(bool discardChanges = false);
  180. virtual int DoGetItemState(const wxTreeItemId& item) const;
  181. virtual void DoSetItemState(const wxTreeItemId& item, int state);
  182. virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
  183. size_t pos,
  184. const wxString& text,
  185. int image, int selectedImage,
  186. wxTreeItemData *data);
  187. virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
  188. const wxTreeItemId& idPrevious,
  189. const wxString& text,
  190. int image = -1, int selImage = -1,
  191. wxTreeItemData *data = NULL);
  192. virtual wxTreeItemId DoTreeHitTest(const wxPoint& point, int& flags) const;
  193. // obtain the user data for the lParam member of TV_ITEM
  194. class wxTreeItemParam *GetItemParam(const wxTreeItemId& item) const;
  195. // update the event to include the items client data and pass it to
  196. // HandleWindowEvent(), return true if it processed it
  197. bool HandleTreeEvent(wxTreeEvent& event) const;
  198. // pass the event to HandleTreeEvent() and return true if the event was
  199. // either unprocessed or not vetoed
  200. bool IsTreeEventAllowed(wxTreeEvent& event) const
  201. {
  202. return !HandleTreeEvent(event) || event.IsAllowed();
  203. }
  204. // generate a wxEVT_KEY_DOWN event from the specified WPARAM/LPARAM values
  205. // having the same meaning as for WM_KEYDOWN, return true if it was
  206. // processed
  207. bool MSWHandleTreeKeyDownEvent(WXWPARAM wParam, WXLPARAM lParam);
  208. // handle a key event in a multi-selection control, should be only called
  209. // for keys which can be used to change the selection
  210. //
  211. // return true if the key was processed, false otherwise
  212. bool MSWHandleSelectionKey(unsigned vkey);
  213. // data used only while editing the item label:
  214. wxTextCtrl *m_textCtrl; // text control in which it is edited
  215. wxTreeItemId m_idEdited; // the item being edited
  216. private:
  217. // the common part of all ctors
  218. void Init();
  219. // helper functions
  220. bool DoGetItem(wxTreeViewItem *tvItem) const;
  221. void DoSetItem(wxTreeViewItem *tvItem);
  222. void DoExpand(const wxTreeItemId& item, int flag);
  223. void DoSelectItem(const wxTreeItemId& item, bool select = true);
  224. void DoUnselectItem(const wxTreeItemId& item);
  225. void DoToggleItemSelection(const wxTreeItemId& item);
  226. void DoUnselectAll();
  227. void DoSelectChildren(const wxTreeItemId& parent);
  228. void DeleteTextCtrl();
  229. // return true if the item is the hidden root one (i.e. it's the root item
  230. // and the tree has wxTR_HIDE_ROOT style)
  231. bool IsHiddenRoot(const wxTreeItemId& item) const;
  232. // check if the given flags (taken from TV_HITTESTINFO structure)
  233. // indicate a position "on item": this is less trivial than just checking
  234. // for TVHT_ONITEM because we consider that points to the left and right of
  235. // item text are also "on item" when wxTR_FULL_ROW_HIGHLIGHT is used as the
  236. // item visually spans the entire breadth of the window then
  237. bool MSWIsOnItem(unsigned flags) const;
  238. // the hash storing the items attributes (indexed by item ids)
  239. wxMapTreeAttr m_attrs;
  240. // true if the hash above is not empty
  241. bool m_hasAnyAttr;
  242. #if wxUSE_DRAGIMAGE
  243. // used for dragging
  244. wxDragImage *m_dragImage;
  245. #endif
  246. // Virtual root item, if wxTR_HIDE_ROOT is set.
  247. void* m_pVirtualRoot;
  248. // the starting item for selection with Shift
  249. wxTreeItemId m_htSelStart, m_htClickedItem;
  250. wxPoint m_ptClick;
  251. // whether dragging has started
  252. bool m_dragStarted;
  253. // whether focus was lost between subsequent clicks of a single item
  254. bool m_focusLost;
  255. // set when we are changing selection ourselves (only used in multi
  256. // selection mode)
  257. bool m_changingSelection;
  258. // whether we need to trigger a state image click event
  259. bool m_triggerStateImageClick;
  260. // whether we need to deselect other items on mouse up
  261. bool m_mouseUpDeselect;
  262. // The size to restore the control to when it is thawed, see DoThaw().
  263. wxSize m_thawnSize;
  264. friend class wxTreeItemIndirectData;
  265. friend class wxTreeSortHelper;
  266. DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
  267. wxDECLARE_NO_COPY_CLASS(wxTreeCtrl);
  268. };
  269. #endif // wxUSE_TREECTRL
  270. #endif // _WX_MSW_TREECTRL_H_