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