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