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 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/msw/private.h"
35 // Set this to 1 to be _absolutely_ sure that repainting will work for all
36 // comctl32.dll versions
37 #define wxUSE_COMCTL32_SAFELY 0
41 #include "wx/dynarray.h"
42 #include "wx/imaglist.h"
43 #include "wx/settings.h"
44 #include "wx/msw/treectrl.h"
45 #include "wx/msw/dragimag.h"
47 // include <commctrl.h> "properly"
48 #include "wx/msw/wrapcctl.h"
50 // macros to hide the cast ugliness
51 // --------------------------------
53 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
54 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
56 // item here is a wxTreeItemId
57 #define HITEM(item) HITEM_PTR((item).m_pItem)
59 // the native control doesn't support multiple selections under MSW and we
60 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
61 // checkboxes be the selection status (checked == selected) or by really
62 // emulating everything, i.e. intercepting mouse and key events &c. The first
63 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
65 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // wrapper for TreeView_HitTest
72 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
78 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
81 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
83 // wrappers for TreeView_GetItem/TreeView_SetItem
84 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
88 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
89 tvi
.stateMask
= TVIS_SELECTED
;
92 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
94 wxLogLastError(wxT("TreeView_GetItem"));
97 return (tvi
.state
& TVIS_SELECTED
) != 0;
100 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
103 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
104 tvi
.stateMask
= TVIS_SELECTED
;
105 tvi
.state
= select
? TVIS_SELECTED
: 0;
108 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
110 wxLogLastError(wxT("TreeView_SetItem"));
114 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
116 SelectItem(hwndTV
, htItem
, false);
119 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
121 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
124 // helper function which selects all items in a range and, optionally,
125 // unselects all others
126 static void SelectRange(HWND hwndTV
,
129 bool unselectOthers
= true)
131 // find the first (or last) item and select it
133 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
134 while ( htItem
&& cont
)
136 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
138 if ( !IsItemSelected(hwndTV
, htItem
) )
140 SelectItem(hwndTV
, htItem
);
147 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
149 UnselectItem(hwndTV
, htItem
);
153 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
156 // select the items in range
157 cont
= htFirst
!= htLast
;
158 while ( htItem
&& cont
)
160 if ( !IsItemSelected(hwndTV
, htItem
) )
162 SelectItem(hwndTV
, htItem
);
165 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
167 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
171 if ( unselectOthers
)
175 if ( IsItemSelected(hwndTV
, htItem
) )
177 UnselectItem(hwndTV
, htItem
);
180 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
184 // seems to be necessary - otherwise the just selected items don't always
185 // appear as selected
186 UpdateWindow(hwndTV
);
189 // helper function which tricks the standard control into changing the focused
190 // item without changing anything else (if someone knows why Microsoft doesn't
191 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
192 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
195 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
200 if ( htItem
!= htFocus
)
202 // remember the selection state of the item
203 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
205 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
207 // prevent the tree from unselecting the old focus which it
208 // would do by default (TreeView_SelectItem unselects the
210 TreeView_SelectItem(hwndTV
, 0);
211 SelectItem(hwndTV
, htFocus
);
214 TreeView_SelectItem(hwndTV
, htItem
);
218 // need to clear the selection which TreeView_SelectItem() gave
220 UnselectItem(hwndTV
, htItem
);
222 //else: was selected, still selected - ok
224 //else: nothing to do, focus already there
230 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
232 // just clear the focus
233 TreeView_SelectItem(hwndTV
, 0);
235 if ( wasFocusSelected
)
237 // restore the selection state
238 SelectItem(hwndTV
, htFocus
);
241 //else: nothing to do, no focus already
245 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // a convenient wrapper around TV_ITEM struct which adds a ctor
253 #pragma warning( disable : 4097 ) // inheriting from typedef
256 struct wxTreeViewItem
: public TV_ITEM
258 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
259 UINT mask_
, // fields which are valid
260 UINT stateMask_
= 0) // for TVIF_STATE only
264 // hItem member is always valid
265 mask
= mask_
| TVIF_HANDLE
;
266 stateMask
= stateMask_
;
271 // wxVirutalNode is used in place of a single root when 'hidden' root is
273 class wxVirtualNode
: public wxTreeViewItem
276 wxVirtualNode(wxTreeItemData
*data
)
277 : wxTreeViewItem(TVI_ROOT
, 0)
287 wxTreeItemData
*GetData() const { return m_data
; }
288 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
291 wxTreeItemData
*m_data
;
293 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
297 #pragma warning( default : 4097 )
300 // a macro to get the virtual root, returns NULL if none
301 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
303 // returns true if the item is the virtual root
304 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
306 // a class which encapsulates the tree traversal logic: it vists all (unless
307 // OnVisit() returns false) items under the given one
308 class wxTreeTraversal
311 wxTreeTraversal(const wxTreeCtrl
*tree
)
316 // do traverse the tree: visit all items (recursively by default) under the
317 // given one; return true if all items were traversed or false if the
318 // traversal was aborted because OnVisit returned false
319 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
321 // override this function to do whatever is needed for each item, return
322 // false to stop traversing
323 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
326 const wxTreeCtrl
*GetTree() const { return m_tree
; }
329 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
331 const wxTreeCtrl
*m_tree
;
333 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
336 // internal class for getting the selected items
337 class TraverseSelections
: public wxTreeTraversal
340 TraverseSelections(const wxTreeCtrl
*tree
,
341 wxArrayTreeItemIds
& selections
)
342 : wxTreeTraversal(tree
), m_selections(selections
)
344 m_selections
.Empty();
346 DoTraverse(tree
->GetRootItem());
349 virtual bool OnVisit(const wxTreeItemId
& item
)
351 // can't visit a virtual node.
352 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
357 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
358 if ( GetTree()->IsItemChecked(item
) )
360 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
363 m_selections
.Add(item
);
369 size_t GetCount() const { return m_selections
.GetCount(); }
372 wxArrayTreeItemIds
& m_selections
;
374 DECLARE_NO_COPY_CLASS(TraverseSelections
)
377 // internal class for counting tree items
378 class TraverseCounter
: public wxTreeTraversal
381 TraverseCounter(const wxTreeCtrl
*tree
,
382 const wxTreeItemId
& root
,
384 : wxTreeTraversal(tree
)
388 DoTraverse(root
, recursively
);
391 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
398 size_t GetCount() const { return m_count
; }
403 DECLARE_NO_COPY_CLASS(TraverseCounter
)
406 // ----------------------------------------------------------------------------
407 // This class is needed for support of different images: the Win32 common
408 // control natively supports only 2 images (the normal one and another for the
409 // selected state). We wish to provide support for 2 more of them for folder
410 // items (i.e. those which have children): for expanded state and for expanded
411 // selected state. For this we use this structure to store the additional items
414 // There is only one problem with this: when we retrieve the item's data, we
415 // don't know whether we get a pointer to wxTreeItemData or
416 // wxTreeItemIndirectData. So we always set the item id to an invalid value
417 // in this class and the code using the client data checks for it and retrieves
418 // the real client data in this case.
419 // ----------------------------------------------------------------------------
421 class wxTreeItemIndirectData
: public wxTreeItemData
424 // ctor associates this data with the item and the real item data becomes
425 // available through our GetData() method
426 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
428 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
434 m_data
= tree
->GetItemData(item
);
436 // and set ourselves as the new one
437 tree
->SetIndirectItemData(item
, this);
439 // we must have the invalid value for the item
443 // dtor deletes the associated data as well
444 virtual ~wxTreeItemIndirectData() { delete m_data
; }
447 // get the real data associated with the item
448 wxTreeItemData
*GetData() const { return m_data
; }
450 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
452 // do we have such image?
453 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
455 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
457 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
460 // all the images associated with the item
461 int m_images
[wxTreeItemIcon_Max
];
463 // the real client data
464 wxTreeItemData
*m_data
;
466 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
475 // ----------------------------------------------------------------------------
477 // ----------------------------------------------------------------------------
479 // indices in gs_expandEvents table below
494 // handy table for sending events - it has to be initialized during run-time
495 // now so can't be const any more
496 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
499 but logically it's a const table with the following entries:
502 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
503 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
507 // ============================================================================
509 // ============================================================================
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
517 if ( !OnVisit(root
) )
520 return Traverse(root
, recursively
);
523 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
525 wxTreeItemIdValue cookie
;
526 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
527 while ( child
.IsOk() )
529 // depth first traversal
530 if ( recursively
&& !Traverse(child
, true) )
533 if ( !OnVisit(child
) )
536 child
= m_tree
->GetNextChild(root
, cookie
);
542 // ----------------------------------------------------------------------------
543 // construction and destruction
544 // ----------------------------------------------------------------------------
546 void wxTreeCtrl::Init()
548 m_imageListNormal
= NULL
;
549 m_imageListState
= NULL
;
550 m_ownsImageListNormal
= m_ownsImageListState
= false;
552 m_hasAnyAttr
= false;
554 m_pVirtualRoot
= NULL
;
556 // initialize the global array of events now as it can't be done statically
557 // with the wxEVT_XXX values being allocated during run-time only
558 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
559 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
560 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
561 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
564 bool wxTreeCtrl::Create(wxWindow
*parent
,
569 const wxValidator
& validator
,
570 const wxString
& name
)
574 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
575 style
|= wxBORDER_SUNKEN
;
577 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
581 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
582 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
584 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
585 wstyle
|= TVS_HASLINES
;
586 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
587 wstyle
|= TVS_HASBUTTONS
;
589 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
590 wstyle
|= TVS_EDITLABELS
;
592 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
593 wstyle
|= TVS_LINESATROOT
;
595 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
597 if ( wxTheApp
->GetComCtl32Version() >= 471 )
598 wstyle
|= TVS_FULLROWSELECT
;
601 // using TVS_CHECKBOXES for emulation of a multiselection tree control
602 // doesn't work without the new enough headers
603 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
604 !defined( __GNUWIN32_OLD__ ) && \
605 !defined( __BORLANDC__ ) && \
606 !defined( __WATCOMC__ ) && \
607 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
609 // we emulate the multiple selection tree controls by using checkboxes: set
610 // up the image list we need for this if we do have multiple selections
611 if ( m_windowStyle
& wxTR_MULTIPLE
)
612 wstyle
|= TVS_CHECKBOXES
;
613 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
615 // Create the tree control.
616 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
619 #if wxUSE_COMCTL32_SAFELY
620 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
621 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
623 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
624 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
626 // This works around a bug in the Windows tree control whereby for some versions
627 // of comctrl32, setting any colour actually draws the background in black.
628 // This will initialise the background to the system colour.
629 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
630 // Assume the user has an updated comctl32.dll.
631 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
632 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
633 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
637 // VZ: this is some experimental code which may be used to get the
638 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
639 // AFAIK, the standard DLL does about the same thing anyhow.
641 if ( m_windowStyle
& wxTR_MULTIPLE
)
645 // create the DC compatible with the current screen
646 HDC hdcMem
= CreateCompatibleDC(NULL
);
648 // create a mono bitmap of the standard size
649 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
650 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
651 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
652 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
653 1, // # of color planes
654 1, // # bits needed for one pixel
655 0); // array containing colour data
656 SelectObject(hdcMem
, hbmpCheck
);
658 // then draw a check mark into it
659 RECT rect
= { 0, 0, x
, y
};
660 if ( !::DrawFrameControl(hdcMem
, &rect
,
662 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
664 wxLogLastError(wxT("DrawFrameControl(check)"));
667 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
668 imagelistCheckboxes
.Add(bmp
);
670 if ( !::DrawFrameControl(hdcMem
, &rect
,
674 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
677 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
678 imagelistCheckboxes
.Add(bmp
);
684 SetStateImageList(&imagelistCheckboxes
);
688 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
693 wxTreeCtrl::~wxTreeCtrl()
695 // delete any attributes
698 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
700 // prevent TVN_DELETEITEM handler from deleting the attributes again!
701 m_hasAnyAttr
= false;
706 // delete user data to prevent memory leaks
707 // also deletes hidden root node storage.
710 if (m_ownsImageListNormal
) delete m_imageListNormal
;
711 if (m_ownsImageListState
) delete m_imageListState
;
714 // ----------------------------------------------------------------------------
716 // ----------------------------------------------------------------------------
718 // simple wrappers which add error checking in debug mode
720 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
722 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
723 _T("can't retrieve virtual root item") );
725 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
727 wxLogLastError(wxT("TreeView_GetItem"));
735 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
737 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
739 wxLogLastError(wxT("TreeView_SetItem"));
743 size_t wxTreeCtrl::GetCount() const
745 return (size_t)TreeView_GetCount(GetHwnd());
748 unsigned int wxTreeCtrl::GetIndent() const
750 return TreeView_GetIndent(GetHwnd());
753 void wxTreeCtrl::SetIndent(unsigned int indent
)
755 TreeView_SetIndent(GetHwnd(), indent
);
758 wxImageList
*wxTreeCtrl::GetImageList() const
760 return m_imageListNormal
;
763 wxImageList
*wxTreeCtrl::GetStateImageList() const
765 return m_imageListState
;
768 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
771 TreeView_SetImageList(GetHwnd(),
772 imageList
? imageList
->GetHIMAGELIST() : 0,
776 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
778 if (m_ownsImageListNormal
)
779 delete m_imageListNormal
;
781 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
782 m_ownsImageListNormal
= false;
785 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
787 if (m_ownsImageListState
) delete m_imageListState
;
788 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
789 m_ownsImageListState
= false;
792 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
794 SetImageList(imageList
);
795 m_ownsImageListNormal
= true;
798 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
800 SetStateImageList(imageList
);
801 m_ownsImageListState
= true;
804 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
805 bool recursively
) const
807 TraverseCounter
counter(this, item
, recursively
);
809 return counter
.GetCount() - 1;
812 // ----------------------------------------------------------------------------
814 // ----------------------------------------------------------------------------
816 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
818 #if !wxUSE_COMCTL32_SAFELY
819 if ( !wxWindowBase::SetBackgroundColour(colour
) )
822 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
828 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
830 #if !wxUSE_COMCTL32_SAFELY
831 if ( !wxWindowBase::SetForegroundColour(colour
) )
834 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
840 // ----------------------------------------------------------------------------
842 // ----------------------------------------------------------------------------
844 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
846 wxChar buf
[512]; // the size is arbitrary...
848 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
849 tvItem
.pszText
= buf
;
850 tvItem
.cchTextMax
= WXSIZEOF(buf
);
851 if ( !DoGetItem(&tvItem
) )
853 // don't return some garbage which was on stack, but an empty string
857 return wxString(buf
);
860 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
862 if ( IS_VIRTUAL_ROOT(item
) )
865 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
866 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
869 // when setting the text of the item being edited, the text control should
870 // be updated to reflect the new text as well, otherwise calling
871 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
873 // don't use GetEditControl() here because m_textCtrl is not set yet
874 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
877 if ( item
== GetSelection() )
879 ::SetWindowText(hwndEdit
, text
);
884 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
885 wxTreeItemIcon which
) const
887 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
888 if ( !DoGetItem(&tvItem
) )
893 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
896 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
898 wxTreeItemIcon which
) const
900 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
901 if ( !DoGetItem(&tvItem
) )
906 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
908 data
->SetImage(image
, which
);
910 // make sure that we have selected images as well
911 if ( which
== wxTreeItemIcon_Normal
&&
912 !data
->HasImage(wxTreeItemIcon_Selected
) )
914 data
->SetImage(image
, wxTreeItemIcon_Selected
);
917 if ( which
== wxTreeItemIcon_Expanded
&&
918 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
920 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
924 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
928 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
929 tvItem
.iSelectedImage
= imageSel
;
930 tvItem
.iImage
= image
;
934 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
935 wxTreeItemIcon which
) const
937 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
939 // TODO: Maybe a hidden root can still provide images?
943 if ( HasIndirectData(item
) )
945 return DoGetItemImageFromData(item
, which
);
952 wxFAIL_MSG( wxT("unknown tree item image type") );
954 case wxTreeItemIcon_Normal
:
958 case wxTreeItemIcon_Selected
:
959 mask
= TVIF_SELECTEDIMAGE
;
962 case wxTreeItemIcon_Expanded
:
963 case wxTreeItemIcon_SelectedExpanded
:
967 wxTreeViewItem
tvItem(item
, mask
);
970 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
973 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
974 wxTreeItemIcon which
)
976 if ( IS_VIRTUAL_ROOT(item
) )
978 // TODO: Maybe a hidden root can still store images?
988 wxFAIL_MSG( wxT("unknown tree item image type") );
991 case wxTreeItemIcon_Normal
:
993 const int imageNormalOld
= GetItemImage(item
);
994 const int imageSelOld
=
995 GetItemImage(item
, wxTreeItemIcon_Selected
);
997 // always set the normal image
1000 // if the selected and normal images were the same, they should
1001 // be the same after the update, otherwise leave the selected
1003 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1007 case wxTreeItemIcon_Selected
:
1008 imageNormal
= GetItemImage(item
);
1012 case wxTreeItemIcon_Expanded
:
1013 case wxTreeItemIcon_SelectedExpanded
:
1014 if ( !HasIndirectData(item
) )
1016 // we need to get the old images first, because after we create
1017 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1019 imageNormal
= GetItemImage(item
);
1020 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1022 // if it doesn't have it yet, add it
1023 wxTreeItemIndirectData
*data
= new
1024 wxTreeItemIndirectData(this, item
);
1026 // copy the data to the new location
1027 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1028 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1031 DoSetItemImageFromData(item
, image
, which
);
1033 // reset the normal/selected images because we won't use them any
1034 // more - now they're stored inside the indirect data
1036 imageSel
= I_IMAGECALLBACK
;
1040 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1041 // change both normal and selected image - otherwise the change simply
1042 // doesn't take place!
1043 DoSetItemImages(item
, imageNormal
, imageSel
);
1046 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1048 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1050 // Hidden root may have data.
1051 if ( IS_VIRTUAL_ROOT(item
) )
1053 return GET_VIRTUAL_ROOT()->GetData();
1057 if ( !DoGetItem(&tvItem
) )
1062 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1063 if ( IsDataIndirect(data
) )
1065 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1071 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1073 if ( IS_VIRTUAL_ROOT(item
) )
1075 GET_VIRTUAL_ROOT()->SetData(data
);
1078 // first, associate this piece of data with this item
1084 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1086 if ( HasIndirectData(item
) )
1088 if ( DoGetItem(&tvItem
) )
1090 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1094 wxFAIL_MSG( wxT("failed to change tree items data") );
1099 tvItem
.lParam
= (LPARAM
)data
;
1104 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1105 wxTreeItemIndirectData
*data
)
1107 // this should never happen because it's unnecessary and will probably lead
1108 // to crash too because the code elsewhere supposes that the pointer the
1109 // wxTreeItemIndirectData has is a real wxItemData and not
1110 // wxTreeItemIndirectData as well
1111 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1113 SetItemData(item
, data
);
1116 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1118 // query the item itself
1119 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1120 if ( !DoGetItem(&tvItem
) )
1125 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1127 return data
&& IsDataIndirect(data
);
1130 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1132 if ( IS_VIRTUAL_ROOT(item
) )
1135 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1136 tvItem
.cChildren
= (int)has
;
1140 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1142 if ( IS_VIRTUAL_ROOT(item
) )
1145 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1146 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1150 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1152 if ( IS_VIRTUAL_ROOT(item
) )
1155 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1156 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1160 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1162 if ( IS_VIRTUAL_ROOT(item
) )
1166 if ( GetBoundingRect(item
, rect
) )
1172 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1174 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1176 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1179 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1181 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1183 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1186 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1188 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1190 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1193 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1194 const wxColour
& col
)
1196 wxTreeItemAttr
*attr
;
1197 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1198 if ( it
== m_attrs
.end() )
1200 m_hasAnyAttr
= true;
1202 m_attrs
[item
.m_pItem
] =
1203 attr
= new wxTreeItemAttr
;
1210 attr
->SetTextColour(col
);
1215 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1216 const wxColour
& col
)
1218 wxTreeItemAttr
*attr
;
1219 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1220 if ( it
== m_attrs
.end() )
1222 m_hasAnyAttr
= true;
1224 m_attrs
[item
.m_pItem
] =
1225 attr
= new wxTreeItemAttr
;
1227 else // already in the hash
1232 attr
->SetBackgroundColour(col
);
1237 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1239 wxTreeItemAttr
*attr
;
1240 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1241 if ( it
== m_attrs
.end() )
1243 m_hasAnyAttr
= true;
1245 m_attrs
[item
.m_pItem
] =
1246 attr
= new wxTreeItemAttr
;
1248 else // already in the hash
1253 attr
->SetFont(font
);
1258 // ----------------------------------------------------------------------------
1260 // ----------------------------------------------------------------------------
1262 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1264 if ( item
== wxTreeItemId(TVI_ROOT
) )
1266 // virtual (hidden) root is never visible
1270 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1273 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1274 // the HTREEITEM with TVM_GETITEMRECT
1275 *(HTREEITEM
*)&rect
= HITEM(item
);
1277 // false means get item rect for the whole item, not only text
1278 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1281 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1283 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1286 return tvItem
.cChildren
!= 0;
1289 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1291 // probably not a good idea to put it here
1292 //wxASSERT( ItemHasChildren(item) );
1294 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1297 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1300 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1302 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1305 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1308 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1310 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1313 return (tvItem
.state
& TVIS_BOLD
) != 0;
1316 // ----------------------------------------------------------------------------
1318 // ----------------------------------------------------------------------------
1320 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1322 // Root may be real (visible) or virtual (hidden).
1323 if ( GET_VIRTUAL_ROOT() )
1326 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1329 wxTreeItemId
wxTreeCtrl::GetSelection() const
1331 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1332 wxT("this only works with single selection controls") );
1334 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1337 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1341 if ( IS_VIRTUAL_ROOT(item
) )
1343 // no parent for the virtual root
1348 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1349 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1351 // the top level items should have the virtual root as their parent
1356 return wxTreeItemId(hItem
);
1359 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1360 wxTreeItemIdValue
& cookie
) const
1362 // remember the last child returned in 'cookie'
1363 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1365 return wxTreeItemId(cookie
);
1368 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1369 wxTreeItemIdValue
& cookie
) const
1371 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1372 HITEM(wxTreeItemId(cookie
))));
1373 cookie
= item
.m_pItem
;
1378 #if WXWIN_COMPATIBILITY_2_4
1380 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1383 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1385 return wxTreeItemId((void *)cookie
);
1388 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1391 wxTreeItemId
item(TreeView_GetNextSibling
1394 HITEM(wxTreeItemId((void *)cookie
)
1396 cookie
= (long)item
.m_pItem
;
1401 #endif // WXWIN_COMPATIBILITY_2_4
1403 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1405 // can this be done more efficiently?
1406 wxTreeItemIdValue cookie
;
1408 wxTreeItemId childLast
,
1409 child
= GetFirstChild(item
, cookie
);
1410 while ( child
.IsOk() )
1413 child
= GetNextChild(item
, cookie
);
1419 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1421 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1424 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1426 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1429 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1431 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1434 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1436 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1438 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1441 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1443 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1445 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1448 // ----------------------------------------------------------------------------
1449 // multiple selections emulation
1450 // ----------------------------------------------------------------------------
1452 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1454 // receive the desired information.
1455 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1458 // state image indices are 1 based
1459 return ((tvItem
.state
>> 12) - 1) == 1;
1462 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1464 // receive the desired information.
1465 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1469 // state images are one-based
1470 tvItem
.state
= (check
? 2 : 1) << 12;
1475 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1477 TraverseSelections
selector(this, selections
);
1479 return selector
.GetCount();
1482 // ----------------------------------------------------------------------------
1484 // ----------------------------------------------------------------------------
1486 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1487 wxTreeItemId hInsertAfter
,
1488 const wxString
& text
,
1489 int image
, int selectedImage
,
1490 wxTreeItemData
*data
)
1492 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1494 _T("can't have more than one root in the tree") );
1496 TV_INSERTSTRUCT tvIns
;
1497 tvIns
.hParent
= HITEM(parent
);
1498 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1500 // this is how we insert the item as the first child: supply a NULL
1502 if ( !tvIns
.hInsertAfter
)
1504 tvIns
.hInsertAfter
= TVI_FIRST
;
1508 if ( !text
.IsEmpty() )
1511 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1515 tvIns
.item
.pszText
= NULL
;
1516 tvIns
.item
.cchTextMax
= 0;
1522 tvIns
.item
.iImage
= image
;
1524 if ( selectedImage
== -1 )
1526 // take the same image for selected icon if not specified
1527 selectedImage
= image
;
1531 if ( selectedImage
!= -1 )
1533 mask
|= TVIF_SELECTEDIMAGE
;
1534 tvIns
.item
.iSelectedImage
= selectedImage
;
1540 tvIns
.item
.lParam
= (LPARAM
)data
;
1543 tvIns
.item
.mask
= mask
;
1545 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1548 wxLogLastError(wxT("TreeView_InsertItem"));
1553 // associate the application tree item with Win32 tree item handle
1557 return wxTreeItemId(id
);
1560 // for compatibility only
1561 #if WXWIN_COMPATIBILITY_2_4
1563 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1564 const wxString
& text
,
1565 int image
, int selImage
,
1568 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1569 image
, selImage
, NULL
);
1572 #endif // WXWIN_COMPATIBILITY_2_4
1574 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1575 int image
, int selectedImage
,
1576 wxTreeItemData
*data
)
1579 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1581 // create a virtual root item, the parent for all the others
1582 m_pVirtualRoot
= new wxVirtualNode(data
);
1587 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1588 text
, image
, selectedImage
, data
);
1591 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1592 const wxString
& text
,
1593 int image
, int selectedImage
,
1594 wxTreeItemData
*data
)
1596 return DoInsertItem(parent
, TVI_FIRST
,
1597 text
, image
, selectedImage
, data
);
1600 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1601 const wxTreeItemId
& idPrevious
,
1602 const wxString
& text
,
1603 int image
, int selectedImage
,
1604 wxTreeItemData
*data
)
1606 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1609 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1611 const wxString
& text
,
1612 int image
, int selectedImage
,
1613 wxTreeItemData
*data
)
1615 // find the item from index
1616 wxTreeItemIdValue cookie
;
1617 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1618 while ( index
!= 0 && idCur
.IsOk() )
1623 idCur
= GetNextChild(parent
, cookie
);
1626 // assert, not check: if the index is invalid, we will append the item
1628 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1630 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1633 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1634 const wxString
& text
,
1635 int image
, int selectedImage
,
1636 wxTreeItemData
*data
)
1638 return DoInsertItem(parent
, TVI_LAST
,
1639 text
, image
, selectedImage
, data
);
1642 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1644 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1646 wxLogLastError(wxT("TreeView_DeleteItem"));
1650 // delete all children (but don't delete the item itself)
1651 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1653 wxTreeItemIdValue cookie
;
1655 wxArrayTreeItemIds children
;
1656 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1657 while ( child
.IsOk() )
1659 children
.Add(child
);
1661 child
= GetNextChild(item
, cookie
);
1664 size_t nCount
= children
.Count();
1665 for ( size_t n
= 0; n
< nCount
; n
++ )
1667 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1669 wxLogLastError(wxT("TreeView_DeleteItem"));
1674 void wxTreeCtrl::DeleteAllItems()
1676 // delete the "virtual" root item.
1677 if ( GET_VIRTUAL_ROOT() )
1679 delete GET_VIRTUAL_ROOT();
1680 m_pVirtualRoot
= NULL
;
1683 // and all the real items
1685 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1687 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1691 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1693 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1694 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1695 flag
== TVE_EXPAND
||
1697 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1699 // A hidden root can be neither expanded nor collapsed.
1700 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1701 wxT("Can't expand/collapse hidden root node!") )
1703 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1704 // emulate them. This behaviour has changed slightly with comctl32.dll
1705 // v 4.70 - now it does send them but only the first time. To maintain
1706 // compatible behaviour and also in order to not have surprises with the
1707 // future versions, don't rely on this and still do everything ourselves.
1708 // To avoid that the messages be sent twice when the item is expanded for
1709 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1711 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1715 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1717 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1718 event
.m_item
= item
;
1719 event
.SetEventObject(this);
1721 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1723 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1727 (void)GetEventHandler()->ProcessEvent(event
);
1729 //else: change didn't took place, so do nothing at all
1732 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1734 DoExpand(item
, TVE_EXPAND
);
1737 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1739 DoExpand(item
, TVE_COLLAPSE
);
1742 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1744 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1747 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1749 DoExpand(item
, TVE_TOGGLE
);
1752 #if WXWIN_COMPATIBILITY_2_4
1753 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1755 DoExpand(item
, action
);
1759 void wxTreeCtrl::Unselect()
1761 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1762 wxT("doesn't make sense, may be you want UnselectAll()?") );
1764 // just remove the selection
1765 SelectItem(wxTreeItemId());
1768 void wxTreeCtrl::UnselectAll()
1770 if ( m_windowStyle
& wxTR_MULTIPLE
)
1772 wxArrayTreeItemIds selections
;
1773 size_t count
= GetSelections(selections
);
1774 for ( size_t n
= 0; n
< count
; n
++ )
1776 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1777 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1778 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1779 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1780 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1785 // just remove the selection
1790 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1792 if ( m_windowStyle
& wxTR_MULTIPLE
)
1794 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1795 // selecting the item means checking it
1797 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1798 ::SelectItem(GetHwnd(), HITEM(item
));
1799 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1803 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1804 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1805 // send them ourselves
1807 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1808 event
.m_item
= item
;
1809 event
.SetEventObject(this);
1811 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1812 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1814 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1816 wxLogLastError(wxT("TreeView_SelectItem"));
1820 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1821 (void)GetEventHandler()->ProcessEvent(event
);
1824 //else: program vetoed the change
1828 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1831 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1834 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1836 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1838 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1842 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1847 void wxTreeCtrl::DeleteTextCtrl()
1851 // the HWND corresponding to this control is deleted by the tree
1852 // control itself and we don't know when exactly this happens, so check
1853 // if the window still exists before calling UnsubclassWin()
1854 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1856 m_textCtrl
->SetHWND(0);
1859 m_textCtrl
->UnsubclassWin();
1860 m_textCtrl
->SetHWND(0);
1866 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1867 wxClassInfo
* textControlClass
)
1869 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1873 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1874 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1876 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1885 // textctrl is subclassed in MSWOnNotify
1889 // End label editing, optionally cancelling the edit
1890 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1892 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1897 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1899 TV_HITTESTINFO hitTestInfo
;
1900 hitTestInfo
.pt
.x
= (int)point
.x
;
1901 hitTestInfo
.pt
.y
= (int)point
.y
;
1903 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1908 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1909 flags |= wxTREE_HITTEST_##flag
1911 TRANSLATE_FLAG(ABOVE
);
1912 TRANSLATE_FLAG(BELOW
);
1913 TRANSLATE_FLAG(NOWHERE
);
1914 TRANSLATE_FLAG(ONITEMBUTTON
);
1915 TRANSLATE_FLAG(ONITEMICON
);
1916 TRANSLATE_FLAG(ONITEMINDENT
);
1917 TRANSLATE_FLAG(ONITEMLABEL
);
1918 TRANSLATE_FLAG(ONITEMRIGHT
);
1919 TRANSLATE_FLAG(ONITEMSTATEICON
);
1920 TRANSLATE_FLAG(TOLEFT
);
1921 TRANSLATE_FLAG(TORIGHT
);
1923 #undef TRANSLATE_FLAG
1925 return wxTreeItemId(hitTestInfo
.hItem
);
1928 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1930 bool textOnly
) const
1934 // Virtual root items have no bounding rectangle
1935 if ( IS_VIRTUAL_ROOT(item
) )
1940 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1943 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1949 // couldn't retrieve rect: for example, item isn't visible
1954 // ----------------------------------------------------------------------------
1956 // ----------------------------------------------------------------------------
1958 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1959 // functions such as IsDataIndirect()
1960 class wxTreeSortHelper
1963 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1966 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1968 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1969 if ( tree
->IsDataIndirect(data
) )
1971 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1974 return data
->GetId();
1978 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1982 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1983 wxT("sorting tree without data doesn't make sense") );
1985 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1987 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
1988 GetIdFromData(tree
, pItem2
));
1991 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1992 const wxTreeItemId
& item2
)
1994 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1997 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1999 // rely on the fact that TreeView_SortChildren does the same thing as our
2000 // default behaviour, i.e. sorts items alphabetically and so call it
2001 // directly if we're not in derived class (much more efficient!)
2002 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2004 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2009 tvSort
.hParent
= HITEM(item
);
2010 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2011 tvSort
.lParam
= (LPARAM
)this;
2012 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2016 // ----------------------------------------------------------------------------
2018 // ----------------------------------------------------------------------------
2020 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2022 if ( cmd
== EN_UPDATE
)
2024 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2025 event
.SetEventObject( this );
2026 ProcessCommand(event
);
2028 else if ( cmd
== EN_KILLFOCUS
)
2030 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2031 event
.SetEventObject( this );
2032 ProcessCommand(event
);
2040 // command processed
2044 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2045 // only do it during dragging, minimize wxWin overhead (this is important for
2046 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2047 // instead of passing by wxWin events
2048 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2050 bool processed
= false;
2052 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2054 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2056 // we only process mouse messages here and these parameters have the
2057 // same meaning for all of them
2058 int x
= GET_X_LPARAM(lParam
),
2059 y
= GET_Y_LPARAM(lParam
);
2060 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2064 case WM_RBUTTONDOWN
:
2065 // if the item we are about to right click on
2066 // is not already select, remove the entire
2067 // previous selection
2068 if (!::IsItemSelected(GetHwnd(), htItem
))
2073 // select item and set the focus to the
2074 // newly selected item
2075 ::SelectItem(GetHwnd(), htItem
);
2076 ::SetFocus(GetHwnd(), htItem
);
2079 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2080 case WM_LBUTTONDOWN
:
2081 if ( htItem
&& isMultiple
)
2083 if ( wParam
& MK_CONTROL
)
2087 // toggle selected state
2088 ToggleItemSelection(GetHwnd(), htItem
);
2090 ::SetFocus(GetHwnd(), htItem
);
2092 // reset on any click without Shift
2093 m_htSelStart
.Unset();
2097 else if ( wParam
& MK_SHIFT
)
2099 // this selects all items between the starting one and
2102 if ( !m_htSelStart
)
2104 // take the focused item
2105 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2108 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2109 !(wParam
& MK_CONTROL
));
2111 ::SetFocus(GetHwnd(), htItem
);
2115 else // normal click
2117 // avoid doing anything if we click on the only
2118 // currently selected item
2120 wxArrayTreeItemIds selections
;
2121 size_t count
= GetSelections(selections
);
2124 HITEM_PTR(selections
[0]) != htItem
)
2126 // clear the previously selected items, if the
2127 // user clicked outside of the present selection.
2128 // otherwise, perform the deselection on mouse-up.
2129 // this allows multiple drag and drop to work.
2131 if (IsItemSelected(GetHwnd(), htItem
))
2133 ::SetFocus(GetHwnd(), htItem
);
2139 // prevent the click from starting in-place editing
2140 // which should only happen if we click on the
2141 // already selected item (and nothing else is
2144 TreeView_SelectItem(GetHwnd(), 0);
2145 ::SelectItem(GetHwnd(), htItem
);
2149 // reset on any click without Shift
2150 m_htSelStart
.Unset();
2154 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2159 m_dragImage
->Move(wxPoint(x
, y
));
2162 // highlight the item as target (hiding drag image is
2163 // necessary - otherwise the display will be corrupted)
2164 m_dragImage
->Hide();
2165 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2166 m_dragImage
->Show();
2173 // facilitates multiple drag-and-drop
2174 if (htItem
&& isMultiple
)
2176 wxArrayTreeItemIds selections
;
2177 size_t count
= GetSelections(selections
);
2180 !(wParam
& MK_CONTROL
) &&
2181 !(wParam
& MK_SHIFT
))
2184 TreeView_SelectItem(GetHwnd(), htItem
);
2193 m_dragImage
->EndDrag();
2197 // generate the drag end event
2198 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2200 event
.m_item
= htItem
;
2201 event
.m_pointDrag
= wxPoint(x
, y
);
2202 event
.SetEventObject(this);
2204 (void)GetEventHandler()->ProcessEvent(event
);
2206 // if we don't do it, the tree seems to think that 2 items
2207 // are selected simultaneously which is quite weird
2208 TreeView_SelectDropTarget(GetHwnd(), 0);
2213 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2214 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2216 // the tree control greys out the selected item when it loses focus and
2217 // paints it as selected again when it regains it, but it won't do it
2218 // for the other items itself - help it
2219 wxArrayTreeItemIds selections
;
2220 size_t count
= GetSelections(selections
);
2222 for ( size_t n
= 0; n
< count
; n
++ )
2224 // TreeView_GetItemRect() will return false if item is not visible,
2225 // which may happen perfectly well
2226 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2229 ::InvalidateRect(GetHwnd(), &rect
, false);
2233 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2235 bool bCtrl
= wxIsCtrlDown(),
2236 bShift
= wxIsShiftDown();
2238 // we handle.arrows and space, but not page up/down and home/end: the
2239 // latter should be easy, but not the former
2241 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2242 if ( !m_htSelStart
)
2244 m_htSelStart
= htSel
;
2247 if ( wParam
== VK_SPACE
)
2251 ToggleItemSelection(GetHwnd(), htSel
);
2257 ::SelectItem(GetHwnd(), htSel
);
2262 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2264 if ( !bCtrl
&& !bShift
)
2266 // no modifiers, just clear selection and then let the default
2267 // processing to take place
2272 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2274 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2275 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2276 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2280 // at the top/bottom
2286 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2290 // without changing selection
2291 ::SetFocus(GetHwnd(), htNext
);
2298 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2299 else if ( nMsg
== WM_CHAR
)
2301 // don't let the control process Space and Return keys because it
2302 // doesn't do anything useful with them anyhow but always beeps
2303 // annoyingly when it receives them and there is no way to turn it off
2304 // simply if you just process TREEITEM_ACTIVATED event to which Space
2305 // and Enter presses are mapped in your code
2306 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2313 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2318 // process WM_NOTIFY Windows message
2319 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2321 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2322 wxEventType eventType
= wxEVT_NULL
;
2323 NMHDR
*hdr
= (NMHDR
*)lParam
;
2325 switch ( hdr
->code
)
2328 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2331 case TVN_BEGINRDRAG
:
2333 if ( eventType
== wxEVT_NULL
)
2334 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2335 //else: left drag, already set above
2337 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2339 event
.m_item
= tv
->itemNew
.hItem
;
2340 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2342 // don't allow dragging by default: the user code must
2343 // explicitly say that it wants to allow it to avoid breaking
2349 case TVN_BEGINLABELEDIT
:
2351 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2352 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2354 event
.m_item
= info
->item
.hItem
;
2355 event
.m_label
= info
->item
.pszText
;
2356 event
.m_editCancelled
= false;
2360 case TVN_DELETEITEM
:
2362 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2363 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2365 event
.m_item
= tv
->itemOld
.hItem
;
2369 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2370 if ( it
!= m_attrs
.end() )
2379 case TVN_ENDLABELEDIT
:
2381 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2382 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2384 event
.m_item
= info
->item
.hItem
;
2385 event
.m_label
= info
->item
.pszText
;
2386 if (info
->item
.pszText
== NULL
)
2388 event
.m_editCancelled
= true;
2392 event
.m_editCancelled
= false;
2397 case TVN_GETDISPINFO
:
2398 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2401 case TVN_SETDISPINFO
:
2403 if ( eventType
== wxEVT_NULL
)
2404 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2405 //else: get, already set above
2407 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2409 event
.m_item
= info
->item
.hItem
;
2413 case TVN_ITEMEXPANDING
:
2414 case TVN_ITEMEXPANDED
:
2416 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2419 switch ( tv
->action
)
2422 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2430 what
= IDX_COLLAPSE
;
2434 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2437 eventType
= gs_expandEvents
[what
][how
];
2439 event
.m_item
= tv
->itemNew
.hItem
;
2445 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2446 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2448 // fabricate the lParam and wParam parameters sufficiently
2449 // similar to the ones from a "real" WM_KEYDOWN so that
2450 // CreateKeyEvent() works correctly
2452 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2454 WXWPARAM wParam
= info
->wVKey
;
2456 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2459 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2464 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2469 // a separate event for Space/Return
2470 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2471 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2473 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2475 event2
.SetEventObject(this);
2476 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2478 event2
.m_item
= GetSelection();
2480 //else: don't know how to get it
2482 (void)GetEventHandler()->ProcessEvent(event2
);
2487 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2488 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2489 // we have to handle both messages:
2490 case TVN_SELCHANGEDA
:
2491 case TVN_SELCHANGEDW
:
2492 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2495 case TVN_SELCHANGINGA
:
2496 case TVN_SELCHANGINGW
:
2498 if ( eventType
== wxEVT_NULL
)
2499 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2500 //else: already set above
2502 if (hdr
->code
== TVN_SELCHANGINGW
||
2503 hdr
->code
== TVN_SELCHANGEDW
)
2505 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2506 event
.m_item
= tv
->itemNew
.hItem
;
2507 event
.m_itemOld
= tv
->itemOld
.hItem
;
2511 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2512 event
.m_item
= tv
->itemNew
.hItem
;
2513 event
.m_itemOld
= tv
->itemOld
.hItem
;
2518 // instead of explicitly checking for _WIN32_IE, check if the
2519 // required symbols are available in the headers
2520 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2523 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2524 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2525 switch ( nmcd
.dwDrawStage
)
2528 // if we've got any items with non standard attributes,
2529 // notify us before painting each item
2530 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2534 case CDDS_ITEMPREPAINT
:
2536 wxMapTreeAttr::iterator
2537 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2539 if ( it
== m_attrs
.end() )
2541 // nothing to do for this item
2542 *result
= CDRF_DODEFAULT
;
2546 wxTreeItemAttr
* const attr
= it
->second
;
2549 if ( attr
->HasFont() )
2551 hFont
= GetHfontOf(attr
->GetFont());
2559 if ( attr
->HasTextColour() )
2561 colText
= attr
->GetTextColour();
2565 colText
= GetForegroundColour();
2568 // selection colours should override ours
2569 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2572 ::GetSysColor(COLOR_HIGHLIGHT
);
2574 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2579 if ( attr
->HasBackgroundColour() )
2581 colBack
= attr
->GetBackgroundColour();
2585 colBack
= GetBackgroundColour();
2588 lptvcd
->clrText
= wxColourToRGB(colText
);
2589 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2592 // note that if we wanted to set colours for
2593 // individual columns (subitems), we would have
2594 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2597 ::SelectObject(nmcd
.hdc
, hFont
);
2599 *result
= CDRF_NEWFONT
;
2603 *result
= CDRF_DODEFAULT
;
2609 *result
= CDRF_DODEFAULT
;
2613 // we always process it
2615 #endif // have owner drawn support in headers
2619 DWORD pos
= GetMessagePos();
2621 point
.x
= LOWORD(pos
);
2622 point
.y
= HIWORD(pos
);
2623 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2625 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2626 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2628 event
.m_item
= item
;
2629 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2637 TV_HITTESTINFO tvhti
;
2638 ::GetCursorPos(&tvhti
.pt
);
2639 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2640 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2642 if ( tvhti
.flags
& TVHT_ONITEM
)
2644 event
.m_item
= tvhti
.hItem
;
2645 eventType
= (int)hdr
->code
== NM_DBLCLK
2646 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2647 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2649 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2650 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2659 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2662 event
.SetEventObject(this);
2663 event
.SetEventType(eventType
);
2665 bool processed
= GetEventHandler()->ProcessEvent(event
);
2668 switch ( hdr
->code
)
2671 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2672 // the return code of this event handler as the return value for
2673 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2674 // expanded status would never work
2679 case TVN_BEGINRDRAG
:
2680 if ( event
.IsAllowed() )
2682 // normally this is impossible because the m_dragImage is
2683 // deleted once the drag operation is over
2684 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2686 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2687 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2688 m_dragImage
->Show();
2692 case TVN_DELETEITEM
:
2694 // NB: we might process this message using wxWindows event
2695 // tables, but due to overhead of wxWin event system we
2696 // prefer to do it here ourself (otherwise deleting a tree
2697 // with many items is just too slow)
2698 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2700 wxTreeItemId item
= event
.m_item
;
2701 if ( HasIndirectData(item
) )
2703 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2705 delete data
; // can't be NULL here
2709 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2710 delete data
; // may be NULL, ok
2713 processed
= true; // Make sure we don't get called twice
2717 case TVN_BEGINLABELEDIT
:
2718 // return true to cancel label editing
2719 *result
= !event
.IsAllowed();
2720 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2721 if(event
.IsAllowed())
2723 HWND hText
= TreeView_GetEditControl(GetHwnd());
2726 // MBN: if m_textCtrl already has an HWND, it is a stale
2727 // pointer from a previous edit (because the user
2728 // didn't modify the label before dismissing the control,
2729 // and TVN_ENDLABELEDIT was not sent), so delete it
2730 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2733 m_textCtrl
= new wxTextCtrl();
2734 m_textCtrl
->SetParent(this);
2735 m_textCtrl
->SetHWND((WXHWND
)hText
);
2736 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2738 // set wxTE_PROCESS_ENTER style for the text control to
2739 // force it to process the Enter presses itself, otherwise
2740 // they could be stolen from it by the dialog
2742 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2743 | wxTE_PROCESS_ENTER
);
2748 case TVN_ENDLABELEDIT
:
2749 // return true to set the label to the new string: note that we
2750 // also must pretend that we did process the message or it is going
2751 // to be passed to DefWindowProc() which will happily return false
2752 // cancelling the label change
2753 *result
= event
.IsAllowed();
2756 // ensure that we don't have the text ctrl which is going to be
2761 case TVN_SELCHANGING
:
2762 case TVN_ITEMEXPANDING
:
2763 // return true to prevent the action from happening
2764 *result
= !event
.IsAllowed();
2767 case TVN_ITEMEXPANDED
:
2768 // the item is not refreshed properly after expansion when it has
2769 // an image depending on the expanded/collapsed state - bug in
2770 // comctl32.dll or our code?
2772 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2773 wxTreeItemId
id(tv
->itemNew
.hItem
);
2775 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2783 case TVN_GETDISPINFO
:
2784 // NB: so far the user can't set the image himself anyhow, so do it
2785 // anyway - but this may change later
2786 //if ( /* !processed && */ 1 )
2788 wxTreeItemId item
= event
.m_item
;
2789 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2790 if ( info
->item
.mask
& TVIF_IMAGE
)
2793 DoGetItemImageFromData
2796 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2797 : wxTreeItemIcon_Normal
2800 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2802 info
->item
.iSelectedImage
=
2803 DoGetItemImageFromData
2806 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2807 : wxTreeItemIcon_Selected
2814 // for the other messages the return value is ignored and there is
2815 // nothing special to do
2820 // ----------------------------------------------------------------------------
2822 // ----------------------------------------------------------------------------
2824 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2825 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2827 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2830 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2831 tvi
.mask
= TVIF_STATE
;
2832 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2834 // Select the specified state, or -1 == cycle to the next one.
2837 TreeView_GetItem(GetHwnd(), &tvi
);
2839 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2840 if ( state
== m_imageListState
->GetImageCount() )
2844 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2845 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2847 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2849 TreeView_SetItem(GetHwnd(), &tvi
);
2852 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2855 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2856 tvi
.mask
= TVIF_STATE
;
2857 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2858 TreeView_GetItem(GetHwnd(), &tvi
);
2860 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2863 #endif // wxUSE_TREECTRL