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))
347 // we emulate the multiple selection tree controls by using checkboxes: set
348 // up the image list we need for this if we do have multiple selections
349 if ( m_windowStyle
& wxTR_MULTIPLE
)
350 wstyle
|= TVS_CHECKBOXES
;
353 // Create the tree control.
354 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
357 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
358 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
360 // VZ: this is some experimental code which may be used to get the
361 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
362 // AFAIK, the standard DLL does about the same thing anyhow.
364 if ( m_windowStyle
& wxTR_MULTIPLE
)
368 // create the DC compatible with the current screen
369 HDC hdcMem
= CreateCompatibleDC(NULL
);
371 // create a mono bitmap of the standard size
372 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
373 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
374 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
375 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
376 1, // # of color planes
377 1, // # bits needed for one pixel
378 0); // array containing colour data
379 SelectObject(hdcMem
, hbmpCheck
);
381 // then draw a check mark into it
382 RECT rect
= { 0, 0, x
, y
};
383 if ( !::DrawFrameControl(hdcMem
, &rect
,
385 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
387 wxLogLastError(wxT("DrawFrameControl(check)"));
390 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
391 imagelistCheckboxes
.Add(bmp
);
393 if ( !::DrawFrameControl(hdcMem
, &rect
,
397 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
400 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
401 imagelistCheckboxes
.Add(bmp
);
407 SetStateImageList(&imagelistCheckboxes
);
411 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
416 wxTreeCtrl::~wxTreeCtrl()
418 // delete any attributes
421 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
423 delete (wxTreeItemAttr
*)node
->Data();
426 // prevent TVN_DELETEITEM handler from deleting the attributes again!
427 m_hasAnyAttr
= FALSE
;
432 // delete user data to prevent memory leaks
436 // ----------------------------------------------------------------------------
438 // ----------------------------------------------------------------------------
440 // simple wrappers which add error checking in debug mode
442 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
444 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
446 wxLogLastError("TreeView_GetItem");
454 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
456 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
458 wxLogLastError("TreeView_SetItem");
462 size_t wxTreeCtrl::GetCount() const
464 return (size_t)TreeView_GetCount(GetHwnd());
467 unsigned int wxTreeCtrl::GetIndent() const
469 return TreeView_GetIndent(GetHwnd());
472 void wxTreeCtrl::SetIndent(unsigned int indent
)
474 TreeView_SetIndent(GetHwnd(), indent
);
477 wxImageList
*wxTreeCtrl::GetImageList() const
479 return m_imageListNormal
;
482 wxImageList
*wxTreeCtrl::GetStateImageList() const
484 return m_imageListNormal
;
487 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
490 TreeView_SetImageList(GetHwnd(),
491 imageList
? imageList
->GetHIMAGELIST() : 0,
495 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
497 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
500 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
502 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
505 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
506 bool recursively
) const
508 TraverseCounter
counter(this, item
, recursively
);
510 return counter
.GetCount() - 1;
513 // ----------------------------------------------------------------------------
515 // ----------------------------------------------------------------------------
517 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
519 if ( !wxWindowBase::SetBackgroundColour(colour
) )
522 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
527 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
529 if ( !wxWindowBase::SetForegroundColour(colour
) )
532 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
543 wxChar buf
[512]; // the size is arbitrary...
545 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
546 tvItem
.pszText
= buf
;
547 tvItem
.cchTextMax
= WXSIZEOF(buf
);
548 if ( !DoGetItem(&tvItem
) )
550 // don't return some garbage which was on stack, but an empty string
554 return wxString(buf
);
557 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
559 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
560 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
564 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
565 wxTreeItemIcon which
) const
567 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
568 if ( !DoGetItem(&tvItem
) )
573 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
576 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
578 wxTreeItemIcon which
) const
580 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
581 if ( !DoGetItem(&tvItem
) )
586 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
588 data
->SetImage(image
, which
);
590 // make sure that we have selected images as well
591 if ( which
== wxTreeItemIcon_Normal
&&
592 !data
->HasImage(wxTreeItemIcon_Selected
) )
594 data
->SetImage(image
, wxTreeItemIcon_Selected
);
597 if ( which
== wxTreeItemIcon_Expanded
&&
598 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
600 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
604 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
608 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
609 tvItem
.iSelectedImage
= imageSel
;
610 tvItem
.iImage
= image
;
614 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
615 wxTreeItemIcon which
) const
617 if ( HasIndirectData(item
) )
619 return DoGetItemImageFromData(item
, which
);
626 wxFAIL_MSG( wxT("unknown tree item image type") );
628 case wxTreeItemIcon_Normal
:
632 case wxTreeItemIcon_Selected
:
633 mask
= TVIF_SELECTEDIMAGE
;
636 case wxTreeItemIcon_Expanded
:
637 case wxTreeItemIcon_SelectedExpanded
:
641 wxTreeViewItem
tvItem(item
, mask
);
644 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
647 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
648 wxTreeItemIcon which
)
650 int imageNormal
, imageSel
;
654 wxFAIL_MSG( wxT("unknown tree item image type") );
656 case wxTreeItemIcon_Normal
:
658 imageSel
= GetItemSelectedImage(item
);
661 case wxTreeItemIcon_Selected
:
662 imageNormal
= GetItemImage(item
);
666 case wxTreeItemIcon_Expanded
:
667 case wxTreeItemIcon_SelectedExpanded
:
668 if ( !HasIndirectData(item
) )
670 // we need to get the old images first, because after we create
671 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
673 imageNormal
= GetItemImage(item
);
674 imageSel
= GetItemSelectedImage(item
);
676 // if it doesn't have it yet, add it
677 wxTreeItemIndirectData
*data
= new
678 wxTreeItemIndirectData(this, item
);
680 // copy the data to the new location
681 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
682 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
685 DoSetItemImageFromData(item
, image
, which
);
687 // reset the normal/selected images because we won't use them any
688 // more - now they're stored inside the indirect data
690 imageSel
= I_IMAGECALLBACK
;
694 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
695 // change both normal and selected image - otherwise the change simply
696 // doesn't take place!
697 DoSetItemImages(item
, imageNormal
, imageSel
);
700 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
702 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
703 if ( !DoGetItem(&tvItem
) )
708 if ( HasIndirectData(item
) )
710 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
714 return (wxTreeItemData
*)tvItem
.lParam
;
718 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
720 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
722 if ( HasIndirectData(item
) )
724 if ( DoGetItem(&tvItem
) )
726 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
730 wxFAIL_MSG( wxT("failed to change tree items data") );
735 tvItem
.lParam
= (LPARAM
)data
;
740 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
741 wxTreeItemIndirectData
*data
)
743 // this should never happen because it's unnecessary and will probably lead
744 // to crash too because the code elsewhere supposes that the pointer the
745 // wxTreeItemIndirectData has is a real wxItemData and not
746 // wxTreeItemIndirectData as well
747 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
749 SetItemData(item
, (wxTreeItemData
*)data
);
751 m_itemsWithIndirectData
.Add(item
);
754 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
756 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
759 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
761 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
762 tvItem
.cChildren
= (int)has
;
766 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
768 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
769 tvItem
.state
= bold
? TVIS_BOLD
: 0;
773 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
775 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
776 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
780 void wxTreeCtrl::SetItemTextColour(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
->SetTextColour(col
);
796 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
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
);
809 attr
->SetBackgroundColour(col
);
812 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
816 long id
= (long)(WXHTREEITEM
)item
;
817 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
820 attr
= new wxTreeItemAttr
;
821 m_attrs
.Put(id
, (wxObject
*)attr
);
827 // ----------------------------------------------------------------------------
829 // ----------------------------------------------------------------------------
831 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
833 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
836 // this ugliness comes directly from MSDN - it *is* the correct way to pass
837 // the HTREEITEM with TVM_GETITEMRECT
838 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
840 // FALSE means get item rect for the whole item, not only text
841 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
845 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
847 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
850 return tvItem
.cChildren
!= 0;
853 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
855 // probably not a good idea to put it here
856 //wxASSERT( ItemHasChildren(item) );
858 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
861 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
864 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
866 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
869 return (tvItem
.state
& TVIS_SELECTED
) != 0;
872 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
874 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
877 return (tvItem
.state
& TVIS_BOLD
) != 0;
880 // ----------------------------------------------------------------------------
882 // ----------------------------------------------------------------------------
884 wxTreeItemId
wxTreeCtrl::GetRootItem() const
886 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
889 wxTreeItemId
wxTreeCtrl::GetSelection() const
891 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
892 wxT("this only works with single selection controls") );
894 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
897 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
899 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
902 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
905 // remember the last child returned in 'cookie'
906 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
908 return wxTreeItemId((WXHTREEITEM
)_cookie
);
911 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
914 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
915 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
921 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
923 // can this be done more efficiently?
926 wxTreeItemId childLast
,
927 child
= GetFirstChild(item
, cookie
);
928 while ( child
.IsOk() )
931 child
= GetNextChild(item
, cookie
);
937 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
939 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
942 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
944 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
947 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
949 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
952 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
954 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
955 "for must be visible itself!"));
957 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
960 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
962 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
963 "for must be visible itself!"));
965 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
968 // ----------------------------------------------------------------------------
969 // multiple selections emulation
970 // ----------------------------------------------------------------------------
972 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
974 // receive the desired information.
975 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
978 // state image indices are 1 based
979 return ((tvItem
.state
>> 12) - 1) == 1;
982 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
984 // receive the desired information.
985 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
987 // state images are one-based
988 tvItem
.state
= (check
? 2 : 1) << 12;
993 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
995 TraverseSelections
selector(this, selections
);
997 return selector
.GetCount();
1000 // ----------------------------------------------------------------------------
1002 // ----------------------------------------------------------------------------
1004 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1005 wxTreeItemId hInsertAfter
,
1006 const wxString
& text
,
1007 int image
, int selectedImage
,
1008 wxTreeItemData
*data
)
1010 TV_INSERTSTRUCT tvIns
;
1011 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
1012 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
1014 // this is how we insert the item as the first child: supply a NULL
1016 if ( !tvIns
.hInsertAfter
)
1018 tvIns
.hInsertAfter
= TVI_FIRST
;
1022 if ( !text
.IsEmpty() )
1025 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1031 tvIns
.item
.iImage
= image
;
1033 if ( selectedImage
== -1 )
1035 // take the same image for selected icon if not specified
1036 selectedImage
= image
;
1040 if ( selectedImage
!= -1 )
1042 mask
|= TVIF_SELECTEDIMAGE
;
1043 tvIns
.item
.iSelectedImage
= selectedImage
;
1049 tvIns
.item
.lParam
= (LPARAM
)data
;
1052 tvIns
.item
.mask
= mask
;
1054 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1057 wxLogLastError("TreeView_InsertItem");
1062 // associate the application tree item with Win32 tree item handle
1063 data
->SetId((WXHTREEITEM
)id
);
1066 return wxTreeItemId((WXHTREEITEM
)id
);
1069 // for compatibility only
1070 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1071 const wxString
& text
,
1072 int image
, int selImage
,
1075 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1076 image
, selImage
, NULL
);
1079 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1080 int image
, int selectedImage
,
1081 wxTreeItemData
*data
)
1083 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1084 text
, image
, selectedImage
, data
);
1087 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1088 const wxString
& text
,
1089 int image
, int selectedImage
,
1090 wxTreeItemData
*data
)
1092 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1093 text
, image
, selectedImage
, data
);
1096 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1097 const wxTreeItemId
& idPrevious
,
1098 const wxString
& text
,
1099 int image
, int selectedImage
,
1100 wxTreeItemData
*data
)
1102 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1105 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1107 const wxString
& text
,
1108 int image
, int selectedImage
,
1109 wxTreeItemData
*data
)
1111 // find the item from index
1113 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1114 while ( index
!= 0 && idCur
.IsOk() )
1119 idCur
= GetNextChild(parent
, cookie
);
1122 // assert, not check: if the index is invalid, we will append the item
1124 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1126 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1129 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1130 const wxString
& text
,
1131 int image
, int selectedImage
,
1132 wxTreeItemData
*data
)
1134 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1135 text
, image
, selectedImage
, data
);
1138 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1140 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
1142 wxLogLastError("TreeView_DeleteItem");
1146 // delete all children (but don't delete the item itself)
1147 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1151 wxArrayLong children
;
1152 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1153 while ( child
.IsOk() )
1155 children
.Add((long)(WXHTREEITEM
)child
);
1157 child
= GetNextChild(item
, cookie
);
1160 size_t nCount
= children
.Count();
1161 for ( size_t n
= 0; n
< nCount
; n
++ )
1163 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1165 wxLogLastError("TreeView_DeleteItem");
1170 void wxTreeCtrl::DeleteAllItems()
1172 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1174 wxLogLastError("TreeView_DeleteAllItems");
1178 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1180 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1181 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1182 flag
== TVE_EXPAND
||
1184 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1186 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1187 // emulate them. This behaviour has changed slightly with comctl32.dll
1188 // v 4.70 - now it does send them but only the first time. To maintain
1189 // compatible behaviour and also in order to not have surprises with the
1190 // future versions, don't rely on this and still do everything ourselves.
1191 // To avoid that the messages be sent twice when the item is expanded for
1192 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1194 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1198 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
1200 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1201 event
.m_item
= item
;
1203 bool isExpanded
= IsExpanded(item
);
1205 event
.SetEventObject(this);
1207 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1208 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1209 GetEventHandler()->ProcessEvent(event
);
1211 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1212 GetEventHandler()->ProcessEvent(event
);
1214 //else: change didn't took place, so do nothing at all
1217 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1219 DoExpand(item
, TVE_EXPAND
);
1222 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1224 DoExpand(item
, TVE_COLLAPSE
);
1227 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1229 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1232 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1234 DoExpand(item
, TVE_TOGGLE
);
1237 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1239 DoExpand(item
, action
);
1242 void wxTreeCtrl::Unselect()
1244 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxT("doesn't make sense") );
1246 // just remove the selection
1247 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1250 void wxTreeCtrl::UnselectAll()
1252 if ( m_windowStyle
& wxTR_MULTIPLE
)
1254 wxArrayTreeItemIds selections
;
1255 size_t count
= GetSelections(selections
);
1256 for ( size_t n
= 0; n
< count
; n
++ )
1258 SetItemCheck(selections
[n
], FALSE
);
1263 // just remove the selection
1268 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1270 if ( m_windowStyle
& wxTR_MULTIPLE
)
1272 // selecting the item means checking it
1277 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1278 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1279 // send them ourselves
1281 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1282 event
.m_item
= item
;
1283 event
.SetEventObject(this);
1285 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1286 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1288 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1290 wxLogLastError("TreeView_SelectItem");
1294 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1295 (void)GetEventHandler()->ProcessEvent(event
);
1298 //else: program vetoed the change
1302 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1305 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1308 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1310 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1312 wxLogLastError("TreeView_SelectSetFirstVisible");
1316 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1321 void wxTreeCtrl::DeleteTextCtrl()
1325 m_textCtrl
->UnsubclassWin();
1326 m_textCtrl
->SetHWND(0);
1332 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1333 wxClassInfo
* textControlClass
)
1335 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1337 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1339 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1348 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1349 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1350 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1355 // End label editing, optionally cancelling the edit
1356 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1358 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1363 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1365 TV_HITTESTINFO hitTestInfo
;
1366 hitTestInfo
.pt
.x
= (int)point
.x
;
1367 hitTestInfo
.pt
.y
= (int)point
.y
;
1369 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1374 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1375 flags |= wxTREE_HITTEST_##flag
1377 TRANSLATE_FLAG(ABOVE
);
1378 TRANSLATE_FLAG(BELOW
);
1379 TRANSLATE_FLAG(NOWHERE
);
1380 TRANSLATE_FLAG(ONITEMBUTTON
);
1381 TRANSLATE_FLAG(ONITEMICON
);
1382 TRANSLATE_FLAG(ONITEMINDENT
);
1383 TRANSLATE_FLAG(ONITEMLABEL
);
1384 TRANSLATE_FLAG(ONITEMRIGHT
);
1385 TRANSLATE_FLAG(ONITEMSTATEICON
);
1386 TRANSLATE_FLAG(TOLEFT
);
1387 TRANSLATE_FLAG(TORIGHT
);
1389 #undef TRANSLATE_FLAG
1391 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1394 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1396 bool textOnly
) const
1399 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1402 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1408 // couldn't retrieve rect: for example, item isn't visible
1413 // ----------------------------------------------------------------------------
1415 // ----------------------------------------------------------------------------
1417 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1418 wxTreeItemData
*pItem2
,
1421 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1422 wxT("sorting tree without data doesn't make sense") );
1424 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1427 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1428 const wxTreeItemId
& item2
)
1430 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1433 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1435 // rely on the fact that TreeView_SortChildren does the same thing as our
1436 // default behaviour, i.e. sorts items alphabetically and so call it
1437 // directly if we're not in derived class (much more efficient!)
1438 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1440 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1445 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1446 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1447 tvSort
.lParam
= (LPARAM
)this;
1448 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1452 // ----------------------------------------------------------------------------
1454 // ----------------------------------------------------------------------------
1456 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1458 if ( cmd
== EN_UPDATE
)
1460 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1461 event
.SetEventObject( this );
1462 ProcessCommand(event
);
1464 else if ( cmd
== EN_KILLFOCUS
)
1466 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1467 event
.SetEventObject( this );
1468 ProcessCommand(event
);
1476 // command processed
1480 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1481 // only do it during dragging, minimize wxWin overhead (this is important for
1482 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1483 // instead of passing by wxWin events
1484 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1492 int x
= GET_X_LPARAM(lParam
),
1493 y
= GET_Y_LPARAM(lParam
);
1495 m_dragImage
->Move(wxPoint(x
, y
), this);
1497 HTREEITEM htiTarget
= GetItemFromPoint(GetHwnd(), x
, y
);
1500 // highlight the item as target (hiding drag image is
1501 // necessary - otherwise the display will be corrupted)
1502 m_dragImage
->Hide(this);
1503 TreeView_SelectDropTarget(GetHwnd(), htiTarget
);
1504 m_dragImage
->Show(this);
1512 m_dragImage
->EndDrag(this);
1516 // generate the drag end event
1517 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1519 int x
= GET_X_LPARAM(lParam
),
1520 y
= GET_Y_LPARAM(lParam
);
1523 = (WXHTREEITEM
)GetItemFromPoint(GetHwnd(), x
, y
);
1524 event
.m_pointDrag
= wxPoint(x
, y
);
1525 event
.SetEventObject(this);
1527 (void)GetEventHandler()->ProcessEvent(event
);
1533 return wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1536 // process WM_NOTIFY Windows message
1537 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1539 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1540 wxEventType eventType
= wxEVT_NULL
;
1541 NMHDR
*hdr
= (NMHDR
*)lParam
;
1543 switch ( hdr
->code
)
1547 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1550 TV_HITTESTINFO tvhti
;
1551 ::GetCursorPos(&(tvhti
.pt
));
1552 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1553 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1555 if( tvhti
.flags
& TVHT_ONITEM
)
1557 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1558 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1565 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1568 case TVN_BEGINRDRAG
:
1570 if ( eventType
== wxEVT_NULL
)
1571 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1572 //else: left drag, already set above
1574 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1576 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1577 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1579 // don't allow dragging by default: the user code must
1580 // explicitly say that it wants to allow it to avoid breaking
1586 case TVN_BEGINLABELEDIT
:
1588 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1589 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1591 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1592 event
.m_label
= info
->item
.pszText
;
1596 case TVN_DELETEITEM
:
1598 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1599 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1601 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1605 delete (wxTreeItemAttr
*)m_attrs
.
1606 Delete((long)tv
->itemOld
.hItem
);
1611 case TVN_ENDLABELEDIT
:
1613 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1614 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1616 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1617 event
.m_label
= info
->item
.pszText
;
1618 if (info
->item
.pszText
== NULL
)
1623 case TVN_GETDISPINFO
:
1624 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1627 case TVN_SETDISPINFO
:
1629 if ( eventType
== wxEVT_NULL
)
1630 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1631 //else: get, already set above
1633 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1635 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1639 case TVN_ITEMEXPANDING
:
1640 event
.m_code
= FALSE
;
1643 case TVN_ITEMEXPANDED
:
1645 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1647 bool expand
= FALSE
;
1648 switch ( tv
->action
)
1659 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1660 "message"), tv
->action
);
1663 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
1664 eventType
= g_events
[expand
][ing
];
1666 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1672 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1673 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1675 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1677 // a separate event for this case
1678 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1680 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1682 event2
.SetEventObject(this);
1684 GetEventHandler()->ProcessEvent(event2
);
1689 case TVN_SELCHANGED
:
1690 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1693 case TVN_SELCHANGING
:
1695 if ( eventType
== wxEVT_NULL
)
1696 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1697 //else: already set above
1699 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1701 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1702 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1706 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
1709 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
1710 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
1711 switch( nmcd
.dwDrawStage
)
1714 // if we've got any items with non standard attributes,
1715 // notify us before painting each item
1716 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
1720 case CDDS_ITEMPREPAINT
:
1722 wxTreeItemAttr
*attr
=
1723 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
1727 // nothing to do for this item
1728 return CDRF_DODEFAULT
;
1732 wxColour colText
, colBack
;
1733 if ( attr
->HasFont() )
1735 wxFont font
= attr
->GetFont();
1736 hFont
= (HFONT
)font
.GetResourceHandle();
1743 if ( attr
->HasTextColour() )
1745 colText
= attr
->GetTextColour();
1749 colText
= GetForegroundColour();
1752 // selection colours should override ours
1753 if ( nmcd
.uItemState
& CDIS_SELECTED
)
1755 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
1756 lptvcd
->clrTextBk
= clrBk
;
1758 // try to make the text visible
1759 lptvcd
->clrText
= wxColourToRGB(colText
);
1760 lptvcd
->clrText
|= ~clrBk
;
1761 lptvcd
->clrText
&= 0x00ffffff;
1765 if ( attr
->HasBackgroundColour() )
1767 colBack
= attr
->GetBackgroundColour();
1771 colBack
= GetBackgroundColour();
1774 lptvcd
->clrText
= wxColourToRGB(colText
);
1775 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
1778 // note that if we wanted to set colours for
1779 // individual columns (subitems), we would have
1780 // returned CDRF_NOTIFYSUBITEMREDRAW from here
1783 ::SelectObject(nmcd
.hdc
, hFont
);
1785 *result
= CDRF_NEWFONT
;
1789 *result
= CDRF_DODEFAULT
;
1796 *result
= CDRF_DODEFAULT
;
1801 #endif // _WIN32_IE >= 0x300
1804 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1807 event
.SetEventObject(this);
1808 event
.SetEventType(eventType
);
1810 bool processed
= GetEventHandler()->ProcessEvent(event
);
1813 switch ( hdr
->code
)
1816 case TVN_BEGINRDRAG
:
1817 if ( event
.IsAllowed() )
1819 // normally this is impossible because the m_dragImage is
1820 // deleted once the drag operation is over
1821 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
1823 m_dragImage
= new wxDragImage(*this, event
.m_item
);
1824 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
1825 m_dragImage
->Show(this);
1829 case TVN_DELETEITEM
:
1831 // NB: we might process this message using wxWindows event
1832 // tables, but due to overhead of wxWin event system we
1833 // prefer to do it here ourself (otherwise deleting a tree
1834 // with many items is just too slow)
1835 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1837 wxTreeItemId item
= event
.m_item
;
1838 if ( HasIndirectData(item
) )
1840 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
1842 delete data
; // can't be NULL here
1844 m_itemsWithIndirectData
.Remove(item
);
1848 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1849 delete data
; // may be NULL, ok
1852 processed
= TRUE
; // Make sure we don't get called twice
1856 case TVN_BEGINLABELEDIT
:
1857 // return TRUE to cancel label editing
1858 *result
= !event
.IsAllowed();
1861 case TVN_ENDLABELEDIT
:
1862 // return TRUE to set the label to the new string
1863 *result
= event
.IsAllowed();
1865 // ensure that we don't have the text ctrl which is going to be
1870 case TVN_SELCHANGING
:
1871 case TVN_ITEMEXPANDING
:
1872 // return TRUE to prevent the action from happening
1873 *result
= !event
.IsAllowed();
1876 case TVN_GETDISPINFO
:
1877 // NB: so far the user can't set the image himself anyhow, so do it
1878 // anyway - but this may change later
1879 if ( /* !processed && */ 1 )
1881 wxTreeItemId item
= event
.m_item
;
1882 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1883 if ( info
->item
.mask
& TVIF_IMAGE
)
1886 DoGetItemImageFromData
1889 IsExpanded(item
) ? wxTreeItemIcon_Expanded
1890 : wxTreeItemIcon_Normal
1893 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
1895 info
->item
.iSelectedImage
=
1896 DoGetItemImageFromData
1899 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
1900 : wxTreeItemIcon_Selected
1907 // for the other messages the return value is ignored and there is
1908 // nothing special to do
1914 // ----------------------------------------------------------------------------
1916 // ----------------------------------------------------------------------------
1918 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1920 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1921 : wxNotifyEvent(commandType
, id
)