]> git.saurik.com Git - wxWidgets.git/blame - include/wx/msw/treectrl.h
add detachmenu, insert menu, replace menu
[wxWidgets.git] / include / wx / msw / treectrl.h
CommitLineData
2bda0e17
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.h
3// Purpose: wxTreeCtrl class
4// Author: Julian Smart
08b7c251 5// Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
2bda0e17
KB
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart and Markus Holzem
08b7c251 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
bbcdf8bc
JS
12#ifndef _WX_TREECTRL_H_
13#define _WX_TREECTRL_H_
2bda0e17 14
08b7c251
VZ
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
d427503c 18
2bda0e17 19#ifdef __GNUG__
08b7c251 20 #pragma interface "treectrl.h"
2bda0e17
KB
21#endif
22
ad5c34f3 23#include "wx/textctrl.h"
2899e223 24#include "wx/dynarray.h"
2bda0e17 25
d427503c
VZ
26#ifdef __GNUWIN32__
27 // Cygwin windows.h defines these identifiers
28 #undef GetFirstChild
29 #undef GetNextSibling
30#endif // Cygwin
31
08b7c251
VZ
32// the type for "untyped" data
33typedef long wxDataType;
34
35// fwd decl
06e38c8e
JS
36class WXDLLEXPORT wxImageList;
37struct WXDLLEXPORT wxTreeViewItem;
08b7c251
VZ
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
42class wxTreeItemData;
08b7c251
VZ
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.
51static const int wxTREE_HITTEST_ABOVE = 0x0001;
52 // below the client area.
53static const int wxTREE_HITTEST_BELOW = 0x0002;
54 // in the client area but below the last item.
55static const int wxTREE_HITTEST_NOWHERE = 0x0004;
56 // on the button associated with an item.
57static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0010;
58 // on the bitmap associated with an item.
59static const int wxTREE_HITTEST_ONITEMICON = 0x0020;
60 // in the indentation associated with an item.
61static const int wxTREE_HITTEST_ONITEMINDENT = 0x0040;
62 // on the label (string) associated with an item.
63static const int wxTREE_HITTEST_ONITEMLABEL = 0x0080;
64 // in the area to the right of an item.
65static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0100;
66 // on the state icon for a tree view item that is in a user-defined state.
67static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0200;
68 // to the right of the client area.
69static const int wxTREE_HITTEST_TOLEFT = 0x0400;
70 // to the left of the client area.
71static const int wxTREE_HITTEST_TORIGHT = 0x0800;
72 // anywhere on the item
73static 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)'
81enum
82{
2bda0e17
KB
83 wxTREE_EXPAND_EXPAND,
84 wxTREE_EXPAND_COLLAPSE,
85 wxTREE_EXPAND_COLLAPSE_RESET,
86 wxTREE_EXPAND_TOGGLE
87};
88
08b7c251 89// flags for deprecated InsertItem() variant
3197ed26
VZ
90#define wxTREE_INSERT_FIRST 0xFFFF0001
91#define wxTREE_INSERT_LAST 0xFFFF0002
2bda0e17 92
08b7c251
VZ
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// ----------------------------------------------------------------------------
98class WXDLLEXPORT wxTreeItemId
2bda0e17 99{
2bda0e17 100public:
08b7c251
VZ
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
08b7c251 115 // for wxTreeCtrl usage only
06e38c8e
JS
116 wxTreeItemId(WXHTREEITEM itemId) { m_itemId = (long)itemId; }
117 operator WXHTREEITEM() const { return (WXHTREEITEM)m_itemId; }
118
add28c55 119 void operator=(WXHTREEITEM item) { m_itemId = (long) item; }
08b7c251
VZ
120
121protected:
122 long m_itemId;
2bda0e17
KB
123};
124
9dfbf520
VZ
125WX_DEFINE_ARRAY(wxTreeItemId, wxArrayTreeItemIds);
126
08b7c251
VZ
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// ----------------------------------------------------------------------------
3a5a2f56 140class WXDLLEXPORT wxTreeItemData : private wxTreeItemId
08b7c251 141{
08b7c251 142public:
3a5a2f56 143 // default ctor/copy ctor/assignment operator are ok
08b7c251 144
3a5a2f56
VZ
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
08b7c251
VZ
148 virtual ~wxTreeItemData() { }
149
3a5a2f56
VZ
150 // accessors: set/get the item associated with this node
151 void SetId(const wxTreeItemId& id) { m_itemId = id; }
8a2c6ef8
JS
152#ifdef __WATCOMC__
153 const wxTreeItemId GetId() const { return m_itemId; }
154#else
a7e594b2 155 const wxTreeItemId& GetId() const { return (wxTreeItemId&) m_itemId; }
8a2c6ef8 156#endif
08b7c251
VZ
157};
158
159// ----------------------------------------------------------------------------
160// wxTreeCtrl
161// ----------------------------------------------------------------------------
162class WXDLLEXPORT wxTreeCtrl : public wxControl
2bda0e17 163{
092bddef 164public:
092bddef
VZ
165 // creation
166 // --------
08b7c251
VZ
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")
092bddef
VZ
175 {
176 Create(parent, id, pos, size, style, validator, name);
177 }
08b7c251
VZ
178
179 virtual ~wxTreeCtrl();
180
092bddef
VZ
181 bool Create(wxWindow *parent, wxWindowID id = -1,
182 const wxPoint& pos = wxDefaultPosition,
183 const wxSize& size = wxDefaultSize,
08b7c251 184 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
092bddef
VZ
185 const wxValidator& validator = wxDefaultValidator,
186 const wxString& name = "wxTreeCtrl");
08b7c251 187
092bddef
VZ
188 // accessors
189 // ---------
092bddef 190
08b7c251
VZ
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
cf724bce
RR
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
08b7c251
VZ
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
3a5a2f56
VZ
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
add28c55
VZ
256 // the item will be shown in bold
257 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
258
58a8ab88
JS
259 // the item will be shown with a drop highlight
260 void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = TRUE);
261
08b7c251
VZ
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;
add28c55
VZ
273 // is item text in bold font?
274 bool IsBold(const wxTreeItemId& item) const;
08b7c251 275
4832f7c0
VZ
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
9dfbf520
VZ
281 size_t GetChildrenCount(const wxTreeItemId& item,
282 bool recursively = TRUE) const;
4832f7c0 283
08b7c251
VZ
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
9dfbf520
VZ
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
08b7c251
VZ
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
06e38c8e 312 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
08b7c251 313 // get the next child
06e38c8e 314 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
978f38c2
VZ
315 // get the last child of this item - this method doesn't use cookies
316 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
08b7c251
VZ
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
092bddef
VZ
331 // operations
332 // ----------
08b7c251
VZ
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);
372edb9d
VZ
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);
08b7c251 363 // delete all items from the tree
372edb9d 364 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
08b7c251
VZ
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();
9dfbf520
VZ
378 // unselect all items (only makes sense for multiple selection control)
379 void UnselectAll();
08b7c251
VZ
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
95aabccc
VZ
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
08b7c251 409 //
95aabccc
VZ
410 // NB: this function is not reentrant and not MT-safe (FIXME)!
411 void SortChildren(const wxTreeItemId& item);
08b7c251
VZ
412
413 // helpers
414 // -------
415
add28c55
VZ
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);
08b7c251
VZ
426
427 // get the bounding rectangle of the item (or of its label only)
f7c832a7 428 bool GetBoundingRect(const wxTreeItemId& item,
16e93305 429 wxRect& rect,
08b7c251
VZ
430 bool textOnly = FALSE) const;
431
08b7c251
VZ
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 // --------------
fd3f686c 457 virtual bool MSWCommand(WXUINT param, WXWORD id);
a23fd0e1 458 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
08b7c251 459
9dfbf520
VZ
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
2bda0e17 464protected:
08b7c251
VZ
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
472private:
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
9dfbf520
VZ
488 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
489
08b7c251 490 void DeleteTextCtrl();
092bddef
VZ
491
492 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
2bda0e17
KB
493};
494
2bda0e17 495#endif
bbcdf8bc 496 // _WX_TREECTRL_H_