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