]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/treectrl.h
1. wxTreeCtrl::SetBackgroundColour() works (with new comctl32 anyhow)
[wxWidgets.git] / include / wx / msw / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #ifdef __GNUG__
20 #pragma interface "treectrl.h"
21 #endif
22
23 #include "wx/textctrl.h"
24 #include "wx/dynarray.h"
25
26 #ifdef __GNUWIN32__
27 // Cygwin windows.h defines these identifiers
28 #undef GetFirstChild
29 #undef GetNextSibling
30 #endif // Cygwin
31
32 // the type for "untyped" data
33 typedef long wxDataType;
34
35 // fwd decl
36 class WXDLLEXPORT wxImageList;
37 struct WXDLLEXPORT wxTreeViewItem;
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
42 class wxTreeItemData;
43
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)'
48 enum
49 {
50 wxTREE_EXPAND_EXPAND,
51 wxTREE_EXPAND_COLLAPSE,
52 wxTREE_EXPAND_COLLAPSE_RESET,
53 wxTREE_EXPAND_TOGGLE
54 };
55
56 // flags for deprecated InsertItem() variant
57 #define wxTREE_INSERT_FIRST 0xFFFF0001
58 #define wxTREE_INSERT_LAST 0xFFFF0002
59
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 // ----------------------------------------------------------------------------
65 class WXDLLEXPORT wxTreeItemId
66 {
67 public:
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
82 // for wxTreeCtrl usage only
83 wxTreeItemId(WXHTREEITEM itemId) { m_itemId = (long)itemId; }
84 operator WXHTREEITEM() const { return (WXHTREEITEM)m_itemId; }
85
86 void operator=(WXHTREEITEM item) { m_itemId = (long) item; }
87
88 protected:
89 long m_itemId;
90 };
91
92 WX_DEFINE_EXPORTED_ARRAY(WXHTREEITEM, wxArrayTreeItemIds);
93
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 // ----------------------------------------------------------------------------
107 class WXDLLEXPORT wxTreeItemData : private wxTreeItemId
108 {
109 public:
110 // default ctor/copy ctor/assignment operator are ok
111
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
115 virtual ~wxTreeItemData() { }
116
117 // accessors: set/get the item associated with this node
118 void SetId(const wxTreeItemId& id) { m_itemId = id; }
119 const wxTreeItemId GetId() const { return *this; }
120 };
121
122 // ----------------------------------------------------------------------------
123 // wxTreeCtrl
124 // ----------------------------------------------------------------------------
125 class WXDLLEXPORT wxTreeCtrl : public wxControl
126 {
127 public:
128 // creation
129 // --------
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")
138 {
139 Create(parent, id, pos, size, style, validator, name);
140 }
141
142 virtual ~wxTreeCtrl();
143
144 bool Create(wxWindow *parent, wxWindowID id = -1,
145 const wxPoint& pos = wxDefaultPosition,
146 const wxSize& size = wxDefaultSize,
147 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
148 const wxValidator& validator = wxDefaultValidator,
149 const wxString& name = "wxTreeCtrl");
150
151 // accessors
152 // ---------
153
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
163 // spacing is the number of pixels between the start and the Text
164 // not implemented under wxMSW
165 unsigned int GetSpacing() const { return 18; } // return wxGTK default
166 void SetSpacing(unsigned int WXUNUSED(spacing)) { }
167
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;
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;
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);
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);
208 // associate some data with the item
209 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
210
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
217 // the item will be shown in bold
218 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
219
220 // the item will be shown with a drop highlight
221 void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = TRUE);
222
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
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;
243 // is item text in bold font?
244 bool IsBold(const wxTreeItemId& item) const;
245
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
251 size_t GetChildrenCount(const wxTreeItemId& item,
252 bool recursively = TRUE) const;
253
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
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
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
282 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
283 // get the next child
284 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
285 // get the last child of this item - this method doesn't use cookies
286 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
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
301 // operations
302 // ----------
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 before the one with the given index
323 wxTreeItemId InsertItem(const wxTreeItemId& parent,
324 size_t index,
325 const wxString& text,
326 int image = -1, int selectedImage = -1,
327 wxTreeItemData *data = NULL);
328
329 // insert a new item in as the last child of the parent
330 wxTreeItemId AppendItem(const wxTreeItemId& parent,
331 const wxString& text,
332 int image = -1, int selectedImage = -1,
333 wxTreeItemData *data = NULL);
334
335 // delete this item and associated data if any
336 void Delete(const wxTreeItemId& item);
337 // delete all children (but don't delete the item itself)
338 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
339 void DeleteChildren(const wxTreeItemId& item);
340 // delete all items from the tree
341 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
342 void DeleteAllItems();
343
344 // expand this item
345 void Expand(const wxTreeItemId& item);
346 // collapse the item without removing its children
347 void Collapse(const wxTreeItemId& item);
348 // collapse the item and remove all children
349 void CollapseAndReset(const wxTreeItemId& item);
350 // toggles the current state
351 void Toggle(const wxTreeItemId& item);
352
353 // remove the selection from currently selected item (if any)
354 void Unselect();
355 // unselect all items (only makes sense for multiple selection control)
356 void UnselectAll();
357 // select this item
358 void SelectItem(const wxTreeItemId& item);
359 // make sure this item is visible (expanding the parent item and/or
360 // scrolling to this item if necessary)
361 void EnsureVisible(const wxTreeItemId& item);
362 // scroll to this item (but don't expand its parent)
363 void ScrollTo(const wxTreeItemId& item);
364
365 // start editing the item label: this (temporarily) replaces the item
366 // with a one line edit control. The item will be selected if it hadn't
367 // been before. textCtrlClass parameter allows you to create an edit
368 // control of arbitrary user-defined class deriving from wxTextCtrl.
369 wxTextCtrl* EditLabel(const wxTreeItemId& item,
370 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
371 // returns the same pointer as StartEdit() if the item is being edited,
372 // NULL otherwise (it's assumed that no more than one item may be
373 // edited simultaneously)
374 wxTextCtrl* GetEditControl() const;
375 // end editing and accept or discard the changes to item label
376 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
377
378 // sorting
379 // this function is called to compare 2 items and should return -1, 0
380 // or +1 if the first item is less than, equal to or greater than the
381 // second one. The base class version performs alphabetic comparaison
382 // of item labels (GetText)
383 virtual int OnCompareItems(const wxTreeItemId& item1,
384 const wxTreeItemId& item2);
385 // sort the children of this item using OnCompareItems
386 //
387 // NB: this function is not reentrant and not MT-safe (FIXME)!
388 void SortChildren(const wxTreeItemId& item);
389
390 // helpers
391 // -------
392
393 // determine to which item (if any) belongs the given point (the
394 // coordinates specified are relative to the client area of tree ctrl)
395 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
396 // constants.
397 //
398 // The first function is more portable (because easier to implement
399 // on other platforms), but the second one returns some extra info.
400 wxTreeItemId HitTest(const wxPoint& point)
401 { int dummy; return HitTest(point, dummy); }
402 wxTreeItemId HitTest(const wxPoint& point, int& flags);
403
404 // get the bounding rectangle of the item (or of its label only)
405 bool GetBoundingRect(const wxTreeItemId& item,
406 wxRect& rect,
407 bool textOnly = FALSE) const;
408
409 // deprecated
410 // ----------
411
412 // these methods are deprecated and will be removed in future versions of
413 // wxWindows, they're here for compatibility only, don't use them in new
414 // code (the comments indicate why these methods are now useless and how to
415 // replace them)
416
417 // use Expand, Collapse, CollapseAndReset or Toggle
418 void ExpandItem(const wxTreeItemId& item, int action);
419
420 // use AddRoot, PrependItem or AppendItem
421 wxTreeItemId InsertItem(const wxTreeItemId& parent,
422 const wxString& text,
423 int image = -1, int selImage = -1,
424 long insertAfter = wxTREE_INSERT_LAST);
425
426 // use Set/GetImageList and Set/GetStateImageList
427 wxImageList *GetImageList(int) const
428 { return GetImageList(); }
429 void SetImageList(wxImageList *imageList, int)
430 { SetImageList(imageList); }
431
432 // use Set/GetItemImage directly
433 // get the selected item image
434 int GetItemSelectedImage(const wxTreeItemId& item) const
435 { return GetItemImage(item, wxTreeItemIcon_Selected); }
436 // set the selected item image
437 void SetItemSelectedImage(const wxTreeItemId& item, int image)
438 { SetItemImage(item, image, wxTreeItemIcon_Selected); }
439
440 // implementation
441 // --------------
442 virtual bool MSWCommand(WXUINT param, WXWORD id);
443 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
444
445 // override some base class virtuals
446 virtual bool SetBackgroundColour(const wxColour &colour);
447 virtual bool SetForegroundColour(const wxColour &colour);
448
449 // get/set the check state for the item (only for wxTR_MULTIPLE)
450 bool IsItemChecked(const wxTreeItemId& item) const;
451 void SetItemCheck(const wxTreeItemId& item, bool check = TRUE);
452
453 protected:
454 // SetImageList helper
455 void SetAnyImageList(wxImageList *imageList, int which);
456
457 wxTextCtrl *m_textCtrl; // used while editing the item label
458 wxImageList *m_imageListNormal, // images for tree elements
459 *m_imageListState; // special images for app defined states
460
461 private:
462 // the common part of all ctors
463 void Init();
464
465 // helper functions
466 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
467 inline void DoSetItem(wxTreeViewItem *tvItem);
468
469 inline void DoExpand(const wxTreeItemId& item, int flag);
470
471 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
472 wxTreeItemId hInsertAfter,
473 const wxString& text,
474 int image, int selectedImage,
475 wxTreeItemData *data);
476
477 int DoGetItemImageFromData(const wxTreeItemId& item,
478 wxTreeItemIcon which) const;
479 void DoSetItemImageFromData(const wxTreeItemId& item,
480 int image,
481 wxTreeItemIcon which) const;
482 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
483
484 void DeleteTextCtrl();
485
486 // support for additional item images
487 friend class wxTreeItemIndirectData;
488 void SetIndirectItemData(const wxTreeItemId& item,
489 wxTreeItemIndirectData *data);
490 bool HasIndirectData(const wxTreeItemId& item) const;
491
492 // the array storing all item ids which have indirect data
493 wxArrayTreeItemIds m_itemsWithIndirectData;
494
495 // the hash storing the items attributes (indexed by items ids)
496 wxHashTable m_attrs;
497
498 // TRUE if the hash above is not empty
499 bool m_hasAnyAttr;
500
501 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
502 };
503
504 #endif
505 // _WX_TREECTRL_H_