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