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
41 #include "wx/dynarray.h"
42 #include "wx/imaglist.h"
43 #include "wx/settings.h"
44 #include "wx/msw/treectrl.h"
45 #include "wx/msw/dragimag.h"
47 #ifdef __GNUWIN32_OLD__
48 #include "wx/msw/gnuwin32/extra.h"
51 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) && !defined(__CYGWIN10__))
55 // Bug in headers, sometimes
57 #define TVIS_FOCUSED 0x0001
61 #define TV_FIRST 0x1100
64 #ifndef TVS_CHECKBOXES
65 #define TVS_CHECKBOXES 0x0100
68 #ifndef TVS_FULLROWSELECT
69 #define TVS_FULLROWSELECT 0x1000
72 // old headers might miss these messages (comctl32.dll 4.71+ only)
73 #ifndef TVM_SETBKCOLOR
74 #define TVM_SETBKCOLOR (TV_FIRST + 29)
75 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
78 // a macro to hide the cast ugliness
79 #define HITEM(item) (HTREEITEM)((item).m_pItem)
81 // the native control doesn't support multiple selections under MSW and we
82 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
83 // checkboxes be the selection status (checked == selected) or by really
84 // emulating everything, i.e. intercepting mouse and key events &c. The first
85 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
87 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // wrapper for TreeView_HitTest
94 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
100 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
103 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
105 // wrappers for TreeView_GetItem/TreeView_SetItem
106 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
110 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
111 tvi
.stateMask
= TVIS_SELECTED
;
114 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
116 wxLogLastError(wxT("TreeView_GetItem"));
119 return (tvi
.state
& TVIS_SELECTED
) != 0;
122 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
125 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
126 tvi
.stateMask
= TVIS_SELECTED
;
127 tvi
.state
= select
? TVIS_SELECTED
: 0;
130 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
132 wxLogLastError(wxT("TreeView_SetItem"));
136 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
138 SelectItem(hwndTV
, htItem
, false);
141 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
143 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
146 // helper function which selects all items in a range and, optionally,
147 // unselects all others
148 static void SelectRange(HWND hwndTV
,
151 bool unselectOthers
= true)
153 // find the first (or last) item and select it
155 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
156 while ( htItem
&& cont
)
158 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
160 if ( !IsItemSelected(hwndTV
, htItem
) )
162 SelectItem(hwndTV
, htItem
);
169 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
171 UnselectItem(hwndTV
, htItem
);
175 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
178 // select the items in range
179 cont
= htFirst
!= htLast
;
180 while ( htItem
&& cont
)
182 if ( !IsItemSelected(hwndTV
, htItem
) )
184 SelectItem(hwndTV
, htItem
);
187 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
189 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
193 if ( unselectOthers
)
197 if ( IsItemSelected(hwndTV
, htItem
) )
199 UnselectItem(hwndTV
, htItem
);
202 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
206 // seems to be necessary - otherwise the just selected items don't always
207 // appear as selected
208 UpdateWindow(hwndTV
);
211 // helper function which tricks the standard control into changing the focused
212 // item without changing anything else (if someone knows why Microsoft doesn't
213 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
214 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
217 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
222 if ( htItem
!= htFocus
)
224 // remember the selection state of the item
225 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
227 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
229 // prevent the tree from unselecting the old focus which it
230 // would do by default (TreeView_SelectItem unselects the
232 TreeView_SelectItem(hwndTV
, 0);
233 SelectItem(hwndTV
, htFocus
);
236 TreeView_SelectItem(hwndTV
, htItem
);
240 // need to clear the selection which TreeView_SelectItem() gave
242 UnselectItem(hwndTV
, htItem
);
244 //else: was selected, still selected - ok
246 //else: nothing to do, focus already there
252 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
254 // just clear the focus
255 TreeView_SelectItem(hwndTV
, 0);
257 if ( wasFocusSelected
)
259 // restore the selection state
260 SelectItem(hwndTV
, htFocus
);
263 //else: nothing to do, no focus already
267 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 // a convenient wrapper around TV_ITEM struct which adds a ctor
275 #pragma warning( disable : 4097 ) // inheriting from typedef
278 struct wxTreeViewItem
: public TV_ITEM
280 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
281 UINT mask_
, // fields which are valid
282 UINT stateMask_
= 0) // for TVIF_STATE only
286 // hItem member is always valid
287 mask
= mask_
| TVIF_HANDLE
;
288 stateMask
= stateMask_
;
293 // wxVirutalNode is used in place of a single root when 'hidden' root is
295 class wxVirtualNode
: public wxTreeViewItem
298 wxVirtualNode(wxTreeItemData
*data
)
299 : wxTreeViewItem(TVI_ROOT
, 0)
309 wxTreeItemData
*GetData() const { return m_data
; }
310 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
313 wxTreeItemData
*m_data
;
315 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
319 #pragma warning( default : 4097 )
322 // a macro to get the virtual root, returns NULL if none
323 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
325 // returns true if the item is the virtual root
326 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
328 // a class which encapsulates the tree traversal logic: it vists all (unless
329 // OnVisit() returns false) items under the given one
330 class wxTreeTraversal
333 wxTreeTraversal(const wxTreeCtrl
*tree
)
338 // do traverse the tree: visit all items (recursively by default) under the
339 // given one; return true if all items were traversed or false if the
340 // traversal was aborted because OnVisit returned false
341 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
343 // override this function to do whatever is needed for each item, return
344 // false to stop traversing
345 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
348 const wxTreeCtrl
*GetTree() const { return m_tree
; }
351 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
353 const wxTreeCtrl
*m_tree
;
355 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
358 // internal class for getting the selected items
359 class TraverseSelections
: public wxTreeTraversal
362 TraverseSelections(const wxTreeCtrl
*tree
,
363 wxArrayTreeItemIds
& selections
)
364 : wxTreeTraversal(tree
), m_selections(selections
)
366 m_selections
.Empty();
368 DoTraverse(tree
->GetRootItem());
371 virtual bool OnVisit(const wxTreeItemId
& item
)
373 // can't visit a virtual node.
374 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
379 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
380 if ( GetTree()->IsItemChecked(item
) )
382 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
385 m_selections
.Add(item
);
391 size_t GetCount() const { return m_selections
.GetCount(); }
394 wxArrayTreeItemIds
& m_selections
;
397 // internal class for counting tree items
398 class TraverseCounter
: public wxTreeTraversal
401 TraverseCounter(const wxTreeCtrl
*tree
,
402 const wxTreeItemId
& root
,
404 : wxTreeTraversal(tree
)
408 DoTraverse(root
, recursively
);
411 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
418 size_t GetCount() const { return m_count
; }
424 // ----------------------------------------------------------------------------
425 // This class is needed for support of different images: the Win32 common
426 // control natively supports only 2 images (the normal one and another for the
427 // selected state). We wish to provide support for 2 more of them for folder
428 // items (i.e. those which have children): for expanded state and for expanded
429 // selected state. For this we use this structure to store the additional items
432 // There is only one problem with this: when we retrieve the item's data, we
433 // don't know whether we get a pointer to wxTreeItemData or
434 // wxTreeItemIndirectData. So we always set the item id to an invalid value
435 // in this class and the code using the client data checks for it and retrieves
436 // the real client data in this case.
437 // ----------------------------------------------------------------------------
439 class wxTreeItemIndirectData
: public wxTreeItemData
442 // ctor associates this data with the item and the real item data becomes
443 // available through our GetData() method
444 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
446 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
452 m_data
= tree
->GetItemData(item
);
454 // and set ourselves as the new one
455 tree
->SetIndirectItemData(item
, this);
457 // we must have the invalid value for the item
461 // dtor deletes the associated data as well
462 virtual ~wxTreeItemIndirectData() { delete m_data
; }
465 // get the real data associated with the item
466 wxTreeItemData
*GetData() const { return m_data
; }
468 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
470 // do we have such image?
471 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
473 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
475 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
478 // all the images associated with the item
479 int m_images
[wxTreeItemIcon_Max
];
481 // the real client data
482 wxTreeItemData
*m_data
;
484 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 // indices in gs_expandEvents table below
512 // handy table for sending events - it has to be initialized during run-time
513 // now so can't be const any more
514 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
517 but logically it's a const table with the following entries:
520 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
521 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
525 // ============================================================================
527 // ============================================================================
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
535 if ( !OnVisit(root
) )
538 return Traverse(root
, recursively
);
541 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
543 wxTreeItemIdValue cookie
;
544 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
545 while ( child
.IsOk() )
547 // depth first traversal
548 if ( recursively
&& !Traverse(child
, true) )
551 if ( !OnVisit(child
) )
554 child
= m_tree
->GetNextChild(root
, cookie
);
560 // ----------------------------------------------------------------------------
561 // construction and destruction
562 // ----------------------------------------------------------------------------
564 void wxTreeCtrl::Init()
566 m_imageListNormal
= NULL
;
567 m_imageListState
= NULL
;
568 m_ownsImageListNormal
= m_ownsImageListState
= false;
570 m_hasAnyAttr
= false;
573 m_pVirtualRoot
= NULL
;
575 // initialize the global array of events now as it can't be done statically
576 // with the wxEVT_XXX values being allocated during run-time only
577 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
578 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
579 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
580 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
583 bool wxTreeCtrl::Create(wxWindow
*parent
,
588 const wxValidator
& validator
,
589 const wxString
& name
)
593 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
594 style
|= wxBORDER_SUNKEN
;
596 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
600 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
601 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
603 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
604 wstyle
|= TVS_HASLINES
;
605 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
606 wstyle
|= TVS_HASBUTTONS
;
608 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
609 wstyle
|= TVS_EDITLABELS
;
611 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
612 wstyle
|= TVS_LINESATROOT
;
614 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
616 if ( wxTheApp
->GetComCtl32Version() >= 471 )
617 wstyle
|= TVS_FULLROWSELECT
;
620 // using TVS_CHECKBOXES for emulation of a multiselection tree control
621 // doesn't work without the new enough headers
622 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
623 !defined( __GNUWIN32_OLD__ ) && \
624 !defined( __BORLANDC__ ) && \
625 !defined( __WATCOMC__ ) && \
626 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
628 // we emulate the multiple selection tree controls by using checkboxes: set
629 // up the image list we need for this if we do have multiple selections
630 if ( m_windowStyle
& wxTR_MULTIPLE
)
631 wstyle
|= TVS_CHECKBOXES
;
632 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
634 // Create the tree control.
635 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
638 #if wxUSE_COMCTL32_SAFELY
639 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
640 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
642 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
643 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
645 // This works around a bug in the Windows tree control whereby for some versions
646 // of comctrl32, setting any colour actually draws the background in black.
647 // This will initialise the background to the system colour.
648 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
649 // Assume the user has an updated comctl32.dll.
650 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
651 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
652 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
656 // VZ: this is some experimental code which may be used to get the
657 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
658 // AFAIK, the standard DLL does about the same thing anyhow.
660 if ( m_windowStyle
& wxTR_MULTIPLE
)
664 // create the DC compatible with the current screen
665 HDC hdcMem
= CreateCompatibleDC(NULL
);
667 // create a mono bitmap of the standard size
668 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
669 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
670 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
671 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
672 1, // # of color planes
673 1, // # bits needed for one pixel
674 0); // array containing colour data
675 SelectObject(hdcMem
, hbmpCheck
);
677 // then draw a check mark into it
678 RECT rect
= { 0, 0, x
, y
};
679 if ( !::DrawFrameControl(hdcMem
, &rect
,
681 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
683 wxLogLastError(wxT("DrawFrameControl(check)"));
686 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
687 imagelistCheckboxes
.Add(bmp
);
689 if ( !::DrawFrameControl(hdcMem
, &rect
,
693 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
696 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
697 imagelistCheckboxes
.Add(bmp
);
703 SetStateImageList(&imagelistCheckboxes
);
707 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
712 wxTreeCtrl::~wxTreeCtrl()
714 // delete any attributes
717 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
719 // prevent TVN_DELETEITEM handler from deleting the attributes again!
720 m_hasAnyAttr
= false;
725 // delete user data to prevent memory leaks
726 // also deletes hidden root node storage.
729 if (m_ownsImageListNormal
) delete m_imageListNormal
;
730 if (m_ownsImageListState
) delete m_imageListState
;
733 // ----------------------------------------------------------------------------
735 // ----------------------------------------------------------------------------
737 // simple wrappers which add error checking in debug mode
739 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
741 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
742 _T("can't retrieve virtual root item") );
744 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
746 wxLogLastError(wxT("TreeView_GetItem"));
754 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
756 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
758 wxLogLastError(wxT("TreeView_SetItem"));
762 size_t wxTreeCtrl::GetCount() const
764 return (size_t)TreeView_GetCount(GetHwnd());
767 unsigned int wxTreeCtrl::GetIndent() const
769 return TreeView_GetIndent(GetHwnd());
772 void wxTreeCtrl::SetIndent(unsigned int indent
)
774 TreeView_SetIndent(GetHwnd(), indent
);
777 wxImageList
*wxTreeCtrl::GetImageList() const
779 return m_imageListNormal
;
782 wxImageList
*wxTreeCtrl::GetStateImageList() const
784 return m_imageListState
;
787 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
790 TreeView_SetImageList(GetHwnd(),
791 imageList
? imageList
->GetHIMAGELIST() : 0,
795 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
797 if (m_ownsImageListNormal
)
798 delete m_imageListNormal
;
800 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
801 m_ownsImageListNormal
= false;
804 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
806 if (m_ownsImageListState
) delete m_imageListState
;
807 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
808 m_ownsImageListState
= false;
811 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
813 SetImageList(imageList
);
814 m_ownsImageListNormal
= true;
817 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
819 SetStateImageList(imageList
);
820 m_ownsImageListState
= true;
823 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
824 bool recursively
) const
826 TraverseCounter
counter(this, item
, recursively
);
828 return counter
.GetCount() - 1;
831 // ----------------------------------------------------------------------------
833 // ----------------------------------------------------------------------------
835 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
837 #if !wxUSE_COMCTL32_SAFELY
838 if ( !wxWindowBase::SetBackgroundColour(colour
) )
841 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
847 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
849 #if !wxUSE_COMCTL32_SAFELY
850 if ( !wxWindowBase::SetForegroundColour(colour
) )
853 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
859 // ----------------------------------------------------------------------------
861 // ----------------------------------------------------------------------------
863 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
865 wxChar buf
[512]; // the size is arbitrary...
867 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
868 tvItem
.pszText
= buf
;
869 tvItem
.cchTextMax
= WXSIZEOF(buf
);
870 if ( !DoGetItem(&tvItem
) )
872 // don't return some garbage which was on stack, but an empty string
876 return wxString(buf
);
879 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
881 if ( IS_VIRTUAL_ROOT(item
) )
884 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
885 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
888 // when setting the text of the item being edited, the text control should
889 // be updated to reflect the new text as well, otherwise calling
890 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
892 // don't use GetEditControl() here because m_textCtrl is not set yet
893 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
896 if ( item
== GetSelection() )
898 ::SetWindowText(hwndEdit
, text
);
903 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
904 wxTreeItemIcon which
) const
906 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
907 if ( !DoGetItem(&tvItem
) )
912 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
915 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
917 wxTreeItemIcon which
) const
919 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
920 if ( !DoGetItem(&tvItem
) )
925 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
927 data
->SetImage(image
, which
);
929 // make sure that we have selected images as well
930 if ( which
== wxTreeItemIcon_Normal
&&
931 !data
->HasImage(wxTreeItemIcon_Selected
) )
933 data
->SetImage(image
, wxTreeItemIcon_Selected
);
936 if ( which
== wxTreeItemIcon_Expanded
&&
937 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
939 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
943 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
947 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
948 tvItem
.iSelectedImage
= imageSel
;
949 tvItem
.iImage
= image
;
953 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
954 wxTreeItemIcon which
) const
956 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
958 // TODO: Maybe a hidden root can still provide images?
962 if ( HasIndirectData(item
) )
964 return DoGetItemImageFromData(item
, which
);
971 wxFAIL_MSG( wxT("unknown tree item image type") );
973 case wxTreeItemIcon_Normal
:
977 case wxTreeItemIcon_Selected
:
978 mask
= TVIF_SELECTEDIMAGE
;
981 case wxTreeItemIcon_Expanded
:
982 case wxTreeItemIcon_SelectedExpanded
:
986 wxTreeViewItem
tvItem(item
, mask
);
989 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
992 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
993 wxTreeItemIcon which
)
995 if ( IS_VIRTUAL_ROOT(item
) )
997 // TODO: Maybe a hidden root can still store images?
1007 wxFAIL_MSG( wxT("unknown tree item image type") );
1010 case wxTreeItemIcon_Normal
:
1012 const int imageNormalOld
= GetItemImage(item
);
1013 const int imageSelOld
= GetItemSelectedImage(item
);
1015 // always set the normal image
1016 imageNormal
= image
;
1018 // if the selected and normal images were the same, they should
1019 // be the same after the update, otherwise leave the selected
1021 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1025 case wxTreeItemIcon_Selected
:
1026 imageNormal
= GetItemImage(item
);
1030 case wxTreeItemIcon_Expanded
:
1031 case wxTreeItemIcon_SelectedExpanded
:
1032 if ( !HasIndirectData(item
) )
1034 // we need to get the old images first, because after we create
1035 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1037 imageNormal
= GetItemImage(item
);
1038 imageSel
= GetItemSelectedImage(item
);
1040 // if it doesn't have it yet, add it
1041 wxTreeItemIndirectData
*data
= new
1042 wxTreeItemIndirectData(this, item
);
1044 // copy the data to the new location
1045 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1046 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1049 DoSetItemImageFromData(item
, image
, which
);
1051 // reset the normal/selected images because we won't use them any
1052 // more - now they're stored inside the indirect data
1054 imageSel
= I_IMAGECALLBACK
;
1058 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1059 // change both normal and selected image - otherwise the change simply
1060 // doesn't take place!
1061 DoSetItemImages(item
, imageNormal
, imageSel
);
1064 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1066 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1068 // Hidden root may have data.
1069 if ( IS_VIRTUAL_ROOT(item
) )
1071 return GET_VIRTUAL_ROOT()->GetData();
1075 if ( !DoGetItem(&tvItem
) )
1080 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1081 if ( IsDataIndirect(data
) )
1083 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1089 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1091 if ( IS_VIRTUAL_ROOT(item
) )
1093 GET_VIRTUAL_ROOT()->SetData(data
);
1096 // first, associate this piece of data with this item
1102 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1104 if ( HasIndirectData(item
) )
1106 if ( DoGetItem(&tvItem
) )
1108 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1112 wxFAIL_MSG( wxT("failed to change tree items data") );
1117 tvItem
.lParam
= (LPARAM
)data
;
1122 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1123 wxTreeItemIndirectData
*data
)
1125 // this should never happen because it's unnecessary and will probably lead
1126 // to crash too because the code elsewhere supposes that the pointer the
1127 // wxTreeItemIndirectData has is a real wxItemData and not
1128 // wxTreeItemIndirectData as well
1129 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1131 SetItemData(item
, data
);
1134 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1136 // query the item itself
1137 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1138 if ( !DoGetItem(&tvItem
) )
1143 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1145 return data
&& IsDataIndirect(data
);
1148 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1150 if ( IS_VIRTUAL_ROOT(item
) )
1153 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1154 tvItem
.cChildren
= (int)has
;
1158 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1160 if ( IS_VIRTUAL_ROOT(item
) )
1163 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1164 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1168 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1170 if ( IS_VIRTUAL_ROOT(item
) )
1173 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1174 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1178 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1180 if ( IS_VIRTUAL_ROOT(item
) )
1184 if ( GetBoundingRect(item
, rect
) )
1190 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1192 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
);
1194 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1197 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1199 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
);
1201 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1204 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1206 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
);
1208 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1211 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1212 const wxColour
& col
)
1214 wxTreeItemAttr
*attr
;
1215 wxMapTreeAttr::iterator it
= m_attrs
.find(item
);
1216 if ( it
== m_attrs
.end() )
1218 m_hasAnyAttr
= true;
1221 attr
= new wxTreeItemAttr
;
1228 attr
->SetTextColour(col
);
1233 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1234 const wxColour
& col
)
1236 wxTreeItemAttr
*attr
;
1237 wxMapTreeAttr::iterator it
= m_attrs
.find(item
);
1238 if ( it
== m_attrs
.end() )
1240 m_hasAnyAttr
= true;
1243 attr
= new wxTreeItemAttr
;
1245 else // already in the hash
1250 attr
->SetBackgroundColour(col
);
1255 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1257 wxTreeItemAttr
*attr
;
1258 wxMapTreeAttr::iterator it
= m_attrs
.find(item
);
1259 if ( it
== m_attrs
.end() )
1261 m_hasAnyAttr
= true;
1264 attr
= new wxTreeItemAttr
;
1266 else // already in the hash
1271 attr
->SetFont(font
);
1276 // ----------------------------------------------------------------------------
1278 // ----------------------------------------------------------------------------
1280 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1282 if ( item
== wxTreeItemId(TVI_ROOT
) )
1284 // virtual (hidden) root is never visible
1288 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1291 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1292 // the HTREEITEM with TVM_GETITEMRECT
1293 *(HTREEITEM
*)&rect
= HITEM(item
);
1295 // false means get item rect for the whole item, not only text
1296 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1299 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1301 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1304 return tvItem
.cChildren
!= 0;
1307 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1309 // probably not a good idea to put it here
1310 //wxASSERT( ItemHasChildren(item) );
1312 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1315 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1318 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1320 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1323 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1326 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1328 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1331 return (tvItem
.state
& TVIS_BOLD
) != 0;
1334 // ----------------------------------------------------------------------------
1336 // ----------------------------------------------------------------------------
1338 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1340 // Root may be real (visible) or virtual (hidden).
1341 if ( GET_VIRTUAL_ROOT() )
1344 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1347 wxTreeItemId
wxTreeCtrl::GetSelection() const
1349 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), NULL
,
1350 wxT("this only works with single selection controls") );
1352 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1355 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1359 if ( IS_VIRTUAL_ROOT(item
) )
1361 // no parent for the virtual root
1366 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1367 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1369 // the top level items should have the virtual root as their parent
1374 return wxTreeItemId(hItem
);
1377 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1378 wxTreeItemIdValue
& cookie
) const
1380 // remember the last child returned in 'cookie'
1381 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1383 return wxTreeItemId(cookie
);
1386 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1387 wxTreeItemIdValue
& cookie
) const
1389 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1390 HITEM(wxTreeItemId(cookie
))));
1391 cookie
= item
.m_pItem
;
1396 #if WXWIN_COMPATIBILITY_2_4
1398 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1401 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1403 return wxTreeItemId((void *)cookie
);
1406 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1409 wxTreeItemId
item(TreeView_GetNextSibling
1412 HITEM(wxTreeItemId((void *)cookie
)
1414 cookie
= (long)item
.m_pItem
;
1419 #endif // WXWIN_COMPATIBILITY_2_4
1421 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1423 // can this be done more efficiently?
1424 wxTreeItemIdValue cookie
;
1426 wxTreeItemId childLast
,
1427 child
= GetFirstChild(item
, cookie
);
1428 while ( child
.IsOk() )
1431 child
= GetNextChild(item
, cookie
);
1437 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1439 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1442 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1444 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1447 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1449 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1452 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1454 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1456 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1459 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1461 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1463 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1466 // ----------------------------------------------------------------------------
1467 // multiple selections emulation
1468 // ----------------------------------------------------------------------------
1470 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1472 // receive the desired information.
1473 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1476 // state image indices are 1 based
1477 return ((tvItem
.state
>> 12) - 1) == 1;
1480 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1482 // receive the desired information.
1483 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1487 // state images are one-based
1488 tvItem
.state
= (check
? 2 : 1) << 12;
1493 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1495 TraverseSelections
selector(this, selections
);
1497 return selector
.GetCount();
1500 // ----------------------------------------------------------------------------
1502 // ----------------------------------------------------------------------------
1504 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1505 wxTreeItemId hInsertAfter
,
1506 const wxString
& text
,
1507 int image
, int selectedImage
,
1508 wxTreeItemData
*data
)
1510 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1512 _T("can't have more than one root in the tree") );
1514 TV_INSERTSTRUCT tvIns
;
1515 tvIns
.hParent
= HITEM(parent
);
1516 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1518 // this is how we insert the item as the first child: supply a NULL
1520 if ( !tvIns
.hInsertAfter
)
1522 tvIns
.hInsertAfter
= TVI_FIRST
;
1526 if ( !text
.IsEmpty() )
1529 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1533 tvIns
.item
.pszText
= NULL
;
1534 tvIns
.item
.cchTextMax
= 0;
1540 tvIns
.item
.iImage
= image
;
1542 if ( selectedImage
== -1 )
1544 // take the same image for selected icon if not specified
1545 selectedImage
= image
;
1549 if ( selectedImage
!= -1 )
1551 mask
|= TVIF_SELECTEDIMAGE
;
1552 tvIns
.item
.iSelectedImage
= selectedImage
;
1558 tvIns
.item
.lParam
= (LPARAM
)data
;
1561 tvIns
.item
.mask
= mask
;
1563 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1566 wxLogLastError(wxT("TreeView_InsertItem"));
1571 // associate the application tree item with Win32 tree item handle
1575 return wxTreeItemId(id
);
1578 // for compatibility only
1579 #if WXWIN_COMPATIBILITY_2_4
1581 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1582 const wxString
& text
,
1583 int image
, int selImage
,
1586 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1587 image
, selImage
, NULL
);
1590 #endif // WXWIN_COMPATIBILITY_2_4
1592 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1593 int image
, int selectedImage
,
1594 wxTreeItemData
*data
)
1597 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1599 // create a virtual root item, the parent for all the others
1600 m_pVirtualRoot
= new wxVirtualNode(data
);
1605 return DoInsertItem(wxTreeItemId(), 0,
1606 text
, image
, selectedImage
, data
);
1609 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1610 const wxString
& text
,
1611 int image
, int selectedImage
,
1612 wxTreeItemData
*data
)
1614 return DoInsertItem(parent
, TVI_FIRST
,
1615 text
, image
, selectedImage
, data
);
1618 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1619 const wxTreeItemId
& idPrevious
,
1620 const wxString
& text
,
1621 int image
, int selectedImage
,
1622 wxTreeItemData
*data
)
1624 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1627 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1629 const wxString
& text
,
1630 int image
, int selectedImage
,
1631 wxTreeItemData
*data
)
1633 // find the item from index
1634 wxTreeItemIdValue cookie
;
1635 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1636 while ( index
!= 0 && idCur
.IsOk() )
1641 idCur
= GetNextChild(parent
, cookie
);
1644 // assert, not check: if the index is invalid, we will append the item
1646 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1648 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1651 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1652 const wxString
& text
,
1653 int image
, int selectedImage
,
1654 wxTreeItemData
*data
)
1656 return DoInsertItem(parent
, TVI_LAST
,
1657 text
, image
, selectedImage
, data
);
1660 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1662 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1664 wxLogLastError(wxT("TreeView_DeleteItem"));
1668 // delete all children (but don't delete the item itself)
1669 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1671 wxTreeItemIdValue cookie
;
1673 wxArrayTreeItemIds children
;
1674 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1675 while ( child
.IsOk() )
1677 children
.Add(child
);
1679 child
= GetNextChild(item
, cookie
);
1682 size_t nCount
= children
.Count();
1683 for ( size_t n
= 0; n
< nCount
; n
++ )
1685 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1687 wxLogLastError(wxT("TreeView_DeleteItem"));
1692 void wxTreeCtrl::DeleteAllItems()
1694 // delete the "virtual" root item.
1695 if ( GET_VIRTUAL_ROOT() )
1697 delete GET_VIRTUAL_ROOT();
1698 m_pVirtualRoot
= NULL
;
1701 // and all the real items
1703 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1705 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1709 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1711 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1712 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1713 flag
== TVE_EXPAND
||
1715 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1717 // A hidden root can be neither expanded nor collapsed.
1718 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1719 wxT("Can't expand/collapse hidden root node!") )
1721 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1722 // emulate them. This behaviour has changed slightly with comctl32.dll
1723 // v 4.70 - now it does send them but only the first time. To maintain
1724 // compatible behaviour and also in order to not have surprises with the
1725 // future versions, don't rely on this and still do everything ourselves.
1726 // To avoid that the messages be sent twice when the item is expanded for
1727 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1729 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1733 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1735 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1736 event
.m_item
= item
;
1737 event
.SetEventObject(this);
1739 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1741 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1745 (void)GetEventHandler()->ProcessEvent(event
);
1747 //else: change didn't took place, so do nothing at all
1750 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1752 DoExpand(item
, TVE_EXPAND
);
1755 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1757 DoExpand(item
, TVE_COLLAPSE
);
1760 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1762 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1765 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1767 DoExpand(item
, TVE_TOGGLE
);
1770 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1772 DoExpand(item
, action
);
1775 void wxTreeCtrl::Unselect()
1777 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1778 wxT("doesn't make sense, may be you want UnselectAll()?") );
1780 // just remove the selection
1781 SelectItem(wxTreeItemId());
1784 void wxTreeCtrl::UnselectAll()
1786 if ( m_windowStyle
& wxTR_MULTIPLE
)
1788 wxArrayTreeItemIds selections
;
1789 size_t count
= GetSelections(selections
);
1790 for ( size_t n
= 0; n
< count
; n
++ )
1792 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1793 SetItemCheck(selections
[n
], false);
1794 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1795 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1796 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1801 // just remove the selection
1806 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1808 if ( m_windowStyle
& wxTR_MULTIPLE
)
1810 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1811 // selecting the item means checking it
1813 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1814 ::SelectItem(GetHwnd(), HITEM(item
));
1815 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1819 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1820 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1821 // send them ourselves
1823 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1824 event
.m_item
= item
;
1825 event
.SetEventObject(this);
1827 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1828 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1830 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1832 wxLogLastError(wxT("TreeView_SelectItem"));
1836 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1837 (void)GetEventHandler()->ProcessEvent(event
);
1840 //else: program vetoed the change
1844 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1847 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1850 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1852 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1854 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1858 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1863 void wxTreeCtrl::DeleteTextCtrl()
1867 // the HWND corresponding to this control is deleted by the tree
1868 // control itself and we don't know when exactly this happens, so check
1869 // if the window still exists before calling UnsubclassWin()
1870 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1872 m_textCtrl
->SetHWND(0);
1875 m_textCtrl
->UnsubclassWin();
1876 m_textCtrl
->SetHWND(0);
1882 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1883 wxClassInfo
* textControlClass
)
1885 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1889 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1890 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1892 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1901 // textctrl is subclassed in MSWOnNotify
1905 // End label editing, optionally cancelling the edit
1906 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1908 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1913 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1915 TV_HITTESTINFO hitTestInfo
;
1916 hitTestInfo
.pt
.x
= (int)point
.x
;
1917 hitTestInfo
.pt
.y
= (int)point
.y
;
1919 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1924 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1925 flags |= wxTREE_HITTEST_##flag
1927 TRANSLATE_FLAG(ABOVE
);
1928 TRANSLATE_FLAG(BELOW
);
1929 TRANSLATE_FLAG(NOWHERE
);
1930 TRANSLATE_FLAG(ONITEMBUTTON
);
1931 TRANSLATE_FLAG(ONITEMICON
);
1932 TRANSLATE_FLAG(ONITEMINDENT
);
1933 TRANSLATE_FLAG(ONITEMLABEL
);
1934 TRANSLATE_FLAG(ONITEMRIGHT
);
1935 TRANSLATE_FLAG(ONITEMSTATEICON
);
1936 TRANSLATE_FLAG(TOLEFT
);
1937 TRANSLATE_FLAG(TORIGHT
);
1939 #undef TRANSLATE_FLAG
1941 return wxTreeItemId(hitTestInfo
.hItem
);
1944 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1946 bool textOnly
) const
1950 // Virtual root items have no bounding rectangle
1951 if ( IS_VIRTUAL_ROOT(item
) )
1956 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1959 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1965 // couldn't retrieve rect: for example, item isn't visible
1970 // ----------------------------------------------------------------------------
1972 // ----------------------------------------------------------------------------
1974 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1975 // functions such as IsDataIndirect()
1976 class wxTreeSortHelper
1979 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1982 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1984 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1985 if ( tree
->IsDataIndirect(data
) )
1987 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1990 return data
->GetId();
1994 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1998 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1999 wxT("sorting tree without data doesn't make sense") );
2001 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2003 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2004 GetIdFromData(tree
, pItem2
));
2007 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2008 const wxTreeItemId
& item2
)
2010 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2013 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2015 // rely on the fact that TreeView_SortChildren does the same thing as our
2016 // default behaviour, i.e. sorts items alphabetically and so call it
2017 // directly if we're not in derived class (much more efficient!)
2018 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2020 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2025 tvSort
.hParent
= HITEM(item
);
2026 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2027 tvSort
.lParam
= (LPARAM
)this;
2028 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2032 // ----------------------------------------------------------------------------
2034 // ----------------------------------------------------------------------------
2036 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2038 if ( cmd
== EN_UPDATE
)
2040 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2041 event
.SetEventObject( this );
2042 ProcessCommand(event
);
2044 else if ( cmd
== EN_KILLFOCUS
)
2046 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2047 event
.SetEventObject( this );
2048 ProcessCommand(event
);
2056 // command processed
2060 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2061 // only do it during dragging, minimize wxWin overhead (this is important for
2062 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2063 // instead of passing by wxWin events
2064 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2066 bool processed
= false;
2068 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2070 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2072 // we only process mouse messages here and these parameters have the
2073 // same meaning for all of them
2074 int x
= GET_X_LPARAM(lParam
),
2075 y
= GET_Y_LPARAM(lParam
);
2076 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2080 case WM_RBUTTONDOWN
:
2081 // if the item we are about to right click on
2082 // is not already select, remove the entire
2083 // previous selection
2084 if (!::IsItemSelected(GetHwnd(), htItem
))
2089 // select item and set the focus to the
2090 // newly selected item
2091 ::SelectItem(GetHwnd(), htItem
);
2092 ::SetFocus(GetHwnd(), htItem
);
2095 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2096 case WM_LBUTTONDOWN
:
2097 if ( htItem
&& isMultiple
)
2099 if ( wParam
& MK_CONTROL
)
2103 // toggle selected state
2104 ToggleItemSelection(GetHwnd(), htItem
);
2106 ::SetFocus(GetHwnd(), htItem
);
2108 // reset on any click without Shift
2113 else if ( wParam
& MK_SHIFT
)
2115 // this selects all items between the starting one and
2118 if ( !m_htSelStart
)
2120 // take the focused item
2121 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2124 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2125 !(wParam
& MK_CONTROL
));
2127 ::SetFocus(GetHwnd(), htItem
);
2131 else // normal click
2133 // avoid doing anything if we click on the only
2134 // currently selected item
2136 wxArrayTreeItemIds selections
;
2137 size_t count
= GetSelections(selections
);
2140 HITEM(selections
[0]) != htItem
)
2142 // clear the previously selected items, if the
2143 // user clicked outside of the present selection.
2144 // otherwise, perform the deselection on mouse-up.
2145 // this allows multiple drag and drop to work.
2147 if (IsItemSelected(GetHwnd(), htItem
))
2149 ::SetFocus(GetHwnd(), htItem
);
2155 // prevent the click from starting in-place editing
2156 // which should only happen if we click on the
2157 // already selected item (and nothing else is
2160 TreeView_SelectItem(GetHwnd(), 0);
2161 ::SelectItem(GetHwnd(), htItem
);
2165 // reset on any click without Shift
2170 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2175 m_dragImage
->Move(wxPoint(x
, y
));
2178 // highlight the item as target (hiding drag image is
2179 // necessary - otherwise the display will be corrupted)
2180 m_dragImage
->Hide();
2181 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2182 m_dragImage
->Show();
2189 // facilitates multiple drag-and-drop
2190 if (htItem
&& isMultiple
)
2192 wxArrayTreeItemIds selections
;
2193 size_t count
= GetSelections(selections
);
2196 !(wParam
& MK_CONTROL
) &&
2197 !(wParam
& MK_SHIFT
))
2200 TreeView_SelectItem(GetHwnd(), htItem
);
2209 m_dragImage
->EndDrag();
2213 // generate the drag end event
2214 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2216 event
.m_item
= htItem
;
2217 event
.m_pointDrag
= wxPoint(x
, y
);
2218 event
.SetEventObject(this);
2220 (void)GetEventHandler()->ProcessEvent(event
);
2222 // if we don't do it, the tree seems to think that 2 items
2223 // are selected simultaneously which is quite weird
2224 TreeView_SelectDropTarget(GetHwnd(), 0);
2229 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2230 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2232 // the tree control greys out the selected item when it loses focus and
2233 // paints it as selected again when it regains it, but it won't do it
2234 // for the other items itself - help it
2235 wxArrayTreeItemIds selections
;
2236 size_t count
= GetSelections(selections
);
2238 for ( size_t n
= 0; n
< count
; n
++ )
2240 // TreeView_GetItemRect() will return false if item is not visible,
2241 // which may happen perfectly well
2242 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2245 ::InvalidateRect(GetHwnd(), &rect
, false);
2249 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2251 bool bCtrl
= wxIsCtrlDown(),
2252 bShift
= wxIsShiftDown();
2254 // we handle.arrows and space, but not page up/down and home/end: the
2255 // latter should be easy, but not the former
2257 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2258 if ( !m_htSelStart
)
2260 m_htSelStart
= htSel
;
2263 if ( wParam
== VK_SPACE
)
2267 ToggleItemSelection(GetHwnd(), htSel
);
2273 ::SelectItem(GetHwnd(), htSel
);
2278 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2280 if ( !bCtrl
&& !bShift
)
2282 // no modifiers, just clear selection and then let the default
2283 // processing to take place
2288 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2290 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2291 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2292 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2296 // at the top/bottom
2302 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2306 // without changing selection
2307 ::SetFocus(GetHwnd(), htNext
);
2314 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2315 else if ( nMsg
== WM_CHAR
)
2317 // don't let the control process Space and Return keys because it
2318 // doesn't do anything useful with them anyhow but always beeps
2319 // annoyingly when it receives them and there is no way to turn it off
2320 // simply if you just process TREEITEM_ACTIVATED event to which Space
2321 // and Enter presses are mapped in your code
2322 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2329 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2334 // process WM_NOTIFY Windows message
2335 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2337 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2338 wxEventType eventType
= wxEVT_NULL
;
2339 NMHDR
*hdr
= (NMHDR
*)lParam
;
2341 switch ( hdr
->code
)
2344 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2347 case TVN_BEGINRDRAG
:
2349 if ( eventType
== wxEVT_NULL
)
2350 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2351 //else: left drag, already set above
2353 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2355 event
.m_item
= tv
->itemNew
.hItem
;
2356 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2358 // don't allow dragging by default: the user code must
2359 // explicitly say that it wants to allow it to avoid breaking
2365 case TVN_BEGINLABELEDIT
:
2367 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2368 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2370 event
.m_item
= info
->item
.hItem
;
2371 event
.m_label
= info
->item
.pszText
;
2372 event
.m_editCancelled
= false;
2376 case TVN_DELETEITEM
:
2378 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2379 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2381 event
.m_item
= tv
->itemOld
.hItem
;
2385 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2386 if ( it
!= m_attrs
.end() )
2395 case TVN_ENDLABELEDIT
:
2397 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2398 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2400 event
.m_item
= info
->item
.hItem
;
2401 event
.m_label
= info
->item
.pszText
;
2402 if (info
->item
.pszText
== NULL
)
2404 event
.m_editCancelled
= true;
2408 event
.m_editCancelled
= false;
2413 case TVN_GETDISPINFO
:
2414 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2417 case TVN_SETDISPINFO
:
2419 if ( eventType
== wxEVT_NULL
)
2420 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2421 //else: get, already set above
2423 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2425 event
.m_item
= info
->item
.hItem
;
2429 case TVN_ITEMEXPANDING
:
2430 case TVN_ITEMEXPANDED
:
2432 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2435 switch ( tv
->action
)
2438 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2446 what
= IDX_COLLAPSE
;
2450 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2453 eventType
= gs_expandEvents
[what
][how
];
2455 event
.m_item
= tv
->itemNew
.hItem
;
2461 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2462 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2464 // fabricate the lParam and wParam parameters sufficiently
2465 // similar to the ones from a "real" WM_KEYDOWN so that
2466 // CreateKeyEvent() works correctly
2468 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2470 WXWPARAM wParam
= info
->wVKey
;
2472 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2475 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2480 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2485 // a separate event for Space/Return
2486 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2487 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2489 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2491 event2
.SetEventObject(this);
2492 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2494 event2
.m_item
= GetSelection();
2496 //else: don't know how to get it
2498 (void)GetEventHandler()->ProcessEvent(event2
);
2503 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2504 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2505 // we have to handle both messages:
2506 case TVN_SELCHANGEDA
:
2507 case TVN_SELCHANGEDW
:
2508 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2511 case TVN_SELCHANGINGA
:
2512 case TVN_SELCHANGINGW
:
2514 if ( eventType
== wxEVT_NULL
)
2515 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2516 //else: already set above
2518 if (hdr
->code
== TVN_SELCHANGINGW
||
2519 hdr
->code
== TVN_SELCHANGEDW
)
2521 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2522 event
.m_item
= tv
->itemNew
.hItem
;
2523 event
.m_itemOld
= tv
->itemOld
.hItem
;
2527 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2528 event
.m_item
= tv
->itemNew
.hItem
;
2529 event
.m_itemOld
= tv
->itemOld
.hItem
;
2534 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2537 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2538 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2539 switch ( nmcd
.dwDrawStage
)
2542 // if we've got any items with non standard attributes,
2543 // notify us before painting each item
2544 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2548 case CDDS_ITEMPREPAINT
:
2550 wxMapTreeAttr::iterator
2551 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2553 if ( it
== m_attrs
.end() )
2555 // nothing to do for this item
2556 *result
= CDRF_DODEFAULT
;
2560 wxTreeItemAttr
* const attr
= it
->second
;
2563 if ( attr
->HasFont() )
2565 hFont
= GetHfontOf(attr
->GetFont());
2573 if ( attr
->HasTextColour() )
2575 colText
= attr
->GetTextColour();
2579 colText
= GetForegroundColour();
2582 // selection colours should override ours
2583 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2586 ::GetSysColor(COLOR_HIGHLIGHT
);
2588 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2593 if ( attr
->HasBackgroundColour() )
2595 colBack
= attr
->GetBackgroundColour();
2599 colBack
= GetBackgroundColour();
2602 lptvcd
->clrText
= wxColourToRGB(colText
);
2603 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2606 // note that if we wanted to set colours for
2607 // individual columns (subitems), we would have
2608 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2611 ::SelectObject(nmcd
.hdc
, hFont
);
2613 *result
= CDRF_NEWFONT
;
2617 *result
= CDRF_DODEFAULT
;
2623 *result
= CDRF_DODEFAULT
;
2627 // we always process it
2629 #endif // _WIN32_IE >= 0x300
2633 DWORD pos
= GetMessagePos();
2635 point
.x
= LOWORD(pos
);
2636 point
.y
= HIWORD(pos
);
2637 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2639 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2640 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2642 event
.m_item
= item
;
2643 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2651 TV_HITTESTINFO tvhti
;
2652 ::GetCursorPos(&tvhti
.pt
);
2653 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2654 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2656 if ( tvhti
.flags
& TVHT_ONITEM
)
2658 event
.m_item
= tvhti
.hItem
;
2659 eventType
= (int)hdr
->code
== NM_DBLCLK
2660 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2661 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2663 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2664 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2673 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2676 event
.SetEventObject(this);
2677 event
.SetEventType(eventType
);
2679 bool processed
= GetEventHandler()->ProcessEvent(event
);
2682 switch ( hdr
->code
)
2685 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2686 // the return code of this event handler as the return value for
2687 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2688 // expanded status would never work
2693 case TVN_BEGINRDRAG
:
2694 if ( event
.IsAllowed() )
2696 // normally this is impossible because the m_dragImage is
2697 // deleted once the drag operation is over
2698 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2700 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2701 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2702 m_dragImage
->Show();
2706 case TVN_DELETEITEM
:
2708 // NB: we might process this message using wxWindows event
2709 // tables, but due to overhead of wxWin event system we
2710 // prefer to do it here ourself (otherwise deleting a tree
2711 // with many items is just too slow)
2712 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2714 wxTreeItemId item
= event
.m_item
;
2715 if ( HasIndirectData(item
) )
2717 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2719 delete data
; // can't be NULL here
2723 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2724 delete data
; // may be NULL, ok
2727 processed
= true; // Make sure we don't get called twice
2731 case TVN_BEGINLABELEDIT
:
2732 // return true to cancel label editing
2733 *result
= !event
.IsAllowed();
2734 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2735 if(event
.IsAllowed())
2737 HWND hText
= TreeView_GetEditControl(GetHwnd());
2740 // MBN: if m_textCtrl already has an HWND, it is a stale
2741 // pointer from a previous edit (because the user
2742 // didn't modify the label before dismissing the control,
2743 // and TVN_ENDLABELEDIT was not sent), so delete it
2744 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2747 m_textCtrl
= new wxTextCtrl();
2748 m_textCtrl
->SetParent(this);
2749 m_textCtrl
->SetHWND((WXHWND
)hText
);
2750 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2752 // set wxTE_PROCESS_ENTER style for the text control to
2753 // force it to process the Enter presses itself, otherwise
2754 // they could be stolen from it by the dialog
2756 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2757 | wxTE_PROCESS_ENTER
);
2762 case TVN_ENDLABELEDIT
:
2763 // return true to set the label to the new string: note that we
2764 // also must pretend that we did process the message or it is going
2765 // to be passed to DefWindowProc() which will happily return false
2766 // cancelling the label change
2767 *result
= event
.IsAllowed();
2770 // ensure that we don't have the text ctrl which is going to be
2775 case TVN_SELCHANGING
:
2776 case TVN_ITEMEXPANDING
:
2777 // return true to prevent the action from happening
2778 *result
= !event
.IsAllowed();
2781 case TVN_ITEMEXPANDED
:
2782 // the item is not refreshed properly after expansion when it has
2783 // an image depending on the expanded/collapsed state - bug in
2784 // comctl32.dll or our code?
2786 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2787 wxTreeItemId
id(tv
->itemNew
.hItem
);
2789 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2797 case TVN_GETDISPINFO
:
2798 // NB: so far the user can't set the image himself anyhow, so do it
2799 // anyway - but this may change later
2800 //if ( /* !processed && */ 1 )
2802 wxTreeItemId item
= event
.m_item
;
2803 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2804 if ( info
->item
.mask
& TVIF_IMAGE
)
2807 DoGetItemImageFromData
2810 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2811 : wxTreeItemIcon_Normal
2814 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2816 info
->item
.iSelectedImage
=
2817 DoGetItemImageFromData
2820 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2821 : wxTreeItemIcon_Selected
2828 // for the other messages the return value is ignored and there is
2829 // nothing special to do
2834 // ----------------------------------------------------------------------------
2836 // ----------------------------------------------------------------------------
2838 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2839 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2841 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2844 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2845 tvi
.mask
= TVIF_STATE
;
2846 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2848 // Select the specified state, or -1 == cycle to the next one.
2851 TreeView_GetItem(GetHwnd(), &tvi
);
2853 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2854 if ( state
== m_imageListState
->GetImageCount() )
2858 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2859 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2861 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2863 TreeView_SetItem(GetHwnd(), &tvi
);
2866 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2869 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2870 tvi
.mask
= TVIF_STATE
;
2871 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2872 TreeView_GetItem(GetHwnd(), &tvi
);
2874 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2877 #endif // wxUSE_TREECTRL