]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/treectrl.h
fixed right click handling
[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 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 #include "wx/treebase.h"
26
27 #ifdef __GNUWIN32__
28 // Cygwin windows.h defines these identifiers
29 #undef GetFirstChild
30 #undef GetNextSibling
31 #endif // Cygwin
32
33 // the type for "untyped" data
34 typedef long wxDataType;
35
36 // fwd decl
37 class WXDLLEXPORT wxImageList;
38 class WXDLLEXPORT wxDragImage;
39 struct WXDLLEXPORT wxTreeViewItem;
40
41 // a callback function used for sorting tree items, it should return -1 if the
42 // first item precedes the second, +1 if the second precedes the first or 0 if
43 // they're equivalent
44 class wxTreeItemData;
45
46 // NB: all the following flags are for compatbility only and will be removed in the
47 // next versions
48
49 // flags for deprecated `Expand(int action)'
50 enum
51 {
52 wxTREE_EXPAND_EXPAND,
53 wxTREE_EXPAND_COLLAPSE,
54 wxTREE_EXPAND_COLLAPSE_RESET,
55 wxTREE_EXPAND_TOGGLE
56 };
57
58 // flags for deprecated InsertItem() variant
59 #define wxTREE_INSERT_FIRST 0xFFFF0001
60 #define wxTREE_INSERT_LAST 0xFFFF0002
61
62 // ----------------------------------------------------------------------------
63 // wxTreeCtrl
64 // ----------------------------------------------------------------------------
65 class WXDLLEXPORT wxTreeCtrl : public wxControl
66 {
67 public:
68 // creation
69 // --------
70 wxTreeCtrl() { Init(); }
71
72 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
73 const wxPoint& pos = wxDefaultPosition,
74 const wxSize& size = wxDefaultSize,
75 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
76 const wxValidator& validator = wxDefaultValidator,
77 const wxString& name = "wxTreeCtrl")
78 {
79 Create(parent, id, pos, size, style, validator, name);
80 }
81
82 virtual ~wxTreeCtrl();
83
84 bool Create(wxWindow *parent, wxWindowID id = -1,
85 const wxPoint& pos = wxDefaultPosition,
86 const wxSize& size = wxDefaultSize,
87 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
88 const wxValidator& validator = wxDefaultValidator,
89 const wxString& name = "wxTreeCtrl");
90
91 // accessors
92 // ---------
93
94 // get the total number of items in the control
95 size_t GetCount() const;
96
97 // indent is the number of pixels the children are indented relative to
98 // the parents position. SetIndent() also redraws the control
99 // immediately.
100 unsigned int GetIndent() const;
101 void SetIndent(unsigned int indent);
102
103 // spacing is the number of pixels between the start and the Text
104 // not implemented under wxMSW
105 unsigned int GetSpacing() const { return 18; } // return wxGTK default
106 void SetSpacing(unsigned int WXUNUSED(spacing)) { }
107
108 // image list: these functions allow to associate an image list with
109 // the control and retrieve it. Note that the control does _not_ delete
110 // the associated image list when it's deleted in order to allow image
111 // lists to be shared between different controls.
112 //
113 // The normal image list is for the icons which correspond to the
114 // normal tree item state (whether it is selected or not).
115 // Additionally, the application might choose to show a state icon
116 // which corresponds to an app-defined item state (for example,
117 // checked/unchecked) which are taken from the state image list.
118 wxImageList *GetImageList() const;
119 wxImageList *GetStateImageList() const;
120
121 void SetImageList(wxImageList *imageList);
122 void SetStateImageList(wxImageList *imageList);
123 void AssignImageList(wxImageList *imageList);
124 void AssignStateImageList(wxImageList *imageList);
125
126 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
127 // member functions of wxTreeItem because they must know the tree the item
128 // belongs to for Windows implementation and storing the pointer to
129 // wxTreeCtrl in each wxTreeItem is just too much waste.
130
131 // accessors
132 // ---------
133
134 // retrieve items label
135 wxString GetItemText(const wxTreeItemId& item) const;
136 // get one of the images associated with the item (normal by default)
137 int GetItemImage(const wxTreeItemId& item,
138 wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
139 // get the data associated with the item
140 wxTreeItemData *GetItemData(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 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 GetParent(const wxTreeItemId& item) const;
215
216 // for this enumeration function you must pass in a "cookie" parameter
217 // which is opaque for the application but is necessary for the library
218 // to make these functions reentrant (i.e. allow more than one
219 // enumeration on one and the same object simultaneously). Of course,
220 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
221 // the same!
222
223 // get the first child of this item
224 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& _cookie) const;
225 // get the next child
226 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& _cookie) const;
227 // get the last child of this item - this method doesn't use cookies
228 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
229
230 // get the next sibling of this item
231 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
232 // get the previous sibling
233 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
234
235 // get first visible item
236 wxTreeItemId GetFirstVisibleItem() const;
237 // get the next visible item: item must be visible itself!
238 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
239 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
240 // get the previous visible item: item must be visible itself!
241 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
242
243 // operations
244 // ----------
245
246 // add the root node to the tree
247 wxTreeItemId AddRoot(const wxString& text,
248 int image = -1, int selectedImage = -1,
249 wxTreeItemData *data = NULL);
250
251 // insert a new item in as the first child of the parent
252 wxTreeItemId PrependItem(const wxTreeItemId& parent,
253 const wxString& text,
254 int image = -1, int selectedImage = -1,
255 wxTreeItemData *data = NULL);
256
257 // insert a new item after a given one
258 wxTreeItemId InsertItem(const wxTreeItemId& parent,
259 const wxTreeItemId& idPrevious,
260 const wxString& text,
261 int image = -1, int selectedImage = -1,
262 wxTreeItemData *data = NULL);
263
264 // insert a new item before the one with the given index
265 wxTreeItemId InsertItem(const wxTreeItemId& parent,
266 size_t index,
267 const wxString& text,
268 int image = -1, int selectedImage = -1,
269 wxTreeItemData *data = NULL);
270
271 // insert a new item in as the last child of the parent
272 wxTreeItemId AppendItem(const wxTreeItemId& parent,
273 const wxString& text,
274 int image = -1, int selectedImage = -1,
275 wxTreeItemData *data = NULL);
276
277 // delete this item and associated data if any
278 void Delete(const wxTreeItemId& item);
279 // delete all children (but don't delete the item itself)
280 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
281 void DeleteChildren(const wxTreeItemId& item);
282 // delete all items from the tree
283 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
284 void DeleteAllItems();
285
286 // expand this item
287 void Expand(const wxTreeItemId& item);
288 // collapse the item without removing its children
289 void Collapse(const wxTreeItemId& item);
290 // collapse the item and remove all children
291 void CollapseAndReset(const wxTreeItemId& item);
292 // toggles the current state
293 void Toggle(const wxTreeItemId& item);
294
295 // remove the selection from currently selected item (if any)
296 void Unselect();
297 // unselect all items (only makes sense for multiple selection control)
298 void UnselectAll();
299 // select this item
300 void SelectItem(const wxTreeItemId& item);
301 // make sure this item is visible (expanding the parent item and/or
302 // scrolling to this item if necessary)
303 void EnsureVisible(const wxTreeItemId& item);
304 // scroll to this item (but don't expand its parent)
305 void ScrollTo(const wxTreeItemId& item);
306
307 // start editing the item label: this (temporarily) replaces the item
308 // with a one line edit control. The item will be selected if it hadn't
309 // been before. textCtrlClass parameter allows you to create an edit
310 // control of arbitrary user-defined class deriving from wxTextCtrl.
311 wxTextCtrl* EditLabel(const wxTreeItemId& item,
312 wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl));
313 // returns the same pointer as StartEdit() if the item is being edited,
314 // NULL otherwise (it's assumed that no more than one item may be
315 // edited simultaneously)
316 wxTextCtrl* GetEditControl() const;
317 // end editing and accept or discard the changes to item label
318 void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE);
319
320 // sorting
321 // this function is called to compare 2 items and should return -1, 0
322 // or +1 if the first item is less than, equal to or greater than the
323 // second one. The base class version performs alphabetic comparaison
324 // of item labels (GetText)
325 virtual int OnCompareItems(const wxTreeItemId& item1,
326 const wxTreeItemId& item2);
327 // sort the children of this item using OnCompareItems
328 //
329 // NB: this function is not reentrant and not MT-safe (FIXME)!
330 void SortChildren(const wxTreeItemId& item);
331
332 // helpers
333 // -------
334
335 // determine to which item (if any) belongs the given point (the
336 // coordinates specified are relative to the client area of tree ctrl)
337 // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx
338 // constants.
339 //
340 // The first function is more portable (because easier to implement
341 // on other platforms), but the second one returns some extra info.
342 wxTreeItemId HitTest(const wxPoint& point)
343 { int dummy; return HitTest(point, dummy); }
344 wxTreeItemId HitTest(const wxPoint& point, int& flags);
345
346 // get the bounding rectangle of the item (or of its label only)
347 bool GetBoundingRect(const wxTreeItemId& item,
348 wxRect& rect,
349 bool textOnly = FALSE) const;
350
351 // deprecated
352 // ----------
353
354 // these methods are deprecated and will be removed in future versions of
355 // wxWindows, they're here for compatibility only, don't use them in new
356 // code (the comments indicate why these methods are now useless and how to
357 // replace them)
358
359 // use Expand, Collapse, CollapseAndReset or Toggle
360 void ExpandItem(const wxTreeItemId& item, int action);
361
362 // use AddRoot, PrependItem or AppendItem
363 wxTreeItemId InsertItem(const wxTreeItemId& parent,
364 const wxString& text,
365 int image = -1, int selImage = -1,
366 long insertAfter = wxTREE_INSERT_LAST);
367
368 // use Set/GetImageList and Set/GetStateImageList
369 wxImageList *GetImageList(int) const
370 { return GetImageList(); }
371 void SetImageList(wxImageList *imageList, int)
372 { SetImageList(imageList); }
373
374 // use Set/GetItemImage directly
375 // get the selected item image
376 int GetItemSelectedImage(const wxTreeItemId& item) const
377 { return GetItemImage(item, wxTreeItemIcon_Selected); }
378 // set the selected item image
379 void SetItemSelectedImage(const wxTreeItemId& item, int image)
380 { SetItemImage(item, image, wxTreeItemIcon_Selected); }
381
382 // implementation
383 // --------------
384
385 virtual long MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
386 virtual bool MSWCommand(WXUINT param, WXWORD id);
387 virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
388
389 // override some base class virtuals
390 virtual bool SetBackgroundColour(const wxColour &colour);
391 virtual bool SetForegroundColour(const wxColour &colour);
392
393 // get/set the check state for the item (only for wxTR_MULTIPLE)
394 bool IsItemChecked(const wxTreeItemId& item) const;
395 void SetItemCheck(const wxTreeItemId& item, bool check = TRUE);
396
397 protected:
398 // SetImageList helper
399 void SetAnyImageList(wxImageList *imageList, int which);
400
401 wxTextCtrl *m_textCtrl; // used while editing the item label
402 wxImageList *m_imageListNormal, // images for tree elements
403 *m_imageListState; // special images for app defined states
404 bool m_ownsImageListNormal, m_ownsImageListState;
405
406 private:
407 // the common part of all ctors
408 void Init();
409
410 // helper functions
411 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
412 inline void DoSetItem(wxTreeViewItem *tvItem);
413
414 inline void DoExpand(const wxTreeItemId& item, int flag);
415
416 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
417 wxTreeItemId hInsertAfter,
418 const wxString& text,
419 int image, int selectedImage,
420 wxTreeItemData *data);
421
422 int DoGetItemImageFromData(const wxTreeItemId& item,
423 wxTreeItemIcon which) const;
424 void DoSetItemImageFromData(const wxTreeItemId& item,
425 int image,
426 wxTreeItemIcon which) const;
427 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
428
429 void DeleteTextCtrl();
430
431 // support for additional item images
432 friend class wxTreeItemIndirectData;
433 void SetIndirectItemData(const wxTreeItemId& item,
434 wxTreeItemIndirectData *data);
435 bool HasIndirectData(const wxTreeItemId& item) const;
436
437 // the array storing all item ids which have indirect data
438 wxArrayTreeItemIds m_itemsWithIndirectData;
439
440 // the hash storing the items attributes (indexed by items ids)
441 wxHashTable m_attrs;
442
443 // TRUE if the hash above is not empty
444 bool m_hasAnyAttr;
445
446 // used for dragging
447 wxDragImage *m_dragImage;
448
449 // the starting item for selection with Shift
450 WXHTREEITEM m_htSelStart;
451
452 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
453 };
454
455 #endif
456 // _WX_TREECTRL_H_