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(__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
;
326 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
330 #pragma warning( default : 4097 )
333 // a macro to get the virtual root, returns NULL if none
334 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
336 // returns true if the item is the virtual root
337 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
339 // a class which encapsulates the tree traversal logic: it vists all (unless
340 // OnVisit() returns false) items under the given one
341 class wxTreeTraversal
344 wxTreeTraversal(const wxTreeCtrl
*tree
)
349 // do traverse the tree: visit all items (recursively by default) under the
350 // given one; return true if all items were traversed or false if the
351 // traversal was aborted because OnVisit returned false
352 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
354 // override this function to do whatever is needed for each item, return
355 // false to stop traversing
356 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
359 const wxTreeCtrl
*GetTree() const { return m_tree
; }
362 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
364 const wxTreeCtrl
*m_tree
;
366 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
369 // internal class for getting the selected items
370 class TraverseSelections
: public wxTreeTraversal
373 TraverseSelections(const wxTreeCtrl
*tree
,
374 wxArrayTreeItemIds
& selections
)
375 : wxTreeTraversal(tree
), m_selections(selections
)
377 m_selections
.Empty();
379 DoTraverse(tree
->GetRootItem());
382 virtual bool OnVisit(const wxTreeItemId
& item
)
384 // can't visit a virtual node.
385 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
390 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
391 if ( GetTree()->IsItemChecked(item
) )
393 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
396 m_selections
.Add(item
);
402 size_t GetCount() const { return m_selections
.GetCount(); }
405 wxArrayTreeItemIds
& m_selections
;
408 // internal class for counting tree items
409 class TraverseCounter
: public wxTreeTraversal
412 TraverseCounter(const wxTreeCtrl
*tree
,
413 const wxTreeItemId
& root
,
415 : wxTreeTraversal(tree
)
419 DoTraverse(root
, recursively
);
422 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
429 size_t GetCount() const { return m_count
; }
435 // ----------------------------------------------------------------------------
436 // This class is needed for support of different images: the Win32 common
437 // control natively supports only 2 images (the normal one and another for the
438 // selected state). We wish to provide support for 2 more of them for folder
439 // items (i.e. those which have children): for expanded state and for expanded
440 // selected state. For this we use this structure to store the additional items
443 // There is only one problem with this: when we retrieve the item's data, we
444 // don't know whether we get a pointer to wxTreeItemData or
445 // wxTreeItemIndirectData. So we always set the item id to an invalid value
446 // in this class and the code using the client data checks for it and retrieves
447 // the real client data in this case.
448 // ----------------------------------------------------------------------------
450 class wxTreeItemIndirectData
: public wxTreeItemData
453 // ctor associates this data with the item and the real item data becomes
454 // available through our GetData() method
455 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
457 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
463 m_data
= tree
->GetItemData(item
);
465 // and set ourselves as the new one
466 tree
->SetIndirectItemData(item
, this);
468 // we must have the invalid value for the item
472 // dtor deletes the associated data as well
473 virtual ~wxTreeItemIndirectData() { delete m_data
; }
476 // get the real data associated with the item
477 wxTreeItemData
*GetData() const { return m_data
; }
479 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
481 // do we have such image?
482 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
484 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
486 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
489 // all the images associated with the item
490 int m_images
[wxTreeItemIcon_Max
];
492 // the real client data
493 wxTreeItemData
*m_data
;
495 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
498 // ----------------------------------------------------------------------------
500 // ----------------------------------------------------------------------------
502 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
508 // indices in gs_expandEvents table below
523 // handy table for sending events - it has to be initialized during run-time
524 // now so can't be const any more
525 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
528 but logically it's a const table with the following entries:
531 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
532 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
536 // ============================================================================
538 // ============================================================================
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
544 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
546 if ( !OnVisit(root
) )
549 return Traverse(root
, recursively
);
552 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
555 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
556 while ( child
.IsOk() )
558 // depth first traversal
559 if ( recursively
&& !Traverse(child
, true) )
562 if ( !OnVisit(child
) )
565 child
= m_tree
->GetNextChild(root
, cookie
);
571 // ----------------------------------------------------------------------------
572 // construction and destruction
573 // ----------------------------------------------------------------------------
575 void wxTreeCtrl::Init()
577 m_imageListNormal
= NULL
;
578 m_imageListState
= NULL
;
579 m_ownsImageListNormal
= m_ownsImageListState
= false;
581 m_hasAnyAttr
= false;
584 m_pVirtualRoot
= NULL
;
586 // initialize the global array of events now as it can't be done statically
587 // with the wxEVT_XXX values being allocated during run-time only
588 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
589 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
590 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
591 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
594 bool wxTreeCtrl::Create(wxWindow
*parent
,
599 const wxValidator
& validator
,
600 const wxString
& name
)
604 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
605 style
|= wxBORDER_SUNKEN
;
607 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
611 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
612 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
614 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
615 wstyle
|= TVS_HASLINES
;
616 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
617 wstyle
|= TVS_HASBUTTONS
;
619 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
620 wstyle
|= TVS_EDITLABELS
;
622 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
623 wstyle
|= TVS_LINESATROOT
;
625 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
627 if ( wxTheApp
->GetComCtl32Version() >= 471 )
628 wstyle
|= TVS_FULLROWSELECT
;
631 // using TVS_CHECKBOXES for emulation of a multiselection tree control
632 // doesn't work without the new enough headers
633 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
634 !defined( __GNUWIN32_OLD__ ) && \
635 !defined( __BORLANDC__ ) && \
636 !defined( __WATCOMC__ ) && \
637 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
639 // we emulate the multiple selection tree controls by using checkboxes: set
640 // up the image list we need for this if we do have multiple selections
641 if ( m_windowStyle
& wxTR_MULTIPLE
)
642 wstyle
|= TVS_CHECKBOXES
;
643 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
645 // Create the tree control.
646 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
649 #if wxUSE_COMCTL32_SAFELY
650 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
651 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
653 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
654 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
656 // This works around a bug in the Windows tree control whereby for some versions
657 // of comctrl32, setting any colour actually draws the background in black.
658 // This will initialise the background to the system colour.
659 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
660 // Assume the user has an updated comctl32.dll.
661 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
662 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
663 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
667 // VZ: this is some experimental code which may be used to get the
668 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
669 // AFAIK, the standard DLL does about the same thing anyhow.
671 if ( m_windowStyle
& wxTR_MULTIPLE
)
675 // create the DC compatible with the current screen
676 HDC hdcMem
= CreateCompatibleDC(NULL
);
678 // create a mono bitmap of the standard size
679 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
680 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
681 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
682 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
683 1, // # of color planes
684 1, // # bits needed for one pixel
685 0); // array containing colour data
686 SelectObject(hdcMem
, hbmpCheck
);
688 // then draw a check mark into it
689 RECT rect
= { 0, 0, x
, y
};
690 if ( !::DrawFrameControl(hdcMem
, &rect
,
692 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
694 wxLogLastError(wxT("DrawFrameControl(check)"));
697 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
698 imagelistCheckboxes
.Add(bmp
);
700 if ( !::DrawFrameControl(hdcMem
, &rect
,
704 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
707 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
708 imagelistCheckboxes
.Add(bmp
);
714 SetStateImageList(&imagelistCheckboxes
);
718 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
723 wxTreeCtrl::~wxTreeCtrl()
725 // delete any attributes
728 for ( wxHashTable::compatibility_iterator node
= m_attrs
.Next();
729 node
; node
= m_attrs
.Next() )
731 delete (wxTreeItemAttr
*)node
->GetData();
734 // prevent TVN_DELETEITEM handler from deleting the attributes again!
735 m_hasAnyAttr
= false;
740 // delete user data to prevent memory leaks
741 // also deletes hidden root node storage.
744 if (m_ownsImageListNormal
) delete m_imageListNormal
;
745 if (m_ownsImageListState
) delete m_imageListState
;
748 // ----------------------------------------------------------------------------
750 // ----------------------------------------------------------------------------
752 // simple wrappers which add error checking in debug mode
754 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
756 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
757 _T("can't retrieve virtual root item") );
759 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
761 wxLogLastError(wxT("TreeView_GetItem"));
769 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
771 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
773 wxLogLastError(wxT("TreeView_SetItem"));
777 size_t wxTreeCtrl::GetCount() const
779 return (size_t)TreeView_GetCount(GetHwnd());
782 unsigned int wxTreeCtrl::GetIndent() const
784 return TreeView_GetIndent(GetHwnd());
787 void wxTreeCtrl::SetIndent(unsigned int indent
)
789 TreeView_SetIndent(GetHwnd(), indent
);
792 wxImageList
*wxTreeCtrl::GetImageList() const
794 return m_imageListNormal
;
797 wxImageList
*wxTreeCtrl::GetStateImageList() const
799 return m_imageListState
;
802 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
805 TreeView_SetImageList(GetHwnd(),
806 imageList
? imageList
->GetHIMAGELIST() : 0,
810 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
812 if (m_ownsImageListNormal
)
813 delete m_imageListNormal
;
815 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
816 m_ownsImageListNormal
= false;
819 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
821 if (m_ownsImageListState
) delete m_imageListState
;
822 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
823 m_ownsImageListState
= false;
826 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
828 SetImageList(imageList
);
829 m_ownsImageListNormal
= true;
832 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
834 SetStateImageList(imageList
);
835 m_ownsImageListState
= true;
838 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
839 bool recursively
) const
841 TraverseCounter
counter(this, item
, recursively
);
843 return counter
.GetCount() - 1;
846 // ----------------------------------------------------------------------------
848 // ----------------------------------------------------------------------------
850 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
852 #if !wxUSE_COMCTL32_SAFELY
853 if ( !wxWindowBase::SetBackgroundColour(colour
) )
856 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
862 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
864 #if !wxUSE_COMCTL32_SAFELY
865 if ( !wxWindowBase::SetForegroundColour(colour
) )
868 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
874 // ----------------------------------------------------------------------------
876 // ----------------------------------------------------------------------------
878 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
880 wxChar buf
[512]; // the size is arbitrary...
882 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
883 tvItem
.pszText
= buf
;
884 tvItem
.cchTextMax
= WXSIZEOF(buf
);
885 if ( !DoGetItem(&tvItem
) )
887 // don't return some garbage which was on stack, but an empty string
891 return wxString(buf
);
894 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
896 if ( IS_VIRTUAL_ROOT(item
) )
899 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
900 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
903 // when setting the text of the item being edited, the text control should
904 // be updated to reflect the new text as well, otherwise calling
905 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
907 // don't use GetEditControl() here because m_textCtrl is not set yet
908 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
911 if ( item
== GetSelection() )
913 ::SetWindowText(hwndEdit
, text
);
918 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
919 wxTreeItemIcon which
) const
921 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
922 if ( !DoGetItem(&tvItem
) )
927 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
930 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
932 wxTreeItemIcon which
) const
934 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
935 if ( !DoGetItem(&tvItem
) )
940 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
942 data
->SetImage(image
, which
);
944 // make sure that we have selected images as well
945 if ( which
== wxTreeItemIcon_Normal
&&
946 !data
->HasImage(wxTreeItemIcon_Selected
) )
948 data
->SetImage(image
, wxTreeItemIcon_Selected
);
951 if ( which
== wxTreeItemIcon_Expanded
&&
952 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
954 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
958 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
962 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
963 tvItem
.iSelectedImage
= imageSel
;
964 tvItem
.iImage
= image
;
968 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
969 wxTreeItemIcon which
) const
971 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
973 // TODO: Maybe a hidden root can still provide images?
977 if ( HasIndirectData(item
) )
979 return DoGetItemImageFromData(item
, which
);
986 wxFAIL_MSG( wxT("unknown tree item image type") );
988 case wxTreeItemIcon_Normal
:
992 case wxTreeItemIcon_Selected
:
993 mask
= TVIF_SELECTEDIMAGE
;
996 case wxTreeItemIcon_Expanded
:
997 case wxTreeItemIcon_SelectedExpanded
:
1001 wxTreeViewItem
tvItem(item
, mask
);
1004 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1007 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1008 wxTreeItemIcon which
)
1010 if ( IS_VIRTUAL_ROOT(item
) )
1012 // TODO: Maybe a hidden root can still store images?
1022 wxFAIL_MSG( wxT("unknown tree item image type") );
1025 case wxTreeItemIcon_Normal
:
1027 const int imageNormalOld
= GetItemImage(item
);
1028 const int imageSelOld
= GetItemSelectedImage(item
);
1030 // always set the normal image
1031 imageNormal
= image
;
1033 // if the selected and normal images were the same, they should
1034 // be the same after the update, otherwise leave the selected
1036 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1040 case wxTreeItemIcon_Selected
:
1041 imageNormal
= GetItemImage(item
);
1045 case wxTreeItemIcon_Expanded
:
1046 case wxTreeItemIcon_SelectedExpanded
:
1047 if ( !HasIndirectData(item
) )
1049 // we need to get the old images first, because after we create
1050 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1052 imageNormal
= GetItemImage(item
);
1053 imageSel
= GetItemSelectedImage(item
);
1055 // if it doesn't have it yet, add it
1056 wxTreeItemIndirectData
*data
= new
1057 wxTreeItemIndirectData(this, item
);
1059 // copy the data to the new location
1060 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1061 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1064 DoSetItemImageFromData(item
, image
, which
);
1066 // reset the normal/selected images because we won't use them any
1067 // more - now they're stored inside the indirect data
1069 imageSel
= I_IMAGECALLBACK
;
1073 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1074 // change both normal and selected image - otherwise the change simply
1075 // doesn't take place!
1076 DoSetItemImages(item
, imageNormal
, imageSel
);
1079 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1081 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1083 // Hidden root may have data.
1084 if ( IS_VIRTUAL_ROOT(item
) )
1086 return GET_VIRTUAL_ROOT()->GetData();
1090 if ( !DoGetItem(&tvItem
) )
1095 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1096 if ( IsDataIndirect(data
) )
1098 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1104 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1106 if ( IS_VIRTUAL_ROOT(item
) )
1108 GET_VIRTUAL_ROOT()->SetData(data
);
1111 // first, associate this piece of data with this item
1117 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1119 if ( HasIndirectData(item
) )
1121 if ( DoGetItem(&tvItem
) )
1123 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1127 wxFAIL_MSG( wxT("failed to change tree items data") );
1132 tvItem
.lParam
= (LPARAM
)data
;
1137 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1138 wxTreeItemIndirectData
*data
)
1140 // this should never happen because it's unnecessary and will probably lead
1141 // to crash too because the code elsewhere supposes that the pointer the
1142 // wxTreeItemIndirectData has is a real wxItemData and not
1143 // wxTreeItemIndirectData as well
1144 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1146 SetItemData(item
, data
);
1149 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1151 // query the item itself
1152 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1153 if ( !DoGetItem(&tvItem
) )
1158 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1160 return data
&& IsDataIndirect(data
);
1163 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1165 if ( IS_VIRTUAL_ROOT(item
) )
1168 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1169 tvItem
.cChildren
= (int)has
;
1173 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1175 if ( IS_VIRTUAL_ROOT(item
) )
1178 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1179 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1183 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1185 if ( IS_VIRTUAL_ROOT(item
) )
1188 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1189 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1193 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1195 if ( IS_VIRTUAL_ROOT(item
) )
1199 if ( GetBoundingRect(item
, rect
) )
1205 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1207 long id
= (long)(WXHTREEITEM
)item
;
1208 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1211 return wxNullColour
;
1214 return attr
->GetTextColour();
1217 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1219 long id
= (long)(WXHTREEITEM
)item
;
1220 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1223 return wxNullColour
;
1226 return attr
->GetBackgroundColour();
1229 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1231 long id
= (long)(WXHTREEITEM
)item
;
1232 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1238 return attr
->GetFont();
1241 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1242 const wxColour
& col
)
1244 m_hasAnyAttr
= true;
1246 long id
= (long)(WXHTREEITEM
)item
;
1247 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1250 attr
= new wxTreeItemAttr
;
1251 m_attrs
.Put(id
, (wxObject
*)attr
);
1254 attr
->SetTextColour(col
);
1259 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1260 const wxColour
& col
)
1262 m_hasAnyAttr
= true;
1264 long id
= (long)(WXHTREEITEM
)item
;
1265 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1268 attr
= new wxTreeItemAttr
;
1269 m_attrs
.Put(id
, (wxObject
*)attr
);
1272 attr
->SetBackgroundColour(col
);
1277 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1279 m_hasAnyAttr
= true;
1281 long id
= (long)(WXHTREEITEM
)item
;
1282 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1285 attr
= new wxTreeItemAttr
;
1286 m_attrs
.Put(id
, (wxObject
*)attr
);
1289 attr
->SetFont(font
);
1294 // ----------------------------------------------------------------------------
1296 // ----------------------------------------------------------------------------
1298 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1300 if ( item
== wxTreeItemId(TVI_ROOT
) )
1302 // virtual (hidden) root is never visible
1306 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1309 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1310 // the HTREEITEM with TVM_GETITEMRECT
1311 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1313 // false means get item rect for the whole item, not only text
1314 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1317 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1319 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1322 return tvItem
.cChildren
!= 0;
1325 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1327 // probably not a good idea to put it here
1328 //wxASSERT( ItemHasChildren(item) );
1330 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1333 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1336 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1338 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1341 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1344 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1346 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1349 return (tvItem
.state
& TVIS_BOLD
) != 0;
1352 // ----------------------------------------------------------------------------
1354 // ----------------------------------------------------------------------------
1356 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1358 // Root may be real (visible) or virtual (hidden).
1359 if ( GET_VIRTUAL_ROOT() )
1362 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1365 wxTreeItemId
wxTreeCtrl::GetSelection() const
1367 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1368 wxT("this only works with single selection controls") );
1370 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1373 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1377 if ( IS_VIRTUAL_ROOT(item
) )
1379 // no parent for the virtual root
1384 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1385 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1387 // the top level items should have the virtual root as their parent
1392 return wxTreeItemId((WXHTREEITEM
)hItem
);
1395 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1396 long& _cookie
) const
1398 // remember the last child returned in 'cookie'
1399 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1401 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1404 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1405 long& _cookie
) const
1407 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1414 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1416 // can this be done more efficiently?
1419 wxTreeItemId childLast
,
1420 child
= GetFirstChild(item
, cookie
);
1421 while ( child
.IsOk() )
1424 child
= GetNextChild(item
, cookie
);
1430 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1432 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1435 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1437 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1440 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1442 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1445 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1447 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1449 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1452 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1454 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1456 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1459 // ----------------------------------------------------------------------------
1460 // multiple selections emulation
1461 // ----------------------------------------------------------------------------
1463 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1465 // receive the desired information.
1466 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1469 // state image indices are 1 based
1470 return ((tvItem
.state
>> 12) - 1) == 1;
1473 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1475 // receive the desired information.
1476 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1480 // state images are one-based
1481 tvItem
.state
= (check
? 2 : 1) << 12;
1486 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1488 TraverseSelections
selector(this, selections
);
1490 return selector
.GetCount();
1493 // ----------------------------------------------------------------------------
1495 // ----------------------------------------------------------------------------
1497 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1498 wxTreeItemId hInsertAfter
,
1499 const wxString
& text
,
1500 int image
, int selectedImage
,
1501 wxTreeItemData
*data
)
1503 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1505 _T("can't have more than one root in the tree") );
1507 TV_INSERTSTRUCT tvIns
;
1508 tvIns
.hParent
= HITEM(parent
);
1509 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1511 // this is how we insert the item as the first child: supply a NULL
1513 if ( !tvIns
.hInsertAfter
)
1515 tvIns
.hInsertAfter
= TVI_FIRST
;
1519 if ( !text
.IsEmpty() )
1522 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1526 tvIns
.item
.pszText
= NULL
;
1527 tvIns
.item
.cchTextMax
= 0;
1533 tvIns
.item
.iImage
= image
;
1535 if ( selectedImage
== -1 )
1537 // take the same image for selected icon if not specified
1538 selectedImage
= image
;
1542 if ( selectedImage
!= -1 )
1544 mask
|= TVIF_SELECTEDIMAGE
;
1545 tvIns
.item
.iSelectedImage
= selectedImage
;
1551 tvIns
.item
.lParam
= (LPARAM
)data
;
1554 tvIns
.item
.mask
= mask
;
1556 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1559 wxLogLastError(wxT("TreeView_InsertItem"));
1564 // associate the application tree item with Win32 tree item handle
1565 data
->SetId((WXHTREEITEM
)id
);
1568 return wxTreeItemId((WXHTREEITEM
)id
);
1571 // for compatibility only
1572 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1573 const wxString
& text
,
1574 int image
, int selImage
,
1577 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1578 image
, selImage
, NULL
);
1581 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1582 int image
, int selectedImage
,
1583 wxTreeItemData
*data
)
1586 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1588 // create a virtual root item, the parent for all the others
1589 m_pVirtualRoot
= new wxVirtualNode(data
);
1594 return DoInsertItem(wxTreeItemId((long)(WXHTREEITEM
) 0), (long)(WXHTREEITEM
) 0,
1595 text
, image
, selectedImage
, data
);
1598 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1599 const wxString
& text
,
1600 int image
, int selectedImage
,
1601 wxTreeItemData
*data
)
1603 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1604 text
, image
, selectedImage
, data
);
1607 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1608 const wxTreeItemId
& idPrevious
,
1609 const wxString
& text
,
1610 int image
, int selectedImage
,
1611 wxTreeItemData
*data
)
1613 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1616 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1618 const wxString
& text
,
1619 int image
, int selectedImage
,
1620 wxTreeItemData
*data
)
1622 // find the item from index
1624 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1625 while ( index
!= 0 && idCur
.IsOk() )
1630 idCur
= GetNextChild(parent
, cookie
);
1633 // assert, not check: if the index is invalid, we will append the item
1635 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1637 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1640 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1641 const wxString
& text
,
1642 int image
, int selectedImage
,
1643 wxTreeItemData
*data
)
1645 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1646 text
, image
, selectedImage
, data
);
1649 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1651 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1653 wxLogLastError(wxT("TreeView_DeleteItem"));
1657 // delete all children (but don't delete the item itself)
1658 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1662 wxArrayLong children
;
1663 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1664 while ( child
.IsOk() )
1666 children
.Add((long)(WXHTREEITEM
)child
);
1668 child
= GetNextChild(item
, cookie
);
1671 size_t nCount
= children
.Count();
1672 for ( size_t n
= 0; n
< nCount
; n
++ )
1674 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1676 wxLogLastError(wxT("TreeView_DeleteItem"));
1681 void wxTreeCtrl::DeleteAllItems()
1683 // delete the "virtual" root item.
1684 if ( GET_VIRTUAL_ROOT() )
1686 delete GET_VIRTUAL_ROOT();
1687 m_pVirtualRoot
= NULL
;
1690 // and all the real items
1692 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1694 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1698 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1700 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1701 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1702 flag
== TVE_EXPAND
||
1704 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1706 // A hidden root can be neither expanded nor collapsed.
1707 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1708 wxT("Can't expand/collapse hidden root node!") )
1710 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1711 // emulate them. This behaviour has changed slightly with comctl32.dll
1712 // v 4.70 - now it does send them but only the first time. To maintain
1713 // compatible behaviour and also in order to not have surprises with the
1714 // future versions, don't rely on this and still do everything ourselves.
1715 // To avoid that the messages be sent twice when the item is expanded for
1716 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1718 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1722 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1724 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1725 event
.m_item
= item
;
1726 event
.SetEventObject(this);
1728 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1730 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1734 (void)GetEventHandler()->ProcessEvent(event
);
1736 //else: change didn't took place, so do nothing at all
1739 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1741 DoExpand(item
, TVE_EXPAND
);
1744 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1746 DoExpand(item
, TVE_COLLAPSE
);
1749 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1751 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1754 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1756 DoExpand(item
, TVE_TOGGLE
);
1759 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1761 DoExpand(item
, action
);
1764 void wxTreeCtrl::Unselect()
1766 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1767 wxT("doesn't make sense, may be you want UnselectAll()?") );
1769 // just remove the selection
1770 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1773 void wxTreeCtrl::UnselectAll()
1775 if ( m_windowStyle
& wxTR_MULTIPLE
)
1777 wxArrayTreeItemIds selections
;
1778 size_t count
= GetSelections(selections
);
1779 for ( size_t n
= 0; n
< count
; n
++ )
1781 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1782 SetItemCheck(selections
[n
], false);
1783 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1784 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1785 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1790 // just remove the selection
1795 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1797 if ( m_windowStyle
& wxTR_MULTIPLE
)
1799 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1800 // selecting the item means checking it
1802 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1803 ::SelectItem(GetHwnd(), HITEM(item
));
1804 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1808 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1809 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1810 // send them ourselves
1812 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1813 event
.m_item
= item
;
1814 event
.SetEventObject(this);
1816 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1817 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1819 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1821 wxLogLastError(wxT("TreeView_SelectItem"));
1825 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1826 (void)GetEventHandler()->ProcessEvent(event
);
1829 //else: program vetoed the change
1833 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1836 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1839 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1841 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1843 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1847 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1852 void wxTreeCtrl::DeleteTextCtrl()
1856 // the HWND corresponding to this control is deleted by the tree
1857 // control itself and we don't know when exactly this happens, so check
1858 // if the window still exists before calling UnsubclassWin()
1859 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1861 m_textCtrl
->SetHWND(0);
1864 m_textCtrl
->UnsubclassWin();
1865 m_textCtrl
->SetHWND(0);
1871 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1872 wxClassInfo
* textControlClass
)
1874 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1878 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1879 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1881 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1890 // textctrl is subclassed in MSWOnNotify
1894 // End label editing, optionally cancelling the edit
1895 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1897 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1902 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1904 TV_HITTESTINFO hitTestInfo
;
1905 hitTestInfo
.pt
.x
= (int)point
.x
;
1906 hitTestInfo
.pt
.y
= (int)point
.y
;
1908 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1913 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1914 flags |= wxTREE_HITTEST_##flag
1916 TRANSLATE_FLAG(ABOVE
);
1917 TRANSLATE_FLAG(BELOW
);
1918 TRANSLATE_FLAG(NOWHERE
);
1919 TRANSLATE_FLAG(ONITEMBUTTON
);
1920 TRANSLATE_FLAG(ONITEMICON
);
1921 TRANSLATE_FLAG(ONITEMINDENT
);
1922 TRANSLATE_FLAG(ONITEMLABEL
);
1923 TRANSLATE_FLAG(ONITEMRIGHT
);
1924 TRANSLATE_FLAG(ONITEMSTATEICON
);
1925 TRANSLATE_FLAG(TOLEFT
);
1926 TRANSLATE_FLAG(TORIGHT
);
1928 #undef TRANSLATE_FLAG
1930 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1933 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1935 bool textOnly
) const
1939 // Virtual root items have no bounding rectangle
1940 if ( IS_VIRTUAL_ROOT(item
) )
1945 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1948 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1954 // couldn't retrieve rect: for example, item isn't visible
1959 // ----------------------------------------------------------------------------
1961 // ----------------------------------------------------------------------------
1963 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1964 // functions such as IsDataIndirect()
1965 class wxTreeSortHelper
1968 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1971 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1973 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1974 if ( tree
->IsDataIndirect(data
) )
1976 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1979 return data
->GetId();
1983 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1987 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1988 wxT("sorting tree without data doesn't make sense") );
1990 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1992 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
1993 GetIdFromData(tree
, pItem2
));
1996 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1997 const wxTreeItemId
& item2
)
1999 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2002 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2004 // rely on the fact that TreeView_SortChildren does the same thing as our
2005 // default behaviour, i.e. sorts items alphabetically and so call it
2006 // directly if we're not in derived class (much more efficient!)
2007 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2009 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2014 tvSort
.hParent
= HITEM(item
);
2015 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2016 tvSort
.lParam
= (LPARAM
)this;
2017 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2021 // ----------------------------------------------------------------------------
2023 // ----------------------------------------------------------------------------
2025 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2027 if ( cmd
== EN_UPDATE
)
2029 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2030 event
.SetEventObject( this );
2031 ProcessCommand(event
);
2033 else if ( cmd
== EN_KILLFOCUS
)
2035 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2036 event
.SetEventObject( this );
2037 ProcessCommand(event
);
2045 // command processed
2049 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2050 // only do it during dragging, minimize wxWin overhead (this is important for
2051 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2052 // instead of passing by wxWin events
2053 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2055 bool processed
= false;
2057 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2059 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2061 // we only process mouse messages here and these parameters have the
2062 // same meaning for all of them
2063 int x
= GET_X_LPARAM(lParam
),
2064 y
= GET_Y_LPARAM(lParam
);
2065 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2069 case WM_RBUTTONDOWN
:
2070 // if the item we are about to right click on
2071 // is not already select, remove the entire
2072 // previous selection
2073 if (!::IsItemSelected(GetHwnd(), htItem
))
2078 // select item and set the focus to the
2079 // newly selected item
2080 ::SelectItem(GetHwnd(), htItem
);
2081 ::SetFocus(GetHwnd(), htItem
);
2084 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2085 case WM_LBUTTONDOWN
:
2086 if ( htItem
&& isMultiple
)
2088 if ( wParam
& MK_CONTROL
)
2092 // toggle selected state
2093 ToggleItemSelection(GetHwnd(), htItem
);
2095 ::SetFocus(GetHwnd(), htItem
);
2097 // reset on any click without Shift
2102 else if ( wParam
& MK_SHIFT
)
2104 // this selects all items between the starting one and
2107 if ( !m_htSelStart
)
2109 // take the focused item
2110 m_htSelStart
= (WXHTREEITEM
)
2111 TreeView_GetSelection(GetHwnd());
2114 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2115 !(wParam
& MK_CONTROL
));
2117 ::SetFocus(GetHwnd(), htItem
);
2121 else // normal click
2123 // avoid doing anything if we click on the only
2124 // currently selected item
2126 wxArrayTreeItemIds selections
;
2127 size_t count
= GetSelections(selections
);
2130 HITEM(selections
[0]) != htItem
)
2132 // clear the previously selected items, if the
2133 // user clicked outside of the present selection.
2134 // otherwise, perform the deselection on mouse-up.
2135 // this allows multiple drag and drop to work.
2137 if (IsItemSelected(GetHwnd(), htItem
))
2139 ::SetFocus(GetHwnd(), htItem
);
2145 // prevent the click from starting in-place editing
2146 // which should only happen if we click on the
2147 // already selected item (and nothing else is
2150 TreeView_SelectItem(GetHwnd(), 0);
2151 ::SelectItem(GetHwnd(), htItem
);
2155 // reset on any click without Shift
2160 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2165 m_dragImage
->Move(wxPoint(x
, y
));
2168 // highlight the item as target (hiding drag image is
2169 // necessary - otherwise the display will be corrupted)
2170 m_dragImage
->Hide();
2171 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2172 m_dragImage
->Show();
2179 // facilitates multiple drag-and-drop
2180 if (htItem
&& isMultiple
)
2182 wxArrayTreeItemIds selections
;
2183 size_t count
= GetSelections(selections
);
2186 !(wParam
& MK_CONTROL
) &&
2187 !(wParam
& MK_SHIFT
))
2190 TreeView_SelectItem(GetHwnd(), htItem
);
2199 m_dragImage
->EndDrag();
2203 // generate the drag end event
2204 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2206 event
.m_item
= (WXHTREEITEM
)htItem
;
2207 event
.m_pointDrag
= wxPoint(x
, y
);
2208 event
.SetEventObject(this);
2210 (void)GetEventHandler()->ProcessEvent(event
);
2212 // if we don't do it, the tree seems to think that 2 items
2213 // are selected simultaneously which is quite weird
2214 TreeView_SelectDropTarget(GetHwnd(), 0);
2219 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2220 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2222 // the tree control greys out the selected item when it loses focus and
2223 // paints it as selected again when it regains it, but it won't do it
2224 // for the other items itself - help it
2225 wxArrayTreeItemIds selections
;
2226 size_t count
= GetSelections(selections
);
2228 for ( size_t n
= 0; n
< count
; n
++ )
2230 // TreeView_GetItemRect() will return false if item is not visible,
2231 // which may happen perfectly well
2232 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2235 ::InvalidateRect(GetHwnd(), &rect
, false);
2239 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2241 bool bCtrl
= wxIsCtrlDown(),
2242 bShift
= wxIsShiftDown();
2244 // we handle.arrows and space, but not page up/down and home/end: the
2245 // latter should be easy, but not the former
2247 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2248 if ( !m_htSelStart
)
2250 m_htSelStart
= (WXHTREEITEM
)htSel
;
2253 if ( wParam
== VK_SPACE
)
2257 ToggleItemSelection(GetHwnd(), htSel
);
2263 ::SelectItem(GetHwnd(), htSel
);
2268 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2270 if ( !bCtrl
&& !bShift
)
2272 // no modifiers, just clear selection and then let the default
2273 // processing to take place
2278 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2280 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2281 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2282 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2286 // at the top/bottom
2292 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2296 // without changing selection
2297 ::SetFocus(GetHwnd(), htNext
);
2304 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2305 else if ( nMsg
== WM_CHAR
)
2307 // don't let the control process Space and Return keys because it
2308 // doesn't do anything useful with them anyhow but always beeps
2309 // annoyingly when it receives them and there is no way to turn it off
2310 // simply if you just process TREEITEM_ACTIVATED event to which Space
2311 // and Enter presses are mapped in your code
2312 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2319 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2324 // process WM_NOTIFY Windows message
2325 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2327 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2328 wxEventType eventType
= wxEVT_NULL
;
2329 NMHDR
*hdr
= (NMHDR
*)lParam
;
2331 switch ( hdr
->code
)
2334 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2337 case TVN_BEGINRDRAG
:
2339 if ( eventType
== wxEVT_NULL
)
2340 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2341 //else: left drag, already set above
2343 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2345 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2346 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2348 // don't allow dragging by default: the user code must
2349 // explicitly say that it wants to allow it to avoid breaking
2355 case TVN_BEGINLABELEDIT
:
2357 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2358 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2360 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2361 event
.m_label
= info
->item
.pszText
;
2362 event
.m_editCancelled
= false;
2366 case TVN_DELETEITEM
:
2368 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2369 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2371 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2375 delete (wxTreeItemAttr
*)m_attrs
.
2376 Delete((long)tv
->itemOld
.hItem
);
2381 case TVN_ENDLABELEDIT
:
2383 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2384 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2386 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2387 event
.m_label
= info
->item
.pszText
;
2388 if (info
->item
.pszText
== NULL
)
2390 event
.m_editCancelled
= true;
2394 event
.m_editCancelled
= false;
2399 case TVN_GETDISPINFO
:
2400 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2403 case TVN_SETDISPINFO
:
2405 if ( eventType
== wxEVT_NULL
)
2406 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2407 //else: get, already set above
2409 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2411 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2415 case TVN_ITEMEXPANDING
:
2416 case TVN_ITEMEXPANDED
:
2418 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2421 switch ( tv
->action
)
2424 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2432 what
= IDX_COLLAPSE
;
2436 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2439 eventType
= gs_expandEvents
[what
][how
];
2441 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2447 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2448 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2450 // fabricate the lParam and wParam parameters sufficiently
2451 // similar to the ones from a "real" WM_KEYDOWN so that
2452 // CreateKeyEvent() works correctly
2454 // (::GetKeyState(VK_MENU) & 0x100 ? KF_ALTDOWN : 0) << 16;
2455 // Returns different negative values on WinME and WinNT,
2456 // so simply test for negative value.
2457 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2459 WXWPARAM wParam
= info
->wVKey
;
2461 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2464 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2469 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2474 // a separate event for Space/Return
2475 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2476 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2478 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2480 event2
.SetEventObject(this);
2481 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2483 event2
.m_item
= GetSelection();
2485 //else: don't know how to get it
2487 (void)GetEventHandler()->ProcessEvent(event2
);
2492 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2493 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2494 // we have to handle both messages:
2495 case TVN_SELCHANGEDA
:
2496 case TVN_SELCHANGEDW
:
2497 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2500 case TVN_SELCHANGINGA
:
2501 case TVN_SELCHANGINGW
:
2503 if ( eventType
== wxEVT_NULL
)
2504 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2505 //else: already set above
2507 if (hdr
->code
== TVN_SELCHANGINGW
||
2508 hdr
->code
== TVN_SELCHANGEDW
)
2510 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2511 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2512 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2516 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2517 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2518 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2523 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !( defined(__GNUWIN32__) && !wxCHECK_W32API_VERSION( 1, 0 ) )
2526 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2527 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2528 switch ( nmcd
.dwDrawStage
)
2531 // if we've got any items with non standard attributes,
2532 // notify us before painting each item
2533 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2537 case CDDS_ITEMPREPAINT
:
2539 wxTreeItemAttr
*attr
=
2540 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2544 // nothing to do for this item
2545 *result
= CDRF_DODEFAULT
;
2550 if ( attr
->HasFont() )
2552 hFont
= GetHfontOf(attr
->GetFont());
2560 if ( attr
->HasTextColour() )
2562 colText
= attr
->GetTextColour();
2566 colText
= GetForegroundColour();
2569 // selection colours should override ours
2570 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2573 ::GetSysColor(COLOR_HIGHLIGHT
);
2575 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2580 if ( attr
->HasBackgroundColour() )
2582 colBack
= attr
->GetBackgroundColour();
2586 colBack
= GetBackgroundColour();
2589 lptvcd
->clrText
= wxColourToRGB(colText
);
2590 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2593 // note that if we wanted to set colours for
2594 // individual columns (subitems), we would have
2595 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2598 ::SelectObject(nmcd
.hdc
, hFont
);
2600 *result
= CDRF_NEWFONT
;
2604 *result
= CDRF_DODEFAULT
;
2610 *result
= CDRF_DODEFAULT
;
2614 // we always process it
2616 #endif // _WIN32_IE >= 0x300
2620 DWORD pos
= GetMessagePos();
2622 point
.x
= LOWORD(pos
);
2623 point
.y
= HIWORD(pos
);
2624 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2626 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2627 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2629 event
.m_item
= item
;
2630 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2638 TV_HITTESTINFO tvhti
;
2639 ::GetCursorPos(&tvhti
.pt
);
2640 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2641 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2643 if ( tvhti
.flags
& TVHT_ONITEM
)
2645 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2646 eventType
= (int)hdr
->code
== NM_DBLCLK
2647 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2648 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2650 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2651 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2660 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2663 event
.SetEventObject(this);
2664 event
.SetEventType(eventType
);
2666 bool processed
= GetEventHandler()->ProcessEvent(event
);
2669 switch ( hdr
->code
)
2672 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2673 // the return code of this event handler as the return value for
2674 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2675 // expanded status would never work
2680 case TVN_BEGINRDRAG
:
2681 if ( event
.IsAllowed() )
2683 // normally this is impossible because the m_dragImage is
2684 // deleted once the drag operation is over
2685 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2687 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2688 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2689 m_dragImage
->Show();
2693 case TVN_DELETEITEM
:
2695 // NB: we might process this message using wxWindows event
2696 // tables, but due to overhead of wxWin event system we
2697 // prefer to do it here ourself (otherwise deleting a tree
2698 // with many items is just too slow)
2699 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2701 wxTreeItemId item
= event
.m_item
;
2702 if ( HasIndirectData(item
) )
2704 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2706 delete data
; // can't be NULL here
2710 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2711 delete data
; // may be NULL, ok
2714 processed
= true; // Make sure we don't get called twice
2718 case TVN_BEGINLABELEDIT
:
2719 // return true to cancel label editing
2720 *result
= !event
.IsAllowed();
2721 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2722 if(event
.IsAllowed())
2724 HWND hText
= TreeView_GetEditControl(GetHwnd());
2727 // MBN: if m_textCtrl already has an HWND, it is a stale
2728 // pointer from a previous edit (because the user
2729 // didn't modify the label before dismissing the control,
2730 // and TVN_ENDLABELEDIT was not sent), so delete it
2731 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2734 m_textCtrl
= new wxTextCtrl();
2735 m_textCtrl
->SetParent(this);
2736 m_textCtrl
->SetHWND((WXHWND
)hText
);
2737 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2739 // set wxTE_PROCESS_ENTER style for the text control to
2740 // force it to process the Enter presses itself, otherwise
2741 // they could be stolen from it by the dialog
2743 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2744 | wxTE_PROCESS_ENTER
);
2749 case TVN_ENDLABELEDIT
:
2750 // return true to set the label to the new string: note that we
2751 // also must pretend that we did process the message or it is going
2752 // to be passed to DefWindowProc() which will happily return false
2753 // cancelling the label change
2754 *result
= event
.IsAllowed();
2757 // ensure that we don't have the text ctrl which is going to be
2762 case TVN_SELCHANGING
:
2763 case TVN_ITEMEXPANDING
:
2764 // return true to prevent the action from happening
2765 *result
= !event
.IsAllowed();
2768 case TVN_ITEMEXPANDED
:
2769 // the item is not refreshed properly after expansion when it has
2770 // an image depending on the expanded/collapsed state - bug in
2771 // comctl32.dll or our code?
2773 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2774 wxTreeItemId id
= (WXHTREEITEM
)tv
->itemNew
.hItem
;
2776 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2784 case TVN_GETDISPINFO
:
2785 // NB: so far the user can't set the image himself anyhow, so do it
2786 // anyway - but this may change later
2787 //if ( /* !processed && */ 1 )
2789 wxTreeItemId item
= event
.m_item
;
2790 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2791 if ( info
->item
.mask
& TVIF_IMAGE
)
2794 DoGetItemImageFromData
2797 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2798 : wxTreeItemIcon_Normal
2801 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2803 info
->item
.iSelectedImage
=
2804 DoGetItemImageFromData
2807 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2808 : wxTreeItemIcon_Selected
2815 // for the other messages the return value is ignored and there is
2816 // nothing special to do
2821 // ----------------------------------------------------------------------------
2823 // ----------------------------------------------------------------------------
2825 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2826 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2828 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2831 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2832 tvi
.mask
= TVIF_STATE
;
2833 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2835 // Select the specified state, or -1 == cycle to the next one.
2838 TreeView_GetItem(GetHwnd(), &tvi
);
2840 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2841 if ( state
== m_imageListState
->GetImageCount() )
2845 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2846 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2848 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2850 TreeView_SetItem(GetHwnd(), &tvi
);
2853 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2856 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2857 tvi
.mask
= TVIF_STATE
;
2858 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2859 TreeView_GetItem(GetHwnd(), &tvi
);
2861 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2866 #endif // wxUSE_TREECTRL