added more methods to transparently convert wxTreeItemIdValue to wxTreeItemId to...
[wxWidgets.git] / include / wx / treebase.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: treebase.h
3 // Purpose: wxTreeCtrl base classes and types
4 // Author: Julian Smart et al
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 _WX_TREEBASE_H_
13 #define _WX_TREEBASE_H_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #include "wx/defs.h"
20
21 #if wxUSE_TREECTRL
22
23 #include "wx/window.h" // for wxClientData
24 #include "wx/event.h"
25 #include "wx/dynarray.h"
26
27 // flags for deprecated `Expand(int action)', will be removed in next versions
28 enum
29 {
30 wxTREE_EXPAND_EXPAND,
31 wxTREE_EXPAND_COLLAPSE,
32 wxTREE_EXPAND_COLLAPSE_RESET,
33 wxTREE_EXPAND_TOGGLE
34 };
35
36 // ----------------------------------------------------------------------------
37 // wxTreeItemId identifies an element of the tree. In this implementation, it's
38 // just a trivial wrapper around Win32 HTREEITEM or a pointer to some private
39 // data structure in the generic version. It's opaque for the application and
40 // the only method which can be used by user code is IsOk().
41 // ----------------------------------------------------------------------------
42
43 // Using this typedef removes an ambiguity when calling Remove()
44 typedef void *wxTreeItemIdValue;
45
46 class WXDLLEXPORT wxTreeItemId
47 {
48 friend bool operator==(const wxTreeItemId&, const wxTreeItemId&);
49 public:
50 // ctors
51 // 0 is invalid value for HTREEITEM
52 wxTreeItemId() { m_pItem = 0; }
53
54 // construct wxTreeItemId from the native item id
55 wxTreeItemId(void *pItem) { m_pItem = pItem; }
56
57 // default copy ctor/assignment operator are ok for us
58
59 // accessors
60 // is this a valid tree item?
61 bool IsOk() const { return m_pItem != 0; }
62 // return true if this item is not valid
63 bool operator!() const { return !IsOk(); }
64
65 // operations
66 // invalidate the item
67 void Unset() { m_pItem = 0; }
68
69 #if WXWIN_COMPATIBILITY_2_4
70 // deprecated: only for compatibility, don't work on 64 bit archs
71 wxTreeItemId(long item) { m_pItem = wxUIntToPtr(item); }
72 operator long() const { return (long)wxPtrToUInt(m_pItem); }
73 #else // !WXWIN_COMPATIBILITY_2_4
74 operator bool() const { return IsOk(); }
75 #endif // WXWIN_COMPATIBILITY_2_4/!WXWIN_COMPATIBILITY_2_4
76
77 wxTreeItemIdValue m_pItem;
78 };
79
80 inline bool operator==(const wxTreeItemId& i1, const wxTreeItemId& i2)
81 {
82 return i1.m_pItem == i2.m_pItem;
83 }
84
85 inline bool operator!=(const wxTreeItemId& i1, const wxTreeItemId& i2)
86 {
87 return i1.m_pItem != i2.m_pItem;
88 }
89
90 // ----------------------------------------------------------------------------
91 // wxTreeItemData is some (arbitrary) user class associated with some item. The
92 // main advantage of having this class (compared to old untyped interface) is
93 // that wxTreeItemData's are destroyed automatically by the tree and, as this
94 // class has virtual dtor, it means that the memory will be automatically
95 // freed. OTOH, we don't just use wxObject instead of wxTreeItemData because
96 // the size of this class is critical: in any real application, each tree leaf
97 // will have wxTreeItemData associated with it and number of leaves may be
98 // quite big.
99 //
100 // Because the objects of this class are deleted by the tree, they should
101 // always be allocated on the heap!
102 // ----------------------------------------------------------------------------
103
104 class WXDLLEXPORT wxTreeItemData: public wxClientData
105 {
106 friend class WXDLLEXPORT wxTreeCtrl;
107 friend class WXDLLEXPORT wxGenericTreeCtrl;
108 public:
109 // creation/destruction
110 // --------------------
111 // default ctor
112 wxTreeItemData() { }
113
114 // default copy ctor/assignment operator are ok
115
116 // accessor: get the item associated with us
117 const wxTreeItemId& GetId() const { return m_pItem; }
118 void SetId(const wxTreeItemId& id) { m_pItem = id; }
119
120 protected:
121 wxTreeItemId m_pItem;
122 };
123
124 WX_DEFINE_EXPORTED_ARRAY_PTR(wxTreeItemIdValue, wxArrayTreeItemIdsBase);
125
126 // this is a wrapper around the array class defined above which allow to wok
127 // with vaue of natural wxTreeItemId type instead of using wxTreeItemIdValue
128 // and does it without any loss of efficiency
129 class WXDLLEXPORT wxArrayTreeItemIds : public wxArrayTreeItemIdsBase
130 {
131 public:
132 void Add(const wxTreeItemId& id)
133 { wxArrayTreeItemIdsBase::Add(id.m_pItem); }
134 void Insert(const wxTreeItemId& id, size_t pos)
135 { wxArrayTreeItemIdsBase::Insert(id.m_pItem, pos); }
136 wxTreeItemId Item(size_t i) const
137 { return wxTreeItemId(wxArrayTreeItemIdsBase::Item(i)); }
138 wxTreeItemId operator[](size_t i) const { return Item(i); }
139 };
140
141 // ----------------------------------------------------------------------------
142 // constants
143 // ----------------------------------------------------------------------------
144
145 // enum for different images associated with a treectrl item
146 enum wxTreeItemIcon
147 {
148 wxTreeItemIcon_Normal, // not selected, not expanded
149 wxTreeItemIcon_Selected, // selected, not expanded
150 wxTreeItemIcon_Expanded, // not selected, expanded
151 wxTreeItemIcon_SelectedExpanded, // selected, expanded
152 wxTreeItemIcon_Max
153 };
154
155 // ----------------------------------------------------------------------------
156 // wxTreeCtrl flags
157 // ----------------------------------------------------------------------------
158
159 #define wxTR_NO_BUTTONS 0x0000 // for convenience
160 #define wxTR_HAS_BUTTONS 0x0001 // draw collapsed/expanded btns
161 #define wxTR_NO_LINES 0x0004 // don't draw lines at all
162 #define wxTR_LINES_AT_ROOT 0x0008 // connect top-level nodes
163 #define wxTR_TWIST_BUTTONS 0x0010 // still used by wxTreeListCtrl
164
165 #define wxTR_SINGLE 0x0000 // for convenience
166 #define wxTR_MULTIPLE 0x0020 // can select multiple items
167 #define wxTR_EXTENDED 0x0040 // TODO: allow extended selection
168 #define wxTR_HAS_VARIABLE_ROW_HEIGHT 0x0080 // what it says
169
170 #define wxTR_EDIT_LABELS 0x0200 // can edit item labels
171 #define wxTR_ROW_LINES 0x0400 // put border around items
172 #define wxTR_HIDE_ROOT 0x0800 // don't display root node
173
174 #define wxTR_FULL_ROW_HIGHLIGHT 0x2000 // highlight full horz space
175
176 #ifdef __WXGTK20__
177 #define wxTR_DEFAULT_STYLE (wxTR_HAS_BUTTONS | wxTR_NO_LINES)
178 #else
179 #define wxTR_DEFAULT_STYLE (wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT)
180 #endif
181
182 // deprecated, don't use
183 #define wxTR_MAC_BUTTONS 0
184 #define wxTR_AQUA_BUTTONS 0
185
186
187 // values for the `flags' parameter of wxTreeCtrl::HitTest() which determine
188 // where exactly the specified point is situated:
189
190 static const int wxTREE_HITTEST_ABOVE = 0x0001;
191 static const int wxTREE_HITTEST_BELOW = 0x0002;
192 static const int wxTREE_HITTEST_NOWHERE = 0x0004;
193 // on the button associated with an item.
194 static const int wxTREE_HITTEST_ONITEMBUTTON = 0x0008;
195 // on the bitmap associated with an item.
196 static const int wxTREE_HITTEST_ONITEMICON = 0x0010;
197 // on the indent associated with an item.
198 static const int wxTREE_HITTEST_ONITEMINDENT = 0x0020;
199 // on the label (string) associated with an item.
200 static const int wxTREE_HITTEST_ONITEMLABEL = 0x0040;
201 // on the right of the label associated with an item.
202 static const int wxTREE_HITTEST_ONITEMRIGHT = 0x0080;
203 // on the label (string) associated with an item.
204 static const int wxTREE_HITTEST_ONITEMSTATEICON = 0x0100;
205 // on the left of the wxTreeCtrl.
206 static const int wxTREE_HITTEST_TOLEFT = 0x0200;
207 // on the right of the wxTreeCtrl.
208 static const int wxTREE_HITTEST_TORIGHT = 0x0400;
209 // on the upper part (first half) of the item.
210 static const int wxTREE_HITTEST_ONITEMUPPERPART = 0x0800;
211 // on the lower part (second half) of the item.
212 static const int wxTREE_HITTEST_ONITEMLOWERPART = 0x1000;
213
214 // anywhere on the item
215 static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
216 wxTREE_HITTEST_ONITEMLABEL;
217
218 // tree ctrl default name
219 extern WXDLLEXPORT_DATA(const wxChar*) wxTreeCtrlNameStr;
220
221 // ----------------------------------------------------------------------------
222 // wxTreeItemAttr: a structure containing the visual attributes of an item
223 // ----------------------------------------------------------------------------
224
225 class WXDLLEXPORT wxTreeItemAttr
226 {
227 public:
228 // ctors
229 wxTreeItemAttr() { }
230 wxTreeItemAttr(const wxColour& colText,
231 const wxColour& colBack,
232 const wxFont& font)
233 : m_colText(colText), m_colBack(colBack), m_font(font) { }
234
235 // setters
236 void SetTextColour(const wxColour& colText) { m_colText = colText; }
237 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
238 void SetFont(const wxFont& font) { m_font = font; }
239
240 // accessors
241 bool HasTextColour() const { return m_colText.Ok(); }
242 bool HasBackgroundColour() const { return m_colBack.Ok(); }
243 bool HasFont() const { return m_font.Ok(); }
244
245 const wxColour& GetTextColour() const { return m_colText; }
246 const wxColour& GetBackgroundColour() const { return m_colBack; }
247 const wxFont& GetFont() const { return m_font; }
248
249 private:
250 wxColour m_colText,
251 m_colBack;
252 wxFont m_font;
253 };
254
255 // ----------------------------------------------------------------------------
256 // wxTreeEvent is a special class for all events associated with tree controls
257 //
258 // NB: note that not all accessors make sense for all events, see the event
259 // descriptions below
260 // ----------------------------------------------------------------------------
261
262 class WXDLLEXPORT wxTreeEvent : public wxNotifyEvent
263 {
264 public:
265 wxTreeEvent(wxEventType commandType = wxEVT_NULL, int id = 0);
266 wxTreeEvent(const wxTreeEvent & event);
267
268 virtual wxEvent *Clone() const { return new wxTreeEvent(*this); }
269
270 // accessors
271 // get the item on which the operation was performed or the newly
272 // selected item for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events
273 wxTreeItemId GetItem() const { return m_item; }
274 void SetItem(const wxTreeItemId& item) { m_item = item; }
275
276 // for wxEVT_COMMAND_TREE_SEL_CHANGED/ING events, get the previously
277 // selected item
278 wxTreeItemId GetOldItem() const { return m_itemOld; }
279 void SetOldItem(const wxTreeItemId& item) { m_itemOld = item; }
280
281 // the point where the mouse was when the drag operation started (for
282 // wxEVT_COMMAND_TREE_BEGIN_(R)DRAG events only) or click position
283 wxPoint GetPoint() const { return m_pointDrag; }
284 void SetPoint(const wxPoint& pt) { m_pointDrag = pt; }
285
286 // keyboard data (for wxEVT_COMMAND_TREE_KEY_DOWN only)
287 const wxKeyEvent& GetKeyEvent() const { return m_evtKey; }
288 int GetKeyCode() const { return m_evtKey.GetKeyCode(); }
289 void SetKeyEvent(const wxKeyEvent& evt) { m_evtKey = evt; }
290
291 // label (for EVT_TREE_{BEGIN|END}_LABEL_EDIT only)
292 const wxString& GetLabel() const { return m_label; }
293 void SetLabel(const wxString& label) { m_label = label; }
294
295 // edit cancel flag (for EVT_TREE_{BEGIN|END}_LABEL_EDIT only)
296 bool IsEditCancelled() const { return m_editCancelled; }
297 void SetEditCanceled(bool editCancelled) { m_editCancelled = editCancelled; }
298
299 // Set the tooltip for the item (for EVT\_TREE\_ITEM\_GETTOOLTIP events)
300 void SetToolTip(const wxString& toolTip) { m_label = toolTip; }
301 wxString GetToolTip() { return m_label; }
302
303 #if WXWIN_COMPATIBILITY_2_2
304 // for compatibility only, don't use
305 wxDEPRECATED( int GetCode() const);
306 #endif // WXWIN_COMPATIBILITY_2_2
307
308 private:
309 // not all of the members are used (or initialized) for all events
310 wxKeyEvent m_evtKey;
311 wxTreeItemId m_item,
312 m_itemOld;
313 wxPoint m_pointDrag;
314 wxString m_label;
315 bool m_editCancelled;
316
317 friend class WXDLLEXPORT wxTreeCtrl;
318 friend class WXDLLEXPORT wxGenericTreeCtrl;
319
320 DECLARE_DYNAMIC_CLASS(wxTreeEvent)
321 };
322
323 typedef void (wxEvtHandler::*wxTreeEventFunction)(wxTreeEvent&);
324
325 // ----------------------------------------------------------------------------
326 // tree control events and macros for handling them
327 // ----------------------------------------------------------------------------
328
329 BEGIN_DECLARE_EVENT_TYPES()
330 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_DRAG, 600)
331 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_RDRAG, 601)
332 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, 602)
333 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_LABEL_EDIT, 603)
334 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_DELETE_ITEM, 604)
335 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_GET_INFO, 605)
336 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_SET_INFO, 606)
337 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDED, 607)
338 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDING, 608)
339 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, 609)
340 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, 610)
341 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGED, 611)
342 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGING, 612)
343 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_KEY_DOWN, 613)
344 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, 614)
345 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, 615)
346 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK, 616)
347 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_DRAG, 617)
348 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK, 618)
349 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP, 619)
350 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MENU, 620)
351 END_DECLARE_EVENT_TYPES()
352
353 #define wxTreeEventHandler(func) \
354 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTreeEventFunction, &func)
355
356 #define wx__DECLARE_TREEEVT(evt, id, fn) \
357 wx__DECLARE_EVT1(wxEVT_COMMAND_TREE_ ## evt, id, wxTreeEventHandler(fn))
358
359 // GetItem() returns the item being dragged, GetPoint() the mouse coords
360 //
361 // if you call event.Allow(), the drag operation will start and a
362 // EVT_TREE_END_DRAG event will be sent when the drag is over.
363 #define EVT_TREE_BEGIN_DRAG(id, fn) wx__DECLARE_TREEEVT(BEGIN_DRAG, id, fn)
364 #define EVT_TREE_BEGIN_RDRAG(id, fn) wx__DECLARE_TREEEVT(BEGIN_RDRAG, id, fn)
365
366 // GetItem() is the item on which the drop occurred (if any) and GetPoint() the
367 // current mouse coords
368 #define EVT_TREE_END_DRAG(id, fn) wx__DECLARE_TREEEVT(END_DRAG, id, fn)
369
370 // GetItem() returns the itme whose label is being edited, GetLabel() returns
371 // the current item label for BEGIN and the would be new one for END.
372 //
373 // Vetoing BEGIN event means that label editing won't happen at all,
374 // vetoing END means that the new value is discarded and the old one kept
375 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) wx__DECLARE_TREEEVT(BEGIN_LABEL_EDIT, id, fn)
376 #define EVT_TREE_END_LABEL_EDIT(id, fn) wx__DECLARE_TREEEVT(END_LABEL_EDIT, id, fn)
377
378 // provide/update information about GetItem() item
379 #define EVT_TREE_GET_INFO(id, fn) wx__DECLARE_TREEEVT(GET_INFO, id, fn)
380 #define EVT_TREE_SET_INFO(id, fn) wx__DECLARE_TREEEVT(SET_INFO, id, fn)
381
382 // GetItem() is the item being expanded/collapsed, the "ING" versions can use
383 #define EVT_TREE_ITEM_EXPANDED(id, fn) wx__DECLARE_TREEEVT(ITEM_EXPANDED, id, fn)
384 #define EVT_TREE_ITEM_EXPANDING(id, fn) wx__DECLARE_TREEEVT(ITEM_EXPANDING, id, fn)
385 #define EVT_TREE_ITEM_COLLAPSED(id, fn) wx__DECLARE_TREEEVT(ITEM_COLLAPSED, id, fn)
386 #define EVT_TREE_ITEM_COLLAPSING(id, fn) wx__DECLARE_TREEEVT(ITEM_COLLAPSING, id, fn)
387
388 // GetOldItem() is the item which had the selection previously, GetItem() is
389 // the item which acquires selection
390 #define EVT_TREE_SEL_CHANGED(id, fn) wx__DECLARE_TREEEVT(SEL_CHANGED, id, fn)
391 #define EVT_TREE_SEL_CHANGING(id, fn) wx__DECLARE_TREEEVT(SEL_CHANGING, id, fn)
392
393 // GetKeyCode() returns the key code
394 // NB: this is the only message for which GetItem() is invalid (you may get the
395 // item from GetSelection())
396 #define EVT_TREE_KEY_DOWN(id, fn) wx__DECLARE_TREEEVT(KEY_DOWN, id, fn)
397
398 // GetItem() returns the item being deleted, the associated data (if any) will
399 // be deleted just after the return of this event handler (if any)
400 #define EVT_TREE_DELETE_ITEM(id, fn) wx__DECLARE_TREEEVT(DELETE_ITEM, id, fn)
401
402 // GetItem() returns the item that was activated (double click, enter, space)
403 #define EVT_TREE_ITEM_ACTIVATED(id, fn) wx__DECLARE_TREEEVT(ITEM_ACTIVATED, id, fn)
404
405 // GetItem() returns the item for which the context menu shall be shown
406 #define EVT_TREE_ITEM_MENU(id, fn) wx__DECLARE_TREEEVT(ITEM_MENU, id, fn)
407
408 // GetItem() returns the item that was clicked on
409 #define EVT_TREE_ITEM_RIGHT_CLICK(id, fn) wx__DECLARE_TREEEVT(ITEM_RIGHT_CLICK, id, fn)
410 #define EVT_TREE_ITEM_MIDDLE_CLICK(id, fn) wx__DECLARE_TREEEVT(ITEM_MIDDLE_CLICK, id, fn)
411
412 // GetItem() returns the item whose state image was clicked on
413 #define EVT_TREE_STATE_IMAGE_CLICK(id, fn) wx__DECLARE_TREEEVT(STATE_IMAGE_CLICK, id, fn)
414
415 // GetItem() is the item for which the tooltip is being requested
416 #define EVT_TREE_ITEM_GETTOOLTIP(id, fn) wx__DECLARE_TREEEVT(ITEM_GETTOOLTIP, id, fn)
417
418 #endif // wxUSE_TREECTRL
419
420 #endif // _WX_TREEBASE_H_
421