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