]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/treectrl.h
many changes to make generic tree control (more) MSW compatible:
[wxWidgets.git] / include / wx / generic / treectrl.h
1 /*
2 * Author: Robert Roebling
3 *
4 * Copyright: (C) 1997,1998 Robert Roebling
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the wxWindows Licence, which
8 * you have received with this library (see Licence.htm).
9 *
10 */
11
12 #ifndef __GTKTREECTRLH_G__
13 #define __GTKTREECTRLH_G__
14
15 #ifdef __GNUG__
16 #pragma interface
17 #endif
18
19 #include "wx/defs.h"
20 #include "wx/string.h"
21 #include "wx/list.h"
22 #include "wx/control.h"
23 #include "wx/event.h"
24 #include "wx/imaglist.h"
25 #include "wx/scrolwin.h"
26 #include "wx/dcclient.h"
27
28 //-----------------------------------------------------------------------------
29 // classes
30 //-----------------------------------------------------------------------------
31
32 class wxTreeItem;
33 class wxTreeEvent;
34
35 class wxGenericTreeItem;
36 class wxTreeCtrl;
37
38 class wxImageList;
39
40 //-----------------------------------------------------------------------------
41 // constants
42 //-----------------------------------------------------------------------------
43
44 // WXDLLEXPORT extern const char *wxTreeNameStr;
45
46 #define wxTREE_MASK_HANDLE 0x0001
47 #define wxTREE_MASK_STATE 0x0002
48 #define wxTREE_MASK_TEXT 0x0004
49 #define wxTREE_MASK_IMAGE 0x0008
50 #define wxTREE_MASK_SELECTED_IMAGE 0x0010
51 #define wxTREE_MASK_CHILDREN 0x0020
52 #define wxTREE_MASK_DATA 0x0040
53
54 #define wxTREE_STATE_BOLD 0x0001
55 #define wxTREE_STATE_DROPHILITED 0x0002
56 #define wxTREE_STATE_EXPANDED 0x0004
57 #define wxTREE_STATE_EXPANDEDONCE 0x0008
58 #define wxTREE_STATE_FOCUSED 0x0010
59 #define wxTREE_STATE_SELECTED 0x0020
60 #define wxTREE_STATE_CUT 0x0040
61
62 #define wxTREE_HITTEST_ABOVE 0x0001 // Above the client area.
63 #define wxTREE_HITTEST_BELOW 0x0002 // Below the client area.
64 #define wxTREE_HITTEST_NOWHERE 0x0004 // In the client area but below the last item.
65 #define wxTREE_HITTEST_ONITEMBUTTON 0x0010 // On the button associated with an item.
66 #define wxTREE_HITTEST_ONITEMICON 0x0020 // On the bitmap associated with an item.
67 #define wxTREE_HITTEST_ONITEMINDENT 0x0040 // In the indentation associated with an item.
68 #define wxTREE_HITTEST_ONITEMLABEL 0x0080 // On the label (string) associated with an item.
69 #define wxTREE_HITTEST_ONITEMRIGHT 0x0100 // In the area to the right of an item.
70 #define wxTREE_HITTEST_ONITEMSTATEICON 0x0200 // On the state icon for a tree view item that is in a user-defined state.
71 #define wxTREE_HITTEST_TOLEFT 0x0400 // To the right of the client area.
72 #define wxTREE_HITTEST_TORIGHT 0x0800 // To the left of the client area.
73
74 #define wxTREE_HITTEST_ONITEM (wxTREE_HITTEST_ONITEMICON | wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMSTATEICON)
75
76 // Flags for GetNextItem
77 enum {
78 wxTREE_NEXT_CARET, // Retrieves the currently selected item.
79 wxTREE_NEXT_CHILD, // Retrieves the first child item. The hItem parameter must be NULL.
80 wxTREE_NEXT_DROPHILITE, // Retrieves the item that is the target of a drag-and-drop operation.
81 wxTREE_NEXT_FIRSTVISIBLE, // Retrieves the first visible item.
82 wxTREE_NEXT_NEXT, // Retrieves the next sibling item.
83 wxTREE_NEXT_NEXTVISIBLE, // Retrieves the next visible item that follows the specified item.
84 wxTREE_NEXT_PARENT, // Retrieves the parent of the specified item.
85 wxTREE_NEXT_PREVIOUS, // Retrieves the previous sibling item.
86 wxTREE_NEXT_PREVIOUSVISIBLE, // Retrieves the first visible item that precedes the specified item.
87 wxTREE_NEXT_ROOT // Retrieves the first child item of the root item of which the specified item is a part.
88 };
89
90 // Flags for ExpandItem
91 enum {
92 wxTREE_EXPAND_EXPAND,
93 wxTREE_EXPAND_COLLAPSE,
94 wxTREE_EXPAND_COLLAPSE_RESET,
95 wxTREE_EXPAND_TOGGLE
96 };
97
98 // Flags for InsertItem
99 enum {
100 wxTREE_INSERT_LAST = -1,
101 wxTREE_INSERT_FIRST = -2,
102 wxTREE_INSERT_SORT = -3
103 };
104
105 /* defined in "wx/event.h"
106 wxEVT_COMMAND_TREE_BEGIN_DRAG,
107 wxEVT_COMMAND_TREE_BEGIN_RDRAG,
108 wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT,
109 wxEVT_COMMAND_TREE_END_LABEL_EDIT,
110 wxEVT_COMMAND_TREE_DELETE_ITEM,
111 wxEVT_COMMAND_TREE_GET_INFO,
112 wxEVT_COMMAND_TREE_SET_INFO,
113 wxEVT_COMMAND_TREE_ITEM_EXPANDED,
114 wxEVT_COMMAND_TREE_ITEM_EXPANDING,
115 wxEVT_COMMAND_TREE_SEL_CHANGED,
116 wxEVT_COMMAND_TREE_SEL_CHANGING,
117 wxEVT_COMMAND_TREE_KEY_DOWN
118 */
119
120 //-----------------------------------------------------------------------------
121 // wxTreeItem
122 //-----------------------------------------------------------------------------
123
124 class WXDLLEXPORT wxTreeItem: public wxObject
125 {
126 DECLARE_DYNAMIC_CLASS(wxTreeItem)
127
128 public:
129 long m_mask;
130 long m_itemId;
131 long m_state;
132 long m_stateMask;
133 wxString m_text;
134 int m_image;
135 int m_selectedImage;
136 int m_children;
137 long m_data;
138
139 wxTreeItem(void);
140
141 // Accessors
142 inline long GetMask() const { return m_mask; }
143 inline long GetItemId() const { return m_itemId; }
144 inline long GetState() const { return m_state; }
145 inline long GetStateMask() const { return m_stateMask; }
146 inline wxString GetText() const { return m_text; }
147 inline int GetImage() const { return m_image; }
148 inline int GetSelectedImage() const { return m_selectedImage; }
149 inline int GetChildren() const { return m_children; }
150 inline long GetData() const { return m_data; }
151
152 inline void SetMask(long mask) { m_mask = mask; }
153 inline void SetItemId(long id) { m_itemId = m_itemId = id; }
154 inline void GetState(long state) { m_state = state; }
155 inline void SetStateMask(long stateMask) { m_stateMask = stateMask; }
156 inline void GetText(const wxString& text) { m_text = text; }
157 inline void SetImage(int image) { m_image = image; }
158 inline void SetSelectedImage(int selImage) { m_selectedImage = selImage; }
159 inline void SetChildren(int children) { m_children = children; }
160 inline void SetData(long data) { m_data = data; }
161 };
162
163 //-----------------------------------------------------------------------------
164 // wxTreeEvent
165 //-----------------------------------------------------------------------------
166
167 class WXDLLEXPORT wxTreeEvent: public wxCommandEvent
168 {
169 DECLARE_DYNAMIC_CLASS(wxTreeEvent)
170
171 public:
172 wxTreeEvent(wxEventType commandType = wxEVT_NULL, int id = 0);
173
174 int m_code;
175 wxTreeItem m_item;
176 long m_oldItem;
177 wxPoint m_pointDrag;
178
179 inline long GetOldItem() const { return m_oldItem; }
180 inline wxTreeItem& GetItem() const { return (wxTreeItem&) m_item; }
181 inline wxPoint GetPoint() const { return m_pointDrag; }
182 inline void SetCode(int code) { m_code = code; }
183 inline int GetCode() const { return m_code; }
184 };
185
186 typedef void (wxEvtHandler::*wxTreeEventFunction)(wxTreeEvent&);
187
188 #define EVT_TREE_BEGIN_DRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_DRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
189 #define EVT_TREE_BEGIN_RDRAG(id, fn) { wxEVT_COMMAND_TREE_BEGIN_RDRAG, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
190 #define EVT_TREE_BEGIN_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
191 #define EVT_TREE_END_LABEL_EDIT(id, fn) { wxEVT_COMMAND_TREE_END_LABEL_EDIT, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
192 #define EVT_TREE_DELETE_ITEM(id, fn) { wxEVT_COMMAND_TREE_DELETE_ITEM, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
193 #define EVT_TREE_GET_INFO(id, fn) { wxEVT_COMMAND_TREE_GET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
194 #define EVT_TREE_SET_INFO(id, fn) { wxEVT_COMMAND_TREE_SET_INFO, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
195 #define EVT_TREE_ITEM_EXPANDED(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
196 #define EVT_TREE_ITEM_EXPANDING(id, fn) { wxEVT_COMMAND_TREE_ITEM_EXPANDING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
197 #define EVT_TREE_SEL_CHANGED(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGED, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
198 #define EVT_TREE_SEL_CHANGING(id, fn) { wxEVT_COMMAND_TREE_SEL_CHANGING, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
199 #define EVT_TREE_KEY_DOWN(id, fn) { wxEVT_COMMAND_TREE_KEY_DOWN, id, -1, (wxObjectEventFunction) (wxEventFunction) (wxTreeEventFunction) & fn, NULL },
200
201 //-----------------------------------------------------------------------------
202 // wxGenericTreeItem
203 //-----------------------------------------------------------------------------
204
205 class WXDLLEXPORT wxGenericTreeItem: public wxTreeItem
206 {
207 DECLARE_DYNAMIC_CLASS(wxGenericTreeItem)
208
209 public:
210 bool m_hasChildren,
211 m_isCollapsed;
212
213 int m_x,m_y;
214 int m_height,m_width;
215 int m_xCross,m_yCross;
216 int m_level;
217 wxList m_children;
218 wxGenericTreeItem *m_parent;
219 bool m_hasHilight;
220
221 wxGenericTreeItem(void) {};
222 wxGenericTreeItem( wxGenericTreeItem *parent );
223 wxGenericTreeItem( wxGenericTreeItem *parent, const wxTreeItem& item, wxDC *dc );
224 void SetItem( const wxTreeItem &item, wxDC *dc );
225 void SetText( const wxString &text, wxDC *dc );
226 void Reset(void);
227 void GetItem( wxTreeItem &item ) const;
228 void AddChild( const wxTreeItem &item );
229 bool HasChildren(void);
230 bool HasPlus(void);
231 int NumberOfVisibleDescendents(void);
232 int NumberOfVisibleChildren(void);
233 wxGenericTreeItem *FindItem( long itemId ) const;
234 void AddChild( wxGenericTreeItem *child );
235 void SetCross( int x, int y );
236 void GetSize( int &x, int &y );
237 long HitTest( const wxPoint& point, int &flags );
238 void PrepareEvent( wxTreeEvent &event );
239 void SendKeyDown( wxWindow *target );
240 void SendSelected( wxWindow *target );
241 void SendDelete( wxWindow *target );
242 void SendExpand( wxWindow *target );
243 void SendCollapse( wxWindow *target );
244 void SetHilight( bool set = TRUE );
245 bool HasHilight(void);
246 bool IsExpanded() const { return !m_isCollapsed; }
247 };
248
249 //-----------------------------------------------------------------------------
250 // wxTreeCtrl
251 //-----------------------------------------------------------------------------
252
253 class wxTreeCtrl: public wxScrolledWindow
254 {
255 DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
256
257 public:
258
259 wxTreeCtrl(void);
260 wxTreeCtrl(wxWindow *parent, const wxWindowID id = -1,
261 const wxPoint& pos = wxDefaultPosition,
262 const wxSize& size = wxDefaultSize,
263 long style = wxTR_HAS_BUTTONS,
264 const wxString& name = "wxTreeCtrl" );
265 ~wxTreeCtrl(void);
266 bool Create(wxWindow *parent, const wxWindowID id = -1,
267 const wxPoint& pos = wxDefaultPosition,
268 const wxSize& size = wxDefaultSize,
269 long style = wxTR_HAS_BUTTONS,
270 const wxString& name = "wxTreeCtrl");
271
272 int GetCount(void) const;
273 long InsertItem( long parent, const wxString& label, const int image = -1,
274 const int selImage = -1, long insertAfter = wxTREE_INSERT_LAST );
275 long InsertItem( long parent, wxTreeItem &info, long insertAfter = wxTREE_INSERT_LAST );
276 bool DeleteAllItems(void);
277 bool ExpandItem( long item, const int action );
278 bool GetItem( wxTreeItem &info ) const;
279 long GetItemData( long item ) const;
280 wxString GetItemText( long item ) const;
281 int GetItemImage(long item) const;
282 long GetParent( long item ) const;
283 long GetRootItem(void) const;
284 long GetSelection(void) const;
285 bool SelectItem( long item ) const;
286 bool ItemHasChildren( long item ) const;
287 void SetIndent( const int indent );
288 int GetIndent(void) const;
289 bool SetItem( wxTreeItem &info );
290 bool SetItemData( long item, long data );
291 bool SetItemText( long item, const wxString &text );
292 void SetItemImage(long item, int image, int imageSel) const;
293 long HitTest( const wxPoint& point, int &flags );
294
295 void AdjustMyScrollbars(void);
296 void PaintLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y );
297 void OnPaint( const wxPaintEvent &event );
298 void OnSetFocus( const wxFocusEvent &event );
299 void OnKillFocus( const wxFocusEvent &event );
300 void OnChar( wxKeyEvent &event );
301 void OnMouse( const wxMouseEvent &event );
302
303 void SetImageList(wxImageList *imageList) { m_imageList = imageList; }
304 wxImageList *GetImageList() const { return m_imageList; }
305
306 private:
307
308 wxGenericTreeItem *m_anchor;
309 wxGenericTreeItem *m_current;
310 bool m_hasFocus;
311 int m_xScroll,m_yScroll;
312 int m_indent;
313 long m_lastId;
314 int m_lineHeight;
315 wxPen m_dottedPen;
316 bool m_isCreated;
317 wxPaintDC *m_dc;
318 wxBrush *m_hilightBrush;
319 wxImageList *m_imageList;
320
321 void CalculateLevel( wxGenericTreeItem *item, wxPaintDC &dc, int level, int &y );
322 void CalculatePositions(void);
323 wxGenericTreeItem *FindItem( long itemId ) const;
324 void RefreshLine( wxGenericTreeItem *item );
325
326 DECLARE_EVENT_TABLE()
327 };
328
329 #endif
330 // __GTKTREECTRLH_G__