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