]>
Commit | Line | Data |
---|---|---|
f135ff73 VZ |
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_ | |
c801d85f KB |
14 | |
15 | #ifdef __GNUG__ | |
f135ff73 | 16 | #pragma interface "treectrl.h" |
c801d85f KB |
17 | #endif |
18 | ||
19 | #include "wx/defs.h" | |
20 | #include "wx/string.h" | |
f135ff73 | 21 | #include "wx/object.h" |
c801d85f | 22 | #include "wx/event.h" |
c801d85f | 23 | #include "wx/scrolwin.h" |
f135ff73 | 24 | #include "wx/textctrl.h" |
c801d85f | 25 | |
f135ff73 VZ |
26 | // ----------------------------------------------------------------------------- |
27 | // forward declaration | |
28 | // ----------------------------------------------------------------------------- | |
c801d85f | 29 | |
f135ff73 | 30 | class wxImageList; |
c801d85f | 31 | class wxGenericTreeItem; |
c801d85f | 32 | |
f135ff73 VZ |
33 | class wxTreeItemData; |
34 | typedef int (*wxTreeItemCmpFunc)(wxTreeItemData *item1, wxTreeItemData *item2); | |
74bedbeb | 35 | |
f135ff73 VZ |
36 | // ----------------------------------------------------------------------------- |
37 | // wxTreeItemId - unique identifier of a tree element | |
38 | // ----------------------------------------------------------------------------- | |
c801d85f | 39 | |
f135ff73 VZ |
40 | class WXDLLEXPORT wxTreeItemId |
41 | { | |
42 | friend class wxTreeCtrl; | |
43 | friend class wxTreeEvent; | |
44 | public: | |
45 | // ctors | |
46 | // 0 is invalid value for HTREEITEM | |
47 | wxTreeItemId() { m_pItem = 0; } | |
48 | ||
49 | // default copy ctor/assignment operator are ok for us | |
50 | ||
51 | // accessors | |
52 | // is this a valid tree item? | |
53 | bool IsOk() const { return m_pItem != 0; } | |
c801d85f | 54 | |
f135ff73 VZ |
55 | // deprecated: only for compatibility |
56 | wxTreeItemId(long itemId) { m_pItem = (wxGenericTreeItem *)itemId; } | |
57 | operator long() const { return (long)m_pItem; } | |
58 | ||
7bce6aec | 59 | //protected: |
f135ff73 VZ |
60 | // for wxTreeCtrl usage only |
61 | wxTreeItemId(wxGenericTreeItem *pItem) { m_pItem = pItem; } | |
62 | ||
63 | wxGenericTreeItem *m_pItem; | |
c801d85f KB |
64 | }; |
65 | ||
f135ff73 VZ |
66 | // ---------------------------------------------------------------------------- |
67 | // wxTreeItemData is some (arbitrary) user class associated with some item. | |
68 | // | |
69 | // Because the objects of this class are deleted by the tree, they should | |
70 | // always be allocated on the heap! | |
71 | // ---------------------------------------------------------------------------- | |
72 | class WXDLLEXPORT wxTreeItemData | |
c801d85f | 73 | { |
f135ff73 | 74 | friend class wxTreeCtrl; |
a32dd690 | 75 | public: |
f135ff73 VZ |
76 | // creation/destruction |
77 | // -------------------- | |
78 | // default ctor | |
79 | wxTreeItemData() { } | |
80 | ||
81 | // default copy ctor/assignment operator are ok | |
82 | ||
83 | // dtor is virtual and all the items are deleted by the tree control | |
84 | // when it's deleted, so you normally don't have to care about freeing | |
85 | // memory allocated in your wxTreeItemData-derived class | |
86 | virtual ~wxTreeItemData() { } | |
c801d85f | 87 | |
f135ff73 VZ |
88 | // accessor: get the item associated with us |
89 | const wxTreeItemId& GetId() const { return m_pItem; } | |
90 | void SetId(const wxTreeItemId& id) { m_pItem = id; } | |
c801d85f | 91 | |
f135ff73 VZ |
92 | protected: |
93 | wxTreeItemId m_pItem; | |
94 | }; | |
95 | ||
96 | // ----------------------------------------------------------------------------- | |
97 | // wxTreeEvent - the event generated by the tree control | |
98 | // ----------------------------------------------------------------------------- | |
99 | class WXDLLEXPORT wxTreeEvent : public wxCommandEvent | |
c801d85f | 100 | { |
f135ff73 VZ |
101 | friend class wxTreeCtrl; |
102 | public: | |
103 | wxTreeEvent(wxEventType commandType = wxEVT_NULL, int id = 0); | |
104 | ||
105 | // accessors | |
106 | // get the item on which the operation was performed or the newly | |
107 | // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events | |
108 | wxTreeItemId GetItem() const { return m_item; } | |
c801d85f | 109 | |
f135ff73 VZ |
110 | // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously |
111 | // selected item | |
112 | wxTreeItemId GetOldItem() const { return m_itemOld; } | |
c801d85f | 113 | |
f135ff73 VZ |
114 | // the point where the mouse was when the drag operation started (for |
115 | // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only) | |
116 | wxPoint GetPoint() const { return m_pointDrag; } | |
64693098 | 117 | |
f135ff73 VZ |
118 | // keyboard code (for wxEVT_COMMAND_TREE_KEY_DOWN only) |
119 | int GetCode() const { return m_code; } | |
120 | ||
121 | // set return code for wxEVT_COMMAND_TREE_ITEM_{EXPAND|COLLAPS}ING events | |
122 | // call this to forbid the change in item status | |
123 | void Veto() { m_code = TRUE; } | |
124 | ||
125 | // for implementation usage only | |
126 | bool WasVetoed() const { return m_code; } | |
127 | ||
128 | private: | |
129 | // @@ we could save some space by using union here | |
130 | int m_code; | |
131 | wxTreeItemId m_item, | |
132 | m_itemOld; | |
133 | wxPoint m_pointDrag; | |
134 | ||
135 | DECLARE_DYNAMIC_CLASS(wxTreeEvent) | |
c801d85f KB |
136 | }; |
137 | ||
138 | typedef void (wxEvtHandler::*wxTreeEventFunction)(wxTreeEvent&); | |
139 | ||
f135ff73 VZ |
140 | // ---------------------------------------------------------------------------- |
141 | // macros for handling tree control events | |
142 | // ---------------------------------------------------------------------------- | |
143 | ||
144 | // GetItem() returns the item being dragged, GetPoint() the mouse coords | |
c67daf87 UR |
145 | #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, |
146 | #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, | |
f135ff73 VZ |
147 | |
148 | // GetItem() returns the itme whose label is being edited | |
c67daf87 UR |
149 | #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, |
150 | #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, | |
f135ff73 VZ |
151 | |
152 | // provide/update information about GetItem() item | |
c67daf87 UR |
153 | #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, |
154 | #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, | |
f135ff73 VZ |
155 | |
156 | // GetItem() is the item being expanded/collapsed, the "ING" versions can use | |
c67daf87 UR |
157 | #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, |
158 | #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, | |
159 | #define EVT_TREE_ITEM_COLLAPSED(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, | |
f135ff73 | 160 | #define EVT_TREE_ITEM_COLLAPSING(id, fn) { wxEVT_COMMAND_TREE_ITEM_COLLAPSING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, |
c801d85f | 161 | |
f135ff73 VZ |
162 | // GetOldItem() is the item which had the selection previously, GetItem() is |
163 | // the item which acquires selection | |
164 | #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, | |
165 | #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, | |
c801d85f | 166 | |
f135ff73 VZ |
167 | // GetCode() returns the key code |
168 | // NB: this is the only message for which GetItem() is invalid (you may get the | |
169 | // item from GetSelection()) | |
170 | #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL }, | |
c801d85f | 171 | |
f135ff73 VZ |
172 | // GetItem() returns the item being deleted, the associated data (if any) will |
173 | // be deleted just after the return of this event handler (if any) | |
174 | #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, (wxObject *) NULL }, | |
175 | ||
176 | // ----------------------------------------------------------------------------- | |
177 | // wxTreeCtrl - the tree control | |
178 | // ----------------------------------------------------------------------------- | |
c801d85f | 179 | |
f135ff73 | 180 | class WXDLLEXPORT wxTreeCtrl : public wxScrolledWindow |
c801d85f | 181 | { |
a32dd690 | 182 | public: |
f135ff73 VZ |
183 | // creation |
184 | // -------- | |
185 | wxTreeCtrl() { Init(); } | |
186 | ||
187 | wxTreeCtrl(wxWindow *parent, wxWindowID id = -1, | |
188 | const wxPoint& pos = wxDefaultPosition, | |
189 | const wxSize& size = wxDefaultSize, | |
190 | long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, | |
191 | const wxString& name = "wxTreeCtrl") | |
192 | { | |
193 | Create(parent, id, pos, size, style, name); | |
194 | } | |
195 | ||
196 | virtual ~wxTreeCtrl(); | |
197 | ||
198 | bool Create(wxWindow *parent, wxWindowID id = -1, | |
199 | const wxPoint& pos = wxDefaultPosition, | |
200 | const wxSize& size = wxDefaultSize, | |
201 | long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT, | |
202 | const wxString& name = "wxTreeCtrl"); | |
203 | ||
204 | // accessors | |
205 | // --------- | |
206 | ||
207 | // get the total number of items in the control | |
208 | size_t GetCount() const; | |
209 | ||
210 | // indent is the number of pixels the children are indented relative to | |
211 | // the parents position. SetIndent() also redraws the control | |
212 | // immediately. | |
213 | unsigned int GetIndent() const { return m_indent; } | |
214 | void SetIndent(unsigned int indent); | |
215 | ||
216 | // image list: these functions allow to associate an image list with | |
217 | // the control and retrieve it. Note that the control does _not_ delete | |
218 | // the associated image list when it's deleted in order to allow image | |
219 | // lists to be shared between different controls. | |
220 | // | |
221 | // The normal image list is for the icons which correspond to the | |
222 | // normal tree item state (whether it is selected or not). | |
223 | // Additionally, the application might choose to show a state icon | |
224 | // which corresponds to an app-defined item state (for example, | |
225 | // checked/unchecked) which are taken from the state image list. | |
226 | wxImageList *GetImageList() const; | |
227 | wxImageList *GetStateImageList() const; | |
228 | ||
229 | void SetImageList(wxImageList *imageList); | |
230 | void SetStateImageList(wxImageList *imageList); | |
231 | ||
232 | // Functions to work with tree ctrl items. | |
233 | ||
234 | // accessors | |
235 | // --------- | |
236 | ||
237 | // retrieve items label | |
238 | wxString GetItemText(const wxTreeItemId& item) const; | |
239 | // get the normal item image | |
240 | int GetItemImage(const wxTreeItemId& item) const; | |
241 | // get the selected item image | |
242 | int GetItemSelectedImage(const wxTreeItemId& item) const; | |
243 | // get the data associated with the item | |
244 | wxTreeItemData *GetItemData(const wxTreeItemId& item) const; | |
245 | ||
246 | // modifiers | |
247 | // --------- | |
248 | ||
249 | // set items label | |
250 | void SetItemText(const wxTreeItemId& item, const wxString& text); | |
251 | // set the normal item image | |
252 | void SetItemImage(const wxTreeItemId& item, int image); | |
253 | // set the selected item image | |
254 | void SetItemSelectedImage(const wxTreeItemId& item, int image); | |
255 | // associate some data with the item | |
256 | void SetItemData(const wxTreeItemId& item, wxTreeItemData *data); | |
257 | ||
258 | // force appearance of [+] button near the item. This is useful to | |
259 | // allow the user to expand the items which don't have any children now | |
260 | // - but instead add them only when needed, thus minimizing memory | |
261 | // usage and loading time. | |
262 | void SetItemHasChildren(const wxTreeItemId& item, bool has = TRUE); | |
263 | ||
264 | // item status inquiries | |
265 | // --------------------- | |
266 | ||
267 | // is the item visible (it might be outside the view or not expanded)? | |
268 | bool IsVisible(const wxTreeItemId& item) const; | |
269 | // does the item has any children? | |
270 | bool ItemHasChildren(const wxTreeItemId& item) const; | |
271 | // is the item expanded (only makes sense if HasChildren())? | |
272 | bool IsExpanded(const wxTreeItemId& item) const; | |
273 | // is this item currently selected (the same as has focus)? | |
274 | bool IsSelected(const wxTreeItemId& item) const; | |
275 | ||
276 | // navigation | |
277 | // ---------- | |
278 | ||
279 | // wxTreeItemId.IsOk() will return FALSE if there is no such item | |
280 | ||
281 | // get the root tree item | |
282 | wxTreeItemId GetRootItem() const { return m_anchor; } | |
283 | ||
284 | // get the item currently selected (may return NULL if no selection) | |
285 | wxTreeItemId GetSelection() const { return m_current; } | |
286 | ||
287 | // get the parent of this item (may return NULL if root) | |
288 | wxTreeItemId GetParent(const wxTreeItemId& item) const; | |
289 | ||
290 | // for this enumeration function you must pass in a "cookie" parameter | |
291 | // which is opaque for the application but is necessary for the library | |
292 | // to make these functions reentrant (i.e. allow more than one | |
293 | // enumeration on one and the same object simultaneously). Of course, | |
294 | // the "cookie" passed to GetFirstChild() and GetNextChild() should be | |
295 | // the same! | |
296 | ||
297 | // get the first child of this item | |
298 | wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const; | |
299 | // get the next child | |
300 | wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const; | |
301 | ||
302 | // get the next sibling of this item | |
303 | wxTreeItemId GetNextSibling(const wxTreeItemId& item) const; | |
304 | // get the previous sibling | |
305 | wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const; | |
306 | ||
307 | // get first visible item | |
308 | wxTreeItemId GetFirstVisibleItem() const; | |
309 | // get the next visible item: item must be visible itself! | |
310 | // see IsVisible() and wxTreeCtrl::GetFirstVisibleItem() | |
311 | wxTreeItemId GetNextVisible(const wxTreeItemId& item) const; | |
312 | // get the previous visible item: item must be visible itself! | |
313 | wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const; | |
314 | ||
315 | // operations | |
316 | // ---------- | |
317 | ||
318 | // add the root node to the tree | |
319 | wxTreeItemId AddRoot(const wxString& text, | |
320 | int image = -1, int selectedImage = -1, | |
321 | wxTreeItemData *data = NULL); | |
322 | ||
323 | // insert a new item in as the first child of the parent | |
324 | wxTreeItemId PrependItem(const wxTreeItemId& parent, | |
325 | const wxString& text, | |
326 | int image = -1, int selectedImage = -1, | |
327 | wxTreeItemData *data = NULL); | |
328 | ||
329 | // insert a new item after a given one | |
330 | wxTreeItemId InsertItem(const wxTreeItemId& parent, | |
331 | const wxTreeItemId& idPrevious, | |
332 | const wxString& text, | |
333 | int image = -1, int selectedImage = -1, | |
334 | wxTreeItemData *data = NULL); | |
335 | ||
336 | // insert a new item in as the last child of the parent | |
337 | wxTreeItemId AppendItem(const wxTreeItemId& parent, | |
338 | const wxString& text, | |
339 | int image = -1, int selectedImage = -1, | |
340 | wxTreeItemData *data = NULL); | |
341 | ||
342 | // delete this item and associated data if any | |
343 | void Delete(const wxTreeItemId& item); | |
344 | // delete all items from the tree | |
345 | void DeleteAllItems(); | |
346 | ||
347 | // expand this item | |
348 | void Expand(const wxTreeItemId& item); | |
349 | // collapse the item without removing its children | |
350 | void Collapse(const wxTreeItemId& item); | |
351 | // collapse the item and remove all children | |
352 | void CollapseAndReset(const wxTreeItemId& item); | |
353 | // toggles the current state | |
354 | void Toggle(const wxTreeItemId& item); | |
355 | ||
356 | // remove the selection from currently selected item (if any) | |
357 | void Unselect(); | |
358 | // select this item | |
359 | void SelectItem(const wxTreeItemId& item); | |
360 | // make sure this item is visible (expanding the parent item and/or | |
361 | // scrolling to this item if necessary) | |
362 | void EnsureVisible(const wxTreeItemId& item); | |
363 | // scroll to this item (but don't expand its parent) | |
364 | void ScrollTo(const wxTreeItemId& item); | |
365 | ||
366 | // start editing the item label: this (temporarily) replaces the item | |
367 | // with a one line edit control. The item will be selected if it hadn't | |
368 | // been before. textCtrlClass parameter allows you to create an edit | |
369 | // control of arbitrary user-defined class deriving from wxTextCtrl. | |
370 | wxTextCtrl* EditLabel(const wxTreeItemId& item, | |
371 | wxClassInfo* textCtrlClass = CLASSINFO(wxTextCtrl)); | |
372 | // returns the same pointer as StartEdit() if the item is being edited, | |
373 | // NULL otherwise (it's assumed that no more than one item may be | |
374 | // edited simultaneously) | |
375 | wxTextCtrl* GetEditControl() const; | |
376 | // end editing and accept or discard the changes to item label | |
377 | void EndEditLabel(const wxTreeItemId& item, bool discardChanges = FALSE); | |
378 | ||
379 | // sort the children of this item using the specified callback function | |
380 | // (it should return -1, 0 or +1 as usual), if it's not specified | |
381 | // alphabetical comparaison is performed. | |
382 | // | |
383 | // NB: this function is not reentrant! | |
384 | void SortChildren(const wxTreeItemId& item, | |
385 | wxTreeItemCmpFunc *cmpFunction = NULL); | |
386 | ||
387 | // implementation | |
388 | // -------------- | |
389 | // wxWindows callbacks | |
390 | void OnPaint( const wxPaintEvent &event ); | |
391 | void OnSetFocus( const wxFocusEvent &event ); | |
392 | void OnKillFocus( const wxFocusEvent &event ); | |
393 | void OnChar( wxKeyEvent &event ); | |
394 | void OnMouse( const wxMouseEvent &event ); | |
395 | ||
396 | protected: | |
397 | wxGenericTreeItem *m_anchor; | |
398 | wxGenericTreeItem *m_current; | |
399 | bool m_hasFocus; | |
400 | int m_xScroll,m_yScroll; | |
401 | unsigned int m_indent; | |
402 | int m_lineHeight; | |
403 | wxPen m_dottedPen; | |
404 | wxBrush *m_hilightBrush; | |
405 | wxImageList *m_imageListNormal, | |
406 | *m_imageListState; | |
407 | ||
408 | // the common part of all ctors | |
409 | void Init(); | |
410 | ||
411 | // misc helpers | |
412 | wxTreeItemId DoInsertItem(const wxTreeItemId& parent, | |
413 | size_t previous, | |
414 | const wxString& text, | |
415 | int image, int selectedImage, | |
416 | wxTreeItemData *data); | |
417 | ||
418 | void AdjustMyScrollbars(); | |
419 | void PaintLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ); | |
420 | ||
421 | void CalculateLevel( wxGenericTreeItem *item, wxDC &dc, int level, int &y ); | |
422 | void CalculatePositions(); | |
423 | ||
424 | void RefreshSubtree( wxGenericTreeItem *item ); | |
425 | void RefreshLine( wxGenericTreeItem *item ); | |
43fa96a8 | 426 | |
a32dd690 | 427 | private: |
f135ff73 VZ |
428 | DECLARE_EVENT_TABLE() |
429 | DECLARE_DYNAMIC_CLASS(wxTreeCtrl) | |
c801d85f KB |
430 | }; |
431 | ||
f135ff73 VZ |
432 | #endif // _GENERIC_TREECTRL_H_ |
433 |