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