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
=
1019 GetItemImage(item
, wxTreeItemIcon_Selected
);
1021 // always set the normal image
1022 imageNormal
= image
;
1024 // if the selected and normal images were the same, they should
1025 // be the same after the update, otherwise leave the selected
1027 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1031 case wxTreeItemIcon_Selected
:
1032 imageNormal
= GetItemImage(item
);
1036 case wxTreeItemIcon_Expanded
:
1037 case wxTreeItemIcon_SelectedExpanded
:
1038 if ( !HasIndirectData(item
) )
1040 // we need to get the old images first, because after we create
1041 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1043 imageNormal
= GetItemImage(item
);
1044 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1046 // if it doesn't have it yet, add it
1047 wxTreeItemIndirectData
*data
= new
1048 wxTreeItemIndirectData(this, item
);
1050 // copy the data to the new location
1051 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1052 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1055 DoSetItemImageFromData(item
, image
, which
);
1057 // reset the normal/selected images because we won't use them any
1058 // more - now they're stored inside the indirect data
1060 imageSel
= I_IMAGECALLBACK
;
1064 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1065 // change both normal and selected image - otherwise the change simply
1066 // doesn't take place!
1067 DoSetItemImages(item
, imageNormal
, imageSel
);
1070 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1072 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1074 // Hidden root may have data.
1075 if ( IS_VIRTUAL_ROOT(item
) )
1077 return GET_VIRTUAL_ROOT()->GetData();
1081 if ( !DoGetItem(&tvItem
) )
1086 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1087 if ( IsDataIndirect(data
) )
1089 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1095 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1097 if ( IS_VIRTUAL_ROOT(item
) )
1099 GET_VIRTUAL_ROOT()->SetData(data
);
1102 // first, associate this piece of data with this item
1108 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1110 if ( HasIndirectData(item
) )
1112 if ( DoGetItem(&tvItem
) )
1114 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1118 wxFAIL_MSG( wxT("failed to change tree items data") );
1123 tvItem
.lParam
= (LPARAM
)data
;
1128 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1129 wxTreeItemIndirectData
*data
)
1131 // this should never happen because it's unnecessary and will probably lead
1132 // to crash too because the code elsewhere supposes that the pointer the
1133 // wxTreeItemIndirectData has is a real wxItemData and not
1134 // wxTreeItemIndirectData as well
1135 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1137 SetItemData(item
, data
);
1140 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1142 // query the item itself
1143 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1144 if ( !DoGetItem(&tvItem
) )
1149 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1151 return data
&& IsDataIndirect(data
);
1154 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1156 if ( IS_VIRTUAL_ROOT(item
) )
1159 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1160 tvItem
.cChildren
= (int)has
;
1164 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1166 if ( IS_VIRTUAL_ROOT(item
) )
1169 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1170 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1174 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1176 if ( IS_VIRTUAL_ROOT(item
) )
1179 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1180 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1184 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1186 if ( IS_VIRTUAL_ROOT(item
) )
1190 if ( GetBoundingRect(item
, rect
) )
1196 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1198 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1200 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1203 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1205 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1207 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1210 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1212 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1214 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1217 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1218 const wxColour
& col
)
1220 wxTreeItemAttr
*attr
;
1221 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1222 if ( it
== m_attrs
.end() )
1224 m_hasAnyAttr
= true;
1226 m_attrs
[item
.m_pItem
] =
1227 attr
= new wxTreeItemAttr
;
1234 attr
->SetTextColour(col
);
1239 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1240 const wxColour
& col
)
1242 wxTreeItemAttr
*attr
;
1243 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1244 if ( it
== m_attrs
.end() )
1246 m_hasAnyAttr
= true;
1248 m_attrs
[item
.m_pItem
] =
1249 attr
= new wxTreeItemAttr
;
1251 else // already in the hash
1256 attr
->SetBackgroundColour(col
);
1261 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1263 wxTreeItemAttr
*attr
;
1264 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1265 if ( it
== m_attrs
.end() )
1267 m_hasAnyAttr
= true;
1269 m_attrs
[item
.m_pItem
] =
1270 attr
= new wxTreeItemAttr
;
1272 else // already in the hash
1277 attr
->SetFont(font
);
1282 // ----------------------------------------------------------------------------
1284 // ----------------------------------------------------------------------------
1286 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1288 if ( item
== wxTreeItemId(TVI_ROOT
) )
1290 // virtual (hidden) root is never visible
1294 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1297 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1298 // the HTREEITEM with TVM_GETITEMRECT
1299 *(HTREEITEM
*)&rect
= HITEM(item
);
1301 // false means get item rect for the whole item, not only text
1302 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1305 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1307 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1310 return tvItem
.cChildren
!= 0;
1313 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1315 // probably not a good idea to put it here
1316 //wxASSERT( ItemHasChildren(item) );
1318 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1321 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1324 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1326 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1329 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1332 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1334 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1337 return (tvItem
.state
& TVIS_BOLD
) != 0;
1340 // ----------------------------------------------------------------------------
1342 // ----------------------------------------------------------------------------
1344 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1346 // Root may be real (visible) or virtual (hidden).
1347 if ( GET_VIRTUAL_ROOT() )
1350 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1353 wxTreeItemId
wxTreeCtrl::GetSelection() const
1355 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1356 wxT("this only works with single selection controls") );
1358 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1361 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1365 if ( IS_VIRTUAL_ROOT(item
) )
1367 // no parent for the virtual root
1372 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1373 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1375 // the top level items should have the virtual root as their parent
1380 return wxTreeItemId(hItem
);
1383 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1384 wxTreeItemIdValue
& cookie
) const
1386 // remember the last child returned in 'cookie'
1387 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1389 return wxTreeItemId(cookie
);
1392 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1393 wxTreeItemIdValue
& cookie
) const
1395 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1396 HITEM(wxTreeItemId(cookie
))));
1397 cookie
= item
.m_pItem
;
1402 #if WXWIN_COMPATIBILITY_2_4
1404 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1407 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1409 return wxTreeItemId((void *)cookie
);
1412 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1415 wxTreeItemId
item(TreeView_GetNextSibling
1418 HITEM(wxTreeItemId((void *)cookie
)
1420 cookie
= (long)item
.m_pItem
;
1425 #endif // WXWIN_COMPATIBILITY_2_4
1427 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1429 // can this be done more efficiently?
1430 wxTreeItemIdValue cookie
;
1432 wxTreeItemId childLast
,
1433 child
= GetFirstChild(item
, cookie
);
1434 while ( child
.IsOk() )
1437 child
= GetNextChild(item
, cookie
);
1443 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1445 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1448 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1450 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1453 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1455 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1458 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1460 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1462 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1465 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1467 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1469 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1472 // ----------------------------------------------------------------------------
1473 // multiple selections emulation
1474 // ----------------------------------------------------------------------------
1476 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1478 // receive the desired information.
1479 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1482 // state image indices are 1 based
1483 return ((tvItem
.state
>> 12) - 1) == 1;
1486 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1488 // receive the desired information.
1489 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1493 // state images are one-based
1494 tvItem
.state
= (check
? 2 : 1) << 12;
1499 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1501 TraverseSelections
selector(this, selections
);
1503 return selector
.GetCount();
1506 // ----------------------------------------------------------------------------
1508 // ----------------------------------------------------------------------------
1510 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1511 wxTreeItemId hInsertAfter
,
1512 const wxString
& text
,
1513 int image
, int selectedImage
,
1514 wxTreeItemData
*data
)
1516 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1518 _T("can't have more than one root in the tree") );
1520 TV_INSERTSTRUCT tvIns
;
1521 tvIns
.hParent
= HITEM(parent
);
1522 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1524 // this is how we insert the item as the first child: supply a NULL
1526 if ( !tvIns
.hInsertAfter
)
1528 tvIns
.hInsertAfter
= TVI_FIRST
;
1532 if ( !text
.IsEmpty() )
1535 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1539 tvIns
.item
.pszText
= NULL
;
1540 tvIns
.item
.cchTextMax
= 0;
1546 tvIns
.item
.iImage
= image
;
1548 if ( selectedImage
== -1 )
1550 // take the same image for selected icon if not specified
1551 selectedImage
= image
;
1555 if ( selectedImage
!= -1 )
1557 mask
|= TVIF_SELECTEDIMAGE
;
1558 tvIns
.item
.iSelectedImage
= selectedImage
;
1564 tvIns
.item
.lParam
= (LPARAM
)data
;
1567 tvIns
.item
.mask
= mask
;
1569 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1572 wxLogLastError(wxT("TreeView_InsertItem"));
1577 // associate the application tree item with Win32 tree item handle
1581 return wxTreeItemId(id
);
1584 // for compatibility only
1585 #if WXWIN_COMPATIBILITY_2_4
1587 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1588 const wxString
& text
,
1589 int image
, int selImage
,
1592 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1593 image
, selImage
, NULL
);
1596 #endif // WXWIN_COMPATIBILITY_2_4
1598 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1599 int image
, int selectedImage
,
1600 wxTreeItemData
*data
)
1603 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1605 // create a virtual root item, the parent for all the others
1606 m_pVirtualRoot
= new wxVirtualNode(data
);
1611 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1612 text
, image
, selectedImage
, data
);
1615 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1616 const wxString
& text
,
1617 int image
, int selectedImage
,
1618 wxTreeItemData
*data
)
1620 return DoInsertItem(parent
, TVI_FIRST
,
1621 text
, image
, selectedImage
, data
);
1624 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1625 const wxTreeItemId
& idPrevious
,
1626 const wxString
& text
,
1627 int image
, int selectedImage
,
1628 wxTreeItemData
*data
)
1630 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1633 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1635 const wxString
& text
,
1636 int image
, int selectedImage
,
1637 wxTreeItemData
*data
)
1639 // find the item from index
1640 wxTreeItemIdValue cookie
;
1641 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1642 while ( index
!= 0 && idCur
.IsOk() )
1647 idCur
= GetNextChild(parent
, cookie
);
1650 // assert, not check: if the index is invalid, we will append the item
1652 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1654 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1657 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1658 const wxString
& text
,
1659 int image
, int selectedImage
,
1660 wxTreeItemData
*data
)
1662 return DoInsertItem(parent
, TVI_LAST
,
1663 text
, image
, selectedImage
, data
);
1666 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1668 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1670 wxLogLastError(wxT("TreeView_DeleteItem"));
1674 // delete all children (but don't delete the item itself)
1675 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1677 wxTreeItemIdValue cookie
;
1679 wxArrayTreeItemIds children
;
1680 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1681 while ( child
.IsOk() )
1683 children
.Add(child
);
1685 child
= GetNextChild(item
, cookie
);
1688 size_t nCount
= children
.Count();
1689 for ( size_t n
= 0; n
< nCount
; n
++ )
1691 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1693 wxLogLastError(wxT("TreeView_DeleteItem"));
1698 void wxTreeCtrl::DeleteAllItems()
1700 // delete the "virtual" root item.
1701 if ( GET_VIRTUAL_ROOT() )
1703 delete GET_VIRTUAL_ROOT();
1704 m_pVirtualRoot
= NULL
;
1707 // and all the real items
1709 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1711 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1715 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1717 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1718 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1719 flag
== TVE_EXPAND
||
1721 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1723 // A hidden root can be neither expanded nor collapsed.
1724 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1725 wxT("Can't expand/collapse hidden root node!") )
1727 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1728 // emulate them. This behaviour has changed slightly with comctl32.dll
1729 // v 4.70 - now it does send them but only the first time. To maintain
1730 // compatible behaviour and also in order to not have surprises with the
1731 // future versions, don't rely on this and still do everything ourselves.
1732 // To avoid that the messages be sent twice when the item is expanded for
1733 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1735 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1739 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1741 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1742 event
.m_item
= item
;
1743 event
.SetEventObject(this);
1745 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1747 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1751 (void)GetEventHandler()->ProcessEvent(event
);
1753 //else: change didn't took place, so do nothing at all
1756 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1758 DoExpand(item
, TVE_EXPAND
);
1761 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1763 DoExpand(item
, TVE_COLLAPSE
);
1766 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1768 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1771 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1773 DoExpand(item
, TVE_TOGGLE
);
1776 #if WXWIN_COMPATIBILITY_2_4
1777 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1779 DoExpand(item
, action
);
1783 void wxTreeCtrl::Unselect()
1785 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1786 wxT("doesn't make sense, may be you want UnselectAll()?") );
1788 // just remove the selection
1789 SelectItem(wxTreeItemId());
1792 void wxTreeCtrl::UnselectAll()
1794 if ( m_windowStyle
& wxTR_MULTIPLE
)
1796 wxArrayTreeItemIds selections
;
1797 size_t count
= GetSelections(selections
);
1798 for ( size_t n
= 0; n
< count
; n
++ )
1800 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1801 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1802 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1803 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1804 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1809 // just remove the selection
1814 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1816 if ( m_windowStyle
& wxTR_MULTIPLE
)
1818 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1819 // selecting the item means checking it
1821 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1822 ::SelectItem(GetHwnd(), HITEM(item
));
1823 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1827 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1828 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1829 // send them ourselves
1831 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1832 event
.m_item
= item
;
1833 event
.SetEventObject(this);
1835 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1836 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1838 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1840 wxLogLastError(wxT("TreeView_SelectItem"));
1844 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1845 (void)GetEventHandler()->ProcessEvent(event
);
1848 //else: program vetoed the change
1852 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1855 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1858 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1860 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1862 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1866 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1871 void wxTreeCtrl::DeleteTextCtrl()
1875 // the HWND corresponding to this control is deleted by the tree
1876 // control itself and we don't know when exactly this happens, so check
1877 // if the window still exists before calling UnsubclassWin()
1878 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1880 m_textCtrl
->SetHWND(0);
1883 m_textCtrl
->UnsubclassWin();
1884 m_textCtrl
->SetHWND(0);
1890 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1891 wxClassInfo
* textControlClass
)
1893 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1897 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1898 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1900 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1909 // textctrl is subclassed in MSWOnNotify
1913 // End label editing, optionally cancelling the edit
1914 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1916 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1921 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1923 TV_HITTESTINFO hitTestInfo
;
1924 hitTestInfo
.pt
.x
= (int)point
.x
;
1925 hitTestInfo
.pt
.y
= (int)point
.y
;
1927 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1932 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1933 flags |= wxTREE_HITTEST_##flag
1935 TRANSLATE_FLAG(ABOVE
);
1936 TRANSLATE_FLAG(BELOW
);
1937 TRANSLATE_FLAG(NOWHERE
);
1938 TRANSLATE_FLAG(ONITEMBUTTON
);
1939 TRANSLATE_FLAG(ONITEMICON
);
1940 TRANSLATE_FLAG(ONITEMINDENT
);
1941 TRANSLATE_FLAG(ONITEMLABEL
);
1942 TRANSLATE_FLAG(ONITEMRIGHT
);
1943 TRANSLATE_FLAG(ONITEMSTATEICON
);
1944 TRANSLATE_FLAG(TOLEFT
);
1945 TRANSLATE_FLAG(TORIGHT
);
1947 #undef TRANSLATE_FLAG
1949 return wxTreeItemId(hitTestInfo
.hItem
);
1952 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1954 bool textOnly
) const
1958 // Virtual root items have no bounding rectangle
1959 if ( IS_VIRTUAL_ROOT(item
) )
1964 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1967 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1973 // couldn't retrieve rect: for example, item isn't visible
1978 // ----------------------------------------------------------------------------
1980 // ----------------------------------------------------------------------------
1982 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1983 // functions such as IsDataIndirect()
1984 class wxTreeSortHelper
1987 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1990 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1992 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1993 if ( tree
->IsDataIndirect(data
) )
1995 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1998 return data
->GetId();
2002 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2006 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2007 wxT("sorting tree without data doesn't make sense") );
2009 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2011 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2012 GetIdFromData(tree
, pItem2
));
2015 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2016 const wxTreeItemId
& item2
)
2018 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2021 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2023 // rely on the fact that TreeView_SortChildren does the same thing as our
2024 // default behaviour, i.e. sorts items alphabetically and so call it
2025 // directly if we're not in derived class (much more efficient!)
2026 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2028 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2033 tvSort
.hParent
= HITEM(item
);
2034 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2035 tvSort
.lParam
= (LPARAM
)this;
2036 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2040 // ----------------------------------------------------------------------------
2042 // ----------------------------------------------------------------------------
2044 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2046 if ( cmd
== EN_UPDATE
)
2048 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2049 event
.SetEventObject( this );
2050 ProcessCommand(event
);
2052 else if ( cmd
== EN_KILLFOCUS
)
2054 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2055 event
.SetEventObject( this );
2056 ProcessCommand(event
);
2064 // command processed
2068 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2069 // only do it during dragging, minimize wxWin overhead (this is important for
2070 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2071 // instead of passing by wxWin events
2072 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2074 bool processed
= false;
2076 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2078 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2080 // we only process mouse messages here and these parameters have the
2081 // same meaning for all of them
2082 int x
= GET_X_LPARAM(lParam
),
2083 y
= GET_Y_LPARAM(lParam
);
2084 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2088 case WM_RBUTTONDOWN
:
2089 // if the item we are about to right click on
2090 // is not already select, remove the entire
2091 // previous selection
2092 if (!::IsItemSelected(GetHwnd(), htItem
))
2097 // select item and set the focus to the
2098 // newly selected item
2099 ::SelectItem(GetHwnd(), htItem
);
2100 ::SetFocus(GetHwnd(), htItem
);
2103 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2104 case WM_LBUTTONDOWN
:
2105 if ( htItem
&& isMultiple
)
2107 if ( wParam
& MK_CONTROL
)
2111 // toggle selected state
2112 ToggleItemSelection(GetHwnd(), htItem
);
2114 ::SetFocus(GetHwnd(), htItem
);
2116 // reset on any click without Shift
2117 m_htSelStart
.Unset();
2121 else if ( wParam
& MK_SHIFT
)
2123 // this selects all items between the starting one and
2126 if ( !m_htSelStart
)
2128 // take the focused item
2129 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2132 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2133 !(wParam
& MK_CONTROL
));
2135 ::SetFocus(GetHwnd(), htItem
);
2139 else // normal click
2141 // avoid doing anything if we click on the only
2142 // currently selected item
2144 wxArrayTreeItemIds selections
;
2145 size_t count
= GetSelections(selections
);
2148 HITEM_PTR(selections
[0]) != htItem
)
2150 // clear the previously selected items, if the
2151 // user clicked outside of the present selection.
2152 // otherwise, perform the deselection on mouse-up.
2153 // this allows multiple drag and drop to work.
2155 if (IsItemSelected(GetHwnd(), htItem
))
2157 ::SetFocus(GetHwnd(), htItem
);
2163 // prevent the click from starting in-place editing
2164 // which should only happen if we click on the
2165 // already selected item (and nothing else is
2168 TreeView_SelectItem(GetHwnd(), 0);
2169 ::SelectItem(GetHwnd(), htItem
);
2173 // reset on any click without Shift
2174 m_htSelStart
.Unset();
2178 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2183 m_dragImage
->Move(wxPoint(x
, y
));
2186 // highlight the item as target (hiding drag image is
2187 // necessary - otherwise the display will be corrupted)
2188 m_dragImage
->Hide();
2189 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2190 m_dragImage
->Show();
2197 // facilitates multiple drag-and-drop
2198 if (htItem
&& isMultiple
)
2200 wxArrayTreeItemIds selections
;
2201 size_t count
= GetSelections(selections
);
2204 !(wParam
& MK_CONTROL
) &&
2205 !(wParam
& MK_SHIFT
))
2208 TreeView_SelectItem(GetHwnd(), htItem
);
2217 m_dragImage
->EndDrag();
2221 // generate the drag end event
2222 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2224 event
.m_item
= htItem
;
2225 event
.m_pointDrag
= wxPoint(x
, y
);
2226 event
.SetEventObject(this);
2228 (void)GetEventHandler()->ProcessEvent(event
);
2230 // if we don't do it, the tree seems to think that 2 items
2231 // are selected simultaneously which is quite weird
2232 TreeView_SelectDropTarget(GetHwnd(), 0);
2237 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2238 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2240 // the tree control greys out the selected item when it loses focus and
2241 // paints it as selected again when it regains it, but it won't do it
2242 // for the other items itself - help it
2243 wxArrayTreeItemIds selections
;
2244 size_t count
= GetSelections(selections
);
2246 for ( size_t n
= 0; n
< count
; n
++ )
2248 // TreeView_GetItemRect() will return false if item is not visible,
2249 // which may happen perfectly well
2250 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2253 ::InvalidateRect(GetHwnd(), &rect
, false);
2257 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2259 bool bCtrl
= wxIsCtrlDown(),
2260 bShift
= wxIsShiftDown();
2262 // we handle.arrows and space, but not page up/down and home/end: the
2263 // latter should be easy, but not the former
2265 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2266 if ( !m_htSelStart
)
2268 m_htSelStart
= htSel
;
2271 if ( wParam
== VK_SPACE
)
2275 ToggleItemSelection(GetHwnd(), htSel
);
2281 ::SelectItem(GetHwnd(), htSel
);
2286 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2288 if ( !bCtrl
&& !bShift
)
2290 // no modifiers, just clear selection and then let the default
2291 // processing to take place
2296 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2298 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2299 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2300 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2304 // at the top/bottom
2310 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2314 // without changing selection
2315 ::SetFocus(GetHwnd(), htNext
);
2322 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2323 else if ( nMsg
== WM_CHAR
)
2325 // don't let the control process Space and Return keys because it
2326 // doesn't do anything useful with them anyhow but always beeps
2327 // annoyingly when it receives them and there is no way to turn it off
2328 // simply if you just process TREEITEM_ACTIVATED event to which Space
2329 // and Enter presses are mapped in your code
2330 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2337 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2342 // process WM_NOTIFY Windows message
2343 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2345 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2346 wxEventType eventType
= wxEVT_NULL
;
2347 NMHDR
*hdr
= (NMHDR
*)lParam
;
2349 switch ( hdr
->code
)
2352 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2355 case TVN_BEGINRDRAG
:
2357 if ( eventType
== wxEVT_NULL
)
2358 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2359 //else: left drag, already set above
2361 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2363 event
.m_item
= tv
->itemNew
.hItem
;
2364 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2366 // don't allow dragging by default: the user code must
2367 // explicitly say that it wants to allow it to avoid breaking
2373 case TVN_BEGINLABELEDIT
:
2375 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2376 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2378 event
.m_item
= info
->item
.hItem
;
2379 event
.m_label
= info
->item
.pszText
;
2380 event
.m_editCancelled
= false;
2384 case TVN_DELETEITEM
:
2386 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2387 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2389 event
.m_item
= tv
->itemOld
.hItem
;
2393 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2394 if ( it
!= m_attrs
.end() )
2403 case TVN_ENDLABELEDIT
:
2405 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2406 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2408 event
.m_item
= info
->item
.hItem
;
2409 event
.m_label
= info
->item
.pszText
;
2410 if (info
->item
.pszText
== NULL
)
2412 event
.m_editCancelled
= true;
2416 event
.m_editCancelled
= false;
2421 case TVN_GETDISPINFO
:
2422 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2425 case TVN_SETDISPINFO
:
2427 if ( eventType
== wxEVT_NULL
)
2428 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2429 //else: get, already set above
2431 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2433 event
.m_item
= info
->item
.hItem
;
2437 case TVN_ITEMEXPANDING
:
2438 case TVN_ITEMEXPANDED
:
2440 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2443 switch ( tv
->action
)
2446 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2454 what
= IDX_COLLAPSE
;
2458 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2461 eventType
= gs_expandEvents
[what
][how
];
2463 event
.m_item
= tv
->itemNew
.hItem
;
2469 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2470 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2472 // fabricate the lParam and wParam parameters sufficiently
2473 // similar to the ones from a "real" WM_KEYDOWN so that
2474 // CreateKeyEvent() works correctly
2476 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2478 WXWPARAM wParam
= info
->wVKey
;
2480 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2483 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2488 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2493 // a separate event for Space/Return
2494 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2495 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2497 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2499 event2
.SetEventObject(this);
2500 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2502 event2
.m_item
= GetSelection();
2504 //else: don't know how to get it
2506 (void)GetEventHandler()->ProcessEvent(event2
);
2511 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2512 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2513 // we have to handle both messages:
2514 case TVN_SELCHANGEDA
:
2515 case TVN_SELCHANGEDW
:
2516 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2519 case TVN_SELCHANGINGA
:
2520 case TVN_SELCHANGINGW
:
2522 if ( eventType
== wxEVT_NULL
)
2523 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2524 //else: already set above
2526 if (hdr
->code
== TVN_SELCHANGINGW
||
2527 hdr
->code
== TVN_SELCHANGEDW
)
2529 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2530 event
.m_item
= tv
->itemNew
.hItem
;
2531 event
.m_itemOld
= tv
->itemOld
.hItem
;
2535 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2536 event
.m_item
= tv
->itemNew
.hItem
;
2537 event
.m_itemOld
= tv
->itemOld
.hItem
;
2542 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2545 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2546 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2547 switch ( nmcd
.dwDrawStage
)
2550 // if we've got any items with non standard attributes,
2551 // notify us before painting each item
2552 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2556 case CDDS_ITEMPREPAINT
:
2558 wxMapTreeAttr::iterator
2559 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2561 if ( it
== m_attrs
.end() )
2563 // nothing to do for this item
2564 *result
= CDRF_DODEFAULT
;
2568 wxTreeItemAttr
* const attr
= it
->second
;
2571 if ( attr
->HasFont() )
2573 hFont
= GetHfontOf(attr
->GetFont());
2581 if ( attr
->HasTextColour() )
2583 colText
= attr
->GetTextColour();
2587 colText
= GetForegroundColour();
2590 // selection colours should override ours
2591 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2594 ::GetSysColor(COLOR_HIGHLIGHT
);
2596 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2601 if ( attr
->HasBackgroundColour() )
2603 colBack
= attr
->GetBackgroundColour();
2607 colBack
= GetBackgroundColour();
2610 lptvcd
->clrText
= wxColourToRGB(colText
);
2611 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2614 // note that if we wanted to set colours for
2615 // individual columns (subitems), we would have
2616 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2619 ::SelectObject(nmcd
.hdc
, hFont
);
2621 *result
= CDRF_NEWFONT
;
2625 *result
= CDRF_DODEFAULT
;
2631 *result
= CDRF_DODEFAULT
;
2635 // we always process it
2637 #endif // _WIN32_IE >= 0x300
2641 DWORD pos
= GetMessagePos();
2643 point
.x
= LOWORD(pos
);
2644 point
.y
= HIWORD(pos
);
2645 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2647 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2648 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2650 event
.m_item
= item
;
2651 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2659 TV_HITTESTINFO tvhti
;
2660 ::GetCursorPos(&tvhti
.pt
);
2661 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2662 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2664 if ( tvhti
.flags
& TVHT_ONITEM
)
2666 event
.m_item
= tvhti
.hItem
;
2667 eventType
= (int)hdr
->code
== NM_DBLCLK
2668 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2669 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2671 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2672 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2681 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2684 event
.SetEventObject(this);
2685 event
.SetEventType(eventType
);
2687 bool processed
= GetEventHandler()->ProcessEvent(event
);
2690 switch ( hdr
->code
)
2693 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2694 // the return code of this event handler as the return value for
2695 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2696 // expanded status would never work
2701 case TVN_BEGINRDRAG
:
2702 if ( event
.IsAllowed() )
2704 // normally this is impossible because the m_dragImage is
2705 // deleted once the drag operation is over
2706 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2708 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2709 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2710 m_dragImage
->Show();
2714 case TVN_DELETEITEM
:
2716 // NB: we might process this message using wxWindows event
2717 // tables, but due to overhead of wxWin event system we
2718 // prefer to do it here ourself (otherwise deleting a tree
2719 // with many items is just too slow)
2720 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2722 wxTreeItemId item
= event
.m_item
;
2723 if ( HasIndirectData(item
) )
2725 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2727 delete data
; // can't be NULL here
2731 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2732 delete data
; // may be NULL, ok
2735 processed
= true; // Make sure we don't get called twice
2739 case TVN_BEGINLABELEDIT
:
2740 // return true to cancel label editing
2741 *result
= !event
.IsAllowed();
2742 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2743 if(event
.IsAllowed())
2745 HWND hText
= TreeView_GetEditControl(GetHwnd());
2748 // MBN: if m_textCtrl already has an HWND, it is a stale
2749 // pointer from a previous edit (because the user
2750 // didn't modify the label before dismissing the control,
2751 // and TVN_ENDLABELEDIT was not sent), so delete it
2752 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2755 m_textCtrl
= new wxTextCtrl();
2756 m_textCtrl
->SetParent(this);
2757 m_textCtrl
->SetHWND((WXHWND
)hText
);
2758 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2760 // set wxTE_PROCESS_ENTER style for the text control to
2761 // force it to process the Enter presses itself, otherwise
2762 // they could be stolen from it by the dialog
2764 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2765 | wxTE_PROCESS_ENTER
);
2770 case TVN_ENDLABELEDIT
:
2771 // return true to set the label to the new string: note that we
2772 // also must pretend that we did process the message or it is going
2773 // to be passed to DefWindowProc() which will happily return false
2774 // cancelling the label change
2775 *result
= event
.IsAllowed();
2778 // ensure that we don't have the text ctrl which is going to be
2783 case TVN_SELCHANGING
:
2784 case TVN_ITEMEXPANDING
:
2785 // return true to prevent the action from happening
2786 *result
= !event
.IsAllowed();
2789 case TVN_ITEMEXPANDED
:
2790 // the item is not refreshed properly after expansion when it has
2791 // an image depending on the expanded/collapsed state - bug in
2792 // comctl32.dll or our code?
2794 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2795 wxTreeItemId
id(tv
->itemNew
.hItem
);
2797 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2805 case TVN_GETDISPINFO
:
2806 // NB: so far the user can't set the image himself anyhow, so do it
2807 // anyway - but this may change later
2808 //if ( /* !processed && */ 1 )
2810 wxTreeItemId item
= event
.m_item
;
2811 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2812 if ( info
->item
.mask
& TVIF_IMAGE
)
2815 DoGetItemImageFromData
2818 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2819 : wxTreeItemIcon_Normal
2822 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2824 info
->item
.iSelectedImage
=
2825 DoGetItemImageFromData
2828 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2829 : wxTreeItemIcon_Selected
2836 // for the other messages the return value is ignored and there is
2837 // nothing special to do
2842 // ----------------------------------------------------------------------------
2844 // ----------------------------------------------------------------------------
2846 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2847 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2849 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2852 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2853 tvi
.mask
= TVIF_STATE
;
2854 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2856 // Select the specified state, or -1 == cycle to the next one.
2859 TreeView_GetItem(GetHwnd(), &tvi
);
2861 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2862 if ( state
== m_imageListState
->GetImageCount() )
2866 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2867 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2869 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2871 TreeView_SetItem(GetHwnd(), &tvi
);
2874 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2877 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2878 tvi
.mask
= TVIF_STATE
;
2879 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2880 TreeView_GetItem(GetHwnd(), &tvi
);
2882 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2885 #endif // wxUSE_TREECTRL