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 // macros to hide the cast ugliness
79 // --------------------------------
81 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
82 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
84 // item here is a wxTreeItemId
85 #define HITEM(item) HITEM_PTR((item).m_pItem)
87 // the native control doesn't support multiple selections under MSW and we
88 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
89 // checkboxes be the selection status (checked == selected) or by really
90 // emulating everything, i.e. intercepting mouse and key events &c. The first
91 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
93 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
95 // ----------------------------------------------------------------------------
97 // ----------------------------------------------------------------------------
99 // wrapper for TreeView_HitTest
100 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
106 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
109 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
111 // wrappers for TreeView_GetItem/TreeView_SetItem
112 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
116 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
117 tvi
.stateMask
= TVIS_SELECTED
;
120 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
122 wxLogLastError(wxT("TreeView_GetItem"));
125 return (tvi
.state
& TVIS_SELECTED
) != 0;
128 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
131 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
132 tvi
.stateMask
= TVIS_SELECTED
;
133 tvi
.state
= select
? TVIS_SELECTED
: 0;
136 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
138 wxLogLastError(wxT("TreeView_SetItem"));
142 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
144 SelectItem(hwndTV
, htItem
, false);
147 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
149 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
152 // helper function which selects all items in a range and, optionally,
153 // unselects all others
154 static void SelectRange(HWND hwndTV
,
157 bool unselectOthers
= true)
159 // find the first (or last) item and select it
161 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
162 while ( htItem
&& cont
)
164 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
166 if ( !IsItemSelected(hwndTV
, htItem
) )
168 SelectItem(hwndTV
, htItem
);
175 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
177 UnselectItem(hwndTV
, htItem
);
181 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
184 // select the items in range
185 cont
= htFirst
!= htLast
;
186 while ( htItem
&& cont
)
188 if ( !IsItemSelected(hwndTV
, htItem
) )
190 SelectItem(hwndTV
, htItem
);
193 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
195 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
199 if ( unselectOthers
)
203 if ( IsItemSelected(hwndTV
, htItem
) )
205 UnselectItem(hwndTV
, htItem
);
208 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
212 // seems to be necessary - otherwise the just selected items don't always
213 // appear as selected
214 UpdateWindow(hwndTV
);
217 // helper function which tricks the standard control into changing the focused
218 // item without changing anything else (if someone knows why Microsoft doesn't
219 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
220 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
223 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
228 if ( htItem
!= htFocus
)
230 // remember the selection state of the item
231 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
233 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
235 // prevent the tree from unselecting the old focus which it
236 // would do by default (TreeView_SelectItem unselects the
238 TreeView_SelectItem(hwndTV
, 0);
239 SelectItem(hwndTV
, htFocus
);
242 TreeView_SelectItem(hwndTV
, htItem
);
246 // need to clear the selection which TreeView_SelectItem() gave
248 UnselectItem(hwndTV
, htItem
);
250 //else: was selected, still selected - ok
252 //else: nothing to do, focus already there
258 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
260 // just clear the focus
261 TreeView_SelectItem(hwndTV
, 0);
263 if ( wasFocusSelected
)
265 // restore the selection state
266 SelectItem(hwndTV
, htFocus
);
269 //else: nothing to do, no focus already
273 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 // a convenient wrapper around TV_ITEM struct which adds a ctor
281 #pragma warning( disable : 4097 ) // inheriting from typedef
284 struct wxTreeViewItem
: public TV_ITEM
286 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
287 UINT mask_
, // fields which are valid
288 UINT stateMask_
= 0) // for TVIF_STATE only
292 // hItem member is always valid
293 mask
= mask_
| TVIF_HANDLE
;
294 stateMask
= stateMask_
;
299 // wxVirutalNode is used in place of a single root when 'hidden' root is
301 class wxVirtualNode
: public wxTreeViewItem
304 wxVirtualNode(wxTreeItemData
*data
)
305 : wxTreeViewItem(TVI_ROOT
, 0)
315 wxTreeItemData
*GetData() const { return m_data
; }
316 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
319 wxTreeItemData
*m_data
;
321 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
325 #pragma warning( default : 4097 )
328 // a macro to get the virtual root, returns NULL if none
329 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
331 // returns true if the item is the virtual root
332 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
334 // a class which encapsulates the tree traversal logic: it vists all (unless
335 // OnVisit() returns false) items under the given one
336 class wxTreeTraversal
339 wxTreeTraversal(const wxTreeCtrl
*tree
)
344 // do traverse the tree: visit all items (recursively by default) under the
345 // given one; return true if all items were traversed or false if the
346 // traversal was aborted because OnVisit returned false
347 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
349 // override this function to do whatever is needed for each item, return
350 // false to stop traversing
351 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
354 const wxTreeCtrl
*GetTree() const { return m_tree
; }
357 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
359 const wxTreeCtrl
*m_tree
;
361 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
364 // internal class for getting the selected items
365 class TraverseSelections
: public wxTreeTraversal
368 TraverseSelections(const wxTreeCtrl
*tree
,
369 wxArrayTreeItemIds
& selections
)
370 : wxTreeTraversal(tree
), m_selections(selections
)
372 m_selections
.Empty();
374 DoTraverse(tree
->GetRootItem());
377 virtual bool OnVisit(const wxTreeItemId
& item
)
379 // can't visit a virtual node.
380 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
385 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
386 if ( GetTree()->IsItemChecked(item
) )
388 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
391 m_selections
.Add(item
);
397 size_t GetCount() const { return m_selections
.GetCount(); }
400 wxArrayTreeItemIds
& m_selections
;
403 // internal class for counting tree items
404 class TraverseCounter
: public wxTreeTraversal
407 TraverseCounter(const wxTreeCtrl
*tree
,
408 const wxTreeItemId
& root
,
410 : wxTreeTraversal(tree
)
414 DoTraverse(root
, recursively
);
417 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
424 size_t GetCount() const { return m_count
; }
430 // ----------------------------------------------------------------------------
431 // This class is needed for support of different images: the Win32 common
432 // control natively supports only 2 images (the normal one and another for the
433 // selected state). We wish to provide support for 2 more of them for folder
434 // items (i.e. those which have children): for expanded state and for expanded
435 // selected state. For this we use this structure to store the additional items
438 // There is only one problem with this: when we retrieve the item's data, we
439 // don't know whether we get a pointer to wxTreeItemData or
440 // wxTreeItemIndirectData. So we always set the item id to an invalid value
441 // in this class and the code using the client data checks for it and retrieves
442 // the real client data in this case.
443 // ----------------------------------------------------------------------------
445 class wxTreeItemIndirectData
: public wxTreeItemData
448 // ctor associates this data with the item and the real item data becomes
449 // available through our GetData() method
450 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
452 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
458 m_data
= tree
->GetItemData(item
);
460 // and set ourselves as the new one
461 tree
->SetIndirectItemData(item
, this);
463 // we must have the invalid value for the item
467 // dtor deletes the associated data as well
468 virtual ~wxTreeItemIndirectData() { delete m_data
; }
471 // get the real data associated with the item
472 wxTreeItemData
*GetData() const { return m_data
; }
474 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
476 // do we have such image?
477 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
479 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
481 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
484 // all the images associated with the item
485 int m_images
[wxTreeItemIcon_Max
];
487 // the real client data
488 wxTreeItemData
*m_data
;
490 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
493 // ----------------------------------------------------------------------------
495 // ----------------------------------------------------------------------------
497 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
499 // ----------------------------------------------------------------------------
501 // ----------------------------------------------------------------------------
503 // indices in gs_expandEvents table below
518 // handy table for sending events - it has to be initialized during run-time
519 // now so can't be const any more
520 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
523 but logically it's a const table with the following entries:
526 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
527 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
531 // ============================================================================
533 // ============================================================================
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
541 if ( !OnVisit(root
) )
544 return Traverse(root
, recursively
);
547 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
549 wxTreeItemIdValue cookie
;
550 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
551 while ( child
.IsOk() )
553 // depth first traversal
554 if ( recursively
&& !Traverse(child
, true) )
557 if ( !OnVisit(child
) )
560 child
= m_tree
->GetNextChild(root
, cookie
);
566 // ----------------------------------------------------------------------------
567 // construction and destruction
568 // ----------------------------------------------------------------------------
570 void wxTreeCtrl::Init()
572 m_imageListNormal
= NULL
;
573 m_imageListState
= NULL
;
574 m_ownsImageListNormal
= m_ownsImageListState
= false;
576 m_hasAnyAttr
= false;
578 m_pVirtualRoot
= NULL
;
580 // initialize the global array of events now as it can't be done statically
581 // with the wxEVT_XXX values being allocated during run-time only
582 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
583 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
584 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
585 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
588 bool wxTreeCtrl::Create(wxWindow
*parent
,
593 const wxValidator
& validator
,
594 const wxString
& name
)
598 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
599 style
|= wxBORDER_SUNKEN
;
601 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
605 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
606 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
608 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
609 wstyle
|= TVS_HASLINES
;
610 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
611 wstyle
|= TVS_HASBUTTONS
;
613 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
614 wstyle
|= TVS_EDITLABELS
;
616 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
617 wstyle
|= TVS_LINESATROOT
;
619 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
621 if ( wxTheApp
->GetComCtl32Version() >= 471 )
622 wstyle
|= TVS_FULLROWSELECT
;
625 // using TVS_CHECKBOXES for emulation of a multiselection tree control
626 // doesn't work without the new enough headers
627 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
628 !defined( __GNUWIN32_OLD__ ) && \
629 !defined( __BORLANDC__ ) && \
630 !defined( __WATCOMC__ ) && \
631 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
633 // we emulate the multiple selection tree controls by using checkboxes: set
634 // up the image list we need for this if we do have multiple selections
635 if ( m_windowStyle
& wxTR_MULTIPLE
)
636 wstyle
|= TVS_CHECKBOXES
;
637 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
639 // Create the tree control.
640 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
643 #if wxUSE_COMCTL32_SAFELY
644 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
645 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
647 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
648 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
650 // This works around a bug in the Windows tree control whereby for some versions
651 // of comctrl32, setting any colour actually draws the background in black.
652 // This will initialise the background to the system colour.
653 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
654 // Assume the user has an updated comctl32.dll.
655 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
656 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
657 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
661 // VZ: this is some experimental code which may be used to get the
662 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
663 // AFAIK, the standard DLL does about the same thing anyhow.
665 if ( m_windowStyle
& wxTR_MULTIPLE
)
669 // create the DC compatible with the current screen
670 HDC hdcMem
= CreateCompatibleDC(NULL
);
672 // create a mono bitmap of the standard size
673 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
674 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
675 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
676 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
677 1, // # of color planes
678 1, // # bits needed for one pixel
679 0); // array containing colour data
680 SelectObject(hdcMem
, hbmpCheck
);
682 // then draw a check mark into it
683 RECT rect
= { 0, 0, x
, y
};
684 if ( !::DrawFrameControl(hdcMem
, &rect
,
686 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
688 wxLogLastError(wxT("DrawFrameControl(check)"));
691 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
692 imagelistCheckboxes
.Add(bmp
);
694 if ( !::DrawFrameControl(hdcMem
, &rect
,
698 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
701 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
702 imagelistCheckboxes
.Add(bmp
);
708 SetStateImageList(&imagelistCheckboxes
);
712 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
717 wxTreeCtrl::~wxTreeCtrl()
719 // delete any attributes
722 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
724 // prevent TVN_DELETEITEM handler from deleting the attributes again!
725 m_hasAnyAttr
= false;
730 // delete user data to prevent memory leaks
731 // also deletes hidden root node storage.
734 if (m_ownsImageListNormal
) delete m_imageListNormal
;
735 if (m_ownsImageListState
) delete m_imageListState
;
738 // ----------------------------------------------------------------------------
740 // ----------------------------------------------------------------------------
742 // simple wrappers which add error checking in debug mode
744 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
746 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
747 _T("can't retrieve virtual root item") );
749 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
751 wxLogLastError(wxT("TreeView_GetItem"));
759 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
761 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
763 wxLogLastError(wxT("TreeView_SetItem"));
767 size_t wxTreeCtrl::GetCount() const
769 return (size_t)TreeView_GetCount(GetHwnd());
772 unsigned int wxTreeCtrl::GetIndent() const
774 return TreeView_GetIndent(GetHwnd());
777 void wxTreeCtrl::SetIndent(unsigned int indent
)
779 TreeView_SetIndent(GetHwnd(), indent
);
782 wxImageList
*wxTreeCtrl::GetImageList() const
784 return m_imageListNormal
;
787 wxImageList
*wxTreeCtrl::GetStateImageList() const
789 return m_imageListState
;
792 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
795 TreeView_SetImageList(GetHwnd(),
796 imageList
? imageList
->GetHIMAGELIST() : 0,
800 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
802 if (m_ownsImageListNormal
)
803 delete m_imageListNormal
;
805 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
806 m_ownsImageListNormal
= false;
809 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
811 if (m_ownsImageListState
) delete m_imageListState
;
812 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
813 m_ownsImageListState
= false;
816 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
818 SetImageList(imageList
);
819 m_ownsImageListNormal
= true;
822 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
824 SetStateImageList(imageList
);
825 m_ownsImageListState
= true;
828 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
829 bool recursively
) const
831 TraverseCounter
counter(this, item
, recursively
);
833 return counter
.GetCount() - 1;
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
840 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
842 #if !wxUSE_COMCTL32_SAFELY
843 if ( !wxWindowBase::SetBackgroundColour(colour
) )
846 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
852 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
854 #if !wxUSE_COMCTL32_SAFELY
855 if ( !wxWindowBase::SetForegroundColour(colour
) )
858 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
864 // ----------------------------------------------------------------------------
866 // ----------------------------------------------------------------------------
868 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
870 wxChar buf
[512]; // the size is arbitrary...
872 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
873 tvItem
.pszText
= buf
;
874 tvItem
.cchTextMax
= WXSIZEOF(buf
);
875 if ( !DoGetItem(&tvItem
) )
877 // don't return some garbage which was on stack, but an empty string
881 return wxString(buf
);
884 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
886 if ( IS_VIRTUAL_ROOT(item
) )
889 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
890 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
893 // when setting the text of the item being edited, the text control should
894 // be updated to reflect the new text as well, otherwise calling
895 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
897 // don't use GetEditControl() here because m_textCtrl is not set yet
898 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
901 if ( item
== GetSelection() )
903 ::SetWindowText(hwndEdit
, text
);
908 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
909 wxTreeItemIcon which
) const
911 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
912 if ( !DoGetItem(&tvItem
) )
917 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
920 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
922 wxTreeItemIcon which
) const
924 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
925 if ( !DoGetItem(&tvItem
) )
930 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
932 data
->SetImage(image
, which
);
934 // make sure that we have selected images as well
935 if ( which
== wxTreeItemIcon_Normal
&&
936 !data
->HasImage(wxTreeItemIcon_Selected
) )
938 data
->SetImage(image
, wxTreeItemIcon_Selected
);
941 if ( which
== wxTreeItemIcon_Expanded
&&
942 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
944 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
948 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
952 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
953 tvItem
.iSelectedImage
= imageSel
;
954 tvItem
.iImage
= image
;
958 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
959 wxTreeItemIcon which
) const
961 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
963 // TODO: Maybe a hidden root can still provide images?
967 if ( HasIndirectData(item
) )
969 return DoGetItemImageFromData(item
, which
);
976 wxFAIL_MSG( wxT("unknown tree item image type") );
978 case wxTreeItemIcon_Normal
:
982 case wxTreeItemIcon_Selected
:
983 mask
= TVIF_SELECTEDIMAGE
;
986 case wxTreeItemIcon_Expanded
:
987 case wxTreeItemIcon_SelectedExpanded
:
991 wxTreeViewItem
tvItem(item
, mask
);
994 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
997 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
998 wxTreeItemIcon which
)
1000 if ( IS_VIRTUAL_ROOT(item
) )
1002 // TODO: Maybe a hidden root can still store images?
1012 wxFAIL_MSG( wxT("unknown tree item image type") );
1015 case wxTreeItemIcon_Normal
:
1017 const int imageNormalOld
= GetItemImage(item
);
1018 const int imageSelOld
= GetItemSelectedImage(item
);
1020 // always set the normal image
1021 imageNormal
= image
;
1023 // if the selected and normal images were the same, they should
1024 // be the same after the update, otherwise leave the selected
1026 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1030 case wxTreeItemIcon_Selected
:
1031 imageNormal
= GetItemImage(item
);
1035 case wxTreeItemIcon_Expanded
:
1036 case wxTreeItemIcon_SelectedExpanded
:
1037 if ( !HasIndirectData(item
) )
1039 // we need to get the old images first, because after we create
1040 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1042 imageNormal
= GetItemImage(item
);
1043 imageSel
= GetItemSelectedImage(item
);
1045 // if it doesn't have it yet, add it
1046 wxTreeItemIndirectData
*data
= new
1047 wxTreeItemIndirectData(this, item
);
1049 // copy the data to the new location
1050 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1051 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1054 DoSetItemImageFromData(item
, image
, which
);
1056 // reset the normal/selected images because we won't use them any
1057 // more - now they're stored inside the indirect data
1059 imageSel
= I_IMAGECALLBACK
;
1063 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1064 // change both normal and selected image - otherwise the change simply
1065 // doesn't take place!
1066 DoSetItemImages(item
, imageNormal
, imageSel
);
1069 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1071 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1073 // Hidden root may have data.
1074 if ( IS_VIRTUAL_ROOT(item
) )
1076 return GET_VIRTUAL_ROOT()->GetData();
1080 if ( !DoGetItem(&tvItem
) )
1085 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1086 if ( IsDataIndirect(data
) )
1088 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1094 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1096 if ( IS_VIRTUAL_ROOT(item
) )
1098 GET_VIRTUAL_ROOT()->SetData(data
);
1101 // first, associate this piece of data with this item
1107 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1109 if ( HasIndirectData(item
) )
1111 if ( DoGetItem(&tvItem
) )
1113 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1117 wxFAIL_MSG( wxT("failed to change tree items data") );
1122 tvItem
.lParam
= (LPARAM
)data
;
1127 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1128 wxTreeItemIndirectData
*data
)
1130 // this should never happen because it's unnecessary and will probably lead
1131 // to crash too because the code elsewhere supposes that the pointer the
1132 // wxTreeItemIndirectData has is a real wxItemData and not
1133 // wxTreeItemIndirectData as well
1134 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1136 SetItemData(item
, data
);
1139 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1141 // query the item itself
1142 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1143 if ( !DoGetItem(&tvItem
) )
1148 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1150 return data
&& IsDataIndirect(data
);
1153 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1155 if ( IS_VIRTUAL_ROOT(item
) )
1158 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1159 tvItem
.cChildren
= (int)has
;
1163 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1165 if ( IS_VIRTUAL_ROOT(item
) )
1168 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1169 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1173 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1175 if ( IS_VIRTUAL_ROOT(item
) )
1178 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1179 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1183 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1185 if ( IS_VIRTUAL_ROOT(item
) )
1189 if ( GetBoundingRect(item
, rect
) )
1195 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1197 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1199 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1202 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1204 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1206 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1209 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1211 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1213 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1216 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1217 const wxColour
& col
)
1219 wxTreeItemAttr
*attr
;
1220 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1221 if ( it
== m_attrs
.end() )
1223 m_hasAnyAttr
= true;
1225 m_attrs
[item
.m_pItem
] =
1226 attr
= new wxTreeItemAttr
;
1233 attr
->SetTextColour(col
);
1238 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1239 const wxColour
& col
)
1241 wxTreeItemAttr
*attr
;
1242 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1243 if ( it
== m_attrs
.end() )
1245 m_hasAnyAttr
= true;
1247 m_attrs
[item
.m_pItem
] =
1248 attr
= new wxTreeItemAttr
;
1250 else // already in the hash
1255 attr
->SetBackgroundColour(col
);
1260 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1262 wxTreeItemAttr
*attr
;
1263 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1264 if ( it
== m_attrs
.end() )
1266 m_hasAnyAttr
= true;
1268 m_attrs
[item
.m_pItem
] =
1269 attr
= new wxTreeItemAttr
;
1271 else // already in the hash
1276 attr
->SetFont(font
);
1281 // ----------------------------------------------------------------------------
1283 // ----------------------------------------------------------------------------
1285 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1287 if ( item
== wxTreeItemId(TVI_ROOT
) )
1289 // virtual (hidden) root is never visible
1293 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1296 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1297 // the HTREEITEM with TVM_GETITEMRECT
1298 *(HTREEITEM
*)&rect
= HITEM(item
);
1300 // false means get item rect for the whole item, not only text
1301 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1304 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1306 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1309 return tvItem
.cChildren
!= 0;
1312 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1314 // probably not a good idea to put it here
1315 //wxASSERT( ItemHasChildren(item) );
1317 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1320 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1323 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1325 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1328 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1331 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1333 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1336 return (tvItem
.state
& TVIS_BOLD
) != 0;
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1343 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1345 // Root may be real (visible) or virtual (hidden).
1346 if ( GET_VIRTUAL_ROOT() )
1349 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1352 wxTreeItemId
wxTreeCtrl::GetSelection() const
1354 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1355 wxT("this only works with single selection controls") );
1357 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1360 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1364 if ( IS_VIRTUAL_ROOT(item
) )
1366 // no parent for the virtual root
1371 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1372 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1374 // the top level items should have the virtual root as their parent
1379 return wxTreeItemId(hItem
);
1382 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1383 wxTreeItemIdValue
& cookie
) const
1385 // remember the last child returned in 'cookie'
1386 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1388 return wxTreeItemId(cookie
);
1391 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1392 wxTreeItemIdValue
& cookie
) const
1394 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1395 HITEM(wxTreeItemId(cookie
))));
1396 cookie
= item
.m_pItem
;
1401 #if WXWIN_COMPATIBILITY_2_4
1403 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1406 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1408 return wxTreeItemId((void *)cookie
);
1411 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1414 wxTreeItemId
item(TreeView_GetNextSibling
1417 HITEM(wxTreeItemId((void *)cookie
)
1419 cookie
= (long)item
.m_pItem
;
1424 #endif // WXWIN_COMPATIBILITY_2_4
1426 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1428 // can this be done more efficiently?
1429 wxTreeItemIdValue cookie
;
1431 wxTreeItemId childLast
,
1432 child
= GetFirstChild(item
, cookie
);
1433 while ( child
.IsOk() )
1436 child
= GetNextChild(item
, cookie
);
1442 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1444 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1447 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1449 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1452 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1454 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1457 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1459 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1461 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1464 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1466 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1468 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1471 // ----------------------------------------------------------------------------
1472 // multiple selections emulation
1473 // ----------------------------------------------------------------------------
1475 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1477 // receive the desired information.
1478 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1481 // state image indices are 1 based
1482 return ((tvItem
.state
>> 12) - 1) == 1;
1485 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1487 // receive the desired information.
1488 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1492 // state images are one-based
1493 tvItem
.state
= (check
? 2 : 1) << 12;
1498 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1500 TraverseSelections
selector(this, selections
);
1502 return selector
.GetCount();
1505 // ----------------------------------------------------------------------------
1507 // ----------------------------------------------------------------------------
1509 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1510 wxTreeItemId hInsertAfter
,
1511 const wxString
& text
,
1512 int image
, int selectedImage
,
1513 wxTreeItemData
*data
)
1515 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1517 _T("can't have more than one root in the tree") );
1519 TV_INSERTSTRUCT tvIns
;
1520 tvIns
.hParent
= HITEM(parent
);
1521 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1523 // this is how we insert the item as the first child: supply a NULL
1525 if ( !tvIns
.hInsertAfter
)
1527 tvIns
.hInsertAfter
= TVI_FIRST
;
1531 if ( !text
.IsEmpty() )
1534 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1538 tvIns
.item
.pszText
= NULL
;
1539 tvIns
.item
.cchTextMax
= 0;
1545 tvIns
.item
.iImage
= image
;
1547 if ( selectedImage
== -1 )
1549 // take the same image for selected icon if not specified
1550 selectedImage
= image
;
1554 if ( selectedImage
!= -1 )
1556 mask
|= TVIF_SELECTEDIMAGE
;
1557 tvIns
.item
.iSelectedImage
= selectedImage
;
1563 tvIns
.item
.lParam
= (LPARAM
)data
;
1566 tvIns
.item
.mask
= mask
;
1568 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1571 wxLogLastError(wxT("TreeView_InsertItem"));
1576 // associate the application tree item with Win32 tree item handle
1580 return wxTreeItemId(id
);
1583 // for compatibility only
1584 #if WXWIN_COMPATIBILITY_2_4
1586 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1587 const wxString
& text
,
1588 int image
, int selImage
,
1591 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1592 image
, selImage
, NULL
);
1595 #endif // WXWIN_COMPATIBILITY_2_4
1597 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1598 int image
, int selectedImage
,
1599 wxTreeItemData
*data
)
1602 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1604 // create a virtual root item, the parent for all the others
1605 m_pVirtualRoot
= new wxVirtualNode(data
);
1610 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1611 text
, image
, selectedImage
, data
);
1614 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1615 const wxString
& text
,
1616 int image
, int selectedImage
,
1617 wxTreeItemData
*data
)
1619 return DoInsertItem(parent
, TVI_FIRST
,
1620 text
, image
, selectedImage
, data
);
1623 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1624 const wxTreeItemId
& idPrevious
,
1625 const wxString
& text
,
1626 int image
, int selectedImage
,
1627 wxTreeItemData
*data
)
1629 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1632 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1634 const wxString
& text
,
1635 int image
, int selectedImage
,
1636 wxTreeItemData
*data
)
1638 // find the item from index
1639 wxTreeItemIdValue cookie
;
1640 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1641 while ( index
!= 0 && idCur
.IsOk() )
1646 idCur
= GetNextChild(parent
, cookie
);
1649 // assert, not check: if the index is invalid, we will append the item
1651 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1653 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1656 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1657 const wxString
& text
,
1658 int image
, int selectedImage
,
1659 wxTreeItemData
*data
)
1661 return DoInsertItem(parent
, TVI_LAST
,
1662 text
, image
, selectedImage
, data
);
1665 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1667 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1669 wxLogLastError(wxT("TreeView_DeleteItem"));
1673 // delete all children (but don't delete the item itself)
1674 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1676 wxTreeItemIdValue cookie
;
1678 wxArrayTreeItemIds children
;
1679 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1680 while ( child
.IsOk() )
1682 children
.Add(child
);
1684 child
= GetNextChild(item
, cookie
);
1687 size_t nCount
= children
.Count();
1688 for ( size_t n
= 0; n
< nCount
; n
++ )
1690 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1692 wxLogLastError(wxT("TreeView_DeleteItem"));
1697 void wxTreeCtrl::DeleteAllItems()
1699 // delete the "virtual" root item.
1700 if ( GET_VIRTUAL_ROOT() )
1702 delete GET_VIRTUAL_ROOT();
1703 m_pVirtualRoot
= NULL
;
1706 // and all the real items
1708 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1710 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1714 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1716 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1717 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1718 flag
== TVE_EXPAND
||
1720 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1722 // A hidden root can be neither expanded nor collapsed.
1723 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1724 wxT("Can't expand/collapse hidden root node!") )
1726 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1727 // emulate them. This behaviour has changed slightly with comctl32.dll
1728 // v 4.70 - now it does send them but only the first time. To maintain
1729 // compatible behaviour and also in order to not have surprises with the
1730 // future versions, don't rely on this and still do everything ourselves.
1731 // To avoid that the messages be sent twice when the item is expanded for
1732 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1734 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1738 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1740 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1741 event
.m_item
= item
;
1742 event
.SetEventObject(this);
1744 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1746 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1750 (void)GetEventHandler()->ProcessEvent(event
);
1752 //else: change didn't took place, so do nothing at all
1755 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1757 DoExpand(item
, TVE_EXPAND
);
1760 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1762 DoExpand(item
, TVE_COLLAPSE
);
1765 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1767 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1770 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1772 DoExpand(item
, TVE_TOGGLE
);
1775 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1777 DoExpand(item
, action
);
1780 void wxTreeCtrl::Unselect()
1782 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1783 wxT("doesn't make sense, may be you want UnselectAll()?") );
1785 // just remove the selection
1786 SelectItem(wxTreeItemId());
1789 void wxTreeCtrl::UnselectAll()
1791 if ( m_windowStyle
& wxTR_MULTIPLE
)
1793 wxArrayTreeItemIds selections
;
1794 size_t count
= GetSelections(selections
);
1795 for ( size_t n
= 0; n
< count
; n
++ )
1797 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1798 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1799 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1800 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1801 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1806 // just remove the selection
1811 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1813 if ( m_windowStyle
& wxTR_MULTIPLE
)
1815 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1816 // selecting the item means checking it
1818 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1819 ::SelectItem(GetHwnd(), HITEM(item
));
1820 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1824 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1825 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1826 // send them ourselves
1828 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1829 event
.m_item
= item
;
1830 event
.SetEventObject(this);
1832 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1833 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1835 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1837 wxLogLastError(wxT("TreeView_SelectItem"));
1841 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1842 (void)GetEventHandler()->ProcessEvent(event
);
1845 //else: program vetoed the change
1849 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1852 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1855 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1857 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1859 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1863 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1868 void wxTreeCtrl::DeleteTextCtrl()
1872 // the HWND corresponding to this control is deleted by the tree
1873 // control itself and we don't know when exactly this happens, so check
1874 // if the window still exists before calling UnsubclassWin()
1875 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1877 m_textCtrl
->SetHWND(0);
1880 m_textCtrl
->UnsubclassWin();
1881 m_textCtrl
->SetHWND(0);
1887 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1888 wxClassInfo
* textControlClass
)
1890 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1894 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1895 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1897 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1906 // textctrl is subclassed in MSWOnNotify
1910 // End label editing, optionally cancelling the edit
1911 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1913 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1918 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1920 TV_HITTESTINFO hitTestInfo
;
1921 hitTestInfo
.pt
.x
= (int)point
.x
;
1922 hitTestInfo
.pt
.y
= (int)point
.y
;
1924 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1929 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1930 flags |= wxTREE_HITTEST_##flag
1932 TRANSLATE_FLAG(ABOVE
);
1933 TRANSLATE_FLAG(BELOW
);
1934 TRANSLATE_FLAG(NOWHERE
);
1935 TRANSLATE_FLAG(ONITEMBUTTON
);
1936 TRANSLATE_FLAG(ONITEMICON
);
1937 TRANSLATE_FLAG(ONITEMINDENT
);
1938 TRANSLATE_FLAG(ONITEMLABEL
);
1939 TRANSLATE_FLAG(ONITEMRIGHT
);
1940 TRANSLATE_FLAG(ONITEMSTATEICON
);
1941 TRANSLATE_FLAG(TOLEFT
);
1942 TRANSLATE_FLAG(TORIGHT
);
1944 #undef TRANSLATE_FLAG
1946 return wxTreeItemId(hitTestInfo
.hItem
);
1949 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1951 bool textOnly
) const
1955 // Virtual root items have no bounding rectangle
1956 if ( IS_VIRTUAL_ROOT(item
) )
1961 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1964 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1970 // couldn't retrieve rect: for example, item isn't visible
1975 // ----------------------------------------------------------------------------
1977 // ----------------------------------------------------------------------------
1979 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1980 // functions such as IsDataIndirect()
1981 class wxTreeSortHelper
1984 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1987 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1989 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1990 if ( tree
->IsDataIndirect(data
) )
1992 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1995 return data
->GetId();
1999 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2003 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2004 wxT("sorting tree without data doesn't make sense") );
2006 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2008 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2009 GetIdFromData(tree
, pItem2
));
2012 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2013 const wxTreeItemId
& item2
)
2015 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2018 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2020 // rely on the fact that TreeView_SortChildren does the same thing as our
2021 // default behaviour, i.e. sorts items alphabetically and so call it
2022 // directly if we're not in derived class (much more efficient!)
2023 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2025 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2030 tvSort
.hParent
= HITEM(item
);
2031 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2032 tvSort
.lParam
= (LPARAM
)this;
2033 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2037 // ----------------------------------------------------------------------------
2039 // ----------------------------------------------------------------------------
2041 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2043 if ( cmd
== EN_UPDATE
)
2045 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2046 event
.SetEventObject( this );
2047 ProcessCommand(event
);
2049 else if ( cmd
== EN_KILLFOCUS
)
2051 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2052 event
.SetEventObject( this );
2053 ProcessCommand(event
);
2061 // command processed
2065 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2066 // only do it during dragging, minimize wxWin overhead (this is important for
2067 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2068 // instead of passing by wxWin events
2069 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2071 bool processed
= false;
2073 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2075 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2077 // we only process mouse messages here and these parameters have the
2078 // same meaning for all of them
2079 int x
= GET_X_LPARAM(lParam
),
2080 y
= GET_Y_LPARAM(lParam
);
2081 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2085 case WM_RBUTTONDOWN
:
2086 // if the item we are about to right click on
2087 // is not already select, remove the entire
2088 // previous selection
2089 if (!::IsItemSelected(GetHwnd(), htItem
))
2094 // select item and set the focus to the
2095 // newly selected item
2096 ::SelectItem(GetHwnd(), htItem
);
2097 ::SetFocus(GetHwnd(), htItem
);
2100 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2101 case WM_LBUTTONDOWN
:
2102 if ( htItem
&& isMultiple
)
2104 if ( wParam
& MK_CONTROL
)
2108 // toggle selected state
2109 ToggleItemSelection(GetHwnd(), htItem
);
2111 ::SetFocus(GetHwnd(), htItem
);
2113 // reset on any click without Shift
2114 m_htSelStart
.Unset();
2118 else if ( wParam
& MK_SHIFT
)
2120 // this selects all items between the starting one and
2123 if ( !m_htSelStart
)
2125 // take the focused item
2126 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2129 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2130 !(wParam
& MK_CONTROL
));
2132 ::SetFocus(GetHwnd(), htItem
);
2136 else // normal click
2138 // avoid doing anything if we click on the only
2139 // currently selected item
2141 wxArrayTreeItemIds selections
;
2142 size_t count
= GetSelections(selections
);
2145 HITEM_PTR(selections
[0]) != htItem
)
2147 // clear the previously selected items, if the
2148 // user clicked outside of the present selection.
2149 // otherwise, perform the deselection on mouse-up.
2150 // this allows multiple drag and drop to work.
2152 if (IsItemSelected(GetHwnd(), htItem
))
2154 ::SetFocus(GetHwnd(), htItem
);
2160 // prevent the click from starting in-place editing
2161 // which should only happen if we click on the
2162 // already selected item (and nothing else is
2165 TreeView_SelectItem(GetHwnd(), 0);
2166 ::SelectItem(GetHwnd(), htItem
);
2170 // reset on any click without Shift
2171 m_htSelStart
.Unset();
2175 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2180 m_dragImage
->Move(wxPoint(x
, y
));
2183 // highlight the item as target (hiding drag image is
2184 // necessary - otherwise the display will be corrupted)
2185 m_dragImage
->Hide();
2186 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2187 m_dragImage
->Show();
2194 // facilitates multiple drag-and-drop
2195 if (htItem
&& isMultiple
)
2197 wxArrayTreeItemIds selections
;
2198 size_t count
= GetSelections(selections
);
2201 !(wParam
& MK_CONTROL
) &&
2202 !(wParam
& MK_SHIFT
))
2205 TreeView_SelectItem(GetHwnd(), htItem
);
2214 m_dragImage
->EndDrag();
2218 // generate the drag end event
2219 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2221 event
.m_item
= htItem
;
2222 event
.m_pointDrag
= wxPoint(x
, y
);
2223 event
.SetEventObject(this);
2225 (void)GetEventHandler()->ProcessEvent(event
);
2227 // if we don't do it, the tree seems to think that 2 items
2228 // are selected simultaneously which is quite weird
2229 TreeView_SelectDropTarget(GetHwnd(), 0);
2234 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2235 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2237 // the tree control greys out the selected item when it loses focus and
2238 // paints it as selected again when it regains it, but it won't do it
2239 // for the other items itself - help it
2240 wxArrayTreeItemIds selections
;
2241 size_t count
= GetSelections(selections
);
2243 for ( size_t n
= 0; n
< count
; n
++ )
2245 // TreeView_GetItemRect() will return false if item is not visible,
2246 // which may happen perfectly well
2247 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2250 ::InvalidateRect(GetHwnd(), &rect
, false);
2254 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2256 bool bCtrl
= wxIsCtrlDown(),
2257 bShift
= wxIsShiftDown();
2259 // we handle.arrows and space, but not page up/down and home/end: the
2260 // latter should be easy, but not the former
2262 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2263 if ( !m_htSelStart
)
2265 m_htSelStart
= htSel
;
2268 if ( wParam
== VK_SPACE
)
2272 ToggleItemSelection(GetHwnd(), htSel
);
2278 ::SelectItem(GetHwnd(), htSel
);
2283 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2285 if ( !bCtrl
&& !bShift
)
2287 // no modifiers, just clear selection and then let the default
2288 // processing to take place
2293 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2295 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2296 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2297 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2301 // at the top/bottom
2307 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2311 // without changing selection
2312 ::SetFocus(GetHwnd(), htNext
);
2319 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2320 else if ( nMsg
== WM_CHAR
)
2322 // don't let the control process Space and Return keys because it
2323 // doesn't do anything useful with them anyhow but always beeps
2324 // annoyingly when it receives them and there is no way to turn it off
2325 // simply if you just process TREEITEM_ACTIVATED event to which Space
2326 // and Enter presses are mapped in your code
2327 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2334 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2339 // process WM_NOTIFY Windows message
2340 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2342 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2343 wxEventType eventType
= wxEVT_NULL
;
2344 NMHDR
*hdr
= (NMHDR
*)lParam
;
2346 switch ( hdr
->code
)
2349 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2352 case TVN_BEGINRDRAG
:
2354 if ( eventType
== wxEVT_NULL
)
2355 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2356 //else: left drag, already set above
2358 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2360 event
.m_item
= tv
->itemNew
.hItem
;
2361 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2363 // don't allow dragging by default: the user code must
2364 // explicitly say that it wants to allow it to avoid breaking
2370 case TVN_BEGINLABELEDIT
:
2372 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2373 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2375 event
.m_item
= info
->item
.hItem
;
2376 event
.m_label
= info
->item
.pszText
;
2377 event
.m_editCancelled
= false;
2381 case TVN_DELETEITEM
:
2383 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2384 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2386 event
.m_item
= tv
->itemOld
.hItem
;
2390 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2391 if ( it
!= m_attrs
.end() )
2400 case TVN_ENDLABELEDIT
:
2402 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2403 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2405 event
.m_item
= info
->item
.hItem
;
2406 event
.m_label
= info
->item
.pszText
;
2407 if (info
->item
.pszText
== NULL
)
2409 event
.m_editCancelled
= true;
2413 event
.m_editCancelled
= false;
2418 case TVN_GETDISPINFO
:
2419 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2422 case TVN_SETDISPINFO
:
2424 if ( eventType
== wxEVT_NULL
)
2425 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2426 //else: get, already set above
2428 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2430 event
.m_item
= info
->item
.hItem
;
2434 case TVN_ITEMEXPANDING
:
2435 case TVN_ITEMEXPANDED
:
2437 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2440 switch ( tv
->action
)
2443 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2451 what
= IDX_COLLAPSE
;
2455 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2458 eventType
= gs_expandEvents
[what
][how
];
2460 event
.m_item
= tv
->itemNew
.hItem
;
2466 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2467 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2469 // fabricate the lParam and wParam parameters sufficiently
2470 // similar to the ones from a "real" WM_KEYDOWN so that
2471 // CreateKeyEvent() works correctly
2473 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2475 WXWPARAM wParam
= info
->wVKey
;
2477 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2480 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2485 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2490 // a separate event for Space/Return
2491 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2492 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2494 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2496 event2
.SetEventObject(this);
2497 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2499 event2
.m_item
= GetSelection();
2501 //else: don't know how to get it
2503 (void)GetEventHandler()->ProcessEvent(event2
);
2508 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2509 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2510 // we have to handle both messages:
2511 case TVN_SELCHANGEDA
:
2512 case TVN_SELCHANGEDW
:
2513 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2516 case TVN_SELCHANGINGA
:
2517 case TVN_SELCHANGINGW
:
2519 if ( eventType
== wxEVT_NULL
)
2520 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2521 //else: already set above
2523 if (hdr
->code
== TVN_SELCHANGINGW
||
2524 hdr
->code
== TVN_SELCHANGEDW
)
2526 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2527 event
.m_item
= tv
->itemNew
.hItem
;
2528 event
.m_itemOld
= tv
->itemOld
.hItem
;
2532 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2533 event
.m_item
= tv
->itemNew
.hItem
;
2534 event
.m_itemOld
= tv
->itemOld
.hItem
;
2539 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2542 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2543 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2544 switch ( nmcd
.dwDrawStage
)
2547 // if we've got any items with non standard attributes,
2548 // notify us before painting each item
2549 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2553 case CDDS_ITEMPREPAINT
:
2555 wxMapTreeAttr::iterator
2556 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2558 if ( it
== m_attrs
.end() )
2560 // nothing to do for this item
2561 *result
= CDRF_DODEFAULT
;
2565 wxTreeItemAttr
* const attr
= it
->second
;
2568 if ( attr
->HasFont() )
2570 hFont
= GetHfontOf(attr
->GetFont());
2578 if ( attr
->HasTextColour() )
2580 colText
= attr
->GetTextColour();
2584 colText
= GetForegroundColour();
2587 // selection colours should override ours
2588 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2591 ::GetSysColor(COLOR_HIGHLIGHT
);
2593 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2598 if ( attr
->HasBackgroundColour() )
2600 colBack
= attr
->GetBackgroundColour();
2604 colBack
= GetBackgroundColour();
2607 lptvcd
->clrText
= wxColourToRGB(colText
);
2608 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2611 // note that if we wanted to set colours for
2612 // individual columns (subitems), we would have
2613 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2616 ::SelectObject(nmcd
.hdc
, hFont
);
2618 *result
= CDRF_NEWFONT
;
2622 *result
= CDRF_DODEFAULT
;
2628 *result
= CDRF_DODEFAULT
;
2632 // we always process it
2634 #endif // _WIN32_IE >= 0x300
2638 DWORD pos
= GetMessagePos();
2640 point
.x
= LOWORD(pos
);
2641 point
.y
= HIWORD(pos
);
2642 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2644 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2645 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2647 event
.m_item
= item
;
2648 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2656 TV_HITTESTINFO tvhti
;
2657 ::GetCursorPos(&tvhti
.pt
);
2658 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2659 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2661 if ( tvhti
.flags
& TVHT_ONITEM
)
2663 event
.m_item
= tvhti
.hItem
;
2664 eventType
= (int)hdr
->code
== NM_DBLCLK
2665 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2666 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2668 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2669 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2678 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2681 event
.SetEventObject(this);
2682 event
.SetEventType(eventType
);
2684 bool processed
= GetEventHandler()->ProcessEvent(event
);
2687 switch ( hdr
->code
)
2690 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2691 // the return code of this event handler as the return value for
2692 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2693 // expanded status would never work
2698 case TVN_BEGINRDRAG
:
2699 if ( event
.IsAllowed() )
2701 // normally this is impossible because the m_dragImage is
2702 // deleted once the drag operation is over
2703 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2705 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2706 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2707 m_dragImage
->Show();
2711 case TVN_DELETEITEM
:
2713 // NB: we might process this message using wxWindows event
2714 // tables, but due to overhead of wxWin event system we
2715 // prefer to do it here ourself (otherwise deleting a tree
2716 // with many items is just too slow)
2717 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2719 wxTreeItemId item
= event
.m_item
;
2720 if ( HasIndirectData(item
) )
2722 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2724 delete data
; // can't be NULL here
2728 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2729 delete data
; // may be NULL, ok
2732 processed
= true; // Make sure we don't get called twice
2736 case TVN_BEGINLABELEDIT
:
2737 // return true to cancel label editing
2738 *result
= !event
.IsAllowed();
2739 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2740 if(event
.IsAllowed())
2742 HWND hText
= TreeView_GetEditControl(GetHwnd());
2745 // MBN: if m_textCtrl already has an HWND, it is a stale
2746 // pointer from a previous edit (because the user
2747 // didn't modify the label before dismissing the control,
2748 // and TVN_ENDLABELEDIT was not sent), so delete it
2749 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2752 m_textCtrl
= new wxTextCtrl();
2753 m_textCtrl
->SetParent(this);
2754 m_textCtrl
->SetHWND((WXHWND
)hText
);
2755 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2757 // set wxTE_PROCESS_ENTER style for the text control to
2758 // force it to process the Enter presses itself, otherwise
2759 // they could be stolen from it by the dialog
2761 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2762 | wxTE_PROCESS_ENTER
);
2767 case TVN_ENDLABELEDIT
:
2768 // return true to set the label to the new string: note that we
2769 // also must pretend that we did process the message or it is going
2770 // to be passed to DefWindowProc() which will happily return false
2771 // cancelling the label change
2772 *result
= event
.IsAllowed();
2775 // ensure that we don't have the text ctrl which is going to be
2780 case TVN_SELCHANGING
:
2781 case TVN_ITEMEXPANDING
:
2782 // return true to prevent the action from happening
2783 *result
= !event
.IsAllowed();
2786 case TVN_ITEMEXPANDED
:
2787 // the item is not refreshed properly after expansion when it has
2788 // an image depending on the expanded/collapsed state - bug in
2789 // comctl32.dll or our code?
2791 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2792 wxTreeItemId
id(tv
->itemNew
.hItem
);
2794 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2802 case TVN_GETDISPINFO
:
2803 // NB: so far the user can't set the image himself anyhow, so do it
2804 // anyway - but this may change later
2805 //if ( /* !processed && */ 1 )
2807 wxTreeItemId item
= event
.m_item
;
2808 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2809 if ( info
->item
.mask
& TVIF_IMAGE
)
2812 DoGetItemImageFromData
2815 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2816 : wxTreeItemIcon_Normal
2819 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2821 info
->item
.iSelectedImage
=
2822 DoGetItemImageFromData
2825 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2826 : wxTreeItemIcon_Selected
2833 // for the other messages the return value is ignored and there is
2834 // nothing special to do
2839 // ----------------------------------------------------------------------------
2841 // ----------------------------------------------------------------------------
2843 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2844 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2846 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2849 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2850 tvi
.mask
= TVIF_STATE
;
2851 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2853 // Select the specified state, or -1 == cycle to the next one.
2856 TreeView_GetItem(GetHwnd(), &tvi
);
2858 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2859 if ( state
== m_imageListState
->GetImageCount() )
2863 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2864 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2866 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2868 TreeView_SetItem(GetHwnd(), &tvi
);
2871 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2874 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2875 tvi
.mask
= TVIF_STATE
;
2876 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2877 TreeView_GetItem(GetHwnd(), &tvi
);
2879 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2882 #endif // wxUSE_TREECTRL