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