1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gtk/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: Denis Pershin
8 // Copyright: (c) Denis Pershin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
15 #include "wx/textctrl.h"
16 #include "wx/imaglist.h"
20 // the type for "untyped" data
21 typedef long wxDataType
;
24 class WXDLLIMPEXP_CORE wxImageList
;
25 struct wxTreeViewItem
;
27 // a callback function used for sorting tree items, it should return -1 if the
28 // first item precedes the second, +1 if the second precedes the first or 0 if
30 class WXDLLIMPEXP_CORE wxTreeItemData
;
31 typedef int (*wxTreeItemCmpFunc
)(wxTreeItemData
*item1
, wxTreeItemData
*item2
);
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
38 // where exactly the specified point is situated:
39 // above the client area.
40 static const int wxTREE_HITTEST_ABOVE
= 0x0001;
41 // below the client area.
42 static const int wxTREE_HITTEST_BELOW
= 0x0002;
43 // in the client area but below the last item.
44 static const int wxTREE_HITTEST_NOWHERE
= 0x0004;
45 // on the button associated with an item.
46 static const int wxTREE_HITTEST_ONITEMBUTTON
= 0x0010;
47 // on the bitmap associated with an item.
48 static const int wxTREE_HITTEST_ONITEMICON
= 0x0020;
49 // in the indentation associated with an item.
50 static const int wxTREE_HITTEST_ONITEMINDENT
= 0x0040;
51 // on the label (string) associated with an item.
52 static const int wxTREE_HITTEST_ONITEMLABEL
= 0x0080;
53 // in the area to the right of an item.
54 static const int wxTREE_HITTEST_ONITEMRIGHT
= 0x0100;
55 // on the state icon for a tree view item that is in a user-defined state.
56 static const int wxTREE_HITTEST_ONITEMSTATEICON
= 0x0200;
57 // to the right of the client area.
58 static const int wxTREE_HITTEST_TOLEFT
= 0x0400;
59 // to the left of the client area.
60 static const int wxTREE_HITTEST_TORIGHT
= 0x0800;
61 // anywhere on the item
62 static const int wxTREE_HITTEST_ONITEM
= wxTREE_HITTEST_ONITEMICON
|
63 wxTREE_HITTEST_ONITEMLABEL
|
64 wxTREE_HITTEST_ONITEMSTATEICON
;
66 #if WXWIN_COMPATIBILITY_2_6
67 // NB: all the following flags are for compatbility only and will be removed in
69 // flags for deprecated InsertItem() variant
70 #define wxTREE_INSERT_FIRST 0xFFFF0001
71 #define wxTREE_INSERT_LAST 0xFFFF0002
74 // ----------------------------------------------------------------------------
75 // wxTreeItemId identifies an element of the tree. In this implementation, it's
76 // just a trivial wrapper around GTK GtkTreeItem *. It's opaque for the
78 // ----------------------------------------------------------------------------
79 class WXDLLEXPORT wxTreeItemId
{
82 wxTreeItemId() { m_itemId
= NULL
; }
84 // default copy ctor/assignment operator are ok for us
87 // is this a valid tree item?
88 bool IsOk() const { return m_itemId
!= NULL
; }
90 // conversion to/from either real (system-dependent) tree item id or
91 // to "long" which used to be the type for tree item ids in previous
92 // versions of wxWidgets
94 // for wxTreeCtrl usage only
95 wxTreeItemId(GtkTreeItem
*itemId
) { m_itemId
= itemId
; }
96 operator GtkTreeItem
*() const { return m_itemId
; }
97 void operator =(GtkTreeItem
*item
) { m_itemId
= item
; }
100 GtkTreeItem
*m_itemId
;
103 // ----------------------------------------------------------------------------
104 // wxTreeItemData is some (arbitrary) user class associated with some item. The
105 // main advantage of having this class (compared to old untyped interface) is
106 // that wxTreeItemData's are destroyed automatically by the tree and, as this
107 // class has virtual dtor, it means that the memory will be automatically
108 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
109 // the size of this class is critical: in any real application, each tree leaf
110 // will have wxTreeItemData associated with it and number of leaves may be
113 // Because the objects of this class are deleted by the tree, they should
114 // always be allocated on the heap!
115 // ----------------------------------------------------------------------------
116 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
{
118 // default ctor/copy ctor/assignment operator are ok
120 // dtor is virtual and all the items are deleted by the tree control when
121 // it's deleted, so you normally don't have to care about freeing memory
122 // allocated in your wxTreeItemData-derived class
123 virtual ~wxTreeItemData() { }
125 // accessors: set/get the item associated with this node
126 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
127 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
130 class WXDLLEXPORT wxTreeCtrl
: public wxControl
135 wxTreeCtrl() { Init(); }
137 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
138 const wxPoint
& pos
= wxDefaultPosition
,
139 const wxSize
& size
= wxDefaultSize
,
140 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
141 const wxValidator
& validator
= wxDefaultValidator
,
142 const wxString
& name
= "wxTreeCtrl")
144 Create(parent
, id
, pos
, size
, style
, validator
, name
);
147 virtual ~wxTreeCtrl();
149 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
150 const wxPoint
& pos
= wxDefaultPosition
,
151 const wxSize
& size
= wxDefaultSize
,
152 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
153 const wxValidator
& validator
= wxDefaultValidator
,
154 const wxString
& name
= "wxTreeCtrl");
159 // get the total number of items in the control
160 virtual unsigned int GetCount() const;
162 // indent is the number of pixels the children are indented relative to
163 // the parents position. SetIndent() also redraws the control
165 unsigned int GetIndent() const;
166 void SetIndent(unsigned int indent
);
168 // image list: these functions allow to associate an image list with
169 // the control and retrieve it. Note that the control does _not_ delete
170 // the associated image list when it's deleted in order to allow image
171 // lists to be shared between different controls.
173 // The normal image list is for the icons which correspond to the
174 // normal tree item state (whether it is selected or not).
175 // Additionally, the application might choose to show a state icon
176 // which corresponds to an app-defined item state (for example,
177 // checked/unchecked) which are taken from the state image list.
178 wxImageList
*GetImageList() const;
179 wxImageList
*GetStateImageList() const;
181 void SetImageList(wxImageList
*imageList
);
182 void SetStateImageList(wxImageList
*imageList
);
184 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
185 // member functions of wxTreeItem because they must know the tree the item
186 // belongs to for Windows implementation and storing the pointer to
187 // wxTreeCtrl in each wxTreeItem is just too much waste.
192 // retrieve items label
193 wxString
GetItemText(const wxTreeItemId
& item
) const;
194 // get the normal item image
195 int GetItemImage(const wxTreeItemId
& item
) const;
196 // get the data associated with the item
197 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
203 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
204 // set the normal item image
205 void SetItemImage(const wxTreeItemId
& item
, int image
);
206 // associate some data with the item
207 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
209 // item status inquiries
210 // ---------------------
212 // is the item visible (it might be outside the view or not expanded)?
213 bool IsVisible(const wxTreeItemId
& item
) const;
214 // does the item has any children?
215 bool ItemHasChildren(const wxTreeItemId
& item
) const;
216 // is the item expanded (only makes sense if HasChildren())?
217 bool IsExpanded(const wxTreeItemId
& item
) const;
218 // is this item currently selected (the same as has focus)?
219 bool IsSelected(const wxTreeItemId
& item
) const;
221 // number of children
222 // ------------------
224 // if 'recursively' is false, only immediate children count, otherwise
225 // the returned number is the number of all items in this branch
226 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= true);
231 // wxTreeItemId.IsOk() will return false if there is no such item
233 // get the root tree item
234 wxTreeItemId
GetRootItem() const;
236 // get the item currently selected (may return NULL if no selection)
237 wxTreeItemId
GetSelection() const;
239 // get the parent of this item (may return NULL if root)
240 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
242 // for this enumeration function you must pass in a "cookie" parameter
243 // which is opaque for the application but is necessary for the library
244 // to make these functions reentrant (i.e. allow more than one
245 // enumeration on one and the same object simultaneously). Of course,
246 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
249 // get the last child of this item - this method doesn't use cookies
250 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
252 // get the next sibling of this item
253 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
254 // get the previous sibling
255 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
257 // get first visible item
258 wxTreeItemId
GetFirstVisibleItem() const;
259 // get the next visible item: item must be visible itself!
260 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
261 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
262 // get the previous visible item: item must be visible itself!
263 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
268 // add the root node to the tree
269 wxTreeItemId
AddRoot(const wxString
& text
,
270 int image
= -1, int selectedImage
= -1,
271 wxTreeItemData
*data
= NULL
);
273 // insert a new item in as the first child of the parent
274 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
275 const wxString
& text
,
276 int image
= -1, int selectedImage
= -1,
277 wxTreeItemData
*data
= NULL
);
279 // insert a new item after a given one
280 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
281 const wxTreeItemId
& idPrevious
,
282 const wxString
& text
,
283 int image
= -1, int selectedImage
= -1,
284 wxTreeItemData
*data
= NULL
);
286 // insert a new item in as the last child of the parent
287 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
288 const wxString
& text
,
289 int image
= -1, int selectedImage
= -1,
290 wxTreeItemData
*data
= NULL
);
292 // delete this item and associated data if any
293 void Delete(const wxTreeItemId
& item
);
294 // delete all items from the tree
295 void DeleteAllItems();
298 void Expand(const wxTreeItemId
& item
);
299 // collapse the item without removing its children
300 void Collapse(const wxTreeItemId
& item
);
301 // collapse the item and remove all children
302 void CollapseAndReset(const wxTreeItemId
& item
);
303 // toggles the current state
304 void Toggle(const wxTreeItemId
& item
);
306 // remove the selection from currently selected item (if any)
309 void SelectItem(const wxTreeItemId
& item
);
310 // make sure this item is visible (expanding the parent item and/or
311 // scrolling to this item if necessary)
312 void EnsureVisible(const wxTreeItemId
& item
);
313 // scroll to this item (but don't expand its parent)
314 void ScrollTo(const wxTreeItemId
& item
);
316 // start editing the item label: this (temporarily) replaces the item
317 // with a one line edit control. The item will be selected if it hadn't
318 // been before. textCtrlClass parameter allows you to create an edit
319 // control of arbitrary user-defined class deriving from wxTextCtrl.
320 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
321 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
322 // returns the same pointer as StartEdit() if the item is being edited,
323 // NULL otherwise (it's assumed that no more than one item may be
324 // edited simultaneously)
325 wxTextCtrl
* GetEditControl() const;
326 // end editing and accept or discard the changes to item label
327 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= false);
329 // sort the children of this item using the specified callback function
330 // (it should return -1, 0 or +1 as usual), if it's not specified
331 // alphabetical comparaison is performed.
333 // NB: this function is not reentrant!
334 void SortChildren(const wxTreeItemId
& item
,
335 wxTreeItemCmpFunc
*cmpFunction
= NULL
);
340 #if WXWIN_COMPATIBILITY_2_6
341 // these methods are deprecated and will be removed in future versions of
342 // wxWidgets, they're here for compatibility only, don't use them in new
343 // code (the comments indicate why these methods are now useless and how to
346 // use Expand, Collapse, CollapseAndReset or Toggle
347 wxDEPRECATED( void ExpandItem(const wxTreeItemId
& item
, int action
) );
350 wxDEPRECATED( void SetImageList(wxImageList
*imageList
, int) )
351 { SetImageList(imageList
); }
353 // use Set/GetItemImage directly
354 wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId
& item
) const );
355 wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId
& item
, int image
) );
357 // use the versions taking wxTreeItemIdValue cookies
358 wxDEPRECATED( wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
,
359 long& cookie
) const );
360 wxDEPRECATED( wxTreeItemId
GetNextChild(const wxTreeItemId
& item
,
361 long& cookie
) const );
362 // use AddRoot, PrependItem or AppendItem
363 wxDEPRECATED( wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
364 const wxString
& text
,
365 int image
= -1, int selImage
= -1,
366 long insertAfter
= wxTREE_INSERT_LAST
) );
368 #endif // WXWIN_COMPATIBILITY_2_6
370 // use Set/GetImageList and Set/GetStateImageList
371 wxImageList
*GetImageList(int) const
372 { return GetImageList(); }
374 void SendExpanding(const wxTreeItemId
& item
);
375 void SendExpanded(const wxTreeItemId
& item
);
376 void SendCollapsing(const wxTreeItemId
& item
);
377 void SendCollapsed(const wxTreeItemId
& item
);
378 void SendSelChanging(const wxTreeItemId
& item
);
379 void SendSelChanged(const wxTreeItemId
& item
);
383 wxTreeItemId m_editItem
;
385 GtkTreeItem
*m_anchor
;
386 wxTextCtrl
* m_textCtrl
;
387 wxImageList
* m_imageListNormal
;
388 wxImageList
* m_imageListState
;
392 void SendMessage(wxEventType command
, const wxTreeItemId
& item
);
393 // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
395 // the common part of all ctors
397 // insert a new item in as the last child of the parent
398 wxTreeItemId
p_InsertItem(GtkTreeItem
*p
,
399 const wxString
& text
,
400 int image
, int selectedImage
,
401 wxTreeItemData
*data
);
404 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)