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