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