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