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