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