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