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