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