]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/treectrl.h
BC++/16-bit support now working, but without resource system
[wxWidgets.git] / include / wx / generic / treectrl.h
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_
14
15 #ifdef __GNUG__
16 #pragma interface "treectrl.h"
17 #endif
18
19 #ifdef __WXMSW__
20 WXDLLEXPORT_DATA(extern const char*) wxTreeCtrlNameStr;
21 #else
22 #define wxTreeCtrlNameStr "wxTreeCtrl"
23 #endif
24
25 #include "wx/defs.h"
26 #include "wx/string.h"
27 #include "wx/object.h"
28 #include "wx/event.h"
29 #include "wx/scrolwin.h"
30 #include "wx/textctrl.h"
31
32 // -----------------------------------------------------------------------------
33 // constants
34 // -----------------------------------------------------------------------------
35
36 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
37 // where exactly the specified point is situated:
38 static const int wxTREE_HITTEST_NOWHERE = 0x0004;
39 // on the bitmap associated with an item.
40 static const int wxTREE_HITTEST_ONITEMICON = 0x0020;
41 // on the label (string) associated with an item.
42 static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080;
43 // anywhere on the item
44 static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
45 wxTREE_HITTEST_ONITEMLABEL;
46
47 // -----------------------------------------------------------------------------
48 // forward declaration
49 // -----------------------------------------------------------------------------
50
51 class wxImageList;
52 class wxGenericTreeItem;
53
54 class wxTreeItemData;
55
56 // -----------------------------------------------------------------------------
57 // wxTreeItemId - unique identifier of a tree element
58 // -----------------------------------------------------------------------------
59
60 class WXDLLEXPORT wxTreeItemId
61 {
62 friend class wxTreeCtrl;
63 friend class wxTreeEvent;
64 public:
65 // ctors
66 // 0 is invalid value for HTREEITEM
67 wxTreeItemId() { m_pItem = 0; }
68
69 // default copy ctor/assignment operator are ok for us
70
71 // accessors
72 // is this a valid tree item?
73 bool IsOk() const { return m_pItem != 0; }
74
75 // deprecated: only for compatibility
76 wxTreeItemId(long itemId) { m_pItem = (wxGenericTreeItem *)itemId; }
77 operator long() const { return (long)m_pItem; }
78
79 //protected: // not for gcc
80 // for wxTreeCtrl usage only
81 wxTreeItemId(wxGenericTreeItem *pItem) { m_pItem = pItem; }
82
83 wxGenericTreeItem *m_pItem;
84 };
85
86 // ----------------------------------------------------------------------------
87 // wxTreeItemData is some (arbitrary) user class associated with some item.
88 //
89 // Because the objects of this class are deleted by the tree, they should
90 // always be allocated on the heap!
91 // ----------------------------------------------------------------------------
92 class WXDLLEXPORT wxTreeItemData: public wxClientData
93 {
94 friend class wxTreeCtrl;
95 public:
96 // creation/destruction
97 // --------------------
98 // default ctor
99 wxTreeItemData() { }
100
101 // default copy ctor/assignment operator are ok
102
103 // accessor: get the item associated with us
104 const wxTreeItemId& GetId() const { return m_pItem; }
105 void SetId(const wxTreeItemId& id) { m_pItem = id; }
106
107 protected:
108 wxTreeItemId m_pItem;
109 };
110
111 // -----------------------------------------------------------------------------
112 // wxTreeEvent - the event generated by the tree control
113 // -----------------------------------------------------------------------------
114 class WXDLLEXPORT wxTreeEvent : public wxNotifyEvent
115 {
116 friend class wxTreeCtrl;
117 public:
118 wxTreeEvent(wxEventType commandType = wxEVT_NULL, int id = 0);
119
120 // accessors
121 // get the item on which the operation was performed or the newly
122 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
123 wxTreeItemId GetItem() const { return m_item; }
124
125 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
126 // selected item
127 wxTreeItemId GetOldItem() const { return m_itemOld; }
128
129 // the point where the mouse was when the drag operation started (for
130 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
131 wxPoint GetPoint() const { return m_pointDrag; }
132
133 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
134 int GetCode() const { return m_code; }
135
136 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
137 // call this to forbid the change in item status
138 void Veto() { m_code = TRUE; }
139
140 // for implementation usage only
141 bool WasVetoed() const { return m_code; }
142
143 private:
144 // @@ we could save some space by using union here
145 int m_code;
146 wxTreeItemId m_item,
147 m_itemOld;
148 wxPoint m_pointDrag;
149
150 DECLARE_DYNAMIC_CLASS(wxTreeEvent)
151 };
152
153 typedef void (wxEvtHandler::*wxTreeEventFunction)(wxTreeEvent&);
154
155 // ----------------------------------------------------------------------------
156 // macros for handling tree control events
157 // ----------------------------------------------------------------------------
158
159 // GetItem() returns the item being dragged, GetPoint() the mouse coords
160 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
161 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
162
163 // GetItem() returns the itme whose label is being edited
164 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
165 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
166
167 // provide/update information about GetItem() item
168 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
169 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
170
171 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
172 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
173 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
174 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
175 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
176
177 // GetOldItem() is the item which had the selection previously, GetItem() is
178 // the item which acquires selection
179 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
180 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
181
182 // GetCode() returns the key code
183 // NB: this is the only message for which GetItem() is invalid (you may get the
184 // item from GetSelection())
185 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
186
187 // GetItem() returns the item being deleted, the associated data (if any) will
188 // be deleted just after the return of this event handler (if any)
189 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
190
191 // GetItem() returns the item that was activated (double click, enter, space)
192 #define EVT_TREE_ITEM_ACTIVATED(id, fn) { wxEVT_COMMAND_TREE_ITEM_ACTIVATED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
193
194 // -----------------------------------------------------------------------------
195 // wxTreeCtrl - the tree control
196 // -----------------------------------------------------------------------------
197
198 class WXDLLEXPORT wxTreeCtrl : public wxScrolledWindow
199 {
200 public:
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,
209 const wxValidator &validator = wxDefaultValidator,
210 const wxString& name = wxTreeCtrlNameStr)
211 {
212 Create(parent, id, pos, size, style, validator, name);
213 }
214
215 virtual ~wxTreeCtrl();
216
217 bool Create(wxWindow *parent, wxWindowID id = -1,
218 const wxPoint& pos = wxDefaultPosition,
219 const wxSize& size = wxDefaultSize,
220 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
221 const wxValidator &validator = wxDefaultValidator,
222 const wxString& name = wxTreeCtrlNameStr);
223
224 // accessors
225 // ---------
226
227 // get the total number of items in the control
228 size_t GetCount() const;
229
230 // indent is the number of pixels the children are indented relative to
231 // the parents position. SetIndent() also redraws the control
232 // immediately.
233 unsigned int GetIndent() const { return m_indent; }
234 void SetIndent(unsigned int indent);
235
236 // image list: these functions allow to associate an image list with
237 // the control and retrieve it. Note that the control does _not_ delete
238 // the associated image list when it's deleted in order to allow image
239 // lists to be shared between different controls.
240 //
241 // The normal image list is for the icons which correspond to the
242 // normal tree item state (whether it is selected or not).
243 // Additionally, the application might choose to show a state icon
244 // which corresponds to an app-defined item state (for example,
245 // checked/unchecked) which are taken from the state image list.
246 wxImageList *GetImageList() const;
247 wxImageList *GetStateImageList() const;
248
249 void SetImageList(wxImageList *imageList);
250 void SetStateImageList(wxImageList *imageList);
251
252 // Functions to work with tree ctrl items.
253
254 // accessors
255 // ---------
256
257 // retrieve items label
258 wxString GetItemText(const wxTreeItemId& item) const;
259 // get the normal item image
260 int GetItemImage(const wxTreeItemId& item) const;
261 // get the selected item image
262 int GetItemSelectedImage(const wxTreeItemId& item) const;
263 // get the data associated with the item
264 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
265
266 // modifiers
267 // ---------
268
269 // set items label
270 void SetItemText(const wxTreeItemId& item, const wxString& text);
271 // set the normal item image
272 void SetItemImage(const wxTreeItemId& item, int image);
273 // set the selected item image
274 void SetItemSelectedImage(const wxTreeItemId& item, int image);
275 // associate some data with the item
276 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
277
278 // force appearance of [+] button near the item. This is useful to
279 // allow the user to expand the items which don't have any children now
280 // - but instead add them only when needed, thus minimizing memory
281 // usage and loading time.
282 void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE);
283
284 // the item will be shown in bold
285 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
286
287 // item status inquiries
288 // ---------------------
289
290 // is the item visible (it might be outside the view or not expanded)?
291 bool IsVisible(const wxTreeItemId& item) const;
292 // does the item has any children?
293 bool HasChildren(const wxTreeItemId& item) const
294 { return ItemHasChildren(item); }
295 bool ItemHasChildren(const wxTreeItemId& item) const;
296 // is the item expanded (only makes sense if HasChildren())?
297 bool IsExpanded(const wxTreeItemId& item) const;
298 // is this item currently selected (the same as has focus)?
299 bool IsSelected(const wxTreeItemId& item) const;
300 // is item text in bold font?
301 bool IsBold(const wxTreeItemId& item) const;
302
303 // number of children
304 // ------------------
305
306 // if 'recursively' is FALSE, only immediate children count, otherwise
307 // the returned number is the number of all items in this branch
308 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE);
309
310 // navigation
311 // ----------
312
313 // wxTreeItemId.IsOk() will return FALSE if there is no such item
314
315 // get the root tree item
316 wxTreeItemId GetRootItem() const { return m_anchor; }
317
318 // get the item currently selected (may return NULL if no selection)
319 wxTreeItemId GetSelection() const { return m_current; }
320
321 // get the parent of this item (may return NULL if root)
322 wxTreeItemId GetParent(const wxTreeItemId& item) const;
323
324 // for this enumeration function you must pass in a "cookie" parameter
325 // which is opaque for the application but is necessary for the library
326 // to make these functions reentrant (i.e. allow more than one
327 // enumeration on one and the same object simultaneously). Of course,
328 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
329 // the same!
330
331 // get the first child of this item
332 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
333 // get the next child
334 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
335
336 // get the next sibling of this item
337 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
338 // get the previous sibling
339 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
340
341 // get first visible item
342 wxTreeItemId GetFirstVisibleItem() const;
343 // get the next visible item: item must be visible itself!
344 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
345 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
346 // get the previous visible item: item must be visible itself!
347 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
348
349 // operations
350 // ----------
351
352 // add the root node to the tree
353 wxTreeItemId AddRoot(const wxString& text,
354 int image = -1, int selectedImage = -1,
355 wxTreeItemData *data = NULL);
356
357 // insert a new item in as the first child of the parent
358 wxTreeItemId PrependItem(const wxTreeItemId& parent,
359 const wxString& text,
360 int image = -1, int selectedImage = -1,
361 wxTreeItemData *data = NULL);
362
363 // insert a new item after a given one
364 wxTreeItemId InsertItem(const wxTreeItemId& parent,
365 const wxTreeItemId& idPrevious,
366 const wxString& text,
367 int image = -1, int selectedImage = -1,
368 wxTreeItemData *data = NULL);
369
370 // insert a new item in as the last child of the parent
371 wxTreeItemId AppendItem(const wxTreeItemId& parent,
372 const wxString& text,
373 int image = -1, int selectedImage = -1,
374 wxTreeItemData *data = NULL);
375
376 // delete this item and associated data if any
377 void Delete(const wxTreeItemId& item);
378 // delete all children (but don't delete the item itself)
379 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
380 void DeleteChildren(const wxTreeItemId& item);
381 // delete all items from the tree
382 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
383 void DeleteAllItems();
384
385 // expand this item
386 void Expand(const wxTreeItemId& item);
387 // collapse the item without removing its children
388 void Collapse(const wxTreeItemId& item);
389 // collapse the item and remove all children
390 void CollapseAndReset(const wxTreeItemId& item);
391 // toggles the current state
392 void Toggle(const wxTreeItemId& item);
393
394 // remove the selection from currently selected item (if any)
395 void Unselect();
396 // select this item
397 void SelectItem(const wxTreeItemId& item);
398 // make sure this item is visible (expanding the parent item and/or
399 // scrolling to this item if necessary)
400 void EnsureVisible(const wxTreeItemId& item);
401 // scroll to this item (but don't expand its parent)
402 void ScrollTo(const wxTreeItemId& item);
403
404 // The first function is more portable (because easier to implement
405 // on other platforms), but the second one returns some extra info.
406 wxTreeItemId HitTest(const wxPoint& point)
407 { int dummy; return HitTest(point, dummy); }
408 wxTreeItemId HitTest(const wxPoint& point, int& flags);
409
410 // start editing the item label: this (temporarily) replaces the item
411 // with a one line edit control. The item will be selected if it hadn't
412 // been before. textCtrlClass parameter allows you to create an edit
413 // control of arbitrary user-defined class deriving from wxTextCtrl.
414 wxTextCtrl* EditLabel(const wxTreeItemId& item,
415 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
416 // returns the same pointer as StartEdit() if the item is being edited,
417 // NULL otherwise (it's assumed that no more than one item may be
418 // edited simultaneously)
419 wxTextCtrl* GetEditControl() const;
420 // end editing and accept or discard the changes to item label
421 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
422
423 // sorting
424 // this function is called to compare 2 items and should return -1, 0
425 // or +1 if the first item is less than, equal to or greater than the
426 // second one. The base class version performs alphabetic comparaison
427 // of item labels (GetText)
428 virtual int OnCompareItems(const wxTreeItemId& item1,
429 const wxTreeItemId& item2);
430 // sort the children of this item using OnCompareItems
431 //
432 // NB: this function is not reentrant and not MT-safe (FIXME)!
433 void SortChildren(const wxTreeItemId& item);
434
435 // callbacks
436 void OnPaint( wxPaintEvent &event );
437 void OnSetFocus( wxFocusEvent &event );
438 void OnKillFocus( wxFocusEvent &event );
439 void OnChar( wxKeyEvent &event );
440 void OnMouse( wxMouseEvent &event );
441 void OnIdle( wxIdleEvent &event );
442
443 // implementation
444 void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted);
445
446 protected:
447 wxGenericTreeItem *m_anchor;
448 wxGenericTreeItem *m_current;
449 bool m_hasFocus;
450 bool m_dirty;
451 int m_xScroll,m_yScroll;
452 unsigned int m_indent;
453 int m_lineHeight;
454 wxPen m_dottedPen;
455 wxBrush *m_hilightBrush;
456 wxImageList *m_imageListNormal,
457 *m_imageListState;
458
459 // the common part of all ctors
460 void Init();
461
462 // misc helpers
463 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
464 size_t previous,
465 const wxString& text,
466 int image, int selectedImage,
467 wxTreeItemData *data);
468
469 void AdjustMyScrollbars();
470 void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y );
471 void PaintItem( wxGenericTreeItem *item, wxDC& dc);
472
473 void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y );
474 void CalculatePositions();
475
476 void RefreshSubtree( wxGenericTreeItem *item );
477 void RefreshLine( wxGenericTreeItem *item );
478
479 private:
480 DECLARE_EVENT_TABLE()
481 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
482 };
483
484 #endif // _GENERIC_TREECTRL_H_
485