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