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