]> git.saurik.com Git - wxWidgets.git/blob - include/wx/gtk/treectrl.h
Did much work on colors. It doesn't work and I guess
[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 license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
14
15 #ifdef __GNUG__
16 #pragma interface "treectrl.h"
17 #endif
18
19 #include "wx/textctrl.h"
20 #include "wx/control.h"
21 #include "wx/event.h"
22 #include "wx/imaglist.h"
23
24 #include <gtk/gtk.h>
25
26 // the type for "untyped" data
27 typedef long wxDataType;
28
29 // fwd decl
30 class wxImageList;
31 struct wxTreeViewItem;
32
33 // a callback function used for sorting tree items, it should return -1 if the
34 // first item precedes the second, +1 if the second precedes the first or 0 if
35 // they're equivalent
36 class wxTreeItemData;
37 typedef int (*wxTreeItemCmpFunc)(wxTreeItemData *item1, wxTreeItemData *item2);
38
39 // ----------------------------------------------------------------------------
40 // constants
41 // ----------------------------------------------------------------------------
42
43 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
44 // where exactly the specified point is situated:
45 // above the client area.
46 static const int wxTREE_HITTEST_ABOVE = 0x0001;
47 // below the client area.
48 static const int wxTREE_HITTEST_BELOW = 0x0002;
49 // in the client area but below the last item.
50 static const int wxTREE_HITTEST_NOWHERE = 0x0004;
51 // on the button associated with an item.
52 static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0010;
53 // on the bitmap associated with an item.
54 static const int wxTREE_HITTEST_ONITEMICON = 0x0020;
55 // in the indentation associated with an item.
56 static const int wxTREE_HITTEST_ONITEMINDENT = 0x0040;
57 // on the label (string) associated with an item.
58 static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080;
59 // in the area to the right of an item.
60 static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0100;
61 // on the state icon for a tree view item that is in a user-defined state.
62 static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0200;
63 // to the right of the client area.
64 static const int wxTREE_HITTEST_TOLEFT = 0x0400;
65 // to the left of the client area.
66 static const int wxTREE_HITTEST_TORIGHT = 0x0800;
67 // anywhere on the item
68 static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
69 wxTREE_HITTEST_ONITEMLABEL |
70 wxTREE_HITTEST_ONITEMSTATEICON;
71
72 // NB: all the following flags are for compatbility only and will be removed in
73 // next versions
74
75 // flags for deprecated `Expand(int action)'
76 enum
77 {
78 wxTREE_EXPAND_EXPAND,
79 wxTREE_EXPAND_COLLAPSE,
80 wxTREE_EXPAND_COLLAPSE_RESET,
81 wxTREE_EXPAND_TOGGLE
82 };
83
84 // flags for deprecated InsertItem() variant
85 #define wxTREE_INSERT_FIRST 0xFFFF0001
86 #define wxTREE_INSERT_LAST 0xFFFF0002
87
88 // ----------------------------------------------------------------------------
89 // wxTreeItemId identifies an element of the tree. In this implementation, it's
90 // just a trivial wrapper around GTK GtkTreeItem *. It's opaque for the
91 // application.
92 // ----------------------------------------------------------------------------
93 class WXDLLEXPORT wxTreeItemId {
94 public:
95 // ctors
96 wxTreeItemId() { m_itemId = NULL; }
97
98 // default copy ctor/assignment operator are ok for us
99
100 // accessors
101 // is this a valid tree item?
102 bool IsOk() const { return m_itemId != NULL; }
103
104 // conversion to/from either real (system-dependent) tree item id or
105 // to "long" which used to be the type for tree item ids in previous
106 // versions of wxWindows
107
108 // for wxTreeCtrl usage only
109 wxTreeItemId(GtkTreeItem *itemId) { m_itemId = itemId; }
110 operator GtkTreeItem *() const { return m_itemId; }
111
112 protected:
113 GtkTreeItem *m_itemId;
114 };
115
116 // ----------------------------------------------------------------------------
117 // wxTreeItemData is some (arbitrary) user class associated with some item. The
118 // main advantage of having this class (compared to old untyped interface) is
119 // that wxTreeItemData's are destroyed automatically by the tree and, as this
120 // class has virtual dtor, it means that the memory will be automatically
121 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
122 // the size of this class is critical: in any real application, each tree leaf
123 // will have wxTreeItemData associated with it and number of leaves may be
124 // quite big.
125 //
126 // Because the objects of this class are deleted by the tree, they should
127 // always be allocated on the heap!
128 // ----------------------------------------------------------------------------
129 class WXDLLEXPORT wxTreeItemData {
130 friend class wxTreeCtrl;
131 public:
132 // creation/destruction
133 // --------------------
134 // default ctor
135 wxTreeItemData() { }
136
137 // default copy ctor/assignment operator are ok
138 // dtor is virtual and all the items are deleted by the tree control
139 // when it's deleted, so you normally don't have to care about freeing
140 // memory allocated in your wxTreeItemData-derived class
141 virtual ~wxTreeItemData() { }
142
143 // accessor: get the item associated with us
144 const wxTreeItemId& GetItemId() const { return m_itemId; }
145
146 protected:
147 wxTreeItemId m_itemId;
148 };
149
150 class WXDLLEXPORT wxTreeCtrl: public wxControl {
151 public:
152 // creation
153 // --------
154 wxTreeCtrl() { Init(); }
155
156 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
157 const wxPoint& pos = wxDefaultPosition,
158 const wxSize& size = wxDefaultSize,
159 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
160 const wxValidator& validator = wxDefaultValidator,
161 const wxString& name = "wxTreeCtrl") {
162 Create(parent, id, pos, size, style, validator, name);
163 }
164
165 virtual ~wxTreeCtrl();
166
167 bool Create(wxWindow *parent, wxWindowID id = -1,
168 const wxPoint& pos = wxDefaultPosition,
169 const wxSize& size = wxDefaultSize,
170 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
171 const wxValidator& validator = wxDefaultValidator,
172 const wxString& name = "wxTreeCtrl");
173
174 // accessors
175 // ---------
176
177 // get the total number of items in the control
178 size_t GetCount() const;
179
180 // indent is the number of pixels the children are indented relative to
181 // the parents position. SetIndent() also redraws the control
182 // immediately.
183 unsigned int GetIndent() const;
184 void SetIndent(unsigned int indent);
185
186 // image list: these functions allow to associate an image list with
187 // the control and retrieve it. Note that the control does _not_ delete
188 // the associated image list when it's deleted in order to allow image
189 // lists to be shared between different controls.
190 //
191 // The normal image list is for the icons which correspond to the
192 // normal tree item state (whether it is selected or not).
193 // Additionally, the application might choose to show a state icon
194 // which corresponds to an app-defined item state (for example,
195 // checked/unchecked) which are taken from the state image list.
196 wxImageList *GetImageList() const;
197 wxImageList *GetStateImageList() const;
198
199 void SetImageList(wxImageList *imageList);
200 void SetStateImageList(wxImageList *imageList);
201
202 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
203 // member functions of wxTreeItem because they must know the tree the item
204 // belongs to for Windows implementation and storing the pointer to
205 // wxTreeCtrl in each wxTreeItem is just too much waste.
206
207 // accessors
208 // ---------
209
210 // retrieve items label
211 wxString GetItemText(const wxTreeItemId& item) const;
212 // get the normal item image
213 int GetItemImage(const wxTreeItemId& item) const;
214 // get the selected item image
215 int GetItemSelectedImage(const wxTreeItemId& item) const;
216 // get the data associated with the item
217 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
218
219 // modifiers
220 // ---------
221
222 // set items label
223 void SetItemText(const wxTreeItemId& item, const wxString& text);
224 // set the normal item image
225 void SetItemImage(const wxTreeItemId& item, int image);
226 // set the selected item image
227 void SetItemSelectedImage(const wxTreeItemId& item, int image);
228 // associate some data with the item
229 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
230
231 // item status inquiries
232 // ---------------------
233
234 // is the item visible (it might be outside the view or not expanded)?
235 bool IsVisible(const wxTreeItemId& item) const;
236 // does the item has any children?
237 bool ItemHasChildren(const wxTreeItemId& item) const;
238 // is the item expanded (only makes sense if HasChildren())?
239 bool IsExpanded(const wxTreeItemId& item) const;
240 // is this item currently selected (the same as has focus)?
241 bool IsSelected(const wxTreeItemId& item) const;
242
243 // navigation
244 // ----------
245
246 // wxTreeItemId.IsOk() will return FALSE if there is no such item
247
248 // get the root tree item
249 wxTreeItemId GetRootItem() const;
250
251 // get the item currently selected (may return NULL if no selection)
252 wxTreeItemId GetSelection() const;
253
254 // get the parent of this item (may return NULL if root)
255 wxTreeItemId GetParent(const wxTreeItemId& item) const;
256
257 // for this enumeration function you must pass in a "cookie" parameter
258 // which is opaque for the application but is necessary for the library
259 // to make these functions reentrant (i.e. allow more than one
260 // enumeration on one and the same object simultaneously). Of course,
261 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
262 // the same!
263
264 // get the first child of this item
265 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
266 // get the next child
267 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
268
269 // get the next sibling of this item
270 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
271 // get the previous sibling
272 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
273
274 // get first visible item
275 wxTreeItemId GetFirstVisibleItem() const;
276 // get the next visible item: item must be visible itself!
277 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
278 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
279 // get the previous visible item: item must be visible itself!
280 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
281
282 // operations
283 // ----------
284
285 // add the root node to the tree
286 wxTreeItemId AddRoot(const wxString& text,
287 int image = -1, int selectedImage = -1,
288 wxTreeItemData *data = NULL);
289
290 // insert a new item in as the first child of the parent
291 wxTreeItemId PrependItem(const wxTreeItemId& parent,
292 const wxString& text,
293 int image = -1, int selectedImage = -1,
294 wxTreeItemData *data = NULL);
295
296 // insert a new item after a given one
297 wxTreeItemId InsertItem(const wxTreeItemId& parent,
298 const wxTreeItemId& idPrevious,
299 const wxString& text,
300 int image = -1, int selectedImage = -1,
301 wxTreeItemData *data = NULL);
302
303 // insert a new item in as the last child of the parent
304 wxTreeItemId AppendItem(const wxTreeItemId& parent,
305 const wxString& text,
306 int image = -1, int selectedImage = -1,
307 wxTreeItemData *data = NULL);
308
309 // delete this item and associated data if any
310 void Delete(const wxTreeItemId& item);
311 // delete all items from the tree
312 void DeleteAllItems();
313
314 // expand this item
315 void Expand(const wxTreeItemId& item);
316 // collapse the item without removing its children
317 void Collapse(const wxTreeItemId& item);
318 // collapse the item and remove all children
319 void CollapseAndReset(const wxTreeItemId& item);
320 // toggles the current state
321 void Toggle(const wxTreeItemId& item);
322
323 // remove the selection from currently selected item (if any)
324 void Unselect();
325 // select this item
326 void SelectItem(const wxTreeItemId& item);
327 // make sure this item is visible (expanding the parent item and/or
328 // scrolling to this item if necessary)
329 void EnsureVisible(const wxTreeItemId& item);
330 // scroll to this item (but don't expand its parent)
331 void ScrollTo(const wxTreeItemId& item);
332
333 // start editing the item label: this (temporarily) replaces the item
334 // with a one line edit control. The item will be selected if it hadn't
335 // been before. textCtrlClass parameter allows you to create an edit
336 // control of arbitrary user-defined class deriving from wxTextCtrl.
337 wxTextCtrl* EditLabel(const wxTreeItemId& item,
338 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
339 // returns the same pointer as StartEdit() if the item is being edited,
340 // NULL otherwise (it's assumed that no more than one item may be
341 // edited simultaneously)
342 wxTextCtrl* GetEditControl() const;
343 // end editing and accept or discard the changes to item label
344 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
345
346 // sort the children of this item using the specified callback function
347 // (it should return -1, 0 or +1 as usual), if it's not specified
348 // alphabetical comparaison is performed.
349 //
350 // NB: this function is not reentrant!
351 void SortChildren(const wxTreeItemId& item,
352 wxTreeItemCmpFunc *cmpFunction = NULL);
353
354 // deprecated
355 // ----------
356
357 // these methods are deprecated and will be removed in future versions of
358 // wxWindows, they're here for compatibility only, don't use them in new
359 // code (the comments indicate why these methods are now useless and how to
360 // replace them)
361
362 // use Expand, Collapse, CollapseAndReset or Toggle
363 void ExpandItem(const wxTreeItemId& item, int action);
364
365 // use AddRoot, PrependItem or AppendItem
366 wxTreeItemId InsertItem(const wxTreeItemId& parent,
367 const wxString& text,
368 int image = -1, int selImage = -1,
369 long insertAfter = wxTREE_INSERT_LAST);
370
371 // use Set/GetImageList and Set/GetStateImageList
372 wxImageList *GetImageList(int) const
373 { return GetImageList(); }
374 void SetImageList(wxImageList *imageList, int)
375 { SetImageList(imageList); }
376
377 void SendExpanding(const wxTreeItemId& item);
378 void SendExpanded(const wxTreeItemId& item);
379 void SendCollapsing(const wxTreeItemId& item);
380 void SendCollapsed(const wxTreeItemId& item);
381 void SendSelChanging(const wxTreeItemId& item);
382 void SendSelChanged(const wxTreeItemId& item);
383 protected:
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 // GtkTreeItem *findGtkTreeItem(wxTreeCtrlId &id) const;
393
394 // the common part of all ctors
395 void Init();
396 // insert a new item in as the last child of the parent
397 wxTreeItemId p_InsertItem(GtkTreeItem *p,
398 const wxString& text,
399 int image, int selectedImage,
400 wxTreeItemData *data);
401
402
403 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
404 };
405
406 // ----------------------------------------------------------------------------
407 // wxTreeEvent is a special class for all events associated with tree controls
408 //
409 // NB: note that not all accessors make sense for all events, see the event
410 // descriptions below
411 // ----------------------------------------------------------------------------
412 class WXDLLEXPORT wxTreeEvent : public wxCommandEvent
413 {
414 friend wxTreeCtrl;
415 public:
416 wxTreeEvent(wxEventType commandType = wxEVT_NULL, int id = 0);
417
418 // accessors
419 // get the item on which the operation was performed or the newly
420 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
421 wxTreeItemId GetItem() const { return m_item; }
422
423 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
424 // selected item
425 wxTreeItemId GetOldItem() const { return m_itemOld; }
426
427 // the point where the mouse was when the drag operation started (for
428 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only)
429 wxPoint GetPoint() const { return m_pointDrag; }
430
431 // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only)
432 int GetCode() const { return m_code; }
433
434 // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events
435 // call this to forbid the change in item status
436 void Veto() { m_code = TRUE; }
437
438 private:
439 // @@ we could save some space by using union here
440 int m_code;
441 wxTreeItemId m_item,
442 m_itemOld;
443 wxPoint m_pointDrag;
444
445 DECLARE_DYNAMIC_CLASS(wxTreeEvent)
446 };
447
448 typedef void (wxEvtHandler::*wxTreeEventFunction)(wxTreeEvent&);
449
450 // ----------------------------------------------------------------------------
451 // macros for handling tree control events
452 // ----------------------------------------------------------------------------
453
454 // GetItem() returns the item being dragged, GetPoint() the mouse coords
455 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
456 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
457
458 // GetItem() returns the itme whose label is being edited
459 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
460 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
461
462 // provide/update information about GetItem() item
463 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
464 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
465
466 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
467 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
468 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
469 #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
470 #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
471
472 // GetOldItem() is the item which had the selection previously, GetItem() is
473 // the item which acquires selection
474 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
475 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
476
477 // GetCode() returns the key code
478 // NB: this is the only message for which GetItem() is invalid (you may get the
479 // item from GetSelection())
480 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
481
482 // GetItem() returns the item being deleted, the associated data (if any) will
483 // be deleted just after the return of this event handler (if any)
484 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL },
485
486 #endif
487 // _WX_TREECTRL_H_