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