]>
Commit | Line | Data |
---|---|---|
ffecfa5a JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: wx/palmos/treectrl.h | |
3 | // Purpose: wxTreeCtrl class | |
4 | // Author: William Osborne | |
5 | // Modified by: | |
6 | // Created: 10/13/04 | |
5445a26c | 7 | // RCS-ID: $Id: |
ffecfa5a JS |
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 | #ifdef __GNUWIN32__ | |
31 | // Cygwin windows.h defines these identifiers | |
32 | #undef GetFirstChild | |
33 | #undef GetNextSibling | |
34 | #endif // Cygwin | |
35 | ||
36 | // fwd decl | |
37 | class WXDLLEXPORT wxImageList; | |
38 | class WXDLLEXPORT wxDragImage; | |
39 | struct WXDLLEXPORT wxTreeViewItem; | |
40 | ||
41 | // NB: all the following flags are for compatbility only and will be removed in the | |
42 | // next versions | |
43 | ||
44 | // flags for deprecated `Expand(int action)' | |
45 | enum | |
46 | { | |
47 | wxTREE_EXPAND_EXPAND, | |
48 | wxTREE_EXPAND_COLLAPSE, | |
49 | wxTREE_EXPAND_COLLAPSE_RESET, | |
50 | wxTREE_EXPAND_TOGGLE | |
51 | }; | |
52 | ||
53 | // flags for deprecated InsertItem() variant (their values are the same as of | |
54 | // TVI_FIRST and TVI_LAST) | |
55 | #define wxTREE_INSERT_FIRST 0xFFFF0001 | |
56 | #define wxTREE_INSERT_LAST 0xFFFF0002 | |
57 | ||
58 | // hash storing attributes for our items | |
59 | WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr); | |
60 | ||
61 | // ---------------------------------------------------------------------------- | |
62 | // wxTreeCtrl | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
65 | class WXDLLEXPORT wxTreeCtrl : public wxControl | |
66 | { | |
67 | public: | |
68 | // creation | |
69 | // -------- | |
70 | wxTreeCtrl() { Init(); } | |
71 | ||
5445a26c | 72 | wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY, |
ffecfa5a JS |
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 = wxTreeCtrlNameStr) | |
78 | { | |
79 | Create(parent, id, pos, size, style, validator, name); | |
80 | } | |
81 | ||
82 | virtual ~wxTreeCtrl(); | |
83 | ||
5445a26c | 84 | bool Create(wxWindow *parent, wxWindowID id = wxID_ANY, |
ffecfa5a JS |
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 = wxTreeCtrlNameStr); | |
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 | |
ffecfa5a JS |
104 | unsigned int GetSpacing() const { return 18; } // return wxGTK default |
105 | void SetSpacing(unsigned int WXUNUSED(spacing)) { } | |
106 | ||
107 | // image list: these functions allow to associate an image list with | |
108 | // the control and retrieve it. Note that the control does _not_ delete | |
109 | // the associated image list when it's deleted in order to allow image | |
110 | // lists to be shared between different controls. | |
111 | // | |
112 | // The normal image list is for the icons which correspond to the | |
113 | // normal tree item state (whether it is selected or not). | |
114 | // Additionally, the application might choose to show a state icon | |
115 | // which corresponds to an app-defined item state (for example, | |
116 | // checked/unchecked) which are taken from the state image list. | |
117 | wxImageList *GetImageList() const; | |
118 | wxImageList *GetStateImageList() const; | |
119 | ||
120 | void SetImageList(wxImageList *imageList); | |
121 | void SetStateImageList(wxImageList *imageList); | |
122 | void AssignImageList(wxImageList *imageList); | |
123 | void AssignStateImageList(wxImageList *imageList); | |
124 | ||
125 | // Functions to work with tree ctrl items. Unfortunately, they can _not_ be | |
126 | // member functions of wxTreeItem because they must know the tree the item | |
127 | // belongs to for Windows implementation and storing the pointer to | |
128 | // wxTreeCtrl in each wxTreeItem is just too much waste. | |
129 | ||
130 | // accessors | |
131 | // --------- | |
132 | ||
133 | // retrieve items label | |
134 | wxString GetItemText(const wxTreeItemId& item) const; | |
135 | // get one of the images associated with the item (normal by default) | |
136 | int GetItemImage(const wxTreeItemId& item, | |
137 | wxTreeItemIcon which = wxTreeItemIcon_Normal) const; | |
138 | // get the data associated with the item | |
139 | wxTreeItemData *GetItemData(const wxTreeItemId& item) const; | |
140 | ||
141 | // get the item's text colour | |
142 | wxColour GetItemTextColour(const wxTreeItemId& item) const; | |
143 | ||
144 | // get the item's background colour | |
145 | wxColour GetItemBackgroundColour(const wxTreeItemId& item) const; | |
146 | ||
147 | // get the item's font | |
148 | wxFont GetItemFont(const wxTreeItemId& item) const; | |
149 | ||
150 | // modifiers | |
151 | // --------- | |
152 | ||
153 | // set items label | |
154 | void SetItemText(const wxTreeItemId& item, const wxString& text); | |
155 | // get one of the images associated with the item (normal by default) | |
156 | void SetItemImage(const wxTreeItemId& item, int image, | |
157 | wxTreeItemIcon which = wxTreeItemIcon_Normal); | |
158 | // associate some data with the item | |
159 | void SetItemData(const wxTreeItemId& item, wxTreeItemData *data); | |
160 | ||
161 | // force appearance of [+] button near the item. This is useful to | |
162 | // allow the user to expand the items which don't have any children now | |
163 | // - but instead add them only when needed, thus minimizing memory | |
164 | // usage and loading time. | |
5445a26c | 165 | void SetItemHasChildren(const wxTreeItemId& item, bool has = true); |
ffecfa5a JS |
166 | |
167 | // the item will be shown in bold | |
5445a26c | 168 | void SetItemBold(const wxTreeItemId& item, bool bold = true); |
ffecfa5a JS |
169 | |
170 | // the item will be shown with a drop highlight | |
5445a26c | 171 | void SetItemDropHighlight(const wxTreeItemId& item, bool highlight = true); |
ffecfa5a JS |
172 | |
173 | // set the items text colour | |
174 | void SetItemTextColour(const wxTreeItemId& item, const wxColour& col); | |
175 | ||
176 | // set the items background colour | |
177 | void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col); | |
178 | ||
179 | // set the items font (should be of the same height for all items) | |
180 | void SetItemFont(const wxTreeItemId& item, const wxFont& font); | |
181 | ||
182 | // item status inquiries | |
183 | // --------------------- | |
184 | ||
185 | // is the item visible (it might be outside the view or not expanded)? | |
186 | bool IsVisible(const wxTreeItemId& item) const; | |
187 | // does the item has any children? | |
188 | bool ItemHasChildren(const wxTreeItemId& item) const; | |
189 | // is the item expanded (only makes sense if HasChildren())? | |
190 | bool IsExpanded(const wxTreeItemId& item) const; | |
191 | // is this item currently selected (the same as has focus)? | |
192 | bool IsSelected(const wxTreeItemId& item) const; | |
193 | // is item text in bold font? | |
194 | bool IsBold(const wxTreeItemId& item) const; | |
195 | ||
196 | // number of children | |
197 | // ------------------ | |
198 | ||
5445a26c | 199 | // if 'recursively' is false, only immediate children count, otherwise |
ffecfa5a JS |
200 | // the returned number is the number of all items in this branch |
201 | size_t GetChildrenCount(const wxTreeItemId& item, | |
5445a26c | 202 | bool recursively = true) const; |
ffecfa5a JS |
203 | |
204 | // navigation | |
205 | // ---------- | |
206 | ||
5445a26c | 207 | // wxTreeItemId.IsOk() will return false if there is no such item |
ffecfa5a JS |
208 | |
209 | // get the root tree item | |
210 | wxTreeItemId GetRootItem() const; | |
211 | ||
212 | // get the item currently selected (may return NULL if no selection) | |
213 | wxTreeItemId GetSelection() const; | |
214 | ||
215 | // get the items currently selected, return the number of such item | |
216 | // | |
217 | // NB: this operation is expensive and can take a long time for a | |
218 | // control with a lot of items (~ O(number of items)). | |
219 | size_t GetSelections(wxArrayTreeItemIds& selections) const; | |
220 | ||
221 | // get the parent of this item (may return NULL if root) | |
222 | wxTreeItemId GetItemParent(const wxTreeItemId& item) const; | |
223 | ||
ffecfa5a JS |
224 | // for this enumeration function you must pass in a "cookie" parameter |
225 | // which is opaque for the application but is necessary for the library | |
226 | // to make these functions reentrant (i.e. allow more than one | |
227 | // enumeration on one and the same object simultaneously). Of course, | |
228 | // the "cookie" passed to GetFirstChild() and GetNextChild() should be | |
229 | // the same! | |
230 | ||
231 | // get the first child of this item | |
232 | wxTreeItemId GetFirstChild(const wxTreeItemId& item, | |
233 | wxTreeItemIdValue& cookie) const; | |
234 | // get the next child | |
235 | wxTreeItemId GetNextChild(const wxTreeItemId& item, | |
236 | wxTreeItemIdValue& cookie) const; | |
237 | // get the last child of this item - this method doesn't use cookies | |
238 | wxTreeItemId GetLastChild(const wxTreeItemId& item) const; | |
239 | ||
240 | // get the next sibling of this item | |
241 | wxTreeItemId GetNextSibling(const wxTreeItemId& item) const; | |
242 | // get the previous sibling | |
243 | wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const; | |
244 | ||
245 | // get first visible item | |
246 | wxTreeItemId GetFirstVisibleItem() const; | |
247 | // get the next visible item: item must be visible itself! | |
248 | // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem() | |
249 | wxTreeItemId GetNextVisible(const wxTreeItemId& item) const; | |
250 | // get the previous visible item: item must be visible itself! | |
251 | wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const; | |
252 | ||
253 | // operations | |
254 | // ---------- | |
255 | ||
256 | // add the root node to the tree | |
257 | wxTreeItemId AddRoot(const wxString& text, | |
258 | int image = -1, int selectedImage = -1, | |
259 | wxTreeItemData *data = NULL); | |
260 | ||
261 | // insert a new item in as the first child of the parent | |
262 | wxTreeItemId PrependItem(const wxTreeItemId& parent, | |
263 | const wxString& text, | |
264 | int image = -1, int selectedImage = -1, | |
265 | wxTreeItemData *data = NULL); | |
266 | ||
267 | // insert a new item after a given one | |
268 | wxTreeItemId InsertItem(const wxTreeItemId& parent, | |
269 | const wxTreeItemId& idPrevious, | |
270 | const wxString& text, | |
271 | int image = -1, int selectedImage = -1, | |
272 | wxTreeItemData *data = NULL); | |
273 | ||
274 | // insert a new item before the one with the given index | |
275 | wxTreeItemId InsertItem(const wxTreeItemId& parent, | |
276 | size_t index, | |
277 | const wxString& text, | |
278 | int image = -1, int selectedImage = -1, | |
279 | wxTreeItemData *data = NULL); | |
280 | ||
281 | // insert a new item in as the last child of the parent | |
282 | wxTreeItemId AppendItem(const wxTreeItemId& parent, | |
283 | const wxString& text, | |
284 | int image = -1, int selectedImage = -1, | |
285 | wxTreeItemData *data = NULL); | |
286 | ||
287 | // delete this item and associated data if any | |
288 | void Delete(const wxTreeItemId& item); | |
289 | // delete all children (but don't delete the item itself) | |
290 | // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events | |
291 | void DeleteChildren(const wxTreeItemId& item); | |
292 | // delete all items from the tree | |
293 | // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events | |
294 | void DeleteAllItems(); | |
295 | ||
296 | // expand this item | |
297 | void Expand(const wxTreeItemId& item); | |
298 | // collapse the item without removing its children | |
299 | void Collapse(const wxTreeItemId& item); | |
300 | // collapse the item and remove all children | |
301 | void CollapseAndReset(const wxTreeItemId& item); | |
302 | // toggles the current state | |
303 | void Toggle(const wxTreeItemId& item); | |
304 | ||
305 | // remove the selection from currently selected item (if any) | |
306 | void Unselect(); | |
307 | // unselect all items (only makes sense for multiple selection control) | |
308 | void UnselectAll(); | |
309 | // select this item | |
310 | void SelectItem(const wxTreeItemId& item, bool select = true); | |
311 | // unselect this item | |
312 | void UnselectItem(const wxTreeItemId& item); | |
313 | // toggle item selection | |
314 | void ToggleItemSelection(const wxTreeItemId& item); | |
315 | ||
316 | // make sure this item is visible (expanding the parent item and/or | |
317 | // scrolling to this item if necessary) | |
318 | void EnsureVisible(const wxTreeItemId& item); | |
319 | // scroll to this item (but don't expand its parent) | |
320 | void ScrollTo(const wxTreeItemId& item); | |
321 | ||
322 | // start editing the item label: this (temporarily) replaces the item | |
323 | // with a one line edit control. The item will be selected if it hadn't | |
324 | // been before. textCtrlClass parameter allows you to create an edit | |
325 | // control of arbitrary user-defined class deriving from wxTextCtrl. | |
326 | wxTextCtrl* EditLabel(const wxTreeItemId& item, | |
327 | wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl)); | |
328 | // returns the same pointer as StartEdit() if the item is being edited, | |
329 | // NULL otherwise (it's assumed that no more than one item may be | |
330 | // edited simultaneously) | |
331 | wxTextCtrl* GetEditControl() const; | |
332 | // end editing and accept or discard the changes to item label | |
5445a26c | 333 | void EndEditLabel(const wxTreeItemId& item, bool discardChanges = false); |
ffecfa5a JS |
334 | |
335 | // sorting | |
336 | // this function is called to compare 2 items and should return -1, 0 | |
337 | // or +1 if the first item is less than, equal to or greater than the | |
338 | // second one. The base class version performs alphabetic comparaison | |
339 | // of item labels (GetText) | |
340 | virtual int OnCompareItems(const wxTreeItemId& item1, | |
341 | const wxTreeItemId& item2); | |
342 | // sort the children of this item using OnCompareItems | |
343 | // | |
344 | // NB: this function is not reentrant and not MT-safe (FIXME)! | |
345 | void SortChildren(const wxTreeItemId& item); | |
346 | ||
347 | // helpers | |
348 | // ------- | |
349 | ||
350 | // determine to which item (if any) belongs the given point (the | |
351 | // coordinates specified are relative to the client area of tree ctrl) | |
352 | // and fill the flags parameter with a bitmask of wxTREE_HITTEST_xxx | |
353 | // constants. | |
354 | // | |
355 | // The first function is more portable (because easier to implement | |
356 | // on other platforms), but the second one returns some extra info. | |
357 | wxTreeItemId HitTest(const wxPoint& point) | |
358 | { int dummy; return HitTest(point, dummy); } | |
359 | wxTreeItemId HitTest(const wxPoint& point, int& flags); | |
360 | ||
361 | // get the bounding rectangle of the item (or of its label only) | |
362 | bool GetBoundingRect(const wxTreeItemId& item, | |
363 | wxRect& rect, | |
5445a26c | 364 | bool textOnly = false) const; |
ffecfa5a JS |
365 | |
366 | // deprecated | |
367 | // ---------- | |
368 | ||
369 | #if WXWIN_COMPATIBILITY_2_4 | |
370 | // these methods are deprecated and will be removed in future versions of | |
371 | // wxWidgets, they're here for compatibility only, don't use them in new | |
372 | // code (the comments indicate why these methods are now useless and how to | |
373 | // replace them) | |
374 | ||
375 | // use Expand, Collapse, CollapseAndReset or Toggle | |
376 | wxDEPRECATED( void ExpandItem(const wxTreeItemId& item, int action) ); | |
377 | ||
378 | // use AddRoot, PrependItem or AppendItem | |
379 | wxDEPRECATED( wxTreeItemId InsertItem(const wxTreeItemId& parent, | |
380 | const wxString& text, | |
381 | int image = -1, int selImage = -1, | |
382 | long insertAfter = wxTREE_INSERT_LAST) ); | |
383 | ||
384 | // use Set/GetImageList and Set/GetStateImageList | |
385 | wxImageList *GetImageList(int) const { return GetImageList(); } | |
386 | void SetImageList(wxImageList *imageList, int) { SetImageList(imageList); } | |
387 | ||
388 | // use Set/GetItemImage directly | |
389 | int GetItemSelectedImage(const wxTreeItemId& item) const | |
390 | { return GetItemImage(item, wxTreeItemIcon_Selected); } | |
391 | void SetItemSelectedImage(const wxTreeItemId& item, int image) | |
392 | { SetItemImage(item, image, wxTreeItemIcon_Selected); } | |
393 | ||
394 | // use the versions taking wxTreeItemIdValue cookies | |
395 | wxDEPRECATED( wxTreeItemId GetFirstChild(const wxTreeItemId& item, | |
396 | long& cookie) const ); | |
397 | wxDEPRECATED( wxTreeItemId GetNextChild(const wxTreeItemId& item, | |
398 | long& cookie) const ); | |
399 | #endif // WXWIN_COMPATIBILITY_2_4 | |
400 | ||
401 | ||
402 | // implementation | |
403 | // -------------- | |
404 | ||
405 | virtual bool ShouldInheritColours() const { return false; } | |
406 | ||
407 | virtual wxVisualAttributes GetDefaultAttributes() const | |
408 | { | |
409 | return GetClassDefaultAttributes(GetWindowVariant()); | |
410 | } | |
411 | ||
412 | static wxVisualAttributes | |
413 | GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL); | |
414 | ||
ffecfa5a JS |
415 | // override some base class virtuals |
416 | virtual bool SetBackgroundColour(const wxColour &colour); | |
417 | virtual bool SetForegroundColour(const wxColour &colour); | |
418 | ||
419 | // get/set the check state for the item (only for wxTR_MULTIPLE) | |
420 | bool IsItemChecked(const wxTreeItemId& item) const; | |
5445a26c | 421 | void SetItemCheck(const wxTreeItemId& item, bool check = true); |
ffecfa5a JS |
422 | |
423 | // set/get the item state.image (state == -1 means cycle to the next one) | |
424 | void SetState(const wxTreeItemId& node, int state); | |
425 | int GetState(const wxTreeItemId& node); | |
426 | ||
427 | protected: | |
428 | // SetImageList helper | |
429 | void SetAnyImageList(wxImageList *imageList, int which); | |
430 | ||
431 | // refresh a single item | |
432 | void RefreshItem(const wxTreeItemId& item); | |
433 | ||
434 | wxTextCtrl *m_textCtrl; // used while editing the item label | |
435 | wxImageList *m_imageListNormal, // images for tree elements | |
436 | *m_imageListState; // special images for app defined states | |
437 | bool m_ownsImageListNormal, m_ownsImageListState; | |
438 | ||
439 | private: | |
440 | // the common part of all ctors | |
441 | void Init(); | |
442 | ||
443 | // helper functions | |
444 | inline bool DoGetItem(wxTreeViewItem *tvItem) const; | |
445 | inline void DoSetItem(wxTreeViewItem *tvItem); | |
446 | ||
447 | inline void DoExpand(const wxTreeItemId& item, int flag); | |
448 | ||
449 | wxTreeItemId DoInsertItem(const wxTreeItemId& parent, | |
450 | wxTreeItemId hInsertAfter, | |
451 | const wxString& text, | |
452 | int image, int selectedImage, | |
453 | wxTreeItemData *data); | |
454 | ||
455 | int DoGetItemImageFromData(const wxTreeItemId& item, | |
456 | wxTreeItemIcon which) const; | |
457 | void DoSetItemImageFromData(const wxTreeItemId& item, | |
458 | int image, | |
459 | wxTreeItemIcon which) const; | |
460 | void DoSetItemImages(const wxTreeItemId& item, int image, int imageSel); | |
461 | ||
462 | void DeleteTextCtrl(); | |
463 | ||
464 | // support for additional item images which we implement using | |
465 | // wxTreeItemIndirectData technique - see the comments in msw/treectrl.cpp | |
466 | void SetIndirectItemData(const wxTreeItemId& item, | |
467 | class wxTreeItemIndirectData *data); | |
468 | bool HasIndirectData(const wxTreeItemId& item) const; | |
469 | bool IsDataIndirect(wxTreeItemData *data) const | |
470 | { return data && data->GetId().m_pItem == 0; } | |
471 | ||
472 | // the hash storing the items attributes (indexed by item ids) | |
473 | wxMapTreeAttr m_attrs; | |
474 | ||
5445a26c | 475 | // true if the hash above is not empty |
ffecfa5a JS |
476 | bool m_hasAnyAttr; |
477 | ||
478 | // used for dragging | |
479 | wxDragImage *m_dragImage; | |
480 | ||
481 | // Virtual root item, if wxTR_HIDE_ROOT is set. | |
482 | void* m_pVirtualRoot; | |
483 | ||
484 | // the starting item for selection with Shift | |
485 | wxTreeItemId m_htSelStart; | |
486 | ||
487 | friend class wxTreeItemIndirectData; | |
488 | friend class wxTreeSortHelper; | |
489 | ||
490 | DECLARE_DYNAMIC_CLASS(wxTreeCtrl) | |
491 | DECLARE_NO_COPY_CLASS(wxTreeCtrl) | |
492 | }; | |
493 | ||
494 | #endif // wxUSE_TREECTRL | |
495 | ||
496 | #endif | |
497 | // _WX_TREECTRL_H_ |