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 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // wrapper for TreeView_HitTest
94 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
100 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
103 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
105 // wrappers for TreeView_GetItem/TreeView_SetItem
106 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
109 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
110 tvi
.stateMask
= TVIS_SELECTED
;
113 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
115 wxLogLastError("TreeView_GetItem");
118 return (tvi
.state
& TVIS_SELECTED
) != 0;
121 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
124 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
125 tvi
.stateMask
= TVIS_SELECTED
;
126 tvi
.state
= select
? TVIS_SELECTED
: 0;
129 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
131 wxLogLastError("TreeView_SetItem");
135 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
137 SelectItem(hwndTV
, htItem
, FALSE
);
140 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
142 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
145 // helper function which selects all items in a range and, optionally,
146 // unselects all others
147 static void SelectRange(HWND hwndTV
,
150 bool unselectOthers
= TRUE
)
152 // find the first (or last) item and select it
154 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
155 while ( htItem
&& cont
)
157 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
159 if ( !IsItemSelected(hwndTV
, htItem
) )
161 SelectItem(hwndTV
, htItem
);
168 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
170 UnselectItem(hwndTV
, htItem
);
174 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
177 // select the items in range
178 cont
= htFirst
!= htLast
;
179 while ( htItem
&& cont
)
181 if ( !IsItemSelected(hwndTV
, htItem
) )
183 SelectItem(hwndTV
, htItem
);
186 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
188 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
192 if ( unselectOthers
)
196 if ( IsItemSelected(hwndTV
, htItem
) )
198 UnselectItem(hwndTV
, htItem
);
201 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
205 // seems to be necessary - otherwise the just selected items don't always
206 // appear as selected
207 UpdateWindow(hwndTV
);
210 // helper function which tricks the standard control into changing the focused
211 // item without changing anything else (if someone knows why Microsoft doesn't
212 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
213 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
216 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
221 if ( htItem
!= htFocus
)
223 // remember the selection state of the item
224 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
226 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
228 // prevent the tree from unselecting the old focus which it
229 // would do by default (TreeView_SelectItem unselects the
231 TreeView_SelectItem(hwndTV
, 0);
232 SelectItem(hwndTV
, htFocus
);
235 TreeView_SelectItem(hwndTV
, htItem
);
239 // need to clear the selection which TreeView_SelectItem() gave
241 UnselectItem(hwndTV
, htItem
);
243 //else: was selected, still selected - ok
245 //else: nothing to do, focus already there
251 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
253 // just clear the focus
254 TreeView_SelectItem(hwndTV
, 0);
256 if ( wasFocusSelected
)
258 // restore the selection state
259 SelectItem(hwndTV
, htFocus
);
262 //else: nothing to do, no focus already
266 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
268 // ----------------------------------------------------------------------------
270 // ----------------------------------------------------------------------------
272 // a convenient wrapper around TV_ITEM struct which adds a ctor
274 #pragma warning( disable : 4097 ) // inheriting from typedef
277 struct wxTreeViewItem
: public TV_ITEM
279 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
280 UINT mask_
, // fields which are valid
281 UINT stateMask_
= 0) // for TVIF_STATE only
283 // hItem member is always valid
284 mask
= mask_
| TVIF_HANDLE
;
285 stateMask
= stateMask_
;
291 #pragma warning( default : 4097 )
294 // a class which encapsulates the tree traversal logic: it vists all (unless
295 // OnVisit() returns FALSE) items under the given one
296 class wxTreeTraversal
299 wxTreeTraversal(const wxTreeCtrl
*tree
)
304 // do traverse the tree: visit all items (recursively by default) under the
305 // given one; return TRUE if all items were traversed or FALSE if the
306 // traversal was aborted because OnVisit returned FALSE
307 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
309 // override this function to do whatever is needed for each item, return
310 // FALSE to stop traversing
311 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
314 const wxTreeCtrl
*GetTree() const { return m_tree
; }
317 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
319 const wxTreeCtrl
*m_tree
;
322 // internal class for getting the selected items
323 class TraverseSelections
: public wxTreeTraversal
326 TraverseSelections(const wxTreeCtrl
*tree
,
327 wxArrayTreeItemIds
& selections
)
328 : wxTreeTraversal(tree
), m_selections(selections
)
330 m_selections
.Empty();
332 DoTraverse(tree
->GetRootItem());
335 virtual bool OnVisit(const wxTreeItemId
& item
)
337 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
338 if ( GetTree()->IsItemChecked(item
) )
340 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
343 m_selections
.Add(item
);
349 size_t GetCount() const { return m_selections
.GetCount(); }
352 wxArrayTreeItemIds
& m_selections
;
355 // internal class for counting tree items
356 class TraverseCounter
: public wxTreeTraversal
359 TraverseCounter(const wxTreeCtrl
*tree
,
360 const wxTreeItemId
& root
,
362 : wxTreeTraversal(tree
)
366 DoTraverse(root
, recursively
);
369 virtual bool OnVisit(const wxTreeItemId
& item
)
376 size_t GetCount() const { return m_count
; }
382 // ----------------------------------------------------------------------------
383 // This class is needed for support of different images: the Win32 common
384 // control natively supports only 2 images (the normal one and another for the
385 // selected state). We wish to provide support for 2 more of them for folder
386 // items (i.e. those which have children): for expanded state and for expanded
387 // selected state. For this we use this structure to store the additional items
390 // There is only one problem with this: when we retrieve the item's data, we
391 // don't know whether we get a pointer to wxTreeItemData or
392 // wxTreeItemIndirectData. So we have to maintain a list of all items which
393 // have indirect data inside the listctrl itself.
394 // ----------------------------------------------------------------------------
396 class wxTreeItemIndirectData
399 // ctor associates this data with the item and the real item data becomes
400 // available through our GetData() method
401 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
403 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
409 m_data
= tree
->GetItemData(item
);
411 // and set ourselves as the new one
412 tree
->SetIndirectItemData(item
, this);
415 // dtor deletes the associated data as well
416 ~wxTreeItemIndirectData() { delete m_data
; }
419 // get the real data associated with the item
420 wxTreeItemData
*GetData() const { return m_data
; }
422 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
424 // do we have such image?
425 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
427 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
429 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
432 // all the images associated with the item
433 int m_images
[wxTreeItemIcon_Max
];
435 wxTreeItemData
*m_data
;
438 // ----------------------------------------------------------------------------
440 // ----------------------------------------------------------------------------
442 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 // handy table for sending events
449 static const wxEventType g_events
[2][2] =
451 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
452 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
455 // ============================================================================
457 // ============================================================================
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
465 if ( !OnVisit(root
) )
468 return Traverse(root
, recursively
);
471 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
474 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
475 while ( child
.IsOk() )
477 // depth first traversal
478 if ( recursively
&& !Traverse(child
, TRUE
) )
481 if ( !OnVisit(child
) )
484 child
= m_tree
->GetNextChild(root
, cookie
);
490 // ----------------------------------------------------------------------------
491 // construction and destruction
492 // ----------------------------------------------------------------------------
494 void wxTreeCtrl::Init()
496 m_imageListNormal
= NULL
;
497 m_imageListState
= NULL
;
499 m_hasAnyAttr
= FALSE
;
505 bool wxTreeCtrl::Create(wxWindow
*parent
,
510 const wxValidator
& validator
,
511 const wxString
& name
)
515 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
518 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
519 TVS_HASLINES
| TVS_SHOWSELALWAYS
;
521 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
522 wstyle
|= TVS_HASBUTTONS
;
524 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
525 wstyle
|= TVS_EDITLABELS
;
527 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
528 wstyle
|= TVS_LINESATROOT
;
530 // using TVS_CHECKBOXES for emulation of a multiselection tree control
531 // doesn't work without the new enough headers
532 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
533 !defined( __GNUWIN32_OLD__ ) && \
534 !defined( __BORLANDC__ ) && \
535 !defined( __WATCOMC__ ) && \
536 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
538 // we emulate the multiple selection tree controls by using checkboxes: set
539 // up the image list we need for this if we do have multiple selections
540 if ( m_windowStyle
& wxTR_MULTIPLE
)
541 wstyle
|= TVS_CHECKBOXES
;
542 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
544 // Create the tree control.
545 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
548 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
549 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
551 // VZ: this is some experimental code which may be used to get the
552 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
553 // AFAIK, the standard DLL does about the same thing anyhow.
555 if ( m_windowStyle
& wxTR_MULTIPLE
)
559 // create the DC compatible with the current screen
560 HDC hdcMem
= CreateCompatibleDC(NULL
);
562 // create a mono bitmap of the standard size
563 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
564 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
565 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
566 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
567 1, // # of color planes
568 1, // # bits needed for one pixel
569 0); // array containing colour data
570 SelectObject(hdcMem
, hbmpCheck
);
572 // then draw a check mark into it
573 RECT rect
= { 0, 0, x
, y
};
574 if ( !::DrawFrameControl(hdcMem
, &rect
,
576 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
578 wxLogLastError(wxT("DrawFrameControl(check)"));
581 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
582 imagelistCheckboxes
.Add(bmp
);
584 if ( !::DrawFrameControl(hdcMem
, &rect
,
588 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
591 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
592 imagelistCheckboxes
.Add(bmp
);
598 SetStateImageList(&imagelistCheckboxes
);
602 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
607 wxTreeCtrl::~wxTreeCtrl()
609 // delete any attributes
612 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
614 delete (wxTreeItemAttr
*)node
->Data();
617 // prevent TVN_DELETEITEM handler from deleting the attributes again!
618 m_hasAnyAttr
= FALSE
;
623 // delete user data to prevent memory leaks
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
631 // simple wrappers which add error checking in debug mode
633 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
635 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
637 wxLogLastError("TreeView_GetItem");
645 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
647 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
649 wxLogLastError("TreeView_SetItem");
653 size_t wxTreeCtrl::GetCount() const
655 return (size_t)TreeView_GetCount(GetHwnd());
658 unsigned int wxTreeCtrl::GetIndent() const
660 return TreeView_GetIndent(GetHwnd());
663 void wxTreeCtrl::SetIndent(unsigned int indent
)
665 TreeView_SetIndent(GetHwnd(), indent
);
668 wxImageList
*wxTreeCtrl::GetImageList() const
670 return m_imageListNormal
;
673 wxImageList
*wxTreeCtrl::GetStateImageList() const
675 return m_imageListNormal
;
678 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
681 TreeView_SetImageList(GetHwnd(),
682 imageList
? imageList
->GetHIMAGELIST() : 0,
686 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
688 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
691 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
693 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
696 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
697 bool recursively
) const
699 TraverseCounter
counter(this, item
, recursively
);
701 return counter
.GetCount() - 1;
704 // ----------------------------------------------------------------------------
706 // ----------------------------------------------------------------------------
708 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
710 if ( !wxWindowBase::SetBackgroundColour(colour
) )
713 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
718 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
720 if ( !wxWindowBase::SetForegroundColour(colour
) )
723 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
728 // ----------------------------------------------------------------------------
730 // ----------------------------------------------------------------------------
732 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
734 wxChar buf
[512]; // the size is arbitrary...
736 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
737 tvItem
.pszText
= buf
;
738 tvItem
.cchTextMax
= WXSIZEOF(buf
);
739 if ( !DoGetItem(&tvItem
) )
741 // don't return some garbage which was on stack, but an empty string
745 return wxString(buf
);
748 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
750 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
751 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
755 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
756 wxTreeItemIcon which
) const
758 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
759 if ( !DoGetItem(&tvItem
) )
764 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
767 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
769 wxTreeItemIcon which
) const
771 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
772 if ( !DoGetItem(&tvItem
) )
777 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
779 data
->SetImage(image
, which
);
781 // make sure that we have selected images as well
782 if ( which
== wxTreeItemIcon_Normal
&&
783 !data
->HasImage(wxTreeItemIcon_Selected
) )
785 data
->SetImage(image
, wxTreeItemIcon_Selected
);
788 if ( which
== wxTreeItemIcon_Expanded
&&
789 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
791 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
795 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
799 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
800 tvItem
.iSelectedImage
= imageSel
;
801 tvItem
.iImage
= image
;
805 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
806 wxTreeItemIcon which
) const
808 if ( HasIndirectData(item
) )
810 return DoGetItemImageFromData(item
, which
);
817 wxFAIL_MSG( wxT("unknown tree item image type") );
819 case wxTreeItemIcon_Normal
:
823 case wxTreeItemIcon_Selected
:
824 mask
= TVIF_SELECTEDIMAGE
;
827 case wxTreeItemIcon_Expanded
:
828 case wxTreeItemIcon_SelectedExpanded
:
832 wxTreeViewItem
tvItem(item
, mask
);
835 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
838 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
839 wxTreeItemIcon which
)
841 int imageNormal
, imageSel
;
845 wxFAIL_MSG( wxT("unknown tree item image type") );
847 case wxTreeItemIcon_Normal
:
849 imageSel
= GetItemSelectedImage(item
);
852 case wxTreeItemIcon_Selected
:
853 imageNormal
= GetItemImage(item
);
857 case wxTreeItemIcon_Expanded
:
858 case wxTreeItemIcon_SelectedExpanded
:
859 if ( !HasIndirectData(item
) )
861 // we need to get the old images first, because after we create
862 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
864 imageNormal
= GetItemImage(item
);
865 imageSel
= GetItemSelectedImage(item
);
867 // if it doesn't have it yet, add it
868 wxTreeItemIndirectData
*data
= new
869 wxTreeItemIndirectData(this, item
);
871 // copy the data to the new location
872 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
873 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
876 DoSetItemImageFromData(item
, image
, which
);
878 // reset the normal/selected images because we won't use them any
879 // more - now they're stored inside the indirect data
881 imageSel
= I_IMAGECALLBACK
;
885 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
886 // change both normal and selected image - otherwise the change simply
887 // doesn't take place!
888 DoSetItemImages(item
, imageNormal
, imageSel
);
891 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
893 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
894 if ( !DoGetItem(&tvItem
) )
899 if ( HasIndirectData(item
) )
901 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
905 return (wxTreeItemData
*)tvItem
.lParam
;
909 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
911 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
913 if ( HasIndirectData(item
) )
915 if ( DoGetItem(&tvItem
) )
917 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
921 wxFAIL_MSG( wxT("failed to change tree items data") );
926 tvItem
.lParam
= (LPARAM
)data
;
931 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
932 wxTreeItemIndirectData
*data
)
934 // this should never happen because it's unnecessary and will probably lead
935 // to crash too because the code elsewhere supposes that the pointer the
936 // wxTreeItemIndirectData has is a real wxItemData and not
937 // wxTreeItemIndirectData as well
938 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
940 SetItemData(item
, (wxTreeItemData
*)data
);
942 m_itemsWithIndirectData
.Add(item
);
945 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
947 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
950 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
952 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
953 tvItem
.cChildren
= (int)has
;
957 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
959 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
960 tvItem
.state
= bold
? TVIS_BOLD
: 0;
964 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
966 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
967 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
971 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
976 long id
= (long)(WXHTREEITEM
)item
;
977 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
980 attr
= new wxTreeItemAttr
;
981 m_attrs
.Put(id
, (wxObject
*)attr
);
984 attr
->SetTextColour(col
);
987 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
992 long id
= (long)(WXHTREEITEM
)item
;
993 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
996 attr
= new wxTreeItemAttr
;
997 m_attrs
.Put(id
, (wxObject
*)attr
);
1000 attr
->SetBackgroundColour(col
);
1003 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1005 m_hasAnyAttr
= TRUE
;
1007 long id
= (long)(WXHTREEITEM
)item
;
1008 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1011 attr
= new wxTreeItemAttr
;
1012 m_attrs
.Put(id
, (wxObject
*)attr
);
1015 attr
->SetFont(font
);
1018 // ----------------------------------------------------------------------------
1020 // ----------------------------------------------------------------------------
1022 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1024 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1027 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1028 // the HTREEITEM with TVM_GETITEMRECT
1029 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1031 // FALSE means get item rect for the whole item, not only text
1032 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1036 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1038 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1041 return tvItem
.cChildren
!= 0;
1044 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1046 // probably not a good idea to put it here
1047 //wxASSERT( ItemHasChildren(item) );
1049 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1052 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1055 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1057 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1060 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1063 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1065 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1068 return (tvItem
.state
& TVIS_BOLD
) != 0;
1071 // ----------------------------------------------------------------------------
1073 // ----------------------------------------------------------------------------
1075 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1077 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1080 wxTreeItemId
wxTreeCtrl::GetSelection() const
1082 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (WXHTREEITEM
)0,
1083 wxT("this only works with single selection controls") );
1085 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1088 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1090 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1093 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1094 long& _cookie
) const
1096 // remember the last child returned in 'cookie'
1097 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1099 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1102 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1103 long& _cookie
) const
1105 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1112 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1114 // can this be done more efficiently?
1117 wxTreeItemId childLast
,
1118 child
= GetFirstChild(item
, cookie
);
1119 while ( child
.IsOk() )
1122 child
= GetNextChild(item
, cookie
);
1128 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1130 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1133 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1135 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1138 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1140 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1143 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1145 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() "
1146 "for must be visible itself!"));
1148 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1151 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1153 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() "
1154 "for must be visible itself!"));
1156 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1159 // ----------------------------------------------------------------------------
1160 // multiple selections emulation
1161 // ----------------------------------------------------------------------------
1163 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1165 // receive the desired information.
1166 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1169 // state image indices are 1 based
1170 return ((tvItem
.state
>> 12) - 1) == 1;
1173 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1175 // receive the desired information.
1176 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1178 // state images are one-based
1179 tvItem
.state
= (check
? 2 : 1) << 12;
1184 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1186 TraverseSelections
selector(this, selections
);
1188 return selector
.GetCount();
1191 // ----------------------------------------------------------------------------
1193 // ----------------------------------------------------------------------------
1195 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1196 wxTreeItemId hInsertAfter
,
1197 const wxString
& text
,
1198 int image
, int selectedImage
,
1199 wxTreeItemData
*data
)
1201 TV_INSERTSTRUCT tvIns
;
1202 tvIns
.hParent
= HITEM(parent
);
1203 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1205 // this is how we insert the item as the first child: supply a NULL
1207 if ( !tvIns
.hInsertAfter
)
1209 tvIns
.hInsertAfter
= TVI_FIRST
;
1213 if ( !text
.IsEmpty() )
1216 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1222 tvIns
.item
.iImage
= image
;
1224 if ( selectedImage
== -1 )
1226 // take the same image for selected icon if not specified
1227 selectedImage
= image
;
1231 if ( selectedImage
!= -1 )
1233 mask
|= TVIF_SELECTEDIMAGE
;
1234 tvIns
.item
.iSelectedImage
= selectedImage
;
1240 tvIns
.item
.lParam
= (LPARAM
)data
;
1243 tvIns
.item
.mask
= mask
;
1245 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1248 wxLogLastError("TreeView_InsertItem");
1253 // associate the application tree item with Win32 tree item handle
1254 data
->SetId((WXHTREEITEM
)id
);
1257 return wxTreeItemId((WXHTREEITEM
)id
);
1260 // for compatibility only
1261 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1262 const wxString
& text
,
1263 int image
, int selImage
,
1266 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1267 image
, selImage
, NULL
);
1270 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1271 int image
, int selectedImage
,
1272 wxTreeItemData
*data
)
1274 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1275 text
, image
, selectedImage
, data
);
1278 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1279 const wxString
& text
,
1280 int image
, int selectedImage
,
1281 wxTreeItemData
*data
)
1283 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1284 text
, image
, selectedImage
, data
);
1287 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1288 const wxTreeItemId
& idPrevious
,
1289 const wxString
& text
,
1290 int image
, int selectedImage
,
1291 wxTreeItemData
*data
)
1293 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1296 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1298 const wxString
& text
,
1299 int image
, int selectedImage
,
1300 wxTreeItemData
*data
)
1302 // find the item from index
1304 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1305 while ( index
!= 0 && idCur
.IsOk() )
1310 idCur
= GetNextChild(parent
, cookie
);
1313 // assert, not check: if the index is invalid, we will append the item
1315 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1317 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1320 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1321 const wxString
& text
,
1322 int image
, int selectedImage
,
1323 wxTreeItemData
*data
)
1325 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1326 text
, image
, selectedImage
, data
);
1329 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1331 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1333 wxLogLastError("TreeView_DeleteItem");
1337 // delete all children (but don't delete the item itself)
1338 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1342 wxArrayLong children
;
1343 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1344 while ( child
.IsOk() )
1346 children
.Add((long)(WXHTREEITEM
)child
);
1348 child
= GetNextChild(item
, cookie
);
1351 size_t nCount
= children
.Count();
1352 for ( size_t n
= 0; n
< nCount
; n
++ )
1354 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1356 wxLogLastError("TreeView_DeleteItem");
1361 void wxTreeCtrl::DeleteAllItems()
1363 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1365 wxLogLastError("TreeView_DeleteAllItems");
1369 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1371 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1372 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1373 flag
== TVE_EXPAND
||
1375 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1377 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1378 // emulate them. This behaviour has changed slightly with comctl32.dll
1379 // v 4.70 - now it does send them but only the first time. To maintain
1380 // compatible behaviour and also in order to not have surprises with the
1381 // future versions, don't rely on this and still do everything ourselves.
1382 // To avoid that the messages be sent twice when the item is expanded for
1383 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1385 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1389 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1391 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1392 event
.m_item
= item
;
1394 bool isExpanded
= IsExpanded(item
);
1396 event
.SetEventObject(this);
1398 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1399 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1400 GetEventHandler()->ProcessEvent(event
);
1402 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1403 GetEventHandler()->ProcessEvent(event
);
1405 //else: change didn't took place, so do nothing at all
1408 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1410 DoExpand(item
, TVE_EXPAND
);
1413 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1415 DoExpand(item
, TVE_COLLAPSE
);
1418 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1420 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1423 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1425 DoExpand(item
, TVE_TOGGLE
);
1428 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1430 DoExpand(item
, action
);
1433 void wxTreeCtrl::Unselect()
1435 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1436 wxT("doesn't make sense, may be you want UnselectAll()?") );
1438 // just remove the selection
1439 SelectItem(wxTreeItemId((WXHTREEITEM
) 0));
1442 void wxTreeCtrl::UnselectAll()
1444 if ( m_windowStyle
& wxTR_MULTIPLE
)
1446 wxArrayTreeItemIds selections
;
1447 size_t count
= GetSelections(selections
);
1448 for ( size_t n
= 0; n
< count
; n
++ )
1450 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1451 SetItemCheck(selections
[n
], FALSE
);
1452 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1453 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1454 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1459 // just remove the selection
1464 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1466 if ( m_windowStyle
& wxTR_MULTIPLE
)
1468 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1469 // selecting the item means checking it
1471 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1472 ::SelectItem(GetHwnd(), HITEM(item
));
1473 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1477 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1478 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1479 // send them ourselves
1481 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1482 event
.m_item
= item
;
1483 event
.SetEventObject(this);
1485 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1486 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1488 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1490 wxLogLastError("TreeView_SelectItem");
1494 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1495 (void)GetEventHandler()->ProcessEvent(event
);
1498 //else: program vetoed the change
1502 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1505 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1508 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1510 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1512 wxLogLastError("TreeView_SelectSetFirstVisible");
1516 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1518 // normally, we could try to do something like this to return something
1519 // even when the editing was started by the user and not by calling
1520 // EditLabel() - but as nobody has asked for this so far and there might be
1521 // problems in the code below, I leave it disabled for now (VZ)
1525 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1528 m_textCtrl
= new wxTextCtrl(this, -1);
1530 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1532 //else: not editing label right now
1539 void wxTreeCtrl::DeleteTextCtrl()
1543 m_textCtrl
->UnsubclassWin();
1544 m_textCtrl
->SetHWND(0);
1550 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1551 wxClassInfo
* textControlClass
)
1553 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1557 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1559 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1566 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1567 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1568 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1573 // End label editing, optionally cancelling the edit
1574 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1576 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1581 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1583 TV_HITTESTINFO hitTestInfo
;
1584 hitTestInfo
.pt
.x
= (int)point
.x
;
1585 hitTestInfo
.pt
.y
= (int)point
.y
;
1587 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1592 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1593 flags |= wxTREE_HITTEST_##flag
1595 TRANSLATE_FLAG(ABOVE
);
1596 TRANSLATE_FLAG(BELOW
);
1597 TRANSLATE_FLAG(NOWHERE
);
1598 TRANSLATE_FLAG(ONITEMBUTTON
);
1599 TRANSLATE_FLAG(ONITEMICON
);
1600 TRANSLATE_FLAG(ONITEMINDENT
);
1601 TRANSLATE_FLAG(ONITEMLABEL
);
1602 TRANSLATE_FLAG(ONITEMRIGHT
);
1603 TRANSLATE_FLAG(ONITEMSTATEICON
);
1604 TRANSLATE_FLAG(TOLEFT
);
1605 TRANSLATE_FLAG(TORIGHT
);
1607 #undef TRANSLATE_FLAG
1609 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1612 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1614 bool textOnly
) const
1617 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1620 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1626 // couldn't retrieve rect: for example, item isn't visible
1631 // ----------------------------------------------------------------------------
1633 // ----------------------------------------------------------------------------
1635 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1636 wxTreeItemData
*pItem2
,
1639 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1640 wxT("sorting tree without data doesn't make sense") );
1642 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1645 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1646 const wxTreeItemId
& item2
)
1648 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1651 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1653 // rely on the fact that TreeView_SortChildren does the same thing as our
1654 // default behaviour, i.e. sorts items alphabetically and so call it
1655 // directly if we're not in derived class (much more efficient!)
1656 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1658 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1663 tvSort
.hParent
= HITEM(item
);
1664 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1665 tvSort
.lParam
= (LPARAM
)this;
1666 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1670 // ----------------------------------------------------------------------------
1672 // ----------------------------------------------------------------------------
1674 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1676 if ( cmd
== EN_UPDATE
)
1678 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1679 event
.SetEventObject( this );
1680 ProcessCommand(event
);
1682 else if ( cmd
== EN_KILLFOCUS
)
1684 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1685 event
.SetEventObject( this );
1686 ProcessCommand(event
);
1694 // command processed
1698 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1699 // only do it during dragging, minimize wxWin overhead (this is important for
1700 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1701 // instead of passing by wxWin events
1702 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1704 bool processed
= FALSE
;
1707 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1709 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1711 // we only process mouse messages here and these parameters have the same
1712 // meaning for all of them
1713 int x
= GET_X_LPARAM(lParam
),
1714 y
= GET_Y_LPARAM(lParam
);
1715 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1719 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1720 case WM_LBUTTONDOWN
:
1721 if ( htItem
&& isMultiple
)
1723 if ( wParam
& MK_CONTROL
)
1727 // toggle selected state
1728 ToggleItemSelection(GetHwnd(), htItem
);
1730 ::SetFocus(GetHwnd(), htItem
);
1732 // reset on any click without Shift
1737 else if ( wParam
& MK_SHIFT
)
1739 // this selects all items between the starting one and
1742 if ( !m_htSelStart
)
1744 // take the focused item
1745 m_htSelStart
= (WXHTREEITEM
)
1746 TreeView_GetSelection(GetHwnd());
1749 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1750 !(wParam
& MK_CONTROL
));
1752 ::SetFocus(GetHwnd(), htItem
);
1756 else // normal click
1758 // clear the selection and then let the default handler
1762 // prevent the click from starting in-place editing
1763 // when there was no selection in the control
1764 TreeView_SelectItem(GetHwnd(), 0);
1766 // reset on any click without Shift
1771 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1776 m_dragImage
->Move(wxPoint(x
, y
));
1779 // highlight the item as target (hiding drag image is
1780 // necessary - otherwise the display will be corrupted)
1781 m_dragImage
->Hide();
1782 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1783 m_dragImage
->Show();
1792 m_dragImage
->EndDrag();
1796 // generate the drag end event
1797 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1799 event
.m_item
= (WXHTREEITEM
)htItem
;
1800 event
.m_pointDrag
= wxPoint(x
, y
);
1801 event
.SetEventObject(this);
1803 (void)GetEventHandler()->ProcessEvent(event
);
1805 // if we don't do it, the tree seems to think that 2 items
1806 // are selected simultaneously which is quite weird
1807 TreeView_SelectDropTarget(GetHwnd(), 0);
1812 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1813 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1815 // the tree control greys out the selected item when it loses focus and
1816 // paints it as selected again when it regains it, but it won't do it
1817 // for the other items itself - help it
1818 wxArrayTreeItemIds selections
;
1819 size_t count
= GetSelections(selections
);
1821 for ( size_t n
= 0; n
< count
; n
++ )
1823 // TreeView_GetItemRect() will return FALSE if item is not visible,
1824 // which may happen perfectly well
1825 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1828 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1832 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1834 bool bCtrl
= wxIsCtrlDown(),
1835 bShift
= wxIsShiftDown();
1837 // we handle.arrows and space, but not page up/down and home/end: the
1838 // latter should be easy, but not the former
1840 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1841 if ( !m_htSelStart
)
1843 m_htSelStart
= (WXHTREEITEM
)htSel
;
1846 if ( wParam
== VK_SPACE
)
1850 ToggleItemSelection(GetHwnd(), htSel
);
1856 ::SelectItem(GetHwnd(), htSel
);
1861 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1863 if ( !bCtrl
&& !bShift
)
1865 // no modifiers, just clear selection and then let the default
1866 // processing to take place
1871 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1873 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
1874 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1875 : TreeView_GetNextVisible(GetHwnd(), htSel
));
1879 // at the top/bottom
1885 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1889 // without changing selection
1890 ::SetFocus(GetHwnd(), htNext
);
1897 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1900 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1905 // process WM_NOTIFY Windows message
1906 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1908 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1909 wxEventType eventType
= wxEVT_NULL
;
1910 NMHDR
*hdr
= (NMHDR
*)lParam
;
1912 switch ( hdr
->code
)
1916 if ( wxControl::MSWOnNotify(idCtrl
, lParam
, result
) )
1919 TV_HITTESTINFO tvhti
;
1920 ::GetCursorPos(&(tvhti
.pt
));
1921 ::ScreenToClient(GetHwnd(),&(tvhti
.pt
));
1922 if ( TreeView_HitTest(GetHwnd(),&tvhti
) )
1924 if( tvhti
.flags
& TVHT_ONITEM
)
1926 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
1927 eventType
= wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
1934 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1937 case TVN_BEGINRDRAG
:
1939 if ( eventType
== wxEVT_NULL
)
1940 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1941 //else: left drag, already set above
1943 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1945 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1946 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1948 // don't allow dragging by default: the user code must
1949 // explicitly say that it wants to allow it to avoid breaking
1955 case TVN_BEGINLABELEDIT
:
1957 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
1958 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1960 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
1961 event
.m_label
= info
->item
.pszText
;
1965 case TVN_DELETEITEM
:
1967 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
1968 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1970 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
1974 delete (wxTreeItemAttr
*)m_attrs
.
1975 Delete((long)tv
->itemOld
.hItem
);
1980 case TVN_ENDLABELEDIT
:
1982 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
1983 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
1985 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
1986 event
.m_label
= info
->item
.pszText
;
1987 if (info
->item
.pszText
== NULL
)
1992 case TVN_GETDISPINFO
:
1993 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
1996 case TVN_SETDISPINFO
:
1998 if ( eventType
== wxEVT_NULL
)
1999 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2000 //else: get, already set above
2002 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2004 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2008 case TVN_ITEMEXPANDING
:
2009 event
.m_code
= FALSE
;
2012 case TVN_ITEMEXPANDED
:
2014 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2016 bool expand
= FALSE
;
2017 switch ( tv
->action
)
2028 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND "
2029 "message"), tv
->action
);
2032 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2033 eventType
= g_events
[expand
][ing
];
2035 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2041 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2042 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2044 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
2046 // a separate event for Space/Return
2047 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2048 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2050 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2052 event2
.SetEventObject(this);
2053 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2055 event2
.m_item
= GetSelection();
2057 //else: don't know how to get it
2059 (void)GetEventHandler()->ProcessEvent(event2
);
2064 case TVN_SELCHANGED
:
2065 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2068 case TVN_SELCHANGING
:
2070 if ( eventType
== wxEVT_NULL
)
2071 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2072 //else: already set above
2074 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2076 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2077 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2081 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300
2084 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2085 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2086 switch( nmcd
.dwDrawStage
)
2089 // if we've got any items with non standard attributes,
2090 // notify us before painting each item
2091 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2095 case CDDS_ITEMPREPAINT
:
2097 wxTreeItemAttr
*attr
=
2098 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2102 // nothing to do for this item
2103 return CDRF_DODEFAULT
;
2107 wxColour colText
, colBack
;
2108 if ( attr
->HasFont() )
2110 wxFont font
= attr
->GetFont();
2111 hFont
= (HFONT
)font
.GetResourceHandle();
2118 if ( attr
->HasTextColour() )
2120 colText
= attr
->GetTextColour();
2124 colText
= GetForegroundColour();
2127 // selection colours should override ours
2128 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2130 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2131 lptvcd
->clrTextBk
= clrBk
;
2133 // try to make the text visible
2134 lptvcd
->clrText
= wxColourToRGB(colText
);
2135 lptvcd
->clrText
|= ~clrBk
;
2136 lptvcd
->clrText
&= 0x00ffffff;
2140 if ( attr
->HasBackgroundColour() )
2142 colBack
= attr
->GetBackgroundColour();
2146 colBack
= GetBackgroundColour();
2149 lptvcd
->clrText
= wxColourToRGB(colText
);
2150 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2153 // note that if we wanted to set colours for
2154 // individual columns (subitems), we would have
2155 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2158 ::SelectObject(nmcd
.hdc
, hFont
);
2160 *result
= CDRF_NEWFONT
;
2164 *result
= CDRF_DODEFAULT
;
2171 *result
= CDRF_DODEFAULT
;
2176 #endif // _WIN32_IE >= 0x300
2179 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2182 event
.SetEventObject(this);
2183 event
.SetEventType(eventType
);
2185 bool processed
= GetEventHandler()->ProcessEvent(event
);
2188 switch ( hdr
->code
)
2191 case TVN_BEGINRDRAG
:
2192 if ( event
.IsAllowed() )
2194 // normally this is impossible because the m_dragImage is
2195 // deleted once the drag operation is over
2196 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2198 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2199 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2200 m_dragImage
->Show();
2204 case TVN_DELETEITEM
:
2206 // NB: we might process this message using wxWindows event
2207 // tables, but due to overhead of wxWin event system we
2208 // prefer to do it here ourself (otherwise deleting a tree
2209 // with many items is just too slow)
2210 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2212 wxTreeItemId item
= event
.m_item
;
2213 if ( HasIndirectData(item
) )
2215 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2217 delete data
; // can't be NULL here
2219 m_itemsWithIndirectData
.Remove(item
);
2223 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2224 delete data
; // may be NULL, ok
2227 processed
= TRUE
; // Make sure we don't get called twice
2231 case TVN_BEGINLABELEDIT
:
2232 // return TRUE to cancel label editing
2233 *result
= !event
.IsAllowed();
2236 case TVN_ENDLABELEDIT
:
2237 // return TRUE to set the label to the new string
2238 *result
= event
.IsAllowed();
2240 // ensure that we don't have the text ctrl which is going to be
2245 case TVN_SELCHANGING
:
2246 case TVN_ITEMEXPANDING
:
2247 // return TRUE to prevent the action from happening
2248 *result
= !event
.IsAllowed();
2251 case TVN_GETDISPINFO
:
2252 // NB: so far the user can't set the image himself anyhow, so do it
2253 // anyway - but this may change later
2254 if ( /* !processed && */ 1 )
2256 wxTreeItemId item
= event
.m_item
;
2257 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2258 if ( info
->item
.mask
& TVIF_IMAGE
)
2261 DoGetItemImageFromData
2264 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2265 : wxTreeItemIcon_Normal
2268 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2270 info
->item
.iSelectedImage
=
2271 DoGetItemImageFromData
2274 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2275 : wxTreeItemIcon_Selected
2282 // for the other messages the return value is ignored and there is
2283 // nothing special to do
2289 // ----------------------------------------------------------------------------
2291 // ----------------------------------------------------------------------------
2293 IMPLEMENT_DYNAMIC_CLASS(wxTreeEvent
, wxNotifyEvent
)
2295 wxTreeEvent::wxTreeEvent(wxEventType commandType
, int id
)
2296 : wxNotifyEvent(commandType
, id
)