]> git.saurik.com Git - wxWidgets.git/blame - src/msw/treectrl.cpp
fix compilation without wxUSE_STREAMS (closes #10900)
[wxWidgets.git] / src / msw / treectrl.cpp
CommitLineData
b823f5a1 1/////////////////////////////////////////////////////////////////////////////
1e6feb95 2// Name: src/msw/treectrl.cpp
b823f5a1
JS
3// Purpose: wxTreeCtrl
4// Author: Julian Smart
08b7c251 5// Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
b823f5a1
JS
6// Created: 1997
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
b823f5a1 10/////////////////////////////////////////////////////////////////////////////
2bda0e17 11
08b7c251
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
1e6feb95 19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
08b7c251 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
1e6feb95
VZ
27#if wxUSE_TREECTRL
28
e4db172a
WS
29#include "wx/treectrl.h"
30
ad9835c9 31#ifndef WX_PRECOMP
57bd4c60
WS
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
ad9835c9 34 #include "wx/dynarray.h"
e4db172a 35 #include "wx/log.h"
670f9935 36 #include "wx/app.h"
9eddec69 37 #include "wx/settings.h"
ad9835c9
WS
38#endif
39
c27dce18 40#include "wx/dynlib.h"
2f17da8b
WS
41#include "wx/msw/private.h"
42
1e6feb95
VZ
43// Set this to 1 to be _absolutely_ sure that repainting will work for all
44// comctl32.dll versions
f6bcfd97
BP
45#define wxUSE_COMCTL32_SAFELY 0
46
08b7c251 47#include "wx/imaglist.h"
23f681ec
VZ
48#include "wx/msw/dragimag.h"
49
f888d614
VZ
50// macros to hide the cast ugliness
51// --------------------------------
52
14ba002a
VZ
53// get HTREEITEM from wxTreeItemId
54#define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
3f7bc32b 55
5fe302ef
BW
56
57// older SDKs are missing these
58#ifndef TVN_ITEMCHANGINGA
59
60#define TVN_ITEMCHANGINGA (TVN_FIRST-16)
61#define TVN_ITEMCHANGINGW (TVN_FIRST-17)
62
63typedef struct tagNMTVITEMCHANGE
64{
65 NMHDR hdr;
66 UINT uChanged;
67 HTREEITEM hItem;
68 UINT uStateNew;
69 UINT uStateOld;
70 LPARAM lParam;
71} NMTVITEMCHANGE;
72
73#endif
74
75
396f85ba
BW
76// this helper class is used on vista systems for preventing unwanted
77// item state changes in the vista tree control. It is only effective in
5fe302ef
BW
78// multi-select mode on vista systems.
79
396f85ba
BW
80// The vista tree control includes some new code that originally broke the
81// multi-selection tree, causing seemingly spurious item selection state changes
82// during Shift or Ctrl-click item selection. (To witness the original broken
83// behavior, simply make IsLocked() below always return false). This problem was
84// solved by using the following class to 'unlock' an item's selection state.
5fe302ef
BW
85
86class TreeItemUnlocker
87{
88public:
396f85ba
BW
89 // unlock a single item
90 TreeItemUnlocker(HTREEITEM item) { ms_unlockedItem = item; }
91
92 // unlock all items, don't use unless absolutely necessary
93 TreeItemUnlocker() { ms_unlockedItem = (HTREEITEM)-1; }
94
95 // lock everything back
96 ~TreeItemUnlocker() { ms_unlockedItem = NULL; }
97
98
99 // check if the item state is currently locked
100 static bool IsLocked(HTREEITEM item)
101 { return ms_unlockedItem != (HTREEITEM)-1 && item != ms_unlockedItem; }
102
103private:
104 static HTREEITEM ms_unlockedItem;
5fe302ef
BW
105};
106
396f85ba 107HTREEITEM TreeItemUnlocker::ms_unlockedItem = NULL;
5fe302ef 108
7dac12ef
VZ
109// another helper class: set the variable to true during its lifetime and reset
110// it to false when it is destroyed
111//
112// it is currently always used with wxTreeCtrl::m_changingSelection
113class TempSetter
114{
115public:
116 TempSetter(bool& var) : m_var(var)
117 {
118 wxASSERT_MSG( !m_var, "variable shouldn't be already set" );
119 m_var = true;
120 }
121
122 ~TempSetter()
123 {
124 m_var = false;
125 }
126
127private:
128 bool& m_var;
129
130 wxDECLARE_NO_COPY_CLASS(TempSetter);
131};
132
3f7bc32b
VZ
133// ----------------------------------------------------------------------------
134// private functions
135// ----------------------------------------------------------------------------
136
75afdc2d
VZ
137// wrappers for TreeView_GetItem/TreeView_SetItem
138static bool IsItemSelected(HWND hwndTV, HTREEITEM hItem)
139{
75afdc2d
VZ
140 TV_ITEM tvi;
141 tvi.mask = TVIF_STATE | TVIF_HANDLE;
142 tvi.stateMask = TVIS_SELECTED;
143 tvi.hItem = hItem;
144
5fe302ef
BW
145 TreeItemUnlocker unlocker(hItem);
146
75afdc2d
VZ
147 if ( !TreeView_GetItem(hwndTV, &tvi) )
148 {
149 wxLogLastError(wxT("TreeView_GetItem"));
150 }
151
152 return (tvi.state & TVIS_SELECTED) != 0;
153}
154
155static bool SelectItem(HWND hwndTV, HTREEITEM hItem, bool select = true)
156{
157 TV_ITEM tvi;
158 tvi.mask = TVIF_STATE | TVIF_HANDLE;
159 tvi.stateMask = TVIS_SELECTED;
160 tvi.state = select ? TVIS_SELECTED : 0;
161 tvi.hItem = hItem;
162
5fe302ef
BW
163 TreeItemUnlocker unlocker(hItem);
164
75afdc2d
VZ
165 if ( TreeView_SetItem(hwndTV, &tvi) == -1 )
166 {
167 wxLogLastError(wxT("TreeView_SetItem"));
168 return false;
169 }
170
171 return true;
172}
173
174static inline void UnselectItem(HWND hwndTV, HTREEITEM htItem)
175{
176 SelectItem(hwndTV, htItem, false);
177}
178
c7d9c476
VZ
179static inline void ToggleItemSelection(HWND hwndTV, HTREEITEM htItem)
180{
181 SelectItem(hwndTV, htItem, !IsItemSelected(hwndTV, htItem));
182}
183
75afdc2d 184// helper function which selects all items in a range and, optionally,
c7d9c476
VZ
185// deselects all the other ones
186//
187// returns true if the selection changed at all or false if nothing changed
188
189// flags for SelectRange()
190enum
191{
192 SR_SIMULATE = 1, // don't do anything, just return true or false
193 SR_UNSELECT_OTHERS = 2 // deselect the items not in range
194};
195
196static bool SelectRange(HWND hwndTV,
75afdc2d
VZ
197 HTREEITEM htFirst,
198 HTREEITEM htLast,
c7d9c476 199 int flags)
75afdc2d
VZ
200{
201 // find the first (or last) item and select it
c7d9c476 202 bool changed = false;
75afdc2d
VZ
203 bool cont = true;
204 HTREEITEM htItem = (HTREEITEM)TreeView_GetRoot(hwndTV);
c7d9c476 205
75afdc2d
VZ
206 while ( htItem && cont )
207 {
208 if ( (htItem == htFirst) || (htItem == htLast) )
209 {
210 if ( !IsItemSelected(hwndTV, htItem) )
211 {
c7d9c476
VZ
212 if ( !(flags & SR_SIMULATE) )
213 {
214 SelectItem(hwndTV, htItem);
215 }
216
217 changed = true;
75afdc2d
VZ
218 }
219
220 cont = false;
221 }
c7d9c476 222 else // not first or last
75afdc2d 223 {
c7d9c476 224 if ( flags & SR_UNSELECT_OTHERS )
75afdc2d 225 {
c7d9c476
VZ
226 if ( IsItemSelected(hwndTV, htItem) )
227 {
228 if ( !(flags & SR_SIMULATE) )
229 UnselectItem(hwndTV, htItem);
230
231 changed = true;
232 }
75afdc2d
VZ
233 }
234 }
235
236 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
237 }
238
239 // select the items in range
240 cont = htFirst != htLast;
241 while ( htItem && cont )
242 {
243 if ( !IsItemSelected(hwndTV, htItem) )
244 {
c7d9c476
VZ
245 if ( !(flags & SR_SIMULATE) )
246 {
247 SelectItem(hwndTV, htItem);
248 }
249
250 changed = true;
75afdc2d
VZ
251 }
252
253 cont = (htItem != htFirst) && (htItem != htLast);
254
255 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
256 }
257
c7d9c476
VZ
258 // optionally deselect the rest
259 if ( flags & SR_UNSELECT_OTHERS )
75afdc2d
VZ
260 {
261 while ( htItem )
262 {
263 if ( IsItemSelected(hwndTV, htItem) )
264 {
c7d9c476
VZ
265 if ( !(flags & SR_SIMULATE) )
266 {
267 UnselectItem(hwndTV, htItem);
268 }
269
270 changed = true;
75afdc2d
VZ
271 }
272
273 htItem = (HTREEITEM)TreeView_GetNextVisible(hwndTV, htItem);
274 }
275 }
276
277 // seems to be necessary - otherwise the just selected items don't always
278 // appear as selected
c7d9c476
VZ
279 if ( !(flags & SR_SIMULATE) )
280 {
281 UpdateWindow(hwndTV);
282 }
283
284 return changed;
75afdc2d
VZ
285}
286
287// helper function which tricks the standard control into changing the focused
288// item without changing anything else (if someone knows why Microsoft doesn't
289// allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
cc87a04c
VZ
290//
291// returns true if the focus was changed, false if the given item was already
292// the focused one
293static bool SetFocus(HWND hwndTV, HTREEITEM htItem)
75afdc2d
VZ
294{
295 // the current focus
296 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(hwndTV);
297
cc87a04c
VZ
298 if ( htItem == htFocus )
299 return false;
300
75afdc2d
VZ
301 if ( htItem )
302 {
cc87a04c
VZ
303 // remember the selection state of the item
304 bool wasSelected = IsItemSelected(hwndTV, htItem);
75afdc2d 305
cc87a04c
VZ
306 if ( htFocus && IsItemSelected(hwndTV, htFocus) )
307 {
308 // prevent the tree from unselecting the old focus which it
309 // would do by default (TreeView_SelectItem unselects the
310 // focused item)
311 TreeView_SelectItem(hwndTV, 0);
312 SelectItem(hwndTV, htFocus);
313 }
75afdc2d 314
cc87a04c 315 TreeView_SelectItem(hwndTV, htItem);
75afdc2d 316
cc87a04c
VZ
317 if ( !wasSelected )
318 {
319 // need to clear the selection which TreeView_SelectItem() gave
320 // us
321 UnselectItem(hwndTV, htItem);
75afdc2d 322 }
cc87a04c 323 //else: was selected, still selected - ok
75afdc2d 324 }
cc87a04c 325 else // reset focus
75afdc2d 326 {
cc87a04c 327 bool wasFocusSelected = IsItemSelected(hwndTV, htFocus);
75afdc2d 328
cc87a04c
VZ
329 // just clear the focus
330 TreeView_SelectItem(hwndTV, 0);
75afdc2d 331
cc87a04c
VZ
332 if ( wasFocusSelected )
333 {
334 // restore the selection state
335 SelectItem(hwndTV, htFocus);
75afdc2d 336 }
75afdc2d 337 }
cc87a04c
VZ
338
339 return true;
75afdc2d
VZ
340}
341
08b7c251
VZ
342// ----------------------------------------------------------------------------
343// private classes
344// ----------------------------------------------------------------------------
2bda0e17 345
08b7c251 346// a convenient wrapper around TV_ITEM struct which adds a ctor
f3ef286f 347#ifdef __VISUALC__
3f7bc32b 348#pragma warning( disable : 4097 ) // inheriting from typedef
f3ef286f
JS
349#endif
350
08b7c251
VZ
351struct wxTreeViewItem : public TV_ITEM
352{
9dfbf520
VZ
353 wxTreeViewItem(const wxTreeItemId& item, // the item handle
354 UINT mask_, // fields which are valid
355 UINT stateMask_ = 0) // for TVIF_STATE only
08b7c251 356 {
a9c1265f
VZ
357 wxZeroMemory(*this);
358
9dfbf520
VZ
359 // hItem member is always valid
360 mask = mask_ | TVIF_HANDLE;
08b7c251 361 stateMask = stateMask_;
3f7bc32b 362 hItem = HITEM(item);
08b7c251
VZ
363 }
364};
f3ef286f 365
4523ebb3
VZ
366// ----------------------------------------------------------------------------
367// This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
368//
369// We need this for a couple of reasons:
370//
371// 1) This class is needed for support of different images: the Win32 common
372// control natively supports only 2 images (the normal one and another for the
373// selected state). We wish to provide support for 2 more of them for folder
374// items (i.e. those which have children): for expanded state and for expanded
375// selected state. For this we use this structure to store the additional items
376// images.
377//
378// 2) This class is also needed to hold the HITEM so that we can sort
379// it correctly in the MSW sort callback.
380//
381// In addition it makes other workarounds such as this easier and helps
382// simplify the code.
383// ----------------------------------------------------------------------------
384
385class wxTreeItemParam
386{
387public:
388 wxTreeItemParam()
4523ebb3 389 {
20f4b566
VZ
390 m_data = NULL;
391
4523ebb3
VZ
392 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
393 {
394 m_images[n] = -1;
395 }
396 }
397
398 // dtor deletes the associated data as well
399 virtual ~wxTreeItemParam() { delete m_data; }
400
401 // accessors
402 // get the real data associated with the item
403 wxTreeItemData *GetData() const { return m_data; }
404 // change it
405 void SetData(wxTreeItemData *data) { m_data = data; }
406
407 // do we have such image?
408 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
1b182562
VZ
409 // get image, falling back to the other images if this one is not
410 // specified
411 int GetImage(wxTreeItemIcon which) const
412 {
413 int image = m_images[which];
414 if ( image == -1 )
415 {
416 switch ( which )
417 {
418 case wxTreeItemIcon_SelectedExpanded:
419 image = GetImage(wxTreeItemIcon_Expanded);
420 if ( image != -1 )
421 break;
422 //else: fall through
423
424 case wxTreeItemIcon_Selected:
425 case wxTreeItemIcon_Expanded:
426 image = GetImage(wxTreeItemIcon_Normal);
427 break;
428
429 case wxTreeItemIcon_Normal:
430 // no fallback
431 break;
432
433 default:
434 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
435 }
436 }
437
438 return image;
439 }
440 // change the given image
4523ebb3
VZ
441 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
442
443 // get item
444 const wxTreeItemId& GetItem() const { return m_item; }
445 // set item
446 void SetItem(const wxTreeItemId& item) { m_item = item; }
447
448protected:
449 // all the images associated with the item
450 int m_images[wxTreeItemIcon_Max];
451
452 // item for sort callbacks
453 wxTreeItemId m_item;
454
455 // the real client data
456 wxTreeItemData *m_data;
457
c0c133e1 458 wxDECLARE_NO_COPY_CLASS(wxTreeItemParam);
4523ebb3
VZ
459};
460
a9c1265f
VZ
461// wxVirutalNode is used in place of a single root when 'hidden' root is
462// specified.
efa38350 463class wxVirtualNode : public wxTreeViewItem
a9c1265f
VZ
464{
465public:
4523ebb3 466 wxVirtualNode(wxTreeItemParam *param)
efa38350 467 : wxTreeViewItem(TVI_ROOT, 0)
a9c1265f 468 {
4523ebb3 469 m_param = param;
a9c1265f
VZ
470 }
471
472 ~wxVirtualNode()
473 {
4523ebb3 474 delete m_param;
a9c1265f
VZ
475 }
476
4523ebb3
VZ
477 wxTreeItemParam *GetParam() const { return m_param; }
478 void SetParam(wxTreeItemParam *param) { delete m_param; m_param = param; }
a9c1265f
VZ
479
480private:
4523ebb3 481 wxTreeItemParam *m_param;
22f3361e 482
c0c133e1 483 wxDECLARE_NO_COPY_CLASS(wxVirtualNode);
a9c1265f
VZ
484};
485
f3ef286f 486#ifdef __VISUALC__
197dd9af 487#pragma warning( default : 4097 )
f3ef286f 488#endif
2bda0e17 489
a9c1265f
VZ
490// a macro to get the virtual root, returns NULL if none
491#define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
492
04cd30de 493// returns true if the item is the virtual root
a9c1265f
VZ
494#define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
495
9dfbf520 496// a class which encapsulates the tree traversal logic: it vists all (unless
04cd30de 497// OnVisit() returns false) items under the given one
9dfbf520
VZ
498class wxTreeTraversal
499{
500public:
501 wxTreeTraversal(const wxTreeCtrl *tree)
502 {
503 m_tree = tree;
504 }
505
cc11ded1
VZ
506 // give it a virtual dtor: not really needed as the class is never used
507 // polymorphically and not even allocated on heap at all, but this is safer
508 // (in case it ever is) and silences the compiler warnings for now
509 virtual ~wxTreeTraversal() { }
510
9dfbf520 511 // do traverse the tree: visit all items (recursively by default) under the
04cd30de
RL
512 // given one; return true if all items were traversed or false if the
513 // traversal was aborted because OnVisit returned false
514 bool DoTraverse(const wxTreeItemId& root, bool recursively = true);
9dfbf520
VZ
515
516 // override this function to do whatever is needed for each item, return
04cd30de 517 // false to stop traversing
9dfbf520
VZ
518 virtual bool OnVisit(const wxTreeItemId& item) = 0;
519
520protected:
521 const wxTreeCtrl *GetTree() const { return m_tree; }
522
523private:
524 bool Traverse(const wxTreeItemId& root, bool recursively);
525
526 const wxTreeCtrl *m_tree;
22f3361e 527
c0c133e1 528 wxDECLARE_NO_COPY_CLASS(wxTreeTraversal);
9dfbf520
VZ
529};
530
74b31181
VZ
531// internal class for getting the selected items
532class TraverseSelections : public wxTreeTraversal
533{
534public:
535 TraverseSelections(const wxTreeCtrl *tree,
536 wxArrayTreeItemIds& selections)
537 : wxTreeTraversal(tree), m_selections(selections)
538 {
539 m_selections.Empty();
540
b9e9b40c
JS
541 if (tree->GetCount() > 0)
542 DoTraverse(tree->GetRootItem());
74b31181
VZ
543 }
544
545 virtual bool OnVisit(const wxTreeItemId& item)
546 {
0228081f
VZ
547 const wxTreeCtrl * const tree = GetTree();
548
a9c1265f 549 // can't visit a virtual node.
0228081f 550 if ( (tree->GetRootItem() == item) && tree->HasFlag(wxTR_HIDE_ROOT) )
a9c1265f 551 {
04cd30de 552 return true;
a9c1265f
VZ
553 }
554
0228081f 555 if ( ::IsItemSelected(GetHwndOf(tree), HITEM(item)) )
74b31181
VZ
556 {
557 m_selections.Add(item);
558 }
559
04cd30de 560 return true;
74b31181
VZ
561 }
562
e47c4d48
VZ
563 size_t GetCount() const { return m_selections.GetCount(); }
564
74b31181
VZ
565private:
566 wxArrayTreeItemIds& m_selections;
fc7a2a60 567
c0c133e1 568 wxDECLARE_NO_COPY_CLASS(TraverseSelections);
74b31181
VZ
569};
570
571// internal class for counting tree items
572class TraverseCounter : public wxTreeTraversal
573{
574public:
575 TraverseCounter(const wxTreeCtrl *tree,
576 const wxTreeItemId& root,
577 bool recursively)
578 : wxTreeTraversal(tree)
579 {
580 m_count = 0;
581
582 DoTraverse(root, recursively);
583 }
584
33ac7e6f 585 virtual bool OnVisit(const wxTreeItemId& WXUNUSED(item))
74b31181
VZ
586 {
587 m_count++;
588
04cd30de 589 return true;
74b31181
VZ
590 }
591
592 size_t GetCount() const { return m_count; }
593
594private:
595 size_t m_count;
fc7a2a60 596
c0c133e1 597 wxDECLARE_NO_COPY_CLASS(TraverseCounter);
74b31181
VZ
598};
599
23f681ec 600// ----------------------------------------------------------------------------
3f7bc32b 601// wxWin macros
08b7c251
VZ
602// ----------------------------------------------------------------------------
603
786a2425 604#if wxUSE_EXTENDED_RTTI
bc9fb572
JS
605WX_DEFINE_FLAGS( wxTreeCtrlStyle )
606
3ff066a4 607wxBEGIN_FLAGS( wxTreeCtrlStyle )
bc9fb572
JS
608 // new style border flags, we put them first to
609 // use them for streaming out
3ff066a4
SC
610 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
611 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
612 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
613 wxFLAGS_MEMBER(wxBORDER_RAISED)
614 wxFLAGS_MEMBER(wxBORDER_STATIC)
615 wxFLAGS_MEMBER(wxBORDER_NONE)
5e7718a2 616
bc9fb572 617 // old style border flags
3ff066a4
SC
618 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
619 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
620 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
621 wxFLAGS_MEMBER(wxRAISED_BORDER)
622 wxFLAGS_MEMBER(wxSTATIC_BORDER)
cb0afb26 623 wxFLAGS_MEMBER(wxBORDER)
bc9fb572
JS
624
625 // standard window styles
3ff066a4
SC
626 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
627 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
628 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
629 wxFLAGS_MEMBER(wxWANTS_CHARS)
cb0afb26 630 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
3ff066a4
SC
631 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
632 wxFLAGS_MEMBER(wxVSCROLL)
633 wxFLAGS_MEMBER(wxHSCROLL)
634
635 wxFLAGS_MEMBER(wxTR_EDIT_LABELS)
636 wxFLAGS_MEMBER(wxTR_NO_BUTTONS)
637 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS)
638 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS)
639 wxFLAGS_MEMBER(wxTR_NO_LINES)
640 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT)
641 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT)
642 wxFLAGS_MEMBER(wxTR_HIDE_ROOT)
643 wxFLAGS_MEMBER(wxTR_ROW_LINES)
644 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT)
645 wxFLAGS_MEMBER(wxTR_SINGLE)
646 wxFLAGS_MEMBER(wxTR_MULTIPLE)
70cab5b7 647#if WXWIN_COMPATIBILITY_2_8
3ff066a4 648 wxFLAGS_MEMBER(wxTR_EXTENDED)
70cab5b7 649#endif
3ff066a4
SC
650 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE)
651
652wxEND_FLAGS( wxTreeCtrlStyle )
bc9fb572 653
786a2425
SC
654IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl, wxControl,"wx/treectrl.h")
655
3ff066a4 656wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl)
5e7718a2 657 wxEVENT_PROPERTY( TextUpdated , wxEVT_COMMAND_TEXT_UPDATED , wxCommandEvent )
3ff066a4 658 wxEVENT_RANGE_PROPERTY( TreeEvent , wxEVT_COMMAND_TREE_BEGIN_DRAG , wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK , wxTreeEvent )
af498247 659 wxPROPERTY_FLAGS( WindowStyle , wxTreeCtrlStyle , long , SetWindowStyleFlag , GetWindowStyleFlag , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
3ff066a4 660wxEND_PROPERTIES_TABLE()
786a2425 661
3ff066a4
SC
662wxBEGIN_HANDLERS_TABLE(wxTreeCtrl)
663wxEND_HANDLERS_TABLE()
786a2425 664
5e7718a2 665wxCONSTRUCTOR_5( wxTreeCtrl , wxWindow* , Parent , wxWindowID , Id , wxPoint , Position , wxSize , Size , long , WindowStyle )
786a2425 666#else
23f681ec 667IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
786a2425 668#endif
2bda0e17 669
08b7c251 670// ----------------------------------------------------------------------------
deb1de30 671// constants
08b7c251
VZ
672// ----------------------------------------------------------------------------
673
deb1de30
VZ
674// indices in gs_expandEvents table below
675enum
676{
677 IDX_COLLAPSE,
678 IDX_EXPAND,
679 IDX_WHAT_MAX
680};
681
682enum
683{
684 IDX_DONE,
685 IDX_DOING,
686 IDX_HOW_MAX
687};
688
689// handy table for sending events - it has to be initialized during run-time
690// now so can't be const any more
691static /* const */ wxEventType gs_expandEvents[IDX_WHAT_MAX][IDX_HOW_MAX];
692
693/*
694 but logically it's a const table with the following entries:
695=
2bda0e17 696{
08b7c251
VZ
697 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
698 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
699};
deb1de30 700*/
08b7c251
VZ
701
702// ============================================================================
703// implementation
704// ============================================================================
705
9dfbf520
VZ
706// ----------------------------------------------------------------------------
707// tree traversal
708// ----------------------------------------------------------------------------
709
710bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
711{
712 if ( !OnVisit(root) )
04cd30de 713 return false;
9dfbf520
VZ
714
715 return Traverse(root, recursively);
716}
717
718bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
719{
ee4b2721 720 wxTreeItemIdValue cookie;
9dfbf520
VZ
721 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
722 while ( child.IsOk() )
723 {
724 // depth first traversal
04cd30de
RL
725 if ( recursively && !Traverse(child, true) )
726 return false;
9dfbf520
VZ
727
728 if ( !OnVisit(child) )
04cd30de 729 return false;
9dfbf520
VZ
730
731 child = m_tree->GetNextChild(root, cookie);
732 }
733
04cd30de 734 return true;
9dfbf520
VZ
735}
736
08b7c251
VZ
737// ----------------------------------------------------------------------------
738// construction and destruction
739// ----------------------------------------------------------------------------
740
741void wxTreeCtrl::Init()
742{
08b7c251 743 m_textCtrl = NULL;
04cd30de 744 m_hasAnyAttr = false;
68d9be05 745#if wxUSE_DRAGIMAGE
23f681ec 746 m_dragImage = NULL;
68d9be05 747#endif
a9c1265f 748 m_pVirtualRoot = NULL;
c7d9c476
VZ
749 m_dragStarted = false;
750 m_focusLost = true;
7dac12ef 751 m_changingSelection = false;
c7d9c476 752 m_triggerStateImageClick = false;
9ab5ef13 753
deb1de30
VZ
754 // initialize the global array of events now as it can't be done statically
755 // with the wxEVT_XXX values being allocated during run-time only
756 gs_expandEvents[IDX_COLLAPSE][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED;
757 gs_expandEvents[IDX_COLLAPSE][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING;
758 gs_expandEvents[IDX_EXPAND][IDX_DONE] = wxEVT_COMMAND_TREE_ITEM_EXPANDED;
759 gs_expandEvents[IDX_EXPAND][IDX_DOING] = wxEVT_COMMAND_TREE_ITEM_EXPANDING;
2bda0e17
KB
760}
761
9dfbf520
VZ
762bool wxTreeCtrl::Create(wxWindow *parent,
763 wxWindowID id,
764 const wxPoint& pos,
765 const wxSize& size,
766 long style,
767 const wxValidator& validator,
08b7c251 768 const wxString& name)
2bda0e17 769{
08b7c251 770 Init();
2bda0e17 771
7699361c
JS
772 if ( (style & wxBORDER_MASK) == wxBORDER_DEFAULT )
773 style |= wxBORDER_SUNKEN;
774
9dfbf520 775 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
04cd30de 776 return false;
2bda0e17 777
823b140b 778 WXDWORD exStyle = 0;
7699361c
JS
779 DWORD wstyle = MSWGetStyle(m_windowStyle, & exStyle);
780 wstyle |= WS_TABSTOP | TVS_SHOWSELALWAYS;
2bda0e17 781
c7d9c476 782 if ( !(m_windowStyle & wxTR_NO_LINES) )
63da7df7 783 wstyle |= TVS_HASLINES;
08b7c251
VZ
784 if ( m_windowStyle & wxTR_HAS_BUTTONS )
785 wstyle |= TVS_HASBUTTONS;
2bda0e17 786
08b7c251
VZ
787 if ( m_windowStyle & wxTR_EDIT_LABELS )
788 wstyle |= TVS_EDITLABELS;
2bda0e17 789
08b7c251
VZ
790 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
791 wstyle |= TVS_LINESATROOT;
deb1de30 792
c6f4913a 793 if ( m_windowStyle & wxTR_FULL_ROW_HIGHLIGHT )
deb1de30 794 {
2a1f999f 795 if ( wxApp::GetComCtl32Version() >= 471 )
c6f4913a
VS
796 wstyle |= TVS_FULLROWSELECT;
797 }
798
f3f71703 799#if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
156194e1
JS
800 // Need so that TVN_GETINFOTIP messages will be sent
801 wstyle |= TVS_INFOTIP;
676d6550 802#endif
5e7718a2 803
08b7c251 804 // Create the tree control.
8779cca3 805 if ( !MSWCreateControl(WC_TREEVIEW, wstyle, pos, size) )
04cd30de 806 return false;
9dfbf520 807
f6bcfd97 808#if wxUSE_COMCTL32_SAFELY
a756f210 809 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97
BP
810 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
811#elif 1
a756f210 812 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97 813 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
fbdcff4a
JS
814#else
815 // This works around a bug in the Windows tree control whereby for some versions
816 // of comctrl32, setting any colour actually draws the background in black.
817 // This will initialise the background to the system colour.
f6bcfd97
BP
818 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
819 // Assume the user has an updated comctl32.dll.
fbdcff4a 820 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0,-1);
a756f210 821 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW));
f6bcfd97 822 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
fbdcff4a
JS
823#endif
824
9a63feff
VZ
825 wxSetCCUnicodeFormat(GetHwnd());
826
04cd30de 827 return true;
2bda0e17
KB
828}
829
08b7c251 830wxTreeCtrl::~wxTreeCtrl()
2bda0e17 831{
696e1ea0
VZ
832 // delete any attributes
833 if ( m_hasAnyAttr )
834 {
ee4b2721 835 WX_CLEAR_HASH_MAP(wxMapTreeAttr, m_attrs);
696e1ea0
VZ
836
837 // prevent TVN_DELETEITEM handler from deleting the attributes again!
04cd30de 838 m_hasAnyAttr = false;
696e1ea0
VZ
839 }
840
08b7c251 841 DeleteTextCtrl();
2bda0e17 842
08b7c251 843 // delete user data to prevent memory leaks
a9c1265f 844 // also deletes hidden root node storage.
08b7c251 845 DeleteAllItems();
2bda0e17
KB
846}
847
08b7c251
VZ
848// ----------------------------------------------------------------------------
849// accessors
850// ----------------------------------------------------------------------------
2bda0e17 851
39c7a53c
VZ
852/* static */ wxVisualAttributes
853wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant)
854{
855 wxVisualAttributes attrs = GetCompositeControlsDefaultAttributes(variant);
856
857 // common controls have their own default font
858 attrs.font = wxGetCCDefaultFont();
859
860 return attrs;
861}
862
863
08b7c251 864// simple wrappers which add error checking in debug mode
2bda0e17 865
4523ebb3 866bool wxTreeCtrl::DoGetItem(wxTreeViewItem *tvItem) const
2bda0e17 867{
04cd30de 868 wxCHECK_MSG( tvItem->hItem != TVI_ROOT, false,
a9c1265f
VZ
869 _T("can't retrieve virtual root item") );
870
d220ae32 871 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
2bda0e17 872 {
f6bcfd97 873 wxLogLastError(wxT("TreeView_GetItem"));
08b7c251 874
04cd30de 875 return false;
08b7c251
VZ
876 }
877
04cd30de 878 return true;
2bda0e17
KB
879}
880
4523ebb3 881void wxTreeCtrl::DoSetItem(wxTreeViewItem *tvItem)
2bda0e17 882{
5fe302ef
BW
883 TreeItemUnlocker unlocker(tvItem->hItem);
884
d220ae32 885 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
2bda0e17 886 {
f6bcfd97 887 wxLogLastError(wxT("TreeView_SetItem"));
08b7c251 888 }
2bda0e17
KB
889}
890
027d45e8 891unsigned int wxTreeCtrl::GetCount() const
2bda0e17 892{
027d45e8 893 return (unsigned int)TreeView_GetCount(GetHwnd());
2bda0e17
KB
894}
895
08b7c251 896unsigned int wxTreeCtrl::GetIndent() const
2bda0e17 897{
d220ae32 898 return TreeView_GetIndent(GetHwnd());
2bda0e17
KB
899}
900
08b7c251 901void wxTreeCtrl::SetIndent(unsigned int indent)
2bda0e17 902{
d220ae32 903 TreeView_SetIndent(GetHwnd(), indent);
2bda0e17
KB
904}
905
08b7c251 906void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
2bda0e17 907{
08b7c251 908 // no error return
8ecd5de2
WS
909 (void) TreeView_SetImageList(GetHwnd(),
910 imageList ? imageList->GetHIMAGELIST() : 0,
911 which);
2bda0e17
KB
912}
913
08b7c251 914void wxTreeCtrl::SetImageList(wxImageList *imageList)
2bda0e17 915{
a9c1265f
VZ
916 if (m_ownsImageListNormal)
917 delete m_imageListNormal;
918
08b7c251 919 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
04cd30de 920 m_ownsImageListNormal = false;
2bda0e17
KB
921}
922
08b7c251 923void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
2bda0e17 924{
0377efa2 925 if (m_ownsImageListState) delete m_imageListState;
08b7c251 926 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
04cd30de 927 m_ownsImageListState = false;
0377efa2
VS
928}
929
33961d59
RR
930size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
931 bool recursively) const
932{
05b2a432 933 wxCHECK_MSG( item.IsOk(), 0u, wxT("invalid tree item") );
23fd5130 934
05b2a432 935 TraverseCounter counter(this, item, recursively);
73974df1 936 return counter.GetCount() - 1;
23fd5130
VZ
937}
938
bb448552
VZ
939// ----------------------------------------------------------------------------
940// control colours
941// ----------------------------------------------------------------------------
942
943bool wxTreeCtrl::SetBackgroundColour(const wxColour &colour)
944{
f6bcfd97 945#if !wxUSE_COMCTL32_SAFELY
bb448552 946 if ( !wxWindowBase::SetBackgroundColour(colour) )
04cd30de 947 return false;
bb448552 948
bfbb0b4c 949 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR, 0, colour.GetPixel());
f6bcfd97 950#endif
bb448552 951
04cd30de 952 return true;
bb448552
VZ
953}
954
955bool wxTreeCtrl::SetForegroundColour(const wxColour &colour)
956{
f6bcfd97 957#if !wxUSE_COMCTL32_SAFELY
bb448552 958 if ( !wxWindowBase::SetForegroundColour(colour) )
04cd30de 959 return false;
bb448552 960
bfbb0b4c 961 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR, 0, colour.GetPixel());
f6bcfd97 962#endif
bb448552 963
04cd30de 964 return true;
bb448552
VZ
965}
966
08b7c251
VZ
967// ----------------------------------------------------------------------------
968// Item access
969// ----------------------------------------------------------------------------
970
0228081f
VZ
971bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId& item) const
972{
973 return HITEM(item) == TVI_ROOT && HasFlag(wxTR_HIDE_ROOT);
974}
975
08b7c251 976wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
2bda0e17 977{
f31a4098 978 wxCHECK_MSG( item.IsOk(), wxEmptyString, wxT("invalid tree item") );
05b2a432 979
837e5743 980 wxChar buf[512]; // the size is arbitrary...
02ce7b72 981
08b7c251
VZ
982 wxTreeViewItem tvItem(item, TVIF_TEXT);
983 tvItem.pszText = buf;
984 tvItem.cchTextMax = WXSIZEOF(buf);
985 if ( !DoGetItem(&tvItem) )
986 {
987 // don't return some garbage which was on stack, but an empty string
223d09f6 988 buf[0] = wxT('\0');
08b7c251 989 }
2bda0e17 990
08b7c251
VZ
991 return wxString(buf);
992}
2bda0e17 993
08b7c251
VZ
994void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
995{
05b2a432
RD
996 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
997
a9c1265f
VZ
998 if ( IS_VIRTUAL_ROOT(item) )
999 return;
1000
08b7c251 1001 wxTreeViewItem tvItem(item, TVIF_TEXT);
c9f78968 1002 tvItem.pszText = (wxChar *)text.wx_str(); // conversion is ok
08b7c251 1003 DoSetItem(&tvItem);
44fbc477
VZ
1004
1005 // when setting the text of the item being edited, the text control should
1006 // be updated to reflect the new text as well, otherwise calling
1007 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
1008 //
1009 // don't use GetEditControl() here because m_textCtrl is not set yet
1010 HWND hwndEdit = TreeView_GetEditControl(GetHwnd());
1011 if ( hwndEdit )
1012 {
1a4088e1 1013 if ( item == m_idEdited )
44fbc477 1014 {
e0a050e3 1015 ::SetWindowText(hwndEdit, text.wx_str());
44fbc477
VZ
1016 }
1017 }
08b7c251 1018}
2bda0e17 1019
4523ebb3
VZ
1020int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
1021 wxTreeItemIcon which) const
74b31181 1022{
4523ebb3
VZ
1023 wxCHECK_MSG( item.IsOk(), -1, wxT("invalid tree item") );
1024
0228081f 1025 if ( IsHiddenRoot(item) )
74b31181 1026 {
4523ebb3 1027 // no images for hidden root item
74b31181
VZ
1028 return -1;
1029 }
1030
4523ebb3
VZ
1031 wxTreeItemParam *param = GetItemParam(item);
1032
1b182562 1033 return param && param->HasImage(which) ? param->GetImage(which) : -1;
74b31181
VZ
1034}
1035
4523ebb3
VZ
1036void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
1037 wxTreeItemIcon which)
74b31181 1038{
4523ebb3
VZ
1039 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1040 wxCHECK_RET( which >= 0 &&
1041 which < wxTreeItemIcon_Max,
1042 wxT("invalid image index"));
1043
1044
0228081f 1045 if ( IsHiddenRoot(item) )
74b31181 1046 {
4523ebb3 1047 // no images for hidden root item
74b31181
VZ
1048 return;
1049 }
1050
4523ebb3
VZ
1051 wxTreeItemParam *data = GetItemParam(item);
1052 if ( !data )
1053 return;
74b31181
VZ
1054
1055 data->SetImage(image, which);
f5bed7a8
VS
1056
1057 RefreshItem(item);
74b31181
VZ
1058}
1059
4523ebb3 1060wxTreeItemParam *wxTreeCtrl::GetItemParam(const wxTreeItemId& item) const
2bda0e17 1061{
05b2a432
RD
1062 wxCHECK_MSG( item.IsOk(), NULL, wxT("invalid tree item") );
1063
08b7c251 1064 wxTreeViewItem tvItem(item, TVIF_PARAM);
a9c1265f 1065
ae322a48
VZ
1066 // hidden root may still have data.
1067 if ( IS_VIRTUAL_ROOT(item) )
1068 {
1069 return GET_VIRTUAL_ROOT()->GetParam();
1070 }
1071
1072 // visible node.
1073 if ( !DoGetItem(&tvItem) )
08b7c251
VZ
1074 {
1075 return NULL;
1076 }
2bda0e17 1077
4523ebb3 1078 return (wxTreeItemParam *)tvItem.lParam;
2bda0e17
KB
1079}
1080
c7d9c476
VZ
1081bool wxTreeCtrl::HandleTreeEvent(wxTreeEvent& event) const
1082{
1083 if ( event.m_item.IsOk() )
1084 {
1085 event.SetClientObject(GetItemData(event.m_item));
1086 }
1087
1088 return HandleWindowEvent(event);
1089}
1090
4523ebb3 1091wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
2bda0e17 1092{
4523ebb3 1093 wxTreeItemParam *data = GetItemParam(item);
05b2a432 1094
4523ebb3
VZ
1095 return data ? data->GetData() : NULL;
1096}
a9c1265f 1097
4523ebb3
VZ
1098void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
1099{
188781db
VZ
1100 // first, associate this piece of data with this item
1101 if ( data )
1102 {
1103 data->SetId(item);
1104 }
1105
4523ebb3 1106 wxTreeItemParam *param = GetItemParam(item);
74b31181 1107
4523ebb3 1108 wxCHECK_RET( param, wxT("failed to change tree items data") );
74b31181 1109
4523ebb3 1110 param->SetData(data);
08b7c251 1111}
2bda0e17 1112
3a5a2f56
VZ
1113void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
1114{
05b2a432
RD
1115 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1116
a9c1265f
VZ
1117 if ( IS_VIRTUAL_ROOT(item) )
1118 return;
1119
3a5a2f56
VZ
1120 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
1121 tvItem.cChildren = (int)has;
1122 DoSetItem(&tvItem);
1123}
1124
add28c55
VZ
1125void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
1126{
05b2a432
RD
1127 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1128
a9c1265f
VZ
1129 if ( IS_VIRTUAL_ROOT(item) )
1130 return;
1131
add28c55
VZ
1132 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1133 tvItem.state = bold ? TVIS_BOLD : 0;
1134 DoSetItem(&tvItem);
1135}
1136
58a8ab88
JS
1137void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
1138{
a9c1265f
VZ
1139 if ( IS_VIRTUAL_ROOT(item) )
1140 return;
1141
58a8ab88
JS
1142 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
1143 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
1144 DoSetItem(&tvItem);
1145}
1146
d00407b2
VZ
1147void wxTreeCtrl::RefreshItem(const wxTreeItemId& item)
1148{
a9c1265f
VZ
1149 if ( IS_VIRTUAL_ROOT(item) )
1150 return;
1151
d00407b2
VZ
1152 wxRect rect;
1153 if ( GetBoundingRect(item, rect) )
1154 {
1155 RefreshRect(rect);
1156 }
1157}
1158
2b5f62a0
VZ
1159wxColour wxTreeCtrl::GetItemTextColour(const wxTreeItemId& item) const
1160{
05b2a432 1161 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
2b5f62a0 1162
05b2a432 1163 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1164 return it == m_attrs.end() ? wxNullColour : it->second->GetTextColour();
2b5f62a0
VZ
1165}
1166
1167wxColour wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId& item) const
1168{
05b2a432 1169 wxCHECK_MSG( item.IsOk(), wxNullColour, wxT("invalid tree item") );
2b5f62a0 1170
05b2a432 1171 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1172 return it == m_attrs.end() ? wxNullColour : it->second->GetBackgroundColour();
2b5f62a0
VZ
1173}
1174
1175wxFont wxTreeCtrl::GetItemFont(const wxTreeItemId& item) const
1176{
05b2a432 1177 wxCHECK_MSG( item.IsOk(), wxNullFont, wxT("invalid tree item") );
2b5f62a0 1178
05b2a432 1179 wxMapTreeAttr::const_iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1180 return it == m_attrs.end() ? wxNullFont : it->second->GetFont();
2b5f62a0
VZ
1181}
1182
696e1ea0
VZ
1183void wxTreeCtrl::SetItemTextColour(const wxTreeItemId& item,
1184 const wxColour& col)
1185{
05b2a432
RD
1186 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1187
ee4b2721 1188 wxTreeItemAttr *attr;
f888d614 1189 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1190 if ( it == m_attrs.end() )
696e1ea0 1191 {
ee4b2721
VZ
1192 m_hasAnyAttr = true;
1193
f888d614 1194 m_attrs[item.m_pItem] =
696e1ea0 1195 attr = new wxTreeItemAttr;
ee4b2721
VZ
1196 }
1197 else
1198 {
1199 attr = it->second;
696e1ea0
VZ
1200 }
1201
1202 attr->SetTextColour(col);
d00407b2
VZ
1203
1204 RefreshItem(item);
696e1ea0
VZ
1205}
1206
1207void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
1208 const wxColour& col)
1209{
05b2a432
RD
1210 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1211
ee4b2721 1212 wxTreeItemAttr *attr;
f888d614 1213 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1214 if ( it == m_attrs.end() )
696e1ea0 1215 {
ee4b2721
VZ
1216 m_hasAnyAttr = true;
1217
f888d614 1218 m_attrs[item.m_pItem] =
696e1ea0 1219 attr = new wxTreeItemAttr;
ee4b2721
VZ
1220 }
1221 else // already in the hash
1222 {
1223 attr = it->second;
696e1ea0
VZ
1224 }
1225
1226 attr->SetBackgroundColour(col);
d00407b2
VZ
1227
1228 RefreshItem(item);
696e1ea0
VZ
1229}
1230
1231void wxTreeCtrl::SetItemFont(const wxTreeItemId& item, const wxFont& font)
1232{
05b2a432
RD
1233 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
1234
ee4b2721 1235 wxTreeItemAttr *attr;
f888d614 1236 wxMapTreeAttr::iterator it = m_attrs.find(item.m_pItem);
ee4b2721 1237 if ( it == m_attrs.end() )
696e1ea0 1238 {
ee4b2721
VZ
1239 m_hasAnyAttr = true;
1240
f888d614 1241 m_attrs[item.m_pItem] =
696e1ea0 1242 attr = new wxTreeItemAttr;
ee4b2721
VZ
1243 }
1244 else // already in the hash
1245 {
1246 attr = it->second;
696e1ea0
VZ
1247 }
1248
1249 attr->SetFont(font);
d00407b2 1250
2bd16277
RD
1251 // Reset the item's text to ensure that the bounding rect will be adjusted
1252 // for the new font.
1253 SetItemText(item, GetItemText(item));
72e61024 1254
d00407b2 1255 RefreshItem(item);
696e1ea0
VZ
1256}
1257
08b7c251
VZ
1258// ----------------------------------------------------------------------------
1259// Item status
1260// ----------------------------------------------------------------------------
2bda0e17 1261
08b7c251
VZ
1262bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
1263{
bfbb0b4c 1264 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1265
2b5f62a0
VZ
1266 if ( item == wxTreeItemId(TVI_ROOT) )
1267 {
1268 // virtual (hidden) root is never visible
04cd30de 1269 return false;
2b5f62a0
VZ
1270 }
1271
add28c55 1272 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
08b7c251 1273 RECT rect;
955be36c
VZ
1274
1275 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1276 // the HTREEITEM with TVM_GETITEMRECT
ee4b2721 1277 *(HTREEITEM *)&rect = HITEM(item);
955be36c 1278
caea927d
VZ
1279 // true means to get rect for just the text, not the whole line
1280 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT, true, (LPARAM)&rect) )
1281 {
1282 // if TVM_GETITEMRECT returned false, then the item is definitely not
1283 // visible (because its parent is not expanded)
1284 return false;
1285 }
1286
1287 // however if it returned true, the item might still be outside the
1288 // currently visible part of the tree, test for it (notice that partly
1289 // visible means visible here)
1290 return rect.bottom > 0 && rect.top < GetClientSize().y;
2bda0e17
KB
1291}
1292
08b7c251 1293bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
2bda0e17 1294{
bfbb0b4c 1295 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1296
fa97ee24
VZ
1297 if ( IS_VIRTUAL_ROOT(item) )
1298 {
1299 wxTreeItemIdValue cookie;
1300 return GetFirstChild(item, cookie).IsOk();
1301 }
1302
08b7c251 1303 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
ae322a48 1304 DoGetItem(&tvItem);
2bda0e17 1305
08b7c251 1306 return tvItem.cChildren != 0;
2bda0e17
KB
1307}
1308
08b7c251 1309bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
2bda0e17 1310{
bfbb0b4c 1311 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
2bda0e17 1312
08b7c251
VZ
1313 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
1314 DoGetItem(&tvItem);
2bda0e17 1315
08b7c251 1316 return (tvItem.state & TVIS_EXPANDED) != 0;
2bda0e17
KB
1317}
1318
08b7c251 1319bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
2bda0e17 1320{
bfbb0b4c 1321 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1322
08b7c251
VZ
1323 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
1324 DoGetItem(&tvItem);
2bda0e17 1325
08b7c251 1326 return (tvItem.state & TVIS_SELECTED) != 0;
2bda0e17
KB
1327}
1328
add28c55
VZ
1329bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
1330{
bfbb0b4c 1331 wxCHECK_MSG( item.IsOk(), false, wxT("invalid tree item") );
05b2a432 1332
add28c55
VZ
1333 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
1334 DoGetItem(&tvItem);
1335
1336 return (tvItem.state & TVIS_BOLD) != 0;
1337}
1338
08b7c251
VZ
1339// ----------------------------------------------------------------------------
1340// navigation
1341// ----------------------------------------------------------------------------
2bda0e17 1342
08b7c251
VZ
1343wxTreeItemId wxTreeCtrl::GetRootItem() const
1344{
a9c1265f
VZ
1345 // Root may be real (visible) or virtual (hidden).
1346 if ( GET_VIRTUAL_ROOT() )
1347 return TVI_ROOT;
1348
ee4b2721 1349 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
08b7c251 1350}
2bda0e17 1351
08b7c251
VZ
1352wxTreeItemId wxTreeCtrl::GetSelection() const
1353{
c7d9c476 1354 wxCHECK_MSG( !HasFlag(wxTR_MULTIPLE), wxTreeItemId(),
223d09f6 1355 wxT("this only works with single selection controls") );
9dfbf520 1356
ee4b2721 1357 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2bda0e17
KB
1358}
1359
99006e44 1360wxTreeItemId wxTreeCtrl::GetItemParent(const wxTreeItemId& item) const
2bda0e17 1361{
05b2a432
RD
1362 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1363
c9b142c9
VZ
1364 HTREEITEM hItem;
1365
1366 if ( IS_VIRTUAL_ROOT(item) )
1367 {
1368 // no parent for the virtual root
1369 hItem = 0;
1370 }
1371 else // normal item
a9c1265f 1372 {
c9b142c9
VZ
1373 hItem = TreeView_GetParent(GetHwnd(), HITEM(item));
1374 if ( !hItem && HasFlag(wxTR_HIDE_ROOT) )
1375 {
1376 // the top level items should have the virtual root as their parent
1377 hItem = TVI_ROOT;
1378 }
a9c1265f
VZ
1379 }
1380
ee4b2721 1381 return wxTreeItemId(hItem);
08b7c251 1382}
2bda0e17 1383
08b7c251 1384wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
ee4b2721 1385 wxTreeItemIdValue& cookie) const
08b7c251 1386{
05b2a432
RD
1387 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1388
08b7c251 1389 // remember the last child returned in 'cookie'
ee4b2721
VZ
1390 cookie = TreeView_GetChild(GetHwnd(), HITEM(item));
1391
1392 return wxTreeItemId(cookie);
1393}
1394
1395wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
1396 wxTreeItemIdValue& cookie) const
1397{
44d60c0b
WS
1398 wxTreeItemId fromCookie(cookie);
1399
1400 HTREEITEM hitem = HITEM(fromCookie);
1401
1402 hitem = TreeView_GetNextSibling(GetHwnd(), hitem);
1403
1404 wxTreeItemId item(hitem);
1405
ee4b2721
VZ
1406 cookie = item.m_pItem;
1407
1408 return item;
1409}
2bda0e17 1410
978f38c2
VZ
1411wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
1412{
05b2a432
RD
1413 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1414
978f38c2 1415 // can this be done more efficiently?
ee4b2721 1416 wxTreeItemIdValue cookie;
978f38c2
VZ
1417
1418 wxTreeItemId childLast,
2165ad93 1419 child = GetFirstChild(item, cookie);
978f38c2
VZ
1420 while ( child.IsOk() )
1421 {
1422 childLast = child;
2165ad93 1423 child = GetNextChild(item, cookie);
978f38c2
VZ
1424 }
1425
1426 return childLast;
1427}
1428
08b7c251
VZ
1429wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
1430{
05b2a432 1431 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
ee4b2721 1432 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1433}
1434
08b7c251 1435wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
2bda0e17 1436{
05b2a432 1437 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
ee4b2721 1438 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item)));
2bda0e17
KB
1439}
1440
08b7c251 1441wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
2bda0e17 1442{
ee4b2721 1443 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
2bda0e17
KB
1444}
1445
08b7c251 1446wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
2bda0e17 1447{
05b2a432 1448 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f6bcfd97 1449 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() for must be visible itself!"));
02ce7b72 1450
f73eddd2
VZ
1451 wxTreeItemId next(TreeView_GetNextVisible(GetHwnd(), HITEM(item)));
1452 if ( next.IsOk() && !IsVisible(next) )
1453 {
1454 // Win32 considers that any non-collapsed item is visible while we want
1455 // to return only really visible items
1456 next.Unset();
1457 }
1458
1459 return next;
08b7c251 1460}
02ce7b72 1461
08b7c251
VZ
1462wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
1463{
05b2a432 1464 wxCHECK_MSG( item.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
f6bcfd97 1465 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() for must be visible itself!"));
02ce7b72 1466
f73eddd2
VZ
1467 wxTreeItemId prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item)));
1468 if ( prev.IsOk() && !IsVisible(prev) )
1469 {
1470 // just as above, Win32 function will happily return the previous item
1471 // in the tree for the first visible item too
1472 prev.Unset();
1473 }
1474
1475 return prev;
08b7c251 1476}
02ce7b72 1477
9dfbf520
VZ
1478// ----------------------------------------------------------------------------
1479// multiple selections emulation
1480// ----------------------------------------------------------------------------
1481
33961d59
RR
1482size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
1483{
1484 TraverseSelections selector(this, selections);
9dfbf520 1485
e47c4d48 1486 return selector.GetCount();
9dfbf520
VZ
1487}
1488
08b7c251
VZ
1489// ----------------------------------------------------------------------------
1490// Usual operations
1491// ----------------------------------------------------------------------------
02ce7b72 1492
8cee4a30
VZ
1493wxTreeItemId wxTreeCtrl::DoInsertAfter(const wxTreeItemId& parent,
1494 const wxTreeItemId& hInsertAfter,
1495 const wxString& text,
1496 int image, int selectedImage,
1497 wxTreeItemData *data)
08b7c251 1498{
0e935169
VZ
1499 wxCHECK_MSG( parent.IsOk() || !TreeView_GetRoot(GetHwnd()),
1500 wxTreeItemId(),
1501 _T("can't have more than one root in the tree") );
1502
08b7c251 1503 TV_INSERTSTRUCT tvIns;
3f7bc32b
VZ
1504 tvIns.hParent = HITEM(parent);
1505 tvIns.hInsertAfter = HITEM(hInsertAfter);
58a8ab88 1506
74b31181
VZ
1507 // this is how we insert the item as the first child: supply a NULL
1508 // hInsertAfter
1509 if ( !tvIns.hInsertAfter )
58a8ab88
JS
1510 {
1511 tvIns.hInsertAfter = TVI_FIRST;
1512 }
1513
08b7c251 1514 UINT mask = 0;
44d60c0b 1515 if ( !text.empty() )
08b7c251
VZ
1516 {
1517 mask |= TVIF_TEXT;
c9f78968 1518 tvIns.item.pszText = (wxChar *)text.wx_str(); // cast is ok
08b7c251 1519 }
f6bcfd97
BP
1520 else
1521 {
1522 tvIns.item.pszText = NULL;
1523 tvIns.item.cchTextMax = 0;
1524 }
02ce7b72 1525
1b182562
VZ
1526 // create the param which will store the other item parameters
1527 wxTreeItemParam *param = new wxTreeItemParam;
1528
1529 // we return the images on demand as they depend on whether the item is
1530 // expanded or collapsed too in our case
4523ebb3
VZ
1531 mask |= TVIF_IMAGE | TVIF_SELECTEDIMAGE;
1532 tvIns.item.iImage = I_IMAGECALLBACK;
1533 tvIns.item.iSelectedImage = I_IMAGECALLBACK;
3a5a2f56 1534
4523ebb3 1535 param->SetImage(image, wxTreeItemIcon_Normal);
1b182562 1536 param->SetImage(selectedImage, wxTreeItemIcon_Selected);
02ce7b72 1537
4523ebb3
VZ
1538 mask |= TVIF_PARAM;
1539 tvIns.item.lParam = (LPARAM)param;
08b7c251 1540 tvIns.item.mask = mask;
02ce7b72 1541
4a36036e
VZ
1542 // don't use the hack below for the children of hidden root: this results
1543 // in a crash inside comctl32.dll when we call TreeView_GetItemRect()
1544 const bool firstChild = !IsHiddenRoot(parent) &&
1545 !TreeView_GetChild(GetHwnd(), HITEM(parent));
7e45c159 1546
1b182562 1547 HTREEITEM id = TreeView_InsertItem(GetHwnd(), &tvIns);
08b7c251
VZ
1548 if ( id == 0 )
1549 {
f6bcfd97 1550 wxLogLastError(wxT("TreeView_InsertItem"));
08b7c251 1551 }
02ce7b72 1552
7e45c159
VZ
1553 // apparently some Windows versions (2000 and XP are reported to do this)
1554 // sometimes don't refresh the tree after adding the first child and so we
1555 // need this to make the "[+]" appear
1556 if ( firstChild )
1557 {
1558 RECT rect;
1559 TreeView_GetItemRect(GetHwnd(), HITEM(parent), &rect, FALSE);
1560 ::InvalidateRect(GetHwnd(), &rect, FALSE);
1561 }
1562
4523ebb3
VZ
1563 // associate the application tree item with Win32 tree item handle
1564 param->SetItem(id);
1565
1566 // setup wxTreeItemData
fd3f686c
VZ
1567 if ( data != NULL )
1568 {
4523ebb3 1569 param->SetData(data);
ee4b2721 1570 data->SetId(id);
fd3f686c
VZ
1571 }
1572
ee4b2721 1573 return wxTreeItemId(id);
2bda0e17
KB
1574}
1575
08b7c251
VZ
1576wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
1577 int image, int selectedImage,
1578 wxTreeItemData *data)
2bda0e17 1579{
0228081f 1580 if ( HasFlag(wxTR_HIDE_ROOT) )
a9c1265f 1581 {
2a69b4e8
VZ
1582 wxASSERT_MSG( !m_pVirtualRoot, _T("tree can have only a single root") );
1583
a9c1265f 1584 // create a virtual root item, the parent for all the others
4523ebb3
VZ
1585 wxTreeItemParam *param = new wxTreeItemParam;
1586 param->SetData(data);
1587
1588 m_pVirtualRoot = new wxVirtualNode(param);
a9c1265f
VZ
1589
1590 return TVI_ROOT;
1591 }
1592
8cee4a30
VZ
1593 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1594 text, image, selectedImage, data);
2bda0e17
KB
1595}
1596
8cee4a30
VZ
1597wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
1598 size_t index,
1599 const wxString& text,
1600 int image, int selectedImage,
1601 wxTreeItemData *data)
2ef31e80 1602{
8cee4a30
VZ
1603 wxTreeItemId idPrev;
1604 if ( index == (size_t)-1 )
2ef31e80 1605 {
8cee4a30
VZ
1606 // special value: append to the end
1607 idPrev = TVI_LAST;
2ef31e80 1608 }
8cee4a30
VZ
1609 else // find the item from index
1610 {
1611 wxTreeItemIdValue cookie;
1612 wxTreeItemId idCur = GetFirstChild(parent, cookie);
1613 while ( index != 0 && idCur.IsOk() )
1614 {
1615 index--;
2ef31e80 1616
8cee4a30
VZ
1617 idPrev = idCur;
1618 idCur = GetNextChild(parent, cookie);
1619 }
2ef31e80 1620
8cee4a30
VZ
1621 // assert, not check: if the index is invalid, we will append the item
1622 // to the end
1623 wxASSERT_MSG( index == 0, _T("bad index in wxTreeCtrl::InsertItem") );
1624 }
2ef31e80 1625
8cee4a30 1626 return DoInsertAfter(parent, idPrev, text, image, selectedImage, data);
2bda0e17
KB
1627}
1628
08b7c251 1629void wxTreeCtrl::Delete(const wxTreeItemId& item)
2bda0e17 1630{
396f85ba
BW
1631 // unlock tree selections on vista, without this the
1632 // tree ctrl will eventually crash after item deletion
1633 TreeItemUnlocker unlock_all;
1634
c7d9c476
VZ
1635 if ( HasFlag(wxTR_MULTIPLE) )
1636 {
1637 bool selected = IsSelected(item);
1638 wxTreeItemId next;
1639
1640 if ( selected )
1641 {
1642 next = TreeView_GetNextVisible(GetHwnd(), HITEM(item));
1643
1644 if ( !next.IsOk() )
1645 {
1646 next = TreeView_GetPrevVisible(GetHwnd(), HITEM(item));
1647 }
1648 }
1649
c7d9c476 1650 {
7dac12ef
VZ
1651 TempSetter set(m_changingSelection);
1652 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1653 {
1654 wxLogLastError(wxT("TreeView_DeleteItem"));
1655 return;
1656 }
c7d9c476
VZ
1657 }
1658
1659 if ( !selected )
1660 {
1661 return;
1662 }
1663
1664 if ( next.IsOk() )
1665 {
1666 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
1667
1668 if ( IsTreeEventAllowed(changingEvent) )
1669 {
1670 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
1671 (void)HandleTreeEvent(changedEvent);
1672 }
1673 else
1674 {
7dac12ef
VZ
1675 DoUnselectItem(next);
1676 ClearFocusedItem();
c7d9c476
VZ
1677 }
1678 }
1679 }
1680 else
bbcdf8bc 1681 {
c7d9c476
VZ
1682 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item)) )
1683 {
1684 wxLogLastError(wxT("TreeView_DeleteItem"));
1685 }
bbcdf8bc 1686 }
bbcdf8bc
JS
1687}
1688
23fd5130
VZ
1689// delete all children (but don't delete the item itself)
1690void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1691{
396f85ba
BW
1692 // unlock tree selections on vista for the duration of this call
1693 TreeItemUnlocker unlock_all;
1694
ee4b2721 1695 wxTreeItemIdValue cookie;
23fd5130 1696
ee4b2721 1697 wxArrayTreeItemIds children;
23fd5130
VZ
1698 wxTreeItemId child = GetFirstChild(item, cookie);
1699 while ( child.IsOk() )
1700 {
ee4b2721 1701 children.Add(child);
23fd5130
VZ
1702
1703 child = GetNextChild(item, cookie);
1704 }
1705
1706 size_t nCount = children.Count();
1707 for ( size_t n = 0; n < nCount; n++ )
1708 {
c7d9c476 1709 Delete(children[n]);
23fd5130
VZ
1710 }
1711}
1712
08b7c251 1713void wxTreeCtrl::DeleteAllItems()
bbcdf8bc 1714{
396f85ba
BW
1715 // unlock tree selections on vista for the duration of this call
1716 TreeItemUnlocker unlock_all;
1717
bfedf621
VZ
1718 // delete the "virtual" root item.
1719 if ( GET_VIRTUAL_ROOT() )
1720 {
1721 delete GET_VIRTUAL_ROOT();
1722 m_pVirtualRoot = NULL;
1723 }
1724
1725 // and all the real items
a9c1265f 1726
d220ae32 1727 if ( !TreeView_DeleteAllItems(GetHwnd()) )
bbcdf8bc 1728 {
f6bcfd97 1729 wxLogLastError(wxT("TreeView_DeleteAllItems"));
bbcdf8bc 1730 }
2bda0e17
KB
1731}
1732
08b7c251 1733void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
2bda0e17 1734{
dd3646fd
VZ
1735 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1736 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1737 flag == TVE_EXPAND ||
1738 flag == TVE_TOGGLE,
223d09f6 1739 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
08b7c251 1740
a9c1265f 1741 // A hidden root can be neither expanded nor collapsed.
0228081f 1742 wxCHECK_RET( !IsHiddenRoot(item),
637b7e4f 1743 wxT("Can't expand/collapse hidden root node!") );
a9c1265f 1744
08b7c251 1745 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
d220ae32
VZ
1746 // emulate them. This behaviour has changed slightly with comctl32.dll
1747 // v 4.70 - now it does send them but only the first time. To maintain
1748 // compatible behaviour and also in order to not have surprises with the
1749 // future versions, don't rely on this and still do everything ourselves.
1750 // To avoid that the messages be sent twice when the item is expanded for
1751 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1752
1753 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1754 tvItem.state = 0;
1755 DoSetItem(&tvItem);
1756
c7d9c476
VZ
1757 if ( IsExpanded(item) )
1758 {
1759 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING,
1760 this, wxTreeItemId(item));
1761
1762 if ( !IsTreeEventAllowed(event) )
1763 return;
1764 }
1765
1766 if ( TreeView_Expand(GetHwnd(), HITEM(item), flag) )
08b7c251 1767 {
c7d9c476
VZ
1768 if ( IsExpanded(item) )
1769 return;
1770
1771 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, this, item);
1772 (void)HandleTreeEvent(event);
08b7c251 1773 }
d220ae32 1774 //else: change didn't took place, so do nothing at all
2bda0e17
KB
1775}
1776
08b7c251 1777void wxTreeCtrl::Expand(const wxTreeItemId& item)
2bda0e17 1778{
08b7c251 1779 DoExpand(item, TVE_EXPAND);
2bda0e17 1780}
2bda0e17 1781
08b7c251 1782void wxTreeCtrl::Collapse(const wxTreeItemId& item)
2bda0e17 1783{
08b7c251 1784 DoExpand(item, TVE_COLLAPSE);
2bda0e17
KB
1785}
1786
08b7c251 1787void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
2bda0e17 1788{
dd3646fd 1789 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
2bda0e17
KB
1790}
1791
08b7c251 1792void wxTreeCtrl::Toggle(const wxTreeItemId& item)
2bda0e17 1793{
08b7c251 1794 DoExpand(item, TVE_TOGGLE);
2bda0e17
KB
1795}
1796
08b7c251 1797void wxTreeCtrl::Unselect()
2bda0e17 1798{
c7d9c476 1799 wxASSERT_MSG( !HasFlag(wxTR_MULTIPLE),
3f7bc32b 1800 wxT("doesn't make sense, may be you want UnselectAll()?") );
9dfbf520 1801
c7d9c476
VZ
1802 // the current focus
1803 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
1804
1805 if ( !htFocus )
1806 {
1807 return;
1808 }
1809
1810 if ( HasFlag(wxTR_MULTIPLE) )
1811 {
1812 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
1813 this, wxTreeItemId());
1814 changingEvent.m_itemOld = htFocus;
1815
1816 if ( IsTreeEventAllowed(changingEvent) )
1817 {
7dac12ef 1818 ClearFocusedItem();
c7d9c476
VZ
1819
1820 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
1821 this, wxTreeItemId());
1822 changedEvent.m_itemOld = htFocus;
1823 (void)HandleTreeEvent(changedEvent);
1824 }
1825 }
1826 else
1827 {
7dac12ef 1828 ClearFocusedItem();
c7d9c476
VZ
1829 }
1830}
1831
1832void wxTreeCtrl::DoUnselectAll()
1833{
1834 wxArrayTreeItemIds selections;
1835 size_t count = GetSelections(selections);
1836
1837 for ( size_t n = 0; n < count; n++ )
1838 {
7dac12ef 1839 DoUnselectItem(selections[n]);
c7d9c476
VZ
1840 }
1841
1842 m_htSelStart.Unset();
08b7c251 1843}
02ce7b72 1844
9dfbf520 1845void wxTreeCtrl::UnselectAll()
08b7c251 1846{
c7d9c476 1847 if ( HasFlag(wxTR_MULTIPLE) )
2bda0e17 1848 {
c7d9c476
VZ
1849 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
1850 if ( !htFocus ) return;
1851
1852 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this);
1853 changingEvent.m_itemOld = htFocus;
1854
1855 if ( IsTreeEventAllowed(changingEvent) )
d220ae32 1856 {
c7d9c476 1857 DoUnselectAll();
ba8a1961 1858
c7d9c476
VZ
1859 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this);
1860 changedEvent.m_itemOld = htFocus;
1861 (void)HandleTreeEvent(changedEvent);
1862 }
9dfbf520 1863 }
75afdc2d 1864 else
9dfbf520 1865 {
9dfbf520
VZ
1866 Unselect();
1867 }
1868}
1869
7dac12ef
VZ
1870void wxTreeCtrl::DoSelectItem(const wxTreeItemId& item, bool select)
1871{
1872 TempSetter set(m_changingSelection);
1873
1874 ::SelectItem(GetHwnd(), HITEM(item), select);
1875}
1876
3e9af289 1877void wxTreeCtrl::SelectItem(const wxTreeItemId& item, bool select)
9dfbf520 1878{
8a536fa3
VZ
1879 wxCHECK_RET( !IsHiddenRoot(item), _T("can't select hidden root item") );
1880
86646693 1881 if ( select == IsSelected(item) )
c7d9c476 1882 {
86646693 1883 // nothing to do, the item is already in the requested state
c7d9c476
VZ
1884 return;
1885 }
9dfbf520 1886
c7d9c476 1887 if ( HasFlag(wxTR_MULTIPLE) )
cc87a04c 1888 {
c7d9c476
VZ
1889 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, item);
1890
1891 if ( IsTreeEventAllowed(changingEvent) )
d220ae32 1892 {
c7d9c476 1893 HTREEITEM htFocus = (HTREEITEM)TreeView_GetSelection(GetHwnd());
7dac12ef 1894 DoSelectItem(item, select);
c7d9c476
VZ
1895
1896 if ( !htFocus )
7549148b 1897 {
7dac12ef 1898 SetFocusedItem(item);
7549148b 1899 }
c7d9c476
VZ
1900
1901 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
1902 this, item);
1903 (void)HandleTreeEvent(changedEvent);
cc87a04c 1904 }
c7d9c476 1905 }
86646693 1906 else // single selection
c7d9c476 1907 {
86646693
VZ
1908 wxTreeItemId itemOld, itemNew;
1909 if ( select )
1910 {
1911 itemOld = GetSelection();
1912 itemNew = item;
1913 }
1914 else // deselecting the currently selected item
1915 {
1916 itemOld = item;
1917 // leave itemNew invalid
1918 }
c7d9c476
VZ
1919
1920 // in spite of the docs (MSDN Jan 99 edition), we don't seem to receive
1921 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1922 // send them ourselves
1923
86646693
VZ
1924 wxTreeEvent
1925 changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, itemNew);
1926 changingEvent.SetOldItem(itemOld);
c7d9c476
VZ
1927
1928 if ( IsTreeEventAllowed(changingEvent) )
cc87a04c 1929 {
86646693 1930 if ( !TreeView_SelectItem(GetHwnd(), HITEM(itemNew)) )
7549148b
VZ
1931 {
1932 wxLogLastError(wxT("TreeView_SelectItem"));
7549148b 1933 }
c7d9c476
VZ
1934 else // ok
1935 {
7dac12ef 1936 SetFocusedItem(item);
7549148b 1937
c7d9c476 1938 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
86646693
VZ
1939 this, itemNew);
1940 changedEvent.SetOldItem(itemOld);
c7d9c476
VZ
1941 (void)HandleTreeEvent(changedEvent);
1942 }
1943 }
1944 //else: program vetoed the change
2bda0e17 1945 }
08b7c251 1946}
2bda0e17 1947
08b7c251
VZ
1948void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1949{
0228081f
VZ
1950 wxCHECK_RET( !IsHiddenRoot(item), _T("can't show hidden root item") );
1951
08b7c251 1952 // no error return
3f7bc32b 1953 TreeView_EnsureVisible(GetHwnd(), HITEM(item));
08b7c251
VZ
1954}
1955
1956void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1957{
3f7bc32b 1958 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item)) )
2bda0e17 1959 {
f6bcfd97 1960 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2bda0e17 1961 }
08b7c251
VZ
1962}
1963
44fbc477 1964wxTextCtrl *wxTreeCtrl::GetEditControl() const
08b7c251
VZ
1965{
1966 return m_textCtrl;
1967}
1968
1969void wxTreeCtrl::DeleteTextCtrl()
1970{
1971 if ( m_textCtrl )
2bda0e17 1972 {
b6a3d6ad
VZ
1973 // the HWND corresponding to this control is deleted by the tree
1974 // control itself and we don't know when exactly this happens, so check
1975 // if the window still exists before calling UnsubclassWin()
1976 if ( !::IsWindow(GetHwndOf(m_textCtrl)) )
1977 {
1978 m_textCtrl->SetHWND(0);
1979 }
1980
08b7c251
VZ
1981 m_textCtrl->UnsubclassWin();
1982 m_textCtrl->SetHWND(0);
1983 delete m_textCtrl;
1984 m_textCtrl = NULL;
1a4088e1
VZ
1985
1986 m_idEdited.Unset();
2bda0e17 1987 }
08b7c251 1988}
2bda0e17 1989
4523ebb3
VZ
1990wxTextCtrl *wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1991 wxClassInfo *textControlClass)
08b7c251
VZ
1992{
1993 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1994
b94ae1ea
VZ
1995 DeleteTextCtrl();
1996
1a4088e1 1997 m_idEdited = item;
cd6bd270 1998 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
3f7bc32b 1999 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), HITEM(item));
2bda0e17 2000
5ea47806 2001 // this is not an error - the TVN_BEGINLABELEDIT handler might have
04cd30de 2002 // returned false
5ea47806
VZ
2003 if ( !hWnd )
2004 {
cd6bd270
MB
2005 delete m_textCtrl;
2006 m_textCtrl = NULL;
5ea47806
VZ
2007 return NULL;
2008 }
2bda0e17 2009
cd6bd270 2010 // textctrl is subclassed in MSWOnNotify
08b7c251 2011 return m_textCtrl;
2bda0e17
KB
2012}
2013
08b7c251 2014// End label editing, optionally cancelling the edit
d35dce3a 2015void wxTreeCtrl::DoEndEditLabel(bool discardChanges)
2bda0e17 2016{
d220ae32 2017 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
08b7c251
VZ
2018
2019 DeleteTextCtrl();
2bda0e17
KB
2020}
2021
be0e5d69 2022wxTreeItemId wxTreeCtrl::DoTreeHitTest(const wxPoint& point, int& flags) const
2bda0e17 2023{
08b7c251
VZ
2024 TV_HITTESTINFO hitTestInfo;
2025 hitTestInfo.pt.x = (int)point.x;
2026 hitTestInfo.pt.y = (int)point.y;
2bda0e17 2027
8ecd5de2 2028 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo);
2bda0e17 2029
08b7c251
VZ
2030 flags = 0;
2031
2032 // avoid repetition
2033 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2034 flags |= wxTREE_HITTEST_##flag
2035
2036 TRANSLATE_FLAG(ABOVE);
2037 TRANSLATE_FLAG(BELOW);
2038 TRANSLATE_FLAG(NOWHERE);
2039 TRANSLATE_FLAG(ONITEMBUTTON);
2040 TRANSLATE_FLAG(ONITEMICON);
2041 TRANSLATE_FLAG(ONITEMINDENT);
2042 TRANSLATE_FLAG(ONITEMLABEL);
2043 TRANSLATE_FLAG(ONITEMRIGHT);
2044 TRANSLATE_FLAG(ONITEMSTATEICON);
2045 TRANSLATE_FLAG(TOLEFT);
2046 TRANSLATE_FLAG(TORIGHT);
2bda0e17 2047
08b7c251
VZ
2048 #undef TRANSLATE_FLAG
2049
ee4b2721 2050 return wxTreeItemId(hitTestInfo.hItem);
08b7c251
VZ
2051}
2052
f7c832a7
VZ
2053bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
2054 wxRect& rect,
2055 bool textOnly) const
2056{
2057 RECT rc;
f2c3db9d
RD
2058
2059 // Virtual root items have no bounding rectangle
2060 if ( IS_VIRTUAL_ROOT(item) )
2061 {
2062 return false;
2063 }
2064
3f7bc32b 2065 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item),
f7c832a7
VZ
2066 &rc, textOnly) )
2067 {
2068 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
2069
04cd30de 2070 return true;
f7c832a7
VZ
2071 }
2072 else
2073 {
2074 // couldn't retrieve rect: for example, item isn't visible
04cd30de 2075 return false;
f7c832a7
VZ
2076 }
2077}
2078
7dac12ef
VZ
2079void wxTreeCtrl::ClearFocusedItem()
2080{
2081 TempSetter set(m_changingSelection);
2082
2083 if ( !TreeView_SelectItem(GetHwnd(), 0) )
2084 {
2085 wxLogLastError(wxT("TreeView_SelectItem"));
2086 }
2087}
2088
2089void wxTreeCtrl::SetFocusedItem(const wxTreeItemId& item)
2090{
2091 TempSetter set(m_changingSelection);
2092
2093 ::SetFocus(GetHwnd(), HITEM(item));
2094}
2095
2096void wxTreeCtrl::DoUnselectItem(const wxTreeItemId& item)
2097{
2098 TempSetter set(m_changingSelection);
2099
2100 ::UnselectItem(GetHwnd(), HITEM(item));
2101}
2102
2103void wxTreeCtrl::DoToggleItemSelection(const wxTreeItemId& item)
2104{
2105 TempSetter set(m_changingSelection);
2106
2107 ::ToggleItemSelection(GetHwnd(), HITEM(item));
2108}
2109
23fd5130
VZ
2110// ----------------------------------------------------------------------------
2111// sorting stuff
2112// ----------------------------------------------------------------------------
f7c832a7 2113
502a2b18
VZ
2114// this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2115// functions such as IsDataIndirect()
2116class wxTreeSortHelper
2117{
2118public:
2119 static int CALLBACK Compare(LPARAM data1, LPARAM data2, LPARAM tree);
2120
2121private:
4523ebb3 2122 static wxTreeItemId GetIdFromData(LPARAM lParam)
502a2b18 2123 {
4523ebb3 2124 return ((wxTreeItemParam*)lParam)->GetItem();
502a2b18 2125 }
502a2b18
VZ
2126};
2127
2128int CALLBACK wxTreeSortHelper::Compare(LPARAM pItem1,
2129 LPARAM pItem2,
2130 LPARAM htree)
23fd5130 2131{
096c9f9b 2132 wxCHECK_MSG( pItem1 && pItem2, 0,
223d09f6 2133 wxT("sorting tree without data doesn't make sense") );
096c9f9b 2134
502a2b18
VZ
2135 wxTreeCtrl *tree = (wxTreeCtrl *)htree;
2136
4523ebb3
VZ
2137 return tree->OnCompareItems(GetIdFromData(pItem1),
2138 GetIdFromData(pItem2));
23fd5130
VZ
2139}
2140
95aabccc
VZ
2141void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
2142{
05b2a432
RD
2143 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
2144
95aabccc 2145 // rely on the fact that TreeView_SortChildren does the same thing as our
23fd5130
VZ
2146 // default behaviour, i.e. sorts items alphabetically and so call it
2147 // directly if we're not in derived class (much more efficient!)
4523ebb3
VZ
2148 // RN: Note that if you find you're code doesn't sort as expected this
2149 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
2150 // combo for your derived wxTreeCtrl if will sort without
2151 // OnCompareItems
23fd5130 2152 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
2bda0e17 2153 {
3f7bc32b 2154 TreeView_SortChildren(GetHwnd(), HITEM(item), 0);
2bda0e17 2155 }
08b7c251 2156 else
2bda0e17 2157 {
62448488 2158 TV_SORTCB tvSort;
3f7bc32b 2159 tvSort.hParent = HITEM(item);
502a2b18 2160 tvSort.lpfnCompare = wxTreeSortHelper::Compare;
23fd5130 2161 tvSort.lParam = (LPARAM)this;
d220ae32 2162 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
2bda0e17 2163 }
08b7c251 2164}
2bda0e17 2165
08b7c251
VZ
2166// ----------------------------------------------------------------------------
2167// implementation
2168// ----------------------------------------------------------------------------
2bda0e17 2169
90c6edd7
VZ
2170bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG* msg)
2171{
2172 if ( msg->message == WM_KEYDOWN )
2173 {
6719c06a
VZ
2174 // Only eat VK_RETURN if not being used by the application in
2175 // conjunction with modifiers
2176 if ( (msg->wParam == VK_RETURN) && !wxIsAnyModifierDown() )
90c6edd7
VZ
2177 {
2178 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2179 return false;
2180 }
2181 }
2182
2183 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg);
2184}
2185
0edeeb6d 2186bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id_)
08b7c251 2187{
0edeeb6d
VZ
2188 const int id = (signed short)id_;
2189
08b7c251 2190 if ( cmd == EN_UPDATE )
2bda0e17 2191 {
08b7c251
VZ
2192 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
2193 event.SetEventObject( this );
2194 ProcessCommand(event);
2bda0e17 2195 }
08b7c251 2196 else if ( cmd == EN_KILLFOCUS )
2bda0e17 2197 {
08b7c251
VZ
2198 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
2199 event.SetEventObject( this );
2200 ProcessCommand(event);
2bda0e17 2201 }
08b7c251 2202 else
2bda0e17 2203 {
08b7c251 2204 // nothing done
04cd30de 2205 return false;
2bda0e17 2206 }
08b7c251
VZ
2207
2208 // command processed
04cd30de 2209 return true;
08b7c251
VZ
2210}
2211
25f701e6 2212bool wxTreeCtrl::MSWHandleSelectionKey(unsigned vkey)
c7d9c476
VZ
2213{
2214 const bool bCtrl = wxIsCtrlDown();
2215 const bool bShift = wxIsShiftDown();
2216 const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2217
2218 switch ( vkey )
2219 {
2220 case VK_RETURN:
2221 case VK_SPACE:
2222 if ( !htSel )
2223 break;
2224
2225 if ( vkey != VK_RETURN && bCtrl )
2226 {
2227 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2228 this, htSel);
2229 changingEvent.m_itemOld = htSel;
2230
2231 if ( IsTreeEventAllowed(changingEvent) )
2232 {
7dac12ef 2233 DoToggleItemSelection(wxTreeItemId(htSel));
c7d9c476
VZ
2234
2235 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2236 this, htSel);
2237 changedEvent.m_itemOld = htSel;
2238 (void)HandleTreeEvent(changedEvent);
2239 }
2240 }
2241 else
2242 {
2243 wxArrayTreeItemIds selections;
2244 size_t count = GetSelections(selections);
2245
2246 if ( count != 1 || HITEM(selections[0]) != htSel )
2247 {
2248 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2249 this, htSel);
2250 changingEvent.m_itemOld = htSel;
2251
2252 if ( IsTreeEventAllowed(changingEvent) )
2253 {
2254 DoUnselectAll();
7dac12ef 2255 DoSelectItem(wxTreeItemId(htSel));
c7d9c476
VZ
2256
2257 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2258 this, htSel);
2259 changedEvent.m_itemOld = htSel;
2260 (void)HandleTreeEvent(changedEvent);
2261 }
2262 }
2263 }
2264 break;
2265
2266 case VK_UP:
2267 case VK_DOWN:
2268 if ( !bCtrl && !bShift )
2269 {
2270 wxArrayTreeItemIds selections;
c7d9c476
VZ
2271 wxTreeItemId next;
2272
7dac12ef 2273 if ( htSel )
c7d9c476
VZ
2274 {
2275 next = vkey == VK_UP
2276 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2277 : TreeView_GetNextVisible(GetHwnd(), htSel);
2278 }
2279 else
2280 {
2281 next = GetRootItem();
2282
2283 if ( IsHiddenRoot(next) )
2284 next = TreeView_GetChild(GetHwnd(), HITEM(next));
c7d9c476
VZ
2285 }
2286
2287 if ( !next.IsOk() )
2288 {
2289 break;
2290 }
2291
2292 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2293 this, next);
2294 changingEvent.m_itemOld = htSel;
2295
2296 if ( IsTreeEventAllowed(changingEvent) )
2297 {
2298 DoUnselectAll();
7dac12ef
VZ
2299 DoSelectItem(next);
2300 SetFocusedItem(next);
c7d9c476
VZ
2301
2302 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2303 this, next);
2304 changedEvent.m_itemOld = htSel;
2305 (void)HandleTreeEvent(changedEvent);
2306 }
2307 }
2308 else if ( htSel )
2309 {
2310 wxTreeItemId next = vkey == VK_UP
2311 ? TreeView_GetPrevVisible(GetHwnd(), htSel)
2312 : TreeView_GetNextVisible(GetHwnd(), htSel);
2313
2314 if ( !next.IsOk() )
2315 {
2316 break;
2317 }
2318
2319 if ( !m_htSelStart )
2320 {
2321 m_htSelStart = htSel;
2322 }
2323
2324 if ( bShift && SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next),
2325 SR_UNSELECT_OTHERS | SR_SIMULATE) )
2326 {
2327 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
2328 changingEvent.m_itemOld = htSel;
2329
2330 if ( IsTreeEventAllowed(changingEvent) )
2331 {
2332 SelectRange(GetHwnd(), HITEM(m_htSelStart), HITEM(next),
2333 SR_UNSELECT_OTHERS);
2334
2335 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
2336 changedEvent.m_itemOld = htSel;
2337 (void)HandleTreeEvent(changedEvent);
2338 }
2339 }
2340
7dac12ef 2341 SetFocusedItem(next);
c7d9c476
VZ
2342 }
2343 break;
2344
2345 case VK_LEFT:
2346 if ( HasChildren(htSel) && IsExpanded(htSel) )
2347 {
2348 Collapse(htSel);
2349 }
2350 else
2351 {
2352 wxTreeItemId next = GetItemParent(htSel);
2353
2354 if ( next.IsOk() && !IsHiddenRoot(next) )
2355 {
2356 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2357 this, next);
2358 changingEvent.m_itemOld = htSel;
2359
2360 if ( IsTreeEventAllowed(changingEvent) )
2361 {
2362 DoUnselectAll();
7dac12ef
VZ
2363 DoSelectItem(next);
2364 SetFocusedItem(next);
c7d9c476
VZ
2365
2366 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2367 this, next);
2368 changedEvent.m_itemOld = htSel;
2369 (void)HandleTreeEvent(changedEvent);
2370 }
2371 }
2372 }
2373 break;
2374
2375 case VK_RIGHT:
2376 if ( !IsVisible(htSel) )
2377 {
2378 EnsureVisible(htSel);
2379 }
2380
2381 if ( !HasChildren(htSel) )
2382 break;
2383
2384 if ( !IsExpanded(htSel) )
2385 {
2386 Expand(htSel);
2387 }
2388 else
2389 {
2390 wxTreeItemId next = TreeView_GetChild(GetHwnd(), htSel);
2391
2392 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING, this, next);
2393 changingEvent.m_itemOld = htSel;
2394
2395 if ( IsTreeEventAllowed(changingEvent) )
2396 {
2397 DoUnselectAll();
7dac12ef
VZ
2398 DoSelectItem(next);
2399 SetFocusedItem(next);
c7d9c476
VZ
2400
2401 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED, this, next);
2402 changedEvent.m_itemOld = htSel;
2403 (void)HandleTreeEvent(changedEvent);
2404 }
2405 }
2406 break;
2407
2408 case VK_HOME:
2409 case VK_END:
2410 {
2411 wxTreeItemId next = GetRootItem();
2412
2413 if ( IsHiddenRoot(next) )
2414 {
2415 next = TreeView_GetChild(GetHwnd(), HITEM(next));
2416 }
2417
2418 if ( !next.IsOk() )
2419 break;
2420
2421 if ( vkey == VK_END )
2422 {
2423 for ( ;; )
2424 {
2425 wxTreeItemId nextTemp = TreeView_GetNextVisible(
2426 GetHwnd(), HITEM(next));
2427
2428 if ( !nextTemp.IsOk() )
2429 break;
2430
2431 next = nextTemp;
2432 }
2433 }
2434
2435 if ( htSel == HITEM(next) )
2436 break;
2437
2438 if ( bShift )
2439 {
2440 if ( !m_htSelStart )
2441 {
2442 m_htSelStart = htSel;
2443 }
2444
2445 if ( SelectRange(GetHwnd(),
2446 HITEM(m_htSelStart), HITEM(next),
2447 SR_UNSELECT_OTHERS | SR_SIMULATE) )
2448 {
2449 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2450 this, next);
2451 changingEvent.m_itemOld = htSel;
2452
2453 if ( IsTreeEventAllowed(changingEvent) )
2454 {
2455 SelectRange(GetHwnd(),
2456 HITEM(m_htSelStart), HITEM(next),
2457 SR_UNSELECT_OTHERS);
7dac12ef 2458 SetFocusedItem(next);
c7d9c476
VZ
2459
2460 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2461 this, next);
2462 changedEvent.m_itemOld = htSel;
2463 (void)HandleTreeEvent(changedEvent);
2464 }
2465 }
2466 }
2467 else // no Shift
2468 {
2469 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2470 this, next);
2471 changingEvent.m_itemOld = htSel;
2472
2473 if ( IsTreeEventAllowed(changingEvent) )
2474 {
2475 DoUnselectAll();
7dac12ef
VZ
2476 DoSelectItem(next);
2477 SetFocusedItem(next);
c7d9c476
VZ
2478
2479 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2480 this, next);
2481 changedEvent.m_itemOld = htSel;
2482 (void)HandleTreeEvent(changedEvent);
2483 }
2484 }
2485 }
2486 break;
2487
2488 case VK_PRIOR:
2489 case VK_NEXT:
2490 if ( bCtrl )
2491 {
2492 wxTreeItemId firstVisible = GetFirstVisibleItem();
2493 size_t visibleCount = TreeView_GetVisibleCount(GetHwnd());
2494 wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ?
2495 TreeView_GetPrevVisible(GetHwnd(), HITEM(firstVisible)) :
2496 TreeView_GetNextVisible(GetHwnd(), HITEM(firstVisible));
2497
2498 if ( !nextAdjacent )
2499 {
2500 break;
2501 }
2502
2503 wxTreeItemId nextStart = firstVisible;
2504
2505 for ( size_t n = 1; n < visibleCount; n++ )
2506 {
2507 wxTreeItemId nextTemp = (vkey == VK_PRIOR) ?
2508 TreeView_GetPrevVisible(GetHwnd(), HITEM(nextStart)) :
2509 TreeView_GetNextVisible(GetHwnd(), HITEM(nextStart));
2510
2511 if ( nextTemp.IsOk() )
2512 {
2513 nextStart = nextTemp;
2514 }
2515 else
2516 {
2517 break;
2518 }
2519 }
2520
2521 EnsureVisible(nextStart);
2522
2523 if ( vkey == VK_NEXT )
2524 {
2525 wxTreeItemId nextEnd = nextStart;
2526
2527 for ( size_t n = 1; n < visibleCount; n++ )
2528 {
2529 wxTreeItemId nextTemp =
2530 TreeView_GetNextVisible(GetHwnd(), HITEM(nextEnd));
2531
2532 if ( nextTemp.IsOk() )
2533 {
2534 nextEnd = nextTemp;
2535 }
2536 else
2537 {
2538 break;
2539 }
2540 }
2541
2542 EnsureVisible(nextEnd);
2543 }
2544 }
2545 else // no Ctrl
2546 {
2547 size_t visibleCount = TreeView_GetVisibleCount(GetHwnd());
2548 wxTreeItemId nextAdjacent = (vkey == VK_PRIOR) ?
2549 TreeView_GetPrevVisible(GetHwnd(), htSel) :
2550 TreeView_GetNextVisible(GetHwnd(), htSel);
2551
2552 if ( !nextAdjacent )
2553 {
2554 break;
2555 }
2556
2557 wxTreeItemId next(htSel);
2558
2559 for ( size_t n = 1; n < visibleCount; n++ )
2560 {
2561 wxTreeItemId nextTemp = vkey == VK_PRIOR ?
2562 TreeView_GetPrevVisible(GetHwnd(), HITEM(next)) :
2563 TreeView_GetNextVisible(GetHwnd(), HITEM(next));
2564
2565 if ( !nextTemp.IsOk() )
2566 break;
2567
2568 next = nextTemp;
2569 }
2570
2571 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2572 this, next);
2573 changingEvent.m_itemOld = htSel;
2574
2575 if ( IsTreeEventAllowed(changingEvent) )
2576 {
2577 DoUnselectAll();
2578 m_htSelStart.Unset();
7dac12ef
VZ
2579 DoSelectItem(next);
2580 SetFocusedItem(next);
c7d9c476
VZ
2581
2582 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2583 this, next);
2584 changedEvent.m_itemOld = htSel;
2585 (void)HandleTreeEvent(changedEvent);
2586 }
2587 }
2588 break;
2589
2590 default:
2591 return false;
2592 }
2593
2594 return true;
2595}
2596
aa8166dd
VZ
2597bool wxTreeCtrl::MSWHandleTreeKeyDownEvent(WXWPARAM wParam, WXLPARAM lParam)
2598{
2599 wxTreeEvent keyEvent(wxEVT_COMMAND_TREE_KEY_DOWN, this);
2600
2601 int keyCode = wxCharCodeMSWToWX(wParam);
2602
2603 if ( !keyCode )
2604 {
2605 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2606 // simple ASCII key
2607 keyCode = wParam;
2608 }
2609
2610 keyEvent.m_evtKey = CreateKeyEvent(wxEVT_KEY_DOWN, keyCode,
2611 lParam, wParam);
2612
2613 bool processed = HandleTreeEvent(keyEvent);
2614
2615 // generate a separate event for Space/Return
2616 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !wxIsAltDown() &&
2617 ((wParam == VK_SPACE) || (wParam == VK_RETURN)) )
2618 {
2619 const HTREEITEM htSel = (HTREEITEM)TreeView_GetSelection(GetHwnd());
2620 if ( htSel )
2621 {
2622 wxTreeEvent activatedEvent(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
2623 this, htSel);
2624 (void)HandleTreeEvent(activatedEvent);
2625 }
2626 }
2627
2628 return processed;
2629}
2630
23f681ec
VZ
2631// we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2632// only do it during dragging, minimize wxWin overhead (this is important for
2633// WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2634// instead of passing by wxWin events
c7d9c476
VZ
2635WXLRESULT
2636wxTreeCtrl::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
23f681ec 2637{
04cd30de 2638 bool processed = false;
c140b7e7 2639 WXLRESULT rc = 0;
2b9a7d4c 2640 bool isMultiple = HasFlag(wxTR_MULTIPLE);
3f7bc32b 2641
499bbaeb 2642 if ( nMsg == WM_CONTEXTMENU )
30f5e34f 2643 {
147964ca
KH
2644 int x = GET_X_LPARAM(lParam),
2645 y = GET_Y_LPARAM(lParam);
8f6dc819
VZ
2646
2647 // the item for which the menu should be shown
2648 wxTreeItemId item;
2649
2650 // the position where the menu should be shown in client coordinates
2651 // (so that it can be passed directly to PopupMenu())
2652 wxPoint pt;
2653
2654 if ( x == -1 || y == -1 )
147964ca 2655 {
8f6dc819
VZ
2656 // this means that the event was generated from keyboard (e.g. with
2657 // Shift-F10 or special Windows menu key)
2658 //
2659 // use the Explorer standard of putting the menu at the left edge
2660 // of the text, in the vertical middle of the text
2661 item = wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2662 if ( item.IsOk() )
2663 {
2664 // Use the bounding rectangle of only the text part
2665 wxRect rect;
2666 GetBoundingRect(item, rect, true);
2667 pt = wxPoint(rect.GetX(), rect.GetY() + rect.GetHeight() / 2);
2668 }
147964ca 2669 }
8f6dc819 2670 else // event from mouse, use mouse position
147964ca 2671 {
8f6dc819
VZ
2672 pt = ScreenToClient(wxPoint(x, y));
2673
2674 TV_HITTESTINFO tvhti;
2675 tvhti.pt.x = pt.x;
2676 tvhti.pt.y = pt.y;
c7d9c476 2677
8f6dc819
VZ
2678 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
2679 item = wxTreeItemId(tvhti.hItem);
147964ca
KH
2680 }
2681
8f6dc819
VZ
2682 // create the event
2683 wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_MENU, this, item);
2684
2685 event.m_pointDrag = pt;
2686
c7d9c476 2687 if ( HandleTreeEvent(event) )
2b9a7d4c 2688 processed = true;
9994e2da 2689 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
30f5e34f 2690 }
2b9a7d4c 2691 else if ( (nMsg >= WM_MOUSEFIRST) && (nMsg <= WM_MOUSELAST) )
23f681ec 2692 {
7bb8798c
VZ
2693 // we only process mouse messages here and these parameters have the
2694 // same meaning for all of them
3f7bc32b
VZ
2695 int x = GET_X_LPARAM(lParam),
2696 y = GET_Y_LPARAM(lParam);
8ecd5de2 2697
e3ad5702
JS
2698 TV_HITTESTINFO tvht;
2699 tvht.pt.x = x;
2700 tvht.pt.y = y;
8ecd5de2 2701
c7d9c476 2702 HTREEITEM htOldItem = TreeView_GetSelection(GetHwnd());
8854a202 2703 HTREEITEM htItem = TreeView_HitTest(GetHwnd(), &tvht);
3f7bc32b 2704
23f681ec
VZ
2705 switch ( nMsg )
2706 {
3f7bc32b 2707 case WM_LBUTTONDOWN:
c7d9c476
VZ
2708 if ( !isMultiple )
2709 break;
2710
c7d9c476
VZ
2711 m_htClickedItem.Unset();
2712
2713 if ( !(tvht.flags & TVHT_ONITEM) )
23f681ec 2714 {
c7d9c476
VZ
2715 if ( !HandleMouseEvent(nMsg, x, y, wParam) )
2716 {
2717 if ( tvht.flags & TVHT_ONITEMBUTTON )
2718 {
2719 if ( !IsExpanded(htItem) )
2720 {
2721 Expand(htItem);
2722 }
2723 else
2724 {
2725 Collapse(htItem);
2726 }
2727 }
7dac12ef
VZ
2728
2729 processed = true;
c7d9c476 2730 }
8ecd5de2 2731
7dac12ef 2732 m_focusLost = false;
c7d9c476
VZ
2733 break;
2734 }
2735
7dac12ef 2736 processed = true;
c7d9c476
VZ
2737 SetFocus();
2738 m_htClickedItem = (WXHTREEITEM) htItem;
2739 m_ptClick = wxPoint(x, y);
2740
2741 if ( wParam & MK_CONTROL )
2742 {
2743 if ( HandleMouseEvent(nMsg, x, y, wParam) )
3f7bc32b 2744 {
c7d9c476
VZ
2745 m_htClickedItem.Unset();
2746 break;
2747 }
23f681ec 2748
c7d9c476
VZ
2749 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2750 this, htItem);
2751 changingEvent.m_itemOld = htOldItem;
2752
2753 if ( IsTreeEventAllowed(changingEvent) )
2754 {
3f7bc32b 2755 // toggle selected state
7dac12ef 2756 DoToggleItemSelection(wxTreeItemId(htItem));
3f7bc32b 2757
7dac12ef 2758 SetFocusedItem(wxTreeItemId(htItem));
3f7bc32b
VZ
2759
2760 // reset on any click without Shift
f888d614 2761 m_htSelStart.Unset();
3f7bc32b 2762
c7d9c476
VZ
2763 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2764 this, htItem);
2765 changedEvent.m_itemOld = htOldItem;
2766 (void)HandleTreeEvent(changedEvent);
3f7bc32b 2767 }
c7d9c476
VZ
2768 }
2769 else if ( wParam & MK_SHIFT )
2770 {
2771 if ( HandleMouseEvent(nMsg, x, y, wParam) )
3f7bc32b 2772 {
c7d9c476
VZ
2773 m_htClickedItem.Unset();
2774 break;
2775 }
3f7bc32b 2776
c7d9c476
VZ
2777 int srFlags = 0;
2778 bool willChange = true;
3f7bc32b 2779
c7d9c476
VZ
2780 if ( !(wParam & MK_CONTROL) )
2781 {
2782 srFlags |= SR_UNSELECT_OTHERS;
2783 }
23f681ec 2784
c7d9c476
VZ
2785 if ( !m_htSelStart )
2786 {
2787 // take the focused item
2788 m_htSelStart = htOldItem;
3f7bc32b 2789 }
c7d9c476 2790 else
3f7bc32b 2791 {
c7d9c476
VZ
2792 willChange = SelectRange(GetHwnd(), HITEM(m_htSelStart),
2793 htItem, srFlags | SR_SIMULATE);
2794 }
8ecd5de2 2795
c7d9c476
VZ
2796 if ( willChange )
2797 {
2798 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2799 this, htItem);
2800 changingEvent.m_itemOld = htOldItem;
5e7718a2 2801
c7d9c476 2802 if ( IsTreeEventAllowed(changingEvent) )
df4ac4c7 2803 {
c7d9c476
VZ
2804 // this selects all items between the starting one
2805 // and the current
2806 if ( m_htSelStart )
2807 {
2808 SelectRange(GetHwnd(), HITEM(m_htSelStart),
2809 htItem, srFlags);
2810 }
2811 else
35cf1ec6 2812 {
7dac12ef 2813 DoSelectItem(wxTreeItemId(htItem));
c7d9c476 2814 }
35cf1ec6 2815
7dac12ef 2816 SetFocusedItem(wxTreeItemId(htItem));
35cf1ec6 2817
c7d9c476
VZ
2818 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2819 this, htItem);
2820 changedEvent.m_itemOld = htOldItem;
2821 (void)HandleTreeEvent(changedEvent);
2822 }
2823 }
2824 }
2825 else // normal click
2826 {
2827 // avoid doing anything if we click on the only
2828 // currently selected item
2829
2830 wxArrayTreeItemIds selections;
2831 size_t count = GetSelections(selections);
2832
2833 if ( count == 0 ||
2834 count > 1 ||
2835 HITEM(selections[0]) != htItem )
2836 {
2837 if ( HandleMouseEvent(nMsg, x, y, wParam) )
2838 {
2839 m_htClickedItem.Unset();
2840 break;
2841 }
2842
2843 // clear the previously selected items, if the user
2844 // clicked outside of the present selection, otherwise,
2845 // perform the deselection on mouse-up, this allows
2846 // multiple drag and drop to work.
c7d9c476
VZ
2847 if ( !IsItemSelected(GetHwnd(), htItem))
2848 {
2849 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2850 this, htItem);
2851 changingEvent.m_itemOld = htOldItem;
2852
2853 if ( IsTreeEventAllowed(changingEvent) )
2854 {
2855 DoUnselectAll();
7dac12ef
VZ
2856 DoSelectItem(wxTreeItemId(htItem));
2857 SetFocusedItem(wxTreeItemId(htItem));
c7d9c476
VZ
2858
2859 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2860 this, htItem);
2861 changedEvent.m_itemOld = htOldItem;
2862 (void)HandleTreeEvent(changedEvent);
35cf1ec6 2863 }
c7d9c476
VZ
2864 }
2865 else
2866 {
7dac12ef 2867 SetFocusedItem(wxTreeItemId(htItem));
df4ac4c7 2868 }
c7d9c476
VZ
2869 }
2870 else // click on a single selected item
2871 {
2872 // don't interfere with the default processing in
2873 // WM_MOUSEMOVE handler below as the default window
2874 // proc will start the drag itself if we let have
2875 // WM_LBUTTONDOWN
2876 m_htClickedItem.Unset();
2877
2878 // prevent in-place editing from starting if focus lost
2879 // since previous click
2880 if ( m_focusLost )
b8e3f1cf 2881 {
7dac12ef
VZ
2882 ClearFocusedItem();
2883 DoSelectItem(wxTreeItemId(htItem));
2884 SetFocusedItem(wxTreeItemId(htItem));
2885 }
2886 else
2887 {
2888 processed = false;
b8e3f1cf 2889 }
c7d9c476
VZ
2890 }
2891
2892 // reset on any click without Shift
2893 m_htSelStart.Unset();
2894 }
2895
2896 m_focusLost = false;
2897
2898 // we consumed the event so we need to trigger state image
2899 // click if needed
2900 if ( processed )
2901 {
2902 int htFlags = 0;
2903 wxTreeItemId item = HitTest(wxPoint(x, y), htFlags);
2904
2905 if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON )
2906 {
2907 m_triggerStateImageClick = true;
3f7bc32b
VZ
2908 }
2909 }
2910 break;
3f7bc32b 2911
e0bf68d6 2912 case WM_RBUTTONDOWN:
c7d9c476
VZ
2913 if ( !isMultiple )
2914 break;
2915
2916 processed = true;
2917 SetFocus();
2918
2919 if ( HandleMouseEvent(nMsg, x, y, wParam) || !htItem )
2920 {
2921 break;
2922 }
2923
e0bf68d6
VZ
2924 // default handler removes the highlight from the currently
2925 // focused item when right mouse button is pressed on another
2926 // one but keeps the remaining items highlighted, which is
c7d9c476
VZ
2927 // confusing, so override this default behaviour
2928 if ( !IsItemSelected(GetHwnd(), htItem) )
e0bf68d6 2929 {
c7d9c476
VZ
2930 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
2931 this, htItem);
2932 changingEvent.m_itemOld = htOldItem;
2933
2934 if ( IsTreeEventAllowed(changingEvent) )
e0bf68d6 2935 {
c7d9c476 2936 DoUnselectAll();
7dac12ef
VZ
2937 DoSelectItem(wxTreeItemId(htItem));
2938 SetFocusedItem(wxTreeItemId(htItem));
e0bf68d6 2939
c7d9c476
VZ
2940 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
2941 this, htItem);
2942 changedEvent.m_itemOld = htOldItem;
2943 (void)HandleTreeEvent(changedEvent);
2944 }
e0bf68d6 2945 }
c7d9c476 2946
e0bf68d6
VZ
2947 break;
2948
3f7bc32b 2949 case WM_MOUSEMOVE:
68fc69f3 2950#ifndef __WXWINCE__
e3ad5702
JS
2951 if ( m_htClickedItem )
2952 {
2953 int cx = abs(m_ptClick.x - x);
2954 int cy = abs(m_ptClick.y - y);
2955
b8e3f1cf
VZ
2956 if ( cx > ::GetSystemMetrics(SM_CXDRAG) ||
2957 cy > ::GetSystemMetrics(SM_CYDRAG) )
e3ad5702 2958 {
b8e3f1cf
VZ
2959 NM_TREEVIEW tv;
2960 wxZeroMemory(tv);
e3ad5702 2961
b8e3f1cf
VZ
2962 tv.hdr.hwndFrom = GetHwnd();
2963 tv.hdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
2964 tv.hdr.code = TVN_BEGINDRAG;
8ecd5de2 2965
b8e3f1cf 2966 tv.itemNew.hItem = HITEM(m_htClickedItem);
8ecd5de2 2967
8ecd5de2 2968
b8e3f1cf
VZ
2969 TVITEM tviAux;
2970 wxZeroMemory(tviAux);
8ecd5de2 2971
b8e3f1cf
VZ
2972 tviAux.hItem = HITEM(m_htClickedItem);
2973 tviAux.mask = TVIF_STATE | TVIF_PARAM;
2974 tviAux.stateMask = 0xffffffff;
2975 TreeView_GetItem(GetHwnd(), &tviAux);
8ecd5de2 2976
b8e3f1cf
VZ
2977 tv.itemNew.state = tviAux.state;
2978 tv.itemNew.lParam = tviAux.lParam;
2979
2980 tv.ptDrag.x = x;
2981 tv.ptDrag.y = y;
2982
2983 // do it before SendMessage() call below to avoid
2984 // reentrancies here if there is another WM_MOUSEMOVE
2985 // in the queue already
e3ad5702 2986 m_htClickedItem.Unset();
b8e3f1cf
VZ
2987
2988 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY,
2989 tv.hdr.idFrom, (LPARAM)&tv );
2990
2991 // don't pass it to the default window proc, it would
2992 // start dragging again
2993 processed = true;
e3ad5702 2994 }
e3ad5702 2995 }
68fc69f3 2996#endif // __WXWINCE__
8ecd5de2 2997
68d9be05 2998#if wxUSE_DRAGIMAGE
3f7bc32b
VZ
2999 if ( m_dragImage )
3000 {
afff720b 3001 m_dragImage->Move(wxPoint(x, y));
3f7bc32b 3002 if ( htItem )
23f681ec
VZ
3003 {
3004 // highlight the item as target (hiding drag image is
3005 // necessary - otherwise the display will be corrupted)
68be9f09 3006 m_dragImage->Hide();
3f7bc32b 3007 TreeView_SelectDropTarget(GetHwnd(), htItem);
68be9f09 3008 m_dragImage->Show();
23f681ec
VZ
3009 }
3010 }
68d9be05 3011#endif // wxUSE_DRAGIMAGE
23f681ec
VZ
3012 break;
3013
3014 case WM_LBUTTONUP:
c7d9c476 3015 if ( isMultiple )
35cf1ec6 3016 {
c7d9c476
VZ
3017 // deselect other items if multiple items selected
3018 if ( htItem )
35cf1ec6 3019 {
c7d9c476
VZ
3020 wxArrayTreeItemIds selections;
3021 size_t count = GetSelections(selections);
3022
3023 if ( count > 1 &&
3024 !(wParam & MK_CONTROL) &&
3025 !(wParam & MK_SHIFT) )
3026 {
3027 wxTreeEvent changingEvent(wxEVT_COMMAND_TREE_SEL_CHANGING,
3028 this, htItem);
3029 changingEvent.m_itemOld = htOldItem;
3030
3031 if ( IsTreeEventAllowed(changingEvent) )
3032 {
3033 DoUnselectAll();
7dac12ef
VZ
3034 DoSelectItem(wxTreeItemId(htItem));
3035 SetFocusedItem(wxTreeItemId(htItem));
c7d9c476
VZ
3036
3037 wxTreeEvent changedEvent(wxEVT_COMMAND_TREE_SEL_CHANGED,
3038 this, htItem);
3039 changedEvent.m_itemOld = htOldItem;
3040 (void)HandleTreeEvent(changedEvent);
3041 }
3042 }
35cf1ec6 3043 }
c7d9c476 3044
e3ad5702 3045 m_htClickedItem.Unset();
c7d9c476
VZ
3046
3047 if ( m_triggerStateImageClick )
3048 {
3049 if ( tvht.flags & TVHT_ONITEMSTATEICON )
3050 {
3051 wxTreeEvent event(wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK,
3052 this, htItem);
3053 (void)HandleTreeEvent(event);
3054
3055 m_triggerStateImageClick = false;
3056 processed = true;
3057 }
3058 }
3059
3060 if ( !m_dragStarted &&
3061 (tvht.flags & TVHT_ONITEMSTATEICON ||
3062 tvht.flags & TVHT_ONITEMICON ||
3063 tvht.flags & TVHT_ONITEM) )
3064 {
3065 processed = true;
3066 }
35cf1ec6
VZ
3067 }
3068
3069 // fall through
3070
23f681ec 3071 case WM_RBUTTONUP:
68d9be05 3072#if wxUSE_DRAGIMAGE
3f7bc32b 3073 if ( m_dragImage )
23f681ec 3074 {
68be9f09 3075 m_dragImage->EndDrag();
23f681ec
VZ
3076 delete m_dragImage;
3077 m_dragImage = NULL;
3078
3079 // generate the drag end event
c7d9c476
VZ
3080 wxTreeEvent event(wxEVT_COMMAND_TREE_END_DRAG,
3081 this, htItem);
72e61024 3082 event.m_pointDrag = wxPoint(x, y);
c7d9c476 3083 (void)HandleTreeEvent(event);
225fe9d6
VZ
3084
3085 // if we don't do it, the tree seems to think that 2 items
3086 // are selected simultaneously which is quite weird
3087 TreeView_SelectDropTarget(GetHwnd(), 0);
23f681ec 3088 }
68d9be05 3089#endif // wxUSE_DRAGIMAGE
c7d9c476
VZ
3090
3091 if ( isMultiple && nMsg == WM_RBUTTONUP )
3092 {
3093 // send NM_RCLICK
3094 NMHDR nmhdr;
3095 nmhdr.hwndFrom = GetHwnd();
3096 nmhdr.idFrom = ::GetWindowLong(GetHwnd(), GWL_ID);
3097 nmhdr.code = NM_RCLICK;
3098 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY,
3099 nmhdr.idFrom, (LPARAM)&nmhdr);
3100 processed = true;
3101 }
3102
3103 m_dragStarted = false;
3104
23f681ec
VZ
3105 break;
3106 }
3107 }
c7d9c476 3108 else if ( (nMsg == WM_SETFOCUS || nMsg == WM_KILLFOCUS) )
3f7bc32b 3109 {
c7d9c476 3110 if ( isMultiple )
3f7bc32b 3111 {
c7d9c476
VZ
3112 // the tree control greys out the selected item when it loses focus
3113 // and paints it as selected again when it regains it, but it won't
3114 // do it for the other items itself - help it
3115 wxArrayTreeItemIds selections;
3116 size_t count = GetSelections(selections);
3117 RECT rect;
3118
3119 for ( size_t n = 0; n < count; n++ )
3f7bc32b 3120 {
c7d9c476
VZ
3121 // TreeView_GetItemRect() will return false if item is not
3122 // visible, which may happen perfectly well
3123 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections[n]),
3124 &rect, TRUE) )
3125 {
3126 ::InvalidateRect(GetHwnd(), &rect, FALSE);
3127 }
3f7bc32b
VZ
3128 }
3129 }
3f7bc32b 3130
c7d9c476 3131 if ( nMsg == WM_KILLFOCUS )
3f7bc32b 3132 {
c7d9c476
VZ
3133 m_focusLost = true;
3134 }
3135 }
3136 else if ( (nMsg == WM_KEYDOWN || nMsg == WM_SYSKEYDOWN) && isMultiple )
3137 {
aa8166dd
VZ
3138 // normally we want to generate wxEVT_KEY_DOWN events from TVN_KEYDOWN
3139 // notification but for the keys which can be used to change selection
3140 // we need to do it from here so as to not apply the default behaviour
3141 // if the events are handled by the user code
3142 switch ( wParam )
c7d9c476 3143 {
aa8166dd
VZ
3144 case VK_RETURN:
3145 case VK_SPACE:
3146 case VK_UP:
3147 case VK_DOWN:
3148 case VK_LEFT:
3149 case VK_RIGHT:
3150 case VK_HOME:
3151 case VK_END:
3152 case VK_PRIOR:
3153 case VK_NEXT:
3154 if ( !MSWHandleTreeKeyDownEvent(wParam, lParam) )
3155 {
3156 // use the key to update the selection if it was left
3157 // unprocessed
3158 MSWHandleSelectionKey(wParam);
aa8166dd 3159 }
ba8a1961 3160
7dac12ef
VZ
3161 // pretend that we did process it in any case as we already
3162 // generated an event for it
3163 processed = true;
3164
aa8166dd
VZ
3165 //default: for all the other keys leave processed as false so that
3166 // the tree control generates a TVN_KEYDOWN for us
c7d9c476 3167 }
3f7bc32b 3168
3f7bc32b 3169 }
d35dce3a
VZ
3170 else if ( nMsg == WM_COMMAND )
3171 {
3172 // if we receive a EN_KILLFOCUS command from the in-place edit control
3173 // used for label editing, make sure to end editing
3174 WORD id, cmd;
3175 WXHWND hwnd;
3176 UnpackCommand(wParam, lParam, &id, &hwnd, &cmd);
3177
3178 if ( cmd == EN_KILLFOCUS )
3179 {
3180 if ( m_textCtrl && m_textCtrl->GetHandle() == hwnd )
3181 {
3182 DoEndEditLabel();
3183
3184 processed = true;
3185 }
3186 }
3187 }
7bb8798c 3188
3f7bc32b
VZ
3189 if ( !processed )
3190 rc = wxControl::MSWWindowProc(nMsg, wParam, lParam);
3191
3192 return rc;
23f681ec
VZ
3193}
3194
fbd8ac52
VZ
3195WXLRESULT
3196wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
3197{
068b764a
VZ
3198 if ( nMsg == WM_CHAR )
3199 {
df63b2a4 3200 // don't let the control process Space and Return keys because it
068b764a
VZ
3201 // doesn't do anything useful with them anyhow but always beeps
3202 // annoyingly when it receives them and there is no way to turn it off
3203 // simply if you just process TREEITEM_ACTIVATED event to which Space
3204 // and Enter presses are mapped in your code
3205 if ( wParam == VK_SPACE || wParam == VK_RETURN )
3206 return 0;
3207 }
c4cd8712
VZ
3208#if wxUSE_DRAGIMAGE
3209 else if ( nMsg == WM_KEYDOWN )
3210 {
3211 if ( wParam == VK_ESCAPE )
3212 {
3213 if ( m_dragImage )
3214 {
3215 m_dragImage->EndDrag();
3216 delete m_dragImage;
3217 m_dragImage = NULL;
3218
3219 // if we don't do it, the tree seems to think that 2 items
3220 // are selected simultaneously which is quite weird
3221 TreeView_SelectDropTarget(GetHwnd(), 0);
3222 }
3223 }
3224 }
3225#endif // wxUSE_DRAGIMAGE
068b764a 3226
fbd8ac52
VZ
3227 return wxControl::MSWDefWindowProc(nMsg, wParam, lParam);
3228}
3229
08b7c251 3230// process WM_NOTIFY Windows message
a23fd0e1 3231bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
08b7c251 3232{
09f277d6 3233 wxTreeEvent event(wxEVT_NULL, this);
08b7c251
VZ
3234 wxEventType eventType = wxEVT_NULL;
3235 NMHDR *hdr = (NMHDR *)lParam;
3236
3237 switch ( hdr->code )
2bda0e17 3238 {
08b7c251
VZ
3239 case TVN_BEGINDRAG:
3240 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
3241 // fall through
3242
3243 case TVN_BEGINRDRAG:
3244 {
3245 if ( eventType == wxEVT_NULL )
3246 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
3247 //else: left drag, already set above
3248
3249 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3250
ee4b2721 3251 event.m_item = tv->itemNew.hItem;
08b7c251 3252 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
23f681ec
VZ
3253
3254 // don't allow dragging by default: the user code must
3255 // explicitly say that it wants to allow it to avoid breaking
3256 // the old apps
3257 event.Veto();
08b7c251 3258 }
696e1ea0 3259 break;
08b7c251
VZ
3260
3261 case TVN_BEGINLABELEDIT:
3262 {
3263 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
3264 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3265
646a8a4d
VZ
3266 // although the user event handler may still veto it, it is
3267 // important to set it now so that calls to SetItemText() from
3268 // the event handler would change the text controls contents
3269 m_idEdited =
ee4b2721 3270 event.m_item = info->item.hItem;
5ea47806 3271 event.m_label = info->item.pszText;
04cd30de 3272 event.m_editCancelled = false;
08b7c251 3273 }
696e1ea0 3274 break;
08b7c251
VZ
3275
3276 case TVN_DELETEITEM:
3277 {
3278 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
3279 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
3280
ee4b2721 3281 event.m_item = tv->itemOld.hItem;
696e1ea0
VZ
3282
3283 if ( m_hasAnyAttr )
3284 {
ee4b2721
VZ
3285 wxMapTreeAttr::iterator it = m_attrs.find(tv->itemOld.hItem);
3286 if ( it != m_attrs.end() )
3287 {
3288 delete it->second;
3289 m_attrs.erase(it);
3290 }
696e1ea0 3291 }
08b7c251 3292 }
696e1ea0 3293 break;
08b7c251
VZ
3294
3295 case TVN_ENDLABELEDIT:
3296 {
3297 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
3298 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3299
ee4b2721 3300 event.m_item = info->item.hItem;
5ea47806 3301 event.m_label = info->item.pszText;
06110b00 3302 event.m_editCancelled = info->item.pszText == NULL;
08b7c251
VZ
3303 break;
3304 }
3305
676d6550 3306#ifndef __WXWINCE__
156194e1
JS
3307 // These *must* not be removed or TVN_GETINFOTIP will
3308 // not be processed each time the mouse is moved
3309 // and the tooltip will only ever update once.
3310 case TTN_NEEDTEXTA:
3311 case TTN_NEEDTEXTW:
3312 {
3313 *result = 0;
3314
3315 break;
3316 }
3317
f3f71703 3318#ifdef TVN_GETINFOTIP
156194e1
JS
3319 case TVN_GETINFOTIP:
3320 {
3321 eventType = wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP;
3322 NMTVGETINFOTIP *info = (NMTVGETINFOTIP*)lParam;
3323
3324 // Which item are we trying to get a tooltip for?
bc0aebab 3325 event.m_item = info->hItem;
156194e1
JS
3326
3327 break;
3328 }
df63b2a4
VZ
3329#endif // TVN_GETINFOTIP
3330#endif // !__WXWINCE__
5e7718a2 3331
08b7c251
VZ
3332 case TVN_GETDISPINFO:
3333 eventType = wxEVT_COMMAND_TREE_GET_INFO;
3334 // fall through
3335
3336 case TVN_SETDISPINFO:
3337 {
3338 if ( eventType == wxEVT_NULL )
3339 eventType = wxEVT_COMMAND_TREE_SET_INFO;
3340 //else: get, already set above
3341
3342 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
3343
ee4b2721 3344 event.m_item = info->item.hItem;
08b7c251
VZ
3345 break;
3346 }
3347
3348 case TVN_ITEMEXPANDING:
08b7c251
VZ
3349 case TVN_ITEMEXPANDED:
3350 {
4523ebb3 3351 NM_TREEVIEW *tv = (NM_TREEVIEW*)lParam;
08b7c251 3352
deb1de30 3353 int what;
08b7c251
VZ
3354 switch ( tv->action )
3355 {
deb1de30
VZ
3356 default:
3357 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv->action);
3358 // fall through
3359
08b7c251 3360 case TVE_EXPAND:
deb1de30 3361 what = IDX_EXPAND;
08b7c251
VZ
3362 break;
3363
3364 case TVE_COLLAPSE:
deb1de30 3365 what = IDX_COLLAPSE;
08b7c251 3366 break;
08b7c251
VZ
3367 }
3368
2b5f62a0
VZ
3369 int how = hdr->code == TVN_ITEMEXPANDING ? IDX_DOING
3370 : IDX_DONE;
deb1de30
VZ
3371
3372 eventType = gs_expandEvents[what][how];
08b7c251 3373
ee4b2721 3374 event.m_item = tv->itemNew.hItem;
08b7c251 3375 }
696e1ea0 3376 break;
08b7c251
VZ
3377
3378 case TVN_KEYDOWN:
3379 {
08b7c251
VZ
3380 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
3381
1944ad76
VZ
3382 // fabricate the lParam and wParam parameters sufficiently
3383 // similar to the ones from a "real" WM_KEYDOWN so that
3384 // CreateKeyEvent() works correctly
47e10541
VZ
3385 return MSWHandleTreeKeyDownEvent(
3386 info->wVKey, (wxIsAltDown() ? KF_ALTDOWN : 0) << 16);
08b7c251
VZ
3387 }
3388
5fe302ef
BW
3389
3390 // Vista's tree control has introduced some problems with our
3391 // multi-selection tree. When TreeView_SelectItem() is called,
3392 // the wrong items are deselected.
20bdddad 3393
5fe302ef
BW
3394 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
3395 // that can be used to regulate this incorrect behavior. The
3396 // following messages will allow only the unlocked item's selection
3397 // state to change
396f85ba 3398
5fe302ef
BW
3399 case TVN_ITEMCHANGINGA:
3400 case TVN_ITEMCHANGINGW:
3401 {
3402 // we only need to handles these in multi-select trees
3403 if ( HasFlag(wxTR_MULTIPLE) )
3404 {
3405 // get info about the item about to be changed
3406 NMTVITEMCHANGE* info = (NMTVITEMCHANGE*)lParam;
396f85ba 3407 if (TreeItemUnlocker::IsLocked(info->hItem))
5fe302ef
BW
3408 {
3409 // item's state is locked, don't allow the change
3410 // returning 1 will disallow the change
20bdddad 3411 *result = 1;
5fe302ef
BW
3412 return true;
3413 }
3414 }
3415
3416 // allow the state change
3417 }
3418 return false;
3419
5e7718a2 3420 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
69a8b5b4
VS
3421 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
3422 // we have to handle both messages:
3423 case TVN_SELCHANGEDA:
3424 case TVN_SELCHANGEDW:
7dac12ef 3425 if ( !HasFlag(wxTR_MULTIPLE) || !m_changingSelection )
c7d9c476
VZ
3426 {
3427 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
3428 }
08b7c251
VZ
3429 // fall through
3430
69a8b5b4
VS
3431 case TVN_SELCHANGINGA:
3432 case TVN_SELCHANGINGW:
7dac12ef 3433 if ( !HasFlag(wxTR_MULTIPLE) || !m_changingSelection )
08b7c251
VZ
3434 {
3435 if ( eventType == wxEVT_NULL )
3436 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
3437 //else: already set above
3438
5e7718a2 3439 if (hdr->code == TVN_SELCHANGINGW ||
69a8b5b4
VS
3440 hdr->code == TVN_SELCHANGEDW)
3441 {
4523ebb3 3442 NM_TREEVIEWW *tv = (NM_TREEVIEWW *)lParam;
ee4b2721
VZ
3443 event.m_item = tv->itemNew.hItem;
3444 event.m_itemOld = tv->itemOld.hItem;
69a8b5b4
VS
3445 }
3446 else
3447 {
4523ebb3 3448 NM_TREEVIEWA *tv = (NM_TREEVIEWA *)lParam;
ee4b2721
VZ
3449 event.m_item = tv->itemNew.hItem;
3450 event.m_itemOld = tv->itemOld.hItem;
69a8b5b4 3451 }
08b7c251 3452 }
cb776b66
VZ
3453
3454 // we receive this message from WM_LBUTTONDOWN handler inside
3455 // comctl32.dll and so before the click is passed to
3456 // DefWindowProc() which sets the focus to the window which was
3457 // clicked and this can lead to unexpected event sequences: for
3458 // example, we may get a "selection change" event from the tree
3459 // before getting a "kill focus" event for the text control which
3460 // had the focus previously, thus breaking user code doing input
3461 // validation
3462 //
3463 // to avoid such surprises, we force the generation of focus events
3464 // now, before we generate the selection change ones
3465 SetFocus();
696e1ea0
VZ
3466 break;
3467
c7d9c476
VZ
3468 // instead of explicitly checking for _WIN32_IE, check if the
3469 // required symbols are available in the headers
5b59df83 3470#if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
696e1ea0
VZ
3471 case NM_CUSTOMDRAW:
3472 {
3473 LPNMTVCUSTOMDRAW lptvcd = (LPNMTVCUSTOMDRAW)lParam;
3474 NMCUSTOMDRAW& nmcd = lptvcd->nmcd;
d7926e0b 3475 switch ( nmcd.dwDrawStage )
696e1ea0
VZ
3476 {
3477 case CDDS_PREPAINT:
3478 // if we've got any items with non standard attributes,
3479 // notify us before painting each item
3480 *result = m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
3481 : CDRF_DODEFAULT;
4754ab16
RR
3482
3483 // windows in TreeCtrl use one-based index for item state images,
3484 // 0 indexed image is not being used, we're using zero-based index,
3485 // so we have to add temp image (of zero index) to state image list
3486 // before we draw any item, then after items are drawn we have to
3487 // delete it (in POSTPAINT notify)
3488 if (m_imageListState && m_imageListState->GetImageCount() > 0)
3489 {
2ec94d6b 3490 typedef BOOL (wxSTDCALL *ImageList_Copy_t)
c27dce18
VZ
3491 (HIMAGELIST, int, HIMAGELIST, int, UINT);
3492 static ImageList_Copy_t s_pfnImageList_Copy = NULL;
3493 static bool loaded = false;
3494
3495 if ( !loaded )
3496 {
3497 wxLoadedDLL dllComCtl32(_T("comctl32.dll"));
3498 if ( dllComCtl32.IsLoaded() )
3499 wxDL_INIT_FUNC(s_pfn, ImageList_Copy, dllComCtl32);
3500 }
3501
3502 if ( !s_pfnImageList_Copy )
3503 {
3504 // this code is broken with ImageList_Copy()
3505 // but I don't care enough about Win95 support
3506 // to write it now -- if anybody does, please
3507 // do it
3508 wxFAIL_MSG("TODO: implement this for Win95");
3509 break;
3510 }
3511
3512 const HIMAGELIST
3513 hImageList = GetHimagelistOf(m_imageListState);
4754ab16
RR
3514
3515 // add temporary image
3516 int width, height;
3517 m_imageListState->GetSize(0, width, height);
3518
3519 HBITMAP hbmpTemp = ::CreateBitmap(width, height, 1, 1, NULL);
3520 int index = ::ImageList_Add(hImageList, hbmpTemp, hbmpTemp);
3521 ::DeleteObject(hbmpTemp);
3522
3523 if ( index != -1 )
3524 {
3525 // move images to right
3526 for ( int i = index; i > 0; i-- )
c27dce18
VZ
3527 {
3528 (*s_pfnImageList_Copy)(hImageList, i,
3529 hImageList, i-1,
3530 ILCF_MOVE);
3531 }
4754ab16
RR
3532
3533 // we must remove the image in POSTPAINT notify
3534 *result |= CDRF_NOTIFYPOSTPAINT;
3535 }
4754ab16
RR
3536 }
3537 break;
3538
3539 case CDDS_POSTPAINT:
3540 // we are deleting temp image of 0 index, which was
3541 // added before items were drawn (in PREPAINT notify)
3542 if (m_imageListState && m_imageListState->GetImageCount() > 0)
3543 m_imageListState->Remove(0);
d7926e0b 3544 break;
696e1ea0
VZ
3545
3546 case CDDS_ITEMPREPAINT:
3547 {
ee4b2721
VZ
3548 wxMapTreeAttr::iterator
3549 it = m_attrs.find((void *)nmcd.dwItemSpec);
696e1ea0 3550
ee4b2721 3551 if ( it == m_attrs.end() )
696e1ea0
VZ
3552 {
3553 // nothing to do for this item
d7926e0b
VZ
3554 *result = CDRF_DODEFAULT;
3555 break;
696e1ea0
VZ
3556 }
3557
ee4b2721
VZ
3558 wxTreeItemAttr * const attr = it->second;
3559
8b713759
VZ
3560 wxTreeViewItem tvItem((void *)nmcd.dwItemSpec,
3561 TVIF_STATE, TVIS_DROPHILITED);
3562 DoGetItem(&tvItem);
3563 const UINT tvItemState = tvItem.state;
3564
19da49fc 3565 // selection colours should override ours,
8b713759
VZ
3566 // otherwise it is too confusing to the user
3567 if ( !(nmcd.uItemState & CDIS_SELECTED) &&
3568 !(tvItemState & TVIS_DROPHILITED) )
696e1ea0 3569 {
2076893b 3570 wxColour colBack;
696e1ea0
VZ
3571 if ( attr->HasBackgroundColour() )
3572 {
3573 colBack = attr->GetBackgroundColour();
19da49fc 3574 lptvcd->clrTextBk = wxColourToRGB(colBack);
696e1ea0 3575 }
19da49fc
VZ
3576 }
3577
3578 // but we still want to keep the special foreground
3579 // colour when we don't have focus (we can't keep
3580 // it when we do, it would usually be unreadable on
3581 // the almost inverted bg colour...)
8b713759
VZ
3582 if ( ( !(nmcd.uItemState & CDIS_SELECTED) ||
3583 FindFocus() != this ) &&
3584 !(tvItemState & TVIS_DROPHILITED) )
19da49fc
VZ
3585 {
3586 wxColour colText;
3587 if ( attr->HasTextColour() )
696e1ea0 3588 {
19da49fc
VZ
3589 colText = attr->GetTextColour();
3590 lptvcd->clrText = wxColourToRGB(colText);
696e1ea0 3591 }
696e1ea0
VZ
3592 }
3593
19da49fc 3594 if ( attr->HasFont() )
696e1ea0 3595 {
19da49fc
VZ
3596 HFONT hFont = GetHfontOf(attr->GetFont());
3597
696e1ea0
VZ
3598 ::SelectObject(nmcd.hdc, hFont);
3599
3600 *result = CDRF_NEWFONT;
3601 }
19da49fc 3602 else // no specific font
696e1ea0
VZ
3603 {
3604 *result = CDRF_DODEFAULT;
3605 }
696e1ea0 3606 }
d7926e0b 3607 break;
696e1ea0
VZ
3608
3609 default:
3610 *result = CDRF_DODEFAULT;
696e1ea0
VZ
3611 }
3612 }
d7926e0b
VZ
3613
3614 // we always process it
04cd30de 3615 return true;
5b59df83 3616#endif // have owner drawn support in headers
08b7c251 3617
8a000b6b
VZ
3618 case NM_CLICK:
3619 {
3620 DWORD pos = GetMessagePos();
3621 POINT point;
3622 point.x = LOWORD(pos);
3623 point.y = HIWORD(pos);
3624 ::MapWindowPoints(HWND_DESKTOP, GetHwnd(), &point, 1);
c7d9c476
VZ
3625 int htFlags = 0;
3626 wxTreeItemId item = HitTest(wxPoint(point.x, point.y), htFlags);
3627
3628 if ( htFlags & wxTREE_HITTEST_ONITEMSTATEICON )
8a000b6b
VZ
3629 {
3630 event.m_item = item;
3631 eventType = wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK;
3632 }
c7d9c476 3633
8a000b6b
VZ
3634 break;
3635 }
3636
f6bcfd97
BP
3637 case NM_DBLCLK:
3638 case NM_RCLICK:
3639 {
3640 TV_HITTESTINFO tvhti;
3641 ::GetCursorPos(&tvhti.pt);
3642 ::ScreenToClient(GetHwnd(), &tvhti.pt);
3643 if ( TreeView_HitTest(GetHwnd(), &tvhti) )
3644 {
3645 if ( tvhti.flags & TVHT_ONITEM )
3646 {
ee4b2721 3647 event.m_item = tvhti.hItem;
f6bcfd97
BP
3648 eventType = (int)hdr->code == NM_DBLCLK
3649 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
3650 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
8591268f
VZ
3651
3652 event.m_pointDrag.x = tvhti.pt.x;
3653 event.m_pointDrag.y = tvhti.pt.y;
f6bcfd97
BP
3654 }
3655
3656 break;
3657 }
3658 }
3659 // fall through
3660
08b7c251 3661 default:
a23fd0e1 3662 return wxControl::MSWOnNotify(idCtrl, lParam, result);
2bda0e17 3663 }
08b7c251 3664
08b7c251
VZ
3665 event.SetEventType(eventType);
3666
c7d9c476 3667 bool processed = HandleTreeEvent(event);
08b7c251
VZ
3668
3669 // post processing
5ea47806 3670 switch ( hdr->code )
2bda0e17 3671 {
f6bcfd97 3672 case NM_DBLCLK:
0e2ad323
VZ
3673 // we translate NM_DBLCLK into ACTIVATED event and if the user
3674 // handled the activation of the item we shouldn't proceed with
3675 // also using the same double click for toggling the item expanded
3676 // state -- but OTOH do let the user to expand/collapse the item by
3677 // double clicking on it if the activation is not handled specially
3678 *result = processed;
f6bcfd97
BP
3679 break;
3680
df63b2a4
VZ
3681 case NM_RCLICK:
3682 // prevent tree control from sending WM_CONTEXTMENU to our parent
3683 // (which it does if NM_RCLICK is not handled) because we want to
3684 // send it to the control itself
3685 *result =
3686 processed = true;
3687
3688 ::SendMessage(GetHwnd(), WM_CONTEXTMENU,
3689 (WPARAM)GetHwnd(), ::GetMessagePos());
3690 break;
3691
23f681ec
VZ
3692 case TVN_BEGINDRAG:
3693 case TVN_BEGINRDRAG:
68d9be05 3694#if wxUSE_DRAGIMAGE
23f681ec
VZ
3695 if ( event.IsAllowed() )
3696 {
3697 // normally this is impossible because the m_dragImage is
3698 // deleted once the drag operation is over
3699 wxASSERT_MSG( !m_dragImage, _T("starting to drag once again?") );
3700
3701 m_dragImage = new wxDragImage(*this, event.m_item);
c47addef 3702 m_dragImage->BeginDrag(wxPoint(0,0), this);
68be9f09 3703 m_dragImage->Show();
c7d9c476
VZ
3704
3705 m_dragStarted = true;
23f681ec 3706 }
68d9be05 3707#endif // wxUSE_DRAGIMAGE
23f681ec
VZ
3708 break;
3709
5ea47806
VZ
3710 case TVN_DELETEITEM:
3711 {
77ffb593 3712 // NB: we might process this message using wxWidgets event
5ea47806
VZ
3713 // tables, but due to overhead of wxWin event system we
3714 // prefer to do it here ourself (otherwise deleting a tree
3715 // with many items is just too slow)
4523ebb3 3716 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
4523ebb3
VZ
3717
3718 wxTreeItemParam *param =
3719 (wxTreeItemParam *)tv->itemOld.lParam;
3720 delete param;
08b7c251 3721
04cd30de 3722 processed = true; // Make sure we don't get called twice
5ea47806
VZ
3723 }
3724 break;
3725
3726 case TVN_BEGINLABELEDIT:
04cd30de 3727 // return true to cancel label editing
5ea47806 3728 *result = !event.IsAllowed();
646a8a4d 3729
cd6bd270 3730 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
646a8a4d 3731 if ( event.IsAllowed() )
cd6bd270
MB
3732 {
3733 HWND hText = TreeView_GetEditControl(GetHwnd());
4523ebb3 3734 if ( hText )
cd6bd270 3735 {
5e7718a2 3736 // MBN: if m_textCtrl already has an HWND, it is a stale
cd6bd270
MB
3737 // pointer from a previous edit (because the user
3738 // didn't modify the label before dismissing the control,
3739 // and TVN_ENDLABELEDIT was not sent), so delete it
4523ebb3 3740 if ( m_textCtrl && m_textCtrl->GetHWND() )
cd6bd270 3741 DeleteTextCtrl();
4523ebb3 3742 if ( !m_textCtrl )
cd6bd270
MB
3743 m_textCtrl = new wxTextCtrl();
3744 m_textCtrl->SetParent(this);
3745 m_textCtrl->SetHWND((WXHWND)hText);
3746 m_textCtrl->SubclassWin((WXHWND)hText);
3747
3748 // set wxTE_PROCESS_ENTER style for the text control to
3749 // force it to process the Enter presses itself, otherwise
3750 // they could be stolen from it by the dialog
3751 // navigation code
3752 m_textCtrl->SetWindowStyle(m_textCtrl->GetWindowStyle()
3753 | wxTE_PROCESS_ENTER);
3754 }
3755 }
646a8a4d
VZ
3756 else // we had set m_idEdited before
3757 {
3758 m_idEdited.Unset();
3759 }
5ea47806
VZ
3760 break;
3761
3762 case TVN_ENDLABELEDIT:
04cd30de 3763 // return true to set the label to the new string: note that we
188781db 3764 // also must pretend that we did process the message or it is going
04cd30de 3765 // to be passed to DefWindowProc() which will happily return false
188781db 3766 // cancelling the label change
5ea47806 3767 *result = event.IsAllowed();
04cd30de 3768 processed = true;
5ea47806
VZ
3769
3770 // ensure that we don't have the text ctrl which is going to be
3771 // deleted any more
3772 DeleteTextCtrl();
3773 break;
3774
676d6550 3775#ifndef __WXWINCE__
f3f71703 3776#ifdef TVN_GETINFOTIP
156194e1
JS
3777 case TVN_GETINFOTIP:
3778 {
3779 // If the user permitted a tooltip change, change it
3780 if (event.IsAllowed())
3781 {
3782 SetToolTip(event.m_label);
3783 }
3784 }
3785 break;
f3f71703 3786#endif
676d6550 3787#endif
5e7718a2 3788
5ea47806
VZ
3789 case TVN_SELCHANGING:
3790 case TVN_ITEMEXPANDING:
04cd30de 3791 // return true to prevent the action from happening
5ea47806
VZ
3792 *result = !event.IsAllowed();
3793 break;
3794
deb1de30 3795 case TVN_ITEMEXPANDED:
deb1de30 3796 {
4523ebb3 3797 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
5188000b 3798 const wxTreeItemId id(tv->itemNew.hItem);
deb1de30 3799
5188000b 3800 if ( tv->action == TVE_COLLAPSE )
bf43d750 3801 {
5188000b
VZ
3802 if ( wxApp::GetComCtl32Version() >= 600 )
3803 {
3804 // for some reason the item selection rectangle depends
3805 // on whether it is expanded or collapsed (at least
3806 // with comctl32.dll v6): it is wider (by 3 pixels) in
3807 // the expanded state, so when the item collapses and
3808 // then is deselected the rightmost 3 pixels of the
3809 // previously drawn selection are left on the screen
3810 //
3811 // it's not clear if it's a bug in comctl32.dll or in
3812 // our code (because it does not happen in Explorer but
3813 // OTOH we don't do anything which could result in this
3814 // AFAICS) but we do need to work around it to avoid
3815 // ugly artifacts
3816 RefreshItem(id);
3817 }
3818 }
3819 else // expand
3820 {
3821 // the item is also not refreshed properly after expansion when
3822 // it has an image depending on the expanded/collapsed state:
3823 // again, it's not clear if the bug is in comctl32.dll or our
3824 // code...
3825 int image = GetItemImage(id, wxTreeItemIcon_Expanded);
3826 if ( image != -1 )
3827 {
3828 RefreshItem(id);
3829 }
deb1de30
VZ
3830 }
3831 }
3832 break;
3833
74b31181
VZ
3834 case TVN_GETDISPINFO:
3835 // NB: so far the user can't set the image himself anyhow, so do it
3836 // anyway - but this may change later
4523ebb3 3837 //if ( /* !processed && */ )
74b31181
VZ
3838 {
3839 wxTreeItemId item = event.m_item;
3840 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
4523ebb3 3841
1b182562 3842 const wxTreeItemParam * const param = GetItemParam(item);
4523ebb3
VZ
3843 if ( !param )
3844 break;
3845
74b31181
VZ
3846 if ( info->item.mask & TVIF_IMAGE )
3847 {
3848 info->item.iImage =
4523ebb3 3849 param->GetImage
74b31181 3850 (
74b31181
VZ
3851 IsExpanded(item) ? wxTreeItemIcon_Expanded
3852 : wxTreeItemIcon_Normal
3853 );
3854 }
3855 if ( info->item.mask & TVIF_SELECTEDIMAGE )
3856 {
3857 info->item.iSelectedImage =
4523ebb3 3858 param->GetImage
74b31181 3859 (
74b31181
VZ
3860 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
3861 : wxTreeItemIcon_Selected
3862 );
3863 }
deb1de30 3864 }
74b31181
VZ
3865 break;
3866
5ea47806
VZ
3867 //default:
3868 // for the other messages the return value is ignored and there is
3869 // nothing special to do
3870 }
fd3f686c 3871 return processed;
2bda0e17
KB
3872}
3873
8a000b6b
VZ
3874// ----------------------------------------------------------------------------
3875// State control.
3876// ----------------------------------------------------------------------------
3877
3878// why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3879#define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3880
03966fcb 3881int wxTreeCtrl::DoGetItemState(const wxTreeItemId& item) const
8a000b6b 3882{
03966fcb 3883 wxCHECK_MSG( item.IsOk(), wxTREE_ITEMSTATE_NONE, wxT("invalid tree item") );
8a000b6b 3884
03966fcb
RR
3885 // receive the desired information
3886 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
3887 DoGetItem(&tvItem);
8a000b6b 3888
03966fcb
RR
3889 // state images are one-based
3890 return STATEIMAGEMASKTOINDEX(tvItem.state) - 1;
8a000b6b
VZ
3891}
3892
03966fcb 3893void wxTreeCtrl::DoSetItemState(const wxTreeItemId& item, int state)
8a000b6b 3894{
03966fcb
RR
3895 wxCHECK_RET( item.IsOk(), wxT("invalid tree item") );
3896
3897 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
8a000b6b 3898
03966fcb
RR
3899 // state images are one-based
3900 // 0 if no state image display (wxTREE_ITEMSTATE_NONE = -1)
3901 tvItem.state = INDEXTOSTATEIMAGEMASK(state + 1);
3902
3903 DoSetItem(&tvItem);
8a000b6b
VZ
3904}
3905
1e6feb95 3906#endif // wxUSE_TREECTRL