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