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
;
402 DECLARE_NO_COPY_CLASS(TraverseSelections
)
405 // internal class for counting tree items
406 class TraverseCounter
: public wxTreeTraversal
409 TraverseCounter(const wxTreeCtrl
*tree
,
410 const wxTreeItemId
& root
,
412 : wxTreeTraversal(tree
)
416 DoTraverse(root
, recursively
);
419 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
426 size_t GetCount() const { return m_count
; }
431 DECLARE_NO_COPY_CLASS(TraverseCounter
)
434 // ----------------------------------------------------------------------------
435 // This class is needed for support of different images: the Win32 common
436 // control natively supports only 2 images (the normal one and another for the
437 // selected state). We wish to provide support for 2 more of them for folder
438 // items (i.e. those which have children): for expanded state and for expanded
439 // selected state. For this we use this structure to store the additional items
442 // There is only one problem with this: when we retrieve the item's data, we
443 // don't know whether we get a pointer to wxTreeItemData or
444 // wxTreeItemIndirectData. So we always set the item id to an invalid value
445 // in this class and the code using the client data checks for it and retrieves
446 // the real client data in this case.
447 // ----------------------------------------------------------------------------
449 class wxTreeItemIndirectData
: public wxTreeItemData
452 // ctor associates this data with the item and the real item data becomes
453 // available through our GetData() method
454 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
456 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
462 m_data
= tree
->GetItemData(item
);
464 // and set ourselves as the new one
465 tree
->SetIndirectItemData(item
, this);
467 // we must have the invalid value for the item
471 // dtor deletes the associated data as well
472 virtual ~wxTreeItemIndirectData() { delete m_data
; }
475 // get the real data associated with the item
476 wxTreeItemData
*GetData() const { return m_data
; }
478 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
480 // do we have such image?
481 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
483 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
485 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
488 // all the images associated with the item
489 int m_images
[wxTreeItemIcon_Max
];
491 // the real client data
492 wxTreeItemData
*m_data
;
494 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
503 // ----------------------------------------------------------------------------
505 // ----------------------------------------------------------------------------
507 // indices in gs_expandEvents table below
522 // handy table for sending events - it has to be initialized during run-time
523 // now so can't be const any more
524 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
527 but logically it's a const table with the following entries:
530 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
531 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
535 // ============================================================================
537 // ============================================================================
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
545 if ( !OnVisit(root
) )
548 return Traverse(root
, recursively
);
551 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
553 wxTreeItemIdValue cookie
;
554 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
555 while ( child
.IsOk() )
557 // depth first traversal
558 if ( recursively
&& !Traverse(child
, true) )
561 if ( !OnVisit(child
) )
564 child
= m_tree
->GetNextChild(root
, cookie
);
570 // ----------------------------------------------------------------------------
571 // construction and destruction
572 // ----------------------------------------------------------------------------
574 void wxTreeCtrl::Init()
576 m_imageListNormal
= NULL
;
577 m_imageListState
= NULL
;
578 m_ownsImageListNormal
= m_ownsImageListState
= false;
580 m_hasAnyAttr
= false;
582 m_pVirtualRoot
= NULL
;
584 // initialize the global array of events now as it can't be done statically
585 // with the wxEVT_XXX values being allocated during run-time only
586 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
587 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
588 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
589 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
592 bool wxTreeCtrl::Create(wxWindow
*parent
,
597 const wxValidator
& validator
,
598 const wxString
& name
)
602 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
603 style
|= wxBORDER_SUNKEN
;
605 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
609 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
610 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
612 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
613 wstyle
|= TVS_HASLINES
;
614 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
615 wstyle
|= TVS_HASBUTTONS
;
617 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
618 wstyle
|= TVS_EDITLABELS
;
620 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
621 wstyle
|= TVS_LINESATROOT
;
623 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
625 if ( wxTheApp
->GetComCtl32Version() >= 471 )
626 wstyle
|= TVS_FULLROWSELECT
;
629 // using TVS_CHECKBOXES for emulation of a multiselection tree control
630 // doesn't work without the new enough headers
631 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
632 !defined( __GNUWIN32_OLD__ ) && \
633 !defined( __BORLANDC__ ) && \
634 !defined( __WATCOMC__ ) && \
635 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
637 // we emulate the multiple selection tree controls by using checkboxes: set
638 // up the image list we need for this if we do have multiple selections
639 if ( m_windowStyle
& wxTR_MULTIPLE
)
640 wstyle
|= TVS_CHECKBOXES
;
641 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
643 // Create the tree control.
644 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
647 #if wxUSE_COMCTL32_SAFELY
648 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
649 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
651 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
652 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
654 // This works around a bug in the Windows tree control whereby for some versions
655 // of comctrl32, setting any colour actually draws the background in black.
656 // This will initialise the background to the system colour.
657 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
658 // Assume the user has an updated comctl32.dll.
659 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
660 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
661 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
665 // VZ: this is some experimental code which may be used to get the
666 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
667 // AFAIK, the standard DLL does about the same thing anyhow.
669 if ( m_windowStyle
& wxTR_MULTIPLE
)
673 // create the DC compatible with the current screen
674 HDC hdcMem
= CreateCompatibleDC(NULL
);
676 // create a mono bitmap of the standard size
677 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
678 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
679 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
680 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
681 1, // # of color planes
682 1, // # bits needed for one pixel
683 0); // array containing colour data
684 SelectObject(hdcMem
, hbmpCheck
);
686 // then draw a check mark into it
687 RECT rect
= { 0, 0, x
, y
};
688 if ( !::DrawFrameControl(hdcMem
, &rect
,
690 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
692 wxLogLastError(wxT("DrawFrameControl(check)"));
695 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
696 imagelistCheckboxes
.Add(bmp
);
698 if ( !::DrawFrameControl(hdcMem
, &rect
,
702 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
705 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
706 imagelistCheckboxes
.Add(bmp
);
712 SetStateImageList(&imagelistCheckboxes
);
716 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
721 wxTreeCtrl::~wxTreeCtrl()
723 // delete any attributes
726 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
728 // prevent TVN_DELETEITEM handler from deleting the attributes again!
729 m_hasAnyAttr
= false;
734 // delete user data to prevent memory leaks
735 // also deletes hidden root node storage.
738 if (m_ownsImageListNormal
) delete m_imageListNormal
;
739 if (m_ownsImageListState
) delete m_imageListState
;
742 // ----------------------------------------------------------------------------
744 // ----------------------------------------------------------------------------
746 // simple wrappers which add error checking in debug mode
748 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
750 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
751 _T("can't retrieve virtual root item") );
753 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
755 wxLogLastError(wxT("TreeView_GetItem"));
763 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
765 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
767 wxLogLastError(wxT("TreeView_SetItem"));
771 size_t wxTreeCtrl::GetCount() const
773 return (size_t)TreeView_GetCount(GetHwnd());
776 unsigned int wxTreeCtrl::GetIndent() const
778 return TreeView_GetIndent(GetHwnd());
781 void wxTreeCtrl::SetIndent(unsigned int indent
)
783 TreeView_SetIndent(GetHwnd(), indent
);
786 wxImageList
*wxTreeCtrl::GetImageList() const
788 return m_imageListNormal
;
791 wxImageList
*wxTreeCtrl::GetStateImageList() const
793 return m_imageListState
;
796 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
799 TreeView_SetImageList(GetHwnd(),
800 imageList
? imageList
->GetHIMAGELIST() : 0,
804 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
806 if (m_ownsImageListNormal
)
807 delete m_imageListNormal
;
809 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
810 m_ownsImageListNormal
= false;
813 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
815 if (m_ownsImageListState
) delete m_imageListState
;
816 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
817 m_ownsImageListState
= false;
820 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
822 SetImageList(imageList
);
823 m_ownsImageListNormal
= true;
826 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
828 SetStateImageList(imageList
);
829 m_ownsImageListState
= true;
832 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
833 bool recursively
) const
835 TraverseCounter
counter(this, item
, recursively
);
837 return counter
.GetCount() - 1;
840 // ----------------------------------------------------------------------------
842 // ----------------------------------------------------------------------------
844 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
846 #if !wxUSE_COMCTL32_SAFELY
847 if ( !wxWindowBase::SetBackgroundColour(colour
) )
850 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
856 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
858 #if !wxUSE_COMCTL32_SAFELY
859 if ( !wxWindowBase::SetForegroundColour(colour
) )
862 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
874 wxChar buf
[512]; // the size is arbitrary...
876 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
877 tvItem
.pszText
= buf
;
878 tvItem
.cchTextMax
= WXSIZEOF(buf
);
879 if ( !DoGetItem(&tvItem
) )
881 // don't return some garbage which was on stack, but an empty string
885 return wxString(buf
);
888 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
890 if ( IS_VIRTUAL_ROOT(item
) )
893 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
894 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
897 // when setting the text of the item being edited, the text control should
898 // be updated to reflect the new text as well, otherwise calling
899 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
901 // don't use GetEditControl() here because m_textCtrl is not set yet
902 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
905 if ( item
== GetSelection() )
907 ::SetWindowText(hwndEdit
, text
);
912 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
913 wxTreeItemIcon which
) const
915 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
916 if ( !DoGetItem(&tvItem
) )
921 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
924 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
926 wxTreeItemIcon which
) const
928 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
929 if ( !DoGetItem(&tvItem
) )
934 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
936 data
->SetImage(image
, which
);
938 // make sure that we have selected images as well
939 if ( which
== wxTreeItemIcon_Normal
&&
940 !data
->HasImage(wxTreeItemIcon_Selected
) )
942 data
->SetImage(image
, wxTreeItemIcon_Selected
);
945 if ( which
== wxTreeItemIcon_Expanded
&&
946 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
948 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
952 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
956 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
957 tvItem
.iSelectedImage
= imageSel
;
958 tvItem
.iImage
= image
;
962 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
963 wxTreeItemIcon which
) const
965 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
967 // TODO: Maybe a hidden root can still provide images?
971 if ( HasIndirectData(item
) )
973 return DoGetItemImageFromData(item
, which
);
980 wxFAIL_MSG( wxT("unknown tree item image type") );
982 case wxTreeItemIcon_Normal
:
986 case wxTreeItemIcon_Selected
:
987 mask
= TVIF_SELECTEDIMAGE
;
990 case wxTreeItemIcon_Expanded
:
991 case wxTreeItemIcon_SelectedExpanded
:
995 wxTreeViewItem
tvItem(item
, mask
);
998 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1001 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1002 wxTreeItemIcon which
)
1004 if ( IS_VIRTUAL_ROOT(item
) )
1006 // TODO: Maybe a hidden root can still store images?
1016 wxFAIL_MSG( wxT("unknown tree item image type") );
1019 case wxTreeItemIcon_Normal
:
1021 const int imageNormalOld
= GetItemImage(item
);
1022 const int imageSelOld
=
1023 GetItemImage(item
, wxTreeItemIcon_Selected
);
1025 // always set the normal image
1026 imageNormal
= image
;
1028 // if the selected and normal images were the same, they should
1029 // be the same after the update, otherwise leave the selected
1031 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1035 case wxTreeItemIcon_Selected
:
1036 imageNormal
= GetItemImage(item
);
1040 case wxTreeItemIcon_Expanded
:
1041 case wxTreeItemIcon_SelectedExpanded
:
1042 if ( !HasIndirectData(item
) )
1044 // we need to get the old images first, because after we create
1045 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1047 imageNormal
= GetItemImage(item
);
1048 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1050 // if it doesn't have it yet, add it
1051 wxTreeItemIndirectData
*data
= new
1052 wxTreeItemIndirectData(this, item
);
1054 // copy the data to the new location
1055 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1056 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1059 DoSetItemImageFromData(item
, image
, which
);
1061 // reset the normal/selected images because we won't use them any
1062 // more - now they're stored inside the indirect data
1064 imageSel
= I_IMAGECALLBACK
;
1068 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1069 // change both normal and selected image - otherwise the change simply
1070 // doesn't take place!
1071 DoSetItemImages(item
, imageNormal
, imageSel
);
1074 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1076 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1078 // Hidden root may have data.
1079 if ( IS_VIRTUAL_ROOT(item
) )
1081 return GET_VIRTUAL_ROOT()->GetData();
1085 if ( !DoGetItem(&tvItem
) )
1090 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1091 if ( IsDataIndirect(data
) )
1093 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1099 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1101 if ( IS_VIRTUAL_ROOT(item
) )
1103 GET_VIRTUAL_ROOT()->SetData(data
);
1106 // first, associate this piece of data with this item
1112 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1114 if ( HasIndirectData(item
) )
1116 if ( DoGetItem(&tvItem
) )
1118 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1122 wxFAIL_MSG( wxT("failed to change tree items data") );
1127 tvItem
.lParam
= (LPARAM
)data
;
1132 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1133 wxTreeItemIndirectData
*data
)
1135 // this should never happen because it's unnecessary and will probably lead
1136 // to crash too because the code elsewhere supposes that the pointer the
1137 // wxTreeItemIndirectData has is a real wxItemData and not
1138 // wxTreeItemIndirectData as well
1139 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1141 SetItemData(item
, data
);
1144 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1146 // query the item itself
1147 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1148 if ( !DoGetItem(&tvItem
) )
1153 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1155 return data
&& IsDataIndirect(data
);
1158 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1160 if ( IS_VIRTUAL_ROOT(item
) )
1163 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1164 tvItem
.cChildren
= (int)has
;
1168 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1170 if ( IS_VIRTUAL_ROOT(item
) )
1173 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1174 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1178 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1180 if ( IS_VIRTUAL_ROOT(item
) )
1183 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1184 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1188 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1190 if ( IS_VIRTUAL_ROOT(item
) )
1194 if ( GetBoundingRect(item
, rect
) )
1200 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1202 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1204 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1207 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1209 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1211 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1214 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1216 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1218 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1221 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1222 const wxColour
& col
)
1224 wxTreeItemAttr
*attr
;
1225 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1226 if ( it
== m_attrs
.end() )
1228 m_hasAnyAttr
= true;
1230 m_attrs
[item
.m_pItem
] =
1231 attr
= new wxTreeItemAttr
;
1238 attr
->SetTextColour(col
);
1243 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1244 const wxColour
& col
)
1246 wxTreeItemAttr
*attr
;
1247 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1248 if ( it
== m_attrs
.end() )
1250 m_hasAnyAttr
= true;
1252 m_attrs
[item
.m_pItem
] =
1253 attr
= new wxTreeItemAttr
;
1255 else // already in the hash
1260 attr
->SetBackgroundColour(col
);
1265 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1267 wxTreeItemAttr
*attr
;
1268 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1269 if ( it
== m_attrs
.end() )
1271 m_hasAnyAttr
= true;
1273 m_attrs
[item
.m_pItem
] =
1274 attr
= new wxTreeItemAttr
;
1276 else // already in the hash
1281 attr
->SetFont(font
);
1286 // ----------------------------------------------------------------------------
1288 // ----------------------------------------------------------------------------
1290 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1292 if ( item
== wxTreeItemId(TVI_ROOT
) )
1294 // virtual (hidden) root is never visible
1298 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1301 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1302 // the HTREEITEM with TVM_GETITEMRECT
1303 *(HTREEITEM
*)&rect
= HITEM(item
);
1305 // false means get item rect for the whole item, not only text
1306 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1309 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1311 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1314 return tvItem
.cChildren
!= 0;
1317 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1319 // probably not a good idea to put it here
1320 //wxASSERT( ItemHasChildren(item) );
1322 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1325 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1328 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1330 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1333 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1336 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1338 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1341 return (tvItem
.state
& TVIS_BOLD
) != 0;
1344 // ----------------------------------------------------------------------------
1346 // ----------------------------------------------------------------------------
1348 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1350 // Root may be real (visible) or virtual (hidden).
1351 if ( GET_VIRTUAL_ROOT() )
1354 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1357 wxTreeItemId
wxTreeCtrl::GetSelection() const
1359 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1360 wxT("this only works with single selection controls") );
1362 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1365 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1369 if ( IS_VIRTUAL_ROOT(item
) )
1371 // no parent for the virtual root
1376 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1377 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1379 // the top level items should have the virtual root as their parent
1384 return wxTreeItemId(hItem
);
1387 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1388 wxTreeItemIdValue
& cookie
) const
1390 // remember the last child returned in 'cookie'
1391 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1393 return wxTreeItemId(cookie
);
1396 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1397 wxTreeItemIdValue
& cookie
) const
1399 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1400 HITEM(wxTreeItemId(cookie
))));
1401 cookie
= item
.m_pItem
;
1406 #if WXWIN_COMPATIBILITY_2_4
1408 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1411 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1413 return wxTreeItemId((void *)cookie
);
1416 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1419 wxTreeItemId
item(TreeView_GetNextSibling
1422 HITEM(wxTreeItemId((void *)cookie
)
1424 cookie
= (long)item
.m_pItem
;
1429 #endif // WXWIN_COMPATIBILITY_2_4
1431 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1433 // can this be done more efficiently?
1434 wxTreeItemIdValue cookie
;
1436 wxTreeItemId childLast
,
1437 child
= GetFirstChild(item
, cookie
);
1438 while ( child
.IsOk() )
1441 child
= GetNextChild(item
, cookie
);
1447 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1449 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1452 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1454 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1457 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1459 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1462 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1464 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1466 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1469 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1471 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1473 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1476 // ----------------------------------------------------------------------------
1477 // multiple selections emulation
1478 // ----------------------------------------------------------------------------
1480 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1482 // receive the desired information.
1483 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1486 // state image indices are 1 based
1487 return ((tvItem
.state
>> 12) - 1) == 1;
1490 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1492 // receive the desired information.
1493 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1497 // state images are one-based
1498 tvItem
.state
= (check
? 2 : 1) << 12;
1503 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1505 TraverseSelections
selector(this, selections
);
1507 return selector
.GetCount();
1510 // ----------------------------------------------------------------------------
1512 // ----------------------------------------------------------------------------
1514 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1515 wxTreeItemId hInsertAfter
,
1516 const wxString
& text
,
1517 int image
, int selectedImage
,
1518 wxTreeItemData
*data
)
1520 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1522 _T("can't have more than one root in the tree") );
1524 TV_INSERTSTRUCT tvIns
;
1525 tvIns
.hParent
= HITEM(parent
);
1526 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1528 // this is how we insert the item as the first child: supply a NULL
1530 if ( !tvIns
.hInsertAfter
)
1532 tvIns
.hInsertAfter
= TVI_FIRST
;
1536 if ( !text
.IsEmpty() )
1539 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1543 tvIns
.item
.pszText
= NULL
;
1544 tvIns
.item
.cchTextMax
= 0;
1550 tvIns
.item
.iImage
= image
;
1552 if ( selectedImage
== -1 )
1554 // take the same image for selected icon if not specified
1555 selectedImage
= image
;
1559 if ( selectedImage
!= -1 )
1561 mask
|= TVIF_SELECTEDIMAGE
;
1562 tvIns
.item
.iSelectedImage
= selectedImage
;
1568 tvIns
.item
.lParam
= (LPARAM
)data
;
1571 tvIns
.item
.mask
= mask
;
1573 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1576 wxLogLastError(wxT("TreeView_InsertItem"));
1581 // associate the application tree item with Win32 tree item handle
1585 return wxTreeItemId(id
);
1588 // for compatibility only
1589 #if WXWIN_COMPATIBILITY_2_4
1591 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1592 const wxString
& text
,
1593 int image
, int selImage
,
1596 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1597 image
, selImage
, NULL
);
1600 #endif // WXWIN_COMPATIBILITY_2_4
1602 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1603 int image
, int selectedImage
,
1604 wxTreeItemData
*data
)
1607 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1609 // create a virtual root item, the parent for all the others
1610 m_pVirtualRoot
= new wxVirtualNode(data
);
1615 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1616 text
, image
, selectedImage
, data
);
1619 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1620 const wxString
& text
,
1621 int image
, int selectedImage
,
1622 wxTreeItemData
*data
)
1624 return DoInsertItem(parent
, TVI_FIRST
,
1625 text
, image
, selectedImage
, data
);
1628 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1629 const wxTreeItemId
& idPrevious
,
1630 const wxString
& text
,
1631 int image
, int selectedImage
,
1632 wxTreeItemData
*data
)
1634 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1637 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1639 const wxString
& text
,
1640 int image
, int selectedImage
,
1641 wxTreeItemData
*data
)
1643 // find the item from index
1644 wxTreeItemIdValue cookie
;
1645 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1646 while ( index
!= 0 && idCur
.IsOk() )
1651 idCur
= GetNextChild(parent
, cookie
);
1654 // assert, not check: if the index is invalid, we will append the item
1656 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1658 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1661 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1662 const wxString
& text
,
1663 int image
, int selectedImage
,
1664 wxTreeItemData
*data
)
1666 return DoInsertItem(parent
, TVI_LAST
,
1667 text
, image
, selectedImage
, data
);
1670 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1672 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1674 wxLogLastError(wxT("TreeView_DeleteItem"));
1678 // delete all children (but don't delete the item itself)
1679 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1681 wxTreeItemIdValue cookie
;
1683 wxArrayTreeItemIds children
;
1684 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1685 while ( child
.IsOk() )
1687 children
.Add(child
);
1689 child
= GetNextChild(item
, cookie
);
1692 size_t nCount
= children
.Count();
1693 for ( size_t n
= 0; n
< nCount
; n
++ )
1695 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1697 wxLogLastError(wxT("TreeView_DeleteItem"));
1702 void wxTreeCtrl::DeleteAllItems()
1704 // delete the "virtual" root item.
1705 if ( GET_VIRTUAL_ROOT() )
1707 delete GET_VIRTUAL_ROOT();
1708 m_pVirtualRoot
= NULL
;
1711 // and all the real items
1713 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1715 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1719 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1721 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1722 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1723 flag
== TVE_EXPAND
||
1725 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1727 // A hidden root can be neither expanded nor collapsed.
1728 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1729 wxT("Can't expand/collapse hidden root node!") )
1731 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1732 // emulate them. This behaviour has changed slightly with comctl32.dll
1733 // v 4.70 - now it does send them but only the first time. To maintain
1734 // compatible behaviour and also in order to not have surprises with the
1735 // future versions, don't rely on this and still do everything ourselves.
1736 // To avoid that the messages be sent twice when the item is expanded for
1737 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1739 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1743 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1745 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1746 event
.m_item
= item
;
1747 event
.SetEventObject(this);
1749 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1751 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1755 (void)GetEventHandler()->ProcessEvent(event
);
1757 //else: change didn't took place, so do nothing at all
1760 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1762 DoExpand(item
, TVE_EXPAND
);
1765 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1767 DoExpand(item
, TVE_COLLAPSE
);
1770 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1772 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1775 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1777 DoExpand(item
, TVE_TOGGLE
);
1780 #if WXWIN_COMPATIBILITY_2_4
1781 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1783 DoExpand(item
, action
);
1787 void wxTreeCtrl::Unselect()
1789 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1790 wxT("doesn't make sense, may be you want UnselectAll()?") );
1792 // just remove the selection
1793 SelectItem(wxTreeItemId());
1796 void wxTreeCtrl::UnselectAll()
1798 if ( m_windowStyle
& wxTR_MULTIPLE
)
1800 wxArrayTreeItemIds selections
;
1801 size_t count
= GetSelections(selections
);
1802 for ( size_t n
= 0; n
< count
; n
++ )
1804 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1805 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1806 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1807 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1808 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1813 // just remove the selection
1818 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1820 if ( m_windowStyle
& wxTR_MULTIPLE
)
1822 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1823 // selecting the item means checking it
1825 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1826 ::SelectItem(GetHwnd(), HITEM(item
));
1827 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1831 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1832 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1833 // send them ourselves
1835 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1836 event
.m_item
= item
;
1837 event
.SetEventObject(this);
1839 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1840 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1842 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1844 wxLogLastError(wxT("TreeView_SelectItem"));
1848 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1849 (void)GetEventHandler()->ProcessEvent(event
);
1852 //else: program vetoed the change
1856 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1859 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1862 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1864 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1866 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1870 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1875 void wxTreeCtrl::DeleteTextCtrl()
1879 // the HWND corresponding to this control is deleted by the tree
1880 // control itself and we don't know when exactly this happens, so check
1881 // if the window still exists before calling UnsubclassWin()
1882 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1884 m_textCtrl
->SetHWND(0);
1887 m_textCtrl
->UnsubclassWin();
1888 m_textCtrl
->SetHWND(0);
1894 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1895 wxClassInfo
* textControlClass
)
1897 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1901 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1902 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1904 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1913 // textctrl is subclassed in MSWOnNotify
1917 // End label editing, optionally cancelling the edit
1918 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1920 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1925 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1927 TV_HITTESTINFO hitTestInfo
;
1928 hitTestInfo
.pt
.x
= (int)point
.x
;
1929 hitTestInfo
.pt
.y
= (int)point
.y
;
1931 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1936 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1937 flags |= wxTREE_HITTEST_##flag
1939 TRANSLATE_FLAG(ABOVE
);
1940 TRANSLATE_FLAG(BELOW
);
1941 TRANSLATE_FLAG(NOWHERE
);
1942 TRANSLATE_FLAG(ONITEMBUTTON
);
1943 TRANSLATE_FLAG(ONITEMICON
);
1944 TRANSLATE_FLAG(ONITEMINDENT
);
1945 TRANSLATE_FLAG(ONITEMLABEL
);
1946 TRANSLATE_FLAG(ONITEMRIGHT
);
1947 TRANSLATE_FLAG(ONITEMSTATEICON
);
1948 TRANSLATE_FLAG(TOLEFT
);
1949 TRANSLATE_FLAG(TORIGHT
);
1951 #undef TRANSLATE_FLAG
1953 return wxTreeItemId(hitTestInfo
.hItem
);
1956 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1958 bool textOnly
) const
1962 // Virtual root items have no bounding rectangle
1963 if ( IS_VIRTUAL_ROOT(item
) )
1968 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1971 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1977 // couldn't retrieve rect: for example, item isn't visible
1982 // ----------------------------------------------------------------------------
1984 // ----------------------------------------------------------------------------
1986 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1987 // functions such as IsDataIndirect()
1988 class wxTreeSortHelper
1991 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1994 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1996 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1997 if ( tree
->IsDataIndirect(data
) )
1999 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2002 return data
->GetId();
2006 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2010 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2011 wxT("sorting tree without data doesn't make sense") );
2013 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2015 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2016 GetIdFromData(tree
, pItem2
));
2019 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2020 const wxTreeItemId
& item2
)
2022 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2025 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2027 // rely on the fact that TreeView_SortChildren does the same thing as our
2028 // default behaviour, i.e. sorts items alphabetically and so call it
2029 // directly if we're not in derived class (much more efficient!)
2030 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2032 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2037 tvSort
.hParent
= HITEM(item
);
2038 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2039 tvSort
.lParam
= (LPARAM
)this;
2040 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2044 // ----------------------------------------------------------------------------
2046 // ----------------------------------------------------------------------------
2048 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2050 if ( cmd
== EN_UPDATE
)
2052 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2053 event
.SetEventObject( this );
2054 ProcessCommand(event
);
2056 else if ( cmd
== EN_KILLFOCUS
)
2058 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2059 event
.SetEventObject( this );
2060 ProcessCommand(event
);
2068 // command processed
2072 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2073 // only do it during dragging, minimize wxWin overhead (this is important for
2074 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2075 // instead of passing by wxWin events
2076 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2078 bool processed
= false;
2080 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2082 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2084 // we only process mouse messages here and these parameters have the
2085 // same meaning for all of them
2086 int x
= GET_X_LPARAM(lParam
),
2087 y
= GET_Y_LPARAM(lParam
);
2088 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2092 case WM_RBUTTONDOWN
:
2093 // if the item we are about to right click on
2094 // is not already select, remove the entire
2095 // previous selection
2096 if (!::IsItemSelected(GetHwnd(), htItem
))
2101 // select item and set the focus to the
2102 // newly selected item
2103 ::SelectItem(GetHwnd(), htItem
);
2104 ::SetFocus(GetHwnd(), htItem
);
2107 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2108 case WM_LBUTTONDOWN
:
2109 if ( htItem
&& isMultiple
)
2111 if ( wParam
& MK_CONTROL
)
2115 // toggle selected state
2116 ToggleItemSelection(GetHwnd(), htItem
);
2118 ::SetFocus(GetHwnd(), htItem
);
2120 // reset on any click without Shift
2121 m_htSelStart
.Unset();
2125 else if ( wParam
& MK_SHIFT
)
2127 // this selects all items between the starting one and
2130 if ( !m_htSelStart
)
2132 // take the focused item
2133 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2136 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2137 !(wParam
& MK_CONTROL
));
2139 ::SetFocus(GetHwnd(), htItem
);
2143 else // normal click
2145 // avoid doing anything if we click on the only
2146 // currently selected item
2148 wxArrayTreeItemIds selections
;
2149 size_t count
= GetSelections(selections
);
2152 HITEM_PTR(selections
[0]) != htItem
)
2154 // clear the previously selected items, if the
2155 // user clicked outside of the present selection.
2156 // otherwise, perform the deselection on mouse-up.
2157 // this allows multiple drag and drop to work.
2159 if (IsItemSelected(GetHwnd(), htItem
))
2161 ::SetFocus(GetHwnd(), htItem
);
2167 // prevent the click from starting in-place editing
2168 // which should only happen if we click on the
2169 // already selected item (and nothing else is
2172 TreeView_SelectItem(GetHwnd(), 0);
2173 ::SelectItem(GetHwnd(), htItem
);
2177 // reset on any click without Shift
2178 m_htSelStart
.Unset();
2182 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2187 m_dragImage
->Move(wxPoint(x
, y
));
2190 // highlight the item as target (hiding drag image is
2191 // necessary - otherwise the display will be corrupted)
2192 m_dragImage
->Hide();
2193 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2194 m_dragImage
->Show();
2201 // facilitates multiple drag-and-drop
2202 if (htItem
&& isMultiple
)
2204 wxArrayTreeItemIds selections
;
2205 size_t count
= GetSelections(selections
);
2208 !(wParam
& MK_CONTROL
) &&
2209 !(wParam
& MK_SHIFT
))
2212 TreeView_SelectItem(GetHwnd(), htItem
);
2221 m_dragImage
->EndDrag();
2225 // generate the drag end event
2226 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2228 event
.m_item
= htItem
;
2229 event
.m_pointDrag
= wxPoint(x
, y
);
2230 event
.SetEventObject(this);
2232 (void)GetEventHandler()->ProcessEvent(event
);
2234 // if we don't do it, the tree seems to think that 2 items
2235 // are selected simultaneously which is quite weird
2236 TreeView_SelectDropTarget(GetHwnd(), 0);
2241 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2242 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2244 // the tree control greys out the selected item when it loses focus and
2245 // paints it as selected again when it regains it, but it won't do it
2246 // for the other items itself - help it
2247 wxArrayTreeItemIds selections
;
2248 size_t count
= GetSelections(selections
);
2250 for ( size_t n
= 0; n
< count
; n
++ )
2252 // TreeView_GetItemRect() will return false if item is not visible,
2253 // which may happen perfectly well
2254 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2257 ::InvalidateRect(GetHwnd(), &rect
, false);
2261 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2263 bool bCtrl
= wxIsCtrlDown(),
2264 bShift
= wxIsShiftDown();
2266 // we handle.arrows and space, but not page up/down and home/end: the
2267 // latter should be easy, but not the former
2269 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2270 if ( !m_htSelStart
)
2272 m_htSelStart
= htSel
;
2275 if ( wParam
== VK_SPACE
)
2279 ToggleItemSelection(GetHwnd(), htSel
);
2285 ::SelectItem(GetHwnd(), htSel
);
2290 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2292 if ( !bCtrl
&& !bShift
)
2294 // no modifiers, just clear selection and then let the default
2295 // processing to take place
2300 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2302 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2303 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2304 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2308 // at the top/bottom
2314 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2318 // without changing selection
2319 ::SetFocus(GetHwnd(), htNext
);
2326 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2327 else if ( nMsg
== WM_CHAR
)
2329 // don't let the control process Space and Return keys because it
2330 // doesn't do anything useful with them anyhow but always beeps
2331 // annoyingly when it receives them and there is no way to turn it off
2332 // simply if you just process TREEITEM_ACTIVATED event to which Space
2333 // and Enter presses are mapped in your code
2334 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2341 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2346 // process WM_NOTIFY Windows message
2347 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2349 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2350 wxEventType eventType
= wxEVT_NULL
;
2351 NMHDR
*hdr
= (NMHDR
*)lParam
;
2353 switch ( hdr
->code
)
2356 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2359 case TVN_BEGINRDRAG
:
2361 if ( eventType
== wxEVT_NULL
)
2362 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2363 //else: left drag, already set above
2365 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2367 event
.m_item
= tv
->itemNew
.hItem
;
2368 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2370 // don't allow dragging by default: the user code must
2371 // explicitly say that it wants to allow it to avoid breaking
2377 case TVN_BEGINLABELEDIT
:
2379 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2380 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2382 event
.m_item
= info
->item
.hItem
;
2383 event
.m_label
= info
->item
.pszText
;
2384 event
.m_editCancelled
= false;
2388 case TVN_DELETEITEM
:
2390 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2391 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2393 event
.m_item
= tv
->itemOld
.hItem
;
2397 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2398 if ( it
!= m_attrs
.end() )
2407 case TVN_ENDLABELEDIT
:
2409 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2410 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2412 event
.m_item
= info
->item
.hItem
;
2413 event
.m_label
= info
->item
.pszText
;
2414 if (info
->item
.pszText
== NULL
)
2416 event
.m_editCancelled
= true;
2420 event
.m_editCancelled
= false;
2425 case TVN_GETDISPINFO
:
2426 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2429 case TVN_SETDISPINFO
:
2431 if ( eventType
== wxEVT_NULL
)
2432 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2433 //else: get, already set above
2435 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2437 event
.m_item
= info
->item
.hItem
;
2441 case TVN_ITEMEXPANDING
:
2442 case TVN_ITEMEXPANDED
:
2444 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2447 switch ( tv
->action
)
2450 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2458 what
= IDX_COLLAPSE
;
2462 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2465 eventType
= gs_expandEvents
[what
][how
];
2467 event
.m_item
= tv
->itemNew
.hItem
;
2473 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2474 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2476 // fabricate the lParam and wParam parameters sufficiently
2477 // similar to the ones from a "real" WM_KEYDOWN so that
2478 // CreateKeyEvent() works correctly
2480 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2482 WXWPARAM wParam
= info
->wVKey
;
2484 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2487 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2492 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2497 // a separate event for Space/Return
2498 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2499 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2501 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2503 event2
.SetEventObject(this);
2504 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2506 event2
.m_item
= GetSelection();
2508 //else: don't know how to get it
2510 (void)GetEventHandler()->ProcessEvent(event2
);
2515 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2516 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2517 // we have to handle both messages:
2518 case TVN_SELCHANGEDA
:
2519 case TVN_SELCHANGEDW
:
2520 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2523 case TVN_SELCHANGINGA
:
2524 case TVN_SELCHANGINGW
:
2526 if ( eventType
== wxEVT_NULL
)
2527 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2528 //else: already set above
2530 if (hdr
->code
== TVN_SELCHANGINGW
||
2531 hdr
->code
== TVN_SELCHANGEDW
)
2533 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2534 event
.m_item
= tv
->itemNew
.hItem
;
2535 event
.m_itemOld
= tv
->itemOld
.hItem
;
2539 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2540 event
.m_item
= tv
->itemNew
.hItem
;
2541 event
.m_itemOld
= tv
->itemOld
.hItem
;
2546 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2549 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2550 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2551 switch ( nmcd
.dwDrawStage
)
2554 // if we've got any items with non standard attributes,
2555 // notify us before painting each item
2556 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2560 case CDDS_ITEMPREPAINT
:
2562 wxMapTreeAttr::iterator
2563 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2565 if ( it
== m_attrs
.end() )
2567 // nothing to do for this item
2568 *result
= CDRF_DODEFAULT
;
2572 wxTreeItemAttr
* const attr
= it
->second
;
2575 if ( attr
->HasFont() )
2577 hFont
= GetHfontOf(attr
->GetFont());
2585 if ( attr
->HasTextColour() )
2587 colText
= attr
->GetTextColour();
2591 colText
= GetForegroundColour();
2594 // selection colours should override ours
2595 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2598 ::GetSysColor(COLOR_HIGHLIGHT
);
2600 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2605 if ( attr
->HasBackgroundColour() )
2607 colBack
= attr
->GetBackgroundColour();
2611 colBack
= GetBackgroundColour();
2614 lptvcd
->clrText
= wxColourToRGB(colText
);
2615 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2618 // note that if we wanted to set colours for
2619 // individual columns (subitems), we would have
2620 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2623 ::SelectObject(nmcd
.hdc
, hFont
);
2625 *result
= CDRF_NEWFONT
;
2629 *result
= CDRF_DODEFAULT
;
2635 *result
= CDRF_DODEFAULT
;
2639 // we always process it
2641 #endif // _WIN32_IE >= 0x300
2645 DWORD pos
= GetMessagePos();
2647 point
.x
= LOWORD(pos
);
2648 point
.y
= HIWORD(pos
);
2649 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2651 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2652 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2654 event
.m_item
= item
;
2655 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2663 TV_HITTESTINFO tvhti
;
2664 ::GetCursorPos(&tvhti
.pt
);
2665 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2666 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2668 if ( tvhti
.flags
& TVHT_ONITEM
)
2670 event
.m_item
= tvhti
.hItem
;
2671 eventType
= (int)hdr
->code
== NM_DBLCLK
2672 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2673 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2675 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2676 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2685 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2688 event
.SetEventObject(this);
2689 event
.SetEventType(eventType
);
2691 bool processed
= GetEventHandler()->ProcessEvent(event
);
2694 switch ( hdr
->code
)
2697 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2698 // the return code of this event handler as the return value for
2699 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2700 // expanded status would never work
2705 case TVN_BEGINRDRAG
:
2706 if ( event
.IsAllowed() )
2708 // normally this is impossible because the m_dragImage is
2709 // deleted once the drag operation is over
2710 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2712 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2713 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2714 m_dragImage
->Show();
2718 case TVN_DELETEITEM
:
2720 // NB: we might process this message using wxWindows event
2721 // tables, but due to overhead of wxWin event system we
2722 // prefer to do it here ourself (otherwise deleting a tree
2723 // with many items is just too slow)
2724 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2726 wxTreeItemId item
= event
.m_item
;
2727 if ( HasIndirectData(item
) )
2729 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2731 delete data
; // can't be NULL here
2735 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2736 delete data
; // may be NULL, ok
2739 processed
= true; // Make sure we don't get called twice
2743 case TVN_BEGINLABELEDIT
:
2744 // return true to cancel label editing
2745 *result
= !event
.IsAllowed();
2746 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2747 if(event
.IsAllowed())
2749 HWND hText
= TreeView_GetEditControl(GetHwnd());
2752 // MBN: if m_textCtrl already has an HWND, it is a stale
2753 // pointer from a previous edit (because the user
2754 // didn't modify the label before dismissing the control,
2755 // and TVN_ENDLABELEDIT was not sent), so delete it
2756 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2759 m_textCtrl
= new wxTextCtrl();
2760 m_textCtrl
->SetParent(this);
2761 m_textCtrl
->SetHWND((WXHWND
)hText
);
2762 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2764 // set wxTE_PROCESS_ENTER style for the text control to
2765 // force it to process the Enter presses itself, otherwise
2766 // they could be stolen from it by the dialog
2768 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2769 | wxTE_PROCESS_ENTER
);
2774 case TVN_ENDLABELEDIT
:
2775 // return true to set the label to the new string: note that we
2776 // also must pretend that we did process the message or it is going
2777 // to be passed to DefWindowProc() which will happily return false
2778 // cancelling the label change
2779 *result
= event
.IsAllowed();
2782 // ensure that we don't have the text ctrl which is going to be
2787 case TVN_SELCHANGING
:
2788 case TVN_ITEMEXPANDING
:
2789 // return true to prevent the action from happening
2790 *result
= !event
.IsAllowed();
2793 case TVN_ITEMEXPANDED
:
2794 // the item is not refreshed properly after expansion when it has
2795 // an image depending on the expanded/collapsed state - bug in
2796 // comctl32.dll or our code?
2798 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2799 wxTreeItemId
id(tv
->itemNew
.hItem
);
2801 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2809 case TVN_GETDISPINFO
:
2810 // NB: so far the user can't set the image himself anyhow, so do it
2811 // anyway - but this may change later
2812 //if ( /* !processed && */ 1 )
2814 wxTreeItemId item
= event
.m_item
;
2815 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2816 if ( info
->item
.mask
& TVIF_IMAGE
)
2819 DoGetItemImageFromData
2822 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2823 : wxTreeItemIcon_Normal
2826 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2828 info
->item
.iSelectedImage
=
2829 DoGetItemImageFromData
2832 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2833 : wxTreeItemIcon_Selected
2840 // for the other messages the return value is ignored and there is
2841 // nothing special to do
2846 // ----------------------------------------------------------------------------
2848 // ----------------------------------------------------------------------------
2850 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2851 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2853 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2856 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2857 tvi
.mask
= TVIF_STATE
;
2858 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2860 // Select the specified state, or -1 == cycle to the next one.
2863 TreeView_GetItem(GetHwnd(), &tvi
);
2865 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2866 if ( state
== m_imageListState
->GetImageCount() )
2870 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2871 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2873 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2875 TreeView_SetItem(GetHwnd(), &tvi
);
2878 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2881 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2882 tvi
.mask
= TVIF_STATE
;
2883 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2884 TreeView_GetItem(GetHwnd(), &tvi
);
2886 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2889 #endif // wxUSE_TREECTRL