]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/treectrl.h
converted to 16 colors
[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 // ----------------------------------------------------------------------------
45 // constants
46 // ----------------------------------------------------------------------------
47
48 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
49 // where exactly the specified point is situated:
50 // above the client area.
51 static const int wxTREE_HITTEST_ABOVE = 0x0001;
52 // below the client area.
53 static const int wxTREE_HITTEST_BELOW = 0x0002;
54 // in the client area but below the last item.
55 static const int wxTREE_HITTEST_NOWHERE = 0x0004;
56 // on the button associated with an item.
57 static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0010;
58 // on the bitmap associated with an item.
59 static const int wxTREE_HITTEST_ONITEMICON = 0x0020;
60 // in the indentation associated with an item.
61 static const int wxTREE_HITTEST_ONITEMINDENT = 0x0040;
62 // on the label (string) associated with an item.
63 static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080;
64 // in the area to the right of an item.
65 static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0100;
66 // on the state icon for a tree view item that is in a user-defined state.
67 static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0200;
68 // to the right of the client area.
69 static const int wxTREE_HITTEST_TOLEFT = 0x0400;
70 // to the left of the client area.
71 static const int wxTREE_HITTEST_TORIGHT = 0x0800;
72 // anywhere on the item
73 static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
74 wxTREE_HITTEST_ONITEMLABEL |
75 wxTREE_HITTEST_ONITEMSTATEICON;
76
77 // NB: all the following flags are for compatbility only and will be removed in the
78 // next versions
79
80 // flags for deprecated `Expand(int action)'
81 enum
82 {
83 wxTREE_EXPAND_EXPAND,
84 wxTREE_EXPAND_COLLAPSE,
85 wxTREE_EXPAND_COLLAPSE_RESET,
86 wxTREE_EXPAND_TOGGLE
87 };
88
89 // flags for deprecated InsertItem() variant
90 #define wxTREE_INSERT_FIRST 0xFFFF0001
91 #define wxTREE_INSERT_LAST 0xFFFF0002
92
93 // ----------------------------------------------------------------------------
94 // wxTreeItemId identifies an element of the tree. In this implementation, it's
95 // just a trivial wrapper around Win32 HTREEITEM. It's opaque for the
96 // application.
97 // ----------------------------------------------------------------------------
98 class WXDLLEXPORT wxTreeItemId
99 {
100 public:
101 // ctors
102 // 0 is invalid value for HTREEITEM
103 wxTreeItemId() { m_itemId = 0; }
104
105 // default copy ctor/assignment operator are ok for us
106
107 // accessors
108 // is this a valid tree item?
109 bool IsOk() const { return m_itemId != 0; }
110
111 // conversion to/from either real (system-dependent) tree item id or
112 // to "long" which used to be the type for tree item ids in previous
113 // versions of wxWindows
114
115 // for wxTreeCtrl usage only
116 wxTreeItemId(WXHTREEITEM itemId) { m_itemId = (long)itemId; }
117 operator WXHTREEITEM() const { return (WXHTREEITEM)m_itemId; }
118
119 void operator=(WXHTREEITEM item) { m_itemId = (long) item; }
120
121 protected:
122 long m_itemId;
123 };
124
125 WX_DEFINE_ARRAY(wxTreeItemId, wxArrayTreeItemIds);
126
127 // ----------------------------------------------------------------------------
128 // wxTreeItemData is some (arbitrary) user class associated with some item. The
129 // main advantage of having this class (compared to old untyped interface) is
130 // that wxTreeItemData's are destroyed automatically by the tree and, as this
131 // class has virtual dtor, it means that the memory will be automatically
132 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
133 // the size of this class is critical: in any real application, each tree leaf
134 // will have wxTreeItemData associated with it and number of leaves may be
135 // quite big.
136 //
137 // Because the objects of this class are deleted by the tree, they should
138 // always be allocated on the heap!
139 // ----------------------------------------------------------------------------
140 class WXDLLEXPORT wxTreeItemData : private wxTreeItemId
141 {
142 public:
143 // default ctor/copy ctor/assignment operator are ok
144
145 // dtor is virtual and all the items are deleted by the tree control when
146 // it's deleted, so you normally don't have to care about freeing memory
147 // allocated in your wxTreeItemData-derived class
148 virtual ~wxTreeItemData() { }
149
150 // accessors: set/get the item associated with this node
151 void SetId(const wxTreeItemId& id) { m_itemId = id; }
152 #ifdef __WATCOMC__
153 const wxTreeItemId GetId() const { return m_itemId; }
154 #else
155 const wxTreeItemId& GetId() const { return (wxTreeItemId&) m_itemId; }
156 #endif
157 };
158
159 // ----------------------------------------------------------------------------
160 // wxTreeCtrl
161 // ----------------------------------------------------------------------------
162 class WXDLLEXPORT wxTreeCtrl : public wxControl
163 {
164 public:
165 // creation
166 // --------
167 wxTreeCtrl() { Init(); }
168
169 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
170 const wxPoint& pos = wxDefaultPosition,
171 const wxSize& size = wxDefaultSize,
172 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
173 const wxValidator& validator = wxDefaultValidator,
174 const wxString& name = "wxTreeCtrl")
175 {
176 Create(parent, id, pos, size, style, validator, name);
177 }
178
179 virtual ~wxTreeCtrl();
180
181 bool Create(wxWindow *parent, wxWindowID id = -1,
182 const wxPoint& pos = wxDefaultPosition,
183 const wxSize& size = wxDefaultSize,
184 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
185 const wxValidator& validator = wxDefaultValidator,
186 const wxString& name = "wxTreeCtrl");
187
188 // accessors
189 // ---------
190
191 // get the total number of items in the control
192 size_t GetCount() const;
193
194 // indent is the number of pixels the children are indented relative to
195 // the parents position. SetIndent() also redraws the control
196 // immediately.
197 unsigned int GetIndent() const;
198 void SetIndent(unsigned int indent);
199
200 // spacing is the number of pixels between the start and the Text
201 // not implemented under wxMSW
202 unsigned int GetSpacing() const { return 18; } // return wxGTK default
203 void SetSpacing(unsigned int ) {}
204
205 // image list: these functions allow to associate an image list with
206 // the control and retrieve it. Note that the control does _not_ delete
207 // the associated image list when it's deleted in order to allow image
208 // lists to be shared between different controls.
209 //
210 // The normal image list is for the icons which correspond to the
211 // normal tree item state (whether it is selected or not).
212 // Additionally, the application might choose to show a state icon
213 // which corresponds to an app-defined item state (for example,
214 // checked/unchecked) which are taken from the state image list.
215 wxImageList *GetImageList() const;
216 wxImageList *GetStateImageList() const;
217
218 void SetImageList(wxImageList *imageList);
219 void SetStateImageList(wxImageList *imageList);
220
221 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
222 // member functions of wxTreeItem because they must know the tree the item
223 // belongs to for Windows implementation and storing the pointer to
224 // wxTreeCtrl in each wxTreeItem is just too much waste.
225
226 // accessors
227 // ---------
228
229 // retrieve items label
230 wxString GetItemText(const wxTreeItemId& item) const;
231 // get the normal item image
232 int GetItemImage(const wxTreeItemId& item) const;
233 // get the selected item image
234 int GetItemSelectedImage(const wxTreeItemId& item) const;
235 // get the data associated with the item
236 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
237
238 // modifiers
239 // ---------
240
241 // set items label
242 void SetItemText(const wxTreeItemId& item, const wxString& text);
243 // set the normal item image
244 void SetItemImage(const wxTreeItemId& item, int image);
245 // set the selected item image
246 void SetItemSelectedImage(const wxTreeItemId& item, int image);
247 // associate some data with the item
248 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
249
250 // force appearance of [+] button near the item. This is useful to
251 // allow the user to expand the items which don't have any children now
252 // - but instead add them only when needed, thus minimizing memory
253 // usage and loading time.
254 void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE);
255
256 // the item will be shown in bold
257 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
258
259 // the item will be shown with a drop highlight
260 void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = TRUE);
261
262 // item status inquiries
263 // ---------------------
264
265 // is the item visible (it might be outside the view or not expanded)?
266 bool IsVisible(const wxTreeItemId& item) const;
267 // does the item has any children?
268 bool ItemHasChildren(const wxTreeItemId& item) const;
269 // is the item expanded (only makes sense if HasChildren())?
270 bool IsExpanded(const wxTreeItemId& item) const;
271 // is this item currently selected (the same as has focus)?
272 bool IsSelected(const wxTreeItemId& item) const;
273 // is item text in bold font?
274 bool IsBold(const wxTreeItemId& item) const;
275
276 // number of children
277 // ------------------
278
279 // if 'recursively' is FALSE, only immediate children count, otherwise
280 // the returned number is the number of all items in this branch
281 size_t GetChildrenCount(const wxTreeItemId& item,
282 bool recursively = TRUE) const;
283
284 // navigation
285 // ----------
286
287 // wxTreeItemId.IsOk() will return FALSE if there is no such item
288
289 // get the root tree item
290 wxTreeItemId GetRootItem() const;
291
292 // get the item currently selected (may return NULL if no selection)
293 wxTreeItemId GetSelection() const;
294
295 // get the items currently selected, return the number of such item
296 //
297 // NB: this operation is expensive and can take a long time for a
298 // control with a lot of items (~ O(number of items)).
299 size_t GetSelections(wxArrayTreeItemIds& selections) const;
300
301 // get the parent of this item (may return NULL if root)
302 wxTreeItemId GetParent(const wxTreeItemId& item) const;
303
304 // for this enumeration function you must pass in a "cookie" parameter
305 // which is opaque for the application but is necessary for the library
306 // to make these functions reentrant (i.e. allow more than one
307 // enumeration on one and the same object simultaneously). Of course,
308 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
309 // the same!
310
311 // get the first child of this item
312 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
313 // get the next child
314 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
315 // get the last child of this item - this method doesn't use cookies
316 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
317
318 // get the next sibling of this item
319 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
320 // get the previous sibling
321 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
322
323 // get first visible item
324 wxTreeItemId GetFirstVisibleItem() const;
325 // get the next visible item: item must be visible itself!
326 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
327 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
328 // get the previous visible item: item must be visible itself!
329 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
330
331 // operations
332 // ----------
333
334 // add the root node to the tree
335 wxTreeItemId AddRoot(const wxString& text,
336 int image = -1, int selectedImage = -1,
337 wxTreeItemData *data = NULL);
338
339 // insert a new item in as the first child of the parent
340 wxTreeItemId PrependItem(const wxTreeItemId& parent,
341 const wxString& text,
342 int image = -1, int selectedImage = -1,
343 wxTreeItemData *data = NULL);
344
345 // insert a new item after a given one
346 wxTreeItemId InsertItem(const wxTreeItemId& parent,
347 const wxTreeItemId& idPrevious,
348 const wxString& text,
349 int image = -1, int selectedImage = -1,
350 wxTreeItemData *data = NULL);
351
352 // insert a new item in as the last child of the parent
353 wxTreeItemId AppendItem(const wxTreeItemId& parent,
354 const wxString& text,
355 int image = -1, int selectedImage = -1,
356 wxTreeItemData *data = NULL);
357
358 // delete this item and associated data if any
359 void Delete(const wxTreeItemId& item);
360 // delete all children (but don't delete the item itself)
361 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
362 void DeleteChildren(const wxTreeItemId& item);
363 // delete all items from the tree
364 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
365 void DeleteAllItems();
366
367 // expand this item
368 void Expand(const wxTreeItemId& item);
369 // collapse the item without removing its children
370 void Collapse(const wxTreeItemId& item);
371 // collapse the item and remove all children
372 void CollapseAndReset(const wxTreeItemId& item);
373 // toggles the current state
374 void Toggle(const wxTreeItemId& item);
375
376 // remove the selection from currently selected item (if any)
377 void Unselect();
378 // unselect all items (only makes sense for multiple selection control)
379 void UnselectAll();
380 // select this item
381 void SelectItem(const wxTreeItemId& item);
382 // make sure this item is visible (expanding the parent item and/or
383 // scrolling to this item if necessary)
384 void EnsureVisible(const wxTreeItemId& item);
385 // scroll to this item (but don't expand its parent)
386 void ScrollTo(const wxTreeItemId& item);
387
388 // start editing the item label: this (temporarily) replaces the item
389 // with a one line edit control. The item will be selected if it hadn't
390 // been before. textCtrlClass parameter allows you to create an edit
391 // control of arbitrary user-defined class deriving from wxTextCtrl.
392 wxTextCtrl* EditLabel(const wxTreeItemId& item,
393 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
394 // returns the same pointer as StartEdit() if the item is being edited,
395 // NULL otherwise (it's assumed that no more than one item may be
396 // edited simultaneously)
397 wxTextCtrl* GetEditControl() const;
398 // end editing and accept or discard the changes to item label
399 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
400
401 // sorting
402 // this function is called to compare 2 items and should return -1, 0
403 // or +1 if the first item is less than, equal to or greater than the
404 // second one. The base class version performs alphabetic comparaison
405 // of item labels (GetText)
406 virtual int OnCompareItems(const wxTreeItemId& item1,
407 const wxTreeItemId& item2);
408 // sort the children of this item using OnCompareItems
409 //
410 // NB: this function is not reentrant and not MT-safe (FIXME)!
411 void SortChildren(const wxTreeItemId& item);
412
413 // helpers
414 // -------
415
416 // determine to which item (if any) belongs the given point (the
417 // coordinates specified are relative to the client area of tree ctrl)
418 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
419 // constants.
420 //
421 // The first function is more portable (because easier to implement
422 // on other platforms), but the second one returns some extra info.
423 wxTreeItemId HitTest(const wxPoint& point)
424 { int dummy; return HitTest(point, dummy); }
425 wxTreeItemId HitTest(const wxPoint& point, int& flags);
426
427 // get the bounding rectangle of the item (or of its label only)
428 bool GetBoundingRect(const wxTreeItemId& item,
429 wxRect& rect,
430 bool textOnly = FALSE) const;
431
432 // deprecated
433 // ----------
434
435 // these methods are deprecated and will be removed in future versions of
436 // wxWindows, they're here for compatibility only, don't use them in new
437 // code (the comments indicate why these methods are now useless and how to
438 // replace them)
439
440 // use Expand, Collapse, CollapseAndReset or Toggle
441 void ExpandItem(const wxTreeItemId& item, int action);
442
443 // use AddRoot, PrependItem or AppendItem
444 wxTreeItemId InsertItem(const wxTreeItemId& parent,
445 const wxString& text,
446 int image = -1, int selImage = -1,
447 long insertAfter = wxTREE_INSERT_LAST);
448
449 // use Set/GetImageList and Set/GetStateImageList
450 wxImageList *GetImageList(int) const
451 { return GetImageList(); }
452 void SetImageList(wxImageList *imageList, int)
453 { SetImageList(imageList); }
454
455 // implementation
456 // --------------
457 virtual bool MSWCommand(WXUINT param, WXWORD id);
458 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
459
460 // get/set the check state for the item (only for wxTR_MULTIPLE)
461 bool IsItemChecked(const wxTreeItemId& item) const;
462 void SetItemCheck(const wxTreeItemId& item, bool check = TRUE);
463
464 protected:
465 // SetImageList helper
466 void SetAnyImageList(wxImageList *imageList, int which);
467
468 wxTextCtrl* m_textCtrl; // used while editing the item label
469 wxImageList *m_imageListNormal, // images for tree elements
470 *m_imageListState; // special images for app defined states
471
472 private:
473 // the common part of all ctors
474 void Init();
475
476 // helper functions
477 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
478 inline void DoSetItem(wxTreeViewItem *tvItem);
479
480 inline void DoExpand(const wxTreeItemId& item, int flag);
481
482 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
483 wxTreeItemId hInsertAfter,
484 const wxString& text,
485 int image, int selectedImage,
486 wxTreeItemData *data);
487
488 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
489
490 void DeleteTextCtrl();
491
492 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
493 };
494
495 #endif
496 // _WX_TREECTRL_H_