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