1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "treectrl.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/window.h"
31 #include "wx/msw/private.h"
33 // Mingw32 is a bit mental even though this is done in winundef
42 #if defined(__WIN95__)
45 #include "wx/dynarray.h"
46 #include "wx/imaglist.h"
47 #include "wx/treectrl.h"
48 #include "wx/settings.h"
51 #ifndef wxUSE_NORLANDER_HEADERS
52 #include "wx/msw/gnuwin32/extra.h"
56 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
60 // Bug in headers, sometimes
62 #define TVIS_FOCUSED 0x0001
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // a convenient wrapper around TV_ITEM struct which adds a ctor
71 #pragma warning( disable : 4097 )
74 struct wxTreeViewItem
: public TV_ITEM
76 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
77 UINT mask_
, // fields which are valid
78 UINT stateMask_
= 0) // for TVIF_STATE only
80 // hItem member is always valid
81 mask
= mask_
| TVIF_HANDLE
;
82 stateMask
= stateMask_
;
83 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
88 #pragma warning( default : 4097 )
91 // a class which encapsulates the tree traversal logic: it vists all (unless
92 // OnVisit() returns FALSE) items under the given one
96 wxTreeTraversal(const wxTreeCtrl
*tree
)
101 // do traverse the tree: visit all items (recursively by default) under the
102 // given one; return TRUE if all items were traversed or FALSE if the
103 // traversal was aborted because OnVisit returned FALSE
104 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
106 // override this function to do whatever is needed for each item, return
107 // FALSE to stop traversing
108 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
111 const wxTreeCtrl
*GetTree() const { return m_tree
; }
114 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
116 const wxTreeCtrl
*m_tree
;
119 // internal class for getting the selected items
120 class TraverseSelections
: public wxTreeTraversal
123 TraverseSelections(const wxTreeCtrl
*tree
,
124 wxArrayTreeItemIds
& selections
)
125 : wxTreeTraversal(tree
), m_selections(selections
)
127 m_selections
.Empty();
129 DoTraverse(tree
->GetRootItem());
132 virtual bool OnVisit(const wxTreeItemId
& item
)
134 if ( GetTree()->IsItemChecked(item
) )
136 m_selections
.Add(item
);
143 wxArrayTreeItemIds
& m_selections
;
146 // internal class for counting tree items
147 class TraverseCounter
: public wxTreeTraversal
150 TraverseCounter(const wxTreeCtrl
*tree
,
151 const wxTreeItemId
& root
,
153 : wxTreeTraversal(tree
)
157 DoTraverse(root
, recursively
);
160 virtual bool OnVisit(const wxTreeItemId
& item
)
167 size_t GetCount() const { return m_count
; }
173 // ----------------------------------------------------------------------------
174 // This class is needed for support of different images: the Win32 common
175 // control natively supports only 2 images (the normal one and another for the
176 // selected state). We wish to provide support for 2 more of them for folder
177 // items (i.e. those which have children): for expanded state and for expanded
178 // selected state. For this we use this structure to store the additional items
181 // There is only one problem with this: when we retrieve the item's data, we
182 // don't know whether we get a pointer to wxTreeItemData or
183 // wxTreeItemIndirectData. So we have to maintain a list of all items which
184 // have indirect data inside the listctrl itself.
185 // ----------------------------------------------------------------------------
186 class wxTreeItemIndirectData
189 // ctor associates this data with the item and the real item data becomes
190 // available through our GetData() method
191 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
193 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
199 m_data
= tree
->GetItemData(item
);
201 // and set ourselves as the new one
202 tree
->SetIndirectItemData(item
, this);
205 // dtor deletes the associated data as well
206 ~wxTreeItemIndirectData() { delete m_data
; }
209 // get the real data associated with the item
210 wxTreeItemData
*GetData() const { return m_data
; }
212 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
214 // do we have such image?
215 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
217 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
219 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
222 // all the images associated with the item
223 int m_images
[wxTreeItemIcon_Max
];
225 wxTreeItemData
*m_data
;
228 // ----------------------------------------------------------------------------
230 // ----------------------------------------------------------------------------
232 #if !USE_SHARED_LIBRARY
233 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
236 // ----------------------------------------------------------------------------
238 // ----------------------------------------------------------------------------
240 // handy table for sending events
241 static const wxEventType g_events
[2][2] =
243 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
244 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
247 // ============================================================================
249 // ============================================================================
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
255 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
257 if ( !OnVisit(root
) )
260 return Traverse(root
, recursively
);
263 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
266 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
267 while ( child
.IsOk() )
269 // depth first traversal
270 if ( recursively
&& !Traverse(child
, TRUE
) )
273 if ( !OnVisit(child
) )
276 child
= m_tree
->GetNextChild(root
, cookie
);
282 // ----------------------------------------------------------------------------
283 // construction and destruction
284 // ----------------------------------------------------------------------------
286 void wxTreeCtrl::Init()
288 m_imageListNormal
= NULL
;
289 m_imageListState
= NULL
;
293 bool wxTreeCtrl::Create(wxWindow
*parent
,
298 const wxValidator
& validator
,
299 const wxString
& name
)
303 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
306 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
307 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
309 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
310 wstyle
|= TVS_HASBUTTONS
;
312 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
313 wstyle
|= TVS_EDITLABELS
;
315 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
316 wstyle
|= TVS_LINESATROOT
;
318 #if !defined( __GNUWIN32__ ) && !defined( __BORLANDC__ ) && !defined(wxUSE_NORLANDER_HEADERS)
319 // we emulate the multiple selection tree controls by using checkboxes: set
320 // up the image list we need for this if we do have multiple selections
321 #if !defined(__VISUALC__) || (__VISUALC__ != 1010)
322 if ( m_windowStyle
& wxTR_MULTIPLE
)
323 wstyle
|= TVS_CHECKBOXES
;
327 // Create the tree control.
328 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
331 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
332 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
334 // VZ: this is some experimental code which may be used to get the
335 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
336 // AFAIK, the standard DLL does about the same thing anyhow.
338 if ( m_windowStyle
& wxTR_MULTIPLE
)
342 // create the DC compatible with the current screen
343 HDC hdcMem
= CreateCompatibleDC(NULL
);
345 // create a mono bitmap of the standard size
346 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
347 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
348 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
349 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
350 1, // # of color planes
351 1, // # bits needed for one pixel
352 0); // array containing colour data
353 SelectObject(hdcMem
, hbmpCheck
);
355 // then draw a check mark into it
356 RECT rect
= { 0, 0, x
, y
};
357 if ( !::DrawFrameControl(hdcMem
, &rect
,
359 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
361 wxLogLastError(wxT("DrawFrameControl(check)"));
364 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
365 imagelistCheckboxes
.Add(bmp
);
367 if ( !::DrawFrameControl(hdcMem
, &rect
,
371 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
374 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
375 imagelistCheckboxes
.Add(bmp
);
381 SetStateImageList(&imagelistCheckboxes
);
385 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
390 wxTreeCtrl::~wxTreeCtrl()
394 // delete user data to prevent memory leaks
398 // ----------------------------------------------------------------------------
400 // ----------------------------------------------------------------------------
402 // simple wrappers which add error checking in debug mode
404 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
406 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
408 wxLogLastError("TreeView_GetItem");
416 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
418 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
420 wxLogLastError("TreeView_SetItem");
424 size_t wxTreeCtrl::GetCount() const
426 return (size_t)TreeView_GetCount(GetHwnd());
429 unsigned int wxTreeCtrl::GetIndent() const
431 return TreeView_GetIndent(GetHwnd());
434 void wxTreeCtrl::SetIndent(unsigned int indent
)
436 TreeView_SetIndent(GetHwnd(), indent
);
439 wxImageList
*wxTreeCtrl::GetImageList() const
441 return m_imageListNormal
;
444 wxImageList
*wxTreeCtrl::GetStateImageList() const
446 return m_imageListNormal
;
449 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
452 TreeView_SetImageList(GetHwnd(),
453 imageList
? imageList
->GetHIMAGELIST() : 0,
457 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
459 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
462 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
464 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
467 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
468 bool recursively
) const
470 TraverseCounter
counter(this, item
, recursively
);
472 return counter
.GetCount() - 1;
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
481 wxChar buf
[512]; // the size is arbitrary...
483 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
484 tvItem
.pszText
= buf
;
485 tvItem
.cchTextMax
= WXSIZEOF(buf
);
486 if ( !DoGetItem(&tvItem
) )
488 // don't return some garbage which was on stack, but an empty string
492 return wxString(buf
);
495 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
497 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
498 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
502 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
503 wxTreeItemIcon which
) const
505 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
506 if ( !DoGetItem(&tvItem
) )
511 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
514 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
516 wxTreeItemIcon which
) const
518 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
519 if ( !DoGetItem(&tvItem
) )
524 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
526 data
->SetImage(image
, which
);
528 // make sure that we have selected images as well
529 if ( which
== wxTreeItemIcon_Normal
&&
530 !data
->HasImage(wxTreeItemIcon_Selected
) )
532 data
->SetImage(image
, wxTreeItemIcon_Selected
);
535 if ( which
== wxTreeItemIcon_Expanded
&&
536 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
538 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
542 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
546 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
547 tvItem
.iSelectedImage
= imageSel
;
548 tvItem
.iImage
= image
;
552 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
553 wxTreeItemIcon which
) const
555 if ( HasIndirectData(item
) )
557 return DoGetItemImageFromData(item
, which
);
564 wxFAIL_MSG( wxT("unknown tree item image type") );
566 case wxTreeItemIcon_Normal
:
570 case wxTreeItemIcon_Selected
:
571 mask
= TVIF_SELECTEDIMAGE
;
574 case wxTreeItemIcon_Expanded
:
575 case wxTreeItemIcon_SelectedExpanded
:
579 wxTreeViewItem
tvItem(item
, mask
);
582 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
585 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
586 wxTreeItemIcon which
)
588 int imageNormal
, imageSel
;
592 wxFAIL_MSG( wxT("unknown tree item image type") );
594 case wxTreeItemIcon_Normal
:
596 imageSel
= GetItemSelectedImage(item
);
599 case wxTreeItemIcon_Selected
:
600 imageNormal
= GetItemImage(item
);
604 case wxTreeItemIcon_Expanded
:
605 case wxTreeItemIcon_SelectedExpanded
:
606 if ( !HasIndirectData(item
) )
608 // we need to get the old images first, because after we create
609 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
611 imageNormal
= GetItemImage(item
);
612 imageSel
= GetItemSelectedImage(item
);
614 // if it doesn't have it yet, add it
615 wxTreeItemIndirectData
*data
= new
616 wxTreeItemIndirectData(this, item
);
618 // copy the data to the new location
619 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
620 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
623 DoSetItemImageFromData(item
, image
, which
);
625 // reset the normal/selected images because we won't use them any
626 // more - now they're stored inside the indirect data
628 imageSel
= I_IMAGECALLBACK
;
632 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
633 // change both normal and selected image - otherwise the change simply
634 // doesn't take place!
635 DoSetItemImages(item
, imageNormal
, imageSel
);
638 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
640 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
641 if ( !DoGetItem(&tvItem
) )
646 if ( HasIndirectData(item
) )
648 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
652 return (wxTreeItemData
*)tvItem
.lParam
;
656 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
658 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
660 if ( HasIndirectData(item
) )
662 if ( DoGetItem(&tvItem
) )
664 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
668 wxFAIL_MSG( wxT("failed to change tree items data") );
673 tvItem
.lParam
= (LPARAM
)data
;
678 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
679 wxTreeItemIndirectData
*data
)
681 // this should never happen because it's unnecessary and will probably lead
682 // to crash too because the code elsewhere supposes that the pointer the
683 // wxTreeItemIndirectData has is a real wxItemData and not
684 // wxTreeItemIndirectData as well
685 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
687 SetItemData(item
, (wxTreeItemData
*)data
);
689 m_itemsWithIndirectData
.Add(item
);
692 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
694 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
697 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
699 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
700 tvItem
.cChildren
= (int)has
;
704 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
706 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
707 tvItem
.state
= bold
? TVIS_BOLD
: 0;
711 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
713 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
714 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
718 // ----------------------------------------------------------------------------
720 // ----------------------------------------------------------------------------
722 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
724 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
726 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
730 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
732 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
735 return tvItem
.cChildren
!= 0;
738 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
740 // probably not a good idea to put it here
741 //wxASSERT( ItemHasChildren(item) );
743 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
746 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
749 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
751 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
754 return (tvItem
.state
& TVIS_SELECTED
) != 0;
757 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
759 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
762 return (tvItem
.state
& TVIS_BOLD
) != 0;
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
769 wxTreeItemId
wxTreeCtrl::GetRootItem() const
771 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
774 wxTreeItemId
wxTreeCtrl::GetSelection() const
776 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
777 wxT("this only works with single selection controls") );
779 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
782 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
784 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
787 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
790 // remember the last child returned in 'cookie'
791 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
793 return wxTreeItemId((WXHTREEITEM
)_cookie
);
796 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
799 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
800 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
806 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
808 // can this be done more efficiently?
811 wxTreeItemId childLast
,
812 child
= GetFirstChild(item
, cookie
);
813 while ( child
.IsOk() )
816 child
= GetNextChild(item
, cookie
);
822 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
824 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
827 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
829 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
832 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
834 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
837 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
839 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
840 "for must be visible itself!"));
842 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
845 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
847 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
848 "for must be visible itself!"));
850 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
853 // ----------------------------------------------------------------------------
854 // multiple selections emulation
855 // ----------------------------------------------------------------------------
857 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
859 // receive the desired information.
860 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
863 // state image indices are 1 based
864 return ((tvItem
.state
>> 12) - 1) == 1;
867 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
869 // receive the desired information.
870 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
872 // state images are one-based
873 tvItem
.state
= (check
? 2 : 1) << 12;
878 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
880 TraverseSelections
selector(this, selections
);
882 return selections
.GetCount();
885 // ----------------------------------------------------------------------------
887 // ----------------------------------------------------------------------------
889 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
890 wxTreeItemId hInsertAfter
,
891 const wxString
& text
,
892 int image
, int selectedImage
,
893 wxTreeItemData
*data
)
895 TV_INSERTSTRUCT tvIns
;
896 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
897 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
899 // this is how we insert the item as the first child: supply a NULL
901 if ( !tvIns
.hInsertAfter
)
903 tvIns
.hInsertAfter
= TVI_FIRST
;
907 if ( !text
.IsEmpty() )
910 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
916 tvIns
.item
.iImage
= image
;
918 if ( selectedImage
== -1 )
920 // take the same image for selected icon if not specified
921 selectedImage
= image
;
925 if ( selectedImage
!= -1 )
927 mask
|= TVIF_SELECTEDIMAGE
;
928 tvIns
.item
.iSelectedImage
= selectedImage
;
934 tvIns
.item
.lParam
= (LPARAM
)data
;
937 tvIns
.item
.mask
= mask
;
939 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
942 wxLogLastError("TreeView_InsertItem");
947 // associate the application tree item with Win32 tree item handle
948 data
->SetId((WXHTREEITEM
)id
);
951 return wxTreeItemId((WXHTREEITEM
)id
);
954 // for compatibility only
955 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
956 const wxString
& text
,
957 int image
, int selImage
,
960 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
961 image
, selImage
, NULL
);
964 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
965 int image
, int selectedImage
,
966 wxTreeItemData
*data
)
968 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
969 text
, image
, selectedImage
, data
);
972 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
973 const wxString
& text
,
974 int image
, int selectedImage
,
975 wxTreeItemData
*data
)
977 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
978 text
, image
, selectedImage
, data
);
981 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
982 const wxTreeItemId
& idPrevious
,
983 const wxString
& text
,
984 int image
, int selectedImage
,
985 wxTreeItemData
*data
)
987 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
990 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
991 const wxString
& text
,
992 int image
, int selectedImage
,
993 wxTreeItemData
*data
)
995 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
996 text
, image
, selectedImage
, data
);
999 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1001 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
1003 wxLogLastError("TreeView_DeleteItem");
1007 // delete all children (but don't delete the item itself)
1008 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1012 wxArrayLong children
;
1013 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1014 while ( child
.IsOk() )
1016 children
.Add((long)(WXHTREEITEM
)child
);
1018 child
= GetNextChild(item
, cookie
);
1021 size_t nCount
= children
.Count();
1022 for ( size_t n
= 0; n
< nCount
; n
++ )
1024 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1026 wxLogLastError("TreeView_DeleteItem");
1031 void wxTreeCtrl::DeleteAllItems()
1033 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1035 wxLogLastError("TreeView_DeleteAllItems");
1039 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1041 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1042 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1043 flag
== TVE_EXPAND
||
1045 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1047 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1048 // emulate them. This behaviour has changed slightly with comctl32.dll
1049 // v 4.70 - now it does send them but only the first time. To maintain
1050 // compatible behaviour and also in order to not have surprises with the
1051 // future versions, don't rely on this and still do everything ourselves.
1052 // To avoid that the messages be sent twice when the item is expanded for
1053 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1055 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1059 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
1061 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1062 event
.m_item
= item
;
1064 bool isExpanded
= IsExpanded(item
);
1066 event
.SetEventObject(this);
1068 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1069 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1070 GetEventHandler()->ProcessEvent(event
);
1072 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1073 GetEventHandler()->ProcessEvent(event
);
1075 //else: change didn't took place, so do nothing at all
1078 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1080 DoExpand(item
, TVE_EXPAND
);
1083 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1085 DoExpand(item
, TVE_COLLAPSE
);
1088 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1090 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1093 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1095 DoExpand(item
, TVE_TOGGLE
);
1098 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1100 DoExpand(item
, action
);
1103 void wxTreeCtrl::Unselect()
1105 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxT("doesn't make sense") );
1107 // just remove the selection
1108 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1111 void wxTreeCtrl::UnselectAll()
1113 if ( m_windowStyle
& wxTR_MULTIPLE
)
1115 wxArrayTreeItemIds selections
;
1116 size_t count
= GetSelections(selections
);
1117 for ( size_t n
= 0; n
< count
; n
++ )
1119 SetItemCheck(selections
[n
], FALSE
);
1124 // just remove the selection
1129 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1131 if ( m_windowStyle
& wxTR_MULTIPLE
)
1133 // selecting the item means checking it
1138 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1139 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1140 // send them ourselves
1142 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1143 event
.m_item
= item
;
1144 event
.SetEventObject(this);
1146 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1147 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1149 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1151 wxLogLastError("TreeView_SelectItem");
1155 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1156 (void)GetEventHandler()->ProcessEvent(event
);
1159 //else: program vetoed the change
1163 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1166 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1169 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1171 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
1173 wxLogLastError("TreeView_SelectSetFirstVisible");
1177 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1182 void wxTreeCtrl::DeleteTextCtrl()
1186 m_textCtrl
->UnsubclassWin();
1187 m_textCtrl
->SetHWND(0);
1193 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1194 wxClassInfo
* textControlClass
)
1196 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1198 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1200 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1209 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1210 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1211 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1216 // End label editing, optionally cancelling the edit
1217 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1219 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1224 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1226 TV_HITTESTINFO hitTestInfo
;
1227 hitTestInfo
.pt
.x
= (int)point
.x
;
1228 hitTestInfo
.pt
.y
= (int)point
.y
;
1230 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1235 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1236 flags |= wxTREE_HITTEST_##flag
1238 TRANSLATE_FLAG(ABOVE
);
1239 TRANSLATE_FLAG(BELOW
);
1240 TRANSLATE_FLAG(NOWHERE
);
1241 TRANSLATE_FLAG(ONITEMBUTTON
);
1242 TRANSLATE_FLAG(ONITEMICON
);
1243 TRANSLATE_FLAG(ONITEMINDENT
);
1244 TRANSLATE_FLAG(ONITEMLABEL
);
1245 TRANSLATE_FLAG(ONITEMRIGHT
);
1246 TRANSLATE_FLAG(ONITEMSTATEICON
);
1247 TRANSLATE_FLAG(TOLEFT
);
1248 TRANSLATE_FLAG(TORIGHT
);
1250 #undef TRANSLATE_FLAG
1252 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1255 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1257 bool textOnly
) const
1260 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1263 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1269 // couldn't retrieve rect: for example, item isn't visible
1274 // ----------------------------------------------------------------------------
1276 // ----------------------------------------------------------------------------
1278 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1279 wxTreeItemData
*pItem2
,
1282 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1283 wxT("sorting tree without data doesn't make sense") );
1285 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1288 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1289 const wxTreeItemId
& item2
)
1291 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1294 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1296 // rely on the fact that TreeView_SortChildren does the same thing as our
1297 // default behaviour, i.e. sorts items alphabetically and so call it
1298 // directly if we're not in derived class (much more efficient!)
1299 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1301 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1306 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1307 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1308 tvSort
.lParam
= (LPARAM
)this;
1309 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1313 // ----------------------------------------------------------------------------
1315 // ----------------------------------------------------------------------------
1317 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1319 if ( cmd
== EN_UPDATE
)
1321 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1322 event
.SetEventObject( this );
1323 ProcessCommand(event
);
1325 else if ( cmd
== EN_KILLFOCUS
)
1327 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1328 event
.SetEventObject( this );
1329 ProcessCommand(event
);
1337 // command processed
1341 // process WM_NOTIFY Windows message
1342 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1344 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1345 wxEventType eventType
= wxEVT_NULL
;
1346 NMHDR
*hdr
= (NMHDR
*)lParam
;
1348 switch ( hdr
->code
)
1352 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1355 TV_HITTESTINFO tvhti
;
1356 ::GetCursorPos(&(tvhti
.pt
));
1357 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1358 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1360 if( tvhti
.flags
& TVHT_ONITEM
)
1362 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1363 eventType
=wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1370 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1373 case TVN_BEGINRDRAG
:
1375 if ( eventType
== wxEVT_NULL
)
1376 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1377 //else: left drag, already set above
1379 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1381 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1382 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1386 case TVN_BEGINLABELEDIT
:
1388 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1389 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1391 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1392 event
.m_label
= info
->item
.pszText
;
1396 case TVN_DELETEITEM
:
1398 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1399 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1401 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1405 case TVN_ENDLABELEDIT
:
1407 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1408 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1410 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1411 event
.m_label
= info
->item
.pszText
;
1415 case TVN_GETDISPINFO
:
1416 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1419 case TVN_SETDISPINFO
:
1421 if ( eventType
== wxEVT_NULL
)
1422 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1423 //else: get, already set above
1425 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1427 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1431 case TVN_ITEMEXPANDING
:
1432 event
.m_code
= FALSE
;
1435 case TVN_ITEMEXPANDED
:
1437 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1439 bool expand
= FALSE
;
1440 switch ( tv
->action
)
1451 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
1452 "message"), tv
->action
);
1455 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
1456 eventType
= g_events
[expand
][ing
];
1458 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1464 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1465 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1467 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1469 // a separate event for this case
1470 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1472 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1474 event2
.SetEventObject(this);
1476 GetEventHandler()->ProcessEvent(event2
);
1481 case TVN_SELCHANGED
:
1482 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1485 case TVN_SELCHANGING
:
1487 if ( eventType
== wxEVT_NULL
)
1488 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1489 //else: already set above
1491 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1493 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1494 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1499 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1502 event
.SetEventObject(this);
1503 event
.SetEventType(eventType
);
1505 bool processed
= GetEventHandler()->ProcessEvent(event
);
1508 switch ( hdr
->code
)
1510 case TVN_DELETEITEM
:
1512 // NB: we might process this message using wxWindows event
1513 // tables, but due to overhead of wxWin event system we
1514 // prefer to do it here ourself (otherwise deleting a tree
1515 // with many items is just too slow)
1516 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1518 wxTreeItemId item
= event
.m_item
;
1519 if ( HasIndirectData(item
) )
1521 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
1523 delete data
; // can't be NULL here
1525 m_itemsWithIndirectData
.Remove(item
);
1529 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1530 delete data
; // may be NULL, ok
1533 processed
= TRUE
; // Make sure we don't get called twice
1537 case TVN_BEGINLABELEDIT
:
1538 // return TRUE to cancel label editing
1539 *result
= !event
.IsAllowed();
1542 case TVN_ENDLABELEDIT
:
1543 // return TRUE to set the label to the new string
1544 *result
= event
.IsAllowed();
1546 // ensure that we don't have the text ctrl which is going to be
1551 case TVN_SELCHANGING
:
1552 case TVN_ITEMEXPANDING
:
1553 // return TRUE to prevent the action from happening
1554 *result
= !event
.IsAllowed();
1557 case TVN_GETDISPINFO
:
1558 // NB: so far the user can't set the image himself anyhow, so do it
1559 // anyway - but this may change later
1560 if ( /* !processed && */ 1 )
1562 wxTreeItemId item
= event
.m_item
;
1563 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1564 if ( info
->item
.mask
& TVIF_IMAGE
)
1567 DoGetItemImageFromData
1570 IsExpanded(item
) ? wxTreeItemIcon_Expanded
1571 : wxTreeItemIcon_Normal
1574 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
1576 info
->item
.iSelectedImage
=
1577 DoGetItemImageFromData
1580 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
1581 : wxTreeItemIcon_Selected
1588 // for the other messages the return value is ignored and there is
1589 // nothing special to do
1595 // ----------------------------------------------------------------------------
1597 // ----------------------------------------------------------------------------
1599 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1601 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1602 : wxNotifyEvent(commandType
, id
)