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