1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/msw/private.h"
35 // Set this to 1 to be _absolutely_ sure that repainting will work for all
36 // comctl32.dll versions
37 #define wxUSE_COMCTL32_SAFELY 0
39 // Mingw32 is a bit mental even though this is done in winundef
48 #if defined(__WIN95__)
52 #include "wx/dynarray.h"
53 #include "wx/imaglist.h"
54 #include "wx/settings.h"
55 #include "wx/msw/treectrl.h"
56 #include "wx/msw/dragimag.h"
58 #ifdef __GNUWIN32_OLD__
59 #include "wx/msw/gnuwin32/extra.h"
62 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
66 // Bug in headers, sometimes
68 #define TVIS_FOCUSED 0x0001
72 #define TV_FIRST 0x1100
75 #ifndef TVS_CHECKBOXES
76 #define TVS_CHECKBOXES 0x0100
79 #ifndef TVS_FULLROWSELECT
80 #define TVS_FULLROWSELECT 0x1000
83 // old headers might miss these messages (comctl32.dll 4.71+ only)
84 #ifndef TVM_SETBKCOLOR
85 #define TVM_SETBKCOLOR (TV_FIRST + 29)
86 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
89 // a macro to hide the ugliness of nested casts
90 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
92 // the native control doesn't support multiple selections under MSW and we
93 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
94 // checkboxes be the selection status (checked == selected) or by really
95 // emulating everything, i.e. intercepting mouse and key events &c. The first
96 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
98 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
104 // wrapper for TreeView_HitTest
105 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
111 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
114 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
116 // wrappers for TreeView_GetItem/TreeView_SetItem
117 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
121 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
122 tvi
.stateMask
= TVIS_SELECTED
;
125 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
127 wxLogLastError(wxT("TreeView_GetItem"));
130 return (tvi
.state
& TVIS_SELECTED
) != 0;
133 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
136 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
137 tvi
.stateMask
= TVIS_SELECTED
;
138 tvi
.state
= select
? TVIS_SELECTED
: 0;
141 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
143 wxLogLastError(wxT("TreeView_SetItem"));
147 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
149 SelectItem(hwndTV
, htItem
, FALSE
);
152 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
154 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
157 // helper function which selects all items in a range and, optionally,
158 // unselects all others
159 static void SelectRange(HWND hwndTV
,
162 bool unselectOthers
= TRUE
)
164 // find the first (or last) item and select it
166 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
167 while ( htItem
&& cont
)
169 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
171 if ( !IsItemSelected(hwndTV
, htItem
) )
173 SelectItem(hwndTV
, htItem
);
180 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
182 UnselectItem(hwndTV
, htItem
);
186 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
189 // select the items in range
190 cont
= htFirst
!= htLast
;
191 while ( htItem
&& cont
)
193 if ( !IsItemSelected(hwndTV
, htItem
) )
195 SelectItem(hwndTV
, htItem
);
198 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
200 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
204 if ( unselectOthers
)
208 if ( IsItemSelected(hwndTV
, htItem
) )
210 UnselectItem(hwndTV
, htItem
);
213 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
217 // seems to be necessary - otherwise the just selected items don't always
218 // appear as selected
219 UpdateWindow(hwndTV
);
222 // helper function which tricks the standard control into changing the focused
223 // item without changing anything else (if someone knows why Microsoft doesn't
224 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
225 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
228 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
233 if ( htItem
!= htFocus
)
235 // remember the selection state of the item
236 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
238 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
240 // prevent the tree from unselecting the old focus which it
241 // would do by default (TreeView_SelectItem unselects the
243 TreeView_SelectItem(hwndTV
, 0);
244 SelectItem(hwndTV
, htFocus
);
247 TreeView_SelectItem(hwndTV
, htItem
);
251 // need to clear the selection which TreeView_SelectItem() gave
253 UnselectItem(hwndTV
, htItem
);
255 //else: was selected, still selected - ok
257 //else: nothing to do, focus already there
263 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
265 // just clear the focus
266 TreeView_SelectItem(hwndTV
, 0);
268 if ( wasFocusSelected
)
270 // restore the selection state
271 SelectItem(hwndTV
, htFocus
);
274 //else: nothing to do, no focus already
278 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
280 // ----------------------------------------------------------------------------
282 // ----------------------------------------------------------------------------
284 // a convenient wrapper around TV_ITEM struct which adds a ctor
286 #pragma warning( disable : 4097 ) // inheriting from typedef
289 struct wxTreeViewItem
: public TV_ITEM
291 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
292 UINT mask_
, // fields which are valid
293 UINT stateMask_
= 0) // for TVIF_STATE only
297 // hItem member is always valid
298 mask
= mask_
| TVIF_HANDLE
;
299 stateMask
= stateMask_
;
304 // wxVirutalNode is used in place of a single root when 'hidden' root is
306 class wxVirtualNode
: public wxTreeViewItem
309 wxVirtualNode(wxTreeItemData
*data
)
310 : wxTreeViewItem(TVI_ROOT
, 0)
320 wxTreeItemData
*GetData() const { return m_data
; }
321 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
324 wxTreeItemData
*m_data
;
328 #pragma warning( default : 4097 )
331 // a macro to get the virtual root, returns NULL if none
332 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
334 // returns TRUE if the item is the virtual root
335 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
337 // a class which encapsulates the tree traversal logic: it vists all (unless
338 // OnVisit() returns FALSE) items under the given one
339 class wxTreeTraversal
342 wxTreeTraversal(const wxTreeCtrl
*tree
)
347 // do traverse the tree: visit all items (recursively by default) under the
348 // given one; return TRUE if all items were traversed or FALSE if the
349 // traversal was aborted because OnVisit returned FALSE
350 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
352 // override this function to do whatever is needed for each item, return
353 // FALSE to stop traversing
354 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
357 const wxTreeCtrl
*GetTree() const { return m_tree
; }
360 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
362 const wxTreeCtrl
*m_tree
;
365 // internal class for getting the selected items
366 class TraverseSelections
: public wxTreeTraversal
369 TraverseSelections(const wxTreeCtrl
*tree
,
370 wxArrayTreeItemIds
& selections
)
371 : wxTreeTraversal(tree
), m_selections(selections
)
373 m_selections
.Empty();
375 DoTraverse(tree
->GetRootItem());
378 virtual bool OnVisit(const wxTreeItemId
& item
)
380 // can't visit a virtual node.
381 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
386 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
387 if ( GetTree()->IsItemChecked(item
) )
389 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
392 m_selections
.Add(item
);
398 size_t GetCount() const { return m_selections
.GetCount(); }
401 wxArrayTreeItemIds
& m_selections
;
404 // internal class for counting tree items
405 class TraverseCounter
: public wxTreeTraversal
408 TraverseCounter(const wxTreeCtrl
*tree
,
409 const wxTreeItemId
& root
,
411 : wxTreeTraversal(tree
)
415 DoTraverse(root
, recursively
);
418 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
425 size_t GetCount() const { return m_count
; }
431 // ----------------------------------------------------------------------------
432 // This class is needed for support of different images: the Win32 common
433 // control natively supports only 2 images (the normal one and another for the
434 // selected state). We wish to provide support for 2 more of them for folder
435 // items (i.e. those which have children): for expanded state and for expanded
436 // selected state. For this we use this structure to store the additional items
439 // There is only one problem with this: when we retrieve the item's data, we
440 // don't know whether we get a pointer to wxTreeItemData or
441 // wxTreeItemIndirectData. So we always set the item id to an invalid value
442 // in this class and the code using the client data checks for it and retrieves
443 // the real client data in this case.
444 // ----------------------------------------------------------------------------
446 class wxTreeItemIndirectData
: public wxTreeItemData
449 // ctor associates this data with the item and the real item data becomes
450 // available through our GetData() method
451 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
453 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
459 m_data
= tree
->GetItemData(item
);
461 // and set ourselves as the new one
462 tree
->SetIndirectItemData(item
, this);
464 // we must have the invalid value for the item
468 // dtor deletes the associated data as well
469 virtual ~wxTreeItemIndirectData() { delete m_data
; }
472 // get the real data associated with the item
473 wxTreeItemData
*GetData() const { return m_data
; }
475 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
477 // do we have such image?
478 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
480 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
482 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
485 // all the images associated with the item
486 int m_images
[wxTreeItemIcon_Max
];
488 // the real client data
489 wxTreeItemData
*m_data
;
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 // indices in gs_expandEvents table below
517 // handy table for sending events - it has to be initialized during run-time
518 // now so can't be const any more
519 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
522 but logically it's a const table with the following entries:
525 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
526 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
530 // ============================================================================
532 // ============================================================================
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
540 if ( !OnVisit(root
) )
543 return Traverse(root
, recursively
);
546 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
549 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
550 while ( child
.IsOk() )
552 // depth first traversal
553 if ( recursively
&& !Traverse(child
, TRUE
) )
556 if ( !OnVisit(child
) )
559 child
= m_tree
->GetNextChild(root
, cookie
);
565 // ----------------------------------------------------------------------------
566 // construction and destruction
567 // ----------------------------------------------------------------------------
569 void wxTreeCtrl::Init()
571 m_imageListNormal
= NULL
;
572 m_imageListState
= NULL
;
573 m_ownsImageListNormal
= m_ownsImageListState
= FALSE
;
575 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 ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
601 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
604 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
605 wstyle
|= WS_CLIPSIBLINGS
;
607 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
608 wstyle
|= TVS_HASLINES
;
609 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
610 wstyle
|= TVS_HASBUTTONS
;
612 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
613 wstyle
|= TVS_EDITLABELS
;
615 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
616 wstyle
|= TVS_LINESATROOT
;
618 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
620 if ( wxTheApp
->GetComCtl32Version() >= 471 )
621 wstyle
|= TVS_FULLROWSELECT
;
624 // using TVS_CHECKBOXES for emulation of a multiselection tree control
625 // doesn't work without the new enough headers
626 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
627 !defined( __GNUWIN32_OLD__ ) && \
628 !defined( __BORLANDC__ ) && \
629 !defined( __WATCOMC__ ) && \
630 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
632 // we emulate the multiple selection tree controls by using checkboxes: set
633 // up the image list we need for this if we do have multiple selections
634 if ( m_windowStyle
& wxTR_MULTIPLE
)
635 wstyle
|= TVS_CHECKBOXES
;
636 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
638 // Create the tree control.
639 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
642 #if wxUSE_COMCTL32_SAFELY
643 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
644 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
646 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
647 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
649 // This works around a bug in the Windows tree control whereby for some versions
650 // of comctrl32, setting any colour actually draws the background in black.
651 // This will initialise the background to the system colour.
652 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
653 // Assume the user has an updated comctl32.dll.
654 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
655 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
656 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
660 // VZ: this is some experimental code which may be used to get the
661 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
662 // AFAIK, the standard DLL does about the same thing anyhow.
664 if ( m_windowStyle
& wxTR_MULTIPLE
)
668 // create the DC compatible with the current screen
669 HDC hdcMem
= CreateCompatibleDC(NULL
);
671 // create a mono bitmap of the standard size
672 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
673 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
674 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
675 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
676 1, // # of color planes
677 1, // # bits needed for one pixel
678 0); // array containing colour data
679 SelectObject(hdcMem
, hbmpCheck
);
681 // then draw a check mark into it
682 RECT rect
= { 0, 0, x
, y
};
683 if ( !::DrawFrameControl(hdcMem
, &rect
,
685 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
687 wxLogLastError(wxT("DrawFrameControl(check)"));
690 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
691 imagelistCheckboxes
.Add(bmp
);
693 if ( !::DrawFrameControl(hdcMem
, &rect
,
697 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
700 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
701 imagelistCheckboxes
.Add(bmp
);
707 SetStateImageList(&imagelistCheckboxes
);
711 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
716 wxTreeCtrl::~wxTreeCtrl()
718 // delete any attributes
721 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
723 delete (wxTreeItemAttr
*)node
->Data();
726 // prevent TVN_DELETEITEM handler from deleting the attributes again!
727 m_hasAnyAttr
= FALSE
;
732 // delete user data to prevent memory leaks
733 // also deletes hidden root node storage.
736 if (m_ownsImageListNormal
) delete m_imageListNormal
;
737 if (m_ownsImageListState
) delete m_imageListState
;
740 // ----------------------------------------------------------------------------
742 // ----------------------------------------------------------------------------
744 // simple wrappers which add error checking in debug mode
746 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
748 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, FALSE
,
749 _T("can't retrieve virtual root item") );
751 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
753 wxLogLastError(wxT("TreeView_GetItem"));
761 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
763 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
765 wxLogLastError(wxT("TreeView_SetItem"));
769 size_t wxTreeCtrl::GetCount() const
771 return (size_t)TreeView_GetCount(GetHwnd());
774 unsigned int wxTreeCtrl::GetIndent() const
776 return TreeView_GetIndent(GetHwnd());
779 void wxTreeCtrl::SetIndent(unsigned int indent
)
781 TreeView_SetIndent(GetHwnd(), indent
);
784 wxImageList
*wxTreeCtrl::GetImageList() const
786 return m_imageListNormal
;
789 wxImageList
*wxTreeCtrl::GetStateImageList() const
791 return m_imageListNormal
;
794 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
797 TreeView_SetImageList(GetHwnd(),
798 imageList
? imageList
->GetHIMAGELIST() : 0,
802 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
804 if (m_ownsImageListNormal
)
805 delete m_imageListNormal
;
807 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
808 m_ownsImageListNormal
= FALSE
;
811 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
813 if (m_ownsImageListState
) delete m_imageListState
;
814 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
815 m_ownsImageListState
= FALSE
;
818 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
820 SetImageList(imageList
);
821 m_ownsImageListNormal
= TRUE
;
824 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
826 SetStateImageList(imageList
);
827 m_ownsImageListState
= TRUE
;
830 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
831 bool recursively
) const
833 TraverseCounter
counter(this, item
, recursively
);
835 return counter
.GetCount() - 1;
838 // ----------------------------------------------------------------------------
840 // ----------------------------------------------------------------------------
842 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
844 #if !wxUSE_COMCTL32_SAFELY
845 if ( !wxWindowBase::SetBackgroundColour(colour
) )
848 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
854 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
856 #if !wxUSE_COMCTL32_SAFELY
857 if ( !wxWindowBase::SetForegroundColour(colour
) )
860 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
866 // ----------------------------------------------------------------------------
868 // ----------------------------------------------------------------------------
870 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
872 wxChar buf
[512]; // the size is arbitrary...
874 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
875 tvItem
.pszText
= buf
;
876 tvItem
.cchTextMax
= WXSIZEOF(buf
);
877 if ( !DoGetItem(&tvItem
) )
879 // don't return some garbage which was on stack, but an empty string
883 return wxString(buf
);
886 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
888 if ( IS_VIRTUAL_ROOT(item
) )
891 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
892 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
895 // when setting the text of the item being edited, the text control should
896 // be updated to reflect the new text as well, otherwise calling
897 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
899 // don't use GetEditControl() here because m_textCtrl is not set yet
900 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
903 if ( item
== GetSelection() )
905 ::SetWindowText(hwndEdit
, text
);
910 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
911 wxTreeItemIcon which
) const
913 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
914 if ( !DoGetItem(&tvItem
) )
919 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
922 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
924 wxTreeItemIcon which
) const
926 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
927 if ( !DoGetItem(&tvItem
) )
932 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
934 data
->SetImage(image
, which
);
936 // make sure that we have selected images as well
937 if ( which
== wxTreeItemIcon_Normal
&&
938 !data
->HasImage(wxTreeItemIcon_Selected
) )
940 data
->SetImage(image
, wxTreeItemIcon_Selected
);
943 if ( which
== wxTreeItemIcon_Expanded
&&
944 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
946 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
950 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
954 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
955 tvItem
.iSelectedImage
= imageSel
;
956 tvItem
.iImage
= image
;
960 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
961 wxTreeItemIcon which
) const
963 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
965 // TODO: Maybe a hidden root can still provide images?
969 if ( HasIndirectData(item
) )
971 return DoGetItemImageFromData(item
, which
);
978 wxFAIL_MSG( wxT("unknown tree item image type") );
980 case wxTreeItemIcon_Normal
:
984 case wxTreeItemIcon_Selected
:
985 mask
= TVIF_SELECTEDIMAGE
;
988 case wxTreeItemIcon_Expanded
:
989 case wxTreeItemIcon_SelectedExpanded
:
993 wxTreeViewItem
tvItem(item
, mask
);
996 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
999 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1000 wxTreeItemIcon which
)
1002 if ( IS_VIRTUAL_ROOT(item
) )
1004 // TODO: Maybe a hidden root can still store images?
1008 int imageNormal
, imageSel
;
1012 wxFAIL_MSG( wxT("unknown tree item image type") );
1014 case wxTreeItemIcon_Normal
:
1015 imageNormal
= image
;
1016 imageSel
= GetItemSelectedImage(item
);
1019 case wxTreeItemIcon_Selected
:
1020 imageNormal
= GetItemImage(item
);
1024 case wxTreeItemIcon_Expanded
:
1025 case wxTreeItemIcon_SelectedExpanded
:
1026 if ( !HasIndirectData(item
) )
1028 // we need to get the old images first, because after we create
1029 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1031 imageNormal
= GetItemImage(item
);
1032 imageSel
= GetItemSelectedImage(item
);
1034 // if it doesn't have it yet, add it
1035 wxTreeItemIndirectData
*data
= new
1036 wxTreeItemIndirectData(this, item
);
1038 // copy the data to the new location
1039 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1040 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1043 DoSetItemImageFromData(item
, image
, which
);
1045 // reset the normal/selected images because we won't use them any
1046 // more - now they're stored inside the indirect data
1048 imageSel
= I_IMAGECALLBACK
;
1052 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1053 // change both normal and selected image - otherwise the change simply
1054 // doesn't take place!
1055 DoSetItemImages(item
, imageNormal
, imageSel
);
1058 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1060 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1062 // Hidden root may have data.
1063 if ( IS_VIRTUAL_ROOT(item
) )
1065 return GET_VIRTUAL_ROOT()->GetData();
1069 if ( !DoGetItem(&tvItem
) )
1074 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1075 if ( IsDataIndirect(data
) )
1077 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1083 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1085 if ( IS_VIRTUAL_ROOT(item
) )
1087 GET_VIRTUAL_ROOT()->SetData(data
);
1090 // first, associate this piece of data with this item
1096 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1098 if ( HasIndirectData(item
) )
1100 if ( DoGetItem(&tvItem
) )
1102 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1106 wxFAIL_MSG( wxT("failed to change tree items data") );
1111 tvItem
.lParam
= (LPARAM
)data
;
1116 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1117 wxTreeItemIndirectData
*data
)
1119 // this should never happen because it's unnecessary and will probably lead
1120 // to crash too because the code elsewhere supposes that the pointer the
1121 // wxTreeItemIndirectData has is a real wxItemData and not
1122 // wxTreeItemIndirectData as well
1123 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1125 SetItemData(item
, data
);
1128 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1130 // query the item itself
1131 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1132 if ( !DoGetItem(&tvItem
) )
1137 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1139 return data
&& IsDataIndirect(data
);
1142 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1144 if ( IS_VIRTUAL_ROOT(item
) )
1147 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1148 tvItem
.cChildren
= (int)has
;
1152 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1154 if ( IS_VIRTUAL_ROOT(item
) )
1157 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1158 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1162 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1164 if ( IS_VIRTUAL_ROOT(item
) )
1167 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1168 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1172 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1174 if ( IS_VIRTUAL_ROOT(item
) )
1178 if ( GetBoundingRect(item
, rect
) )
1184 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1186 long id
= (long)(WXHTREEITEM
)item
;
1187 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1190 return wxNullColour
;
1193 return attr
->GetTextColour();
1196 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1198 long id
= (long)(WXHTREEITEM
)item
;
1199 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1202 return wxNullColour
;
1205 return attr
->GetBackgroundColour();
1208 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1210 long id
= (long)(WXHTREEITEM
)item
;
1211 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1217 return attr
->GetFont();
1220 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1221 const wxColour
& col
)
1223 m_hasAnyAttr
= TRUE
;
1225 long id
= (long)(WXHTREEITEM
)item
;
1226 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1229 attr
= new wxTreeItemAttr
;
1230 m_attrs
.Put(id
, (wxObject
*)attr
);
1233 attr
->SetTextColour(col
);
1238 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1239 const wxColour
& col
)
1241 m_hasAnyAttr
= TRUE
;
1243 long id
= (long)(WXHTREEITEM
)item
;
1244 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1247 attr
= new wxTreeItemAttr
;
1248 m_attrs
.Put(id
, (wxObject
*)attr
);
1251 attr
->SetBackgroundColour(col
);
1256 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1258 m_hasAnyAttr
= TRUE
;
1260 long id
= (long)(WXHTREEITEM
)item
;
1261 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1264 attr
= new wxTreeItemAttr
;
1265 m_attrs
.Put(id
, (wxObject
*)attr
);
1268 attr
->SetFont(font
);
1273 // ----------------------------------------------------------------------------
1275 // ----------------------------------------------------------------------------
1277 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1279 if ( item
== wxTreeItemId(TVI_ROOT
) )
1281 // virtual (hidden) root is never visible
1285 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1288 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1289 // the HTREEITEM with TVM_GETITEMRECT
1290 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1292 // FALSE means get item rect for the whole item, not only text
1293 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1296 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1298 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1301 return tvItem
.cChildren
!= 0;
1304 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1306 // probably not a good idea to put it here
1307 //wxASSERT( ItemHasChildren(item) );
1309 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1312 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1315 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1317 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1320 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1323 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1325 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1328 return (tvItem
.state
& TVIS_BOLD
) != 0;
1331 // ----------------------------------------------------------------------------
1333 // ----------------------------------------------------------------------------
1335 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1337 // Root may be real (visible) or virtual (hidden).
1338 if ( GET_VIRTUAL_ROOT() )
1341 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1344 wxTreeItemId
wxTreeCtrl::GetSelection() const
1346 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1347 wxT("this only works with single selection controls") );
1349 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1352 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1356 if ( IS_VIRTUAL_ROOT(item
) )
1358 // no parent for the virtual root
1363 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1364 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1366 // the top level items should have the virtual root as their parent
1371 return wxTreeItemId((WXHTREEITEM
)hItem
);
1374 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1375 long& _cookie
) const
1377 // remember the last child returned in 'cookie'
1378 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1380 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1383 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1384 long& _cookie
) const
1386 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1393 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1395 // can this be done more efficiently?
1398 wxTreeItemId childLast
,
1399 child
= GetFirstChild(item
, cookie
);
1400 while ( child
.IsOk() )
1403 child
= GetNextChild(item
, cookie
);
1409 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1411 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1414 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1416 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1419 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1421 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1424 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1426 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1428 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1431 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1433 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1435 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1438 // ----------------------------------------------------------------------------
1439 // multiple selections emulation
1440 // ----------------------------------------------------------------------------
1442 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1444 // receive the desired information.
1445 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1448 // state image indices are 1 based
1449 return ((tvItem
.state
>> 12) - 1) == 1;
1452 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1454 // receive the desired information.
1455 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1459 // state images are one-based
1460 tvItem
.state
= (check
? 2 : 1) << 12;
1465 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1467 TraverseSelections
selector(this, selections
);
1469 return selector
.GetCount();
1472 // ----------------------------------------------------------------------------
1474 // ----------------------------------------------------------------------------
1476 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1477 wxTreeItemId hInsertAfter
,
1478 const wxString
& text
,
1479 int image
, int selectedImage
,
1480 wxTreeItemData
*data
)
1482 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1484 _T("can't have more than one root in the tree") );
1486 TV_INSERTSTRUCT tvIns
;
1487 tvIns
.hParent
= HITEM(parent
);
1488 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1490 // this is how we insert the item as the first child: supply a NULL
1492 if ( !tvIns
.hInsertAfter
)
1494 tvIns
.hInsertAfter
= TVI_FIRST
;
1498 if ( !text
.IsEmpty() )
1501 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1505 tvIns
.item
.pszText
= NULL
;
1506 tvIns
.item
.cchTextMax
= 0;
1512 tvIns
.item
.iImage
= image
;
1514 if ( selectedImage
== -1 )
1516 // take the same image for selected icon if not specified
1517 selectedImage
= image
;
1521 if ( selectedImage
!= -1 )
1523 mask
|= TVIF_SELECTEDIMAGE
;
1524 tvIns
.item
.iSelectedImage
= selectedImage
;
1530 tvIns
.item
.lParam
= (LPARAM
)data
;
1533 tvIns
.item
.mask
= mask
;
1535 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1538 wxLogLastError(wxT("TreeView_InsertItem"));
1543 // associate the application tree item with Win32 tree item handle
1544 data
->SetId((WXHTREEITEM
)id
);
1547 return wxTreeItemId((WXHTREEITEM
)id
);
1550 // for compatibility only
1551 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1552 const wxString
& text
,
1553 int image
, int selImage
,
1556 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1557 image
, selImage
, NULL
);
1560 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1561 int image
, int selectedImage
,
1562 wxTreeItemData
*data
)
1565 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1567 // create a virtual root item, the parent for all the others
1568 m_pVirtualRoot
= new wxVirtualNode(data
);
1573 return DoInsertItem(wxTreeItemId((long)(WXHTREEITEM
) 0), (long)(WXHTREEITEM
) 0,
1574 text
, image
, selectedImage
, data
);
1577 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1578 const wxString
& text
,
1579 int image
, int selectedImage
,
1580 wxTreeItemData
*data
)
1582 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1583 text
, image
, selectedImage
, data
);
1586 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1587 const wxTreeItemId
& idPrevious
,
1588 const wxString
& text
,
1589 int image
, int selectedImage
,
1590 wxTreeItemData
*data
)
1592 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1595 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1597 const wxString
& text
,
1598 int image
, int selectedImage
,
1599 wxTreeItemData
*data
)
1601 // find the item from index
1603 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1604 while ( index
!= 0 && idCur
.IsOk() )
1609 idCur
= GetNextChild(parent
, cookie
);
1612 // assert, not check: if the index is invalid, we will append the item
1614 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1616 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1619 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1620 const wxString
& text
,
1621 int image
, int selectedImage
,
1622 wxTreeItemData
*data
)
1624 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1625 text
, image
, selectedImage
, data
);
1628 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1630 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1632 wxLogLastError(wxT("TreeView_DeleteItem"));
1636 // delete all children (but don't delete the item itself)
1637 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1641 wxArrayLong children
;
1642 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1643 while ( child
.IsOk() )
1645 children
.Add((long)(WXHTREEITEM
)child
);
1647 child
= GetNextChild(item
, cookie
);
1650 size_t nCount
= children
.Count();
1651 for ( size_t n
= 0; n
< nCount
; n
++ )
1653 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1655 wxLogLastError(wxT("TreeView_DeleteItem"));
1660 void wxTreeCtrl::DeleteAllItems()
1662 // delete stored root item.
1663 delete GET_VIRTUAL_ROOT();
1665 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1667 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1671 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1673 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1674 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1675 flag
== TVE_EXPAND
||
1677 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1679 // A hidden root can be neither expanded nor collapsed.
1680 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1681 wxT("Can't expand/collapse hidden root node!") )
1683 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1684 // emulate them. This behaviour has changed slightly with comctl32.dll
1685 // v 4.70 - now it does send them but only the first time. To maintain
1686 // compatible behaviour and also in order to not have surprises with the
1687 // future versions, don't rely on this and still do everything ourselves.
1688 // To avoid that the messages be sent twice when the item is expanded for
1689 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1691 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1695 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1697 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1698 event
.m_item
= item
;
1699 event
.SetEventObject(this);
1701 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1703 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1707 (void)GetEventHandler()->ProcessEvent(event
);
1709 //else: change didn't took place, so do nothing at all
1712 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1714 DoExpand(item
, TVE_EXPAND
);
1717 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1719 DoExpand(item
, TVE_COLLAPSE
);
1722 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1724 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1727 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1729 DoExpand(item
, TVE_TOGGLE
);
1732 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1734 DoExpand(item
, action
);
1737 void wxTreeCtrl::Unselect()
1739 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1740 wxT("doesn't make sense, may be you want UnselectAll()?") );
1742 // just remove the selection
1743 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1746 void wxTreeCtrl::UnselectAll()
1748 if ( m_windowStyle
& wxTR_MULTIPLE
)
1750 wxArrayTreeItemIds selections
;
1751 size_t count
= GetSelections(selections
);
1752 for ( size_t n
= 0; n
< count
; n
++ )
1754 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1755 SetItemCheck(selections
[n
], FALSE
);
1756 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1757 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1758 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1763 // just remove the selection
1768 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1770 if ( m_windowStyle
& wxTR_MULTIPLE
)
1772 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1773 // selecting the item means checking it
1775 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1776 ::SelectItem(GetHwnd(), HITEM(item
));
1777 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1781 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1782 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1783 // send them ourselves
1785 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1786 event
.m_item
= item
;
1787 event
.SetEventObject(this);
1789 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1790 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1792 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1794 wxLogLastError(wxT("TreeView_SelectItem"));
1798 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1799 (void)GetEventHandler()->ProcessEvent(event
);
1802 //else: program vetoed the change
1806 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1809 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1812 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1814 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1816 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1820 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1825 void wxTreeCtrl::DeleteTextCtrl()
1829 // the HWND corresponding to this control is deleted by the tree
1830 // control itself and we don't know when exactly this happens, so check
1831 // if the window still exists before calling UnsubclassWin()
1832 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1834 m_textCtrl
->SetHWND(0);
1837 m_textCtrl
->UnsubclassWin();
1838 m_textCtrl
->SetHWND(0);
1844 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1845 wxClassInfo
* textControlClass
)
1847 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1851 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1852 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1854 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1863 // textctrl is subclassed in MSWOnNotify
1867 // End label editing, optionally cancelling the edit
1868 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1870 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1875 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1877 TV_HITTESTINFO hitTestInfo
;
1878 hitTestInfo
.pt
.x
= (int)point
.x
;
1879 hitTestInfo
.pt
.y
= (int)point
.y
;
1881 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1886 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1887 flags |= wxTREE_HITTEST_##flag
1889 TRANSLATE_FLAG(ABOVE
);
1890 TRANSLATE_FLAG(BELOW
);
1891 TRANSLATE_FLAG(NOWHERE
);
1892 TRANSLATE_FLAG(ONITEMBUTTON
);
1893 TRANSLATE_FLAG(ONITEMICON
);
1894 TRANSLATE_FLAG(ONITEMINDENT
);
1895 TRANSLATE_FLAG(ONITEMLABEL
);
1896 TRANSLATE_FLAG(ONITEMRIGHT
);
1897 TRANSLATE_FLAG(ONITEMSTATEICON
);
1898 TRANSLATE_FLAG(TOLEFT
);
1899 TRANSLATE_FLAG(TORIGHT
);
1901 #undef TRANSLATE_FLAG
1903 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1906 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1908 bool textOnly
) const
1911 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1914 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1920 // couldn't retrieve rect: for example, item isn't visible
1925 // ----------------------------------------------------------------------------
1927 // ----------------------------------------------------------------------------
1929 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1930 // functions such as IsDataIndirect()
1931 class wxTreeSortHelper
1934 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1937 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1939 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1940 if ( tree
->IsDataIndirect(data
) )
1942 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1945 return data
->GetId();
1949 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1953 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1954 wxT("sorting tree without data doesn't make sense") );
1956 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1958 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
1959 GetIdFromData(tree
, pItem2
));
1962 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1963 const wxTreeItemId
& item2
)
1965 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1968 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1970 // rely on the fact that TreeView_SortChildren does the same thing as our
1971 // default behaviour, i.e. sorts items alphabetically and so call it
1972 // directly if we're not in derived class (much more efficient!)
1973 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1975 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1980 tvSort
.hParent
= HITEM(item
);
1981 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1982 tvSort
.lParam
= (LPARAM
)this;
1983 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1987 // ----------------------------------------------------------------------------
1989 // ----------------------------------------------------------------------------
1991 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1993 if ( cmd
== EN_UPDATE
)
1995 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1996 event
.SetEventObject( this );
1997 ProcessCommand(event
);
1999 else if ( cmd
== EN_KILLFOCUS
)
2001 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2002 event
.SetEventObject( this );
2003 ProcessCommand(event
);
2011 // command processed
2015 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2016 // only do it during dragging, minimize wxWin overhead (this is important for
2017 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2018 // instead of passing by wxWin events
2019 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2021 bool processed
= FALSE
;
2023 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2025 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2027 // we only process mouse messages here and these parameters have the
2028 // same meaning for all of them
2029 int x
= GET_X_LPARAM(lParam
),
2030 y
= GET_Y_LPARAM(lParam
);
2031 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2035 case WM_RBUTTONDOWN
:
2036 // if the item we are about to right click on
2037 // is not already select, remove the entire
2038 // previous selection
2039 if (!::IsItemSelected(GetHwnd(), htItem
))
2044 // select item and set the focus to the
2045 // newly selected item
2046 ::SelectItem(GetHwnd(), htItem
);
2047 ::SetFocus(GetHwnd(), htItem
);
2050 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2051 case WM_LBUTTONDOWN
:
2052 if ( htItem
&& isMultiple
)
2054 if ( wParam
& MK_CONTROL
)
2058 // toggle selected state
2059 ToggleItemSelection(GetHwnd(), htItem
);
2061 ::SetFocus(GetHwnd(), htItem
);
2063 // reset on any click without Shift
2068 else if ( wParam
& MK_SHIFT
)
2070 // this selects all items between the starting one and
2073 if ( !m_htSelStart
)
2075 // take the focused item
2076 m_htSelStart
= (WXHTREEITEM
)
2077 TreeView_GetSelection(GetHwnd());
2080 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2081 !(wParam
& MK_CONTROL
));
2083 ::SetFocus(GetHwnd(), htItem
);
2087 else // normal click
2089 // clear the selection and then let the default handler
2093 // prevent the click from starting in-place editing
2094 // when there was no selection in the control
2095 TreeView_SelectItem(GetHwnd(), 0);
2097 // reset on any click without Shift
2102 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2107 m_dragImage
->Move(wxPoint(x
, y
));
2110 // highlight the item as target (hiding drag image is
2111 // necessary - otherwise the display will be corrupted)
2112 m_dragImage
->Hide();
2113 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2114 m_dragImage
->Show();
2123 m_dragImage
->EndDrag();
2127 // generate the drag end event
2128 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2130 event
.m_item
= (WXHTREEITEM
)htItem
;
2131 event
.m_pointDrag
= wxPoint(x
, y
);
2132 event
.SetEventObject(this);
2134 (void)GetEventHandler()->ProcessEvent(event
);
2136 // if we don't do it, the tree seems to think that 2 items
2137 // are selected simultaneously which is quite weird
2138 TreeView_SelectDropTarget(GetHwnd(), 0);
2143 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2144 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2146 // the tree control greys out the selected item when it loses focus and
2147 // paints it as selected again when it regains it, but it won't do it
2148 // for the other items itself - help it
2149 wxArrayTreeItemIds selections
;
2150 size_t count
= GetSelections(selections
);
2152 for ( size_t n
= 0; n
< count
; n
++ )
2154 // TreeView_GetItemRect() will return FALSE if item is not visible,
2155 // which may happen perfectly well
2156 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2159 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2163 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2165 bool bCtrl
= wxIsCtrlDown(),
2166 bShift
= wxIsShiftDown();
2168 // we handle.arrows and space, but not page up/down and home/end: the
2169 // latter should be easy, but not the former
2171 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2172 if ( !m_htSelStart
)
2174 m_htSelStart
= (WXHTREEITEM
)htSel
;
2177 if ( wParam
== VK_SPACE
)
2181 ToggleItemSelection(GetHwnd(), htSel
);
2187 ::SelectItem(GetHwnd(), htSel
);
2192 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2194 if ( !bCtrl
&& !bShift
)
2196 // no modifiers, just clear selection and then let the default
2197 // processing to take place
2202 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2204 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2205 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2206 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2210 // at the top/bottom
2216 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2220 // without changing selection
2221 ::SetFocus(GetHwnd(), htNext
);
2228 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2229 else if ( nMsg
== WM_CHAR
)
2231 // don't let the control process Space and Return keys because it
2232 // doesn't do anything useful with them anyhow but always beeps
2233 // annoyingly when it receives them and there is no way to turn it off
2234 // simply if you just process TREEITEM_ACTIVATED event to which Space
2235 // and Enter presses are mapped in your code
2236 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2243 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2248 // process WM_NOTIFY Windows message
2249 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2251 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2252 wxEventType eventType
= wxEVT_NULL
;
2253 NMHDR
*hdr
= (NMHDR
*)lParam
;
2255 switch ( hdr
->code
)
2258 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2261 case TVN_BEGINRDRAG
:
2263 if ( eventType
== wxEVT_NULL
)
2264 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2265 //else: left drag, already set above
2267 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2269 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2270 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2272 // don't allow dragging by default: the user code must
2273 // explicitly say that it wants to allow it to avoid breaking
2279 case TVN_BEGINLABELEDIT
:
2281 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2282 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2284 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2285 event
.m_label
= info
->item
.pszText
;
2286 event
.m_editCancelled
= FALSE
;
2290 case TVN_DELETEITEM
:
2292 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2293 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2295 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2299 delete (wxTreeItemAttr
*)m_attrs
.
2300 Delete((long)tv
->itemOld
.hItem
);
2305 case TVN_ENDLABELEDIT
:
2307 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2308 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2310 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2311 event
.m_label
= info
->item
.pszText
;
2312 if (info
->item
.pszText
== NULL
)
2314 event
.m_editCancelled
= TRUE
;
2318 event
.m_editCancelled
= FALSE
;
2323 case TVN_GETDISPINFO
:
2324 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2327 case TVN_SETDISPINFO
:
2329 if ( eventType
== wxEVT_NULL
)
2330 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2331 //else: get, already set above
2333 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2335 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2339 case TVN_ITEMEXPANDING
:
2340 case TVN_ITEMEXPANDED
:
2342 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2345 switch ( tv
->action
)
2348 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2356 what
= IDX_COLLAPSE
;
2360 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2363 eventType
= gs_expandEvents
[what
][how
];
2365 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2371 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2372 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2374 // fabricate the lParam and wParam parameters sufficiently
2375 // similar to the ones from a "real" WM_KEYDOWN so that
2376 // CreateKeyEvent() works correctly
2378 (::GetKeyState(VK_MENU
) & 0x100 ? KF_ALTDOWN
: 0) << 16;
2380 WXWPARAM wParam
= info
->wVKey
;
2382 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2385 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2390 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2395 // a separate event for Space/Return
2396 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2397 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2399 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2401 event2
.SetEventObject(this);
2402 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2404 event2
.m_item
= GetSelection();
2406 //else: don't know how to get it
2408 (void)GetEventHandler()->ProcessEvent(event2
);
2413 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2414 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2415 // we have to handle both messages:
2416 case TVN_SELCHANGEDA
:
2417 case TVN_SELCHANGEDW
:
2418 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2421 case TVN_SELCHANGINGA
:
2422 case TVN_SELCHANGINGW
:
2424 if ( eventType
== wxEVT_NULL
)
2425 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2426 //else: already set above
2428 if (hdr
->code
== TVN_SELCHANGINGW
||
2429 hdr
->code
== TVN_SELCHANGEDW
)
2431 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2432 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2433 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2437 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2438 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2439 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2444 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2447 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2448 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2449 switch ( nmcd
.dwDrawStage
)
2452 // if we've got any items with non standard attributes,
2453 // notify us before painting each item
2454 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2458 case CDDS_ITEMPREPAINT
:
2460 wxTreeItemAttr
*attr
=
2461 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2465 // nothing to do for this item
2466 *result
= CDRF_DODEFAULT
;
2471 if ( attr
->HasFont() )
2473 hFont
= GetHfontOf(attr
->GetFont());
2481 if ( attr
->HasTextColour() )
2483 colText
= attr
->GetTextColour();
2487 colText
= GetForegroundColour();
2490 // selection colours should override ours
2491 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2494 ::GetSysColor(COLOR_HIGHLIGHT
);
2496 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2501 if ( attr
->HasBackgroundColour() )
2503 colBack
= attr
->GetBackgroundColour();
2507 colBack
= GetBackgroundColour();
2510 lptvcd
->clrText
= wxColourToRGB(colText
);
2511 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2514 // note that if we wanted to set colours for
2515 // individual columns (subitems), we would have
2516 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2519 ::SelectObject(nmcd
.hdc
, hFont
);
2521 *result
= CDRF_NEWFONT
;
2525 *result
= CDRF_DODEFAULT
;
2531 *result
= CDRF_DODEFAULT
;
2535 // we always process it
2537 #endif // _WIN32_IE >= 0x300
2542 TV_HITTESTINFO tvhti
;
2543 ::GetCursorPos(&tvhti
.pt
);
2544 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2545 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2547 if ( tvhti
.flags
& TVHT_ONITEM
)
2549 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2550 eventType
= (int)hdr
->code
== NM_DBLCLK
2551 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2552 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2554 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2555 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2564 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2567 event
.SetEventObject(this);
2568 event
.SetEventType(eventType
);
2570 bool processed
= GetEventHandler()->ProcessEvent(event
);
2573 switch ( hdr
->code
)
2576 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2577 // the return code of this event handler as the return value for
2578 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2579 // expanded status would never work
2584 case TVN_BEGINRDRAG
:
2585 if ( event
.IsAllowed() )
2587 // normally this is impossible because the m_dragImage is
2588 // deleted once the drag operation is over
2589 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2591 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2592 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2593 m_dragImage
->Show();
2597 case TVN_DELETEITEM
:
2599 // NB: we might process this message using wxWindows event
2600 // tables, but due to overhead of wxWin event system we
2601 // prefer to do it here ourself (otherwise deleting a tree
2602 // with many items is just too slow)
2603 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2605 wxTreeItemId item
= event
.m_item
;
2606 if ( HasIndirectData(item
) )
2608 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2610 delete data
; // can't be NULL here
2614 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2615 delete data
; // may be NULL, ok
2618 processed
= TRUE
; // Make sure we don't get called twice
2622 case TVN_BEGINLABELEDIT
:
2623 // return TRUE to cancel label editing
2624 *result
= !event
.IsAllowed();
2625 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2626 if(event
.IsAllowed())
2628 HWND hText
= TreeView_GetEditControl(GetHwnd());
2631 // MBN: if m_textCtrl already has an HWND, it is a stale
2632 // pointer from a previous edit (because the user
2633 // didn't modify the label before dismissing the control,
2634 // and TVN_ENDLABELEDIT was not sent), so delete it
2635 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2638 m_textCtrl
= new wxTextCtrl();
2639 m_textCtrl
->SetParent(this);
2640 m_textCtrl
->SetHWND((WXHWND
)hText
);
2641 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2643 // set wxTE_PROCESS_ENTER style for the text control to
2644 // force it to process the Enter presses itself, otherwise
2645 // they could be stolen from it by the dialog
2647 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2648 | wxTE_PROCESS_ENTER
);
2653 case TVN_ENDLABELEDIT
:
2654 // return TRUE to set the label to the new string: note that we
2655 // also must pretend that we did process the message or it is going
2656 // to be passed to DefWindowProc() which will happily return FALSE
2657 // cancelling the label change
2658 *result
= event
.IsAllowed();
2661 // ensure that we don't have the text ctrl which is going to be
2666 case TVN_SELCHANGING
:
2667 case TVN_ITEMEXPANDING
:
2668 // return TRUE to prevent the action from happening
2669 *result
= !event
.IsAllowed();
2672 case TVN_ITEMEXPANDED
:
2673 // the item is not refreshed properly after expansion when it has
2674 // an image depending on the expanded/collapsed state - bug in
2675 // comctl32.dll or our code?
2677 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2678 wxTreeItemId id
= (WXHTREEITEM
)tv
->itemNew
.hItem
;
2680 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2688 case TVN_GETDISPINFO
:
2689 // NB: so far the user can't set the image himself anyhow, so do it
2690 // anyway - but this may change later
2691 //if ( /* !processed && */ 1 )
2693 wxTreeItemId item
= event
.m_item
;
2694 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2695 if ( info
->item
.mask
& TVIF_IMAGE
)
2698 DoGetItemImageFromData
2701 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2702 : wxTreeItemIcon_Normal
2705 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2707 info
->item
.iSelectedImage
=
2708 DoGetItemImageFromData
2711 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2712 : wxTreeItemIcon_Selected
2719 // for the other messages the return value is ignored and there is
2720 // nothing special to do
2727 #endif // wxUSE_TREECTRL