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