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