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 // old headers might miss these messages (comctl32.dll 4.71+ only)
80 #ifndef TVM_SETBKCOLOR
81 #define TVM_SETBKCOLOR (TV_FIRST + 29)
82 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
85 // a macro to hide the ugliness of nested casts
86 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
88 // the native control doesn't support multiple selections under MSW and we
89 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
90 // checkboxes be the selection status (checked == selected) or by really
91 // emulating everything, i.e. intercepting mouse and key events &c. The first
92 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
94 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
97 // ----------------------------------------------------------------------------
99 // ----------------------------------------------------------------------------
101 // wrapper for TreeView_HitTest
102 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
108 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
111 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
113 // wrappers for TreeView_GetItem/TreeView_SetItem
114 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
117 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
118 tvi
.stateMask
= TVIS_SELECTED
;
121 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
123 wxLogLastError(wxT("TreeView_GetItem"));
126 return (tvi
.state
& TVIS_SELECTED
) != 0;
129 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
132 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
133 tvi
.stateMask
= TVIS_SELECTED
;
134 tvi
.state
= select
? TVIS_SELECTED
: 0;
137 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
139 wxLogLastError(wxT("TreeView_SetItem"));
143 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
145 SelectItem(hwndTV
, htItem
, FALSE
);
148 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
150 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
153 // helper function which selects all items in a range and, optionally,
154 // unselects all others
155 static void SelectRange(HWND hwndTV
,
158 bool unselectOthers
= TRUE
)
160 // find the first (or last) item and select it
162 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
163 while ( htItem
&& cont
)
165 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
167 if ( !IsItemSelected(hwndTV
, htItem
) )
169 SelectItem(hwndTV
, htItem
);
176 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
178 UnselectItem(hwndTV
, htItem
);
182 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
185 // select the items in range
186 cont
= htFirst
!= htLast
;
187 while ( htItem
&& cont
)
189 if ( !IsItemSelected(hwndTV
, htItem
) )
191 SelectItem(hwndTV
, htItem
);
194 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
196 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
200 if ( unselectOthers
)
204 if ( IsItemSelected(hwndTV
, htItem
) )
206 UnselectItem(hwndTV
, htItem
);
209 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
213 // seems to be necessary - otherwise the just selected items don't always
214 // appear as selected
215 UpdateWindow(hwndTV
);
218 // helper function which tricks the standard control into changing the focused
219 // item without changing anything else (if someone knows why Microsoft doesn't
220 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
221 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
224 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
229 if ( htItem
!= htFocus
)
231 // remember the selection state of the item
232 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
234 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
236 // prevent the tree from unselecting the old focus which it
237 // would do by default (TreeView_SelectItem unselects the
239 TreeView_SelectItem(hwndTV
, 0);
240 SelectItem(hwndTV
, htFocus
);
243 TreeView_SelectItem(hwndTV
, htItem
);
247 // need to clear the selection which TreeView_SelectItem() gave
249 UnselectItem(hwndTV
, htItem
);
251 //else: was selected, still selected - ok
253 //else: nothing to do, focus already there
259 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
261 // just clear the focus
262 TreeView_SelectItem(hwndTV
, 0);
264 if ( wasFocusSelected
)
266 // restore the selection state
267 SelectItem(hwndTV
, htFocus
);
270 //else: nothing to do, no focus already
274 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 // a convenient wrapper around TV_ITEM struct which adds a ctor
282 #pragma warning( disable : 4097 ) // inheriting from typedef
285 struct wxTreeViewItem
: public TV_ITEM
287 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
288 UINT mask_
, // fields which are valid
289 UINT stateMask_
= 0) // for TVIF_STATE only
291 // hItem member is always valid
292 mask
= mask_
| TVIF_HANDLE
;
293 stateMask
= stateMask_
;
299 #pragma warning( default : 4097 )
302 // a class which encapsulates the tree traversal logic: it vists all (unless
303 // OnVisit() returns FALSE) items under the given one
304 class wxTreeTraversal
307 wxTreeTraversal(const wxTreeCtrl
*tree
)
312 // do traverse the tree: visit all items (recursively by default) under the
313 // given one; return TRUE if all items were traversed or FALSE if the
314 // traversal was aborted because OnVisit returned FALSE
315 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
317 // override this function to do whatever is needed for each item, return
318 // FALSE to stop traversing
319 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
322 const wxTreeCtrl
*GetTree() const { return m_tree
; }
325 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
327 const wxTreeCtrl
*m_tree
;
330 // internal class for getting the selected items
331 class TraverseSelections
: public wxTreeTraversal
334 TraverseSelections(const wxTreeCtrl
*tree
,
335 wxArrayTreeItemIds
& selections
)
336 : wxTreeTraversal(tree
), m_selections(selections
)
338 m_selections
.Empty();
340 DoTraverse(tree
->GetRootItem());
343 virtual bool OnVisit(const wxTreeItemId
& item
)
345 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
346 if ( GetTree()->IsItemChecked(item
) )
348 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
351 m_selections
.Add(item
);
357 size_t GetCount() const { return m_selections
.GetCount(); }
360 wxArrayTreeItemIds
& m_selections
;
363 // internal class for counting tree items
364 class TraverseCounter
: public wxTreeTraversal
367 TraverseCounter(const wxTreeCtrl
*tree
,
368 const wxTreeItemId
& root
,
370 : wxTreeTraversal(tree
)
374 DoTraverse(root
, recursively
);
377 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
384 size_t GetCount() const { return m_count
; }
390 // ----------------------------------------------------------------------------
391 // This class is needed for support of different images: the Win32 common
392 // control natively supports only 2 images (the normal one and another for the
393 // selected state). We wish to provide support for 2 more of them for folder
394 // items (i.e. those which have children): for expanded state and for expanded
395 // selected state. For this we use this structure to store the additional items
398 // There is only one problem with this: when we retrieve the item's data, we
399 // don't know whether we get a pointer to wxTreeItemData or
400 // wxTreeItemIndirectData. So we have to maintain a list of all items which
401 // have indirect data inside the listctrl itself.
402 // ----------------------------------------------------------------------------
404 class wxTreeItemIndirectData
407 // ctor associates this data with the item and the real item data becomes
408 // available through our GetData() method
409 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
411 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
417 m_data
= tree
->GetItemData(item
);
419 // and set ourselves as the new one
420 tree
->SetIndirectItemData(item
, this);
423 // dtor deletes the associated data as well
424 ~wxTreeItemIndirectData() { delete m_data
; }
427 // get the real data associated with the item
428 wxTreeItemData
*GetData() const { return m_data
; }
430 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
432 // do we have such image?
433 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
435 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
437 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
440 // all the images associated with the item
441 int m_images
[wxTreeItemIcon_Max
];
443 wxTreeItemData
*m_data
;
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
452 // ----------------------------------------------------------------------------
454 // ----------------------------------------------------------------------------
456 // handy table for sending events
457 static wxEventType g_events
[2][2] =
459 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
460 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
463 // ============================================================================
465 // ============================================================================
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
473 if ( !OnVisit(root
) )
476 return Traverse(root
, recursively
);
479 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
482 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
483 while ( child
.IsOk() )
485 // depth first traversal
486 if ( recursively
&& !Traverse(child
, TRUE
) )
489 if ( !OnVisit(child
) )
492 child
= m_tree
->GetNextChild(root
, cookie
);
498 // ----------------------------------------------------------------------------
499 // construction and destruction
500 // ----------------------------------------------------------------------------
502 void wxTreeCtrl::Init()
504 m_imageListNormal
= NULL
;
505 m_imageListState
= NULL
;
506 m_ownsImageListNormal
= m_ownsImageListState
= FALSE
;
508 m_hasAnyAttr
= FALSE
;
512 // Initialize static array of events, because with the new event system,
513 // they may not be initialized yet.
515 g_events
[0][0] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
516 g_events
[0][1] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
517 g_events
[1][0] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
518 g_events
[1][1] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
521 bool wxTreeCtrl::Create(wxWindow
*parent
,
526 const wxValidator
& validator
,
527 const wxString
& name
)
531 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
534 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
537 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
538 wstyle
|= WS_CLIPSIBLINGS
;
540 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
541 wstyle
|= TVS_HASLINES
;
542 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
543 wstyle
|= TVS_HASBUTTONS
;
545 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
546 wstyle
|= TVS_EDITLABELS
;
548 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
549 wstyle
|= TVS_LINESATROOT
;
551 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
553 if ( wxTheApp
->GetComCtl32Version() >= 471 )
554 wstyle
|= TVS_FULLROWSELECT
;
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::RefreshItem(const wxTreeItemId
& item
)
1046 if ( GetBoundingRect(item
, rect
) )
1052 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1053 const wxColour
& col
)
1055 m_hasAnyAttr
= TRUE
;
1057 long id
= (long)(WXHTREEITEM
)item
;
1058 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1061 attr
= new wxTreeItemAttr
;
1062 m_attrs
.Put(id
, (wxObject
*)attr
);
1065 attr
->SetTextColour(col
);
1070 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1071 const wxColour
& col
)
1073 m_hasAnyAttr
= TRUE
;
1075 long id
= (long)(WXHTREEITEM
)item
;
1076 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1079 attr
= new wxTreeItemAttr
;
1080 m_attrs
.Put(id
, (wxObject
*)attr
);
1083 attr
->SetBackgroundColour(col
);
1088 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1090 m_hasAnyAttr
= TRUE
;
1092 long id
= (long)(WXHTREEITEM
)item
;
1093 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1096 attr
= new wxTreeItemAttr
;
1097 m_attrs
.Put(id
, (wxObject
*)attr
);
1100 attr
->SetFont(font
);
1105 // ----------------------------------------------------------------------------
1107 // ----------------------------------------------------------------------------
1109 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1111 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1114 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1115 // the HTREEITEM with TVM_GETITEMRECT
1116 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1118 // FALSE means get item rect for the whole item, not only text
1119 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1122 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1124 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1127 return tvItem
.cChildren
!= 0;
1130 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1132 // probably not a good idea to put it here
1133 //wxASSERT( ItemHasChildren(item) );
1135 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1138 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1141 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1143 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1146 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1149 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1151 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1154 return (tvItem
.state
& TVIS_BOLD
) != 0;
1157 // ----------------------------------------------------------------------------
1159 // ----------------------------------------------------------------------------
1161 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1163 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1166 wxTreeItemId
wxTreeCtrl::GetSelection() const
1168 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1169 wxT("this only works with single selection controls") );
1171 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1174 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1176 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1179 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1180 long& _cookie
) const
1182 // remember the last child returned in 'cookie'
1183 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1185 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1188 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1189 long& _cookie
) const
1191 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1198 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1200 // can this be done more efficiently?
1203 wxTreeItemId childLast
,
1204 child
= GetFirstChild(item
, cookie
);
1205 while ( child
.IsOk() )
1208 child
= GetNextChild(item
, cookie
);
1214 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1216 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1219 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1221 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1224 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1226 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1229 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1231 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1233 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1236 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1238 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1240 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1243 // ----------------------------------------------------------------------------
1244 // multiple selections emulation
1245 // ----------------------------------------------------------------------------
1247 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1249 // receive the desired information.
1250 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1253 // state image indices are 1 based
1254 return ((tvItem
.state
>> 12) - 1) == 1;
1257 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1259 // receive the desired information.
1260 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1262 // state images are one-based
1263 tvItem
.state
= (check
? 2 : 1) << 12;
1268 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1270 TraverseSelections
selector(this, selections
);
1272 return selector
.GetCount();
1275 // ----------------------------------------------------------------------------
1277 // ----------------------------------------------------------------------------
1279 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1280 wxTreeItemId hInsertAfter
,
1281 const wxString
& text
,
1282 int image
, int selectedImage
,
1283 wxTreeItemData
*data
)
1285 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1287 _T("can't have more than one root in the tree") );
1289 TV_INSERTSTRUCT tvIns
;
1290 tvIns
.hParent
= HITEM(parent
);
1291 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1293 // this is how we insert the item as the first child: supply a NULL
1295 if ( !tvIns
.hInsertAfter
)
1297 tvIns
.hInsertAfter
= TVI_FIRST
;
1301 if ( !text
.IsEmpty() )
1304 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1308 tvIns
.item
.pszText
= NULL
;
1309 tvIns
.item
.cchTextMax
= 0;
1315 tvIns
.item
.iImage
= image
;
1317 if ( selectedImage
== -1 )
1319 // take the same image for selected icon if not specified
1320 selectedImage
= image
;
1324 if ( selectedImage
!= -1 )
1326 mask
|= TVIF_SELECTEDIMAGE
;
1327 tvIns
.item
.iSelectedImage
= selectedImage
;
1333 tvIns
.item
.lParam
= (LPARAM
)data
;
1336 tvIns
.item
.mask
= mask
;
1338 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1341 wxLogLastError(wxT("TreeView_InsertItem"));
1346 // associate the application tree item with Win32 tree item handle
1347 data
->SetId((WXHTREEITEM
)id
);
1350 return wxTreeItemId((WXHTREEITEM
)id
);
1353 // for compatibility only
1354 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1355 const wxString
& text
,
1356 int image
, int selImage
,
1359 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1360 image
, selImage
, NULL
);
1363 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1364 int image
, int selectedImage
,
1365 wxTreeItemData
*data
)
1367 return DoInsertItem(wxTreeItemId((long) (WXHTREEITEM
) 0), (long)(WXHTREEITEM
) 0,
1368 text
, image
, selectedImage
, data
);
1371 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1372 const wxString
& text
,
1373 int image
, int selectedImage
,
1374 wxTreeItemData
*data
)
1376 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1377 text
, image
, selectedImage
, data
);
1380 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1381 const wxTreeItemId
& idPrevious
,
1382 const wxString
& text
,
1383 int image
, int selectedImage
,
1384 wxTreeItemData
*data
)
1386 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1389 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1391 const wxString
& text
,
1392 int image
, int selectedImage
,
1393 wxTreeItemData
*data
)
1395 // find the item from index
1397 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1398 while ( index
!= 0 && idCur
.IsOk() )
1403 idCur
= GetNextChild(parent
, cookie
);
1406 // assert, not check: if the index is invalid, we will append the item
1408 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1410 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1413 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1414 const wxString
& text
,
1415 int image
, int selectedImage
,
1416 wxTreeItemData
*data
)
1418 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1419 text
, image
, selectedImage
, data
);
1422 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1424 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1426 wxLogLastError(wxT("TreeView_DeleteItem"));
1430 // delete all children (but don't delete the item itself)
1431 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1435 wxArrayLong children
;
1436 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1437 while ( child
.IsOk() )
1439 children
.Add((long)(WXHTREEITEM
)child
);
1441 child
= GetNextChild(item
, cookie
);
1444 size_t nCount
= children
.Count();
1445 for ( size_t n
= 0; n
< nCount
; n
++ )
1447 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1449 wxLogLastError(wxT("TreeView_DeleteItem"));
1454 void wxTreeCtrl::DeleteAllItems()
1456 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1458 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1462 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1464 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1465 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1466 flag
== TVE_EXPAND
||
1468 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1470 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1471 // emulate them. This behaviour has changed slightly with comctl32.dll
1472 // v 4.70 - now it does send them but only the first time. To maintain
1473 // compatible behaviour and also in order to not have surprises with the
1474 // future versions, don't rely on this and still do everything ourselves.
1475 // To avoid that the messages be sent twice when the item is expanded for
1476 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1478 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1482 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1484 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1485 event
.m_item
= item
;
1487 bool isExpanded
= IsExpanded(item
);
1489 event
.SetEventObject(this);
1491 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1492 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1493 GetEventHandler()->ProcessEvent(event
);
1495 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1496 GetEventHandler()->ProcessEvent(event
);
1498 //else: change didn't took place, so do nothing at all
1501 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1503 DoExpand(item
, TVE_EXPAND
);
1506 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1508 DoExpand(item
, TVE_COLLAPSE
);
1511 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1513 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1516 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1518 DoExpand(item
, TVE_TOGGLE
);
1521 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1523 DoExpand(item
, action
);
1526 void wxTreeCtrl::Unselect()
1528 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1529 wxT("doesn't make sense, may be you want UnselectAll()?") );
1531 // just remove the selection
1532 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1535 void wxTreeCtrl::UnselectAll()
1537 if ( m_windowStyle
& wxTR_MULTIPLE
)
1539 wxArrayTreeItemIds selections
;
1540 size_t count
= GetSelections(selections
);
1541 for ( size_t n
= 0; n
< count
; n
++ )
1543 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1544 SetItemCheck(selections
[n
], FALSE
);
1545 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1546 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1547 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1552 // just remove the selection
1557 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1559 if ( m_windowStyle
& wxTR_MULTIPLE
)
1561 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1562 // selecting the item means checking it
1564 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1565 ::SelectItem(GetHwnd(), HITEM(item
));
1566 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1570 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1571 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1572 // send them ourselves
1574 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1575 event
.m_item
= item
;
1576 event
.SetEventObject(this);
1578 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1579 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1581 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1583 wxLogLastError(wxT("TreeView_SelectItem"));
1587 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1588 (void)GetEventHandler()->ProcessEvent(event
);
1591 //else: program vetoed the change
1595 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1598 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1601 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1603 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1605 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1609 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1611 // normally, we could try to do something like this to return something
1612 // even when the editing was started by the user and not by calling
1613 // EditLabel() - but as nobody has asked for this so far and there might be
1614 // problems in the code below, I leave it disabled for now (VZ)
1618 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1621 m_textCtrl
= new wxTextCtrl(this, -1);
1623 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1625 //else: not editing label right now
1632 void wxTreeCtrl::DeleteTextCtrl()
1636 // the HWND corresponding to this control is deleted by the tree
1637 // control itself and we don't know when exactly this happens, so check
1638 // if the window still exists before calling UnsubclassWin()
1639 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1641 m_textCtrl
->SetHWND(0);
1644 m_textCtrl
->UnsubclassWin();
1645 m_textCtrl
->SetHWND(0);
1651 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1652 wxClassInfo
* textControlClass
)
1654 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1658 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1660 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1667 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1668 m_textCtrl
->SetParent(this);
1669 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1670 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1675 // End label editing, optionally cancelling the edit
1676 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1678 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1683 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1685 TV_HITTESTINFO hitTestInfo
;
1686 hitTestInfo
.pt
.x
= (int)point
.x
;
1687 hitTestInfo
.pt
.y
= (int)point
.y
;
1689 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1694 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1695 flags |= wxTREE_HITTEST_##flag
1697 TRANSLATE_FLAG(ABOVE
);
1698 TRANSLATE_FLAG(BELOW
);
1699 TRANSLATE_FLAG(NOWHERE
);
1700 TRANSLATE_FLAG(ONITEMBUTTON
);
1701 TRANSLATE_FLAG(ONITEMICON
);
1702 TRANSLATE_FLAG(ONITEMINDENT
);
1703 TRANSLATE_FLAG(ONITEMLABEL
);
1704 TRANSLATE_FLAG(ONITEMRIGHT
);
1705 TRANSLATE_FLAG(ONITEMSTATEICON
);
1706 TRANSLATE_FLAG(TOLEFT
);
1707 TRANSLATE_FLAG(TORIGHT
);
1709 #undef TRANSLATE_FLAG
1711 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1714 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1716 bool textOnly
) const
1719 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1722 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1728 // couldn't retrieve rect: for example, item isn't visible
1733 // ----------------------------------------------------------------------------
1735 // ----------------------------------------------------------------------------
1737 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1738 wxTreeItemData
*pItem2
,
1741 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1742 wxT("sorting tree without data doesn't make sense") );
1744 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1747 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1748 const wxTreeItemId
& item2
)
1750 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1753 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1755 // rely on the fact that TreeView_SortChildren does the same thing as our
1756 // default behaviour, i.e. sorts items alphabetically and so call it
1757 // directly if we're not in derived class (much more efficient!)
1758 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1760 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1765 tvSort
.hParent
= HITEM(item
);
1766 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1767 tvSort
.lParam
= (LPARAM
)this;
1768 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1772 // ----------------------------------------------------------------------------
1774 // ----------------------------------------------------------------------------
1776 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1778 if ( cmd
== EN_UPDATE
)
1780 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1781 event
.SetEventObject( this );
1782 ProcessCommand(event
);
1784 else if ( cmd
== EN_KILLFOCUS
)
1786 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1787 event
.SetEventObject( this );
1788 ProcessCommand(event
);
1796 // command processed
1800 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1801 // only do it during dragging, minimize wxWin overhead (this is important for
1802 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1803 // instead of passing by wxWin events
1804 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1806 bool processed
= FALSE
;
1808 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1810 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1812 // we only process mouse messages here and these parameters have the same
1813 // meaning for all of them
1814 int x
= GET_X_LPARAM(lParam
),
1815 y
= GET_Y_LPARAM(lParam
);
1816 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1820 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1821 case WM_LBUTTONDOWN
:
1822 if ( htItem
&& isMultiple
)
1824 if ( wParam
& MK_CONTROL
)
1828 // toggle selected state
1829 ToggleItemSelection(GetHwnd(), htItem
);
1831 ::SetFocus(GetHwnd(), htItem
);
1833 // reset on any click without Shift
1838 else if ( wParam
& MK_SHIFT
)
1840 // this selects all items between the starting one and
1843 if ( !m_htSelStart
)
1845 // take the focused item
1846 m_htSelStart
= (WXHTREEITEM
)
1847 TreeView_GetSelection(GetHwnd());
1850 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1851 !(wParam
& MK_CONTROL
));
1853 ::SetFocus(GetHwnd(), htItem
);
1857 else // normal click
1859 // clear the selection and then let the default handler
1863 // prevent the click from starting in-place editing
1864 // when there was no selection in the control
1865 TreeView_SelectItem(GetHwnd(), 0);
1867 // reset on any click without Shift
1872 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1877 m_dragImage
->Move(wxPoint(x
, y
));
1880 // highlight the item as target (hiding drag image is
1881 // necessary - otherwise the display will be corrupted)
1882 m_dragImage
->Hide();
1883 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1884 m_dragImage
->Show();
1893 m_dragImage
->EndDrag();
1897 // generate the drag end event
1898 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1900 event
.m_item
= (WXHTREEITEM
)htItem
;
1901 event
.m_pointDrag
= wxPoint(x
, y
);
1902 event
.SetEventObject(this);
1904 (void)GetEventHandler()->ProcessEvent(event
);
1906 // if we don't do it, the tree seems to think that 2 items
1907 // are selected simultaneously which is quite weird
1908 TreeView_SelectDropTarget(GetHwnd(), 0);
1913 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1914 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1916 // the tree control greys out the selected item when it loses focus and
1917 // paints it as selected again when it regains it, but it won't do it
1918 // for the other items itself - help it
1919 wxArrayTreeItemIds selections
;
1920 size_t count
= GetSelections(selections
);
1922 for ( size_t n
= 0; n
< count
; n
++ )
1924 // TreeView_GetItemRect() will return FALSE if item is not visible,
1925 // which may happen perfectly well
1926 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1929 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1933 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1935 bool bCtrl
= wxIsCtrlDown(),
1936 bShift
= wxIsShiftDown();
1938 // we handle.arrows and space, but not page up/down and home/end: the
1939 // latter should be easy, but not the former
1941 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1942 if ( !m_htSelStart
)
1944 m_htSelStart
= (WXHTREEITEM
)htSel
;
1947 if ( wParam
== VK_SPACE
)
1951 ToggleItemSelection(GetHwnd(), htSel
);
1957 ::SelectItem(GetHwnd(), htSel
);
1962 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1964 if ( !bCtrl
&& !bShift
)
1966 // no modifiers, just clear selection and then let the default
1967 // processing to take place
1972 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1974 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
1975 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1976 : TreeView_GetNextVisible(GetHwnd(), htSel
));
1980 // at the top/bottom
1986 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1990 // without changing selection
1991 ::SetFocus(GetHwnd(), htNext
);
1998 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2000 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2005 // process WM_NOTIFY Windows message
2006 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2008 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2009 wxEventType eventType
= wxEVT_NULL
;
2010 NMHDR
*hdr
= (NMHDR
*)lParam
;
2012 switch ( hdr
->code
)
2015 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2018 case TVN_BEGINRDRAG
:
2020 if ( eventType
== wxEVT_NULL
)
2021 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2022 //else: left drag, already set above
2024 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2026 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2027 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2029 // don't allow dragging by default: the user code must
2030 // explicitly say that it wants to allow it to avoid breaking
2036 case TVN_BEGINLABELEDIT
:
2038 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2039 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2041 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2042 event
.m_label
= info
->item
.pszText
;
2046 case TVN_DELETEITEM
:
2048 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2049 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2051 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2055 delete (wxTreeItemAttr
*)m_attrs
.
2056 Delete((long)tv
->itemOld
.hItem
);
2061 case TVN_ENDLABELEDIT
:
2063 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2064 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2066 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2067 event
.m_label
= info
->item
.pszText
;
2068 if (info
->item
.pszText
== NULL
)
2073 case TVN_GETDISPINFO
:
2074 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2077 case TVN_SETDISPINFO
:
2079 if ( eventType
== wxEVT_NULL
)
2080 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2081 //else: get, already set above
2083 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2085 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2089 case TVN_ITEMEXPANDING
:
2090 case TVN_ITEMEXPANDED
:
2092 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2094 bool expand
= FALSE
;
2095 switch ( tv
->action
)
2106 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2109 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2110 eventType
= g_events
[expand
][ing
];
2112 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2118 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2119 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2121 // we pass 0 as last CreateKeyEvent() parameter because we
2122 // don't have access to the real key press flags here - but as
2123 // it is only used to determin wxKeyEvent::m_altDown flag it's
2125 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2126 wxCharCodeMSWToWX(info
->wVKey
),
2129 // a separate event for Space/Return
2130 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2131 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2133 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2135 event2
.SetEventObject(this);
2136 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2138 event2
.m_item
= GetSelection();
2140 //else: don't know how to get it
2142 (void)GetEventHandler()->ProcessEvent(event2
);
2147 case TVN_SELCHANGED
:
2148 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2151 case TVN_SELCHANGING
:
2153 if ( eventType
== wxEVT_NULL
)
2154 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2155 //else: already set above
2157 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2159 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2160 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2164 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2167 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2168 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2169 switch ( nmcd
.dwDrawStage
)
2172 // if we've got any items with non standard attributes,
2173 // notify us before painting each item
2174 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2178 case CDDS_ITEMPREPAINT
:
2180 wxTreeItemAttr
*attr
=
2181 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2185 // nothing to do for this item
2186 *result
= CDRF_DODEFAULT
;
2191 wxColour colText
, colBack
;
2192 if ( attr
->HasFont() )
2194 wxFont font
= attr
->GetFont();
2195 hFont
= (HFONT
)font
.GetResourceHandle();
2202 if ( attr
->HasTextColour() )
2204 colText
= attr
->GetTextColour();
2208 colText
= GetForegroundColour();
2211 // selection colours should override ours
2212 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2214 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2215 lptvcd
->clrTextBk
= clrBk
;
2217 // try to make the text visible
2218 lptvcd
->clrText
= wxColourToRGB(colText
);
2219 lptvcd
->clrText
|= ~clrBk
;
2220 lptvcd
->clrText
&= 0x00ffffff;
2224 if ( attr
->HasBackgroundColour() )
2226 colBack
= attr
->GetBackgroundColour();
2230 colBack
= GetBackgroundColour();
2233 lptvcd
->clrText
= wxColourToRGB(colText
);
2234 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2237 // note that if we wanted to set colours for
2238 // individual columns (subitems), we would have
2239 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2242 ::SelectObject(nmcd
.hdc
, hFont
);
2244 *result
= CDRF_NEWFONT
;
2248 *result
= CDRF_DODEFAULT
;
2254 *result
= CDRF_DODEFAULT
;
2258 // we always process it
2260 #endif // _WIN32_IE >= 0x300
2265 TV_HITTESTINFO tvhti
;
2266 ::GetCursorPos(&tvhti
.pt
);
2267 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2268 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2270 if ( tvhti
.flags
& TVHT_ONITEM
)
2272 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2273 eventType
= (int)hdr
->code
== NM_DBLCLK
2274 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2275 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2277 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2278 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2287 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2290 event
.SetEventObject(this);
2291 event
.SetEventType(eventType
);
2293 bool processed
= GetEventHandler()->ProcessEvent(event
);
2296 switch ( hdr
->code
)
2299 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2300 // the return code of this event handler as the return value for
2301 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2302 // expanded status would never work
2307 case TVN_BEGINRDRAG
:
2308 if ( event
.IsAllowed() )
2310 // normally this is impossible because the m_dragImage is
2311 // deleted once the drag operation is over
2312 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2314 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2315 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2316 m_dragImage
->Show();
2320 case TVN_DELETEITEM
:
2322 // NB: we might process this message using wxWindows event
2323 // tables, but due to overhead of wxWin event system we
2324 // prefer to do it here ourself (otherwise deleting a tree
2325 // with many items is just too slow)
2326 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2328 wxTreeItemId item
= event
.m_item
;
2329 if ( HasIndirectData(item
) )
2331 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2333 delete data
; // can't be NULL here
2335 m_itemsWithIndirectData
.Remove(item
);
2337 int iIndex
= m_itemsWithIndirectData
.Index(item
);
2338 wxASSERT( iIndex
!= wxNOT_FOUND
) ;
2339 m_itemsWithIndirectData
.wxBaseArray::RemoveAt((size_t)iIndex
);
2344 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2345 delete data
; // may be NULL, ok
2348 processed
= TRUE
; // Make sure we don't get called twice
2352 case TVN_BEGINLABELEDIT
:
2353 // return TRUE to cancel label editing
2354 *result
= !event
.IsAllowed();
2357 case TVN_ENDLABELEDIT
:
2358 // return TRUE to set the label to the new string: note that we
2359 // also must pretend that we did process the message or it is going
2360 // to be passed to DefWindowProc() which will happily return FALSE
2361 // cancelling the label change
2362 *result
= event
.IsAllowed();
2365 // ensure that we don't have the text ctrl which is going to be
2370 case TVN_SELCHANGING
:
2371 case TVN_ITEMEXPANDING
:
2372 // return TRUE to prevent the action from happening
2373 *result
= !event
.IsAllowed();
2376 case TVN_GETDISPINFO
:
2377 // NB: so far the user can't set the image himself anyhow, so do it
2378 // anyway - but this may change later
2379 // if ( /* !processed && */ 1 )
2381 wxTreeItemId item
= event
.m_item
;
2382 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2383 if ( info
->item
.mask
& TVIF_IMAGE
)
2386 DoGetItemImageFromData
2389 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2390 : wxTreeItemIcon_Normal
2393 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2395 info
->item
.iSelectedImage
=
2396 DoGetItemImageFromData
2399 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2400 : wxTreeItemIcon_Selected
2407 // for the other messages the return value is ignored and there is
2408 // nothing special to do
2415 #endif // wxUSE_TREECTRL