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