]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gtk/treectrl.h
Tweaks for demos on MacOSX
[wxWidgets.git] / include / wx / gtk / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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_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 // NB: all the following flags are for compatbility only and will be removed in
67 // next versions
68
69 // flags for deprecated InsertItem() variant
70 #define wxTREE_INSERT_FIRST 0xFFFF0001
71 #define wxTREE_INSERT_LAST 0xFFFF0002
72
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
76 // application.
77 // ----------------------------------------------------------------------------
78 class WXDLLEXPORT wxTreeItemId {
79 public:
80 // ctors
81 wxTreeItemId() { m_itemId = NULL; }
82
83 // default copy ctor/assignment operator are ok for us
84
85 // accessors
86 // is this a valid tree item?
87 bool IsOk() const { return m_itemId != NULL; }
88
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
92
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; }
97
98 protected:
99 GtkTreeItem *m_itemId;
100 };
101
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
110 // quite big.
111 //
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 {
116 public:
117 // default ctor/copy ctor/assignment operator are ok
118
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() { }
123
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; }
127 };
128
129 class WXDLLEXPORT wxTreeCtrl: public wxControl {
130 public:
131 // creation
132 // --------
133 wxTreeCtrl() { Init(); }
134
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);
142 }
143
144 virtual ~wxTreeCtrl();
145
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");
152
153 // accessors
154 // ---------
155
156 // get the total number of items in the control
157 size_t GetCount() const;
158
159 // indent is the number of pixels the children are indented relative to
160 // the parents position. SetIndent() also redraws the control
161 // immediately.
162 unsigned int GetIndent() const;
163 void SetIndent(unsigned int indent);
164
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.
169 //
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;
177
178 void SetImageList(wxImageList *imageList);
179 void SetStateImageList(wxImageList *imageList);
180
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.
185
186 // accessors
187 // ---------
188
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;
197
198 // modifiers
199 // ---------
200
201 // set items label
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);
209
210 // item status inquiries
211 // ---------------------
212
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;
221
222 // number of children
223 // ------------------
224
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);
228
229 // navigation
230 // ----------
231
232 // wxTreeItemId.IsOk() will return false if there is no such item
233
234 // get the root tree item
235 wxTreeItemId GetRootItem() const;
236
237 // get the item currently selected (may return NULL if no selection)
238 wxTreeItemId GetSelection() const;
239
240 // get the parent of this item (may return NULL if root)
241 wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
242
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
248 // the same!
249
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;
256
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;
261
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;
269
270 // operations
271 // ----------
272
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);
277
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);
283
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);
290
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);
296
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();
301
302 // expand this item
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);
310
311 // remove the selection from currently selected item (if any)
312 void Unselect();
313 // select this item
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);
320
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);
333
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.
337 //
338 // NB: this function is not reentrant!
339 void SortChildren(const wxTreeItemId& item,
340 wxTreeItemCmpFunc *cmpFunction = NULL);
341
342 // deprecated
343 // ----------
344
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
348 // replace them)
349
350 // use Expand, Collapse, CollapseAndReset or Toggle
351 void ExpandItem(const wxTreeItemId& item, int action);
352
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);
358
359 // use Set/GetImageList and Set/GetStateImageList
360 wxImageList *GetImageList(int) const
361 { return GetImageList(); }
362 void SetImageList(wxImageList *imageList, int)
363 { SetImageList(imageList); }
364
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);
371 protected:
372 wxTreeItemId m_editItem;
373 GtkTree *m_tree;
374 GtkTreeItem *m_anchor;
375 wxTextCtrl* m_textCtrl;
376 wxImageList* m_imageListNormal;
377 wxImageList* m_imageListState;
378
379 long m_curitemId;
380
381 void SendMessage(wxEventType command, const wxTreeItemId& item);
382 // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
383
384 // the common part of all ctors
385 void Init();
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);
391
392
393 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
394 };
395
396 #endif
397 // _WX_TREECTRL_H_