don't use WPARAM in the header (build fix after r59336)
[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 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2529 // only do it during dragging, minimize wxWin overhead (this is important for
2530 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2531 // instead of passing by wxWin events
2532 WXLRESULT
2533 wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2534 {
2535 bool processed = false;
2536 WXLRESULT rc = 0;
2537 bool isMultiple = HasFlag(wxTR_MULTIPLE);
2538
2539 if ( nMsg == WM_CONTEXTMENU )
2540 {
2541 int x = GET_X_LPARAM(lParam),
2542 y = GET_Y_LPARAM(lParam);
2543
2544 // the item for which the menu should be shown
2545 wxTreeItemId item;
2546
2547 // the position where the menu should be shown in client coordinates
2548 // (so that it can be passed directly to PopupMenu())
2549 wxPoint pt;
2550
2551 if ( x == -1 || y == -1 )
2552 {
2553 // this means that the event was generated from keyboard (e.g. with
2554 // Shift-F10 or special Windows menu key)
2555 //
2556 // use the Explorer standard of putting the menu at the left edge
2557 // of the text, in the vertical middle of the text
2558 item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2559 if ( item.IsOk() )
2560 {
2561 // Use the bounding rectangle of only the text part
2562 wxRect rect;
2563 GetBoundingRect(item, rect, true);
2564 pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2);
2565 }
2566 }
2567 else // event from mouse, use mouse position
2568 {
2569 pt = ScreenToClient(wxPoint(x, y));
2570
2571 TV_HITTESTINFO tvhti;
2572 tvhti.pt.x = pt.x;
2573 tvhti.pt.y = pt.y;
2574
2575 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2576 item = wxTreeItemId(tvhti.hItem);
2577 }
2578
2579 // create the event
2580 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
2581
2582 event.m_pointDrag = pt;
2583
2584 if ( HandleTreeEvent(event) )
2585 processed = true;
2586 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2587 }
2588 else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
2589 {
2590 // we only process mouse messages here and these parameters have the
2591 // same meaning for all of them
2592 int x = GET_X_LPARAM(lParam),
2593 y = GET_Y_LPARAM(lParam);
2594
2595 TV_HITTESTINFO tvht;
2596 tvht.pt.x = x;
2597 tvht.pt.y = y;
2598
2599 HTREEITEM htOldItem = TreeView_GetSelection(GetHwnd());
2600 HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht);
2601
2602 switch ( nMsg )
2603 {
2604 case WM_LBUTTONDOWN:
2605 if ( !isMultiple )
2606 break;
2607
2608 processed = true;
2609 m_htClickedItem.Unset();
2610
2611 if ( !(tvht.flags & TVHT_ONITEM) )
2612 {
2613 if ( !HandleMouseEvent(nMsg, x, y, wParam) )
2614 {
2615 if ( tvht.flags & TVHT_ONITEMBUTTON )
2616 {
2617 if ( !IsExpanded(htItem) )
2618 {
2619 Expand(htItem);
2620 }
2621 else
2622 {
2623 Collapse(htItem);
2624 }
2625 }
2626 }
2627
2628 break;
2629 }
2630
2631 SetFocus();
2632 m_htClickedItem = (WXHTREEITEM) htItem;
2633 m_ptClick = wxPoint(x, y);
2634
2635 if ( wParam & MK_CONTROL )
2636 {
2637 if ( HandleMouseEvent(nMsg, x, y, wParam) )
2638 {
2639 m_htClickedItem.Unset();
2640 break;
2641 }
2642
2643 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2644 this, htItem);
2645 changingEvent.m_itemOld = htOldItem;
2646
2647 if ( IsTreeEventAllowed(changingEvent) )
2648 {
2649 // toggle selected state
2650 ::ToggleItemSelection(GetHwnd(), htItem);
2651
2652 ::SetFocus(GetHwnd(), htItem);
2653
2654 // reset on any click without Shift
2655 m_htSelStart.Unset();
2656
2657 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2658 this, htItem);
2659 changedEvent.m_itemOld = htOldItem;
2660 (void)HandleTreeEvent(changedEvent);
2661 }
2662 }
2663 else if ( wParam & MK_SHIFT )
2664 {
2665 if ( HandleMouseEvent(nMsg, x, y, wParam) )
2666 {
2667 m_htClickedItem.Unset();
2668 break;
2669 }
2670
2671 int srFlags = 0;
2672 bool willChange = true;
2673
2674 if ( !(wParam & MK_CONTROL) )
2675 {
2676 srFlags |= SR_UNSELECT_OTHERS;
2677 }
2678
2679 if ( !m_htSelStart )
2680 {
2681 // take the focused item
2682 m_htSelStart = htOldItem;
2683 }
2684 else
2685 {
2686 willChange = SelectRange(GetHwnd(), HITEM(m_htSelStart),
2687 htItem, srFlags | SR_SIMULATE);
2688 }
2689
2690 if ( willChange )
2691 {
2692 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2693 this, htItem);
2694 changingEvent.m_itemOld = htOldItem;
2695
2696 if ( IsTreeEventAllowed(changingEvent) )
2697 {
2698 // this selects all items between the starting one
2699 // and the current
2700 if ( m_htSelStart )
2701 {
2702 SelectRange(GetHwnd(), HITEM(m_htSelStart),
2703 htItem, srFlags);
2704 }
2705 else
2706 {
2707 ::SelectItem(GetHwnd(), htItem);
2708 }
2709
2710 ::SetFocus(GetHwnd(), htItem);
2711
2712 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2713 this, htItem);
2714 changedEvent.m_itemOld = htOldItem;
2715 (void)HandleTreeEvent(changedEvent);
2716 }
2717 }
2718 }
2719 else // normal click
2720 {
2721 // avoid doing anything if we click on the only
2722 // currently selected item
2723
2724 wxArrayTreeItemIds selections;
2725 size_t count = GetSelections(selections);
2726
2727 if ( count == 0 ||
2728 count > 1 ||
2729 HITEM(selections[0]) != htItem )
2730 {
2731 if ( HandleMouseEvent(nMsg, x, y, wParam) )
2732 {
2733 m_htClickedItem.Unset();
2734 break;
2735 }
2736
2737 // clear the previously selected items, if the user
2738 // clicked outside of the present selection, otherwise,
2739 // perform the deselection on mouse-up, this allows
2740 // multiple drag and drop to work.
2741
2742 if ( !IsItemSelected(GetHwnd(), htItem))
2743 {
2744 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2745 this, htItem);
2746 changingEvent.m_itemOld = htOldItem;
2747
2748 if ( IsTreeEventAllowed(changingEvent) )
2749 {
2750 DoUnselectAll();
2751 ::SelectItem(GetHwnd(), htItem);
2752 ::SetFocus(GetHwnd(), htItem);
2753
2754 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2755 this, htItem);
2756 changedEvent.m_itemOld = htOldItem;
2757 (void)HandleTreeEvent(changedEvent);
2758 }
2759 }
2760 else
2761 {
2762 ::SetFocus(GetHwnd(), htItem);
2763 }
2764 }
2765 else // click on a single selected item
2766 {
2767 // don't interfere with the default processing in
2768 // WM_MOUSEMOVE handler below as the default window
2769 // proc will start the drag itself if we let have
2770 // WM_LBUTTONDOWN
2771 m_htClickedItem.Unset();
2772
2773 // prevent in-place editing from starting if focus lost
2774 // since previous click
2775 if ( m_focusLost )
2776 {
2777 TreeView_SelectItem(GetHwnd(), 0);
2778 ::SelectItem(GetHwnd(), htItem);
2779 }
2780
2781 processed = false;
2782 }
2783
2784 // reset on any click without Shift
2785 m_htSelStart.Unset();
2786 }
2787
2788 m_focusLost = false;
2789
2790 // we consumed the event so we need to trigger state image
2791 // click if needed
2792 if ( processed )
2793 {
2794 int htFlags = 0;
2795 wxTreeItemId item = HitTest(wxPoint(x, y), htFlags);
2796
2797 if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON )
2798 {
2799 m_triggerStateImageClick = true;
2800 }
2801 }
2802 break;
2803
2804 case WM_RBUTTONDOWN:
2805 if ( !isMultiple )
2806 break;
2807
2808 processed = true;
2809 SetFocus();
2810
2811 if ( HandleMouseEvent(nMsg, x, y, wParam) || !htItem )
2812 {
2813 break;
2814 }
2815
2816 // default handler removes the highlight from the currently
2817 // focused item when right mouse button is pressed on another
2818 // one but keeps the remaining items highlighted, which is
2819 // confusing, so override this default behaviour
2820 if ( !IsItemSelected(GetHwnd(), htItem) )
2821 {
2822 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2823 this, htItem);
2824 changingEvent.m_itemOld = htOldItem;
2825
2826 if ( IsTreeEventAllowed(changingEvent) )
2827 {
2828 DoUnselectAll();
2829 ::SelectItem(GetHwnd(), htItem);
2830 ::SetFocus(GetHwnd(), htItem);
2831
2832 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2833 this, htItem);
2834 changedEvent.m_itemOld = htOldItem;
2835 (void)HandleTreeEvent(changedEvent);
2836 }
2837 }
2838
2839 break;
2840
2841 case WM_MOUSEMOVE:
2842 #ifndef __WXWINCE__
2843 if ( m_htClickedItem )
2844 {
2845 int cx = abs(m_ptClick.x - x);
2846 int cy = abs(m_ptClick.y - y);
2847
2848 if ( cx > ::GetSystemMetrics(SM_CXDRAG) ||
2849 cy > ::GetSystemMetrics(SM_CYDRAG) )
2850 {
2851 NM_TREEVIEW tv;
2852 wxZeroMemory(tv);
2853
2854 tv.hdr.hwndFrom = GetHwnd();
2855 tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
2856 tv.hdr.code = TVN_BEGINDRAG;
2857
2858 tv.itemNew.hItem = HITEM(m_htClickedItem);
2859
2860
2861 TVITEM tviAux;
2862 wxZeroMemory(tviAux);
2863
2864 tviAux.hItem = HITEM(m_htClickedItem);
2865 tviAux.mask = TVIF_STATE | TVIF_PARAM;
2866 tviAux.stateMask = 0xffffffff;
2867 TreeView_GetItem(GetHwnd(), &tviAux);
2868
2869 tv.itemNew.state = tviAux.state;
2870 tv.itemNew.lParam = tviAux.lParam;
2871
2872 tv.ptDrag.x = x;
2873 tv.ptDrag.y = y;
2874
2875 // do it before SendMessage() call below to avoid
2876 // reentrancies here if there is another WM_MOUSEMOVE
2877 // in the queue already
2878 m_htClickedItem.Unset();
2879
2880 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY,
2881 tv.hdr.idFrom, (LPARAM)&tv );
2882
2883 // don't pass it to the default window proc, it would
2884 // start dragging again
2885 processed = true;
2886 }
2887 }
2888 #endif // __WXWINCE__
2889
2890 #if wxUSE_DRAGIMAGE
2891 if ( m_dragImage )
2892 {
2893 m_dragImage->Move(wxPoint(x, y));
2894 if ( htItem )
2895 {
2896 // highlight the item as target (hiding drag image is
2897 // necessary - otherwise the display will be corrupted)
2898 m_dragImage->Hide();
2899 TreeView_SelectDropTarget(GetHwnd(), htItem);
2900 m_dragImage->Show();
2901 }
2902 }
2903 #endif // wxUSE_DRAGIMAGE
2904 break;
2905
2906 case WM_LBUTTONUP:
2907 if ( isMultiple )
2908 {
2909 // deselect other items if multiple items selected
2910 if ( htItem )
2911 {
2912 wxArrayTreeItemIds selections;
2913 size_t count = GetSelections(selections);
2914
2915 if ( count > 1 &&
2916 !(wParam & MK_CONTROL) &&
2917 !(wParam & MK_SHIFT) )
2918 {
2919 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2920 this, htItem);
2921 changingEvent.m_itemOld = htOldItem;
2922
2923 if ( IsTreeEventAllowed(changingEvent) )
2924 {
2925 DoUnselectAll();
2926 ::SelectItem(GetHwnd(), htItem);
2927 ::SetFocus(GetHwnd(), htItem);
2928
2929 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2930 this, htItem);
2931 changedEvent.m_itemOld = htOldItem;
2932 (void)HandleTreeEvent(changedEvent);
2933 }
2934 }
2935 }
2936
2937 m_htClickedItem.Unset();
2938
2939 if ( m_triggerStateImageClick )
2940 {
2941 if ( tvht.flags & TVHT_ONITEMSTATEICON )
2942 {
2943 wxTreeEvent event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK,
2944 this, htItem);
2945 (void)HandleTreeEvent(event);
2946
2947 m_triggerStateImageClick = false;
2948 processed = true;
2949 }
2950 }
2951
2952 if ( !m_dragStarted &&
2953 (tvht.flags & TVHT_ONITEMSTATEICON ||
2954 tvht.flags & TVHT_ONITEMICON ||
2955 tvht.flags & TVHT_ONITEM) )
2956 {
2957 processed = true;
2958 }
2959 }
2960
2961 // fall through
2962
2963 case WM_RBUTTONUP:
2964 #if wxUSE_DRAGIMAGE
2965 if ( m_dragImage )
2966 {
2967 m_dragImage->EndDrag();
2968 delete m_dragImage;
2969 m_dragImage = NULL;
2970
2971 // generate the drag end event
2972 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG,
2973 this, htItem);
2974 event.m_pointDrag = wxPoint(x, y);
2975 (void)HandleTreeEvent(event);
2976
2977 // if we don't do it, the tree seems to think that 2 items
2978 // are selected simultaneously which is quite weird
2979 TreeView_SelectDropTarget(GetHwnd(), 0);
2980 }
2981 #endif // wxUSE_DRAGIMAGE
2982
2983 if ( isMultiple && nMsg == WM_RBUTTONUP )
2984 {
2985 // send NM_RCLICK
2986 NMHDR nmhdr;
2987 nmhdr.hwndFrom = GetHwnd();
2988 nmhdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
2989 nmhdr.code = NM_RCLICK;
2990 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY,
2991 nmhdr.idFrom, (LPARAM)&nmhdr);
2992 processed = true;
2993 }
2994
2995 m_dragStarted = false;
2996
2997 break;
2998 }
2999 }
3000 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) )
3001 {
3002 if ( isMultiple )
3003 {
3004 // the tree control greys out the selected item when it loses focus
3005 // and paints it as selected again when it regains it, but it won't
3006 // do it for the other items itself - help it
3007 wxArrayTreeItemIds selections;
3008 size_t count = GetSelections(selections);
3009 RECT rect;
3010
3011 for ( size_t n = 0; n < count; n++ )
3012 {
3013 // TreeView_GetItemRect() will return false if item is not
3014 // visible, which may happen perfectly well
3015 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
3016 &rect, TRUE) )
3017 {
3018 ::InvalidateRect(GetHwnd(), &rect, FALSE);
3019 }
3020 }
3021 }
3022
3023 if ( nMsg == WM_KILLFOCUS )
3024 {
3025 m_focusLost = true;
3026 }
3027 }
3028 else if ( (nMsg == WM_KEYDOWN || nMsg == WM_SYSKEYDOWN) && isMultiple )
3029 {
3030 wxTreeEvent keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN, this);
3031
3032 int keyCode = wxCharCodeMSWToWX(wParam);
3033
3034 if ( !keyCode )
3035 {
3036 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
3037 // simple ASCII key
3038 keyCode = wParam;
3039 }
3040
3041 keyEvent.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, keyCode,
3042 lParam, wParam);
3043
3044 processed = HandleTreeEvent(keyEvent);
3045 if ( !processed )
3046 {
3047 // update the selection if key was left unprocessed
3048 processed = MSWHandleSelectionKey(wParam);
3049 }
3050
3051 // generate a separate event for Space/Return
3052 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
3053 ((wParam == VK_SPACE) || (wParam == VK_RETURN)) )
3054 {
3055 const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
3056 if ( htSel )
3057 {
3058 wxTreeEvent activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
3059 this, htSel);
3060 (void)HandleTreeEvent(activatedEvent);
3061 }
3062 }
3063 }
3064 else if ( nMsg == WM_COMMAND )
3065 {
3066 // if we receive a EN_KILLFOCUS command from the in-place edit control
3067 // used for label editing, make sure to end editing
3068 WORD id, cmd;
3069 WXHWND hwnd;
3070 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
3071
3072 if ( cmd == EN_KILLFOCUS )
3073 {
3074 if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd )
3075 {
3076 DoEndEditLabel();
3077
3078 processed = true;
3079 }
3080 }
3081 }
3082
3083 if ( !processed )
3084 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
3085
3086 return rc;
3087 }
3088
3089 WXLRESULT
3090 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
3091 {
3092 if ( nMsg == WM_CHAR )
3093 {
3094 // don't let the control process Space and Return keys because it
3095 // doesn't do anything useful with them anyhow but always beeps
3096 // annoyingly when it receives them and there is no way to turn it off
3097 // simply if you just process TREEITEM_ACTIVATED event to which Space
3098 // and Enter presses are mapped in your code
3099 if ( wParam == VK_SPACE || wParam == VK_RETURN )
3100 return 0;
3101 }
3102 #if wxUSE_DRAGIMAGE
3103 else if ( nMsg == WM_KEYDOWN )
3104 {
3105 if ( wParam == VK_ESCAPE )
3106 {
3107 if ( m_dragImage )
3108 {
3109 m_dragImage->EndDrag();
3110 delete m_dragImage;
3111 m_dragImage = NULL;
3112
3113 // if we don't do it, the tree seems to think that 2 items
3114 // are selected simultaneously which is quite weird
3115 TreeView_SelectDropTarget(GetHwnd(), 0);
3116 }
3117 }
3118 }
3119 #endif // wxUSE_DRAGIMAGE
3120
3121 return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);
3122 }
3123
3124 // process WM_NOTIFY Windows message
3125 bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
3126 {
3127 wxTreeEvent event(wxEVT_NULL, this);
3128 wxEventType eventType = wxEVT_NULL;
3129 NMHDR *hdr = (NMHDR *)lParam;
3130
3131 switch ( hdr->code )
3132 {
3133 case TVN_BEGINDRAG:
3134 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
3135 // fall through
3136
3137 case TVN_BEGINRDRAG:
3138 {
3139 if ( eventType == wxEVT_NULL )
3140 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
3141 //else: left drag, already set above
3142
3143 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3144
3145 event.m_item = tv->itemNew.hItem;
3146 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
3147
3148 // don't allow dragging by default: the user code must
3149 // explicitly say that it wants to allow it to avoid breaking
3150 // the old apps
3151 event.Veto();
3152 }
3153 break;
3154
3155 case TVN_BEGINLABELEDIT:
3156 {
3157 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
3158 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3159
3160 // although the user event handler may still veto it, it is
3161 // important to set it now so that calls to SetItemText() from
3162 // the event handler would change the text controls contents
3163 m_idEdited =
3164 event.m_item = info->item.hItem;
3165 event.m_label = info->item.pszText;
3166 event.m_editCancelled = false;
3167 }
3168 break;
3169
3170 case TVN_DELETEITEM:
3171 {
3172 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
3173 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3174
3175 event.m_item = tv->itemOld.hItem;
3176
3177 if ( m_hasAnyAttr )
3178 {
3179 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
3180 if ( it != m_attrs.end() )
3181 {
3182 delete it->second;
3183 m_attrs.erase(it);
3184 }
3185 }
3186 }
3187 break;
3188
3189 case TVN_ENDLABELEDIT:
3190 {
3191 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
3192 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3193
3194 event.m_item = info->item.hItem;
3195 event.m_label = info->item.pszText;
3196 event.m_editCancelled = info->item.pszText == NULL;
3197 break;
3198 }
3199
3200 #ifndef __WXWINCE__
3201 // These *must* not be removed or TVN_GETINFOTIP will
3202 // not be processed each time the mouse is moved
3203 // and the tooltip will only ever update once.
3204 case TTN_NEEDTEXTA:
3205 case TTN_NEEDTEXTW:
3206 {
3207 *result = 0;
3208
3209 break;
3210 }
3211
3212 #ifdef TVN_GETINFOTIP
3213 case TVN_GETINFOTIP:
3214 {
3215 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
3216 NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam;
3217
3218 // Which item are we trying to get a tooltip for?
3219 event.m_item = info->hItem;
3220
3221 break;
3222 }
3223 #endif // TVN_GETINFOTIP
3224 #endif // !__WXWINCE__
3225
3226 case TVN_GETDISPINFO:
3227 eventType = wxEVT_COMMAND_TREE_GET_INFO;
3228 // fall through
3229
3230 case TVN_SETDISPINFO:
3231 {
3232 if ( eventType == wxEVT_NULL )
3233 eventType = wxEVT_COMMAND_TREE_SET_INFO;
3234 //else: get, already set above
3235
3236 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3237
3238 event.m_item = info->item.hItem;
3239 break;
3240 }
3241
3242 case TVN_ITEMEXPANDING:
3243 case TVN_ITEMEXPANDED:
3244 {
3245 NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam;
3246
3247 int what;
3248 switch ( tv->action )
3249 {
3250 default:
3251 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
3252 // fall through
3253
3254 case TVE_EXPAND:
3255 what = IDX_EXPAND;
3256 break;
3257
3258 case TVE_COLLAPSE:
3259 what = IDX_COLLAPSE;
3260 break;
3261 }
3262
3263 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
3264 : IDX_DONE;
3265
3266 eventType = gs_expandEvents[what][how];
3267
3268 event.m_item = tv->itemNew.hItem;
3269 }
3270 break;
3271
3272 case TVN_KEYDOWN:
3273 {
3274 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
3275
3276 // fabricate the lParam and wParam parameters sufficiently
3277 // similar to the ones from a "real" WM_KEYDOWN so that
3278 // CreateKeyEvent() works correctly
3279 const bool isAltDown = ::GetKeyState(VK_MENU) < 0;
3280 WXLPARAM lParam = (isAltDown ? KF_ALTDOWN : 0) << 16;
3281
3282 WXWPARAM wParam = info->wVKey;
3283
3284 int keyCode = wxCharCodeMSWToWX(wParam);
3285 if ( !keyCode )
3286 {
3287 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
3288 // simple ASCII key
3289 keyCode = wParam;
3290 }
3291
3292 wxTreeEvent keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN, this);
3293 keyEvent.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
3294 keyCode,
3295 lParam,
3296 wParam);
3297
3298 if ( HandleTreeEvent(keyEvent) )
3299 {
3300 return true;
3301 }
3302
3303 wxTreeItemId item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
3304
3305 // a separate event for Space/Return
3306 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown &&
3307 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) &&
3308 item )
3309 {
3310 wxTreeEvent activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
3311 this, item);
3312 (void)HandleTreeEvent(activatedEvent);
3313 }
3314
3315 return false;
3316 }
3317 break;
3318
3319
3320 // Vista's tree control has introduced some problems with our
3321 // multi-selection tree. When TreeView_SelectItem() is called,
3322 // the wrong items are deselected.
3323
3324 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3325 // that can be used to regulate this incorrect behavior. The
3326 // following messages will allow only the unlocked item's selection
3327 // state to change
3328
3329 case TVN_ITEMCHANGINGA:
3330 case TVN_ITEMCHANGINGW:
3331 {
3332 // we only need to handles these in multi-select trees
3333 if ( HasFlag(wxTR_MULTIPLE) )
3334 {
3335 // get info about the item about to be changed
3336 NMTVITEMCHANGE* info = (NMTVITEMCHANGE*)lParam;
3337 if (TreeItemUnlocker::IsLocked(info->hItem))
3338 {
3339 // item's state is locked, don't allow the change
3340 // returning 1 will disallow the change
3341 *result = 1;
3342 return true;
3343 }
3344 }
3345
3346 // allow the state change
3347 }
3348 return false;
3349
3350 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
3351 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3352 // we have to handle both messages:
3353 case TVN_SELCHANGEDA:
3354 case TVN_SELCHANGEDW:
3355 if ( !HasFlag(wxTR_MULTIPLE) )
3356 {
3357 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
3358 }
3359 // fall through
3360
3361 case TVN_SELCHANGINGA:
3362 case TVN_SELCHANGINGW:
3363 if ( !HasFlag(wxTR_MULTIPLE) )
3364 {
3365 if ( eventType == wxEVT_NULL )
3366 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
3367 //else: already set above
3368
3369 if (hdr->code == TVN_SELCHANGINGW ||
3370 hdr->code == TVN_SELCHANGEDW)
3371 {
3372 NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam;
3373 event.m_item = tv->itemNew.hItem;
3374 event.m_itemOld = tv->itemOld.hItem;
3375 }
3376 else
3377 {
3378 NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam;
3379 event.m_item = tv->itemNew.hItem;
3380 event.m_itemOld = tv->itemOld.hItem;
3381 }
3382 }
3383
3384 // we receive this message from WM_LBUTTONDOWN handler inside
3385 // comctl32.dll and so before the click is passed to
3386 // DefWindowProc() which sets the focus to the window which was
3387 // clicked and this can lead to unexpected event sequences: for
3388 // example, we may get a "selection change" event from the tree
3389 // before getting a "kill focus" event for the text control which
3390 // had the focus previously, thus breaking user code doing input
3391 // validation
3392 //
3393 // to avoid such surprises, we force the generation of focus events
3394 // now, before we generate the selection change ones
3395 SetFocus();
3396 break;
3397
3398 // instead of explicitly checking for _WIN32_IE, check if the
3399 // required symbols are available in the headers
3400 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
3401 case NM_CUSTOMDRAW:
3402 {
3403 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
3404 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
3405 switch ( nmcd.dwDrawStage )
3406 {
3407 case CDDS_PREPAINT:
3408 // if we've got any items with non standard attributes,
3409 // notify us before painting each item
3410 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
3411 : CDRF_DODEFAULT;
3412
3413 // windows in TreeCtrl use one-based index for item state images,
3414 // 0 indexed image is not being used, we're using zero-based index,
3415 // so we have to add temp image (of zero index) to state image list
3416 // before we draw any item, then after items are drawn we have to
3417 // delete it (in POSTPAINT notify)
3418 if (m_imageListState && m_imageListState->GetImageCount() > 0)
3419 {
3420 typedef BOOL (wxSTDCALL *ImageList_Copy_t)
3421 (HIMAGELIST, int, HIMAGELIST, int, UINT);
3422 static ImageList_Copy_t s_pfnImageList_Copy = NULL;
3423 static bool loaded = false;
3424
3425 if ( !loaded )
3426 {
3427 wxLoadedDLL dllComCtl32(_T("comctl32.dll"));
3428 if ( dllComCtl32.IsLoaded() )
3429 wxDL_INIT_FUNC(s_pfn, ImageList_Copy, dllComCtl32);
3430 }
3431
3432 if ( !s_pfnImageList_Copy )
3433 {
3434 // this code is broken with ImageList_Copy()
3435 // but I don't care enough about Win95 support
3436 // to write it now -- if anybody does, please
3437 // do it
3438 wxFAIL_MSG("TODO: implement this for Win95");
3439 break;
3440 }
3441
3442 const HIMAGELIST
3443 hImageList = GetHimagelistOf(m_imageListState);
3444
3445 // add temporary image
3446 int width, height;
3447 m_imageListState->GetSize(0, width, height);
3448
3449 HBITMAP hbmpTemp = ::CreateBitmap(width, height, 1, 1, NULL);
3450 int index = ::ImageList_Add(hImageList, hbmpTemp, hbmpTemp);
3451 ::DeleteObject(hbmpTemp);
3452
3453 if ( index != -1 )
3454 {
3455 // move images to right
3456 for ( int i = index; i > 0; i-- )
3457 {
3458 (*s_pfnImageList_Copy)(hImageList, i,
3459 hImageList, i-1,
3460 ILCF_MOVE);
3461 }
3462
3463 // we must remove the image in POSTPAINT notify
3464 *result |= CDRF_NOTIFYPOSTPAINT;
3465 }
3466 }
3467 break;
3468
3469 case CDDS_POSTPAINT:
3470 // we are deleting temp image of 0 index, which was
3471 // added before items were drawn (in PREPAINT notify)
3472 if (m_imageListState && m_imageListState->GetImageCount() > 0)
3473 m_imageListState->Remove(0);
3474 break;
3475
3476 case CDDS_ITEMPREPAINT:
3477 {
3478 wxMapTreeAttr::iterator
3479 it = m_attrs.find((void *)nmcd.dwItemSpec);
3480
3481 if ( it == m_attrs.end() )
3482 {
3483 // nothing to do for this item
3484 *result = CDRF_DODEFAULT;
3485 break;
3486 }
3487
3488 wxTreeItemAttr * const attr = it->second;
3489
3490 wxTreeViewItem tvItem((void *)nmcd.dwItemSpec,
3491 TVIF_STATE, TVIS_DROPHILITED);
3492 DoGetItem(&tvItem);
3493 const UINT tvItemState = tvItem.state;
3494
3495 // selection colours should override ours,
3496 // otherwise it is too confusing to the user
3497 if ( !(nmcd.uItemState & CDIS_SELECTED) &&
3498 !(tvItemState & TVIS_DROPHILITED) )
3499 {
3500 wxColour colBack;
3501 if ( attr->HasBackgroundColour() )
3502 {
3503 colBack = attr->GetBackgroundColour();
3504 lptvcd->clrTextBk = wxColourToRGB(colBack);
3505 }
3506 }
3507
3508 // but we still want to keep the special foreground
3509 // colour when we don't have focus (we can't keep
3510 // it when we do, it would usually be unreadable on
3511 // the almost inverted bg colour...)
3512 if ( ( !(nmcd.uItemState & CDIS_SELECTED) ||
3513 FindFocus() != this ) &&
3514 !(tvItemState & TVIS_DROPHILITED) )
3515 {
3516 wxColour colText;
3517 if ( attr->HasTextColour() )
3518 {
3519 colText = attr->GetTextColour();
3520 lptvcd->clrText = wxColourToRGB(colText);
3521 }
3522 }
3523
3524 if ( attr->HasFont() )
3525 {
3526 HFONT hFont = GetHfontOf(attr->GetFont());
3527
3528 ::SelectObject(nmcd.hdc, hFont);
3529
3530 *result = CDRF_NEWFONT;
3531 }
3532 else // no specific font
3533 {
3534 *result = CDRF_DODEFAULT;
3535 }
3536 }
3537 break;
3538
3539 default:
3540 *result = CDRF_DODEFAULT;
3541 }
3542 }
3543
3544 // we always process it
3545 return true;
3546 #endif // have owner drawn support in headers
3547
3548 case NM_CLICK:
3549 {
3550 DWORD pos = GetMessagePos();
3551 POINT point;
3552 point.x = LOWORD(pos);
3553 point.y = HIWORD(pos);
3554 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
3555 int htFlags = 0;
3556 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), htFlags);
3557
3558 if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON )
3559 {
3560 event.m_item = item;
3561 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
3562 }
3563
3564 break;
3565 }
3566
3567 case NM_DBLCLK:
3568 case NM_RCLICK:
3569 {
3570 TV_HITTESTINFO tvhti;
3571 ::GetCursorPos(&tvhti.pt);
3572 ::ScreenToClient(GetHwnd(), &tvhti.pt);
3573 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
3574 {
3575 if ( tvhti.flags & TVHT_ONITEM )
3576 {
3577 event.m_item = tvhti.hItem;
3578 eventType = (int)hdr->code == NM_DBLCLK
3579 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3580 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
3581
3582 event.m_pointDrag.x = tvhti.pt.x;
3583 event.m_pointDrag.y = tvhti.pt.y;
3584 }
3585
3586 break;
3587 }
3588 }
3589 // fall through
3590
3591 default:
3592 return wxControl::MSWOnNotify(idCtrl, lParam, result);
3593 }
3594
3595 event.SetEventType(eventType);
3596
3597 bool processed = HandleTreeEvent(event);
3598
3599 // post processing
3600 switch ( hdr->code )
3601 {
3602 case NM_DBLCLK:
3603 // we translate NM_DBLCLK into ACTIVATED event and if the user
3604 // handled the activation of the item we shouldn't proceed with
3605 // also using the same double click for toggling the item expanded
3606 // state -- but OTOH do let the user to expand/collapse the item by
3607 // double clicking on it if the activation is not handled specially
3608 *result = processed;
3609 break;
3610
3611 case NM_RCLICK:
3612 // prevent tree control from sending WM_CONTEXTMENU to our parent
3613 // (which it does if NM_RCLICK is not handled) because we want to
3614 // send it to the control itself
3615 *result =
3616 processed = true;
3617
3618 ::SendMessage(GetHwnd(), WM_CONTEXTMENU,
3619 (WPARAM)GetHwnd(), ::GetMessagePos());
3620 break;
3621
3622 case TVN_BEGINDRAG:
3623 case TVN_BEGINRDRAG:
3624 #if wxUSE_DRAGIMAGE
3625 if ( event.IsAllowed() )
3626 {
3627 // normally this is impossible because the m_dragImage is
3628 // deleted once the drag operation is over
3629 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
3630
3631 m_dragImage = new wxDragImage(*this, event.m_item);
3632 m_dragImage->BeginDrag(wxPoint(0,0), this);
3633 m_dragImage->Show();
3634
3635 m_dragStarted = true;
3636 }
3637 #endif // wxUSE_DRAGIMAGE
3638 break;
3639
3640 case TVN_DELETEITEM:
3641 {
3642 // NB: we might process this message using wxWidgets event
3643 // tables, but due to overhead of wxWin event system we
3644 // prefer to do it here ourself (otherwise deleting a tree
3645 // with many items is just too slow)
3646 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3647
3648 wxTreeItemParam *param =
3649 (wxTreeItemParam *)tv->itemOld.lParam;
3650 delete param;
3651
3652 processed = true; // Make sure we don't get called twice
3653 }
3654 break;
3655
3656 case TVN_BEGINLABELEDIT:
3657 // return true to cancel label editing
3658 *result = !event.IsAllowed();
3659
3660 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3661 if ( event.IsAllowed() )
3662 {
3663 HWND hText = TreeView_GetEditControl(GetHwnd());
3664 if ( hText )
3665 {
3666 // MBN: if m_textCtrl already has an HWND, it is a stale
3667 // pointer from a previous edit (because the user
3668 // didn't modify the label before dismissing the control,
3669 // and TVN_ENDLABELEDIT was not sent), so delete it
3670 if ( m_textCtrl && m_textCtrl->GetHWND() )
3671 DeleteTextCtrl();
3672 if ( !m_textCtrl )
3673 m_textCtrl = new wxTextCtrl();
3674 m_textCtrl->SetParent(this);
3675 m_textCtrl->SetHWND((WXHWND)hText);
3676 m_textCtrl->SubclassWin((WXHWND)hText);
3677
3678 // set wxTE_PROCESS_ENTER style for the text control to
3679 // force it to process the Enter presses itself, otherwise
3680 // they could be stolen from it by the dialog
3681 // navigation code
3682 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
3683 | wxTE_PROCESS_ENTER);
3684 }
3685 }
3686 else // we had set m_idEdited before
3687 {
3688 m_idEdited.Unset();
3689 }
3690 break;
3691
3692 case TVN_ENDLABELEDIT:
3693 // return true to set the label to the new string: note that we
3694 // also must pretend that we did process the message or it is going
3695 // to be passed to DefWindowProc() which will happily return false
3696 // cancelling the label change
3697 *result = event.IsAllowed();
3698 processed = true;
3699
3700 // ensure that we don't have the text ctrl which is going to be
3701 // deleted any more
3702 DeleteTextCtrl();
3703 break;
3704
3705 #ifndef __WXWINCE__
3706 #ifdef TVN_GETINFOTIP
3707 case TVN_GETINFOTIP:
3708 {
3709 // If the user permitted a tooltip change, change it
3710 if (event.IsAllowed())
3711 {
3712 SetToolTip(event.m_label);
3713 }
3714 }
3715 break;
3716 #endif
3717 #endif
3718
3719 case TVN_SELCHANGING:
3720 case TVN_ITEMEXPANDING:
3721 // return true to prevent the action from happening
3722 *result = !event.IsAllowed();
3723 break;
3724
3725 case TVN_ITEMEXPANDED:
3726 {
3727 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3728 const wxTreeItemId id(tv->itemNew.hItem);
3729
3730 if ( tv->action == TVE_COLLAPSE )
3731 {
3732 if ( wxApp::GetComCtl32Version() >= 600 )
3733 {
3734 // for some reason the item selection rectangle depends
3735 // on whether it is expanded or collapsed (at least
3736 // with comctl32.dll v6): it is wider (by 3 pixels) in
3737 // the expanded state, so when the item collapses and
3738 // then is deselected the rightmost 3 pixels of the
3739 // previously drawn selection are left on the screen
3740 //
3741 // it's not clear if it's a bug in comctl32.dll or in
3742 // our code (because it does not happen in Explorer but
3743 // OTOH we don't do anything which could result in this
3744 // AFAICS) but we do need to work around it to avoid
3745 // ugly artifacts
3746 RefreshItem(id);
3747 }
3748 }
3749 else // expand
3750 {
3751 // the item is also not refreshed properly after expansion when
3752 // it has an image depending on the expanded/collapsed state:
3753 // again, it's not clear if the bug is in comctl32.dll or our
3754 // code...
3755 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
3756 if ( image != -1 )
3757 {
3758 RefreshItem(id);
3759 }
3760 }
3761 }
3762 break;
3763
3764 case TVN_GETDISPINFO:
3765 // NB: so far the user can't set the image himself anyhow, so do it
3766 // anyway - but this may change later
3767 //if ( /* !processed && */ )
3768 {
3769 wxTreeItemId item = event.m_item;
3770 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3771
3772 const wxTreeItemParam * const param = GetItemParam(item);
3773 if ( !param )
3774 break;
3775
3776 if ( info->item.mask & TVIF_IMAGE )
3777 {
3778 info->item.iImage =
3779 param->GetImage
3780 (
3781 IsExpanded(item) ? wxTreeItemIcon_Expanded
3782 : wxTreeItemIcon_Normal
3783 );
3784 }
3785 if ( info->item.mask & TVIF_SELECTEDIMAGE )
3786 {
3787 info->item.iSelectedImage =
3788 param->GetImage
3789 (
3790 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
3791 : wxTreeItemIcon_Selected
3792 );
3793 }
3794 }
3795 break;
3796
3797 //default:
3798 // for the other messages the return value is ignored and there is
3799 // nothing special to do
3800 }
3801 return processed;
3802 }
3803
3804 // ----------------------------------------------------------------------------
3805 // State control.
3806 // ----------------------------------------------------------------------------
3807
3808 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3809 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3810
3811 int wxTreeCtrl::DoGetItemState(const wxTreeItemId& item) const
3812 {
3813 wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") );
3814
3815 // receive the desired information
3816 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
3817 DoGetItem(&tvItem);
3818
3819 // state images are one-based
3820 return STATEIMAGEMASKTOINDEX(tvItem.state) - 1;
3821 }
3822
3823 void wxTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state)
3824 {
3825 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
3826
3827 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
3828
3829 // state images are one-based
3830 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3831 tvItem.state = INDEXTOSTATEIMAGEMASK(state + 1);
3832
3833 DoSetItem(&tvItem);
3834 }
3835
3836 #endif // wxUSE_TREECTRL