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