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