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