]> git.saurik.com Git - wxWidgets.git/blob - include/wx/palmos/treectrl.h
added wxMOTIF_STR() macro casting away string literal constness for use with Motif...
[wxWidgets.git] / include / wx / palmos / treectrl.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/palmos/treectrl.h
3 // Purpose: wxTreeCtrl class
4 // Author: William Osborne - minimal working wxPalmOS port
5 // Modified by:
6 // Created: 10/13/04
7 // RCS-ID: $Id$
8 // Copyright: (c) William Osborne
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TREECTRL_H_
13 #define _WX_TREECTRL_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma interface "treectrl.h"
21 #endif
22
23 #if wxUSE_TREECTRL
24
25 #include "wx/textctrl.h"
26 #include "wx/dynarray.h"
27 #include "wx/treebase.h"
28 #include "wx/hashmap.h"
29
30 // fwd decl
31 class WXDLLEXPORT wxImageList;
32 class WXDLLEXPORT wxDragImage;
33 struct WXDLLEXPORT wxTreeViewItem;
34
35 // NB: all the following flags are for compatbility only and will be removed in the
36 // next versions
37
38 // flags for deprecated InsertItem() variant (their values are the same as of
39 // TVI_FIRST and TVI_LAST)
40 #define wxTREE_INSERT_FIRST 0xFFFF0001
41 #define wxTREE_INSERT_LAST 0xFFFF0002
42
43 // hash storing attributes for our items
44 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr);
45
46 // ----------------------------------------------------------------------------
47 // wxTreeCtrl
48 // ----------------------------------------------------------------------------
49
50 class WXDLLEXPORT wxTreeCtrl : public wxControl
51 {
52 public:
53 // creation
54 // --------
55 wxTreeCtrl() { Init(); }
56
57 wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
58 const wxPoint& pos = wxDefaultPosition,
59 const wxSize& size = wxDefaultSize,
60 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
61 const wxValidator& validator = wxDefaultValidator,
62 const wxString& name = wxTreeCtrlNameStr)
63 {
64 Create(parent, id, pos, size, style, validator, name);
65 }
66
67 virtual ~wxTreeCtrl();
68
69 bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
70 const wxPoint& pos = wxDefaultPosition,
71 const wxSize& size = wxDefaultSize,
72 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
73 const wxValidator& validator = wxDefaultValidator,
74 const wxString& name = wxTreeCtrlNameStr);
75
76 // accessors
77 // ---------
78
79 // get the total number of items in the control
80 size_t GetCount() const;
81
82 // indent is the number of pixels the children are indented relative to
83 // the parents position. SetIndent() also redraws the control
84 // immediately.
85 unsigned int GetIndent() const;
86 void SetIndent(unsigned int indent);
87
88 // spacing is the number of pixels between the start and the Text
89 unsigned int GetSpacing() const { return 18; } // return wxGTK default
90 void SetSpacing(unsigned int WXUNUSED(spacing)) { }
91
92 // image list: these functions allow to associate an image list with
93 // the control and retrieve it. Note that the control does _not_ delete
94 // the associated image list when it's deleted in order to allow image
95 // lists to be shared between different controls.
96 //
97 // The normal image list is for the icons which correspond to the
98 // normal tree item state (whether it is selected or not).
99 // Additionally, the application might choose to show a state icon
100 // which corresponds to an app-defined item state (for example,
101 // checked/unchecked) which are taken from the state image list.
102 wxImageList *GetImageList() const;
103 wxImageList *GetStateImageList() const;
104
105 void SetImageList(wxImageList *imageList);
106 void SetStateImageList(wxImageList *imageList);
107 void AssignImageList(wxImageList *imageList);
108 void AssignStateImageList(wxImageList *imageList);
109
110 // Functions to work with tree ctrl items. Unfortunately, they can _not_ be
111 // member functions of wxTreeItem because they must know the tree the item
112 // belongs to for Windows implementation and storing the pointer to
113 // wxTreeCtrl in each wxTreeItem is just too much waste.
114
115 // accessors
116 // ---------
117
118 // retrieve items label
119 wxString GetItemText(const wxTreeItemId& item) const;
120 // get one of the images associated with the item (normal by default)
121 int GetItemImage(const wxTreeItemId& item,
122 wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
123 // get the data associated with the item
124 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
125
126 // get the item's text colour
127 wxColour GetItemTextColour(const wxTreeItemId& item) const;
128
129 // get the item's background colour
130 wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
131
132 // get the item's font
133 wxFont GetItemFont(const wxTreeItemId& item) const;
134
135 // modifiers
136 // ---------
137
138 // set items label
139 void SetItemText(const wxTreeItemId& item, const wxString& text);
140 // get one of the images associated with the item (normal by default)
141 void SetItemImage(const wxTreeItemId& item, int image,
142 wxTreeItemIcon which = wxTreeItemIcon_Normal);
143 // associate some data with the item
144 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
145
146 // force appearance of [+] button near the item. This is useful to
147 // allow the user to expand the items which don't have any children now
148 // - but instead add them only when needed, thus minimizing memory
149 // usage and loading time.
150 void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
151
152 // the item will be shown in bold
153 void SetItemBold(const wxTreeItemId& item, bool bold = true);
154
155 // the item will be shown with a drop highlight
156 void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true);
157
158 // set the items text colour
159 void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
160
161 // set the items background colour
162 void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
163
164 // set the items font (should be of the same height for all items)
165 void SetItemFont(const wxTreeItemId& item, const wxFont& font);
166
167 // item status inquiries
168 // ---------------------
169
170 // is the item visible (it might be outside the view or not expanded)?
171 bool IsVisible(const wxTreeItemId& item) const;
172 // does the item has any children?
173 bool ItemHasChildren(const wxTreeItemId& item) const;
174 // is the item expanded (only makes sense if HasChildren())?
175 bool IsExpanded(const wxTreeItemId& item) const;
176 // is this item currently selected (the same as has focus)?
177 bool IsSelected(const wxTreeItemId& item) const;
178 // is item text in bold font?
179 bool IsBold(const wxTreeItemId& item) const;
180
181 // number of children
182 // ------------------
183
184 // if 'recursively' is false, only immediate children count, otherwise
185 // the returned number is the number of all items in this branch
186 size_t GetChildrenCount(const wxTreeItemId& item,
187 bool recursively = true) const;
188
189 // navigation
190 // ----------
191
192 // wxTreeItemId.IsOk() will return false if there is no such item
193
194 // get the root tree item
195 wxTreeItemId GetRootItem() const;
196
197 // get the item currently selected (may return NULL if no selection)
198 wxTreeItemId GetSelection() const;
199
200 // get the items currently selected, return the number of such item
201 //
202 // NB: this operation is expensive and can take a long time for a
203 // control with a lot of items (~ O(number of items)).
204 size_t GetSelections(wxArrayTreeItemIds& selections) const;
205
206 // get the parent of this item (may return NULL if root)
207 wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
208
209 // for this enumeration function you must pass in a "cookie" parameter
210 // which is opaque for the application but is necessary for the library
211 // to make these functions reentrant (i.e. allow more than one
212 // enumeration on one and the same object simultaneously). Of course,
213 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
214 // the same!
215
216 // get the first child of this item
217 wxTreeItemId GetFirstChild(const wxTreeItemId& item,
218 wxTreeItemIdValue& cookie) const;
219 // get the next child
220 wxTreeItemId GetNextChild(const wxTreeItemId& item,
221 wxTreeItemIdValue& cookie) const;
222 // get the last child of this item - this method doesn't use cookies
223 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
224
225 // get the next sibling of this item
226 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
227 // get the previous sibling
228 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
229
230 // get first visible item
231 wxTreeItemId GetFirstVisibleItem() const;
232 // get the next visible item: item must be visible itself!
233 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
234 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
235 // get the previous visible item: item must be visible itself!
236 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
237
238 // operations
239 // ----------
240
241 // add the root node to the tree
242 wxTreeItemId AddRoot(const wxString& text,
243 int image = -1, int selectedImage = -1,
244 wxTreeItemData *data = NULL);
245
246 // insert a new item in as the first child of the parent
247 wxTreeItemId PrependItem(const wxTreeItemId& parent,
248 const wxString& text,
249 int image = -1, int selectedImage = -1,
250 wxTreeItemData *data = NULL);
251
252 // insert a new item after a given one
253 wxTreeItemId InsertItem(const wxTreeItemId& parent,
254 const wxTreeItemId& idPrevious,
255 const wxString& text,
256 int image = -1, int selectedImage = -1,
257 wxTreeItemData *data = NULL);
258
259 // insert a new item before the one with the given index
260 wxTreeItemId InsertItem(const wxTreeItemId& parent,
261 size_t index,
262 const wxString& text,
263 int image = -1, int selectedImage = -1,
264 wxTreeItemData *data = NULL);
265
266 // insert a new item in as the last child of the parent
267 wxTreeItemId AppendItem(const wxTreeItemId& parent,
268 const wxString& text,
269 int image = -1, int selectedImage = -1,
270 wxTreeItemData *data = NULL);
271
272 // delete this item and associated data if any
273 void Delete(const wxTreeItemId& item);
274 // delete all children (but don't delete the item itself)
275 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
276 void DeleteChildren(const wxTreeItemId& item);
277 // delete all items from the tree
278 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
279 void DeleteAllItems();
280
281 // expand this item
282 void Expand(const wxTreeItemId& item);
283 // collapse the item without removing its children
284 void Collapse(const wxTreeItemId& item);
285 // collapse the item and remove all children
286 void CollapseAndReset(const wxTreeItemId& item);
287 // toggles the current state
288 void Toggle(const wxTreeItemId& item);
289
290 // remove the selection from currently selected item (if any)
291 void Unselect();
292 // unselect all items (only makes sense for multiple selection control)
293 void UnselectAll();
294 // select this item
295 void SelectItem(const wxTreeItemId& item, bool select = true);
296 // unselect this item
297 void UnselectItem(const wxTreeItemId& item);
298 // toggle item selection
299 void ToggleItemSelection(const wxTreeItemId& item);
300
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 // implementation
352 // --------------
353
354 virtual bool ShouldInheritColours() const { return false; }
355
356 virtual wxVisualAttributes GetDefaultAttributes() const
357 {
358 return GetClassDefaultAttributes(GetWindowVariant());
359 }
360
361 static wxVisualAttributes
362 GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
363
364 // override some base class virtuals
365 virtual bool SetBackgroundColour(const wxColour &colour);
366 virtual bool SetForegroundColour(const wxColour &colour);
367
368 // get/set the check state for the item (only for wxTR_MULTIPLE)
369 bool IsItemChecked(const wxTreeItemId& item) const;
370 void SetItemCheck(const wxTreeItemId& item, bool check = true);
371
372 // set/get the item state.image (state == -1 means cycle to the next one)
373 void SetState(const wxTreeItemId& node, int state);
374 int GetState(const wxTreeItemId& node);
375
376 protected:
377 // SetImageList helper
378 void SetAnyImageList(wxImageList *imageList, int which);
379
380 // refresh a single item
381 void RefreshItem(const wxTreeItemId& item);
382
383 wxTextCtrl *m_textCtrl; // used while editing the item label
384 wxImageList *m_imageListNormal, // images for tree elements
385 *m_imageListState; // special images for app defined states
386 bool m_ownsImageListNormal, m_ownsImageListState;
387
388 private:
389 // the common part of all ctors
390 void Init();
391
392 // helper functions
393 inline bool DoGetItem(wxTreeViewItem *tvItem) const;
394 inline void DoSetItem(wxTreeViewItem *tvItem);
395
396 inline void DoExpand(const wxTreeItemId& item, int flag);
397
398 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
399 wxTreeItemId hInsertAfter,
400 const wxString& text,
401 int image, int selectedImage,
402 wxTreeItemData *data);
403
404 int DoGetItemImageFromData(const wxTreeItemId& item,
405 wxTreeItemIcon which) const;
406 void DoSetItemImageFromData(const wxTreeItemId& item,
407 int image,
408 wxTreeItemIcon which) const;
409 void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel);
410
411 void DeleteTextCtrl();
412
413 // support for additional item images which we implement using
414 // wxTreeItemIndirectData technique
415 void SetIndirectItemData(const wxTreeItemId& item,
416 class wxTreeItemIndirectData *data);
417 bool HasIndirectData(const wxTreeItemId& item) const;
418 bool IsDataIndirect(wxTreeItemData *data) const
419 { return data && data->GetId().m_pItem == 0; }
420
421 // the hash storing the items attributes (indexed by item ids)
422 wxMapTreeAttr m_attrs;
423
424 // true if the hash above is not empty
425 bool m_hasAnyAttr;
426
427 // used for dragging
428 wxDragImage *m_dragImage;
429
430 // Virtual root item, if wxTR_HIDE_ROOT is set.
431 void* m_pVirtualRoot;
432
433 // the starting item for selection with Shift
434 wxTreeItemId m_htSelStart;
435
436 friend class wxTreeItemIndirectData;
437 friend class wxTreeSortHelper;
438
439 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
440 DECLARE_NO_COPY_CLASS(wxTreeCtrl)
441 };
442
443 #endif // wxUSE_TREECTRL
444
445 #endif // _WX_TREECTRL_H_