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