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