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