]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/treectrl.h
destructor for wxMenu deletes menu now
[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
177 public:
178 wxTreeTextCtrl(void) {};
179 wxTreeTextCtrl( wxWindow *parent, const wxWindowID id,
180 bool *accept, wxString *res, wxTreeCtrl *owner,
181 const wxString &value = "",
182 const wxPoint &pos = wxDefaultPosition, const wxSize &size = wxDefaultSize,
183 int style = 0, const wxValidator& validator = wxDefaultValidator,
184 const wxString &name = "wxTreeTextCtrlText" );
185 void OnChar( wxKeyEvent &event );
186 void OnKillFocus( wxFocusEvent &event );
187
188 DECLARE_EVENT_TABLE()
189 };
190
191 // -----------------------------------------------------------------------------
192 // wxTreeCtrl - the tree control
193 // -----------------------------------------------------------------------------
194
195 class WXDLLEXPORT wxTreeCtrl : public wxScrolledWindow
196 {
197 public:
198 // creation
199 // --------
200 wxTreeCtrl() { Init(); }
201
202 wxTreeCtrl(wxWindow *parent, wxWindowID id = -1,
203 const wxPoint& pos = wxDefaultPosition,
204 const wxSize& size = wxDefaultSize,
205 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
206 const wxValidator &validator = wxDefaultValidator,
207 const wxString& name = wxTreeCtrlNameStr)
208 {
209 Create(parent, id, pos, size, style, validator, name);
210 }
211
212 virtual ~wxTreeCtrl();
213
214 bool Create(wxWindow *parent, wxWindowID id = -1,
215 const wxPoint& pos = wxDefaultPosition,
216 const wxSize& size = wxDefaultSize,
217 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
218 const wxValidator &validator = wxDefaultValidator,
219 const wxString& name = wxTreeCtrlNameStr);
220
221 // accessors
222 // ---------
223
224 // get the total number of items in the control
225 size_t GetCount() const;
226
227 // indent is the number of pixels the children are indented relative to
228 // the parents position. SetIndent() also redraws the control
229 // immediately.
230 unsigned int GetIndent() const { return m_indent; }
231 void SetIndent(unsigned int indent);
232
233 // spacing is the number of pixels between the start and the Text
234 unsigned int GetSpacing() const { return m_spacing; }
235 void SetSpacing(unsigned int spacing);
236
237 // image list: these functions allow to associate an image list with
238 // the control and retrieve it. Note that the control does _not_ delete
239 // the associated image list when it's deleted in order to allow image
240 // lists to be shared between different controls.
241 //
242 // The normal image list is for the icons which correspond to the
243 // normal tree item state (whether it is selected or not).
244 // Additionally, the application might choose to show a state icon
245 // which corresponds to an app-defined item state (for example,
246 // checked/unchecked) which are taken from the state image list.
247 wxImageList *GetImageList() const;
248 wxImageList *GetStateImageList() const;
249
250 void SetImageList(wxImageList *imageList);
251 void SetStateImageList(wxImageList *imageList);
252
253 // Functions to work with tree ctrl items.
254
255 // accessors
256 // ---------
257
258 // retrieve items label
259 wxString GetItemText(const wxTreeItemId& item) const;
260 // get the normal item image
261 int GetItemImage(const wxTreeItemId& item) const;
262 // get the selected item image
263 int GetItemSelectedImage(const wxTreeItemId& item) const;
264 // get the data associated with the item
265 wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
266
267 // modifiers
268 // ---------
269
270 // set items label
271 void SetItemText(const wxTreeItemId& item, const wxString& text);
272 // set the normal item image
273 void SetItemImage(const wxTreeItemId& item, int image);
274 // set the selected item image
275 void SetItemSelectedImage(const wxTreeItemId& item, int image);
276 // associate some data with the item
277 void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
278
279 // force appearance of [+] button near the item. This is useful to
280 // allow the user to expand the items which don't have any children now
281 // - but instead add them only when needed, thus minimizing memory
282 // usage and loading time.
283 void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE);
284
285 // the item will be shown in bold
286 void SetItemBold(const wxTreeItemId& item, bool bold = TRUE);
287
288 // item status inquiries
289 // ---------------------
290
291 // is the item visible (it might be outside the view or not expanded)?
292 bool IsVisible(const wxTreeItemId& item) const;
293 // does the item has any children?
294 bool HasChildren(const wxTreeItemId& item) const
295 { return ItemHasChildren(item); }
296 bool ItemHasChildren(const wxTreeItemId& item) const;
297 // is the item expanded (only makes sense if HasChildren())?
298 bool IsExpanded(const wxTreeItemId& item) const;
299 // is this item currently selected (the same as has focus)?
300 bool IsSelected(const wxTreeItemId& item) const;
301 // is item text in bold font?
302 bool IsBold(const wxTreeItemId& item) const;
303
304 // number of children
305 // ------------------
306
307 // if 'recursively' is FALSE, only immediate children count, otherwise
308 // the returned number is the number of all items in this branch
309 size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = TRUE);
310
311 // navigation
312 // ----------
313
314 // wxTreeItemId.IsOk() will return FALSE if there is no such item
315
316 // get the root tree item
317 wxTreeItemId GetRootItem() const { return m_anchor; }
318
319 // get the item currently selected (may return NULL if no selection)
320 wxTreeItemId GetSelection() const { return m_current; }
321
322 // get the items currently selected, return the number of such item
323 size_t GetSelections(wxArrayTreeItemIds&) const;
324
325 // get the parent of this item (may return NULL if root)
326 wxTreeItemId GetParent(const wxTreeItemId& item) const;
327
328 // for this enumeration function you must pass in a "cookie" parameter
329 // which is opaque for the application but is necessary for the library
330 // to make these functions reentrant (i.e. allow more than one
331 // enumeration on one and the same object simultaneously). Of course,
332 // the "cookie" passed to GetFirstChild() and GetNextChild() should be
333 // the same!
334
335 // get the first child of this item
336 wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
337 // get the next child
338 wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
339 // get the last child of this item - this method doesn't use cookies
340 wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
341
342 // get the next sibling of this item
343 wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
344 // get the previous sibling
345 wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
346
347 // get first visible item
348 wxTreeItemId GetFirstVisibleItem() const;
349 // get the next visible item: item must be visible itself!
350 // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem()
351 wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
352 // get the previous visible item: item must be visible itself!
353 wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
354
355 // operations
356 // ----------
357
358 // add the root node to the tree
359 wxTreeItemId AddRoot(const wxString& text,
360 int image = -1, int selectedImage = -1,
361 wxTreeItemData *data = NULL);
362
363 // insert a new item in as the first child of the parent
364 wxTreeItemId PrependItem(const wxTreeItemId& parent,
365 const wxString& text,
366 int image = -1, int selectedImage = -1,
367 wxTreeItemData *data = NULL);
368
369 // insert a new item after a given one
370 wxTreeItemId InsertItem(const wxTreeItemId& parent,
371 const wxTreeItemId& idPrevious,
372 const wxString& text,
373 int image = -1, int selectedImage = -1,
374 wxTreeItemData *data = NULL);
375
376 // insert a new item in as the last child of the parent
377 wxTreeItemId AppendItem(const wxTreeItemId& parent,
378 const wxString& text,
379 int image = -1, int selectedImage = -1,
380 wxTreeItemData *data = NULL);
381
382 // delete this item and associated data if any
383 void Delete(const wxTreeItemId& item);
384 // delete all children (but don't delete the item itself)
385 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
386 void DeleteChildren(const wxTreeItemId& item);
387 // delete all items from the tree
388 // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
389 void DeleteAllItems();
390
391 // expand this item
392 void Expand(const wxTreeItemId& item);
393 // collapse the item without removing its children
394 void Collapse(const wxTreeItemId& item);
395 // collapse the item and remove all children
396 void CollapseAndReset(const wxTreeItemId& item);
397 // toggles the current state
398 void Toggle(const wxTreeItemId& item);
399
400 // remove the selection from currently selected item (if any)
401 void Unselect();
402 void UnselectAll();
403 // select this item
404 void SelectItem(const wxTreeItemId& item, bool unselect_others=TRUE, bool extended_select=FALSE);
405 // make sure this item is visible (expanding the parent item and/or
406 // scrolling to this item if necessary)
407 void EnsureVisible(const wxTreeItemId& item);
408 // scroll to this item (but don't expand its parent)
409 void ScrollTo(const wxTreeItemId& item);
410
411 // The first function is more portable (because easier to implement
412 // on other platforms), but the second one returns some extra info.
413 wxTreeItemId HitTest(const wxPoint& point)
414 { int dummy; return HitTest(point, dummy); }
415 wxTreeItemId HitTest(const wxPoint& point, int& flags);
416
417 // Start editing the item label: this (temporarily) replaces the item
418 // with a one line edit control. The item will be selected if it hadn't
419 // been before.
420 void EditLabel( const wxTreeItemId& item ) { Edit( item ); }
421 void Edit( const wxTreeItemId& item );
422
423 // sorting
424 // this function is called to compare 2 items and should return -1, 0
425 // or +1 if the first item is less than, equal to or greater than the
426 // second one. The base class version performs alphabetic comparaison
427 // of item labels (GetText)
428 virtual int OnCompareItems(const wxTreeItemId& item1,
429 const wxTreeItemId& item2);
430 // sort the children of this item using OnCompareItems
431 //
432 // NB: this function is not reentrant and not MT-safe (FIXME)!
433 void SortChildren(const wxTreeItemId& item);
434
435 // callbacks
436 void OnPaint( wxPaintEvent &event );
437 void OnSetFocus( wxFocusEvent &event );
438 void OnKillFocus( wxFocusEvent &event );
439 void OnChar( wxKeyEvent &event );
440 void OnMouse( wxMouseEvent &event );
441 void OnIdle( wxIdleEvent &event );
442
443 // implementation
444 void SendDeleteEvent(wxGenericTreeItem *itemBeingDeleted);
445
446 // Draw Special Information
447 void DrawBorder(wxTreeItemId& item);
448 void DrawLine(wxTreeItemId& item, bool below);
449
450 protected:
451 friend class wxGenericTreeItem;
452 friend class wxTreeRenameTimer;
453 friend class wxTreeTextCtrl;
454
455 wxGenericTreeItem *m_anchor;
456 wxGenericTreeItem *m_current, *m_key_current, *m_currentEdit;
457 bool m_hasFocus;
458 bool m_dirty;
459 int m_xScroll,m_yScroll;
460 unsigned int m_indent;
461 unsigned int m_spacing;
462 int m_lineHeight;
463 wxPen m_dottedPen;
464 wxBrush *m_hilightBrush;
465 wxImageList *m_imageListNormal,
466 *m_imageListState;
467 int m_dragCount;
468 wxTimer *m_renameTimer;
469 bool m_renameAccept;
470 wxString m_renameRes;
471
472 // the common part of all ctors
473 void Init();
474
475 // misc helpers
476 wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
477 size_t previous,
478 const wxString& text,
479 int image, int selectedImage,
480 wxTreeItemData *data);
481
482 void AdjustMyScrollbars();
483 int GetLineHeight(wxGenericTreeItem *item) const;
484 void PaintLevel( wxGenericTreeItem *item, wxDC& dc, int level, int &y );
485 void PaintItem( wxGenericTreeItem *item, wxDC& dc);
486
487 void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y );
488 void CalculatePositions();
489 void CalculateSize( wxGenericTreeItem *item, wxDC &dc );
490
491 void RefreshSubtree( wxGenericTreeItem *item );
492 void RefreshLine( wxGenericTreeItem *item );
493
494 void OnRenameTimer();
495 void OnRenameAccept();
496
497 void FillArray(wxGenericTreeItem*, wxArrayTreeItemIds&) const;
498 void SelectItemRange( wxGenericTreeItem *item1, wxGenericTreeItem *item2 );
499 bool TagAllChildrenUntilLast(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
500 bool TagNextChildren(wxGenericTreeItem *crt_item, wxGenericTreeItem *last_item, bool select);
501 void UnselectAllChildren( wxGenericTreeItem *item );
502
503 private:
504 DECLARE_EVENT_TABLE()
505 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
506 };
507
508 #endif // _GENERIC_TREECTRL_H_
509