1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "treectrl.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/window.h"
31 #include "wx/msw/private.h"
33 // Mingw32 is a bit mental even though this is done in winundef
42 #if defined(__WIN95__)
45 #include "wx/dynarray.h"
46 #include "wx/imaglist.h"
47 #include "wx/treectrl.h"
48 #include "wx/settings.h"
51 #ifndef wxUSE_NORLANDER_HEADERS
52 #include "wx/msw/gnuwin32/extra.h"
56 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
60 // Bug in headers, sometimes
62 #define TVIS_FOCUSED 0x0001
66 #define TV_FIRST 0x1100
69 // old headers might miss these messages (comctl32.dll 4.71+ only)
70 #ifndef TVM_SETBKCOLOR
71 #define TVM_SETBKCOLOR (TV_FIRST + 29)
72 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // a convenient wrapper around TV_ITEM struct which adds a ctor
81 #pragma warning( disable : 4097 )
84 struct wxTreeViewItem
: public TV_ITEM
86 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
87 UINT mask_
, // fields which are valid
88 UINT stateMask_
= 0) // for TVIF_STATE only
90 // hItem member is always valid
91 mask
= mask_
| TVIF_HANDLE
;
92 stateMask
= stateMask_
;
93 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
98 #pragma warning( default : 4097 )
101 // a class which encapsulates the tree traversal logic: it vists all (unless
102 // OnVisit() returns FALSE) items under the given one
103 class wxTreeTraversal
106 wxTreeTraversal(const wxTreeCtrl
*tree
)
111 // do traverse the tree: visit all items (recursively by default) under the
112 // given one; return TRUE if all items were traversed or FALSE if the
113 // traversal was aborted because OnVisit returned FALSE
114 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
116 // override this function to do whatever is needed for each item, return
117 // FALSE to stop traversing
118 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
121 const wxTreeCtrl
*GetTree() const { return m_tree
; }
124 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
126 const wxTreeCtrl
*m_tree
;
129 // internal class for getting the selected items
130 class TraverseSelections
: public wxTreeTraversal
133 TraverseSelections(const wxTreeCtrl
*tree
,
134 wxArrayTreeItemIds
& selections
)
135 : wxTreeTraversal(tree
), m_selections(selections
)
137 m_selections
.Empty();
139 DoTraverse(tree
->GetRootItem());
142 virtual bool OnVisit(const wxTreeItemId
& item
)
144 if ( GetTree()->IsItemChecked(item
) )
146 m_selections
.Add(item
);
152 size_t GetCount() const { return m_selections
.GetCount(); }
155 wxArrayTreeItemIds
& m_selections
;
158 // internal class for counting tree items
159 class TraverseCounter
: public wxTreeTraversal
162 TraverseCounter(const wxTreeCtrl
*tree
,
163 const wxTreeItemId
& root
,
165 : wxTreeTraversal(tree
)
169 DoTraverse(root
, recursively
);
172 virtual bool OnVisit(const wxTreeItemId
& item
)
179 size_t GetCount() const { return m_count
; }
185 // ----------------------------------------------------------------------------
186 // This class is needed for support of different images: the Win32 common
187 // control natively supports only 2 images (the normal one and another for the
188 // selected state). We wish to provide support for 2 more of them for folder
189 // items (i.e. those which have children): for expanded state and for expanded
190 // selected state. For this we use this structure to store the additional items
193 // There is only one problem with this: when we retrieve the item's data, we
194 // don't know whether we get a pointer to wxTreeItemData or
195 // wxTreeItemIndirectData. So we have to maintain a list of all items which
196 // have indirect data inside the listctrl itself.
197 // ----------------------------------------------------------------------------
199 class wxTreeItemIndirectData
202 // ctor associates this data with the item and the real item data becomes
203 // available through our GetData() method
204 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
206 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
212 m_data
= tree
->GetItemData(item
);
214 // and set ourselves as the new one
215 tree
->SetIndirectItemData(item
, this);
218 // dtor deletes the associated data as well
219 ~wxTreeItemIndirectData() { delete m_data
; }
222 // get the real data associated with the item
223 wxTreeItemData
*GetData() const { return m_data
; }
225 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
227 // do we have such image?
228 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
230 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
232 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
235 // all the images associated with the item
236 int m_images
[wxTreeItemIcon_Max
];
238 wxTreeItemData
*m_data
;
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // handy table for sending events
252 static const wxEventType g_events
[2][2] =
254 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
255 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
258 // ============================================================================
260 // ============================================================================
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
268 if ( !OnVisit(root
) )
271 return Traverse(root
, recursively
);
274 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
277 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
278 while ( child
.IsOk() )
280 // depth first traversal
281 if ( recursively
&& !Traverse(child
, TRUE
) )
284 if ( !OnVisit(child
) )
287 child
= m_tree
->GetNextChild(root
, cookie
);
293 // ----------------------------------------------------------------------------
294 // construction and destruction
295 // ----------------------------------------------------------------------------
297 void wxTreeCtrl::Init()
299 m_imageListNormal
= NULL
;
300 m_imageListState
= NULL
;
302 m_hasAnyAttr
= FALSE
;
305 bool wxTreeCtrl::Create(wxWindow
*parent
,
310 const wxValidator
& validator
,
311 const wxString
& name
)
315 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
318 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
319 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
321 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
322 wstyle
|= TVS_HASBUTTONS
;
324 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
325 wstyle
|= TVS_EDITLABELS
;
327 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
328 wstyle
|= TVS_LINESATROOT
;
330 #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined( __WATCOMC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
331 // we emulate the multiple selection tree controls by using checkboxes: set
332 // up the image list we need for this if we do have multiple selections
333 #if !defined(__VISUALC__) || (__VISUALC__ > 1010)
334 if ( m_windowStyle
& wxTR_MULTIPLE
)
335 wstyle
|= TVS_CHECKBOXES
;
339 // Create the tree control.
340 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
343 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
344 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
346 // VZ: this is some experimental code which may be used to get the
347 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
348 // AFAIK, the standard DLL does about the same thing anyhow.
350 if ( m_windowStyle
& wxTR_MULTIPLE
)
354 // create the DC compatible with the current screen
355 HDC hdcMem
= CreateCompatibleDC(NULL
);
357 // create a mono bitmap of the standard size
358 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
359 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
360 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
361 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
362 1, // # of color planes
363 1, // # bits needed for one pixel
364 0); // array containing colour data
365 SelectObject(hdcMem
, hbmpCheck
);
367 // then draw a check mark into it
368 RECT rect
= { 0, 0, x
, y
};
369 if ( !::DrawFrameControl(hdcMem
, &rect
,
371 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
373 wxLogLastError(wxT("DrawFrameControl(check)"));
376 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
377 imagelistCheckboxes
.Add(bmp
);
379 if ( !::DrawFrameControl(hdcMem
, &rect
,
383 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
386 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
387 imagelistCheckboxes
.Add(bmp
);
393 SetStateImageList(&imagelistCheckboxes
);
397 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
402 wxTreeCtrl::~wxTreeCtrl()
404 // delete any attributes
407 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
409 delete (wxTreeItemAttr
*)node
->Data();
412 // prevent TVN_DELETEITEM handler from deleting the attributes again!
413 m_hasAnyAttr
= FALSE
;
418 // delete user data to prevent memory leaks
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 // simple wrappers which add error checking in debug mode
428 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
430 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
432 wxLogLastError("TreeView_GetItem");
440 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
442 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
444 wxLogLastError("TreeView_SetItem");
448 size_t wxTreeCtrl::GetCount() const
450 return (size_t)TreeView_GetCount(GetHwnd());
453 unsigned int wxTreeCtrl::GetIndent() const
455 return TreeView_GetIndent(GetHwnd());
458 void wxTreeCtrl::SetIndent(unsigned int indent
)
460 TreeView_SetIndent(GetHwnd(), indent
);
463 wxImageList
*wxTreeCtrl::GetImageList() const
465 return m_imageListNormal
;
468 wxImageList
*wxTreeCtrl::GetStateImageList() const
470 return m_imageListNormal
;
473 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
476 TreeView_SetImageList(GetHwnd(),
477 imageList
? imageList
->GetHIMAGELIST() : 0,
481 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
483 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
486 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
488 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
491 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
492 bool recursively
) const
494 TraverseCounter
counter(this, item
, recursively
);
496 return counter
.GetCount() - 1;
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
503 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
505 if ( !wxWindowBase::SetBackgroundColour(colour
) )
508 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
513 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
515 if ( !wxWindowBase::SetForegroundColour(colour
) )
518 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
527 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
529 wxChar buf
[512]; // the size is arbitrary...
531 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
532 tvItem
.pszText
= buf
;
533 tvItem
.cchTextMax
= WXSIZEOF(buf
);
534 if ( !DoGetItem(&tvItem
) )
536 // don't return some garbage which was on stack, but an empty string
540 return wxString(buf
);
543 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
545 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
546 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
550 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
551 wxTreeItemIcon which
) const
553 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
554 if ( !DoGetItem(&tvItem
) )
559 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
562 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
564 wxTreeItemIcon which
) const
566 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
567 if ( !DoGetItem(&tvItem
) )
572 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
574 data
->SetImage(image
, which
);
576 // make sure that we have selected images as well
577 if ( which
== wxTreeItemIcon_Normal
&&
578 !data
->HasImage(wxTreeItemIcon_Selected
) )
580 data
->SetImage(image
, wxTreeItemIcon_Selected
);
583 if ( which
== wxTreeItemIcon_Expanded
&&
584 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
586 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
590 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
594 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
595 tvItem
.iSelectedImage
= imageSel
;
596 tvItem
.iImage
= image
;
600 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
601 wxTreeItemIcon which
) const
603 if ( HasIndirectData(item
) )
605 return DoGetItemImageFromData(item
, which
);
612 wxFAIL_MSG( wxT("unknown tree item image type") );
614 case wxTreeItemIcon_Normal
:
618 case wxTreeItemIcon_Selected
:
619 mask
= TVIF_SELECTEDIMAGE
;
622 case wxTreeItemIcon_Expanded
:
623 case wxTreeItemIcon_SelectedExpanded
:
627 wxTreeViewItem
tvItem(item
, mask
);
630 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
633 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
634 wxTreeItemIcon which
)
636 int imageNormal
, imageSel
;
640 wxFAIL_MSG( wxT("unknown tree item image type") );
642 case wxTreeItemIcon_Normal
:
644 imageSel
= GetItemSelectedImage(item
);
647 case wxTreeItemIcon_Selected
:
648 imageNormal
= GetItemImage(item
);
652 case wxTreeItemIcon_Expanded
:
653 case wxTreeItemIcon_SelectedExpanded
:
654 if ( !HasIndirectData(item
) )
656 // we need to get the old images first, because after we create
657 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
659 imageNormal
= GetItemImage(item
);
660 imageSel
= GetItemSelectedImage(item
);
662 // if it doesn't have it yet, add it
663 wxTreeItemIndirectData
*data
= new
664 wxTreeItemIndirectData(this, item
);
666 // copy the data to the new location
667 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
668 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
671 DoSetItemImageFromData(item
, image
, which
);
673 // reset the normal/selected images because we won't use them any
674 // more - now they're stored inside the indirect data
676 imageSel
= I_IMAGECALLBACK
;
680 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
681 // change both normal and selected image - otherwise the change simply
682 // doesn't take place!
683 DoSetItemImages(item
, imageNormal
, imageSel
);
686 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
688 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
689 if ( !DoGetItem(&tvItem
) )
694 if ( HasIndirectData(item
) )
696 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
700 return (wxTreeItemData
*)tvItem
.lParam
;
704 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
706 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
708 if ( HasIndirectData(item
) )
710 if ( DoGetItem(&tvItem
) )
712 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
716 wxFAIL_MSG( wxT("failed to change tree items data") );
721 tvItem
.lParam
= (LPARAM
)data
;
726 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
727 wxTreeItemIndirectData
*data
)
729 // this should never happen because it's unnecessary and will probably lead
730 // to crash too because the code elsewhere supposes that the pointer the
731 // wxTreeItemIndirectData has is a real wxItemData and not
732 // wxTreeItemIndirectData as well
733 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
735 SetItemData(item
, (wxTreeItemData
*)data
);
737 m_itemsWithIndirectData
.Add(item
);
740 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
742 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
745 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
747 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
748 tvItem
.cChildren
= (int)has
;
752 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
754 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
755 tvItem
.state
= bold
? TVIS_BOLD
: 0;
759 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
761 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
762 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
766 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
771 long id
= (long)(WXHTREEITEM
)item
;
772 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
775 attr
= new wxTreeItemAttr
;
776 m_attrs
.Put(id
, (wxObject
*)attr
);
779 attr
->SetTextColour(col
);
782 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
787 long id
= (long)(WXHTREEITEM
)item
;
788 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
791 attr
= new wxTreeItemAttr
;
792 m_attrs
.Put(id
, (wxObject
*)attr
);
795 attr
->SetBackgroundColour(col
);
798 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
802 long id
= (long)(WXHTREEITEM
)item
;
803 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
806 attr
= new wxTreeItemAttr
;
807 m_attrs
.Put(id
, (wxObject
*)attr
);
813 // ----------------------------------------------------------------------------
815 // ----------------------------------------------------------------------------
817 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
819 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
822 // this ugliness comes directly from MSDN - it *is* the correct way to pass
823 // the HTREEITEM with TVM_GETITEMRECT
824 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
826 // FALSE means get item rect for the whole item, not only text
827 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
831 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
833 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
836 return tvItem
.cChildren
!= 0;
839 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
841 // probably not a good idea to put it here
842 //wxASSERT( ItemHasChildren(item) );
844 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
847 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
850 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
852 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
855 return (tvItem
.state
& TVIS_SELECTED
) != 0;
858 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
860 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
863 return (tvItem
.state
& TVIS_BOLD
) != 0;
866 // ----------------------------------------------------------------------------
868 // ----------------------------------------------------------------------------
870 wxTreeItemId
wxTreeCtrl::GetRootItem() const
872 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
875 wxTreeItemId
wxTreeCtrl::GetSelection() const
877 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
878 wxT("this only works with single selection controls") );
880 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
883 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
885 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
888 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
891 // remember the last child returned in 'cookie'
892 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
894 return wxTreeItemId((WXHTREEITEM
)_cookie
);
897 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
900 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
901 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
907 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
909 // can this be done more efficiently?
912 wxTreeItemId childLast
,
913 child
= GetFirstChild(item
, cookie
);
914 while ( child
.IsOk() )
917 child
= GetNextChild(item
, cookie
);
923 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
925 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
928 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
930 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
933 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
935 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
938 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
940 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
941 "for must be visible itself!"));
943 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
946 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
948 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
949 "for must be visible itself!"));
951 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
954 // ----------------------------------------------------------------------------
955 // multiple selections emulation
956 // ----------------------------------------------------------------------------
958 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
960 // receive the desired information.
961 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
964 // state image indices are 1 based
965 return ((tvItem
.state
>> 12) - 1) == 1;
968 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
970 // receive the desired information.
971 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
973 // state images are one-based
974 tvItem
.state
= (check
? 2 : 1) << 12;
979 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
981 TraverseSelections
selector(this, selections
);
983 return selector
.GetCount();
986 // ----------------------------------------------------------------------------
988 // ----------------------------------------------------------------------------
990 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
991 wxTreeItemId hInsertAfter
,
992 const wxString
& text
,
993 int image
, int selectedImage
,
994 wxTreeItemData
*data
)
996 TV_INSERTSTRUCT tvIns
;
997 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
998 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
1000 // this is how we insert the item as the first child: supply a NULL
1002 if ( !tvIns
.hInsertAfter
)
1004 tvIns
.hInsertAfter
= TVI_FIRST
;
1008 if ( !text
.IsEmpty() )
1011 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1017 tvIns
.item
.iImage
= image
;
1019 if ( selectedImage
== -1 )
1021 // take the same image for selected icon if not specified
1022 selectedImage
= image
;
1026 if ( selectedImage
!= -1 )
1028 mask
|= TVIF_SELECTEDIMAGE
;
1029 tvIns
.item
.iSelectedImage
= selectedImage
;
1035 tvIns
.item
.lParam
= (LPARAM
)data
;
1038 tvIns
.item
.mask
= mask
;
1040 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1043 wxLogLastError("TreeView_InsertItem");
1048 // associate the application tree item with Win32 tree item handle
1049 data
->SetId((WXHTREEITEM
)id
);
1052 return wxTreeItemId((WXHTREEITEM
)id
);
1055 // for compatibility only
1056 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1057 const wxString
& text
,
1058 int image
, int selImage
,
1061 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1062 image
, selImage
, NULL
);
1065 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1066 int image
, int selectedImage
,
1067 wxTreeItemData
*data
)
1069 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1070 text
, image
, selectedImage
, data
);
1073 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1074 const wxString
& text
,
1075 int image
, int selectedImage
,
1076 wxTreeItemData
*data
)
1078 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1079 text
, image
, selectedImage
, data
);
1082 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1083 const wxTreeItemId
& idPrevious
,
1084 const wxString
& text
,
1085 int image
, int selectedImage
,
1086 wxTreeItemData
*data
)
1088 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1091 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1093 const wxString
& text
,
1094 int image
, int selectedImage
,
1095 wxTreeItemData
*data
)
1097 // find the item from index
1099 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1100 while ( index
!= 0 && idCur
.IsOk() )
1105 idCur
= GetNextChild(parent
, cookie
);
1108 // assert, not check: if the index is invalid, we will append the item
1110 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1112 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1115 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1116 const wxString
& text
,
1117 int image
, int selectedImage
,
1118 wxTreeItemData
*data
)
1120 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1121 text
, image
, selectedImage
, data
);
1124 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1126 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
1128 wxLogLastError("TreeView_DeleteItem");
1132 // delete all children (but don't delete the item itself)
1133 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1137 wxArrayLong children
;
1138 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1139 while ( child
.IsOk() )
1141 children
.Add((long)(WXHTREEITEM
)child
);
1143 child
= GetNextChild(item
, cookie
);
1146 size_t nCount
= children
.Count();
1147 for ( size_t n
= 0; n
< nCount
; n
++ )
1149 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1151 wxLogLastError("TreeView_DeleteItem");
1156 void wxTreeCtrl::DeleteAllItems()
1158 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1160 wxLogLastError("TreeView_DeleteAllItems");
1164 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1166 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1167 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1168 flag
== TVE_EXPAND
||
1170 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1172 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1173 // emulate them. This behaviour has changed slightly with comctl32.dll
1174 // v 4.70 - now it does send them but only the first time. To maintain
1175 // compatible behaviour and also in order to not have surprises with the
1176 // future versions, don't rely on this and still do everything ourselves.
1177 // To avoid that the messages be sent twice when the item is expanded for
1178 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1180 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1184 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
1186 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1187 event
.m_item
= item
;
1189 bool isExpanded
= IsExpanded(item
);
1191 event
.SetEventObject(this);
1193 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1194 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1195 GetEventHandler()->ProcessEvent(event
);
1197 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1198 GetEventHandler()->ProcessEvent(event
);
1200 //else: change didn't took place, so do nothing at all
1203 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1205 DoExpand(item
, TVE_EXPAND
);
1208 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1210 DoExpand(item
, TVE_COLLAPSE
);
1213 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1215 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1218 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1220 DoExpand(item
, TVE_TOGGLE
);
1223 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1225 DoExpand(item
, action
);
1228 void wxTreeCtrl::Unselect()
1230 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxT("doesn't make sense") );
1232 // just remove the selection
1233 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1236 void wxTreeCtrl::UnselectAll()
1238 if ( m_windowStyle
& wxTR_MULTIPLE
)
1240 wxArrayTreeItemIds selections
;
1241 size_t count
= GetSelections(selections
);
1242 for ( size_t n
= 0; n
< count
; n
++ )
1244 SetItemCheck(selections
[n
], FALSE
);
1249 // just remove the selection
1254 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1256 if ( m_windowStyle
& wxTR_MULTIPLE
)
1258 // selecting the item means checking it
1263 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1264 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1265 // send them ourselves
1267 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1268 event
.m_item
= item
;
1269 event
.SetEventObject(this);
1271 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1272 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1274 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1276 wxLogLastError("TreeView_SelectItem");
1280 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1281 (void)GetEventHandler()->ProcessEvent(event
);
1284 //else: program vetoed the change
1288 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1291 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1294 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1296 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1298 wxLogLastError("TreeView_SelectSetFirstVisible");
1302 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1307 void wxTreeCtrl::DeleteTextCtrl()
1311 m_textCtrl
->UnsubclassWin();
1312 m_textCtrl
->SetHWND(0);
1318 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1319 wxClassInfo
* textControlClass
)
1321 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1323 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1325 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1334 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1335 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1336 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1341 // End label editing, optionally cancelling the edit
1342 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1344 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1349 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1351 TV_HITTESTINFO hitTestInfo
;
1352 hitTestInfo
.pt
.x
= (int)point
.x
;
1353 hitTestInfo
.pt
.y
= (int)point
.y
;
1355 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1360 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1361 flags |= wxTREE_HITTEST_##flag
1363 TRANSLATE_FLAG(ABOVE
);
1364 TRANSLATE_FLAG(BELOW
);
1365 TRANSLATE_FLAG(NOWHERE
);
1366 TRANSLATE_FLAG(ONITEMBUTTON
);
1367 TRANSLATE_FLAG(ONITEMICON
);
1368 TRANSLATE_FLAG(ONITEMINDENT
);
1369 TRANSLATE_FLAG(ONITEMLABEL
);
1370 TRANSLATE_FLAG(ONITEMRIGHT
);
1371 TRANSLATE_FLAG(ONITEMSTATEICON
);
1372 TRANSLATE_FLAG(TOLEFT
);
1373 TRANSLATE_FLAG(TORIGHT
);
1375 #undef TRANSLATE_FLAG
1377 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1380 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1382 bool textOnly
) const
1385 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1388 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1394 // couldn't retrieve rect: for example, item isn't visible
1399 // ----------------------------------------------------------------------------
1401 // ----------------------------------------------------------------------------
1403 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1404 wxTreeItemData
*pItem2
,
1407 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1408 wxT("sorting tree without data doesn't make sense") );
1410 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1413 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1414 const wxTreeItemId
& item2
)
1416 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1419 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1421 // rely on the fact that TreeView_SortChildren does the same thing as our
1422 // default behaviour, i.e. sorts items alphabetically and so call it
1423 // directly if we're not in derived class (much more efficient!)
1424 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1426 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1431 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1432 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1433 tvSort
.lParam
= (LPARAM
)this;
1434 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1438 // ----------------------------------------------------------------------------
1440 // ----------------------------------------------------------------------------
1442 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1444 if ( cmd
== EN_UPDATE
)
1446 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1447 event
.SetEventObject( this );
1448 ProcessCommand(event
);
1450 else if ( cmd
== EN_KILLFOCUS
)
1452 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1453 event
.SetEventObject( this );
1454 ProcessCommand(event
);
1462 // command processed
1466 // process WM_NOTIFY Windows message
1467 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1469 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1470 wxEventType eventType
= wxEVT_NULL
;
1471 NMHDR
*hdr
= (NMHDR
*)lParam
;
1473 switch ( hdr
->code
)
1477 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1480 TV_HITTESTINFO tvhti
;
1481 ::GetCursorPos(&(tvhti
.pt
));
1482 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1483 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1485 if( tvhti
.flags
& TVHT_ONITEM
)
1487 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1488 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1495 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1498 case TVN_BEGINRDRAG
:
1500 if ( eventType
== wxEVT_NULL
)
1501 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1502 //else: left drag, already set above
1504 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1506 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1507 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1511 case TVN_BEGINLABELEDIT
:
1513 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1514 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1516 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1517 event
.m_label
= info
->item
.pszText
;
1521 case TVN_DELETEITEM
:
1523 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1524 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1526 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1530 delete (wxTreeItemAttr
*)m_attrs
.
1531 Delete((long)tv
->itemOld
.hItem
);
1536 case TVN_ENDLABELEDIT
:
1538 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1539 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1541 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1542 event
.m_label
= info
->item
.pszText
;
1543 if (info
->item
.pszText
== NULL
)
1548 case TVN_GETDISPINFO
:
1549 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1552 case TVN_SETDISPINFO
:
1554 if ( eventType
== wxEVT_NULL
)
1555 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1556 //else: get, already set above
1558 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1560 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1564 case TVN_ITEMEXPANDING
:
1565 event
.m_code
= FALSE
;
1568 case TVN_ITEMEXPANDED
:
1570 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1572 bool expand
= FALSE
;
1573 switch ( tv
->action
)
1584 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1585 "message"), tv
->action
);
1588 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
1589 eventType
= g_events
[expand
][ing
];
1591 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1597 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1598 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1600 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1602 // a separate event for this case
1603 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1605 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1607 event2
.SetEventObject(this);
1609 GetEventHandler()->ProcessEvent(event2
);
1614 case TVN_SELCHANGED
:
1615 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1618 case TVN_SELCHANGING
:
1620 if ( eventType
== wxEVT_NULL
)
1621 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1622 //else: already set above
1624 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1626 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1627 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1631 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1634 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
1635 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
1636 switch( nmcd
.dwDrawStage
)
1639 // if we've got any items with non standard attributes,
1640 // notify us before painting each item
1641 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1645 case CDDS_ITEMPREPAINT
:
1647 wxTreeItemAttr
*attr
=
1648 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1652 // nothing to do for this item
1653 return CDRF_DODEFAULT
;
1657 wxColour colText
, colBack
;
1658 if ( attr
->HasFont() )
1660 wxFont font
= attr
->GetFont();
1661 hFont
= (HFONT
)font
.GetResourceHandle();
1668 if ( attr
->HasTextColour() )
1670 colText
= attr
->GetTextColour();
1674 colText
= GetForegroundColour();
1677 // selection colours should override ours
1678 if ( nmcd
.uItemState
& CDIS_SELECTED
)
1680 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
1681 lptvcd
->clrTextBk
= clrBk
;
1683 // try to make the text visible
1684 lptvcd
->clrText
= wxColourToRGB(colText
);
1685 lptvcd
->clrText
|= ~clrBk
;
1686 lptvcd
->clrText
&= 0x00ffffff;
1690 if ( attr
->HasBackgroundColour() )
1692 colBack
= attr
->GetBackgroundColour();
1696 colBack
= GetBackgroundColour();
1699 lptvcd
->clrText
= wxColourToRGB(colText
);
1700 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
1703 // note that if we wanted to set colours for
1704 // individual columns (subitems), we would have
1705 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1708 ::SelectObject(nmcd
.hdc
, hFont
);
1710 *result
= CDRF_NEWFONT
;
1714 *result
= CDRF_DODEFAULT
;
1721 *result
= CDRF_DODEFAULT
;
1726 #endif // _WIN32_IE >= 0x300
1729 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1732 event
.SetEventObject(this);
1733 event
.SetEventType(eventType
);
1735 bool processed
= GetEventHandler()->ProcessEvent(event
);
1738 switch ( hdr
->code
)
1740 case TVN_DELETEITEM
:
1742 // NB: we might process this message using wxWindows event
1743 // tables, but due to overhead of wxWin event system we
1744 // prefer to do it here ourself (otherwise deleting a tree
1745 // with many items is just too slow)
1746 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1748 wxTreeItemId item
= event
.m_item
;
1749 if ( HasIndirectData(item
) )
1751 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
1753 delete data
; // can't be NULL here
1755 m_itemsWithIndirectData
.Remove(item
);
1759 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1760 delete data
; // may be NULL, ok
1763 processed
= TRUE
; // Make sure we don't get called twice
1767 case TVN_BEGINLABELEDIT
:
1768 // return TRUE to cancel label editing
1769 *result
= !event
.IsAllowed();
1772 case TVN_ENDLABELEDIT
:
1773 // return TRUE to set the label to the new string
1774 *result
= event
.IsAllowed();
1776 // ensure that we don't have the text ctrl which is going to be
1781 case TVN_SELCHANGING
:
1782 case TVN_ITEMEXPANDING
:
1783 // return TRUE to prevent the action from happening
1784 *result
= !event
.IsAllowed();
1787 case TVN_GETDISPINFO
:
1788 // NB: so far the user can't set the image himself anyhow, so do it
1789 // anyway - but this may change later
1790 if ( /* !processed && */ 1 )
1792 wxTreeItemId item
= event
.m_item
;
1793 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1794 if ( info
->item
.mask
& TVIF_IMAGE
)
1797 DoGetItemImageFromData
1800 IsExpanded(item
) ? wxTreeItemIcon_Expanded
1801 : wxTreeItemIcon_Normal
1804 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
1806 info
->item
.iSelectedImage
=
1807 DoGetItemImageFromData
1810 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
1811 : wxTreeItemIcon_Selected
1818 // for the other messages the return value is ignored and there is
1819 // nothing special to do
1825 // ----------------------------------------------------------------------------
1827 // ----------------------------------------------------------------------------
1829 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1831 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1832 : wxNotifyEvent(commandType
, id
)