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