]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/treectrl.h
I changed wxUSE_HTML (and other related wxUSEs) to 1. wxHTML is stable enough and...
[wxWidgets.git] / include / wx / generic / treectrl.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 "treectrl.h"
17 #endif
18
19 #ifdef __WXMSW__
20 WXDLLEXPORT_DATA(extern const char*) wxTreeCtrlNameStr;
21 #else
22 #define wxTreeCtrlNameStr "wxTreeCtrl"
23 #endif
24
25 #include "wx/defs.h"
26 #include "wx/string.h"
27 #include "wx/object.h"
28 #include "wx/event.h"
29 #include "wx/scrolwin.h"
30 #include "wx/textctrl.h"
31 #include "wx/pen.h"
32 #include "wx/dynarray.h"
33 #include "wx/timer.h"
34
35 //those defines should only be done in generic/treectrl.h,
36 //because wxMSW doesn't allow mutiple selection
37
38 #ifndef wxTR_SINGLE
39 #define wxTR_SINGLE 0x0000
40 #define wxTR_MULTIPLE 0x0020
41 #define wxTR_EXTENDED 0x0040
42 #define wxTR_HAS_VARIABLE_ROW_HIGHT 0x0080
43 #endif
44
45 // -----------------------------------------------------------------------------
46 // constants
47 // -----------------------------------------------------------------------------
48
49 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
50 // where exactly the specified point is situated:
51
52 static const int wxTREE_HITTEST_ABOVE = 0x0001;
53 static const int wxTREE_HITTEST_BELOW = 0x0002;
54 static const int wxTREE_HITTEST_NOWHERE = 0x0004;
55 // on the button associated with an item.
56 static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0008;
57 // on the bitmap associated with an item.
58 static const int wxTREE_HITTEST_ONITEMICON = 0x0010;
59 // on the ident associated with an item.
60 static const int wxTREE_HITTEST_ONITEMIDENT = 0x0020;
61 // on the label (string) associated with an item.
62 static const int wxTREE_HITTEST_ONITEMLABEL = 0x0040;
63 // on the right of the label associated with an item.
64 static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0080;
65 // on the label (string) associated with an item.
66 //static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0100;
67 // on the left of the wxTreeCtrl.
68 static const int wxTREE_HITTEST_TOLEFT = 0x0200;
69 // on the right of the wxTreeCtrl.
70 static const int wxTREE_HITTEST_TORIGHT = 0x0400;
71 // on the upper part (first half) of the item.
72 static const int wxTREE_HITTEST_ONITEMUPPERPART = 0x0800;
73 // on the lower part (second half) of the item.
74 static const int wxTREE_HITTEST_ONITEMLOWERPART = 0x1000;
75
76 // anywhere on the item
77 static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
78 wxTREE_HITTEST_ONITEMLABEL;
79
80 // -----------------------------------------------------------------------------
81 // forward declaration
82 // -----------------------------------------------------------------------------
83
84 class WXDLLEXPORT wxImageList;
85 class WXDLLEXPORT wxGenericTreeItem;
86
87 class WXDLLEXPORT wxTreeItemData;
88
89 class WXDLLEXPORT wxTreeRenameTimer;
90 class WXDLLEXPORT wxTreeTextCtrl;
91
92 // -----------------------------------------------------------------------------
93 // wxTreeItemId - unique identifier of a tree element
94 // -----------------------------------------------------------------------------
95
96 class WXDLLEXPORT wxTreeItemId
97 {
98 friend class wxTreeCtrl;
99 friend class wxTreeEvent;
100 public:
101 // ctors
102 // 0 is invalid value for HTREEITEM
103 wxTreeItemId() { m_pItem = 0; }
104
105 // default copy ctor/assignment operator are ok for us
106
107 // accessors
108 // is this a valid tree item?
109 bool IsOk() const { return m_pItem != 0; }
110
111 // deprecated: only for compatibility
112 wxTreeItemId(long itemId) { m_pItem = (wxGenericTreeItem *)itemId; }
113 operator long() const { return (long)m_pItem; }
114
115 //protected: // not for gcc
116 // for wxTreeCtrl usage only
117 wxTreeItemId(wxGenericTreeItem *pItem) { m_pItem = pItem; }
118
119 wxGenericTreeItem *m_pItem;
120 };
121
122 WX_DECLARE_OBJARRAY(wxTreeItemId, wxArrayTreeItemIds);
123
124 // ----------------------------------------------------------------------------
125 // wxTreeItemData is some (arbitrary) user class associated with some item.
126 //
127 // Because the objects of this class are deleted by the tree, they should
128 // always be allocated on the heap!
129 // ----------------------------------------------------------------------------
130
131 class WXDLLEXPORT wxTreeItemData: public wxClientData
132 {
133 friend class wxTreeCtrl;
134 public:
135 // creation/destruction
136 // --------------------
137 // default ctor
138 wxTreeItemData() { }
139
140 // default copy ctor/assignment operator are ok
141
142 // accessor: get the item associated with us
143 const wxTreeItemId& GetId() const { return m_pItem; }
144 void SetId(const wxTreeItemId& id) { m_pItem = id; }
145
146 protected:
147 wxTreeItemId m_pItem;
148 };
149
150 //-----------------------------------------------------------------------------
151 // wxTreeRenameTimer (internal)
152 //-----------------------------------------------------------------------------
153
154 class WXDLLEXPORT wxTreeRenameTimer: public wxTimer
155 {
156 private:
157 wxTreeCtrl *m_owner;
158
159 public:
160 wxTreeRenameTimer( wxTreeCtrl *owner );
161 void Notify();
162 };
163
164 //-----------------------------------------------------------------------------
165 // wxTreeTextCtrl (internal)
166 //-----------------------------------------------------------------------------
167
168 class WXDLLEXPORT wxTreeTextCtrl: public wxTextCtrl
169 {
170 DECLARE_DYNAMIC_CLASS(wxTreeTextCtrl);
171
172 private:
173 bool *m_accept;
174 wxString *m_res;
175 wxTreeCtrl *m_owner;
176 wxString m_startValue;
177
178 public:
179 wxTreeTextCtrl(void) {};
180 wxTreeTextCtrl( wxWindow *parent, const wxWindowID id,
181 bool *accept, wxString *res, wxTreeCtrl *owner,
182 const wxString &value = wxEmptyString,
183 const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize,
184 #if wxUSE_VALIDATORS
185 # if defined(__VISAGECPP__)
186 int style = 0, const wxValidator* validator = wxDefaultValidator,
187 # else
188 int style = 0, const wxValidator& validator = wxDefaultValidator,
189 # endif
190 #endif
191 const wxString &name = wxTextCtrlNameStr );
192 void OnChar( wxKeyEvent &event );
193 void OnKillFocus( wxFocusEvent &event );
194
195 DECLARE_EVENT_TABLE()
196 };
197
198 // -----------------------------------------------------------------------------
199 // wxTreeCtrl - the tree control
200 // -----------------------------------------------------------------------------
201
202 class WXDLLEXPORT wxTreeCtrl : public wxScrolledWindow
203 {
204 public:
205 // creation
206 // --------
207 wxTreeCtrl() { Init(); }
208
209 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
210 const wxPoint& pos = wxDefaultPosition,
211 const wxSize& size = wxDefaultSize,
212 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
213 #if wxUSE_VALIDATORS
214 # if defined(__VISAGECPP__)
215 const wxValidator *validator = wxDefaultValidator,
216 # else
217 const wxValidator &validator = wxDefaultValidator,
218 # endif
219 #endif
220 const wxString& name = wxTreeCtrlNameStr)
221 {
222 Create(parent, id, pos, size, style, validator, name);
223 }
224
225 virtual ~wxTreeCtrl();
226
227 bool Create(wxWindow *parent, wxWindowID id = -1,
228 const wxPoint& pos = wxDefaultPosition,
229 const wxSize& size = wxDefaultSize,
230 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
231 #if wxUSE_VALIDATORS
232 # if defined(__VISAGECPP__)
233 const wxValidator *validator = wxDefaultValidator,
234 # else
235 const wxValidator &validator = wxDefaultValidator,
236 # endif
237 #endif
238 const wxString& name = wxTreeCtrlNameStr);
239
240 // accessors
241 // ---------
242
243 // get the total number of items in the control
244 size_t GetCount() const;
245
246 // indent is the number of pixels the children are indented relative to
247 // the parents position. SetIndent() also redraws the control
248 // immediately.
249 unsigned int GetIndent() const { return m_indent; }
250 void SetIndent(unsigned int indent);
251
252 // spacing is the number of pixels between the start and the Text
253 unsigned int GetSpacing() const { return m_spacing; }
254 void SetSpacing(unsigned int spacing);
255
256 // image list: these functions allow to associate an image list with
257 // the control and retrieve it. Note that the control does _not_ delete
258 // the associated image list when it's deleted in order to allow image
259 // lists to be shared between different controls.
260 //
261 // The normal image list is for the icons which correspond to the
262 // normal tree item state (whether it is selected or not).
263 // Additionally, the application might choose to show a state icon
264 // which corresponds to an app-defined item state (for example,
265 // checked/unchecked) which are taken from the state image list.
266 wxImageList *GetImageList() const;
267 wxImageList *GetStateImageList() const;
268
269 void SetImageList(wxImageList *imageList);
270 void SetStateImageList(wxImageList *imageList);
271
272 // Functions to work with tree ctrl items.
273
274 // accessors
275 // ---------
276
277 // retrieve items label
278 wxString GetItemText(const wxTreeItemId& item) const;
279 // get one of the images associated with the item (normal by default)
280 int GetItemImage(const wxTreeItemId& item,
281 wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
282 // get the data associated with the item
283 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
284
285 // modifiers
286 // ---------
287
288 // set items label
289 void SetItemText(const wxTreeItemId& item, const wxString& text);
290 // get one of the images associated with the item (normal by default)
291 void SetItemImage(const wxTreeItemId& item, int image,
292 wxTreeItemIcon which = wxTreeItemIcon_Normal);
293 // associate some data with the item
294 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
295
296 // force appearance of [+] button near the item. This is useful to
297 // allow the user to expand the items which don't have any children now
298 // - but instead add them only when needed, thus minimizing memory
299 // usage and loading time.
300 void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE);
301
302 // the item will be shown in bold
303 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
304
305 // item status inquiries
306 // ---------------------
307
308 // is the item visible (it might be outside the view or not expanded)?
309 bool IsVisible(const wxTreeItemId& item) const;
310 // does the item has any children?
311 bool HasChildren(const wxTreeItemId& item) const
312 { return ItemHasChildren(item); }
313 bool ItemHasChildren(const wxTreeItemId& item) const;
314 // is the item expanded (only makes sense if HasChildren())?
315 bool IsExpanded(const wxTreeItemId& item) const;
316 // is this item currently selected (the same as has focus)?
317 bool IsSelected(const wxTreeItemId& item) const;
318 // is item text in bold font?
319 bool IsBold(const wxTreeItemId& item) const;
320
321 // number of children
322 // ------------------
323
324 // if 'recursively' is FALSE, only immediate children count, otherwise
325 // the returned number is the number of all items in this branch
326 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE);
327
328 // navigation
329 // ----------
330
331 // wxTreeItemId.IsOk() will return FALSE if there is no such item
332
333 // get the root tree item
334 wxTreeItemId GetRootItem() const { return m_anchor; }
335
336 // get the item currently selected (may return NULL if no selection)
337 wxTreeItemId GetSelection() const { return m_current; }
338
339 // get the items currently selected, return the number of such item
340 size_t GetSelections(wxArrayTreeItemIds&) const;
341
342 // get the parent of this item (may return NULL if root)
343 wxTreeItemId GetParent(const wxTreeItemId& item) const;
344
345 // for this enumeration function you must pass in a "cookie" parameter
346 // which is opaque for the application but is necessary for the library
347 // to make these functions reentrant (i.e. allow more than one
348 // enumeration on one and the same object simultaneously). Of course,
349 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
350 // the same!
351
352 // get the first child of this item
353 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
354 // get the next child
355 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
356 // get the last child of this item - this method doesn't use cookies
357 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
358
359 // get the next sibling of this item
360 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
361 // get the previous sibling
362 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
363
364 // get first visible item
365 wxTreeItemId GetFirstVisibleItem() const;
366 // get the next visible item: item must be visible itself!
367 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
368 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
369 // get the previous visible item: item must be visible itself!
370 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
371
372 // operations
373 // ----------
374
375 // add the root node to the tree
376 wxTreeItemId AddRoot(const wxString& text,
377 int image = -1, int selectedImage = -1,
378 wxTreeItemData *data = NULL);
379
380 // insert a new item in as the first child of the parent
381 wxTreeItemId PrependItem(const wxTreeItemId& parent,
382 const wxString& text,
383 int image = -1, int selectedImage = -1,
384 wxTreeItemData *data = NULL);
385
386 // insert a new item after a given one
387 wxTreeItemId InsertItem(const wxTreeItemId& parent,
388 const wxTreeItemId& idPrevious,
389 const wxString& text,
390 int image = -1, int selectedImage = -1,
391 wxTreeItemData *data = NULL);
392
393 // insert a new item in as the last child of the parent
394 wxTreeItemId AppendItem(const wxTreeItemId& parent,
395 const wxString& text,
396 int image = -1, int selectedImage = -1,
397 wxTreeItemData *data = NULL);
398
399 // delete this item and associated data if any
400 void Delete(const wxTreeItemId& item);
401 // delete all children (but don't delete the item itself)
402 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
403 void DeleteChildren(const wxTreeItemId& item);
404 // delete all items from the tree
405 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
406 void DeleteAllItems();
407
408 // expand this item
409 void Expand(const wxTreeItemId& item);
410 // collapse the item without removing its children
411 void Collapse(const wxTreeItemId& item);
412 // collapse the item and remove all children
413 void CollapseAndReset(const wxTreeItemId& item);
414 // toggles the current state
415 void Toggle(const wxTreeItemId& item);
416
417 // remove the selection from currently selected item (if any)
418 void Unselect();
419 void UnselectAll();
420 // select this item
421 void SelectItem(const wxTreeItemId& item, bool unselect_others=TRUE, bool extended_select=FALSE);
422 // make sure this item is visible (expanding the parent item and/or
423 // scrolling to this item if necessary)
424 void EnsureVisible(const wxTreeItemId& item);
425 // scroll to this item (but don't expand its parent)
426 void ScrollTo(const wxTreeItemId& item);
427
428 // The first function is more portable (because easier to implement
429 // on other platforms), but the second one returns some extra info.
430 wxTreeItemId HitTest(const wxPoint& point)
431 { int dummy; return HitTest(point, dummy); }
432 wxTreeItemId HitTest(const wxPoint& point, int& flags);
433
434 // Start editing the item label: this (temporarily) replaces the item
435 // with a one line edit control. The item will be selected if it hadn't
436 // been before.
437 void EditLabel( const wxTreeItemId& item ) { Edit( item ); }
438 void Edit( const wxTreeItemId& item );
439
440 // sorting
441 // this function is called to compare 2 items and should return -1, 0
442 // or +1 if the first item is less than, equal to or greater than the
443 // second one. The base class version performs alphabetic comparaison
444 // of item labels (GetText)
445 virtual int OnCompareItems(const wxTreeItemId& item1,
446 const wxTreeItemId& item2);
447 // sort the children of this item using OnCompareItems
448 //
449 // NB: this function is not reentrant and not MT-safe (FIXME)!
450 void SortChildren(const wxTreeItemId& item);
451
452 // deprecated functions: use Set/GetItemImage directly
453 // get the selected item image
454 int GetItemSelectedImage(const wxTreeItemId& item) const
455 { return GetItemImage(item, wxTreeItemIcon_Selected); }
456 // set the selected item image
457 void SetItemSelectedImage(const wxTreeItemId& item, int image)
458 { SetItemImage(item, image, wxTreeItemIcon_Selected); }
459
460 // implementation
461
462 // callbacks
463 void OnPaint( wxPaintEvent &event );
464 void OnSetFocus( wxFocusEvent &event );
465 void OnKillFocus( wxFocusEvent &event );
466 void OnChar( wxKeyEvent &event );
467 void OnMouse( wxMouseEvent &event );
468 void OnIdle( wxIdleEvent &event );
469
470 // implementation
471 void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted);
472
473 // Draw Special Information
474 void DrawBorder(wxTreeItemId& item);
475 void DrawLine(wxTreeItemId& item, bool below);
476
477 protected:
478 friend class wxGenericTreeItem;
479 friend class wxTreeRenameTimer;
480 friend class wxTreeTextCtrl;
481
482 wxFont m_normalFont;
483 wxFont m_boldFont;
484
485 wxGenericTreeItem *m_anchor;
486 wxGenericTreeItem *m_current, *m_key_current, *m_currentEdit;
487 bool m_hasFocus;
488 bool m_dirty;
489 int m_xScroll,m_yScroll;
490 unsigned int m_indent;
491 unsigned int m_spacing;
492 int m_lineHeight;
493 wxPen m_dottedPen;
494 wxBrush *m_hilightBrush;
495 wxImageList *m_imageListNormal,
496 *m_imageListState;
497 int m_dragCount;
498 wxPoint m_dragStart;
499 wxTimer *m_renameTimer;
500 bool m_renameAccept;
501 wxString m_renameRes;
502
503 // the common part of all ctors
504 void Init();
505
506 // misc helpers
507 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
508 size_t previous,
509 const wxString& text,
510 int image, int selectedImage,
511 wxTreeItemData *data);
512
513 void AdjustMyScrollbars();
514 int GetLineHeight(wxGenericTreeItem *item) const;
515 void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y );
516 void PaintItem( wxGenericTreeItem *item, wxDC& dc);
517
518 void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y );
519 void CalculatePositions();
520 void CalculateSize( wxGenericTreeItem *item, wxDC &dc );
521
522 void RefreshSubtree( wxGenericTreeItem *item );
523 void RefreshLine( wxGenericTreeItem *item );
524
525 void OnRenameTimer();
526 void OnRenameAccept();
527
528 void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const;
529 void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 );
530 bool TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
531 bool TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
532 void UnselectAllChildren( wxGenericTreeItem *item );
533
534 private:
535 DECLARE_EVENT_TABLE()
536 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
537 };
538
539 #endif // _GENERIC_TREECTRL_H_
540