1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gtk1/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 // 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
73 // ----------------------------------------------------------------------------
74 // wxTreeItemId identifies an element of the tree. In this implementation, it's
75 // just a trivial wrapper around GTK GtkTreeItem *. It's opaque for the
77 // ----------------------------------------------------------------------------
78 class WXDLLEXPORT wxTreeItemId
{
81 wxTreeItemId() { m_itemId
= NULL
; }
83 // default copy ctor/assignment operator are ok for us
86 // is this a valid tree item?
87 bool IsOk() const { return m_itemId
!= NULL
; }
89 // conversion to/from either real (system-dependent) tree item id or
90 // to "long" which used to be the type for tree item ids in previous
91 // versions of wxWidgets
93 // for wxTreeCtrl usage only
94 wxTreeItemId(GtkTreeItem
*itemId
) { m_itemId
= itemId
; }
95 operator GtkTreeItem
*() const { return m_itemId
; }
96 void operator =(GtkTreeItem
*item
) { m_itemId
= item
; }
99 GtkTreeItem
*m_itemId
;
102 // ----------------------------------------------------------------------------
103 // wxTreeItemData is some (arbitrary) user class associated with some item. The
104 // main advantage of having this class (compared to old untyped interface) is
105 // that wxTreeItemData's are destroyed automatically by the tree and, as this
106 // class has virtual dtor, it means that the memory will be automatically
107 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
108 // the size of this class is critical: in any real application, each tree leaf
109 // will have wxTreeItemData associated with it and number of leaves may be
112 // Because the objects of this class are deleted by the tree, they should
113 // always be allocated on the heap!
114 // ----------------------------------------------------------------------------
115 class WXDLLEXPORT wxTreeItemData
: private wxTreeItemId
{
117 // default ctor/copy ctor/assignment operator are ok
119 // dtor is virtual and all the items are deleted by the tree control when
120 // it's deleted, so you normally don't have to care about freeing memory
121 // allocated in your wxTreeItemData-derived class
122 virtual ~wxTreeItemData() { }
124 // accessors: set/get the item associated with this node
125 void SetId(const wxTreeItemId
& id
) { m_itemId
= id
; }
126 const wxTreeItemId
& GetId() const { return (wxTreeItemId
&) m_itemId
; }
129 class WXDLLEXPORT wxTreeCtrl
: public wxControl
{
133 wxTreeCtrl() { Init(); }
135 wxTreeCtrl(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
136 const wxPoint
& pos
= wxDefaultPosition
,
137 const wxSize
& size
= wxDefaultSize
,
138 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
139 const wxValidator
& validator
= wxDefaultValidator
,
140 const wxString
& name
= "wxTreeCtrl") {
141 Create(parent
, id
, pos
, size
, style
, validator
, name
);
144 virtual ~wxTreeCtrl();
146 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
147 const wxPoint
& pos
= wxDefaultPosition
,
148 const wxSize
& size
= wxDefaultSize
,
149 long style
= wxTR_HAS_BUTTONS
| wxTR_LINES_AT_ROOT
,
150 const wxValidator
& validator
= wxDefaultValidator
,
151 const wxString
& name
= "wxTreeCtrl");
156 // get the total number of items in the control
157 size_t GetCount() const;
159 // indent is the number of pixels the children are indented relative to
160 // the parents position. SetIndent() also redraws the control
162 unsigned int GetIndent() const;
163 void SetIndent(unsigned int indent
);
165 // image list: these functions allow to associate an image list with
166 // the control and retrieve it. Note that the control does _not_ delete
167 // the associated image list when it's deleted in order to allow image
168 // lists to be shared between different controls.
170 // The normal image list is for the icons which correspond to the
171 // normal tree item state (whether it is selected or not).
172 // Additionally, the application might choose to show a state icon
173 // which corresponds to an app-defined item state (for example,
174 // checked/unchecked) which are taken from the state image list.
175 wxImageList
*GetImageList() const;
176 wxImageList
*GetStateImageList() const;
178 void SetImageList(wxImageList
*imageList
);
179 void SetStateImageList(wxImageList
*imageList
);
181 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
182 // member functions of wxTreeItem because they must know the tree the item
183 // belongs to for Windows implementation and storing the pointer to
184 // wxTreeCtrl in each wxTreeItem is just too much waste.
189 // retrieve items label
190 wxString
GetItemText(const wxTreeItemId
& item
) const;
191 // get the normal item image
192 int GetItemImage(const wxTreeItemId
& item
) const;
193 // get the selected item image
194 int GetItemSelectedImage(const wxTreeItemId
& item
) const;
195 // get the data associated with the item
196 wxTreeItemData
*GetItemData(const wxTreeItemId
& item
) const;
202 void SetItemText(const wxTreeItemId
& item
, const wxString
& text
);
203 // set the normal item image
204 void SetItemImage(const wxTreeItemId
& item
, int image
);
205 // set the selected item image
206 void SetItemSelectedImage(const wxTreeItemId
& item
, int image
);
207 // associate some data with the item
208 void SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
);
210 // item status inquiries
211 // ---------------------
213 // is the item visible (it might be outside the view or not expanded)?
214 bool IsVisible(const wxTreeItemId
& item
) const;
215 // does the item has any children?
216 bool ItemHasChildren(const wxTreeItemId
& item
) const;
217 // is the item expanded (only makes sense if HasChildren())?
218 bool IsExpanded(const wxTreeItemId
& item
) const;
219 // is this item currently selected (the same as has focus)?
220 bool IsSelected(const wxTreeItemId
& item
) const;
222 // number of children
223 // ------------------
225 // if 'recursively' is false, only immediate children count, otherwise
226 // the returned number is the number of all items in this branch
227 size_t GetChildrenCount(const wxTreeItemId
& item
, bool recursively
= true);
232 // wxTreeItemId.IsOk() will return false if there is no such item
234 // get the root tree item
235 wxTreeItemId
GetRootItem() const;
237 // get the item currently selected (may return NULL if no selection)
238 wxTreeItemId
GetSelection() const;
240 // get the parent of this item (may return NULL if root)
241 wxTreeItemId
GetItemParent(const wxTreeItemId
& item
) const;
243 // for this enumeration function you must pass in a "cookie" parameter
244 // which is opaque for the application but is necessary for the library
245 // to make these functions reentrant (i.e. allow more than one
246 // enumeration on one and the same object simultaneously). Of course,
247 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
250 // get the first child of this item
251 wxTreeItemId
GetFirstChild(const wxTreeItemId
& item
, long& cookie
) const;
252 // get the next child (after GetFirstChild or GetNextChild)
253 wxTreeItemId
GetNextChild(const wxTreeItemId
& item
, long& cookie
) const;
254 // get the last child of this item - this method doesn't use cookies
255 wxTreeItemId
GetLastChild(const wxTreeItemId
& item
) const;
257 // get the next sibling of this item
258 wxTreeItemId
GetNextSibling(const wxTreeItemId
& item
) const;
259 // get the previous sibling
260 wxTreeItemId
GetPrevSibling(const wxTreeItemId
& item
) const;
262 // get first visible item
263 wxTreeItemId
GetFirstVisibleItem() const;
264 // get the next visible item: item must be visible itself!
265 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
266 wxTreeItemId
GetNextVisible(const wxTreeItemId
& item
) const;
267 // get the previous visible item: item must be visible itself!
268 wxTreeItemId
GetPrevVisible(const wxTreeItemId
& item
) const;
273 // add the root node to the tree
274 wxTreeItemId
AddRoot(const wxString
& text
,
275 int image
= -1, int selectedImage
= -1,
276 wxTreeItemData
*data
= NULL
);
278 // insert a new item in as the first child of the parent
279 wxTreeItemId
PrependItem(const wxTreeItemId
& parent
,
280 const wxString
& text
,
281 int image
= -1, int selectedImage
= -1,
282 wxTreeItemData
*data
= NULL
);
284 // insert a new item after a given one
285 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
286 const wxTreeItemId
& idPrevious
,
287 const wxString
& text
,
288 int image
= -1, int selectedImage
= -1,
289 wxTreeItemData
*data
= NULL
);
291 // insert a new item in as the last child of the parent
292 wxTreeItemId
AppendItem(const wxTreeItemId
& parent
,
293 const wxString
& text
,
294 int image
= -1, int selectedImage
= -1,
295 wxTreeItemData
*data
= NULL
);
297 // delete this item and associated data if any
298 void Delete(const wxTreeItemId
& item
);
299 // delete all items from the tree
300 void DeleteAllItems();
303 void Expand(const wxTreeItemId
& item
);
304 // collapse the item without removing its children
305 void Collapse(const wxTreeItemId
& item
);
306 // collapse the item and remove all children
307 void CollapseAndReset(const wxTreeItemId
& item
);
308 // toggles the current state
309 void Toggle(const wxTreeItemId
& item
);
311 // remove the selection from currently selected item (if any)
314 void SelectItem(const wxTreeItemId
& item
);
315 // make sure this item is visible (expanding the parent item and/or
316 // scrolling to this item if necessary)
317 void EnsureVisible(const wxTreeItemId
& item
);
318 // scroll to this item (but don't expand its parent)
319 void ScrollTo(const wxTreeItemId
& item
);
321 // start editing the item label: this (temporarily) replaces the item
322 // with a one line edit control. The item will be selected if it hadn't
323 // been before. textCtrlClass parameter allows you to create an edit
324 // control of arbitrary user-defined class deriving from wxTextCtrl.
325 wxTextCtrl
* EditLabel(const wxTreeItemId
& item
,
326 wxClassInfo
* textCtrlClass
= CLASSINFO(wxTextCtrl
));
327 // returns the same pointer as StartEdit() if the item is being edited,
328 // NULL otherwise (it's assumed that no more than one item may be
329 // edited simultaneously)
330 wxTextCtrl
* GetEditControl() const;
331 // end editing and accept or discard the changes to item label
332 void EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
= false);
334 // sort the children of this item using the specified callback function
335 // (it should return -1, 0 or +1 as usual), if it's not specified
336 // alphabetical comparaison is performed.
338 // NB: this function is not reentrant!
339 void SortChildren(const wxTreeItemId
& item
,
340 wxTreeItemCmpFunc
*cmpFunction
= NULL
);
345 // these methods are deprecated and will be removed in future versions of
346 // wxWidgets, they're here for compatibility only, don't use them in new
347 // code (the comments indicate why these methods are now useless and how to
350 // use Expand, Collapse, CollapseAndReset or Toggle
351 void ExpandItem(const wxTreeItemId
& item
, int action
);
353 // use AddRoot, PrependItem or AppendItem
354 wxTreeItemId
InsertItem(const wxTreeItemId
& parent
,
355 const wxString
& text
,
356 int image
= -1, int selImage
= -1,
357 long insertAfter
= wxTREE_INSERT_LAST
);
359 // use Set/GetImageList and Set/GetStateImageList
360 wxImageList
*GetImageList(int) const
361 { return GetImageList(); }
362 void SetImageList(wxImageList
*imageList
, int)
363 { SetImageList(imageList
); }
365 void SendExpanding(const wxTreeItemId
& item
);
366 void SendExpanded(const wxTreeItemId
& item
);
367 void SendCollapsing(const wxTreeItemId
& item
);
368 void SendCollapsed(const wxTreeItemId
& item
);
369 void SendSelChanging(const wxTreeItemId
& item
);
370 void SendSelChanged(const wxTreeItemId
& item
);
372 wxTreeItemId m_editItem
;
374 GtkTreeItem
*m_anchor
;
375 wxTextCtrl
* m_textCtrl
;
376 wxImageList
* m_imageListNormal
;
377 wxImageList
* m_imageListState
;
381 void SendMessage(wxEventType command
, const wxTreeItemId
& item
);
382 // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
384 // the common part of all ctors
386 // insert a new item in as the last child of the parent
387 wxTreeItemId
p_InsertItem(GtkTreeItem
*p
,
388 const wxString
& text
,
389 int image
, int selectedImage
,
390 wxTreeItemData
*data
);
393 DECLARE_DYNAMIC_CLASS(wxTreeCtrl
)