1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
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 // ----------------------------------------------------------------------------
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/msw/private.h"
35 // Set this to 1 to be _absolutely_ sure that repainting will work for all
36 // comctl32.dll versions
37 #define wxUSE_COMCTL32_SAFELY 0
39 // Mingw32 is a bit mental even though this is done in winundef
48 #if defined(__WIN95__)
52 #include "wx/dynarray.h"
53 #include "wx/imaglist.h"
54 #include "wx/settings.h"
55 #include "wx/msw/treectrl.h"
56 #include "wx/msw/dragimag.h"
58 #ifdef __GNUWIN32_OLD__
59 #include "wx/msw/gnuwin32/extra.h"
62 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
66 // Bug in headers, sometimes
68 #define TVIS_FOCUSED 0x0001
72 #define TV_FIRST 0x1100
75 #ifndef TVS_CHECKBOXES
76 #define TVS_CHECKBOXES 0x0100
79 #ifndef TVS_FULLROWSELECT
80 #define TVS_FULLROWSELECT 0x1000
83 // old headers might miss these messages (comctl32.dll 4.71+ only)
84 #ifndef TVM_SETBKCOLOR
85 #define TVM_SETBKCOLOR (TV_FIRST + 29)
86 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
89 // a macro to hide the ugliness of nested casts
90 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
92 // the native control doesn't support multiple selections under MSW and we
93 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
94 // checkboxes be the selection status (checked == selected) or by really
95 // emulating everything, i.e. intercepting mouse and key events &c. The first
96 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
98 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // wrapper for TreeView_HitTest
106 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
112 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
115 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
117 // wrappers for TreeView_GetItem/TreeView_SetItem
118 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
121 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
122 tvi
.stateMask
= TVIS_SELECTED
;
125 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
127 wxLogLastError(wxT("TreeView_GetItem"));
130 return (tvi
.state
& TVIS_SELECTED
) != 0;
133 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
136 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
137 tvi
.stateMask
= TVIS_SELECTED
;
138 tvi
.state
= select
? TVIS_SELECTED
: 0;
141 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
143 wxLogLastError(wxT("TreeView_SetItem"));
147 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
149 SelectItem(hwndTV
, htItem
, FALSE
);
152 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
154 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
157 // helper function which selects all items in a range and, optionally,
158 // unselects all others
159 static void SelectRange(HWND hwndTV
,
162 bool unselectOthers
= TRUE
)
164 // find the first (or last) item and select it
166 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
167 while ( htItem
&& cont
)
169 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
171 if ( !IsItemSelected(hwndTV
, htItem
) )
173 SelectItem(hwndTV
, htItem
);
180 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
182 UnselectItem(hwndTV
, htItem
);
186 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
189 // select the items in range
190 cont
= htFirst
!= htLast
;
191 while ( htItem
&& cont
)
193 if ( !IsItemSelected(hwndTV
, htItem
) )
195 SelectItem(hwndTV
, htItem
);
198 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
200 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
204 if ( unselectOthers
)
208 if ( IsItemSelected(hwndTV
, htItem
) )
210 UnselectItem(hwndTV
, htItem
);
213 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
217 // seems to be necessary - otherwise the just selected items don't always
218 // appear as selected
219 UpdateWindow(hwndTV
);
222 // helper function which tricks the standard control into changing the focused
223 // item without changing anything else (if someone knows why Microsoft doesn't
224 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
225 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
228 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
233 if ( htItem
!= htFocus
)
235 // remember the selection state of the item
236 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
238 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
240 // prevent the tree from unselecting the old focus which it
241 // would do by default (TreeView_SelectItem unselects the
243 TreeView_SelectItem(hwndTV
, 0);
244 SelectItem(hwndTV
, htFocus
);
247 TreeView_SelectItem(hwndTV
, htItem
);
251 // need to clear the selection which TreeView_SelectItem() gave
253 UnselectItem(hwndTV
, htItem
);
255 //else: was selected, still selected - ok
257 //else: nothing to do, focus already there
263 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
265 // just clear the focus
266 TreeView_SelectItem(hwndTV
, 0);
268 if ( wasFocusSelected
)
270 // restore the selection state
271 SelectItem(hwndTV
, htFocus
);
274 //else: nothing to do, no focus already
278 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 // a convenient wrapper around TV_ITEM struct which adds a ctor
286 #pragma warning( disable : 4097 ) // inheriting from typedef
289 struct wxTreeViewItem
: public TV_ITEM
291 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
292 UINT mask_
, // fields which are valid
293 UINT stateMask_
= 0) // for TVIF_STATE only
295 // hItem member is always valid
296 mask
= mask_
| TVIF_HANDLE
;
297 stateMask
= stateMask_
;
303 #pragma warning( default : 4097 )
306 // a class which encapsulates the tree traversal logic: it vists all (unless
307 // OnVisit() returns FALSE) items under the given one
308 class wxTreeTraversal
311 wxTreeTraversal(const wxTreeCtrl
*tree
)
316 // do traverse the tree: visit all items (recursively by default) under the
317 // given one; return TRUE if all items were traversed or FALSE if the
318 // traversal was aborted because OnVisit returned FALSE
319 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
321 // override this function to do whatever is needed for each item, return
322 // FALSE to stop traversing
323 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
326 const wxTreeCtrl
*GetTree() const { return m_tree
; }
329 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
331 const wxTreeCtrl
*m_tree
;
334 // internal class for getting the selected items
335 class TraverseSelections
: public wxTreeTraversal
338 TraverseSelections(const wxTreeCtrl
*tree
,
339 wxArrayTreeItemIds
& selections
)
340 : wxTreeTraversal(tree
), m_selections(selections
)
342 m_selections
.Empty();
344 DoTraverse(tree
->GetRootItem());
347 virtual bool OnVisit(const wxTreeItemId
& item
)
349 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
350 if ( GetTree()->IsItemChecked(item
) )
352 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
355 m_selections
.Add(item
);
361 size_t GetCount() const { return m_selections
.GetCount(); }
364 wxArrayTreeItemIds
& m_selections
;
367 // internal class for counting tree items
368 class TraverseCounter
: public wxTreeTraversal
371 TraverseCounter(const wxTreeCtrl
*tree
,
372 const wxTreeItemId
& root
,
374 : wxTreeTraversal(tree
)
378 DoTraverse(root
, recursively
);
381 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
388 size_t GetCount() const { return m_count
; }
394 // ----------------------------------------------------------------------------
395 // This class is needed for support of different images: the Win32 common
396 // control natively supports only 2 images (the normal one and another for the
397 // selected state). We wish to provide support for 2 more of them for folder
398 // items (i.e. those which have children): for expanded state and for expanded
399 // selected state. For this we use this structure to store the additional items
402 // There is only one problem with this: when we retrieve the item's data, we
403 // don't know whether we get a pointer to wxTreeItemData or
404 // wxTreeItemIndirectData. So we have to maintain a list of all items which
405 // have indirect data inside the listctrl itself.
406 // ----------------------------------------------------------------------------
408 class wxTreeItemIndirectData
411 // ctor associates this data with the item and the real item data becomes
412 // available through our GetData() method
413 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
415 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
421 m_data
= tree
->GetItemData(item
);
423 // and set ourselves as the new one
424 tree
->SetIndirectItemData(item
, this);
427 // dtor deletes the associated data as well
428 ~wxTreeItemIndirectData() { delete m_data
; }
431 // get the real data associated with the item
432 wxTreeItemData
*GetData() const { return m_data
; }
434 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
436 // do we have such image?
437 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
439 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
441 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
444 // all the images associated with the item
445 int m_images
[wxTreeItemIcon_Max
];
447 wxTreeItemData
*m_data
;
450 // ----------------------------------------------------------------------------
452 // ----------------------------------------------------------------------------
454 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
456 // ----------------------------------------------------------------------------
458 // ----------------------------------------------------------------------------
460 // handy table for sending events
461 static wxEventType g_events
[2][2] =
463 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
464 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
467 // ============================================================================
469 // ============================================================================
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
477 if ( !OnVisit(root
) )
480 return Traverse(root
, recursively
);
483 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
486 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
487 while ( child
.IsOk() )
489 // depth first traversal
490 if ( recursively
&& !Traverse(child
, TRUE
) )
493 if ( !OnVisit(child
) )
496 child
= m_tree
->GetNextChild(root
, cookie
);
502 // ----------------------------------------------------------------------------
503 // construction and destruction
504 // ----------------------------------------------------------------------------
506 void wxTreeCtrl::Init()
508 m_imageListNormal
= NULL
;
509 m_imageListState
= NULL
;
510 m_ownsImageListNormal
= m_ownsImageListState
= FALSE
;
512 m_hasAnyAttr
= FALSE
;
516 // Initialize static array of events, because with the new event system,
517 // they may not be initialized yet.
519 g_events
[0][0] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
520 g_events
[0][1] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
521 g_events
[1][0] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
522 g_events
[1][1] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
525 bool wxTreeCtrl::Create(wxWindow
*parent
,
530 const wxValidator
& validator
,
531 const wxString
& name
)
535 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
538 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
541 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
542 wstyle
|= WS_CLIPSIBLINGS
;
544 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
545 wstyle
|= TVS_HASLINES
;
546 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
547 wstyle
|= TVS_HASBUTTONS
;
549 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
550 wstyle
|= TVS_EDITLABELS
;
552 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
553 wstyle
|= TVS_LINESATROOT
;
555 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
557 if ( wxTheApp
->GetComCtl32Version() >= 471 )
558 wstyle
|= TVS_FULLROWSELECT
;
562 // using TVS_CHECKBOXES for emulation of a multiselection tree control
563 // doesn't work without the new enough headers
564 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
565 !defined( __GNUWIN32_OLD__ ) && \
566 !defined( __BORLANDC__ ) && \
567 !defined( __WATCOMC__ ) && \
568 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
570 // we emulate the multiple selection tree controls by using checkboxes: set
571 // up the image list we need for this if we do have multiple selections
572 if ( m_windowStyle
& wxTR_MULTIPLE
)
573 wstyle
|= TVS_CHECKBOXES
;
574 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
576 // Create the tree control.
577 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
580 #if wxUSE_COMCTL32_SAFELY
581 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
582 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
584 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
585 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
587 // This works around a bug in the Windows tree control whereby for some versions
588 // of comctrl32, setting any colour actually draws the background in black.
589 // This will initialise the background to the system colour.
590 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
591 // Assume the user has an updated comctl32.dll.
592 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
593 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
594 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
598 // VZ: this is some experimental code which may be used to get the
599 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
600 // AFAIK, the standard DLL does about the same thing anyhow.
602 if ( m_windowStyle
& wxTR_MULTIPLE
)
606 // create the DC compatible with the current screen
607 HDC hdcMem
= CreateCompatibleDC(NULL
);
609 // create a mono bitmap of the standard size
610 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
611 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
612 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
613 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
614 1, // # of color planes
615 1, // # bits needed for one pixel
616 0); // array containing colour data
617 SelectObject(hdcMem
, hbmpCheck
);
619 // then draw a check mark into it
620 RECT rect
= { 0, 0, x
, y
};
621 if ( !::DrawFrameControl(hdcMem
, &rect
,
623 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
625 wxLogLastError(wxT("DrawFrameControl(check)"));
628 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
629 imagelistCheckboxes
.Add(bmp
);
631 if ( !::DrawFrameControl(hdcMem
, &rect
,
635 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
638 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
639 imagelistCheckboxes
.Add(bmp
);
645 SetStateImageList(&imagelistCheckboxes
);
649 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
654 wxTreeCtrl::~wxTreeCtrl()
656 // delete any attributes
659 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
661 delete (wxTreeItemAttr
*)node
->Data();
664 // prevent TVN_DELETEITEM handler from deleting the attributes again!
665 m_hasAnyAttr
= FALSE
;
670 // delete user data to prevent memory leaks
673 if (m_ownsImageListNormal
) delete m_imageListNormal
;
674 if (m_ownsImageListState
) delete m_imageListState
;
677 // ----------------------------------------------------------------------------
679 // ----------------------------------------------------------------------------
681 // simple wrappers which add error checking in debug mode
683 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
685 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
687 wxLogLastError(wxT("TreeView_GetItem"));
695 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
697 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
699 wxLogLastError(wxT("TreeView_SetItem"));
703 size_t wxTreeCtrl::GetCount() const
705 return (size_t)TreeView_GetCount(GetHwnd());
708 unsigned int wxTreeCtrl::GetIndent() const
710 return TreeView_GetIndent(GetHwnd());
713 void wxTreeCtrl::SetIndent(unsigned int indent
)
715 TreeView_SetIndent(GetHwnd(), indent
);
718 wxImageList
*wxTreeCtrl::GetImageList() const
720 return m_imageListNormal
;
723 wxImageList
*wxTreeCtrl::GetStateImageList() const
725 return m_imageListNormal
;
728 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
731 TreeView_SetImageList(GetHwnd(),
732 imageList
? imageList
->GetHIMAGELIST() : 0,
736 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
738 if (m_ownsImageListNormal
) delete m_imageListNormal
;
739 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
740 m_ownsImageListNormal
= FALSE
;
743 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
745 if (m_ownsImageListState
) delete m_imageListState
;
746 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
747 m_ownsImageListState
= FALSE
;
750 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
752 SetImageList(imageList
);
753 m_ownsImageListNormal
= TRUE
;
756 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
758 SetStateImageList(imageList
);
759 m_ownsImageListState
= TRUE
;
762 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
763 bool recursively
) const
765 TraverseCounter
counter(this, item
, recursively
);
767 return counter
.GetCount() - 1;
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
774 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
776 #if !wxUSE_COMCTL32_SAFELY
777 if ( !wxWindowBase::SetBackgroundColour(colour
) )
780 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
786 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
788 #if !wxUSE_COMCTL32_SAFELY
789 if ( !wxWindowBase::SetForegroundColour(colour
) )
792 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
798 // ----------------------------------------------------------------------------
800 // ----------------------------------------------------------------------------
802 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
804 wxChar buf
[512]; // the size is arbitrary...
806 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
807 tvItem
.pszText
= buf
;
808 tvItem
.cchTextMax
= WXSIZEOF(buf
);
809 if ( !DoGetItem(&tvItem
) )
811 // don't return some garbage which was on stack, but an empty string
815 return wxString(buf
);
818 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
820 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
821 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
825 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
826 wxTreeItemIcon which
) const
828 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
829 if ( !DoGetItem(&tvItem
) )
834 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
837 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
839 wxTreeItemIcon which
) const
841 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
842 if ( !DoGetItem(&tvItem
) )
847 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
849 data
->SetImage(image
, which
);
851 // make sure that we have selected images as well
852 if ( which
== wxTreeItemIcon_Normal
&&
853 !data
->HasImage(wxTreeItemIcon_Selected
) )
855 data
->SetImage(image
, wxTreeItemIcon_Selected
);
858 if ( which
== wxTreeItemIcon_Expanded
&&
859 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
861 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
865 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
869 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
870 tvItem
.iSelectedImage
= imageSel
;
871 tvItem
.iImage
= image
;
875 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
876 wxTreeItemIcon which
) const
878 if ( HasIndirectData(item
) )
880 return DoGetItemImageFromData(item
, which
);
887 wxFAIL_MSG( wxT("unknown tree item image type") );
889 case wxTreeItemIcon_Normal
:
893 case wxTreeItemIcon_Selected
:
894 mask
= TVIF_SELECTEDIMAGE
;
897 case wxTreeItemIcon_Expanded
:
898 case wxTreeItemIcon_SelectedExpanded
:
902 wxTreeViewItem
tvItem(item
, mask
);
905 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
908 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
909 wxTreeItemIcon which
)
911 int imageNormal
, imageSel
;
915 wxFAIL_MSG( wxT("unknown tree item image type") );
917 case wxTreeItemIcon_Normal
:
919 imageSel
= GetItemSelectedImage(item
);
922 case wxTreeItemIcon_Selected
:
923 imageNormal
= GetItemImage(item
);
927 case wxTreeItemIcon_Expanded
:
928 case wxTreeItemIcon_SelectedExpanded
:
929 if ( !HasIndirectData(item
) )
931 // we need to get the old images first, because after we create
932 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
934 imageNormal
= GetItemImage(item
);
935 imageSel
= GetItemSelectedImage(item
);
937 // if it doesn't have it yet, add it
938 wxTreeItemIndirectData
*data
= new
939 wxTreeItemIndirectData(this, item
);
941 // copy the data to the new location
942 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
943 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
946 DoSetItemImageFromData(item
, image
, which
);
948 // reset the normal/selected images because we won't use them any
949 // more - now they're stored inside the indirect data
951 imageSel
= I_IMAGECALLBACK
;
955 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
956 // change both normal and selected image - otherwise the change simply
957 // doesn't take place!
958 DoSetItemImages(item
, imageNormal
, imageSel
);
961 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
963 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
964 if ( !DoGetItem(&tvItem
) )
969 if ( HasIndirectData(item
) )
971 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
975 return (wxTreeItemData
*)tvItem
.lParam
;
979 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
981 // first, associate this piece of data with this item
987 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
989 if ( HasIndirectData(item
) )
991 if ( DoGetItem(&tvItem
) )
993 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
997 wxFAIL_MSG( wxT("failed to change tree items data") );
1002 tvItem
.lParam
= (LPARAM
)data
;
1007 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1008 wxTreeItemIndirectData
*data
)
1010 // this should never happen because it's unnecessary and will probably lead
1011 // to crash too because the code elsewhere supposes that the pointer the
1012 // wxTreeItemIndirectData has is a real wxItemData and not
1013 // wxTreeItemIndirectData as well
1014 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1016 SetItemData(item
, (wxTreeItemData
*)data
);
1018 m_itemsWithIndirectData
.Add(item
);
1021 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1023 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
1026 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1028 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1029 tvItem
.cChildren
= (int)has
;
1033 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1035 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1036 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1040 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1042 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1043 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1047 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1050 if ( GetBoundingRect(item
, rect
) )
1056 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1057 const wxColour
& col
)
1059 m_hasAnyAttr
= TRUE
;
1061 long id
= (long)(WXHTREEITEM
)item
;
1062 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1065 attr
= new wxTreeItemAttr
;
1066 m_attrs
.Put(id
, (wxObject
*)attr
);
1069 attr
->SetTextColour(col
);
1074 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1075 const wxColour
& col
)
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
->SetBackgroundColour(col
);
1092 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1094 m_hasAnyAttr
= TRUE
;
1096 long id
= (long)(WXHTREEITEM
)item
;
1097 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1100 attr
= new wxTreeItemAttr
;
1101 m_attrs
.Put(id
, (wxObject
*)attr
);
1104 attr
->SetFont(font
);
1109 // ----------------------------------------------------------------------------
1111 // ----------------------------------------------------------------------------
1113 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1115 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1118 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1119 // the HTREEITEM with TVM_GETITEMRECT
1120 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1122 // FALSE means get item rect for the whole item, not only text
1123 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1126 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1128 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1131 return tvItem
.cChildren
!= 0;
1134 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1136 // probably not a good idea to put it here
1137 //wxASSERT( ItemHasChildren(item) );
1139 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1142 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1145 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1147 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1150 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1153 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1155 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1158 return (tvItem
.state
& TVIS_BOLD
) != 0;
1161 // ----------------------------------------------------------------------------
1163 // ----------------------------------------------------------------------------
1165 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1167 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1170 wxTreeItemId
wxTreeCtrl::GetSelection() const
1172 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1173 wxT("this only works with single selection controls") );
1175 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1178 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1180 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1183 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1184 long& _cookie
) const
1186 // remember the last child returned in 'cookie'
1187 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1189 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1192 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1193 long& _cookie
) const
1195 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1202 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1204 // can this be done more efficiently?
1207 wxTreeItemId childLast
,
1208 child
= GetFirstChild(item
, cookie
);
1209 while ( child
.IsOk() )
1212 child
= GetNextChild(item
, cookie
);
1218 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1220 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1223 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1225 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1228 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1230 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1233 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1235 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1237 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1240 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1242 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1244 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1247 // ----------------------------------------------------------------------------
1248 // multiple selections emulation
1249 // ----------------------------------------------------------------------------
1251 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1253 // receive the desired information.
1254 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1257 // state image indices are 1 based
1258 return ((tvItem
.state
>> 12) - 1) == 1;
1261 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1263 // receive the desired information.
1264 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1266 // state images are one-based
1267 tvItem
.state
= (check
? 2 : 1) << 12;
1272 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1274 TraverseSelections
selector(this, selections
);
1276 return selector
.GetCount();
1279 // ----------------------------------------------------------------------------
1281 // ----------------------------------------------------------------------------
1283 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1284 wxTreeItemId hInsertAfter
,
1285 const wxString
& text
,
1286 int image
, int selectedImage
,
1287 wxTreeItemData
*data
)
1289 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1291 _T("can't have more than one root in the tree") );
1293 TV_INSERTSTRUCT tvIns
;
1294 tvIns
.hParent
= HITEM(parent
);
1295 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1297 // this is how we insert the item as the first child: supply a NULL
1299 if ( !tvIns
.hInsertAfter
)
1301 tvIns
.hInsertAfter
= TVI_FIRST
;
1305 if ( !text
.IsEmpty() )
1308 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1312 tvIns
.item
.pszText
= NULL
;
1313 tvIns
.item
.cchTextMax
= 0;
1319 tvIns
.item
.iImage
= image
;
1321 if ( selectedImage
== -1 )
1323 // take the same image for selected icon if not specified
1324 selectedImage
= image
;
1328 if ( selectedImage
!= -1 )
1330 mask
|= TVIF_SELECTEDIMAGE
;
1331 tvIns
.item
.iSelectedImage
= selectedImage
;
1337 tvIns
.item
.lParam
= (LPARAM
)data
;
1340 tvIns
.item
.mask
= mask
;
1342 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1345 wxLogLastError(wxT("TreeView_InsertItem"));
1350 // associate the application tree item with Win32 tree item handle
1351 data
->SetId((WXHTREEITEM
)id
);
1354 return wxTreeItemId((WXHTREEITEM
)id
);
1357 // for compatibility only
1358 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1359 const wxString
& text
,
1360 int image
, int selImage
,
1363 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1364 image
, selImage
, NULL
);
1367 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1368 int image
, int selectedImage
,
1369 wxTreeItemData
*data
)
1371 return DoInsertItem(wxTreeItemId((long) (WXHTREEITEM
) 0), (long)(WXHTREEITEM
) 0,
1372 text
, image
, selectedImage
, data
);
1375 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1376 const wxString
& text
,
1377 int image
, int selectedImage
,
1378 wxTreeItemData
*data
)
1380 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1381 text
, image
, selectedImage
, data
);
1384 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1385 const wxTreeItemId
& idPrevious
,
1386 const wxString
& text
,
1387 int image
, int selectedImage
,
1388 wxTreeItemData
*data
)
1390 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1393 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1395 const wxString
& text
,
1396 int image
, int selectedImage
,
1397 wxTreeItemData
*data
)
1399 // find the item from index
1401 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1402 while ( index
!= 0 && idCur
.IsOk() )
1407 idCur
= GetNextChild(parent
, cookie
);
1410 // assert, not check: if the index is invalid, we will append the item
1412 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1414 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1417 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1418 const wxString
& text
,
1419 int image
, int selectedImage
,
1420 wxTreeItemData
*data
)
1422 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1423 text
, image
, selectedImage
, data
);
1426 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1428 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1430 wxLogLastError(wxT("TreeView_DeleteItem"));
1434 // delete all children (but don't delete the item itself)
1435 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1439 wxArrayLong children
;
1440 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1441 while ( child
.IsOk() )
1443 children
.Add((long)(WXHTREEITEM
)child
);
1445 child
= GetNextChild(item
, cookie
);
1448 size_t nCount
= children
.Count();
1449 for ( size_t n
= 0; n
< nCount
; n
++ )
1451 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1453 wxLogLastError(wxT("TreeView_DeleteItem"));
1458 void wxTreeCtrl::DeleteAllItems()
1460 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1462 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1466 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1468 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1469 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1470 flag
== TVE_EXPAND
||
1472 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1474 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1475 // emulate them. This behaviour has changed slightly with comctl32.dll
1476 // v 4.70 - now it does send them but only the first time. To maintain
1477 // compatible behaviour and also in order to not have surprises with the
1478 // future versions, don't rely on this and still do everything ourselves.
1479 // To avoid that the messages be sent twice when the item is expanded for
1480 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1482 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1486 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1488 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1489 event
.m_item
= item
;
1491 bool isExpanded
= IsExpanded(item
);
1493 event
.SetEventObject(this);
1495 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1496 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1497 GetEventHandler()->ProcessEvent(event
);
1499 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1500 GetEventHandler()->ProcessEvent(event
);
1502 //else: change didn't took place, so do nothing at all
1505 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1507 DoExpand(item
, TVE_EXPAND
);
1510 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1512 DoExpand(item
, TVE_COLLAPSE
);
1515 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1517 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1520 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1522 DoExpand(item
, TVE_TOGGLE
);
1525 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1527 DoExpand(item
, action
);
1530 void wxTreeCtrl::Unselect()
1532 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1533 wxT("doesn't make sense, may be you want UnselectAll()?") );
1535 // just remove the selection
1536 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1539 void wxTreeCtrl::UnselectAll()
1541 if ( m_windowStyle
& wxTR_MULTIPLE
)
1543 wxArrayTreeItemIds selections
;
1544 size_t count
= GetSelections(selections
);
1545 for ( size_t n
= 0; n
< count
; n
++ )
1547 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1548 SetItemCheck(selections
[n
], FALSE
);
1549 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1550 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1551 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1556 // just remove the selection
1561 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1563 if ( m_windowStyle
& wxTR_MULTIPLE
)
1565 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1566 // selecting the item means checking it
1568 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1569 ::SelectItem(GetHwnd(), HITEM(item
));
1570 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1574 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1575 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1576 // send them ourselves
1578 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1579 event
.m_item
= item
;
1580 event
.SetEventObject(this);
1582 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1583 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1585 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1587 wxLogLastError(wxT("TreeView_SelectItem"));
1591 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1592 (void)GetEventHandler()->ProcessEvent(event
);
1595 //else: program vetoed the change
1599 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1602 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1605 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1607 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1609 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1613 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1615 // normally, we could try to do something like this to return something
1616 // even when the editing was started by the user and not by calling
1617 // EditLabel() - but as nobody has asked for this so far and there might be
1618 // problems in the code below, I leave it disabled for now (VZ)
1622 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1625 m_textCtrl
= new wxTextCtrl(this, -1);
1627 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1629 //else: not editing label right now
1636 void wxTreeCtrl::DeleteTextCtrl()
1640 // the HWND corresponding to this control is deleted by the tree
1641 // control itself and we don't know when exactly this happens, so check
1642 // if the window still exists before calling UnsubclassWin()
1643 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1645 m_textCtrl
->SetHWND(0);
1648 m_textCtrl
->UnsubclassWin();
1649 m_textCtrl
->SetHWND(0);
1655 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1656 wxClassInfo
* textControlClass
)
1658 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1662 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1664 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1671 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1672 m_textCtrl
->SetParent(this);
1673 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1674 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1679 // End label editing, optionally cancelling the edit
1680 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1682 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1687 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1689 TV_HITTESTINFO hitTestInfo
;
1690 hitTestInfo
.pt
.x
= (int)point
.x
;
1691 hitTestInfo
.pt
.y
= (int)point
.y
;
1693 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1698 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1699 flags |= wxTREE_HITTEST_##flag
1701 TRANSLATE_FLAG(ABOVE
);
1702 TRANSLATE_FLAG(BELOW
);
1703 TRANSLATE_FLAG(NOWHERE
);
1704 TRANSLATE_FLAG(ONITEMBUTTON
);
1705 TRANSLATE_FLAG(ONITEMICON
);
1706 TRANSLATE_FLAG(ONITEMINDENT
);
1707 TRANSLATE_FLAG(ONITEMLABEL
);
1708 TRANSLATE_FLAG(ONITEMRIGHT
);
1709 TRANSLATE_FLAG(ONITEMSTATEICON
);
1710 TRANSLATE_FLAG(TOLEFT
);
1711 TRANSLATE_FLAG(TORIGHT
);
1713 #undef TRANSLATE_FLAG
1715 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1718 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1720 bool textOnly
) const
1723 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1726 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1732 // couldn't retrieve rect: for example, item isn't visible
1737 // ----------------------------------------------------------------------------
1739 // ----------------------------------------------------------------------------
1741 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1742 wxTreeItemData
*pItem2
,
1745 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1746 wxT("sorting tree without data doesn't make sense") );
1748 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1751 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1752 const wxTreeItemId
& item2
)
1754 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1757 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1759 // rely on the fact that TreeView_SortChildren does the same thing as our
1760 // default behaviour, i.e. sorts items alphabetically and so call it
1761 // directly if we're not in derived class (much more efficient!)
1762 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1764 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1769 tvSort
.hParent
= HITEM(item
);
1770 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1771 tvSort
.lParam
= (LPARAM
)this;
1772 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1776 // ----------------------------------------------------------------------------
1778 // ----------------------------------------------------------------------------
1780 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1782 if ( cmd
== EN_UPDATE
)
1784 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1785 event
.SetEventObject( this );
1786 ProcessCommand(event
);
1788 else if ( cmd
== EN_KILLFOCUS
)
1790 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1791 event
.SetEventObject( this );
1792 ProcessCommand(event
);
1800 // command processed
1804 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1805 // only do it during dragging, minimize wxWin overhead (this is important for
1806 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1807 // instead of passing by wxWin events
1808 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1810 bool processed
= FALSE
;
1812 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1814 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1816 // we only process mouse messages here and these parameters have the same
1817 // meaning for all of them
1818 int x
= GET_X_LPARAM(lParam
),
1819 y
= GET_Y_LPARAM(lParam
);
1820 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1824 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1825 case WM_LBUTTONDOWN
:
1826 if ( htItem
&& isMultiple
)
1828 if ( wParam
& MK_CONTROL
)
1832 // toggle selected state
1833 ToggleItemSelection(GetHwnd(), htItem
);
1835 ::SetFocus(GetHwnd(), htItem
);
1837 // reset on any click without Shift
1842 else if ( wParam
& MK_SHIFT
)
1844 // this selects all items between the starting one and
1847 if ( !m_htSelStart
)
1849 // take the focused item
1850 m_htSelStart
= (WXHTREEITEM
)
1851 TreeView_GetSelection(GetHwnd());
1854 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1855 !(wParam
& MK_CONTROL
));
1857 ::SetFocus(GetHwnd(), htItem
);
1861 else // normal click
1863 // clear the selection and then let the default handler
1867 // prevent the click from starting in-place editing
1868 // when there was no selection in the control
1869 TreeView_SelectItem(GetHwnd(), 0);
1871 // reset on any click without Shift
1876 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1881 m_dragImage
->Move(wxPoint(x
, y
));
1884 // highlight the item as target (hiding drag image is
1885 // necessary - otherwise the display will be corrupted)
1886 m_dragImage
->Hide();
1887 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1888 m_dragImage
->Show();
1897 m_dragImage
->EndDrag();
1901 // generate the drag end event
1902 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1904 event
.m_item
= (WXHTREEITEM
)htItem
;
1905 event
.m_pointDrag
= wxPoint(x
, y
);
1906 event
.SetEventObject(this);
1908 (void)GetEventHandler()->ProcessEvent(event
);
1910 // if we don't do it, the tree seems to think that 2 items
1911 // are selected simultaneously which is quite weird
1912 TreeView_SelectDropTarget(GetHwnd(), 0);
1917 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1918 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1920 // the tree control greys out the selected item when it loses focus and
1921 // paints it as selected again when it regains it, but it won't do it
1922 // for the other items itself - help it
1923 wxArrayTreeItemIds selections
;
1924 size_t count
= GetSelections(selections
);
1926 for ( size_t n
= 0; n
< count
; n
++ )
1928 // TreeView_GetItemRect() will return FALSE if item is not visible,
1929 // which may happen perfectly well
1930 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1933 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1937 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1939 bool bCtrl
= wxIsCtrlDown(),
1940 bShift
= wxIsShiftDown();
1942 // we handle.arrows and space, but not page up/down and home/end: the
1943 // latter should be easy, but not the former
1945 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1946 if ( !m_htSelStart
)
1948 m_htSelStart
= (WXHTREEITEM
)htSel
;
1951 if ( wParam
== VK_SPACE
)
1955 ToggleItemSelection(GetHwnd(), htSel
);
1961 ::SelectItem(GetHwnd(), htSel
);
1966 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1968 if ( !bCtrl
&& !bShift
)
1970 // no modifiers, just clear selection and then let the default
1971 // processing to take place
1976 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1978 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
1979 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1980 : TreeView_GetNextVisible(GetHwnd(), htSel
));
1984 // at the top/bottom
1990 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1994 // without changing selection
1995 ::SetFocus(GetHwnd(), htNext
);
2002 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2004 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2009 // process WM_NOTIFY Windows message
2010 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2012 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2013 wxEventType eventType
= wxEVT_NULL
;
2014 NMHDR
*hdr
= (NMHDR
*)lParam
;
2016 switch ( hdr
->code
)
2019 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2022 case TVN_BEGINRDRAG
:
2024 if ( eventType
== wxEVT_NULL
)
2025 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2026 //else: left drag, already set above
2028 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2030 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2031 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2033 // don't allow dragging by default: the user code must
2034 // explicitly say that it wants to allow it to avoid breaking
2040 case TVN_BEGINLABELEDIT
:
2042 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2043 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2045 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2046 event
.m_label
= info
->item
.pszText
;
2050 case TVN_DELETEITEM
:
2052 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2053 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2055 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2059 delete (wxTreeItemAttr
*)m_attrs
.
2060 Delete((long)tv
->itemOld
.hItem
);
2065 case TVN_ENDLABELEDIT
:
2067 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2068 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2070 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2071 event
.m_label
= info
->item
.pszText
;
2072 if (info
->item
.pszText
== NULL
)
2077 case TVN_GETDISPINFO
:
2078 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2081 case TVN_SETDISPINFO
:
2083 if ( eventType
== wxEVT_NULL
)
2084 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2085 //else: get, already set above
2087 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2089 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2093 case TVN_ITEMEXPANDING
:
2094 case TVN_ITEMEXPANDED
:
2096 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2098 bool expand
= FALSE
;
2099 switch ( tv
->action
)
2110 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2113 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2114 eventType
= g_events
[expand
][ing
];
2116 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2122 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2123 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2125 // we pass 0 as last CreateKeyEvent() parameter because we
2126 // don't have access to the real key press flags here - but as
2127 // it is only used to determin wxKeyEvent::m_altDown flag it's
2129 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2130 wxCharCodeMSWToWX(info
->wVKey
),
2133 // a separate event for Space/Return
2134 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2135 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2137 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2139 event2
.SetEventObject(this);
2140 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2142 event2
.m_item
= GetSelection();
2144 //else: don't know how to get it
2146 (void)GetEventHandler()->ProcessEvent(event2
);
2151 case TVN_SELCHANGED
:
2152 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2155 case TVN_SELCHANGING
:
2157 if ( eventType
== wxEVT_NULL
)
2158 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2159 //else: already set above
2161 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2163 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2164 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2168 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2171 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2172 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2173 switch ( nmcd
.dwDrawStage
)
2176 // if we've got any items with non standard attributes,
2177 // notify us before painting each item
2178 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2182 case CDDS_ITEMPREPAINT
:
2184 wxTreeItemAttr
*attr
=
2185 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2189 // nothing to do for this item
2190 *result
= CDRF_DODEFAULT
;
2195 wxColour colText
, colBack
;
2196 if ( attr
->HasFont() )
2198 wxFont font
= attr
->GetFont();
2199 hFont
= (HFONT
)font
.GetResourceHandle();
2206 if ( attr
->HasTextColour() )
2208 colText
= attr
->GetTextColour();
2212 colText
= GetForegroundColour();
2215 // selection colours should override ours
2216 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2218 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2219 lptvcd
->clrTextBk
= clrBk
;
2221 // try to make the text visible
2222 lptvcd
->clrText
= wxColourToRGB(colText
);
2223 lptvcd
->clrText
|= ~clrBk
;
2224 lptvcd
->clrText
&= 0x00ffffff;
2228 if ( attr
->HasBackgroundColour() )
2230 colBack
= attr
->GetBackgroundColour();
2234 colBack
= GetBackgroundColour();
2237 lptvcd
->clrText
= wxColourToRGB(colText
);
2238 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2241 // note that if we wanted to set colours for
2242 // individual columns (subitems), we would have
2243 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2246 ::SelectObject(nmcd
.hdc
, hFont
);
2248 *result
= CDRF_NEWFONT
;
2252 *result
= CDRF_DODEFAULT
;
2258 *result
= CDRF_DODEFAULT
;
2262 // we always process it
2264 #endif // _WIN32_IE >= 0x300
2269 TV_HITTESTINFO tvhti
;
2270 ::GetCursorPos(&tvhti
.pt
);
2271 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2272 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2274 if ( tvhti
.flags
& TVHT_ONITEM
)
2276 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2277 eventType
= (int)hdr
->code
== NM_DBLCLK
2278 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2279 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2281 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2282 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2291 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2294 event
.SetEventObject(this);
2295 event
.SetEventType(eventType
);
2297 bool processed
= GetEventHandler()->ProcessEvent(event
);
2300 switch ( hdr
->code
)
2303 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2304 // the return code of this event handler as the return value for
2305 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2306 // expanded status would never work
2311 case TVN_BEGINRDRAG
:
2312 if ( event
.IsAllowed() )
2314 // normally this is impossible because the m_dragImage is
2315 // deleted once the drag operation is over
2316 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2318 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2319 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2320 m_dragImage
->Show();
2324 case TVN_DELETEITEM
:
2326 // NB: we might process this message using wxWindows event
2327 // tables, but due to overhead of wxWin event system we
2328 // prefer to do it here ourself (otherwise deleting a tree
2329 // with many items is just too slow)
2330 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2332 wxTreeItemId item
= event
.m_item
;
2333 if ( HasIndirectData(item
) )
2335 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2337 delete data
; // can't be NULL here
2339 m_itemsWithIndirectData
.Remove(item
);
2341 int iIndex
= m_itemsWithIndirectData
.Index(item
);
2342 wxASSERT( iIndex
!= wxNOT_FOUND
) ;
2343 m_itemsWithIndirectData
.wxBaseArray::RemoveAt((size_t)iIndex
);
2348 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2349 delete data
; // may be NULL, ok
2352 processed
= TRUE
; // Make sure we don't get called twice
2356 case TVN_BEGINLABELEDIT
:
2357 // return TRUE to cancel label editing
2358 *result
= !event
.IsAllowed();
2361 case TVN_ENDLABELEDIT
:
2362 // return TRUE to set the label to the new string: note that we
2363 // also must pretend that we did process the message or it is going
2364 // to be passed to DefWindowProc() which will happily return FALSE
2365 // cancelling the label change
2366 *result
= event
.IsAllowed();
2369 // ensure that we don't have the text ctrl which is going to be
2374 case TVN_SELCHANGING
:
2375 case TVN_ITEMEXPANDING
:
2376 // return TRUE to prevent the action from happening
2377 *result
= !event
.IsAllowed();
2380 case TVN_GETDISPINFO
:
2381 // NB: so far the user can't set the image himself anyhow, so do it
2382 // anyway - but this may change later
2383 // if ( /* !processed && */ 1 )
2385 wxTreeItemId item
= event
.m_item
;
2386 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2387 if ( info
->item
.mask
& TVIF_IMAGE
)
2390 DoGetItemImageFromData
2393 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2394 : wxTreeItemIcon_Normal
2397 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2399 info
->item
.iSelectedImage
=
2400 DoGetItemImageFromData
2403 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2404 : wxTreeItemIcon_Selected
2411 // for the other messages the return value is ignored and there is
2412 // nothing special to do
2419 #endif // wxUSE_TREECTRL