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