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 // internal class for counting tree items
350 class TraverseCounter
: public wxTreeTraversal
353 TraverseCounter(const wxTreeCtrl
*tree
,
354 const wxTreeItemId
& root
,
356 : wxTreeTraversal(tree
)
360 DoTraverse(root
, recursively
);
363 virtual bool OnVisit(const wxTreeItemId
& item
)
370 size_t GetCount() const { return m_count
; }
377 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
378 bool recursively
) const
380 TraverseCounter
counter(this, item
, recursively
);
382 return counter
.GetCount();
385 // ----------------------------------------------------------------------------
387 // ----------------------------------------------------------------------------
389 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
391 wxChar buf
[512]; // the size is arbitrary...
393 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
394 tvItem
.pszText
= buf
;
395 tvItem
.cchTextMax
= WXSIZEOF(buf
);
396 if ( !DoGetItem(&tvItem
) )
398 // don't return some garbage which was on stack, but an empty string
402 return wxString(buf
);
405 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
407 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
408 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
412 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
416 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
417 tvItem
.iSelectedImage
= imageSel
;
418 tvItem
.iImage
= image
;
422 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
) const
424 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
);
427 return tvItem
.iImage
;
430 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
)
432 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
433 // change both normal and selected image - otherwise the change simply
434 // doesn't take place!
435 DoSetItemImages(item
, image
, GetItemSelectedImage(item
));
438 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
440 wxTreeViewItem
tvItem(item
, TVIF_SELECTEDIMAGE
);
443 return tvItem
.iSelectedImage
;
446 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
448 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
449 // change both normal and selected image - otherwise the change simply
450 // doesn't take place!
451 DoSetItemImages(item
, GetItemImage(item
), image
);
454 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
456 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
457 if ( !DoGetItem(&tvItem
) )
462 return (wxTreeItemData
*)tvItem
.lParam
;
465 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
467 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
468 tvItem
.lParam
= (LPARAM
)data
;
472 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
474 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
475 tvItem
.cChildren
= (int)has
;
479 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
481 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
482 tvItem
.state
= bold
? TVIS_BOLD
: 0;
486 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
488 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
489 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
499 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
501 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
505 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
507 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
510 return tvItem
.cChildren
!= 0;
513 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
515 // probably not a good idea to put it here
516 //wxASSERT( ItemHasChildren(item) );
518 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
521 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
524 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
526 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
529 return (tvItem
.state
& TVIS_SELECTED
) != 0;
532 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
534 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
537 return (tvItem
.state
& TVIS_BOLD
) != 0;
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 wxTreeItemId
wxTreeCtrl::GetRootItem() const
546 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
549 wxTreeItemId
wxTreeCtrl::GetSelection() const
551 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
552 _T("this only works with single selection controls") );
554 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
557 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
559 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
562 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
565 // remember the last child returned in 'cookie'
566 _cookie
= (long)TreeView_GetChild(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
)item
);
568 return wxTreeItemId((WXHTREEITEM
)_cookie
);
571 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
574 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
575 (HTREEITEM
)(WXHTREEITEM
)_cookie
));
581 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
583 // can this be done more efficiently?
586 wxTreeItemId childLast
,
587 child
= GetFirstChild(item
, cookie
);
588 while ( child
.IsOk() )
591 child
= GetNextChild(item
, cookie
);
597 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
599 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
602 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
604 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
607 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
609 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
612 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
614 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetNextVisible() "
615 "for must be visible itself!"));
617 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
620 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
622 wxASSERT_MSG( IsVisible(item
), _T("The item you call GetPrevVisible() "
623 "for must be visible itself!"));
625 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
));
628 // ----------------------------------------------------------------------------
629 // multiple selections emulation
630 // ----------------------------------------------------------------------------
632 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
634 // receive the desired information.
635 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
638 // state image indices are 1 based
639 return ((tvItem
.state
>> 12) - 1) == 1;
642 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
644 // receive the desired information.
645 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
647 // state images are one-based
648 tvItem
.state
= (check
? 2 : 1) << 12;
653 // internal class for getting the selected
655 class TraverseSelections
: public wxTreeTraversal
658 TraverseSelections(const wxTreeCtrl
*tree
,
659 wxArrayTreeItemIds
& selections
)
660 : wxTreeTraversal(tree
), m_selections(selections
)
662 m_selections
.Empty();
664 DoTraverse(tree
->GetRootItem());
667 virtual bool OnVisit(const wxTreeItemId
& item
)
669 if ( GetTree()->IsItemChecked(item
) )
671 m_selections
.Add(item
);
678 wxArrayTreeItemIds
& m_selections
;
681 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
683 TraverseSelections
selector(this, selections
);
685 return selections
.GetCount();
688 // ----------------------------------------------------------------------------
690 // ----------------------------------------------------------------------------
692 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
693 wxTreeItemId hInsertAfter
,
694 const wxString
& text
,
695 int image
, int selectedImage
,
696 wxTreeItemData
*data
)
698 TV_INSERTSTRUCT tvIns
;
699 tvIns
.hParent
= (HTREEITEM
) (WXHTREEITEM
)parent
;
700 tvIns
.hInsertAfter
= (HTREEITEM
) (WXHTREEITEM
) hInsertAfter
;
702 // This is how we insert the item as the first child: supply a NULL hInsertAfter
703 if (tvIns
.hInsertAfter
== (HTREEITEM
) 0)
705 tvIns
.hInsertAfter
= TVI_FIRST
;
709 if ( !text
.IsEmpty() )
712 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
718 tvIns
.item
.iImage
= image
;
720 if ( selectedImage
== -1 )
722 // take the same image for selected icon if not specified
723 selectedImage
= image
;
727 if ( selectedImage
!= -1 )
729 mask
|= TVIF_SELECTEDIMAGE
;
730 tvIns
.item
.iSelectedImage
= selectedImage
;
736 tvIns
.item
.lParam
= (LPARAM
)data
;
739 tvIns
.item
.mask
= mask
;
741 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
744 wxLogLastError("TreeView_InsertItem");
749 // associate the application tree item with Win32 tree item handle
750 data
->SetId((WXHTREEITEM
)id
);
753 return wxTreeItemId((WXHTREEITEM
)id
);
756 // for compatibility only
757 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
758 const wxString
& text
,
759 int image
, int selImage
,
762 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
763 image
, selImage
, NULL
);
766 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
767 int image
, int selectedImage
,
768 wxTreeItemData
*data
)
770 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
771 text
, image
, selectedImage
, data
);
774 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
775 const wxString
& text
,
776 int image
, int selectedImage
,
777 wxTreeItemData
*data
)
779 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
780 text
, image
, selectedImage
, data
);
783 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
784 const wxTreeItemId
& idPrevious
,
785 const wxString
& text
,
786 int image
, int selectedImage
,
787 wxTreeItemData
*data
)
789 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
792 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
793 const wxString
& text
,
794 int image
, int selectedImage
,
795 wxTreeItemData
*data
)
797 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
798 text
, image
, selectedImage
, data
);
801 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
803 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
) )
805 wxLogLastError("TreeView_DeleteItem");
809 // delete all children (but don't delete the item itself)
810 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
814 wxArrayLong children
;
815 wxTreeItemId child
= GetFirstChild(item
, cookie
);
816 while ( child
.IsOk() )
818 children
.Add((long)(WXHTREEITEM
)child
);
820 child
= GetNextChild(item
, cookie
);
823 size_t nCount
= children
.Count();
824 for ( size_t n
= 0; n
< nCount
; n
++ )
826 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
828 wxLogLastError("TreeView_DeleteItem");
833 void wxTreeCtrl::DeleteAllItems()
835 if ( !TreeView_DeleteAllItems(GetHwnd()) )
837 wxLogLastError("TreeView_DeleteAllItems");
841 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
843 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
844 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
845 flag
== TVE_EXPAND
||
847 _T("Unknown flag in wxTreeCtrl::DoExpand") );
849 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
850 // emulate them. This behaviour has changed slightly with comctl32.dll
851 // v 4.70 - now it does send them but only the first time. To maintain
852 // compatible behaviour and also in order to not have surprises with the
853 // future versions, don't rely on this and still do everything ourselves.
854 // To avoid that the messages be sent twice when the item is expanded for
855 // the first time we must clear TVIS_EXPANDEDONCE style manually.
857 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
861 if ( TreeView_Expand(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
, flag
) != 0 )
863 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
866 bool isExpanded
= IsExpanded(item
);
868 event
.SetEventObject(this);
870 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
871 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
872 GetEventHandler()->ProcessEvent(event
);
874 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
875 GetEventHandler()->ProcessEvent(event
);
877 //else: change didn't took place, so do nothing at all
880 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
882 DoExpand(item
, TVE_EXPAND
);
885 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
887 DoExpand(item
, TVE_COLLAPSE
);
890 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
892 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
895 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
897 DoExpand(item
, TVE_TOGGLE
);
900 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
902 DoExpand(item
, action
);
905 void wxTreeCtrl::Unselect()
907 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), _T("doesn't make sense") );
909 // just remove the selection
910 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
913 void wxTreeCtrl::UnselectAll()
915 if ( m_windowStyle
& wxTR_MULTIPLE
)
917 wxArrayTreeItemIds selections
;
918 size_t count
= GetSelections(selections
);
919 for ( size_t n
= 0; n
< count
; n
++ )
921 SetItemCheck(selections
[n
], FALSE
);
926 // just remove the selection
931 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
933 if ( m_windowStyle
& wxTR_MULTIPLE
)
935 // selecting the item means checking it
940 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
941 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
942 // send them ourselves
944 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
946 event
.SetEventObject(this);
948 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
949 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
951 if ( !TreeView_SelectItem(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
953 wxLogLastError("TreeView_SelectItem");
957 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
958 (void)GetEventHandler()->ProcessEvent(event
);
961 //else: program vetoed the change
965 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
968 TreeView_EnsureVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
971 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
973 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
) )
975 wxLogLastError("TreeView_SelectSetFirstVisible");
979 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
984 void wxTreeCtrl::DeleteTextCtrl()
988 m_textCtrl
->UnsubclassWin();
989 m_textCtrl
->SetHWND(0);
995 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
996 wxClassInfo
* textControlClass
)
998 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1000 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), (HTREEITEM
) (WXHTREEITEM
) item
);
1002 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1011 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1012 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1013 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1018 // End label editing, optionally cancelling the edit
1019 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1021 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1026 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1028 TV_HITTESTINFO hitTestInfo
;
1029 hitTestInfo
.pt
.x
= (int)point
.x
;
1030 hitTestInfo
.pt
.y
= (int)point
.y
;
1032 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1037 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1038 flags |= wxTREE_HITTEST_##flag
1040 TRANSLATE_FLAG(ABOVE
);
1041 TRANSLATE_FLAG(BELOW
);
1042 TRANSLATE_FLAG(NOWHERE
);
1043 TRANSLATE_FLAG(ONITEMBUTTON
);
1044 TRANSLATE_FLAG(ONITEMICON
);
1045 TRANSLATE_FLAG(ONITEMINDENT
);
1046 TRANSLATE_FLAG(ONITEMLABEL
);
1047 TRANSLATE_FLAG(ONITEMRIGHT
);
1048 TRANSLATE_FLAG(ONITEMSTATEICON
);
1049 TRANSLATE_FLAG(TOLEFT
);
1050 TRANSLATE_FLAG(TORIGHT
);
1052 #undef TRANSLATE_FLAG
1054 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1057 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1059 bool textOnly
) const
1062 if ( TreeView_GetItemRect(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
,
1065 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1071 // couldn't retrieve rect: for example, item isn't visible
1076 // ----------------------------------------------------------------------------
1078 // ----------------------------------------------------------------------------
1080 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1081 wxTreeItemData
*pItem2
,
1084 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1085 _T("sorting tree without data doesn't make sense") );
1087 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1090 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1091 const wxTreeItemId
& item2
)
1093 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1096 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1098 // rely on the fact that TreeView_SortChildren does the same thing as our
1099 // default behaviour, i.e. sorts items alphabetically and so call it
1100 // directly if we're not in derived class (much more efficient!)
1101 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1103 TreeView_SortChildren(GetHwnd(), (HTREEITEM
)(WXHTREEITEM
)item
, 0);
1108 tvSort
.hParent
= (HTREEITEM
)(WXHTREEITEM
)item
;
1109 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1110 tvSort
.lParam
= (LPARAM
)this;
1111 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1115 // ----------------------------------------------------------------------------
1117 // ----------------------------------------------------------------------------
1119 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1121 if ( cmd
== EN_UPDATE
)
1123 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1124 event
.SetEventObject( this );
1125 ProcessCommand(event
);
1127 else if ( cmd
== EN_KILLFOCUS
)
1129 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1130 event
.SetEventObject( this );
1131 ProcessCommand(event
);
1139 // command processed
1143 // process WM_NOTIFY Windows message
1144 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1146 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1147 wxEventType eventType
= wxEVT_NULL
;
1148 NMHDR
*hdr
= (NMHDR
*)lParam
;
1150 switch ( hdr
->code
)
1153 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1156 case TVN_BEGINRDRAG
:
1158 if ( eventType
== wxEVT_NULL
)
1159 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1160 //else: left drag, already set above
1162 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1164 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1165 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1169 case TVN_BEGINLABELEDIT
:
1171 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1172 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1174 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1175 event
.m_label
= info
->item
.pszText
;
1179 case TVN_DELETEITEM
:
1181 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1182 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1184 event
.m_item
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1188 case TVN_ENDLABELEDIT
:
1190 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1191 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1193 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1194 event
.m_label
= info
->item
.pszText
;
1198 case TVN_GETDISPINFO
:
1199 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1202 case TVN_SETDISPINFO
:
1204 if ( eventType
== wxEVT_NULL
)
1205 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
1206 //else: get, already set above
1208 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1210 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1214 case TVN_ITEMEXPANDING
:
1215 event
.m_code
= FALSE
;
1218 case TVN_ITEMEXPANDED
:
1220 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1222 bool expand
= FALSE
;
1223 switch ( tv
->action
)
1234 wxLogDebug(_T("unexpected code %d in TVN_ITEMEXPAND "
1235 "message"), tv
->action
);
1238 bool ing
= (hdr
->code
== TVN_ITEMEXPANDING
);
1239 eventType
= g_events
[expand
][ing
];
1241 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1247 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
1248 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
1250 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
1252 // a separate event for this case
1253 if ( info
->wVKey
== VK_SPACE
|| info
->wVKey
== VK_RETURN
)
1255 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
1257 event2
.SetEventObject(this);
1259 GetEventHandler()->ProcessEvent(event2
);
1264 case TVN_SELCHANGED
:
1265 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
1268 case TVN_SELCHANGING
:
1270 if ( eventType
== wxEVT_NULL
)
1271 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
1272 //else: already set above
1274 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1276 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1277 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
1282 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
1285 event
.SetEventObject(this);
1286 event
.SetEventType(eventType
);
1288 bool processed
= GetEventHandler()->ProcessEvent(event
);
1291 switch ( hdr
->code
)
1293 case TVN_DELETEITEM
:
1295 // NB: we might process this message using wxWindows event
1296 // tables, but due to overhead of wxWin event system we
1297 // prefer to do it here ourself (otherwise deleting a tree
1298 // with many items is just too slow)
1299 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
1300 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
1301 delete data
; // may be NULL, ok
1303 processed
= TRUE
; // Make sure we don't get called twice
1307 case TVN_BEGINLABELEDIT
:
1308 // return TRUE to cancel label editing
1309 *result
= !event
.IsAllowed();
1312 case TVN_ENDLABELEDIT
:
1313 // return TRUE to set the label to the new string
1314 *result
= event
.IsAllowed();
1316 // ensure that we don't have the text ctrl which is going to be
1321 case TVN_SELCHANGING
:
1322 case TVN_ITEMEXPANDING
:
1323 // return TRUE to prevent the action from happening
1324 *result
= !event
.IsAllowed();
1328 // for the other messages the return value is ignored and there is
1329 // nothing special to do
1335 // ----------------------------------------------------------------------------
1337 // ----------------------------------------------------------------------------
1339 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
1341 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
1342 : wxNotifyEvent(commandType
, id
)