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