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
309 wxVirtualNode(wxTreeItemData
*data
)
313 m_tvItem
= new wxTreeViewItem(TVI_ROOT
, 0);
322 wxTreeItemData
*GetData() const { return m_data
; }
323 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
326 wxTreeViewItem
*m_tvItem
;
327 wxTreeItemData
*m_data
;
331 #pragma warning( default : 4097 )
334 // a macro to get the virtual root, returns NULL if none
335 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
337 // returns TRUE if the item is the virtual root
338 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
340 // a class which encapsulates the tree traversal logic: it vists all (unless
341 // OnVisit() returns FALSE) items under the given one
342 class wxTreeTraversal
345 wxTreeTraversal(const wxTreeCtrl
*tree
)
350 // do traverse the tree: visit all items (recursively by default) under the
351 // given one; return TRUE if all items were traversed or FALSE if the
352 // traversal was aborted because OnVisit returned FALSE
353 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
355 // override this function to do whatever is needed for each item, return
356 // FALSE to stop traversing
357 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
360 const wxTreeCtrl
*GetTree() const { return m_tree
; }
363 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
365 const wxTreeCtrl
*m_tree
;
368 // internal class for getting the selected items
369 class TraverseSelections
: public wxTreeTraversal
372 TraverseSelections(const wxTreeCtrl
*tree
,
373 wxArrayTreeItemIds
& selections
)
374 : wxTreeTraversal(tree
), m_selections(selections
)
376 m_selections
.Empty();
378 DoTraverse(tree
->GetRootItem());
381 virtual bool OnVisit(const wxTreeItemId
& item
)
383 // can't visit a virtual node.
384 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
389 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
390 if ( GetTree()->IsItemChecked(item
) )
392 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
395 m_selections
.Add(item
);
401 size_t GetCount() const { return m_selections
.GetCount(); }
404 wxArrayTreeItemIds
& m_selections
;
407 // internal class for counting tree items
408 class TraverseCounter
: public wxTreeTraversal
411 TraverseCounter(const wxTreeCtrl
*tree
,
412 const wxTreeItemId
& root
,
414 : wxTreeTraversal(tree
)
418 DoTraverse(root
, recursively
);
421 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
428 size_t GetCount() const { return m_count
; }
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
;
495 // ----------------------------------------------------------------------------
497 // ----------------------------------------------------------------------------
499 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
501 // ----------------------------------------------------------------------------
503 // ----------------------------------------------------------------------------
505 // indices in gs_expandEvents table below
520 // handy table for sending events - it has to be initialized during run-time
521 // now so can't be const any more
522 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
525 but logically it's a const table with the following entries:
528 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
529 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
533 // ============================================================================
535 // ============================================================================
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
543 if ( !OnVisit(root
) )
546 return Traverse(root
, recursively
);
549 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
552 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
553 while ( child
.IsOk() )
555 // depth first traversal
556 if ( recursively
&& !Traverse(child
, TRUE
) )
559 if ( !OnVisit(child
) )
562 child
= m_tree
->GetNextChild(root
, cookie
);
568 // ----------------------------------------------------------------------------
569 // construction and destruction
570 // ----------------------------------------------------------------------------
572 void wxTreeCtrl::Init()
574 m_imageListNormal
= NULL
;
575 m_imageListState
= NULL
;
576 m_ownsImageListNormal
= m_ownsImageListState
= FALSE
;
578 m_hasAnyAttr
= FALSE
;
581 m_pVirtualRoot
= NULL
;
583 // initialize the global array of events now as it can't be done statically
584 // with the wxEVT_XXX values being allocated during run-time only
585 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
586 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
587 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
588 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
591 bool wxTreeCtrl::Create(wxWindow
*parent
,
596 const wxValidator
& validator
,
597 const wxString
& name
)
601 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
604 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
607 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
608 wstyle
|= WS_CLIPSIBLINGS
;
610 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
611 wstyle
|= TVS_HASLINES
;
612 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
613 wstyle
|= TVS_HASBUTTONS
;
615 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
616 wstyle
|= TVS_EDITLABELS
;
618 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
619 wstyle
|= TVS_LINESATROOT
;
621 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
623 if ( wxTheApp
->GetComCtl32Version() >= 471 )
624 wstyle
|= TVS_FULLROWSELECT
;
627 // using TVS_CHECKBOXES for emulation of a multiselection tree control
628 // doesn't work without the new enough headers
629 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
630 !defined( __GNUWIN32_OLD__ ) && \
631 !defined( __BORLANDC__ ) && \
632 !defined( __WATCOMC__ ) && \
633 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
635 // we emulate the multiple selection tree controls by using checkboxes: set
636 // up the image list we need for this if we do have multiple selections
637 if ( m_windowStyle
& wxTR_MULTIPLE
)
638 wstyle
|= TVS_CHECKBOXES
;
639 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
641 // Create the tree control.
642 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
645 #if wxUSE_COMCTL32_SAFELY
646 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
647 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
649 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
650 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
652 // This works around a bug in the Windows tree control whereby for some versions
653 // of comctrl32, setting any colour actually draws the background in black.
654 // This will initialise the background to the system colour.
655 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
656 // Assume the user has an updated comctl32.dll.
657 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
658 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
659 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
663 // VZ: this is some experimental code which may be used to get the
664 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
665 // AFAIK, the standard DLL does about the same thing anyhow.
667 if ( m_windowStyle
& wxTR_MULTIPLE
)
671 // create the DC compatible with the current screen
672 HDC hdcMem
= CreateCompatibleDC(NULL
);
674 // create a mono bitmap of the standard size
675 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
676 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
677 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
678 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
679 1, // # of color planes
680 1, // # bits needed for one pixel
681 0); // array containing colour data
682 SelectObject(hdcMem
, hbmpCheck
);
684 // then draw a check mark into it
685 RECT rect
= { 0, 0, x
, y
};
686 if ( !::DrawFrameControl(hdcMem
, &rect
,
688 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
690 wxLogLastError(wxT("DrawFrameControl(check)"));
693 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
694 imagelistCheckboxes
.Add(bmp
);
696 if ( !::DrawFrameControl(hdcMem
, &rect
,
700 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
703 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
704 imagelistCheckboxes
.Add(bmp
);
710 SetStateImageList(&imagelistCheckboxes
);
714 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
719 wxTreeCtrl::~wxTreeCtrl()
721 // delete any attributes
724 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
726 delete (wxTreeItemAttr
*)node
->Data();
729 // prevent TVN_DELETEITEM handler from deleting the attributes again!
730 m_hasAnyAttr
= FALSE
;
735 // delete user data to prevent memory leaks
736 // also deletes hidden root node storage.
739 if (m_ownsImageListNormal
) delete m_imageListNormal
;
740 if (m_ownsImageListState
) delete m_imageListState
;
743 // ----------------------------------------------------------------------------
745 // ----------------------------------------------------------------------------
747 // simple wrappers which add error checking in debug mode
749 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
751 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, FALSE
,
752 _T("can't retrieve virtual root item") );
754 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
756 wxLogLastError(wxT("TreeView_GetItem"));
764 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
766 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
768 wxLogLastError(wxT("TreeView_SetItem"));
772 size_t wxTreeCtrl::GetCount() const
774 return (size_t)TreeView_GetCount(GetHwnd());
777 unsigned int wxTreeCtrl::GetIndent() const
779 return TreeView_GetIndent(GetHwnd());
782 void wxTreeCtrl::SetIndent(unsigned int indent
)
784 TreeView_SetIndent(GetHwnd(), indent
);
787 wxImageList
*wxTreeCtrl::GetImageList() const
789 return m_imageListNormal
;
792 wxImageList
*wxTreeCtrl::GetStateImageList() const
794 return m_imageListNormal
;
797 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
800 TreeView_SetImageList(GetHwnd(),
801 imageList
? imageList
->GetHIMAGELIST() : 0,
805 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
807 if (m_ownsImageListNormal
)
808 delete m_imageListNormal
;
810 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
811 m_ownsImageListNormal
= FALSE
;
814 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
816 if (m_ownsImageListState
) delete m_imageListState
;
817 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
818 m_ownsImageListState
= FALSE
;
821 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
823 SetImageList(imageList
);
824 m_ownsImageListNormal
= TRUE
;
827 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
829 SetStateImageList(imageList
);
830 m_ownsImageListState
= TRUE
;
833 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
834 bool recursively
) const
836 TraverseCounter
counter(this, item
, recursively
);
838 return counter
.GetCount() - 1;
841 // ----------------------------------------------------------------------------
843 // ----------------------------------------------------------------------------
845 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
847 #if !wxUSE_COMCTL32_SAFELY
848 if ( !wxWindowBase::SetBackgroundColour(colour
) )
851 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
857 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
859 #if !wxUSE_COMCTL32_SAFELY
860 if ( !wxWindowBase::SetForegroundColour(colour
) )
863 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
869 // ----------------------------------------------------------------------------
871 // ----------------------------------------------------------------------------
873 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
875 wxChar buf
[512]; // the size is arbitrary...
877 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
878 tvItem
.pszText
= buf
;
879 tvItem
.cchTextMax
= WXSIZEOF(buf
);
880 if ( !DoGetItem(&tvItem
) )
882 // don't return some garbage which was on stack, but an empty string
886 return wxString(buf
);
889 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
891 if ( IS_VIRTUAL_ROOT(item
) )
894 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
895 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
899 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
900 wxTreeItemIcon which
) const
902 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
903 if ( !DoGetItem(&tvItem
) )
908 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
911 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
913 wxTreeItemIcon which
) const
915 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
916 if ( !DoGetItem(&tvItem
) )
921 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
923 data
->SetImage(image
, which
);
925 // make sure that we have selected images as well
926 if ( which
== wxTreeItemIcon_Normal
&&
927 !data
->HasImage(wxTreeItemIcon_Selected
) )
929 data
->SetImage(image
, wxTreeItemIcon_Selected
);
932 if ( which
== wxTreeItemIcon_Expanded
&&
933 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
935 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
939 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
943 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
944 tvItem
.iSelectedImage
= imageSel
;
945 tvItem
.iImage
= image
;
949 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
950 wxTreeItemIcon which
) const
952 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
954 // TODO: Maybe a hidden root can still provide images?
958 if ( HasIndirectData(item
) )
960 return DoGetItemImageFromData(item
, which
);
967 wxFAIL_MSG( wxT("unknown tree item image type") );
969 case wxTreeItemIcon_Normal
:
973 case wxTreeItemIcon_Selected
:
974 mask
= TVIF_SELECTEDIMAGE
;
977 case wxTreeItemIcon_Expanded
:
978 case wxTreeItemIcon_SelectedExpanded
:
982 wxTreeViewItem
tvItem(item
, mask
);
985 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
988 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
989 wxTreeItemIcon which
)
991 if ( IS_VIRTUAL_ROOT(item
) )
993 // TODO: Maybe a hidden root can still store images?
997 int imageNormal
, imageSel
;
1001 wxFAIL_MSG( wxT("unknown tree item image type") );
1003 case wxTreeItemIcon_Normal
:
1004 imageNormal
= image
;
1005 imageSel
= GetItemSelectedImage(item
);
1008 case wxTreeItemIcon_Selected
:
1009 imageNormal
= GetItemImage(item
);
1013 case wxTreeItemIcon_Expanded
:
1014 case wxTreeItemIcon_SelectedExpanded
:
1015 if ( !HasIndirectData(item
) )
1017 // we need to get the old images first, because after we create
1018 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1020 imageNormal
= GetItemImage(item
);
1021 imageSel
= GetItemSelectedImage(item
);
1023 // if it doesn't have it yet, add it
1024 wxTreeItemIndirectData
*data
= new
1025 wxTreeItemIndirectData(this, item
);
1027 // copy the data to the new location
1028 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1029 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1032 DoSetItemImageFromData(item
, image
, which
);
1034 // reset the normal/selected images because we won't use them any
1035 // more - now they're stored inside the indirect data
1037 imageSel
= I_IMAGECALLBACK
;
1041 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1042 // change both normal and selected image - otherwise the change simply
1043 // doesn't take place!
1044 DoSetItemImages(item
, imageNormal
, imageSel
);
1047 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1049 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1051 // Hidden root may have data.
1052 if ( IS_VIRTUAL_ROOT(item
) )
1054 return GET_VIRTUAL_ROOT()->GetData();
1058 if ( !DoGetItem(&tvItem
) )
1063 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1064 if ( IsDataIndirect(data
) )
1066 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1072 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1074 if ( IS_VIRTUAL_ROOT(item
) )
1076 GET_VIRTUAL_ROOT()->SetData(data
);
1079 // first, associate this piece of data with this item
1085 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1087 if ( HasIndirectData(item
) )
1089 if ( DoGetItem(&tvItem
) )
1091 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1095 wxFAIL_MSG( wxT("failed to change tree items data") );
1100 tvItem
.lParam
= (LPARAM
)data
;
1105 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1106 wxTreeItemIndirectData
*data
)
1108 // this should never happen because it's unnecessary and will probably lead
1109 // to crash too because the code elsewhere supposes that the pointer the
1110 // wxTreeItemIndirectData has is a real wxItemData and not
1111 // wxTreeItemIndirectData as well
1112 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1114 SetItemData(item
, data
);
1117 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1119 // query the item itself
1120 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1121 if ( !DoGetItem(&tvItem
) )
1126 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1128 return data
&& IsDataIndirect(data
);
1131 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1133 if ( IS_VIRTUAL_ROOT(item
) )
1136 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1137 tvItem
.cChildren
= (int)has
;
1141 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1143 if ( IS_VIRTUAL_ROOT(item
) )
1146 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1147 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1151 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1153 if ( IS_VIRTUAL_ROOT(item
) )
1156 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1157 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1161 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1163 if ( IS_VIRTUAL_ROOT(item
) )
1167 if ( GetBoundingRect(item
, rect
) )
1173 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1174 const wxColour
& col
)
1176 m_hasAnyAttr
= TRUE
;
1178 long id
= (long)(WXHTREEITEM
)item
;
1179 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1182 attr
= new wxTreeItemAttr
;
1183 m_attrs
.Put(id
, (wxObject
*)attr
);
1186 attr
->SetTextColour(col
);
1191 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1192 const wxColour
& col
)
1194 m_hasAnyAttr
= TRUE
;
1196 long id
= (long)(WXHTREEITEM
)item
;
1197 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1200 attr
= new wxTreeItemAttr
;
1201 m_attrs
.Put(id
, (wxObject
*)attr
);
1204 attr
->SetBackgroundColour(col
);
1209 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1211 m_hasAnyAttr
= TRUE
;
1213 long id
= (long)(WXHTREEITEM
)item
;
1214 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1217 attr
= new wxTreeItemAttr
;
1218 m_attrs
.Put(id
, (wxObject
*)attr
);
1221 attr
->SetFont(font
);
1226 // ----------------------------------------------------------------------------
1228 // ----------------------------------------------------------------------------
1230 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1232 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1235 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1236 // the HTREEITEM with TVM_GETITEMRECT
1237 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1239 // FALSE means get item rect for the whole item, not only text
1240 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1243 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1245 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1248 return tvItem
.cChildren
!= 0;
1251 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1253 // probably not a good idea to put it here
1254 //wxASSERT( ItemHasChildren(item) );
1256 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1259 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1262 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1264 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1267 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1270 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1272 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1275 return (tvItem
.state
& TVIS_BOLD
) != 0;
1278 // ----------------------------------------------------------------------------
1280 // ----------------------------------------------------------------------------
1282 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1284 // Root may be real (visible) or virtual (hidden).
1285 if ( GET_VIRTUAL_ROOT() )
1288 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1291 wxTreeItemId
wxTreeCtrl::GetSelection() const
1293 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1294 wxT("this only works with single selection controls") );
1296 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1299 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1303 if ( IS_VIRTUAL_ROOT(item
) )
1305 // no parent for the virtual root
1310 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1311 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1313 // the top level items should have the virtual root as their parent
1318 return wxTreeItemId((WXHTREEITEM
)hItem
);
1321 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1322 long& _cookie
) const
1324 // remember the last child returned in 'cookie'
1325 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1327 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1330 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1331 long& _cookie
) const
1333 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1340 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1342 // can this be done more efficiently?
1345 wxTreeItemId childLast
,
1346 child
= GetFirstChild(item
, cookie
);
1347 while ( child
.IsOk() )
1350 child
= GetNextChild(item
, cookie
);
1356 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1358 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1361 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1363 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1366 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1368 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1371 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1373 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1375 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1378 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1380 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1382 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1385 // ----------------------------------------------------------------------------
1386 // multiple selections emulation
1387 // ----------------------------------------------------------------------------
1389 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1391 // receive the desired information.
1392 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1395 // state image indices are 1 based
1396 return ((tvItem
.state
>> 12) - 1) == 1;
1399 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1401 // receive the desired information.
1402 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1406 // state images are one-based
1407 tvItem
.state
= (check
? 2 : 1) << 12;
1412 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1414 TraverseSelections
selector(this, selections
);
1416 return selector
.GetCount();
1419 // ----------------------------------------------------------------------------
1421 // ----------------------------------------------------------------------------
1423 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1424 wxTreeItemId hInsertAfter
,
1425 const wxString
& text
,
1426 int image
, int selectedImage
,
1427 wxTreeItemData
*data
)
1429 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1431 _T("can't have more than one root in the tree") );
1433 TV_INSERTSTRUCT tvIns
;
1434 tvIns
.hParent
= HITEM(parent
);
1435 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1437 // this is how we insert the item as the first child: supply a NULL
1439 if ( !tvIns
.hInsertAfter
)
1441 tvIns
.hInsertAfter
= TVI_FIRST
;
1445 if ( !text
.IsEmpty() )
1448 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1452 tvIns
.item
.pszText
= NULL
;
1453 tvIns
.item
.cchTextMax
= 0;
1459 tvIns
.item
.iImage
= image
;
1461 if ( selectedImage
== -1 )
1463 // take the same image for selected icon if not specified
1464 selectedImage
= image
;
1468 if ( selectedImage
!= -1 )
1470 mask
|= TVIF_SELECTEDIMAGE
;
1471 tvIns
.item
.iSelectedImage
= selectedImage
;
1477 tvIns
.item
.lParam
= (LPARAM
)data
;
1480 tvIns
.item
.mask
= mask
;
1482 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1485 wxLogLastError(wxT("TreeView_InsertItem"));
1490 // associate the application tree item with Win32 tree item handle
1491 data
->SetId((WXHTREEITEM
)id
);
1494 return wxTreeItemId((WXHTREEITEM
)id
);
1497 // for compatibility only
1498 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1499 const wxString
& text
,
1500 int image
, int selImage
,
1503 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1504 image
, selImage
, NULL
);
1507 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1508 int image
, int selectedImage
,
1509 wxTreeItemData
*data
)
1512 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1514 // create a virtual root item, the parent for all the others
1515 m_pVirtualRoot
= new wxVirtualNode(data
);
1520 return DoInsertItem(wxTreeItemId((WXHTREEITEM
) 0), (WXHTREEITEM
) 0,
1521 text
, image
, selectedImage
, data
);
1524 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1525 const wxString
& text
,
1526 int image
, int selectedImage
,
1527 wxTreeItemData
*data
)
1529 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1530 text
, image
, selectedImage
, data
);
1533 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1534 const wxTreeItemId
& idPrevious
,
1535 const wxString
& text
,
1536 int image
, int selectedImage
,
1537 wxTreeItemData
*data
)
1539 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1542 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1544 const wxString
& text
,
1545 int image
, int selectedImage
,
1546 wxTreeItemData
*data
)
1548 // find the item from index
1550 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1551 while ( index
!= 0 && idCur
.IsOk() )
1556 idCur
= GetNextChild(parent
, cookie
);
1559 // assert, not check: if the index is invalid, we will append the item
1561 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1563 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1566 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1567 const wxString
& text
,
1568 int image
, int selectedImage
,
1569 wxTreeItemData
*data
)
1571 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1572 text
, image
, selectedImage
, data
);
1575 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1577 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1579 wxLogLastError(wxT("TreeView_DeleteItem"));
1583 // delete all children (but don't delete the item itself)
1584 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1588 wxArrayLong children
;
1589 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1590 while ( child
.IsOk() )
1592 children
.Add((long)(WXHTREEITEM
)child
);
1594 child
= GetNextChild(item
, cookie
);
1597 size_t nCount
= children
.Count();
1598 for ( size_t n
= 0; n
< nCount
; n
++ )
1600 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1602 wxLogLastError(wxT("TreeView_DeleteItem"));
1607 void wxTreeCtrl::DeleteAllItems()
1609 // delete stored root item.
1610 delete GET_VIRTUAL_ROOT();
1612 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1614 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1618 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1620 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1621 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1622 flag
== TVE_EXPAND
||
1624 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1626 // A hidden root can be neither expanded nor collapsed.
1627 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1629 // No action will be taken.
1633 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1634 // emulate them. This behaviour has changed slightly with comctl32.dll
1635 // v 4.70 - now it does send them but only the first time. To maintain
1636 // compatible behaviour and also in order to not have surprises with the
1637 // future versions, don't rely on this and still do everything ourselves.
1638 // To avoid that the messages be sent twice when the item is expanded for
1639 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1641 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1645 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1647 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1648 event
.m_item
= item
;
1649 event
.SetEventObject(this);
1651 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1653 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1657 (void)GetEventHandler()->ProcessEvent(event
);
1659 //else: change didn't took place, so do nothing at all
1662 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1664 DoExpand(item
, TVE_EXPAND
);
1667 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1669 DoExpand(item
, TVE_COLLAPSE
);
1672 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1674 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1677 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1679 DoExpand(item
, TVE_TOGGLE
);
1682 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1684 DoExpand(item
, action
);
1687 void wxTreeCtrl::Unselect()
1689 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1690 wxT("doesn't make sense, may be you want UnselectAll()?") );
1692 // just remove the selection
1693 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1696 void wxTreeCtrl::UnselectAll()
1698 if ( m_windowStyle
& wxTR_MULTIPLE
)
1700 wxArrayTreeItemIds selections
;
1701 size_t count
= GetSelections(selections
);
1702 for ( size_t n
= 0; n
< count
; n
++ )
1704 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1705 SetItemCheck(selections
[n
], FALSE
);
1706 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1707 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1708 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1713 // just remove the selection
1718 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1720 if ( m_windowStyle
& wxTR_MULTIPLE
)
1722 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1723 // selecting the item means checking it
1725 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1726 ::SelectItem(GetHwnd(), HITEM(item
));
1727 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1731 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1732 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1733 // send them ourselves
1735 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1736 event
.m_item
= item
;
1737 event
.SetEventObject(this);
1739 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1740 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1742 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1744 wxLogLastError(wxT("TreeView_SelectItem"));
1748 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1749 (void)GetEventHandler()->ProcessEvent(event
);
1752 //else: program vetoed the change
1756 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1759 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1762 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1764 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1766 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1770 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1772 // normally, we could try to do something like this to return something
1773 // even when the editing was started by the user and not by calling
1774 // EditLabel() - but as nobody has asked for this so far and there might be
1775 // problems in the code below, I leave it disabled for now (VZ)
1779 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1782 m_textCtrl
= new wxTextCtrl(this, -1);
1784 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1786 //else: not editing label right now
1793 void wxTreeCtrl::DeleteTextCtrl()
1797 // the HWND corresponding to this control is deleted by the tree
1798 // control itself and we don't know when exactly this happens, so check
1799 // if the window still exists before calling UnsubclassWin()
1800 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1802 m_textCtrl
->SetHWND(0);
1805 m_textCtrl
->UnsubclassWin();
1806 m_textCtrl
->SetHWND(0);
1812 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1813 wxClassInfo
* textControlClass
)
1815 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1819 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1821 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1828 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1829 m_textCtrl
->SetParent(this);
1830 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1831 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1836 // End label editing, optionally cancelling the edit
1837 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1839 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1844 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1846 TV_HITTESTINFO hitTestInfo
;
1847 hitTestInfo
.pt
.x
= (int)point
.x
;
1848 hitTestInfo
.pt
.y
= (int)point
.y
;
1850 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1855 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1856 flags |= wxTREE_HITTEST_##flag
1858 TRANSLATE_FLAG(ABOVE
);
1859 TRANSLATE_FLAG(BELOW
);
1860 TRANSLATE_FLAG(NOWHERE
);
1861 TRANSLATE_FLAG(ONITEMBUTTON
);
1862 TRANSLATE_FLAG(ONITEMICON
);
1863 TRANSLATE_FLAG(ONITEMINDENT
);
1864 TRANSLATE_FLAG(ONITEMLABEL
);
1865 TRANSLATE_FLAG(ONITEMRIGHT
);
1866 TRANSLATE_FLAG(ONITEMSTATEICON
);
1867 TRANSLATE_FLAG(TOLEFT
);
1868 TRANSLATE_FLAG(TORIGHT
);
1870 #undef TRANSLATE_FLAG
1872 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1875 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1877 bool textOnly
) const
1880 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1883 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1889 // couldn't retrieve rect: for example, item isn't visible
1894 // ----------------------------------------------------------------------------
1896 // ----------------------------------------------------------------------------
1898 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1899 // functions such as IsDataIndirect()
1900 class wxTreeSortHelper
1903 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1906 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1908 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1909 if ( tree
->IsDataIndirect(data
) )
1911 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1914 return data
->GetId();
1918 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1922 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1923 wxT("sorting tree without data doesn't make sense") );
1925 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1927 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
1928 GetIdFromData(tree
, pItem2
));
1931 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1932 const wxTreeItemId
& item2
)
1934 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1937 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1939 // rely on the fact that TreeView_SortChildren does the same thing as our
1940 // default behaviour, i.e. sorts items alphabetically and so call it
1941 // directly if we're not in derived class (much more efficient!)
1942 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1944 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1949 tvSort
.hParent
= HITEM(item
);
1950 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1951 tvSort
.lParam
= (LPARAM
)this;
1952 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1956 // ----------------------------------------------------------------------------
1958 // ----------------------------------------------------------------------------
1960 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1962 if ( cmd
== EN_UPDATE
)
1964 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1965 event
.SetEventObject( this );
1966 ProcessCommand(event
);
1968 else if ( cmd
== EN_KILLFOCUS
)
1970 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1971 event
.SetEventObject( this );
1972 ProcessCommand(event
);
1980 // command processed
1984 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1985 // only do it during dragging, minimize wxWin overhead (this is important for
1986 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1987 // instead of passing by wxWin events
1988 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1990 bool processed
= FALSE
;
1992 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1994 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1996 // we only process mouse messages here and these parameters have the same
1997 // meaning for all of them
1998 int x
= GET_X_LPARAM(lParam
),
1999 y
= GET_Y_LPARAM(lParam
);
2000 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2004 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2005 case WM_LBUTTONDOWN
:
2006 if ( htItem
&& isMultiple
)
2008 if ( wParam
& MK_CONTROL
)
2012 // toggle selected state
2013 ToggleItemSelection(GetHwnd(), htItem
);
2015 ::SetFocus(GetHwnd(), htItem
);
2017 // reset on any click without Shift
2022 else if ( wParam
& MK_SHIFT
)
2024 // this selects all items between the starting one and
2027 if ( !m_htSelStart
)
2029 // take the focused item
2030 m_htSelStart
= (WXHTREEITEM
)
2031 TreeView_GetSelection(GetHwnd());
2034 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2035 !(wParam
& MK_CONTROL
));
2037 ::SetFocus(GetHwnd(), htItem
);
2041 else // normal click
2043 // clear the selection and then let the default handler
2047 // prevent the click from starting in-place editing
2048 // when there was no selection in the control
2049 TreeView_SelectItem(GetHwnd(), 0);
2051 // reset on any click without Shift
2056 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2061 m_dragImage
->Move(wxPoint(x
, y
));
2064 // highlight the item as target (hiding drag image is
2065 // necessary - otherwise the display will be corrupted)
2066 m_dragImage
->Hide();
2067 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2068 m_dragImage
->Show();
2077 m_dragImage
->EndDrag();
2081 // generate the drag end event
2082 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2084 event
.m_item
= (WXHTREEITEM
)htItem
;
2085 event
.m_pointDrag
= wxPoint(x
, y
);
2086 event
.SetEventObject(this);
2088 (void)GetEventHandler()->ProcessEvent(event
);
2090 // if we don't do it, the tree seems to think that 2 items
2091 // are selected simultaneously which is quite weird
2092 TreeView_SelectDropTarget(GetHwnd(), 0);
2097 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2098 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2100 // the tree control greys out the selected item when it loses focus and
2101 // paints it as selected again when it regains it, but it won't do it
2102 // for the other items itself - help it
2103 wxArrayTreeItemIds selections
;
2104 size_t count
= GetSelections(selections
);
2106 for ( size_t n
= 0; n
< count
; n
++ )
2108 // TreeView_GetItemRect() will return FALSE if item is not visible,
2109 // which may happen perfectly well
2110 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2113 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2117 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2119 bool bCtrl
= wxIsCtrlDown(),
2120 bShift
= wxIsShiftDown();
2122 // we handle.arrows and space, but not page up/down and home/end: the
2123 // latter should be easy, but not the former
2125 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2126 if ( !m_htSelStart
)
2128 m_htSelStart
= (WXHTREEITEM
)htSel
;
2131 if ( wParam
== VK_SPACE
)
2135 ToggleItemSelection(GetHwnd(), htSel
);
2141 ::SelectItem(GetHwnd(), htSel
);
2146 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2148 if ( !bCtrl
&& !bShift
)
2150 // no modifiers, just clear selection and then let the default
2151 // processing to take place
2156 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2158 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2159 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2160 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2164 // at the top/bottom
2170 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2174 // without changing selection
2175 ::SetFocus(GetHwnd(), htNext
);
2182 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2184 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2189 // process WM_NOTIFY Windows message
2190 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2192 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2193 wxEventType eventType
= wxEVT_NULL
;
2194 NMHDR
*hdr
= (NMHDR
*)lParam
;
2196 switch ( hdr
->code
)
2199 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2202 case TVN_BEGINRDRAG
:
2204 if ( eventType
== wxEVT_NULL
)
2205 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2206 //else: left drag, already set above
2208 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2210 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2211 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2213 // don't allow dragging by default: the user code must
2214 // explicitly say that it wants to allow it to avoid breaking
2220 case TVN_BEGINLABELEDIT
:
2222 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2223 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2225 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2226 event
.m_label
= info
->item
.pszText
;
2230 case TVN_DELETEITEM
:
2232 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2233 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2235 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2239 delete (wxTreeItemAttr
*)m_attrs
.
2240 Delete((long)tv
->itemOld
.hItem
);
2245 case TVN_ENDLABELEDIT
:
2247 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2248 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2250 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2251 event
.m_label
= info
->item
.pszText
;
2252 if (info
->item
.pszText
== NULL
)
2257 case TVN_GETDISPINFO
:
2258 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2261 case TVN_SETDISPINFO
:
2263 if ( eventType
== wxEVT_NULL
)
2264 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2265 //else: get, already set above
2267 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2269 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2273 case TVN_ITEMEXPANDING
:
2274 case TVN_ITEMEXPANDED
:
2276 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2279 switch ( tv
->action
)
2282 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2290 what
= IDX_COLLAPSE
;
2294 int how
= (int)hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2297 eventType
= gs_expandEvents
[what
][how
];
2299 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2305 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2306 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2308 // we pass 0 as last CreateKeyEvent() parameter because we
2309 // don't have access to the real key press flags here - but as
2310 // it is only used to determin wxKeyEvent::m_altDown flag it's
2312 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2313 wxCharCodeMSWToWX(info
->wVKey
),
2316 // a separate event for Space/Return
2317 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2318 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2320 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2322 event2
.SetEventObject(this);
2323 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2325 event2
.m_item
= GetSelection();
2327 //else: don't know how to get it
2329 (void)GetEventHandler()->ProcessEvent(event2
);
2334 case TVN_SELCHANGED
:
2335 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2338 case TVN_SELCHANGING
:
2340 if ( eventType
== wxEVT_NULL
)
2341 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2342 //else: already set above
2344 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2346 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2347 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2351 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2354 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2355 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2356 switch ( nmcd
.dwDrawStage
)
2359 // if we've got any items with non standard attributes,
2360 // notify us before painting each item
2361 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2365 case CDDS_ITEMPREPAINT
:
2367 wxTreeItemAttr
*attr
=
2368 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2372 // nothing to do for this item
2373 *result
= CDRF_DODEFAULT
;
2378 wxColour colText
, colBack
;
2379 if ( attr
->HasFont() )
2381 wxFont font
= attr
->GetFont();
2382 hFont
= (HFONT
)font
.GetResourceHandle();
2389 if ( attr
->HasTextColour() )
2391 colText
= attr
->GetTextColour();
2395 colText
= GetForegroundColour();
2398 // selection colours should override ours
2399 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2401 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2402 lptvcd
->clrTextBk
= clrBk
;
2404 // try to make the text visible
2405 lptvcd
->clrText
= wxColourToRGB(colText
);
2406 lptvcd
->clrText
|= ~clrBk
;
2407 lptvcd
->clrText
&= 0x00ffffff;
2411 if ( attr
->HasBackgroundColour() )
2413 colBack
= attr
->GetBackgroundColour();
2417 colBack
= GetBackgroundColour();
2420 lptvcd
->clrText
= wxColourToRGB(colText
);
2421 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2424 // note that if we wanted to set colours for
2425 // individual columns (subitems), we would have
2426 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2429 ::SelectObject(nmcd
.hdc
, hFont
);
2431 *result
= CDRF_NEWFONT
;
2435 *result
= CDRF_DODEFAULT
;
2441 *result
= CDRF_DODEFAULT
;
2445 // we always process it
2447 #endif // _WIN32_IE >= 0x300
2452 TV_HITTESTINFO tvhti
;
2453 ::GetCursorPos(&tvhti
.pt
);
2454 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2455 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2457 if ( tvhti
.flags
& TVHT_ONITEM
)
2459 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2460 eventType
= (int)hdr
->code
== NM_DBLCLK
2461 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2462 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2464 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2465 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2474 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2477 event
.SetEventObject(this);
2478 event
.SetEventType(eventType
);
2480 bool processed
= GetEventHandler()->ProcessEvent(event
);
2483 switch ( hdr
->code
)
2486 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2487 // the return code of this event handler as the return value for
2488 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2489 // expanded status would never work
2494 case TVN_BEGINRDRAG
:
2495 if ( event
.IsAllowed() )
2497 // normally this is impossible because the m_dragImage is
2498 // deleted once the drag operation is over
2499 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2501 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2502 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2503 m_dragImage
->Show();
2507 case TVN_DELETEITEM
:
2509 // NB: we might process this message using wxWindows event
2510 // tables, but due to overhead of wxWin event system we
2511 // prefer to do it here ourself (otherwise deleting a tree
2512 // with many items is just too slow)
2513 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2515 wxTreeItemId item
= event
.m_item
;
2516 if ( HasIndirectData(item
) )
2518 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2520 delete data
; // can't be NULL here
2524 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2525 delete data
; // may be NULL, ok
2528 processed
= TRUE
; // Make sure we don't get called twice
2532 case TVN_BEGINLABELEDIT
:
2533 // return TRUE to cancel label editing
2534 *result
= !event
.IsAllowed();
2537 case TVN_ENDLABELEDIT
:
2538 // return TRUE to set the label to the new string: note that we
2539 // also must pretend that we did process the message or it is going
2540 // to be passed to DefWindowProc() which will happily return FALSE
2541 // cancelling the label change
2542 *result
= event
.IsAllowed();
2545 // ensure that we don't have the text ctrl which is going to be
2550 case TVN_SELCHANGING
:
2551 case TVN_ITEMEXPANDING
:
2552 // return TRUE to prevent the action from happening
2553 *result
= !event
.IsAllowed();
2556 case TVN_ITEMEXPANDED
:
2557 // the item is not refreshed properly after expansion when it has
2558 // an image depending on the expanded/collapsed state - bug in
2559 // comctl32.dll or our code?
2561 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2562 if ( tv
->action
== TVE_EXPAND
)
2564 wxTreeItemId id
= (WXHTREEITEM
)tv
->itemNew
.hItem
;
2566 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2575 case TVN_GETDISPINFO
:
2576 // NB: so far the user can't set the image himself anyhow, so do it
2577 // anyway - but this may change later
2578 // if ( /* !processed && */ 1 )
2580 wxTreeItemId item
= event
.m_item
;
2581 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2582 if ( info
->item
.mask
& TVIF_IMAGE
)
2585 DoGetItemImageFromData
2588 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2589 : wxTreeItemIcon_Normal
2592 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2594 info
->item
.iSelectedImage
=
2595 DoGetItemImageFromData
2598 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2599 : wxTreeItemIcon_Selected
2606 // for the other messages the return value is ignored and there is
2607 // nothing special to do
2614 #endif // wxUSE_TREECTRL