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