1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTreeCtrl class
4 // Author: Denis Pershin
8 // Copyright: (c) Denis Pershin
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
16 #pragma interface "treectrl.h"
19 #include "wx/textctrl.h"
20 #include "wx/control.h"
22 #include "wx/imaglist.h"
26 // the type for "untyped" data
27 typedef long wxDataType
;
31 struct wxTreeViewItem
;
33 // a callback function used for sorting tree items, it should return -1 if the
34 // first item precedes the second, +1 if the second precedes the first or 0 if
37 typedef int (*wxTreeItemCmpFunc
)(wxTreeItemData
*item1
, wxTreeItemData
*item2
);
39 // ----------------------------------------------------------------------------
41 // ----------------------------------------------------------------------------
43 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
44 // where exactly the specified point is situated:
45 // above the client area.
46 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
47 // below the client area.
48 static const int wxTREE_HITTEST_BELOW
= 0x0002;
49 // in the client area but below the last item.
50 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
51 // on the button associated with an item.
52 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
53 // on the bitmap associated with an item.
54 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
55 // in the indentation associated with an item.
56 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
57 // on the label (string) associated with an item.
58 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
59 // in the area to the right of an item.
60 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
61 // on the state icon for a tree view item that is in a user-defined state.
62 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
63 // to the right of the client area.
64 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
65 // to the left of the client area.
66 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
67 // anywhere on the item
68 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
69 wxTREE_HITTEST_ONITEMLABEL
|
70 wxTREE_HITTEST_ONITEMSTATEICON
;
72 // NB: all the following flags are for compatbility only and will be removed in
75 // flags for deprecated `Expand(int action)'
79 wxTREE_EXPAND_COLLAPSE
,
80 wxTREE_EXPAND_COLLAPSE_RESET
,
84 // flags for deprecated InsertItem() variant
85 #define wxTREE_INSERT_FIRST 0xFFFF0001
86 #define wxTREE_INSERT_LAST 0xFFFF0002
88 // ----------------------------------------------------------------------------
89 // wxTreeItemId identifies an element of the tree. In this implementation, it's
90 // just a trivial wrapper around GTK GtkTreeItem *. It's opaque for the
92 // ----------------------------------------------------------------------------
93 class WXDLLEXPORT wxTreeItemId
{
96 wxTreeItemId() { m_itemId
= NULL
; }
98 // default copy ctor/assignment operator are ok for us
101 // is this a valid tree item?
102 bool IsOk() const { return m_itemId
!= NULL
; }
104 // conversion to/from either real (system-dependent) tree item id or
105 // to "long" which used to be the type for tree item ids in previous
106 // versions of wxWindows
108 // for wxTreeCtrl usage only
109 wxTreeItemId(GtkTreeItem
*itemId
) { m_itemId
= itemId
; }
110 operator GtkTreeItem
*() const { return m_itemId
; }
111 void operator =(GtkTreeItem
*item
) { m_itemId
= item
; }
114 GtkTreeItem
*m_itemId
;
117 // ----------------------------------------------------------------------------
118 // wxTreeItemData is some (arbitrary) user class associated with some item. The
119 // main advantage of having this class (compared to old untyped interface) is
120 // that wxTreeItemData's are destroyed automatically by the tree and, as this
121 // class has virtual dtor, it means that the memory will be automatically
122 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
123 // the size of this class is critical: in any real application, each tree leaf
124 // will have wxTreeItemData associated with it and number of leaves may be
127 // Because the objects of this class are deleted by the tree, they should
128 // always be allocated on the heap!
129 // ----------------------------------------------------------------------------
130 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
{
132 // default ctor/copy ctor/assignment operator are ok
134 // dtor is virtual and all the items are deleted by the tree control when
135 // it's deleted, so you normally don't have to care about freeing memory
136 // allocated in your wxTreeItemData-derived class
137 virtual ~wxTreeItemData() { }
139 // accessors: set/get the item associated with this node
140 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
141 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
144 class WXDLLEXPORT wxTreeCtrl
: public wxControl
{
148 wxTreeCtrl() { Init(); }
150 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= -1,
151 const wxPoint
& pos
= wxDefaultPosition
,
152 const wxSize
& size
= wxDefaultSize
,
153 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
154 const wxValidator
& validator
= wxDefaultValidator
,
155 const wxString
& name
= "wxTreeCtrl") {
156 Create(parent
, id
, pos
, size
, style
, validator
, name
);
159 virtual ~wxTreeCtrl();
161 bool Create(wxWindow
*parent
, wxWindowID id
= -1,
162 const wxPoint
& pos
= wxDefaultPosition
,
163 const wxSize
& size
= wxDefaultSize
,
164 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
165 const wxValidator
& validator
= wxDefaultValidator
,
166 const wxString
& name
= "wxTreeCtrl");
171 // get the total number of items in the control
172 size_t GetCount() const;
174 // indent is the number of pixels the children are indented relative to
175 // the parents position. SetIndent() also redraws the control
177 unsigned int GetIndent() const;
178 void SetIndent(unsigned int indent
);
180 // image list: these functions allow to associate an image list with
181 // the control and retrieve it. Note that the control does _not_ delete
182 // the associated image list when it's deleted in order to allow image
183 // lists to be shared between different controls.
185 // The normal image list is for the icons which correspond to the
186 // normal tree item state (whether it is selected or not).
187 // Additionally, the application might choose to show a state icon
188 // which corresponds to an app-defined item state (for example,
189 // checked/unchecked) which are taken from the state image list.
190 wxImageList
*GetImageList() const;
191 wxImageList
*GetStateImageList() const;
193 void SetImageList(wxImageList
*imageList
);
194 void SetStateImageList(wxImageList
*imageList
);
196 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
197 // member functions of wxTreeItem because they must know the tree the item
198 // belongs to for Windows implementation and storing the pointer to
199 // wxTreeCtrl in each wxTreeItem is just too much waste.
204 // retrieve items label
205 wxString
GetItemText(const wxTreeItemId
& item
) const;
206 // get the normal item image
207 int GetItemImage(const wxTreeItemId
& item
) const;
208 // get the selected item image
209 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
210 // get the data associated with the item
211 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
217 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
218 // set the normal item image
219 void SetItemImage(const wxTreeItemId
& item
, int image
);
220 // set the selected item image
221 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
222 // associate some data with the item
223 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
225 // item status inquiries
226 // ---------------------
228 // is the item visible (it might be outside the view or not expanded)?
229 bool IsVisible(const wxTreeItemId
& item
) const;
230 // does the item has any children?
231 bool ItemHasChildren(const wxTreeItemId
& item
) const;
232 // is the item expanded (only makes sense if HasChildren())?
233 bool IsExpanded(const wxTreeItemId
& item
) const;
234 // is this item currently selected (the same as has focus)?
235 bool IsSelected(const wxTreeItemId
& item
) const;
237 // number of children
238 // ------------------
240 // if 'recursively' is FALSE, only immediate children count, otherwise
241 // the returned number is the number of all items in this branch
242 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= TRUE
);
247 // wxTreeItemId.IsOk() will return FALSE if there is no such item
249 // get the root tree item
250 wxTreeItemId
GetRootItem() const;
252 // get the item currently selected (may return NULL if no selection)
253 wxTreeItemId
GetSelection() const;
255 // get the parent of this item (may return NULL if root)
256 wxTreeItemId
GetParent(const wxTreeItemId
& item
) const;
258 // for this enumeration function you must pass in a "cookie" parameter
259 // which is opaque for the application but is necessary for the library
260 // to make these functions reentrant (i.e. allow more than one
261 // enumeration on one and the same object simultaneously). Of course,
262 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
265 // get the first child of this item
266 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
267 // get the next child
268 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
270 // get the next sibling of this item
271 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
272 // get the previous sibling
273 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
275 // get first visible item
276 wxTreeItemId
GetFirstVisibleItem() const;
277 // get the next visible item: item must be visible itself!
278 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
279 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
280 // get the previous visible item: item must be visible itself!
281 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
286 // add the root node to the tree
287 wxTreeItemId
AddRoot(const wxString
& text
,
288 int image
= -1, int selectedImage
= -1,
289 wxTreeItemData
*data
= NULL
);
291 // insert a new item in as the first child of the parent
292 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
293 const wxString
& text
,
294 int image
= -1, int selectedImage
= -1,
295 wxTreeItemData
*data
= NULL
);
297 // insert a new item after a given one
298 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
299 const wxTreeItemId
& idPrevious
,
300 const wxString
& text
,
301 int image
= -1, int selectedImage
= -1,
302 wxTreeItemData
*data
= NULL
);
304 // insert a new item in as the last child of the parent
305 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
306 const wxString
& text
,
307 int image
= -1, int selectedImage
= -1,
308 wxTreeItemData
*data
= NULL
);
310 // delete this item and associated data if any
311 void Delete(const wxTreeItemId
& item
);
312 // delete all items from the tree
313 void DeleteAllItems();
316 void Expand(const wxTreeItemId
& item
);
317 // collapse the item without removing its children
318 void Collapse(const wxTreeItemId
& item
);
319 // collapse the item and remove all children
320 void CollapseAndReset(const wxTreeItemId
& item
);
321 // toggles the current state
322 void Toggle(const wxTreeItemId
& item
);
324 // remove the selection from currently selected item (if any)
327 void SelectItem(const wxTreeItemId
& item
);
328 // make sure this item is visible (expanding the parent item and/or
329 // scrolling to this item if necessary)
330 void EnsureVisible(const wxTreeItemId
& item
);
331 // scroll to this item (but don't expand its parent)
332 void ScrollTo(const wxTreeItemId
& item
);
334 // start editing the item label: this (temporarily) replaces the item
335 // with a one line edit control. The item will be selected if it hadn't
336 // been before. textCtrlClass parameter allows you to create an edit
337 // control of arbitrary user-defined class deriving from wxTextCtrl.
338 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
339 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
340 // returns the same pointer as StartEdit() if the item is being edited,
341 // NULL otherwise (it's assumed that no more than one item may be
342 // edited simultaneously)
343 wxTextCtrl
* GetEditControl() const;
344 // end editing and accept or discard the changes to item label
345 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= FALSE
);
347 // sort the children of this item using the specified callback function
348 // (it should return -1, 0 or +1 as usual), if it's not specified
349 // alphabetical comparaison is performed.
351 // NB: this function is not reentrant!
352 void SortChildren(const wxTreeItemId
& item
,
353 wxTreeItemCmpFunc
*cmpFunction
= NULL
);
358 // these methods are deprecated and will be removed in future versions of
359 // wxWindows, they're here for compatibility only, don't use them in new
360 // code (the comments indicate why these methods are now useless and how to
363 // use Expand, Collapse, CollapseAndReset or Toggle
364 void ExpandItem(const wxTreeItemId
& item
, int action
);
366 // use AddRoot, PrependItem or AppendItem
367 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
368 const wxString
& text
,
369 int image
= -1, int selImage
= -1,
370 long insertAfter
= wxTREE_INSERT_LAST
);
372 // use Set/GetImageList and Set/GetStateImageList
373 wxImageList
*GetImageList(int) const
374 { return GetImageList(); }
375 void SetImageList(wxImageList
*imageList
, int)
376 { SetImageList(imageList
); }
378 void SendExpanding(const wxTreeItemId
& item
);
379 void SendExpanded(const wxTreeItemId
& item
);
380 void SendCollapsing(const wxTreeItemId
& item
);
381 void SendCollapsed(const wxTreeItemId
& item
);
382 void SendSelChanging(const wxTreeItemId
& item
);
383 void SendSelChanged(const wxTreeItemId
& item
);
385 wxTreeItemId m_editItem
;
387 GtkTreeItem
*m_anchor
;
388 wxTextCtrl
* m_textCtrl
;
389 wxImageList
* m_imageListNormal
;
390 wxImageList
* m_imageListState
;
394 void SendMessage(wxEventType command
, const wxTreeItemId
& item
);
395 // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
397 // the common part of all ctors
399 // insert a new item in as the last child of the parent
400 wxTreeItemId
p_InsertItem(GtkTreeItem
*p
,
401 const wxString
& text
,
402 int image
, int selectedImage
,
403 wxTreeItemData
*data
);
406 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)
409 // ----------------------------------------------------------------------------
410 // wxTreeEvent is a special class for all events associated with tree controls
412 // NB: note that not all accessors make sense for all events, see the event
413 // descriptions below
414 // ----------------------------------------------------------------------------
415 class WXDLLEXPORT wxTreeEvent
: public wxCommandEvent
419 wxTreeEvent(wxEventType commandType
= wxEVT_NULL
, int id
= 0);
422 // get the item on which the operation was performed or the newly
423 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
424 wxTreeItemId
GetItem() const { return m_item
; }
426 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
428 wxTreeItemId
GetOldItem() const { return m_itemOld
; }
430 // the point where the mouse was when the drag operation started (for
431 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
432 wxPoint
GetPoint() const { return m_pointDrag
; }
434 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
435 int GetCode() const { return m_code
; }
437 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
438 // call this to forbid the change in item status
439 void Veto() { m_code
= TRUE
; }
442 // @@ we could save some space by using union here
448 DECLARE_DYNAMIC_CLASS(wxTreeEvent
)
451 typedef void (wxEvtHandler::*wxTreeEventFunction
)(wxTreeEvent
&);
453 // ----------------------------------------------------------------------------
454 // macros for handling tree control events
455 // ----------------------------------------------------------------------------
457 // GetItem() returns the item being dragged, GetPoint() the mouse coords
458 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
459 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
461 // GetItem() returns the itme whose label is being edited
462 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
463 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
465 // provide/update information about GetItem() item
466 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
467 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
469 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
470 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
471 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
472 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
473 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
475 // GetOldItem() is the item which had the selection previously, GetItem() is
476 // the item which acquires selection
477 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
478 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
480 // GetCode() returns the key code
481 // NB: this is the only message for which GetItem() is invalid (you may get the
482 // item from GetSelection())
483 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
485 // GetItem() returns the item being deleted, the associated data (if any) will
486 // be deleted just after the return of this event handler (if any)
487 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },