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