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
);
153 wxArrayTreeItemIds
& m_selections
;
156 // internal class for counting tree items
157 class TraverseCounter
: public wxTreeTraversal
160 TraverseCounter(const wxTreeCtrl
*tree
,
161 const wxTreeItemId
& root
,
163 : wxTreeTraversal(tree
)
167 DoTraverse(root
, recursively
);
170 virtual bool OnVisit(const wxTreeItemId
& item
)
177 size_t GetCount() const { return m_count
; }
183 // ----------------------------------------------------------------------------
184 // This class is needed for support of different images: the Win32 common
185 // control natively supports only 2 images (the normal one and another for the
186 // selected state). We wish to provide support for 2 more of them for folder
187 // items (i.e. those which have children): for expanded state and for expanded
188 // selected state. For this we use this structure to store the additional items
191 // There is only one problem with this: when we retrieve the item's data, we
192 // don't know whether we get a pointer to wxTreeItemData or
193 // wxTreeItemIndirectData. So we have to maintain a list of all items which
194 // have indirect data inside the listctrl itself.
195 // ----------------------------------------------------------------------------
197 class wxTreeItemIndirectData
200 // ctor associates this data with the item and the real item data becomes
201 // available through our GetData() method
202 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
204 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
210 m_data
= tree
->GetItemData(item
);
212 // and set ourselves as the new one
213 tree
->SetIndirectItemData(item
, this);
216 // dtor deletes the associated data as well
217 ~wxTreeItemIndirectData() { delete m_data
; }
220 // get the real data associated with the item
221 wxTreeItemData
*GetData() const { return m_data
; }
223 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
225 // do we have such image?
226 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
228 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
230 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
233 // all the images associated with the item
234 int m_images
[wxTreeItemIcon_Max
];
236 wxTreeItemData
*m_data
;
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 // handy table for sending events
250 static const wxEventType g_events
[2][2] =
252 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
253 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
256 // ============================================================================
258 // ============================================================================
260 // ----------------------------------------------------------------------------
262 // ----------------------------------------------------------------------------
264 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
266 if ( !OnVisit(root
) )
269 return Traverse(root
, recursively
);
272 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
275 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
276 while ( child
.IsOk() )
278 // depth first traversal
279 if ( recursively
&& !Traverse(child
, TRUE
) )
282 if ( !OnVisit(child
) )
285 child
= m_tree
->GetNextChild(root
, cookie
);
291 // ----------------------------------------------------------------------------
292 // construction and destruction
293 // ----------------------------------------------------------------------------
295 void wxTreeCtrl::Init()
297 m_imageListNormal
= NULL
;
298 m_imageListState
= NULL
;
300 m_hasAnyAttr
= FALSE
;
303 bool wxTreeCtrl::Create(wxWindow
*parent
,
308 const wxValidator
& validator
,
309 const wxString
& name
)
313 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
316 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
317 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
319 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
320 wstyle
|= TVS_HASBUTTONS
;
322 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
323 wstyle
|= TVS_EDITLABELS
;
325 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
326 wstyle
|= TVS_LINESATROOT
;
328 #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined( __WATCOMC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
329 // we emulate the multiple selection tree controls by using checkboxes: set
330 // up the image list we need for this if we do have multiple selections
331 #if !defined(__VISUALC__) || (__VISUALC__ > 1010)
332 if ( m_windowStyle
& wxTR_MULTIPLE
)
333 wstyle
|= TVS_CHECKBOXES
;
337 // Create the tree control.
338 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
341 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
342 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
344 // VZ: this is some experimental code which may be used to get the
345 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
346 // AFAIK, the standard DLL does about the same thing anyhow.
348 if ( m_windowStyle
& wxTR_MULTIPLE
)
352 // create the DC compatible with the current screen
353 HDC hdcMem
= CreateCompatibleDC(NULL
);
355 // create a mono bitmap of the standard size
356 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
357 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
358 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
359 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
360 1, // # of color planes
361 1, // # bits needed for one pixel
362 0); // array containing colour data
363 SelectObject(hdcMem
, hbmpCheck
);
365 // then draw a check mark into it
366 RECT rect
= { 0, 0, x
, y
};
367 if ( !::DrawFrameControl(hdcMem
, &rect
,
369 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
371 wxLogLastError(wxT("DrawFrameControl(check)"));
374 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
375 imagelistCheckboxes
.Add(bmp
);
377 if ( !::DrawFrameControl(hdcMem
, &rect
,
381 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
384 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
385 imagelistCheckboxes
.Add(bmp
);
391 SetStateImageList(&imagelistCheckboxes
);
395 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
400 wxTreeCtrl::~wxTreeCtrl()
402 // delete any attributes
405 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
407 delete (wxTreeItemAttr
*)node
->Data();
410 // prevent TVN_DELETEITEM handler from deleting the attributes again!
411 m_hasAnyAttr
= FALSE
;
416 // delete user data to prevent memory leaks
420 // ----------------------------------------------------------------------------
422 // ----------------------------------------------------------------------------
424 // simple wrappers which add error checking in debug mode
426 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
428 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
430 wxLogLastError("TreeView_GetItem");
438 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
440 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
442 wxLogLastError("TreeView_SetItem");
446 size_t wxTreeCtrl::GetCount() const
448 return (size_t)TreeView_GetCount(GetHwnd());
451 unsigned int wxTreeCtrl::GetIndent() const
453 return TreeView_GetIndent(GetHwnd());
456 void wxTreeCtrl::SetIndent(unsigned int indent
)
458 TreeView_SetIndent(GetHwnd(), indent
);
461 wxImageList
*wxTreeCtrl::GetImageList() const
463 return m_imageListNormal
;
466 wxImageList
*wxTreeCtrl::GetStateImageList() const
468 return m_imageListNormal
;
471 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
474 TreeView_SetImageList(GetHwnd(),
475 imageList
? imageList
->GetHIMAGELIST() : 0,
479 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
481 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
484 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
486 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
489 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
490 bool recursively
) const
492 TraverseCounter
counter(this, item
, recursively
);
494 return counter
.GetCount() - 1;
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
503 if ( !wxWindowBase::SetBackgroundColour(colour
) )
506 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
511 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
513 if ( !wxWindowBase::SetForegroundColour(colour
) )
516 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
521 // ----------------------------------------------------------------------------
523 // ----------------------------------------------------------------------------
525 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
527 wxChar buf
[512]; // the size is arbitrary...
529 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
530 tvItem
.pszText
= buf
;
531 tvItem
.cchTextMax
= WXSIZEOF(buf
);
532 if ( !DoGetItem(&tvItem
) )
534 // don't return some garbage which was on stack, but an empty string
538 return wxString(buf
);
541 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
543 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
544 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
548 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
549 wxTreeItemIcon which
) const
551 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
552 if ( !DoGetItem(&tvItem
) )
557 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
560 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
562 wxTreeItemIcon which
) const
564 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
565 if ( !DoGetItem(&tvItem
) )
570 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
572 data
->SetImage(image
, which
);
574 // make sure that we have selected images as well
575 if ( which
== wxTreeItemIcon_Normal
&&
576 !data
->HasImage(wxTreeItemIcon_Selected
) )
578 data
->SetImage(image
, wxTreeItemIcon_Selected
);
581 if ( which
== wxTreeItemIcon_Expanded
&&
582 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
584 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
588 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
592 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
593 tvItem
.iSelectedImage
= imageSel
;
594 tvItem
.iImage
= image
;
598 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
599 wxTreeItemIcon which
) const
601 if ( HasIndirectData(item
) )
603 return DoGetItemImageFromData(item
, which
);
610 wxFAIL_MSG( wxT("unknown tree item image type") );
612 case wxTreeItemIcon_Normal
:
616 case wxTreeItemIcon_Selected
:
617 mask
= TVIF_SELECTEDIMAGE
;
620 case wxTreeItemIcon_Expanded
:
621 case wxTreeItemIcon_SelectedExpanded
:
625 wxTreeViewItem
tvItem(item
, mask
);
628 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
631 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
632 wxTreeItemIcon which
)
634 int imageNormal
, imageSel
;
638 wxFAIL_MSG( wxT("unknown tree item image type") );
640 case wxTreeItemIcon_Normal
:
642 imageSel
= GetItemSelectedImage(item
);
645 case wxTreeItemIcon_Selected
:
646 imageNormal
= GetItemImage(item
);
650 case wxTreeItemIcon_Expanded
:
651 case wxTreeItemIcon_SelectedExpanded
:
652 if ( !HasIndirectData(item
) )
654 // we need to get the old images first, because after we create
655 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
657 imageNormal
= GetItemImage(item
);
658 imageSel
= GetItemSelectedImage(item
);
660 // if it doesn't have it yet, add it
661 wxTreeItemIndirectData
*data
= new
662 wxTreeItemIndirectData(this, item
);
664 // copy the data to the new location
665 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
666 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
669 DoSetItemImageFromData(item
, image
, which
);
671 // reset the normal/selected images because we won't use them any
672 // more - now they're stored inside the indirect data
674 imageSel
= I_IMAGECALLBACK
;
678 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
679 // change both normal and selected image - otherwise the change simply
680 // doesn't take place!
681 DoSetItemImages(item
, imageNormal
, imageSel
);
684 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
686 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
687 if ( !DoGetItem(&tvItem
) )
692 if ( HasIndirectData(item
) )
694 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
698 return (wxTreeItemData
*)tvItem
.lParam
;
702 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
704 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
706 if ( HasIndirectData(item
) )
708 if ( DoGetItem(&tvItem
) )
710 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
714 wxFAIL_MSG( wxT("failed to change tree items data") );
719 tvItem
.lParam
= (LPARAM
)data
;
724 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
725 wxTreeItemIndirectData
*data
)
727 // this should never happen because it's unnecessary and will probably lead
728 // to crash too because the code elsewhere supposes that the pointer the
729 // wxTreeItemIndirectData has is a real wxItemData and not
730 // wxTreeItemIndirectData as well
731 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
733 SetItemData(item
, (wxTreeItemData
*)data
);
735 m_itemsWithIndirectData
.Add(item
);
738 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
740 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
743 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
745 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
746 tvItem
.cChildren
= (int)has
;
750 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
752 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
753 tvItem
.state
= bold
? TVIS_BOLD
: 0;
757 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
759 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
760 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
764 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
769 long id
= (long)(WXHTREEITEM
)item
;
770 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
773 attr
= new wxTreeItemAttr
;
774 m_attrs
.Put(id
, (wxObject
*)attr
);
777 attr
->SetTextColour(col
);
780 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
785 long id
= (long)(WXHTREEITEM
)item
;
786 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
789 attr
= new wxTreeItemAttr
;
790 m_attrs
.Put(id
, (wxObject
*)attr
);
793 attr
->SetBackgroundColour(col
);
796 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
800 long id
= (long)(WXHTREEITEM
)item
;
801 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
804 attr
= new wxTreeItemAttr
;
805 m_attrs
.Put(id
, (wxObject
*)attr
);
811 // ----------------------------------------------------------------------------
813 // ----------------------------------------------------------------------------
815 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
817 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
820 // this ugliness comes directly from MSDN - it *is* the correct way to pass
821 // the HTREEITEM with TVM_GETITEMRECT
822 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
824 // FALSE means get item rect for the whole item, not only text
825 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
829 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
831 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
834 return tvItem
.cChildren
!= 0;
837 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
839 // probably not a good idea to put it here
840 //wxASSERT( ItemHasChildren(item) );
842 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
845 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
848 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
850 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
853 return (tvItem
.state
& TVIS_SELECTED
) != 0;
856 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
858 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
861 return (tvItem
.state
& TVIS_BOLD
) != 0;
864 // ----------------------------------------------------------------------------
866 // ----------------------------------------------------------------------------
868 wxTreeItemId
wxTreeCtrl::GetRootItem() const
870 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
873 wxTreeItemId
wxTreeCtrl::GetSelection() const
875 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
876 wxT("this only works with single selection controls") );
878 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
881 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
883 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
886 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
889 // remember the last child returned in 'cookie'
890 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
892 return wxTreeItemId((WXHTREEITEM
)_cookie
);
895 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
898 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
899 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
905 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
907 // can this be done more efficiently?
910 wxTreeItemId childLast
,
911 child
= GetFirstChild(item
, cookie
);
912 while ( child
.IsOk() )
915 child
= GetNextChild(item
, cookie
);
921 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
923 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
926 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
928 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
931 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
933 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
936 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
938 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
939 "for must be visible itself!"));
941 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
944 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
946 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
947 "for must be visible itself!"));
949 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
952 // ----------------------------------------------------------------------------
953 // multiple selections emulation
954 // ----------------------------------------------------------------------------
956 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
958 // receive the desired information.
959 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
962 // state image indices are 1 based
963 return ((tvItem
.state
>> 12) - 1) == 1;
966 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
968 // receive the desired information.
969 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
971 // state images are one-based
972 tvItem
.state
= (check
? 2 : 1) << 12;
977 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
979 TraverseSelections
selector(this, selections
);
981 return selections
.GetCount();
984 // ----------------------------------------------------------------------------
986 // ----------------------------------------------------------------------------
988 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
989 wxTreeItemId hInsertAfter
,
990 const wxString
& text
,
991 int image
, int selectedImage
,
992 wxTreeItemData
*data
)
994 TV_INSERTSTRUCT tvIns
;
995 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
996 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
998 // this is how we insert the item as the first child: supply a NULL
1000 if ( !tvIns
.hInsertAfter
)
1002 tvIns
.hInsertAfter
= TVI_FIRST
;
1006 if ( !text
.IsEmpty() )
1009 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1015 tvIns
.item
.iImage
= image
;
1017 if ( selectedImage
== -1 )
1019 // take the same image for selected icon if not specified
1020 selectedImage
= image
;
1024 if ( selectedImage
!= -1 )
1026 mask
|= TVIF_SELECTEDIMAGE
;
1027 tvIns
.item
.iSelectedImage
= selectedImage
;
1033 tvIns
.item
.lParam
= (LPARAM
)data
;
1036 tvIns
.item
.mask
= mask
;
1038 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1041 wxLogLastError("TreeView_InsertItem");
1046 // associate the application tree item with Win32 tree item handle
1047 data
->SetId((WXHTREEITEM
)id
);
1050 return wxTreeItemId((WXHTREEITEM
)id
);
1053 // for compatibility only
1054 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1055 const wxString
& text
,
1056 int image
, int selImage
,
1059 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1060 image
, selImage
, NULL
);
1063 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1064 int image
, int selectedImage
,
1065 wxTreeItemData
*data
)
1067 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1068 text
, image
, selectedImage
, data
);
1071 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1072 const wxString
& text
,
1073 int image
, int selectedImage
,
1074 wxTreeItemData
*data
)
1076 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1077 text
, image
, selectedImage
, data
);
1080 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1081 const wxTreeItemId
& idPrevious
,
1082 const wxString
& text
,
1083 int image
, int selectedImage
,
1084 wxTreeItemData
*data
)
1086 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1089 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1091 const wxString
& text
,
1092 int image
, int selectedImage
,
1093 wxTreeItemData
*data
)
1095 // find the item from index
1097 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1098 while ( index
!= 0 && idCur
.IsOk() )
1103 idCur
= GetNextChild(parent
, cookie
);
1106 // assert, not check: if the index is invalid, we will append the item
1108 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1110 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1113 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1114 const wxString
& text
,
1115 int image
, int selectedImage
,
1116 wxTreeItemData
*data
)
1118 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1119 text
, image
, selectedImage
, data
);
1122 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1124 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
1126 wxLogLastError("TreeView_DeleteItem");
1130 // delete all children (but don't delete the item itself)
1131 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1135 wxArrayLong children
;
1136 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1137 while ( child
.IsOk() )
1139 children
.Add((long)(WXHTREEITEM
)child
);
1141 child
= GetNextChild(item
, cookie
);
1144 size_t nCount
= children
.Count();
1145 for ( size_t n
= 0; n
< nCount
; n
++ )
1147 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1149 wxLogLastError("TreeView_DeleteItem");
1154 void wxTreeCtrl::DeleteAllItems()
1156 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1158 wxLogLastError("TreeView_DeleteAllItems");
1162 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1164 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1165 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1166 flag
== TVE_EXPAND
||
1168 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1170 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1171 // emulate them. This behaviour has changed slightly with comctl32.dll
1172 // v 4.70 - now it does send them but only the first time. To maintain
1173 // compatible behaviour and also in order to not have surprises with the
1174 // future versions, don't rely on this and still do everything ourselves.
1175 // To avoid that the messages be sent twice when the item is expanded for
1176 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1178 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1182 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
1184 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1185 event
.m_item
= item
;
1187 bool isExpanded
= IsExpanded(item
);
1189 event
.SetEventObject(this);
1191 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1192 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1193 GetEventHandler()->ProcessEvent(event
);
1195 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1196 GetEventHandler()->ProcessEvent(event
);
1198 //else: change didn't took place, so do nothing at all
1201 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1203 DoExpand(item
, TVE_EXPAND
);
1206 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1208 DoExpand(item
, TVE_COLLAPSE
);
1211 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1213 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1216 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1218 DoExpand(item
, TVE_TOGGLE
);
1221 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1223 DoExpand(item
, action
);
1226 void wxTreeCtrl::Unselect()
1228 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxT("doesn't make sense") );
1230 // just remove the selection
1231 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1234 void wxTreeCtrl::UnselectAll()
1236 if ( m_windowStyle
& wxTR_MULTIPLE
)
1238 wxArrayTreeItemIds selections
;
1239 size_t count
= GetSelections(selections
);
1240 for ( size_t n
= 0; n
< count
; n
++ )
1242 SetItemCheck(selections
[n
], FALSE
);
1247 // just remove the selection
1252 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1254 if ( m_windowStyle
& wxTR_MULTIPLE
)
1256 // selecting the item means checking it
1261 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1262 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1263 // send them ourselves
1265 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1266 event
.m_item
= item
;
1267 event
.SetEventObject(this);
1269 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1270 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1272 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1274 wxLogLastError("TreeView_SelectItem");
1278 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1279 (void)GetEventHandler()->ProcessEvent(event
);
1282 //else: program vetoed the change
1286 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1289 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1292 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1294 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1296 wxLogLastError("TreeView_SelectSetFirstVisible");
1300 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1305 void wxTreeCtrl::DeleteTextCtrl()
1309 m_textCtrl
->UnsubclassWin();
1310 m_textCtrl
->SetHWND(0);
1316 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1317 wxClassInfo
* textControlClass
)
1319 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1321 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1323 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1332 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1333 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1334 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1339 // End label editing, optionally cancelling the edit
1340 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1342 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1347 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1349 TV_HITTESTINFO hitTestInfo
;
1350 hitTestInfo
.pt
.x
= (int)point
.x
;
1351 hitTestInfo
.pt
.y
= (int)point
.y
;
1353 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1358 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1359 flags |= wxTREE_HITTEST_##flag
1361 TRANSLATE_FLAG(ABOVE
);
1362 TRANSLATE_FLAG(BELOW
);
1363 TRANSLATE_FLAG(NOWHERE
);
1364 TRANSLATE_FLAG(ONITEMBUTTON
);
1365 TRANSLATE_FLAG(ONITEMICON
);
1366 TRANSLATE_FLAG(ONITEMINDENT
);
1367 TRANSLATE_FLAG(ONITEMLABEL
);
1368 TRANSLATE_FLAG(ONITEMRIGHT
);
1369 TRANSLATE_FLAG(ONITEMSTATEICON
);
1370 TRANSLATE_FLAG(TOLEFT
);
1371 TRANSLATE_FLAG(TORIGHT
);
1373 #undef TRANSLATE_FLAG
1375 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1378 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1380 bool textOnly
) const
1383 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1386 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1392 // couldn't retrieve rect: for example, item isn't visible
1397 // ----------------------------------------------------------------------------
1399 // ----------------------------------------------------------------------------
1401 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1402 wxTreeItemData
*pItem2
,
1405 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1406 wxT("sorting tree without data doesn't make sense") );
1408 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1411 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1412 const wxTreeItemId
& item2
)
1414 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1417 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1419 // rely on the fact that TreeView_SortChildren does the same thing as our
1420 // default behaviour, i.e. sorts items alphabetically and so call it
1421 // directly if we're not in derived class (much more efficient!)
1422 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1424 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1429 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1430 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1431 tvSort
.lParam
= (LPARAM
)this;
1432 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1436 // ----------------------------------------------------------------------------
1438 // ----------------------------------------------------------------------------
1440 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1442 if ( cmd
== EN_UPDATE
)
1444 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1445 event
.SetEventObject( this );
1446 ProcessCommand(event
);
1448 else if ( cmd
== EN_KILLFOCUS
)
1450 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1451 event
.SetEventObject( this );
1452 ProcessCommand(event
);
1460 // command processed
1464 // process WM_NOTIFY Windows message
1465 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1467 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1468 wxEventType eventType
= wxEVT_NULL
;
1469 NMHDR
*hdr
= (NMHDR
*)lParam
;
1471 switch ( hdr
->code
)
1475 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1478 TV_HITTESTINFO tvhti
;
1479 ::GetCursorPos(&(tvhti
.pt
));
1480 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1481 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1483 if( tvhti
.flags
& TVHT_ONITEM
)
1485 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1486 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1493 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1496 case TVN_BEGINRDRAG
:
1498 if ( eventType
== wxEVT_NULL
)
1499 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1500 //else: left drag, already set above
1502 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1504 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1505 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1509 case TVN_BEGINLABELEDIT
:
1511 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1512 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1514 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1515 event
.m_label
= info
->item
.pszText
;
1519 case TVN_DELETEITEM
:
1521 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1522 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1524 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1528 delete (wxTreeItemAttr
*)m_attrs
.
1529 Delete((long)tv
->itemOld
.hItem
);
1534 case TVN_ENDLABELEDIT
:
1536 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1537 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1539 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1540 event
.m_label
= info
->item
.pszText
;
1541 if (info
->item
.pszText
== NULL
)
1546 case TVN_GETDISPINFO
:
1547 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1550 case TVN_SETDISPINFO
:
1552 if ( eventType
== wxEVT_NULL
)
1553 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1554 //else: get, already set above
1556 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1558 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1562 case TVN_ITEMEXPANDING
:
1563 event
.m_code
= FALSE
;
1566 case TVN_ITEMEXPANDED
:
1568 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1570 bool expand
= FALSE
;
1571 switch ( tv
->action
)
1582 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1583 "message"), tv
->action
);
1586 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
1587 eventType
= g_events
[expand
][ing
];
1589 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1595 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1596 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1598 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1600 // a separate event for this case
1601 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1603 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1605 event2
.SetEventObject(this);
1607 GetEventHandler()->ProcessEvent(event2
);
1612 case TVN_SELCHANGED
:
1613 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1616 case TVN_SELCHANGING
:
1618 if ( eventType
== wxEVT_NULL
)
1619 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1620 //else: already set above
1622 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1624 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1625 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1629 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1632 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
1633 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
1634 switch( nmcd
.dwDrawStage
)
1637 // if we've got any items with non standard attributes,
1638 // notify us before painting each item
1639 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1643 case CDDS_ITEMPREPAINT
:
1645 wxTreeItemAttr
*attr
=
1646 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1650 // nothing to do for this item
1651 return CDRF_DODEFAULT
;
1655 wxColour colText
, colBack
;
1656 if ( attr
->HasFont() )
1658 wxFont font
= attr
->GetFont();
1659 hFont
= (HFONT
)font
.GetResourceHandle();
1666 if ( attr
->HasTextColour() )
1668 colText
= attr
->GetTextColour();
1672 colText
= GetForegroundColour();
1675 // selection colours should override ours
1676 if ( nmcd
.uItemState
& CDIS_SELECTED
)
1678 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
1679 lptvcd
->clrTextBk
= clrBk
;
1681 // try to make the text visible
1682 lptvcd
->clrText
= wxColourToRGB(colText
);
1683 lptvcd
->clrText
|= ~clrBk
;
1684 lptvcd
->clrText
&= 0x00ffffff;
1688 if ( attr
->HasBackgroundColour() )
1690 colBack
= attr
->GetBackgroundColour();
1694 colBack
= GetBackgroundColour();
1697 lptvcd
->clrText
= wxColourToRGB(colText
);
1698 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
1701 // note that if we wanted to set colours for
1702 // individual columns (subitems), we would have
1703 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1706 ::SelectObject(nmcd
.hdc
, hFont
);
1708 *result
= CDRF_NEWFONT
;
1712 *result
= CDRF_DODEFAULT
;
1719 *result
= CDRF_DODEFAULT
;
1724 #endif // _WIN32_IE >= 0x300
1727 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1730 event
.SetEventObject(this);
1731 event
.SetEventType(eventType
);
1733 bool processed
= GetEventHandler()->ProcessEvent(event
);
1736 switch ( hdr
->code
)
1738 case TVN_DELETEITEM
:
1740 // NB: we might process this message using wxWindows event
1741 // tables, but due to overhead of wxWin event system we
1742 // prefer to do it here ourself (otherwise deleting a tree
1743 // with many items is just too slow)
1744 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1746 wxTreeItemId item
= event
.m_item
;
1747 if ( HasIndirectData(item
) )
1749 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
1751 delete data
; // can't be NULL here
1753 m_itemsWithIndirectData
.Remove(item
);
1757 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1758 delete data
; // may be NULL, ok
1761 processed
= TRUE
; // Make sure we don't get called twice
1765 case TVN_BEGINLABELEDIT
:
1766 // return TRUE to cancel label editing
1767 *result
= !event
.IsAllowed();
1770 case TVN_ENDLABELEDIT
:
1771 // return TRUE to set the label to the new string
1772 *result
= event
.IsAllowed();
1774 // ensure that we don't have the text ctrl which is going to be
1779 case TVN_SELCHANGING
:
1780 case TVN_ITEMEXPANDING
:
1781 // return TRUE to prevent the action from happening
1782 *result
= !event
.IsAllowed();
1785 case TVN_GETDISPINFO
:
1786 // NB: so far the user can't set the image himself anyhow, so do it
1787 // anyway - but this may change later
1788 if ( /* !processed && */ 1 )
1790 wxTreeItemId item
= event
.m_item
;
1791 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1792 if ( info
->item
.mask
& TVIF_IMAGE
)
1795 DoGetItemImageFromData
1798 IsExpanded(item
) ? wxTreeItemIcon_Expanded
1799 : wxTreeItemIcon_Normal
1802 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
1804 info
->item
.iSelectedImage
=
1805 DoGetItemImageFromData
1808 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
1809 : wxTreeItemIcon_Selected
1816 // for the other messages the return value is ignored and there is
1817 // nothing special to do
1823 // ----------------------------------------------------------------------------
1825 // ----------------------------------------------------------------------------
1827 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1829 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1830 : wxNotifyEvent(commandType
, id
)