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