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