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