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 // Set this to 1 to be _absolutely_ sure that repainting will work for all comctl32.dll versions
33 #define wxUSE_COMCTL32_SAFELY 0
35 // Mingw32 is a bit mental even though this is done in winundef
44 #if defined(__WIN95__)
47 #include "wx/dynarray.h"
48 #include "wx/imaglist.h"
49 #include "wx/settings.h"
50 #include "wx/msw/treectrl.h"
51 #include "wx/msw/dragimag.h"
53 #ifdef __GNUWIN32_OLD__
54 #include "wx/msw/gnuwin32/extra.h"
57 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
61 // Bug in headers, sometimes
63 #define TVIS_FOCUSED 0x0001
67 #define TV_FIRST 0x1100
70 #ifndef TVS_CHECKBOXES
71 #define TVS_CHECKBOXES 0x0100
74 // old headers might miss these messages (comctl32.dll 4.71+ only)
75 #ifndef TVM_SETBKCOLOR
76 #define TVM_SETBKCOLOR (TV_FIRST + 29)
77 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
80 // a macro to hide the ugliness of nested casts
81 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
83 // the native control doesn't support multiple selections under MSW and we
84 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
85 // checkboxes be the selection status (checked == selected) or by really
86 // emulating everything, i.e. intercepting mouse and key events &c. The first
87 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
89 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_DRAG
)
96 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_RDRAG
)
97 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
)
98 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_LABEL_EDIT
)
99 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_DELETE_ITEM
)
100 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_GET_INFO
)
101 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SET_INFO
)
102 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDED
)
103 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_EXPANDING
)
104 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
105 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_COLLAPSING
)
106 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGED
)
107 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_SEL_CHANGING
)
108 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_KEY_DOWN
)
109 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
)
110 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
)
111 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_ITEM_MIDDLE_CLICK
)
112 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TREE_END_DRAG
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // wrapper for TreeView_HitTest
119 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
125 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
128 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
130 // wrappers for TreeView_GetItem/TreeView_SetItem
131 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
134 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
135 tvi
.stateMask
= TVIS_SELECTED
;
138 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
140 wxLogLastError(wxT("TreeView_GetItem"));
143 return (tvi
.state
& TVIS_SELECTED
) != 0;
146 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
149 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
150 tvi
.stateMask
= TVIS_SELECTED
;
151 tvi
.state
= select
? TVIS_SELECTED
: 0;
154 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
156 wxLogLastError(wxT("TreeView_SetItem"));
160 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
162 SelectItem(hwndTV
, htItem
, FALSE
);
165 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
167 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
170 // helper function which selects all items in a range and, optionally,
171 // unselects all others
172 static void SelectRange(HWND hwndTV
,
175 bool unselectOthers
= TRUE
)
177 // find the first (or last) item and select it
179 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
180 while ( htItem
&& cont
)
182 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
184 if ( !IsItemSelected(hwndTV
, htItem
) )
186 SelectItem(hwndTV
, htItem
);
193 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
195 UnselectItem(hwndTV
, htItem
);
199 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
202 // select the items in range
203 cont
= htFirst
!= htLast
;
204 while ( htItem
&& cont
)
206 if ( !IsItemSelected(hwndTV
, htItem
) )
208 SelectItem(hwndTV
, htItem
);
211 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
213 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
217 if ( unselectOthers
)
221 if ( IsItemSelected(hwndTV
, htItem
) )
223 UnselectItem(hwndTV
, htItem
);
226 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
230 // seems to be necessary - otherwise the just selected items don't always
231 // appear as selected
232 UpdateWindow(hwndTV
);
235 // helper function which tricks the standard control into changing the focused
236 // item without changing anything else (if someone knows why Microsoft doesn't
237 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
238 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
241 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
246 if ( htItem
!= htFocus
)
248 // remember the selection state of the item
249 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
251 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
253 // prevent the tree from unselecting the old focus which it
254 // would do by default (TreeView_SelectItem unselects the
256 TreeView_SelectItem(hwndTV
, 0);
257 SelectItem(hwndTV
, htFocus
);
260 TreeView_SelectItem(hwndTV
, htItem
);
264 // need to clear the selection which TreeView_SelectItem() gave
266 UnselectItem(hwndTV
, htItem
);
268 //else: was selected, still selected - ok
270 //else: nothing to do, focus already there
276 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
278 // just clear the focus
279 TreeView_SelectItem(hwndTV
, 0);
281 if ( wasFocusSelected
)
283 // restore the selection state
284 SelectItem(hwndTV
, htFocus
);
287 //else: nothing to do, no focus already
291 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
293 // ----------------------------------------------------------------------------
295 // ----------------------------------------------------------------------------
297 // a convenient wrapper around TV_ITEM struct which adds a ctor
299 #pragma warning( disable : 4097 ) // inheriting from typedef
302 struct wxTreeViewItem
: public TV_ITEM
304 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
305 UINT mask_
, // fields which are valid
306 UINT stateMask_
= 0) // for TVIF_STATE only
308 // hItem member is always valid
309 mask
= mask_
| TVIF_HANDLE
;
310 stateMask
= stateMask_
;
316 #pragma warning( default : 4097 )
319 // a class which encapsulates the tree traversal logic: it vists all (unless
320 // OnVisit() returns FALSE) items under the given one
321 class wxTreeTraversal
324 wxTreeTraversal(const wxTreeCtrl
*tree
)
329 // do traverse the tree: visit all items (recursively by default) under the
330 // given one; return TRUE if all items were traversed or FALSE if the
331 // traversal was aborted because OnVisit returned FALSE
332 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
334 // override this function to do whatever is needed for each item, return
335 // FALSE to stop traversing
336 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
339 const wxTreeCtrl
*GetTree() const { return m_tree
; }
342 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
344 const wxTreeCtrl
*m_tree
;
347 // internal class for getting the selected items
348 class TraverseSelections
: public wxTreeTraversal
351 TraverseSelections(const wxTreeCtrl
*tree
,
352 wxArrayTreeItemIds
& selections
)
353 : wxTreeTraversal(tree
), m_selections(selections
)
355 m_selections
.Empty();
357 DoTraverse(tree
->GetRootItem());
360 virtual bool OnVisit(const wxTreeItemId
& item
)
362 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
363 if ( GetTree()->IsItemChecked(item
) )
365 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
368 m_selections
.Add(item
);
374 size_t GetCount() const { return m_selections
.GetCount(); }
377 wxArrayTreeItemIds
& m_selections
;
380 // internal class for counting tree items
381 class TraverseCounter
: public wxTreeTraversal
384 TraverseCounter(const wxTreeCtrl
*tree
,
385 const wxTreeItemId
& root
,
387 : wxTreeTraversal(tree
)
391 DoTraverse(root
, recursively
);
394 virtual bool OnVisit(const wxTreeItemId
& item
)
401 size_t GetCount() const { return m_count
; }
407 // ----------------------------------------------------------------------------
408 // This class is needed for support of different images: the Win32 common
409 // control natively supports only 2 images (the normal one and another for the
410 // selected state). We wish to provide support for 2 more of them for folder
411 // items (i.e. those which have children): for expanded state and for expanded
412 // selected state. For this we use this structure to store the additional items
415 // There is only one problem with this: when we retrieve the item's data, we
416 // don't know whether we get a pointer to wxTreeItemData or
417 // wxTreeItemIndirectData. So we have to maintain a list of all items which
418 // have indirect data inside the listctrl itself.
419 // ----------------------------------------------------------------------------
421 class wxTreeItemIndirectData
424 // ctor associates this data with the item and the real item data becomes
425 // available through our GetData() method
426 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
428 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
434 m_data
= tree
->GetItemData(item
);
436 // and set ourselves as the new one
437 tree
->SetIndirectItemData(item
, this);
440 // dtor deletes the associated data as well
441 ~wxTreeItemIndirectData() { delete m_data
; }
444 // get the real data associated with the item
445 wxTreeItemData
*GetData() const { return m_data
; }
447 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
449 // do we have such image?
450 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
452 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
454 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
457 // all the images associated with the item
458 int m_images
[wxTreeItemIcon_Max
];
460 wxTreeItemData
*m_data
;
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 // handy table for sending events
474 static const wxEventType g_events
[2][2] =
476 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
477 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
480 // ============================================================================
482 // ============================================================================
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
490 if ( !OnVisit(root
) )
493 return Traverse(root
, recursively
);
496 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
499 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
500 while ( child
.IsOk() )
502 // depth first traversal
503 if ( recursively
&& !Traverse(child
, TRUE
) )
506 if ( !OnVisit(child
) )
509 child
= m_tree
->GetNextChild(root
, cookie
);
515 // ----------------------------------------------------------------------------
516 // construction and destruction
517 // ----------------------------------------------------------------------------
519 void wxTreeCtrl::Init()
521 m_imageListNormal
= NULL
;
522 m_imageListState
= NULL
;
523 m_ownsImageListNormal
= m_ownsImageListState
= FALSE
;
525 m_hasAnyAttr
= FALSE
;
531 bool wxTreeCtrl::Create(wxWindow
*parent
,
536 const wxValidator
& validator
,
537 const wxString
& name
)
541 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
544 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
545 TVS_SHOWSELALWAYS
/* | WS_CLIPSIBLINGS */;
547 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
548 wstyle
|= TVS_HASLINES
;
549 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
550 wstyle
|= TVS_HASBUTTONS
;
552 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
553 wstyle
|= TVS_EDITLABELS
;
555 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
556 wstyle
|= TVS_LINESATROOT
;
558 // using TVS_CHECKBOXES for emulation of a multiselection tree control
559 // doesn't work without the new enough headers
560 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
561 !defined( __GNUWIN32_OLD__ ) && \
562 !defined( __BORLANDC__ ) && \
563 !defined( __WATCOMC__ ) && \
564 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
566 // we emulate the multiple selection tree controls by using checkboxes: set
567 // up the image list we need for this if we do have multiple selections
568 if ( m_windowStyle
& wxTR_MULTIPLE
)
569 wstyle
|= TVS_CHECKBOXES
;
570 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
572 // Create the tree control.
573 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
576 #if wxUSE_COMCTL32_SAFELY
577 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
578 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
580 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
581 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
583 // This works around a bug in the Windows tree control whereby for some versions
584 // of comctrl32, setting any colour actually draws the background in black.
585 // This will initialise the background to the system colour.
586 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
587 // Assume the user has an updated comctl32.dll.
588 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
589 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
590 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
594 // VZ: this is some experimental code which may be used to get the
595 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
596 // AFAIK, the standard DLL does about the same thing anyhow.
598 if ( m_windowStyle
& wxTR_MULTIPLE
)
602 // create the DC compatible with the current screen
603 HDC hdcMem
= CreateCompatibleDC(NULL
);
605 // create a mono bitmap of the standard size
606 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
607 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
608 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
609 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
610 1, // # of color planes
611 1, // # bits needed for one pixel
612 0); // array containing colour data
613 SelectObject(hdcMem
, hbmpCheck
);
615 // then draw a check mark into it
616 RECT rect
= { 0, 0, x
, y
};
617 if ( !::DrawFrameControl(hdcMem
, &rect
,
619 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
621 wxLogLastError(wxT("DrawFrameControl(check)"));
624 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
625 imagelistCheckboxes
.Add(bmp
);
627 if ( !::DrawFrameControl(hdcMem
, &rect
,
631 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
634 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
635 imagelistCheckboxes
.Add(bmp
);
641 SetStateImageList(&imagelistCheckboxes
);
645 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
650 wxTreeCtrl::~wxTreeCtrl()
652 // delete any attributes
655 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
657 delete (wxTreeItemAttr
*)node
->Data();
660 // prevent TVN_DELETEITEM handler from deleting the attributes again!
661 m_hasAnyAttr
= FALSE
;
666 // delete user data to prevent memory leaks
669 if (m_ownsImageListNormal
) delete m_imageListNormal
;
670 if (m_ownsImageListState
) delete m_imageListState
;
673 // ----------------------------------------------------------------------------
675 // ----------------------------------------------------------------------------
677 // simple wrappers which add error checking in debug mode
679 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
681 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
683 wxLogLastError(wxT("TreeView_GetItem"));
691 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
693 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
695 wxLogLastError(wxT("TreeView_SetItem"));
699 size_t wxTreeCtrl::GetCount() const
701 return (size_t)TreeView_GetCount(GetHwnd());
704 unsigned int wxTreeCtrl::GetIndent() const
706 return TreeView_GetIndent(GetHwnd());
709 void wxTreeCtrl::SetIndent(unsigned int indent
)
711 TreeView_SetIndent(GetHwnd(), indent
);
714 wxImageList
*wxTreeCtrl::GetImageList() const
716 return m_imageListNormal
;
719 wxImageList
*wxTreeCtrl::GetStateImageList() const
721 return m_imageListNormal
;
724 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
727 TreeView_SetImageList(GetHwnd(),
728 imageList
? imageList
->GetHIMAGELIST() : 0,
732 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
734 if (m_ownsImageListNormal
) delete m_imageListNormal
;
735 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
736 m_ownsImageListNormal
= FALSE
;
739 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
741 if (m_ownsImageListState
) delete m_imageListState
;
742 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
743 m_ownsImageListState
= FALSE
;
746 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
748 SetImageList(imageList
);
749 m_ownsImageListNormal
= TRUE
;
752 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
754 SetStateImageList(imageList
);
755 m_ownsImageListState
= TRUE
;
758 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
759 bool recursively
) const
761 TraverseCounter
counter(this, item
, recursively
);
763 return counter
.GetCount() - 1;
766 // ----------------------------------------------------------------------------
768 // ----------------------------------------------------------------------------
770 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
772 #if !wxUSE_COMCTL32_SAFELY
773 if ( !wxWindowBase::SetBackgroundColour(colour
) )
776 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
782 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
784 #if !wxUSE_COMCTL32_SAFELY
785 if ( !wxWindowBase::SetForegroundColour(colour
) )
788 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
794 // ----------------------------------------------------------------------------
796 // ----------------------------------------------------------------------------
798 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
800 wxChar buf
[512]; // the size is arbitrary...
802 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
803 tvItem
.pszText
= buf
;
804 tvItem
.cchTextMax
= WXSIZEOF(buf
);
805 if ( !DoGetItem(&tvItem
) )
807 // don't return some garbage which was on stack, but an empty string
811 return wxString(buf
);
814 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
816 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
817 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
821 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
822 wxTreeItemIcon which
) const
824 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
825 if ( !DoGetItem(&tvItem
) )
830 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
833 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
835 wxTreeItemIcon which
) const
837 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
838 if ( !DoGetItem(&tvItem
) )
843 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
845 data
->SetImage(image
, which
);
847 // make sure that we have selected images as well
848 if ( which
== wxTreeItemIcon_Normal
&&
849 !data
->HasImage(wxTreeItemIcon_Selected
) )
851 data
->SetImage(image
, wxTreeItemIcon_Selected
);
854 if ( which
== wxTreeItemIcon_Expanded
&&
855 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
857 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
861 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
865 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
866 tvItem
.iSelectedImage
= imageSel
;
867 tvItem
.iImage
= image
;
871 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
872 wxTreeItemIcon which
) const
874 if ( HasIndirectData(item
) )
876 return DoGetItemImageFromData(item
, which
);
883 wxFAIL_MSG( wxT("unknown tree item image type") );
885 case wxTreeItemIcon_Normal
:
889 case wxTreeItemIcon_Selected
:
890 mask
= TVIF_SELECTEDIMAGE
;
893 case wxTreeItemIcon_Expanded
:
894 case wxTreeItemIcon_SelectedExpanded
:
898 wxTreeViewItem
tvItem(item
, mask
);
901 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
904 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
905 wxTreeItemIcon which
)
907 int imageNormal
, imageSel
;
911 wxFAIL_MSG( wxT("unknown tree item image type") );
913 case wxTreeItemIcon_Normal
:
915 imageSel
= GetItemSelectedImage(item
);
918 case wxTreeItemIcon_Selected
:
919 imageNormal
= GetItemImage(item
);
923 case wxTreeItemIcon_Expanded
:
924 case wxTreeItemIcon_SelectedExpanded
:
925 if ( !HasIndirectData(item
) )
927 // we need to get the old images first, because after we create
928 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
930 imageNormal
= GetItemImage(item
);
931 imageSel
= GetItemSelectedImage(item
);
933 // if it doesn't have it yet, add it
934 wxTreeItemIndirectData
*data
= new
935 wxTreeItemIndirectData(this, item
);
937 // copy the data to the new location
938 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
939 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
942 DoSetItemImageFromData(item
, image
, which
);
944 // reset the normal/selected images because we won't use them any
945 // more - now they're stored inside the indirect data
947 imageSel
= I_IMAGECALLBACK
;
951 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
952 // change both normal and selected image - otherwise the change simply
953 // doesn't take place!
954 DoSetItemImages(item
, imageNormal
, imageSel
);
957 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
959 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
960 if ( !DoGetItem(&tvItem
) )
965 if ( HasIndirectData(item
) )
967 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
971 return (wxTreeItemData
*)tvItem
.lParam
;
975 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
977 // first, associate this piece of data with this item
983 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
985 if ( HasIndirectData(item
) )
987 if ( DoGetItem(&tvItem
) )
989 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
993 wxFAIL_MSG( wxT("failed to change tree items data") );
998 tvItem
.lParam
= (LPARAM
)data
;
1003 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1004 wxTreeItemIndirectData
*data
)
1006 // this should never happen because it's unnecessary and will probably lead
1007 // to crash too because the code elsewhere supposes that the pointer the
1008 // wxTreeItemIndirectData has is a real wxItemData and not
1009 // wxTreeItemIndirectData as well
1010 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1012 SetItemData(item
, (wxTreeItemData
*)data
);
1014 m_itemsWithIndirectData
.Add(item
);
1017 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1019 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
1022 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1024 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1025 tvItem
.cChildren
= (int)has
;
1029 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1031 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1032 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1036 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1038 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1039 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1043 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1044 const wxColour
& col
)
1046 m_hasAnyAttr
= TRUE
;
1048 long id
= (long)(WXHTREEITEM
)item
;
1049 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1052 attr
= new wxTreeItemAttr
;
1053 m_attrs
.Put(id
, (wxObject
*)attr
);
1056 attr
->SetTextColour(col
);
1059 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1060 const wxColour
& col
)
1062 m_hasAnyAttr
= TRUE
;
1064 long id
= (long)(WXHTREEITEM
)item
;
1065 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1068 attr
= new wxTreeItemAttr
;
1069 m_attrs
.Put(id
, (wxObject
*)attr
);
1072 attr
->SetBackgroundColour(col
);
1075 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1077 m_hasAnyAttr
= TRUE
;
1079 long id
= (long)(WXHTREEITEM
)item
;
1080 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1083 attr
= new wxTreeItemAttr
;
1084 m_attrs
.Put(id
, (wxObject
*)attr
);
1087 attr
->SetFont(font
);
1090 // ----------------------------------------------------------------------------
1092 // ----------------------------------------------------------------------------
1094 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1096 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1099 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1100 // the HTREEITEM with TVM_GETITEMRECT
1101 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1103 // FALSE means get item rect for the whole item, not only text
1104 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1108 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1110 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1113 return tvItem
.cChildren
!= 0;
1116 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1118 // probably not a good idea to put it here
1119 //wxASSERT( ItemHasChildren(item) );
1121 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1124 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1127 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1129 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1132 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1135 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1137 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1140 return (tvItem
.state
& TVIS_BOLD
) != 0;
1143 // ----------------------------------------------------------------------------
1145 // ----------------------------------------------------------------------------
1147 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1149 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1152 wxTreeItemId
wxTreeCtrl::GetSelection() const
1154 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1155 wxT("this only works with single selection controls") );
1157 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1160 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1162 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1165 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1166 long& _cookie
) const
1168 // remember the last child returned in 'cookie'
1169 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1171 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1174 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1175 long& _cookie
) const
1177 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1184 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1186 // can this be done more efficiently?
1189 wxTreeItemId childLast
,
1190 child
= GetFirstChild(item
, cookie
);
1191 while ( child
.IsOk() )
1194 child
= GetNextChild(item
, cookie
);
1200 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1202 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1205 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1207 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1210 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1212 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1215 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1217 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1219 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1222 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1224 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1226 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1229 // ----------------------------------------------------------------------------
1230 // multiple selections emulation
1231 // ----------------------------------------------------------------------------
1233 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1235 // receive the desired information.
1236 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1239 // state image indices are 1 based
1240 return ((tvItem
.state
>> 12) - 1) == 1;
1243 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1245 // receive the desired information.
1246 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1248 // state images are one-based
1249 tvItem
.state
= (check
? 2 : 1) << 12;
1254 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1256 TraverseSelections
selector(this, selections
);
1258 return selector
.GetCount();
1261 // ----------------------------------------------------------------------------
1263 // ----------------------------------------------------------------------------
1265 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1266 wxTreeItemId hInsertAfter
,
1267 const wxString
& text
,
1268 int image
, int selectedImage
,
1269 wxTreeItemData
*data
)
1271 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1273 _T("can't have more than one root in the tree") );
1275 TV_INSERTSTRUCT tvIns
;
1276 tvIns
.hParent
= HITEM(parent
);
1277 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1279 // this is how we insert the item as the first child: supply a NULL
1281 if ( !tvIns
.hInsertAfter
)
1283 tvIns
.hInsertAfter
= TVI_FIRST
;
1287 if ( !text
.IsEmpty() )
1290 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1294 tvIns
.item
.pszText
= NULL
;
1295 tvIns
.item
.cchTextMax
= 0;
1301 tvIns
.item
.iImage
= image
;
1303 if ( selectedImage
== -1 )
1305 // take the same image for selected icon if not specified
1306 selectedImage
= image
;
1310 if ( selectedImage
!= -1 )
1312 mask
|= TVIF_SELECTEDIMAGE
;
1313 tvIns
.item
.iSelectedImage
= selectedImage
;
1319 tvIns
.item
.lParam
= (LPARAM
)data
;
1322 tvIns
.item
.mask
= mask
;
1324 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1327 wxLogLastError(wxT("TreeView_InsertItem"));
1332 // associate the application tree item with Win32 tree item handle
1333 data
->SetId((WXHTREEITEM
)id
);
1336 return wxTreeItemId((WXHTREEITEM
)id
);
1339 // for compatibility only
1340 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1341 const wxString
& text
,
1342 int image
, int selImage
,
1345 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1346 image
, selImage
, NULL
);
1349 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1350 int image
, int selectedImage
,
1351 wxTreeItemData
*data
)
1353 return DoInsertItem(wxTreeItemId((long) (WXHTREEITEM
) 0), (long)(WXHTREEITEM
) 0,
1354 text
, image
, selectedImage
, data
);
1357 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1358 const wxString
& text
,
1359 int image
, int selectedImage
,
1360 wxTreeItemData
*data
)
1362 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1363 text
, image
, selectedImage
, data
);
1366 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1367 const wxTreeItemId
& idPrevious
,
1368 const wxString
& text
,
1369 int image
, int selectedImage
,
1370 wxTreeItemData
*data
)
1372 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1375 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1377 const wxString
& text
,
1378 int image
, int selectedImage
,
1379 wxTreeItemData
*data
)
1381 // find the item from index
1383 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1384 while ( index
!= 0 && idCur
.IsOk() )
1389 idCur
= GetNextChild(parent
, cookie
);
1392 // assert, not check: if the index is invalid, we will append the item
1394 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1396 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1399 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1400 const wxString
& text
,
1401 int image
, int selectedImage
,
1402 wxTreeItemData
*data
)
1404 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1405 text
, image
, selectedImage
, data
);
1408 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1410 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1412 wxLogLastError(wxT("TreeView_DeleteItem"));
1416 // delete all children (but don't delete the item itself)
1417 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1421 wxArrayLong children
;
1422 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1423 while ( child
.IsOk() )
1425 children
.Add((long)(WXHTREEITEM
)child
);
1427 child
= GetNextChild(item
, cookie
);
1430 size_t nCount
= children
.Count();
1431 for ( size_t n
= 0; n
< nCount
; n
++ )
1433 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1435 wxLogLastError(wxT("TreeView_DeleteItem"));
1440 void wxTreeCtrl::DeleteAllItems()
1442 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1444 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1448 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1450 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1451 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1452 flag
== TVE_EXPAND
||
1454 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1456 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1457 // emulate them. This behaviour has changed slightly with comctl32.dll
1458 // v 4.70 - now it does send them but only the first time. To maintain
1459 // compatible behaviour and also in order to not have surprises with the
1460 // future versions, don't rely on this and still do everything ourselves.
1461 // To avoid that the messages be sent twice when the item is expanded for
1462 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1464 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1468 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1470 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1471 event
.m_item
= item
;
1473 bool isExpanded
= IsExpanded(item
);
1475 event
.SetEventObject(this);
1477 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1478 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1479 GetEventHandler()->ProcessEvent(event
);
1481 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1482 GetEventHandler()->ProcessEvent(event
);
1484 //else: change didn't took place, so do nothing at all
1487 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1489 DoExpand(item
, TVE_EXPAND
);
1492 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1494 DoExpand(item
, TVE_COLLAPSE
);
1497 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1499 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1502 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1504 DoExpand(item
, TVE_TOGGLE
);
1507 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1509 DoExpand(item
, action
);
1512 void wxTreeCtrl::Unselect()
1514 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1515 wxT("doesn't make sense, may be you want UnselectAll()?") );
1517 // just remove the selection
1518 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1521 void wxTreeCtrl::UnselectAll()
1523 if ( m_windowStyle
& wxTR_MULTIPLE
)
1525 wxArrayTreeItemIds selections
;
1526 size_t count
= GetSelections(selections
);
1527 for ( size_t n
= 0; n
< count
; n
++ )
1529 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1530 SetItemCheck(selections
[n
], FALSE
);
1531 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1532 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1533 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1538 // just remove the selection
1543 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1545 if ( m_windowStyle
& wxTR_MULTIPLE
)
1547 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1548 // selecting the item means checking it
1550 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1551 ::SelectItem(GetHwnd(), HITEM(item
));
1552 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1556 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1557 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1558 // send them ourselves
1560 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1561 event
.m_item
= item
;
1562 event
.SetEventObject(this);
1564 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1565 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1567 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1569 wxLogLastError(wxT("TreeView_SelectItem"));
1573 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1574 (void)GetEventHandler()->ProcessEvent(event
);
1577 //else: program vetoed the change
1581 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1584 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1587 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1589 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1591 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1595 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1597 // normally, we could try to do something like this to return something
1598 // even when the editing was started by the user and not by calling
1599 // EditLabel() - but as nobody has asked for this so far and there might be
1600 // problems in the code below, I leave it disabled for now (VZ)
1604 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1607 m_textCtrl
= new wxTextCtrl(this, -1);
1609 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1611 //else: not editing label right now
1618 void wxTreeCtrl::DeleteTextCtrl()
1622 // the HWND corresponding to this control is deleted by the tree
1623 // control itself and we don't know when exactly this happens, so check
1624 // if the window still exists before calling UnsubclassWin()
1625 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1627 m_textCtrl
->SetHWND(0);
1630 m_textCtrl
->UnsubclassWin();
1631 m_textCtrl
->SetHWND(0);
1637 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1638 wxClassInfo
* textControlClass
)
1640 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1644 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1646 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1653 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1654 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1655 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1660 // End label editing, optionally cancelling the edit
1661 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1663 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1668 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1670 TV_HITTESTINFO hitTestInfo
;
1671 hitTestInfo
.pt
.x
= (int)point
.x
;
1672 hitTestInfo
.pt
.y
= (int)point
.y
;
1674 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1679 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1680 flags |= wxTREE_HITTEST_##flag
1682 TRANSLATE_FLAG(ABOVE
);
1683 TRANSLATE_FLAG(BELOW
);
1684 TRANSLATE_FLAG(NOWHERE
);
1685 TRANSLATE_FLAG(ONITEMBUTTON
);
1686 TRANSLATE_FLAG(ONITEMICON
);
1687 TRANSLATE_FLAG(ONITEMINDENT
);
1688 TRANSLATE_FLAG(ONITEMLABEL
);
1689 TRANSLATE_FLAG(ONITEMRIGHT
);
1690 TRANSLATE_FLAG(ONITEMSTATEICON
);
1691 TRANSLATE_FLAG(TOLEFT
);
1692 TRANSLATE_FLAG(TORIGHT
);
1694 #undef TRANSLATE_FLAG
1696 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1699 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1701 bool textOnly
) const
1704 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1707 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1713 // couldn't retrieve rect: for example, item isn't visible
1718 // ----------------------------------------------------------------------------
1720 // ----------------------------------------------------------------------------
1722 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1723 wxTreeItemData
*pItem2
,
1726 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1727 wxT("sorting tree without data doesn't make sense") );
1729 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1732 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1733 const wxTreeItemId
& item2
)
1735 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1738 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1740 // rely on the fact that TreeView_SortChildren does the same thing as our
1741 // default behaviour, i.e. sorts items alphabetically and so call it
1742 // directly if we're not in derived class (much more efficient!)
1743 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1745 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1750 tvSort
.hParent
= HITEM(item
);
1751 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1752 tvSort
.lParam
= (LPARAM
)this;
1753 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1757 // ----------------------------------------------------------------------------
1759 // ----------------------------------------------------------------------------
1761 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1763 if ( cmd
== EN_UPDATE
)
1765 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1766 event
.SetEventObject( this );
1767 ProcessCommand(event
);
1769 else if ( cmd
== EN_KILLFOCUS
)
1771 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1772 event
.SetEventObject( this );
1773 ProcessCommand(event
);
1781 // command processed
1785 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1786 // only do it during dragging, minimize wxWin overhead (this is important for
1787 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1788 // instead of passing by wxWin events
1789 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1791 bool processed
= FALSE
;
1794 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1796 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1798 // we only process mouse messages here and these parameters have the same
1799 // meaning for all of them
1800 int x
= GET_X_LPARAM(lParam
),
1801 y
= GET_Y_LPARAM(lParam
);
1802 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1806 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1807 case WM_LBUTTONDOWN
:
1808 if ( htItem
&& isMultiple
)
1810 if ( wParam
& MK_CONTROL
)
1814 // toggle selected state
1815 ToggleItemSelection(GetHwnd(), htItem
);
1817 ::SetFocus(GetHwnd(), htItem
);
1819 // reset on any click without Shift
1824 else if ( wParam
& MK_SHIFT
)
1826 // this selects all items between the starting one and
1829 if ( !m_htSelStart
)
1831 // take the focused item
1832 m_htSelStart
= (WXHTREEITEM
)
1833 TreeView_GetSelection(GetHwnd());
1836 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1837 !(wParam
& MK_CONTROL
));
1839 ::SetFocus(GetHwnd(), htItem
);
1843 else // normal click
1845 // clear the selection and then let the default handler
1849 // prevent the click from starting in-place editing
1850 // when there was no selection in the control
1851 TreeView_SelectItem(GetHwnd(), 0);
1853 // reset on any click without Shift
1858 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1863 m_dragImage
->Move(wxPoint(x
, y
));
1866 // highlight the item as target (hiding drag image is
1867 // necessary - otherwise the display will be corrupted)
1868 m_dragImage
->Hide();
1869 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1870 m_dragImage
->Show();
1879 m_dragImage
->EndDrag();
1883 // generate the drag end event
1884 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1886 event
.m_item
= (WXHTREEITEM
)htItem
;
1887 event
.m_pointDrag
= wxPoint(x
, y
);
1888 event
.SetEventObject(this);
1890 (void)GetEventHandler()->ProcessEvent(event
);
1892 // if we don't do it, the tree seems to think that 2 items
1893 // are selected simultaneously which is quite weird
1894 TreeView_SelectDropTarget(GetHwnd(), 0);
1899 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1900 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1902 // the tree control greys out the selected item when it loses focus and
1903 // paints it as selected again when it regains it, but it won't do it
1904 // for the other items itself - help it
1905 wxArrayTreeItemIds selections
;
1906 size_t count
= GetSelections(selections
);
1908 for ( size_t n
= 0; n
< count
; n
++ )
1910 // TreeView_GetItemRect() will return FALSE if item is not visible,
1911 // which may happen perfectly well
1912 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1915 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1919 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1921 bool bCtrl
= wxIsCtrlDown(),
1922 bShift
= wxIsShiftDown();
1924 // we handle.arrows and space, but not page up/down and home/end: the
1925 // latter should be easy, but not the former
1927 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1928 if ( !m_htSelStart
)
1930 m_htSelStart
= (WXHTREEITEM
)htSel
;
1933 if ( wParam
== VK_SPACE
)
1937 ToggleItemSelection(GetHwnd(), htSel
);
1943 ::SelectItem(GetHwnd(), htSel
);
1948 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1950 if ( !bCtrl
&& !bShift
)
1952 // no modifiers, just clear selection and then let the default
1953 // processing to take place
1958 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1960 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
1961 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1962 : TreeView_GetNextVisible(GetHwnd(), htSel
));
1966 // at the top/bottom
1972 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1976 // without changing selection
1977 ::SetFocus(GetHwnd(), htNext
);
1984 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1987 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1992 // process WM_NOTIFY Windows message
1993 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1995 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1996 wxEventType eventType
= wxEVT_NULL
;
1997 NMHDR
*hdr
= (NMHDR
*)lParam
;
1999 switch ( hdr
->code
)
2002 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2005 case TVN_BEGINRDRAG
:
2007 if ( eventType
== wxEVT_NULL
)
2008 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2009 //else: left drag, already set above
2011 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2013 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2014 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2016 // don't allow dragging by default: the user code must
2017 // explicitly say that it wants to allow it to avoid breaking
2023 case TVN_BEGINLABELEDIT
:
2025 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2026 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2028 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2029 event
.m_label
= info
->item
.pszText
;
2033 case TVN_DELETEITEM
:
2035 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2036 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2038 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2042 delete (wxTreeItemAttr
*)m_attrs
.
2043 Delete((long)tv
->itemOld
.hItem
);
2048 case TVN_ENDLABELEDIT
:
2050 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2051 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2053 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2054 event
.m_label
= info
->item
.pszText
;
2055 if (info
->item
.pszText
== NULL
)
2060 case TVN_GETDISPINFO
:
2061 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2064 case TVN_SETDISPINFO
:
2066 if ( eventType
== wxEVT_NULL
)
2067 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2068 //else: get, already set above
2070 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2072 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2076 case TVN_ITEMEXPANDING
:
2077 event
.m_code
= FALSE
;
2080 case TVN_ITEMEXPANDED
:
2082 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2084 bool expand
= FALSE
;
2085 switch ( tv
->action
)
2096 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2099 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2100 eventType
= g_events
[expand
][ing
];
2102 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2108 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2109 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2111 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
2113 // a separate event for Space/Return
2114 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2115 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2117 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2119 event2
.SetEventObject(this);
2120 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2122 event2
.m_item
= GetSelection();
2124 //else: don't know how to get it
2126 (void)GetEventHandler()->ProcessEvent(event2
);
2131 case TVN_SELCHANGED
:
2132 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2135 case TVN_SELCHANGING
:
2137 if ( eventType
== wxEVT_NULL
)
2138 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2139 //else: already set above
2141 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2143 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2144 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2148 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY
2151 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2152 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2153 switch( nmcd
.dwDrawStage
)
2156 // if we've got any items with non standard attributes,
2157 // notify us before painting each item
2158 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2162 case CDDS_ITEMPREPAINT
:
2164 wxTreeItemAttr
*attr
=
2165 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2169 // nothing to do for this item
2170 return CDRF_DODEFAULT
;
2174 wxColour colText
, colBack
;
2175 if ( attr
->HasFont() )
2177 wxFont font
= attr
->GetFont();
2178 hFont
= (HFONT
)font
.GetResourceHandle();
2185 if ( attr
->HasTextColour() )
2187 colText
= attr
->GetTextColour();
2191 colText
= GetForegroundColour();
2194 // selection colours should override ours
2195 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2197 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2198 lptvcd
->clrTextBk
= clrBk
;
2200 // try to make the text visible
2201 lptvcd
->clrText
= wxColourToRGB(colText
);
2202 lptvcd
->clrText
|= ~clrBk
;
2203 lptvcd
->clrText
&= 0x00ffffff;
2207 if ( attr
->HasBackgroundColour() )
2209 colBack
= attr
->GetBackgroundColour();
2213 colBack
= GetBackgroundColour();
2216 lptvcd
->clrText
= wxColourToRGB(colText
);
2217 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2220 // note that if we wanted to set colours for
2221 // individual columns (subitems), we would have
2222 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2225 ::SelectObject(nmcd
.hdc
, hFont
);
2227 *result
= CDRF_NEWFONT
;
2231 *result
= CDRF_DODEFAULT
;
2238 *result
= CDRF_DODEFAULT
;
2243 #endif // _WIN32_IE >= 0x300
2248 TV_HITTESTINFO tvhti
;
2249 ::GetCursorPos(&tvhti
.pt
);
2250 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2251 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2253 if ( tvhti
.flags
& TVHT_ONITEM
)
2255 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2256 eventType
= (int)hdr
->code
== NM_DBLCLK
2257 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2258 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2260 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2261 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2270 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2273 event
.SetEventObject(this);
2274 event
.SetEventType(eventType
);
2276 bool processed
= GetEventHandler()->ProcessEvent(event
);
2279 switch ( hdr
->code
)
2282 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2283 // the return code of this event handler as the return value for
2284 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2285 // expanded status would never work
2290 case TVN_BEGINRDRAG
:
2291 if ( event
.IsAllowed() )
2293 // normally this is impossible because the m_dragImage is
2294 // deleted once the drag operation is over
2295 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2297 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2298 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2299 m_dragImage
->Show();
2303 case TVN_DELETEITEM
:
2305 // NB: we might process this message using wxWindows event
2306 // tables, but due to overhead of wxWin event system we
2307 // prefer to do it here ourself (otherwise deleting a tree
2308 // with many items is just too slow)
2309 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2311 wxTreeItemId item
= event
.m_item
;
2312 if ( HasIndirectData(item
) )
2314 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2316 delete data
; // can't be NULL here
2318 m_itemsWithIndirectData
.Remove(item
);
2320 int iIndex
= m_itemsWithIndirectData
.Index(item
);
2321 wxASSERT( iIndex
!= wxNOT_FOUND
) ;
2322 m_itemsWithIndirectData
.wxBaseArray::RemoveAt((size_t)iIndex
);
2327 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2328 delete data
; // may be NULL, ok
2331 processed
= TRUE
; // Make sure we don't get called twice
2335 case TVN_BEGINLABELEDIT
:
2336 // return TRUE to cancel label editing
2337 *result
= !event
.IsAllowed();
2340 case TVN_ENDLABELEDIT
:
2341 // return TRUE to set the label to the new string: note that we
2342 // also must pretend that we did process the message or it is going
2343 // to be passed to DefWindowProc() which will happily return FALSE
2344 // cancelling the label change
2345 *result
= event
.IsAllowed();
2348 // ensure that we don't have the text ctrl which is going to be
2353 case TVN_SELCHANGING
:
2354 case TVN_ITEMEXPANDING
:
2355 // return TRUE to prevent the action from happening
2356 *result
= !event
.IsAllowed();
2359 case TVN_GETDISPINFO
:
2360 // NB: so far the user can't set the image himself anyhow, so do it
2361 // anyway - but this may change later
2362 if ( /* !processed && */ 1 )
2364 wxTreeItemId item
= event
.m_item
;
2365 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2366 if ( info
->item
.mask
& TVIF_IMAGE
)
2369 DoGetItemImageFromData
2372 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2373 : wxTreeItemIcon_Normal
2376 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2378 info
->item
.iSelectedImage
=
2379 DoGetItemImageFromData
2382 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2383 : wxTreeItemIcon_Selected
2390 // for the other messages the return value is ignored and there is
2391 // nothing special to do