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