Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / include / wx / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/treectrl.h
3 // Purpose: wxTreeCtrl base header
4 // Author: Karsten Ballueder
5 // Modified by:
6 // Created:
7 // Copyright: (c) Karsten Ballueder
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_TREECTRL_H_BASE_
12 #define _WX_TREECTRL_H_BASE_
13
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17
18 #include "wx/defs.h"
19
20 #if wxUSE_TREECTRL
21
22 #include "wx/control.h"
23 #include "wx/treebase.h"
24 #include "wx/textctrl.h" // wxTextCtrl::ms_classinfo used through wxCLASSINFO macro
25
26 class WXDLLIMPEXP_FWD_CORE wxImageList;
27
28 // ----------------------------------------------------------------------------
29 // wxTreeCtrlBase
30 // ----------------------------------------------------------------------------
31
32 class WXDLLIMPEXP_CORE wxTreeCtrlBase : public wxControl
33 {
34 public:
35 wxTreeCtrlBase();
36 virtual ~wxTreeCtrlBase();
37
38 // accessors
39 // ---------
40
41 // get the total number of items in the control
42 virtual unsigned int GetCount() const = 0;
43
44 // indent is the number of pixels the children are indented relative to
45 // the parents position. SetIndent() also redraws the control
46 // immediately.
47 virtual unsigned int GetIndent() const = 0;
48 virtual void SetIndent(unsigned int indent) = 0;
49
50 // spacing is the number of pixels between the start and the Text
51 // (has no effect under wxMSW)
52 unsigned int GetSpacing() const { return m_spacing; }
53 void SetSpacing(unsigned int spacing) { m_spacing = spacing; }
54
55 // image list: these functions allow to associate an image list with
56 // the control and retrieve it. Note that the control does _not_ delete
57 // the associated image list when it's deleted in order to allow image
58 // lists to be shared between different controls.
59 //
60 // The normal image list is for the icons which correspond to the
61 // normal tree item state (whether it is selected or not).
62 // Additionally, the application might choose to show a state icon
63 // which corresponds to an app-defined item state (for example,
64 // checked/unchecked) which are taken from the state image list.
65 wxImageList *GetImageList() const { return m_imageListNormal; }
66 wxImageList *GetStateImageList() const { return m_imageListState; }
67
68 virtual void SetImageList(wxImageList *imageList) = 0;
69 virtual void SetStateImageList(wxImageList *imageList) = 0;
70 void AssignImageList(wxImageList *imageList)
71 {
72 SetImageList(imageList);
73 m_ownsImageListNormal = true;
74 }
75 void AssignStateImageList(wxImageList *imageList)
76 {
77 SetStateImageList(imageList);
78 m_ownsImageListState = true;
79 }
80
81
82 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
83 // member functions of wxTreeItem because they must know the tree the item
84 // belongs to for Windows implementation and storing the pointer to
85 // wxTreeCtrl in each wxTreeItem is just too much waste.
86
87 // accessors
88 // ---------
89
90 // retrieve items label
91 virtual wxString GetItemText(const wxTreeItemId& item) const = 0;
92 // get one of the images associated with the item (normal by default)
93 virtual int GetItemImage(const wxTreeItemId& item,
94 wxTreeItemIcon which = wxTreeItemIcon_Normal) const = 0;
95 // get the data associated with the item
96 virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const = 0;
97
98 // get the item's text colour
99 virtual wxColour GetItemTextColour(const wxTreeItemId& item) const = 0;
100
101 // get the item's background colour
102 virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const = 0;
103
104 // get the item's font
105 virtual wxFont GetItemFont(const wxTreeItemId& item) const = 0;
106
107 // get the items state
108 int GetItemState(const wxTreeItemId& item) const
109 {
110 return DoGetItemState(item);
111 }
112
113 // modifiers
114 // ---------
115
116 // set items label
117 virtual void SetItemText(const wxTreeItemId& item, const wxString& text) = 0;
118 // set one of the images associated with the item (normal by default)
119 virtual void SetItemImage(const wxTreeItemId& item,
120 int image,
121 wxTreeItemIcon which = wxTreeItemIcon_Normal) = 0;
122 // associate some data with the item
123 virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data) = 0;
124
125 // force appearance of [+] button near the item. This is useful to
126 // allow the user to expand the items which don't have any children now
127 // - but instead add them only when needed, thus minimizing memory
128 // usage and loading time.
129 virtual void SetItemHasChildren(const wxTreeItemId& item,
130 bool has = true) = 0;
131
132 // the item will be shown in bold
133 virtual void SetItemBold(const wxTreeItemId& item, bool bold = true) = 0;
134
135 // the item will be shown with a drop highlight
136 virtual void SetItemDropHighlight(const wxTreeItemId& item,
137 bool highlight = true) = 0;
138
139 // set the items text colour
140 virtual void SetItemTextColour(const wxTreeItemId& item,
141 const wxColour& col) = 0;
142
143 // set the items background colour
144 virtual void SetItemBackgroundColour(const wxTreeItemId& item,
145 const wxColour& col) = 0;
146
147 // set the items font (should be of the same height for all items)
148 virtual void SetItemFont(const wxTreeItemId& item,
149 const wxFont& font) = 0;
150
151 // set the items state (special state values: wxTREE_ITEMSTATE_NONE/NEXT/PREV)
152 void SetItemState(const wxTreeItemId& item, int state);
153
154 // item status inquiries
155 // ---------------------
156
157 // is the item visible (it might be outside the view or not expanded)?
158 virtual bool IsVisible(const wxTreeItemId& item) const = 0;
159 // does the item has any children?
160 virtual bool ItemHasChildren(const wxTreeItemId& item) const = 0;
161 // same as above
162 bool HasChildren(const wxTreeItemId& item) const
163 { return ItemHasChildren(item); }
164 // is the item expanded (only makes sense if HasChildren())?
165 virtual bool IsExpanded(const wxTreeItemId& item) const = 0;
166 // is this item currently selected (the same as has focus)?
167 virtual bool IsSelected(const wxTreeItemId& item) const = 0;
168 // is item text in bold font?
169 virtual bool IsBold(const wxTreeItemId& item) const = 0;
170 // is the control empty?
171 bool IsEmpty() const;
172
173
174 // number of children
175 // ------------------
176
177 // if 'recursively' is false, only immediate children count, otherwise
178 // the returned number is the number of all items in this branch
179 virtual size_t GetChildrenCount(const wxTreeItemId& item,
180 bool recursively = true) const = 0;
181
182 // navigation
183 // ----------
184
185 // wxTreeItemId.IsOk() will return false if there is no such item
186
187 // get the root tree item
188 virtual wxTreeItemId GetRootItem() const = 0;
189
190 // get the item currently selected (may return NULL if no selection)
191 virtual wxTreeItemId GetSelection() const = 0;
192
193 // get the items currently selected, return the number of such item
194 //
195 // NB: this operation is expensive and can take a long time for a
196 // control with a lot of items (~ O(number of items)).
197 virtual size_t GetSelections(wxArrayTreeItemIds& selections) const = 0;
198
199 // get the last item to be clicked when the control has wxTR_MULTIPLE
200 // equivalent to GetSelection() if not wxTR_MULTIPLE
201 virtual wxTreeItemId GetFocusedItem() const = 0;
202
203
204 // Clears the currently focused item
205 virtual void ClearFocusedItem() = 0;
206 // Sets the currently focused item. Item should be valid
207 virtual void SetFocusedItem(const wxTreeItemId& item) = 0;
208
209
210 // get the parent of this item (may return NULL if root)
211 virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const = 0;
212
213 // for this enumeration function you must pass in a "cookie" parameter
214 // which is opaque for the application but is necessary for the library
215 // to make these functions reentrant (i.e. allow more than one
216 // enumeration on one and the same object simultaneously). Of course,
217 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
218 // the same!
219
220 // get the first child of this item
221 virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
222 wxTreeItemIdValue& cookie) const = 0;
223 // get the next child
224 virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
225 wxTreeItemIdValue& cookie) const = 0;
226 // get the last child of this item - this method doesn't use cookies
227 virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const = 0;
228
229 // get the next sibling of this item
230 virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const = 0;
231 // get the previous sibling
232 virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const = 0;
233
234 // get first visible item
235 virtual wxTreeItemId GetFirstVisibleItem() const = 0;
236 // get the next visible item: item must be visible itself!
237 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
238 virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const = 0;
239 // get the previous visible item: item must be visible itself!
240 virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const = 0;
241
242 // operations
243 // ----------
244
245 // add the root node to the tree
246 virtual wxTreeItemId AddRoot(const wxString& text,
247 int image = -1, int selImage = -1,
248 wxTreeItemData *data = NULL) = 0;
249
250 // insert a new item in as the first child of the parent
251 wxTreeItemId PrependItem(const wxTreeItemId& parent,
252 const wxString& text,
253 int image = -1, int selImage = -1,
254 wxTreeItemData *data = NULL)
255 {
256 return DoInsertItem(parent, 0u, text, image, selImage, data);
257 }
258
259 // insert a new item after a given one
260 wxTreeItemId InsertItem(const wxTreeItemId& parent,
261 const wxTreeItemId& idPrevious,
262 const wxString& text,
263 int image = -1, int selImage = -1,
264 wxTreeItemData *data = NULL)
265 {
266 return DoInsertAfter(parent, idPrevious, text, image, selImage, data);
267 }
268
269 // insert a new item before the one with the given index
270 wxTreeItemId InsertItem(const wxTreeItemId& parent,
271 size_t pos,
272 const wxString& text,
273 int image = -1, int selImage = -1,
274 wxTreeItemData *data = NULL)
275 {
276 return DoInsertItem(parent, pos, text, image, selImage, data);
277 }
278
279 // insert a new item in as the last child of the parent
280 wxTreeItemId AppendItem(const wxTreeItemId& parent,
281 const wxString& text,
282 int image = -1, int selImage = -1,
283 wxTreeItemData *data = NULL)
284 {
285 return DoInsertItem(parent, (size_t)-1, text, image, selImage, data);
286 }
287
288 // delete this item and associated data if any
289 virtual void Delete(const wxTreeItemId& item) = 0;
290 // delete all children (but don't delete the item itself)
291 // NB: this won't send wxEVT_TREE_ITEM_DELETED events
292 virtual void DeleteChildren(const wxTreeItemId& item) = 0;
293 // delete all items from the tree
294 // NB: this won't send wxEVT_TREE_ITEM_DELETED events
295 virtual void DeleteAllItems() = 0;
296
297 // expand this item
298 virtual void Expand(const wxTreeItemId& item) = 0;
299 // expand the item and all its children recursively
300 void ExpandAllChildren(const wxTreeItemId& item);
301 // expand all items
302 void ExpandAll();
303 // collapse the item without removing its children
304 virtual void Collapse(const wxTreeItemId& item) = 0;
305 // collapse the item and all its children
306 void CollapseAllChildren(const wxTreeItemId& item);
307 // collapse all items
308 void CollapseAll();
309 // collapse the item and remove all children
310 virtual void CollapseAndReset(const wxTreeItemId& item) = 0;
311 // toggles the current state
312 virtual void Toggle(const wxTreeItemId& item) = 0;
313
314 // remove the selection from currently selected item (if any)
315 virtual void Unselect() = 0;
316 // unselect all items (only makes sense for multiple selection control)
317 virtual void UnselectAll() = 0;
318 // select this item
319 virtual void SelectItem(const wxTreeItemId& item, bool select = true) = 0;
320 // selects all (direct) children for given parent (only for
321 // multiselection controls)
322 virtual void SelectChildren(const wxTreeItemId& parent) = 0;
323 // unselect this item
324 void UnselectItem(const wxTreeItemId& item) { SelectItem(item, false); }
325 // toggle item selection
326 void ToggleItemSelection(const wxTreeItemId& item)
327 {
328 SelectItem(item, !IsSelected(item));
329 }
330
331 // make sure this item is visible (expanding the parent item and/or
332 // scrolling to this item if necessary)
333 virtual void EnsureVisible(const wxTreeItemId& item) = 0;
334 // scroll to this item (but don't expand its parent)
335 virtual void ScrollTo(const wxTreeItemId& item) = 0;
336
337 // start editing the item label: this (temporarily) replaces the item
338 // with a one line edit control. The item will be selected if it hadn't
339 // been before. textCtrlClass parameter allows you to create an edit
340 // control of arbitrary user-defined class deriving from wxTextCtrl.
341 virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
342 wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl)) = 0;
343 // returns the same pointer as StartEdit() if the item is being edited,
344 // NULL otherwise (it's assumed that no more than one item may be
345 // edited simultaneously)
346 virtual wxTextCtrl *GetEditControl() const = 0;
347 // end editing and accept or discard the changes to item label
348 virtual void EndEditLabel(const wxTreeItemId& item,
349 bool discardChanges = false) = 0;
350
351 // Enable or disable beep when incremental match doesn't find any item.
352 // Only implemented in the generic version currently.
353 virtual void EnableBellOnNoMatch(bool WXUNUSED(on) = true) { }
354
355 // sorting
356 // -------
357
358 // this function is called to compare 2 items and should return -1, 0
359 // or +1 if the first item is less than, equal to or greater than the
360 // second one. The base class version performs alphabetic comparaison
361 // of item labels (GetText)
362 virtual int OnCompareItems(const wxTreeItemId& item1,
363 const wxTreeItemId& item2)
364 {
365 return wxStrcmp(GetItemText(item1), GetItemText(item2));
366 }
367
368 // sort the children of this item using OnCompareItems
369 //
370 // NB: this function is not reentrant and not MT-safe (FIXME)!
371 virtual void SortChildren(const wxTreeItemId& item) = 0;
372
373 // items geometry
374 // --------------
375
376 // determine to which item (if any) belongs the given point (the
377 // coordinates specified are relative to the client area of tree ctrl)
378 // and, in the second variant, fill the flags parameter with a bitmask
379 // of wxTREE_HITTEST_xxx constants.
380 wxTreeItemId HitTest(const wxPoint& point) const
381 { int dummy; return DoTreeHitTest(point, dummy); }
382 wxTreeItemId HitTest(const wxPoint& point, int& flags) const
383 { return DoTreeHitTest(point, flags); }
384
385 // get the bounding rectangle of the item (or of its label only)
386 virtual bool GetBoundingRect(const wxTreeItemId& item,
387 wxRect& rect,
388 bool textOnly = false) const = 0;
389
390
391 // implementation
392 // --------------
393
394 virtual bool ShouldInheritColours() const { return false; }
395
396 // hint whether to calculate best size quickly or accurately
397 void SetQuickBestSize(bool q) { m_quickBestSize = q; }
398 bool GetQuickBestSize() const { return m_quickBestSize; }
399
400 protected:
401 virtual wxSize DoGetBestSize() const;
402
403 // common part of Get/SetItemState()
404 virtual int DoGetItemState(const wxTreeItemId& item) const = 0;
405 virtual void DoSetItemState(const wxTreeItemId& item, int state) = 0;
406
407 // common part of Append/Prepend/InsertItem()
408 //
409 // pos is the position at which to insert the item or (size_t)-1 to append
410 // it to the end
411 virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
412 size_t pos,
413 const wxString& text,
414 int image, int selImage,
415 wxTreeItemData *data) = 0;
416
417 // and this function implements overloaded InsertItem() taking wxTreeItemId
418 // (it can't be called InsertItem() as we'd have virtual function hiding
419 // problem in derived classes then)
420 virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
421 const wxTreeItemId& idPrevious,
422 const wxString& text,
423 int image = -1, int selImage = -1,
424 wxTreeItemData *data = NULL) = 0;
425
426 // real HitTest() implementation: again, can't be called just HitTest()
427 // because it's overloaded and so the non-virtual overload would be hidden
428 // (and can't be called DoHitTest() because this is already in wxWindow)
429 virtual wxTreeItemId DoTreeHitTest(const wxPoint& point,
430 int& flags) const = 0;
431
432
433 wxImageList *m_imageListNormal, // images for tree elements
434 *m_imageListState; // special images for app defined states
435 bool m_ownsImageListNormal,
436 m_ownsImageListState;
437
438 // spacing between left border and the text
439 unsigned int m_spacing;
440
441 // whether full or quick calculation is done in DoGetBestSize
442 bool m_quickBestSize;
443
444
445 private:
446 // Intercept Escape and Return keys to ensure that our in-place edit
447 // control always gets them before they're used for dialog navigation or
448 // anything else.
449 void OnCharHook(wxKeyEvent& event);
450
451
452 wxDECLARE_NO_COPY_CLASS(wxTreeCtrlBase);
453 };
454
455 // ----------------------------------------------------------------------------
456 // include the platform-dependent wxTreeCtrl class
457 // ----------------------------------------------------------------------------
458
459 #if defined(__WXUNIVERSAL__)
460 #include "wx/generic/treectlg.h"
461 #elif defined(__WXMSW__)
462 #include "wx/msw/treectrl.h"
463 #elif defined(__WXMOTIF__)
464 #include "wx/generic/treectlg.h"
465 #elif defined(__WXGTK__)
466 #include "wx/generic/treectlg.h"
467 #elif defined(__WXMAC__)
468 #include "wx/generic/treectlg.h"
469 #elif defined(__WXCOCOA__)
470 #include "wx/generic/treectlg.h"
471 #elif defined(__WXPM__)
472 #include "wx/generic/treectlg.h"
473 #endif
474
475 #endif // wxUSE_TREECTRL
476
477 #endif // _WX_TREECTRL_H_BASE_