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