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