1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #pragma implementation "treectrl.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/msw/private.h"
32 // Mingw32 is a bit mental even though this is done in winundef
41 #if defined(__WIN95__)
44 #include "wx/dynarray.h"
45 #include "wx/imaglist.h"
46 #include "wx/treectrl.h"
47 #include "wx/settings.h"
49 #include "wx/msw/dragimag.h"
51 #ifdef __GNUWIN32_OLD__
52 #include "wx/msw/gnuwin32/extra.h"
55 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
59 // Bug in headers, sometimes
61 #define TVIS_FOCUSED 0x0001
65 #define TV_FIRST 0x1100
68 #ifndef TVS_CHECKBOXES
69 #define TVS_CHECKBOXES 0x0100
72 // old headers might miss these messages (comctl32.dll 4.71+ only)
73 #ifndef TVM_SETBKCOLOR
74 #define TVM_SETBKCOLOR (TV_FIRST + 29)
75 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
78 // a macro to hide the ugliness of nested casts
79 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
81 // the native control doesn't support multiple selections under MSW and we
82 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
83 // checkboxes be the selection status (checked == selected) or by really
84 // emulating everything, i.e. intercepting mouse and key events &c. The first
85 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
87 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
89 // from msw/window.cpp
90 extern bool wxIsShiftDown();
91 extern bool wxIsCtrlDown();
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // wrapper for TreeView_HitTest
98 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
104 // TreeView_HitTest() doesn't do the right cast in mingw32 headers
105 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
108 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
110 // wrappers for TreeView_GetItem/TreeView_SetItem
111 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
114 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
115 tvi
.stateMask
= TVIS_SELECTED
;
118 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
120 wxLogLastError("TreeView_GetItem");
123 return (tvi
.state
& TVIS_SELECTED
) != 0;
126 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
129 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
130 tvi
.stateMask
= TVIS_SELECTED
;
131 tvi
.state
= select
? TVIS_SELECTED
: 0;
134 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
136 wxLogLastError("TreeView_SetItem");
140 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
142 SelectItem(hwndTV
, htItem
, FALSE
);
145 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
147 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
150 // helper function which selects all items in a range and, optionally,
151 // unselects all others
152 static void SelectRange(HWND hwndTV
,
155 bool unselectOthers
= TRUE
)
157 // find the first (or last) item and select it
159 HTREEITEM htItem
= TreeView_GetRoot(hwndTV
);
160 while ( htItem
&& cont
)
162 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
164 if ( !IsItemSelected(hwndTV
, htItem
) )
166 SelectItem(hwndTV
, htItem
);
173 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
175 UnselectItem(hwndTV
, htItem
);
179 htItem
= TreeView_GetNextVisible(hwndTV
, htItem
);
182 // select the items in range
183 cont
= htFirst
!= htLast
;
184 while ( htItem
&& cont
)
186 if ( !IsItemSelected(hwndTV
, htItem
) )
188 SelectItem(hwndTV
, htItem
);
191 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
193 htItem
= TreeView_GetNextVisible(hwndTV
, htItem
);
197 if ( unselectOthers
)
201 if ( IsItemSelected(hwndTV
, htItem
) )
203 UnselectItem(hwndTV
, htItem
);
206 htItem
= TreeView_GetNextVisible(hwndTV
, htItem
);
210 // seems to be necessary - otherwise the just selected items don't always
211 // appear as selected
212 UpdateWindow(hwndTV
);
215 // helper function which tricks the standard control into changing the focused
216 // item without changing anything else (if someone knows why Microsoft doesn't
217 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
218 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
221 HTREEITEM htFocus
= TreeView_GetSelection(hwndTV
);
226 if ( htItem
!= htFocus
)
228 // remember the selection state of the item
229 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
231 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
233 // prevent the tree from unselecting the old focus which it
234 // would do by default (TreeView_SelectItem unselects the
236 TreeView_SelectItem(hwndTV
, 0);
237 SelectItem(hwndTV
, htFocus
);
240 TreeView_SelectItem(hwndTV
, htItem
);
244 // need to clear the selection which TreeView_SelectItem() gave
246 UnselectItem(hwndTV
, htItem
);
248 //else: was selected, still selected - ok
250 //else: nothing to do, focus already there
256 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
258 // just clear the focus
259 TreeView_SelectItem(hwndTV
, 0);
261 if ( wasFocusSelected
)
263 // restore the selection state
264 SelectItem(hwndTV
, htFocus
);
267 //else: nothing to do, no focus already
271 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
273 // ----------------------------------------------------------------------------
275 // ----------------------------------------------------------------------------
277 // a convenient wrapper around TV_ITEM struct which adds a ctor
279 #pragma warning( disable : 4097 ) // inheriting from typedef
282 struct wxTreeViewItem
: public TV_ITEM
284 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
285 UINT mask_
, // fields which are valid
286 UINT stateMask_
= 0) // for TVIF_STATE only
288 // hItem member is always valid
289 mask
= mask_
| TVIF_HANDLE
;
290 stateMask
= stateMask_
;
296 #pragma warning( default : 4097 )
299 // a class which encapsulates the tree traversal logic: it vists all (unless
300 // OnVisit() returns FALSE) items under the given one
301 class wxTreeTraversal
304 wxTreeTraversal(const wxTreeCtrl
*tree
)
309 // do traverse the tree: visit all items (recursively by default) under the
310 // given one; return TRUE if all items were traversed or FALSE if the
311 // traversal was aborted because OnVisit returned FALSE
312 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
314 // override this function to do whatever is needed for each item, return
315 // FALSE to stop traversing
316 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
319 const wxTreeCtrl
*GetTree() const { return m_tree
; }
322 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
324 const wxTreeCtrl
*m_tree
;
327 // internal class for getting the selected items
328 class TraverseSelections
: public wxTreeTraversal
331 TraverseSelections(const wxTreeCtrl
*tree
,
332 wxArrayTreeItemIds
& selections
)
333 : wxTreeTraversal(tree
), m_selections(selections
)
335 m_selections
.Empty();
337 DoTraverse(tree
->GetRootItem());
340 virtual bool OnVisit(const wxTreeItemId
& item
)
342 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
343 if ( GetTree()->IsItemChecked(item
) )
345 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
348 m_selections
.Add(item
);
354 size_t GetCount() const { return m_selections
.GetCount(); }
357 wxArrayTreeItemIds
& m_selections
;
360 // internal class for counting tree items
361 class TraverseCounter
: public wxTreeTraversal
364 TraverseCounter(const wxTreeCtrl
*tree
,
365 const wxTreeItemId
& root
,
367 : wxTreeTraversal(tree
)
371 DoTraverse(root
, recursively
);
374 virtual bool OnVisit(const wxTreeItemId
& item
)
381 size_t GetCount() const { return m_count
; }
387 // ----------------------------------------------------------------------------
388 // This class is needed for support of different images: the Win32 common
389 // control natively supports only 2 images (the normal one and another for the
390 // selected state). We wish to provide support for 2 more of them for folder
391 // items (i.e. those which have children): for expanded state and for expanded
392 // selected state. For this we use this structure to store the additional items
395 // There is only one problem with this: when we retrieve the item's data, we
396 // don't know whether we get a pointer to wxTreeItemData or
397 // wxTreeItemIndirectData. So we have to maintain a list of all items which
398 // have indirect data inside the listctrl itself.
399 // ----------------------------------------------------------------------------
401 class wxTreeItemIndirectData
404 // ctor associates this data with the item and the real item data becomes
405 // available through our GetData() method
406 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
408 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
414 m_data
= tree
->GetItemData(item
);
416 // and set ourselves as the new one
417 tree
->SetIndirectItemData(item
, this);
420 // dtor deletes the associated data as well
421 ~wxTreeItemIndirectData() { delete m_data
; }
424 // get the real data associated with the item
425 wxTreeItemData
*GetData() const { return m_data
; }
427 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
429 // do we have such image?
430 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
432 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
434 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
437 // all the images associated with the item
438 int m_images
[wxTreeItemIcon_Max
];
440 wxTreeItemData
*m_data
;
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
449 // ----------------------------------------------------------------------------
451 // ----------------------------------------------------------------------------
453 // handy table for sending events
454 static const wxEventType g_events
[2][2] =
456 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
457 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
460 // ============================================================================
462 // ============================================================================
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
470 if ( !OnVisit(root
) )
473 return Traverse(root
, recursively
);
476 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
479 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
480 while ( child
.IsOk() )
482 // depth first traversal
483 if ( recursively
&& !Traverse(child
, TRUE
) )
486 if ( !OnVisit(child
) )
489 child
= m_tree
->GetNextChild(root
, cookie
);
495 // ----------------------------------------------------------------------------
496 // construction and destruction
497 // ----------------------------------------------------------------------------
499 void wxTreeCtrl::Init()
501 m_imageListNormal
= NULL
;
502 m_imageListState
= NULL
;
504 m_hasAnyAttr
= FALSE
;
510 bool wxTreeCtrl::Create(wxWindow
*parent
,
515 const wxValidator
& validator
,
516 const wxString
& name
)
520 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
523 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
524 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
526 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
527 wstyle
|= TVS_HASBUTTONS
;
529 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
530 wstyle
|= TVS_EDITLABELS
;
532 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
533 wstyle
|= TVS_LINESATROOT
;
535 // using TVS_CHECKBOXES for emulation of a multiselection tree control
536 // doesn't work without the new enough headers
537 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
538 !defined( __GNUWIN32_OLD__ ) && \
539 !defined( __BORLANDC__ ) && \
540 !defined( __WATCOMC__ ) && \
541 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
543 // we emulate the multiple selection tree controls by using checkboxes: set
544 // up the image list we need for this if we do have multiple selections
545 if ( m_windowStyle
& wxTR_MULTIPLE
)
546 wstyle
|= TVS_CHECKBOXES
;
547 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
549 // Create the tree control.
550 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
553 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
554 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
556 // VZ: this is some experimental code which may be used to get the
557 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
558 // AFAIK, the standard DLL does about the same thing anyhow.
560 if ( m_windowStyle
& wxTR_MULTIPLE
)
564 // create the DC compatible with the current screen
565 HDC hdcMem
= CreateCompatibleDC(NULL
);
567 // create a mono bitmap of the standard size
568 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
569 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
570 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
571 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
572 1, // # of color planes
573 1, // # bits needed for one pixel
574 0); // array containing colour data
575 SelectObject(hdcMem
, hbmpCheck
);
577 // then draw a check mark into it
578 RECT rect
= { 0, 0, x
, y
};
579 if ( !::DrawFrameControl(hdcMem
, &rect
,
581 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
583 wxLogLastError(wxT("DrawFrameControl(check)"));
586 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
587 imagelistCheckboxes
.Add(bmp
);
589 if ( !::DrawFrameControl(hdcMem
, &rect
,
593 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
596 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
597 imagelistCheckboxes
.Add(bmp
);
603 SetStateImageList(&imagelistCheckboxes
);
607 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
612 wxTreeCtrl::~wxTreeCtrl()
614 // delete any attributes
617 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
619 delete (wxTreeItemAttr
*)node
->Data();
622 // prevent TVN_DELETEITEM handler from deleting the attributes again!
623 m_hasAnyAttr
= FALSE
;
628 // delete user data to prevent memory leaks
632 // ----------------------------------------------------------------------------
634 // ----------------------------------------------------------------------------
636 // simple wrappers which add error checking in debug mode
638 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
640 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
642 wxLogLastError("TreeView_GetItem");
650 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
652 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
654 wxLogLastError("TreeView_SetItem");
658 size_t wxTreeCtrl::GetCount() const
660 return (size_t)TreeView_GetCount(GetHwnd());
663 unsigned int wxTreeCtrl::GetIndent() const
665 return TreeView_GetIndent(GetHwnd());
668 void wxTreeCtrl::SetIndent(unsigned int indent
)
670 TreeView_SetIndent(GetHwnd(), indent
);
673 wxImageList
*wxTreeCtrl::GetImageList() const
675 return m_imageListNormal
;
678 wxImageList
*wxTreeCtrl::GetStateImageList() const
680 return m_imageListNormal
;
683 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
686 TreeView_SetImageList(GetHwnd(),
687 imageList
? imageList
->GetHIMAGELIST() : 0,
691 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
693 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
696 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
698 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
701 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
702 bool recursively
) const
704 TraverseCounter
counter(this, item
, recursively
);
706 return counter
.GetCount() - 1;
709 // ----------------------------------------------------------------------------
711 // ----------------------------------------------------------------------------
713 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
715 if ( !wxWindowBase::SetBackgroundColour(colour
) )
718 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
723 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
725 if ( !wxWindowBase::SetForegroundColour(colour
) )
728 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
739 wxChar buf
[512]; // the size is arbitrary...
741 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
742 tvItem
.pszText
= buf
;
743 tvItem
.cchTextMax
= WXSIZEOF(buf
);
744 if ( !DoGetItem(&tvItem
) )
746 // don't return some garbage which was on stack, but an empty string
750 return wxString(buf
);
753 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
755 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
756 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
760 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
761 wxTreeItemIcon which
) const
763 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
764 if ( !DoGetItem(&tvItem
) )
769 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
772 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
774 wxTreeItemIcon which
) const
776 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
777 if ( !DoGetItem(&tvItem
) )
782 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
784 data
->SetImage(image
, which
);
786 // make sure that we have selected images as well
787 if ( which
== wxTreeItemIcon_Normal
&&
788 !data
->HasImage(wxTreeItemIcon_Selected
) )
790 data
->SetImage(image
, wxTreeItemIcon_Selected
);
793 if ( which
== wxTreeItemIcon_Expanded
&&
794 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
796 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
800 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
804 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
805 tvItem
.iSelectedImage
= imageSel
;
806 tvItem
.iImage
= image
;
810 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
811 wxTreeItemIcon which
) const
813 if ( HasIndirectData(item
) )
815 return DoGetItemImageFromData(item
, which
);
822 wxFAIL_MSG( wxT("unknown tree item image type") );
824 case wxTreeItemIcon_Normal
:
828 case wxTreeItemIcon_Selected
:
829 mask
= TVIF_SELECTEDIMAGE
;
832 case wxTreeItemIcon_Expanded
:
833 case wxTreeItemIcon_SelectedExpanded
:
837 wxTreeViewItem
tvItem(item
, mask
);
840 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
843 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
844 wxTreeItemIcon which
)
846 int imageNormal
, imageSel
;
850 wxFAIL_MSG( wxT("unknown tree item image type") );
852 case wxTreeItemIcon_Normal
:
854 imageSel
= GetItemSelectedImage(item
);
857 case wxTreeItemIcon_Selected
:
858 imageNormal
= GetItemImage(item
);
862 case wxTreeItemIcon_Expanded
:
863 case wxTreeItemIcon_SelectedExpanded
:
864 if ( !HasIndirectData(item
) )
866 // we need to get the old images first, because after we create
867 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
869 imageNormal
= GetItemImage(item
);
870 imageSel
= GetItemSelectedImage(item
);
872 // if it doesn't have it yet, add it
873 wxTreeItemIndirectData
*data
= new
874 wxTreeItemIndirectData(this, item
);
876 // copy the data to the new location
877 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
878 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
881 DoSetItemImageFromData(item
, image
, which
);
883 // reset the normal/selected images because we won't use them any
884 // more - now they're stored inside the indirect data
886 imageSel
= I_IMAGECALLBACK
;
890 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
891 // change both normal and selected image - otherwise the change simply
892 // doesn't take place!
893 DoSetItemImages(item
, imageNormal
, imageSel
);
896 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
898 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
899 if ( !DoGetItem(&tvItem
) )
904 if ( HasIndirectData(item
) )
906 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
910 return (wxTreeItemData
*)tvItem
.lParam
;
914 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
916 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
918 if ( HasIndirectData(item
) )
920 if ( DoGetItem(&tvItem
) )
922 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
926 wxFAIL_MSG( wxT("failed to change tree items data") );
931 tvItem
.lParam
= (LPARAM
)data
;
936 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
937 wxTreeItemIndirectData
*data
)
939 // this should never happen because it's unnecessary and will probably lead
940 // to crash too because the code elsewhere supposes that the pointer the
941 // wxTreeItemIndirectData has is a real wxItemData and not
942 // wxTreeItemIndirectData as well
943 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
945 SetItemData(item
, (wxTreeItemData
*)data
);
947 m_itemsWithIndirectData
.Add(item
);
950 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
952 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
955 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
957 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
958 tvItem
.cChildren
= (int)has
;
962 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
964 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
965 tvItem
.state
= bold
? TVIS_BOLD
: 0;
969 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
971 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
972 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
976 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
981 long id
= (long)(WXHTREEITEM
)item
;
982 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
985 attr
= new wxTreeItemAttr
;
986 m_attrs
.Put(id
, (wxObject
*)attr
);
989 attr
->SetTextColour(col
);
992 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
997 long id
= (long)(WXHTREEITEM
)item
;
998 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1001 attr
= new wxTreeItemAttr
;
1002 m_attrs
.Put(id
, (wxObject
*)attr
);
1005 attr
->SetBackgroundColour(col
);
1008 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1010 m_hasAnyAttr
= TRUE
;
1012 long id
= (long)(WXHTREEITEM
)item
;
1013 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1016 attr
= new wxTreeItemAttr
;
1017 m_attrs
.Put(id
, (wxObject
*)attr
);
1020 attr
->SetFont(font
);
1023 // ----------------------------------------------------------------------------
1025 // ----------------------------------------------------------------------------
1027 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1029 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1032 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1033 // the HTREEITEM with TVM_GETITEMRECT
1034 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1036 // FALSE means get item rect for the whole item, not only text
1037 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1041 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1043 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1046 return tvItem
.cChildren
!= 0;
1049 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1051 // probably not a good idea to put it here
1052 //wxASSERT( ItemHasChildren(item) );
1054 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1057 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1060 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1062 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1065 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1068 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1070 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1073 return (tvItem
.state
& TVIS_BOLD
) != 0;
1076 // ----------------------------------------------------------------------------
1078 // ----------------------------------------------------------------------------
1080 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1082 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1085 wxTreeItemId
wxTreeCtrl::GetSelection() const
1087 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
1088 wxT("this only works with single selection controls") );
1090 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1093 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1095 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1098 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1099 long& _cookie
) const
1101 // remember the last child returned in 'cookie'
1102 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1104 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1107 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1108 long& _cookie
) const
1110 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1117 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1119 // can this be done more efficiently?
1122 wxTreeItemId childLast
,
1123 child
= GetFirstChild(item
, cookie
);
1124 while ( child
.IsOk() )
1127 child
= GetNextChild(item
, cookie
);
1133 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1135 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1138 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1140 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1143 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1145 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1148 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1150 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
1151 "for must be visible itself!"));
1153 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1156 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1158 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
1159 "for must be visible itself!"));
1161 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1164 // ----------------------------------------------------------------------------
1165 // multiple selections emulation
1166 // ----------------------------------------------------------------------------
1168 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1170 // receive the desired information.
1171 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1174 // state image indices are 1 based
1175 return ((tvItem
.state
>> 12) - 1) == 1;
1178 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1180 // receive the desired information.
1181 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1183 // state images are one-based
1184 tvItem
.state
= (check
? 2 : 1) << 12;
1189 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1191 TraverseSelections
selector(this, selections
);
1193 return selector
.GetCount();
1196 // ----------------------------------------------------------------------------
1198 // ----------------------------------------------------------------------------
1200 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1201 wxTreeItemId hInsertAfter
,
1202 const wxString
& text
,
1203 int image
, int selectedImage
,
1204 wxTreeItemData
*data
)
1206 TV_INSERTSTRUCT tvIns
;
1207 tvIns
.hParent
= HITEM(parent
);
1208 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1210 // this is how we insert the item as the first child: supply a NULL
1212 if ( !tvIns
.hInsertAfter
)
1214 tvIns
.hInsertAfter
= TVI_FIRST
;
1218 if ( !text
.IsEmpty() )
1221 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1227 tvIns
.item
.iImage
= image
;
1229 if ( selectedImage
== -1 )
1231 // take the same image for selected icon if not specified
1232 selectedImage
= image
;
1236 if ( selectedImage
!= -1 )
1238 mask
|= TVIF_SELECTEDIMAGE
;
1239 tvIns
.item
.iSelectedImage
= selectedImage
;
1245 tvIns
.item
.lParam
= (LPARAM
)data
;
1248 tvIns
.item
.mask
= mask
;
1250 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1253 wxLogLastError("TreeView_InsertItem");
1258 // associate the application tree item with Win32 tree item handle
1259 data
->SetId((WXHTREEITEM
)id
);
1262 return wxTreeItemId((WXHTREEITEM
)id
);
1265 // for compatibility only
1266 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1267 const wxString
& text
,
1268 int image
, int selImage
,
1271 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1272 image
, selImage
, NULL
);
1275 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1276 int image
, int selectedImage
,
1277 wxTreeItemData
*data
)
1279 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1280 text
, image
, selectedImage
, data
);
1283 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1284 const wxString
& text
,
1285 int image
, int selectedImage
,
1286 wxTreeItemData
*data
)
1288 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1289 text
, image
, selectedImage
, data
);
1292 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1293 const wxTreeItemId
& idPrevious
,
1294 const wxString
& text
,
1295 int image
, int selectedImage
,
1296 wxTreeItemData
*data
)
1298 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1301 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1303 const wxString
& text
,
1304 int image
, int selectedImage
,
1305 wxTreeItemData
*data
)
1307 // find the item from index
1309 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1310 while ( index
!= 0 && idCur
.IsOk() )
1315 idCur
= GetNextChild(parent
, cookie
);
1318 // assert, not check: if the index is invalid, we will append the item
1320 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1322 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1325 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1326 const wxString
& text
,
1327 int image
, int selectedImage
,
1328 wxTreeItemData
*data
)
1330 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1331 text
, image
, selectedImage
, data
);
1334 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1336 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1338 wxLogLastError("TreeView_DeleteItem");
1342 // delete all children (but don't delete the item itself)
1343 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1347 wxArrayLong children
;
1348 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1349 while ( child
.IsOk() )
1351 children
.Add((long)(WXHTREEITEM
)child
);
1353 child
= GetNextChild(item
, cookie
);
1356 size_t nCount
= children
.Count();
1357 for ( size_t n
= 0; n
< nCount
; n
++ )
1359 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1361 wxLogLastError("TreeView_DeleteItem");
1366 void wxTreeCtrl::DeleteAllItems()
1368 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1370 wxLogLastError("TreeView_DeleteAllItems");
1374 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1376 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1377 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1378 flag
== TVE_EXPAND
||
1380 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1382 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1383 // emulate them. This behaviour has changed slightly with comctl32.dll
1384 // v 4.70 - now it does send them but only the first time. To maintain
1385 // compatible behaviour and also in order to not have surprises with the
1386 // future versions, don't rely on this and still do everything ourselves.
1387 // To avoid that the messages be sent twice when the item is expanded for
1388 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1390 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1394 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1396 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1397 event
.m_item
= item
;
1399 bool isExpanded
= IsExpanded(item
);
1401 event
.SetEventObject(this);
1403 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1404 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1405 GetEventHandler()->ProcessEvent(event
);
1407 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1408 GetEventHandler()->ProcessEvent(event
);
1410 //else: change didn't took place, so do nothing at all
1413 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1415 DoExpand(item
, TVE_EXPAND
);
1418 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1420 DoExpand(item
, TVE_COLLAPSE
);
1423 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1425 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1428 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1430 DoExpand(item
, TVE_TOGGLE
);
1433 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1435 DoExpand(item
, action
);
1438 void wxTreeCtrl::Unselect()
1440 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1441 wxT("doesn't make sense, may be you want UnselectAll()?") );
1443 // just remove the selection
1444 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1447 void wxTreeCtrl::UnselectAll()
1449 if ( m_windowStyle
& wxTR_MULTIPLE
)
1451 wxArrayTreeItemIds selections
;
1452 size_t count
= GetSelections(selections
);
1453 for ( size_t n
= 0; n
< count
; n
++ )
1455 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1456 SetItemCheck(selections
[n
], FALSE
);
1457 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1458 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1459 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1464 // just remove the selection
1469 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1471 if ( m_windowStyle
& wxTR_MULTIPLE
)
1473 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1474 // selecting the item means checking it
1476 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1477 ::SelectItem(GetHwnd(), HITEM(item
));
1478 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1482 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1483 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1484 // send them ourselves
1486 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1487 event
.m_item
= item
;
1488 event
.SetEventObject(this);
1490 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1491 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1493 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1495 wxLogLastError("TreeView_SelectItem");
1499 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1500 (void)GetEventHandler()->ProcessEvent(event
);
1503 //else: program vetoed the change
1507 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1510 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1513 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1515 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1517 wxLogLastError("TreeView_SelectSetFirstVisible");
1521 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1523 // normally, we could try to do something like this to return something
1524 // even when the editing was started by the user and not by calling
1525 // EditLabel() - but as nobody has asked for this so far and there might be
1526 // problems in the code below, I leave it disabled for now (VZ)
1530 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1533 m_textCtrl
= new wxTextCtrl(this, -1);
1535 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1537 //else: not editing label right now
1544 void wxTreeCtrl::DeleteTextCtrl()
1548 m_textCtrl
->UnsubclassWin();
1549 m_textCtrl
->SetHWND(0);
1555 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1556 wxClassInfo
* textControlClass
)
1558 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1562 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1564 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1571 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1572 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1573 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1578 // End label editing, optionally cancelling the edit
1579 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1581 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1586 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1588 TV_HITTESTINFO hitTestInfo
;
1589 hitTestInfo
.pt
.x
= (int)point
.x
;
1590 hitTestInfo
.pt
.y
= (int)point
.y
;
1592 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1597 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1598 flags |= wxTREE_HITTEST_##flag
1600 TRANSLATE_FLAG(ABOVE
);
1601 TRANSLATE_FLAG(BELOW
);
1602 TRANSLATE_FLAG(NOWHERE
);
1603 TRANSLATE_FLAG(ONITEMBUTTON
);
1604 TRANSLATE_FLAG(ONITEMICON
);
1605 TRANSLATE_FLAG(ONITEMINDENT
);
1606 TRANSLATE_FLAG(ONITEMLABEL
);
1607 TRANSLATE_FLAG(ONITEMRIGHT
);
1608 TRANSLATE_FLAG(ONITEMSTATEICON
);
1609 TRANSLATE_FLAG(TOLEFT
);
1610 TRANSLATE_FLAG(TORIGHT
);
1612 #undef TRANSLATE_FLAG
1614 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1617 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1619 bool textOnly
) const
1622 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1625 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1631 // couldn't retrieve rect: for example, item isn't visible
1636 // ----------------------------------------------------------------------------
1638 // ----------------------------------------------------------------------------
1640 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1641 wxTreeItemData
*pItem2
,
1644 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1645 wxT("sorting tree without data doesn't make sense") );
1647 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1650 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1651 const wxTreeItemId
& item2
)
1653 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1656 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1658 // rely on the fact that TreeView_SortChildren does the same thing as our
1659 // default behaviour, i.e. sorts items alphabetically and so call it
1660 // directly if we're not in derived class (much more efficient!)
1661 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1663 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1668 tvSort
.hParent
= HITEM(item
);
1669 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1670 tvSort
.lParam
= (LPARAM
)this;
1671 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1675 // ----------------------------------------------------------------------------
1677 // ----------------------------------------------------------------------------
1679 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1681 if ( cmd
== EN_UPDATE
)
1683 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1684 event
.SetEventObject( this );
1685 ProcessCommand(event
);
1687 else if ( cmd
== EN_KILLFOCUS
)
1689 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1690 event
.SetEventObject( this );
1691 ProcessCommand(event
);
1699 // command processed
1703 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1704 // only do it during dragging, minimize wxWin overhead (this is important for
1705 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1706 // instead of passing by wxWin events
1707 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1709 bool processed
= FALSE
;
1712 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1714 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1716 // we only process mouse messages here and these parameters have the same
1717 // meaning for all of them
1718 int x
= GET_X_LPARAM(lParam
),
1719 y
= GET_Y_LPARAM(lParam
);
1720 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1724 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1725 case WM_LBUTTONDOWN
:
1726 if ( htItem
&& isMultiple
)
1728 if ( wParam
& MK_CONTROL
)
1732 // toggle selected state
1733 ToggleItemSelection(GetHwnd(), htItem
);
1735 ::SetFocus(GetHwnd(), htItem
);
1737 // reset on any click without Shift
1742 else if ( wParam
& MK_SHIFT
)
1744 // this selects all items between the starting one and
1747 if ( !m_htSelStart
)
1749 // take the focused item
1750 m_htSelStart
= (WXHTREEITEM
)
1751 TreeView_GetSelection(GetHwnd());
1754 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1755 !(wParam
& MK_CONTROL
));
1757 ::SetFocus(GetHwnd(), htItem
);
1761 else // normal click
1763 // clear the selection and then let the default handler
1767 // prevent the click from starting in-place editing
1768 // when there was no selection in the control
1769 TreeView_SelectItem(GetHwnd(), 0);
1771 // reset on any click without Shift
1776 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1781 m_dragImage
->Move(wxPoint(x
, y
), this);
1784 // highlight the item as target (hiding drag image is
1785 // necessary - otherwise the display will be corrupted)
1786 m_dragImage
->Hide(this);
1787 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1788 m_dragImage
->Show(this);
1797 m_dragImage
->EndDrag(this);
1801 // generate the drag end event
1802 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1804 event
.m_item
= (WXHTREEITEM
)htItem
;
1805 event
.m_pointDrag
= wxPoint(x
, y
);
1806 event
.SetEventObject(this);
1808 (void)GetEventHandler()->ProcessEvent(event
);
1810 // if we don't do it, the tree seems to think that 2 items
1811 // are selected simultaneously which is quite weird
1812 TreeView_SelectDropTarget(GetHwnd(), 0);
1817 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1818 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1820 // the tree control greys out the selected item when it loses focus and
1821 // paints it as selected again when it regains it, but it won't do it
1822 // for the other items itself - help it
1823 wxArrayTreeItemIds selections
;
1824 size_t count
= GetSelections(selections
);
1826 for ( size_t n
= 0; n
< count
; n
++ )
1828 // TreeView_GetItemRect() will return FALSE if item is not visible,
1829 // which may happen perfectly well
1830 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1833 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1837 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1839 bool bCtrl
= wxIsCtrlDown(),
1840 bShift
= wxIsShiftDown();
1842 // we handle.arrows and space, but not page up/down and home/end: the
1843 // latter should be easy, but not the former
1845 HTREEITEM htSel
= TreeView_GetSelection(GetHwnd());
1846 if ( !m_htSelStart
)
1848 m_htSelStart
= (WXHTREEITEM
)htSel
;
1851 if ( wParam
== VK_SPACE
)
1855 ToggleItemSelection(GetHwnd(), htSel
);
1861 ::SelectItem(GetHwnd(), htSel
);
1866 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1868 if ( !bCtrl
&& !bShift
)
1870 // no modifiers, just clear selection and then let the default
1871 // processing to take place
1876 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1879 wParam
== VK_UP
? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1880 : TreeView_GetNextVisible(GetHwnd(), htSel
);
1884 // at the top/bottom
1890 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1894 // without changing selection
1895 ::SetFocus(GetHwnd(), htNext
);
1902 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1905 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1910 // process WM_NOTIFY Windows message
1911 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1913 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1914 wxEventType eventType
= wxEVT_NULL
;
1915 NMHDR
*hdr
= (NMHDR
*)lParam
;
1917 switch ( hdr
->code
)
1921 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1924 TV_HITTESTINFO tvhti
;
1925 ::GetCursorPos(&(tvhti
.pt
));
1926 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1927 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1929 if( tvhti
.flags
& TVHT_ONITEM
)
1931 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1932 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1939 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1942 case TVN_BEGINRDRAG
:
1944 if ( eventType
== wxEVT_NULL
)
1945 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1946 //else: left drag, already set above
1948 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1950 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1951 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1953 // don't allow dragging by default: the user code must
1954 // explicitly say that it wants to allow it to avoid breaking
1960 case TVN_BEGINLABELEDIT
:
1962 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1963 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1965 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1966 event
.m_label
= info
->item
.pszText
;
1970 case TVN_DELETEITEM
:
1972 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1973 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1975 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1979 delete (wxTreeItemAttr
*)m_attrs
.
1980 Delete((long)tv
->itemOld
.hItem
);
1985 case TVN_ENDLABELEDIT
:
1987 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1988 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1990 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1991 event
.m_label
= info
->item
.pszText
;
1992 if (info
->item
.pszText
== NULL
)
1997 case TVN_GETDISPINFO
:
1998 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2001 case TVN_SETDISPINFO
:
2003 if ( eventType
== wxEVT_NULL
)
2004 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2005 //else: get, already set above
2007 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2009 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2013 case TVN_ITEMEXPANDING
:
2014 event
.m_code
= FALSE
;
2017 case TVN_ITEMEXPANDED
:
2019 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2021 bool expand
= FALSE
;
2022 switch ( tv
->action
)
2033 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
2034 "message"), tv
->action
);
2037 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2038 eventType
= g_events
[expand
][ing
];
2040 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2046 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2047 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2049 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
2051 // a separate event for Space/Return
2052 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2053 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2055 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2057 event2
.SetEventObject(this);
2058 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2060 event2
.m_item
= GetSelection();
2062 //else: don't know how to get it
2064 (void)GetEventHandler()->ProcessEvent(event2
);
2069 case TVN_SELCHANGED
:
2070 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2073 case TVN_SELCHANGING
:
2075 if ( eventType
== wxEVT_NULL
)
2076 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2077 //else: already set above
2079 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2081 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2082 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2086 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
2089 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2090 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2091 switch( nmcd
.dwDrawStage
)
2094 // if we've got any items with non standard attributes,
2095 // notify us before painting each item
2096 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2100 case CDDS_ITEMPREPAINT
:
2102 wxTreeItemAttr
*attr
=
2103 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2107 // nothing to do for this item
2108 return CDRF_DODEFAULT
;
2112 wxColour colText
, colBack
;
2113 if ( attr
->HasFont() )
2115 wxFont font
= attr
->GetFont();
2116 hFont
= (HFONT
)font
.GetResourceHandle();
2123 if ( attr
->HasTextColour() )
2125 colText
= attr
->GetTextColour();
2129 colText
= GetForegroundColour();
2132 // selection colours should override ours
2133 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2135 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2136 lptvcd
->clrTextBk
= clrBk
;
2138 // try to make the text visible
2139 lptvcd
->clrText
= wxColourToRGB(colText
);
2140 lptvcd
->clrText
|= ~clrBk
;
2141 lptvcd
->clrText
&= 0x00ffffff;
2145 if ( attr
->HasBackgroundColour() )
2147 colBack
= attr
->GetBackgroundColour();
2151 colBack
= GetBackgroundColour();
2154 lptvcd
->clrText
= wxColourToRGB(colText
);
2155 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2158 // note that if we wanted to set colours for
2159 // individual columns (subitems), we would have
2160 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2163 ::SelectObject(nmcd
.hdc
, hFont
);
2165 *result
= CDRF_NEWFONT
;
2169 *result
= CDRF_DODEFAULT
;
2176 *result
= CDRF_DODEFAULT
;
2181 #endif // _WIN32_IE >= 0x300
2184 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2187 event
.SetEventObject(this);
2188 event
.SetEventType(eventType
);
2190 bool processed
= GetEventHandler()->ProcessEvent(event
);
2193 switch ( hdr
->code
)
2196 case TVN_BEGINRDRAG
:
2197 if ( event
.IsAllowed() )
2199 // normally this is impossible because the m_dragImage is
2200 // deleted once the drag operation is over
2201 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2203 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2204 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2205 m_dragImage
->Show(this);
2209 case TVN_DELETEITEM
:
2211 // NB: we might process this message using wxWindows event
2212 // tables, but due to overhead of wxWin event system we
2213 // prefer to do it here ourself (otherwise deleting a tree
2214 // with many items is just too slow)
2215 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2217 wxTreeItemId item
= event
.m_item
;
2218 if ( HasIndirectData(item
) )
2220 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2222 delete data
; // can't be NULL here
2224 m_itemsWithIndirectData
.Remove(item
);
2228 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2229 delete data
; // may be NULL, ok
2232 processed
= TRUE
; // Make sure we don't get called twice
2236 case TVN_BEGINLABELEDIT
:
2237 // return TRUE to cancel label editing
2238 *result
= !event
.IsAllowed();
2241 case TVN_ENDLABELEDIT
:
2242 // return TRUE to set the label to the new string
2243 *result
= event
.IsAllowed();
2245 // ensure that we don't have the text ctrl which is going to be
2250 case TVN_SELCHANGING
:
2251 case TVN_ITEMEXPANDING
:
2252 // return TRUE to prevent the action from happening
2253 *result
= !event
.IsAllowed();
2256 case TVN_GETDISPINFO
:
2257 // NB: so far the user can't set the image himself anyhow, so do it
2258 // anyway - but this may change later
2259 if ( /* !processed && */ 1 )
2261 wxTreeItemId item
= event
.m_item
;
2262 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2263 if ( info
->item
.mask
& TVIF_IMAGE
)
2266 DoGetItemImageFromData
2269 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2270 : wxTreeItemIcon_Normal
2273 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2275 info
->item
.iSelectedImage
=
2276 DoGetItemImageFromData
2279 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2280 : wxTreeItemIcon_Selected
2287 // for the other messages the return value is ignored and there is
2288 // nothing special to do
2294 // ----------------------------------------------------------------------------
2296 // ----------------------------------------------------------------------------
2298 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
2300 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
2301 : wxNotifyEvent(commandType
, id
)