]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gtk1/treectrl.h
Fix wxPropertyGrid::GetPropertyRect when the last item is collapsed.
[wxWidgets.git] / include / wx / gtk1 / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/gtk1/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: Denis Pershin
5 // Modified by:
6 // Created: 08/08/98
7 // Copyright: (c) Denis Pershin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_TREECTRL_H_
12 #define _WX_TREECTRL_H_
13
14 #include "wx/textctrl.h"
15 #include "wx/imaglist.h"
16
17 #include <gtk/gtk.h>
18
19 // the type for "untyped" data
20 typedef long wxDataType;
21
22 // fwd decl
23 class WXDLLIMPEXP_CORE wxImageList;
24 struct wxTreeViewItem;
25
26 // a callback function used for sorting tree items, it should return -1 if the
27 // first item precedes the second, +1 if the second precedes the first or 0 if
28 // they're equivalent
29 class WXDLLIMPEXP_FWD_CORE wxTreeItemData;
30 typedef int (*wxTreeItemCmpFunc)(wxTreeItemData *item1, wxTreeItemData *item2);
31
32 // ----------------------------------------------------------------------------
33 // constants
34 // ----------------------------------------------------------------------------
35
36 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
37 // where exactly the specified point is situated:
38 // above the client area.
39 static const int wxTREE_HITTEST_ABOVE = 0x0001;
40 // below the client area.
41 static const int wxTREE_HITTEST_BELOW = 0x0002;
42 // in the client area but below the last item.
43 static const int wxTREE_HITTEST_NOWHERE = 0x0004;
44 // on the button associated with an item.
45 static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0010;
46 // on the bitmap associated with an item.
47 static const int wxTREE_HITTEST_ONITEMICON = 0x0020;
48 // in the indentation associated with an item.
49 static const int wxTREE_HITTEST_ONITEMINDENT = 0x0040;
50 // on the label (string) associated with an item.
51 static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080;
52 // in the area to the right of an item.
53 static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0100;
54 // on the state icon for a tree view item that is in a user-defined state.
55 static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0200;
56 // to the right of the client area.
57 static const int wxTREE_HITTEST_TOLEFT = 0x0400;
58 // to the left of the client area.
59 static const int wxTREE_HITTEST_TORIGHT = 0x0800;
60 // anywhere on the item
61 static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
62 wxTREE_HITTEST_ONITEMLABEL |
63 wxTREE_HITTEST_ONITEMSTATEICON;
64
65 #if WXWIN_COMPATIBILITY_2_6
66 // NB: all the following flags are for compatbility only and will be removed in
67 // next versions
68 // flags for deprecated InsertItem() variant
69 #define wxTREE_INSERT_FIRST 0xFFFF0001
70 #define wxTREE_INSERT_LAST 0xFFFF0002
71 #endif
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 WXDLLIMPEXP_CORE 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 WXDLLIMPEXP_CORE 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 WXDLLIMPEXP_CORE 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 virtual unsigned int 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 data associated with the item
194 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
195
196 // modifiers
197 // ---------
198
199 // set items label
200 void SetItemText(const wxTreeItemId& item, const wxString& text);
201 // set the normal item image
202 void SetItemImage(const wxTreeItemId& item, int image);
203 // associate some data with the item
204 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
205
206 // item status inquiries
207 // ---------------------
208
209 // is the item visible (it might be outside the view or not expanded)?
210 bool IsVisible(const wxTreeItemId& item) const;
211 // does the item has any children?
212 bool ItemHasChildren(const wxTreeItemId& item) const;
213 // is the item expanded (only makes sense if HasChildren())?
214 bool IsExpanded(const wxTreeItemId& item) const;
215 // is this item currently selected (the same as has focus)?
216 bool IsSelected(const wxTreeItemId& item) const;
217
218 // number of children
219 // ------------------
220
221 // if 'recursively' is false, only immediate children count, otherwise
222 // the returned number is the number of all items in this branch
223 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = true);
224
225 // navigation
226 // ----------
227
228 // wxTreeItemId.IsOk() will return false if there is no such item
229
230 // get the root tree item
231 wxTreeItemId GetRootItem() const;
232
233 // get the item currently selected (may return NULL if no selection)
234 wxTreeItemId GetSelection() const;
235
236 // get the parent of this item (may return NULL if root)
237 wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
238
239 // for this enumeration function you must pass in a "cookie" parameter
240 // which is opaque for the application but is necessary for the library
241 // to make these functions reentrant (i.e. allow more than one
242 // enumeration on one and the same object simultaneously). Of course,
243 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
244 // the same!
245
246 // get the last child of this item - this method doesn't use cookies
247 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
248
249 // get the next sibling of this item
250 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
251 // get the previous sibling
252 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
253
254 // get first visible item
255 wxTreeItemId GetFirstVisibleItem() const;
256 // get the next visible item: item must be visible itself!
257 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
258 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
259 // get the previous visible item: item must be visible itself!
260 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
261
262 // operations
263 // ----------
264
265 // add the root node to the tree
266 wxTreeItemId AddRoot(const wxString& text,
267 int image = -1, int selectedImage = -1,
268 wxTreeItemData *data = NULL);
269
270 // insert a new item in as the first child of the parent
271 wxTreeItemId PrependItem(const wxTreeItemId& parent,
272 const wxString& text,
273 int image = -1, int selectedImage = -1,
274 wxTreeItemData *data = NULL);
275
276 // insert a new item after a given one
277 wxTreeItemId InsertItem(const wxTreeItemId& parent,
278 const wxTreeItemId& idPrevious,
279 const wxString& text,
280 int image = -1, int selectedImage = -1,
281 wxTreeItemData *data = NULL);
282
283 // insert a new item in as the last child of the parent
284 wxTreeItemId AppendItem(const wxTreeItemId& parent,
285 const wxString& text,
286 int image = -1, int selectedImage = -1,
287 wxTreeItemData *data = NULL);
288
289 // delete this item and associated data if any
290 void Delete(const wxTreeItemId& item);
291 // delete all items from the tree
292 void DeleteAllItems();
293
294 // expand this item
295 void Expand(const wxTreeItemId& item);
296 // collapse the item without removing its children
297 void Collapse(const wxTreeItemId& item);
298 // collapse the item and remove all children
299 void CollapseAndReset(const wxTreeItemId& item);
300 // toggles the current state
301 void Toggle(const wxTreeItemId& item);
302
303 // remove the selection from currently selected item (if any)
304 void Unselect();
305 // select this item
306 void SelectItem(const wxTreeItemId& item);
307 // make sure this item is visible (expanding the parent item and/or
308 // scrolling to this item if necessary)
309 void EnsureVisible(const wxTreeItemId& item);
310 // scroll to this item (but don't expand its parent)
311 void ScrollTo(const wxTreeItemId& item);
312
313 // start editing the item label: this (temporarily) replaces the item
314 // with a one line edit control. The item will be selected if it hadn't
315 // been before. textCtrlClass parameter allows you to create an edit
316 // control of arbitrary user-defined class deriving from wxTextCtrl.
317 wxTextCtrl* EditLabel(const wxTreeItemId& item,
318 wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl));
319 // returns the same pointer as StartEdit() if the item is being edited,
320 // NULL otherwise (it's assumed that no more than one item may be
321 // edited simultaneously)
322 wxTextCtrl* GetEditControl() const;
323 // end editing and accept or discard the changes to item label
324 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = false);
325
326 // sort the children of this item using the specified callback function
327 // (it should return -1, 0 or +1 as usual), if it's not specified
328 // alphabetical comparaison is performed.
329 //
330 // NB: this function is not reentrant!
331 void SortChildren(const wxTreeItemId& item,
332 wxTreeItemCmpFunc *cmpFunction = NULL);
333
334 // deprecated
335 // ----------
336
337 #if WXWIN_COMPATIBILITY_2_6
338 // these methods are deprecated and will be removed in future versions of
339 // wxWidgets, they're here for compatibility only, don't use them in new
340 // code (the comments indicate why these methods are now useless and how to
341 // replace them)
342
343 // use Expand, Collapse, CollapseAndReset or Toggle
344 wxDEPRECATED( void ExpandItem(const wxTreeItemId& item, int action) );
345
346 // use SetImageList
347 wxDEPRECATED( void SetImageList(wxImageList *imageList, int) )
348 { SetImageList(imageList); }
349
350 // use Set/GetItemImage directly
351 wxDEPRECATED( int GetItemSelectedImage(const wxTreeItemId& item) const );
352 wxDEPRECATED( void SetItemSelectedImage(const wxTreeItemId& item, int image) );
353
354 // get the first child of this item
355 wxDEPRECATED( wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const );
356 // get the next child (after GetFirstChild or GetNextChild)
357 wxDEPRECATED( wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const );
358
359 // use AddRoot, PrependItem or AppendItem
360 wxDEPRECATED( wxTreeItemId InsertItem(const wxTreeItemId& parent,
361 const wxString& text,
362 int image = -1, int selImage = -1,
363 long insertAfter = wxTREE_INSERT_LAST) );
364
365 #endif // WXWIN_COMPATIBILITY_2_6
366
367 // use Set/GetImageList and Set/GetStateImageList
368 wxImageList *GetImageList(int) const
369 { return GetImageList(); }
370
371 void SendExpanding(const wxTreeItemId& item);
372 void SendExpanded(const wxTreeItemId& item);
373 void SendCollapsing(const wxTreeItemId& item);
374 void SendCollapsed(const wxTreeItemId& item);
375 void SendSelChanging(const wxTreeItemId& item);
376 void SendSelChanged(const wxTreeItemId& item);
377
378 protected:
379
380 wxTreeItemId m_editItem;
381 GtkTree *m_tree;
382 GtkTreeItem *m_anchor;
383 wxTextCtrl* m_textCtrl;
384 wxImageList* m_imageListNormal;
385 wxImageList* m_imageListState;
386
387 long m_curitemId;
388
389 void SendMessage(wxEventType command, const wxTreeItemId& item);
390 // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
391
392 // the common part of all ctors
393 void Init();
394 // insert a new item in as the last child of the parent
395 wxTreeItemId p_InsertItem(GtkTreeItem *p,
396 const wxString& text,
397 int image, int selectedImage,
398 wxTreeItemData *data);
399
400
401 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
402 };
403
404 #endif
405 // _WX_TREECTRL_H_