added WXDLLIMPEXP_FWD_FOO macros in addition to WXDLLIMPEXP_FOO for use with forward...
[wxWidgets.git] / include / wx / gtk / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gtk/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: Denis Pershin
5 // Modified by:
6 // Created: 08/08/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Denis Pershin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
14
15 #include "wx/textctrl.h"
16 #include "wx/imaglist.h"
17
18 #include <gtk/gtk.h>
19
20 // the type for "untyped" data
21 typedef long wxDataType;
22
23 // fwd decl
24 class WXDLLIMPEXP_CORE wxImageList;
25 struct wxTreeViewItem;
26
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
29 // they're equivalent
30 class WXDLLIMPEXP_FWD_CORE wxTreeItemData;
31 typedef int (*wxTreeItemCmpFunc)(wxTreeItemData *item1, wxTreeItemData *item2);
32
33 // ----------------------------------------------------------------------------
34 // constants
35 // ----------------------------------------------------------------------------
36
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;
65
66 #if WXWIN_COMPATIBILITY_2_6
67 // NB: all the following flags are for compatbility only and will be removed in
68 // next versions
69 // flags for deprecated InsertItem() variant
70 #define wxTREE_INSERT_FIRST 0xFFFF0001
71 #define wxTREE_INSERT_LAST 0xFFFF0002
72 #endif
73
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
77 // application.
78 // ----------------------------------------------------------------------------
79 class WXDLLEXPORT wxTreeItemId {
80 public:
81 // ctors
82 wxTreeItemId() { m_itemId = NULL; }
83
84 // default copy ctor/assignment operator are ok for us
85
86 // accessors
87 // is this a valid tree item?
88 bool IsOk() const { return m_itemId != NULL; }
89
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
93
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; }
98
99 protected:
100 GtkTreeItem *m_itemId;
101 };
102
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
111 // quite big.
112 //
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 {
117 public:
118 // default ctor/copy ctor/assignment operator are ok
119
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() { }
124
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; }
128 };
129
130 class WXDLLEXPORT wxTreeCtrl: public wxControl
131 {
132 public:
133 // creation
134 // --------
135 wxTreeCtrl() { Init(); }
136
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")
143 {
144 Create(parent, id, pos, size, style, validator, name);
145 }
146
147 virtual ~wxTreeCtrl();
148
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");
155
156 // accessors
157 // ---------
158
159 // get the total number of items in the control
160 virtual unsigned int GetCount() const;
161
162 // indent is the number of pixels the children are indented relative to
163 // the parents position. SetIndent() also redraws the control
164 // immediately.
165 unsigned int GetIndent() const;
166 void SetIndent(unsigned int indent);
167
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.
172 //
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;
180
181 void SetImageList(wxImageList *imageList);
182 void SetStateImageList(wxImageList *imageList);
183
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.
188
189 // accessors
190 // ---------
191
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;
198
199 // modifiers
200 // ---------
201
202 // set items label
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);
208
209 // item status inquiries
210 // ---------------------
211
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;
220
221 // number of children
222 // ------------------
223
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);
227
228 // navigation
229 // ----------
230
231 // wxTreeItemId.IsOk() will return false if there is no such item
232
233 // get the root tree item
234 wxTreeItemId GetRootItem() const;
235
236 // get the item currently selected (may return NULL if no selection)
237 wxTreeItemId GetSelection() const;
238
239 // get the parent of this item (may return NULL if root)
240 wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
241
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
247 // the same!
248
249 // get the last child of this item - this method doesn't use cookies
250 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
251
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;
256
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;
264
265 // operations
266 // ----------
267
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);
272
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);
278
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);
285
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);
291
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();
296
297 // expand this item
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);
305
306 // remove the selection from currently selected item (if any)
307 void Unselect();
308 // select this item
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);
315
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);
328
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.
332 //
333 // NB: this function is not reentrant!
334 void SortChildren(const wxTreeItemId& item,
335 wxTreeItemCmpFunc *cmpFunction = NULL);
336
337 // deprecated
338 // ----------
339
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
344 // replace them)
345
346 // use Expand, Collapse, CollapseAndReset or Toggle
347 wxDEPRECATED( void ExpandItem(const wxTreeItemId& item, int action) );
348
349 // use SetImageList
350 wxDEPRECATED( void SetImageList(wxImageList *imageList, int) )
351 { SetImageList(imageList); }
352
353 // use Set/GetItemImage directly
354 wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId& item) const );
355 wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId& item, int image) );
356
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) );
367
368 #endif // WXWIN_COMPATIBILITY_2_6
369
370 // use Set/GetImageList and Set/GetStateImageList
371 wxImageList *GetImageList(int) const
372 { return GetImageList(); }
373
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);
380
381 protected:
382
383 wxTreeItemId m_editItem;
384 GtkTree *m_tree;
385 GtkTreeItem *m_anchor;
386 wxTextCtrl* m_textCtrl;
387 wxImageList* m_imageListNormal;
388 wxImageList* m_imageListState;
389
390 long m_curitemId;
391
392 void SendMessage(wxEventType command, const wxTreeItemId& item);
393 // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
394
395 // the common part of all ctors
396 void Init();
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);
402
403
404 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
405 };
406
407 #endif
408 // _WX_TREECTRL_H_