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"
50 #ifndef wxUSE_NORLANDER_HEADERS
51 #include "wx/msw/gnuwin32/extra.h"
55 #if (defined(__WIN95__) && !defined(__GNUWIN32__)) || defined(__TWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
59 // Bug in headers, sometimes
61 #define TVIS_FOCUSED 0x0001
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 // a convenient wrapper around TV_ITEM struct which adds a ctor
69 struct wxTreeViewItem
: public TV_ITEM
71 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
72 UINT mask_
, // fields which are valid
73 UINT stateMask_
= 0) // for TVIF_STATE only
75 // hItem member is always valid
76 mask
= mask_
| TVIF_HANDLE
;
77 stateMask
= stateMask_
;
78 hItem
= (HTREEITEM
) (WXHTREEITEM
) item
;
82 // a class which encapsulates the tree traversal logic: it vists all (unless
83 // OnVisit() returns FALSE) items under the given one
87 wxTreeTraversal(const wxTreeCtrl
*tree
)
92 // do traverse the tree: visit all items (recursively by default) under the
93 // given one; return TRUE if all items were traversed or FALSE if the
94 // traversal was aborted because OnVisit returned FALSE
95 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
97 // override this function to do whatever is needed for each item, return
98 // FALSE to stop traversing
99 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
102 const wxTreeCtrl
*GetTree() const { return m_tree
; }
105 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
107 const wxTreeCtrl
*m_tree
;
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 #if !USE_SHARED_LIBRARY
115 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 // handy table for sending events
123 static const wxEventType g_events
[2][2] =
125 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
126 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
129 // ============================================================================
131 // ============================================================================
133 // ----------------------------------------------------------------------------
135 // ----------------------------------------------------------------------------
137 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
139 if ( !OnVisit(root
) )
142 return Traverse(root
, recursively
);
145 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
148 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
149 while ( child
.IsOk() )
151 // depth first traversal
152 if ( recursively
&& !Traverse(child
, TRUE
) )
155 if ( !OnVisit(child
) )
158 child
= m_tree
->GetNextChild(root
, cookie
);
164 // ----------------------------------------------------------------------------
165 // construction and destruction
166 // ----------------------------------------------------------------------------
168 void wxTreeCtrl::Init()
170 m_imageListNormal
= NULL
;
171 m_imageListState
= NULL
;
175 bool wxTreeCtrl::Create(wxWindow
*parent
,
180 const wxValidator
& validator
,
181 const wxString
& name
)
185 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
188 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
189 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
191 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
192 wstyle
|= TVS_HASBUTTONS
;
194 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
195 wstyle
|= TVS_EDITLABELS
;
197 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
198 wstyle
|= TVS_LINESATROOT
;
200 #if !defined( __GNUWIN32__ ) && !defined(wxUSE_NORLANDER_HEADERS)
201 // we emulate the multiple selection tree controls by using checkboxes: set
202 // up the image list we need for this if we do have multiple selections
203 if ( m_windowStyle
& wxTR_MULTIPLE
)
204 wstyle
|= TVS_CHECKBOXES
;
207 // Create the tree control.
208 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
211 // the treectrl with any other background looks ugly because the items
212 // background is white anyhow
213 SetBackgroundColour(*wxWHITE
);
215 // VZ: this is some experimental code which may be used to get the
216 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
217 // AFAIK, the standard DLL does about the same thing anyhow.
219 if ( m_windowStyle
& wxTR_MULTIPLE
)
223 // create the DC compatible with the current screen
224 HDC hdcMem
= CreateCompatibleDC(NULL
);
226 // create a mono bitmap of the standard size
227 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
228 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
229 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
230 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
231 1, // # of color planes
232 1, // # bits needed for one pixel
233 0); // array containing colour data
234 SelectObject(hdcMem
, hbmpCheck
);
236 // then draw a check mark into it
237 RECT rect
= { 0, 0, x
, y
};
238 if ( !::DrawFrameControl(hdcMem
, &rect
,
240 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
242 wxLogLastError(_T("DrawFrameControl(check)"));
245 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
246 imagelistCheckboxes
.Add(bmp
);
248 if ( !::DrawFrameControl(hdcMem
, &rect
,
252 wxLogLastError(_T("DrawFrameControl(uncheck)"));
255 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
256 imagelistCheckboxes
.Add(bmp
);
262 SetStateImageList(&imagelistCheckboxes
);
266 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
271 wxTreeCtrl::~wxTreeCtrl()
275 // delete user data to prevent memory leaks
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 // simple wrappers which add error checking in debug mode
285 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
287 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
289 wxLogLastError("TreeView_GetItem");
297 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
299 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
301 wxLogLastError("TreeView_SetItem");
305 size_t wxTreeCtrl::GetCount() const
307 return (size_t)TreeView_GetCount(GetHwnd());
310 unsigned int wxTreeCtrl::GetIndent() const
312 return TreeView_GetIndent(GetHwnd());
315 void wxTreeCtrl::SetIndent(unsigned int indent
)
317 TreeView_SetIndent(GetHwnd(), indent
);
320 wxImageList
*wxTreeCtrl::GetImageList() const
322 return m_imageListNormal
;
325 wxImageList
*wxTreeCtrl::GetStateImageList() const
327 return m_imageListNormal
;
330 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
333 TreeView_SetImageList(GetHwnd(),
334 imageList
? imageList
->GetHIMAGELIST() : 0,
338 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
340 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
343 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
345 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
348 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
349 bool recursively
) const
351 class TraverseCounter
: public wxTreeTraversal
354 TraverseCounter(const wxTreeCtrl
*tree
,
355 const wxTreeItemId
& root
,
357 : wxTreeTraversal(tree
)
361 DoTraverse(root
, recursively
);
364 virtual bool OnVisit(const wxTreeItemId
& item
)
371 size_t GetCount() const { return m_count
; }
375 } counter(this, item
, recursively
);
377 return counter
.GetCount();
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
386 wxChar buf
[512]; // the size is arbitrary...
388 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
389 tvItem
.pszText
= buf
;
390 tvItem
.cchTextMax
= WXSIZEOF(buf
);
391 if ( !DoGetItem(&tvItem
) )
393 // don't return some garbage which was on stack, but an empty string
397 return wxString(buf
);
400 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
402 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
403 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
407 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
411 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
412 tvItem
.iSelectedImage
= imageSel
;
413 tvItem
.iImage
= image
;
417 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
419 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
422 return tvItem
.iImage
;
425 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
427 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
428 // change both normal and selected image - otherwise the change simply
429 // doesn't take place!
430 DoSetItemImages(item
, image
, GetItemSelectedImage(item
));
433 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
435 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
438 return tvItem
.iSelectedImage
;
441 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
443 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
444 // change both normal and selected image - otherwise the change simply
445 // doesn't take place!
446 DoSetItemImages(item
, GetItemImage(item
), image
);
449 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
451 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
452 if ( !DoGetItem(&tvItem
) )
457 return (wxTreeItemData
*)tvItem
.lParam
;
460 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
462 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
463 tvItem
.lParam
= (LPARAM
)data
;
467 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
469 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
470 tvItem
.cChildren
= (int)has
;
474 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
476 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
477 tvItem
.state
= bold
? TVIS_BOLD
: 0;
481 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
483 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
484 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
488 // ----------------------------------------------------------------------------
490 // ----------------------------------------------------------------------------
492 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
494 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
496 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
500 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
502 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
505 return tvItem
.cChildren
!= 0;
508 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
510 // probably not a good idea to put it here
511 //wxASSERT( ItemHasChildren(item) );
513 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
516 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
519 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
521 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
524 return (tvItem
.state
& TVIS_SELECTED
) != 0;
527 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
529 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
532 return (tvItem
.state
& TVIS_BOLD
) != 0;
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 wxTreeItemId
wxTreeCtrl::GetRootItem() const
541 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
544 wxTreeItemId
wxTreeCtrl::GetSelection() const
546 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
547 _T("this only works with single selection controls") );
549 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
552 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
554 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
557 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
560 // remember the last child returned in 'cookie'
561 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
563 return wxTreeItemId((WXHTREEITEM
)_cookie
);
566 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
569 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
570 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
576 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
578 // can this be done more efficiently?
581 wxTreeItemId childLast
,
582 child
= GetFirstChild(item
, cookie
);
583 while ( child
.IsOk() )
586 child
= GetNextChild(item
, cookie
);
592 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
594 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
597 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
599 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
602 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
604 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
607 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
609 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetNextVisible() "
610 "for must be visible itself!"));
612 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
615 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
617 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetPrevVisible() "
618 "for must be visible itself!"));
620 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
623 // ----------------------------------------------------------------------------
624 // multiple selections emulation
625 // ----------------------------------------------------------------------------
627 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
629 // receive the desired information.
630 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
633 // state image indices are 1 based
634 return ((tvItem
.state
>> 12) - 1) == 1;
637 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
639 // receive the desired information.
640 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
642 // state images are one-based
643 tvItem
.state
= (check
? 2 : 1) << 12;
648 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
650 class TraverseSelections
: public wxTreeTraversal
653 TraverseSelections(const wxTreeCtrl
*tree
,
654 wxArrayTreeItemIds
& selections
)
655 : wxTreeTraversal(tree
), m_selections(selections
)
657 m_selections
.Empty();
659 DoTraverse(tree
->GetRootItem());
662 virtual bool OnVisit(const wxTreeItemId
& item
)
664 if ( GetTree()->IsItemChecked(item
) )
666 m_selections
.Add(item
);
673 wxArrayTreeItemIds
& m_selections
;
674 } selector(this, selections
);
676 return selections
.GetCount();
679 // ----------------------------------------------------------------------------
681 // ----------------------------------------------------------------------------
683 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
684 wxTreeItemId hInsertAfter
,
685 const wxString
& text
,
686 int image
, int selectedImage
,
687 wxTreeItemData
*data
)
689 TV_INSERTSTRUCT tvIns
;
690 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
691 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
693 // This is how we insert the item as the first child: supply a NULL hInsertAfter
694 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
696 tvIns
.hInsertAfter
= TVI_FIRST
;
700 if ( !text
.IsEmpty() )
703 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
709 tvIns
.item
.iImage
= image
;
711 if ( selectedImage
== -1 )
713 // take the same image for selected icon if not specified
714 selectedImage
= image
;
718 if ( selectedImage
!= -1 )
720 mask
|= TVIF_SELECTEDIMAGE
;
721 tvIns
.item
.iSelectedImage
= selectedImage
;
727 tvIns
.item
.lParam
= (LPARAM
)data
;
730 tvIns
.item
.mask
= mask
;
732 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
735 wxLogLastError("TreeView_InsertItem");
740 // associate the application tree item with Win32 tree item handle
741 data
->SetId((WXHTREEITEM
)id
);
744 return wxTreeItemId((WXHTREEITEM
)id
);
747 // for compatibility only
748 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
749 const wxString
& text
,
750 int image
, int selImage
,
753 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
754 image
, selImage
, NULL
);
757 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
758 int image
, int selectedImage
,
759 wxTreeItemData
*data
)
761 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
762 text
, image
, selectedImage
, data
);
765 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
766 const wxString
& text
,
767 int image
, int selectedImage
,
768 wxTreeItemData
*data
)
770 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
771 text
, image
, selectedImage
, data
);
774 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
775 const wxTreeItemId
& idPrevious
,
776 const wxString
& text
,
777 int image
, int selectedImage
,
778 wxTreeItemData
*data
)
780 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
783 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
784 const wxString
& text
,
785 int image
, int selectedImage
,
786 wxTreeItemData
*data
)
788 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
789 text
, image
, selectedImage
, data
);
792 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
794 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
796 wxLogLastError("TreeView_DeleteItem");
800 // delete all children (but don't delete the item itself)
801 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
805 wxArrayLong children
;
806 wxTreeItemId child
= GetFirstChild(item
, cookie
);
807 while ( child
.IsOk() )
809 children
.Add((long)(WXHTREEITEM
)child
);
811 child
= GetNextChild(item
, cookie
);
814 size_t nCount
= children
.Count();
815 for ( size_t n
= 0; n
< nCount
; n
++ )
817 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
819 wxLogLastError("TreeView_DeleteItem");
824 void wxTreeCtrl::DeleteAllItems()
826 if ( !TreeView_DeleteAllItems(GetHwnd()) )
828 wxLogLastError("TreeView_DeleteAllItems");
832 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
834 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
835 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
836 flag
== TVE_EXPAND
||
838 _T("Unknown flag in wxTreeCtrl::DoExpand") );
840 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
841 // emulate them. This behaviour has changed slightly with comctl32.dll
842 // v 4.70 - now it does send them but only the first time. To maintain
843 // compatible behaviour and also in order to not have surprises with the
844 // future versions, don't rely on this and still do everything ourselves.
845 // To avoid that the messages be sent twice when the item is expanded for
846 // the first time we must clear TVIS_EXPANDEDONCE style manually.
848 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
852 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
854 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
857 bool isExpanded
= IsExpanded(item
);
859 event
.SetEventObject(this);
861 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
862 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
863 GetEventHandler()->ProcessEvent(event
);
865 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
866 GetEventHandler()->ProcessEvent(event
);
868 //else: change didn't took place, so do nothing at all
871 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
873 DoExpand(item
, TVE_EXPAND
);
876 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
878 DoExpand(item
, TVE_COLLAPSE
);
881 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
883 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
886 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
888 DoExpand(item
, TVE_TOGGLE
);
891 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
893 DoExpand(item
, action
);
896 void wxTreeCtrl::Unselect()
898 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), _T("doesn't make sense") );
900 // just remove the selection
901 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
904 void wxTreeCtrl::UnselectAll()
906 if ( m_windowStyle
& wxTR_MULTIPLE
)
908 wxArrayTreeItemIds selections
;
909 size_t count
= GetSelections(selections
);
910 for ( size_t n
= 0; n
< count
; n
++ )
912 SetItemCheck(selections
[n
], FALSE
);
917 // just remove the selection
922 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
924 if ( m_windowStyle
& wxTR_MULTIPLE
)
926 // selecting the item means checking it
931 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
932 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
933 // send them ourselves
935 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
937 event
.SetEventObject(this);
939 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
940 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
942 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
944 wxLogLastError("TreeView_SelectItem");
948 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
949 (void)GetEventHandler()->ProcessEvent(event
);
952 //else: program vetoed the change
956 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
959 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
962 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
964 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
966 wxLogLastError("TreeView_SelectSetFirstVisible");
970 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
975 void wxTreeCtrl::DeleteTextCtrl()
979 m_textCtrl
->UnsubclassWin();
980 m_textCtrl
->SetHWND(0);
986 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
987 wxClassInfo
* textControlClass
)
989 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
991 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
993 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1002 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1003 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1004 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1009 // End label editing, optionally cancelling the edit
1010 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1012 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1017 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1019 TV_HITTESTINFO hitTestInfo
;
1020 hitTestInfo
.pt
.x
= (int)point
.x
;
1021 hitTestInfo
.pt
.y
= (int)point
.y
;
1023 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1028 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1029 flags |= wxTREE_HITTEST_##flag
1031 TRANSLATE_FLAG(ABOVE
);
1032 TRANSLATE_FLAG(BELOW
);
1033 TRANSLATE_FLAG(NOWHERE
);
1034 TRANSLATE_FLAG(ONITEMBUTTON
);
1035 TRANSLATE_FLAG(ONITEMICON
);
1036 TRANSLATE_FLAG(ONITEMINDENT
);
1037 TRANSLATE_FLAG(ONITEMLABEL
);
1038 TRANSLATE_FLAG(ONITEMRIGHT
);
1039 TRANSLATE_FLAG(ONITEMSTATEICON
);
1040 TRANSLATE_FLAG(TOLEFT
);
1041 TRANSLATE_FLAG(TORIGHT
);
1043 #undef TRANSLATE_FLAG
1045 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1048 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1050 bool textOnly
) const
1053 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1056 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1062 // couldn't retrieve rect: for example, item isn't visible
1067 // ----------------------------------------------------------------------------
1069 // ----------------------------------------------------------------------------
1071 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1072 wxTreeItemData
*pItem2
,
1075 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1076 _T("sorting tree without data doesn't make sense") );
1078 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1081 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1082 const wxTreeItemId
& item2
)
1084 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1087 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1089 // rely on the fact that TreeView_SortChildren does the same thing as our
1090 // default behaviour, i.e. sorts items alphabetically and so call it
1091 // directly if we're not in derived class (much more efficient!)
1092 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1094 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1099 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1100 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1101 tvSort
.lParam
= (LPARAM
)this;
1102 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1106 // ----------------------------------------------------------------------------
1108 // ----------------------------------------------------------------------------
1110 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1112 if ( cmd
== EN_UPDATE
)
1114 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1115 event
.SetEventObject( this );
1116 ProcessCommand(event
);
1118 else if ( cmd
== EN_KILLFOCUS
)
1120 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1121 event
.SetEventObject( this );
1122 ProcessCommand(event
);
1130 // command processed
1134 // process WM_NOTIFY Windows message
1135 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1137 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1138 wxEventType eventType
= wxEVT_NULL
;
1139 NMHDR
*hdr
= (NMHDR
*)lParam
;
1141 switch ( hdr
->code
)
1144 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1147 case TVN_BEGINRDRAG
:
1149 if ( eventType
== wxEVT_NULL
)
1150 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1151 //else: left drag, already set above
1153 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1155 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1156 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1160 case TVN_BEGINLABELEDIT
:
1162 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1163 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1165 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1166 event
.m_label
= info
->item
.pszText
;
1170 case TVN_DELETEITEM
:
1172 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1173 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1175 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1179 case TVN_ENDLABELEDIT
:
1181 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1182 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1184 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1185 event
.m_label
= info
->item
.pszText
;
1189 case TVN_GETDISPINFO
:
1190 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1193 case TVN_SETDISPINFO
:
1195 if ( eventType
== wxEVT_NULL
)
1196 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1197 //else: get, already set above
1199 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1201 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1205 case TVN_ITEMEXPANDING
:
1206 event
.m_code
= FALSE
;
1209 case TVN_ITEMEXPANDED
:
1211 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1213 bool expand
= FALSE
;
1214 switch ( tv
->action
)
1225 wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND "
1226 "message"), tv
->action
);
1229 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1230 eventType
= g_events
[expand
][ing
];
1232 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1238 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1239 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1241 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1243 // a separate event for this case
1244 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1246 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1248 event2
.SetEventObject(this);
1250 GetEventHandler()->ProcessEvent(event2
);
1255 case TVN_SELCHANGED
:
1256 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1259 case TVN_SELCHANGING
:
1261 if ( eventType
== wxEVT_NULL
)
1262 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1263 //else: already set above
1265 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1267 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1268 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1273 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1276 event
.SetEventObject(this);
1277 event
.SetEventType(eventType
);
1279 bool processed
= GetEventHandler()->ProcessEvent(event
);
1282 switch ( hdr
->code
)
1284 case TVN_DELETEITEM
:
1286 // NB: we might process this message using wxWindows event
1287 // tables, but due to overhead of wxWin event system we
1288 // prefer to do it here ourself (otherwise deleting a tree
1289 // with many items is just too slow)
1290 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1291 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1292 delete data
; // may be NULL, ok
1294 processed
= TRUE
; // Make sure we don't get called twice
1298 case TVN_BEGINLABELEDIT
:
1299 // return TRUE to cancel label editing
1300 *result
= !event
.IsAllowed();
1303 case TVN_ENDLABELEDIT
:
1304 // return TRUE to set the label to the new string
1305 *result
= event
.IsAllowed();
1307 // ensure that we don't have the text ctrl which is going to be
1312 case TVN_SELCHANGING
:
1313 case TVN_ITEMEXPANDING
:
1314 // return TRUE to prevent the action from happening
1315 *result
= !event
.IsAllowed();
1319 // for the other messages the return value is ignored and there is
1320 // nothing special to do
1326 // ----------------------------------------------------------------------------
1328 // ----------------------------------------------------------------------------
1330 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1332 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1333 : wxNotifyEvent(commandType
, id
)