Fix choice of tree item icon for the selected state in wxMSW.
[wxWidgets.git] / src / msw / treectrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
3 // Purpose: wxTreeCtrl
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
6 // Created: 1997
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TREECTRL
28
29 #include "wx/treectrl.h"
30
31 #ifndef WX_PRECOMP
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
35 #include "wx/log.h"
36 #include "wx/app.h"
37 #include "wx/settings.h"
38 #endif
39
40 #include "wx/dynlib.h"
41 #include "wx/msw/private.h"
42
43 #include "wx/imaglist.h"
44 #include "wx/msw/dragimag.h"
45 #include "wx/msw/uxtheme.h"
46
47 // macros to hide the cast ugliness
48 // --------------------------------
49
50 // get HTREEITEM from wxTreeItemId
51 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
52
53
54 // older SDKs are missing these
55 #ifndef TVN_ITEMCHANGINGA
56
57 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
58 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
59
60 typedef struct tagNMTVITEMCHANGE
61 {
62 NMHDR hdr;
63 UINT uChanged;
64 HTREEITEM hItem;
65 UINT uStateNew;
66 UINT uStateOld;
67 LPARAM lParam;
68 } NMTVITEMCHANGE;
69
70 #endif
71
72
73 // this helper class is used on vista systems for preventing unwanted
74 // item state changes in the vista tree control. It is only effective in
75 // multi-select mode on vista systems.
76
77 // The vista tree control includes some new code that originally broke the
78 // multi-selection tree, causing seemingly spurious item selection state changes
79 // during Shift or Ctrl-click item selection. (To witness the original broken
80 // behaviour, simply make IsLocked() below always return false). This problem was
81 // solved by using the following class to 'unlock' an item's selection state.
82
83 class TreeItemUnlocker
84 {
85 public:
86 // unlock a single item
87 TreeItemUnlocker(HTREEITEM item) { ms_unlockedItem = item; }
88
89 // unlock all items, don't use unless absolutely necessary
90 TreeItemUnlocker() { ms_unlockedItem = (HTREEITEM)-1; }
91
92 // lock everything back
93 ~TreeItemUnlocker() { ms_unlockedItem = NULL; }
94
95
96 // check if the item state is currently locked
97 static bool IsLocked(HTREEITEM item)
98 { return ms_unlockedItem != (HTREEITEM)-1 && item != ms_unlockedItem; }
99
100 private:
101 static HTREEITEM ms_unlockedItem;
102 };
103
104 HTREEITEM TreeItemUnlocker::ms_unlockedItem = NULL;
105
106 // another helper class: set the variable to true during its lifetime and reset
107 // it to false when it is destroyed
108 //
109 // it is currently always used with wxTreeCtrl::m_changingSelection
110 class TempSetter
111 {
112 public:
113 TempSetter(bool& var) : m_var(var)
114 {
115 wxASSERT_MSG( !m_var, "variable shouldn't be already set" );
116 m_var = true;
117 }
118
119 ~TempSetter()
120 {
121 m_var = false;
122 }
123
124 private:
125 bool& m_var;
126
127 wxDECLARE_NO_COPY_CLASS(TempSetter);
128 };
129
130 // ----------------------------------------------------------------------------
131 // private functions
132 // ----------------------------------------------------------------------------
133
134 namespace
135 {
136
137 // Work around a problem with TreeView_GetItemRect() when using MinGW/Cygwin:
138 // it results in warnings about breaking strict aliasing rules because HITEM is
139 // passed via a RECT pointer, so use a union to avoid them and define our own
140 // version of the standard macro using it.
141 union TVGetItemRectParam
142 {
143 RECT rect;
144 HTREEITEM hItem;
145 };
146
147 inline bool
148 wxTreeView_GetItemRect(HWND hwnd,
149 HTREEITEM hItem,
150 TVGetItemRectParam& param,
151 BOOL fItemRect)
152 {
153 param.hItem = hItem;
154 return ::SendMessage(hwnd, TVM_GETITEMRECT, fItemRect,
155 (LPARAM)&param) == TRUE;
156 }
157
158 } // anonymous namespace
159
160 // wrappers for TreeView_GetItem/TreeView_SetItem
161 static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem)
162 {
163 TV_ITEM tvi;
164 tvi.mask = TVIF_STATE | TVIF_HANDLE;
165 tvi.stateMask = TVIS_SELECTED;
166 tvi.hItem = hItem;
167
168 TreeItemUnlocker unlocker(hItem);
169
170 if ( !TreeView_GetItem(hwndTV, &tvi) )
171 {
172 wxLogLastError(wxT("TreeView_GetItem"));
173 }
174
175 return (tvi.state & TVIS_SELECTED) != 0;
176 }
177
178 static bool SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true)
179 {
180 TV_ITEM tvi;
181 tvi.mask = TVIF_STATE | TVIF_HANDLE;
182 tvi.stateMask = TVIS_SELECTED;
183 tvi.state = select ? TVIS_SELECTED : 0;
184 tvi.hItem = hItem;
185
186 TreeItemUnlocker unlocker(hItem);
187
188 if ( TreeView_SetItem(hwndTV, &tvi) == -1 )
189 {
190 wxLogLastError(wxT("TreeView_SetItem"));
191 return false;
192 }
193
194 return true;
195 }
196
197 static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem)
198 {
199 SelectItem(hwndTV, htItem, false);
200 }
201
202 static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem)
203 {
204 SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem));
205 }
206
207 // helper function which selects all items in a range and, optionally,
208 // deselects all the other ones
209 //
210 // returns true if the selection changed at all or false if nothing changed
211
212 // flags for SelectRange()
213 enum
214 {
215 SR_SIMULATE = 1, // don't do anything, just return true or false
216 SR_UNSELECT_OTHERS = 2 // deselect the items not in range
217 };
218
219 static bool SelectRange(HWND hwndTV,
220 HTREEITEM htFirst,
221 HTREEITEM htLast,
222 int flags)
223 {
224 // find the first (or last) item and select it
225 bool changed = false;
226 bool cont = true;
227 HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV);
228
229 while ( htItem && cont )
230 {
231 if ( (htItem == htFirst) || (htItem == htLast) )
232 {
233 if ( !IsItemSelected(hwndTV, htItem) )
234 {
235 if ( !(flags & SR_SIMULATE) )
236 {
237 SelectItem(hwndTV, htItem);
238 }
239
240 changed = true;
241 }
242
243 cont = false;
244 }
245 else // not first or last
246 {
247 if ( flags & SR_UNSELECT_OTHERS )
248 {
249 if ( IsItemSelected(hwndTV, htItem) )
250 {
251 if ( !(flags & SR_SIMULATE) )
252 UnselectItem(hwndTV, htItem);
253
254 changed = true;
255 }
256 }
257 }
258
259 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
260 }
261
262 // select the items in range
263 cont = htFirst != htLast;
264 while ( htItem && cont )
265 {
266 if ( !IsItemSelected(hwndTV, htItem) )
267 {
268 if ( !(flags & SR_SIMULATE) )
269 {
270 SelectItem(hwndTV, htItem);
271 }
272
273 changed = true;
274 }
275
276 cont = (htItem != htFirst) && (htItem != htLast);
277
278 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
279 }
280
281 // optionally deselect the rest
282 if ( flags & SR_UNSELECT_OTHERS )
283 {
284 while ( htItem )
285 {
286 if ( IsItemSelected(hwndTV, htItem) )
287 {
288 if ( !(flags & SR_SIMULATE) )
289 {
290 UnselectItem(hwndTV, htItem);
291 }
292
293 changed = true;
294 }
295
296 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
297 }
298 }
299
300 // seems to be necessary - otherwise the just selected items don't always
301 // appear as selected
302 if ( !(flags & SR_SIMULATE) )
303 {
304 UpdateWindow(hwndTV);
305 }
306
307 return changed;
308 }
309
310 // helper function which tricks the standard control into changing the focused
311 // item without changing anything else (if someone knows why Microsoft doesn't
312 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
313 //
314 // returns true if the focus was changed, false if the given item was already
315 // the focused one
316 static bool SetFocus(HWND hwndTV, HTREEITEM htItem)
317 {
318 // the current focus
319 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV);
320
321 if ( htItem == htFocus )
322 return false;
323
324 if ( htItem )
325 {
326 // remember the selection state of the item
327 bool wasSelected = IsItemSelected(hwndTV, htItem);
328
329 if ( htFocus && IsItemSelected(hwndTV, htFocus) )
330 {
331 // prevent the tree from unselecting the old focus which it
332 // would do by default (TreeView_SelectItem unselects the
333 // focused item)
334 TreeView_SelectItem(hwndTV, 0);
335 SelectItem(hwndTV, htFocus);
336 }
337
338 TreeView_SelectItem(hwndTV, htItem);
339
340 if ( !wasSelected )
341 {
342 // need to clear the selection which TreeView_SelectItem() gave
343 // us
344 UnselectItem(hwndTV, htItem);
345 }
346 //else: was selected, still selected - ok
347 }
348 else // reset focus
349 {
350 bool wasFocusSelected = IsItemSelected(hwndTV, htFocus);
351
352 // just clear the focus
353 TreeView_SelectItem(hwndTV, 0);
354
355 if ( wasFocusSelected )
356 {
357 // restore the selection state
358 SelectItem(hwndTV, htFocus);
359 }
360 }
361
362 return true;
363 }
364
365 // ----------------------------------------------------------------------------
366 // private classes
367 // ----------------------------------------------------------------------------
368
369 // a convenient wrapper around TV_ITEM struct which adds a ctor
370 #ifdef __VISUALC__
371 #pragma warning( disable : 4097 ) // inheriting from typedef
372 #endif
373
374 struct wxTreeViewItem : public TV_ITEM
375 {
376 wxTreeViewItem(const wxTreeItemId& item, // the item handle
377 UINT mask_, // fields which are valid
378 UINT stateMask_ = 0) // for TVIF_STATE only
379 {
380 wxZeroMemory(*this);
381
382 // hItem member is always valid
383 mask = mask_ | TVIF_HANDLE;
384 stateMask = stateMask_;
385 hItem = HITEM(item);
386 }
387 };
388
389 // ----------------------------------------------------------------------------
390 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
391 //
392 // We need this for a couple of reasons:
393 //
394 // 1) This class is needed for support of different images: the Win32 common
395 // control natively supports only 2 images (the normal one and another for the
396 // selected state). We wish to provide support for 2 more of them for folder
397 // items (i.e. those which have children): for expanded state and for expanded
398 // selected state. For this we use this structure to store the additional items
399 // images.
400 //
401 // 2) This class is also needed to hold the HITEM so that we can sort
402 // it correctly in the MSW sort callback.
403 //
404 // In addition it makes other workarounds such as this easier and helps
405 // simplify the code.
406 // ----------------------------------------------------------------------------
407
408 class wxTreeItemParam
409 {
410 public:
411 wxTreeItemParam()
412 {
413 m_data = NULL;
414
415 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
416 {
417 m_images[n] = -1;
418 }
419 }
420
421 // dtor deletes the associated data as well
422 virtual ~wxTreeItemParam() { delete m_data; }
423
424 // accessors
425 // get the real data associated with the item
426 wxTreeItemData *GetData() const { return m_data; }
427 // change it
428 void SetData(wxTreeItemData *data) { m_data = data; }
429
430 // do we have such image?
431 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
432 // get image, falling back to the other images if this one is not
433 // specified
434 int GetImage(wxTreeItemIcon which) const
435 {
436 int image = m_images[which];
437 if ( image == -1 )
438 {
439 switch ( which )
440 {
441 case wxTreeItemIcon_SelectedExpanded:
442 // We consider that expanded icon is more important than
443 // selected so test for it first.
444 image = m_images[wxTreeItemIcon_Expanded];
445 if ( image == -1 )
446 image = m_images[wxTreeItemIcon_Selected];
447 if ( image != -1 )
448 break;
449 //else: fall through
450
451 case wxTreeItemIcon_Selected:
452 case wxTreeItemIcon_Expanded:
453 image = m_images[wxTreeItemIcon_Normal];
454 break;
455
456 case wxTreeItemIcon_Normal:
457 // no fallback
458 break;
459
460 default:
461 wxFAIL_MSG( wxT("unsupported wxTreeItemIcon value") );
462 }
463 }
464
465 return image;
466 }
467 // change the given image
468 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
469
470 // get item
471 const wxTreeItemId& GetItem() const { return m_item; }
472 // set item
473 void SetItem(const wxTreeItemId& item) { m_item = item; }
474
475 protected:
476 // all the images associated with the item
477 int m_images[wxTreeItemIcon_Max];
478
479 // item for sort callbacks
480 wxTreeItemId m_item;
481
482 // the real client data
483 wxTreeItemData *m_data;
484
485 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam);
486 };
487
488 // wxVirutalNode is used in place of a single root when 'hidden' root is
489 // specified.
490 class wxVirtualNode : public wxTreeViewItem
491 {
492 public:
493 wxVirtualNode(wxTreeItemParam *param)
494 : wxTreeViewItem(TVI_ROOT, 0)
495 {
496 m_param = param;
497 }
498
499 ~wxVirtualNode()
500 {
501 delete m_param;
502 }
503
504 wxTreeItemParam *GetParam() const { return m_param; }
505 void SetParam(wxTreeItemParam *param) { delete m_param; m_param = param; }
506
507 private:
508 wxTreeItemParam *m_param;
509
510 wxDECLARE_NO_COPY_CLASS(wxVirtualNode);
511 };
512
513 #ifdef __VISUALC__
514 #pragma warning( default : 4097 )
515 #endif
516
517 // a macro to get the virtual root, returns NULL if none
518 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
519
520 // returns true if the item is the virtual root
521 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
522
523 // a class which encapsulates the tree traversal logic: it vists all (unless
524 // OnVisit() returns false) items under the given one
525 class wxTreeTraversal
526 {
527 public:
528 wxTreeTraversal(const wxTreeCtrl *tree)
529 {
530 m_tree = tree;
531 }
532
533 // give it a virtual dtor: not really needed as the class is never used
534 // polymorphically and not even allocated on heap at all, but this is safer
535 // (in case it ever is) and silences the compiler warnings for now
536 virtual ~wxTreeTraversal() { }
537
538 // do traverse the tree: visit all items (recursively by default) under the
539 // given one; return true if all items were traversed or false if the
540 // traversal was aborted because OnVisit returned false
541 bool DoTraverse(const wxTreeItemId& root, bool recursively = true);
542
543 // override this function to do whatever is needed for each item, return
544 // false to stop traversing
545 virtual bool OnVisit(const wxTreeItemId& item) = 0;
546
547 protected:
548 const wxTreeCtrl *GetTree() const { return m_tree; }
549
550 private:
551 bool Traverse(const wxTreeItemId& root, bool recursively);
552
553 const wxTreeCtrl *m_tree;
554
555 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal);
556 };
557
558 // internal class for getting the selected items
559 class TraverseSelections : public wxTreeTraversal
560 {
561 public:
562 TraverseSelections(const wxTreeCtrl *tree,
563 wxArrayTreeItemIds& selections)
564 : wxTreeTraversal(tree), m_selections(selections)
565 {
566 m_selections.Empty();
567
568 if (tree->GetCount() > 0)
569 DoTraverse(tree->GetRootItem());
570 }
571
572 virtual bool OnVisit(const wxTreeItemId& item)
573 {
574 const wxTreeCtrl * const tree = GetTree();
575
576 // can't visit a virtual node.
577 if ( (tree->GetRootItem() == item) && tree->HasFlag(wxTR_HIDE_ROOT) )
578 {
579 return true;
580 }
581
582 if ( ::IsItemSelected(GetHwndOf(tree), HITEM(item)) )
583 {
584 m_selections.Add(item);
585 }
586
587 return true;
588 }
589
590 size_t GetCount() const { return m_selections.GetCount(); }
591
592 private:
593 wxArrayTreeItemIds& m_selections;
594
595 wxDECLARE_NO_COPY_CLASS(TraverseSelections);
596 };
597
598 // internal class for counting tree items
599 class TraverseCounter : public wxTreeTraversal
600 {
601 public:
602 TraverseCounter(const wxTreeCtrl *tree,
603 const wxTreeItemId& root,
604 bool recursively)
605 : wxTreeTraversal(tree)
606 {
607 m_count = 0;
608
609 DoTraverse(root, recursively);
610 }
611
612 virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item))
613 {
614 m_count++;
615
616 return true;
617 }
618
619 size_t GetCount() const { return m_count; }
620
621 private:
622 size_t m_count;
623
624 wxDECLARE_NO_COPY_CLASS(TraverseCounter);
625 };
626
627 // ----------------------------------------------------------------------------
628 // wxWin macros
629 // ----------------------------------------------------------------------------
630
631 // ----------------------------------------------------------------------------
632 // constants
633 // ----------------------------------------------------------------------------
634
635 // indices in gs_expandEvents table below
636 enum
637 {
638 IDX_COLLAPSE,
639 IDX_EXPAND,
640 IDX_WHAT_MAX
641 };
642
643 enum
644 {
645 IDX_DONE,
646 IDX_DOING,
647 IDX_HOW_MAX
648 };
649
650 // handy table for sending events - it has to be initialized during run-time
651 // now so can't be const any more
652 static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX];
653
654 /*
655 but logically it's a const table with the following entries:
656 =
657 {
658 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
659 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
660 };
661 */
662
663 // ============================================================================
664 // implementation
665 // ============================================================================
666
667 // ----------------------------------------------------------------------------
668 // tree traversal
669 // ----------------------------------------------------------------------------
670
671 bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
672 {
673 if ( !OnVisit(root) )
674 return false;
675
676 return Traverse(root, recursively);
677 }
678
679 bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
680 {
681 wxTreeItemIdValue cookie;
682 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
683 while ( child.IsOk() )
684 {
685 // depth first traversal
686 if ( recursively && !Traverse(child, true) )
687 return false;
688
689 if ( !OnVisit(child) )
690 return false;
691
692 child = m_tree->GetNextChild(root, cookie);
693 }
694
695 return true;
696 }
697
698 // ----------------------------------------------------------------------------
699 // construction and destruction
700 // ----------------------------------------------------------------------------
701
702 void wxTreeCtrl::Init()
703 {
704 m_textCtrl = NULL;
705 m_hasAnyAttr = false;
706 #if wxUSE_DRAGIMAGE
707 m_dragImage = NULL;
708 #endif
709 m_pVirtualRoot = NULL;
710 m_dragStarted = false;
711 m_focusLost = true;
712 m_changingSelection = false;
713 m_triggerStateImageClick = false;
714 m_mouseUpDeselect = false;
715
716 // initialize the global array of events now as it can't be done statically
717 // with the wxEVT_XXX values being allocated during run-time only
718 gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED;
719 gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING;
720 gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED;
721 gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING;
722 }
723
724 bool wxTreeCtrl::Create(wxWindow *parent,
725 wxWindowID id,
726 const wxPoint& pos,
727 const wxSize& size,
728 long style,
729 const wxValidator& validator,
730 const wxString& name)
731 {
732 Init();
733
734 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
735 style |= wxBORDER_SUNKEN;
736
737 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
738 return false;
739
740 WXDWORD exStyle = 0;
741 DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle);
742 wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS;
743
744 if ( !(m_windowStyle & wxTR_NO_LINES) )
745 wstyle |= TVS_HASLINES;
746 if ( m_windowStyle & wxTR_HAS_BUTTONS )
747 wstyle |= TVS_HASBUTTONS;
748
749 if ( m_windowStyle & wxTR_EDIT_LABELS )
750 wstyle |= TVS_EDITLABELS;
751
752 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
753 wstyle |= TVS_LINESATROOT;
754
755 if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT )
756 {
757 if ( wxApp::GetComCtl32Version() >= 471 )
758 wstyle |= TVS_FULLROWSELECT;
759 }
760
761 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
762 // Need so that TVN_GETINFOTIP messages will be sent
763 wstyle |= TVS_INFOTIP;
764 #endif
765
766 // Create the tree control.
767 if ( !MSWCreateControl(WC_TREEVIEW, wstyle, pos, size) )
768 return false;
769
770 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
771 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
772
773 wxSetCCUnicodeFormat(GetHwnd());
774
775 if ( m_windowStyle & wxTR_TWIST_BUTTONS )
776 {
777 // Under Vista and later Explorer uses rotating ("twist") buttons
778 // instead of the default "+/-" ones so apply its theme to the tree
779 // control to implement this style.
780 if ( wxGetWinVersion() >= wxWinVersion_Vista )
781 {
782 if ( wxUxThemeEngine *theme = wxUxThemeEngine::GetIfActive() )
783 {
784 theme->SetWindowTheme(GetHwnd(), L"EXPLORER", NULL);
785 }
786 }
787 }
788
789 return true;
790 }
791
792 wxTreeCtrl::~wxTreeCtrl()
793 {
794 // delete any attributes
795 if ( m_hasAnyAttr )
796 {
797 WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs);
798
799 // prevent TVN_DELETEITEM handler from deleting the attributes again!
800 m_hasAnyAttr = false;
801 }
802
803 DeleteTextCtrl();
804
805 // delete user data to prevent memory leaks
806 // also deletes hidden root node storage.
807 DeleteAllItems();
808 }
809
810 // ----------------------------------------------------------------------------
811 // accessors
812 // ----------------------------------------------------------------------------
813
814 /* static */ wxVisualAttributes
815 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
816 {
817 wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
818
819 // common controls have their own default font
820 attrs.font = wxGetCCDefaultFont();
821
822 return attrs;
823 }
824
825
826 // simple wrappers which add error checking in debug mode
827
828 bool wxTreeCtrl::DoGetItem(wxTreeViewItem *tvItem) const
829 {
830 wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false,
831 wxT("can't retrieve virtual root item") );
832
833 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
834 {
835 wxLogLastError(wxT("TreeView_GetItem"));
836
837 return false;
838 }
839
840 return true;
841 }
842
843 void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem)
844 {
845 TreeItemUnlocker unlocker(tvItem->hItem);
846
847 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
848 {
849 wxLogLastError(wxT("TreeView_SetItem"));
850 }
851 }
852
853 unsigned int wxTreeCtrl::GetCount() const
854 {
855 return (unsigned int)TreeView_GetCount(GetHwnd());
856 }
857
858 unsigned int wxTreeCtrl::GetIndent() const
859 {
860 return TreeView_GetIndent(GetHwnd());
861 }
862
863 void wxTreeCtrl::SetIndent(unsigned int indent)
864 {
865 TreeView_SetIndent(GetHwnd(), indent);
866 }
867
868 void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
869 {
870 // no error return
871 (void) TreeView_SetImageList(GetHwnd(),
872 imageList ? imageList->GetHIMAGELIST() : 0,
873 which);
874 }
875
876 void wxTreeCtrl::SetImageList(wxImageList *imageList)
877 {
878 if (m_ownsImageListNormal)
879 delete m_imageListNormal;
880
881 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
882 m_ownsImageListNormal = false;
883 }
884
885 void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
886 {
887 if (m_ownsImageListState) delete m_imageListState;
888 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
889 m_ownsImageListState = false;
890 }
891
892 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
893 bool recursively) const
894 {
895 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
896
897 TraverseCounter counter(this, item, recursively);
898 return counter.GetCount() - 1;
899 }
900
901 // ----------------------------------------------------------------------------
902 // control colours
903 // ----------------------------------------------------------------------------
904
905 bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
906 {
907 if ( !wxWindowBase::SetBackgroundColour(colour) )
908 return false;
909
910 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
911
912 return true;
913 }
914
915 bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
916 {
917 if ( !wxWindowBase::SetForegroundColour(colour) )
918 return false;
919
920 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
921
922 return true;
923 }
924
925 // ----------------------------------------------------------------------------
926 // Item access
927 // ----------------------------------------------------------------------------
928
929 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const
930 {
931 return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT);
932 }
933
934 wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
935 {
936 wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
937
938 wxChar buf[512]; // the size is arbitrary...
939
940 wxTreeViewItem tvItem(item, TVIF_TEXT);
941 tvItem.pszText = buf;
942 tvItem.cchTextMax = WXSIZEOF(buf);
943 if ( !DoGetItem(&tvItem) )
944 {
945 // don't return some garbage which was on stack, but an empty string
946 buf[0] = wxT('\0');
947 }
948
949 return wxString(buf);
950 }
951
952 void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
953 {
954 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
955
956 if ( IS_VIRTUAL_ROOT(item) )
957 return;
958
959 wxTreeViewItem tvItem(item, TVIF_TEXT);
960 tvItem.pszText = (wxChar *)text.wx_str(); // conversion is ok
961 DoSetItem(&tvItem);
962
963 // when setting the text of the item being edited, the text control should
964 // be updated to reflect the new text as well, otherwise calling
965 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
966 //
967 // don't use GetEditControl() here because m_textCtrl is not set yet
968 HWND hwndEdit = TreeView_GetEditControl(GetHwnd());
969 if ( hwndEdit )
970 {
971 if ( item == m_idEdited )
972 {
973 ::SetWindowText(hwndEdit, text.wx_str());
974 }
975 }
976 }
977
978 int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
979 wxTreeItemIcon which) const
980 {
981 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
982
983 if ( IsHiddenRoot(item) )
984 {
985 // no images for hidden root item
986 return -1;
987 }
988
989 wxTreeItemParam *param = GetItemParam(item);
990
991 return param && param->HasImage(which) ? param->GetImage(which) : -1;
992 }
993
994 void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
995 wxTreeItemIcon which)
996 {
997 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
998 wxCHECK_RET( which >= 0 &&
999 which < wxTreeItemIcon_Max,
1000 wxT("invalid image index"));
1001
1002
1003 if ( IsHiddenRoot(item) )
1004 {
1005 // no images for hidden root item
1006 return;
1007 }
1008
1009 wxTreeItemParam *data = GetItemParam(item);
1010 if ( !data )
1011 return;
1012
1013 data->SetImage(image, which);
1014
1015 RefreshItem(item);
1016 }
1017
1018 wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const
1019 {
1020 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
1021
1022 wxTreeViewItem tvItem(item, TVIF_PARAM);
1023
1024 // hidden root may still have data.
1025 if ( IS_VIRTUAL_ROOT(item) )
1026 {
1027 return GET_VIRTUAL_ROOT()->GetParam();
1028 }
1029
1030 // visible node.
1031 if ( !DoGetItem(&tvItem) )
1032 {
1033 return NULL;
1034 }
1035
1036 return (wxTreeItemParam *)tvItem.lParam;
1037 }
1038
1039 bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent& event) const
1040 {
1041 if ( event.m_item.IsOk() )
1042 {
1043 event.SetClientObject(GetItemData(event.m_item));
1044 }
1045
1046 return HandleWindowEvent(event);
1047 }
1048
1049 wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
1050 {
1051 wxTreeItemParam *data = GetItemParam(item);
1052
1053 return data ? data->GetData() : NULL;
1054 }
1055
1056 void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
1057 {
1058 // first, associate this piece of data with this item
1059 if ( data )
1060 {
1061 data->SetId(item);
1062 }
1063
1064 wxTreeItemParam *param = GetItemParam(item);
1065
1066 wxCHECK_RET( param, wxT("failed to change tree items data") );
1067
1068 param->SetData(data);
1069 }
1070
1071 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1072 {
1073 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1074
1075 if ( IS_VIRTUAL_ROOT(item) )
1076 return;
1077
1078 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1079 tvItem.cChildren = (int)has;
1080 DoSetItem(&tvItem);
1081 }
1082
1083 void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1084 {
1085 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1086
1087 if ( IS_VIRTUAL_ROOT(item) )
1088 return;
1089
1090 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1091 tvItem.state = bold ? TVIS_BOLD : 0;
1092 DoSetItem(&tvItem);
1093 }
1094
1095 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1096 {
1097 if ( IS_VIRTUAL_ROOT(item) )
1098 return;
1099
1100 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1101 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1102 DoSetItem(&tvItem);
1103 }
1104
1105 void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1106 {
1107 if ( IS_VIRTUAL_ROOT(item) )
1108 return;
1109
1110 wxRect rect;
1111 if ( GetBoundingRect(item, rect) )
1112 {
1113 RefreshRect(rect);
1114 }
1115 }
1116
1117 wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1118 {
1119 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1120
1121 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1122 return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
1123 }
1124
1125 wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
1126 {
1127 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
1128
1129 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1130 return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
1131 }
1132
1133 wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1134 {
1135 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
1136
1137 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
1138 return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
1139 }
1140
1141 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1142 const wxColour& col)
1143 {
1144 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1145
1146 wxTreeItemAttr *attr;
1147 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1148 if ( it == m_attrs.end() )
1149 {
1150 m_hasAnyAttr = true;
1151
1152 m_attrs[item.m_pItem] =
1153 attr = new wxTreeItemAttr;
1154 }
1155 else
1156 {
1157 attr = it->second;
1158 }
1159
1160 attr->SetTextColour(col);
1161
1162 RefreshItem(item);
1163 }
1164
1165 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1166 const wxColour& col)
1167 {
1168 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1169
1170 wxTreeItemAttr *attr;
1171 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1172 if ( it == m_attrs.end() )
1173 {
1174 m_hasAnyAttr = true;
1175
1176 m_attrs[item.m_pItem] =
1177 attr = new wxTreeItemAttr;
1178 }
1179 else // already in the hash
1180 {
1181 attr = it->second;
1182 }
1183
1184 attr->SetBackgroundColour(col);
1185
1186 RefreshItem(item);
1187 }
1188
1189 void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1190 {
1191 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1192
1193 wxTreeItemAttr *attr;
1194 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
1195 if ( it == m_attrs.end() )
1196 {
1197 m_hasAnyAttr = true;
1198
1199 m_attrs[item.m_pItem] =
1200 attr = new wxTreeItemAttr;
1201 }
1202 else // already in the hash
1203 {
1204 attr = it->second;
1205 }
1206
1207 attr->SetFont(font);
1208
1209 // Reset the item's text to ensure that the bounding rect will be adjusted
1210 // for the new font.
1211 SetItemText(item, GetItemText(item));
1212
1213 RefreshItem(item);
1214 }
1215
1216 // ----------------------------------------------------------------------------
1217 // Item status
1218 // ----------------------------------------------------------------------------
1219
1220 bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1221 {
1222 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1223
1224 if ( item == wxTreeItemId(TVI_ROOT) )
1225 {
1226 // virtual (hidden) root is never visible
1227 return false;
1228 }
1229
1230 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1231 TVGetItemRectParam param;
1232
1233 // true means to get rect for just the text, not the whole line
1234 if ( !wxTreeView_GetItemRect(GetHwnd(), HITEM(item), param, TRUE) )
1235 {
1236 // if TVM_GETITEMRECT returned false, then the item is definitely not
1237 // visible (because its parent is not expanded)
1238 return false;
1239 }
1240
1241 // however if it returned true, the item might still be outside the
1242 // currently visible part of the tree, test for it (notice that partly
1243 // visible means visible here)
1244 return param.rect.bottom > 0 && param.rect.top < GetClientSize().y;
1245 }
1246
1247 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
1248 {
1249 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1250
1251 if ( IS_VIRTUAL_ROOT(item) )
1252 {
1253 wxTreeItemIdValue cookie;
1254 return GetFirstChild(item, cookie).IsOk();
1255 }
1256
1257 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1258 DoGetItem(&tvItem);
1259
1260 return tvItem.cChildren != 0;
1261 }
1262
1263 bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
1264 {
1265 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1266
1267 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1268 DoGetItem(&tvItem);
1269
1270 return (tvItem.state & TVIS_EXPANDED) != 0;
1271 }
1272
1273 bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
1274 {
1275 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1276
1277 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1278 DoGetItem(&tvItem);
1279
1280 return (tvItem.state & TVIS_SELECTED) != 0;
1281 }
1282
1283 bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1284 {
1285 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
1286
1287 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1288 DoGetItem(&tvItem);
1289
1290 return (tvItem.state & TVIS_BOLD) != 0;
1291 }
1292
1293 // ----------------------------------------------------------------------------
1294 // navigation
1295 // ----------------------------------------------------------------------------
1296
1297 wxTreeItemId wxTreeCtrl::GetRootItem() const
1298 {
1299 // Root may be real (visible) or virtual (hidden).
1300 if ( GET_VIRTUAL_ROOT() )
1301 return TVI_ROOT;
1302
1303 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1304 }
1305
1306 wxTreeItemId wxTreeCtrl::GetSelection() const
1307 {
1308 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE), wxTreeItemId(),
1309 wxT("this only works with single selection controls") );
1310
1311 return GetFocusedItem();
1312 }
1313
1314 wxTreeItemId wxTreeCtrl::GetFocusedItem() const
1315 {
1316 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1317 }
1318
1319 wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const
1320 {
1321 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1322
1323 HTREEITEM hItem;
1324
1325 if ( IS_VIRTUAL_ROOT(item) )
1326 {
1327 // no parent for the virtual root
1328 hItem = 0;
1329 }
1330 else // normal item
1331 {
1332 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1333 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1334 {
1335 // the top level items should have the virtual root as their parent
1336 hItem = TVI_ROOT;
1337 }
1338 }
1339
1340 return wxTreeItemId(hItem);
1341 }
1342
1343 wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
1344 wxTreeItemIdValue& cookie) const
1345 {
1346 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1347
1348 // remember the last child returned in 'cookie'
1349 cookie = TreeView_GetChild(GetHwnd(), HITEM(item));
1350
1351 return wxTreeItemId(cookie);
1352 }
1353
1354 wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1355 wxTreeItemIdValue& cookie) const
1356 {
1357 wxTreeItemId fromCookie(cookie);
1358
1359 HTREEITEM hitem = HITEM(fromCookie);
1360
1361 hitem = TreeView_GetNextSibling(GetHwnd(), hitem);
1362
1363 wxTreeItemId item(hitem);
1364
1365 cookie = item.m_pItem;
1366
1367 return item;
1368 }
1369
1370 wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1371 {
1372 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1373
1374 // can this be done more efficiently?
1375 wxTreeItemIdValue cookie;
1376
1377 wxTreeItemId childLast,
1378 child = GetFirstChild(item, cookie);
1379 while ( child.IsOk() )
1380 {
1381 childLast = child;
1382 child = GetNextChild(item, cookie);
1383 }
1384
1385 return childLast;
1386 }
1387
1388 wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1389 {
1390 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1391 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
1392 }
1393
1394 wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
1395 {
1396 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1397 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
1398 }
1399
1400 wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
1401 {
1402 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1403 }
1404
1405 wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
1406 {
1407 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1408 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
1409
1410 wxTreeItemId next(TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1411 if ( next.IsOk() && !IsVisible(next) )
1412 {
1413 // Win32 considers that any non-collapsed item is visible while we want
1414 // to return only really visible items
1415 next.Unset();
1416 }
1417
1418 return next;
1419 }
1420
1421 wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1422 {
1423 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1424 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1425
1426 wxTreeItemId prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1427 if ( prev.IsOk() && !IsVisible(prev) )
1428 {
1429 // just as above, Win32 function will happily return the previous item
1430 // in the tree for the first visible item too
1431 prev.Unset();
1432 }
1433
1434 return prev;
1435 }
1436
1437 // ----------------------------------------------------------------------------
1438 // multiple selections emulation
1439 // ----------------------------------------------------------------------------
1440
1441 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1442 {
1443 TraverseSelections selector(this, selections);
1444
1445 return selector.GetCount();
1446 }
1447
1448 // ----------------------------------------------------------------------------
1449 // Usual operations
1450 // ----------------------------------------------------------------------------
1451
1452 wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent,
1453 const wxTreeItemId& hInsertAfter,
1454 const wxString& text,
1455 int image, int selectedImage,
1456 wxTreeItemData *data)
1457 {
1458 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1459 wxTreeItemId(),
1460 wxT("can't have more than one root in the tree") );
1461
1462 TV_INSERTSTRUCT tvIns;
1463 tvIns.hParent = HITEM(parent);
1464 tvIns.hInsertAfter = HITEM(hInsertAfter);
1465
1466 // this is how we insert the item as the first child: supply a NULL
1467 // hInsertAfter
1468 if ( !tvIns.hInsertAfter )
1469 {
1470 tvIns.hInsertAfter = TVI_FIRST;
1471 }
1472
1473 UINT mask = 0;
1474 if ( !text.empty() )
1475 {
1476 mask |= TVIF_TEXT;
1477 tvIns.item.pszText = (wxChar *)text.wx_str(); // cast is ok
1478 }
1479 else
1480 {
1481 tvIns.item.pszText = NULL;
1482 tvIns.item.cchTextMax = 0;
1483 }
1484
1485 // create the param which will store the other item parameters
1486 wxTreeItemParam *param = new wxTreeItemParam;
1487
1488 // we return the images on demand as they depend on whether the item is
1489 // expanded or collapsed too in our case
1490 mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
1491 tvIns.item.iImage = I_IMAGECALLBACK;
1492 tvIns.item.iSelectedImage = I_IMAGECALLBACK;
1493
1494 param->SetImage(image, wxTreeItemIcon_Normal);
1495 param->SetImage(selectedImage, wxTreeItemIcon_Selected);
1496
1497 mask |= TVIF_PARAM;
1498 tvIns.item.lParam = (LPARAM)param;
1499 tvIns.item.mask = mask;
1500
1501 // don't use the hack below for the children of hidden root: this results
1502 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1503 const bool firstChild = !IsHiddenRoot(parent) &&
1504 !TreeView_GetChild(GetHwnd(), HITEM(parent));
1505
1506 HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns);
1507 if ( id == 0 )
1508 {
1509 wxLogLastError(wxT("TreeView_InsertItem"));
1510 }
1511
1512 // apparently some Windows versions (2000 and XP are reported to do this)
1513 // sometimes don't refresh the tree after adding the first child and so we
1514 // need this to make the "[+]" appear
1515 if ( firstChild )
1516 {
1517 TVGetItemRectParam param;
1518
1519 wxTreeView_GetItemRect(GetHwnd(), HITEM(parent), param, FALSE);
1520 ::InvalidateRect(GetHwnd(), &param.rect, FALSE);
1521 }
1522
1523 // associate the application tree item with Win32 tree item handle
1524 param->SetItem(id);
1525
1526 // setup wxTreeItemData
1527 if ( data != NULL )
1528 {
1529 param->SetData(data);
1530 data->SetId(id);
1531 }
1532
1533 return wxTreeItemId(id);
1534 }
1535
1536 wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1537 int image, int selectedImage,
1538 wxTreeItemData *data)
1539 {
1540 if ( HasFlag(wxTR_HIDE_ROOT) )
1541 {
1542 wxASSERT_MSG( !m_pVirtualRoot, wxT("tree can have only a single root") );
1543
1544 // create a virtual root item, the parent for all the others
1545 wxTreeItemParam *param = new wxTreeItemParam;
1546 param->SetData(data);
1547
1548 m_pVirtualRoot = new wxVirtualNode(param);
1549
1550 return TVI_ROOT;
1551 }
1552
1553 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1554 text, image, selectedImage, data);
1555 }
1556
1557 wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1558 size_t index,
1559 const wxString& text,
1560 int image, int selectedImage,
1561 wxTreeItemData *data)
1562 {
1563 wxTreeItemId idPrev;
1564 if ( index == (size_t)-1 )
1565 {
1566 // special value: append to the end
1567 idPrev = TVI_LAST;
1568 }
1569 else // find the item from index
1570 {
1571 wxTreeItemIdValue cookie;
1572 wxTreeItemId idCur = GetFirstChild(parent, cookie);
1573 while ( index != 0 && idCur.IsOk() )
1574 {
1575 index--;
1576
1577 idPrev = idCur;
1578 idCur = GetNextChild(parent, cookie);
1579 }
1580
1581 // assert, not check: if the index is invalid, we will append the item
1582 // to the end
1583 wxASSERT_MSG( index == 0, wxT("bad index in wxTreeCtrl::InsertItem") );
1584 }
1585
1586 return DoInsertAfter(parent, idPrev, text, image, selectedImage, data);
1587 }
1588
1589 void wxTreeCtrl::Delete(const wxTreeItemId& item)
1590 {
1591 // unlock tree selections on vista, without this the
1592 // tree ctrl will eventually crash after item deletion
1593 TreeItemUnlocker unlock_all;
1594
1595 if ( HasFlag(wxTR_MULTIPLE) )
1596 {
1597 bool selected = IsSelected(item);
1598 wxTreeItemId next;
1599
1600 if ( selected )
1601 {
1602 next = TreeView_GetNextVisible(GetHwnd(), HITEM(item));
1603
1604 if ( !next.IsOk() )
1605 {
1606 next = TreeView_GetPrevVisible(GetHwnd(), HITEM(item));
1607 }
1608 }
1609
1610 {
1611 TempSetter set(m_changingSelection);
1612 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1613 {
1614 wxLogLastError(wxT("TreeView_DeleteItem"));
1615 return;
1616 }
1617 }
1618
1619 if ( !selected )
1620 {
1621 return;
1622 }
1623
1624 if ( item == m_htSelStart )
1625 m_htSelStart.Unset();
1626
1627 if ( item == m_htClickedItem )
1628 m_htClickedItem.Unset();
1629
1630 if ( next.IsOk() )
1631 {
1632 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
1633
1634 if ( IsTreeEventAllowed(changingEvent) )
1635 {
1636 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
1637 (void)HandleTreeEvent(changedEvent);
1638 }
1639 else
1640 {
1641 DoUnselectItem(next);
1642 ClearFocusedItem();
1643 }
1644 }
1645 }
1646 else
1647 {
1648 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1649 {
1650 wxLogLastError(wxT("TreeView_DeleteItem"));
1651 }
1652 }
1653 }
1654
1655 // delete all children (but don't delete the item itself)
1656 void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1657 {
1658 // unlock tree selections on vista for the duration of this call
1659 TreeItemUnlocker unlock_all;
1660
1661 wxTreeItemIdValue cookie;
1662
1663 wxArrayTreeItemIds children;
1664 wxTreeItemId child = GetFirstChild(item, cookie);
1665 while ( child.IsOk() )
1666 {
1667 children.Add(child);
1668
1669 child = GetNextChild(item, cookie);
1670 }
1671
1672 size_t nCount = children.Count();
1673 for ( size_t n = 0; n < nCount; n++ )
1674 {
1675 Delete(children[n]);
1676 }
1677 }
1678
1679 void wxTreeCtrl::DeleteAllItems()
1680 {
1681 // unlock tree selections on vista for the duration of this call
1682 TreeItemUnlocker unlock_all;
1683
1684 // invalidate all the items we store as they're going to become invalid
1685 m_htSelStart =
1686 m_htClickedItem = wxTreeItemId();
1687
1688 // delete the "virtual" root item.
1689 if ( GET_VIRTUAL_ROOT() )
1690 {
1691 delete GET_VIRTUAL_ROOT();
1692 m_pVirtualRoot = NULL;
1693 }
1694
1695 // and all the real items
1696
1697 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1698 {
1699 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1700 }
1701 }
1702
1703 void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1704 {
1705 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1706 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1707 flag == TVE_EXPAND ||
1708 flag == TVE_TOGGLE,
1709 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1710
1711 // A hidden root can be neither expanded nor collapsed.
1712 wxCHECK_RET( !IsHiddenRoot(item),
1713 wxT("Can't expand/collapse hidden root node!") );
1714
1715 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1716 // emulate them. This behaviour has changed slightly with comctl32.dll
1717 // v 4.70 - now it does send them but only the first time. To maintain
1718 // compatible behaviour and also in order to not have surprises with the
1719 // future versions, don't rely on this and still do everything ourselves.
1720 // To avoid that the messages be sent twice when the item is expanded for
1721 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1722
1723 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1724 tvItem.state = 0;
1725 DoSetItem(&tvItem);
1726
1727 if ( IsExpanded(item) )
1728 {
1729 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING,
1730 this, wxTreeItemId(item));
1731
1732 if ( !IsTreeEventAllowed(event) )
1733 return;
1734 }
1735
1736 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) )
1737 {
1738 if ( IsExpanded(item) )
1739 return;
1740
1741 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, this, item);
1742 (void)HandleTreeEvent(event);
1743 }
1744 //else: change didn't took place, so do nothing at all
1745 }
1746
1747 void wxTreeCtrl::Expand(const wxTreeItemId& item)
1748 {
1749 DoExpand(item, TVE_EXPAND);
1750 }
1751
1752 void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1753 {
1754 DoExpand(item, TVE_COLLAPSE);
1755 }
1756
1757 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1758 {
1759 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1760 }
1761
1762 void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1763 {
1764 DoExpand(item, TVE_TOGGLE);
1765 }
1766
1767 void wxTreeCtrl::Unselect()
1768 {
1769 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE),
1770 wxT("doesn't make sense, may be you want UnselectAll()?") );
1771
1772 // the current focus
1773 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
1774
1775 if ( !htFocus )
1776 {
1777 return;
1778 }
1779
1780 if ( HasFlag(wxTR_MULTIPLE) )
1781 {
1782 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
1783 this, wxTreeItemId());
1784 changingEvent.m_itemOld = htFocus;
1785
1786 if ( IsTreeEventAllowed(changingEvent) )
1787 {
1788 ClearFocusedItem();
1789
1790 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
1791 this, wxTreeItemId());
1792 changedEvent.m_itemOld = htFocus;
1793 (void)HandleTreeEvent(changedEvent);
1794 }
1795 }
1796 else
1797 {
1798 ClearFocusedItem();
1799 }
1800 }
1801
1802 void wxTreeCtrl::DoUnselectAll()
1803 {
1804 wxArrayTreeItemIds selections;
1805 size_t count = GetSelections(selections);
1806
1807 for ( size_t n = 0; n < count; n++ )
1808 {
1809 DoUnselectItem(selections[n]);
1810 }
1811
1812 m_htSelStart.Unset();
1813 }
1814
1815 void wxTreeCtrl::UnselectAll()
1816 {
1817 if ( HasFlag(wxTR_MULTIPLE) )
1818 {
1819 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
1820 if ( !htFocus ) return;
1821
1822 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this);
1823 changingEvent.m_itemOld = htFocus;
1824
1825 if ( IsTreeEventAllowed(changingEvent) )
1826 {
1827 DoUnselectAll();
1828
1829 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this);
1830 changedEvent.m_itemOld = htFocus;
1831 (void)HandleTreeEvent(changedEvent);
1832 }
1833 }
1834 else
1835 {
1836 Unselect();
1837 }
1838 }
1839
1840 void wxTreeCtrl::DoSelectChildren(const wxTreeItemId& parent)
1841 {
1842 DoUnselectAll();
1843
1844 wxTreeItemIdValue cookie;
1845 wxTreeItemId child = GetFirstChild(parent, cookie);
1846 while ( child.IsOk() )
1847 {
1848 DoSelectItem(child, true);
1849 child = GetNextChild(child, cookie);
1850 }
1851 }
1852
1853 void wxTreeCtrl::SelectChildren(const wxTreeItemId& parent)
1854 {
1855 wxCHECK_RET( HasFlag(wxTR_MULTIPLE),
1856 "this only works with multiple selection controls" );
1857
1858 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
1859
1860 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this);
1861 changingEvent.m_itemOld = htFocus;
1862
1863 if ( IsTreeEventAllowed(changingEvent) )
1864 {
1865 DoSelectChildren(parent);
1866
1867 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this);
1868 changedEvent.m_itemOld = htFocus;
1869 (void)HandleTreeEvent(changedEvent);
1870 }
1871 }
1872
1873 void wxTreeCtrl::DoSelectItem(const wxTreeItemId& item, bool select)
1874 {
1875 TempSetter set(m_changingSelection);
1876
1877 ::SelectItem(GetHwnd(), HITEM(item), select);
1878 }
1879
1880 void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
1881 {
1882 wxCHECK_RET( !IsHiddenRoot(item), wxT("can't select hidden root item") );
1883
1884 if ( select == IsSelected(item) )
1885 {
1886 // nothing to do, the item is already in the requested state
1887 return;
1888 }
1889
1890 if ( HasFlag(wxTR_MULTIPLE) )
1891 {
1892 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
1893
1894 if ( IsTreeEventAllowed(changingEvent) )
1895 {
1896 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
1897 DoSelectItem(item, select);
1898
1899 if ( !htFocus )
1900 {
1901 SetFocusedItem(item);
1902 }
1903
1904 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
1905 this, item);
1906 (void)HandleTreeEvent(changedEvent);
1907 }
1908 }
1909 else // single selection
1910 {
1911 wxTreeItemId itemOld, itemNew;
1912 if ( select )
1913 {
1914 itemOld = GetSelection();
1915 itemNew = item;
1916 }
1917 else // deselecting the currently selected item
1918 {
1919 itemOld = item;
1920 // leave itemNew invalid
1921 }
1922
1923 // Recent versions of comctl32.dll send TVN_SELCHANG{ED,ING} events
1924 // when we call TreeView_SelectItem() but apparently some old ones did
1925 // not so send the events ourselves and ignore those generated by
1926 // TreeView_SelectItem() if m_changingSelection is set.
1927 wxTreeEvent
1928 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, itemNew);
1929 changingEvent.SetOldItem(itemOld);
1930
1931 if ( IsTreeEventAllowed(changingEvent) )
1932 {
1933 TempSetter set(m_changingSelection);
1934
1935 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew)) )
1936 {
1937 wxLogLastError(wxT("TreeView_SelectItem"));
1938 }
1939 else // ok
1940 {
1941 ::SetFocus(GetHwnd(), HITEM(item));
1942
1943 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
1944 this, itemNew);
1945 changedEvent.SetOldItem(itemOld);
1946 (void)HandleTreeEvent(changedEvent);
1947 }
1948 }
1949 //else: program vetoed the change
1950 }
1951 }
1952
1953 void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1954 {
1955 wxCHECK_RET( !IsHiddenRoot(item), wxT("can't show hidden root item") );
1956
1957 // no error return
1958 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
1959 }
1960
1961 void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1962 {
1963 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
1964 {
1965 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1966 }
1967 }
1968
1969 wxTextCtrl *wxTreeCtrl::GetEditControl() const
1970 {
1971 return m_textCtrl;
1972 }
1973
1974 void wxTreeCtrl::DeleteTextCtrl()
1975 {
1976 if ( m_textCtrl )
1977 {
1978 // the HWND corresponding to this control is deleted by the tree
1979 // control itself and we don't know when exactly this happens, so check
1980 // if the window still exists before calling UnsubclassWin()
1981 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1982 {
1983 m_textCtrl->SetHWND(0);
1984 }
1985
1986 m_textCtrl->UnsubclassWin();
1987 m_textCtrl->SetHWND(0);
1988 wxDELETE(m_textCtrl);
1989
1990 m_idEdited.Unset();
1991 }
1992 }
1993
1994 wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1995 wxClassInfo *textControlClass)
1996 {
1997 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1998
1999 DeleteTextCtrl();
2000
2001 m_idEdited = item;
2002 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
2003 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
2004
2005 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2006 // returned false
2007 if ( !hWnd )
2008 {
2009 wxDELETE(m_textCtrl);
2010 return NULL;
2011 }
2012
2013 // textctrl is subclassed in MSWOnNotify
2014 return m_textCtrl;
2015 }
2016
2017 // End label editing, optionally cancelling the edit
2018 void wxTreeCtrl::DoEndEditLabel(bool discardChanges)
2019 {
2020 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
2021
2022 DeleteTextCtrl();
2023 }
2024
2025 wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
2026 {
2027 TV_HITTESTINFO hitTestInfo;
2028 hitTestInfo.pt.x = (int)point.x;
2029 hitTestInfo.pt.y = (int)point.y;
2030
2031 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo);
2032
2033 flags = 0;
2034
2035 // avoid repetition
2036 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2037 flags |= wxTREE_HITTEST_##flag
2038
2039 TRANSLATE_FLAG(ABOVE);
2040 TRANSLATE_FLAG(BELOW);
2041 TRANSLATE_FLAG(NOWHERE);
2042 TRANSLATE_FLAG(ONITEMBUTTON);
2043 TRANSLATE_FLAG(ONITEMICON);
2044 TRANSLATE_FLAG(ONITEMINDENT);
2045 TRANSLATE_FLAG(ONITEMLABEL);
2046 TRANSLATE_FLAG(ONITEMRIGHT);
2047 TRANSLATE_FLAG(ONITEMSTATEICON);
2048 TRANSLATE_FLAG(TOLEFT);
2049 TRANSLATE_FLAG(TORIGHT);
2050
2051 #undef TRANSLATE_FLAG
2052
2053 return wxTreeItemId(hitTestInfo.hItem);
2054 }
2055
2056 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2057 wxRect& rect,
2058 bool textOnly) const
2059 {
2060 // Virtual root items have no bounding rectangle
2061 if ( IS_VIRTUAL_ROOT(item) )
2062 {
2063 return false;
2064 }
2065
2066 TVGetItemRectParam param;
2067
2068 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(item), param, textOnly) )
2069 {
2070 rect = wxRect(wxPoint(param.rect.left, param.rect.top),
2071 wxPoint(param.rect.right, param.rect.bottom));
2072
2073 return true;
2074 }
2075 else
2076 {
2077 // couldn't retrieve rect: for example, item isn't visible
2078 return false;
2079 }
2080 }
2081
2082 void wxTreeCtrl::ClearFocusedItem()
2083 {
2084 TempSetter set(m_changingSelection);
2085
2086 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2087 {
2088 wxLogLastError(wxT("TreeView_SelectItem"));
2089 }
2090 }
2091
2092 void wxTreeCtrl::SetFocusedItem(const wxTreeItemId& item)
2093 {
2094 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
2095
2096 TempSetter set(m_changingSelection);
2097
2098 ::SetFocus(GetHwnd(), HITEM(item));
2099 }
2100
2101 void wxTreeCtrl::DoUnselectItem(const wxTreeItemId& item)
2102 {
2103 TempSetter set(m_changingSelection);
2104
2105 ::UnselectItem(GetHwnd(), HITEM(item));
2106 }
2107
2108 void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId& item)
2109 {
2110 TempSetter set(m_changingSelection);
2111
2112 ::ToggleItemSelection(GetHwnd(), HITEM(item));
2113 }
2114
2115 // ----------------------------------------------------------------------------
2116 // sorting stuff
2117 // ----------------------------------------------------------------------------
2118
2119 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2120 // functions such as IsDataIndirect()
2121 class wxTreeSortHelper
2122 {
2123 public:
2124 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
2125
2126 private:
2127 static wxTreeItemId GetIdFromData(LPARAM lParam)
2128 {
2129 return ((wxTreeItemParam*)lParam)->GetItem();
2130 }
2131 };
2132
2133 int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
2134 LPARAM pItem2,
2135 LPARAM htree)
2136 {
2137 wxCHECK_MSG( pItem1 && pItem2, 0,
2138 wxT("sorting tree without data doesn't make sense") );
2139
2140 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
2141
2142 return tree->OnCompareItems(GetIdFromData(pItem1),
2143 GetIdFromData(pItem2));
2144 }
2145
2146 void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
2147 {
2148 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
2149
2150 // rely on the fact that TreeView_SortChildren does the same thing as our
2151 // default behaviour, i.e. sorts items alphabetically and so call it
2152 // directly if we're not in derived class (much more efficient!)
2153 // RN: Note that if you find you're code doesn't sort as expected this
2154 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2155 // combo for your derived wxTreeCtrl if will sort without
2156 // OnCompareItems
2157 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2158 {
2159 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2160 }
2161 else
2162 {
2163 TV_SORTCB tvSort;
2164 tvSort.hParent = HITEM(item);
2165 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
2166 tvSort.lParam = (LPARAM)this;
2167 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2168 }
2169 }
2170
2171 // ----------------------------------------------------------------------------
2172 // implementation
2173 // ----------------------------------------------------------------------------
2174
2175 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
2176 {
2177 if ( msg->message == WM_KEYDOWN )
2178 {
2179 // Only eat VK_RETURN if not being used by the application in
2180 // conjunction with modifiers
2181 if ( (msg->wParam == VK_RETURN) && !wxIsAnyModifierDown() )
2182 {
2183 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2184 return false;
2185 }
2186 }
2187
2188 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg);
2189 }
2190
2191 bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id_)
2192 {
2193 const int id = (signed short)id_;
2194
2195 if ( cmd == EN_UPDATE )
2196 {
2197 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
2198 event.SetEventObject( this );
2199 ProcessCommand(event);
2200 }
2201 else if ( cmd == EN_KILLFOCUS )
2202 {
2203 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
2204 event.SetEventObject( this );
2205 ProcessCommand(event);
2206 }
2207 else
2208 {
2209 // nothing done
2210 return false;
2211 }
2212
2213 // command processed
2214 return true;
2215 }
2216
2217 bool wxTreeCtrl::MSWIsOnItem(unsigned flags) const
2218 {
2219 unsigned mask = TVHT_ONITEM;
2220 if ( HasFlag(wxTR_FULL_ROW_HIGHLIGHT) )
2221 mask |= TVHT_ONITEMINDENT | TVHT_ONITEMRIGHT;
2222
2223 return (flags & mask) != 0;
2224 }
2225
2226 bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey)
2227 {
2228 const bool bCtrl = wxIsCtrlDown();
2229 const bool bShift = wxIsShiftDown();
2230 const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2231
2232 switch ( vkey )
2233 {
2234 case VK_RETURN:
2235 case VK_SPACE:
2236 if ( !htSel )
2237 break;
2238
2239 if ( vkey != VK_RETURN && bCtrl )
2240 {
2241 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2242 this, htSel);
2243 changingEvent.m_itemOld = htSel;
2244
2245 if ( IsTreeEventAllowed(changingEvent) )
2246 {
2247 DoToggleItemSelection(wxTreeItemId(htSel));
2248
2249 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2250 this, htSel);
2251 changedEvent.m_itemOld = htSel;
2252 (void)HandleTreeEvent(changedEvent);
2253 }
2254 }
2255 else
2256 {
2257 wxArrayTreeItemIds selections;
2258 size_t count = GetSelections(selections);
2259
2260 if ( count != 1 || HITEM(selections[0]) != htSel )
2261 {
2262 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2263 this, htSel);
2264 changingEvent.m_itemOld = htSel;
2265
2266 if ( IsTreeEventAllowed(changingEvent) )
2267 {
2268 DoUnselectAll();
2269 DoSelectItem(wxTreeItemId(htSel));
2270
2271 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2272 this, htSel);
2273 changedEvent.m_itemOld = htSel;
2274 (void)HandleTreeEvent(changedEvent);
2275 }
2276 }
2277 }
2278 break;
2279
2280 case VK_UP:
2281 case VK_DOWN:
2282 if ( !bCtrl && !bShift )
2283 {
2284 wxArrayTreeItemIds selections;
2285 wxTreeItemId next;
2286
2287 if ( htSel )
2288 {
2289 next = vkey == VK_UP
2290 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2291 : TreeView_GetNextVisible(GetHwnd(), htSel);
2292 }
2293 else
2294 {
2295 next = GetRootItem();
2296
2297 if ( IsHiddenRoot(next) )
2298 next = TreeView_GetChild(GetHwnd(), HITEM(next));
2299 }
2300
2301 if ( !next.IsOk() )
2302 {
2303 break;
2304 }
2305
2306 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2307 this, next);
2308 changingEvent.m_itemOld = htSel;
2309
2310 if ( IsTreeEventAllowed(changingEvent) )
2311 {
2312 DoUnselectAll();
2313 DoSelectItem(next);
2314 SetFocusedItem(next);
2315
2316 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2317 this, next);
2318 changedEvent.m_itemOld = htSel;
2319 (void)HandleTreeEvent(changedEvent);
2320 }
2321 }
2322 else if ( htSel )
2323 {
2324 wxTreeItemId next = vkey == VK_UP
2325 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2326 : TreeView_GetNextVisible(GetHwnd(), htSel);
2327
2328 if ( !next.IsOk() )
2329 {
2330 break;
2331 }
2332
2333 if ( !m_htSelStart )
2334 {
2335 m_htSelStart = htSel;
2336 }
2337
2338 if ( bShift && SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next),
2339 SR_UNSELECT_OTHERS | SR_SIMULATE) )
2340 {
2341 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
2342 changingEvent.m_itemOld = htSel;
2343
2344 if ( IsTreeEventAllowed(changingEvent) )
2345 {
2346 SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next),
2347 SR_UNSELECT_OTHERS);
2348
2349 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
2350 changedEvent.m_itemOld = htSel;
2351 (void)HandleTreeEvent(changedEvent);
2352 }
2353 }
2354
2355 SetFocusedItem(next);
2356 }
2357 break;
2358
2359 case VK_LEFT:
2360 if ( HasChildren(htSel) && IsExpanded(htSel) )
2361 {
2362 Collapse(htSel);
2363 }
2364 else
2365 {
2366 wxTreeItemId next = GetItemParent(htSel);
2367
2368 if ( next.IsOk() && !IsHiddenRoot(next) )
2369 {
2370 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2371 this, next);
2372 changingEvent.m_itemOld = htSel;
2373
2374 if ( IsTreeEventAllowed(changingEvent) )
2375 {
2376 DoUnselectAll();
2377 DoSelectItem(next);
2378 SetFocusedItem(next);
2379
2380 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2381 this, next);
2382 changedEvent.m_itemOld = htSel;
2383 (void)HandleTreeEvent(changedEvent);
2384 }
2385 }
2386 }
2387 break;
2388
2389 case VK_RIGHT:
2390 if ( !IsVisible(htSel) )
2391 {
2392 EnsureVisible(htSel);
2393 }
2394
2395 if ( !HasChildren(htSel) )
2396 break;
2397
2398 if ( !IsExpanded(htSel) )
2399 {
2400 Expand(htSel);
2401 }
2402 else
2403 {
2404 wxTreeItemId next = TreeView_GetChild(GetHwnd(), htSel);
2405
2406 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
2407 changingEvent.m_itemOld = htSel;
2408
2409 if ( IsTreeEventAllowed(changingEvent) )
2410 {
2411 DoUnselectAll();
2412 DoSelectItem(next);
2413 SetFocusedItem(next);
2414
2415 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
2416 changedEvent.m_itemOld = htSel;
2417 (void)HandleTreeEvent(changedEvent);
2418 }
2419 }
2420 break;
2421
2422 case VK_HOME:
2423 case VK_END:
2424 {
2425 wxTreeItemId next = GetRootItem();
2426
2427 if ( IsHiddenRoot(next) )
2428 {
2429 next = TreeView_GetChild(GetHwnd(), HITEM(next));
2430 }
2431
2432 if ( !next.IsOk() )
2433 break;
2434
2435 if ( vkey == VK_END )
2436 {
2437 for ( ;; )
2438 {
2439 wxTreeItemId nextTemp = TreeView_GetNextVisible(
2440 GetHwnd(), HITEM(next));
2441
2442 if ( !nextTemp.IsOk() )
2443 break;
2444
2445 next = nextTemp;
2446 }
2447 }
2448
2449 if ( htSel == HITEM(next) )
2450 break;
2451
2452 if ( bShift )
2453 {
2454 if ( !m_htSelStart )
2455 {
2456 m_htSelStart = htSel;
2457 }
2458
2459 if ( SelectRange(GetHwnd(),
2460 HITEM(m_htSelStart), HITEM(next),
2461 SR_UNSELECT_OTHERS | SR_SIMULATE) )
2462 {
2463 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2464 this, next);
2465 changingEvent.m_itemOld = htSel;
2466
2467 if ( IsTreeEventAllowed(changingEvent) )
2468 {
2469 SelectRange(GetHwnd(),
2470 HITEM(m_htSelStart), HITEM(next),
2471 SR_UNSELECT_OTHERS);
2472 SetFocusedItem(next);
2473
2474 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2475 this, next);
2476 changedEvent.m_itemOld = htSel;
2477 (void)HandleTreeEvent(changedEvent);
2478 }
2479 }
2480 }
2481 else // no Shift
2482 {
2483 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2484 this, next);
2485 changingEvent.m_itemOld = htSel;
2486
2487 if ( IsTreeEventAllowed(changingEvent) )
2488 {
2489 DoUnselectAll();
2490 DoSelectItem(next);
2491 SetFocusedItem(next);
2492
2493 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2494 this, next);
2495 changedEvent.m_itemOld = htSel;
2496 (void)HandleTreeEvent(changedEvent);
2497 }
2498 }
2499 }
2500 break;
2501
2502 case VK_PRIOR:
2503 case VK_NEXT:
2504 if ( bCtrl )
2505 {
2506 wxTreeItemId firstVisible = GetFirstVisibleItem();
2507 size_t visibleCount = TreeView_GetVisibleCount(GetHwnd());
2508 wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ?
2509 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible)) :
2510 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible));
2511
2512 if ( !nextAdjacent )
2513 {
2514 break;
2515 }
2516
2517 wxTreeItemId nextStart = firstVisible;
2518
2519 for ( size_t n = 1; n < visibleCount; n++ )
2520 {
2521 wxTreeItemId nextTemp = (vkey == VK_PRIOR) ?
2522 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart)) :
2523 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart));
2524
2525 if ( nextTemp.IsOk() )
2526 {
2527 nextStart = nextTemp;
2528 }
2529 else
2530 {
2531 break;
2532 }
2533 }
2534
2535 EnsureVisible(nextStart);
2536
2537 if ( vkey == VK_NEXT )
2538 {
2539 wxTreeItemId nextEnd = nextStart;
2540
2541 for ( size_t n = 1; n < visibleCount; n++ )
2542 {
2543 wxTreeItemId nextTemp =
2544 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd));
2545
2546 if ( nextTemp.IsOk() )
2547 {
2548 nextEnd = nextTemp;
2549 }
2550 else
2551 {
2552 break;
2553 }
2554 }
2555
2556 EnsureVisible(nextEnd);
2557 }
2558 }
2559 else // no Ctrl
2560 {
2561 size_t visibleCount = TreeView_GetVisibleCount(GetHwnd());
2562 wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ?
2563 TreeView_GetPrevVisible(GetHwnd(), htSel) :
2564 TreeView_GetNextVisible(GetHwnd(), htSel);
2565
2566 if ( !nextAdjacent )
2567 {
2568 break;
2569 }
2570
2571 wxTreeItemId next(htSel);
2572
2573 for ( size_t n = 1; n < visibleCount; n++ )
2574 {
2575 wxTreeItemId nextTemp = vkey == VK_PRIOR ?
2576 TreeView_GetPrevVisible(GetHwnd(), HITEM(next)) :
2577 TreeView_GetNextVisible(GetHwnd(), HITEM(next));
2578
2579 if ( !nextTemp.IsOk() )
2580 break;
2581
2582 next = nextTemp;
2583 }
2584
2585 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2586 this, next);
2587 changingEvent.m_itemOld = htSel;
2588
2589 if ( IsTreeEventAllowed(changingEvent) )
2590 {
2591 DoUnselectAll();
2592 m_htSelStart.Unset();
2593 DoSelectItem(next);
2594 SetFocusedItem(next);
2595
2596 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2597 this, next);
2598 changedEvent.m_itemOld = htSel;
2599 (void)HandleTreeEvent(changedEvent);
2600 }
2601 }
2602 break;
2603
2604 default:
2605 return false;
2606 }
2607
2608 return true;
2609 }
2610
2611 bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam, WXLPARAM lParam)
2612 {
2613 wxTreeEvent keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN, this);
2614 keyEvent.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, wParam, lParam);
2615
2616 bool processed = HandleTreeEvent(keyEvent);
2617
2618 // generate a separate event for Space/Return
2619 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2620 ((wParam == VK_SPACE) || (wParam == VK_RETURN)) )
2621 {
2622 const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2623 if ( htSel )
2624 {
2625 wxTreeEvent activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2626 this, htSel);
2627 (void)HandleTreeEvent(activatedEvent);
2628 }
2629 }
2630
2631 return processed;
2632 }
2633
2634 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2635 // only do it during dragging, minimize wxWin overhead (this is important for
2636 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2637 // instead of passing by wxWin events
2638 WXLRESULT
2639 wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2640 {
2641 bool processed = false;
2642 WXLRESULT rc = 0;
2643 bool isMultiple = HasFlag(wxTR_MULTIPLE);
2644
2645 if ( nMsg == WM_CONTEXTMENU )
2646 {
2647 int x = GET_X_LPARAM(lParam),
2648 y = GET_Y_LPARAM(lParam);
2649
2650 // the item for which the menu should be shown
2651 wxTreeItemId item;
2652
2653 // the position where the menu should be shown in client coordinates
2654 // (so that it can be passed directly to PopupMenu())
2655 wxPoint pt;
2656
2657 if ( x == -1 || y == -1 )
2658 {
2659 // this means that the event was generated from keyboard (e.g. with
2660 // Shift-F10 or special Windows menu key)
2661 //
2662 // use the Explorer standard of putting the menu at the left edge
2663 // of the text, in the vertical middle of the text
2664 item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2665 if ( item.IsOk() )
2666 {
2667 // Use the bounding rectangle of only the text part
2668 wxRect rect;
2669 GetBoundingRect(item, rect, true);
2670 pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2);
2671 }
2672 }
2673 else // event from mouse, use mouse position
2674 {
2675 pt = ScreenToClient(wxPoint(x, y));
2676
2677 TV_HITTESTINFO tvhti;
2678 tvhti.pt.x = pt.x;
2679 tvhti.pt.y = pt.y;
2680
2681 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2682 item = wxTreeItemId(tvhti.hItem);
2683 }
2684
2685 // create the event
2686 if ( item.IsOk() )
2687 {
2688 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
2689
2690 event.m_pointDrag = pt;
2691
2692 if ( HandleTreeEvent(event) )
2693 processed = true;
2694 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2695 }
2696 }
2697 else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
2698 {
2699 // we only process mouse messages here and these parameters have the
2700 // same meaning for all of them
2701 int x = GET_X_LPARAM(lParam),
2702 y = GET_Y_LPARAM(lParam);
2703
2704 TV_HITTESTINFO tvht;
2705 tvht.pt.x = x;
2706 tvht.pt.y = y;
2707
2708 HTREEITEM htOldItem = TreeView_GetSelection(GetHwnd());
2709 HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht);
2710
2711 switch ( nMsg )
2712 {
2713 case WM_LBUTTONDOWN:
2714 if ( !isMultiple )
2715 break;
2716
2717 m_htClickedItem.Unset();
2718
2719 if ( !MSWIsOnItem(tvht.flags) )
2720 {
2721 if ( tvht.flags & TVHT_ONITEMBUTTON )
2722 {
2723 // either it's going to be handled by user code or
2724 // we're going to use it ourselves to toggle the
2725 // branch, in either case don't pass it to the base
2726 // class which would generate another mouse click event
2727 // for it even though it's already handled here
2728 processed = true;
2729 SetFocus();
2730
2731 if ( !HandleMouseEvent(nMsg, x, y, wParam) )
2732 {
2733 if ( !IsExpanded(htItem) )
2734 {
2735 Expand(htItem);
2736 }
2737 else
2738 {
2739 Collapse(htItem);
2740 }
2741 }
2742 }
2743
2744 m_focusLost = false;
2745 break;
2746 }
2747
2748 processed = true;
2749 SetFocus();
2750 m_htClickedItem = (WXHTREEITEM) htItem;
2751 m_ptClick = wxPoint(x, y);
2752
2753 if ( wParam & MK_CONTROL )
2754 {
2755 if ( HandleMouseEvent(nMsg, x, y, wParam) )
2756 {
2757 m_htClickedItem.Unset();
2758 break;
2759 }
2760
2761 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2762 this, htItem);
2763 changingEvent.m_itemOld = htOldItem;
2764
2765 if ( IsTreeEventAllowed(changingEvent) )
2766 {
2767 // toggle selected state
2768 DoToggleItemSelection(wxTreeItemId(htItem));
2769
2770 SetFocusedItem(wxTreeItemId(htItem));
2771
2772 // reset on any click without Shift
2773 m_htSelStart.Unset();
2774
2775 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2776 this, htItem);
2777 changedEvent.m_itemOld = htOldItem;
2778 (void)HandleTreeEvent(changedEvent);
2779 }
2780 }
2781 else if ( wParam & MK_SHIFT )
2782 {
2783 if ( HandleMouseEvent(nMsg, x, y, wParam) )
2784 {
2785 m_htClickedItem.Unset();
2786 break;
2787 }
2788
2789 int srFlags = 0;
2790 bool willChange = true;
2791
2792 if ( !(wParam & MK_CONTROL) )
2793 {
2794 srFlags |= SR_UNSELECT_OTHERS;
2795 }
2796
2797 if ( !m_htSelStart )
2798 {
2799 // take the focused item
2800 m_htSelStart = htOldItem;
2801 }
2802 else
2803 {
2804 willChange = SelectRange(GetHwnd(), HITEM(m_htSelStart),
2805 htItem, srFlags | SR_SIMULATE);
2806 }
2807
2808 if ( willChange )
2809 {
2810 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2811 this, htItem);
2812 changingEvent.m_itemOld = htOldItem;
2813
2814 if ( IsTreeEventAllowed(changingEvent) )
2815 {
2816 // this selects all items between the starting one
2817 // and the current
2818 if ( m_htSelStart )
2819 {
2820 SelectRange(GetHwnd(), HITEM(m_htSelStart),
2821 htItem, srFlags);
2822 }
2823 else
2824 {
2825 DoSelectItem(wxTreeItemId(htItem));
2826 }
2827
2828 SetFocusedItem(wxTreeItemId(htItem));
2829
2830 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2831 this, htItem);
2832 changedEvent.m_itemOld = htOldItem;
2833 (void)HandleTreeEvent(changedEvent);
2834 }
2835 }
2836 }
2837 else // normal click
2838 {
2839 // avoid doing anything if we click on the only
2840 // currently selected item
2841
2842 wxArrayTreeItemIds selections;
2843 size_t count = GetSelections(selections);
2844
2845 if ( count == 0 ||
2846 count > 1 ||
2847 HITEM(selections[0]) != htItem )
2848 {
2849 if ( HandleMouseEvent(nMsg, x, y, wParam) )
2850 {
2851 m_htClickedItem.Unset();
2852 break;
2853 }
2854
2855 // clear the previously selected items, if the user
2856 // clicked outside of the present selection, otherwise,
2857 // perform the deselection on mouse-up, this allows
2858 // multiple drag and drop to work.
2859 if ( !IsItemSelected(GetHwnd(), htItem))
2860 {
2861 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2862 this, htItem);
2863 changingEvent.m_itemOld = htOldItem;
2864
2865 if ( IsTreeEventAllowed(changingEvent) )
2866 {
2867 DoUnselectAll();
2868 DoSelectItem(wxTreeItemId(htItem));
2869 SetFocusedItem(wxTreeItemId(htItem));
2870
2871 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2872 this, htItem);
2873 changedEvent.m_itemOld = htOldItem;
2874 (void)HandleTreeEvent(changedEvent);
2875 }
2876 }
2877 else
2878 {
2879 SetFocusedItem(wxTreeItemId(htItem));
2880 m_mouseUpDeselect = true;
2881 }
2882 }
2883 else // click on a single selected item
2884 {
2885 // don't interfere with the default processing in
2886 // WM_MOUSEMOVE handler below as the default window
2887 // proc will start the drag itself if we let have
2888 // WM_LBUTTONDOWN
2889 m_htClickedItem.Unset();
2890
2891 // prevent in-place editing from starting if focus lost
2892 // since previous click
2893 if ( m_focusLost )
2894 {
2895 ClearFocusedItem();
2896 DoSelectItem(wxTreeItemId(htItem));
2897 SetFocusedItem(wxTreeItemId(htItem));
2898 }
2899 else
2900 {
2901 processed = false;
2902 }
2903 }
2904
2905 // reset on any click without Shift
2906 m_htSelStart.Unset();
2907 }
2908
2909 m_focusLost = false;
2910
2911 // we consumed the event so we need to trigger state image
2912 // click if needed
2913 if ( processed )
2914 {
2915 int htFlags = 0;
2916 wxTreeItemId item = HitTest(wxPoint(x, y), htFlags);
2917
2918 if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON )
2919 {
2920 m_triggerStateImageClick = true;
2921 }
2922 }
2923 break;
2924
2925 case WM_RBUTTONDOWN:
2926 if ( !isMultiple )
2927 break;
2928
2929 processed = true;
2930 SetFocus();
2931
2932 if ( HandleMouseEvent(nMsg, x, y, wParam) || !htItem )
2933 {
2934 break;
2935 }
2936
2937 // default handler removes the highlight from the currently
2938 // focused item when right mouse button is pressed on another
2939 // one but keeps the remaining items highlighted, which is
2940 // confusing, so override this default behaviour
2941 if ( !IsItemSelected(GetHwnd(), htItem) )
2942 {
2943 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2944 this, htItem);
2945 changingEvent.m_itemOld = htOldItem;
2946
2947 if ( IsTreeEventAllowed(changingEvent) )
2948 {
2949 DoUnselectAll();
2950 DoSelectItem(wxTreeItemId(htItem));
2951 SetFocusedItem(wxTreeItemId(htItem));
2952
2953 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2954 this, htItem);
2955 changedEvent.m_itemOld = htOldItem;
2956 (void)HandleTreeEvent(changedEvent);
2957 }
2958 }
2959
2960 break;
2961
2962 case WM_MOUSEMOVE:
2963 #ifndef __WXWINCE__
2964 if ( m_htClickedItem )
2965 {
2966 int cx = abs(m_ptClick.x - x);
2967 int cy = abs(m_ptClick.y - y);
2968
2969 if ( cx > ::GetSystemMetrics(SM_CXDRAG) ||
2970 cy > ::GetSystemMetrics(SM_CYDRAG) )
2971 {
2972 NM_TREEVIEW tv;
2973 wxZeroMemory(tv);
2974
2975 tv.hdr.hwndFrom = GetHwnd();
2976 tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
2977 tv.hdr.code = TVN_BEGINDRAG;
2978
2979 tv.itemNew.hItem = HITEM(m_htClickedItem);
2980
2981
2982 TVITEM tviAux;
2983 wxZeroMemory(tviAux);
2984
2985 tviAux.hItem = HITEM(m_htClickedItem);
2986 tviAux.mask = TVIF_STATE | TVIF_PARAM;
2987 tviAux.stateMask = 0xffffffff;
2988 TreeView_GetItem(GetHwnd(), &tviAux);
2989
2990 tv.itemNew.state = tviAux.state;
2991 tv.itemNew.lParam = tviAux.lParam;
2992
2993 tv.ptDrag.x = x;
2994 tv.ptDrag.y = y;
2995
2996 // do it before SendMessage() call below to avoid
2997 // reentrancies here if there is another WM_MOUSEMOVE
2998 // in the queue already
2999 m_htClickedItem.Unset();
3000
3001 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY,
3002 tv.hdr.idFrom, (LPARAM)&tv );
3003
3004 // don't pass it to the default window proc, it would
3005 // start dragging again
3006 processed = true;
3007 }
3008 }
3009 #endif // __WXWINCE__
3010
3011 #if wxUSE_DRAGIMAGE
3012 if ( m_dragImage )
3013 {
3014 m_dragImage->Move(wxPoint(x, y));
3015 if ( htItem )
3016 {
3017 // highlight the item as target (hiding drag image is
3018 // necessary - otherwise the display will be corrupted)
3019 m_dragImage->Hide();
3020 TreeView_SelectDropTarget(GetHwnd(), htItem);
3021 m_dragImage->Show();
3022 }
3023 }
3024 #endif // wxUSE_DRAGIMAGE
3025 break;
3026
3027 case WM_LBUTTONUP:
3028 if ( isMultiple )
3029 {
3030 // deselect other items if needed
3031 if ( htItem )
3032 {
3033 if ( m_mouseUpDeselect )
3034 {
3035 m_mouseUpDeselect = false;
3036
3037 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
3038 this, htItem);
3039 changingEvent.m_itemOld = htOldItem;
3040
3041 if ( IsTreeEventAllowed(changingEvent) )
3042 {
3043 DoUnselectAll();
3044 DoSelectItem(wxTreeItemId(htItem));
3045 SetFocusedItem(wxTreeItemId(htItem));
3046
3047 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
3048 this, htItem);
3049 changedEvent.m_itemOld = htOldItem;
3050 (void)HandleTreeEvent(changedEvent);
3051 }
3052 }
3053 }
3054
3055 m_htClickedItem.Unset();
3056
3057 if ( m_triggerStateImageClick )
3058 {
3059 if ( tvht.flags & TVHT_ONITEMSTATEICON )
3060 {
3061 wxTreeEvent event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK,
3062 this, htItem);
3063 (void)HandleTreeEvent(event);
3064
3065 m_triggerStateImageClick = false;
3066 processed = true;
3067 }
3068 }
3069
3070 if ( !m_dragStarted && MSWIsOnItem(tvht.flags) )
3071 {
3072 processed = true;
3073 }
3074 }
3075
3076 // fall through
3077
3078 case WM_RBUTTONUP:
3079 #if wxUSE_DRAGIMAGE
3080 if ( m_dragImage )
3081 {
3082 m_dragImage->EndDrag();
3083 wxDELETE(m_dragImage);
3084
3085 // generate the drag end event
3086 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG,
3087 this, htItem);
3088 event.m_pointDrag = wxPoint(x, y);
3089 (void)HandleTreeEvent(event);
3090
3091 // if we don't do it, the tree seems to think that 2 items
3092 // are selected simultaneously which is quite weird
3093 TreeView_SelectDropTarget(GetHwnd(), 0);
3094 }
3095 #endif // wxUSE_DRAGIMAGE
3096
3097 if ( isMultiple && nMsg == WM_RBUTTONUP )
3098 {
3099 // send NM_RCLICK
3100 NMHDR nmhdr;
3101 nmhdr.hwndFrom = GetHwnd();
3102 nmhdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
3103 nmhdr.code = NM_RCLICK;
3104 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY,
3105 nmhdr.idFrom, (LPARAM)&nmhdr);
3106 processed = true;
3107 }
3108
3109 m_dragStarted = false;
3110
3111 break;
3112 }
3113 }
3114 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) )
3115 {
3116 if ( isMultiple )
3117 {
3118 // the tree control greys out the selected item when it loses focus
3119 // and paints it as selected again when it regains it, but it won't
3120 // do it for the other items itself - help it
3121 wxArrayTreeItemIds selections;
3122 size_t count = GetSelections(selections);
3123 TVGetItemRectParam param;
3124
3125 for ( size_t n = 0; n < count; n++ )
3126 {
3127 // TreeView_GetItemRect() will return false if item is not
3128 // visible, which may happen perfectly well
3129 if ( wxTreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
3130 param, TRUE) )
3131 {
3132 ::InvalidateRect(GetHwnd(), &param.rect, FALSE);
3133 }
3134 }
3135 }
3136
3137 if ( nMsg == WM_KILLFOCUS )
3138 {
3139 m_focusLost = true;
3140 }
3141 }
3142 else if ( (nMsg == WM_KEYDOWN || nMsg == WM_SYSKEYDOWN) && isMultiple )
3143 {
3144 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3145 // notification but for the keys which can be used to change selection
3146 // we need to do it from here so as to not apply the default behaviour
3147 // if the events are handled by the user code
3148 switch ( wParam )
3149 {
3150 case VK_RETURN:
3151 case VK_SPACE:
3152 case VK_UP:
3153 case VK_DOWN:
3154 case VK_LEFT:
3155 case VK_RIGHT:
3156 case VK_HOME:
3157 case VK_END:
3158 case VK_PRIOR:
3159 case VK_NEXT:
3160 if ( !HandleKeyDown(wParam, lParam) &&
3161 !MSWHandleTreeKeyDownEvent(wParam, lParam) )
3162 {
3163 // use the key to update the selection if it was left
3164 // unprocessed
3165 MSWHandleSelectionKey(wParam);
3166 }
3167
3168 // pretend that we did process it in any case as we already
3169 // generated an event for it
3170 processed = true;
3171
3172 //default: for all the other keys leave processed as false so that
3173 // the tree control generates a TVN_KEYDOWN for us
3174 }
3175
3176 }
3177 else if ( nMsg == WM_COMMAND )
3178 {
3179 // if we receive a EN_KILLFOCUS command from the in-place edit control
3180 // used for label editing, make sure to end editing
3181 WORD id, cmd;
3182 WXHWND hwnd;
3183 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
3184
3185 if ( cmd == EN_KILLFOCUS )
3186 {
3187 if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd )
3188 {
3189 DoEndEditLabel();
3190
3191 processed = true;
3192 }
3193 }
3194 }
3195
3196 if ( !processed )
3197 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
3198
3199 return rc;
3200 }
3201
3202 WXLRESULT
3203 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
3204 {
3205 if ( nMsg == WM_CHAR )
3206 {
3207 // don't let the control process Space and Return keys because it
3208 // doesn't do anything useful with them anyhow but always beeps
3209 // annoyingly when it receives them and there is no way to turn it off
3210 // simply if you just process TREEITEM_ACTIVATED event to which Space
3211 // and Enter presses are mapped in your code
3212 if ( wParam == VK_SPACE || wParam == VK_RETURN )
3213 return 0;
3214 }
3215 #if wxUSE_DRAGIMAGE
3216 else if ( nMsg == WM_KEYDOWN )
3217 {
3218 if ( wParam == VK_ESCAPE )
3219 {
3220 if ( m_dragImage )
3221 {
3222 m_dragImage->EndDrag();
3223 wxDELETE(m_dragImage);
3224
3225 // if we don't do it, the tree seems to think that 2 items
3226 // are selected simultaneously which is quite weird
3227 TreeView_SelectDropTarget(GetHwnd(), 0);
3228 }
3229 }
3230 }
3231 #endif // wxUSE_DRAGIMAGE
3232
3233 return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);
3234 }
3235
3236 // process WM_NOTIFY Windows message
3237 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
3238 {
3239 wxTreeEvent event(wxEVT_NULL, this);
3240 wxEventType eventType = wxEVT_NULL;
3241 NMHDR *hdr = (NMHDR *)lParam;
3242
3243 switch ( hdr->code )
3244 {
3245 case TVN_BEGINDRAG:
3246 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
3247 // fall through
3248
3249 case TVN_BEGINRDRAG:
3250 {
3251 if ( eventType == wxEVT_NULL )
3252 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
3253 //else: left drag, already set above
3254
3255 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3256
3257 event.m_item = tv->itemNew.hItem;
3258 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
3259
3260 // don't allow dragging by default: the user code must
3261 // explicitly say that it wants to allow it to avoid breaking
3262 // the old apps
3263 event.Veto();
3264 }
3265 break;
3266
3267 case TVN_BEGINLABELEDIT:
3268 {
3269 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
3270 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3271
3272 // although the user event handler may still veto it, it is
3273 // important to set it now so that calls to SetItemText() from
3274 // the event handler would change the text controls contents
3275 m_idEdited =
3276 event.m_item = info->item.hItem;
3277 event.m_label = info->item.pszText;
3278 event.m_editCancelled = false;
3279 }
3280 break;
3281
3282 case TVN_DELETEITEM:
3283 {
3284 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
3285 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3286
3287 event.m_item = tv->itemOld.hItem;
3288
3289 if ( m_hasAnyAttr )
3290 {
3291 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
3292 if ( it != m_attrs.end() )
3293 {
3294 delete it->second;
3295 m_attrs.erase(it);
3296 }
3297 }
3298 }
3299 break;
3300
3301 case TVN_ENDLABELEDIT:
3302 {
3303 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
3304 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3305
3306 event.m_item = info->item.hItem;
3307 event.m_label = info->item.pszText;
3308 event.m_editCancelled = info->item.pszText == NULL;
3309 break;
3310 }
3311
3312 #ifndef __WXWINCE__
3313 // These *must* not be removed or TVN_GETINFOTIP will
3314 // not be processed each time the mouse is moved
3315 // and the tooltip will only ever update once.
3316 case TTN_NEEDTEXTA:
3317 case TTN_NEEDTEXTW:
3318 {
3319 *result = 0;
3320
3321 break;
3322 }
3323
3324 #ifdef TVN_GETINFOTIP
3325 case TVN_GETINFOTIP:
3326 {
3327 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
3328 NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam;
3329
3330 // Which item are we trying to get a tooltip for?
3331 event.m_item = info->hItem;
3332
3333 break;
3334 }
3335 #endif // TVN_GETINFOTIP
3336 #endif // !__WXWINCE__
3337
3338 case TVN_GETDISPINFO:
3339 eventType = wxEVT_COMMAND_TREE_GET_INFO;
3340 // fall through
3341
3342 case TVN_SETDISPINFO:
3343 {
3344 if ( eventType == wxEVT_NULL )
3345 eventType = wxEVT_COMMAND_TREE_SET_INFO;
3346 //else: get, already set above
3347
3348 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3349
3350 event.m_item = info->item.hItem;
3351 break;
3352 }
3353
3354 case TVN_ITEMEXPANDING:
3355 case TVN_ITEMEXPANDED:
3356 {
3357 NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam;
3358
3359 int what;
3360 switch ( tv->action )
3361 {
3362 default:
3363 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
3364 // fall through
3365
3366 case TVE_EXPAND:
3367 what = IDX_EXPAND;
3368 break;
3369
3370 case TVE_COLLAPSE:
3371 what = IDX_COLLAPSE;
3372 break;
3373 }
3374
3375 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
3376 : IDX_DONE;
3377
3378 eventType = gs_expandEvents[what][how];
3379
3380 event.m_item = tv->itemNew.hItem;
3381 }
3382 break;
3383
3384 case TVN_KEYDOWN:
3385 {
3386 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
3387
3388 // fabricate the lParam and wParam parameters sufficiently
3389 // similar to the ones from a "real" WM_KEYDOWN so that
3390 // CreateKeyEvent() works correctly
3391 return MSWHandleTreeKeyDownEvent(
3392 info->wVKey, (wxIsAltDown() ? KF_ALTDOWN : 0) << 16);
3393 }
3394
3395
3396 // Vista's tree control has introduced some problems with our
3397 // multi-selection tree. When TreeView_SelectItem() is called,
3398 // the wrong items are deselected.
3399
3400 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3401 // that can be used to regulate this incorrect behaviour. The
3402 // following messages will allow only the unlocked item's selection
3403 // state to change
3404
3405 case TVN_ITEMCHANGINGA:
3406 case TVN_ITEMCHANGINGW:
3407 {
3408 // we only need to handles these in multi-select trees
3409 if ( HasFlag(wxTR_MULTIPLE) )
3410 {
3411 // get info about the item about to be changed
3412 NMTVITEMCHANGE* info = (NMTVITEMCHANGE*)lParam;
3413 if (TreeItemUnlocker::IsLocked(info->hItem))
3414 {
3415 // item's state is locked, don't allow the change
3416 // returning 1 will disallow the change
3417 *result = 1;
3418 return true;
3419 }
3420 }
3421
3422 // allow the state change
3423 }
3424 return false;
3425
3426 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3427 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3428 // we have to handle both messages:
3429 case TVN_SELCHANGEDA:
3430 case TVN_SELCHANGEDW:
3431 if ( !m_changingSelection )
3432 {
3433 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
3434 }
3435 // fall through
3436
3437 case TVN_SELCHANGINGA:
3438 case TVN_SELCHANGINGW:
3439 if ( !m_changingSelection )
3440 {
3441 if ( eventType == wxEVT_NULL )
3442 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
3443 //else: already set above
3444
3445 if (hdr->code == TVN_SELCHANGINGW ||
3446 hdr->code == TVN_SELCHANGEDW)
3447 {
3448 NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam;
3449 event.m_item = tv->itemNew.hItem;
3450 event.m_itemOld = tv->itemOld.hItem;
3451 }
3452 else
3453 {
3454 NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam;
3455 event.m_item = tv->itemNew.hItem;
3456 event.m_itemOld = tv->itemOld.hItem;
3457 }
3458 }
3459
3460 // we receive this message from WM_LBUTTONDOWN handler inside
3461 // comctl32.dll and so before the click is passed to
3462 // DefWindowProc() which sets the focus to the window which was
3463 // clicked and this can lead to unexpected event sequences: for
3464 // example, we may get a "selection change" event from the tree
3465 // before getting a "kill focus" event for the text control which
3466 // had the focus previously, thus breaking user code doing input
3467 // validation
3468 //
3469 // to avoid such surprises, we force the generation of focus events
3470 // now, before we generate the selection change ones
3471 if ( !m_changingSelection )
3472 SetFocus();
3473 break;
3474
3475 // instead of explicitly checking for _WIN32_IE, check if the
3476 // required symbols are available in the headers
3477 #if defined(CDDS_PREPAINT)
3478 case NM_CUSTOMDRAW:
3479 {
3480 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
3481 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
3482 switch ( nmcd.dwDrawStage )
3483 {
3484 case CDDS_PREPAINT:
3485 // if we've got any items with non standard attributes,
3486 // notify us before painting each item
3487 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
3488 : CDRF_DODEFAULT;
3489
3490 // windows in TreeCtrl use one-based index for item state images,
3491 // 0 indexed image is not being used, we're using zero-based index,
3492 // so we have to add temp image (of zero index) to state image list
3493 // before we draw any item, then after items are drawn we have to
3494 // delete it (in POSTPAINT notify)
3495 if (m_imageListState && m_imageListState->GetImageCount() > 0)
3496 {
3497 typedef BOOL (wxSTDCALL *ImageList_Copy_t)
3498 (HIMAGELIST, int, HIMAGELIST, int, UINT);
3499 static ImageList_Copy_t s_pfnImageList_Copy = NULL;
3500 static bool loaded = false;
3501
3502 if ( !loaded )
3503 {
3504 wxLoadedDLL dllComCtl32(wxT("comctl32.dll"));
3505 if ( dllComCtl32.IsLoaded() )
3506 wxDL_INIT_FUNC(s_pfn, ImageList_Copy, dllComCtl32);
3507 }
3508
3509 if ( !s_pfnImageList_Copy )
3510 {
3511 // this code is broken with ImageList_Copy()
3512 // but I don't care enough about Win95 support
3513 // to write it now -- if anybody does, please
3514 // do it
3515 wxFAIL_MSG("TODO: implement this for Win95");
3516 break;
3517 }
3518
3519 const HIMAGELIST
3520 hImageList = GetHimagelistOf(m_imageListState);
3521
3522 // add temporary image
3523 int width, height;
3524 m_imageListState->GetSize(0, width, height);
3525
3526 HBITMAP hbmpTemp = ::CreateBitmap(width, height, 1, 1, NULL);
3527 int index = ::ImageList_Add(hImageList, hbmpTemp, hbmpTemp);
3528 ::DeleteObject(hbmpTemp);
3529
3530 if ( index != -1 )
3531 {
3532 // move images to right
3533 for ( int i = index; i > 0; i-- )
3534 {
3535 (*s_pfnImageList_Copy)(hImageList, i,
3536 hImageList, i-1,
3537 ILCF_MOVE);
3538 }
3539
3540 // we must remove the image in POSTPAINT notify
3541 *result |= CDRF_NOTIFYPOSTPAINT;
3542 }
3543 }
3544 break;
3545
3546 case CDDS_POSTPAINT:
3547 // we are deleting temp image of 0 index, which was
3548 // added before items were drawn (in PREPAINT notify)
3549 if (m_imageListState && m_imageListState->GetImageCount() > 0)
3550 m_imageListState->Remove(0);
3551 break;
3552
3553 case CDDS_ITEMPREPAINT:
3554 {
3555 wxMapTreeAttr::iterator
3556 it = m_attrs.find((void *)nmcd.dwItemSpec);
3557
3558 if ( it == m_attrs.end() )
3559 {
3560 // nothing to do for this item
3561 *result = CDRF_DODEFAULT;
3562 break;
3563 }
3564
3565 wxTreeItemAttr * const attr = it->second;
3566
3567 wxTreeViewItem tvItem((void *)nmcd.dwItemSpec,
3568 TVIF_STATE, TVIS_DROPHILITED);
3569 DoGetItem(&tvItem);
3570 const UINT tvItemState = tvItem.state;
3571
3572 // selection colours should override ours,
3573 // otherwise it is too confusing to the user
3574 if ( !(nmcd.uItemState & CDIS_SELECTED) &&
3575 !(tvItemState & TVIS_DROPHILITED) )
3576 {
3577 wxColour colBack;
3578 if ( attr->HasBackgroundColour() )
3579 {
3580 colBack = attr->GetBackgroundColour();
3581 lptvcd->clrTextBk = wxColourToRGB(colBack);
3582 }
3583 }
3584
3585 // but we still want to keep the special foreground
3586 // colour when we don't have focus (we can't keep
3587 // it when we do, it would usually be unreadable on
3588 // the almost inverted bg colour...)
3589 if ( ( !(nmcd.uItemState & CDIS_SELECTED) ||
3590 FindFocus() != this ) &&
3591 !(tvItemState & TVIS_DROPHILITED) )
3592 {
3593 wxColour colText;
3594 if ( attr->HasTextColour() )
3595 {
3596 colText = attr->GetTextColour();
3597 lptvcd->clrText = wxColourToRGB(colText);
3598 }
3599 }
3600
3601 if ( attr->HasFont() )
3602 {
3603 HFONT hFont = GetHfontOf(attr->GetFont());
3604
3605 ::SelectObject(nmcd.hdc, hFont);
3606
3607 *result = CDRF_NEWFONT;
3608 }
3609 else // no specific font
3610 {
3611 *result = CDRF_DODEFAULT;
3612 }
3613 }
3614 break;
3615
3616 default:
3617 *result = CDRF_DODEFAULT;
3618 }
3619 }
3620
3621 // we always process it
3622 return true;
3623 #endif // have owner drawn support in headers
3624
3625 case NM_CLICK:
3626 {
3627 DWORD pos = GetMessagePos();
3628 POINT point;
3629 point.x = LOWORD(pos);
3630 point.y = HIWORD(pos);
3631 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
3632 int htFlags = 0;
3633 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), htFlags);
3634
3635 if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON )
3636 {
3637 event.m_item = item;
3638 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
3639 }
3640
3641 break;
3642 }
3643
3644 case NM_DBLCLK:
3645 case NM_RCLICK:
3646 {
3647 TV_HITTESTINFO tvhti;
3648 ::GetCursorPos(&tvhti.pt);
3649 ::ScreenToClient(GetHwnd(), &tvhti.pt);
3650 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
3651 {
3652 if ( MSWIsOnItem(tvhti.flags) )
3653 {
3654 event.m_item = tvhti.hItem;
3655 eventType = (int)hdr->code == NM_DBLCLK
3656 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3657 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
3658
3659 event.m_pointDrag.x = tvhti.pt.x;
3660 event.m_pointDrag.y = tvhti.pt.y;
3661 }
3662
3663 break;
3664 }
3665 }
3666 // fall through
3667
3668 default:
3669 return wxControl::MSWOnNotify(idCtrl, lParam, result);
3670 }
3671
3672 event.SetEventType(eventType);
3673
3674 bool processed = HandleTreeEvent(event);
3675
3676 // post processing
3677 switch ( hdr->code )
3678 {
3679 case NM_DBLCLK:
3680 // we translate NM_DBLCLK into ACTIVATED event and if the user
3681 // handled the activation of the item we shouldn't proceed with
3682 // also using the same double click for toggling the item expanded
3683 // state -- but OTOH do let the user to expand/collapse the item by
3684 // double clicking on it if the activation is not handled specially
3685 *result = processed;
3686 break;
3687
3688 case NM_RCLICK:
3689 // prevent tree control from sending WM_CONTEXTMENU to our parent
3690 // (which it does if NM_RCLICK is not handled) because we want to
3691 // send it to the control itself
3692 *result =
3693 processed = true;
3694
3695 ::SendMessage(GetHwnd(), WM_CONTEXTMENU,
3696 (WPARAM)GetHwnd(), ::GetMessagePos());
3697 break;
3698
3699 case TVN_BEGINDRAG:
3700 case TVN_BEGINRDRAG:
3701 #if wxUSE_DRAGIMAGE
3702 if ( event.IsAllowed() )
3703 {
3704 // normally this is impossible because the m_dragImage is
3705 // deleted once the drag operation is over
3706 wxASSERT_MSG( !m_dragImage, wxT("starting to drag once again?") );
3707
3708 m_dragImage = new wxDragImage(*this, event.m_item);
3709 m_dragImage->BeginDrag(wxPoint(0,0), this);
3710 m_dragImage->Show();
3711
3712 m_dragStarted = true;
3713 }
3714 #endif // wxUSE_DRAGIMAGE
3715 break;
3716
3717 case TVN_DELETEITEM:
3718 {
3719 // NB: we might process this message using wxWidgets event
3720 // tables, but due to overhead of wxWin event system we
3721 // prefer to do it here ourself (otherwise deleting a tree
3722 // with many items is just too slow)
3723 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3724
3725 wxTreeItemParam *param =
3726 (wxTreeItemParam *)tv->itemOld.lParam;
3727 delete param;
3728
3729 processed = true; // Make sure we don't get called twice
3730 }
3731 break;
3732
3733 case TVN_BEGINLABELEDIT:
3734 // return true to cancel label editing
3735 *result = !event.IsAllowed();
3736
3737 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3738 if ( event.IsAllowed() )
3739 {
3740 HWND hText = TreeView_GetEditControl(GetHwnd());
3741 if ( hText )
3742 {
3743 // MBN: if m_textCtrl already has an HWND, it is a stale
3744 // pointer from a previous edit (because the user
3745 // didn't modify the label before dismissing the control,
3746 // and TVN_ENDLABELEDIT was not sent), so delete it
3747 if ( m_textCtrl && m_textCtrl->GetHWND() )
3748 DeleteTextCtrl();
3749 if ( !m_textCtrl )
3750 m_textCtrl = new wxTextCtrl();
3751 m_textCtrl->SetParent(this);
3752 m_textCtrl->SetHWND((WXHWND)hText);
3753 m_textCtrl->SubclassWin((WXHWND)hText);
3754
3755 // set wxTE_PROCESS_ENTER style for the text control to
3756 // force it to process the Enter presses itself, otherwise
3757 // they could be stolen from it by the dialog
3758 // navigation code
3759 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
3760 | wxTE_PROCESS_ENTER);
3761 }
3762 }
3763 else // we had set m_idEdited before
3764 {
3765 m_idEdited.Unset();
3766 }
3767 break;
3768
3769 case TVN_ENDLABELEDIT:
3770 // return true to set the label to the new string: note that we
3771 // also must pretend that we did process the message or it is going
3772 // to be passed to DefWindowProc() which will happily return false
3773 // cancelling the label change
3774 *result = event.IsAllowed();
3775 processed = true;
3776
3777 // ensure that we don't have the text ctrl which is going to be
3778 // deleted any more
3779 DeleteTextCtrl();
3780 break;
3781
3782 #ifndef __WXWINCE__
3783 #ifdef TVN_GETINFOTIP
3784 case TVN_GETINFOTIP:
3785 {
3786 // If the user permitted a tooltip change, change it
3787 if (event.IsAllowed())
3788 {
3789 SetToolTip(event.m_label);
3790 }
3791 }
3792 break;
3793 #endif
3794 #endif
3795
3796 case TVN_SELCHANGING:
3797 case TVN_ITEMEXPANDING:
3798 // return true to prevent the action from happening
3799 *result = !event.IsAllowed();
3800 break;
3801
3802 case TVN_ITEMEXPANDED:
3803 {
3804 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3805 const wxTreeItemId id(tv->itemNew.hItem);
3806
3807 if ( tv->action == TVE_COLLAPSE )
3808 {
3809 if ( wxApp::GetComCtl32Version() >= 600 )
3810 {
3811 // for some reason the item selection rectangle depends
3812 // on whether it is expanded or collapsed (at least
3813 // with comctl32.dll v6): it is wider (by 3 pixels) in
3814 // the expanded state, so when the item collapses and
3815 // then is deselected the rightmost 3 pixels of the
3816 // previously drawn selection are left on the screen
3817 //
3818 // it's not clear if it's a bug in comctl32.dll or in
3819 // our code (because it does not happen in Explorer but
3820 // OTOH we don't do anything which could result in this
3821 // AFAICS) but we do need to work around it to avoid
3822 // ugly artifacts
3823 RefreshItem(id);
3824 }
3825 }
3826 else // expand
3827 {
3828 // the item is also not refreshed properly after expansion when
3829 // it has an image depending on the expanded/collapsed state:
3830 // again, it's not clear if the bug is in comctl32.dll or our
3831 // code...
3832 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
3833 if ( image != -1 )
3834 {
3835 RefreshItem(id);
3836 }
3837 }
3838 }
3839 break;
3840
3841 case TVN_GETDISPINFO:
3842 // NB: so far the user can't set the image himself anyhow, so do it
3843 // anyway - but this may change later
3844 //if ( /* !processed && */ )
3845 {
3846 wxTreeItemId item = event.m_item;
3847 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3848
3849 const wxTreeItemParam * const param = GetItemParam(item);
3850 if ( !param )
3851 break;
3852
3853 if ( info->item.mask & TVIF_IMAGE )
3854 {
3855 info->item.iImage =
3856 param->GetImage
3857 (
3858 IsExpanded(item) ? wxTreeItemIcon_Expanded
3859 : wxTreeItemIcon_Normal
3860 );
3861 }
3862 if ( info->item.mask & TVIF_SELECTEDIMAGE )
3863 {
3864 info->item.iSelectedImage =
3865 param->GetImage
3866 (
3867 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
3868 : wxTreeItemIcon_Selected
3869 );
3870 }
3871 }
3872 break;
3873
3874 //default:
3875 // for the other messages the return value is ignored and there is
3876 // nothing special to do
3877 }
3878 return processed;
3879 }
3880
3881 // ----------------------------------------------------------------------------
3882 // State control.
3883 // ----------------------------------------------------------------------------
3884
3885 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3886 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3887
3888 int wxTreeCtrl::DoGetItemState(const wxTreeItemId& item) const
3889 {
3890 wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") );
3891
3892 // receive the desired information
3893 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
3894 DoGetItem(&tvItem);
3895
3896 // state images are one-based
3897 return STATEIMAGEMASKTOINDEX(tvItem.state) - 1;
3898 }
3899
3900 void wxTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state)
3901 {
3902 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
3903
3904 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
3905
3906 // state images are one-based
3907 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3908 tvItem.state = INDEXTOSTATEIMAGEMASK(state + 1);
3909
3910 DoSetItem(&tvItem);
3911 }
3912
3913 #endif // wxUSE_TREECTRL