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