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/msw/private.h"
32 // Mingw32 is a bit mental even though this is done in winundef
41 #if defined(__WIN95__)
44 #include "wx/dynarray.h"
45 #include "wx/imaglist.h"
46 #include "wx/treectrl.h"
47 #include "wx/settings.h"
49 #include "wx/msw/dragimag.h"
51 #ifdef __GNUWIN32_OLD__
52 #include "wx/msw/gnuwin32/extra.h"
55 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
59 // Bug in headers, sometimes
61 #define TVIS_FOCUSED 0x0001
65 #define TV_FIRST 0x1100
68 // old headers might miss these messages (comctl32.dll 4.71+ only)
69 #ifndef TVM_SETBKCOLOR
70 #define TVM_SETBKCOLOR (TV_FIRST + 29)
71 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 // a convenient wrapper around TV_ITEM struct which adds a ctor
80 #pragma warning( disable : 4097 )
83 struct wxTreeViewItem
: public TV_ITEM
85 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
86 UINT mask_
, // fields which are valid
87 UINT stateMask_
= 0) // for TVIF_STATE only
89 // hItem member is always valid
90 mask
= mask_
| TVIF_HANDLE
;
91 stateMask
= stateMask_
;
92 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
97 #pragma warning( default : 4097 )
100 // a class which encapsulates the tree traversal logic: it vists all (unless
101 // OnVisit() returns FALSE) items under the given one
102 class wxTreeTraversal
105 wxTreeTraversal(const wxTreeCtrl
*tree
)
110 // do traverse the tree: visit all items (recursively by default) under the
111 // given one; return TRUE if all items were traversed or FALSE if the
112 // traversal was aborted because OnVisit returned FALSE
113 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
115 // override this function to do whatever is needed for each item, return
116 // FALSE to stop traversing
117 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
120 const wxTreeCtrl
*GetTree() const { return m_tree
; }
123 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
125 const wxTreeCtrl
*m_tree
;
128 // internal class for getting the selected items
129 class TraverseSelections
: public wxTreeTraversal
132 TraverseSelections(const wxTreeCtrl
*tree
,
133 wxArrayTreeItemIds
& selections
)
134 : wxTreeTraversal(tree
), m_selections(selections
)
136 m_selections
.Empty();
138 DoTraverse(tree
->GetRootItem());
141 virtual bool OnVisit(const wxTreeItemId
& item
)
143 if ( GetTree()->IsItemChecked(item
) )
145 m_selections
.Add(item
);
151 size_t GetCount() const { return m_selections
.GetCount(); }
154 wxArrayTreeItemIds
& m_selections
;
157 // internal class for counting tree items
158 class TraverseCounter
: public wxTreeTraversal
161 TraverseCounter(const wxTreeCtrl
*tree
,
162 const wxTreeItemId
& root
,
164 : wxTreeTraversal(tree
)
168 DoTraverse(root
, recursively
);
171 virtual bool OnVisit(const wxTreeItemId
& item
)
178 size_t GetCount() const { return m_count
; }
184 // ----------------------------------------------------------------------------
185 // This class is needed for support of different images: the Win32 common
186 // control natively supports only 2 images (the normal one and another for the
187 // selected state). We wish to provide support for 2 more of them for folder
188 // items (i.e. those which have children): for expanded state and for expanded
189 // selected state. For this we use this structure to store the additional items
192 // There is only one problem with this: when we retrieve the item's data, we
193 // don't know whether we get a pointer to wxTreeItemData or
194 // wxTreeItemIndirectData. So we have to maintain a list of all items which
195 // have indirect data inside the listctrl itself.
196 // ----------------------------------------------------------------------------
198 class wxTreeItemIndirectData
201 // ctor associates this data with the item and the real item data becomes
202 // available through our GetData() method
203 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
205 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
211 m_data
= tree
->GetItemData(item
);
213 // and set ourselves as the new one
214 tree
->SetIndirectItemData(item
, this);
217 // dtor deletes the associated data as well
218 ~wxTreeItemIndirectData() { delete m_data
; }
221 // get the real data associated with the item
222 wxTreeItemData
*GetData() const { return m_data
; }
224 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
226 // do we have such image?
227 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
229 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
231 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
234 // all the images associated with the item
235 int m_images
[wxTreeItemIcon_Max
];
237 wxTreeItemData
*m_data
;
240 // ----------------------------------------------------------------------------
242 // ----------------------------------------------------------------------------
244 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
250 return TreeView_HitTest(hwndTV
, &tvht
);
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 // handy table for sending events
264 static const wxEventType g_events
[2][2] =
266 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
267 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
270 // ============================================================================
272 // ============================================================================
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
280 if ( !OnVisit(root
) )
283 return Traverse(root
, recursively
);
286 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
289 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
290 while ( child
.IsOk() )
292 // depth first traversal
293 if ( recursively
&& !Traverse(child
, TRUE
) )
296 if ( !OnVisit(child
) )
299 child
= m_tree
->GetNextChild(root
, cookie
);
305 // ----------------------------------------------------------------------------
306 // construction and destruction
307 // ----------------------------------------------------------------------------
309 void wxTreeCtrl::Init()
311 m_imageListNormal
= NULL
;
312 m_imageListState
= NULL
;
314 m_hasAnyAttr
= FALSE
;
318 bool wxTreeCtrl::Create(wxWindow
*parent
,
323 const wxValidator
& validator
,
324 const wxString
& name
)
328 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
331 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
332 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
334 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
335 wstyle
|= TVS_HASBUTTONS
;
337 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
338 wstyle
|= TVS_EDITLABELS
;
340 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
341 wstyle
|= TVS_LINESATROOT
;
343 #if !defined( __GNUWIN32_OLD__ ) && \
344 !defined( __BORLANDC__ ) && \
345 !defined( __WATCOMC__ ) && \
346 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
348 #ifndef TVS_CHECKBOXES
349 #define TVS_CHECKBOXES 0x0100
352 // we emulate the multiple selection tree controls by using checkboxes: set
353 // up the image list we need for this if we do have multiple selections
354 if ( m_windowStyle
& wxTR_MULTIPLE
)
355 wstyle
|= TVS_CHECKBOXES
;
358 // Create the tree control.
359 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
362 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
363 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
365 // VZ: this is some experimental code which may be used to get the
366 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
367 // AFAIK, the standard DLL does about the same thing anyhow.
369 if ( m_windowStyle
& wxTR_MULTIPLE
)
373 // create the DC compatible with the current screen
374 HDC hdcMem
= CreateCompatibleDC(NULL
);
376 // create a mono bitmap of the standard size
377 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
378 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
379 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
380 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
381 1, // # of color planes
382 1, // # bits needed for one pixel
383 0); // array containing colour data
384 SelectObject(hdcMem
, hbmpCheck
);
386 // then draw a check mark into it
387 RECT rect
= { 0, 0, x
, y
};
388 if ( !::DrawFrameControl(hdcMem
, &rect
,
390 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
392 wxLogLastError(wxT("DrawFrameControl(check)"));
395 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
396 imagelistCheckboxes
.Add(bmp
);
398 if ( !::DrawFrameControl(hdcMem
, &rect
,
402 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
405 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
406 imagelistCheckboxes
.Add(bmp
);
412 SetStateImageList(&imagelistCheckboxes
);
416 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
421 wxTreeCtrl::~wxTreeCtrl()
423 // delete any attributes
426 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
428 delete (wxTreeItemAttr
*)node
->Data();
431 // prevent TVN_DELETEITEM handler from deleting the attributes again!
432 m_hasAnyAttr
= FALSE
;
437 // delete user data to prevent memory leaks
441 // ----------------------------------------------------------------------------
443 // ----------------------------------------------------------------------------
445 // simple wrappers which add error checking in debug mode
447 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
449 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
451 wxLogLastError("TreeView_GetItem");
459 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
461 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
463 wxLogLastError("TreeView_SetItem");
467 size_t wxTreeCtrl::GetCount() const
469 return (size_t)TreeView_GetCount(GetHwnd());
472 unsigned int wxTreeCtrl::GetIndent() const
474 return TreeView_GetIndent(GetHwnd());
477 void wxTreeCtrl::SetIndent(unsigned int indent
)
479 TreeView_SetIndent(GetHwnd(), indent
);
482 wxImageList
*wxTreeCtrl::GetImageList() const
484 return m_imageListNormal
;
487 wxImageList
*wxTreeCtrl::GetStateImageList() const
489 return m_imageListNormal
;
492 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
495 TreeView_SetImageList(GetHwnd(),
496 imageList
? imageList
->GetHIMAGELIST() : 0,
500 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
502 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
505 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
507 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
510 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
511 bool recursively
) const
513 TraverseCounter
counter(this, item
, recursively
);
515 return counter
.GetCount() - 1;
518 // ----------------------------------------------------------------------------
520 // ----------------------------------------------------------------------------
522 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
524 if ( !wxWindowBase::SetBackgroundColour(colour
) )
527 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
532 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
534 if ( !wxWindowBase::SetForegroundColour(colour
) )
537 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
548 wxChar buf
[512]; // the size is arbitrary...
550 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
551 tvItem
.pszText
= buf
;
552 tvItem
.cchTextMax
= WXSIZEOF(buf
);
553 if ( !DoGetItem(&tvItem
) )
555 // don't return some garbage which was on stack, but an empty string
559 return wxString(buf
);
562 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
564 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
565 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
569 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
570 wxTreeItemIcon which
) const
572 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
573 if ( !DoGetItem(&tvItem
) )
578 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
581 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
583 wxTreeItemIcon which
) const
585 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
586 if ( !DoGetItem(&tvItem
) )
591 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
593 data
->SetImage(image
, which
);
595 // make sure that we have selected images as well
596 if ( which
== wxTreeItemIcon_Normal
&&
597 !data
->HasImage(wxTreeItemIcon_Selected
) )
599 data
->SetImage(image
, wxTreeItemIcon_Selected
);
602 if ( which
== wxTreeItemIcon_Expanded
&&
603 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
605 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
609 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
613 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
614 tvItem
.iSelectedImage
= imageSel
;
615 tvItem
.iImage
= image
;
619 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
620 wxTreeItemIcon which
) const
622 if ( HasIndirectData(item
) )
624 return DoGetItemImageFromData(item
, which
);
631 wxFAIL_MSG( wxT("unknown tree item image type") );
633 case wxTreeItemIcon_Normal
:
637 case wxTreeItemIcon_Selected
:
638 mask
= TVIF_SELECTEDIMAGE
;
641 case wxTreeItemIcon_Expanded
:
642 case wxTreeItemIcon_SelectedExpanded
:
646 wxTreeViewItem
tvItem(item
, mask
);
649 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
652 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
653 wxTreeItemIcon which
)
655 int imageNormal
, imageSel
;
659 wxFAIL_MSG( wxT("unknown tree item image type") );
661 case wxTreeItemIcon_Normal
:
663 imageSel
= GetItemSelectedImage(item
);
666 case wxTreeItemIcon_Selected
:
667 imageNormal
= GetItemImage(item
);
671 case wxTreeItemIcon_Expanded
:
672 case wxTreeItemIcon_SelectedExpanded
:
673 if ( !HasIndirectData(item
) )
675 // we need to get the old images first, because after we create
676 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
678 imageNormal
= GetItemImage(item
);
679 imageSel
= GetItemSelectedImage(item
);
681 // if it doesn't have it yet, add it
682 wxTreeItemIndirectData
*data
= new
683 wxTreeItemIndirectData(this, item
);
685 // copy the data to the new location
686 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
687 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
690 DoSetItemImageFromData(item
, image
, which
);
692 // reset the normal/selected images because we won't use them any
693 // more - now they're stored inside the indirect data
695 imageSel
= I_IMAGECALLBACK
;
699 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
700 // change both normal and selected image - otherwise the change simply
701 // doesn't take place!
702 DoSetItemImages(item
, imageNormal
, imageSel
);
705 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
707 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
708 if ( !DoGetItem(&tvItem
) )
713 if ( HasIndirectData(item
) )
715 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
719 return (wxTreeItemData
*)tvItem
.lParam
;
723 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
725 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
727 if ( HasIndirectData(item
) )
729 if ( DoGetItem(&tvItem
) )
731 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
735 wxFAIL_MSG( wxT("failed to change tree items data") );
740 tvItem
.lParam
= (LPARAM
)data
;
745 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
746 wxTreeItemIndirectData
*data
)
748 // this should never happen because it's unnecessary and will probably lead
749 // to crash too because the code elsewhere supposes that the pointer the
750 // wxTreeItemIndirectData has is a real wxItemData and not
751 // wxTreeItemIndirectData as well
752 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
754 SetItemData(item
, (wxTreeItemData
*)data
);
756 m_itemsWithIndirectData
.Add(item
);
759 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
761 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
764 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
766 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
767 tvItem
.cChildren
= (int)has
;
771 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
773 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
774 tvItem
.state
= bold
? TVIS_BOLD
: 0;
778 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
780 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
781 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
785 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
790 long id
= (long)(WXHTREEITEM
)item
;
791 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
794 attr
= new wxTreeItemAttr
;
795 m_attrs
.Put(id
, (wxObject
*)attr
);
798 attr
->SetTextColour(col
);
801 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
806 long id
= (long)(WXHTREEITEM
)item
;
807 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
810 attr
= new wxTreeItemAttr
;
811 m_attrs
.Put(id
, (wxObject
*)attr
);
814 attr
->SetBackgroundColour(col
);
817 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
821 long id
= (long)(WXHTREEITEM
)item
;
822 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
825 attr
= new wxTreeItemAttr
;
826 m_attrs
.Put(id
, (wxObject
*)attr
);
832 // ----------------------------------------------------------------------------
834 // ----------------------------------------------------------------------------
836 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
838 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
841 // this ugliness comes directly from MSDN - it *is* the correct way to pass
842 // the HTREEITEM with TVM_GETITEMRECT
843 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
845 // FALSE means get item rect for the whole item, not only text
846 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
850 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
852 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
855 return tvItem
.cChildren
!= 0;
858 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
860 // probably not a good idea to put it here
861 //wxASSERT( ItemHasChildren(item) );
863 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
866 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
869 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
871 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
874 return (tvItem
.state
& TVIS_SELECTED
) != 0;
877 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
879 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
882 return (tvItem
.state
& TVIS_BOLD
) != 0;
885 // ----------------------------------------------------------------------------
887 // ----------------------------------------------------------------------------
889 wxTreeItemId
wxTreeCtrl::GetRootItem() const
891 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
894 wxTreeItemId
wxTreeCtrl::GetSelection() const
896 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
897 wxT("this only works with single selection controls") );
899 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
902 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
904 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
907 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
910 // remember the last child returned in 'cookie'
911 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
913 return wxTreeItemId((WXHTREEITEM
)_cookie
);
916 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
919 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
920 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
926 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
928 // can this be done more efficiently?
931 wxTreeItemId childLast
,
932 child
= GetFirstChild(item
, cookie
);
933 while ( child
.IsOk() )
936 child
= GetNextChild(item
, cookie
);
942 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
944 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
947 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
949 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
952 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
954 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
957 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
959 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
960 "for must be visible itself!"));
962 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
965 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
967 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
968 "for must be visible itself!"));
970 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
973 // ----------------------------------------------------------------------------
974 // multiple selections emulation
975 // ----------------------------------------------------------------------------
977 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
979 // receive the desired information.
980 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
983 // state image indices are 1 based
984 return ((tvItem
.state
>> 12) - 1) == 1;
987 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
989 // receive the desired information.
990 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
992 // state images are one-based
993 tvItem
.state
= (check
? 2 : 1) << 12;
998 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1000 TraverseSelections
selector(this, selections
);
1002 return selector
.GetCount();
1005 // ----------------------------------------------------------------------------
1007 // ----------------------------------------------------------------------------
1009 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1010 wxTreeItemId hInsertAfter
,
1011 const wxString
& text
,
1012 int image
, int selectedImage
,
1013 wxTreeItemData
*data
)
1015 TV_INSERTSTRUCT tvIns
;
1016 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
1017 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
1019 // this is how we insert the item as the first child: supply a NULL
1021 if ( !tvIns
.hInsertAfter
)
1023 tvIns
.hInsertAfter
= TVI_FIRST
;
1027 if ( !text
.IsEmpty() )
1030 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1036 tvIns
.item
.iImage
= image
;
1038 if ( selectedImage
== -1 )
1040 // take the same image for selected icon if not specified
1041 selectedImage
= image
;
1045 if ( selectedImage
!= -1 )
1047 mask
|= TVIF_SELECTEDIMAGE
;
1048 tvIns
.item
.iSelectedImage
= selectedImage
;
1054 tvIns
.item
.lParam
= (LPARAM
)data
;
1057 tvIns
.item
.mask
= mask
;
1059 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1062 wxLogLastError("TreeView_InsertItem");
1067 // associate the application tree item with Win32 tree item handle
1068 data
->SetId((WXHTREEITEM
)id
);
1071 return wxTreeItemId((WXHTREEITEM
)id
);
1074 // for compatibility only
1075 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1076 const wxString
& text
,
1077 int image
, int selImage
,
1080 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1081 image
, selImage
, NULL
);
1084 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1085 int image
, int selectedImage
,
1086 wxTreeItemData
*data
)
1088 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1089 text
, image
, selectedImage
, data
);
1092 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1093 const wxString
& text
,
1094 int image
, int selectedImage
,
1095 wxTreeItemData
*data
)
1097 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1098 text
, image
, selectedImage
, data
);
1101 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1102 const wxTreeItemId
& idPrevious
,
1103 const wxString
& text
,
1104 int image
, int selectedImage
,
1105 wxTreeItemData
*data
)
1107 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1110 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1112 const wxString
& text
,
1113 int image
, int selectedImage
,
1114 wxTreeItemData
*data
)
1116 // find the item from index
1118 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1119 while ( index
!= 0 && idCur
.IsOk() )
1124 idCur
= GetNextChild(parent
, cookie
);
1127 // assert, not check: if the index is invalid, we will append the item
1129 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1131 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1134 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1135 const wxString
& text
,
1136 int image
, int selectedImage
,
1137 wxTreeItemData
*data
)
1139 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1140 text
, image
, selectedImage
, data
);
1143 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1145 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
1147 wxLogLastError("TreeView_DeleteItem");
1151 // delete all children (but don't delete the item itself)
1152 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1156 wxArrayLong children
;
1157 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1158 while ( child
.IsOk() )
1160 children
.Add((long)(WXHTREEITEM
)child
);
1162 child
= GetNextChild(item
, cookie
);
1165 size_t nCount
= children
.Count();
1166 for ( size_t n
= 0; n
< nCount
; n
++ )
1168 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1170 wxLogLastError("TreeView_DeleteItem");
1175 void wxTreeCtrl::DeleteAllItems()
1177 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1179 wxLogLastError("TreeView_DeleteAllItems");
1183 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1185 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1186 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1187 flag
== TVE_EXPAND
||
1189 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1191 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1192 // emulate them. This behaviour has changed slightly with comctl32.dll
1193 // v 4.70 - now it does send them but only the first time. To maintain
1194 // compatible behaviour and also in order to not have surprises with the
1195 // future versions, don't rely on this and still do everything ourselves.
1196 // To avoid that the messages be sent twice when the item is expanded for
1197 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1199 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1203 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
1205 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1206 event
.m_item
= item
;
1208 bool isExpanded
= IsExpanded(item
);
1210 event
.SetEventObject(this);
1212 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1213 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1214 GetEventHandler()->ProcessEvent(event
);
1216 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1217 GetEventHandler()->ProcessEvent(event
);
1219 //else: change didn't took place, so do nothing at all
1222 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1224 DoExpand(item
, TVE_EXPAND
);
1227 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1229 DoExpand(item
, TVE_COLLAPSE
);
1232 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1234 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1237 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1239 DoExpand(item
, TVE_TOGGLE
);
1242 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1244 DoExpand(item
, action
);
1247 void wxTreeCtrl::Unselect()
1249 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxT("doesn't make sense") );
1251 // just remove the selection
1252 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1255 void wxTreeCtrl::UnselectAll()
1257 if ( m_windowStyle
& wxTR_MULTIPLE
)
1259 wxArrayTreeItemIds selections
;
1260 size_t count
= GetSelections(selections
);
1261 for ( size_t n
= 0; n
< count
; n
++ )
1263 SetItemCheck(selections
[n
], FALSE
);
1268 // just remove the selection
1273 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1275 if ( m_windowStyle
& wxTR_MULTIPLE
)
1277 // selecting the item means checking it
1282 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1283 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1284 // send them ourselves
1286 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1287 event
.m_item
= item
;
1288 event
.SetEventObject(this);
1290 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1291 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1293 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1295 wxLogLastError("TreeView_SelectItem");
1299 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1300 (void)GetEventHandler()->ProcessEvent(event
);
1303 //else: program vetoed the change
1307 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1310 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1313 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1315 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1317 wxLogLastError("TreeView_SelectSetFirstVisible");
1321 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1326 void wxTreeCtrl::DeleteTextCtrl()
1330 m_textCtrl
->UnsubclassWin();
1331 m_textCtrl
->SetHWND(0);
1337 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1338 wxClassInfo
* textControlClass
)
1340 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1342 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1344 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1353 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1354 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1355 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1360 // End label editing, optionally cancelling the edit
1361 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1363 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1368 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1370 TV_HITTESTINFO hitTestInfo
;
1371 hitTestInfo
.pt
.x
= (int)point
.x
;
1372 hitTestInfo
.pt
.y
= (int)point
.y
;
1374 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1379 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1380 flags |= wxTREE_HITTEST_##flag
1382 TRANSLATE_FLAG(ABOVE
);
1383 TRANSLATE_FLAG(BELOW
);
1384 TRANSLATE_FLAG(NOWHERE
);
1385 TRANSLATE_FLAG(ONITEMBUTTON
);
1386 TRANSLATE_FLAG(ONITEMICON
);
1387 TRANSLATE_FLAG(ONITEMINDENT
);
1388 TRANSLATE_FLAG(ONITEMLABEL
);
1389 TRANSLATE_FLAG(ONITEMRIGHT
);
1390 TRANSLATE_FLAG(ONITEMSTATEICON
);
1391 TRANSLATE_FLAG(TOLEFT
);
1392 TRANSLATE_FLAG(TORIGHT
);
1394 #undef TRANSLATE_FLAG
1396 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1399 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1401 bool textOnly
) const
1404 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1407 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1413 // couldn't retrieve rect: for example, item isn't visible
1418 // ----------------------------------------------------------------------------
1420 // ----------------------------------------------------------------------------
1422 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1423 wxTreeItemData
*pItem2
,
1426 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1427 wxT("sorting tree without data doesn't make sense") );
1429 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1432 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1433 const wxTreeItemId
& item2
)
1435 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1438 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1440 // rely on the fact that TreeView_SortChildren does the same thing as our
1441 // default behaviour, i.e. sorts items alphabetically and so call it
1442 // directly if we're not in derived class (much more efficient!)
1443 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1445 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1450 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1451 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1452 tvSort
.lParam
= (LPARAM
)this;
1453 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1457 // ----------------------------------------------------------------------------
1459 // ----------------------------------------------------------------------------
1461 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1463 if ( cmd
== EN_UPDATE
)
1465 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1466 event
.SetEventObject( this );
1467 ProcessCommand(event
);
1469 else if ( cmd
== EN_KILLFOCUS
)
1471 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1472 event
.SetEventObject( this );
1473 ProcessCommand(event
);
1481 // command processed
1485 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1486 // only do it during dragging, minimize wxWin overhead (this is important for
1487 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1488 // instead of passing by wxWin events
1489 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1497 int x
= GET_X_LPARAM(lParam
),
1498 y
= GET_Y_LPARAM(lParam
);
1500 m_dragImage
->Move(wxPoint(x
, y
), this);
1502 HTREEITEM htiTarget
= GetItemFromPoint(GetHwnd(), x
, y
);
1505 // highlight the item as target (hiding drag image is
1506 // necessary - otherwise the display will be corrupted)
1507 m_dragImage
->Hide(this);
1508 TreeView_SelectDropTarget(GetHwnd(), htiTarget
);
1509 m_dragImage
->Show(this);
1517 m_dragImage
->EndDrag(this);
1521 // generate the drag end event
1522 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1524 int x
= GET_X_LPARAM(lParam
),
1525 y
= GET_Y_LPARAM(lParam
);
1528 = (WXHTREEITEM
)GetItemFromPoint(GetHwnd(), x
, y
);
1529 event
.m_pointDrag
= wxPoint(x
, y
);
1530 event
.SetEventObject(this);
1532 (void)GetEventHandler()->ProcessEvent(event
);
1538 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1541 // process WM_NOTIFY Windows message
1542 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1544 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1545 wxEventType eventType
= wxEVT_NULL
;
1546 NMHDR
*hdr
= (NMHDR
*)lParam
;
1548 switch ( hdr
->code
)
1552 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1555 TV_HITTESTINFO tvhti
;
1556 ::GetCursorPos(&(tvhti
.pt
));
1557 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1558 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1560 if( tvhti
.flags
& TVHT_ONITEM
)
1562 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1563 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1570 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1573 case TVN_BEGINRDRAG
:
1575 if ( eventType
== wxEVT_NULL
)
1576 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1577 //else: left drag, already set above
1579 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1581 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1582 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1584 // don't allow dragging by default: the user code must
1585 // explicitly say that it wants to allow it to avoid breaking
1591 case TVN_BEGINLABELEDIT
:
1593 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1594 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1596 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1597 event
.m_label
= info
->item
.pszText
;
1601 case TVN_DELETEITEM
:
1603 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1604 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1606 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1610 delete (wxTreeItemAttr
*)m_attrs
.
1611 Delete((long)tv
->itemOld
.hItem
);
1616 case TVN_ENDLABELEDIT
:
1618 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1619 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1621 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1622 event
.m_label
= info
->item
.pszText
;
1623 if (info
->item
.pszText
== NULL
)
1628 case TVN_GETDISPINFO
:
1629 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1632 case TVN_SETDISPINFO
:
1634 if ( eventType
== wxEVT_NULL
)
1635 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1636 //else: get, already set above
1638 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1640 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1644 case TVN_ITEMEXPANDING
:
1645 event
.m_code
= FALSE
;
1648 case TVN_ITEMEXPANDED
:
1650 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1652 bool expand
= FALSE
;
1653 switch ( tv
->action
)
1664 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1665 "message"), tv
->action
);
1668 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
1669 eventType
= g_events
[expand
][ing
];
1671 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1677 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1678 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1680 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1682 // a separate event for this case
1683 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1685 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1687 event2
.SetEventObject(this);
1689 GetEventHandler()->ProcessEvent(event2
);
1694 case TVN_SELCHANGED
:
1695 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1698 case TVN_SELCHANGING
:
1700 if ( eventType
== wxEVT_NULL
)
1701 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1702 //else: already set above
1704 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1706 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1707 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1711 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1714 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
1715 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
1716 switch( nmcd
.dwDrawStage
)
1719 // if we've got any items with non standard attributes,
1720 // notify us before painting each item
1721 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1725 case CDDS_ITEMPREPAINT
:
1727 wxTreeItemAttr
*attr
=
1728 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1732 // nothing to do for this item
1733 return CDRF_DODEFAULT
;
1737 wxColour colText
, colBack
;
1738 if ( attr
->HasFont() )
1740 wxFont font
= attr
->GetFont();
1741 hFont
= (HFONT
)font
.GetResourceHandle();
1748 if ( attr
->HasTextColour() )
1750 colText
= attr
->GetTextColour();
1754 colText
= GetForegroundColour();
1757 // selection colours should override ours
1758 if ( nmcd
.uItemState
& CDIS_SELECTED
)
1760 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
1761 lptvcd
->clrTextBk
= clrBk
;
1763 // try to make the text visible
1764 lptvcd
->clrText
= wxColourToRGB(colText
);
1765 lptvcd
->clrText
|= ~clrBk
;
1766 lptvcd
->clrText
&= 0x00ffffff;
1770 if ( attr
->HasBackgroundColour() )
1772 colBack
= attr
->GetBackgroundColour();
1776 colBack
= GetBackgroundColour();
1779 lptvcd
->clrText
= wxColourToRGB(colText
);
1780 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
1783 // note that if we wanted to set colours for
1784 // individual columns (subitems), we would have
1785 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1788 ::SelectObject(nmcd
.hdc
, hFont
);
1790 *result
= CDRF_NEWFONT
;
1794 *result
= CDRF_DODEFAULT
;
1801 *result
= CDRF_DODEFAULT
;
1806 #endif // _WIN32_IE >= 0x300
1809 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1812 event
.SetEventObject(this);
1813 event
.SetEventType(eventType
);
1815 bool processed
= GetEventHandler()->ProcessEvent(event
);
1818 switch ( hdr
->code
)
1821 case TVN_BEGINRDRAG
:
1822 if ( event
.IsAllowed() )
1824 // normally this is impossible because the m_dragImage is
1825 // deleted once the drag operation is over
1826 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
1828 m_dragImage
= new wxDragImage(*this, event
.m_item
);
1829 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
1830 m_dragImage
->Show(this);
1834 case TVN_DELETEITEM
:
1836 // NB: we might process this message using wxWindows event
1837 // tables, but due to overhead of wxWin event system we
1838 // prefer to do it here ourself (otherwise deleting a tree
1839 // with many items is just too slow)
1840 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1842 wxTreeItemId item
= event
.m_item
;
1843 if ( HasIndirectData(item
) )
1845 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
1847 delete data
; // can't be NULL here
1849 m_itemsWithIndirectData
.Remove(item
);
1853 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1854 delete data
; // may be NULL, ok
1857 processed
= TRUE
; // Make sure we don't get called twice
1861 case TVN_BEGINLABELEDIT
:
1862 // return TRUE to cancel label editing
1863 *result
= !event
.IsAllowed();
1866 case TVN_ENDLABELEDIT
:
1867 // return TRUE to set the label to the new string
1868 *result
= event
.IsAllowed();
1870 // ensure that we don't have the text ctrl which is going to be
1875 case TVN_SELCHANGING
:
1876 case TVN_ITEMEXPANDING
:
1877 // return TRUE to prevent the action from happening
1878 *result
= !event
.IsAllowed();
1881 case TVN_GETDISPINFO
:
1882 // NB: so far the user can't set the image himself anyhow, so do it
1883 // anyway - but this may change later
1884 if ( /* !processed && */ 1 )
1886 wxTreeItemId item
= event
.m_item
;
1887 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1888 if ( info
->item
.mask
& TVIF_IMAGE
)
1891 DoGetItemImageFromData
1894 IsExpanded(item
) ? wxTreeItemIcon_Expanded
1895 : wxTreeItemIcon_Normal
1898 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
1900 info
->item
.iSelectedImage
=
1901 DoGetItemImageFromData
1904 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
1905 : wxTreeItemIcon_Selected
1912 // for the other messages the return value is ignored and there is
1913 // nothing special to do
1919 // ----------------------------------------------------------------------------
1921 // ----------------------------------------------------------------------------
1923 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1925 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1926 : wxNotifyEvent(commandType
, id
)