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