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