]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/treectrl.cpp
Catching up to latest thread changes
[wxWidgets.git] / src / msw / treectrl.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: treectrl.cpp
3// Purpose: wxTreeCtrl
4// Author: Julian Smart
5// Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
6// Created: 1997
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19#ifdef __GNUG__
20 #pragma implementation "treectrl.h"
21#endif
22
23// For compilers that support precompilation, includes "wx.h".
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
30#include "wx/window.h"
31#include "wx/msw/private.h"
32
33// Mingw32 is a bit mental even though this is done in winundef
34#ifdef GetFirstChild
35 #undef GetFirstChild
36#endif
37
38#ifdef GetNextSibling
39 #undef GetNextSibling
40#endif
41
42#if defined(__WIN95__)
43
44#include "wx/log.h"
45#include "wx/dynarray.h"
46#include "wx/imaglist.h"
47#include "wx/treectrl.h"
48#include "wx/settings.h"
49
50#ifdef __GNUWIN32__
51#ifndef wxUSE_NORLANDER_HEADERS
52#include "wx/msw/gnuwin32/extra.h"
53#endif
54#endif
55
56#if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
57 #include <commctrl.h>
58#endif
59
60// Bug in headers, sometimes
61#ifndef TVIS_FOCUSED
62 #define TVIS_FOCUSED 0x0001
63#endif
64
65// ----------------------------------------------------------------------------
66// private classes
67// ----------------------------------------------------------------------------
68
69// a convenient wrapper around TV_ITEM struct which adds a ctor
70#ifdef __VISUALC__
71#pragma warning( disable : 4097 )
72#endif
73
74struct wxTreeViewItem : public TV_ITEM
75{
76 wxTreeViewItem(const wxTreeItemId& item, // the item handle
77 UINT mask_, // fields which are valid
78 UINT stateMask_ = 0) // for TVIF_STATE only
79 {
80 // hItem member is always valid
81 mask = mask_ | TVIF_HANDLE;
82 stateMask = stateMask_;
83 hItem = (HTREEITEM) (WXHTREEITEM) item;
84 }
85};
86
87#ifdef __VISUALC__
88#pragma warning( default : 4097 )
89#endif
90
91// a class which encapsulates the tree traversal logic: it vists all (unless
92// OnVisit() returns FALSE) items under the given one
93class wxTreeTraversal
94{
95public:
96 wxTreeTraversal(const wxTreeCtrl *tree)
97 {
98 m_tree = tree;
99 }
100
101 // do traverse the tree: visit all items (recursively by default) under the
102 // given one; return TRUE if all items were traversed or FALSE if the
103 // traversal was aborted because OnVisit returned FALSE
104 bool DoTraverse(const wxTreeItemId& root, bool recursively = TRUE);
105
106 // override this function to do whatever is needed for each item, return
107 // FALSE to stop traversing
108 virtual bool OnVisit(const wxTreeItemId& item) = 0;
109
110protected:
111 const wxTreeCtrl *GetTree() const { return m_tree; }
112
113private:
114 bool Traverse(const wxTreeItemId& root, bool recursively);
115
116 const wxTreeCtrl *m_tree;
117};
118
119// internal class for getting the selected items
120class TraverseSelections : public wxTreeTraversal
121{
122public:
123 TraverseSelections(const wxTreeCtrl *tree,
124 wxArrayTreeItemIds& selections)
125 : wxTreeTraversal(tree), m_selections(selections)
126 {
127 m_selections.Empty();
128
129 DoTraverse(tree->GetRootItem());
130 }
131
132 virtual bool OnVisit(const wxTreeItemId& item)
133 {
134 if ( GetTree()->IsItemChecked(item) )
135 {
136 m_selections.Add(item);
137 }
138
139 return TRUE;
140 }
141
142private:
143 wxArrayTreeItemIds& m_selections;
144};
145
146// internal class for counting tree items
147class TraverseCounter : public wxTreeTraversal
148{
149public:
150 TraverseCounter(const wxTreeCtrl *tree,
151 const wxTreeItemId& root,
152 bool recursively)
153 : wxTreeTraversal(tree)
154 {
155 m_count = 0;
156
157 DoTraverse(root, recursively);
158 }
159
160 virtual bool OnVisit(const wxTreeItemId& item)
161 {
162 m_count++;
163
164 return TRUE;
165 }
166
167 size_t GetCount() const { return m_count; }
168
169private:
170 size_t m_count;
171};
172
173// ----------------------------------------------------------------------------
174// This class is needed for support of different images: the Win32 common
175// control natively supports only 2 images (the normal one and another for the
176// selected state). We wish to provide support for 2 more of them for folder
177// items (i.e. those which have children): for expanded state and for expanded
178// selected state. For this we use this structure to store the additional items
179// images.
180//
181// There is only one problem with this: when we retrieve the item's data, we
182// don't know whether we get a pointer to wxTreeItemData or
183// wxTreeItemIndirectData. So we have to maintain a list of all items which
184// have indirect data inside the listctrl itself.
185// ----------------------------------------------------------------------------
186class wxTreeItemIndirectData
187{
188public:
189 // ctor associates this data with the item and the real item data becomes
190 // available through our GetData() method
191 wxTreeItemIndirectData(wxTreeCtrl *tree, const wxTreeItemId& item)
192 {
193 for ( size_t n = 0; n < WXSIZEOF(m_images); n++ )
194 {
195 m_images[n] = -1;
196 }
197
198 // save the old data
199 m_data = tree->GetItemData(item);
200
201 // and set ourselves as the new one
202 tree->SetIndirectItemData(item, this);
203 }
204
205 // dtor deletes the associated data as well
206 ~wxTreeItemIndirectData() { delete m_data; }
207
208 // accessors
209 // get the real data associated with the item
210 wxTreeItemData *GetData() const { return m_data; }
211 // change it
212 void SetData(wxTreeItemData *data) { m_data = data; }
213
214 // do we have such image?
215 bool HasImage(wxTreeItemIcon which) const { return m_images[which] != -1; }
216 // get image
217 int GetImage(wxTreeItemIcon which) const { return m_images[which]; }
218 // change it
219 void SetImage(int image, wxTreeItemIcon which) { m_images[which] = image; }
220
221private:
222 // all the images associated with the item
223 int m_images[wxTreeItemIcon_Max];
224
225 wxTreeItemData *m_data;
226};
227
228// ----------------------------------------------------------------------------
229// macros
230// ----------------------------------------------------------------------------
231
232#if !USE_SHARED_LIBRARY
233 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl, wxControl)
234#endif
235
236// ----------------------------------------------------------------------------
237// variables
238// ----------------------------------------------------------------------------
239
240// handy table for sending events
241static const wxEventType g_events[2][2] =
242{
243 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
244 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
245};
246
247// ============================================================================
248// implementation
249// ============================================================================
250
251// ----------------------------------------------------------------------------
252// tree traversal
253// ----------------------------------------------------------------------------
254
255bool wxTreeTraversal::DoTraverse(const wxTreeItemId& root, bool recursively)
256{
257 if ( !OnVisit(root) )
258 return FALSE;
259
260 return Traverse(root, recursively);
261}
262
263bool wxTreeTraversal::Traverse(const wxTreeItemId& root, bool recursively)
264{
265 long cookie;
266 wxTreeItemId child = m_tree->GetFirstChild(root, cookie);
267 while ( child.IsOk() )
268 {
269 // depth first traversal
270 if ( recursively && !Traverse(child, TRUE) )
271 return FALSE;
272
273 if ( !OnVisit(child) )
274 return FALSE;
275
276 child = m_tree->GetNextChild(root, cookie);
277 }
278
279 return TRUE;
280}
281
282// ----------------------------------------------------------------------------
283// construction and destruction
284// ----------------------------------------------------------------------------
285
286void wxTreeCtrl::Init()
287{
288 m_imageListNormal = NULL;
289 m_imageListState = NULL;
290 m_textCtrl = NULL;
291}
292
293bool wxTreeCtrl::Create(wxWindow *parent,
294 wxWindowID id,
295 const wxPoint& pos,
296 const wxSize& size,
297 long style,
298 const wxValidator& validator,
299 const wxString& name)
300{
301 Init();
302
303 if ( !CreateControl(parent, id, pos, size, style, validator, name) )
304 return FALSE;
305
306 DWORD wstyle = WS_VISIBLE | WS_CHILD | WS_TABSTOP |
307 TVS_HASLINES | TVS_SHOWSELALWAYS;
308
309 if ( m_windowStyle & wxTR_HAS_BUTTONS )
310 wstyle |= TVS_HASBUTTONS;
311
312 if ( m_windowStyle & wxTR_EDIT_LABELS )
313 wstyle |= TVS_EDITLABELS;
314
315 if ( m_windowStyle & wxTR_LINES_AT_ROOT )
316 wstyle |= TVS_LINESATROOT;
317
318#if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined( __WATCOMC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
319 // we emulate the multiple selection tree controls by using checkboxes: set
320 // up the image list we need for this if we do have multiple selections
321#if !defined(__VISUALC__) || (__VISUALC__ > 1010)
322 if ( m_windowStyle & wxTR_MULTIPLE )
323 wstyle |= TVS_CHECKBOXES;
324#endif
325#endif
326
327 // Create the tree control.
328 if ( !MSWCreateControl(WC_TREEVIEW, wstyle) )
329 return FALSE;
330
331 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW));
332 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
333
334 // VZ: this is some experimental code which may be used to get the
335 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
336 // AFAIK, the standard DLL does about the same thing anyhow.
337#if 0
338 if ( m_windowStyle & wxTR_MULTIPLE )
339 {
340 wxBitmap bmp;
341
342 // create the DC compatible with the current screen
343 HDC hdcMem = CreateCompatibleDC(NULL);
344
345 // create a mono bitmap of the standard size
346 int x = GetSystemMetrics(SM_CXMENUCHECK);
347 int y = GetSystemMetrics(SM_CYMENUCHECK);
348 wxImageList imagelistCheckboxes(x, y, FALSE, 2);
349 HBITMAP hbmpCheck = CreateBitmap(x, y, // bitmap size
350 1, // # of color planes
351 1, // # bits needed for one pixel
352 0); // array containing colour data
353 SelectObject(hdcMem, hbmpCheck);
354
355 // then draw a check mark into it
356 RECT rect = { 0, 0, x, y };
357 if ( !::DrawFrameControl(hdcMem, &rect,
358 DFC_BUTTON,
359 DFCS_BUTTONCHECK | DFCS_CHECKED) )
360 {
361 wxLogLastError(wxT("DrawFrameControl(check)"));
362 }
363
364 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
365 imagelistCheckboxes.Add(bmp);
366
367 if ( !::DrawFrameControl(hdcMem, &rect,
368 DFC_BUTTON,
369 DFCS_BUTTONCHECK) )
370 {
371 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
372 }
373
374 bmp.SetHBITMAP((WXHBITMAP)hbmpCheck);
375 imagelistCheckboxes.Add(bmp);
376
377 // clean up
378 ::DeleteDC(hdcMem);
379
380 // set the imagelist
381 SetStateImageList(&imagelistCheckboxes);
382 }
383#endif // 0
384
385 SetSize(pos.x, pos.y, size.x, size.y);
386
387 return TRUE;
388}
389
390wxTreeCtrl::~wxTreeCtrl()
391{
392 DeleteTextCtrl();
393
394 // delete user data to prevent memory leaks
395 DeleteAllItems();
396}
397
398// ----------------------------------------------------------------------------
399// accessors
400// ----------------------------------------------------------------------------
401
402// simple wrappers which add error checking in debug mode
403
404bool wxTreeCtrl::DoGetItem(wxTreeViewItem* tvItem) const
405{
406 if ( !TreeView_GetItem(GetHwnd(), tvItem) )
407 {
408 wxLogLastError("TreeView_GetItem");
409
410 return FALSE;
411 }
412
413 return TRUE;
414}
415
416void wxTreeCtrl::DoSetItem(wxTreeViewItem* tvItem)
417{
418 if ( TreeView_SetItem(GetHwnd(), tvItem) == -1 )
419 {
420 wxLogLastError("TreeView_SetItem");
421 }
422}
423
424size_t wxTreeCtrl::GetCount() const
425{
426 return (size_t)TreeView_GetCount(GetHwnd());
427}
428
429unsigned int wxTreeCtrl::GetIndent() const
430{
431 return TreeView_GetIndent(GetHwnd());
432}
433
434void wxTreeCtrl::SetIndent(unsigned int indent)
435{
436 TreeView_SetIndent(GetHwnd(), indent);
437}
438
439wxImageList *wxTreeCtrl::GetImageList() const
440{
441 return m_imageListNormal;
442}
443
444wxImageList *wxTreeCtrl::GetStateImageList() const
445{
446 return m_imageListNormal;
447}
448
449void wxTreeCtrl::SetAnyImageList(wxImageList *imageList, int which)
450{
451 // no error return
452 TreeView_SetImageList(GetHwnd(),
453 imageList ? imageList->GetHIMAGELIST() : 0,
454 which);
455}
456
457void wxTreeCtrl::SetImageList(wxImageList *imageList)
458{
459 SetAnyImageList(m_imageListNormal = imageList, TVSIL_NORMAL);
460}
461
462void wxTreeCtrl::SetStateImageList(wxImageList *imageList)
463{
464 SetAnyImageList(m_imageListState = imageList, TVSIL_STATE);
465}
466
467size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId& item,
468 bool recursively) const
469{
470 TraverseCounter counter(this, item, recursively);
471
472 return counter.GetCount() - 1;
473}
474
475// ----------------------------------------------------------------------------
476// Item access
477// ----------------------------------------------------------------------------
478
479wxString wxTreeCtrl::GetItemText(const wxTreeItemId& item) const
480{
481 wxChar buf[512]; // the size is arbitrary...
482
483 wxTreeViewItem tvItem(item, TVIF_TEXT);
484 tvItem.pszText = buf;
485 tvItem.cchTextMax = WXSIZEOF(buf);
486 if ( !DoGetItem(&tvItem) )
487 {
488 // don't return some garbage which was on stack, but an empty string
489 buf[0] = wxT('\0');
490 }
491
492 return wxString(buf);
493}
494
495void wxTreeCtrl::SetItemText(const wxTreeItemId& item, const wxString& text)
496{
497 wxTreeViewItem tvItem(item, TVIF_TEXT);
498 tvItem.pszText = (wxChar *)text.c_str(); // conversion is ok
499 DoSetItem(&tvItem);
500}
501
502int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId& item,
503 wxTreeItemIcon which) const
504{
505 wxTreeViewItem tvItem(item, TVIF_PARAM);
506 if ( !DoGetItem(&tvItem) )
507 {
508 return -1;
509 }
510
511 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetImage(which);
512}
513
514void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId& item,
515 int image,
516 wxTreeItemIcon which) const
517{
518 wxTreeViewItem tvItem(item, TVIF_PARAM);
519 if ( !DoGetItem(&tvItem) )
520 {
521 return;
522 }
523
524 wxTreeItemIndirectData *data = ((wxTreeItemIndirectData *)tvItem.lParam);
525
526 data->SetImage(image, which);
527
528 // make sure that we have selected images as well
529 if ( which == wxTreeItemIcon_Normal &&
530 !data->HasImage(wxTreeItemIcon_Selected) )
531 {
532 data->SetImage(image, wxTreeItemIcon_Selected);
533 }
534
535 if ( which == wxTreeItemIcon_Expanded &&
536 !data->HasImage(wxTreeItemIcon_SelectedExpanded) )
537 {
538 data->SetImage(image, wxTreeItemIcon_SelectedExpanded);
539 }
540}
541
542void wxTreeCtrl::DoSetItemImages(const wxTreeItemId& item,
543 int image,
544 int imageSel)
545{
546 wxTreeViewItem tvItem(item, TVIF_IMAGE | TVIF_SELECTEDIMAGE);
547 tvItem.iSelectedImage = imageSel;
548 tvItem.iImage = image;
549 DoSetItem(&tvItem);
550}
551
552int wxTreeCtrl::GetItemImage(const wxTreeItemId& item,
553 wxTreeItemIcon which) const
554{
555 if ( HasIndirectData(item) )
556 {
557 return DoGetItemImageFromData(item, which);
558 }
559
560 UINT mask;
561 switch ( which )
562 {
563 default:
564 wxFAIL_MSG( wxT("unknown tree item image type") );
565
566 case wxTreeItemIcon_Normal:
567 mask = TVIF_IMAGE;
568 break;
569
570 case wxTreeItemIcon_Selected:
571 mask = TVIF_SELECTEDIMAGE;
572 break;
573
574 case wxTreeItemIcon_Expanded:
575 case wxTreeItemIcon_SelectedExpanded:
576 return -1;
577 }
578
579 wxTreeViewItem tvItem(item, mask);
580 DoGetItem(&tvItem);
581
582 return mask == TVIF_IMAGE ? tvItem.iImage : tvItem.iSelectedImage;
583}
584
585void wxTreeCtrl::SetItemImage(const wxTreeItemId& item, int image,
586 wxTreeItemIcon which)
587{
588 int imageNormal, imageSel;
589 switch ( which )
590 {
591 default:
592 wxFAIL_MSG( wxT("unknown tree item image type") );
593
594 case wxTreeItemIcon_Normal:
595 imageNormal = image;
596 imageSel = GetItemSelectedImage(item);
597 break;
598
599 case wxTreeItemIcon_Selected:
600 imageNormal = GetItemImage(item);
601 imageSel = image;
602 break;
603
604 case wxTreeItemIcon_Expanded:
605 case wxTreeItemIcon_SelectedExpanded:
606 if ( !HasIndirectData(item) )
607 {
608 // we need to get the old images first, because after we create
609 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
610 // get the images
611 imageNormal = GetItemImage(item);
612 imageSel = GetItemSelectedImage(item);
613
614 // if it doesn't have it yet, add it
615 wxTreeItemIndirectData *data = new
616 wxTreeItemIndirectData(this, item);
617
618 // copy the data to the new location
619 data->SetImage(imageNormal, wxTreeItemIcon_Normal);
620 data->SetImage(imageSel, wxTreeItemIcon_Selected);
621 }
622
623 DoSetItemImageFromData(item, image, which);
624
625 // reset the normal/selected images because we won't use them any
626 // more - now they're stored inside the indirect data
627 imageNormal =
628 imageSel = I_IMAGECALLBACK;
629 break;
630 }
631
632 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
633 // change both normal and selected image - otherwise the change simply
634 // doesn't take place!
635 DoSetItemImages(item, imageNormal, imageSel);
636}
637
638wxTreeItemData *wxTreeCtrl::GetItemData(const wxTreeItemId& item) const
639{
640 wxTreeViewItem tvItem(item, TVIF_PARAM);
641 if ( !DoGetItem(&tvItem) )
642 {
643 return NULL;
644 }
645
646 if ( HasIndirectData(item) )
647 {
648 return ((wxTreeItemIndirectData *)tvItem.lParam)->GetData();
649 }
650 else
651 {
652 return (wxTreeItemData *)tvItem.lParam;
653 }
654}
655
656void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data)
657{
658 wxTreeViewItem tvItem(item, TVIF_PARAM);
659
660 if ( HasIndirectData(item) )
661 {
662 if ( DoGetItem(&tvItem) )
663 {
664 ((wxTreeItemIndirectData *)tvItem.lParam)->SetData(data);
665 }
666 else
667 {
668 wxFAIL_MSG( wxT("failed to change tree items data") );
669 }
670 }
671 else
672 {
673 tvItem.lParam = (LPARAM)data;
674 DoSetItem(&tvItem);
675 }
676}
677
678void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId& item,
679 wxTreeItemIndirectData *data)
680{
681 // this should never happen because it's unnecessary and will probably lead
682 // to crash too because the code elsewhere supposes that the pointer the
683 // wxTreeItemIndirectData has is a real wxItemData and not
684 // wxTreeItemIndirectData as well
685 wxASSERT_MSG( !HasIndirectData(item), wxT("setting indirect data twice?") );
686
687 SetItemData(item, (wxTreeItemData *)data);
688
689 m_itemsWithIndirectData.Add(item);
690}
691
692bool wxTreeCtrl::HasIndirectData(const wxTreeItemId& item) const
693{
694 return m_itemsWithIndirectData.Index(item) != wxNOT_FOUND;
695}
696
697void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
698{
699 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
700 tvItem.cChildren = (int)has;
701 DoSetItem(&tvItem);
702}
703
704void wxTreeCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
705{
706 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
707 tvItem.state = bold ? TVIS_BOLD : 0;
708 DoSetItem(&tvItem);
709}
710
711void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId& item, bool highlight)
712{
713 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_DROPHILITED);
714 tvItem.state = highlight ? TVIS_DROPHILITED : 0;
715 DoSetItem(&tvItem);
716}
717
718// ----------------------------------------------------------------------------
719// Item status
720// ----------------------------------------------------------------------------
721
722bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const
723{
724 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
725 RECT rect;
726
727 // this ugliness comes directly from MSDN - it *is* the correct way to pass
728 // the HTREEITEM with TVM_GETITEMRECT
729 *(WXHTREEITEM *)&rect = (WXHTREEITEM)item;
730
731 // FALSE means get item rect for the whole item, not only text
732 return SendMessage(GetHwnd(), TVM_GETITEMRECT, FALSE, (LPARAM)&rect) != 0;
733
734}
735
736bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const
737{
738 wxTreeViewItem tvItem(item, TVIF_CHILDREN);
739 DoGetItem(&tvItem);
740
741 return tvItem.cChildren != 0;
742}
743
744bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const
745{
746 // probably not a good idea to put it here
747 //wxASSERT( ItemHasChildren(item) );
748
749 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDED);
750 DoGetItem(&tvItem);
751
752 return (tvItem.state & TVIS_EXPANDED) != 0;
753}
754
755bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const
756{
757 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_SELECTED);
758 DoGetItem(&tvItem);
759
760 return (tvItem.state & TVIS_SELECTED) != 0;
761}
762
763bool wxTreeCtrl::IsBold(const wxTreeItemId& item) const
764{
765 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_BOLD);
766 DoGetItem(&tvItem);
767
768 return (tvItem.state & TVIS_BOLD) != 0;
769}
770
771// ----------------------------------------------------------------------------
772// navigation
773// ----------------------------------------------------------------------------
774
775wxTreeItemId wxTreeCtrl::GetRootItem() const
776{
777 return wxTreeItemId((WXHTREEITEM) TreeView_GetRoot(GetHwnd()));
778}
779
780wxTreeItemId wxTreeCtrl::GetSelection() const
781{
782 wxCHECK_MSG( !(m_windowStyle & wxTR_MULTIPLE), (WXHTREEITEM)0,
783 wxT("this only works with single selection controls") );
784
785 return wxTreeItemId((WXHTREEITEM) TreeView_GetSelection(GetHwnd()));
786}
787
788wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const
789{
790 return wxTreeItemId((WXHTREEITEM) TreeView_GetParent(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
791}
792
793wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item,
794 long& _cookie) const
795{
796 // remember the last child returned in 'cookie'
797 _cookie = (long)TreeView_GetChild(GetHwnd(), (HTREEITEM) (WXHTREEITEM)item);
798
799 return wxTreeItemId((WXHTREEITEM)_cookie);
800}
801
802wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& WXUNUSED(item),
803 long& _cookie) const
804{
805 wxTreeItemId l = wxTreeItemId((WXHTREEITEM)TreeView_GetNextSibling(GetHwnd(),
806 (HTREEITEM)(WXHTREEITEM)_cookie));
807 _cookie = (long)l;
808
809 return l;
810}
811
812wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const
813{
814 // can this be done more efficiently?
815 long cookie;
816
817 wxTreeItemId childLast,
818 child = GetFirstChild(item, cookie);
819 while ( child.IsOk() )
820 {
821 childLast = child;
822 child = GetNextChild(item, cookie);
823 }
824
825 return childLast;
826}
827
828wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const
829{
830 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
831}
832
833wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const
834{
835 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
836}
837
838wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const
839{
840 return wxTreeItemId((WXHTREEITEM) TreeView_GetFirstVisible(GetHwnd()));
841}
842
843wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const
844{
845 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetNextVisible() "
846 "for must be visible itself!"));
847
848 return wxTreeItemId((WXHTREEITEM) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
849}
850
851wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const
852{
853 wxASSERT_MSG( IsVisible(item), wxT("The item you call GetPrevVisible() "
854 "for must be visible itself!"));
855
856 return wxTreeItemId((WXHTREEITEM) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item));
857}
858
859// ----------------------------------------------------------------------------
860// multiple selections emulation
861// ----------------------------------------------------------------------------
862
863bool wxTreeCtrl::IsItemChecked(const wxTreeItemId& item) const
864{
865 // receive the desired information.
866 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
867 DoGetItem(&tvItem);
868
869 // state image indices are 1 based
870 return ((tvItem.state >> 12) - 1) == 1;
871}
872
873void wxTreeCtrl::SetItemCheck(const wxTreeItemId& item, bool check)
874{
875 // receive the desired information.
876 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_STATEIMAGEMASK);
877
878 // state images are one-based
879 tvItem.state = (check ? 2 : 1) << 12;
880
881 DoSetItem(&tvItem);
882}
883
884size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds& selections) const
885{
886 TraverseSelections selector(this, selections);
887
888 return selections.GetCount();
889}
890
891// ----------------------------------------------------------------------------
892// Usual operations
893// ----------------------------------------------------------------------------
894
895wxTreeItemId wxTreeCtrl::DoInsertItem(const wxTreeItemId& parent,
896 wxTreeItemId hInsertAfter,
897 const wxString& text,
898 int image, int selectedImage,
899 wxTreeItemData *data)
900{
901 TV_INSERTSTRUCT tvIns;
902 tvIns.hParent = (HTREEITEM) (WXHTREEITEM)parent;
903 tvIns.hInsertAfter = (HTREEITEM) (WXHTREEITEM) hInsertAfter;
904
905 // this is how we insert the item as the first child: supply a NULL
906 // hInsertAfter
907 if ( !tvIns.hInsertAfter )
908 {
909 tvIns.hInsertAfter = TVI_FIRST;
910 }
911
912 UINT mask = 0;
913 if ( !text.IsEmpty() )
914 {
915 mask |= TVIF_TEXT;
916 tvIns.item.pszText = (wxChar *)text.c_str(); // cast is ok
917 }
918
919 if ( image != -1 )
920 {
921 mask |= TVIF_IMAGE;
922 tvIns.item.iImage = image;
923
924 if ( selectedImage == -1 )
925 {
926 // take the same image for selected icon if not specified
927 selectedImage = image;
928 }
929 }
930
931 if ( selectedImage != -1 )
932 {
933 mask |= TVIF_SELECTEDIMAGE;
934 tvIns.item.iSelectedImage = selectedImage;
935 }
936
937 if ( data != NULL )
938 {
939 mask |= TVIF_PARAM;
940 tvIns.item.lParam = (LPARAM)data;
941 }
942
943 tvIns.item.mask = mask;
944
945 HTREEITEM id = (HTREEITEM) TreeView_InsertItem(GetHwnd(), &tvIns);
946 if ( id == 0 )
947 {
948 wxLogLastError("TreeView_InsertItem");
949 }
950
951 if ( data != NULL )
952 {
953 // associate the application tree item with Win32 tree item handle
954 data->SetId((WXHTREEITEM)id);
955 }
956
957 return wxTreeItemId((WXHTREEITEM)id);
958}
959
960// for compatibility only
961wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
962 const wxString& text,
963 int image, int selImage,
964 long insertAfter)
965{
966 return DoInsertItem(parent, (WXHTREEITEM)insertAfter, text,
967 image, selImage, NULL);
968}
969
970wxTreeItemId wxTreeCtrl::AddRoot(const wxString& text,
971 int image, int selectedImage,
972 wxTreeItemData *data)
973{
974 return DoInsertItem(wxTreeItemId((WXHTREEITEM) 0), (WXHTREEITEM) 0,
975 text, image, selectedImage, data);
976}
977
978wxTreeItemId wxTreeCtrl::PrependItem(const wxTreeItemId& parent,
979 const wxString& text,
980 int image, int selectedImage,
981 wxTreeItemData *data)
982{
983 return DoInsertItem(parent, (WXHTREEITEM) TVI_FIRST,
984 text, image, selectedImage, data);
985}
986
987wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent,
988 const wxTreeItemId& idPrevious,
989 const wxString& text,
990 int image, int selectedImage,
991 wxTreeItemData *data)
992{
993 return DoInsertItem(parent, idPrevious, text, image, selectedImage, data);
994}
995
996wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent,
997 const wxString& text,
998 int image, int selectedImage,
999 wxTreeItemData *data)
1000{
1001 return DoInsertItem(parent, (WXHTREEITEM) TVI_LAST,
1002 text, image, selectedImage, data);
1003}
1004
1005void wxTreeCtrl::Delete(const wxTreeItemId& item)
1006{
1007 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item) )
1008 {
1009 wxLogLastError("TreeView_DeleteItem");
1010 }
1011}
1012
1013// delete all children (but don't delete the item itself)
1014void wxTreeCtrl::DeleteChildren(const wxTreeItemId& item)
1015{
1016 long cookie;
1017
1018 wxArrayLong children;
1019 wxTreeItemId child = GetFirstChild(item, cookie);
1020 while ( child.IsOk() )
1021 {
1022 children.Add((long)(WXHTREEITEM)child);
1023
1024 child = GetNextChild(item, cookie);
1025 }
1026
1027 size_t nCount = children.Count();
1028 for ( size_t n = 0; n < nCount; n++ )
1029 {
1030 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM)children[n]) )
1031 {
1032 wxLogLastError("TreeView_DeleteItem");
1033 }
1034 }
1035}
1036
1037void wxTreeCtrl::DeleteAllItems()
1038{
1039 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1040 {
1041 wxLogLastError("TreeView_DeleteAllItems");
1042 }
1043}
1044
1045void wxTreeCtrl::DoExpand(const wxTreeItemId& item, int flag)
1046{
1047 wxASSERT_MSG( flag == TVE_COLLAPSE ||
1048 flag == (TVE_COLLAPSE | TVE_COLLAPSERESET) ||
1049 flag == TVE_EXPAND ||
1050 flag == TVE_TOGGLE,
1051 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1052
1053 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1054 // emulate them. This behaviour has changed slightly with comctl32.dll
1055 // v 4.70 - now it does send them but only the first time. To maintain
1056 // compatible behaviour and also in order to not have surprises with the
1057 // future versions, don't rely on this and still do everything ourselves.
1058 // To avoid that the messages be sent twice when the item is expanded for
1059 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1060
1061 wxTreeViewItem tvItem(item, TVIF_STATE, TVIS_EXPANDEDONCE);
1062 tvItem.state = 0;
1063 DoSetItem(&tvItem);
1064
1065 if ( TreeView_Expand(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item, flag) != 0 )
1066 {
1067 wxTreeEvent event(wxEVT_NULL, m_windowId);
1068 event.m_item = item;
1069
1070 bool isExpanded = IsExpanded(item);
1071
1072 event.SetEventObject(this);
1073
1074 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1075 event.SetEventType(g_events[isExpanded][TRUE]);
1076 GetEventHandler()->ProcessEvent(event);
1077
1078 event.SetEventType(g_events[isExpanded][FALSE]);
1079 GetEventHandler()->ProcessEvent(event);
1080 }
1081 //else: change didn't took place, so do nothing at all
1082}
1083
1084void wxTreeCtrl::Expand(const wxTreeItemId& item)
1085{
1086 DoExpand(item, TVE_EXPAND);
1087}
1088
1089void wxTreeCtrl::Collapse(const wxTreeItemId& item)
1090{
1091 DoExpand(item, TVE_COLLAPSE);
1092}
1093
1094void wxTreeCtrl::CollapseAndReset(const wxTreeItemId& item)
1095{
1096 DoExpand(item, TVE_COLLAPSE | TVE_COLLAPSERESET);
1097}
1098
1099void wxTreeCtrl::Toggle(const wxTreeItemId& item)
1100{
1101 DoExpand(item, TVE_TOGGLE);
1102}
1103
1104void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action)
1105{
1106 DoExpand(item, action);
1107}
1108
1109void wxTreeCtrl::Unselect()
1110{
1111 wxASSERT_MSG( !(m_windowStyle & wxTR_MULTIPLE), wxT("doesn't make sense") );
1112
1113 // just remove the selection
1114 SelectItem(wxTreeItemId((WXHTREEITEM) 0));
1115}
1116
1117void wxTreeCtrl::UnselectAll()
1118{
1119 if ( m_windowStyle & wxTR_MULTIPLE )
1120 {
1121 wxArrayTreeItemIds selections;
1122 size_t count = GetSelections(selections);
1123 for ( size_t n = 0; n < count; n++ )
1124 {
1125 SetItemCheck(selections[n], FALSE);
1126 }
1127 }
1128 else
1129 {
1130 // just remove the selection
1131 Unselect();
1132 }
1133}
1134
1135void wxTreeCtrl::SelectItem(const wxTreeItemId& item)
1136{
1137 if ( m_windowStyle & wxTR_MULTIPLE )
1138 {
1139 // selecting the item means checking it
1140 SetItemCheck(item);
1141 }
1142 else
1143 {
1144 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1145 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1146 // send them ourselves
1147
1148 wxTreeEvent event(wxEVT_NULL, m_windowId);
1149 event.m_item = item;
1150 event.SetEventObject(this);
1151
1152 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING);
1153 if ( !GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
1154 {
1155 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
1156 {
1157 wxLogLastError("TreeView_SelectItem");
1158 }
1159 else
1160 {
1161 event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
1162 (void)GetEventHandler()->ProcessEvent(event);
1163 }
1164 }
1165 //else: program vetoed the change
1166 }
1167}
1168
1169void wxTreeCtrl::EnsureVisible(const wxTreeItemId& item)
1170{
1171 // no error return
1172 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
1173}
1174
1175void wxTreeCtrl::ScrollTo(const wxTreeItemId& item)
1176{
1177 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item) )
1178 {
1179 wxLogLastError("TreeView_SelectSetFirstVisible");
1180 }
1181}
1182
1183wxTextCtrl* wxTreeCtrl::GetEditControl() const
1184{
1185 return m_textCtrl;
1186}
1187
1188void wxTreeCtrl::DeleteTextCtrl()
1189{
1190 if ( m_textCtrl )
1191 {
1192 m_textCtrl->UnsubclassWin();
1193 m_textCtrl->SetHWND(0);
1194 delete m_textCtrl;
1195 m_textCtrl = NULL;
1196 }
1197}
1198
1199wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item,
1200 wxClassInfo* textControlClass)
1201{
1202 wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) );
1203
1204 HWND hWnd = (HWND) TreeView_EditLabel(GetHwnd(), (HTREEITEM) (WXHTREEITEM) item);
1205
1206 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1207 // returned FALSE
1208 if ( !hWnd )
1209 {
1210 return NULL;
1211 }
1212
1213 DeleteTextCtrl();
1214
1215 m_textCtrl = (wxTextCtrl *)textControlClass->CreateObject();
1216 m_textCtrl->SetHWND((WXHWND)hWnd);
1217 m_textCtrl->SubclassWin((WXHWND)hWnd);
1218
1219 return m_textCtrl;
1220}
1221
1222// End label editing, optionally cancelling the edit
1223void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges)
1224{
1225 TreeView_EndEditLabelNow(GetHwnd(), discardChanges);
1226
1227 DeleteTextCtrl();
1228}
1229
1230wxTreeItemId wxTreeCtrl::HitTest(const wxPoint& point, int& flags)
1231{
1232 TV_HITTESTINFO hitTestInfo;
1233 hitTestInfo.pt.x = (int)point.x;
1234 hitTestInfo.pt.y = (int)point.y;
1235
1236 TreeView_HitTest(GetHwnd(), &hitTestInfo);
1237
1238 flags = 0;
1239
1240 // avoid repetition
1241 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1242 flags |= wxTREE_HITTEST_##flag
1243
1244 TRANSLATE_FLAG(ABOVE);
1245 TRANSLATE_FLAG(BELOW);
1246 TRANSLATE_FLAG(NOWHERE);
1247 TRANSLATE_FLAG(ONITEMBUTTON);
1248 TRANSLATE_FLAG(ONITEMICON);
1249 TRANSLATE_FLAG(ONITEMINDENT);
1250 TRANSLATE_FLAG(ONITEMLABEL);
1251 TRANSLATE_FLAG(ONITEMRIGHT);
1252 TRANSLATE_FLAG(ONITEMSTATEICON);
1253 TRANSLATE_FLAG(TOLEFT);
1254 TRANSLATE_FLAG(TORIGHT);
1255
1256 #undef TRANSLATE_FLAG
1257
1258 return wxTreeItemId((WXHTREEITEM) hitTestInfo.hItem);
1259}
1260
1261bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId& item,
1262 wxRect& rect,
1263 bool textOnly) const
1264{
1265 RECT rc;
1266 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item,
1267 &rc, textOnly) )
1268 {
1269 rect = wxRect(wxPoint(rc.left, rc.top), wxPoint(rc.right, rc.bottom));
1270
1271 return TRUE;
1272 }
1273 else
1274 {
1275 // couldn't retrieve rect: for example, item isn't visible
1276 return FALSE;
1277 }
1278}
1279
1280// ----------------------------------------------------------------------------
1281// sorting stuff
1282// ----------------------------------------------------------------------------
1283
1284static int CALLBACK TreeView_CompareCallback(wxTreeItemData *pItem1,
1285 wxTreeItemData *pItem2,
1286 wxTreeCtrl *tree)
1287{
1288 wxCHECK_MSG( pItem1 && pItem2, 0,
1289 wxT("sorting tree without data doesn't make sense") );
1290
1291 return tree->OnCompareItems(pItem1->GetId(), pItem2->GetId());
1292}
1293
1294int wxTreeCtrl::OnCompareItems(const wxTreeItemId& item1,
1295 const wxTreeItemId& item2)
1296{
1297 return wxStrcmp(GetItemText(item1), GetItemText(item2));
1298}
1299
1300void wxTreeCtrl::SortChildren(const wxTreeItemId& item)
1301{
1302 // rely on the fact that TreeView_SortChildren does the same thing as our
1303 // default behaviour, i.e. sorts items alphabetically and so call it
1304 // directly if we're not in derived class (much more efficient!)
1305 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl) )
1306 {
1307 TreeView_SortChildren(GetHwnd(), (HTREEITEM)(WXHTREEITEM)item, 0);
1308 }
1309 else
1310 {
1311 TV_SORTCB tvSort;
1312 tvSort.hParent = (HTREEITEM)(WXHTREEITEM)item;
1313 tvSort.lpfnCompare = (PFNTVCOMPARE)TreeView_CompareCallback;
1314 tvSort.lParam = (LPARAM)this;
1315 TreeView_SortChildrenCB(GetHwnd(), &tvSort, 0 /* reserved */);
1316 }
1317}
1318
1319// ----------------------------------------------------------------------------
1320// implementation
1321// ----------------------------------------------------------------------------
1322
1323bool wxTreeCtrl::MSWCommand(WXUINT cmd, WXWORD id)
1324{
1325 if ( cmd == EN_UPDATE )
1326 {
1327 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, id);
1328 event.SetEventObject( this );
1329 ProcessCommand(event);
1330 }
1331 else if ( cmd == EN_KILLFOCUS )
1332 {
1333 wxCommandEvent event(wxEVT_KILL_FOCUS, id);
1334 event.SetEventObject( this );
1335 ProcessCommand(event);
1336 }
1337 else
1338 {
1339 // nothing done
1340 return FALSE;
1341 }
1342
1343 // command processed
1344 return TRUE;
1345}
1346
1347// process WM_NOTIFY Windows message
1348bool wxTreeCtrl::MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result)
1349{
1350 wxTreeEvent event(wxEVT_NULL, m_windowId);
1351 wxEventType eventType = wxEVT_NULL;
1352 NMHDR *hdr = (NMHDR *)lParam;
1353
1354 switch ( hdr->code )
1355 {
1356 case NM_RCLICK:
1357 {
1358 if ( wxControl::MSWOnNotify(idCtrl, lParam, result) )
1359 return TRUE;
1360
1361 TV_HITTESTINFO tvhti;
1362 ::GetCursorPos(&(tvhti.pt));
1363 ::ScreenToClient(GetHwnd(),&(tvhti.pt));
1364 if ( TreeView_HitTest(GetHwnd(),&tvhti) )
1365 {
1366 if( tvhti.flags & TVHT_ONITEM )
1367 {
1368 event.m_item = (WXHTREEITEM) tvhti.hItem;
1369 eventType=wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK;
1370 }
1371 }
1372 break;
1373 }
1374
1375 case TVN_BEGINDRAG:
1376 eventType = wxEVT_COMMAND_TREE_BEGIN_DRAG;
1377 // fall through
1378
1379 case TVN_BEGINRDRAG:
1380 {
1381 if ( eventType == wxEVT_NULL )
1382 eventType = wxEVT_COMMAND_TREE_BEGIN_RDRAG;
1383 //else: left drag, already set above
1384
1385 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1386
1387 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1388 event.m_pointDrag = wxPoint(tv->ptDrag.x, tv->ptDrag.y);
1389 break;
1390 }
1391
1392 case TVN_BEGINLABELEDIT:
1393 {
1394 eventType = wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT;
1395 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1396
1397 event.m_item = (WXHTREEITEM) info->item.hItem;
1398 event.m_label = info->item.pszText;
1399 break;
1400 }
1401
1402 case TVN_DELETEITEM:
1403 {
1404 eventType = wxEVT_COMMAND_TREE_DELETE_ITEM;
1405 NM_TREEVIEW *tv = (NM_TREEVIEW *)lParam;
1406
1407 event.m_item = (WXHTREEITEM) tv->itemOld.hItem;
1408 break;
1409 }
1410
1411 case TVN_ENDLABELEDIT:
1412 {
1413 eventType = wxEVT_COMMAND_TREE_END_LABEL_EDIT;
1414 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1415
1416 event.m_item = (WXHTREEITEM)info->item.hItem;
1417 event.m_label = info->item.pszText;
1418 if (info->item.pszText == NULL)
1419 return FALSE;
1420 break;
1421 }
1422
1423 case TVN_GETDISPINFO:
1424 eventType = wxEVT_COMMAND_TREE_GET_INFO;
1425 // fall through
1426
1427 case TVN_SETDISPINFO:
1428 {
1429 if ( eventType == wxEVT_NULL )
1430 eventType = wxEVT_COMMAND_TREE_SET_INFO;
1431 //else: get, already set above
1432
1433 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1434
1435 event.m_item = (WXHTREEITEM) info->item.hItem;
1436 break;
1437 }
1438
1439 case TVN_ITEMEXPANDING:
1440 event.m_code = FALSE;
1441 // fall through
1442
1443 case TVN_ITEMEXPANDED:
1444 {
1445 NM_TREEVIEW* tv = (NM_TREEVIEW*)lParam;
1446
1447 bool expand = FALSE;
1448 switch ( tv->action )
1449 {
1450 case TVE_EXPAND:
1451 expand = TRUE;
1452 break;
1453
1454 case TVE_COLLAPSE:
1455 expand = FALSE;
1456 break;
1457
1458 default:
1459 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1460 "message"), tv->action);
1461 }
1462
1463 bool ing = ((int)hdr->code == TVN_ITEMEXPANDING);
1464 eventType = g_events[expand][ing];
1465
1466 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1467 break;
1468 }
1469
1470 case TVN_KEYDOWN:
1471 {
1472 eventType = wxEVT_COMMAND_TREE_KEY_DOWN;
1473 TV_KEYDOWN *info = (TV_KEYDOWN *)lParam;
1474
1475 event.m_code = wxCharCodeMSWToWX(info->wVKey);
1476
1477 // a separate event for this case
1478 if ( info->wVKey == VK_SPACE || info->wVKey == VK_RETURN )
1479 {
1480 wxTreeEvent event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED,
1481 m_windowId);
1482 event2.SetEventObject(this);
1483
1484 GetEventHandler()->ProcessEvent(event2);
1485 }
1486 break;
1487 }
1488
1489 case TVN_SELCHANGED:
1490 eventType = wxEVT_COMMAND_TREE_SEL_CHANGED;
1491 // fall through
1492
1493 case TVN_SELCHANGING:
1494 {
1495 if ( eventType == wxEVT_NULL )
1496 eventType = wxEVT_COMMAND_TREE_SEL_CHANGING;
1497 //else: already set above
1498
1499 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1500
1501 event.m_item = (WXHTREEITEM) tv->itemNew.hItem;
1502 event.m_itemOld = (WXHTREEITEM) tv->itemOld.hItem;
1503 break;
1504 }
1505
1506 default:
1507 return wxControl::MSWOnNotify(idCtrl, lParam, result);
1508 }
1509
1510 event.SetEventObject(this);
1511 event.SetEventType(eventType);
1512
1513 bool processed = GetEventHandler()->ProcessEvent(event);
1514
1515 // post processing
1516 switch ( hdr->code )
1517 {
1518 case TVN_DELETEITEM:
1519 {
1520 // NB: we might process this message using wxWindows event
1521 // tables, but due to overhead of wxWin event system we
1522 // prefer to do it here ourself (otherwise deleting a tree
1523 // with many items is just too slow)
1524 NM_TREEVIEW* tv = (NM_TREEVIEW *)lParam;
1525
1526 wxTreeItemId item = event.m_item;
1527 if ( HasIndirectData(item) )
1528 {
1529 wxTreeItemIndirectData *data = (wxTreeItemIndirectData *)
1530 tv->itemOld.lParam;
1531 delete data; // can't be NULL here
1532
1533 m_itemsWithIndirectData.Remove(item);
1534 }
1535 else
1536 {
1537 wxTreeItemData *data = (wxTreeItemData *)tv->itemOld.lParam;
1538 delete data; // may be NULL, ok
1539 }
1540
1541 processed = TRUE; // Make sure we don't get called twice
1542 }
1543 break;
1544
1545 case TVN_BEGINLABELEDIT:
1546 // return TRUE to cancel label editing
1547 *result = !event.IsAllowed();
1548 break;
1549
1550 case TVN_ENDLABELEDIT:
1551 // return TRUE to set the label to the new string
1552 *result = event.IsAllowed();
1553
1554 // ensure that we don't have the text ctrl which is going to be
1555 // deleted any more
1556 DeleteTextCtrl();
1557 break;
1558
1559 case TVN_SELCHANGING:
1560 case TVN_ITEMEXPANDING:
1561 // return TRUE to prevent the action from happening
1562 *result = !event.IsAllowed();
1563 break;
1564
1565 case TVN_GETDISPINFO:
1566 // NB: so far the user can't set the image himself anyhow, so do it
1567 // anyway - but this may change later
1568 if ( /* !processed && */ 1 )
1569 {
1570 wxTreeItemId item = event.m_item;
1571 TV_DISPINFO *info = (TV_DISPINFO *)lParam;
1572 if ( info->item.mask & TVIF_IMAGE )
1573 {
1574 info->item.iImage =
1575 DoGetItemImageFromData
1576 (
1577 item,
1578 IsExpanded(item) ? wxTreeItemIcon_Expanded
1579 : wxTreeItemIcon_Normal
1580 );
1581 }
1582 if ( info->item.mask & TVIF_SELECTEDIMAGE )
1583 {
1584 info->item.iSelectedImage =
1585 DoGetItemImageFromData
1586 (
1587 item,
1588 IsExpanded(item) ? wxTreeItemIcon_SelectedExpanded
1589 : wxTreeItemIcon_Selected
1590 );
1591 }
1592 }
1593 break;
1594
1595 //default:
1596 // for the other messages the return value is ignored and there is
1597 // nothing special to do
1598 }
1599
1600 return processed;
1601}
1602
1603// ----------------------------------------------------------------------------
1604// Tree event
1605// ----------------------------------------------------------------------------
1606
1607IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent, wxNotifyEvent)
1608
1609wxTreeEvent::wxTreeEvent(wxEventType commandType, int id)
1610 : wxNotifyEvent(commandType, id)
1611{
1612 m_code = 0;
1613 m_itemOld = 0;
1614}
1615
1616#endif // __WIN95__
1617