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