]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/treectrl.h
Mostly Mingw32/Cygwin corrections
[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 in as the last child of the parent
323 wxTreeItemId AppendItem(const wxTreeItemId& parent,
324 const wxString& text,
325 int image = -1, int selectedImage = -1,
326 wxTreeItemData *data = NULL);
327
328 // delete this item and associated data if any
329 void Delete(const wxTreeItemId& item);
330 // delete all children (but don't delete the item itself)
331 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
332 void DeleteChildren(const wxTreeItemId& item);
333 // delete all items from the tree
334 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
335 void DeleteAllItems();
336
337 // expand this item
338 void Expand(const wxTreeItemId& item);
339 // collapse the item without removing its children
340 void Collapse(const wxTreeItemId& item);
341 // collapse the item and remove all children
342 void CollapseAndReset(const wxTreeItemId& item);
343 // toggles the current state
344 void Toggle(const wxTreeItemId& item);
345
346 // remove the selection from currently selected item (if any)
347 void Unselect();
348 // unselect all items (only makes sense for multiple selection control)
349 void UnselectAll();
350 // select this item
351 void SelectItem(const wxTreeItemId& item);
352 // make sure this item is visible (expanding the parent item and/or
353 // scrolling to this item if necessary)
354 void EnsureVisible(const wxTreeItemId& item);
355 // scroll to this item (but don't expand its parent)
356 void ScrollTo(const wxTreeItemId& item);
357
358 // start editing the item label: this (temporarily) replaces the item
359 // with a one line edit control. The item will be selected if it hadn't
360 // been before. textCtrlClass parameter allows you to create an edit
361 // control of arbitrary user-defined class deriving from wxTextCtrl.
362 wxTextCtrl* EditLabel(const wxTreeItemId& item,
363 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
364 // returns the same pointer as StartEdit() if the item is being edited,
365 // NULL otherwise (it's assumed that no more than one item may be
366 // edited simultaneously)
367 wxTextCtrl* GetEditControl() const;
368 // end editing and accept or discard the changes to item label
369 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
370
371 // sorting
372 // this function is called to compare 2 items and should return -1, 0
373 // or +1 if the first item is less than, equal to or greater than the
374 // second one. The base class version performs alphabetic comparaison
375 // of item labels (GetText)
376 virtual int OnCompareItems(const wxTreeItemId& item1,
377 const wxTreeItemId& item2);
378 // sort the children of this item using OnCompareItems
379 //
380 // NB: this function is not reentrant and not MT-safe (FIXME)!
381 void SortChildren(const wxTreeItemId& item);
382
383 // helpers
384 // -------
385
386 // determine to which item (if any) belongs the given point (the
387 // coordinates specified are relative to the client area of tree ctrl)
388 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
389 // constants.
390 //
391 // The first function is more portable (because easier to implement
392 // on other platforms), but the second one returns some extra info.
393 wxTreeItemId HitTest(const wxPoint& point)
394 { int dummy; return HitTest(point, dummy); }
395 wxTreeItemId HitTest(const wxPoint& point, int& flags);
396
397 // get the bounding rectangle of the item (or of its label only)
398 bool GetBoundingRect(const wxTreeItemId& item,
399 wxRect& rect,
400 bool textOnly = FALSE) const;
401
402 // deprecated
403 // ----------
404
405 // these methods are deprecated and will be removed in future versions of
406 // wxWindows, they're here for compatibility only, don't use them in new
407 // code (the comments indicate why these methods are now useless and how to
408 // replace them)
409
410 // use Expand, Collapse, CollapseAndReset or Toggle
411 void ExpandItem(const wxTreeItemId& item, int action);
412
413 // use AddRoot, PrependItem or AppendItem
414 wxTreeItemId InsertItem(const wxTreeItemId& parent,
415 const wxString& text,
416 int image = -1, int selImage = -1,
417 long insertAfter = wxTREE_INSERT_LAST);
418
419 // use Set/GetImageList and Set/GetStateImageList
420 wxImageList *GetImageList(int) const
421 { return GetImageList(); }
422 void SetImageList(wxImageList *imageList, int)
423 { SetImageList(imageList); }
424
425 // use Set/GetItemImage directly
426 // get the selected item image
427 int GetItemSelectedImage(const wxTreeItemId& item) const
428 { return GetItemImage(item, wxTreeItemIcon_Selected); }
429 // set the selected item image
430 void SetItemSelectedImage(const wxTreeItemId& item, int image)
431 { SetItemImage(item, image, wxTreeItemIcon_Selected); }
432
433 // implementation
434 // --------------
435 virtual bool MSWCommand(WXUINT param, WXWORD id);
436 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
437
438 // get/set the check state for the item (only for wxTR_MULTIPLE)
439 bool IsItemChecked(const wxTreeItemId& item) const;
440 void SetItemCheck(const wxTreeItemId& item, bool check = TRUE);
441
442 protected:
443 // SetImageList helper
444 void SetAnyImageList(wxImageList *imageList, int which);
445
446 wxTextCtrl *m_textCtrl; // used while editing the item label
447 wxImageList *m_imageListNormal, // images for tree elements
448 *m_imageListState; // special images for app defined states
449
450 private:
451 // the common part of all ctors
452 void Init();
453
454 // helper functions
455 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
456 inline void DoSetItem(wxTreeViewItem *tvItem);
457
458 inline void DoExpand(const wxTreeItemId& item, int flag);
459
460 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
461 wxTreeItemId hInsertAfter,
462 const wxString& text,
463 int image, int selectedImage,
464 wxTreeItemData *data);
465
466 int DoGetItemImageFromData(const wxTreeItemId& item,
467 wxTreeItemIcon which) const;
468 void DoSetItemImageFromData(const wxTreeItemId& item,
469 int image,
470 wxTreeItemIcon which) const;
471 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
472
473 void DeleteTextCtrl();
474
475 // support for additional item images
476 friend class wxTreeItemIndirectData;
477 void SetIndirectItemData(const wxTreeItemId& item,
478 wxTreeItemIndirectData *data);
479 bool HasIndirectData(const wxTreeItemId& item) const;
480
481 // the array storing all item ids which have indirect data
482 wxArrayTreeItemIds m_itemsWithIndirectData;
483
484 // the hash storing the items attributes (indexed by items ids)
485 wxHashTable m_attrs;
486
487 // TRUE if the hash above is not empty
488 bool m_hasAnyAttr;
489
490 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
491 };
492
493 #endif
494 // _WX_TREECTRL_H_