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"
50 #ifdef __GNUWIN32_OLD__
51 #include "wx/msw/gnuwin32/extra.h"
54 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
58 // Bug in headers, sometimes
60 #define TVIS_FOCUSED 0x0001
64 #define TV_FIRST 0x1100
67 // old headers might miss these messages (comctl32.dll 4.71+ only)
68 #ifndef TVM_SETBKCOLOR
69 #define TVM_SETBKCOLOR (TV_FIRST + 29)
70 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 // a convenient wrapper around TV_ITEM struct which adds a ctor
79 #pragma warning( disable : 4097 )
82 struct wxTreeViewItem
: public TV_ITEM
84 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
85 UINT mask_
, // fields which are valid
86 UINT stateMask_
= 0) // for TVIF_STATE only
88 // hItem member is always valid
89 mask
= mask_
| TVIF_HANDLE
;
90 stateMask
= stateMask_
;
91 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
96 #pragma warning( default : 4097 )
99 // a class which encapsulates the tree traversal logic: it vists all (unless
100 // OnVisit() returns FALSE) items under the given one
101 class wxTreeTraversal
104 wxTreeTraversal(const wxTreeCtrl
*tree
)
109 // do traverse the tree: visit all items (recursively by default) under the
110 // given one; return TRUE if all items were traversed or FALSE if the
111 // traversal was aborted because OnVisit returned FALSE
112 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
114 // override this function to do whatever is needed for each item, return
115 // FALSE to stop traversing
116 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
119 const wxTreeCtrl
*GetTree() const { return m_tree
; }
122 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
124 const wxTreeCtrl
*m_tree
;
127 // internal class for getting the selected items
128 class TraverseSelections
: public wxTreeTraversal
131 TraverseSelections(const wxTreeCtrl
*tree
,
132 wxArrayTreeItemIds
& selections
)
133 : wxTreeTraversal(tree
), m_selections(selections
)
135 m_selections
.Empty();
137 DoTraverse(tree
->GetRootItem());
140 virtual bool OnVisit(const wxTreeItemId
& item
)
142 if ( GetTree()->IsItemChecked(item
) )
144 m_selections
.Add(item
);
150 size_t GetCount() const { return m_selections
.GetCount(); }
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_OLD__ ) && \
329 !defined( __BORLANDC__ ) && \
330 !defined( __WATCOMC__ ) && \
331 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
332 // we emulate the multiple selection tree controls by using checkboxes: set
333 // up the image list we need for this if we do have multiple selections
334 if ( m_windowStyle
& wxTR_MULTIPLE
)
335 wstyle
|= TVS_CHECKBOXES
;
338 // Create the tree control.
339 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
342 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
343 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
345 // VZ: this is some experimental code which may be used to get the
346 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
347 // AFAIK, the standard DLL does about the same thing anyhow.
349 if ( m_windowStyle
& wxTR_MULTIPLE
)
353 // create the DC compatible with the current screen
354 HDC hdcMem
= CreateCompatibleDC(NULL
);
356 // create a mono bitmap of the standard size
357 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
358 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
359 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
360 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
361 1, // # of color planes
362 1, // # bits needed for one pixel
363 0); // array containing colour data
364 SelectObject(hdcMem
, hbmpCheck
);
366 // then draw a check mark into it
367 RECT rect
= { 0, 0, x
, y
};
368 if ( !::DrawFrameControl(hdcMem
, &rect
,
370 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
372 wxLogLastError(wxT("DrawFrameControl(check)"));
375 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
376 imagelistCheckboxes
.Add(bmp
);
378 if ( !::DrawFrameControl(hdcMem
, &rect
,
382 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
385 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
386 imagelistCheckboxes
.Add(bmp
);
392 SetStateImageList(&imagelistCheckboxes
);
396 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
401 wxTreeCtrl::~wxTreeCtrl()
403 // delete any attributes
406 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
408 delete (wxTreeItemAttr
*)node
->Data();
411 // prevent TVN_DELETEITEM handler from deleting the attributes again!
412 m_hasAnyAttr
= FALSE
;
417 // delete user data to prevent memory leaks
421 // ----------------------------------------------------------------------------
423 // ----------------------------------------------------------------------------
425 // simple wrappers which add error checking in debug mode
427 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
429 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
431 wxLogLastError("TreeView_GetItem");
439 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
441 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
443 wxLogLastError("TreeView_SetItem");
447 size_t wxTreeCtrl::GetCount() const
449 return (size_t)TreeView_GetCount(GetHwnd());
452 unsigned int wxTreeCtrl::GetIndent() const
454 return TreeView_GetIndent(GetHwnd());
457 void wxTreeCtrl::SetIndent(unsigned int indent
)
459 TreeView_SetIndent(GetHwnd(), indent
);
462 wxImageList
*wxTreeCtrl::GetImageList() const
464 return m_imageListNormal
;
467 wxImageList
*wxTreeCtrl::GetStateImageList() const
469 return m_imageListNormal
;
472 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
475 TreeView_SetImageList(GetHwnd(),
476 imageList
? imageList
->GetHIMAGELIST() : 0,
480 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
482 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
485 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
487 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
490 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
491 bool recursively
) const
493 TraverseCounter
counter(this, item
, recursively
);
495 return counter
.GetCount() - 1;
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
504 if ( !wxWindowBase::SetBackgroundColour(colour
) )
507 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
512 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
514 if ( !wxWindowBase::SetForegroundColour(colour
) )
517 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
522 // ----------------------------------------------------------------------------
524 // ----------------------------------------------------------------------------
526 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
528 wxChar buf
[512]; // the size is arbitrary...
530 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
531 tvItem
.pszText
= buf
;
532 tvItem
.cchTextMax
= WXSIZEOF(buf
);
533 if ( !DoGetItem(&tvItem
) )
535 // don't return some garbage which was on stack, but an empty string
539 return wxString(buf
);
542 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
544 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
545 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
549 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
550 wxTreeItemIcon which
) const
552 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
553 if ( !DoGetItem(&tvItem
) )
558 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
561 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
563 wxTreeItemIcon which
) const
565 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
566 if ( !DoGetItem(&tvItem
) )
571 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
573 data
->SetImage(image
, which
);
575 // make sure that we have selected images as well
576 if ( which
== wxTreeItemIcon_Normal
&&
577 !data
->HasImage(wxTreeItemIcon_Selected
) )
579 data
->SetImage(image
, wxTreeItemIcon_Selected
);
582 if ( which
== wxTreeItemIcon_Expanded
&&
583 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
585 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
589 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
593 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
594 tvItem
.iSelectedImage
= imageSel
;
595 tvItem
.iImage
= image
;
599 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
600 wxTreeItemIcon which
) const
602 if ( HasIndirectData(item
) )
604 return DoGetItemImageFromData(item
, which
);
611 wxFAIL_MSG( wxT("unknown tree item image type") );
613 case wxTreeItemIcon_Normal
:
617 case wxTreeItemIcon_Selected
:
618 mask
= TVIF_SELECTEDIMAGE
;
621 case wxTreeItemIcon_Expanded
:
622 case wxTreeItemIcon_SelectedExpanded
:
626 wxTreeViewItem
tvItem(item
, mask
);
629 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
632 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
633 wxTreeItemIcon which
)
635 int imageNormal
, imageSel
;
639 wxFAIL_MSG( wxT("unknown tree item image type") );
641 case wxTreeItemIcon_Normal
:
643 imageSel
= GetItemSelectedImage(item
);
646 case wxTreeItemIcon_Selected
:
647 imageNormal
= GetItemImage(item
);
651 case wxTreeItemIcon_Expanded
:
652 case wxTreeItemIcon_SelectedExpanded
:
653 if ( !HasIndirectData(item
) )
655 // we need to get the old images first, because after we create
656 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
658 imageNormal
= GetItemImage(item
);
659 imageSel
= GetItemSelectedImage(item
);
661 // if it doesn't have it yet, add it
662 wxTreeItemIndirectData
*data
= new
663 wxTreeItemIndirectData(this, item
);
665 // copy the data to the new location
666 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
667 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
670 DoSetItemImageFromData(item
, image
, which
);
672 // reset the normal/selected images because we won't use them any
673 // more - now they're stored inside the indirect data
675 imageSel
= I_IMAGECALLBACK
;
679 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
680 // change both normal and selected image - otherwise the change simply
681 // doesn't take place!
682 DoSetItemImages(item
, imageNormal
, imageSel
);
685 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
687 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
688 if ( !DoGetItem(&tvItem
) )
693 if ( HasIndirectData(item
) )
695 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
699 return (wxTreeItemData
*)tvItem
.lParam
;
703 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
705 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
707 if ( HasIndirectData(item
) )
709 if ( DoGetItem(&tvItem
) )
711 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
715 wxFAIL_MSG( wxT("failed to change tree items data") );
720 tvItem
.lParam
= (LPARAM
)data
;
725 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
726 wxTreeItemIndirectData
*data
)
728 // this should never happen because it's unnecessary and will probably lead
729 // to crash too because the code elsewhere supposes that the pointer the
730 // wxTreeItemIndirectData has is a real wxItemData and not
731 // wxTreeItemIndirectData as well
732 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
734 SetItemData(item
, (wxTreeItemData
*)data
);
736 m_itemsWithIndirectData
.Add(item
);
739 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
741 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
744 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
746 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
747 tvItem
.cChildren
= (int)has
;
751 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
753 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
754 tvItem
.state
= bold
? TVIS_BOLD
: 0;
758 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
760 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
761 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
765 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
770 long id
= (long)(WXHTREEITEM
)item
;
771 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
774 attr
= new wxTreeItemAttr
;
775 m_attrs
.Put(id
, (wxObject
*)attr
);
778 attr
->SetTextColour(col
);
781 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
786 long id
= (long)(WXHTREEITEM
)item
;
787 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
790 attr
= new wxTreeItemAttr
;
791 m_attrs
.Put(id
, (wxObject
*)attr
);
794 attr
->SetBackgroundColour(col
);
797 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
801 long id
= (long)(WXHTREEITEM
)item
;
802 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
805 attr
= new wxTreeItemAttr
;
806 m_attrs
.Put(id
, (wxObject
*)attr
);
812 // ----------------------------------------------------------------------------
814 // ----------------------------------------------------------------------------
816 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
818 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
821 // this ugliness comes directly from MSDN - it *is* the correct way to pass
822 // the HTREEITEM with TVM_GETITEMRECT
823 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
825 // FALSE means get item rect for the whole item, not only text
826 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
830 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
832 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
835 return tvItem
.cChildren
!= 0;
838 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
840 // probably not a good idea to put it here
841 //wxASSERT( ItemHasChildren(item) );
843 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
846 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
849 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
851 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
854 return (tvItem
.state
& TVIS_SELECTED
) != 0;
857 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
859 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
862 return (tvItem
.state
& TVIS_BOLD
) != 0;
865 // ----------------------------------------------------------------------------
867 // ----------------------------------------------------------------------------
869 wxTreeItemId
wxTreeCtrl::GetRootItem() const
871 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
874 wxTreeItemId
wxTreeCtrl::GetSelection() const
876 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
877 wxT("this only works with single selection controls") );
879 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
882 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
884 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
887 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
890 // remember the last child returned in 'cookie'
891 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
893 return wxTreeItemId((WXHTREEITEM
)_cookie
);
896 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
899 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
900 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
906 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
908 // can this be done more efficiently?
911 wxTreeItemId childLast
,
912 child
= GetFirstChild(item
, cookie
);
913 while ( child
.IsOk() )
916 child
= GetNextChild(item
, cookie
);
922 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
924 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
927 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
929 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
932 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
934 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
937 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
939 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
940 "for must be visible itself!"));
942 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
945 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
947 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
948 "for must be visible itself!"));
950 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
953 // ----------------------------------------------------------------------------
954 // multiple selections emulation
955 // ----------------------------------------------------------------------------
957 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
959 // receive the desired information.
960 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
963 // state image indices are 1 based
964 return ((tvItem
.state
>> 12) - 1) == 1;
967 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
969 // receive the desired information.
970 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
972 // state images are one-based
973 tvItem
.state
= (check
? 2 : 1) << 12;
978 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
980 TraverseSelections
selector(this, selections
);
982 return selector
.GetCount();
985 // ----------------------------------------------------------------------------
987 // ----------------------------------------------------------------------------
989 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
990 wxTreeItemId hInsertAfter
,
991 const wxString
& text
,
992 int image
, int selectedImage
,
993 wxTreeItemData
*data
)
995 TV_INSERTSTRUCT tvIns
;
996 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
997 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
999 // this is how we insert the item as the first child: supply a NULL
1001 if ( !tvIns
.hInsertAfter
)
1003 tvIns
.hInsertAfter
= TVI_FIRST
;
1007 if ( !text
.IsEmpty() )
1010 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1016 tvIns
.item
.iImage
= image
;
1018 if ( selectedImage
== -1 )
1020 // take the same image for selected icon if not specified
1021 selectedImage
= image
;
1025 if ( selectedImage
!= -1 )
1027 mask
|= TVIF_SELECTEDIMAGE
;
1028 tvIns
.item
.iSelectedImage
= selectedImage
;
1034 tvIns
.item
.lParam
= (LPARAM
)data
;
1037 tvIns
.item
.mask
= mask
;
1039 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1042 wxLogLastError("TreeView_InsertItem");
1047 // associate the application tree item with Win32 tree item handle
1048 data
->SetId((WXHTREEITEM
)id
);
1051 return wxTreeItemId((WXHTREEITEM
)id
);
1054 // for compatibility only
1055 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1056 const wxString
& text
,
1057 int image
, int selImage
,
1060 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1061 image
, selImage
, NULL
);
1064 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1065 int image
, int selectedImage
,
1066 wxTreeItemData
*data
)
1068 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1069 text
, image
, selectedImage
, data
);
1072 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1073 const wxString
& text
,
1074 int image
, int selectedImage
,
1075 wxTreeItemData
*data
)
1077 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1078 text
, image
, selectedImage
, data
);
1081 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1082 const wxTreeItemId
& idPrevious
,
1083 const wxString
& text
,
1084 int image
, int selectedImage
,
1085 wxTreeItemData
*data
)
1087 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1090 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1092 const wxString
& text
,
1093 int image
, int selectedImage
,
1094 wxTreeItemData
*data
)
1096 // find the item from index
1098 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1099 while ( index
!= 0 && idCur
.IsOk() )
1104 idCur
= GetNextChild(parent
, cookie
);
1107 // assert, not check: if the index is invalid, we will append the item
1109 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1111 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1114 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1115 const wxString
& text
,
1116 int image
, int selectedImage
,
1117 wxTreeItemData
*data
)
1119 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1120 text
, image
, selectedImage
, data
);
1123 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1125 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
1127 wxLogLastError("TreeView_DeleteItem");
1131 // delete all children (but don't delete the item itself)
1132 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1136 wxArrayLong children
;
1137 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1138 while ( child
.IsOk() )
1140 children
.Add((long)(WXHTREEITEM
)child
);
1142 child
= GetNextChild(item
, cookie
);
1145 size_t nCount
= children
.Count();
1146 for ( size_t n
= 0; n
< nCount
; n
++ )
1148 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1150 wxLogLastError("TreeView_DeleteItem");
1155 void wxTreeCtrl::DeleteAllItems()
1157 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1159 wxLogLastError("TreeView_DeleteAllItems");
1163 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1165 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1166 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1167 flag
== TVE_EXPAND
||
1169 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1171 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1172 // emulate them. This behaviour has changed slightly with comctl32.dll
1173 // v 4.70 - now it does send them but only the first time. To maintain
1174 // compatible behaviour and also in order to not have surprises with the
1175 // future versions, don't rely on this and still do everything ourselves.
1176 // To avoid that the messages be sent twice when the item is expanded for
1177 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1179 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1183 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
1185 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1186 event
.m_item
= item
;
1188 bool isExpanded
= IsExpanded(item
);
1190 event
.SetEventObject(this);
1192 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1193 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1194 GetEventHandler()->ProcessEvent(event
);
1196 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1197 GetEventHandler()->ProcessEvent(event
);
1199 //else: change didn't took place, so do nothing at all
1202 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1204 DoExpand(item
, TVE_EXPAND
);
1207 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1209 DoExpand(item
, TVE_COLLAPSE
);
1212 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1214 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1217 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1219 DoExpand(item
, TVE_TOGGLE
);
1222 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1224 DoExpand(item
, action
);
1227 void wxTreeCtrl::Unselect()
1229 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxT("doesn't make sense") );
1231 // just remove the selection
1232 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1235 void wxTreeCtrl::UnselectAll()
1237 if ( m_windowStyle
& wxTR_MULTIPLE
)
1239 wxArrayTreeItemIds selections
;
1240 size_t count
= GetSelections(selections
);
1241 for ( size_t n
= 0; n
< count
; n
++ )
1243 SetItemCheck(selections
[n
], FALSE
);
1248 // just remove the selection
1253 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1255 if ( m_windowStyle
& wxTR_MULTIPLE
)
1257 // selecting the item means checking it
1262 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1263 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1264 // send them ourselves
1266 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1267 event
.m_item
= item
;
1268 event
.SetEventObject(this);
1270 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1271 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1273 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1275 wxLogLastError("TreeView_SelectItem");
1279 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1280 (void)GetEventHandler()->ProcessEvent(event
);
1283 //else: program vetoed the change
1287 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1290 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1293 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1295 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1297 wxLogLastError("TreeView_SelectSetFirstVisible");
1301 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1306 void wxTreeCtrl::DeleteTextCtrl()
1310 m_textCtrl
->UnsubclassWin();
1311 m_textCtrl
->SetHWND(0);
1317 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1318 wxClassInfo
* textControlClass
)
1320 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1322 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1324 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1333 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1334 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1335 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1340 // End label editing, optionally cancelling the edit
1341 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1343 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1348 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1350 TV_HITTESTINFO hitTestInfo
;
1351 hitTestInfo
.pt
.x
= (int)point
.x
;
1352 hitTestInfo
.pt
.y
= (int)point
.y
;
1354 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1359 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1360 flags |= wxTREE_HITTEST_##flag
1362 TRANSLATE_FLAG(ABOVE
);
1363 TRANSLATE_FLAG(BELOW
);
1364 TRANSLATE_FLAG(NOWHERE
);
1365 TRANSLATE_FLAG(ONITEMBUTTON
);
1366 TRANSLATE_FLAG(ONITEMICON
);
1367 TRANSLATE_FLAG(ONITEMINDENT
);
1368 TRANSLATE_FLAG(ONITEMLABEL
);
1369 TRANSLATE_FLAG(ONITEMRIGHT
);
1370 TRANSLATE_FLAG(ONITEMSTATEICON
);
1371 TRANSLATE_FLAG(TOLEFT
);
1372 TRANSLATE_FLAG(TORIGHT
);
1374 #undef TRANSLATE_FLAG
1376 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1379 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1381 bool textOnly
) const
1384 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1387 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1393 // couldn't retrieve rect: for example, item isn't visible
1398 // ----------------------------------------------------------------------------
1400 // ----------------------------------------------------------------------------
1402 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1403 wxTreeItemData
*pItem2
,
1406 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1407 wxT("sorting tree without data doesn't make sense") );
1409 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1412 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1413 const wxTreeItemId
& item2
)
1415 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1418 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1420 // rely on the fact that TreeView_SortChildren does the same thing as our
1421 // default behaviour, i.e. sorts items alphabetically and so call it
1422 // directly if we're not in derived class (much more efficient!)
1423 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1425 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1430 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1431 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1432 tvSort
.lParam
= (LPARAM
)this;
1433 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1437 // ----------------------------------------------------------------------------
1439 // ----------------------------------------------------------------------------
1441 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1443 if ( cmd
== EN_UPDATE
)
1445 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1446 event
.SetEventObject( this );
1447 ProcessCommand(event
);
1449 else if ( cmd
== EN_KILLFOCUS
)
1451 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1452 event
.SetEventObject( this );
1453 ProcessCommand(event
);
1461 // command processed
1465 // process WM_NOTIFY Windows message
1466 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1468 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1469 wxEventType eventType
= wxEVT_NULL
;
1470 NMHDR
*hdr
= (NMHDR
*)lParam
;
1472 switch ( hdr
->code
)
1476 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1479 TV_HITTESTINFO tvhti
;
1480 ::GetCursorPos(&(tvhti
.pt
));
1481 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1482 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1484 if( tvhti
.flags
& TVHT_ONITEM
)
1486 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1487 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1494 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1497 case TVN_BEGINRDRAG
:
1499 if ( eventType
== wxEVT_NULL
)
1500 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1501 //else: left drag, already set above
1503 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1505 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1506 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1510 case TVN_BEGINLABELEDIT
:
1512 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1513 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1515 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1516 event
.m_label
= info
->item
.pszText
;
1520 case TVN_DELETEITEM
:
1522 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1523 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1525 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1529 delete (wxTreeItemAttr
*)m_attrs
.
1530 Delete((long)tv
->itemOld
.hItem
);
1535 case TVN_ENDLABELEDIT
:
1537 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1538 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1540 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1541 event
.m_label
= info
->item
.pszText
;
1542 if (info
->item
.pszText
== NULL
)
1547 case TVN_GETDISPINFO
:
1548 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1551 case TVN_SETDISPINFO
:
1553 if ( eventType
== wxEVT_NULL
)
1554 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1555 //else: get, already set above
1557 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1559 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1563 case TVN_ITEMEXPANDING
:
1564 event
.m_code
= FALSE
;
1567 case TVN_ITEMEXPANDED
:
1569 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1571 bool expand
= FALSE
;
1572 switch ( tv
->action
)
1583 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1584 "message"), tv
->action
);
1587 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
1588 eventType
= g_events
[expand
][ing
];
1590 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1596 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1597 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1599 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1601 // a separate event for this case
1602 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1604 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1606 event2
.SetEventObject(this);
1608 GetEventHandler()->ProcessEvent(event2
);
1613 case TVN_SELCHANGED
:
1614 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1617 case TVN_SELCHANGING
:
1619 if ( eventType
== wxEVT_NULL
)
1620 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1621 //else: already set above
1623 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1625 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1626 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1630 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1633 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
1634 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
1635 switch( nmcd
.dwDrawStage
)
1638 // if we've got any items with non standard attributes,
1639 // notify us before painting each item
1640 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1644 case CDDS_ITEMPREPAINT
:
1646 wxTreeItemAttr
*attr
=
1647 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1651 // nothing to do for this item
1652 return CDRF_DODEFAULT
;
1656 wxColour colText
, colBack
;
1657 if ( attr
->HasFont() )
1659 wxFont font
= attr
->GetFont();
1660 hFont
= (HFONT
)font
.GetResourceHandle();
1667 if ( attr
->HasTextColour() )
1669 colText
= attr
->GetTextColour();
1673 colText
= GetForegroundColour();
1676 // selection colours should override ours
1677 if ( nmcd
.uItemState
& CDIS_SELECTED
)
1679 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
1680 lptvcd
->clrTextBk
= clrBk
;
1682 // try to make the text visible
1683 lptvcd
->clrText
= wxColourToRGB(colText
);
1684 lptvcd
->clrText
|= ~clrBk
;
1685 lptvcd
->clrText
&= 0x00ffffff;
1689 if ( attr
->HasBackgroundColour() )
1691 colBack
= attr
->GetBackgroundColour();
1695 colBack
= GetBackgroundColour();
1698 lptvcd
->clrText
= wxColourToRGB(colText
);
1699 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
1702 // note that if we wanted to set colours for
1703 // individual columns (subitems), we would have
1704 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1707 ::SelectObject(nmcd
.hdc
, hFont
);
1709 *result
= CDRF_NEWFONT
;
1713 *result
= CDRF_DODEFAULT
;
1720 *result
= CDRF_DODEFAULT
;
1725 #endif // _WIN32_IE >= 0x300
1728 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1731 event
.SetEventObject(this);
1732 event
.SetEventType(eventType
);
1734 bool processed
= GetEventHandler()->ProcessEvent(event
);
1737 switch ( hdr
->code
)
1739 case TVN_DELETEITEM
:
1741 // NB: we might process this message using wxWindows event
1742 // tables, but due to overhead of wxWin event system we
1743 // prefer to do it here ourself (otherwise deleting a tree
1744 // with many items is just too slow)
1745 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1747 wxTreeItemId item
= event
.m_item
;
1748 if ( HasIndirectData(item
) )
1750 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
1752 delete data
; // can't be NULL here
1754 m_itemsWithIndirectData
.Remove(item
);
1758 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1759 delete data
; // may be NULL, ok
1762 processed
= TRUE
; // Make sure we don't get called twice
1766 case TVN_BEGINLABELEDIT
:
1767 // return TRUE to cancel label editing
1768 *result
= !event
.IsAllowed();
1771 case TVN_ENDLABELEDIT
:
1772 // return TRUE to set the label to the new string
1773 *result
= event
.IsAllowed();
1775 // ensure that we don't have the text ctrl which is going to be
1780 case TVN_SELCHANGING
:
1781 case TVN_ITEMEXPANDING
:
1782 // return TRUE to prevent the action from happening
1783 *result
= !event
.IsAllowed();
1786 case TVN_GETDISPINFO
:
1787 // NB: so far the user can't set the image himself anyhow, so do it
1788 // anyway - but this may change later
1789 if ( /* !processed && */ 1 )
1791 wxTreeItemId item
= event
.m_item
;
1792 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1793 if ( info
->item
.mask
& TVIF_IMAGE
)
1796 DoGetItemImageFromData
1799 IsExpanded(item
) ? wxTreeItemIcon_Expanded
1800 : wxTreeItemIcon_Normal
1803 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
1805 info
->item
.iSelectedImage
=
1806 DoGetItemImageFromData
1809 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
1810 : wxTreeItemIcon_Selected
1817 // for the other messages the return value is ignored and there is
1818 // nothing special to do
1824 // ----------------------------------------------------------------------------
1826 // ----------------------------------------------------------------------------
1828 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1830 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1831 : wxNotifyEvent(commandType
, id
)