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