]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/treectrl.h
66871a4891112ede0857539ca40d7428dcaef8e2
[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
19 #ifdef __GNUG__
20 #pragma interface "treectrl.h"
21 #endif
22
23 #include "wx/textctrl.h"
24
25 #ifdef __GNUWIN32__
26 // Cygwin windows.h defines these identifiers
27 #undef GetFirstChild
28 #undef GetNextSibling
29 #endif // Cygwin
30
31 // the type for "untyped" data
32 typedef long wxDataType;
33
34 // fwd decl
35 class WXDLLEXPORT wxImageList;
36 struct WXDLLEXPORT wxTreeViewItem;
37
38 // a callback function used for sorting tree items, it should return -1 if the
39 // first item precedes the second, +1 if the second precedes the first or 0 if
40 // they're equivalent
41 class wxTreeItemData;
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 WX_DEFINE_ARRAY(wxTreeItemId, wxArrayTreeItemIds);
125
126 // ----------------------------------------------------------------------------
127 // wxTreeItemData is some (arbitrary) user class associated with some item. The
128 // main advantage of having this class (compared to old untyped interface) is
129 // that wxTreeItemData's are destroyed automatically by the tree and, as this
130 // class has virtual dtor, it means that the memory will be automatically
131 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
132 // the size of this class is critical: in any real application, each tree leaf
133 // will have wxTreeItemData associated with it and number of leaves may be
134 // quite big.
135 //
136 // Because the objects of this class are deleted by the tree, they should
137 // always be allocated on the heap!
138 // ----------------------------------------------------------------------------
139 class WXDLLEXPORT wxTreeItemData : private wxTreeItemId
140 {
141 public:
142 // default ctor/copy ctor/assignment operator are ok
143
144 // dtor is virtual and all the items are deleted by the tree control when
145 // it's deleted, so you normally don't have to care about freeing memory
146 // allocated in your wxTreeItemData-derived class
147 virtual ~wxTreeItemData() { }
148
149 // accessors: set/get the item associated with this node
150 void SetId(const wxTreeItemId& id) { m_itemId = id; }
151 #ifdef __WATCOMC__
152 const wxTreeItemId GetId() const { return m_itemId; }
153 #else
154 const wxTreeItemId& GetId() const { return (wxTreeItemId&) m_itemId; }
155 #endif
156 };
157
158 // ----------------------------------------------------------------------------
159 // wxTreeCtrl
160 // ----------------------------------------------------------------------------
161 class WXDLLEXPORT wxTreeCtrl : public wxControl
162 {
163 public:
164 // creation
165 // --------
166 wxTreeCtrl() { Init(); }
167
168 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
169 const wxPoint& pos = wxDefaultPosition,
170 const wxSize& size = wxDefaultSize,
171 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
172 const wxValidator& validator = wxDefaultValidator,
173 const wxString& name = "wxTreeCtrl")
174 {
175 Create(parent, id, pos, size, style, validator, name);
176 }
177
178 virtual ~wxTreeCtrl();
179
180 bool Create(wxWindow *parent, wxWindowID id = -1,
181 const wxPoint& pos = wxDefaultPosition,
182 const wxSize& size = wxDefaultSize,
183 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
184 const wxValidator& validator = wxDefaultValidator,
185 const wxString& name = "wxTreeCtrl");
186
187 // accessors
188 // ---------
189
190 // get the total number of items in the control
191 size_t GetCount() const;
192
193 // indent is the number of pixels the children are indented relative to
194 // the parents position. SetIndent() also redraws the control
195 // immediately.
196 unsigned int GetIndent() const;
197 void SetIndent(unsigned int indent);
198
199 // spacing is the number of pixels between the start and the Text
200 // not implemented under wxMSW
201 unsigned int GetSpacing() const { return 18; } // return wxGTK default
202 void SetSpacing(unsigned int ) {}
203
204 // image list: these functions allow to associate an image list with
205 // the control and retrieve it. Note that the control does _not_ delete
206 // the associated image list when it's deleted in order to allow image
207 // lists to be shared between different controls.
208 //
209 // The normal image list is for the icons which correspond to the
210 // normal tree item state (whether it is selected or not).
211 // Additionally, the application might choose to show a state icon
212 // which corresponds to an app-defined item state (for example,
213 // checked/unchecked) which are taken from the state image list.
214 wxImageList *GetImageList() const;
215 wxImageList *GetStateImageList() const;
216
217 void SetImageList(wxImageList *imageList);
218 void SetStateImageList(wxImageList *imageList);
219
220 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
221 // member functions of wxTreeItem because they must know the tree the item
222 // belongs to for Windows implementation and storing the pointer to
223 // wxTreeCtrl in each wxTreeItem is just too much waste.
224
225 // accessors
226 // ---------
227
228 // retrieve items label
229 wxString GetItemText(const wxTreeItemId& item) const;
230 // get the normal item image
231 int GetItemImage(const wxTreeItemId& item) const;
232 // get the selected item image
233 int GetItemSelectedImage(const wxTreeItemId& item) const;
234 // get the data associated with the item
235 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
236
237 // modifiers
238 // ---------
239
240 // set items label
241 void SetItemText(const wxTreeItemId& item, const wxString& text);
242 // set the normal item image
243 void SetItemImage(const wxTreeItemId& item, int image);
244 // set the selected item image
245 void SetItemSelectedImage(const wxTreeItemId& item, int image);
246 // associate some data with the item
247 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
248
249 // force appearance of [+] button near the item. This is useful to
250 // allow the user to expand the items which don't have any children now
251 // - but instead add them only when needed, thus minimizing memory
252 // usage and loading time.
253 void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE);
254
255 // the item will be shown in bold
256 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
257
258 // the item will be shown with a drop highlight
259 void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = TRUE);
260
261 // item status inquiries
262 // ---------------------
263
264 // is the item visible (it might be outside the view or not expanded)?
265 bool IsVisible(const wxTreeItemId& item) const;
266 // does the item has any children?
267 bool ItemHasChildren(const wxTreeItemId& item) const;
268 // is the item expanded (only makes sense if HasChildren())?
269 bool IsExpanded(const wxTreeItemId& item) const;
270 // is this item currently selected (the same as has focus)?
271 bool IsSelected(const wxTreeItemId& item) const;
272 // is item text in bold font?
273 bool IsBold(const wxTreeItemId& item) const;
274
275 // number of children
276 // ------------------
277
278 // if 'recursively' is FALSE, only immediate children count, otherwise
279 // the returned number is the number of all items in this branch
280 size_t GetChildrenCount(const wxTreeItemId& item,
281 bool recursively = TRUE) const;
282
283 // navigation
284 // ----------
285
286 // wxTreeItemId.IsOk() will return FALSE if there is no such item
287
288 // get the root tree item
289 wxTreeItemId GetRootItem() const;
290
291 // get the item currently selected (may return NULL if no selection)
292 wxTreeItemId GetSelection() const;
293
294 // get the items currently selected, return the number of such item
295 //
296 // NB: this operation is expensive and can take a long time for a
297 // control with a lot of items (~ O(number of items)).
298 size_t GetSelections(wxArrayTreeItemIds& selections) const;
299
300 // get the parent of this item (may return NULL if root)
301 wxTreeItemId GetParent(const wxTreeItemId& item) const;
302
303 // for this enumeration function you must pass in a "cookie" parameter
304 // which is opaque for the application but is necessary for the library
305 // to make these functions reentrant (i.e. allow more than one
306 // enumeration on one and the same object simultaneously). Of course,
307 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
308 // the same!
309
310 // get the first child of this item
311 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
312 // get the next child
313 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
314 // get the last child of this item - this method doesn't use cookies
315 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
316
317 // get the next sibling of this item
318 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
319 // get the previous sibling
320 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
321
322 // get first visible item
323 wxTreeItemId GetFirstVisibleItem() const;
324 // get the next visible item: item must be visible itself!
325 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
326 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
327 // get the previous visible item: item must be visible itself!
328 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
329
330 // operations
331 // ----------
332
333 // add the root node to the tree
334 wxTreeItemId AddRoot(const wxString& text,
335 int image = -1, int selectedImage = -1,
336 wxTreeItemData *data = NULL);
337
338 // insert a new item in as the first child of the parent
339 wxTreeItemId PrependItem(const wxTreeItemId& parent,
340 const wxString& text,
341 int image = -1, int selectedImage = -1,
342 wxTreeItemData *data = NULL);
343
344 // insert a new item after a given one
345 wxTreeItemId InsertItem(const wxTreeItemId& parent,
346 const wxTreeItemId& idPrevious,
347 const wxString& text,
348 int image = -1, int selectedImage = -1,
349 wxTreeItemData *data = NULL);
350
351 // insert a new item in as the last child of the parent
352 wxTreeItemId AppendItem(const wxTreeItemId& parent,
353 const wxString& text,
354 int image = -1, int selectedImage = -1,
355 wxTreeItemData *data = NULL);
356
357 // delete this item and associated data if any
358 void Delete(const wxTreeItemId& item);
359 // delete all children (but don't delete the item itself)
360 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
361 void DeleteChildren(const wxTreeItemId& item);
362 // delete all items from the tree
363 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
364 void DeleteAllItems();
365
366 // expand this item
367 void Expand(const wxTreeItemId& item);
368 // collapse the item without removing its children
369 void Collapse(const wxTreeItemId& item);
370 // collapse the item and remove all children
371 void CollapseAndReset(const wxTreeItemId& item);
372 // toggles the current state
373 void Toggle(const wxTreeItemId& item);
374
375 // remove the selection from currently selected item (if any)
376 void Unselect();
377 // unselect all items (only makes sense for multiple selection control)
378 void UnselectAll();
379 // select this item
380 void SelectItem(const wxTreeItemId& item);
381 // make sure this item is visible (expanding the parent item and/or
382 // scrolling to this item if necessary)
383 void EnsureVisible(const wxTreeItemId& item);
384 // scroll to this item (but don't expand its parent)
385 void ScrollTo(const wxTreeItemId& item);
386
387 // start editing the item label: this (temporarily) replaces the item
388 // with a one line edit control. The item will be selected if it hadn't
389 // been before. textCtrlClass parameter allows you to create an edit
390 // control of arbitrary user-defined class deriving from wxTextCtrl.
391 wxTextCtrl* EditLabel(const wxTreeItemId& item,
392 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
393 // returns the same pointer as StartEdit() if the item is being edited,
394 // NULL otherwise (it's assumed that no more than one item may be
395 // edited simultaneously)
396 wxTextCtrl* GetEditControl() const;
397 // end editing and accept or discard the changes to item label
398 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
399
400 // sorting
401 // this function is called to compare 2 items and should return -1, 0
402 // or +1 if the first item is less than, equal to or greater than the
403 // second one. The base class version performs alphabetic comparaison
404 // of item labels (GetText)
405 virtual int OnCompareItems(const wxTreeItemId& item1,
406 const wxTreeItemId& item2);
407 // sort the children of this item using OnCompareItems
408 //
409 // NB: this function is not reentrant and not MT-safe (FIXME)!
410 void SortChildren(const wxTreeItemId& item);
411
412 // helpers
413 // -------
414
415 // determine to which item (if any) belongs the given point (the
416 // coordinates specified are relative to the client area of tree ctrl)
417 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
418 // constants.
419 //
420 // The first function is more portable (because easier to implement
421 // on other platforms), but the second one returns some extra info.
422 wxTreeItemId HitTest(const wxPoint& point)
423 { int dummy; return HitTest(point, dummy); }
424 wxTreeItemId HitTest(const wxPoint& point, int& flags);
425
426 // get the bounding rectangle of the item (or of its label only)
427 bool GetBoundingRect(const wxTreeItemId& item,
428 wxRect& rect,
429 bool textOnly = FALSE) const;
430
431 // deprecated
432 // ----------
433
434 // these methods are deprecated and will be removed in future versions of
435 // wxWindows, they're here for compatibility only, don't use them in new
436 // code (the comments indicate why these methods are now useless and how to
437 // replace them)
438
439 // use Expand, Collapse, CollapseAndReset or Toggle
440 void ExpandItem(const wxTreeItemId& item, int action);
441
442 // use AddRoot, PrependItem or AppendItem
443 wxTreeItemId InsertItem(const wxTreeItemId& parent,
444 const wxString& text,
445 int image = -1, int selImage = -1,
446 long insertAfter = wxTREE_INSERT_LAST);
447
448 // use Set/GetImageList and Set/GetStateImageList
449 wxImageList *GetImageList(int) const
450 { return GetImageList(); }
451 void SetImageList(wxImageList *imageList, int)
452 { SetImageList(imageList); }
453
454 // implementation
455 // --------------
456 virtual bool MSWCommand(WXUINT param, WXWORD id);
457 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
458
459 // get/set the check state for the item (only for wxTR_MULTIPLE)
460 bool IsItemChecked(const wxTreeItemId& item) const;
461 void SetItemCheck(const wxTreeItemId& item, bool check = TRUE);
462
463 protected:
464 // SetImageList helper
465 void SetAnyImageList(wxImageList *imageList, int which);
466
467 wxTextCtrl* m_textCtrl; // used while editing the item label
468 wxImageList *m_imageListNormal, // images for tree elements
469 *m_imageListState; // special images for app defined states
470
471 private:
472 // the common part of all ctors
473 void Init();
474
475 // helper functions
476 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
477 inline void DoSetItem(wxTreeViewItem *tvItem);
478
479 inline void DoExpand(const wxTreeItemId& item, int flag);
480
481 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
482 wxTreeItemId hInsertAfter,
483 const wxString& text,
484 int image, int selectedImage,
485 wxTreeItemData *data);
486
487 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
488
489 void DeleteTextCtrl();
490
491 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
492 };
493
494 #endif
495 // _WX_TREECTRL_H_