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