]> git.saurik.com Git - wxWidgets.git/blame - include/wx/msw/treectrl.h
test complete :-)
[wxWidgets.git] / include / wx / msw / treectrl.h
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.h
3// Purpose: wxTreeCtrl class
4// Author: Julian Smart
08b7c251 5// Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
2bda0e17
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
08b7c251 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
bbcdf8bc
JS
12#ifndef _WX_TREECTRL_H_
13#define _WX_TREECTRL_H_
2bda0e17 14
08b7c251
VZ
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
d427503c 18
2bda0e17 19#ifdef __GNUG__
08b7c251 20 #pragma interface "treectrl.h"
2bda0e17
KB
21#endif
22
ad5c34f3 23#include "wx/textctrl.h"
2899e223 24#include "wx/dynarray.h"
2bda0e17 25
d427503c
VZ
26#ifdef __GNUWIN32__
27 // Cygwin windows.h defines these identifiers
28 #undef GetFirstChild
29 #undef GetNextSibling
30#endif // Cygwin
31
08b7c251
VZ
32// the type for "untyped" data
33typedef long wxDataType;
34
35// fwd decl
06e38c8e
JS
36class WXDLLEXPORT wxImageList;
37struct WXDLLEXPORT wxTreeViewItem;
08b7c251
VZ
38
39// a callback function used for sorting tree items, it should return -1 if the
40// first item precedes the second, +1 if the second precedes the first or 0 if
41// they're equivalent
42class wxTreeItemData;
08b7c251
VZ
43
44// ----------------------------------------------------------------------------
45// constants
46// ----------------------------------------------------------------------------
47
48// values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
49// where exactly the specified point is situated:
50 // above the client area.
51static const int wxTREE_HITTEST_ABOVE = 0x0001;
52 // below the client area.
53static const int wxTREE_HITTEST_BELOW = 0x0002;
54 // in the client area but below the last item.
55static const int wxTREE_HITTEST_NOWHERE = 0x0004;
56 // on the button associated with an item.
57static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0010;
58 // on the bitmap associated with an item.
59static const int wxTREE_HITTEST_ONITEMICON = 0x0020;
60 // in the indentation associated with an item.
61static const int wxTREE_HITTEST_ONITEMINDENT = 0x0040;
62 // on the label (string) associated with an item.
63static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080;
64 // in the area to the right of an item.
65static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0100;
66 // on the state icon for a tree view item that is in a user-defined state.
67static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0200;
68 // to the right of the client area.
69static const int wxTREE_HITTEST_TOLEFT = 0x0400;
70 // to the left of the client area.
71static const int wxTREE_HITTEST_TORIGHT = 0x0800;
72 // anywhere on the item
73static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
74 wxTREE_HITTEST_ONITEMLABEL |
75 wxTREE_HITTEST_ONITEMSTATEICON;
76
77// NB: all the following flags are for compatbility only and will be removed in the
78// next versions
79
80// flags for deprecated `Expand(int action)'
81enum
82{
2bda0e17
KB
83 wxTREE_EXPAND_EXPAND,
84 wxTREE_EXPAND_COLLAPSE,
85 wxTREE_EXPAND_COLLAPSE_RESET,
86 wxTREE_EXPAND_TOGGLE
87};
88
08b7c251 89// flags for deprecated InsertItem() variant
3197ed26
VZ
90#define wxTREE_INSERT_FIRST 0xFFFF0001
91#define wxTREE_INSERT_LAST 0xFFFF0002
2bda0e17 92
08b7c251
VZ
93// ----------------------------------------------------------------------------
94// wxTreeItemId identifies an element of the tree. In this implementation, it's
95// just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
96// application.
97// ----------------------------------------------------------------------------
98class WXDLLEXPORT wxTreeItemId
2bda0e17 99{
2bda0e17 100public:
08b7c251
VZ
101 // ctors
102 // 0 is invalid value for HTREEITEM
103 wxTreeItemId() { m_itemId = 0; }
104
105 // default copy ctor/assignment operator are ok for us
106
107 // accessors
108 // is this a valid tree item?
109 bool IsOk() const { return m_itemId != 0; }
110
111 // conversion to/from either real (system-dependent) tree item id or
112 // to "long" which used to be the type for tree item ids in previous
113 // versions of wxWindows
114
08b7c251 115 // for wxTreeCtrl usage only
06e38c8e
JS
116 wxTreeItemId(WXHTREEITEM itemId) { m_itemId = (long)itemId; }
117 operator WXHTREEITEM() const { return (WXHTREEITEM)m_itemId; }
118
add28c55 119 void operator=(WXHTREEITEM item) { m_itemId = (long) item; }
08b7c251
VZ
120
121protected:
122 long m_itemId;
2bda0e17
KB
123};
124
2b273975 125WX_DEFINE_EXPORTED_ARRAY(WXHTREEITEM, wxArrayTreeItemIds);
9dfbf520 126
08b7c251
VZ
127// ----------------------------------------------------------------------------
128// wxTreeItemData is some (arbitrary) user class associated with some item. The
129// main advantage of having this class (compared to old untyped interface) is
130// that wxTreeItemData's are destroyed automatically by the tree and, as this
131// class has virtual dtor, it means that the memory will be automatically
132// freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
133// the size of this class is critical: in any real application, each tree leaf
134// will have wxTreeItemData associated with it and number of leaves may be
135// quite big.
136//
137// Because the objects of this class are deleted by the tree, they should
138// always be allocated on the heap!
139// ----------------------------------------------------------------------------
3a5a2f56 140class WXDLLEXPORT wxTreeItemData : private wxTreeItemId
08b7c251 141{
08b7c251 142public:
3a5a2f56 143 // default ctor/copy ctor/assignment operator are ok
08b7c251 144
3a5a2f56
VZ
145 // dtor is virtual and all the items are deleted by the tree control when
146 // it's deleted, so you normally don't have to care about freeing memory
147 // allocated in your wxTreeItemData-derived class
08b7c251
VZ
148 virtual ~wxTreeItemData() { }
149
3a5a2f56
VZ
150 // accessors: set/get the item associated with this node
151 void SetId(const wxTreeItemId& id) { m_itemId = id; }
74b31181 152 const wxTreeItemId GetId() const { return *this; }
08b7c251
VZ
153};
154
155// ----------------------------------------------------------------------------
156// wxTreeCtrl
157// ----------------------------------------------------------------------------
158class WXDLLEXPORT wxTreeCtrl : public wxControl
2bda0e17 159{
092bddef 160public:
092bddef
VZ
161 // creation
162 // --------
08b7c251
VZ
163 wxTreeCtrl() { Init(); }
164
165 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
166 const wxPoint& pos = wxDefaultPosition,
167 const wxSize& size = wxDefaultSize,
168 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
169 const wxValidator& validator = wxDefaultValidator,
170 const wxString& name = "wxTreeCtrl")
092bddef
VZ
171 {
172 Create(parent, id, pos, size, style, validator, name);
173 }
08b7c251
VZ
174
175 virtual ~wxTreeCtrl();
176
092bddef
VZ
177 bool Create(wxWindow *parent, wxWindowID id = -1,
178 const wxPoint& pos = wxDefaultPosition,
179 const wxSize& size = wxDefaultSize,
08b7c251 180 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
092bddef
VZ
181 const wxValidator& validator = wxDefaultValidator,
182 const wxString& name = "wxTreeCtrl");
08b7c251 183
092bddef
VZ
184 // accessors
185 // ---------
092bddef 186
08b7c251
VZ
187 // get the total number of items in the control
188 size_t GetCount() const;
189
190 // indent is the number of pixels the children are indented relative to
191 // the parents position. SetIndent() also redraws the control
192 // immediately.
193 unsigned int GetIndent() const;
194 void SetIndent(unsigned int indent);
195
74b31181
VZ
196 // spacing is the number of pixels between the start and the Text
197 // not implemented under wxMSW
cf724bce 198 unsigned int GetSpacing() const { return 18; } // return wxGTK default
74b31181 199 void SetSpacing(unsigned int WXUNUSED(spacing)) { }
cf724bce 200
08b7c251
VZ
201 // image list: these functions allow to associate an image list with
202 // the control and retrieve it. Note that the control does _not_ delete
203 // the associated image list when it's deleted in order to allow image
204 // lists to be shared between different controls.
205 //
206 // The normal image list is for the icons which correspond to the
207 // normal tree item state (whether it is selected or not).
208 // Additionally, the application might choose to show a state icon
209 // which corresponds to an app-defined item state (for example,
210 // checked/unchecked) which are taken from the state image list.
211 wxImageList *GetImageList() const;
212 wxImageList *GetStateImageList() const;
213
214 void SetImageList(wxImageList *imageList);
215 void SetStateImageList(wxImageList *imageList);
216
217 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
218 // member functions of wxTreeItem because they must know the tree the item
219 // belongs to for Windows implementation and storing the pointer to
220 // wxTreeCtrl in each wxTreeItem is just too much waste.
221
222 // accessors
223 // ---------
224
225 // retrieve items label
226 wxString GetItemText(const wxTreeItemId& item) const;
74b31181
VZ
227 // get one of the images associated with the item (normal by default)
228 int GetItemImage(const wxTreeItemId& item,
229 wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
08b7c251
VZ
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);
74b31181
VZ
238 // get one of the images associated with the item (normal by default)
239 void SetItemImage(const wxTreeItemId& item, int image,
240 wxTreeItemIcon which = wxTreeItemIcon_Normal);
08b7c251
VZ
241 // associate some data with the item
242 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
243
3a5a2f56
VZ
244 // force appearance of [+] button near the item. This is useful to
245 // allow the user to expand the items which don't have any children now
246 // - but instead add them only when needed, thus minimizing memory
247 // usage and loading time.
248 void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE);
249
add28c55
VZ
250 // the item will be shown in bold
251 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
252
58a8ab88
JS
253 // the item will be shown with a drop highlight
254 void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = TRUE);
255
08b7c251
VZ
256 // item status inquiries
257 // ---------------------
258
259 // is the item visible (it might be outside the view or not expanded)?
260 bool IsVisible(const wxTreeItemId& item) const;
261 // does the item has any children?
262 bool ItemHasChildren(const wxTreeItemId& item) const;
263 // is the item expanded (only makes sense if HasChildren())?
264 bool IsExpanded(const wxTreeItemId& item) const;
265 // is this item currently selected (the same as has focus)?
266 bool IsSelected(const wxTreeItemId& item) const;
add28c55
VZ
267 // is item text in bold font?
268 bool IsBold(const wxTreeItemId& item) const;
08b7c251 269
4832f7c0
VZ
270 // number of children
271 // ------------------
272
273 // if 'recursively' is FALSE, only immediate children count, otherwise
274 // the returned number is the number of all items in this branch
9dfbf520
VZ
275 size_t GetChildrenCount(const wxTreeItemId& item,
276 bool recursively = TRUE) const;
4832f7c0 277
08b7c251
VZ
278 // navigation
279 // ----------
280
281 // wxTreeItemId.IsOk() will return FALSE if there is no such item
282
283 // get the root tree item
284 wxTreeItemId GetRootItem() const;
285
286 // get the item currently selected (may return NULL if no selection)
287 wxTreeItemId GetSelection() const;
288
9dfbf520
VZ
289 // get the items currently selected, return the number of such item
290 //
291 // NB: this operation is expensive and can take a long time for a
292 // control with a lot of items (~ O(number of items)).
293 size_t GetSelections(wxArrayTreeItemIds& selections) const;
294
08b7c251
VZ
295 // get the parent of this item (may return NULL if root)
296 wxTreeItemId GetParent(const wxTreeItemId& item) const;
297
298 // for this enumeration function you must pass in a "cookie" parameter
299 // which is opaque for the application but is necessary for the library
300 // to make these functions reentrant (i.e. allow more than one
301 // enumeration on one and the same object simultaneously). Of course,
302 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
303 // the same!
304
305 // get the first child of this item
06e38c8e 306 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
08b7c251 307 // get the next child
06e38c8e 308 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
978f38c2
VZ
309 // get the last child of this item - this method doesn't use cookies
310 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
08b7c251
VZ
311
312 // get the next sibling of this item
313 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
314 // get the previous sibling
315 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
316
317 // get first visible item
318 wxTreeItemId GetFirstVisibleItem() const;
319 // get the next visible item: item must be visible itself!
320 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
321 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
322 // get the previous visible item: item must be visible itself!
323 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
324
092bddef
VZ
325 // operations
326 // ----------
08b7c251
VZ
327
328 // add the root node to the tree
329 wxTreeItemId AddRoot(const wxString& text,
330 int image = -1, int selectedImage = -1,
331 wxTreeItemData *data = NULL);
332
333 // insert a new item in as the first child of the parent
334 wxTreeItemId PrependItem(const wxTreeItemId& parent,
335 const wxString& text,
336 int image = -1, int selectedImage = -1,
337 wxTreeItemData *data = NULL);
338
339 // insert a new item after a given one
340 wxTreeItemId InsertItem(const wxTreeItemId& parent,
341 const wxTreeItemId& idPrevious,
342 const wxString& text,
343 int image = -1, int selectedImage = -1,
344 wxTreeItemData *data = NULL);
345
346 // insert a new item in as the last child of the parent
347 wxTreeItemId AppendItem(const wxTreeItemId& parent,
348 const wxString& text,
349 int image = -1, int selectedImage = -1,
350 wxTreeItemData *data = NULL);
351
352 // delete this item and associated data if any
353 void Delete(const wxTreeItemId& item);
372edb9d
VZ
354 // delete all children (but don't delete the item itself)
355 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
356 void DeleteChildren(const wxTreeItemId& item);
08b7c251 357 // delete all items from the tree
372edb9d 358 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
08b7c251
VZ
359 void DeleteAllItems();
360
361 // expand this item
362 void Expand(const wxTreeItemId& item);
363 // collapse the item without removing its children
364 void Collapse(const wxTreeItemId& item);
365 // collapse the item and remove all children
366 void CollapseAndReset(const wxTreeItemId& item);
367 // toggles the current state
368 void Toggle(const wxTreeItemId& item);
369
370 // remove the selection from currently selected item (if any)
371 void Unselect();
9dfbf520
VZ
372 // unselect all items (only makes sense for multiple selection control)
373 void UnselectAll();
08b7c251
VZ
374 // select this item
375 void SelectItem(const wxTreeItemId& item);
376 // make sure this item is visible (expanding the parent item and/or
377 // scrolling to this item if necessary)
378 void EnsureVisible(const wxTreeItemId& item);
379 // scroll to this item (but don't expand its parent)
380 void ScrollTo(const wxTreeItemId& item);
381
382 // start editing the item label: this (temporarily) replaces the item
383 // with a one line edit control. The item will be selected if it hadn't
384 // been before. textCtrlClass parameter allows you to create an edit
385 // control of arbitrary user-defined class deriving from wxTextCtrl.
386 wxTextCtrl* EditLabel(const wxTreeItemId& item,
387 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
388 // returns the same pointer as StartEdit() if the item is being edited,
389 // NULL otherwise (it's assumed that no more than one item may be
390 // edited simultaneously)
391 wxTextCtrl* GetEditControl() const;
392 // end editing and accept or discard the changes to item label
393 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
394
95aabccc
VZ
395 // sorting
396 // this function is called to compare 2 items and should return -1, 0
397 // or +1 if the first item is less than, equal to or greater than the
398 // second one. The base class version performs alphabetic comparaison
399 // of item labels (GetText)
400 virtual int OnCompareItems(const wxTreeItemId& item1,
401 const wxTreeItemId& item2);
402 // sort the children of this item using OnCompareItems
08b7c251 403 //
95aabccc
VZ
404 // NB: this function is not reentrant and not MT-safe (FIXME)!
405 void SortChildren(const wxTreeItemId& item);
08b7c251
VZ
406
407 // helpers
408 // -------
409
add28c55
VZ
410 // determine to which item (if any) belongs the given point (the
411 // coordinates specified are relative to the client area of tree ctrl)
412 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
413 // constants.
414 //
415 // The first function is more portable (because easier to implement
416 // on other platforms), but the second one returns some extra info.
417 wxTreeItemId HitTest(const wxPoint& point)
418 { int dummy; return HitTest(point, dummy); }
419 wxTreeItemId HitTest(const wxPoint& point, int& flags);
08b7c251
VZ
420
421 // get the bounding rectangle of the item (or of its label only)
f7c832a7 422 bool GetBoundingRect(const wxTreeItemId& item,
16e93305 423 wxRect& rect,
08b7c251
VZ
424 bool textOnly = FALSE) const;
425
08b7c251
VZ
426 // deprecated
427 // ----------
428
429 // these methods are deprecated and will be removed in future versions of
430 // wxWindows, they're here for compatibility only, don't use them in new
431 // code (the comments indicate why these methods are now useless and how to
432 // replace them)
433
434 // use Expand, Collapse, CollapseAndReset or Toggle
435 void ExpandItem(const wxTreeItemId& item, int action);
436
437 // use AddRoot, PrependItem or AppendItem
438 wxTreeItemId InsertItem(const wxTreeItemId& parent,
439 const wxString& text,
440 int image = -1, int selImage = -1,
441 long insertAfter = wxTREE_INSERT_LAST);
442
443 // use Set/GetImageList and Set/GetStateImageList
444 wxImageList *GetImageList(int) const
445 { return GetImageList(); }
446 void SetImageList(wxImageList *imageList, int)
447 { SetImageList(imageList); }
448
74b31181
VZ
449 // use Set/GetItemImage directly
450 // get the selected item image
451 int GetItemSelectedImage(const wxTreeItemId& item) const
452 { return GetItemImage(item, wxTreeItemIcon_Selected); }
453 // set the selected item image
454 void SetItemSelectedImage(const wxTreeItemId& item, int image)
455 { SetItemImage(item, image, wxTreeItemIcon_Selected); }
456
08b7c251
VZ
457 // implementation
458 // --------------
fd3f686c 459 virtual bool MSWCommand(WXUINT param, WXWORD id);
a23fd0e1 460 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
08b7c251 461
9dfbf520
VZ
462 // get/set the check state for the item (only for wxTR_MULTIPLE)
463 bool IsItemChecked(const wxTreeItemId& item) const;
464 void SetItemCheck(const wxTreeItemId& item, bool check = TRUE);
465
2bda0e17 466protected:
08b7c251
VZ
467 // SetImageList helper
468 void SetAnyImageList(wxImageList *imageList, int which);
469
74b31181 470 wxTextCtrl *m_textCtrl; // used while editing the item label
08b7c251
VZ
471 wxImageList *m_imageListNormal, // images for tree elements
472 *m_imageListState; // special images for app defined states
473
474private:
475 // the common part of all ctors
476 void Init();
477
478 // helper functions
479 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
480 inline void DoSetItem(wxTreeViewItem *tvItem);
481
482 inline void DoExpand(const wxTreeItemId& item, int flag);
483
484 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
485 wxTreeItemId hInsertAfter,
486 const wxString& text,
487 int image, int selectedImage,
488 wxTreeItemData *data);
489
74b31181
VZ
490 int DoGetItemImageFromData(const wxTreeItemId& item,
491 wxTreeItemIcon which) const;
492 void DoSetItemImageFromData(const wxTreeItemId& item,
493 int image,
494 wxTreeItemIcon which) const;
9dfbf520
VZ
495 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
496
08b7c251 497 void DeleteTextCtrl();
092bddef 498
74b31181
VZ
499 // support for additional item images
500 friend class wxTreeItemIndirectData;
501 void SetIndirectItemData(const wxTreeItemId& item,
502 wxTreeItemIndirectData *data);
503 bool HasIndirectData(const wxTreeItemId& item) const;
504
505 wxArrayTreeItemIds m_itemsWithIndirectData;
506
092bddef 507 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
2bda0e17
KB
508};
509
2bda0e17 510#endif
bbcdf8bc 511 // _WX_TREECTRL_H_