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