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