]>
Commit | Line | Data |
---|---|---|
b823f5a1 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1e6feb95 | 2 | // Name: src/msw/treectrl.cpp |
b823f5a1 JS |
3 | // Purpose: wxTreeCtrl |
4 | // Author: Julian Smart | |
08b7c251 | 5 | // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98 |
b823f5a1 JS |
6 | // Created: 1997 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
65571936 | 9 | // Licence: wxWindows licence |
b823f5a1 | 10 | ///////////////////////////////////////////////////////////////////////////// |
2bda0e17 | 11 | |
08b7c251 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
1e6feb95 | 19 | |
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
08b7c251 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
1e6feb95 VZ |
27 | #if wxUSE_TREECTRL |
28 | ||
e4db172a WS |
29 | #include "wx/treectrl.h" |
30 | ||
ad9835c9 | 31 | #ifndef WX_PRECOMP |
57bd4c60 WS |
32 | #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly" |
33 | #include "wx/msw/missing.h" | |
ad9835c9 | 34 | #include "wx/dynarray.h" |
e4db172a | 35 | #include "wx/log.h" |
670f9935 | 36 | #include "wx/app.h" |
9eddec69 | 37 | #include "wx/settings.h" |
ad9835c9 WS |
38 | #endif |
39 | ||
2f17da8b WS |
40 | #include "wx/msw/private.h" |
41 | ||
1e6feb95 VZ |
42 | // Set this to 1 to be _absolutely_ sure that repainting will work for all |
43 | // comctl32.dll versions | |
f6bcfd97 BP |
44 | #define wxUSE_COMCTL32_SAFELY 0 |
45 | ||
08b7c251 | 46 | #include "wx/imaglist.h" |
23f681ec VZ |
47 | #include "wx/msw/dragimag.h" |
48 | ||
f888d614 VZ |
49 | // macros to hide the cast ugliness |
50 | // -------------------------------- | |
51 | ||
14ba002a VZ |
52 | // get HTREEITEM from wxTreeItemId |
53 | #define HITEM(item) ((HTREEITEM)(((item).m_pItem))) | |
3f7bc32b | 54 | |
5fe302ef BW |
55 | |
56 | // older SDKs are missing these | |
57 | #ifndef TVN_ITEMCHANGINGA | |
58 | ||
59 | #define TVN_ITEMCHANGINGA (TVN_FIRST-16) | |
60 | #define TVN_ITEMCHANGINGW (TVN_FIRST-17) | |
61 | ||
62 | typedef struct tagNMTVITEMCHANGE | |
63 | { | |
64 | NMHDR hdr; | |
65 | UINT uChanged; | |
66 | HTREEITEM hItem; | |
67 | UINT uStateNew; | |
68 | UINT uStateOld; | |
69 | LPARAM lParam; | |
70 | } NMTVITEMCHANGE; | |
71 | ||
72 | #endif | |
73 | ||
74 | ||
396f85ba BW |
75 | // this helper class is used on vista systems for preventing unwanted |
76 | // item state changes in the vista tree control. It is only effective in | |
5fe302ef BW |
77 | // multi-select mode on vista systems. |
78 | ||
396f85ba BW |
79 | // The vista tree control includes some new code that originally broke the |
80 | // multi-selection tree, causing seemingly spurious item selection state changes | |
81 | // during Shift or Ctrl-click item selection. (To witness the original broken | |
82 | // behavior, simply make IsLocked() below always return false). This problem was | |
83 | // solved by using the following class to 'unlock' an item's selection state. | |
5fe302ef BW |
84 | |
85 | class TreeItemUnlocker | |
86 | { | |
87 | public: | |
396f85ba BW |
88 | // unlock a single item |
89 | TreeItemUnlocker(HTREEITEM item) { ms_unlockedItem = item; } | |
90 | ||
91 | // unlock all items, don't use unless absolutely necessary | |
92 | TreeItemUnlocker() { ms_unlockedItem = (HTREEITEM)-1; } | |
93 | ||
94 | // lock everything back | |
95 | ~TreeItemUnlocker() { ms_unlockedItem = NULL; } | |
96 | ||
97 | ||
98 | // check if the item state is currently locked | |
99 | static bool IsLocked(HTREEITEM item) | |
100 | { return ms_unlockedItem != (HTREEITEM)-1 && item != ms_unlockedItem; } | |
101 | ||
102 | private: | |
103 | static HTREEITEM ms_unlockedItem; | |
5fe302ef BW |
104 | }; |
105 | ||
396f85ba | 106 | HTREEITEM TreeItemUnlocker::ms_unlockedItem = NULL; |
5fe302ef | 107 | |
3f7bc32b VZ |
108 | // ---------------------------------------------------------------------------- |
109 | // private functions | |
110 | // ---------------------------------------------------------------------------- | |
111 | ||
75afdc2d VZ |
112 | // wrappers for TreeView_GetItem/TreeView_SetItem |
113 | static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem) | |
114 | { | |
115 | ||
116 | TV_ITEM tvi; | |
117 | tvi.mask = TVIF_STATE | TVIF_HANDLE; | |
118 | tvi.stateMask = TVIS_SELECTED; | |
119 | tvi.hItem = hItem; | |
120 | ||
5fe302ef BW |
121 | TreeItemUnlocker unlocker(hItem); |
122 | ||
75afdc2d VZ |
123 | if ( !TreeView_GetItem(hwndTV, &tvi) ) |
124 | { | |
125 | wxLogLastError(wxT("TreeView_GetItem")); | |
126 | } | |
127 | ||
128 | return (tvi.state & TVIS_SELECTED) != 0; | |
129 | } | |
130 | ||
131 | static bool SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true) | |
132 | { | |
133 | TV_ITEM tvi; | |
134 | tvi.mask = TVIF_STATE | TVIF_HANDLE; | |
135 | tvi.stateMask = TVIS_SELECTED; | |
136 | tvi.state = select ? TVIS_SELECTED : 0; | |
137 | tvi.hItem = hItem; | |
138 | ||
5fe302ef BW |
139 | TreeItemUnlocker unlocker(hItem); |
140 | ||
75afdc2d VZ |
141 | if ( TreeView_SetItem(hwndTV, &tvi) == -1 ) |
142 | { | |
143 | wxLogLastError(wxT("TreeView_SetItem")); | |
144 | return false; | |
145 | } | |
146 | ||
147 | return true; | |
148 | } | |
149 | ||
150 | static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem) | |
151 | { | |
152 | SelectItem(hwndTV, htItem, false); | |
153 | } | |
154 | ||
75afdc2d VZ |
155 | // helper function which selects all items in a range and, optionally, |
156 | // unselects all others | |
157 | static void SelectRange(HWND hwndTV, | |
158 | HTREEITEM htFirst, | |
159 | HTREEITEM htLast, | |
160 | bool unselectOthers = true) | |
161 | { | |
162 | // find the first (or last) item and select it | |
163 | bool cont = true; | |
164 | HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV); | |
165 | while ( htItem && cont ) | |
166 | { | |
167 | if ( (htItem == htFirst) || (htItem == htLast) ) | |
168 | { | |
169 | if ( !IsItemSelected(hwndTV, htItem) ) | |
170 | { | |
171 | SelectItem(hwndTV, htItem); | |
172 | } | |
173 | ||
174 | cont = false; | |
175 | } | |
176 | else | |
177 | { | |
178 | if ( unselectOthers && IsItemSelected(hwndTV, htItem) ) | |
179 | { | |
180 | UnselectItem(hwndTV, htItem); | |
181 | } | |
182 | } | |
183 | ||
184 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); | |
185 | } | |
186 | ||
187 | // select the items in range | |
188 | cont = htFirst != htLast; | |
189 | while ( htItem && cont ) | |
190 | { | |
191 | if ( !IsItemSelected(hwndTV, htItem) ) | |
192 | { | |
193 | SelectItem(hwndTV, htItem); | |
194 | } | |
195 | ||
196 | cont = (htItem != htFirst) && (htItem != htLast); | |
197 | ||
198 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); | |
199 | } | |
200 | ||
201 | // unselect the rest | |
202 | if ( unselectOthers ) | |
203 | { | |
204 | while ( htItem ) | |
205 | { | |
206 | if ( IsItemSelected(hwndTV, htItem) ) | |
207 | { | |
208 | UnselectItem(hwndTV, htItem); | |
209 | } | |
210 | ||
211 | htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem); | |
212 | } | |
213 | } | |
214 | ||
215 | // seems to be necessary - otherwise the just selected items don't always | |
216 | // appear as selected | |
217 | UpdateWindow(hwndTV); | |
218 | } | |
219 | ||
220 | // helper function which tricks the standard control into changing the focused | |
221 | // item without changing anything else (if someone knows why Microsoft doesn't | |
222 | // allow to do it by just setting TVIS_FOCUSED flag, please tell me!) | |
cc87a04c VZ |
223 | // |
224 | // returns true if the focus was changed, false if the given item was already | |
225 | // the focused one | |
226 | static bool SetFocus(HWND hwndTV, HTREEITEM htItem) | |
75afdc2d VZ |
227 | { |
228 | // the current focus | |
229 | HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV); | |
230 | ||
cc87a04c VZ |
231 | if ( htItem == htFocus ) |
232 | return false; | |
233 | ||
75afdc2d VZ |
234 | if ( htItem ) |
235 | { | |
cc87a04c VZ |
236 | // remember the selection state of the item |
237 | bool wasSelected = IsItemSelected(hwndTV, htItem); | |
75afdc2d | 238 | |
cc87a04c VZ |
239 | if ( htFocus && IsItemSelected(hwndTV, htFocus) ) |
240 | { | |
241 | // prevent the tree from unselecting the old focus which it | |
242 | // would do by default (TreeView_SelectItem unselects the | |
243 | // focused item) | |
244 | TreeView_SelectItem(hwndTV, 0); | |
245 | SelectItem(hwndTV, htFocus); | |
246 | } | |
75afdc2d | 247 | |
cc87a04c | 248 | TreeView_SelectItem(hwndTV, htItem); |
75afdc2d | 249 | |
cc87a04c VZ |
250 | if ( !wasSelected ) |
251 | { | |
252 | // need to clear the selection which TreeView_SelectItem() gave | |
253 | // us | |
254 | UnselectItem(hwndTV, htItem); | |
75afdc2d | 255 | } |
cc87a04c | 256 | //else: was selected, still selected - ok |
75afdc2d | 257 | } |
cc87a04c | 258 | else // reset focus |
75afdc2d | 259 | { |
cc87a04c | 260 | bool wasFocusSelected = IsItemSelected(hwndTV, htFocus); |
75afdc2d | 261 | |
cc87a04c VZ |
262 | // just clear the focus |
263 | TreeView_SelectItem(hwndTV, 0); | |
75afdc2d | 264 | |
cc87a04c VZ |
265 | if ( wasFocusSelected ) |
266 | { | |
267 | // restore the selection state | |
268 | SelectItem(hwndTV, htFocus); | |
75afdc2d | 269 | } |
75afdc2d | 270 | } |
cc87a04c VZ |
271 | |
272 | return true; | |
75afdc2d VZ |
273 | } |
274 | ||
08b7c251 VZ |
275 | // ---------------------------------------------------------------------------- |
276 | // private classes | |
277 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 278 | |
08b7c251 | 279 | // a convenient wrapper around TV_ITEM struct which adds a ctor |
f3ef286f | 280 | #ifdef __VISUALC__ |
3f7bc32b | 281 | #pragma warning( disable : 4097 ) // inheriting from typedef |
f3ef286f JS |
282 | #endif |
283 | ||
08b7c251 VZ |
284 | struct wxTreeViewItem : public TV_ITEM |
285 | { | |
9dfbf520 VZ |
286 | wxTreeViewItem(const wxTreeItemId& item, // the item handle |
287 | UINT mask_, // fields which are valid | |
288 | UINT stateMask_ = 0) // for TVIF_STATE only | |
08b7c251 | 289 | { |
a9c1265f VZ |
290 | wxZeroMemory(*this); |
291 | ||
9dfbf520 VZ |
292 | // hItem member is always valid |
293 | mask = mask_ | TVIF_HANDLE; | |
08b7c251 | 294 | stateMask = stateMask_; |
3f7bc32b | 295 | hItem = HITEM(item); |
08b7c251 VZ |
296 | } |
297 | }; | |
f3ef286f | 298 | |
4523ebb3 VZ |
299 | // ---------------------------------------------------------------------------- |
300 | // This class is our userdata/lParam for the TV_ITEMs stored in the treeview. | |
301 | // | |
302 | // We need this for a couple of reasons: | |
303 | // | |
304 | // 1) This class is needed for support of different images: the Win32 common | |
305 | // control natively supports only 2 images (the normal one and another for the | |
306 | // selected state). We wish to provide support for 2 more of them for folder | |
307 | // items (i.e. those which have children): for expanded state and for expanded | |
308 | // selected state. For this we use this structure to store the additional items | |
309 | // images. | |
310 | // | |
311 | // 2) This class is also needed to hold the HITEM so that we can sort | |
312 | // it correctly in the MSW sort callback. | |
313 | // | |
314 | // In addition it makes other workarounds such as this easier and helps | |
315 | // simplify the code. | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
318 | class wxTreeItemParam | |
319 | { | |
320 | public: | |
321 | wxTreeItemParam() | |
4523ebb3 | 322 | { |
20f4b566 VZ |
323 | m_data = NULL; |
324 | ||
4523ebb3 VZ |
325 | for ( size_t n = 0; n < WXSIZEOF(m_images); n++ ) |
326 | { | |
327 | m_images[n] = -1; | |
328 | } | |
329 | } | |
330 | ||
331 | // dtor deletes the associated data as well | |
332 | virtual ~wxTreeItemParam() { delete m_data; } | |
333 | ||
334 | // accessors | |
335 | // get the real data associated with the item | |
336 | wxTreeItemData *GetData() const { return m_data; } | |
337 | // change it | |
338 | void SetData(wxTreeItemData *data) { m_data = data; } | |
339 | ||
340 | // do we have such image? | |
341 | bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; } | |
1b182562 VZ |
342 | // get image, falling back to the other images if this one is not |
343 | // specified | |
344 | int GetImage(wxTreeItemIcon which) const | |
345 | { | |
346 | int image = m_images[which]; | |
347 | if ( image == -1 ) | |
348 | { | |
349 | switch ( which ) | |
350 | { | |
351 | case wxTreeItemIcon_SelectedExpanded: | |
352 | image = GetImage(wxTreeItemIcon_Expanded); | |
353 | if ( image != -1 ) | |
354 | break; | |
355 | //else: fall through | |
356 | ||
357 | case wxTreeItemIcon_Selected: | |
358 | case wxTreeItemIcon_Expanded: | |
359 | image = GetImage(wxTreeItemIcon_Normal); | |
360 | break; | |
361 | ||
362 | case wxTreeItemIcon_Normal: | |
363 | // no fallback | |
364 | break; | |
365 | ||
366 | default: | |
367 | wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") ); | |
368 | } | |
369 | } | |
370 | ||
371 | return image; | |
372 | } | |
373 | // change the given image | |
4523ebb3 VZ |
374 | void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; } |
375 | ||
376 | // get item | |
377 | const wxTreeItemId& GetItem() const { return m_item; } | |
378 | // set item | |
379 | void SetItem(const wxTreeItemId& item) { m_item = item; } | |
380 | ||
381 | protected: | |
382 | // all the images associated with the item | |
383 | int m_images[wxTreeItemIcon_Max]; | |
384 | ||
385 | // item for sort callbacks | |
386 | wxTreeItemId m_item; | |
387 | ||
388 | // the real client data | |
389 | wxTreeItemData *m_data; | |
390 | ||
391 | DECLARE_NO_COPY_CLASS(wxTreeItemParam) | |
392 | }; | |
393 | ||
a9c1265f VZ |
394 | // wxVirutalNode is used in place of a single root when 'hidden' root is |
395 | // specified. | |
efa38350 | 396 | class wxVirtualNode : public wxTreeViewItem |
a9c1265f VZ |
397 | { |
398 | public: | |
4523ebb3 | 399 | wxVirtualNode(wxTreeItemParam *param) |
efa38350 | 400 | : wxTreeViewItem(TVI_ROOT, 0) |
a9c1265f | 401 | { |
4523ebb3 | 402 | m_param = param; |
a9c1265f VZ |
403 | } |
404 | ||
405 | ~wxVirtualNode() | |
406 | { | |
4523ebb3 | 407 | delete m_param; |
a9c1265f VZ |
408 | } |
409 | ||
4523ebb3 VZ |
410 | wxTreeItemParam *GetParam() const { return m_param; } |
411 | void SetParam(wxTreeItemParam *param) { delete m_param; m_param = param; } | |
a9c1265f VZ |
412 | |
413 | private: | |
4523ebb3 | 414 | wxTreeItemParam *m_param; |
22f3361e VZ |
415 | |
416 | DECLARE_NO_COPY_CLASS(wxVirtualNode) | |
a9c1265f VZ |
417 | }; |
418 | ||
f3ef286f | 419 | #ifdef __VISUALC__ |
197dd9af | 420 | #pragma warning( default : 4097 ) |
f3ef286f | 421 | #endif |
2bda0e17 | 422 | |
a9c1265f VZ |
423 | // a macro to get the virtual root, returns NULL if none |
424 | #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot) | |
425 | ||
04cd30de | 426 | // returns true if the item is the virtual root |
a9c1265f VZ |
427 | #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT) |
428 | ||
9dfbf520 | 429 | // a class which encapsulates the tree traversal logic: it vists all (unless |
04cd30de | 430 | // OnVisit() returns false) items under the given one |
9dfbf520 VZ |
431 | class wxTreeTraversal |
432 | { | |
433 | public: | |
434 | wxTreeTraversal(const wxTreeCtrl *tree) | |
435 | { | |
436 | m_tree = tree; | |
437 | } | |
438 | ||
cc11ded1 VZ |
439 | // give it a virtual dtor: not really needed as the class is never used |
440 | // polymorphically and not even allocated on heap at all, but this is safer | |
441 | // (in case it ever is) and silences the compiler warnings for now | |
442 | virtual ~wxTreeTraversal() { } | |
443 | ||
9dfbf520 | 444 | // do traverse the tree: visit all items (recursively by default) under the |
04cd30de RL |
445 | // given one; return true if all items were traversed or false if the |
446 | // traversal was aborted because OnVisit returned false | |
447 | bool DoTraverse(const wxTreeItemId& root, bool recursively = true); | |
9dfbf520 VZ |
448 | |
449 | // override this function to do whatever is needed for each item, return | |
04cd30de | 450 | // false to stop traversing |
9dfbf520 VZ |
451 | virtual bool OnVisit(const wxTreeItemId& item) = 0; |
452 | ||
453 | protected: | |
454 | const wxTreeCtrl *GetTree() const { return m_tree; } | |
455 | ||
456 | private: | |
457 | bool Traverse(const wxTreeItemId& root, bool recursively); | |
458 | ||
459 | const wxTreeCtrl *m_tree; | |
22f3361e VZ |
460 | |
461 | DECLARE_NO_COPY_CLASS(wxTreeTraversal) | |
9dfbf520 VZ |
462 | }; |
463 | ||
74b31181 VZ |
464 | // internal class for getting the selected items |
465 | class TraverseSelections : public wxTreeTraversal | |
466 | { | |
467 | public: | |
468 | TraverseSelections(const wxTreeCtrl *tree, | |
469 | wxArrayTreeItemIds& selections) | |
470 | : wxTreeTraversal(tree), m_selections(selections) | |
471 | { | |
472 | m_selections.Empty(); | |
473 | ||
b9e9b40c JS |
474 | if (tree->GetCount() > 0) |
475 | DoTraverse(tree->GetRootItem()); | |
74b31181 VZ |
476 | } |
477 | ||
478 | virtual bool OnVisit(const wxTreeItemId& item) | |
479 | { | |
0228081f VZ |
480 | const wxTreeCtrl * const tree = GetTree(); |
481 | ||
a9c1265f | 482 | // can't visit a virtual node. |
0228081f | 483 | if ( (tree->GetRootItem() == item) && tree->HasFlag(wxTR_HIDE_ROOT) ) |
a9c1265f | 484 | { |
04cd30de | 485 | return true; |
a9c1265f VZ |
486 | } |
487 | ||
0228081f | 488 | if ( ::IsItemSelected(GetHwndOf(tree), HITEM(item)) ) |
74b31181 VZ |
489 | { |
490 | m_selections.Add(item); | |
491 | } | |
492 | ||
04cd30de | 493 | return true; |
74b31181 VZ |
494 | } |
495 | ||
e47c4d48 VZ |
496 | size_t GetCount() const { return m_selections.GetCount(); } |
497 | ||
74b31181 VZ |
498 | private: |
499 | wxArrayTreeItemIds& m_selections; | |
fc7a2a60 VZ |
500 | |
501 | DECLARE_NO_COPY_CLASS(TraverseSelections) | |
74b31181 VZ |
502 | }; |
503 | ||
504 | // internal class for counting tree items | |
505 | class TraverseCounter : public wxTreeTraversal | |
506 | { | |
507 | public: | |
508 | TraverseCounter(const wxTreeCtrl *tree, | |
509 | const wxTreeItemId& root, | |
510 | bool recursively) | |
511 | : wxTreeTraversal(tree) | |
512 | { | |
513 | m_count = 0; | |
514 | ||
515 | DoTraverse(root, recursively); | |
516 | } | |
517 | ||
33ac7e6f | 518 | virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item)) |
74b31181 VZ |
519 | { |
520 | m_count++; | |
521 | ||
04cd30de | 522 | return true; |
74b31181 VZ |
523 | } |
524 | ||
525 | size_t GetCount() const { return m_count; } | |
526 | ||
527 | private: | |
528 | size_t m_count; | |
fc7a2a60 VZ |
529 | |
530 | DECLARE_NO_COPY_CLASS(TraverseCounter) | |
74b31181 VZ |
531 | }; |
532 | ||
23f681ec | 533 | // ---------------------------------------------------------------------------- |
3f7bc32b | 534 | // wxWin macros |
08b7c251 VZ |
535 | // ---------------------------------------------------------------------------- |
536 | ||
786a2425 | 537 | #if wxUSE_EXTENDED_RTTI |
bc9fb572 JS |
538 | WX_DEFINE_FLAGS( wxTreeCtrlStyle ) |
539 | ||
3ff066a4 | 540 | wxBEGIN_FLAGS( wxTreeCtrlStyle ) |
bc9fb572 JS |
541 | // new style border flags, we put them first to |
542 | // use them for streaming out | |
3ff066a4 SC |
543 | wxFLAGS_MEMBER(wxBORDER_SIMPLE) |
544 | wxFLAGS_MEMBER(wxBORDER_SUNKEN) | |
545 | wxFLAGS_MEMBER(wxBORDER_DOUBLE) | |
546 | wxFLAGS_MEMBER(wxBORDER_RAISED) | |
547 | wxFLAGS_MEMBER(wxBORDER_STATIC) | |
548 | wxFLAGS_MEMBER(wxBORDER_NONE) | |
5e7718a2 | 549 | |
bc9fb572 | 550 | // old style border flags |
3ff066a4 SC |
551 | wxFLAGS_MEMBER(wxSIMPLE_BORDER) |
552 | wxFLAGS_MEMBER(wxSUNKEN_BORDER) | |
553 | wxFLAGS_MEMBER(wxDOUBLE_BORDER) | |
554 | wxFLAGS_MEMBER(wxRAISED_BORDER) | |
555 | wxFLAGS_MEMBER(wxSTATIC_BORDER) | |
cb0afb26 | 556 | wxFLAGS_MEMBER(wxBORDER) |
bc9fb572 JS |
557 | |
558 | // standard window styles | |
3ff066a4 SC |
559 | wxFLAGS_MEMBER(wxTAB_TRAVERSAL) |
560 | wxFLAGS_MEMBER(wxCLIP_CHILDREN) | |
561 | wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW) | |
562 | wxFLAGS_MEMBER(wxWANTS_CHARS) | |
cb0afb26 | 563 | wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE) |
3ff066a4 SC |
564 | wxFLAGS_MEMBER(wxALWAYS_SHOW_SB ) |
565 | wxFLAGS_MEMBER(wxVSCROLL) | |
566 | wxFLAGS_MEMBER(wxHSCROLL) | |
567 | ||
568 | wxFLAGS_MEMBER(wxTR_EDIT_LABELS) | |
569 | wxFLAGS_MEMBER(wxTR_NO_BUTTONS) | |
570 | wxFLAGS_MEMBER(wxTR_HAS_BUTTONS) | |
571 | wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS) | |
572 | wxFLAGS_MEMBER(wxTR_NO_LINES) | |
573 | wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT) | |
574 | wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT) | |
575 | wxFLAGS_MEMBER(wxTR_HIDE_ROOT) | |
576 | wxFLAGS_MEMBER(wxTR_ROW_LINES) | |
577 | wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT) | |
578 | wxFLAGS_MEMBER(wxTR_SINGLE) | |
579 | wxFLAGS_MEMBER(wxTR_MULTIPLE) | |
70cab5b7 | 580 | #if WXWIN_COMPATIBILITY_2_8 |
3ff066a4 | 581 | wxFLAGS_MEMBER(wxTR_EXTENDED) |
70cab5b7 | 582 | #endif |
3ff066a4 SC |
583 | wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE) |
584 | ||
585 | wxEND_FLAGS( wxTreeCtrlStyle ) | |
bc9fb572 | 586 | |
786a2425 SC |
587 | IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl, wxControl,"wx/treectrl.h") |
588 | ||
3ff066a4 | 589 | wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl) |
5e7718a2 | 590 | wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent ) |
3ff066a4 | 591 | wxEVENT_RANGE_PROPERTY( TreeEvent , wxEVT_COMMAND_TREE_BEGIN_DRAG , wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK , wxTreeEvent ) |
af498247 | 592 | wxPROPERTY_FLAGS( WindowStyle , wxTreeCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style |
3ff066a4 | 593 | wxEND_PROPERTIES_TABLE() |
786a2425 | 594 | |
3ff066a4 SC |
595 | wxBEGIN_HANDLERS_TABLE(wxTreeCtrl) |
596 | wxEND_HANDLERS_TABLE() | |
786a2425 | 597 | |
5e7718a2 | 598 | wxCONSTRUCTOR_5( wxTreeCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle ) |
786a2425 | 599 | #else |
23f681ec | 600 | IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl) |
786a2425 | 601 | #endif |
2bda0e17 | 602 | |
08b7c251 | 603 | // ---------------------------------------------------------------------------- |
deb1de30 | 604 | // constants |
08b7c251 VZ |
605 | // ---------------------------------------------------------------------------- |
606 | ||
deb1de30 VZ |
607 | // indices in gs_expandEvents table below |
608 | enum | |
609 | { | |
610 | IDX_COLLAPSE, | |
611 | IDX_EXPAND, | |
612 | IDX_WHAT_MAX | |
613 | }; | |
614 | ||
615 | enum | |
616 | { | |
617 | IDX_DONE, | |
618 | IDX_DOING, | |
619 | IDX_HOW_MAX | |
620 | }; | |
621 | ||
622 | // handy table for sending events - it has to be initialized during run-time | |
623 | // now so can't be const any more | |
624 | static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX]; | |
625 | ||
626 | /* | |
627 | but logically it's a const table with the following entries: | |
628 | = | |
2bda0e17 | 629 | { |
08b7c251 VZ |
630 | { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING }, |
631 | { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING } | |
632 | }; | |
deb1de30 | 633 | */ |
08b7c251 VZ |
634 | |
635 | // ============================================================================ | |
636 | // implementation | |
637 | // ============================================================================ | |
638 | ||
9dfbf520 VZ |
639 | // ---------------------------------------------------------------------------- |
640 | // tree traversal | |
641 | // ---------------------------------------------------------------------------- | |
642 | ||
643 | bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively) | |
644 | { | |
645 | if ( !OnVisit(root) ) | |
04cd30de | 646 | return false; |
9dfbf520 VZ |
647 | |
648 | return Traverse(root, recursively); | |
649 | } | |
650 | ||
651 | bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively) | |
652 | { | |
ee4b2721 | 653 | wxTreeItemIdValue cookie; |
9dfbf520 VZ |
654 | wxTreeItemId child = m_tree->GetFirstChild(root, cookie); |
655 | while ( child.IsOk() ) | |
656 | { | |
657 | // depth first traversal | |
04cd30de RL |
658 | if ( recursively && !Traverse(child, true) ) |
659 | return false; | |
9dfbf520 VZ |
660 | |
661 | if ( !OnVisit(child) ) | |
04cd30de | 662 | return false; |
9dfbf520 VZ |
663 | |
664 | child = m_tree->GetNextChild(root, cookie); | |
665 | } | |
666 | ||
04cd30de | 667 | return true; |
9dfbf520 VZ |
668 | } |
669 | ||
08b7c251 VZ |
670 | // ---------------------------------------------------------------------------- |
671 | // construction and destruction | |
672 | // ---------------------------------------------------------------------------- | |
673 | ||
674 | void wxTreeCtrl::Init() | |
675 | { | |
08b7c251 | 676 | m_textCtrl = NULL; |
04cd30de | 677 | m_hasAnyAttr = false; |
68d9be05 | 678 | #if wxUSE_DRAGIMAGE |
23f681ec | 679 | m_dragImage = NULL; |
68d9be05 | 680 | #endif |
a9c1265f | 681 | m_pVirtualRoot = NULL; |
9ab5ef13 | 682 | |
deb1de30 VZ |
683 | // initialize the global array of events now as it can't be done statically |
684 | // with the wxEVT_XXX values being allocated during run-time only | |
685 | gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED; | |
686 | gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING; | |
687 | gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED; | |
688 | gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING; | |
2bda0e17 KB |
689 | } |
690 | ||
9dfbf520 VZ |
691 | bool wxTreeCtrl::Create(wxWindow *parent, |
692 | wxWindowID id, | |
693 | const wxPoint& pos, | |
694 | const wxSize& size, | |
695 | long style, | |
696 | const wxValidator& validator, | |
08b7c251 | 697 | const wxString& name) |
2bda0e17 | 698 | { |
08b7c251 | 699 | Init(); |
2bda0e17 | 700 | |
7699361c JS |
701 | if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT ) |
702 | style |= wxBORDER_SUNKEN; | |
703 | ||
9dfbf520 | 704 | if ( !CreateControl(parent, id, pos, size, style, validator, name) ) |
04cd30de | 705 | return false; |
2bda0e17 | 706 | |
823b140b | 707 | WXDWORD exStyle = 0; |
7699361c JS |
708 | DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle); |
709 | wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS; | |
2bda0e17 | 710 | |
63da7df7 JS |
711 | if ((m_windowStyle & wxTR_NO_LINES) == 0) |
712 | wstyle |= TVS_HASLINES; | |
08b7c251 VZ |
713 | if ( m_windowStyle & wxTR_HAS_BUTTONS ) |
714 | wstyle |= TVS_HASBUTTONS; | |
2bda0e17 | 715 | |
08b7c251 VZ |
716 | if ( m_windowStyle & wxTR_EDIT_LABELS ) |
717 | wstyle |= TVS_EDITLABELS; | |
2bda0e17 | 718 | |
08b7c251 VZ |
719 | if ( m_windowStyle & wxTR_LINES_AT_ROOT ) |
720 | wstyle |= TVS_LINESATROOT; | |
deb1de30 | 721 | |
c6f4913a | 722 | if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT ) |
deb1de30 | 723 | { |
2a1f999f | 724 | if ( wxApp::GetComCtl32Version() >= 471 ) |
c6f4913a VS |
725 | wstyle |= TVS_FULLROWSELECT; |
726 | } | |
727 | ||
f3f71703 | 728 | #if !defined(__WXWINCE__) && defined(TVS_INFOTIP) |
156194e1 JS |
729 | // Need so that TVN_GETINFOTIP messages will be sent |
730 | wstyle |= TVS_INFOTIP; | |
676d6550 | 731 | #endif |
5e7718a2 | 732 | |
08b7c251 | 733 | // Create the tree control. |
8779cca3 | 734 | if ( !MSWCreateControl(WC_TREEVIEW, wstyle, pos, size) ) |
04cd30de | 735 | return false; |
9dfbf520 | 736 | |
838ab05e VZ |
737 | // this significantly reduces flicker when refreshing the control, for some |
738 | // reason the native control itself doesn't avoid background repainting | |
739 | SetBackgroundStyle(wxBG_STYLE_CUSTOM); | |
740 | ||
f6bcfd97 | 741 | #if wxUSE_COMCTL32_SAFELY |
a756f210 | 742 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 BP |
743 | wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
744 | #elif 1 | |
a756f210 | 745 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 | 746 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
747 | #else |
748 | // This works around a bug in the Windows tree control whereby for some versions | |
749 | // of comctrl32, setting any colour actually draws the background in black. | |
750 | // This will initialise the background to the system colour. | |
f6bcfd97 BP |
751 | // THIS FIX NOW REVERTED since it caused problems on _other_ systems. |
752 | // Assume the user has an updated comctl32.dll. | |
fbdcff4a | 753 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1); |
a756f210 | 754 | wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW)); |
f6bcfd97 | 755 | SetForegroundColour(wxWindow::GetParent()->GetForegroundColour()); |
fbdcff4a JS |
756 | #endif |
757 | ||
9a63feff VZ |
758 | wxSetCCUnicodeFormat(GetHwnd()); |
759 | ||
04cd30de | 760 | return true; |
2bda0e17 KB |
761 | } |
762 | ||
08b7c251 | 763 | wxTreeCtrl::~wxTreeCtrl() |
2bda0e17 | 764 | { |
696e1ea0 VZ |
765 | // delete any attributes |
766 | if ( m_hasAnyAttr ) | |
767 | { | |
ee4b2721 | 768 | WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs); |
696e1ea0 VZ |
769 | |
770 | // prevent TVN_DELETEITEM handler from deleting the attributes again! | |
04cd30de | 771 | m_hasAnyAttr = false; |
696e1ea0 VZ |
772 | } |
773 | ||
08b7c251 | 774 | DeleteTextCtrl(); |
2bda0e17 | 775 | |
08b7c251 | 776 | // delete user data to prevent memory leaks |
a9c1265f | 777 | // also deletes hidden root node storage. |
08b7c251 | 778 | DeleteAllItems(); |
2bda0e17 KB |
779 | } |
780 | ||
08b7c251 VZ |
781 | // ---------------------------------------------------------------------------- |
782 | // accessors | |
783 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 784 | |
39c7a53c VZ |
785 | /* static */ wxVisualAttributes |
786 | wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant) | |
787 | { | |
788 | wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant); | |
789 | ||
790 | // common controls have their own default font | |
791 | attrs.font = wxGetCCDefaultFont(); | |
792 | ||
793 | return attrs; | |
794 | } | |
795 | ||
796 | ||
08b7c251 | 797 | // simple wrappers which add error checking in debug mode |
2bda0e17 | 798 | |
4523ebb3 | 799 | bool wxTreeCtrl::DoGetItem(wxTreeViewItem *tvItem) const |
2bda0e17 | 800 | { |
04cd30de | 801 | wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false, |
a9c1265f VZ |
802 | _T("can't retrieve virtual root item") ); |
803 | ||
d220ae32 | 804 | if ( !TreeView_GetItem(GetHwnd(), tvItem) ) |
2bda0e17 | 805 | { |
f6bcfd97 | 806 | wxLogLastError(wxT("TreeView_GetItem")); |
08b7c251 | 807 | |
04cd30de | 808 | return false; |
08b7c251 VZ |
809 | } |
810 | ||
04cd30de | 811 | return true; |
2bda0e17 KB |
812 | } |
813 | ||
4523ebb3 | 814 | void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem) |
2bda0e17 | 815 | { |
5fe302ef BW |
816 | TreeItemUnlocker unlocker(tvItem->hItem); |
817 | ||
d220ae32 | 818 | if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 ) |
2bda0e17 | 819 | { |
f6bcfd97 | 820 | wxLogLastError(wxT("TreeView_SetItem")); |
08b7c251 | 821 | } |
2bda0e17 KB |
822 | } |
823 | ||
027d45e8 | 824 | unsigned int wxTreeCtrl::GetCount() const |
2bda0e17 | 825 | { |
027d45e8 | 826 | return (unsigned int)TreeView_GetCount(GetHwnd()); |
2bda0e17 KB |
827 | } |
828 | ||
08b7c251 | 829 | unsigned int wxTreeCtrl::GetIndent() const |
2bda0e17 | 830 | { |
d220ae32 | 831 | return TreeView_GetIndent(GetHwnd()); |
2bda0e17 KB |
832 | } |
833 | ||
08b7c251 | 834 | void wxTreeCtrl::SetIndent(unsigned int indent) |
2bda0e17 | 835 | { |
d220ae32 | 836 | TreeView_SetIndent(GetHwnd(), indent); |
2bda0e17 KB |
837 | } |
838 | ||
08b7c251 | 839 | void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which) |
2bda0e17 | 840 | { |
08b7c251 | 841 | // no error return |
8ecd5de2 WS |
842 | (void) TreeView_SetImageList(GetHwnd(), |
843 | imageList ? imageList->GetHIMAGELIST() : 0, | |
844 | which); | |
2bda0e17 KB |
845 | } |
846 | ||
08b7c251 | 847 | void wxTreeCtrl::SetImageList(wxImageList *imageList) |
2bda0e17 | 848 | { |
a9c1265f VZ |
849 | if (m_ownsImageListNormal) |
850 | delete m_imageListNormal; | |
851 | ||
08b7c251 | 852 | SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL); |
04cd30de | 853 | m_ownsImageListNormal = false; |
2bda0e17 KB |
854 | } |
855 | ||
08b7c251 | 856 | void wxTreeCtrl::SetStateImageList(wxImageList *imageList) |
2bda0e17 | 857 | { |
0377efa2 | 858 | if (m_ownsImageListState) delete m_imageListState; |
08b7c251 | 859 | SetAnyImageList(m_imageListState = imageList, TVSIL_STATE); |
04cd30de | 860 | m_ownsImageListState = false; |
0377efa2 VS |
861 | } |
862 | ||
33961d59 RR |
863 | size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item, |
864 | bool recursively) const | |
865 | { | |
05b2a432 | 866 | wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") ); |
23fd5130 | 867 | |
05b2a432 | 868 | TraverseCounter counter(this, item, recursively); |
73974df1 | 869 | return counter.GetCount() - 1; |
23fd5130 VZ |
870 | } |
871 | ||
bb448552 VZ |
872 | // ---------------------------------------------------------------------------- |
873 | // control colours | |
874 | // ---------------------------------------------------------------------------- | |
875 | ||
876 | bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour) | |
877 | { | |
f6bcfd97 | 878 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 | 879 | if ( !wxWindowBase::SetBackgroundColour(colour) ) |
04cd30de | 880 | return false; |
bb448552 | 881 | |
bfbb0b4c | 882 | ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel()); |
f6bcfd97 | 883 | #endif |
bb448552 | 884 | |
04cd30de | 885 | return true; |
bb448552 VZ |
886 | } |
887 | ||
888 | bool wxTreeCtrl::SetForegroundColour(const wxColour &colour) | |
889 | { | |
f6bcfd97 | 890 | #if !wxUSE_COMCTL32_SAFELY |
bb448552 | 891 | if ( !wxWindowBase::SetForegroundColour(colour) ) |
04cd30de | 892 | return false; |
bb448552 | 893 | |
bfbb0b4c | 894 | ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel()); |
f6bcfd97 | 895 | #endif |
bb448552 | 896 | |
04cd30de | 897 | return true; |
bb448552 VZ |
898 | } |
899 | ||
08b7c251 VZ |
900 | // ---------------------------------------------------------------------------- |
901 | // Item access | |
902 | // ---------------------------------------------------------------------------- | |
903 | ||
0228081f VZ |
904 | bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const |
905 | { | |
906 | return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT); | |
907 | } | |
908 | ||
08b7c251 | 909 | wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const |
2bda0e17 | 910 | { |
f31a4098 | 911 | wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") ); |
05b2a432 | 912 | |
837e5743 | 913 | wxChar buf[512]; // the size is arbitrary... |
02ce7b72 | 914 | |
08b7c251 VZ |
915 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
916 | tvItem.pszText = buf; | |
917 | tvItem.cchTextMax = WXSIZEOF(buf); | |
918 | if ( !DoGetItem(&tvItem) ) | |
919 | { | |
920 | // don't return some garbage which was on stack, but an empty string | |
223d09f6 | 921 | buf[0] = wxT('\0'); |
08b7c251 | 922 | } |
2bda0e17 | 923 | |
08b7c251 VZ |
924 | return wxString(buf); |
925 | } | |
2bda0e17 | 926 | |
08b7c251 VZ |
927 | void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text) |
928 | { | |
05b2a432 RD |
929 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
930 | ||
a9c1265f VZ |
931 | if ( IS_VIRTUAL_ROOT(item) ) |
932 | return; | |
933 | ||
08b7c251 | 934 | wxTreeViewItem tvItem(item, TVIF_TEXT); |
c9f78968 | 935 | tvItem.pszText = (wxChar *)text.wx_str(); // conversion is ok |
08b7c251 | 936 | DoSetItem(&tvItem); |
44fbc477 VZ |
937 | |
938 | // when setting the text of the item being edited, the text control should | |
939 | // be updated to reflect the new text as well, otherwise calling | |
940 | // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect | |
941 | // | |
942 | // don't use GetEditControl() here because m_textCtrl is not set yet | |
943 | HWND hwndEdit = TreeView_GetEditControl(GetHwnd()); | |
944 | if ( hwndEdit ) | |
945 | { | |
1a4088e1 | 946 | if ( item == m_idEdited ) |
44fbc477 | 947 | { |
e0a050e3 | 948 | ::SetWindowText(hwndEdit, text.wx_str()); |
44fbc477 VZ |
949 | } |
950 | } | |
08b7c251 | 951 | } |
2bda0e17 | 952 | |
4523ebb3 VZ |
953 | int wxTreeCtrl::GetItemImage(const wxTreeItemId& item, |
954 | wxTreeItemIcon which) const | |
74b31181 | 955 | { |
4523ebb3 VZ |
956 | wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") ); |
957 | ||
0228081f | 958 | if ( IsHiddenRoot(item) ) |
74b31181 | 959 | { |
4523ebb3 | 960 | // no images for hidden root item |
74b31181 VZ |
961 | return -1; |
962 | } | |
963 | ||
4523ebb3 VZ |
964 | wxTreeItemParam *param = GetItemParam(item); |
965 | ||
1b182562 | 966 | return param && param->HasImage(which) ? param->GetImage(which) : -1; |
74b31181 VZ |
967 | } |
968 | ||
4523ebb3 VZ |
969 | void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image, |
970 | wxTreeItemIcon which) | |
74b31181 | 971 | { |
4523ebb3 VZ |
972 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
973 | wxCHECK_RET( which >= 0 && | |
974 | which < wxTreeItemIcon_Max, | |
975 | wxT("invalid image index")); | |
976 | ||
977 | ||
0228081f | 978 | if ( IsHiddenRoot(item) ) |
74b31181 | 979 | { |
4523ebb3 | 980 | // no images for hidden root item |
74b31181 VZ |
981 | return; |
982 | } | |
983 | ||
4523ebb3 VZ |
984 | wxTreeItemParam *data = GetItemParam(item); |
985 | if ( !data ) | |
986 | return; | |
74b31181 VZ |
987 | |
988 | data->SetImage(image, which); | |
f5bed7a8 VS |
989 | |
990 | RefreshItem(item); | |
74b31181 VZ |
991 | } |
992 | ||
4523ebb3 | 993 | wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const |
2bda0e17 | 994 | { |
05b2a432 RD |
995 | wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") ); |
996 | ||
08b7c251 | 997 | wxTreeViewItem tvItem(item, TVIF_PARAM); |
a9c1265f | 998 | |
ae322a48 VZ |
999 | // hidden root may still have data. |
1000 | if ( IS_VIRTUAL_ROOT(item) ) | |
1001 | { | |
1002 | return GET_VIRTUAL_ROOT()->GetParam(); | |
1003 | } | |
1004 | ||
1005 | // visible node. | |
1006 | if ( !DoGetItem(&tvItem) ) | |
08b7c251 VZ |
1007 | { |
1008 | return NULL; | |
1009 | } | |
2bda0e17 | 1010 | |
4523ebb3 | 1011 | return (wxTreeItemParam *)tvItem.lParam; |
2bda0e17 KB |
1012 | } |
1013 | ||
4523ebb3 | 1014 | wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const |
2bda0e17 | 1015 | { |
4523ebb3 | 1016 | wxTreeItemParam *data = GetItemParam(item); |
05b2a432 | 1017 | |
4523ebb3 VZ |
1018 | return data ? data->GetData() : NULL; |
1019 | } | |
a9c1265f | 1020 | |
4523ebb3 VZ |
1021 | void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) |
1022 | { | |
188781db VZ |
1023 | // first, associate this piece of data with this item |
1024 | if ( data ) | |
1025 | { | |
1026 | data->SetId(item); | |
1027 | } | |
1028 | ||
4523ebb3 | 1029 | wxTreeItemParam *param = GetItemParam(item); |
74b31181 | 1030 | |
4523ebb3 | 1031 | wxCHECK_RET( param, wxT("failed to change tree items data") ); |
74b31181 | 1032 | |
4523ebb3 | 1033 | param->SetData(data); |
08b7c251 | 1034 | } |
2bda0e17 | 1035 | |
3a5a2f56 VZ |
1036 | void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has) |
1037 | { | |
05b2a432 RD |
1038 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1039 | ||
a9c1265f VZ |
1040 | if ( IS_VIRTUAL_ROOT(item) ) |
1041 | return; | |
1042 | ||
3a5a2f56 VZ |
1043 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
1044 | tvItem.cChildren = (int)has; | |
1045 | DoSetItem(&tvItem); | |
1046 | } | |
1047 | ||
add28c55 VZ |
1048 | void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold) |
1049 | { | |
05b2a432 RD |
1050 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1051 | ||
a9c1265f VZ |
1052 | if ( IS_VIRTUAL_ROOT(item) ) |
1053 | return; | |
1054 | ||
add28c55 VZ |
1055 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); |
1056 | tvItem.state = bold ? TVIS_BOLD : 0; | |
1057 | DoSetItem(&tvItem); | |
1058 | } | |
1059 | ||
58a8ab88 JS |
1060 | void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight) |
1061 | { | |
a9c1265f VZ |
1062 | if ( IS_VIRTUAL_ROOT(item) ) |
1063 | return; | |
1064 | ||
58a8ab88 JS |
1065 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED); |
1066 | tvItem.state = highlight ? TVIS_DROPHILITED : 0; | |
1067 | DoSetItem(&tvItem); | |
1068 | } | |
1069 | ||
d00407b2 VZ |
1070 | void wxTreeCtrl::RefreshItem(const wxTreeItemId& item) |
1071 | { | |
a9c1265f VZ |
1072 | if ( IS_VIRTUAL_ROOT(item) ) |
1073 | return; | |
1074 | ||
d00407b2 VZ |
1075 | wxRect rect; |
1076 | if ( GetBoundingRect(item, rect) ) | |
1077 | { | |
1078 | RefreshRect(rect); | |
1079 | } | |
1080 | } | |
1081 | ||
2b5f62a0 VZ |
1082 | wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const |
1083 | { | |
05b2a432 | 1084 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
2b5f62a0 | 1085 | |
05b2a432 | 1086 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1087 | return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour(); |
2b5f62a0 VZ |
1088 | } |
1089 | ||
1090 | wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const | |
1091 | { | |
05b2a432 | 1092 | wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") ); |
2b5f62a0 | 1093 | |
05b2a432 | 1094 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1095 | return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour(); |
2b5f62a0 VZ |
1096 | } |
1097 | ||
1098 | wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const | |
1099 | { | |
05b2a432 | 1100 | wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") ); |
2b5f62a0 | 1101 | |
05b2a432 | 1102 | wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1103 | return it == m_attrs.end() ? wxNullFont : it->second->GetFont(); |
2b5f62a0 VZ |
1104 | } |
1105 | ||
696e1ea0 VZ |
1106 | void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item, |
1107 | const wxColour& col) | |
1108 | { | |
05b2a432 RD |
1109 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1110 | ||
ee4b2721 | 1111 | wxTreeItemAttr *attr; |
f888d614 | 1112 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1113 | if ( it == m_attrs.end() ) |
696e1ea0 | 1114 | { |
ee4b2721 VZ |
1115 | m_hasAnyAttr = true; |
1116 | ||
f888d614 | 1117 | m_attrs[item.m_pItem] = |
696e1ea0 | 1118 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1119 | } |
1120 | else | |
1121 | { | |
1122 | attr = it->second; | |
696e1ea0 VZ |
1123 | } |
1124 | ||
1125 | attr->SetTextColour(col); | |
d00407b2 VZ |
1126 | |
1127 | RefreshItem(item); | |
696e1ea0 VZ |
1128 | } |
1129 | ||
1130 | void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item, | |
1131 | const wxColour& col) | |
1132 | { | |
05b2a432 RD |
1133 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1134 | ||
ee4b2721 | 1135 | wxTreeItemAttr *attr; |
f888d614 | 1136 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1137 | if ( it == m_attrs.end() ) |
696e1ea0 | 1138 | { |
ee4b2721 VZ |
1139 | m_hasAnyAttr = true; |
1140 | ||
f888d614 | 1141 | m_attrs[item.m_pItem] = |
696e1ea0 | 1142 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1143 | } |
1144 | else // already in the hash | |
1145 | { | |
1146 | attr = it->second; | |
696e1ea0 VZ |
1147 | } |
1148 | ||
1149 | attr->SetBackgroundColour(col); | |
d00407b2 VZ |
1150 | |
1151 | RefreshItem(item); | |
696e1ea0 VZ |
1152 | } |
1153 | ||
1154 | void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font) | |
1155 | { | |
05b2a432 RD |
1156 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1157 | ||
ee4b2721 | 1158 | wxTreeItemAttr *attr; |
f888d614 | 1159 | wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem); |
ee4b2721 | 1160 | if ( it == m_attrs.end() ) |
696e1ea0 | 1161 | { |
ee4b2721 VZ |
1162 | m_hasAnyAttr = true; |
1163 | ||
f888d614 | 1164 | m_attrs[item.m_pItem] = |
696e1ea0 | 1165 | attr = new wxTreeItemAttr; |
ee4b2721 VZ |
1166 | } |
1167 | else // already in the hash | |
1168 | { | |
1169 | attr = it->second; | |
696e1ea0 VZ |
1170 | } |
1171 | ||
1172 | attr->SetFont(font); | |
d00407b2 | 1173 | |
2bd16277 RD |
1174 | // Reset the item's text to ensure that the bounding rect will be adjusted |
1175 | // for the new font. | |
1176 | SetItemText(item, GetItemText(item)); | |
72e61024 | 1177 | |
d00407b2 | 1178 | RefreshItem(item); |
696e1ea0 VZ |
1179 | } |
1180 | ||
08b7c251 VZ |
1181 | // ---------------------------------------------------------------------------- |
1182 | // Item status | |
1183 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1184 | |
08b7c251 VZ |
1185 | bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const |
1186 | { | |
bfbb0b4c | 1187 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1188 | |
2b5f62a0 VZ |
1189 | if ( item == wxTreeItemId(TVI_ROOT) ) |
1190 | { | |
1191 | // virtual (hidden) root is never visible | |
04cd30de | 1192 | return false; |
2b5f62a0 VZ |
1193 | } |
1194 | ||
add28c55 | 1195 | // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect |
08b7c251 | 1196 | RECT rect; |
955be36c VZ |
1197 | |
1198 | // this ugliness comes directly from MSDN - it *is* the correct way to pass | |
1199 | // the HTREEITEM with TVM_GETITEMRECT | |
ee4b2721 | 1200 | *(HTREEITEM *)&rect = HITEM(item); |
955be36c | 1201 | |
caea927d VZ |
1202 | // true means to get rect for just the text, not the whole line |
1203 | if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT, true, (LPARAM)&rect) ) | |
1204 | { | |
1205 | // if TVM_GETITEMRECT returned false, then the item is definitely not | |
1206 | // visible (because its parent is not expanded) | |
1207 | return false; | |
1208 | } | |
1209 | ||
1210 | // however if it returned true, the item might still be outside the | |
1211 | // currently visible part of the tree, test for it (notice that partly | |
1212 | // visible means visible here) | |
1213 | return rect.bottom > 0 && rect.top < GetClientSize().y; | |
2bda0e17 KB |
1214 | } |
1215 | ||
08b7c251 | 1216 | bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const |
2bda0e17 | 1217 | { |
bfbb0b4c | 1218 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1219 | |
fa97ee24 VZ |
1220 | if ( IS_VIRTUAL_ROOT(item) ) |
1221 | { | |
1222 | wxTreeItemIdValue cookie; | |
1223 | return GetFirstChild(item, cookie).IsOk(); | |
1224 | } | |
1225 | ||
08b7c251 | 1226 | wxTreeViewItem tvItem(item, TVIF_CHILDREN); |
ae322a48 | 1227 | DoGetItem(&tvItem); |
2bda0e17 | 1228 | |
08b7c251 | 1229 | return tvItem.cChildren != 0; |
2bda0e17 KB |
1230 | } |
1231 | ||
08b7c251 | 1232 | bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const |
2bda0e17 | 1233 | { |
bfbb0b4c | 1234 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
2bda0e17 | 1235 | |
08b7c251 VZ |
1236 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED); |
1237 | DoGetItem(&tvItem); | |
2bda0e17 | 1238 | |
08b7c251 | 1239 | return (tvItem.state & TVIS_EXPANDED) != 0; |
2bda0e17 KB |
1240 | } |
1241 | ||
08b7c251 | 1242 | bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const |
2bda0e17 | 1243 | { |
bfbb0b4c | 1244 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1245 | |
08b7c251 VZ |
1246 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED); |
1247 | DoGetItem(&tvItem); | |
2bda0e17 | 1248 | |
08b7c251 | 1249 | return (tvItem.state & TVIS_SELECTED) != 0; |
2bda0e17 KB |
1250 | } |
1251 | ||
add28c55 VZ |
1252 | bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const |
1253 | { | |
bfbb0b4c | 1254 | wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") ); |
05b2a432 | 1255 | |
add28c55 VZ |
1256 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD); |
1257 | DoGetItem(&tvItem); | |
1258 | ||
1259 | return (tvItem.state & TVIS_BOLD) != 0; | |
1260 | } | |
1261 | ||
08b7c251 VZ |
1262 | // ---------------------------------------------------------------------------- |
1263 | // navigation | |
1264 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1265 | |
08b7c251 VZ |
1266 | wxTreeItemId wxTreeCtrl::GetRootItem() const |
1267 | { | |
a9c1265f VZ |
1268 | // Root may be real (visible) or virtual (hidden). |
1269 | if ( GET_VIRTUAL_ROOT() ) | |
1270 | return TVI_ROOT; | |
1271 | ||
ee4b2721 | 1272 | return wxTreeItemId(TreeView_GetRoot(GetHwnd())); |
08b7c251 | 1273 | } |
2bda0e17 | 1274 | |
08b7c251 VZ |
1275 | wxTreeItemId wxTreeCtrl::GetSelection() const |
1276 | { | |
f888d614 | 1277 | wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(), |
223d09f6 | 1278 | wxT("this only works with single selection controls") ); |
9dfbf520 | 1279 | |
ee4b2721 | 1280 | return wxTreeItemId(TreeView_GetSelection(GetHwnd())); |
2bda0e17 KB |
1281 | } |
1282 | ||
99006e44 | 1283 | wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const |
2bda0e17 | 1284 | { |
05b2a432 RD |
1285 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
1286 | ||
c9b142c9 VZ |
1287 | HTREEITEM hItem; |
1288 | ||
1289 | if ( IS_VIRTUAL_ROOT(item) ) | |
1290 | { | |
1291 | // no parent for the virtual root | |
1292 | hItem = 0; | |
1293 | } | |
1294 | else // normal item | |
a9c1265f | 1295 | { |
c9b142c9 VZ |
1296 | hItem = TreeView_GetParent(GetHwnd(), HITEM(item)); |
1297 | if ( !hItem && HasFlag(wxTR_HIDE_ROOT) ) | |
1298 | { | |
1299 | // the top level items should have the virtual root as their parent | |
1300 | hItem = TVI_ROOT; | |
1301 | } | |
a9c1265f VZ |
1302 | } |
1303 | ||
ee4b2721 | 1304 | return wxTreeItemId(hItem); |
08b7c251 | 1305 | } |
2bda0e17 | 1306 | |
08b7c251 | 1307 | wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, |
ee4b2721 | 1308 | wxTreeItemIdValue& cookie) const |
08b7c251 | 1309 | { |
05b2a432 RD |
1310 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
1311 | ||
08b7c251 | 1312 | // remember the last child returned in 'cookie' |
ee4b2721 VZ |
1313 | cookie = TreeView_GetChild(GetHwnd(), HITEM(item)); |
1314 | ||
1315 | return wxTreeItemId(cookie); | |
1316 | } | |
1317 | ||
1318 | wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item), | |
1319 | wxTreeItemIdValue& cookie) const | |
1320 | { | |
44d60c0b WS |
1321 | wxTreeItemId fromCookie(cookie); |
1322 | ||
1323 | HTREEITEM hitem = HITEM(fromCookie); | |
1324 | ||
1325 | hitem = TreeView_GetNextSibling(GetHwnd(), hitem); | |
1326 | ||
1327 | wxTreeItemId item(hitem); | |
1328 | ||
ee4b2721 VZ |
1329 | cookie = item.m_pItem; |
1330 | ||
1331 | return item; | |
1332 | } | |
2bda0e17 | 1333 | |
978f38c2 VZ |
1334 | wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const |
1335 | { | |
05b2a432 RD |
1336 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
1337 | ||
978f38c2 | 1338 | // can this be done more efficiently? |
ee4b2721 | 1339 | wxTreeItemIdValue cookie; |
978f38c2 VZ |
1340 | |
1341 | wxTreeItemId childLast, | |
2165ad93 | 1342 | child = GetFirstChild(item, cookie); |
978f38c2 VZ |
1343 | while ( child.IsOk() ) |
1344 | { | |
1345 | childLast = child; | |
2165ad93 | 1346 | child = GetNextChild(item, cookie); |
978f38c2 VZ |
1347 | } |
1348 | ||
1349 | return childLast; | |
1350 | } | |
1351 | ||
08b7c251 VZ |
1352 | wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const |
1353 | { | |
05b2a432 | 1354 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
ee4b2721 | 1355 | return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1356 | } |
1357 | ||
08b7c251 | 1358 | wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const |
2bda0e17 | 1359 | { |
05b2a432 | 1360 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
ee4b2721 | 1361 | return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item))); |
2bda0e17 KB |
1362 | } |
1363 | ||
08b7c251 | 1364 | wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const |
2bda0e17 | 1365 | { |
ee4b2721 | 1366 | return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd())); |
2bda0e17 KB |
1367 | } |
1368 | ||
08b7c251 | 1369 | wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const |
2bda0e17 | 1370 | { |
05b2a432 | 1371 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
f6bcfd97 | 1372 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!")); |
02ce7b72 | 1373 | |
f73eddd2 VZ |
1374 | wxTreeItemId next(TreeView_GetNextVisible(GetHwnd(), HITEM(item))); |
1375 | if ( next.IsOk() && !IsVisible(next) ) | |
1376 | { | |
1377 | // Win32 considers that any non-collapsed item is visible while we want | |
1378 | // to return only really visible items | |
1379 | next.Unset(); | |
1380 | } | |
1381 | ||
1382 | return next; | |
08b7c251 | 1383 | } |
02ce7b72 | 1384 | |
08b7c251 VZ |
1385 | wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const |
1386 | { | |
05b2a432 | 1387 | wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") ); |
f6bcfd97 | 1388 | wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!")); |
02ce7b72 | 1389 | |
f73eddd2 VZ |
1390 | wxTreeItemId prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item))); |
1391 | if ( prev.IsOk() && !IsVisible(prev) ) | |
1392 | { | |
1393 | // just as above, Win32 function will happily return the previous item | |
1394 | // in the tree for the first visible item too | |
1395 | prev.Unset(); | |
1396 | } | |
1397 | ||
1398 | return prev; | |
08b7c251 | 1399 | } |
02ce7b72 | 1400 | |
9dfbf520 VZ |
1401 | // ---------------------------------------------------------------------------- |
1402 | // multiple selections emulation | |
1403 | // ---------------------------------------------------------------------------- | |
1404 | ||
33961d59 RR |
1405 | size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const |
1406 | { | |
1407 | TraverseSelections selector(this, selections); | |
9dfbf520 | 1408 | |
e47c4d48 | 1409 | return selector.GetCount(); |
9dfbf520 VZ |
1410 | } |
1411 | ||
08b7c251 VZ |
1412 | // ---------------------------------------------------------------------------- |
1413 | // Usual operations | |
1414 | // ---------------------------------------------------------------------------- | |
02ce7b72 | 1415 | |
8cee4a30 VZ |
1416 | wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent, |
1417 | const wxTreeItemId& hInsertAfter, | |
1418 | const wxString& text, | |
1419 | int image, int selectedImage, | |
1420 | wxTreeItemData *data) | |
08b7c251 | 1421 | { |
0e935169 VZ |
1422 | wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()), |
1423 | wxTreeItemId(), | |
1424 | _T("can't have more than one root in the tree") ); | |
1425 | ||
08b7c251 | 1426 | TV_INSERTSTRUCT tvIns; |
3f7bc32b VZ |
1427 | tvIns.hParent = HITEM(parent); |
1428 | tvIns.hInsertAfter = HITEM(hInsertAfter); | |
58a8ab88 | 1429 | |
74b31181 VZ |
1430 | // this is how we insert the item as the first child: supply a NULL |
1431 | // hInsertAfter | |
1432 | if ( !tvIns.hInsertAfter ) | |
58a8ab88 JS |
1433 | { |
1434 | tvIns.hInsertAfter = TVI_FIRST; | |
1435 | } | |
1436 | ||
08b7c251 | 1437 | UINT mask = 0; |
44d60c0b | 1438 | if ( !text.empty() ) |
08b7c251 VZ |
1439 | { |
1440 | mask |= TVIF_TEXT; | |
c9f78968 | 1441 | tvIns.item.pszText = (wxChar *)text.wx_str(); // cast is ok |
08b7c251 | 1442 | } |
f6bcfd97 BP |
1443 | else |
1444 | { | |
1445 | tvIns.item.pszText = NULL; | |
1446 | tvIns.item.cchTextMax = 0; | |
1447 | } | |
02ce7b72 | 1448 | |
1b182562 VZ |
1449 | // create the param which will store the other item parameters |
1450 | wxTreeItemParam *param = new wxTreeItemParam; | |
1451 | ||
1452 | // we return the images on demand as they depend on whether the item is | |
1453 | // expanded or collapsed too in our case | |
4523ebb3 VZ |
1454 | mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE; |
1455 | tvIns.item.iImage = I_IMAGECALLBACK; | |
1456 | tvIns.item.iSelectedImage = I_IMAGECALLBACK; | |
3a5a2f56 | 1457 | |
4523ebb3 | 1458 | param->SetImage(image, wxTreeItemIcon_Normal); |
1b182562 | 1459 | param->SetImage(selectedImage, wxTreeItemIcon_Selected); |
02ce7b72 | 1460 | |
4523ebb3 VZ |
1461 | mask |= TVIF_PARAM; |
1462 | tvIns.item.lParam = (LPARAM)param; | |
08b7c251 | 1463 | tvIns.item.mask = mask; |
02ce7b72 | 1464 | |
4a36036e VZ |
1465 | // don't use the hack below for the children of hidden root: this results |
1466 | // in a crash inside comctl32.dll when we call TreeView_GetItemRect() | |
1467 | const bool firstChild = !IsHiddenRoot(parent) && | |
1468 | !TreeView_GetChild(GetHwnd(), HITEM(parent)); | |
7e45c159 | 1469 | |
1b182562 | 1470 | HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns); |
08b7c251 VZ |
1471 | if ( id == 0 ) |
1472 | { | |
f6bcfd97 | 1473 | wxLogLastError(wxT("TreeView_InsertItem")); |
08b7c251 | 1474 | } |
02ce7b72 | 1475 | |
7e45c159 VZ |
1476 | // apparently some Windows versions (2000 and XP are reported to do this) |
1477 | // sometimes don't refresh the tree after adding the first child and so we | |
1478 | // need this to make the "[+]" appear | |
1479 | if ( firstChild ) | |
1480 | { | |
1481 | RECT rect; | |
1482 | TreeView_GetItemRect(GetHwnd(), HITEM(parent), &rect, FALSE); | |
1483 | ::InvalidateRect(GetHwnd(), &rect, FALSE); | |
1484 | } | |
1485 | ||
4523ebb3 VZ |
1486 | // associate the application tree item with Win32 tree item handle |
1487 | param->SetItem(id); | |
1488 | ||
1489 | // setup wxTreeItemData | |
fd3f686c VZ |
1490 | if ( data != NULL ) |
1491 | { | |
4523ebb3 | 1492 | param->SetData(data); |
ee4b2721 | 1493 | data->SetId(id); |
fd3f686c VZ |
1494 | } |
1495 | ||
ee4b2721 | 1496 | return wxTreeItemId(id); |
2bda0e17 KB |
1497 | } |
1498 | ||
08b7c251 VZ |
1499 | wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text, |
1500 | int image, int selectedImage, | |
1501 | wxTreeItemData *data) | |
2bda0e17 | 1502 | { |
0228081f | 1503 | if ( HasFlag(wxTR_HIDE_ROOT) ) |
a9c1265f | 1504 | { |
2a69b4e8 VZ |
1505 | wxASSERT_MSG( !m_pVirtualRoot, _T("tree can have only a single root") ); |
1506 | ||
a9c1265f | 1507 | // create a virtual root item, the parent for all the others |
4523ebb3 VZ |
1508 | wxTreeItemParam *param = new wxTreeItemParam; |
1509 | param->SetData(data); | |
1510 | ||
1511 | m_pVirtualRoot = new wxVirtualNode(param); | |
a9c1265f VZ |
1512 | |
1513 | return TVI_ROOT; | |
1514 | } | |
1515 | ||
8cee4a30 VZ |
1516 | return DoInsertAfter(wxTreeItemId(), wxTreeItemId(), |
1517 | text, image, selectedImage, data); | |
2bda0e17 KB |
1518 | } |
1519 | ||
8cee4a30 VZ |
1520 | wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent, |
1521 | size_t index, | |
1522 | const wxString& text, | |
1523 | int image, int selectedImage, | |
1524 | wxTreeItemData *data) | |
2ef31e80 | 1525 | { |
8cee4a30 VZ |
1526 | wxTreeItemId idPrev; |
1527 | if ( index == (size_t)-1 ) | |
2ef31e80 | 1528 | { |
8cee4a30 VZ |
1529 | // special value: append to the end |
1530 | idPrev = TVI_LAST; | |
2ef31e80 | 1531 | } |
8cee4a30 VZ |
1532 | else // find the item from index |
1533 | { | |
1534 | wxTreeItemIdValue cookie; | |
1535 | wxTreeItemId idCur = GetFirstChild(parent, cookie); | |
1536 | while ( index != 0 && idCur.IsOk() ) | |
1537 | { | |
1538 | index--; | |
2ef31e80 | 1539 | |
8cee4a30 VZ |
1540 | idPrev = idCur; |
1541 | idCur = GetNextChild(parent, cookie); | |
1542 | } | |
2ef31e80 | 1543 | |
8cee4a30 VZ |
1544 | // assert, not check: if the index is invalid, we will append the item |
1545 | // to the end | |
1546 | wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") ); | |
1547 | } | |
2ef31e80 | 1548 | |
8cee4a30 | 1549 | return DoInsertAfter(parent, idPrev, text, image, selectedImage, data); |
2bda0e17 KB |
1550 | } |
1551 | ||
08b7c251 | 1552 | void wxTreeCtrl::Delete(const wxTreeItemId& item) |
2bda0e17 | 1553 | { |
396f85ba BW |
1554 | // unlock tree selections on vista, without this the |
1555 | // tree ctrl will eventually crash after item deletion | |
1556 | TreeItemUnlocker unlock_all; | |
1557 | ||
3f7bc32b | 1558 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) ) |
bbcdf8bc | 1559 | { |
f6bcfd97 | 1560 | wxLogLastError(wxT("TreeView_DeleteItem")); |
bbcdf8bc | 1561 | } |
bbcdf8bc JS |
1562 | } |
1563 | ||
23fd5130 VZ |
1564 | // delete all children (but don't delete the item itself) |
1565 | void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item) | |
1566 | { | |
396f85ba BW |
1567 | // unlock tree selections on vista for the duration of this call |
1568 | TreeItemUnlocker unlock_all; | |
1569 | ||
ee4b2721 | 1570 | wxTreeItemIdValue cookie; |
23fd5130 | 1571 | |
ee4b2721 | 1572 | wxArrayTreeItemIds children; |
23fd5130 VZ |
1573 | wxTreeItemId child = GetFirstChild(item, cookie); |
1574 | while ( child.IsOk() ) | |
1575 | { | |
ee4b2721 | 1576 | children.Add(child); |
23fd5130 VZ |
1577 | |
1578 | child = GetNextChild(item, cookie); | |
1579 | } | |
1580 | ||
1581 | size_t nCount = children.Count(); | |
1582 | for ( size_t n = 0; n < nCount; n++ ) | |
1583 | { | |
14ba002a | 1584 | if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children[n])) ) |
23fd5130 | 1585 | { |
f6bcfd97 | 1586 | wxLogLastError(wxT("TreeView_DeleteItem")); |
23fd5130 VZ |
1587 | } |
1588 | } | |
1589 | } | |
1590 | ||
08b7c251 | 1591 | void wxTreeCtrl::DeleteAllItems() |
bbcdf8bc | 1592 | { |
396f85ba BW |
1593 | // unlock tree selections on vista for the duration of this call |
1594 | TreeItemUnlocker unlock_all; | |
1595 | ||
bfedf621 VZ |
1596 | // delete the "virtual" root item. |
1597 | if ( GET_VIRTUAL_ROOT() ) | |
1598 | { | |
1599 | delete GET_VIRTUAL_ROOT(); | |
1600 | m_pVirtualRoot = NULL; | |
1601 | } | |
1602 | ||
1603 | // and all the real items | |
a9c1265f | 1604 | |
d220ae32 | 1605 | if ( !TreeView_DeleteAllItems(GetHwnd()) ) |
bbcdf8bc | 1606 | { |
f6bcfd97 | 1607 | wxLogLastError(wxT("TreeView_DeleteAllItems")); |
bbcdf8bc | 1608 | } |
2bda0e17 KB |
1609 | } |
1610 | ||
08b7c251 | 1611 | void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag) |
2bda0e17 | 1612 | { |
dd3646fd VZ |
1613 | wxASSERT_MSG( flag == TVE_COLLAPSE || |
1614 | flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) || | |
1615 | flag == TVE_EXPAND || | |
1616 | flag == TVE_TOGGLE, | |
223d09f6 | 1617 | wxT("Unknown flag in wxTreeCtrl::DoExpand") ); |
08b7c251 | 1618 | |
a9c1265f | 1619 | // A hidden root can be neither expanded nor collapsed. |
0228081f | 1620 | wxCHECK_RET( !IsHiddenRoot(item), |
637b7e4f | 1621 | wxT("Can't expand/collapse hidden root node!") ); |
a9c1265f | 1622 | |
08b7c251 | 1623 | // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must |
d220ae32 VZ |
1624 | // emulate them. This behaviour has changed slightly with comctl32.dll |
1625 | // v 4.70 - now it does send them but only the first time. To maintain | |
1626 | // compatible behaviour and also in order to not have surprises with the | |
1627 | // future versions, don't rely on this and still do everything ourselves. | |
1628 | // To avoid that the messages be sent twice when the item is expanded for | |
1629 | // the first time we must clear TVIS_EXPANDEDONCE style manually. | |
1630 | ||
1631 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE); | |
1632 | tvItem.state = 0; | |
1633 | DoSetItem(&tvItem); | |
1634 | ||
3f7bc32b | 1635 | if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 ) |
08b7c251 | 1636 | { |
deb1de30 VZ |
1637 | // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand() |
1638 | // itself | |
09f277d6 VZ |
1639 | wxTreeEvent event(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND |
1640 | : IDX_COLLAPSE] | |
1641 | [IDX_DONE], | |
1642 | this, item); | |
937013e0 | 1643 | (void)HandleWindowEvent(event); |
08b7c251 | 1644 | } |
d220ae32 | 1645 | //else: change didn't took place, so do nothing at all |
2bda0e17 KB |
1646 | } |
1647 | ||
08b7c251 | 1648 | void wxTreeCtrl::Expand(const wxTreeItemId& item) |
2bda0e17 | 1649 | { |
08b7c251 | 1650 | DoExpand(item, TVE_EXPAND); |
2bda0e17 | 1651 | } |
2bda0e17 | 1652 | |
08b7c251 | 1653 | void wxTreeCtrl::Collapse(const wxTreeItemId& item) |
2bda0e17 | 1654 | { |
08b7c251 | 1655 | DoExpand(item, TVE_COLLAPSE); |
2bda0e17 KB |
1656 | } |
1657 | ||
08b7c251 | 1658 | void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item) |
2bda0e17 | 1659 | { |
dd3646fd | 1660 | DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET); |
2bda0e17 KB |
1661 | } |
1662 | ||
08b7c251 | 1663 | void wxTreeCtrl::Toggle(const wxTreeItemId& item) |
2bda0e17 | 1664 | { |
08b7c251 | 1665 | DoExpand(item, TVE_TOGGLE); |
2bda0e17 KB |
1666 | } |
1667 | ||
08b7c251 | 1668 | void wxTreeCtrl::Unselect() |
2bda0e17 | 1669 | { |
3f7bc32b VZ |
1670 | wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), |
1671 | wxT("doesn't make sense, may be you want UnselectAll()?") ); | |
9dfbf520 VZ |
1672 | |
1673 | // just remove the selection | |
ee4b2721 | 1674 | SelectItem(wxTreeItemId()); |
08b7c251 | 1675 | } |
02ce7b72 | 1676 | |
9dfbf520 | 1677 | void wxTreeCtrl::UnselectAll() |
08b7c251 | 1678 | { |
9dfbf520 | 1679 | if ( m_windowStyle & wxTR_MULTIPLE ) |
2bda0e17 | 1680 | { |
9dfbf520 VZ |
1681 | wxArrayTreeItemIds selections; |
1682 | size_t count = GetSelections(selections); | |
1683 | for ( size_t n = 0; n < count; n++ ) | |
d220ae32 | 1684 | { |
14ba002a | 1685 | ::UnselectItem(GetHwnd(), HITEM(selections[n])); |
d220ae32 | 1686 | } |
ba8a1961 VZ |
1687 | |
1688 | m_htSelStart.Unset(); | |
9dfbf520 | 1689 | } |
75afdc2d | 1690 | else |
9dfbf520 VZ |
1691 | { |
1692 | // just remove the selection | |
1693 | Unselect(); | |
1694 | } | |
1695 | } | |
1696 | ||
3e9af289 | 1697 | void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select) |
9dfbf520 | 1698 | { |
8a536fa3 VZ |
1699 | wxCHECK_RET( !IsHiddenRoot(item), _T("can't select hidden root item") ); |
1700 | ||
cc87a04c VZ |
1701 | wxASSERT_MSG( select || HasFlag(wxTR_MULTIPLE), |
1702 | _T("SelectItem(false) works only for multiselect") ); | |
9dfbf520 | 1703 | |
cc87a04c | 1704 | wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item); |
937013e0 | 1705 | if ( !HandleWindowEvent(event) || event.IsAllowed() ) |
cc87a04c | 1706 | { |
7549148b | 1707 | if ( HasFlag(wxTR_MULTIPLE) ) |
d220ae32 | 1708 | { |
7549148b VZ |
1709 | if ( !::SelectItem(GetHwnd(), HITEM(item), select) ) |
1710 | { | |
1711 | wxLogLastError(wxT("TreeView_SelectItem")); | |
1712 | return; | |
1713 | } | |
cc87a04c | 1714 | } |
7549148b | 1715 | else // single selection |
cc87a04c | 1716 | { |
7549148b VZ |
1717 | // use TreeView_SelectItem() to deselect the previous selection |
1718 | if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) ) | |
1719 | { | |
1720 | wxLogLastError(wxT("TreeView_SelectItem")); | |
1721 | return; | |
1722 | } | |
d220ae32 | 1723 | } |
7549148b VZ |
1724 | |
1725 | event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED); | |
937013e0 | 1726 | (void)HandleWindowEvent(event); |
2bda0e17 | 1727 | } |
cc87a04c | 1728 | //else: program vetoed the change |
08b7c251 | 1729 | } |
2bda0e17 | 1730 | |
08b7c251 VZ |
1731 | void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item) |
1732 | { | |
0228081f VZ |
1733 | wxCHECK_RET( !IsHiddenRoot(item), _T("can't show hidden root item") ); |
1734 | ||
08b7c251 | 1735 | // no error return |
3f7bc32b | 1736 | TreeView_EnsureVisible(GetHwnd(), HITEM(item)); |
08b7c251 VZ |
1737 | } |
1738 | ||
1739 | void wxTreeCtrl::ScrollTo(const wxTreeItemId& item) | |
1740 | { | |
3f7bc32b | 1741 | if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) ) |
2bda0e17 | 1742 | { |
f6bcfd97 | 1743 | wxLogLastError(wxT("TreeView_SelectSetFirstVisible")); |
2bda0e17 | 1744 | } |
08b7c251 VZ |
1745 | } |
1746 | ||
44fbc477 | 1747 | wxTextCtrl *wxTreeCtrl::GetEditControl() const |
08b7c251 VZ |
1748 | { |
1749 | return m_textCtrl; | |
1750 | } | |
1751 | ||
1752 | void wxTreeCtrl::DeleteTextCtrl() | |
1753 | { | |
1754 | if ( m_textCtrl ) | |
2bda0e17 | 1755 | { |
b6a3d6ad VZ |
1756 | // the HWND corresponding to this control is deleted by the tree |
1757 | // control itself and we don't know when exactly this happens, so check | |
1758 | // if the window still exists before calling UnsubclassWin() | |
1759 | if ( !::IsWindow(GetHwndOf(m_textCtrl)) ) | |
1760 | { | |
1761 | m_textCtrl->SetHWND(0); | |
1762 | } | |
1763 | ||
08b7c251 VZ |
1764 | m_textCtrl->UnsubclassWin(); |
1765 | m_textCtrl->SetHWND(0); | |
1766 | delete m_textCtrl; | |
1767 | m_textCtrl = NULL; | |
1a4088e1 VZ |
1768 | |
1769 | m_idEdited.Unset(); | |
2bda0e17 | 1770 | } |
08b7c251 | 1771 | } |
2bda0e17 | 1772 | |
4523ebb3 VZ |
1773 | wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item, |
1774 | wxClassInfo *textControlClass) | |
08b7c251 VZ |
1775 | { |
1776 | wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); | |
1777 | ||
b94ae1ea VZ |
1778 | DeleteTextCtrl(); |
1779 | ||
1a4088e1 | 1780 | m_idEdited = item; |
cd6bd270 | 1781 | m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject(); |
3f7bc32b | 1782 | HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item)); |
2bda0e17 | 1783 | |
5ea47806 | 1784 | // this is not an error - the TVN_BEGINLABELEDIT handler might have |
04cd30de | 1785 | // returned false |
5ea47806 VZ |
1786 | if ( !hWnd ) |
1787 | { | |
cd6bd270 MB |
1788 | delete m_textCtrl; |
1789 | m_textCtrl = NULL; | |
5ea47806 VZ |
1790 | return NULL; |
1791 | } | |
2bda0e17 | 1792 | |
cd6bd270 | 1793 | // textctrl is subclassed in MSWOnNotify |
08b7c251 | 1794 | return m_textCtrl; |
2bda0e17 KB |
1795 | } |
1796 | ||
08b7c251 | 1797 | // End label editing, optionally cancelling the edit |
d35dce3a | 1798 | void wxTreeCtrl::DoEndEditLabel(bool discardChanges) |
2bda0e17 | 1799 | { |
d220ae32 | 1800 | TreeView_EndEditLabelNow(GetHwnd(), discardChanges); |
08b7c251 VZ |
1801 | |
1802 | DeleteTextCtrl(); | |
2bda0e17 KB |
1803 | } |
1804 | ||
be0e5d69 | 1805 | wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const |
2bda0e17 | 1806 | { |
08b7c251 VZ |
1807 | TV_HITTESTINFO hitTestInfo; |
1808 | hitTestInfo.pt.x = (int)point.x; | |
1809 | hitTestInfo.pt.y = (int)point.y; | |
2bda0e17 | 1810 | |
8ecd5de2 | 1811 | (void) TreeView_HitTest(GetHwnd(), &hitTestInfo); |
2bda0e17 | 1812 | |
08b7c251 VZ |
1813 | flags = 0; |
1814 | ||
1815 | // avoid repetition | |
1816 | #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \ | |
1817 | flags |= wxTREE_HITTEST_##flag | |
1818 | ||
1819 | TRANSLATE_FLAG(ABOVE); | |
1820 | TRANSLATE_FLAG(BELOW); | |
1821 | TRANSLATE_FLAG(NOWHERE); | |
1822 | TRANSLATE_FLAG(ONITEMBUTTON); | |
1823 | TRANSLATE_FLAG(ONITEMICON); | |
1824 | TRANSLATE_FLAG(ONITEMINDENT); | |
1825 | TRANSLATE_FLAG(ONITEMLABEL); | |
1826 | TRANSLATE_FLAG(ONITEMRIGHT); | |
1827 | TRANSLATE_FLAG(ONITEMSTATEICON); | |
1828 | TRANSLATE_FLAG(TOLEFT); | |
1829 | TRANSLATE_FLAG(TORIGHT); | |
2bda0e17 | 1830 | |
08b7c251 VZ |
1831 | #undef TRANSLATE_FLAG |
1832 | ||
ee4b2721 | 1833 | return wxTreeItemId(hitTestInfo.hItem); |
08b7c251 VZ |
1834 | } |
1835 | ||
f7c832a7 VZ |
1836 | bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item, |
1837 | wxRect& rect, | |
1838 | bool textOnly) const | |
1839 | { | |
1840 | RECT rc; | |
f2c3db9d RD |
1841 | |
1842 | // Virtual root items have no bounding rectangle | |
1843 | if ( IS_VIRTUAL_ROOT(item) ) | |
1844 | { | |
1845 | return false; | |
1846 | } | |
1847 | ||
3f7bc32b | 1848 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(item), |
f7c832a7 VZ |
1849 | &rc, textOnly) ) |
1850 | { | |
1851 | rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom)); | |
1852 | ||
04cd30de | 1853 | return true; |
f7c832a7 VZ |
1854 | } |
1855 | else | |
1856 | { | |
1857 | // couldn't retrieve rect: for example, item isn't visible | |
04cd30de | 1858 | return false; |
f7c832a7 VZ |
1859 | } |
1860 | } | |
1861 | ||
23fd5130 VZ |
1862 | // ---------------------------------------------------------------------------- |
1863 | // sorting stuff | |
1864 | // ---------------------------------------------------------------------------- | |
f7c832a7 | 1865 | |
502a2b18 VZ |
1866 | // this is just a tiny namespace which is friend to wxTreeCtrl and so can use |
1867 | // functions such as IsDataIndirect() | |
1868 | class wxTreeSortHelper | |
1869 | { | |
1870 | public: | |
1871 | static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree); | |
1872 | ||
1873 | private: | |
4523ebb3 | 1874 | static wxTreeItemId GetIdFromData(LPARAM lParam) |
502a2b18 | 1875 | { |
4523ebb3 | 1876 | return ((wxTreeItemParam*)lParam)->GetItem(); |
502a2b18 | 1877 | } |
502a2b18 VZ |
1878 | }; |
1879 | ||
1880 | int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1, | |
1881 | LPARAM pItem2, | |
1882 | LPARAM htree) | |
23fd5130 | 1883 | { |
096c9f9b | 1884 | wxCHECK_MSG( pItem1 && pItem2, 0, |
223d09f6 | 1885 | wxT("sorting tree without data doesn't make sense") ); |
096c9f9b | 1886 | |
502a2b18 VZ |
1887 | wxTreeCtrl *tree = (wxTreeCtrl *)htree; |
1888 | ||
4523ebb3 VZ |
1889 | return tree->OnCompareItems(GetIdFromData(pItem1), |
1890 | GetIdFromData(pItem2)); | |
23fd5130 VZ |
1891 | } |
1892 | ||
95aabccc VZ |
1893 | void wxTreeCtrl::SortChildren(const wxTreeItemId& item) |
1894 | { | |
05b2a432 RD |
1895 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
1896 | ||
95aabccc | 1897 | // rely on the fact that TreeView_SortChildren does the same thing as our |
23fd5130 VZ |
1898 | // default behaviour, i.e. sorts items alphabetically and so call it |
1899 | // directly if we're not in derived class (much more efficient!) | |
4523ebb3 VZ |
1900 | // RN: Note that if you find you're code doesn't sort as expected this |
1901 | // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS | |
1902 | // combo for your derived wxTreeCtrl if will sort without | |
1903 | // OnCompareItems | |
23fd5130 | 1904 | if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) ) |
2bda0e17 | 1905 | { |
3f7bc32b | 1906 | TreeView_SortChildren(GetHwnd(), HITEM(item), 0); |
2bda0e17 | 1907 | } |
08b7c251 | 1908 | else |
2bda0e17 | 1909 | { |
62448488 | 1910 | TV_SORTCB tvSort; |
3f7bc32b | 1911 | tvSort.hParent = HITEM(item); |
502a2b18 | 1912 | tvSort.lpfnCompare = wxTreeSortHelper::Compare; |
23fd5130 | 1913 | tvSort.lParam = (LPARAM)this; |
d220ae32 | 1914 | TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */); |
2bda0e17 | 1915 | } |
08b7c251 | 1916 | } |
2bda0e17 | 1917 | |
08b7c251 VZ |
1918 | // ---------------------------------------------------------------------------- |
1919 | // implementation | |
1920 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 1921 | |
90c6edd7 VZ |
1922 | bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg) |
1923 | { | |
1924 | if ( msg->message == WM_KEYDOWN ) | |
1925 | { | |
6719c06a VZ |
1926 | // Only eat VK_RETURN if not being used by the application in |
1927 | // conjunction with modifiers | |
1928 | if ( (msg->wParam == VK_RETURN) && !wxIsAnyModifierDown() ) | |
90c6edd7 VZ |
1929 | { |
1930 | // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED | |
1931 | return false; | |
1932 | } | |
1933 | } | |
1934 | ||
1935 | return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg); | |
1936 | } | |
1937 | ||
0edeeb6d | 1938 | bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id_) |
08b7c251 | 1939 | { |
0edeeb6d VZ |
1940 | const int id = (signed short)id_; |
1941 | ||
08b7c251 | 1942 | if ( cmd == EN_UPDATE ) |
2bda0e17 | 1943 | { |
08b7c251 VZ |
1944 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id); |
1945 | event.SetEventObject( this ); | |
1946 | ProcessCommand(event); | |
2bda0e17 | 1947 | } |
08b7c251 | 1948 | else if ( cmd == EN_KILLFOCUS ) |
2bda0e17 | 1949 | { |
08b7c251 VZ |
1950 | wxCommandEvent event(wxEVT_KILL_FOCUS, id); |
1951 | event.SetEventObject( this ); | |
1952 | ProcessCommand(event); | |
2bda0e17 | 1953 | } |
08b7c251 | 1954 | else |
2bda0e17 | 1955 | { |
08b7c251 | 1956 | // nothing done |
04cd30de | 1957 | return false; |
2bda0e17 | 1958 | } |
08b7c251 VZ |
1959 | |
1960 | // command processed | |
04cd30de | 1961 | return true; |
08b7c251 VZ |
1962 | } |
1963 | ||
23f681ec VZ |
1964 | // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we |
1965 | // only do it during dragging, minimize wxWin overhead (this is important for | |
1966 | // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly | |
1967 | // instead of passing by wxWin events | |
c140b7e7 | 1968 | WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) |
23f681ec | 1969 | { |
04cd30de | 1970 | bool processed = false; |
c140b7e7 | 1971 | WXLRESULT rc = 0; |
2b9a7d4c | 1972 | bool isMultiple = HasFlag(wxTR_MULTIPLE); |
3f7bc32b | 1973 | |
147964ca | 1974 | // This message is sent after a right-click, or when the "menu" key is pressed |
499bbaeb | 1975 | if ( nMsg == WM_CONTEXTMENU ) |
30f5e34f | 1976 | { |
147964ca KH |
1977 | int x = GET_X_LPARAM(lParam), |
1978 | y = GET_Y_LPARAM(lParam); | |
8f6dc819 VZ |
1979 | |
1980 | // the item for which the menu should be shown | |
1981 | wxTreeItemId item; | |
1982 | ||
1983 | // the position where the menu should be shown in client coordinates | |
1984 | // (so that it can be passed directly to PopupMenu()) | |
1985 | wxPoint pt; | |
1986 | ||
1987 | if ( x == -1 || y == -1 ) | |
147964ca | 1988 | { |
8f6dc819 VZ |
1989 | // this means that the event was generated from keyboard (e.g. with |
1990 | // Shift-F10 or special Windows menu key) | |
1991 | // | |
1992 | // use the Explorer standard of putting the menu at the left edge | |
1993 | // of the text, in the vertical middle of the text | |
1994 | item = wxTreeItemId(TreeView_GetSelection(GetHwnd())); | |
1995 | if ( item.IsOk() ) | |
1996 | { | |
1997 | // Use the bounding rectangle of only the text part | |
1998 | wxRect rect; | |
1999 | GetBoundingRect(item, rect, true); | |
2000 | pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2); | |
2001 | } | |
147964ca | 2002 | } |
8f6dc819 | 2003 | else // event from mouse, use mouse position |
147964ca | 2004 | { |
8f6dc819 VZ |
2005 | pt = ScreenToClient(wxPoint(x, y)); |
2006 | ||
2007 | TV_HITTESTINFO tvhti; | |
2008 | tvhti.pt.x = pt.x; | |
2009 | tvhti.pt.y = pt.y; | |
2010 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) | |
2011 | item = wxTreeItemId(tvhti.hItem); | |
147964ca KH |
2012 | } |
2013 | ||
8f6dc819 VZ |
2014 | // create the event |
2015 | wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item); | |
2016 | ||
2017 | event.m_pointDrag = pt; | |
2018 | ||
937013e0 | 2019 | if ( HandleWindowEvent(event) ) |
2b9a7d4c | 2020 | processed = true; |
9994e2da | 2021 | //else: continue with generating wxEVT_CONTEXT_MENU in base class code |
30f5e34f | 2022 | } |
2b9a7d4c | 2023 | else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) ) |
23f681ec | 2024 | { |
7bb8798c VZ |
2025 | // we only process mouse messages here and these parameters have the |
2026 | // same meaning for all of them | |
3f7bc32b VZ |
2027 | int x = GET_X_LPARAM(lParam), |
2028 | y = GET_Y_LPARAM(lParam); | |
8ecd5de2 | 2029 | |
e3ad5702 JS |
2030 | TV_HITTESTINFO tvht; |
2031 | tvht.pt.x = x; | |
2032 | tvht.pt.y = y; | |
8ecd5de2 | 2033 | |
8854a202 | 2034 | HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht); |
3f7bc32b | 2035 | |
23f681ec VZ |
2036 | switch ( nMsg ) |
2037 | { | |
3f7bc32b | 2038 | case WM_LBUTTONDOWN: |
e3ad5702 | 2039 | if ( htItem && isMultiple && (tvht.flags & TVHT_ONITEM) != 0 ) |
23f681ec | 2040 | { |
e3ad5702 JS |
2041 | m_htClickedItem = (WXHTREEITEM) htItem; |
2042 | m_ptClick = wxPoint(x, y); | |
8ecd5de2 | 2043 | |
3f7bc32b VZ |
2044 | if ( wParam & MK_CONTROL ) |
2045 | { | |
2046 | SetFocus(); | |
23f681ec | 2047 | |
3f7bc32b | 2048 | // toggle selected state |
cc87a04c | 2049 | ToggleItemSelection(htItem); |
3f7bc32b VZ |
2050 | |
2051 | ::SetFocus(GetHwnd(), htItem); | |
2052 | ||
2053 | // reset on any click without Shift | |
f888d614 | 2054 | m_htSelStart.Unset(); |
3f7bc32b | 2055 | |
04cd30de | 2056 | processed = true; |
3f7bc32b VZ |
2057 | } |
2058 | else if ( wParam & MK_SHIFT ) | |
2059 | { | |
2060 | // this selects all items between the starting one and | |
2061 | // the current | |
2062 | ||
2063 | if ( !m_htSelStart ) | |
2064 | { | |
2065 | // take the focused item | |
ee4b2721 | 2066 | m_htSelStart = TreeView_GetSelection(GetHwnd()); |
3f7bc32b VZ |
2067 | } |
2068 | ||
e3ad5702 JS |
2069 | if ( m_htSelStart ) |
2070 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem, | |
3f7bc32b | 2071 | !(wParam & MK_CONTROL)); |
e3ad5702 JS |
2072 | else |
2073 | ::SelectItem(GetHwnd(), htItem); | |
3f7bc32b VZ |
2074 | |
2075 | ::SetFocus(GetHwnd(), htItem); | |
23f681ec | 2076 | |
04cd30de | 2077 | processed = true; |
3f7bc32b VZ |
2078 | } |
2079 | else // normal click | |
2080 | { | |
df4ac4c7 VZ |
2081 | // avoid doing anything if we click on the only |
2082 | // currently selected item | |
8ecd5de2 | 2083 | |
e3ad5702 | 2084 | SetFocus(); |
5e7718a2 | 2085 | |
df4ac4c7 VZ |
2086 | wxArrayTreeItemIds selections; |
2087 | size_t count = GetSelections(selections); | |
2088 | if ( count == 0 || | |
35cf1ec6 | 2089 | count > 1 || |
14ba002a | 2090 | HITEM(selections[0]) != htItem ) |
df4ac4c7 | 2091 | { |
35cf1ec6 VZ |
2092 | // clear the previously selected items, if the |
2093 | // user clicked outside of the present selection. | |
2094 | // otherwise, perform the deselection on mouse-up. | |
2095 | // this allows multiple drag and drop to work. | |
5e7718a2 | 2096 | |
e3ad5702 | 2097 | if (!IsItemSelected(GetHwnd(), htItem)) |
35cf1ec6 VZ |
2098 | { |
2099 | UnselectAll(); | |
2100 | ||
2101 | // prevent the click from starting in-place editing | |
2102 | // which should only happen if we click on the | |
2103 | // already selected item (and nothing else is | |
2104 | // selected) | |
2105 | ||
2106 | TreeView_SelectItem(GetHwnd(), 0); | |
2107 | ::SelectItem(GetHwnd(), htItem); | |
2108 | } | |
e3ad5702 JS |
2109 | ::SetFocus(GetHwnd(), htItem); |
2110 | processed = true; | |
df4ac4c7 | 2111 | } |
b8e3f1cf VZ |
2112 | else // click on a single selected item |
2113 | { | |
2114 | // don't interfere with the default processing in | |
2115 | // WM_MOUSEMOVE handler below as the default window | |
2116 | // proc will start the drag itself if we let have | |
2117 | // WM_LBUTTONDOWN | |
2118 | m_htClickedItem.Unset(); | |
2119 | } | |
3f7bc32b VZ |
2120 | |
2121 | // reset on any click without Shift | |
f888d614 | 2122 | m_htSelStart.Unset(); |
3f7bc32b VZ |
2123 | } |
2124 | } | |
2125 | break; | |
3f7bc32b | 2126 | |
e0bf68d6 VZ |
2127 | case WM_RBUTTONDOWN: |
2128 | // default handler removes the highlight from the currently | |
2129 | // focused item when right mouse button is pressed on another | |
2130 | // one but keeps the remaining items highlighted, which is | |
2131 | // confusing, so override this default behaviour for tree with | |
2132 | // multiple selections | |
2133 | if ( isMultiple ) | |
2134 | { | |
2135 | if ( !IsItemSelected(GetHwnd(), htItem) ) | |
2136 | { | |
2137 | UnselectAll(); | |
2138 | SelectItem(htItem); | |
2139 | ::SetFocus(GetHwnd(), htItem); | |
2140 | } | |
2141 | ||
2142 | // fire EVT_RIGHT_DOWN | |
2143 | HandleMouseEvent(nMsg, x, y, wParam); | |
20bdddad | 2144 | |
e0bf68d6 VZ |
2145 | // send NM_RCLICK |
2146 | NMHDR nmhdr; | |
2147 | nmhdr.hwndFrom = GetHwnd(); | |
2148 | nmhdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID); | |
2149 | nmhdr.code = NM_RCLICK; | |
2150 | ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY, | |
2151 | nmhdr.idFrom, (LPARAM)&nmhdr); | |
2152 | ||
2153 | // prevent tree control default processing, as we've | |
20bdddad | 2154 | // already done everything |
e0bf68d6 VZ |
2155 | processed = true; |
2156 | } | |
2157 | break; | |
2158 | ||
3f7bc32b | 2159 | case WM_MOUSEMOVE: |
68fc69f3 | 2160 | #ifndef __WXWINCE__ |
e3ad5702 JS |
2161 | if ( m_htClickedItem ) |
2162 | { | |
2163 | int cx = abs(m_ptClick.x - x); | |
2164 | int cy = abs(m_ptClick.y - y); | |
2165 | ||
b8e3f1cf VZ |
2166 | if ( cx > ::GetSystemMetrics(SM_CXDRAG) || |
2167 | cy > ::GetSystemMetrics(SM_CYDRAG) ) | |
e3ad5702 | 2168 | { |
b8e3f1cf VZ |
2169 | NM_TREEVIEW tv; |
2170 | wxZeroMemory(tv); | |
e3ad5702 | 2171 | |
b8e3f1cf VZ |
2172 | tv.hdr.hwndFrom = GetHwnd(); |
2173 | tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID); | |
2174 | tv.hdr.code = TVN_BEGINDRAG; | |
8ecd5de2 | 2175 | |
b8e3f1cf | 2176 | tv.itemNew.hItem = HITEM(m_htClickedItem); |
8ecd5de2 | 2177 | |
8ecd5de2 | 2178 | |
b8e3f1cf VZ |
2179 | TVITEM tviAux; |
2180 | wxZeroMemory(tviAux); | |
8ecd5de2 | 2181 | |
b8e3f1cf VZ |
2182 | tviAux.hItem = HITEM(m_htClickedItem); |
2183 | tviAux.mask = TVIF_STATE | TVIF_PARAM; | |
2184 | tviAux.stateMask = 0xffffffff; | |
2185 | TreeView_GetItem(GetHwnd(), &tviAux); | |
8ecd5de2 | 2186 | |
b8e3f1cf VZ |
2187 | tv.itemNew.state = tviAux.state; |
2188 | tv.itemNew.lParam = tviAux.lParam; | |
2189 | ||
2190 | tv.ptDrag.x = x; | |
2191 | tv.ptDrag.y = y; | |
2192 | ||
2193 | // do it before SendMessage() call below to avoid | |
2194 | // reentrancies here if there is another WM_MOUSEMOVE | |
2195 | // in the queue already | |
e3ad5702 | 2196 | m_htClickedItem.Unset(); |
b8e3f1cf VZ |
2197 | |
2198 | ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY, | |
2199 | tv.hdr.idFrom, (LPARAM)&tv ); | |
2200 | ||
2201 | // don't pass it to the default window proc, it would | |
2202 | // start dragging again | |
2203 | processed = true; | |
e3ad5702 | 2204 | } |
e3ad5702 | 2205 | } |
68fc69f3 | 2206 | #endif // __WXWINCE__ |
8ecd5de2 | 2207 | |
68d9be05 | 2208 | #if wxUSE_DRAGIMAGE |
3f7bc32b VZ |
2209 | if ( m_dragImage ) |
2210 | { | |
afff720b | 2211 | m_dragImage->Move(wxPoint(x, y)); |
3f7bc32b | 2212 | if ( htItem ) |
23f681ec VZ |
2213 | { |
2214 | // highlight the item as target (hiding drag image is | |
2215 | // necessary - otherwise the display will be corrupted) | |
68be9f09 | 2216 | m_dragImage->Hide(); |
3f7bc32b | 2217 | TreeView_SelectDropTarget(GetHwnd(), htItem); |
68be9f09 | 2218 | m_dragImage->Show(); |
23f681ec VZ |
2219 | } |
2220 | } | |
68d9be05 | 2221 | #endif // wxUSE_DRAGIMAGE |
23f681ec VZ |
2222 | break; |
2223 | ||
2224 | case WM_LBUTTONUP: | |
35cf1ec6 VZ |
2225 | |
2226 | // facilitates multiple drag-and-drop | |
2227 | if (htItem && isMultiple) | |
2228 | { | |
2229 | wxArrayTreeItemIds selections; | |
2230 | size_t count = GetSelections(selections); | |
2231 | ||
2232 | if (count > 1 && | |
2233 | !(wParam & MK_CONTROL) && | |
2234 | !(wParam & MK_SHIFT)) | |
2235 | { | |
2236 | UnselectAll(); | |
2237 | TreeView_SelectItem(GetHwnd(), htItem); | |
e3ad5702 JS |
2238 | ::SelectItem(GetHwnd(), htItem); |
2239 | ::SetFocus(GetHwnd(), htItem); | |
35cf1ec6 | 2240 | } |
e3ad5702 | 2241 | m_htClickedItem.Unset(); |
35cf1ec6 VZ |
2242 | } |
2243 | ||
2244 | // fall through | |
2245 | ||
23f681ec | 2246 | case WM_RBUTTONUP: |
68d9be05 | 2247 | #if wxUSE_DRAGIMAGE |
3f7bc32b | 2248 | if ( m_dragImage ) |
23f681ec | 2249 | { |
68be9f09 | 2250 | m_dragImage->EndDrag(); |
23f681ec VZ |
2251 | delete m_dragImage; |
2252 | m_dragImage = NULL; | |
2253 | ||
2254 | // generate the drag end event | |
09f277d6 | 2255 | wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, this, htItem); |
72e61024 JS |
2256 | event.m_pointDrag = wxPoint(x, y); |
2257 | ||
937013e0 | 2258 | (void)HandleWindowEvent(event); |
225fe9d6 VZ |
2259 | |
2260 | // if we don't do it, the tree seems to think that 2 items | |
2261 | // are selected simultaneously which is quite weird | |
2262 | TreeView_SelectDropTarget(GetHwnd(), 0); | |
23f681ec | 2263 | } |
68d9be05 | 2264 | #endif // wxUSE_DRAGIMAGE |
23f681ec VZ |
2265 | break; |
2266 | } | |
2267 | } | |
3f7bc32b VZ |
2268 | else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple ) |
2269 | { | |
2270 | // the tree control greys out the selected item when it loses focus and | |
2271 | // paints it as selected again when it regains it, but it won't do it | |
2272 | // for the other items itself - help it | |
2273 | wxArrayTreeItemIds selections; | |
2274 | size_t count = GetSelections(selections); | |
2275 | RECT rect; | |
2276 | for ( size_t n = 0; n < count; n++ ) | |
2277 | { | |
04cd30de | 2278 | // TreeView_GetItemRect() will return false if item is not visible, |
3f7bc32b | 2279 | // which may happen perfectly well |
14ba002a | 2280 | if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]), |
2b9a7d4c | 2281 | &rect, TRUE) ) |
3f7bc32b | 2282 | { |
2b9a7d4c | 2283 | ::InvalidateRect(GetHwnd(), &rect, FALSE); |
3f7bc32b VZ |
2284 | } |
2285 | } | |
2286 | } | |
2287 | else if ( nMsg == WM_KEYDOWN && isMultiple ) | |
2288 | { | |
2289 | bool bCtrl = wxIsCtrlDown(), | |
2290 | bShift = wxIsShiftDown(); | |
2291 | ||
2c8e4738 | 2292 | HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd()); |
ba8a1961 | 2293 | switch ( wParam ) |
3f7bc32b | 2294 | { |
ba8a1961 VZ |
2295 | case VK_SPACE: |
2296 | if ( bCtrl ) | |
2297 | { | |
cc87a04c | 2298 | ToggleItemSelection(htSel); |
ba8a1961 VZ |
2299 | } |
2300 | else | |
2301 | { | |
2302 | UnselectAll(); | |
23f681ec | 2303 | |
ba8a1961 VZ |
2304 | ::SelectItem(GetHwnd(), htSel); |
2305 | } | |
3f7bc32b | 2306 | |
ba8a1961 VZ |
2307 | processed = true; |
2308 | break; | |
3f7bc32b | 2309 | |
ba8a1961 VZ |
2310 | case VK_UP: |
2311 | case VK_DOWN: | |
2312 | if ( !bCtrl && !bShift ) | |
3f7bc32b | 2313 | { |
ba8a1961 VZ |
2314 | // no modifiers, just clear selection and then let the default |
2315 | // processing to take place | |
2316 | UnselectAll(); | |
3f7bc32b | 2317 | } |
ba8a1961 | 2318 | else if ( htSel ) |
3f7bc32b | 2319 | { |
ba8a1961 VZ |
2320 | (void)wxControl::MSWWindowProc(nMsg, wParam, lParam); |
2321 | ||
2322 | HTREEITEM htNext = (HTREEITEM) | |
2323 | TreeView_GetNextItem | |
2324 | ( | |
2325 | GetHwnd(), | |
2326 | htSel, | |
2327 | wParam == VK_UP ? TVGN_PREVIOUSVISIBLE | |
2328 | : TVGN_NEXTVISIBLE | |
2329 | ); | |
2330 | ||
2331 | if ( !htNext ) | |
2332 | { | |
2333 | // at the top/bottom | |
2334 | htNext = htSel; | |
2335 | } | |
2336 | ||
2337 | if ( bShift ) | |
2338 | { | |
2339 | if ( !m_htSelStart ) | |
2340 | m_htSelStart = htSel; | |
2341 | ||
2342 | SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext); | |
2343 | } | |
2344 | else // bCtrl | |
2345 | { | |
2346 | // without changing selection | |
2347 | ::SetFocus(GetHwnd(), htNext); | |
2348 | } | |
2349 | ||
2350 | processed = true; | |
3f7bc32b | 2351 | } |
ba8a1961 VZ |
2352 | break; |
2353 | ||
2354 | case VK_HOME: | |
2355 | case VK_END: | |
2356 | case VK_PRIOR: | |
2357 | case VK_NEXT: | |
2358 | // TODO: handle Shift/Ctrl with these keys | |
2359 | if ( !bCtrl && !bShift ) | |
3f7bc32b | 2360 | { |
ba8a1961 | 2361 | UnselectAll(); |
3f7bc32b | 2362 | |
ba8a1961 VZ |
2363 | m_htSelStart.Unset(); |
2364 | } | |
3f7bc32b VZ |
2365 | } |
2366 | } | |
d35dce3a VZ |
2367 | else if ( nMsg == WM_COMMAND ) |
2368 | { | |
2369 | // if we receive a EN_KILLFOCUS command from the in-place edit control | |
2370 | // used for label editing, make sure to end editing | |
2371 | WORD id, cmd; | |
2372 | WXHWND hwnd; | |
2373 | UnpackCommand(wParam, lParam, &id, &hwnd, &cmd); | |
2374 | ||
2375 | if ( cmd == EN_KILLFOCUS ) | |
2376 | { | |
2377 | if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd ) | |
2378 | { | |
2379 | DoEndEditLabel(); | |
2380 | ||
2381 | processed = true; | |
2382 | } | |
2383 | } | |
2384 | } | |
7bb8798c | 2385 | |
3f7bc32b VZ |
2386 | if ( !processed ) |
2387 | rc = wxControl::MSWWindowProc(nMsg, wParam, lParam); | |
2388 | ||
2389 | return rc; | |
23f681ec VZ |
2390 | } |
2391 | ||
fbd8ac52 VZ |
2392 | WXLRESULT |
2393 | wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam) | |
2394 | { | |
068b764a VZ |
2395 | if ( nMsg == WM_CHAR ) |
2396 | { | |
df63b2a4 | 2397 | // don't let the control process Space and Return keys because it |
068b764a VZ |
2398 | // doesn't do anything useful with them anyhow but always beeps |
2399 | // annoyingly when it receives them and there is no way to turn it off | |
2400 | // simply if you just process TREEITEM_ACTIVATED event to which Space | |
2401 | // and Enter presses are mapped in your code | |
2402 | if ( wParam == VK_SPACE || wParam == VK_RETURN ) | |
2403 | return 0; | |
2404 | } | |
c4cd8712 VZ |
2405 | #if wxUSE_DRAGIMAGE |
2406 | else if ( nMsg == WM_KEYDOWN ) | |
2407 | { | |
2408 | if ( wParam == VK_ESCAPE ) | |
2409 | { | |
2410 | if ( m_dragImage ) | |
2411 | { | |
2412 | m_dragImage->EndDrag(); | |
2413 | delete m_dragImage; | |
2414 | m_dragImage = NULL; | |
2415 | ||
2416 | // if we don't do it, the tree seems to think that 2 items | |
2417 | // are selected simultaneously which is quite weird | |
2418 | TreeView_SelectDropTarget(GetHwnd(), 0); | |
2419 | } | |
2420 | } | |
2421 | } | |
2422 | #endif // wxUSE_DRAGIMAGE | |
068b764a | 2423 | |
fbd8ac52 VZ |
2424 | return wxControl::MSWDefWindowProc(nMsg, wParam, lParam); |
2425 | } | |
2426 | ||
08b7c251 | 2427 | // process WM_NOTIFY Windows message |
a23fd0e1 | 2428 | bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result) |
08b7c251 | 2429 | { |
09f277d6 | 2430 | wxTreeEvent event(wxEVT_NULL, this); |
08b7c251 VZ |
2431 | wxEventType eventType = wxEVT_NULL; |
2432 | NMHDR *hdr = (NMHDR *)lParam; | |
2433 | ||
2434 | switch ( hdr->code ) | |
2bda0e17 | 2435 | { |
08b7c251 VZ |
2436 | case TVN_BEGINDRAG: |
2437 | eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG; | |
2438 | // fall through | |
2439 | ||
2440 | case TVN_BEGINRDRAG: | |
2441 | { | |
2442 | if ( eventType == wxEVT_NULL ) | |
2443 | eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG; | |
2444 | //else: left drag, already set above | |
2445 | ||
2446 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
2447 | ||
ee4b2721 | 2448 | event.m_item = tv->itemNew.hItem; |
08b7c251 | 2449 | event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y); |
23f681ec VZ |
2450 | |
2451 | // don't allow dragging by default: the user code must | |
2452 | // explicitly say that it wants to allow it to avoid breaking | |
2453 | // the old apps | |
2454 | event.Veto(); | |
08b7c251 | 2455 | } |
696e1ea0 | 2456 | break; |
08b7c251 VZ |
2457 | |
2458 | case TVN_BEGINLABELEDIT: | |
2459 | { | |
2460 | eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT; | |
2461 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2462 | ||
646a8a4d VZ |
2463 | // although the user event handler may still veto it, it is |
2464 | // important to set it now so that calls to SetItemText() from | |
2465 | // the event handler would change the text controls contents | |
2466 | m_idEdited = | |
ee4b2721 | 2467 | event.m_item = info->item.hItem; |
5ea47806 | 2468 | event.m_label = info->item.pszText; |
04cd30de | 2469 | event.m_editCancelled = false; |
08b7c251 | 2470 | } |
696e1ea0 | 2471 | break; |
08b7c251 VZ |
2472 | |
2473 | case TVN_DELETEITEM: | |
2474 | { | |
2475 | eventType = wxEVT_COMMAND_TREE_DELETE_ITEM; | |
2476 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; | |
2477 | ||
ee4b2721 | 2478 | event.m_item = tv->itemOld.hItem; |
696e1ea0 VZ |
2479 | |
2480 | if ( m_hasAnyAttr ) | |
2481 | { | |
ee4b2721 VZ |
2482 | wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem); |
2483 | if ( it != m_attrs.end() ) | |
2484 | { | |
2485 | delete it->second; | |
2486 | m_attrs.erase(it); | |
2487 | } | |
696e1ea0 | 2488 | } |
08b7c251 | 2489 | } |
696e1ea0 | 2490 | break; |
08b7c251 VZ |
2491 | |
2492 | case TVN_ENDLABELEDIT: | |
2493 | { | |
2494 | eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT; | |
2495 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2496 | ||
ee4b2721 | 2497 | event.m_item = info->item.hItem; |
5ea47806 | 2498 | event.m_label = info->item.pszText; |
06110b00 | 2499 | event.m_editCancelled = info->item.pszText == NULL; |
08b7c251 VZ |
2500 | break; |
2501 | } | |
2502 | ||
676d6550 | 2503 | #ifndef __WXWINCE__ |
156194e1 JS |
2504 | // These *must* not be removed or TVN_GETINFOTIP will |
2505 | // not be processed each time the mouse is moved | |
2506 | // and the tooltip will only ever update once. | |
2507 | case TTN_NEEDTEXTA: | |
2508 | case TTN_NEEDTEXTW: | |
2509 | { | |
2510 | *result = 0; | |
2511 | ||
2512 | break; | |
2513 | } | |
2514 | ||
f3f71703 | 2515 | #ifdef TVN_GETINFOTIP |
156194e1 JS |
2516 | case TVN_GETINFOTIP: |
2517 | { | |
2518 | eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP; | |
2519 | NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam; | |
2520 | ||
2521 | // Which item are we trying to get a tooltip for? | |
bc0aebab | 2522 | event.m_item = info->hItem; |
156194e1 JS |
2523 | |
2524 | break; | |
2525 | } | |
df63b2a4 VZ |
2526 | #endif // TVN_GETINFOTIP |
2527 | #endif // !__WXWINCE__ | |
5e7718a2 | 2528 | |
08b7c251 VZ |
2529 | case TVN_GETDISPINFO: |
2530 | eventType = wxEVT_COMMAND_TREE_GET_INFO; | |
2531 | // fall through | |
2532 | ||
2533 | case TVN_SETDISPINFO: | |
2534 | { | |
2535 | if ( eventType == wxEVT_NULL ) | |
2536 | eventType = wxEVT_COMMAND_TREE_SET_INFO; | |
2537 | //else: get, already set above | |
2538 | ||
2539 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
2540 | ||
ee4b2721 | 2541 | event.m_item = info->item.hItem; |
08b7c251 VZ |
2542 | break; |
2543 | } | |
2544 | ||
2545 | case TVN_ITEMEXPANDING: | |
08b7c251 VZ |
2546 | case TVN_ITEMEXPANDED: |
2547 | { | |
4523ebb3 | 2548 | NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam; |
08b7c251 | 2549 | |
deb1de30 | 2550 | int what; |
08b7c251 VZ |
2551 | switch ( tv->action ) |
2552 | { | |
deb1de30 VZ |
2553 | default: |
2554 | wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action); | |
2555 | // fall through | |
2556 | ||
08b7c251 | 2557 | case TVE_EXPAND: |
deb1de30 | 2558 | what = IDX_EXPAND; |
08b7c251 VZ |
2559 | break; |
2560 | ||
2561 | case TVE_COLLAPSE: | |
deb1de30 | 2562 | what = IDX_COLLAPSE; |
08b7c251 | 2563 | break; |
08b7c251 VZ |
2564 | } |
2565 | ||
2b5f62a0 VZ |
2566 | int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING |
2567 | : IDX_DONE; | |
deb1de30 VZ |
2568 | |
2569 | eventType = gs_expandEvents[what][how]; | |
08b7c251 | 2570 | |
ee4b2721 | 2571 | event.m_item = tv->itemNew.hItem; |
08b7c251 | 2572 | } |
696e1ea0 | 2573 | break; |
08b7c251 VZ |
2574 | |
2575 | case TVN_KEYDOWN: | |
2576 | { | |
2577 | eventType = wxEVT_COMMAND_TREE_KEY_DOWN; | |
2578 | TV_KEYDOWN *info = (TV_KEYDOWN *)lParam; | |
2579 | ||
1944ad76 VZ |
2580 | // fabricate the lParam and wParam parameters sufficiently |
2581 | // similar to the ones from a "real" WM_KEYDOWN so that | |
2582 | // CreateKeyEvent() works correctly | |
6719c06a | 2583 | WXLPARAM lParam = (wxIsAltDown() ? KF_ALTDOWN : 0) << 16; |
1944ad76 VZ |
2584 | |
2585 | WXWPARAM wParam = info->wVKey; | |
2586 | ||
637b7e4f | 2587 | int keyCode = wxCharCodeMSWToWX(wParam); |
1944ad76 VZ |
2588 | if ( !keyCode ) |
2589 | { | |
2590 | // wxCharCodeMSWToWX() returns 0 to indicate that this is a | |
2591 | // simple ASCII key | |
2592 | keyCode = wParam; | |
2593 | } | |
2594 | ||
8191741f | 2595 | event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, |
1944ad76 VZ |
2596 | keyCode, |
2597 | lParam, | |
2598 | wParam); | |
23fd5130 | 2599 | |
3f7bc32b | 2600 | // a separate event for Space/Return |
6719c06a | 2601 | if ( !wxIsAnyModifierDown() && |
3f7bc32b | 2602 | ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) ) |
23fd5130 | 2603 | { |
09f277d6 VZ |
2604 | wxTreeItemId item; |
2605 | if ( !HasFlag(wxTR_MULTIPLE) ) | |
2606 | item = GetSelection(); | |
23fd5130 | 2607 | |
09f277d6 VZ |
2608 | wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED, |
2609 | this, item); | |
937013e0 | 2610 | (void)HandleWindowEvent(event2); |
23fd5130 | 2611 | } |
08b7c251 | 2612 | } |
696e1ea0 | 2613 | break; |
08b7c251 | 2614 | |
5fe302ef BW |
2615 | |
2616 | // Vista's tree control has introduced some problems with our | |
2617 | // multi-selection tree. When TreeView_SelectItem() is called, | |
2618 | // the wrong items are deselected. | |
20bdddad | 2619 | |
5fe302ef BW |
2620 | // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING |
2621 | // that can be used to regulate this incorrect behavior. The | |
2622 | // following messages will allow only the unlocked item's selection | |
2623 | // state to change | |
396f85ba | 2624 | |
5fe302ef BW |
2625 | case TVN_ITEMCHANGINGA: |
2626 | case TVN_ITEMCHANGINGW: | |
2627 | { | |
2628 | // we only need to handles these in multi-select trees | |
2629 | if ( HasFlag(wxTR_MULTIPLE) ) | |
2630 | { | |
2631 | // get info about the item about to be changed | |
2632 | NMTVITEMCHANGE* info = (NMTVITEMCHANGE*)lParam; | |
396f85ba | 2633 | if (TreeItemUnlocker::IsLocked(info->hItem)) |
5fe302ef BW |
2634 | { |
2635 | // item's state is locked, don't allow the change | |
2636 | // returning 1 will disallow the change | |
20bdddad | 2637 | *result = 1; |
5fe302ef BW |
2638 | return true; |
2639 | } | |
2640 | } | |
2641 | ||
2642 | // allow the state change | |
2643 | } | |
2644 | return false; | |
2645 | ||
5e7718a2 | 2646 | // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of |
69a8b5b4 VS |
2647 | // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore |
2648 | // we have to handle both messages: | |
2649 | case TVN_SELCHANGEDA: | |
2650 | case TVN_SELCHANGEDW: | |
08b7c251 VZ |
2651 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGED; |
2652 | // fall through | |
2653 | ||
69a8b5b4 VS |
2654 | case TVN_SELCHANGINGA: |
2655 | case TVN_SELCHANGINGW: | |
08b7c251 VZ |
2656 | { |
2657 | if ( eventType == wxEVT_NULL ) | |
2658 | eventType = wxEVT_COMMAND_TREE_SEL_CHANGING; | |
2659 | //else: already set above | |
2660 | ||
5e7718a2 | 2661 | if (hdr->code == TVN_SELCHANGINGW || |
69a8b5b4 VS |
2662 | hdr->code == TVN_SELCHANGEDW) |
2663 | { | |
4523ebb3 | 2664 | NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam; |
ee4b2721 VZ |
2665 | event.m_item = tv->itemNew.hItem; |
2666 | event.m_itemOld = tv->itemOld.hItem; | |
69a8b5b4 VS |
2667 | } |
2668 | else | |
2669 | { | |
4523ebb3 | 2670 | NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam; |
ee4b2721 VZ |
2671 | event.m_item = tv->itemNew.hItem; |
2672 | event.m_itemOld = tv->itemOld.hItem; | |
69a8b5b4 | 2673 | } |
08b7c251 | 2674 | } |
cb776b66 VZ |
2675 | |
2676 | // we receive this message from WM_LBUTTONDOWN handler inside | |
2677 | // comctl32.dll and so before the click is passed to | |
2678 | // DefWindowProc() which sets the focus to the window which was | |
2679 | // clicked and this can lead to unexpected event sequences: for | |
2680 | // example, we may get a "selection change" event from the tree | |
2681 | // before getting a "kill focus" event for the text control which | |
2682 | // had the focus previously, thus breaking user code doing input | |
2683 | // validation | |
2684 | // | |
2685 | // to avoid such surprises, we force the generation of focus events | |
2686 | // now, before we generate the selection change ones | |
2687 | SetFocus(); | |
696e1ea0 VZ |
2688 | break; |
2689 | ||
5b59df83 VZ |
2690 | // instead of explicitly checking for _WIN32_IE, check if the |
2691 | // required symbols are available in the headers | |
2692 | #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY | |
696e1ea0 VZ |
2693 | case NM_CUSTOMDRAW: |
2694 | { | |
2695 | LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam; | |
2696 | NMCUSTOMDRAW& nmcd = lptvcd->nmcd; | |
d7926e0b | 2697 | switch ( nmcd.dwDrawStage ) |
696e1ea0 VZ |
2698 | { |
2699 | case CDDS_PREPAINT: | |
2700 | // if we've got any items with non standard attributes, | |
2701 | // notify us before painting each item | |
2702 | *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW | |
2703 | : CDRF_DODEFAULT; | |
4754ab16 RR |
2704 | |
2705 | // windows in TreeCtrl use one-based index for item state images, | |
2706 | // 0 indexed image is not being used, we're using zero-based index, | |
2707 | // so we have to add temp image (of zero index) to state image list | |
2708 | // before we draw any item, then after items are drawn we have to | |
2709 | // delete it (in POSTPAINT notify) | |
2710 | if (m_imageListState && m_imageListState->GetImageCount() > 0) | |
2711 | { | |
2712 | #define hImageList (HIMAGELIST)m_imageListState->GetHIMAGELIST() | |
2713 | ||
2714 | // add temporary image | |
2715 | int width, height; | |
2716 | m_imageListState->GetSize(0, width, height); | |
2717 | ||
2718 | HBITMAP hbmpTemp = ::CreateBitmap(width, height, 1, 1, NULL); | |
2719 | int index = ::ImageList_Add(hImageList, hbmpTemp, hbmpTemp); | |
2720 | ::DeleteObject(hbmpTemp); | |
2721 | ||
2722 | if ( index != -1 ) | |
2723 | { | |
2724 | // move images to right | |
2725 | for ( int i = index; i > 0; i-- ) | |
2726 | ::ImageList_Copy(hImageList, i, hImageList, i-1, 0); | |
2727 | ||
2728 | // we must remove the image in POSTPAINT notify | |
2729 | *result |= CDRF_NOTIFYPOSTPAINT; | |
2730 | } | |
2731 | ||
2732 | #undef hImageList | |
2733 | } | |
2734 | break; | |
2735 | ||
2736 | case CDDS_POSTPAINT: | |
2737 | // we are deleting temp image of 0 index, which was | |
2738 | // added before items were drawn (in PREPAINT notify) | |
2739 | if (m_imageListState && m_imageListState->GetImageCount() > 0) | |
2740 | m_imageListState->Remove(0); | |
d7926e0b | 2741 | break; |
696e1ea0 VZ |
2742 | |
2743 | case CDDS_ITEMPREPAINT: | |
2744 | { | |
ee4b2721 VZ |
2745 | wxMapTreeAttr::iterator |
2746 | it = m_attrs.find((void *)nmcd.dwItemSpec); | |
696e1ea0 | 2747 | |
ee4b2721 | 2748 | if ( it == m_attrs.end() ) |
696e1ea0 VZ |
2749 | { |
2750 | // nothing to do for this item | |
d7926e0b VZ |
2751 | *result = CDRF_DODEFAULT; |
2752 | break; | |
696e1ea0 VZ |
2753 | } |
2754 | ||
ee4b2721 VZ |
2755 | wxTreeItemAttr * const attr = it->second; |
2756 | ||
8b713759 VZ |
2757 | wxTreeViewItem tvItem((void *)nmcd.dwItemSpec, |
2758 | TVIF_STATE, TVIS_DROPHILITED); | |
2759 | DoGetItem(&tvItem); | |
2760 | const UINT tvItemState = tvItem.state; | |
2761 | ||
19da49fc | 2762 | // selection colours should override ours, |
8b713759 VZ |
2763 | // otherwise it is too confusing to the user |
2764 | if ( !(nmcd.uItemState & CDIS_SELECTED) && | |
2765 | !(tvItemState & TVIS_DROPHILITED) ) | |
696e1ea0 | 2766 | { |
2076893b | 2767 | wxColour colBack; |
696e1ea0 VZ |
2768 | if ( attr->HasBackgroundColour() ) |
2769 | { | |
2770 | colBack = attr->GetBackgroundColour(); | |
19da49fc | 2771 | lptvcd->clrTextBk = wxColourToRGB(colBack); |
696e1ea0 | 2772 | } |
19da49fc VZ |
2773 | } |
2774 | ||
2775 | // but we still want to keep the special foreground | |
2776 | // colour when we don't have focus (we can't keep | |
2777 | // it when we do, it would usually be unreadable on | |
2778 | // the almost inverted bg colour...) | |
8b713759 VZ |
2779 | if ( ( !(nmcd.uItemState & CDIS_SELECTED) || |
2780 | FindFocus() != this ) && | |
2781 | !(tvItemState & TVIS_DROPHILITED) ) | |
19da49fc VZ |
2782 | { |
2783 | wxColour colText; | |
2784 | if ( attr->HasTextColour() ) | |
696e1ea0 | 2785 | { |
19da49fc VZ |
2786 | colText = attr->GetTextColour(); |
2787 | lptvcd->clrText = wxColourToRGB(colText); | |
696e1ea0 | 2788 | } |
696e1ea0 VZ |
2789 | } |
2790 | ||
19da49fc | 2791 | if ( attr->HasFont() ) |
696e1ea0 | 2792 | { |
19da49fc VZ |
2793 | HFONT hFont = GetHfontOf(attr->GetFont()); |
2794 | ||
696e1ea0 VZ |
2795 | ::SelectObject(nmcd.hdc, hFont); |
2796 | ||
2797 | *result = CDRF_NEWFONT; | |
2798 | } | |
19da49fc | 2799 | else // no specific font |
696e1ea0 VZ |
2800 | { |
2801 | *result = CDRF_DODEFAULT; | |
2802 | } | |
696e1ea0 | 2803 | } |
d7926e0b | 2804 | break; |
696e1ea0 VZ |
2805 | |
2806 | default: | |
2807 | *result = CDRF_DODEFAULT; | |
696e1ea0 VZ |
2808 | } |
2809 | } | |
d7926e0b VZ |
2810 | |
2811 | // we always process it | |
04cd30de | 2812 | return true; |
5b59df83 | 2813 | #endif // have owner drawn support in headers |
08b7c251 | 2814 | |
8a000b6b VZ |
2815 | case NM_CLICK: |
2816 | { | |
2817 | DWORD pos = GetMessagePos(); | |
2818 | POINT point; | |
2819 | point.x = LOWORD(pos); | |
2820 | point.y = HIWORD(pos); | |
2821 | ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1); | |
2822 | int flags = 0; | |
2823 | wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags); | |
2824 | if (flags & wxTREE_HITTEST_ONITEMSTATEICON) | |
2825 | { | |
2826 | event.m_item = item; | |
2827 | eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK; | |
2828 | } | |
2829 | break; | |
2830 | } | |
2831 | ||
f6bcfd97 BP |
2832 | case NM_DBLCLK: |
2833 | case NM_RCLICK: | |
2834 | { | |
2835 | TV_HITTESTINFO tvhti; | |
2836 | ::GetCursorPos(&tvhti.pt); | |
2837 | ::ScreenToClient(GetHwnd(), &tvhti.pt); | |
2838 | if ( TreeView_HitTest(GetHwnd(), &tvhti) ) | |
2839 | { | |
2840 | if ( tvhti.flags & TVHT_ONITEM ) | |
2841 | { | |
ee4b2721 | 2842 | event.m_item = tvhti.hItem; |
f6bcfd97 BP |
2843 | eventType = (int)hdr->code == NM_DBLCLK |
2844 | ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED | |
2845 | : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK; | |
8591268f VZ |
2846 | |
2847 | event.m_pointDrag.x = tvhti.pt.x; | |
2848 | event.m_pointDrag.y = tvhti.pt.y; | |
f6bcfd97 BP |
2849 | } |
2850 | ||
2851 | break; | |
2852 | } | |
2853 | } | |
2854 | // fall through | |
2855 | ||
08b7c251 | 2856 | default: |
a23fd0e1 | 2857 | return wxControl::MSWOnNotify(idCtrl, lParam, result); |
2bda0e17 | 2858 | } |
08b7c251 | 2859 | |
08b7c251 VZ |
2860 | event.SetEventType(eventType); |
2861 | ||
09f277d6 VZ |
2862 | if ( event.m_item.IsOk() ) |
2863 | event.SetClientObject(GetItemData(event.m_item)); | |
2864 | ||
937013e0 | 2865 | bool processed = HandleWindowEvent(event); |
08b7c251 VZ |
2866 | |
2867 | // post processing | |
5ea47806 | 2868 | switch ( hdr->code ) |
2bda0e17 | 2869 | { |
f6bcfd97 | 2870 | case NM_DBLCLK: |
0e2ad323 VZ |
2871 | // we translate NM_DBLCLK into ACTIVATED event and if the user |
2872 | // handled the activation of the item we shouldn't proceed with | |
2873 | // also using the same double click for toggling the item expanded | |
2874 | // state -- but OTOH do let the user to expand/collapse the item by | |
2875 | // double clicking on it if the activation is not handled specially | |
2876 | *result = processed; | |
f6bcfd97 BP |
2877 | break; |
2878 | ||
df63b2a4 VZ |
2879 | case NM_RCLICK: |
2880 | // prevent tree control from sending WM_CONTEXTMENU to our parent | |
2881 | // (which it does if NM_RCLICK is not handled) because we want to | |
2882 | // send it to the control itself | |
2883 | *result = | |
2884 | processed = true; | |
2885 | ||
2886 | ::SendMessage(GetHwnd(), WM_CONTEXTMENU, | |
2887 | (WPARAM)GetHwnd(), ::GetMessagePos()); | |
2888 | break; | |
2889 | ||
23f681ec VZ |
2890 | case TVN_BEGINDRAG: |
2891 | case TVN_BEGINRDRAG: | |
68d9be05 | 2892 | #if wxUSE_DRAGIMAGE |
23f681ec VZ |
2893 | if ( event.IsAllowed() ) |
2894 | { | |
2895 | // normally this is impossible because the m_dragImage is | |
2896 | // deleted once the drag operation is over | |
2897 | wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") ); | |
2898 | ||
2899 | m_dragImage = new wxDragImage(*this, event.m_item); | |
c47addef | 2900 | m_dragImage->BeginDrag(wxPoint(0,0), this); |
68be9f09 | 2901 | m_dragImage->Show(); |
23f681ec | 2902 | } |
68d9be05 | 2903 | #endif // wxUSE_DRAGIMAGE |
23f681ec VZ |
2904 | break; |
2905 | ||
5ea47806 VZ |
2906 | case TVN_DELETEITEM: |
2907 | { | |
77ffb593 | 2908 | // NB: we might process this message using wxWidgets event |
5ea47806 VZ |
2909 | // tables, but due to overhead of wxWin event system we |
2910 | // prefer to do it here ourself (otherwise deleting a tree | |
2911 | // with many items is just too slow) | |
4523ebb3 | 2912 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
4523ebb3 VZ |
2913 | |
2914 | wxTreeItemParam *param = | |
2915 | (wxTreeItemParam *)tv->itemOld.lParam; | |
2916 | delete param; | |
08b7c251 | 2917 | |
04cd30de | 2918 | processed = true; // Make sure we don't get called twice |
5ea47806 VZ |
2919 | } |
2920 | break; | |
2921 | ||
2922 | case TVN_BEGINLABELEDIT: | |
04cd30de | 2923 | // return true to cancel label editing |
5ea47806 | 2924 | *result = !event.IsAllowed(); |
646a8a4d | 2925 | |
cd6bd270 | 2926 | // set ES_WANTRETURN ( like we do in BeginLabelEdit ) |
646a8a4d | 2927 | if ( event.IsAllowed() ) |
cd6bd270 MB |
2928 | { |
2929 | HWND hText = TreeView_GetEditControl(GetHwnd()); | |
4523ebb3 | 2930 | if ( hText ) |
cd6bd270 | 2931 | { |
5e7718a2 | 2932 | // MBN: if m_textCtrl already has an HWND, it is a stale |
cd6bd270 MB |
2933 | // pointer from a previous edit (because the user |
2934 | // didn't modify the label before dismissing the control, | |
2935 | // and TVN_ENDLABELEDIT was not sent), so delete it | |
4523ebb3 | 2936 | if ( m_textCtrl && m_textCtrl->GetHWND() ) |
cd6bd270 | 2937 | DeleteTextCtrl(); |
4523ebb3 | 2938 | if ( !m_textCtrl ) |
cd6bd270 MB |
2939 | m_textCtrl = new wxTextCtrl(); |
2940 | m_textCtrl->SetParent(this); | |
2941 | m_textCtrl->SetHWND((WXHWND)hText); | |
2942 | m_textCtrl->SubclassWin((WXHWND)hText); | |
2943 | ||
2944 | // set wxTE_PROCESS_ENTER style for the text control to | |
2945 | // force it to process the Enter presses itself, otherwise | |
2946 | // they could be stolen from it by the dialog | |
2947 | // navigation code | |
2948 | m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle() | |
2949 | | wxTE_PROCESS_ENTER); | |
2950 | } | |
2951 | } | |
646a8a4d VZ |
2952 | else // we had set m_idEdited before |
2953 | { | |
2954 | m_idEdited.Unset(); | |
2955 | } | |
5ea47806 VZ |
2956 | break; |
2957 | ||
2958 | case TVN_ENDLABELEDIT: | |
04cd30de | 2959 | // return true to set the label to the new string: note that we |
188781db | 2960 | // also must pretend that we did process the message or it is going |
04cd30de | 2961 | // to be passed to DefWindowProc() which will happily return false |
188781db | 2962 | // cancelling the label change |
5ea47806 | 2963 | *result = event.IsAllowed(); |
04cd30de | 2964 | processed = true; |
5ea47806 VZ |
2965 | |
2966 | // ensure that we don't have the text ctrl which is going to be | |
2967 | // deleted any more | |
2968 | DeleteTextCtrl(); | |
2969 | break; | |
2970 | ||
676d6550 | 2971 | #ifndef __WXWINCE__ |
f3f71703 | 2972 | #ifdef TVN_GETINFOTIP |
156194e1 JS |
2973 | case TVN_GETINFOTIP: |
2974 | { | |
2975 | // If the user permitted a tooltip change, change it | |
2976 | if (event.IsAllowed()) | |
2977 | { | |
2978 | SetToolTip(event.m_label); | |
2979 | } | |
2980 | } | |
2981 | break; | |
f3f71703 | 2982 | #endif |
676d6550 | 2983 | #endif |
5e7718a2 | 2984 | |
5ea47806 VZ |
2985 | case TVN_SELCHANGING: |
2986 | case TVN_ITEMEXPANDING: | |
04cd30de | 2987 | // return true to prevent the action from happening |
5ea47806 VZ |
2988 | *result = !event.IsAllowed(); |
2989 | break; | |
2990 | ||
deb1de30 | 2991 | case TVN_ITEMEXPANDED: |
deb1de30 | 2992 | { |
4523ebb3 | 2993 | NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam; |
5188000b | 2994 | const wxTreeItemId id(tv->itemNew.hItem); |
deb1de30 | 2995 | |
5188000b | 2996 | if ( tv->action == TVE_COLLAPSE ) |
bf43d750 | 2997 | { |
5188000b VZ |
2998 | if ( wxApp::GetComCtl32Version() >= 600 ) |
2999 | { | |
3000 | // for some reason the item selection rectangle depends | |
3001 | // on whether it is expanded or collapsed (at least | |
3002 | // with comctl32.dll v6): it is wider (by 3 pixels) in | |
3003 | // the expanded state, so when the item collapses and | |
3004 | // then is deselected the rightmost 3 pixels of the | |
3005 | // previously drawn selection are left on the screen | |
3006 | // | |
3007 | // it's not clear if it's a bug in comctl32.dll or in | |
3008 | // our code (because it does not happen in Explorer but | |
3009 | // OTOH we don't do anything which could result in this | |
3010 | // AFAICS) but we do need to work around it to avoid | |
3011 | // ugly artifacts | |
3012 | RefreshItem(id); | |
3013 | } | |
3014 | } | |
3015 | else // expand | |
3016 | { | |
3017 | // the item is also not refreshed properly after expansion when | |
3018 | // it has an image depending on the expanded/collapsed state: | |
3019 | // again, it's not clear if the bug is in comctl32.dll or our | |
3020 | // code... | |
3021 | int image = GetItemImage(id, wxTreeItemIcon_Expanded); | |
3022 | if ( image != -1 ) | |
3023 | { | |
3024 | RefreshItem(id); | |
3025 | } | |
deb1de30 VZ |
3026 | } |
3027 | } | |
3028 | break; | |
3029 | ||
74b31181 VZ |
3030 | case TVN_GETDISPINFO: |
3031 | // NB: so far the user can't set the image himself anyhow, so do it | |
3032 | // anyway - but this may change later | |
4523ebb3 | 3033 | //if ( /* !processed && */ ) |
74b31181 VZ |
3034 | { |
3035 | wxTreeItemId item = event.m_item; | |
3036 | TV_DISPINFO *info = (TV_DISPINFO *)lParam; | |
4523ebb3 | 3037 | |
1b182562 | 3038 | const wxTreeItemParam * const param = GetItemParam(item); |
4523ebb3 VZ |
3039 | if ( !param ) |
3040 | break; | |
3041 | ||
74b31181 VZ |
3042 | if ( info->item.mask & TVIF_IMAGE ) |
3043 | { | |
3044 | info->item.iImage = | |
4523ebb3 | 3045 | param->GetImage |
74b31181 | 3046 | ( |
74b31181 VZ |
3047 | IsExpanded(item) ? wxTreeItemIcon_Expanded |
3048 | : wxTreeItemIcon_Normal | |
3049 | ); | |
3050 | } | |
3051 | if ( info->item.mask & TVIF_SELECTEDIMAGE ) | |
3052 | { | |
3053 | info->item.iSelectedImage = | |
4523ebb3 | 3054 | param->GetImage |
74b31181 | 3055 | ( |
74b31181 VZ |
3056 | IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded |
3057 | : wxTreeItemIcon_Selected | |
3058 | ); | |
3059 | } | |
deb1de30 | 3060 | } |
74b31181 VZ |
3061 | break; |
3062 | ||
5ea47806 VZ |
3063 | //default: |
3064 | // for the other messages the return value is ignored and there is | |
3065 | // nothing special to do | |
3066 | } | |
fd3f686c | 3067 | return processed; |
2bda0e17 KB |
3068 | } |
3069 | ||
8a000b6b VZ |
3070 | // ---------------------------------------------------------------------------- |
3071 | // State control. | |
3072 | // ---------------------------------------------------------------------------- | |
3073 | ||
3074 | // why do they define INDEXTOSTATEIMAGEMASK but not the inverse? | |
3075 | #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12) | |
3076 | ||
03966fcb | 3077 | int wxTreeCtrl::DoGetItemState(const wxTreeItemId& item) const |
8a000b6b | 3078 | { |
03966fcb | 3079 | wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") ); |
8a000b6b | 3080 | |
03966fcb RR |
3081 | // receive the desired information |
3082 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
3083 | DoGetItem(&tvItem); | |
8a000b6b | 3084 | |
03966fcb RR |
3085 | // state images are one-based |
3086 | return STATEIMAGEMASKTOINDEX(tvItem.state) - 1; | |
8a000b6b VZ |
3087 | } |
3088 | ||
03966fcb | 3089 | void wxTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state) |
8a000b6b | 3090 | { |
03966fcb RR |
3091 | wxCHECK_RET( item.IsOk(), wxT("invalid tree item") ); |
3092 | ||
3093 | wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK); | |
8a000b6b | 3094 | |
03966fcb RR |
3095 | // state images are one-based |
3096 | // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1) | |
3097 | tvItem.state = INDEXTOSTATEIMAGEMASK(state + 1); | |
3098 | ||
3099 | DoSetItem(&tvItem); | |
8a000b6b VZ |
3100 | } |
3101 | ||
1e6feb95 | 3102 | #endif // wxUSE_TREECTRL |