1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/msw/treectrl.h
3 // Purpose:     wxTreeCtrl class
4 // Author:      Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10/10/98
6 // Created:     01/02/97
7 // Copyright:   (c) Julian Smart
8 // Licence:     wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 #ifndef _WX_MSW_TREECTRL_H_
12 #define _WX_MSW_TREECTRL_H_
13 
14 // ----------------------------------------------------------------------------
15 // headers
16 // ----------------------------------------------------------------------------
17 
18 #if wxUSE_TREECTRL
19 
20 #include "wx/textctrl.h"
21 #include "wx/dynarray.h"
22 #include "wx/treebase.h"
23 #include "wx/hashmap.h"
24 
25 #ifdef __GNUWIN32__
26     // Cygwin windows.h defines these identifiers
27     #undef GetFirstChild
28     #undef GetNextSibling
29 #endif // Cygwin
30 
31 // fwd decl
32 class  WXDLLIMPEXP_FWD_CORE wxImageList;
33 class  WXDLLIMPEXP_FWD_CORE wxDragImage;
34 struct WXDLLIMPEXP_FWD_CORE wxTreeViewItem;
35 
36 #if WXWIN_COMPATIBILITY_2_6
37     // NB: all the following flags are for compatbility only and will be removed in the
38     //     next versions
39     // flags for deprecated InsertItem() variant (their values are the same as of
40     // TVI_FIRST and TVI_LAST)
41     #define wxTREE_INSERT_FIRST 0xFFFF0001
42     #define wxTREE_INSERT_LAST  0xFFFF0002
43 #endif
44 
45 // hash storing attributes for our items
46 WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr);
47 
48 // ----------------------------------------------------------------------------
49 // wxTreeCtrl
50 // ----------------------------------------------------------------------------
51 
52 class WXDLLIMPEXP_CORE wxTreeCtrl : public wxTreeCtrlBase
53 {
54 public:
55     // creation
56     // --------
wxTreeCtrl()57     wxTreeCtrl() { Init(); }
58 
59     wxTreeCtrl(wxWindow *parent, wxWindowID id = wxID_ANY,
60                const wxPoint& pos = wxDefaultPosition,
61                const wxSize& size = wxDefaultSize,
62                long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
63                const wxValidator& validator = wxDefaultValidator,
64                const wxString& name = wxTreeCtrlNameStr)
65     {
66         Create(parent, id, pos, size, style, validator, name);
67     }
68 
69     virtual ~wxTreeCtrl();
70 
71     bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
72                 const wxPoint& pos = wxDefaultPosition,
73                 const wxSize& size = wxDefaultSize,
74                 long style = wxTR_HAS_BUTTONS | wxTR_LINES_AT_ROOT,
75                 const wxValidator& validator = wxDefaultValidator,
76                 const wxString& name = wxTreeCtrlNameStr);
77 
78     // implement base class pure virtuals
79     // ----------------------------------
80 
81     virtual unsigned int GetCount() const;
82 
83     virtual unsigned int GetIndent() const;
84     virtual void SetIndent(unsigned int indent);
85 
86     virtual void SetImageList(wxImageList *imageList);
87     virtual void SetStateImageList(wxImageList *imageList);
88 
89     virtual wxString GetItemText(const wxTreeItemId& item) const;
90     virtual int GetItemImage(const wxTreeItemId& item,
91                         wxTreeItemIcon which = wxTreeItemIcon_Normal) const;
92     virtual wxTreeItemData *GetItemData(const wxTreeItemId& item) const;
93     virtual wxColour GetItemTextColour(const wxTreeItemId& item) const;
94     virtual wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
95     virtual wxFont GetItemFont(const wxTreeItemId& item) const;
96 
97     virtual void SetItemText(const wxTreeItemId& item, const wxString& text);
98     virtual void SetItemImage(const wxTreeItemId& item, int image,
99                       wxTreeItemIcon which = wxTreeItemIcon_Normal);
100     virtual void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);
101     virtual void SetItemHasChildren(const wxTreeItemId& item, bool has = true);
102     virtual void SetItemBold(const wxTreeItemId& item, bool bold = true);
103     virtual void SetItemDropHighlight(const wxTreeItemId& item,
104                                       bool highlight = true);
105     virtual void SetItemTextColour(const wxTreeItemId& item,
106                                    const wxColour& col);
107     virtual void SetItemBackgroundColour(const wxTreeItemId& item,
108                                          const wxColour& col);
109     virtual void SetItemFont(const wxTreeItemId& item, const wxFont& font);
110 
111     // item status inquiries
112     // ---------------------
113 
114     virtual bool IsVisible(const wxTreeItemId& item) const;
115     virtual bool ItemHasChildren(const wxTreeItemId& item) const;
116     virtual bool IsExpanded(const wxTreeItemId& item) const;
117     virtual bool IsSelected(const wxTreeItemId& item) const;
118     virtual bool IsBold(const wxTreeItemId& item) const;
119 
120     virtual size_t GetChildrenCount(const wxTreeItemId& item,
121                                     bool recursively = true) const;
122 
123     // navigation
124     // ----------
125 
126     virtual wxTreeItemId GetRootItem() const;
127     virtual wxTreeItemId GetSelection() const;
128     virtual size_t GetSelections(wxArrayTreeItemIds& selections) const;
129     virtual wxTreeItemId GetFocusedItem() const;
130 
131     virtual void ClearFocusedItem();
132     virtual void SetFocusedItem(const wxTreeItemId& item);
133 
134 
135     virtual wxTreeItemId GetItemParent(const wxTreeItemId& item) const;
136     virtual wxTreeItemId GetFirstChild(const wxTreeItemId& item,
137                                        wxTreeItemIdValue& cookie) const;
138     virtual wxTreeItemId GetNextChild(const wxTreeItemId& item,
139                                       wxTreeItemIdValue& cookie) const;
140     virtual wxTreeItemId GetLastChild(const wxTreeItemId& item) const;
141 
142     virtual wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
143     virtual wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;
144 
145     virtual wxTreeItemId GetFirstVisibleItem() const;
146     virtual wxTreeItemId GetNextVisible(const wxTreeItemId& item) const;
147     virtual wxTreeItemId GetPrevVisible(const wxTreeItemId& item) const;
148 
149     // operations
150     // ----------
151 
152     virtual wxTreeItemId AddRoot(const wxString& text,
153                          int image = -1, int selectedImage = -1,
154                          wxTreeItemData *data = NULL);
155 
156     virtual void Delete(const wxTreeItemId& item);
157     virtual void DeleteChildren(const wxTreeItemId& item);
158     virtual void DeleteAllItems();
159 
160     virtual void Expand(const wxTreeItemId& item);
161     virtual void Collapse(const wxTreeItemId& item);
162     virtual void CollapseAndReset(const wxTreeItemId& item);
163     virtual void Toggle(const wxTreeItemId& item);
164 
165     virtual void Unselect();
166     virtual void UnselectAll();
167     virtual void SelectItem(const wxTreeItemId& item, bool select = true);
168     virtual void SelectChildren(const wxTreeItemId& parent);
169 
170     virtual void EnsureVisible(const wxTreeItemId& item);
171     virtual void ScrollTo(const wxTreeItemId& item);
172 
173     virtual wxTextCtrl *EditLabel(const wxTreeItemId& item,
174                           wxClassInfo* textCtrlClass = wxCLASSINFO(wxTextCtrl));
175     virtual wxTextCtrl *GetEditControl() const;
176     virtual void EndEditLabel(const wxTreeItemId& WXUNUSED(item),
177                               bool discardChanges = false)
178     {
179         DoEndEditLabel(discardChanges);
180     }
181 
182     virtual void SortChildren(const wxTreeItemId& item);
183 
184     virtual bool GetBoundingRect(const wxTreeItemId& item,
185                                  wxRect& rect,
186                                  bool textOnly = false) const;
187 
188     // implementation
189     // --------------
190 
GetDefaultAttributes()191     virtual wxVisualAttributes GetDefaultAttributes() const
192     {
193         return GetClassDefaultAttributes(GetWindowVariant());
194     }
195 
196     static wxVisualAttributes
197     GetClassDefaultAttributes(wxWindowVariant variant = wxWINDOW_VARIANT_NORMAL);
198 
199 
200     virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
201     virtual WXLRESULT MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
202     virtual bool MSWCommand(WXUINT param, WXWORD id);
203     virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
204     virtual bool MSWShouldPreProcessMessage(WXMSG* msg);
205 
206     // override some base class virtuals
207     virtual bool SetBackgroundColour(const wxColour &colour);
208     virtual bool SetForegroundColour(const wxColour &colour);
209 
210     // returns true if the platform should explicitly apply a theme border
CanApplyThemeBorder()211     virtual bool CanApplyThemeBorder() const { return false; }
212 
213 protected:
214     // Implement "update locking" in a custom way for this control.
215     virtual void DoFreeze();
216     virtual void DoThaw();
217 
218     virtual void DoSetSize(int x, int y,
219                            int width, int height,
220                            int sizeFlags = wxSIZE_AUTO);
221 
222     // SetImageList helper
223     void SetAnyImageList(wxImageList *imageList, int which);
224 
225     // refresh a single item
226     void RefreshItem(const wxTreeItemId& item);
227 
228     // end edit label
229     void DoEndEditLabel(bool discardChanges = false);
230 
231     virtual int DoGetItemState(const wxTreeItemId& item) const;
232     virtual void DoSetItemState(const wxTreeItemId& item, int state);
233 
234     virtual wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
235                                       size_t pos,
236                                       const wxString& text,
237                                       int image, int selectedImage,
238                                       wxTreeItemData *data);
239     virtual wxTreeItemId DoInsertAfter(const wxTreeItemId& parent,
240                                        const wxTreeItemId& idPrevious,
241                                        const wxString& text,
242                                        int image = -1, int selImage = -1,
243                                        wxTreeItemData *data = NULL);
244     virtual wxTreeItemId DoTreeHitTest(const wxPoint& point, int& flags) const;
245 
246     // obtain the user data for the lParam member of TV_ITEM
247     class wxTreeItemParam *GetItemParam(const wxTreeItemId& item) const;
248 
249     // update the event to include the items client data and pass it to
250     // HandleWindowEvent(), return true if it processed it
251     bool HandleTreeEvent(wxTreeEvent& event) const;
252 
253     // pass the event to HandleTreeEvent() and return true if the event was
254     // either unprocessed or not vetoed
IsTreeEventAllowed(wxTreeEvent & event)255     bool IsTreeEventAllowed(wxTreeEvent& event) const
256     {
257         return !HandleTreeEvent(event) || event.IsAllowed();
258     }
259 
260     // generate a wxEVT_KEY_DOWN event from the specified WPARAM/LPARAM values
261     // having the same meaning as for WM_KEYDOWN, return true if it was
262     // processed
263     bool MSWHandleTreeKeyDownEvent(WXWPARAM wParam, WXLPARAM lParam);
264 
265     // handle a key event in a multi-selection control, should be only called
266     // for keys which can be used to change the selection
267     //
268     // return true if the key was processed, false otherwise
269     bool MSWHandleSelectionKey(unsigned vkey);
270 
271 
272     // data used only while editing the item label:
273     wxTextCtrl  *m_textCtrl;        // text control in which it is edited
274     wxTreeItemId m_idEdited;        // the item being edited
275 
276 private:
277     // the common part of all ctors
278     void Init();
279 
280     // helper functions
281     bool DoGetItem(wxTreeViewItem *tvItem) const;
282     void DoSetItem(wxTreeViewItem *tvItem);
283 
284     void DoExpand(const wxTreeItemId& item, int flag);
285 
286     void DoSelectItem(const wxTreeItemId& item, bool select = true);
287     void DoUnselectItem(const wxTreeItemId& item);
288     void DoToggleItemSelection(const wxTreeItemId& item);
289 
290     void DoUnselectAll();
291     void DoSelectChildren(const wxTreeItemId& parent);
292 
293     void DeleteTextCtrl();
294 
295     // return true if the item is the hidden root one (i.e. it's the root item
296     // and the tree has wxTR_HIDE_ROOT style)
297     bool IsHiddenRoot(const wxTreeItemId& item) const;
298 
299 
300     // check if the given flags (taken from TV_HITTESTINFO structure)
301     // indicate a position "on item": this is less trivial than just checking
302     // for TVHT_ONITEM because we consider that points to the left and right of
303     // item text are also "on item" when wxTR_FULL_ROW_HIGHLIGHT is used as the
304     // item visually spans the entire breadth of the window then
305     bool MSWIsOnItem(unsigned flags) const;
306 
307 
308     // the hash storing the items attributes (indexed by item ids)
309     wxMapTreeAttr m_attrs;
310 
311     // true if the hash above is not empty
312     bool m_hasAnyAttr;
313 
314 #if wxUSE_DRAGIMAGE
315     // used for dragging
316     wxDragImage *m_dragImage;
317 #endif
318 
319     // Virtual root item, if wxTR_HIDE_ROOT is set.
320     void* m_pVirtualRoot;
321 
322     // the starting item for selection with Shift
323     wxTreeItemId m_htSelStart, m_htClickedItem;
324     wxPoint m_ptClick;
325 
326     // whether dragging has started
327     bool m_dragStarted;
328 
329     // whether focus was lost between subsequent clicks of a single item
330     bool m_focusLost;
331 
332     // set when we are changing selection ourselves (only used in multi
333     // selection mode)
334     bool m_changingSelection;
335 
336     // whether we need to trigger a state image click event
337     bool m_triggerStateImageClick;
338 
339     // whether we need to deselect other items on mouse up
340     bool m_mouseUpDeselect;
341 
342     // The size to restore the control to when it is thawed, see DoThaw().
343     wxSize m_thawnSize;
344 
345     friend class wxTreeItemIndirectData;
346     friend class wxTreeSortHelper;
347 
348     DECLARE_DYNAMIC_CLASS(wxTreeCtrl)
349     wxDECLARE_NO_COPY_CLASS(wxTreeCtrl);
350 };
351 
352 #endif // wxUSE_TREECTRL
353 
354 #endif // _WX_MSW_TREECTRL_H_
355