]> git.saurik.com Git - wxWidgets.git/blame - src/msw/treectrl.cpp
New eventb based Drag interface for wxDataViewCtrl
[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
d220ae32 800 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
2bda0e17 801 {
f6bcfd97 802 wxLogLastError(wxT("TreeView_GetItem"));
08b7c251 803
04cd30de 804 return false;
08b7c251
VZ
805 }
806
04cd30de 807 return true;
2bda0e17
KB
808}
809
4523ebb3 810void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem)
2bda0e17 811{
5fe302ef
BW
812 TreeItemUnlocker unlocker(tvItem->hItem);
813
d220ae32 814 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
2bda0e17 815 {
f6bcfd97 816 wxLogLastError(wxT("TreeView_SetItem"));
08b7c251 817 }
2bda0e17
KB
818}
819
027d45e8 820unsigned int wxTreeCtrl::GetCount() const
2bda0e17 821{
027d45e8 822 return (unsigned int)TreeView_GetCount(GetHwnd());
2bda0e17
KB
823}
824
08b7c251 825unsigned int wxTreeCtrl::GetIndent() const
2bda0e17 826{
d220ae32 827 return TreeView_GetIndent(GetHwnd());
2bda0e17
KB
828}
829
08b7c251 830void wxTreeCtrl::SetIndent(unsigned int indent)
2bda0e17 831{
d220ae32 832 TreeView_SetIndent(GetHwnd(), indent);
2bda0e17
KB
833}
834
08b7c251 835void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
2bda0e17 836{
08b7c251 837 // no error return
8ecd5de2
WS
838 (void) TreeView_SetImageList(GetHwnd(),
839 imageList ? imageList->GetHIMAGELIST() : 0,
840 which);
2bda0e17
KB
841}
842
08b7c251 843void wxTreeCtrl::SetImageList(wxImageList *imageList)
2bda0e17 844{
a9c1265f
VZ
845 if (m_ownsImageListNormal)
846 delete m_imageListNormal;
847
08b7c251 848 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
04cd30de 849 m_ownsImageListNormal = false;
2bda0e17
KB
850}
851
08b7c251 852void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
2bda0e17 853{
0377efa2 854 if (m_ownsImageListState) delete m_imageListState;
08b7c251 855 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
04cd30de 856 m_ownsImageListState = false;
0377efa2
VS
857}
858
33961d59
RR
859size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
860 bool recursively) const
861{
05b2a432 862 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
23fd5130 863
05b2a432 864 TraverseCounter counter(this, item, recursively);
73974df1 865 return counter.GetCount() - 1;
23fd5130
VZ
866}
867
bb448552
VZ
868// ----------------------------------------------------------------------------
869// control colours
870// ----------------------------------------------------------------------------
871
872bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
873{
f6bcfd97 874#if !wxUSE_COMCTL32_SAFELY
bb448552 875 if ( !wxWindowBase::SetBackgroundColour(colour) )
04cd30de 876 return false;
bb448552 877
bfbb0b4c 878 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
f6bcfd97 879#endif
bb448552 880
04cd30de 881 return true;
bb448552
VZ
882}
883
884bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
885{
f6bcfd97 886#if !wxUSE_COMCTL32_SAFELY
bb448552 887 if ( !wxWindowBase::SetForegroundColour(colour) )
04cd30de 888 return false;
bb448552 889
bfbb0b4c 890 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
f6bcfd97 891#endif
bb448552 892
04cd30de 893 return true;
bb448552
VZ
894}
895
08b7c251
VZ
896// ----------------------------------------------------------------------------
897// Item access
898// ----------------------------------------------------------------------------
899
0228081f
VZ
900bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const
901{
902 return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT);
903}
904
08b7c251 905wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
2bda0e17 906{
f31a4098 907 wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
05b2a432 908
837e5743 909 wxChar buf[512]; // the size is arbitrary...
02ce7b72 910
08b7c251
VZ
911 wxTreeViewItem tvItem(item, TVIF_TEXT);
912 tvItem.pszText = buf;
913 tvItem.cchTextMax = WXSIZEOF(buf);
914 if ( !DoGetItem(&tvItem) )
915 {
916 // don't return some garbage which was on stack, but an empty string
223d09f6 917 buf[0] = wxT('\0');
08b7c251 918 }
2bda0e17 919
08b7c251
VZ
920 return wxString(buf);
921}
2bda0e17 922
08b7c251
VZ
923void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
924{
05b2a432
RD
925 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
926
a9c1265f
VZ
927 if ( IS_VIRTUAL_ROOT(item) )
928 return;
929
08b7c251 930 wxTreeViewItem tvItem(item, TVIF_TEXT);
c9f78968 931 tvItem.pszText = (wxChar *)text.wx_str(); // conversion is ok
08b7c251 932 DoSetItem(&tvItem);
44fbc477
VZ
933
934 // when setting the text of the item being edited, the text control should
935 // be updated to reflect the new text as well, otherwise calling
936 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
937 //
938 // don't use GetEditControl() here because m_textCtrl is not set yet
939 HWND hwndEdit = TreeView_GetEditControl(GetHwnd());
940 if ( hwndEdit )
941 {
1a4088e1 942 if ( item == m_idEdited )
44fbc477 943 {
e0a050e3 944 ::SetWindowText(hwndEdit, text.wx_str());
44fbc477
VZ
945 }
946 }
08b7c251 947}
2bda0e17 948
4523ebb3
VZ
949int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
950 wxTreeItemIcon which) const
74b31181 951{
4523ebb3
VZ
952 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
953
0228081f 954 if ( IsHiddenRoot(item) )
74b31181 955 {
4523ebb3 956 // no images for hidden root item
74b31181
VZ
957 return -1;
958 }
959
4523ebb3
VZ
960 wxTreeItemParam *param = GetItemParam(item);
961
1b182562 962 return param && param->HasImage(which) ? param->GetImage(which) : -1;
74b31181
VZ
963}
964
4523ebb3
VZ
965void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
966 wxTreeItemIcon which)
74b31181 967{
4523ebb3
VZ
968 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
969 wxCHECK_RET( which >= 0 &&
970 which < wxTreeItemIcon_Max,
971 wxT("invalid image index"));
972
973
0228081f 974 if ( IsHiddenRoot(item) )
74b31181 975 {
4523ebb3 976 // no images for hidden root item
74b31181
VZ
977 return;
978 }
979
4523ebb3
VZ
980 wxTreeItemParam *data = GetItemParam(item);
981 if ( !data )
982 return;
74b31181
VZ
983
984 data->SetImage(image, which);
f5bed7a8
VS
985
986 RefreshItem(item);
74b31181
VZ
987}
988
4523ebb3 989wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const
2bda0e17 990{
05b2a432
RD
991 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
992
08b7c251 993 wxTreeViewItem tvItem(item, TVIF_PARAM);
a9c1265f 994
ae322a48
VZ
995 // hidden root may still have data.
996 if ( IS_VIRTUAL_ROOT(item) )
997 {
998 return GET_VIRTUAL_ROOT()->GetParam();
999 }
1000
1001 // visible node.
1002 if ( !DoGetItem(&tvItem) )
08b7c251
VZ
1003 {
1004 return NULL;
1005 }
2bda0e17 1006
4523ebb3 1007 return (wxTreeItemParam *)tvItem.lParam;
2bda0e17
KB
1008}
1009
4523ebb3 1010wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
2bda0e17 1011{
4523ebb3 1012 wxTreeItemParam *data = GetItemParam(item);
05b2a432 1013
4523ebb3
VZ
1014 return data ? data->GetData() : NULL;
1015}
a9c1265f 1016
4523ebb3
VZ
1017void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
1018{
188781db
VZ
1019 // first, associate this piece of data with this item
1020 if ( data )
1021 {
1022 data->SetId(item);
1023 }
1024
4523ebb3 1025 wxTreeItemParam *param = GetItemParam(item);
74b31181 1026
4523ebb3 1027 wxCHECK_RET( param, wxT("failed to change tree items data") );
74b31181 1028
4523ebb3 1029 param->SetData(data);
08b7c251 1030}
2bda0e17 1031
3a5a2f56
VZ
1032void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1033{
05b2a432
RD
1034 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1035
a9c1265f
VZ
1036 if ( IS_VIRTUAL_ROOT(item) )
1037 return;
1038
3a5a2f56
VZ
1039 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1040 tvItem.cChildren = (int)has;
1041 DoSetItem(&tvItem);
1042}
1043
add28c55
VZ
1044void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1045{
05b2a432
RD
1046 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1047
a9c1265f
VZ
1048 if ( IS_VIRTUAL_ROOT(item) )
1049 return;
1050
add28c55
VZ
1051 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1052 tvItem.state = bold ? TVIS_BOLD : 0;
1053 DoSetItem(&tvItem);
1054}
1055
58a8ab88
JS
1056void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1057{
a9c1265f
VZ
1058 if ( IS_VIRTUAL_ROOT(item) )
1059 return;
1060
58a8ab88
JS
1061 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1062 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1063 DoSetItem(&tvItem);
1064}
1065
d00407b2
VZ
1066void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1067{
a9c1265f
VZ
1068 if ( IS_VIRTUAL_ROOT(item) )
1069 return;
1070
d00407b2
VZ
1071 wxRect rect;
1072 if ( GetBoundingRect(item, rect) )
1073 {
1074 RefreshRect(rect);
1075 }
1076}
1077
2b5f62a0
VZ
1078wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1079{
05b2a432 1080 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
2b5f62a0 1081
05b2a432 1082 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1083 return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
2b5f62a0
VZ
1084}
1085
1086wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
1087{
05b2a432 1088 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
2b5f62a0 1089
05b2a432 1090 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1091 return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
2b5f62a0
VZ
1092}
1093
1094wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1095{
05b2a432 1096 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
2b5f62a0 1097
05b2a432 1098 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1099 return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
2b5f62a0
VZ
1100}
1101
696e1ea0
VZ
1102void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1103 const wxColour& col)
1104{
05b2a432
RD
1105 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1106
ee4b2721 1107 wxTreeItemAttr *attr;
f888d614 1108 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1109 if ( it == m_attrs.end() )
696e1ea0 1110 {
ee4b2721
VZ
1111 m_hasAnyAttr = true;
1112
f888d614 1113 m_attrs[item.m_pItem] =
696e1ea0 1114 attr = new wxTreeItemAttr;
ee4b2721
VZ
1115 }
1116 else
1117 {
1118 attr = it->second;
696e1ea0
VZ
1119 }
1120
1121 attr->SetTextColour(col);
d00407b2
VZ
1122
1123 RefreshItem(item);
696e1ea0
VZ
1124}
1125
1126void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1127 const wxColour& col)
1128{
05b2a432
RD
1129 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1130
ee4b2721 1131 wxTreeItemAttr *attr;
f888d614 1132 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1133 if ( it == m_attrs.end() )
696e1ea0 1134 {
ee4b2721
VZ
1135 m_hasAnyAttr = true;
1136
f888d614 1137 m_attrs[item.m_pItem] =
696e1ea0 1138 attr = new wxTreeItemAttr;
ee4b2721
VZ
1139 }
1140 else // already in the hash
1141 {
1142 attr = it->second;
696e1ea0
VZ
1143 }
1144
1145 attr->SetBackgroundColour(col);
d00407b2
VZ
1146
1147 RefreshItem(item);
696e1ea0
VZ
1148}
1149
1150void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1151{
05b2a432
RD
1152 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1153
ee4b2721 1154 wxTreeItemAttr *attr;
f888d614 1155 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1156 if ( it == m_attrs.end() )
696e1ea0 1157 {
ee4b2721
VZ
1158 m_hasAnyAttr = true;
1159
f888d614 1160 m_attrs[item.m_pItem] =
696e1ea0 1161 attr = new wxTreeItemAttr;
ee4b2721
VZ
1162 }
1163 else // already in the hash
1164 {
1165 attr = it->second;
696e1ea0
VZ
1166 }
1167
1168 attr->SetFont(font);
d00407b2 1169
2bd16277
RD
1170 // Reset the item's text to ensure that the bounding rect will be adjusted
1171 // for the new font.
1172 SetItemText(item, GetItemText(item));
72e61024 1173
d00407b2 1174 RefreshItem(item);
696e1ea0
VZ
1175}
1176
08b7c251
VZ
1177// ----------------------------------------------------------------------------
1178// Item status
1179// ----------------------------------------------------------------------------
2bda0e17 1180
08b7c251
VZ
1181bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1182{
bfbb0b4c 1183 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1184
2b5f62a0
VZ
1185 if ( item == wxTreeItemId(TVI_ROOT) )
1186 {
1187 // virtual (hidden) root is never visible
04cd30de 1188 return false;
2b5f62a0
VZ
1189 }
1190
add28c55 1191 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
08b7c251 1192 RECT rect;
955be36c
VZ
1193
1194 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1195 // the HTREEITEM with TVM_GETITEMRECT
ee4b2721 1196 *(HTREEITEM *)&rect = HITEM(item);
955be36c 1197
caea927d
VZ
1198 // true means to get rect for just the text, not the whole line
1199 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT, true, (LPARAM)&rect) )
1200 {
1201 // if TVM_GETITEMRECT returned false, then the item is definitely not
1202 // visible (because its parent is not expanded)
1203 return false;
1204 }
1205
1206 // however if it returned true, the item might still be outside the
1207 // currently visible part of the tree, test for it (notice that partly
1208 // visible means visible here)
1209 return rect.bottom > 0 && rect.top < GetClientSize().y;
2bda0e17
KB
1210}
1211
08b7c251 1212bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
2bda0e17 1213{
bfbb0b4c 1214 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1215
fa97ee24
VZ
1216 if ( IS_VIRTUAL_ROOT(item) )
1217 {
1218 wxTreeItemIdValue cookie;
1219 return GetFirstChild(item, cookie).IsOk();
1220 }
1221
08b7c251 1222 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
ae322a48 1223 DoGetItem(&tvItem);
2bda0e17 1224
08b7c251 1225 return tvItem.cChildren != 0;
2bda0e17
KB
1226}
1227
08b7c251 1228bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
2bda0e17 1229{
bfbb0b4c 1230 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
2bda0e17 1231
08b7c251
VZ
1232 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1233 DoGetItem(&tvItem);
2bda0e17 1234
08b7c251 1235 return (tvItem.state & TVIS_EXPANDED) != 0;
2bda0e17
KB
1236}
1237
08b7c251 1238bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
2bda0e17 1239{
bfbb0b4c 1240 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1241
08b7c251
VZ
1242 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1243 DoGetItem(&tvItem);
2bda0e17 1244
08b7c251 1245 return (tvItem.state & TVIS_SELECTED) != 0;
2bda0e17
KB
1246}
1247
add28c55
VZ
1248bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1249{
bfbb0b4c 1250 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1251
add28c55
VZ
1252 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1253 DoGetItem(&tvItem);
1254
1255 return (tvItem.state & TVIS_BOLD) != 0;
1256}
1257
08b7c251
VZ
1258// ----------------------------------------------------------------------------
1259// navigation
1260// ----------------------------------------------------------------------------
2bda0e17 1261
08b7c251
VZ
1262wxTreeItemId wxTreeCtrl::GetRootItem() const
1263{
a9c1265f
VZ
1264 // Root may be real (visible) or virtual (hidden).
1265 if ( GET_VIRTUAL_ROOT() )
1266 return TVI_ROOT;
1267
ee4b2721 1268 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
08b7c251 1269}
2bda0e17 1270
08b7c251
VZ
1271wxTreeItemId wxTreeCtrl::GetSelection() const
1272{
f888d614 1273 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxTreeItemId(),
223d09f6 1274 wxT("this only works with single selection controls") );
9dfbf520 1275
ee4b2721 1276 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2bda0e17
KB
1277}
1278
99006e44 1279wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const
2bda0e17 1280{
05b2a432
RD
1281 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1282
c9b142c9
VZ
1283 HTREEITEM hItem;
1284
1285 if ( IS_VIRTUAL_ROOT(item) )
1286 {
1287 // no parent for the virtual root
1288 hItem = 0;
1289 }
1290 else // normal item
a9c1265f 1291 {
c9b142c9
VZ
1292 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1293 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1294 {
1295 // the top level items should have the virtual root as their parent
1296 hItem = TVI_ROOT;
1297 }
a9c1265f
VZ
1298 }
1299
ee4b2721 1300 return wxTreeItemId(hItem);
08b7c251 1301}
2bda0e17 1302
08b7c251 1303wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
ee4b2721 1304 wxTreeItemIdValue& cookie) const
08b7c251 1305{
05b2a432
RD
1306 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1307
08b7c251 1308 // remember the last child returned in 'cookie'
ee4b2721
VZ
1309 cookie = TreeView_GetChild(GetHwnd(), HITEM(item));
1310
1311 return wxTreeItemId(cookie);
1312}
1313
1314wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1315 wxTreeItemIdValue& cookie) const
1316{
44d60c0b
WS
1317 wxTreeItemId fromCookie(cookie);
1318
1319 HTREEITEM hitem = HITEM(fromCookie);
1320
1321 hitem = TreeView_GetNextSibling(GetHwnd(), hitem);
1322
1323 wxTreeItemId item(hitem);
1324
ee4b2721
VZ
1325 cookie = item.m_pItem;
1326
1327 return item;
1328}
2bda0e17 1329
978f38c2
VZ
1330wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1331{
05b2a432
RD
1332 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1333
978f38c2 1334 // can this be done more efficiently?
ee4b2721 1335 wxTreeItemIdValue cookie;
978f38c2
VZ
1336
1337 wxTreeItemId childLast,
2165ad93 1338 child = GetFirstChild(item, cookie);
978f38c2
VZ
1339 while ( child.IsOk() )
1340 {
1341 childLast = child;
2165ad93 1342 child = GetNextChild(item, cookie);
978f38c2
VZ
1343 }
1344
1345 return childLast;
1346}
1347
08b7c251
VZ
1348wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1349{
05b2a432 1350 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
ee4b2721 1351 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1352}
1353
08b7c251 1354wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
2bda0e17 1355{
05b2a432 1356 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
ee4b2721 1357 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1358}
1359
08b7c251 1360wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
2bda0e17 1361{
ee4b2721 1362 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
2bda0e17
KB
1363}
1364
08b7c251 1365wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
2bda0e17 1366{
05b2a432 1367 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f6bcfd97 1368 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
02ce7b72 1369
f73eddd2
VZ
1370 wxTreeItemId next(TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1371 if ( next.IsOk() && !IsVisible(next) )
1372 {
1373 // Win32 considers that any non-collapsed item is visible while we want
1374 // to return only really visible items
1375 next.Unset();
1376 }
1377
1378 return next;
08b7c251 1379}
02ce7b72 1380
08b7c251
VZ
1381wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1382{
05b2a432 1383 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f6bcfd97 1384 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
02ce7b72 1385
f73eddd2
VZ
1386 wxTreeItemId prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1387 if ( prev.IsOk() && !IsVisible(prev) )
1388 {
1389 // just as above, Win32 function will happily return the previous item
1390 // in the tree for the first visible item too
1391 prev.Unset();
1392 }
1393
1394 return prev;
08b7c251 1395}
02ce7b72 1396
9dfbf520
VZ
1397// ----------------------------------------------------------------------------
1398// multiple selections emulation
1399// ----------------------------------------------------------------------------
1400
33961d59
RR
1401size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1402{
1403 TraverseSelections selector(this, selections);
9dfbf520 1404
e47c4d48 1405 return selector.GetCount();
9dfbf520
VZ
1406}
1407
08b7c251
VZ
1408// ----------------------------------------------------------------------------
1409// Usual operations
1410// ----------------------------------------------------------------------------
02ce7b72 1411
8cee4a30
VZ
1412wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent,
1413 const wxTreeItemId& hInsertAfter,
1414 const wxString& text,
1415 int image, int selectedImage,
1416 wxTreeItemData *data)
08b7c251 1417{
0e935169
VZ
1418 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1419 wxTreeItemId(),
1420 _T("can't have more than one root in the tree") );
1421
08b7c251 1422 TV_INSERTSTRUCT tvIns;
3f7bc32b
VZ
1423 tvIns.hParent = HITEM(parent);
1424 tvIns.hInsertAfter = HITEM(hInsertAfter);
58a8ab88 1425
74b31181
VZ
1426 // this is how we insert the item as the first child: supply a NULL
1427 // hInsertAfter
1428 if ( !tvIns.hInsertAfter )
58a8ab88
JS
1429 {
1430 tvIns.hInsertAfter = TVI_FIRST;
1431 }
1432
08b7c251 1433 UINT mask = 0;
44d60c0b 1434 if ( !text.empty() )
08b7c251
VZ
1435 {
1436 mask |= TVIF_TEXT;
c9f78968 1437 tvIns.item.pszText = (wxChar *)text.wx_str(); // cast is ok
08b7c251 1438 }
f6bcfd97
BP
1439 else
1440 {
1441 tvIns.item.pszText = NULL;
1442 tvIns.item.cchTextMax = 0;
1443 }
02ce7b72 1444
1b182562
VZ
1445 // create the param which will store the other item parameters
1446 wxTreeItemParam *param = new wxTreeItemParam;
1447
1448 // we return the images on demand as they depend on whether the item is
1449 // expanded or collapsed too in our case
4523ebb3
VZ
1450 mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
1451 tvIns.item.iImage = I_IMAGECALLBACK;
1452 tvIns.item.iSelectedImage = I_IMAGECALLBACK;
3a5a2f56 1453
4523ebb3 1454 param->SetImage(image, wxTreeItemIcon_Normal);
1b182562 1455 param->SetImage(selectedImage, wxTreeItemIcon_Selected);
02ce7b72 1456
4523ebb3
VZ
1457 mask |= TVIF_PARAM;
1458 tvIns.item.lParam = (LPARAM)param;
08b7c251 1459 tvIns.item.mask = mask;
02ce7b72 1460
4a36036e
VZ
1461 // don't use the hack below for the children of hidden root: this results
1462 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1463 const bool firstChild = !IsHiddenRoot(parent) &&
1464 !TreeView_GetChild(GetHwnd(), HITEM(parent));
7e45c159 1465
1b182562 1466 HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns);
08b7c251
VZ
1467 if ( id == 0 )
1468 {
f6bcfd97 1469 wxLogLastError(wxT("TreeView_InsertItem"));
08b7c251 1470 }
02ce7b72 1471
7e45c159
VZ
1472 // apparently some Windows versions (2000 and XP are reported to do this)
1473 // sometimes don't refresh the tree after adding the first child and so we
1474 // need this to make the "[+]" appear
1475 if ( firstChild )
1476 {
1477 RECT rect;
1478 TreeView_GetItemRect(GetHwnd(), HITEM(parent), &rect, FALSE);
1479 ::InvalidateRect(GetHwnd(), &rect, FALSE);
1480 }
1481
4523ebb3
VZ
1482 // associate the application tree item with Win32 tree item handle
1483 param->SetItem(id);
1484
1485 // setup wxTreeItemData
fd3f686c
VZ
1486 if ( data != NULL )
1487 {
4523ebb3 1488 param->SetData(data);
ee4b2721 1489 data->SetId(id);
fd3f686c
VZ
1490 }
1491
ee4b2721 1492 return wxTreeItemId(id);
2bda0e17
KB
1493}
1494
08b7c251
VZ
1495wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1496 int image, int selectedImage,
1497 wxTreeItemData *data)
2bda0e17 1498{
0228081f 1499 if ( HasFlag(wxTR_HIDE_ROOT) )
a9c1265f 1500 {
2a69b4e8
VZ
1501 wxASSERT_MSG( !m_pVirtualRoot, _T("tree can have only a single root") );
1502
a9c1265f 1503 // create a virtual root item, the parent for all the others
4523ebb3
VZ
1504 wxTreeItemParam *param = new wxTreeItemParam;
1505 param->SetData(data);
1506
1507 m_pVirtualRoot = new wxVirtualNode(param);
a9c1265f
VZ
1508
1509 return TVI_ROOT;
1510 }
1511
8cee4a30
VZ
1512 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1513 text, image, selectedImage, data);
2bda0e17
KB
1514}
1515
8cee4a30
VZ
1516wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1517 size_t index,
1518 const wxString& text,
1519 int image, int selectedImage,
1520 wxTreeItemData *data)
2ef31e80 1521{
8cee4a30
VZ
1522 wxTreeItemId idPrev;
1523 if ( index == (size_t)-1 )
2ef31e80 1524 {
8cee4a30
VZ
1525 // special value: append to the end
1526 idPrev = TVI_LAST;
2ef31e80 1527 }
8cee4a30
VZ
1528 else // find the item from index
1529 {
1530 wxTreeItemIdValue cookie;
1531 wxTreeItemId idCur = GetFirstChild(parent, cookie);
1532 while ( index != 0 && idCur.IsOk() )
1533 {
1534 index--;
2ef31e80 1535
8cee4a30
VZ
1536 idPrev = idCur;
1537 idCur = GetNextChild(parent, cookie);
1538 }
2ef31e80 1539
8cee4a30
VZ
1540 // assert, not check: if the index is invalid, we will append the item
1541 // to the end
1542 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1543 }
2ef31e80 1544
8cee4a30 1545 return DoInsertAfter(parent, idPrev, text, image, selectedImage, data);
2bda0e17
KB
1546}
1547
08b7c251 1548void wxTreeCtrl::Delete(const wxTreeItemId& item)
2bda0e17 1549{
396f85ba
BW
1550 // unlock tree selections on vista, without this the
1551 // tree ctrl will eventually crash after item deletion
1552 TreeItemUnlocker unlock_all;
1553
3f7bc32b 1554 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
bbcdf8bc 1555 {
f6bcfd97 1556 wxLogLastError(wxT("TreeView_DeleteItem"));
bbcdf8bc 1557 }
bbcdf8bc
JS
1558}
1559
23fd5130
VZ
1560// delete all children (but don't delete the item itself)
1561void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1562{
396f85ba
BW
1563 // unlock tree selections on vista for the duration of this call
1564 TreeItemUnlocker unlock_all;
1565
ee4b2721 1566 wxTreeItemIdValue cookie;
23fd5130 1567
ee4b2721 1568 wxArrayTreeItemIds children;
23fd5130
VZ
1569 wxTreeItemId child = GetFirstChild(item, cookie);
1570 while ( child.IsOk() )
1571 {
ee4b2721 1572 children.Add(child);
23fd5130
VZ
1573
1574 child = GetNextChild(item, cookie);
1575 }
1576
1577 size_t nCount = children.Count();
1578 for ( size_t n = 0; n < nCount; n++ )
1579 {
14ba002a 1580 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children[n])) )
23fd5130 1581 {
f6bcfd97 1582 wxLogLastError(wxT("TreeView_DeleteItem"));
23fd5130
VZ
1583 }
1584 }
1585}
1586
08b7c251 1587void wxTreeCtrl::DeleteAllItems()
bbcdf8bc 1588{
396f85ba
BW
1589 // unlock tree selections on vista for the duration of this call
1590 TreeItemUnlocker unlock_all;
1591
bfedf621
VZ
1592 // delete the "virtual" root item.
1593 if ( GET_VIRTUAL_ROOT() )
1594 {
1595 delete GET_VIRTUAL_ROOT();
1596 m_pVirtualRoot = NULL;
1597 }
1598
1599 // and all the real items
a9c1265f 1600
d220ae32 1601 if ( !TreeView_DeleteAllItems(GetHwnd()) )
bbcdf8bc 1602 {
f6bcfd97 1603 wxLogLastError(wxT("TreeView_DeleteAllItems"));
bbcdf8bc 1604 }
2bda0e17
KB
1605}
1606
08b7c251 1607void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
2bda0e17 1608{
dd3646fd
VZ
1609 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1610 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1611 flag == TVE_EXPAND ||
1612 flag == TVE_TOGGLE,
223d09f6 1613 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
08b7c251 1614
a9c1265f 1615 // A hidden root can be neither expanded nor collapsed.
0228081f 1616 wxCHECK_RET( !IsHiddenRoot(item),
637b7e4f 1617 wxT("Can't expand/collapse hidden root node!") );
a9c1265f 1618
08b7c251 1619 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
d220ae32
VZ
1620 // emulate them. This behaviour has changed slightly with comctl32.dll
1621 // v 4.70 - now it does send them but only the first time. To maintain
1622 // compatible behaviour and also in order to not have surprises with the
1623 // future versions, don't rely on this and still do everything ourselves.
1624 // To avoid that the messages be sent twice when the item is expanded for
1625 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1626
1627 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1628 tvItem.state = 0;
1629 DoSetItem(&tvItem);
1630
3f7bc32b 1631 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) != 0 )
08b7c251 1632 {
deb1de30
VZ
1633 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1634 // itself
09f277d6
VZ
1635 wxTreeEvent event(gs_expandEvents[IsExpanded(item) ? IDX_EXPAND
1636 : IDX_COLLAPSE]
1637 [IDX_DONE],
1638 this, item);
937013e0 1639 (void)HandleWindowEvent(event);
08b7c251 1640 }
d220ae32 1641 //else: change didn't took place, so do nothing at all
2bda0e17
KB
1642}
1643
08b7c251 1644void wxTreeCtrl::Expand(const wxTreeItemId& item)
2bda0e17 1645{
08b7c251 1646 DoExpand(item, TVE_EXPAND);
2bda0e17 1647}
2bda0e17 1648
08b7c251 1649void wxTreeCtrl::Collapse(const wxTreeItemId& item)
2bda0e17 1650{
08b7c251 1651 DoExpand(item, TVE_COLLAPSE);
2bda0e17
KB
1652}
1653
08b7c251 1654void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
2bda0e17 1655{
dd3646fd 1656 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
2bda0e17
KB
1657}
1658
08b7c251 1659void wxTreeCtrl::Toggle(const wxTreeItemId& item)
2bda0e17 1660{
08b7c251 1661 DoExpand(item, TVE_TOGGLE);
2bda0e17
KB
1662}
1663
08b7c251 1664void wxTreeCtrl::Unselect()
2bda0e17 1665{
3f7bc32b
VZ
1666 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE),
1667 wxT("doesn't make sense, may be you want UnselectAll()?") );
9dfbf520
VZ
1668
1669 // just remove the selection
ee4b2721 1670 SelectItem(wxTreeItemId());
08b7c251 1671}
02ce7b72 1672
9dfbf520 1673void wxTreeCtrl::UnselectAll()
08b7c251 1674{
9dfbf520 1675 if ( m_windowStyle & wxTR_MULTIPLE )
2bda0e17 1676 {
9dfbf520
VZ
1677 wxArrayTreeItemIds selections;
1678 size_t count = GetSelections(selections);
1679 for ( size_t n = 0; n < count; n++ )
d220ae32 1680 {
14ba002a 1681 ::UnselectItem(GetHwnd(), HITEM(selections[n]));
d220ae32 1682 }
ba8a1961
VZ
1683
1684 m_htSelStart.Unset();
9dfbf520 1685 }
75afdc2d 1686 else
9dfbf520
VZ
1687 {
1688 // just remove the selection
1689 Unselect();
1690 }
1691}
1692
3e9af289 1693void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
9dfbf520 1694{
8a536fa3
VZ
1695 wxCHECK_RET( !IsHiddenRoot(item), _T("can't select hidden root item") );
1696
cc87a04c
VZ
1697 wxASSERT_MSG( select || HasFlag(wxTR_MULTIPLE),
1698 _T("SelectItem(false) works only for multiselect") );
9dfbf520 1699
cc87a04c 1700 wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
937013e0 1701 if ( !HandleWindowEvent(event) || event.IsAllowed() )
cc87a04c 1702 {
7549148b 1703 if ( HasFlag(wxTR_MULTIPLE) )
d220ae32 1704 {
7549148b
VZ
1705 if ( !::SelectItem(GetHwnd(), HITEM(item), select) )
1706 {
1707 wxLogLastError(wxT("TreeView_SelectItem"));
1708 return;
1709 }
cc87a04c 1710 }
7549148b 1711 else // single selection
cc87a04c 1712 {
7549148b
VZ
1713 // use TreeView_SelectItem() to deselect the previous selection
1714 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item)) )
1715 {
1716 wxLogLastError(wxT("TreeView_SelectItem"));
1717 return;
1718 }
d220ae32 1719 }
7549148b
VZ
1720
1721 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
937013e0 1722 (void)HandleWindowEvent(event);
2bda0e17 1723 }
cc87a04c 1724 //else: program vetoed the change
08b7c251 1725}
2bda0e17 1726
08b7c251
VZ
1727void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1728{
0228081f
VZ
1729 wxCHECK_RET( !IsHiddenRoot(item), _T("can't show hidden root item") );
1730
08b7c251 1731 // no error return
3f7bc32b 1732 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
08b7c251
VZ
1733}
1734
1735void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1736{
3f7bc32b 1737 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
2bda0e17 1738 {
f6bcfd97 1739 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2bda0e17 1740 }
08b7c251
VZ
1741}
1742
44fbc477 1743wxTextCtrl *wxTreeCtrl::GetEditControl() const
08b7c251
VZ
1744{
1745 return m_textCtrl;
1746}
1747
1748void wxTreeCtrl::DeleteTextCtrl()
1749{
1750 if ( m_textCtrl )
2bda0e17 1751 {
b6a3d6ad
VZ
1752 // the HWND corresponding to this control is deleted by the tree
1753 // control itself and we don't know when exactly this happens, so check
1754 // if the window still exists before calling UnsubclassWin()
1755 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1756 {
1757 m_textCtrl->SetHWND(0);
1758 }
1759
08b7c251
VZ
1760 m_textCtrl->UnsubclassWin();
1761 m_textCtrl->SetHWND(0);
1762 delete m_textCtrl;
1763 m_textCtrl = NULL;
1a4088e1
VZ
1764
1765 m_idEdited.Unset();
2bda0e17 1766 }
08b7c251 1767}
2bda0e17 1768
4523ebb3
VZ
1769wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1770 wxClassInfo *textControlClass)
08b7c251
VZ
1771{
1772 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1773
b94ae1ea
VZ
1774 DeleteTextCtrl();
1775
1a4088e1 1776 m_idEdited = item;
cd6bd270 1777 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
3f7bc32b 1778 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
2bda0e17 1779
5ea47806 1780 // this is not an error - the TVN_BEGINLABELEDIT handler might have
04cd30de 1781 // returned false
5ea47806
VZ
1782 if ( !hWnd )
1783 {
cd6bd270
MB
1784 delete m_textCtrl;
1785 m_textCtrl = NULL;
5ea47806
VZ
1786 return NULL;
1787 }
2bda0e17 1788
cd6bd270 1789 // textctrl is subclassed in MSWOnNotify
08b7c251 1790 return m_textCtrl;
2bda0e17
KB
1791}
1792
08b7c251 1793// End label editing, optionally cancelling the edit
d35dce3a 1794void wxTreeCtrl::DoEndEditLabel(bool discardChanges)
2bda0e17 1795{
d220ae32 1796 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
08b7c251
VZ
1797
1798 DeleteTextCtrl();
2bda0e17
KB
1799}
1800
be0e5d69 1801wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
2bda0e17 1802{
08b7c251
VZ
1803 TV_HITTESTINFO hitTestInfo;
1804 hitTestInfo.pt.x = (int)point.x;
1805 hitTestInfo.pt.y = (int)point.y;
2bda0e17 1806
8ecd5de2 1807 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo);
2bda0e17 1808
08b7c251
VZ
1809 flags = 0;
1810
1811 // avoid repetition
1812 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1813 flags |= wxTREE_HITTEST_##flag
1814
1815 TRANSLATE_FLAG(ABOVE);
1816 TRANSLATE_FLAG(BELOW);
1817 TRANSLATE_FLAG(NOWHERE);
1818 TRANSLATE_FLAG(ONITEMBUTTON);
1819 TRANSLATE_FLAG(ONITEMICON);
1820 TRANSLATE_FLAG(ONITEMINDENT);
1821 TRANSLATE_FLAG(ONITEMLABEL);
1822 TRANSLATE_FLAG(ONITEMRIGHT);
1823 TRANSLATE_FLAG(ONITEMSTATEICON);
1824 TRANSLATE_FLAG(TOLEFT);
1825 TRANSLATE_FLAG(TORIGHT);
2bda0e17 1826
08b7c251
VZ
1827 #undef TRANSLATE_FLAG
1828
ee4b2721 1829 return wxTreeItemId(hitTestInfo.hItem);
08b7c251
VZ
1830}
1831
f7c832a7
VZ
1832bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1833 wxRect& rect,
1834 bool textOnly) const
1835{
1836 RECT rc;
f2c3db9d
RD
1837
1838 // Virtual root items have no bounding rectangle
1839 if ( IS_VIRTUAL_ROOT(item) )
1840 {
1841 return false;
1842 }
1843
3f7bc32b 1844 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
f7c832a7
VZ
1845 &rc, textOnly) )
1846 {
1847 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1848
04cd30de 1849 return true;
f7c832a7
VZ
1850 }
1851 else
1852 {
1853 // couldn't retrieve rect: for example, item isn't visible
04cd30de 1854 return false;
f7c832a7
VZ
1855 }
1856}
1857
23fd5130
VZ
1858// ----------------------------------------------------------------------------
1859// sorting stuff
1860// ----------------------------------------------------------------------------
f7c832a7 1861
502a2b18
VZ
1862// this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1863// functions such as IsDataIndirect()
1864class wxTreeSortHelper
1865{
1866public:
1867 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
1868
1869private:
4523ebb3 1870 static wxTreeItemId GetIdFromData(LPARAM lParam)
502a2b18 1871 {
4523ebb3 1872 return ((wxTreeItemParam*)lParam)->GetItem();
502a2b18 1873 }
502a2b18
VZ
1874};
1875
1876int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
1877 LPARAM pItem2,
1878 LPARAM htree)
23fd5130 1879{
096c9f9b 1880 wxCHECK_MSG( pItem1 && pItem2, 0,
223d09f6 1881 wxT("sorting tree without data doesn't make sense") );
096c9f9b 1882
502a2b18
VZ
1883 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
1884
4523ebb3
VZ
1885 return tree->OnCompareItems(GetIdFromData(pItem1),
1886 GetIdFromData(pItem2));
23fd5130
VZ
1887}
1888
95aabccc
VZ
1889void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1890{
05b2a432
RD
1891 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1892
95aabccc 1893 // rely on the fact that TreeView_SortChildren does the same thing as our
23fd5130
VZ
1894 // default behaviour, i.e. sorts items alphabetically and so call it
1895 // directly if we're not in derived class (much more efficient!)
4523ebb3
VZ
1896 // RN: Note that if you find you're code doesn't sort as expected this
1897 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1898 // combo for your derived wxTreeCtrl if will sort without
1899 // OnCompareItems
23fd5130 1900 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2bda0e17 1901 {
3f7bc32b 1902 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2bda0e17 1903 }
08b7c251 1904 else
2bda0e17 1905 {
62448488 1906 TV_SORTCB tvSort;
3f7bc32b 1907 tvSort.hParent = HITEM(item);
502a2b18 1908 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
23fd5130 1909 tvSort.lParam = (LPARAM)this;
d220ae32 1910 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2bda0e17 1911 }
08b7c251 1912}
2bda0e17 1913
08b7c251
VZ
1914// ----------------------------------------------------------------------------
1915// implementation
1916// ----------------------------------------------------------------------------
2bda0e17 1917
90c6edd7
VZ
1918bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
1919{
1920 if ( msg->message == WM_KEYDOWN )
1921 {
6719c06a
VZ
1922 // Only eat VK_RETURN if not being used by the application in
1923 // conjunction with modifiers
1924 if ( (msg->wParam == VK_RETURN) && !wxIsAnyModifierDown() )
90c6edd7
VZ
1925 {
1926 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1927 return false;
1928 }
1929 }
1930
1931 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg);
1932}
1933
0edeeb6d 1934bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id_)
08b7c251 1935{
0edeeb6d
VZ
1936 const int id = (signed short)id_;
1937
08b7c251 1938 if ( cmd == EN_UPDATE )
2bda0e17 1939 {
08b7c251
VZ
1940 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1941 event.SetEventObject( this );
1942 ProcessCommand(event);
2bda0e17 1943 }
08b7c251 1944 else if ( cmd == EN_KILLFOCUS )
2bda0e17 1945 {
08b7c251
VZ
1946 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1947 event.SetEventObject( this );
1948 ProcessCommand(event);
2bda0e17 1949 }
08b7c251 1950 else
2bda0e17 1951 {
08b7c251 1952 // nothing done
04cd30de 1953 return false;
2bda0e17 1954 }
08b7c251
VZ
1955
1956 // command processed
04cd30de 1957 return true;
08b7c251
VZ
1958}
1959
23f681ec
VZ
1960// we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1961// only do it during dragging, minimize wxWin overhead (this is important for
1962// WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1963// instead of passing by wxWin events
c140b7e7 1964WXLRESULT wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
23f681ec 1965{
04cd30de 1966 bool processed = false;
c140b7e7 1967 WXLRESULT rc = 0;
2b9a7d4c 1968 bool isMultiple = HasFlag(wxTR_MULTIPLE);
3f7bc32b 1969
147964ca 1970 // This message is sent after a right-click, or when the "menu" key is pressed
499bbaeb 1971 if ( nMsg == WM_CONTEXTMENU )
30f5e34f 1972 {
147964ca
KH
1973 int x = GET_X_LPARAM(lParam),
1974 y = GET_Y_LPARAM(lParam);
8f6dc819
VZ
1975
1976 // the item for which the menu should be shown
1977 wxTreeItemId item;
1978
1979 // the position where the menu should be shown in client coordinates
1980 // (so that it can be passed directly to PopupMenu())
1981 wxPoint pt;
1982
1983 if ( x == -1 || y == -1 )
147964ca 1984 {
8f6dc819
VZ
1985 // this means that the event was generated from keyboard (e.g. with
1986 // Shift-F10 or special Windows menu key)
1987 //
1988 // use the Explorer standard of putting the menu at the left edge
1989 // of the text, in the vertical middle of the text
1990 item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1991 if ( item.IsOk() )
1992 {
1993 // Use the bounding rectangle of only the text part
1994 wxRect rect;
1995 GetBoundingRect(item, rect, true);
1996 pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2);
1997 }
147964ca 1998 }
8f6dc819 1999 else // event from mouse, use mouse position
147964ca 2000 {
8f6dc819
VZ
2001 pt = ScreenToClient(wxPoint(x, y));
2002
2003 TV_HITTESTINFO tvhti;
2004 tvhti.pt.x = pt.x;
2005 tvhti.pt.y = pt.y;
2006 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2007 item = wxTreeItemId(tvhti.hItem);
147964ca
KH
2008 }
2009
8f6dc819
VZ
2010 // create the event
2011 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
2012
2013 event.m_pointDrag = pt;
2014
937013e0 2015 if ( HandleWindowEvent(event) )
2b9a7d4c 2016 processed = true;
9994e2da 2017 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
30f5e34f 2018 }
2b9a7d4c 2019 else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
23f681ec 2020 {
7bb8798c
VZ
2021 // we only process mouse messages here and these parameters have the
2022 // same meaning for all of them
3f7bc32b
VZ
2023 int x = GET_X_LPARAM(lParam),
2024 y = GET_Y_LPARAM(lParam);
8ecd5de2 2025
e3ad5702
JS
2026 TV_HITTESTINFO tvht;
2027 tvht.pt.x = x;
2028 tvht.pt.y = y;
8ecd5de2 2029
8854a202 2030 HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht);
3f7bc32b 2031
23f681ec
VZ
2032 switch ( nMsg )
2033 {
3f7bc32b 2034 case WM_LBUTTONDOWN:
e3ad5702 2035 if ( htItem && isMultiple && (tvht.flags & TVHT_ONITEM) != 0 )
23f681ec 2036 {
e3ad5702
JS
2037 m_htClickedItem = (WXHTREEITEM) htItem;
2038 m_ptClick = wxPoint(x, y);
8ecd5de2 2039
3f7bc32b
VZ
2040 if ( wParam & MK_CONTROL )
2041 {
2042 SetFocus();
23f681ec 2043
3f7bc32b 2044 // toggle selected state
cc87a04c 2045 ToggleItemSelection(htItem);
3f7bc32b
VZ
2046
2047 ::SetFocus(GetHwnd(), htItem);
2048
2049 // reset on any click without Shift
f888d614 2050 m_htSelStart.Unset();
3f7bc32b 2051
04cd30de 2052 processed = true;
3f7bc32b
VZ
2053 }
2054 else if ( wParam & MK_SHIFT )
2055 {
2056 // this selects all items between the starting one and
2057 // the current
2058
2059 if ( !m_htSelStart )
2060 {
2061 // take the focused item
ee4b2721 2062 m_htSelStart = TreeView_GetSelection(GetHwnd());
3f7bc32b
VZ
2063 }
2064
e3ad5702
JS
2065 if ( m_htSelStart )
2066 SelectRange(GetHwnd(), HITEM(m_htSelStart), htItem,
3f7bc32b 2067 !(wParam & MK_CONTROL));
e3ad5702
JS
2068 else
2069 ::SelectItem(GetHwnd(), htItem);
3f7bc32b
VZ
2070
2071 ::SetFocus(GetHwnd(), htItem);
23f681ec 2072
04cd30de 2073 processed = true;
3f7bc32b
VZ
2074 }
2075 else // normal click
2076 {
df4ac4c7
VZ
2077 // avoid doing anything if we click on the only
2078 // currently selected item
8ecd5de2 2079
e3ad5702 2080 SetFocus();
5e7718a2 2081
df4ac4c7
VZ
2082 wxArrayTreeItemIds selections;
2083 size_t count = GetSelections(selections);
2084 if ( count == 0 ||
35cf1ec6 2085 count > 1 ||
14ba002a 2086 HITEM(selections[0]) != htItem )
df4ac4c7 2087 {
35cf1ec6
VZ
2088 // clear the previously selected items, if the
2089 // user clicked outside of the present selection.
2090 // otherwise, perform the deselection on mouse-up.
2091 // this allows multiple drag and drop to work.
5e7718a2 2092
e3ad5702 2093 if (!IsItemSelected(GetHwnd(), htItem))
35cf1ec6
VZ
2094 {
2095 UnselectAll();
2096
2097 // prevent the click from starting in-place editing
2098 // which should only happen if we click on the
2099 // already selected item (and nothing else is
2100 // selected)
2101
2102 TreeView_SelectItem(GetHwnd(), 0);
2103 ::SelectItem(GetHwnd(), htItem);
2104 }
e3ad5702
JS
2105 ::SetFocus(GetHwnd(), htItem);
2106 processed = true;
df4ac4c7 2107 }
b8e3f1cf
VZ
2108 else // click on a single selected item
2109 {
2110 // don't interfere with the default processing in
2111 // WM_MOUSEMOVE handler below as the default window
2112 // proc will start the drag itself if we let have
2113 // WM_LBUTTONDOWN
2114 m_htClickedItem.Unset();
2115 }
3f7bc32b
VZ
2116
2117 // reset on any click without Shift
f888d614 2118 m_htSelStart.Unset();
3f7bc32b
VZ
2119 }
2120 }
2121 break;
3f7bc32b 2122
e0bf68d6
VZ
2123 case WM_RBUTTONDOWN:
2124 // default handler removes the highlight from the currently
2125 // focused item when right mouse button is pressed on another
2126 // one but keeps the remaining items highlighted, which is
2127 // confusing, so override this default behaviour for tree with
2128 // multiple selections
2129 if ( isMultiple )
2130 {
2131 if ( !IsItemSelected(GetHwnd(), htItem) )
2132 {
2133 UnselectAll();
2134 SelectItem(htItem);
2135 ::SetFocus(GetHwnd(), htItem);
2136 }
2137
2138 // fire EVT_RIGHT_DOWN
2139 HandleMouseEvent(nMsg, x, y, wParam);
20bdddad 2140
e0bf68d6
VZ
2141 // send NM_RCLICK
2142 NMHDR nmhdr;
2143 nmhdr.hwndFrom = GetHwnd();
2144 nmhdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
2145 nmhdr.code = NM_RCLICK;
2146 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY,
2147 nmhdr.idFrom, (LPARAM)&nmhdr);
2148
2149 // prevent tree control default processing, as we've
20bdddad 2150 // already done everything
e0bf68d6
VZ
2151 processed = true;
2152 }
2153 break;
2154
3f7bc32b 2155 case WM_MOUSEMOVE:
68fc69f3 2156#ifndef __WXWINCE__
e3ad5702
JS
2157 if ( m_htClickedItem )
2158 {
2159 int cx = abs(m_ptClick.x - x);
2160 int cy = abs(m_ptClick.y - y);
2161
b8e3f1cf
VZ
2162 if ( cx > ::GetSystemMetrics(SM_CXDRAG) ||
2163 cy > ::GetSystemMetrics(SM_CYDRAG) )
e3ad5702 2164 {
b8e3f1cf
VZ
2165 NM_TREEVIEW tv;
2166 wxZeroMemory(tv);
e3ad5702 2167
b8e3f1cf
VZ
2168 tv.hdr.hwndFrom = GetHwnd();
2169 tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
2170 tv.hdr.code = TVN_BEGINDRAG;
8ecd5de2 2171
b8e3f1cf 2172 tv.itemNew.hItem = HITEM(m_htClickedItem);
8ecd5de2 2173
8ecd5de2 2174
b8e3f1cf
VZ
2175 TVITEM tviAux;
2176 wxZeroMemory(tviAux);
8ecd5de2 2177
b8e3f1cf
VZ
2178 tviAux.hItem = HITEM(m_htClickedItem);
2179 tviAux.mask = TVIF_STATE | TVIF_PARAM;
2180 tviAux.stateMask = 0xffffffff;
2181 TreeView_GetItem(GetHwnd(), &tviAux);
8ecd5de2 2182
b8e3f1cf
VZ
2183 tv.itemNew.state = tviAux.state;
2184 tv.itemNew.lParam = tviAux.lParam;
2185
2186 tv.ptDrag.x = x;
2187 tv.ptDrag.y = y;
2188
2189 // do it before SendMessage() call below to avoid
2190 // reentrancies here if there is another WM_MOUSEMOVE
2191 // in the queue already
e3ad5702 2192 m_htClickedItem.Unset();
b8e3f1cf
VZ
2193
2194 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY,
2195 tv.hdr.idFrom, (LPARAM)&tv );
2196
2197 // don't pass it to the default window proc, it would
2198 // start dragging again
2199 processed = true;
e3ad5702 2200 }
e3ad5702 2201 }
68fc69f3 2202#endif // __WXWINCE__
8ecd5de2 2203
68d9be05 2204#if wxUSE_DRAGIMAGE
3f7bc32b
VZ
2205 if ( m_dragImage )
2206 {
afff720b 2207 m_dragImage->Move(wxPoint(x, y));
3f7bc32b 2208 if ( htItem )
23f681ec
VZ
2209 {
2210 // highlight the item as target (hiding drag image is
2211 // necessary - otherwise the display will be corrupted)
68be9f09 2212 m_dragImage->Hide();
3f7bc32b 2213 TreeView_SelectDropTarget(GetHwnd(), htItem);
68be9f09 2214 m_dragImage->Show();
23f681ec
VZ
2215 }
2216 }
68d9be05 2217#endif // wxUSE_DRAGIMAGE
23f681ec
VZ
2218 break;
2219
2220 case WM_LBUTTONUP:
35cf1ec6
VZ
2221
2222 // facilitates multiple drag-and-drop
2223 if (htItem && isMultiple)
2224 {
2225 wxArrayTreeItemIds selections;
2226 size_t count = GetSelections(selections);
2227
2228 if (count > 1 &&
2229 !(wParam & MK_CONTROL) &&
2230 !(wParam & MK_SHIFT))
2231 {
2232 UnselectAll();
2233 TreeView_SelectItem(GetHwnd(), htItem);
e3ad5702
JS
2234 ::SelectItem(GetHwnd(), htItem);
2235 ::SetFocus(GetHwnd(), htItem);
35cf1ec6 2236 }
e3ad5702 2237 m_htClickedItem.Unset();
35cf1ec6
VZ
2238 }
2239
2240 // fall through
2241
23f681ec 2242 case WM_RBUTTONUP:
68d9be05 2243#if wxUSE_DRAGIMAGE
3f7bc32b 2244 if ( m_dragImage )
23f681ec 2245 {
68be9f09 2246 m_dragImage->EndDrag();
23f681ec
VZ
2247 delete m_dragImage;
2248 m_dragImage = NULL;
2249
2250 // generate the drag end event
09f277d6 2251 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG, this, htItem);
72e61024
JS
2252 event.m_pointDrag = wxPoint(x, y);
2253
937013e0 2254 (void)HandleWindowEvent(event);
225fe9d6
VZ
2255
2256 // if we don't do it, the tree seems to think that 2 items
2257 // are selected simultaneously which is quite weird
2258 TreeView_SelectDropTarget(GetHwnd(), 0);
23f681ec 2259 }
68d9be05 2260#endif // wxUSE_DRAGIMAGE
23f681ec
VZ
2261 break;
2262 }
2263 }
3f7bc32b
VZ
2264 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) && isMultiple )
2265 {
2266 // the tree control greys out the selected item when it loses focus and
2267 // paints it as selected again when it regains it, but it won't do it
2268 // for the other items itself - help it
2269 wxArrayTreeItemIds selections;
2270 size_t count = GetSelections(selections);
2271 RECT rect;
2272 for ( size_t n = 0; n < count; n++ )
2273 {
04cd30de 2274 // TreeView_GetItemRect() will return false if item is not visible,
3f7bc32b 2275 // which may happen perfectly well
14ba002a 2276 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
2b9a7d4c 2277 &rect, TRUE) )
3f7bc32b 2278 {
2b9a7d4c 2279 ::InvalidateRect(GetHwnd(), &rect, FALSE);
3f7bc32b
VZ
2280 }
2281 }
2282 }
2283 else if ( nMsg == WM_KEYDOWN && isMultiple )
2284 {
2285 bool bCtrl = wxIsCtrlDown(),
2286 bShift = wxIsShiftDown();
2287
2c8e4738 2288 HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
ba8a1961 2289 switch ( wParam )
3f7bc32b 2290 {
ba8a1961
VZ
2291 case VK_SPACE:
2292 if ( bCtrl )
2293 {
cc87a04c 2294 ToggleItemSelection(htSel);
ba8a1961
VZ
2295 }
2296 else
2297 {
2298 UnselectAll();
23f681ec 2299
ba8a1961
VZ
2300 ::SelectItem(GetHwnd(), htSel);
2301 }
3f7bc32b 2302
ba8a1961
VZ
2303 processed = true;
2304 break;
3f7bc32b 2305
ba8a1961
VZ
2306 case VK_UP:
2307 case VK_DOWN:
2308 if ( !bCtrl && !bShift )
3f7bc32b 2309 {
ba8a1961
VZ
2310 // no modifiers, just clear selection and then let the default
2311 // processing to take place
2312 UnselectAll();
3f7bc32b 2313 }
ba8a1961 2314 else if ( htSel )
3f7bc32b 2315 {
ba8a1961
VZ
2316 (void)wxControl::MSWWindowProc(nMsg, wParam, lParam);
2317
2318 HTREEITEM htNext = (HTREEITEM)
2319 TreeView_GetNextItem
2320 (
2321 GetHwnd(),
2322 htSel,
2323 wParam == VK_UP ? TVGN_PREVIOUSVISIBLE
2324 : TVGN_NEXTVISIBLE
2325 );
2326
2327 if ( !htNext )
2328 {
2329 // at the top/bottom
2330 htNext = htSel;
2331 }
2332
2333 if ( bShift )
2334 {
2335 if ( !m_htSelStart )
2336 m_htSelStart = htSel;
2337
2338 SelectRange(GetHwnd(), HITEM(m_htSelStart), htNext);
2339 }
2340 else // bCtrl
2341 {
2342 // without changing selection
2343 ::SetFocus(GetHwnd(), htNext);
2344 }
2345
2346 processed = true;
3f7bc32b 2347 }
ba8a1961
VZ
2348 break;
2349
2350 case VK_HOME:
2351 case VK_END:
2352 case VK_PRIOR:
2353 case VK_NEXT:
2354 // TODO: handle Shift/Ctrl with these keys
2355 if ( !bCtrl && !bShift )
3f7bc32b 2356 {
ba8a1961 2357 UnselectAll();
3f7bc32b 2358
ba8a1961
VZ
2359 m_htSelStart.Unset();
2360 }
3f7bc32b
VZ
2361 }
2362 }
d35dce3a
VZ
2363 else if ( nMsg == WM_COMMAND )
2364 {
2365 // if we receive a EN_KILLFOCUS command from the in-place edit control
2366 // used for label editing, make sure to end editing
2367 WORD id, cmd;
2368 WXHWND hwnd;
2369 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
2370
2371 if ( cmd == EN_KILLFOCUS )
2372 {
2373 if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd )
2374 {
2375 DoEndEditLabel();
2376
2377 processed = true;
2378 }
2379 }
2380 }
7bb8798c 2381
3f7bc32b
VZ
2382 if ( !processed )
2383 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
2384
2385 return rc;
23f681ec
VZ
2386}
2387
fbd8ac52
VZ
2388WXLRESULT
2389wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
2390{
068b764a
VZ
2391 if ( nMsg == WM_CHAR )
2392 {
df63b2a4 2393 // don't let the control process Space and Return keys because it
068b764a
VZ
2394 // doesn't do anything useful with them anyhow but always beeps
2395 // annoyingly when it receives them and there is no way to turn it off
2396 // simply if you just process TREEITEM_ACTIVATED event to which Space
2397 // and Enter presses are mapped in your code
2398 if ( wParam == VK_SPACE || wParam == VK_RETURN )
2399 return 0;
2400 }
c4cd8712
VZ
2401#if wxUSE_DRAGIMAGE
2402 else if ( nMsg == WM_KEYDOWN )
2403 {
2404 if ( wParam == VK_ESCAPE )
2405 {
2406 if ( m_dragImage )
2407 {
2408 m_dragImage->EndDrag();
2409 delete m_dragImage;
2410 m_dragImage = NULL;
2411
2412 // if we don't do it, the tree seems to think that 2 items
2413 // are selected simultaneously which is quite weird
2414 TreeView_SelectDropTarget(GetHwnd(), 0);
2415 }
2416 }
2417 }
2418#endif // wxUSE_DRAGIMAGE
068b764a 2419
fbd8ac52
VZ
2420 return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);
2421}
2422
08b7c251 2423// process WM_NOTIFY Windows message
a23fd0e1 2424bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
08b7c251 2425{
09f277d6 2426 wxTreeEvent event(wxEVT_NULL, this);
08b7c251
VZ
2427 wxEventType eventType = wxEVT_NULL;
2428 NMHDR *hdr = (NMHDR *)lParam;
2429
2430 switch ( hdr->code )
2bda0e17 2431 {
08b7c251
VZ
2432 case TVN_BEGINDRAG:
2433 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
2434 // fall through
2435
2436 case TVN_BEGINRDRAG:
2437 {
2438 if ( eventType == wxEVT_NULL )
2439 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
2440 //else: left drag, already set above
2441
2442 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2443
ee4b2721 2444 event.m_item = tv->itemNew.hItem;
08b7c251 2445 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
23f681ec
VZ
2446
2447 // don't allow dragging by default: the user code must
2448 // explicitly say that it wants to allow it to avoid breaking
2449 // the old apps
2450 event.Veto();
08b7c251 2451 }
696e1ea0 2452 break;
08b7c251
VZ
2453
2454 case TVN_BEGINLABELEDIT:
2455 {
2456 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
2457 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2458
646a8a4d
VZ
2459 // although the user event handler may still veto it, it is
2460 // important to set it now so that calls to SetItemText() from
2461 // the event handler would change the text controls contents
2462 m_idEdited =
ee4b2721 2463 event.m_item = info->item.hItem;
5ea47806 2464 event.m_label = info->item.pszText;
04cd30de 2465 event.m_editCancelled = false;
08b7c251 2466 }
696e1ea0 2467 break;
08b7c251
VZ
2468
2469 case TVN_DELETEITEM:
2470 {
2471 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
2472 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
2473
ee4b2721 2474 event.m_item = tv->itemOld.hItem;
696e1ea0
VZ
2475
2476 if ( m_hasAnyAttr )
2477 {
ee4b2721
VZ
2478 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
2479 if ( it != m_attrs.end() )
2480 {
2481 delete it->second;
2482 m_attrs.erase(it);
2483 }
696e1ea0 2484 }
08b7c251 2485 }
696e1ea0 2486 break;
08b7c251
VZ
2487
2488 case TVN_ENDLABELEDIT:
2489 {
2490 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
2491 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2492
ee4b2721 2493 event.m_item = info->item.hItem;
5ea47806 2494 event.m_label = info->item.pszText;
06110b00 2495 event.m_editCancelled = info->item.pszText == NULL;
08b7c251
VZ
2496 break;
2497 }
2498
676d6550 2499#ifndef __WXWINCE__
156194e1
JS
2500 // These *must* not be removed or TVN_GETINFOTIP will
2501 // not be processed each time the mouse is moved
2502 // and the tooltip will only ever update once.
2503 case TTN_NEEDTEXTA:
2504 case TTN_NEEDTEXTW:
2505 {
2506 *result = 0;
2507
2508 break;
2509 }
2510
f3f71703 2511#ifdef TVN_GETINFOTIP
156194e1
JS
2512 case TVN_GETINFOTIP:
2513 {
2514 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
2515 NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam;
2516
2517 // Which item are we trying to get a tooltip for?
bc0aebab 2518 event.m_item = info->hItem;
156194e1
JS
2519
2520 break;
2521 }
df63b2a4
VZ
2522#endif // TVN_GETINFOTIP
2523#endif // !__WXWINCE__
5e7718a2 2524
08b7c251
VZ
2525 case TVN_GETDISPINFO:
2526 eventType = wxEVT_COMMAND_TREE_GET_INFO;
2527 // fall through
2528
2529 case TVN_SETDISPINFO:
2530 {
2531 if ( eventType == wxEVT_NULL )
2532 eventType = wxEVT_COMMAND_TREE_SET_INFO;
2533 //else: get, already set above
2534
2535 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
2536
ee4b2721 2537 event.m_item = info->item.hItem;
08b7c251
VZ
2538 break;
2539 }
2540
2541 case TVN_ITEMEXPANDING:
08b7c251
VZ
2542 case TVN_ITEMEXPANDED:
2543 {
4523ebb3 2544 NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam;
08b7c251 2545
deb1de30 2546 int what;
08b7c251
VZ
2547 switch ( tv->action )
2548 {
deb1de30
VZ
2549 default:
2550 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
2551 // fall through
2552
08b7c251 2553 case TVE_EXPAND:
deb1de30 2554 what = IDX_EXPAND;
08b7c251
VZ
2555 break;
2556
2557 case TVE_COLLAPSE:
deb1de30 2558 what = IDX_COLLAPSE;
08b7c251 2559 break;
08b7c251
VZ
2560 }
2561
2b5f62a0
VZ
2562 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
2563 : IDX_DONE;
deb1de30
VZ
2564
2565 eventType = gs_expandEvents[what][how];
08b7c251 2566
ee4b2721 2567 event.m_item = tv->itemNew.hItem;
08b7c251 2568 }
696e1ea0 2569 break;
08b7c251
VZ
2570
2571 case TVN_KEYDOWN:
2572 {
2573 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
2574 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
2575
1944ad76
VZ
2576 // fabricate the lParam and wParam parameters sufficiently
2577 // similar to the ones from a "real" WM_KEYDOWN so that
2578 // CreateKeyEvent() works correctly
6719c06a 2579 WXLPARAM lParam = (wxIsAltDown() ? KF_ALTDOWN : 0) << 16;
1944ad76
VZ
2580
2581 WXWPARAM wParam = info->wVKey;
2582
637b7e4f 2583 int keyCode = wxCharCodeMSWToWX(wParam);
1944ad76
VZ
2584 if ( !keyCode )
2585 {
2586 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2587 // simple ASCII key
2588 keyCode = wParam;
2589 }
2590
8191741f 2591 event.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN,
1944ad76
VZ
2592 keyCode,
2593 lParam,
2594 wParam);
23fd5130 2595
3f7bc32b 2596 // a separate event for Space/Return
6719c06a 2597 if ( !wxIsAnyModifierDown() &&
3f7bc32b 2598 ((info->wVKey == VK_SPACE) || (info->wVKey == VK_RETURN)) )
23fd5130 2599 {
09f277d6
VZ
2600 wxTreeItemId item;
2601 if ( !HasFlag(wxTR_MULTIPLE) )
2602 item = GetSelection();
23fd5130 2603
09f277d6
VZ
2604 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2605 this, item);
937013e0 2606 (void)HandleWindowEvent(event2);
23fd5130 2607 }
08b7c251 2608 }
696e1ea0 2609 break;
08b7c251 2610
5fe302ef
BW
2611
2612 // Vista's tree control has introduced some problems with our
2613 // multi-selection tree. When TreeView_SelectItem() is called,
2614 // the wrong items are deselected.
20bdddad 2615
5fe302ef
BW
2616 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
2617 // that can be used to regulate this incorrect behavior. The
2618 // following messages will allow only the unlocked item's selection
2619 // state to change
396f85ba 2620
5fe302ef
BW
2621 case TVN_ITEMCHANGINGA:
2622 case TVN_ITEMCHANGINGW:
2623 {
2624 // we only need to handles these in multi-select trees
2625 if ( HasFlag(wxTR_MULTIPLE) )
2626 {
2627 // get info about the item about to be changed
2628 NMTVITEMCHANGE* info = (NMTVITEMCHANGE*)lParam;
396f85ba 2629 if (TreeItemUnlocker::IsLocked(info->hItem))
5fe302ef
BW
2630 {
2631 // item's state is locked, don't allow the change
2632 // returning 1 will disallow the change
20bdddad 2633 *result = 1;
5fe302ef
BW
2634 return true;
2635 }
2636 }
2637
2638 // allow the state change
2639 }
2640 return false;
2641
5e7718a2 2642 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
69a8b5b4
VS
2643 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2644 // we have to handle both messages:
2645 case TVN_SELCHANGEDA:
2646 case TVN_SELCHANGEDW:
08b7c251
VZ
2647 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
2648 // fall through
2649
69a8b5b4
VS
2650 case TVN_SELCHANGINGA:
2651 case TVN_SELCHANGINGW:
08b7c251
VZ
2652 {
2653 if ( eventType == wxEVT_NULL )
2654 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
2655 //else: already set above
2656
5e7718a2 2657 if (hdr->code == TVN_SELCHANGINGW ||
69a8b5b4
VS
2658 hdr->code == TVN_SELCHANGEDW)
2659 {
4523ebb3 2660 NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam;
ee4b2721
VZ
2661 event.m_item = tv->itemNew.hItem;
2662 event.m_itemOld = tv->itemOld.hItem;
69a8b5b4
VS
2663 }
2664 else
2665 {
4523ebb3 2666 NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam;
ee4b2721
VZ
2667 event.m_item = tv->itemNew.hItem;
2668 event.m_itemOld = tv->itemOld.hItem;
69a8b5b4 2669 }
08b7c251 2670 }
cb776b66
VZ
2671
2672 // we receive this message from WM_LBUTTONDOWN handler inside
2673 // comctl32.dll and so before the click is passed to
2674 // DefWindowProc() which sets the focus to the window which was
2675 // clicked and this can lead to unexpected event sequences: for
2676 // example, we may get a "selection change" event from the tree
2677 // before getting a "kill focus" event for the text control which
2678 // had the focus previously, thus breaking user code doing input
2679 // validation
2680 //
2681 // to avoid such surprises, we force the generation of focus events
2682 // now, before we generate the selection change ones
2683 SetFocus();
696e1ea0
VZ
2684 break;
2685
5b59df83
VZ
2686 // instead of explicitly checking for _WIN32_IE, check if the
2687 // required symbols are available in the headers
2688#if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
696e1ea0
VZ
2689 case NM_CUSTOMDRAW:
2690 {
2691 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
2692 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
d7926e0b 2693 switch ( nmcd.dwDrawStage )
696e1ea0
VZ
2694 {
2695 case CDDS_PREPAINT:
2696 // if we've got any items with non standard attributes,
2697 // notify us before painting each item
2698 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2699 : CDRF_DODEFAULT;
4754ab16
RR
2700
2701 // windows in TreeCtrl use one-based index for item state images,
2702 // 0 indexed image is not being used, we're using zero-based index,
2703 // so we have to add temp image (of zero index) to state image list
2704 // before we draw any item, then after items are drawn we have to
2705 // delete it (in POSTPAINT notify)
2706 if (m_imageListState && m_imageListState->GetImageCount() > 0)
2707 {
2708 #define hImageList (HIMAGELIST)m_imageListState->GetHIMAGELIST()
2709
2710 // add temporary image
2711 int width, height;
2712 m_imageListState->GetSize(0, width, height);
2713
2714 HBITMAP hbmpTemp = ::CreateBitmap(width, height, 1, 1, NULL);
2715 int index = ::ImageList_Add(hImageList, hbmpTemp, hbmpTemp);
2716 ::DeleteObject(hbmpTemp);
2717
2718 if ( index != -1 )
2719 {
2720 // move images to right
2721 for ( int i = index; i > 0; i-- )
2722 ::ImageList_Copy(hImageList, i, hImageList, i-1, 0);
2723
2724 // we must remove the image in POSTPAINT notify
2725 *result |= CDRF_NOTIFYPOSTPAINT;
2726 }
2727
2728 #undef hImageList
2729 }
2730 break;
2731
2732 case CDDS_POSTPAINT:
2733 // we are deleting temp image of 0 index, which was
2734 // added before items were drawn (in PREPAINT notify)
2735 if (m_imageListState && m_imageListState->GetImageCount() > 0)
2736 m_imageListState->Remove(0);
d7926e0b 2737 break;
696e1ea0
VZ
2738
2739 case CDDS_ITEMPREPAINT:
2740 {
ee4b2721
VZ
2741 wxMapTreeAttr::iterator
2742 it = m_attrs.find((void *)nmcd.dwItemSpec);
696e1ea0 2743
ee4b2721 2744 if ( it == m_attrs.end() )
696e1ea0
VZ
2745 {
2746 // nothing to do for this item
d7926e0b
VZ
2747 *result = CDRF_DODEFAULT;
2748 break;
696e1ea0
VZ
2749 }
2750
ee4b2721
VZ
2751 wxTreeItemAttr * const attr = it->second;
2752
8b713759
VZ
2753 wxTreeViewItem tvItem((void *)nmcd.dwItemSpec,
2754 TVIF_STATE, TVIS_DROPHILITED);
2755 DoGetItem(&tvItem);
2756 const UINT tvItemState = tvItem.state;
2757
19da49fc 2758 // selection colours should override ours,
8b713759
VZ
2759 // otherwise it is too confusing to the user
2760 if ( !(nmcd.uItemState & CDIS_SELECTED) &&
2761 !(tvItemState & TVIS_DROPHILITED) )
696e1ea0 2762 {
2076893b 2763 wxColour colBack;
696e1ea0
VZ
2764 if ( attr->HasBackgroundColour() )
2765 {
2766 colBack = attr->GetBackgroundColour();
19da49fc 2767 lptvcd->clrTextBk = wxColourToRGB(colBack);
696e1ea0 2768 }
19da49fc
VZ
2769 }
2770
2771 // but we still want to keep the special foreground
2772 // colour when we don't have focus (we can't keep
2773 // it when we do, it would usually be unreadable on
2774 // the almost inverted bg colour...)
8b713759
VZ
2775 if ( ( !(nmcd.uItemState & CDIS_SELECTED) ||
2776 FindFocus() != this ) &&
2777 !(tvItemState & TVIS_DROPHILITED) )
19da49fc
VZ
2778 {
2779 wxColour colText;
2780 if ( attr->HasTextColour() )
696e1ea0 2781 {
19da49fc
VZ
2782 colText = attr->GetTextColour();
2783 lptvcd->clrText = wxColourToRGB(colText);
696e1ea0 2784 }
696e1ea0
VZ
2785 }
2786
19da49fc 2787 if ( attr->HasFont() )
696e1ea0 2788 {
19da49fc
VZ
2789 HFONT hFont = GetHfontOf(attr->GetFont());
2790
696e1ea0
VZ
2791 ::SelectObject(nmcd.hdc, hFont);
2792
2793 *result = CDRF_NEWFONT;
2794 }
19da49fc 2795 else // no specific font
696e1ea0
VZ
2796 {
2797 *result = CDRF_DODEFAULT;
2798 }
696e1ea0 2799 }
d7926e0b 2800 break;
696e1ea0
VZ
2801
2802 default:
2803 *result = CDRF_DODEFAULT;
696e1ea0
VZ
2804 }
2805 }
d7926e0b
VZ
2806
2807 // we always process it
04cd30de 2808 return true;
5b59df83 2809#endif // have owner drawn support in headers
08b7c251 2810
8a000b6b
VZ
2811 case NM_CLICK:
2812 {
2813 DWORD pos = GetMessagePos();
2814 POINT point;
2815 point.x = LOWORD(pos);
2816 point.y = HIWORD(pos);
2817 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
2818 int flags = 0;
2819 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), flags);
2820 if (flags & wxTREE_HITTEST_ONITEMSTATEICON)
2821 {
2822 event.m_item = item;
2823 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
2824 }
2825 break;
2826 }
2827
f6bcfd97
BP
2828 case NM_DBLCLK:
2829 case NM_RCLICK:
2830 {
2831 TV_HITTESTINFO tvhti;
2832 ::GetCursorPos(&tvhti.pt);
2833 ::ScreenToClient(GetHwnd(), &tvhti.pt);
2834 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2835 {
2836 if ( tvhti.flags & TVHT_ONITEM )
2837 {
ee4b2721 2838 event.m_item = tvhti.hItem;
f6bcfd97
BP
2839 eventType = (int)hdr->code == NM_DBLCLK
2840 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2841 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
8591268f
VZ
2842
2843 event.m_pointDrag.x = tvhti.pt.x;
2844 event.m_pointDrag.y = tvhti.pt.y;
f6bcfd97
BP
2845 }
2846
2847 break;
2848 }
2849 }
2850 // fall through
2851
08b7c251 2852 default:
a23fd0e1 2853 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2bda0e17 2854 }
08b7c251 2855
08b7c251
VZ
2856 event.SetEventType(eventType);
2857
09f277d6
VZ
2858 if ( event.m_item.IsOk() )
2859 event.SetClientObject(GetItemData(event.m_item));
2860
937013e0 2861 bool processed = HandleWindowEvent(event);
08b7c251
VZ
2862
2863 // post processing
5ea47806 2864 switch ( hdr->code )
2bda0e17 2865 {
f6bcfd97 2866 case NM_DBLCLK:
0e2ad323
VZ
2867 // we translate NM_DBLCLK into ACTIVATED event and if the user
2868 // handled the activation of the item we shouldn't proceed with
2869 // also using the same double click for toggling the item expanded
2870 // state -- but OTOH do let the user to expand/collapse the item by
2871 // double clicking on it if the activation is not handled specially
2872 *result = processed;
f6bcfd97
BP
2873 break;
2874
df63b2a4
VZ
2875 case NM_RCLICK:
2876 // prevent tree control from sending WM_CONTEXTMENU to our parent
2877 // (which it does if NM_RCLICK is not handled) because we want to
2878 // send it to the control itself
2879 *result =
2880 processed = true;
2881
2882 ::SendMessage(GetHwnd(), WM_CONTEXTMENU,
2883 (WPARAM)GetHwnd(), ::GetMessagePos());
2884 break;
2885
23f681ec
VZ
2886 case TVN_BEGINDRAG:
2887 case TVN_BEGINRDRAG:
68d9be05 2888#if wxUSE_DRAGIMAGE
23f681ec
VZ
2889 if ( event.IsAllowed() )
2890 {
2891 // normally this is impossible because the m_dragImage is
2892 // deleted once the drag operation is over
2893 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
2894
2895 m_dragImage = new wxDragImage(*this, event.m_item);
c47addef 2896 m_dragImage->BeginDrag(wxPoint(0,0), this);
68be9f09 2897 m_dragImage->Show();
23f681ec 2898 }
68d9be05 2899#endif // wxUSE_DRAGIMAGE
23f681ec
VZ
2900 break;
2901
5ea47806
VZ
2902 case TVN_DELETEITEM:
2903 {
77ffb593 2904 // NB: we might process this message using wxWidgets event
5ea47806
VZ
2905 // tables, but due to overhead of wxWin event system we
2906 // prefer to do it here ourself (otherwise deleting a tree
2907 // with many items is just too slow)
4523ebb3 2908 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
4523ebb3
VZ
2909
2910 wxTreeItemParam *param =
2911 (wxTreeItemParam *)tv->itemOld.lParam;
2912 delete param;
08b7c251 2913
04cd30de 2914 processed = true; // Make sure we don't get called twice
5ea47806
VZ
2915 }
2916 break;
2917
2918 case TVN_BEGINLABELEDIT:
04cd30de 2919 // return true to cancel label editing
5ea47806 2920 *result = !event.IsAllowed();
646a8a4d 2921
cd6bd270 2922 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
646a8a4d 2923 if ( event.IsAllowed() )
cd6bd270
MB
2924 {
2925 HWND hText = TreeView_GetEditControl(GetHwnd());
4523ebb3 2926 if ( hText )
cd6bd270 2927 {
5e7718a2 2928 // MBN: if m_textCtrl already has an HWND, it is a stale
cd6bd270
MB
2929 // pointer from a previous edit (because the user
2930 // didn't modify the label before dismissing the control,
2931 // and TVN_ENDLABELEDIT was not sent), so delete it
4523ebb3 2932 if ( m_textCtrl && m_textCtrl->GetHWND() )
cd6bd270 2933 DeleteTextCtrl();
4523ebb3 2934 if ( !m_textCtrl )
cd6bd270
MB
2935 m_textCtrl = new wxTextCtrl();
2936 m_textCtrl->SetParent(this);
2937 m_textCtrl->SetHWND((WXHWND)hText);
2938 m_textCtrl->SubclassWin((WXHWND)hText);
2939
2940 // set wxTE_PROCESS_ENTER style for the text control to
2941 // force it to process the Enter presses itself, otherwise
2942 // they could be stolen from it by the dialog
2943 // navigation code
2944 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
2945 | wxTE_PROCESS_ENTER);
2946 }
2947 }
646a8a4d
VZ
2948 else // we had set m_idEdited before
2949 {
2950 m_idEdited.Unset();
2951 }
5ea47806
VZ
2952 break;
2953
2954 case TVN_ENDLABELEDIT:
04cd30de 2955 // return true to set the label to the new string: note that we
188781db 2956 // also must pretend that we did process the message or it is going
04cd30de 2957 // to be passed to DefWindowProc() which will happily return false
188781db 2958 // cancelling the label change
5ea47806 2959 *result = event.IsAllowed();
04cd30de 2960 processed = true;
5ea47806
VZ
2961
2962 // ensure that we don't have the text ctrl which is going to be
2963 // deleted any more
2964 DeleteTextCtrl();
2965 break;
2966
676d6550 2967#ifndef __WXWINCE__
f3f71703 2968#ifdef TVN_GETINFOTIP
156194e1
JS
2969 case TVN_GETINFOTIP:
2970 {
2971 // If the user permitted a tooltip change, change it
2972 if (event.IsAllowed())
2973 {
2974 SetToolTip(event.m_label);
2975 }
2976 }
2977 break;
f3f71703 2978#endif
676d6550 2979#endif
5e7718a2 2980
5ea47806
VZ
2981 case TVN_SELCHANGING:
2982 case TVN_ITEMEXPANDING:
04cd30de 2983 // return true to prevent the action from happening
5ea47806
VZ
2984 *result = !event.IsAllowed();
2985 break;
2986
deb1de30 2987 case TVN_ITEMEXPANDED:
deb1de30 2988 {
4523ebb3 2989 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
5188000b 2990 const wxTreeItemId id(tv->itemNew.hItem);
deb1de30 2991
5188000b 2992 if ( tv->action == TVE_COLLAPSE )
bf43d750 2993 {
5188000b
VZ
2994 if ( wxApp::GetComCtl32Version() >= 600 )
2995 {
2996 // for some reason the item selection rectangle depends
2997 // on whether it is expanded or collapsed (at least
2998 // with comctl32.dll v6): it is wider (by 3 pixels) in
2999 // the expanded state, so when the item collapses and
3000 // then is deselected the rightmost 3 pixels of the
3001 // previously drawn selection are left on the screen
3002 //
3003 // it's not clear if it's a bug in comctl32.dll or in
3004 // our code (because it does not happen in Explorer but
3005 // OTOH we don't do anything which could result in this
3006 // AFAICS) but we do need to work around it to avoid
3007 // ugly artifacts
3008 RefreshItem(id);
3009 }
3010 }
3011 else // expand
3012 {
3013 // the item is also not refreshed properly after expansion when
3014 // it has an image depending on the expanded/collapsed state:
3015 // again, it's not clear if the bug is in comctl32.dll or our
3016 // code...
3017 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
3018 if ( image != -1 )
3019 {
3020 RefreshItem(id);
3021 }
deb1de30
VZ
3022 }
3023 }
3024 break;
3025
74b31181
VZ
3026 case TVN_GETDISPINFO:
3027 // NB: so far the user can't set the image himself anyhow, so do it
3028 // anyway - but this may change later
4523ebb3 3029 //if ( /* !processed && */ )
74b31181
VZ
3030 {
3031 wxTreeItemId item = event.m_item;
3032 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
4523ebb3 3033
1b182562 3034 const wxTreeItemParam * const param = GetItemParam(item);
4523ebb3
VZ
3035 if ( !param )
3036 break;
3037
74b31181
VZ
3038 if ( info->item.mask & TVIF_IMAGE )
3039 {
3040 info->item.iImage =
4523ebb3 3041 param->GetImage
74b31181 3042 (
74b31181
VZ
3043 IsExpanded(item) ? wxTreeItemIcon_Expanded
3044 : wxTreeItemIcon_Normal
3045 );
3046 }
3047 if ( info->item.mask & TVIF_SELECTEDIMAGE )
3048 {
3049 info->item.iSelectedImage =
4523ebb3 3050 param->GetImage
74b31181 3051 (
74b31181
VZ
3052 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
3053 : wxTreeItemIcon_Selected
3054 );
3055 }
deb1de30 3056 }
74b31181
VZ
3057 break;
3058
5ea47806
VZ
3059 //default:
3060 // for the other messages the return value is ignored and there is
3061 // nothing special to do
3062 }
fd3f686c 3063 return processed;
2bda0e17
KB
3064}
3065
8a000b6b
VZ
3066// ----------------------------------------------------------------------------
3067// State control.
3068// ----------------------------------------------------------------------------
3069
3070// why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3071#define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3072
03966fcb 3073int wxTreeCtrl::DoGetItemState(const wxTreeItemId& item) const
8a000b6b 3074{
03966fcb 3075 wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") );
8a000b6b 3076
03966fcb
RR
3077 // receive the desired information
3078 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
3079 DoGetItem(&tvItem);
8a000b6b 3080
03966fcb
RR
3081 // state images are one-based
3082 return STATEIMAGEMASKTOINDEX(tvItem.state) - 1;
8a000b6b
VZ
3083}
3084
03966fcb 3085void wxTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state)
8a000b6b 3086{
03966fcb
RR
3087 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
3088
3089 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
8a000b6b 3090
03966fcb
RR
3091 // state images are one-based
3092 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3093 tvItem.state = INDEXTOSTATEIMAGEMASK(state + 1);
3094
3095 DoSetItem(&tvItem);
8a000b6b
VZ
3096}
3097
1e6feb95 3098#endif // wxUSE_TREECTRL