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