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