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