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