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