treectlg.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/generic/treectlg.h
  3. // Purpose: wxTreeCtrl class
  4. // Author: Robert Roebling
  5. // Modified by:
  6. // Created: 01/02/97
  7. // Copyright: (c) 1997,1998 Robert Roebling
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _GENERIC_TREECTRL_H_
  11. #define _GENERIC_TREECTRL_H_
  12. #if wxUSE_TREECTRL
  13. #include "wx/scrolwin.h"
  14. #include "wx/pen.h"
  15. // -----------------------------------------------------------------------------
  16. // forward declaration
  17. // -----------------------------------------------------------------------------
  18. class WXDLLIMPEXP_FWD_CORE wxGenericTreeItem;
  19. class WXDLLIMPEXP_FWD_CORE wxTreeItemData;
  20. class WXDLLIMPEXP_FWD_CORE wxTreeRenameTimer;
  21. class WXDLLIMPEXP_FWD_CORE wxTreeFindTimer;
  22. class WXDLLIMPEXP_FWD_CORE wxTreeTextCtrl;
  23. class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
  24. // -----------------------------------------------------------------------------
  25. // wxGenericTreeCtrl - the tree control
  26. // -----------------------------------------------------------------------------
  27. class WXDLLIMPEXP_CORE wxGenericTreeCtrl : public wxTreeCtrlBase,
  28. public wxScrollHelper
  29. {
  30. public:
  31. // creation
  32. // --------
  33. wxGenericTreeCtrl() : wxTreeCtrlBase(), wxScrollHelper(this) { Init(); }
  34. wxGenericTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
  35. const wxPoint& pos = wxDefaultPosition,
  36. const wxSize& size = wxDefaultSize,
  37. long style = wxTR_DEFAULT_STYLE,
  38. const wxValidator &validator = wxDefaultValidator,
  39. const wxString& name = wxTreeCtrlNameStr)
  40. : wxTreeCtrlBase(),
  41. wxScrollHelper(this)
  42. {
  43. Init();
  44. Create(parent, id, pos, size, style, validator, name);
  45. }
  46. virtual ~wxGenericTreeCtrl();
  47. bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
  48. const wxPoint& pos = wxDefaultPosition,
  49. const wxSize& size = wxDefaultSize,
  50. long style = wxTR_DEFAULT_STYLE,
  51. const wxValidator &validator = wxDefaultValidator,
  52. const wxString& name = wxTreeCtrlNameStr);
  53. // implement base class pure virtuals
  54. // ----------------------------------
  55. virtual unsigned int GetCount() const;
  56. virtual unsigned int GetIndent() const { return m_indent; }
  57. virtual void SetIndent(unsigned int indent);
  58. virtual void SetImageList(wxImageList *imageList);
  59. virtual void SetStateImageList(wxImageList *imageList);
  60. virtual wxString GetItemText(const wxTreeItemId& item) const;
  61. virtual int GetItemImage(const wxTreeItemId& item,
  62. wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
  63. virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
  64. virtual wxColour GetItemTextColour(const wxTreeItemId& item) const;
  65. virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
  66. virtual wxFont GetItemFont(const wxTreeItemId& item) const;
  67. virtual void SetItemText(const wxTreeItemId& item, const wxString& text);
  68. virtual void SetItemImage(const wxTreeItemId& item,
  69. int image,
  70. wxTreeItemIcon which = wxTreeItemIcon_Normal);
  71. virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
  72. virtual void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
  73. virtual void SetItemBold(const wxTreeItemId& item, bool bold = true);
  74. virtual void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true);
  75. virtual void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
  76. virtual void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
  77. virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font);
  78. virtual bool IsVisible(const wxTreeItemId& item) const;
  79. virtual bool ItemHasChildren(const wxTreeItemId& item) const;
  80. virtual bool IsExpanded(const wxTreeItemId& item) const;
  81. virtual bool IsSelected(const wxTreeItemId& item) const;
  82. virtual bool IsBold(const wxTreeItemId& item) const;
  83. virtual size_t GetChildrenCount(const wxTreeItemId& item,
  84. bool recursively = true) const;
  85. // navigation
  86. // ----------
  87. virtual wxTreeItemId GetRootItem() const { return m_anchor; }
  88. virtual wxTreeItemId GetSelection() const
  89. {
  90. wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE),
  91. wxT("must use GetSelections() with this control") );
  92. return m_current;
  93. }
  94. virtual size_t GetSelections(wxArrayTreeItemIds&) const;
  95. virtual wxTreeItemId GetFocusedItem() const { return m_current; }
  96. virtual void ClearFocusedItem();
  97. virtual void SetFocusedItem(const wxTreeItemId& item);
  98. virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
  99. virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
  100. wxTreeItemIdValue& cookie) const;
  101. virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
  102. wxTreeItemIdValue& cookie) const;
  103. virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
  104. virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
  105. virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
  106. virtual wxTreeItemId GetFirstVisibleItem() const;
  107. virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
  108. virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
  109. // operations
  110. // ----------
  111. virtual wxTreeItemId AddRoot(const wxString& text,
  112. int image = -1, int selectedImage = -1,
  113. wxTreeItemData *data = NULL);
  114. virtual void Delete(const wxTreeItemId& item);
  115. virtual void DeleteChildren(const wxTreeItemId& item);
  116. virtual void DeleteAllItems();
  117. virtual void Expand(const wxTreeItemId& item);
  118. virtual void Collapse(const wxTreeItemId& item);
  119. virtual void CollapseAndReset(const wxTreeItemId& item);
  120. virtual void Toggle(const wxTreeItemId& item);
  121. virtual void Unselect();
  122. virtual void UnselectAll();
  123. virtual void SelectItem(const wxTreeItemId& item, bool select = true);
  124. virtual void SelectChildren(const wxTreeItemId& parent);
  125. virtual void EnsureVisible(const wxTreeItemId& item);
  126. virtual void ScrollTo(const wxTreeItemId& item);
  127. virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
  128. wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl));
  129. virtual wxTextCtrl *GetEditControl() const;
  130. virtual void EndEditLabel(const wxTreeItemId& item,
  131. bool discardChanges = false);
  132. virtual void EnableBellOnNoMatch(bool on = true);
  133. virtual void SortChildren(const wxTreeItemId& item);
  134. // items geometry
  135. // --------------
  136. virtual bool GetBoundingRect(const wxTreeItemId& item,
  137. wxRect& rect,
  138. bool textOnly = false) const;
  139. // this version specific methods
  140. // -----------------------------
  141. wxImageList *GetButtonsImageList() const { return m_imageListButtons; }
  142. void SetButtonsImageList(wxImageList *imageList);
  143. void AssignButtonsImageList(wxImageList *imageList);
  144. void SetDropEffectAboveItem( bool above = false ) { m_dropEffectAboveItem = above; }
  145. bool GetDropEffectAboveItem() const { return m_dropEffectAboveItem; }
  146. wxTreeItemId GetNext(const wxTreeItemId& item) const;
  147. #if WXWIN_COMPATIBILITY_2_6
  148. // use EditLabel() instead
  149. void Edit( const wxTreeItemId& item ) { EditLabel(item); }
  150. #endif // WXWIN_COMPATIBILITY_2_6
  151. // implementation only from now on
  152. // overridden base class virtuals
  153. virtual bool SetBackgroundColour(const wxColour& colour);
  154. virtual bool SetForegroundColour(const wxColour& colour);
  155. virtual void Refresh(bool eraseBackground = true, const wxRect *rect = NULL);
  156. virtual bool SetFont( const wxFont &font );
  157. virtual void SetWindowStyle(const long styles);
  158. // callbacks
  159. void OnPaint( wxPaintEvent &event );
  160. void OnSetFocus( wxFocusEvent &event );
  161. void OnKillFocus( wxFocusEvent &event );
  162. void OnKeyDown( wxKeyEvent &event );
  163. void OnChar( wxKeyEvent &event );
  164. void OnMouse( wxMouseEvent &event );
  165. void OnGetToolTip( wxTreeEvent &event );
  166. void OnSize( wxSizeEvent &event );
  167. void OnInternalIdle( );
  168. virtual wxVisualAttributes GetDefaultAttributes() const
  169. {
  170. return GetClassDefaultAttributes(GetWindowVariant());
  171. }
  172. static wxVisualAttributes
  173. GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
  174. // implementation helpers
  175. void AdjustMyScrollbars();
  176. WX_FORWARD_TO_SCROLL_HELPER()
  177. protected:
  178. friend class wxGenericTreeItem;
  179. friend class wxTreeRenameTimer;
  180. friend class wxTreeFindTimer;
  181. friend class wxTreeTextCtrl;
  182. wxFont m_normalFont;
  183. wxFont m_boldFont;
  184. wxGenericTreeItem *m_anchor;
  185. wxGenericTreeItem *m_current,
  186. *m_key_current,
  187. // A hint to select a parent item after deleting a child
  188. *m_select_me;
  189. unsigned short m_indent;
  190. int m_lineHeight;
  191. wxPen m_dottedPen;
  192. wxBrush *m_hilightBrush,
  193. *m_hilightUnfocusedBrush;
  194. bool m_hasFocus;
  195. bool m_dirty;
  196. bool m_ownsImageListButtons;
  197. bool m_isDragging; // true between BEGIN/END drag events
  198. bool m_lastOnSame; // last click on the same item as prev
  199. wxImageList *m_imageListButtons;
  200. int m_dragCount;
  201. wxPoint m_dragStart;
  202. wxGenericTreeItem *m_dropTarget;
  203. wxCursor m_oldCursor; // cursor is changed while dragging
  204. wxGenericTreeItem *m_oldSelection;
  205. wxGenericTreeItem *m_underMouse; // for visual effects
  206. enum { NoEffect, BorderEffect, AboveEffect, BelowEffect } m_dndEffect;
  207. wxGenericTreeItem *m_dndEffectItem;
  208. wxTreeTextCtrl *m_textCtrl;
  209. wxTimer *m_renameTimer;
  210. // incremental search data
  211. wxString m_findPrefix;
  212. wxTimer *m_findTimer;
  213. // This flag is set to 0 if the bell is disabled, 1 if it is enabled and -1
  214. // if it is globally enabled but has been temporarily disabled because we
  215. // had already beeped for this particular search.
  216. int m_findBell;
  217. bool m_dropEffectAboveItem;
  218. // the common part of all ctors
  219. void Init();
  220. // overridden wxWindow methods
  221. virtual void DoThaw();
  222. // misc helpers
  223. void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted);
  224. void DrawBorder(const wxTreeItemId& item);
  225. void DrawLine(const wxTreeItemId& item, bool below);
  226. void DrawDropEffect(wxGenericTreeItem *item);
  227. void DoSelectItem(const wxTreeItemId& id,
  228. bool unselect_others = true,
  229. bool extended_select = false);
  230. virtual int DoGetItemState(const wxTreeItemId& item) const;
  231. virtual void DoSetItemState(const wxTreeItemId& item, int state);
  232. virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
  233. size_t previous,
  234. const wxString& text,
  235. int image,
  236. int selectedImage,
  237. wxTreeItemData *data);
  238. virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
  239. const wxTreeItemId& idPrevious,
  240. const wxString& text,
  241. int image = -1, int selImage = -1,
  242. wxTreeItemData *data = NULL);
  243. virtual wxTreeItemId DoTreeHitTest(const wxPoint& point, int& flags) const;
  244. // called by wxTextTreeCtrl when it marks itself for deletion
  245. void ResetTextControl();
  246. // find the first item starting with the given prefix after the given item
  247. wxTreeItemId FindItem(const wxTreeItemId& id, const wxString& prefix) const;
  248. bool HasButtons() const { return HasFlag(wxTR_HAS_BUTTONS); }
  249. void CalculateLineHeight();
  250. int GetLineHeight(wxGenericTreeItem *item) const;
  251. void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y );
  252. void PaintItem( wxGenericTreeItem *item, wxDC& dc);
  253. void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y );
  254. void CalculatePositions();
  255. void RefreshSubtree( wxGenericTreeItem *item );
  256. void RefreshLine( wxGenericTreeItem *item );
  257. // redraw all selected items
  258. void RefreshSelected();
  259. // RefreshSelected() recursive helper
  260. void RefreshSelectedUnder(wxGenericTreeItem *item);
  261. void OnRenameTimer();
  262. bool OnRenameAccept(wxGenericTreeItem *item, const wxString& value);
  263. void OnRenameCancelled(wxGenericTreeItem *item);
  264. void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const;
  265. void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 );
  266. bool TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
  267. bool TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
  268. void UnselectAllChildren( wxGenericTreeItem *item );
  269. void ChildrenClosing(wxGenericTreeItem* item);
  270. void DoDirtyProcessing();
  271. virtual wxSize DoGetBestSize() const;
  272. private:
  273. // Reset the state of the last find (i.e. keyboard incremental search)
  274. // operation.
  275. void ResetFindState();
  276. DECLARE_EVENT_TABLE()
  277. DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl)
  278. wxDECLARE_NO_COPY_CLASS(wxGenericTreeCtrl);
  279. };
  280. #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
  281. /*
  282. * wxTreeCtrl has to be a real class or we have problems with
  283. * the run-time information.
  284. */
  285. class WXDLLIMPEXP_CORE wxTreeCtrl: public wxGenericTreeCtrl
  286. {
  287. DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
  288. public:
  289. wxTreeCtrl() {}
  290. wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
  291. const wxPoint& pos = wxDefaultPosition,
  292. const wxSize& size = wxDefaultSize,
  293. long style = wxTR_DEFAULT_STYLE,
  294. const wxValidator &validator = wxDefaultValidator,
  295. const wxString& name = wxTreeCtrlNameStr)
  296. : wxGenericTreeCtrl(parent, id, pos, size, style, validator, name)
  297. {
  298. }
  299. };
  300. #endif // !__WXMSW__ || __WXUNIVERSAL__
  301. #endif // wxUSE_TREECTRL
  302. #endif // _GENERIC_TREECTRL_H_