]>
Commit | Line | Data |
---|---|---|
f135ff73 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: treectrl.h | |
3 | // Purpose: wxTreeCtrl class | |
4 | // Author: Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1997,1998 Robert Roebling | |
9 | // Licence: wxWindows license | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #ifndef _GENERIC_TREECTRL_H_ | |
13 | #define _GENERIC_TREECTRL_H_ | |
c801d85f KB |
14 | |
15 | #ifdef __GNUG__ | |
233058c7 | 16 | #pragma interface "treectlg.h" |
c801d85f KB |
17 | #endif |
18 | ||
19 | #include "wx/defs.h" | |
20 | #include "wx/string.h" | |
f135ff73 | 21 | #include "wx/object.h" |
c801d85f | 22 | #include "wx/event.h" |
c801d85f | 23 | #include "wx/scrolwin.h" |
f135ff73 | 24 | #include "wx/textctrl.h" |
ac57418f | 25 | #include "wx/pen.h" |
91b8de8d | 26 | #include "wx/dynarray.h" |
e179bd65 | 27 | #include "wx/timer.h" |
484523cf | 28 | #include "wx/treebase.h" |
91b8de8d | 29 | |
f135ff73 VZ |
30 | // ----------------------------------------------------------------------------- |
31 | // forward declaration | |
32 | // ----------------------------------------------------------------------------- | |
c801d85f | 33 | |
e179bd65 RR |
34 | class WXDLLEXPORT wxImageList; |
35 | class WXDLLEXPORT wxGenericTreeItem; | |
c801d85f | 36 | |
e179bd65 RR |
37 | class WXDLLEXPORT wxTreeItemData; |
38 | ||
39 | class WXDLLEXPORT wxTreeRenameTimer; | |
40 | class WXDLLEXPORT wxTreeTextCtrl; | |
74bedbeb | 41 | |
f135ff73 | 42 | // ----------------------------------------------------------------------------- |
484523cf | 43 | // wxGenericTreeCtrl - the tree control |
f135ff73 | 44 | // ----------------------------------------------------------------------------- |
c801d85f | 45 | |
484523cf | 46 | class WXDLLEXPORT wxGenericTreeCtrl : public wxScrolledWindow |
c801d85f | 47 | { |
a32dd690 | 48 | public: |
f135ff73 VZ |
49 | // creation |
50 | // -------- | |
484523cf | 51 | wxGenericTreeCtrl() { Init(); } |
f135ff73 | 52 | |
484523cf | 53 | wxGenericTreeCtrl(wxWindow *parent, wxWindowID id = -1, |
f135ff73 VZ |
54 | const wxPoint& pos = wxDefaultPosition, |
55 | const wxSize& size = wxDefaultSize, | |
56 | long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, | |
bfc6fde4 | 57 | const wxValidator &validator = wxDefaultValidator, |
62448488 | 58 | const wxString& name = wxTreeCtrlNameStr) |
f135ff73 | 59 | { |
57591e0e | 60 | Init(); |
4f22cf8d | 61 | Create(parent, id, pos, size, style, validator, name); |
f135ff73 VZ |
62 | } |
63 | ||
484523cf | 64 | virtual ~wxGenericTreeCtrl(); |
f135ff73 VZ |
65 | |
66 | bool Create(wxWindow *parent, wxWindowID id = -1, | |
67 | const wxPoint& pos = wxDefaultPosition, | |
68 | const wxSize& size = wxDefaultSize, | |
69 | long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, | |
bfc6fde4 | 70 | const wxValidator &validator = wxDefaultValidator, |
62448488 | 71 | const wxString& name = wxTreeCtrlNameStr); |
f135ff73 VZ |
72 | |
73 | // accessors | |
74 | // --------- | |
75 | ||
76 | // get the total number of items in the control | |
77 | size_t GetCount() const; | |
78 | ||
79 | // indent is the number of pixels the children are indented relative to | |
80 | // the parents position. SetIndent() also redraws the control | |
81 | // immediately. | |
82 | unsigned int GetIndent() const { return m_indent; } | |
83 | void SetIndent(unsigned int indent); | |
84 | ||
9dfbf520 | 85 | // spacing is the number of pixels between the start and the Text |
cf724bce RR |
86 | unsigned int GetSpacing() const { return m_spacing; } |
87 | void SetSpacing(unsigned int spacing); | |
9dfbf520 | 88 | |
f135ff73 VZ |
89 | // image list: these functions allow to associate an image list with |
90 | // the control and retrieve it. Note that the control does _not_ delete | |
91 | // the associated image list when it's deleted in order to allow image | |
92 | // lists to be shared between different controls. | |
93 | // | |
94 | // The normal image list is for the icons which correspond to the | |
95 | // normal tree item state (whether it is selected or not). | |
96 | // Additionally, the application might choose to show a state icon | |
97 | // which corresponds to an app-defined item state (for example, | |
98 | // checked/unchecked) which are taken from the state image list. | |
99 | wxImageList *GetImageList() const; | |
100 | wxImageList *GetStateImageList() const; | |
101 | ||
102 | void SetImageList(wxImageList *imageList); | |
103 | void SetStateImageList(wxImageList *imageList); | |
104 | ||
105 | // Functions to work with tree ctrl items. | |
106 | ||
107 | // accessors | |
108 | // --------- | |
109 | ||
110 | // retrieve items label | |
111 | wxString GetItemText(const wxTreeItemId& item) const; | |
74b31181 VZ |
112 | // get one of the images associated with the item (normal by default) |
113 | int GetItemImage(const wxTreeItemId& item, | |
114 | wxTreeItemIcon which = wxTreeItemIcon_Normal) const; | |
f135ff73 VZ |
115 | // get the data associated with the item |
116 | wxTreeItemData *GetItemData(const wxTreeItemId& item) const; | |
117 | ||
118 | // modifiers | |
119 | // --------- | |
120 | ||
121 | // set items label | |
122 | void SetItemText(const wxTreeItemId& item, const wxString& text); | |
74b31181 VZ |
123 | // get one of the images associated with the item (normal by default) |
124 | void SetItemImage(const wxTreeItemId& item, int image, | |
8dc99046 | 125 | wxTreeItemIcon which = wxTreeItemIcon_Normal); |
f135ff73 VZ |
126 | // associate some data with the item |
127 | void SetItemData(const wxTreeItemId& item, wxTreeItemData *data); | |
128 | ||
129 | // force appearance of [+] button near the item. This is useful to | |
130 | // allow the user to expand the items which don't have any children now | |
131 | // - but instead add them only when needed, thus minimizing memory | |
132 | // usage and loading time. | |
133 | void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE); | |
134 | ||
ef44a621 VZ |
135 | // the item will be shown in bold |
136 | void SetItemBold(const wxTreeItemId& item, bool bold = TRUE); | |
137 | ||
9ec64fa7 VZ |
138 | // set the items text colour |
139 | void SetItemTextColour(const wxTreeItemId& item, const wxColour& col); | |
140 | ||
141 | // set the items background colour | |
142 | void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col); | |
143 | ||
144 | // set the items font (should be of the same height for all items) | |
145 | void SetItemFont(const wxTreeItemId& item, const wxFont& font); | |
146 | ||
f135ff73 VZ |
147 | // item status inquiries |
148 | // --------------------- | |
149 | ||
150 | // is the item visible (it might be outside the view or not expanded)? | |
151 | bool IsVisible(const wxTreeItemId& item) const; | |
152 | // does the item has any children? | |
6daa0637 RR |
153 | bool HasChildren(const wxTreeItemId& item) const |
154 | { return ItemHasChildren(item); } | |
f135ff73 VZ |
155 | bool ItemHasChildren(const wxTreeItemId& item) const; |
156 | // is the item expanded (only makes sense if HasChildren())? | |
157 | bool IsExpanded(const wxTreeItemId& item) const; | |
158 | // is this item currently selected (the same as has focus)? | |
159 | bool IsSelected(const wxTreeItemId& item) const; | |
ef44a621 VZ |
160 | // is item text in bold font? |
161 | bool IsBold(const wxTreeItemId& item) const; | |
f135ff73 | 162 | |
4832f7c0 VZ |
163 | // number of children |
164 | // ------------------ | |
165 | ||
166 | // if 'recursively' is FALSE, only immediate children count, otherwise | |
167 | // the returned number is the number of all items in this branch | |
168 | size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE); | |
169 | ||
f135ff73 VZ |
170 | // navigation |
171 | // ---------- | |
172 | ||
173 | // wxTreeItemId.IsOk() will return FALSE if there is no such item | |
174 | ||
175 | // get the root tree item | |
176 | wxTreeItemId GetRootItem() const { return m_anchor; } | |
177 | ||
178 | // get the item currently selected (may return NULL if no selection) | |
179 | wxTreeItemId GetSelection() const { return m_current; } | |
180 | ||
88ac883a | 181 | // get the items currently selected, return the number of such item |
91b8de8d | 182 | size_t GetSelections(wxArrayTreeItemIds&) const; |
88ac883a | 183 | |
f135ff73 VZ |
184 | // get the parent of this item (may return NULL if root) |
185 | wxTreeItemId GetParent(const wxTreeItemId& item) const; | |
186 | ||
187 | // for this enumeration function you must pass in a "cookie" parameter | |
188 | // which is opaque for the application but is necessary for the library | |
189 | // to make these functions reentrant (i.e. allow more than one | |
190 | // enumeration on one and the same object simultaneously). Of course, | |
191 | // the "cookie" passed to GetFirstChild() and GetNextChild() should be | |
192 | // the same! | |
193 | ||
194 | // get the first child of this item | |
195 | wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const; | |
196 | // get the next child | |
197 | wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const; | |
978f38c2 VZ |
198 | // get the last child of this item - this method doesn't use cookies |
199 | wxTreeItemId GetLastChild(const wxTreeItemId& item) const; | |
f135ff73 VZ |
200 | |
201 | // get the next sibling of this item | |
202 | wxTreeItemId GetNextSibling(const wxTreeItemId& item) const; | |
203 | // get the previous sibling | |
204 | wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const; | |
205 | ||
206 | // get first visible item | |
207 | wxTreeItemId GetFirstVisibleItem() const; | |
208 | // get the next visible item: item must be visible itself! | |
209 | // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem() | |
210 | wxTreeItemId GetNextVisible(const wxTreeItemId& item) const; | |
211 | // get the previous visible item: item must be visible itself! | |
212 | wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const; | |
213 | ||
1ed01484 JS |
214 | // Only for internal use right now, but should probably be public |
215 | wxTreeItemId GetNext(const wxTreeItemId& item) const; | |
216 | wxTreeItemId GetPrev(const wxTreeItemId& item) const; | |
217 | ||
f135ff73 VZ |
218 | // operations |
219 | // ---------- | |
220 | ||
221 | // add the root node to the tree | |
222 | wxTreeItemId AddRoot(const wxString& text, | |
223 | int image = -1, int selectedImage = -1, | |
224 | wxTreeItemData *data = NULL); | |
225 | ||
226 | // insert a new item in as the first child of the parent | |
227 | wxTreeItemId PrependItem(const wxTreeItemId& parent, | |
228 | const wxString& text, | |
229 | int image = -1, int selectedImage = -1, | |
230 | wxTreeItemData *data = NULL); | |
231 | ||
232 | // insert a new item after a given one | |
233 | wxTreeItemId InsertItem(const wxTreeItemId& parent, | |
234 | const wxTreeItemId& idPrevious, | |
235 | const wxString& text, | |
f2593d0d RR |
236 | int image = -1, int selectedImage = -1, |
237 | wxTreeItemData *data = NULL); | |
238 | ||
2ef31e80 | 239 | // insert a new item before the one with the given index |
f2593d0d | 240 | wxTreeItemId InsertItem(const wxTreeItemId& parent, |
2ef31e80 | 241 | size_t index, |
f2593d0d | 242 | const wxString& text, |
f135ff73 VZ |
243 | int image = -1, int selectedImage = -1, |
244 | wxTreeItemData *data = NULL); | |
245 | ||
246 | // insert a new item in as the last child of the parent | |
247 | wxTreeItemId AppendItem(const wxTreeItemId& parent, | |
248 | const wxString& text, | |
249 | int image = -1, int selectedImage = -1, | |
250 | wxTreeItemData *data = NULL); | |
251 | ||
252 | // delete this item and associated data if any | |
253 | void Delete(const wxTreeItemId& item); | |
372edb9d VZ |
254 | // delete all children (but don't delete the item itself) |
255 | // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events | |
256 | void DeleteChildren(const wxTreeItemId& item); | |
f135ff73 | 257 | // delete all items from the tree |
372edb9d | 258 | // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events |
f135ff73 VZ |
259 | void DeleteAllItems(); |
260 | ||
261 | // expand this item | |
262 | void Expand(const wxTreeItemId& item); | |
f6bcfd97 BP |
263 | // expand this item and all subitems recursively |
264 | void ExpandAll(const wxTreeItemId& item); | |
f135ff73 VZ |
265 | // collapse the item without removing its children |
266 | void Collapse(const wxTreeItemId& item); | |
267 | // collapse the item and remove all children | |
268 | void CollapseAndReset(const wxTreeItemId& item); | |
269 | // toggles the current state | |
270 | void Toggle(const wxTreeItemId& item); | |
271 | ||
272 | // remove the selection from currently selected item (if any) | |
273 | void Unselect(); | |
88ac883a | 274 | void UnselectAll(); |
f135ff73 | 275 | // select this item |
c0de7af4 | 276 | void SelectItem(const wxTreeItemId& item, bool unselect_others=TRUE, bool extended_select=FALSE); |
f135ff73 VZ |
277 | // make sure this item is visible (expanding the parent item and/or |
278 | // scrolling to this item if necessary) | |
279 | void EnsureVisible(const wxTreeItemId& item); | |
280 | // scroll to this item (but don't expand its parent) | |
281 | void ScrollTo(const wxTreeItemId& item); | |
282 | ||
4f22cf8d RR |
283 | // The first function is more portable (because easier to implement |
284 | // on other platforms), but the second one returns some extra info. | |
285 | wxTreeItemId HitTest(const wxPoint& point) | |
286 | { int dummy; return HitTest(point, dummy); } | |
287 | wxTreeItemId HitTest(const wxPoint& point, int& flags); | |
bfc6fde4 | 288 | |
8fb3bfe2 JS |
289 | // get the bounding rectangle of the item (or of its label only) |
290 | bool GetBoundingRect(const wxTreeItemId& item, | |
291 | wxRect& rect, | |
292 | bool textOnly = FALSE) const; | |
293 | ||
e179bd65 | 294 | // Start editing the item label: this (temporarily) replaces the item |
f135ff73 | 295 | // with a one line edit control. The item will be selected if it hadn't |
e179bd65 RR |
296 | // been before. |
297 | void EditLabel( const wxTreeItemId& item ) { Edit( item ); } | |
298 | void Edit( const wxTreeItemId& item ); | |
9dfbf520 | 299 | |
e1ee62bd VZ |
300 | // sorting |
301 | // this function is called to compare 2 items and should return -1, 0 | |
302 | // or +1 if the first item is less than, equal to or greater than the | |
303 | // second one. The base class version performs alphabetic comparaison | |
304 | // of item labels (GetText) | |
305 | virtual int OnCompareItems(const wxTreeItemId& item1, | |
306 | const wxTreeItemId& item2); | |
307 | // sort the children of this item using OnCompareItems | |
f135ff73 | 308 | // |
e1ee62bd VZ |
309 | // NB: this function is not reentrant and not MT-safe (FIXME)! |
310 | void SortChildren(const wxTreeItemId& item); | |
f135ff73 | 311 | |
74b31181 VZ |
312 | // deprecated functions: use Set/GetItemImage directly |
313 | // get the selected item image | |
314 | int GetItemSelectedImage(const wxTreeItemId& item) const | |
315 | { return GetItemImage(item, wxTreeItemIcon_Selected); } | |
316 | // set the selected item image | |
317 | void SetItemSelectedImage(const wxTreeItemId& item, int image) | |
318 | { SetItemImage(item, image, wxTreeItemIcon_Selected); } | |
319 | ||
3dbeaa52 | 320 | // implementation only from now on |
74b31181 | 321 | |
a43a4f9d | 322 | // callbacks |
3db7be80 RR |
323 | void OnPaint( wxPaintEvent &event ); |
324 | void OnSetFocus( wxFocusEvent &event ); | |
325 | void OnKillFocus( wxFocusEvent &event ); | |
f135ff73 | 326 | void OnChar( wxKeyEvent &event ); |
3db7be80 RR |
327 | void OnMouse( wxMouseEvent &event ); |
328 | void OnIdle( wxIdleEvent &event ); | |
f135ff73 | 329 | |
3dbeaa52 | 330 | // implementation helpers |
a43a4f9d VZ |
331 | void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted); |
332 | ||
3dbeaa52 VZ |
333 | void DrawBorder(const wxTreeItemId& item); |
334 | void DrawLine(const wxTreeItemId& item, bool below); | |
9dfbf520 | 335 | |
f135ff73 | 336 | protected: |
91b8de8d | 337 | friend class wxGenericTreeItem; |
e179bd65 RR |
338 | friend class wxTreeRenameTimer; |
339 | friend class wxTreeTextCtrl; | |
91b8de8d | 340 | |
eff869aa RR |
341 | wxFont m_normalFont; |
342 | wxFont m_boldFont; | |
343 | ||
f135ff73 | 344 | wxGenericTreeItem *m_anchor; |
e179bd65 | 345 | wxGenericTreeItem *m_current, *m_key_current, *m_currentEdit; |
f135ff73 | 346 | bool m_hasFocus; |
3db7be80 | 347 | bool m_dirty; |
f135ff73 VZ |
348 | int m_xScroll,m_yScroll; |
349 | unsigned int m_indent; | |
cf724bce | 350 | unsigned int m_spacing; |
f135ff73 VZ |
351 | int m_lineHeight; |
352 | wxPen m_dottedPen; | |
353 | wxBrush *m_hilightBrush; | |
354 | wxImageList *m_imageListNormal, | |
355 | *m_imageListState; | |
3dbeaa52 | 356 | |
bbe0af5b | 357 | int m_dragCount; |
fd9811b1 | 358 | wxPoint m_dragStart; |
3dbeaa52 VZ |
359 | bool m_isDragging; // true between BEGIN/END drag events |
360 | wxGenericTreeItem *m_dropTarget; | |
943d28e4 VZ |
361 | wxCursor m_oldCursor; // cursor is changed while dragging |
362 | wxGenericTreeItem *m_oldSelection; | |
3dbeaa52 | 363 | |
e179bd65 RR |
364 | wxTimer *m_renameTimer; |
365 | bool m_renameAccept; | |
f6bcfd97 | 366 | bool m_lastOnSame; // last click on the same item as prev |
e179bd65 | 367 | wxString m_renameRes; |
f135ff73 VZ |
368 | |
369 | // the common part of all ctors | |
370 | void Init(); | |
371 | ||
372 | // misc helpers | |
373 | wxTreeItemId DoInsertItem(const wxTreeItemId& parent, | |
374 | size_t previous, | |
375 | const wxString& text, | |
376 | int image, int selectedImage, | |
377 | wxTreeItemData *data); | |
378 | ||
1ed01484 | 379 | public: |
f135ff73 | 380 | void AdjustMyScrollbars(); |
1ed01484 | 381 | protected: |
91b8de8d | 382 | int GetLineHeight(wxGenericTreeItem *item) const; |
ef44a621 VZ |
383 | void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y ); |
384 | void PaintItem( wxGenericTreeItem *item, wxDC& dc); | |
f135ff73 VZ |
385 | |
386 | void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ); | |
387 | void CalculatePositions(); | |
91b8de8d | 388 | void CalculateSize( wxGenericTreeItem *item, wxDC &dc ); |
f135ff73 VZ |
389 | |
390 | void RefreshSubtree( wxGenericTreeItem *item ); | |
391 | void RefreshLine( wxGenericTreeItem *item ); | |
9dfbf520 | 392 | |
e179bd65 RR |
393 | void OnRenameTimer(); |
394 | void OnRenameAccept(); | |
43fa96a8 | 395 | |
91b8de8d | 396 | void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const; |
88ac883a VZ |
397 | void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 ); |
398 | bool TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select); | |
399 | bool TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select); | |
400 | void UnselectAllChildren( wxGenericTreeItem *item ); | |
401 | ||
3dbeaa52 VZ |
402 | void DrawDropEffect(wxGenericTreeItem *item); |
403 | ||
a32dd690 | 404 | private: |
f135ff73 | 405 | DECLARE_EVENT_TABLE() |
484523cf | 406 | DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl) |
c801d85f KB |
407 | }; |
408 | ||
233058c7 JS |
409 | #if !defined(__WXMSW__) || defined(__WIN16__) |
410 | /* | |
411 | * wxTreeCtrl has to be a real class or we have problems with | |
412 | * the run-time information. | |
413 | */ | |
414 | ||
415 | class WXDLLEXPORT wxTreeCtrl: public wxGenericTreeCtrl | |
416 | { | |
417 | public: | |
418 | DECLARE_DYNAMIC_CLASS(wxTreeCtrl) | |
419 | ||
420 | wxTreeCtrl() {} | |
421 | ||
422 | wxTreeCtrl(wxWindow *parent, wxWindowID id = -1, | |
423 | const wxPoint& pos = wxDefaultPosition, | |
424 | const wxSize& size = wxDefaultSize, | |
425 | long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, | |
426 | const wxValidator &validator = wxDefaultValidator, | |
427 | const wxString& name = wxTreeCtrlNameStr) | |
428 | { | |
429 | Create(parent, id, pos, size, style, validator, name); | |
430 | } | |
431 | ||
432 | }; | |
433 | ||
434 | #endif | |
435 | ||
f135ff73 VZ |
436 | #endif // _GENERIC_TREECTRL_H_ |
437 |