]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/treectlg.h
Added way to specify in generic tree control
[wxWidgets.git] / include / wx / generic / treectlg.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/treectlg.h
3 // Purpose: wxTreeCtrl class
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) 1997,1998 Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _GENERIC_TREECTRL_H_
13 #define _GENERIC_TREECTRL_H_
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "treectlg.h"
17 #endif
18
19 #if wxUSE_TREECTRL
20
21 #include "wx/scrolwin.h"
22 #include "wx/pen.h"
23 #include "wx/imaglist.h"
24
25 // -----------------------------------------------------------------------------
26 // forward declaration
27 // -----------------------------------------------------------------------------
28
29 class WXDLLEXPORT wxGenericTreeItem;
30
31 class WXDLLEXPORT wxTreeItemData;
32
33 class WXDLLEXPORT wxTreeRenameTimer;
34 class WXDLLEXPORT wxTreeFindTimer;
35 class WXDLLEXPORT wxTreeTextCtrl;
36 class WXDLLEXPORT wxTextCtrl;
37
38 // -----------------------------------------------------------------------------
39 // wxGenericTreeCtrl - the tree control
40 // -----------------------------------------------------------------------------
41
42 class WXDLLEXPORT wxGenericTreeCtrl : public wxScrolledWindow
43 {
44 public:
45 // creation
46 // --------
47 wxGenericTreeCtrl() { Init(); }
48
49 wxGenericTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
50 const wxPoint& pos = wxDefaultPosition,
51 const wxSize& size = wxDefaultSize,
52 long style = wxTR_DEFAULT_STYLE,
53 const wxValidator &validator = wxDefaultValidator,
54 const wxString& name = wxTreeCtrlNameStr)
55 {
56 Init();
57 Create(parent, id, pos, size, style, validator, name);
58 }
59
60 virtual ~wxGenericTreeCtrl();
61
62 bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
63 const wxPoint& pos = wxDefaultPosition,
64 const wxSize& size = wxDefaultSize,
65 long style = wxTR_DEFAULT_STYLE,
66 const wxValidator &validator = wxDefaultValidator,
67 const wxString& name = wxTreeCtrlNameStr);
68
69 // accessors
70 // ---------
71
72 // get the total number of items in the control
73 size_t GetCount() const;
74
75 // indent is the number of pixels the children are indented relative to
76 // the parents position. SetIndent() also redraws the control
77 // immediately.
78 unsigned int GetIndent() const { return m_indent; }
79 void SetIndent(unsigned int indent);
80
81 // spacing is the number of pixels between the start and the Text
82 unsigned int GetSpacing() const { return m_spacing; }
83 void SetSpacing(unsigned int spacing);
84
85 // image list: these functions allow to associate an image list with
86 // the control and retrieve it. Note that when assigned with
87 // SetImageList, the control does _not_ delete
88 // the associated image list when it's deleted in order to allow image
89 // lists to be shared between different controls. If you use
90 // AssignImageList, the control _does_ delete the image list.
91 //
92 // The normal image list is for the icons which correspond to the
93 // normal tree item state (whether it is selected or not).
94 // Additionally, the application might choose to show a state icon
95 // which corresponds to an app-defined item state (for example,
96 // checked/unchecked) which are taken from the state image list.
97 virtual wxImageList *GetImageList() const;
98 virtual wxImageList *GetStateImageList() const;
99 virtual wxImageList *GetButtonsImageList() const;
100
101 virtual void SetImageList(wxImageList *imageList);
102 virtual void SetStateImageList(wxImageList *imageList);
103 virtual void SetButtonsImageList(wxImageList *imageList);
104 virtual void AssignImageList(wxImageList *imageList);
105 virtual void AssignStateImageList(wxImageList *imageList);
106 virtual void AssignButtonsImageList(wxImageList *imageList);
107
108 virtual void SetDropEffectAboveItem( bool above = false ) { m_dropEffectAboveItem = above; }
109 virtual bool GetDropEffectAboveItem() const { return m_dropEffectAboveItem; }
110
111 // Functions to work with tree ctrl items.
112
113 // accessors
114 // ---------
115
116 // retrieve item's label
117 wxString GetItemText(const wxTreeItemId& item) const;
118 // get one of the images associated with the item (normal by default)
119 virtual int GetItemImage(const wxTreeItemId& item,
120 wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
121 // get the data associated with the item
122 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
123
124 // get the item's text colour
125 wxColour GetItemTextColour(const wxTreeItemId& item) const;
126
127 // get the item's background colour
128 wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
129
130 // get the item's font
131 wxFont GetItemFont(const wxTreeItemId& item) const;
132
133 // modifiers
134 // ---------
135
136 // set item's label
137 void SetItemText(const wxTreeItemId& item, const wxString& text);
138 // get one of the images associated with the item (normal by default)
139 virtual void SetItemImage(const wxTreeItemId& item, int image,
140 wxTreeItemIcon which = wxTreeItemIcon_Normal);
141 // associate some data with the item
142 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
143
144 // force appearance of [+] button near the item. This is useful to
145 // allow the user to expand the items which don't have any children now
146 // - but instead add them only when needed, thus minimizing memory
147 // usage and loading time.
148 void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
149
150 // the item will be shown in bold
151 void SetItemBold(const wxTreeItemId& item, bool bold = true);
152
153 // set the item's text colour
154 void SetItemTextColour(const wxTreeItemId& item, const wxColour& col);
155
156 // set the item's background colour
157 void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& col);
158
159 // set the item's font (should be of the same height for all items)
160 void SetItemFont(const wxTreeItemId& item, const wxFont& font);
161
162 // set the window font
163 virtual bool SetFont( const wxFont &font );
164
165 // set the styles. No need to specify a GetWindowStyle here since
166 // the base wxWindow member function will do it for us
167 void SetWindowStyle(const long styles);
168
169 // item status inquiries
170 // ---------------------
171
172 // is the item visible (it might be outside the view or not expanded)?
173 bool IsVisible(const wxTreeItemId& item) const;
174 // does the item has any children?
175 bool HasChildren(const wxTreeItemId& item) const
176 { return ItemHasChildren(item); }
177 bool ItemHasChildren(const wxTreeItemId& item) const;
178 // is the item expanded (only makes sense if HasChildren())?
179 bool IsExpanded(const wxTreeItemId& item) const;
180 // is this item currently selected (the same as has focus)?
181 bool IsSelected(const wxTreeItemId& item) const;
182 // is item text in bold font?
183 bool IsBold(const wxTreeItemId& item) const;
184 // does the layout include space for a button?
185
186 // number of children
187 // ------------------
188
189 // if 'recursively' is false, only immediate children count, otherwise
190 // the returned number is the number of all items in this branch
191 size_t GetChildrenCount(const wxTreeItemId& item,
192 bool recursively = true) const;
193
194 // navigation
195 // ----------
196
197 // wxTreeItemId.IsOk() will return false if there is no such item
198
199 // get the root tree item
200 wxTreeItemId GetRootItem() const { return m_anchor; }
201
202 // get the item currently selected (may return NULL if no selection)
203 wxTreeItemId GetSelection() const { return m_current; }
204
205 // get the items currently selected, return the number of such item
206 size_t GetSelections(wxArrayTreeItemIds&) const;
207
208 // get the parent of this item (may return NULL if root)
209 wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
210
211 #if WXWIN_COMPATIBILITY_2_2
212 // deprecated: Use GetItemParent instead.
213 wxDEPRECATED( wxTreeItemId GetParent(const wxTreeItemId& item) const);
214
215 // Expose the base class method hidden by the one above. Not deprecatable.
216 wxWindow *GetParent() const { return wxScrolledWindow::GetParent(); }
217 #endif // WXWIN_COMPATIBILITY_2_2
218
219 // for this enumeration function you must pass in a "cookie" parameter
220 // which is opaque for the application but is necessary for the library
221 // to make these functions reentrant (i.e. allow more than one
222 // enumeration on one and the same object simultaneously). Of course,
223 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
224 // the same!
225
226 // get the first child of this item
227 wxTreeItemId GetFirstChild(const wxTreeItemId& item,
228 wxTreeItemIdValue& cookie) const;
229 // get the next child
230 wxTreeItemId GetNextChild(const wxTreeItemId& item,
231 wxTreeItemIdValue& cookie) const;
232 // get the last child of this item - this method doesn't use cookies
233 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
234
235 // get the next sibling of this item
236 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
237 // get the previous sibling
238 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
239
240 // get first visible item
241 wxTreeItemId GetFirstVisibleItem() const;
242 // get the next visible item: item must be visible itself!
243 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
244 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
245 // get the previous visible item: item must be visible itself!
246 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
247
248 // Only for internal use right now, but should probably be public
249 wxTreeItemId GetNext(const wxTreeItemId& item) const;
250
251 // operations
252 // ----------
253
254 // add the root node to the tree
255 virtual wxTreeItemId AddRoot(const wxString& text,
256 int image = -1, int selectedImage = -1,
257 wxTreeItemData *data = NULL);
258
259 // insert a new item in as the first child of the parent
260 virtual wxTreeItemId PrependItem(const wxTreeItemId& parent,
261 const wxString& text,
262 int image = -1, int selectedImage = -1,
263 wxTreeItemData *data = NULL);
264
265 // insert a new item after a given one
266 virtual wxTreeItemId InsertItem(const wxTreeItemId& parent,
267 const wxTreeItemId& idPrevious,
268 const wxString& text,
269 int image = -1, int selectedImage = -1,
270 wxTreeItemData *data = NULL);
271
272 // insert a new item before the one with the given index
273 virtual wxTreeItemId InsertItem(const wxTreeItemId& parent,
274 size_t index,
275 const wxString& text,
276 int image = -1, int selectedImage = -1,
277 wxTreeItemData *data = NULL);
278
279 // insert a new item in as the last child of the parent
280 virtual wxTreeItemId AppendItem(const wxTreeItemId& parent,
281 const wxString& text,
282 int image = -1, int selectedImage = -1,
283 wxTreeItemData *data = NULL);
284
285 // delete this item and associated data if any
286 void Delete(const wxTreeItemId& item);
287 // delete all children (but don't delete the item itself)
288 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
289 void DeleteChildren(const wxTreeItemId& item);
290 // delete all items from the tree
291 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
292 void DeleteAllItems();
293
294 // expand this item
295 void Expand(const wxTreeItemId& item);
296 // expand this item and all subitems recursively
297 void ExpandAll(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 // or from the given one (multiselect mode only)
308 void UnselectItem(const wxTreeItemId& item) { SelectItem(item, false); }
309 // or from all
310 void UnselectAll();
311 // select this item
312 void SelectItem(const wxTreeItemId& item, bool select = true);
313 // toggle the item selection
314 void ToggleItemSelection(const wxTreeItemId& item)
315 {
316 SelectItem(item, !IsSelected(item));
317 }
318
319 // make sure this item is visible (expanding the parent item and/or
320 // scrolling to this item if necessary)
321 void EnsureVisible(const wxTreeItemId& item);
322 // scroll to this item (but don't expand its parent)
323 void ScrollTo(const wxTreeItemId& item);
324 void AdjustMyScrollbars();
325
326 // The first function is more portable (because easier to implement
327 // on other platforms), but the second one returns some extra info.
328 wxTreeItemId HitTest(const wxPoint& point)
329 { int dummy; return HitTest(point, dummy); }
330 wxTreeItemId HitTest(const wxPoint& point, int& flags);
331
332 // get the bounding rectangle of the item (or of its label only)
333 bool GetBoundingRect(const wxTreeItemId& item,
334 wxRect& rect,
335 bool textOnly = false) const;
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.
340 void EditLabel( const wxTreeItemId& item ) { Edit( item ); }
341 void Edit( const wxTreeItemId& item );
342 // returns a pointer to the text edit control if the item is being
343 // edited, NULL otherwise (it's assumed that no more than one item may
344 // be edited simultaneously)
345 wxTextCtrl* GetEditControl() const;
346
347 // sorting
348 // this function is called to compare 2 items and should return -1, 0
349 // or +1 if the first item is less than, equal to or greater than the
350 // second one. The base class version performs alphabetic comparaison
351 // of item labels (GetText)
352 virtual int OnCompareItems(const wxTreeItemId& item1,
353 const wxTreeItemId& item2);
354 // sort the children of this item using OnCompareItems
355 //
356 // NB: this function is not reentrant and not MT-safe (FIXME)!
357 void SortChildren(const wxTreeItemId& item);
358
359 #if WXWIN_COMPATIBILITY_2_4
360 // deprecated functions: use Set/GetItemImage directly
361 int GetItemSelectedImage(const wxTreeItemId& item) const
362 { return GetItemImage(item, wxTreeItemIcon_Selected); }
363 void SetItemSelectedImage(const wxTreeItemId& item, int image)
364 { SetItemImage(item, image, wxTreeItemIcon_Selected); }
365
366 // use the versions taking wxTreeItemIdValue cookies (note that
367 // GetNextChild() is not inside wxDEPRECATED on purpose, as otherwise we
368 // get twice as many warnings without any added benefit: it is always used
369 // with GetFirstChild() anyhow)
370 wxDEPRECATED( wxTreeItemId GetFirstChild(const wxTreeItemId& item,
371 long& cookie) const );
372 wxTreeItemId GetNextChild(const wxTreeItemId& item,
373 long& cookie) const;
374 #endif // WXWIN_COMPATIBILITY_2_4
375
376 virtual bool ShouldInheritColours() const { return false; }
377
378 // implementation only from now on
379
380 // overridden base class virtuals
381 virtual bool SetBackgroundColour(const wxColour& colour);
382 virtual bool SetForegroundColour(const wxColour& colour);
383
384 virtual void Freeze();
385 virtual void Thaw();
386
387 // callbacks
388 void OnPaint( wxPaintEvent &event );
389 void OnSetFocus( wxFocusEvent &event );
390 void OnKillFocus( wxFocusEvent &event );
391 void OnChar( wxKeyEvent &event );
392 void OnMouse( wxMouseEvent &event );
393 void OnGetToolTip( wxTreeEvent &event );
394 void OnInternalIdle( );
395
396 virtual wxSize DoGetBestSize() const;
397
398 virtual wxVisualAttributes GetDefaultAttributes() const
399 {
400 return GetClassDefaultAttributes(GetWindowVariant());
401 }
402
403 static wxVisualAttributes
404 GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
405
406 // implementation helpers
407 protected:
408 friend class wxGenericTreeItem;
409 friend class wxTreeRenameTimer;
410 friend class wxTreeFindTimer;
411 friend class wxTreeTextCtrl;
412
413 wxFont m_normalFont;
414 wxFont m_boldFont;
415
416 wxGenericTreeItem *m_anchor;
417 wxGenericTreeItem *m_current,
418 *m_key_current,
419 // A hint to select a parent item after deleting a child
420 *m_select_me;
421 unsigned short m_indent;
422 unsigned short m_spacing;
423 int m_lineHeight;
424 wxPen m_dottedPen;
425 wxBrush *m_hilightBrush,
426 *m_hilightUnfocusedBrush;
427 bool m_hasFocus;
428 bool m_dirty;
429 bool m_ownsImageListNormal,
430 m_ownsImageListState,
431 m_ownsImageListButtons;
432 bool m_isDragging; // true between BEGIN/END drag events
433 bool m_lastOnSame; // last click on the same item as prev
434 wxImageList *m_imageListNormal,
435 *m_imageListState,
436 *m_imageListButtons;
437
438 int m_freezeCount;
439 int m_dragCount;
440 wxPoint m_dragStart;
441 wxGenericTreeItem *m_dropTarget;
442 wxCursor m_oldCursor; // cursor is changed while dragging
443 wxGenericTreeItem *m_oldSelection;
444 wxGenericTreeItem *m_underMouse; // for visual effects
445 wxTreeTextCtrl *m_textCtrl;
446
447 wxTimer *m_renameTimer;
448
449 // incremental search data
450 wxString m_findPrefix;
451 wxTimer *m_findTimer;
452
453 bool m_dropEffectAboveItem;
454
455 // the common part of all ctors
456 void Init();
457
458 // misc helpers
459 void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted);
460
461 void DrawBorder(const wxTreeItemId& item);
462 void DrawLine(const wxTreeItemId& item, bool below);
463 void DrawDropEffect(wxGenericTreeItem *item);
464
465 void DoSelectItem(const wxTreeItemId& id,
466 bool unselect_others = true,
467 bool extended_select = false);
468
469 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
470 size_t previous,
471 const wxString& text,
472 int image, int selectedImage,
473 wxTreeItemData *data);
474
475 // called by wxTextTreeCtrl when it marks itself for deletion
476 void ResetTextControl();
477
478 // find the first item starting with the given prefix after the given item
479 wxTreeItemId FindItem(const wxTreeItemId& id, const wxString& prefix) const;
480
481 bool HasButtons() const { return HasFlag(wxTR_HAS_BUTTONS); }
482
483 void CalculateLineHeight();
484 int GetLineHeight(wxGenericTreeItem *item) const;
485 void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y );
486 void PaintItem( wxGenericTreeItem *item, wxDC& dc);
487
488 void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y );
489 void CalculatePositions();
490 void CalculateSize( wxGenericTreeItem *item, wxDC &dc );
491
492 void RefreshSubtree( wxGenericTreeItem *item );
493 void RefreshLine( wxGenericTreeItem *item );
494
495 // redraw all selected items
496 void RefreshSelected();
497
498 // RefreshSelected() recursive helper
499 void RefreshSelectedUnder(wxGenericTreeItem *item);
500
501 void OnRenameTimer();
502 bool OnRenameAccept(wxGenericTreeItem *item, const wxString& value);
503 void OnRenameCancelled(wxGenericTreeItem *item);
504
505 void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const;
506 void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 );
507 bool TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
508 bool TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
509 void UnselectAllChildren( wxGenericTreeItem *item );
510 void ChildrenClosing(wxGenericTreeItem* item);
511
512 private:
513 DECLARE_EVENT_TABLE()
514 DECLARE_DYNAMIC_CLASS(wxGenericTreeCtrl)
515 DECLARE_NO_COPY_CLASS(wxGenericTreeCtrl)
516 };
517
518 #if !defined(__WXMSW__) || defined(__WXUNIVERSAL__)
519 /*
520 * wxTreeCtrl has to be a real class or we have problems with
521 * the run-time information.
522 */
523
524 class WXDLLEXPORT wxTreeCtrl: public wxGenericTreeCtrl
525 {
526 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
527
528 public:
529 wxTreeCtrl() {}
530
531 wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
532 const wxPoint& pos = wxDefaultPosition,
533 const wxSize& size = wxDefaultSize,
534 long style = wxTR_DEFAULT_STYLE,
535 const wxValidator &validator = wxDefaultValidator,
536 const wxString& name = wxTreeCtrlNameStr)
537 : wxGenericTreeCtrl(parent, id, pos, size, style, validator, name)
538 {
539 }
540 };
541 #endif // !__WXMSW__ || __WXUNIVERSAL__
542
543 #endif // wxUSE_TREECTRL
544
545 #endif // _GENERIC_TREECTRL_H_
546