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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
37 #include "wx/settings.h"
40 #include "wx/msw/private.h"
42 // Set this to 1 to be _absolutely_ sure that repainting will work for all
43 // comctl32.dll versions
44 #define wxUSE_COMCTL32_SAFELY 0
46 #include "wx/imaglist.h"
47 #include "wx/msw/dragimag.h"
49 // macros to hide the cast ugliness
50 // --------------------------------
52 // get HTREEITEM from wxTreeItemId
53 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // wrapper for TreeView_HitTest
60 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
66 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // a convenient wrapper around TV_ITEM struct which adds a ctor
75 #pragma warning( disable : 4097 ) // inheriting from typedef
78 struct wxTreeViewItem
: public TV_ITEM
80 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
81 UINT mask_
, // fields which are valid
82 UINT stateMask_
= 0) // for TVIF_STATE only
86 // hItem member is always valid
87 mask
= mask_
| TVIF_HANDLE
;
88 stateMask
= stateMask_
;
93 // ----------------------------------------------------------------------------
94 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
96 // We need this for a couple of reasons:
98 // 1) This class is needed for support of different images: the Win32 common
99 // control natively supports only 2 images (the normal one and another for the
100 // selected state). We wish to provide support for 2 more of them for folder
101 // items (i.e. those which have children): for expanded state and for expanded
102 // selected state. For this we use this structure to store the additional items
105 // 2) This class is also needed to hold the HITEM so that we can sort
106 // it correctly in the MSW sort callback.
108 // In addition it makes other workarounds such as this easier and helps
109 // simplify the code.
110 // ----------------------------------------------------------------------------
112 class wxTreeItemParam
119 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
125 // dtor deletes the associated data as well
126 virtual ~wxTreeItemParam() { delete m_data
; }
129 // get the real data associated with the item
130 wxTreeItemData
*GetData() const { return m_data
; }
132 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
134 // do we have such image?
135 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
136 // get image, falling back to the other images if this one is not
138 int GetImage(wxTreeItemIcon which
) const
140 int image
= m_images
[which
];
145 case wxTreeItemIcon_SelectedExpanded
:
146 image
= GetImage(wxTreeItemIcon_Expanded
);
151 case wxTreeItemIcon_Selected
:
152 case wxTreeItemIcon_Expanded
:
153 image
= GetImage(wxTreeItemIcon_Normal
);
156 case wxTreeItemIcon_Normal
:
161 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
167 // change the given image
168 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
171 const wxTreeItemId
& GetItem() const { return m_item
; }
173 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
176 // all the images associated with the item
177 int m_images
[wxTreeItemIcon_Max
];
179 // item for sort callbacks
182 // the real client data
183 wxTreeItemData
*m_data
;
185 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
188 // wxVirutalNode is used in place of a single root when 'hidden' root is
190 class wxVirtualNode
: public wxTreeViewItem
193 wxVirtualNode(wxTreeItemParam
*param
)
194 : wxTreeViewItem(TVI_ROOT
, 0)
204 wxTreeItemParam
*GetParam() const { return m_param
; }
205 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
208 wxTreeItemParam
*m_param
;
210 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
214 #pragma warning( default : 4097 )
217 // a macro to get the virtual root, returns NULL if none
218 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
220 // returns true if the item is the virtual root
221 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
223 // a class which encapsulates the tree traversal logic: it vists all (unless
224 // OnVisit() returns false) items under the given one
225 class wxTreeTraversal
228 wxTreeTraversal(const wxTreeCtrl
*tree
)
233 // give it a virtual dtor: not really needed as the class is never used
234 // polymorphically and not even allocated on heap at all, but this is safer
235 // (in case it ever is) and silences the compiler warnings for now
236 virtual ~wxTreeTraversal() { }
238 // do traverse the tree: visit all items (recursively by default) under the
239 // given one; return true if all items were traversed or false if the
240 // traversal was aborted because OnVisit returned false
241 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
243 // override this function to do whatever is needed for each item, return
244 // false to stop traversing
245 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
248 const wxTreeCtrl
*GetTree() const { return m_tree
; }
251 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
253 const wxTreeCtrl
*m_tree
;
255 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
258 // internal class for getting the selected items
259 class TraverseSelections
: public wxTreeTraversal
262 TraverseSelections(const wxTreeCtrl
*tree
,
263 wxArrayTreeItemIds
& selections
)
264 : wxTreeTraversal(tree
), m_selections(selections
)
266 m_selections
.Empty();
268 if (tree
->GetCount() > 0)
269 DoTraverse(tree
->GetRootItem());
272 virtual bool OnVisit(const wxTreeItemId
& item
)
274 const wxTreeCtrl
* const tree
= GetTree();
276 // can't visit a virtual node.
277 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
282 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
284 m_selections
.Add(item
);
290 size_t GetCount() const { return m_selections
.GetCount(); }
293 wxArrayTreeItemIds
& m_selections
;
295 DECLARE_NO_COPY_CLASS(TraverseSelections
)
298 // internal class for counting tree items
299 class TraverseCounter
: public wxTreeTraversal
302 TraverseCounter(const wxTreeCtrl
*tree
,
303 const wxTreeItemId
& root
,
305 : wxTreeTraversal(tree
)
309 DoTraverse(root
, recursively
);
312 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
319 size_t GetCount() const { return m_count
; }
324 DECLARE_NO_COPY_CLASS(TraverseCounter
)
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 #if wxUSE_EXTENDED_RTTI
332 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
334 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
335 // new style border flags, we put them first to
336 // use them for streaming out
337 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
338 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
339 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
340 wxFLAGS_MEMBER(wxBORDER_RAISED
)
341 wxFLAGS_MEMBER(wxBORDER_STATIC
)
342 wxFLAGS_MEMBER(wxBORDER_NONE
)
344 // old style border flags
345 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
346 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
347 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
348 wxFLAGS_MEMBER(wxRAISED_BORDER
)
349 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
350 wxFLAGS_MEMBER(wxBORDER
)
352 // standard window styles
353 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
354 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
355 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
356 wxFLAGS_MEMBER(wxWANTS_CHARS
)
357 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
358 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
359 wxFLAGS_MEMBER(wxVSCROLL
)
360 wxFLAGS_MEMBER(wxHSCROLL
)
362 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
363 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
364 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
365 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
366 wxFLAGS_MEMBER(wxTR_NO_LINES
)
367 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
368 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
369 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
370 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
371 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
372 wxFLAGS_MEMBER(wxTR_SINGLE
)
373 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
374 wxFLAGS_MEMBER(wxTR_EXTENDED
)
375 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
377 wxEND_FLAGS( wxTreeCtrlStyle
)
379 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
381 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
382 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
383 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
384 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
385 wxEND_PROPERTIES_TABLE()
387 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
388 wxEND_HANDLERS_TABLE()
390 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
392 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 // indices in gs_expandEvents table below
414 // handy table for sending events - it has to be initialized during run-time
415 // now so can't be const any more
416 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
419 but logically it's a const table with the following entries:
422 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
423 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
427 // ============================================================================
429 // ============================================================================
431 // ----------------------------------------------------------------------------
433 // ----------------------------------------------------------------------------
435 bool wxTreeTraversal
::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
437 if ( !OnVisit(root
) )
440 return Traverse(root
, recursively
);
443 bool wxTreeTraversal
::Traverse(const wxTreeItemId
& root
, bool recursively
)
445 wxTreeItemIdValue cookie
;
446 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
447 while ( child
.IsOk() )
449 // depth first traversal
450 if ( recursively
&& !Traverse(child
, true) )
453 if ( !OnVisit(child
) )
456 child
= m_tree
->GetNextChild(root
, cookie
);
462 // ----------------------------------------------------------------------------
463 // construction and destruction
464 // ----------------------------------------------------------------------------
466 void wxTreeCtrl
::Init()
469 m_hasAnyAttr
= false;
471 m_pVirtualRoot
= NULL
;
473 // initialize the global array of events now as it can't be done statically
474 // with the wxEVT_XXX values being allocated during run-time only
475 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
476 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
477 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
478 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
481 bool wxTreeCtrl
::Create(wxWindow
*parent
,
486 const wxValidator
& validator
,
487 const wxString
& name
)
491 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
492 style
|= wxBORDER_SUNKEN
;
494 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
498 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
499 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
501 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
502 wstyle
|= TVS_HASLINES
;
503 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
504 wstyle
|= TVS_HASBUTTONS
;
506 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
507 wstyle
|= TVS_EDITLABELS
;
509 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
510 wstyle
|= TVS_LINESATROOT
;
512 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
514 if ( wxApp
::GetComCtl32Version() >= 471 )
515 wstyle
|= TVS_FULLROWSELECT
;
518 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
519 // Need so that TVN_GETINFOTIP messages will be sent
520 wstyle
|= TVS_INFOTIP
;
523 // Create the tree control.
524 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
527 #if wxUSE_COMCTL32_SAFELY
528 wxWindow
::SetBackgroundColour(wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOW
));
529 wxWindow
::SetForegroundColour(wxWindow
::GetParent()->GetForegroundColour());
531 SetBackgroundColour(wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOW
));
532 SetForegroundColour(wxWindow
::GetParent()->GetForegroundColour());
534 // This works around a bug in the Windows tree control whereby for some versions
535 // of comctrl32, setting any colour actually draws the background in black.
536 // This will initialise the background to the system colour.
537 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
538 // Assume the user has an updated comctl32.dll.
539 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
540 wxWindow
::SetBackgroundColour(wxSystemSettings
::GetColour(wxSYS_COLOUR_WINDOW
));
541 SetForegroundColour(wxWindow
::GetParent()->GetForegroundColour());
545 // VZ: this is some experimental code which may be used to get the
546 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
547 // AFAIK, the standard DLL does about the same thing anyhow.
549 if ( m_windowStyle
& wxTR_MULTIPLE
)
553 // create the DC compatible with the current screen
554 HDC hdcMem
= CreateCompatibleDC(NULL
);
556 // create a mono bitmap of the standard size
557 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
558 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
559 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
560 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
561 1, // # of color planes
562 1, // # bits needed for one pixel
563 0); // array containing colour data
564 SelectObject(hdcMem
, hbmpCheck
);
566 // then draw a check mark into it
567 RECT rect
= { 0, 0, x
, y
};
568 if ( !::DrawFrameControl(hdcMem
, &rect
,
570 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
572 wxLogLastError(wxT("DrawFrameControl(check)"));
575 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
576 imagelistCheckboxes
.Add(bmp
);
578 if ( !::DrawFrameControl(hdcMem
, &rect
,
582 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
585 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
586 imagelistCheckboxes
.Add(bmp
);
592 SetStateImageList(&imagelistCheckboxes
);
596 wxSetCCUnicodeFormat(GetHwnd());
601 wxTreeCtrl
::~wxTreeCtrl()
603 // delete any attributes
606 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
608 // prevent TVN_DELETEITEM handler from deleting the attributes again!
609 m_hasAnyAttr
= false;
614 // delete user data to prevent memory leaks
615 // also deletes hidden root node storage.
619 // ----------------------------------------------------------------------------
621 // ----------------------------------------------------------------------------
623 /* static */ wxVisualAttributes
624 wxTreeCtrl
::GetClassDefaultAttributes(wxWindowVariant variant
)
626 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
628 // common controls have their own default font
629 attrs
.font
= wxGetCCDefaultFont();
635 // simple wrappers which add error checking in debug mode
637 bool wxTreeCtrl
::DoGetItem(wxTreeViewItem
*tvItem
) const
639 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
640 _T("can't retrieve virtual root item") );
642 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
644 wxLogLastError(wxT("TreeView_GetItem"));
652 void wxTreeCtrl
::DoSetItem(wxTreeViewItem
*tvItem
)
654 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
656 wxLogLastError(wxT("TreeView_SetItem"));
660 unsigned int wxTreeCtrl
::GetCount() const
662 return (unsigned int)TreeView_GetCount(GetHwnd());
665 unsigned int wxTreeCtrl
::GetIndent() const
667 return TreeView_GetIndent(GetHwnd());
670 void wxTreeCtrl
::SetIndent(unsigned int indent
)
672 TreeView_SetIndent(GetHwnd(), indent
);
675 void wxTreeCtrl
::SetAnyImageList(wxImageList
*imageList
, int which
)
678 (void) TreeView_SetImageList(GetHwnd(),
679 imageList ? imageList
->GetHIMAGELIST() : 0,
683 void wxTreeCtrl
::SetImageList(wxImageList
*imageList
)
685 if (m_ownsImageListNormal
)
686 delete m_imageListNormal
;
688 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
689 m_ownsImageListNormal
= false;
692 void wxTreeCtrl
::SetStateImageList(wxImageList
*imageList
)
694 if (m_ownsImageListState
) delete m_imageListState
;
695 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
696 m_ownsImageListState
= false;
699 size_t wxTreeCtrl
::GetChildrenCount(const wxTreeItemId
& item
,
700 bool recursively
) const
702 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
704 TraverseCounter
counter(this, item
, recursively
);
705 return counter
.GetCount() - 1;
708 // ----------------------------------------------------------------------------
710 // ----------------------------------------------------------------------------
712 bool wxTreeCtrl
::SetBackgroundColour(const wxColour
&colour
)
714 #if !wxUSE_COMCTL32_SAFELY
715 if ( !wxWindowBase
::SetBackgroundColour(colour
) )
718 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
724 bool wxTreeCtrl
::SetForegroundColour(const wxColour
&colour
)
726 #if !wxUSE_COMCTL32_SAFELY
727 if ( !wxWindowBase
::SetForegroundColour(colour
) )
730 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
736 // ----------------------------------------------------------------------------
738 // ----------------------------------------------------------------------------
740 bool wxTreeCtrl
::IsHiddenRoot(const wxTreeItemId
& item
) const
742 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
745 wxString wxTreeCtrl
::GetItemText(const wxTreeItemId
& item
) const
747 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
749 wxChar buf
[512]; // the size is arbitrary...
751 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
752 tvItem
.pszText
= buf
;
753 tvItem
.cchTextMax
= WXSIZEOF(buf
);
754 if ( !DoGetItem(&tvItem
) )
756 // don't return some garbage which was on stack, but an empty string
760 return wxString(buf
);
763 void wxTreeCtrl
::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
765 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
767 if ( IS_VIRTUAL_ROOT(item
) )
770 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
771 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
774 // when setting the text of the item being edited, the text control should
775 // be updated to reflect the new text as well, otherwise calling
776 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
778 // don't use GetEditControl() here because m_textCtrl is not set yet
779 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
782 if ( item
== m_idEdited
)
784 ::SetWindowText(hwndEdit
, text
);
789 int wxTreeCtrl
::GetItemImage(const wxTreeItemId
& item
,
790 wxTreeItemIcon which
) const
792 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
794 if ( IsHiddenRoot(item
) )
796 // no images for hidden root item
800 wxTreeItemParam
*param
= GetItemParam(item
);
802 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
805 void wxTreeCtrl
::SetItemImage(const wxTreeItemId
& item
, int image
,
806 wxTreeItemIcon which
)
808 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
809 wxCHECK_RET( which
>= 0 &&
810 which
< wxTreeItemIcon_Max
,
811 wxT("invalid image index"));
814 if ( IsHiddenRoot(item
) )
816 // no images for hidden root item
820 wxTreeItemParam
*data
= GetItemParam(item
);
824 data
->SetImage(image
, which
);
829 wxTreeItemParam
*wxTreeCtrl
::GetItemParam(const wxTreeItemId
& item
) const
831 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
833 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
835 // hidden root may still have data.
836 if ( IS_VIRTUAL_ROOT(item
) )
838 return GET_VIRTUAL_ROOT()->GetParam();
842 if ( !DoGetItem(&tvItem
) )
847 return (wxTreeItemParam
*)tvItem
.lParam
;
850 wxTreeItemData
*wxTreeCtrl
::GetItemData(const wxTreeItemId
& item
) const
852 wxTreeItemParam
*data
= GetItemParam(item
);
854 return data ? data
->GetData() : NULL
;
857 void wxTreeCtrl
::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
859 // first, associate this piece of data with this item
865 wxTreeItemParam
*param
= GetItemParam(item
);
867 wxCHECK_RET( param
, wxT("failed to change tree items data") );
869 param
->SetData(data
);
872 void wxTreeCtrl
::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
874 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
876 if ( IS_VIRTUAL_ROOT(item
) )
879 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
880 tvItem
.cChildren
= (int)has
;
884 void wxTreeCtrl
::SetItemBold(const wxTreeItemId
& item
, bool bold
)
886 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
888 if ( IS_VIRTUAL_ROOT(item
) )
891 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
892 tvItem
.state
= bold ? TVIS_BOLD
: 0;
896 void wxTreeCtrl
::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
898 if ( IS_VIRTUAL_ROOT(item
) )
901 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
902 tvItem
.state
= highlight ? TVIS_DROPHILITED
: 0;
906 void wxTreeCtrl
::RefreshItem(const wxTreeItemId
& item
)
908 if ( IS_VIRTUAL_ROOT(item
) )
912 if ( GetBoundingRect(item
, rect
) )
918 wxColour wxTreeCtrl
::GetItemTextColour(const wxTreeItemId
& item
) const
920 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
922 wxMapTreeAttr
::const_iterator it
= m_attrs
.find(item
.m_pItem
);
923 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
926 wxColour wxTreeCtrl
::GetItemBackgroundColour(const wxTreeItemId
& item
) const
928 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
930 wxMapTreeAttr
::const_iterator it
= m_attrs
.find(item
.m_pItem
);
931 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
934 wxFont wxTreeCtrl
::GetItemFont(const wxTreeItemId
& item
) const
936 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
938 wxMapTreeAttr
::const_iterator it
= m_attrs
.find(item
.m_pItem
);
939 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
942 void wxTreeCtrl
::SetItemTextColour(const wxTreeItemId
& item
,
945 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
947 wxTreeItemAttr
*attr
;
948 wxMapTreeAttr
::iterator it
= m_attrs
.find(item
.m_pItem
);
949 if ( it
== m_attrs
.end() )
953 m_attrs
[item
.m_pItem
] =
954 attr
= new wxTreeItemAttr
;
961 attr
->SetTextColour(col
);
966 void wxTreeCtrl
::SetItemBackgroundColour(const wxTreeItemId
& item
,
969 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
971 wxTreeItemAttr
*attr
;
972 wxMapTreeAttr
::iterator it
= m_attrs
.find(item
.m_pItem
);
973 if ( it
== m_attrs
.end() )
977 m_attrs
[item
.m_pItem
] =
978 attr
= new wxTreeItemAttr
;
980 else // already in the hash
985 attr
->SetBackgroundColour(col
);
990 void wxTreeCtrl
::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
992 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
994 wxTreeItemAttr
*attr
;
995 wxMapTreeAttr
::iterator it
= m_attrs
.find(item
.m_pItem
);
996 if ( it
== m_attrs
.end() )
1000 m_attrs
[item
.m_pItem
] =
1001 attr
= new wxTreeItemAttr
;
1003 else // already in the hash
1008 attr
->SetFont(font
);
1013 // ----------------------------------------------------------------------------
1015 // ----------------------------------------------------------------------------
1017 bool wxTreeCtrl
::IsVisible(const wxTreeItemId
& item
) const
1019 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1021 if ( item
== wxTreeItemId(TVI_ROOT
) )
1023 // virtual (hidden) root is never visible
1027 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1030 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1031 // the HTREEITEM with TVM_GETITEMRECT
1032 *(HTREEITEM
*)&rect
= HITEM(item
);
1034 // true means to get rect for just the text, not the whole line
1035 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1037 // if TVM_GETITEMRECT returned false, then the item is definitely not
1038 // visible (because its parent is not expanded)
1042 // however if it returned true, the item might still be outside the
1043 // currently visible part of the tree, test for it (notice that partly
1044 // visible means visible here)
1045 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1048 bool wxTreeCtrl
::ItemHasChildren(const wxTreeItemId
& item
) const
1050 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1052 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1055 return tvItem
.cChildren
!= 0;
1058 bool wxTreeCtrl
::IsExpanded(const wxTreeItemId
& item
) const
1060 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1062 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1065 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1068 bool wxTreeCtrl
::IsSelected(const wxTreeItemId
& item
) const
1070 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1072 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1075 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1078 bool wxTreeCtrl
::IsBold(const wxTreeItemId
& item
) const
1080 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1082 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1085 return (tvItem
.state
& TVIS_BOLD
) != 0;
1088 // ----------------------------------------------------------------------------
1090 // ----------------------------------------------------------------------------
1092 wxTreeItemId wxTreeCtrl
::GetRootItem() const
1094 // Root may be real (visible) or virtual (hidden).
1095 if ( GET_VIRTUAL_ROOT() )
1098 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1101 wxTreeItemId wxTreeCtrl
::GetSelection() const
1103 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1104 wxT("this only works with single selection controls") );
1106 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1109 wxTreeItemId wxTreeCtrl
::GetItemParent(const wxTreeItemId
& item
) const
1111 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1115 if ( IS_VIRTUAL_ROOT(item
) )
1117 // no parent for the virtual root
1122 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1123 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1125 // the top level items should have the virtual root as their parent
1130 return wxTreeItemId(hItem
);
1133 wxTreeItemId wxTreeCtrl
::GetFirstChild(const wxTreeItemId
& item
,
1134 wxTreeItemIdValue
& cookie
) const
1136 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1138 // remember the last child returned in 'cookie'
1139 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1141 return wxTreeItemId(cookie
);
1144 wxTreeItemId wxTreeCtrl
::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1145 wxTreeItemIdValue
& cookie
) const
1147 wxTreeItemId
fromCookie(cookie
);
1149 HTREEITEM hitem
= HITEM(fromCookie
);
1151 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1153 wxTreeItemId
item(hitem
);
1155 cookie
= item
.m_pItem
;
1160 #if WXWIN_COMPATIBILITY_2_4
1162 wxTreeItemId wxTreeCtrl
::GetFirstChild(const wxTreeItemId
& item
,
1165 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1167 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1169 return wxTreeItemId((void *)cookie
);
1172 wxTreeItemId wxTreeCtrl
::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1175 wxTreeItemId
fromCookie((void *)cookie
);
1177 HTREEITEM hitem
= HITEM(fromCookie
);
1179 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1181 wxTreeItemId
item(hitem
);
1183 cookie
= (long)item
.m_pItem
;
1188 #endif // WXWIN_COMPATIBILITY_2_4
1190 wxTreeItemId wxTreeCtrl
::GetLastChild(const wxTreeItemId
& item
) const
1192 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1194 // can this be done more efficiently?
1195 wxTreeItemIdValue cookie
;
1197 wxTreeItemId childLast
,
1198 child
= GetFirstChild(item
, cookie
);
1199 while ( child
.IsOk() )
1202 child
= GetNextChild(item
, cookie
);
1208 wxTreeItemId wxTreeCtrl
::GetNextSibling(const wxTreeItemId
& item
) const
1210 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1211 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1214 wxTreeItemId wxTreeCtrl
::GetPrevSibling(const wxTreeItemId
& item
) const
1216 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1217 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1220 wxTreeItemId wxTreeCtrl
::GetFirstVisibleItem() const
1222 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1225 wxTreeItemId wxTreeCtrl
::GetNextVisible(const wxTreeItemId
& item
) const
1227 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1228 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1230 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1233 wxTreeItemId wxTreeCtrl
::GetPrevVisible(const wxTreeItemId
& item
) const
1235 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1236 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1238 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1241 // ----------------------------------------------------------------------------
1242 // multiple selections emulation
1243 // ----------------------------------------------------------------------------
1245 bool wxTreeCtrl
::IsItemChecked(const wxTreeItemId
& item
) const
1247 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1249 // receive the desired information.
1250 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1253 // state image indices are 1 based
1254 return ((tvItem
.state
>> 12) - 1) == 1;
1257 void wxTreeCtrl
::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1259 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1261 // receive the desired information.
1262 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1266 // state images are one-based
1267 tvItem
.state
= (check ?
2 : 1) << 12;
1272 size_t wxTreeCtrl
::GetSelections(wxArrayTreeItemIds
& selections
) const
1274 TraverseSelections
selector(this, selections
);
1276 return selector
.GetCount();
1279 // ----------------------------------------------------------------------------
1281 // ----------------------------------------------------------------------------
1283 wxTreeItemId wxTreeCtrl
::DoInsertAfter(const wxTreeItemId
& parent
,
1284 const wxTreeItemId
& hInsertAfter
,
1285 const wxString
& text
,
1286 int image
, int selectedImage
,
1287 wxTreeItemData
*data
)
1289 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1291 _T("can't have more than one root in the tree") );
1293 TV_INSERTSTRUCT tvIns
;
1294 tvIns
.hParent
= HITEM(parent
);
1295 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1297 // this is how we insert the item as the first child: supply a NULL
1299 if ( !tvIns
.hInsertAfter
)
1301 tvIns
.hInsertAfter
= TVI_FIRST
;
1305 if ( !text
.empty() )
1308 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1312 tvIns
.item
.pszText
= NULL
;
1313 tvIns
.item
.cchTextMax
= 0;
1316 // create the param which will store the other item parameters
1317 wxTreeItemParam
*param
= new wxTreeItemParam
;
1319 // we return the images on demand as they depend on whether the item is
1320 // expanded or collapsed too in our case
1321 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1322 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1323 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1325 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1326 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1329 tvIns
.item
.lParam
= (LPARAM
)param
;
1330 tvIns
.item
.mask
= mask
;
1332 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1335 wxLogLastError(wxT("TreeView_InsertItem"));
1338 // associate the application tree item with Win32 tree item handle
1341 // setup wxTreeItemData
1344 param
->SetData(data
);
1348 return wxTreeItemId(id
);
1351 // for compatibility only
1352 #if WXWIN_COMPATIBILITY_2_4
1354 void wxTreeCtrl
::SetImageList(wxImageList
*imageList
, int)
1356 SetImageList(imageList
);
1359 int wxTreeCtrl
::GetItemSelectedImage(const wxTreeItemId
& item
) const
1361 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1364 void wxTreeCtrl
::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1366 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1369 #endif // WXWIN_COMPATIBILITY_2_4
1371 wxTreeItemId wxTreeCtrl
::AddRoot(const wxString
& text
,
1372 int image
, int selectedImage
,
1373 wxTreeItemData
*data
)
1376 if ( HasFlag(wxTR_HIDE_ROOT
) )
1378 // create a virtual root item, the parent for all the others
1379 wxTreeItemParam
*param
= new wxTreeItemParam
;
1380 param
->SetData(data
);
1382 m_pVirtualRoot
= new wxVirtualNode(param
);
1387 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1388 text
, image
, selectedImage
, data
);
1391 wxTreeItemId wxTreeCtrl
::DoInsertItem(const wxTreeItemId
& parent
,
1393 const wxString
& text
,
1394 int image
, int selectedImage
,
1395 wxTreeItemData
*data
)
1397 wxTreeItemId idPrev
;
1398 if ( index
== (size_t)-1 )
1400 // special value: append to the end
1403 else // find the item from index
1405 wxTreeItemIdValue cookie
;
1406 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1407 while ( index
!= 0 && idCur
.IsOk() )
1412 idCur
= GetNextChild(parent
, cookie
);
1415 // assert, not check: if the index is invalid, we will append the item
1417 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1420 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1423 void wxTreeCtrl
::Delete(const wxTreeItemId
& item
)
1425 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1427 wxLogLastError(wxT("TreeView_DeleteItem"));
1431 // delete all children (but don't delete the item itself)
1432 void wxTreeCtrl
::DeleteChildren(const wxTreeItemId
& item
)
1434 wxTreeItemIdValue cookie
;
1436 wxArrayTreeItemIds children
;
1437 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1438 while ( child
.IsOk() )
1440 children
.Add(child
);
1442 child
= GetNextChild(item
, cookie
);
1445 size_t nCount
= children
.Count();
1446 for ( size_t n
= 0; n
< nCount
; n
++ )
1448 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1450 wxLogLastError(wxT("TreeView_DeleteItem"));
1455 void wxTreeCtrl
::DeleteAllItems()
1457 // delete the "virtual" root item.
1458 if ( GET_VIRTUAL_ROOT() )
1460 delete GET_VIRTUAL_ROOT();
1461 m_pVirtualRoot
= NULL
;
1464 // and all the real items
1466 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1468 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1472 void wxTreeCtrl
::DoExpand(const wxTreeItemId
& item
, int flag
)
1474 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1475 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1476 flag
== TVE_EXPAND
||
1478 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1480 // A hidden root can be neither expanded nor collapsed.
1481 wxCHECK_RET( !IsHiddenRoot(item
),
1482 wxT("Can't expand/collapse hidden root node!") );
1484 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1485 // emulate them. This behaviour has changed slightly with comctl32.dll
1486 // v 4.70 - now it does send them but only the first time. To maintain
1487 // compatible behaviour and also in order to not have surprises with the
1488 // future versions, don't rely on this and still do everything ourselves.
1489 // To avoid that the messages be sent twice when the item is expanded for
1490 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1492 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1496 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1498 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1500 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1504 (void)GetEventHandler()->ProcessEvent(event
);
1506 //else: change didn't took place, so do nothing at all
1509 void wxTreeCtrl
::Expand(const wxTreeItemId
& item
)
1511 DoExpand(item
, TVE_EXPAND
);
1514 void wxTreeCtrl
::Collapse(const wxTreeItemId
& item
)
1516 DoExpand(item
, TVE_COLLAPSE
);
1519 void wxTreeCtrl
::CollapseAndReset(const wxTreeItemId
& item
)
1521 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1524 void wxTreeCtrl
::Toggle(const wxTreeItemId
& item
)
1526 DoExpand(item
, TVE_TOGGLE
);
1529 #if WXWIN_COMPATIBILITY_2_4
1531 void wxTreeCtrl
::ExpandItem(const wxTreeItemId
& item
, int action
)
1533 DoExpand(item
, action
);
1538 void wxTreeCtrl
::Unselect()
1540 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1541 wxT("doesn't make sense, may be you want UnselectAll()?") );
1543 // just remove the selection
1544 SelectItem(wxTreeItemId());
1547 void wxTreeCtrl
::UnselectAll()
1549 if ( m_windowStyle
& wxTR_MULTIPLE
)
1551 wxArrayTreeItemIds selections
;
1552 size_t count
= GetSelections(selections
);
1553 for ( size_t n
= 0; n
< count
; n
++ )
1555 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1558 m_htSelStart
.Unset();
1560 else // single selection
1562 // just remove the selection
1567 void wxTreeCtrl
::SelectItem(const wxTreeItemId
& item
, bool select
)
1569 if ( m_windowStyle
& wxTR_MULTIPLE
)
1571 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1573 else // single selection
1575 wxASSERT_MSG( select
,
1576 _T("SelectItem(false) works only for multiselect") );
1578 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1579 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1580 // send them ourselves
1582 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1583 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1585 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1587 wxLogLastError(wxT("TreeView_SelectItem"));
1591 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1592 (void)GetEventHandler()->ProcessEvent(event
);
1595 //else: program vetoed the change
1599 void wxTreeCtrl
::EnsureVisible(const wxTreeItemId
& item
)
1601 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1604 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1607 void wxTreeCtrl
::ScrollTo(const wxTreeItemId
& item
)
1609 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1611 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1615 wxTextCtrl
*wxTreeCtrl
::GetEditControl() const
1620 void wxTreeCtrl
::DeleteTextCtrl()
1624 // the HWND corresponding to this control is deleted by the tree
1625 // control itself and we don't know when exactly this happens, so check
1626 // if the window still exists before calling UnsubclassWin()
1627 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1629 m_textCtrl
->SetHWND(0);
1632 m_textCtrl
->UnsubclassWin();
1633 m_textCtrl
->SetHWND(0);
1641 wxTextCtrl
*wxTreeCtrl
::EditLabel(const wxTreeItemId
& item
,
1642 wxClassInfo
*textControlClass
)
1644 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1649 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1650 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1652 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1661 // textctrl is subclassed in MSWOnNotify
1665 // End label editing, optionally cancelling the edit
1666 void wxTreeCtrl
::DoEndEditLabel(bool discardChanges
)
1668 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1673 wxTreeItemId wxTreeCtrl
::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1675 TV_HITTESTINFO hitTestInfo
;
1676 hitTestInfo
.pt
.x
= (int)point
.x
;
1677 hitTestInfo
.pt
.y
= (int)point
.y
;
1679 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1684 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1685 flags |= wxTREE_HITTEST_##flag
1687 TRANSLATE_FLAG(ABOVE
);
1688 TRANSLATE_FLAG(BELOW
);
1689 TRANSLATE_FLAG(NOWHERE
);
1690 TRANSLATE_FLAG(ONITEMBUTTON
);
1691 TRANSLATE_FLAG(ONITEMICON
);
1692 TRANSLATE_FLAG(ONITEMINDENT
);
1693 TRANSLATE_FLAG(ONITEMLABEL
);
1694 TRANSLATE_FLAG(ONITEMRIGHT
);
1695 TRANSLATE_FLAG(ONITEMSTATEICON
);
1696 TRANSLATE_FLAG(TOLEFT
);
1697 TRANSLATE_FLAG(TORIGHT
);
1699 #undef TRANSLATE_FLAG
1701 return wxTreeItemId(hitTestInfo
.hItem
);
1704 bool wxTreeCtrl
::GetBoundingRect(const wxTreeItemId
& item
,
1706 bool textOnly
) const
1710 // Virtual root items have no bounding rectangle
1711 if ( IS_VIRTUAL_ROOT(item
) )
1716 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1719 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1725 // couldn't retrieve rect: for example, item isn't visible
1730 // ----------------------------------------------------------------------------
1732 // ----------------------------------------------------------------------------
1734 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1735 // functions such as IsDataIndirect()
1736 class wxTreeSortHelper
1739 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1742 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1744 return ((wxTreeItemParam
*)lParam
)->GetItem();
1748 int CALLBACK wxTreeSortHelper
::Compare(LPARAM pItem1
,
1752 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1753 wxT("sorting tree without data doesn't make sense") );
1755 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1757 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1758 GetIdFromData(pItem2
));
1761 void wxTreeCtrl
::SortChildren(const wxTreeItemId
& item
)
1763 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1765 // rely on the fact that TreeView_SortChildren does the same thing as our
1766 // default behaviour, i.e. sorts items alphabetically and so call it
1767 // directly if we're not in derived class (much more efficient!)
1768 // RN: Note that if you find you're code doesn't sort as expected this
1769 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1770 // combo for your derived wxTreeCtrl if will sort without
1772 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1774 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1779 tvSort
.hParent
= HITEM(item
);
1780 tvSort
.lpfnCompare
= wxTreeSortHelper
::Compare
;
1781 tvSort
.lParam
= (LPARAM
)this;
1782 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1786 // ----------------------------------------------------------------------------
1788 // ----------------------------------------------------------------------------
1790 bool wxTreeCtrl
::MSWShouldPreProcessMessage(WXMSG
* msg
)
1792 if ( msg
->message
== WM_KEYDOWN
)
1794 if ( msg
->wParam
== VK_RETURN
)
1796 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1801 return wxTreeCtrlBase
::MSWShouldPreProcessMessage(msg
);
1804 bool wxTreeCtrl
::MSWCommand(WXUINT cmd
, WXWORD id
)
1806 if ( cmd
== EN_UPDATE
)
1808 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1809 event
.SetEventObject( this );
1810 ProcessCommand(event
);
1812 else if ( cmd
== EN_KILLFOCUS
)
1814 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1815 event
.SetEventObject( this );
1816 ProcessCommand(event
);
1824 // command processed
1828 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1829 // only do it during dragging, minimize wxWin overhead (this is important for
1830 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1831 // instead of passing by wxWin events
1832 WXLRESULT wxTreeCtrl
::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1834 bool processed
= false;
1836 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
1838 // This message is sent after a right-click, or when the "menu" key is pressed
1839 if ( nMsg
== WM_CONTEXTMENU
)
1841 int x
= GET_X_LPARAM(lParam
),
1842 y
= GET_Y_LPARAM(lParam
);
1843 // Convert the screen point to a client point
1844 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
1846 // can't use GetSelection() here as it would assert in multiselect mode
1847 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this,
1848 wxTreeItemId(TreeView_GetSelection(GetHwnd())));
1850 // Get the bounding rectangle for the item, including the non-text areas
1852 GetBoundingRect(event
.m_item
, ItemRect
, false);
1853 // If the point is inside the bounding rectangle, use it as the click position.
1854 // This should be the case for WM_CONTEXTMENU as the result of a right-click
1855 if (ItemRect
.Contains(MenuPoint
))
1857 event
.m_pointDrag
= MenuPoint
;
1859 // Use the Explorer standard of putting the menu at the left edge of the text,
1860 // in the vertical middle of the text. Should be the case for the "menu" key
1863 // Use the bounding rectangle of only the text part
1864 GetBoundingRect(event
.m_item
, ItemRect
, true);
1865 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
1868 if ( GetEventHandler()->ProcessEvent(event
) )
1870 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
1872 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1874 // we only process mouse messages here and these parameters have the
1875 // same meaning for all of them
1876 int x
= GET_X_LPARAM(lParam
),
1877 y
= GET_Y_LPARAM(lParam
);
1878 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1880 TV_HITTESTINFO tvht
;
1884 (void) TreeView_HitTest(GetHwnd(), &tvht
);
1888 case WM_RBUTTONDOWN
:
1889 // if the item we are about to right click on is not already
1890 // selected or if we click outside of any item, remove the
1891 // entire previous selection
1892 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
1897 // select item and set the focus to the
1898 // newly selected item
1899 ::SelectItem(GetHwnd(), htItem
);
1900 ::SetFocus(GetHwnd(), htItem
);
1903 case WM_LBUTTONDOWN
:
1904 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
1906 m_htClickedItem
= (WXHTREEITEM
) htItem
;
1907 m_ptClick
= wxPoint(x
, y
);
1909 if ( wParam
& MK_CONTROL
)
1913 // toggle selected state
1914 ::ToggleItemSelection(GetHwnd(), htItem
);
1916 ::SetFocus(GetHwnd(), htItem
);
1918 // reset on any click without Shift
1919 m_htSelStart
.Unset();
1923 else if ( wParam
& MK_SHIFT
)
1925 // this selects all items between the starting one and
1928 if ( !m_htSelStart
)
1930 // take the focused item
1931 m_htSelStart
= TreeView_GetSelection(GetHwnd());
1935 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1936 !(wParam
& MK_CONTROL
));
1938 ::SelectItem(GetHwnd(), htItem
);
1940 ::SetFocus(GetHwnd(), htItem
);
1944 else // normal click
1946 // avoid doing anything if we click on the only
1947 // currently selected item
1951 wxArrayTreeItemIds selections
;
1952 size_t count
= GetSelections(selections
);
1955 HITEM(selections
[0]) != htItem
)
1957 // clear the previously selected items, if the
1958 // user clicked outside of the present selection.
1959 // otherwise, perform the deselection on mouse-up.
1960 // this allows multiple drag and drop to work.
1962 if (!IsItemSelected(GetHwnd(), htItem
))
1966 // prevent the click from starting in-place editing
1967 // which should only happen if we click on the
1968 // already selected item (and nothing else is
1971 TreeView_SelectItem(GetHwnd(), 0);
1972 ::SelectItem(GetHwnd(), htItem
);
1974 ::SetFocus(GetHwnd(), htItem
);
1978 // reset on any click without Shift
1979 m_htSelStart
.Unset();
1986 if ( m_htClickedItem
)
1988 int cx
= abs(m_ptClick
.x
- x
);
1989 int cy
= abs(m_ptClick
.y
- y
);
1991 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
1993 HWND pWnd
= ::GetParent( GetHwnd() );
1998 tv
.hdr
.hwndFrom
= GetHwnd();
1999 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2000 tv
.hdr
.code
= TVN_BEGINDRAG
;
2002 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2005 ZeroMemory(&tviAux
, sizeof(tviAux
));
2006 tviAux
.hItem
= HITEM(m_htClickedItem
);
2007 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2008 tviAux
.stateMask
= 0xffffffff;
2009 TreeView_GetItem( GetHwnd(), &tviAux
);
2011 tv
.itemNew
.state
= tviAux
.state
;
2012 tv
.itemNew
.lParam
= tviAux
.lParam
;
2017 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2019 m_htClickedItem
.Unset();
2022 #endif // __WXWINCE__
2026 m_dragImage
->Move(wxPoint(x
, y
));
2029 // highlight the item as target (hiding drag image is
2030 // necessary - otherwise the display will be corrupted)
2031 m_dragImage
->Hide();
2032 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2033 m_dragImage
->Show();
2040 // facilitates multiple drag-and-drop
2041 if (htItem
&& isMultiple
)
2043 wxArrayTreeItemIds selections
;
2044 size_t count
= GetSelections(selections
);
2047 !(wParam
& MK_CONTROL
) &&
2048 !(wParam
& MK_SHIFT
))
2051 TreeView_SelectItem(GetHwnd(), htItem
);
2052 ::SelectItem(GetHwnd(), htItem
);
2053 ::SetFocus(GetHwnd(), htItem
);
2055 m_htClickedItem
.Unset();
2063 m_dragImage
->EndDrag();
2067 // generate the drag end event
2068 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2069 (void)GetEventHandler()->ProcessEvent(event
);
2071 // if we don't do it, the tree seems to think that 2 items
2072 // are selected simultaneously which is quite weird
2073 TreeView_SelectDropTarget(GetHwnd(), 0);
2078 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2080 // the tree control greys out the selected item when it loses focus and
2081 // paints it as selected again when it regains it, but it won't do it
2082 // for the other items itself - help it
2083 wxArrayTreeItemIds selections
;
2084 size_t count
= GetSelections(selections
);
2086 for ( size_t n
= 0; n
< count
; n
++ )
2088 // TreeView_GetItemRect() will return false if item is not visible,
2089 // which may happen perfectly well
2090 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2093 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2097 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2099 bool bCtrl
= wxIsCtrlDown(),
2100 bShift
= wxIsShiftDown();
2102 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2108 ::ToggleItemSelection(GetHwnd(), htSel
);
2114 ::SelectItem(GetHwnd(), htSel
);
2122 if ( !bCtrl
&& !bShift
)
2124 // no modifiers, just clear selection and then let the default
2125 // processing to take place
2130 (void)wxControl
::MSWWindowProc(nMsg
, wParam
, lParam
);
2132 HTREEITEM htNext
= (HTREEITEM
)
2133 TreeView_GetNextItem
2137 wParam
== VK_UP ? TVGN_PREVIOUSVISIBLE
2143 // at the top/bottom
2149 if ( !m_htSelStart
)
2150 m_htSelStart
= htSel
;
2152 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2156 // without changing selection
2157 ::SetFocus(GetHwnd(), htNext
);
2168 // TODO: handle Shift/Ctrl with these keys
2169 if ( !bCtrl
&& !bShift
)
2173 m_htSelStart
.Unset();
2177 else if ( nMsg
== WM_COMMAND
)
2179 // if we receive a EN_KILLFOCUS command from the in-place edit control
2180 // used for label editing, make sure to end editing
2183 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2185 if ( cmd
== EN_KILLFOCUS
)
2187 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2197 rc
= wxControl
::MSWWindowProc(nMsg
, wParam
, lParam
);
2203 wxTreeCtrl
::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2205 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2206 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2207 // the parent window, not us, which completely breaks everything so simply
2208 // don't let it see this message at all
2209 if ( nMsg
== WM_RBUTTONDOWN
)
2212 // but because of the above we don't get NM_RCLICK which is normally
2213 // generated by tree window proc when the modal loop mentioned above ends
2214 // because the mouse is released -- synthesize it ourselves instead
2215 if ( nMsg
== WM_RBUTTONUP
)
2218 hdr
.hwndFrom
= GetHwnd();
2219 hdr
.idFrom
= GetId();
2220 hdr
.code
= NM_RCLICK
;
2223 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2225 // continue as usual
2228 if ( nMsg
== WM_CHAR
)
2230 // also don't let the control process Space and Return keys because it
2231 // doesn't do anything useful with them anyhow but always beeps
2232 // annoyingly when it receives them and there is no way to turn it off
2233 // simply if you just process TREEITEM_ACTIVATED event to which Space
2234 // and Enter presses are mapped in your code
2235 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2239 return wxControl
::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2242 // process WM_NOTIFY Windows message
2243 bool wxTreeCtrl
::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2245 wxTreeEvent
event(wxEVT_NULL
, this);
2246 wxEventType eventType
= wxEVT_NULL
;
2247 NMHDR
*hdr
= (NMHDR
*)lParam
;
2249 switch ( hdr
->code
)
2252 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2255 case TVN_BEGINRDRAG
:
2257 if ( eventType
== wxEVT_NULL
)
2258 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2259 //else: left drag, already set above
2261 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2263 event
.m_item
= tv
->itemNew
.hItem
;
2264 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2266 // don't allow dragging by default: the user code must
2267 // explicitly say that it wants to allow it to avoid breaking
2273 case TVN_BEGINLABELEDIT
:
2275 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2276 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2278 // although the user event handler may still veto it, it is
2279 // important to set it now so that calls to SetItemText() from
2280 // the event handler would change the text controls contents
2282 event
.m_item
= info
->item
.hItem
;
2283 event
.m_label
= info
->item
.pszText
;
2284 event
.m_editCancelled
= false;
2288 case TVN_DELETEITEM
:
2290 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2291 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2293 event
.m_item
= tv
->itemOld
.hItem
;
2297 wxMapTreeAttr
::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2298 if ( it
!= m_attrs
.end() )
2307 case TVN_ENDLABELEDIT
:
2309 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2310 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2312 event
.m_item
= info
->item
.hItem
;
2313 event
.m_label
= info
->item
.pszText
;
2314 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2319 // These *must* not be removed or TVN_GETINFOTIP will
2320 // not be processed each time the mouse is moved
2321 // and the tooltip will only ever update once.
2330 #ifdef TVN_GETINFOTIP
2331 case TVN_GETINFOTIP
:
2333 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2334 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2336 // Which item are we trying to get a tooltip for?
2337 event
.m_item
= info
->hItem
;
2344 case TVN_GETDISPINFO
:
2345 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2348 case TVN_SETDISPINFO
:
2350 if ( eventType
== wxEVT_NULL
)
2351 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2352 //else: get, already set above
2354 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2356 event
.m_item
= info
->item
.hItem
;
2360 case TVN_ITEMEXPANDING
:
2361 case TVN_ITEMEXPANDED
:
2363 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2366 switch ( tv
->action
)
2369 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2377 what
= IDX_COLLAPSE
;
2381 int how
= hdr
->code
== TVN_ITEMEXPANDING ? IDX_DOING
2384 eventType
= gs_expandEvents
[what
][how
];
2386 event
.m_item
= tv
->itemNew
.hItem
;
2392 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2393 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2395 // fabricate the lParam and wParam parameters sufficiently
2396 // similar to the ones from a "real" WM_KEYDOWN so that
2397 // CreateKeyEvent() works correctly
2398 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2399 WXLPARAM lParam
= (isAltDown ? KF_ALTDOWN
: 0) << 16;
2401 WXWPARAM wParam
= info
->wVKey
;
2403 int keyCode
= wxCharCodeMSWToWX(wParam
);
2406 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2411 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2416 // a separate event for Space/Return
2417 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2418 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2421 if ( !HasFlag(wxTR_MULTIPLE
) )
2422 item
= GetSelection();
2424 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2426 (void)GetEventHandler()->ProcessEvent(event2
);
2431 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2432 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2433 // we have to handle both messages:
2434 case TVN_SELCHANGEDA
:
2435 case TVN_SELCHANGEDW
:
2436 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2439 case TVN_SELCHANGINGA
:
2440 case TVN_SELCHANGINGW
:
2442 if ( eventType
== wxEVT_NULL
)
2443 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2444 //else: already set above
2446 if (hdr
->code
== TVN_SELCHANGINGW
||
2447 hdr
->code
== TVN_SELCHANGEDW
)
2449 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2450 event
.m_item
= tv
->itemNew
.hItem
;
2451 event
.m_itemOld
= tv
->itemOld
.hItem
;
2455 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2456 event
.m_item
= tv
->itemNew
.hItem
;
2457 event
.m_itemOld
= tv
->itemOld
.hItem
;
2462 // instead of explicitly checking for _WIN32_IE, check if the
2463 // required symbols are available in the headers
2464 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2467 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2468 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2469 switch ( nmcd
.dwDrawStage
)
2472 // if we've got any items with non standard attributes,
2473 // notify us before painting each item
2474 *result
= m_hasAnyAttr ? CDRF_NOTIFYITEMDRAW
2478 case CDDS_ITEMPREPAINT
:
2480 wxMapTreeAttr
::iterator
2481 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2483 if ( it
== m_attrs
.end() )
2485 // nothing to do for this item
2486 *result
= CDRF_DODEFAULT
;
2490 wxTreeItemAttr
* const attr
= it
->second
;
2492 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2493 TVIF_STATE
, TVIS_DROPHILITED
);
2495 const UINT tvItemState
= tvItem
.state
;
2497 // selection colours should override ours,
2498 // otherwise it is too confusing to the user
2499 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2500 !(tvItemState
& TVIS_DROPHILITED
) )
2503 if ( attr
->HasBackgroundColour() )
2505 colBack
= attr
->GetBackgroundColour();
2506 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2510 // but we still want to keep the special foreground
2511 // colour when we don't have focus (we can't keep
2512 // it when we do, it would usually be unreadable on
2513 // the almost inverted bg colour...)
2514 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2515 FindFocus() != this ) &&
2516 !(tvItemState
& TVIS_DROPHILITED
) )
2519 if ( attr
->HasTextColour() )
2521 colText
= attr
->GetTextColour();
2522 lptvcd
->clrText
= wxColourToRGB(colText
);
2526 if ( attr
->HasFont() )
2528 HFONT hFont
= GetHfontOf(attr
->GetFont());
2530 ::SelectObject(nmcd
.hdc
, hFont
);
2532 *result
= CDRF_NEWFONT
;
2534 else // no specific font
2536 *result
= CDRF_DODEFAULT
;
2542 *result
= CDRF_DODEFAULT
;
2546 // we always process it
2548 #endif // have owner drawn support in headers
2552 DWORD pos
= GetMessagePos();
2554 point
.x
= LOWORD(pos
);
2555 point
.y
= HIWORD(pos
);
2556 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2558 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2559 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2561 event
.m_item
= item
;
2562 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2570 TV_HITTESTINFO tvhti
;
2571 ::GetCursorPos(&tvhti
.pt
);
2572 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2573 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2575 if ( tvhti
.flags
& TVHT_ONITEM
)
2577 event
.m_item
= tvhti
.hItem
;
2578 eventType
= (int)hdr
->code
== NM_DBLCLK
2579 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2580 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2582 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2583 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2592 return wxControl
::MSWOnNotify(idCtrl
, lParam
, result
);
2595 event
.SetEventType(eventType
);
2597 if ( event
.m_item
.IsOk() )
2598 event
.SetClientObject(GetItemData(event
.m_item
));
2600 bool processed
= GetEventHandler()->ProcessEvent(event
);
2603 switch ( hdr
->code
)
2606 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2607 // the return code of this event handler as the return value for
2608 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2609 // expanded status would never work
2614 case TVN_BEGINRDRAG
:
2615 if ( event
.IsAllowed() )
2617 // normally this is impossible because the m_dragImage is
2618 // deleted once the drag operation is over
2619 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2621 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2622 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2623 m_dragImage
->Show();
2627 case TVN_DELETEITEM
:
2629 // NB: we might process this message using wxWidgets event
2630 // tables, but due to overhead of wxWin event system we
2631 // prefer to do it here ourself (otherwise deleting a tree
2632 // with many items is just too slow)
2633 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2635 wxTreeItemParam
*param
=
2636 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2639 processed
= true; // Make sure we don't get called twice
2643 case TVN_BEGINLABELEDIT
:
2644 // return true to cancel label editing
2645 *result
= !event
.IsAllowed();
2647 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2648 if ( event
.IsAllowed() )
2650 HWND hText
= TreeView_GetEditControl(GetHwnd());
2653 // MBN: if m_textCtrl already has an HWND, it is a stale
2654 // pointer from a previous edit (because the user
2655 // didn't modify the label before dismissing the control,
2656 // and TVN_ENDLABELEDIT was not sent), so delete it
2657 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2660 m_textCtrl
= new wxTextCtrl();
2661 m_textCtrl
->SetParent(this);
2662 m_textCtrl
->SetHWND((WXHWND
)hText
);
2663 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2665 // set wxTE_PROCESS_ENTER style for the text control to
2666 // force it to process the Enter presses itself, otherwise
2667 // they could be stolen from it by the dialog
2669 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2670 | wxTE_PROCESS_ENTER
);
2673 else // we had set m_idEdited before
2679 case TVN_ENDLABELEDIT
:
2680 // return true to set the label to the new string: note that we
2681 // also must pretend that we did process the message or it is going
2682 // to be passed to DefWindowProc() which will happily return false
2683 // cancelling the label change
2684 *result
= event
.IsAllowed();
2687 // ensure that we don't have the text ctrl which is going to be
2693 #ifdef TVN_GETINFOTIP
2694 case TVN_GETINFOTIP
:
2696 // If the user permitted a tooltip change, change it
2697 if (event
.IsAllowed())
2699 SetToolTip(event
.m_label
);
2706 case TVN_SELCHANGING
:
2707 case TVN_ITEMEXPANDING
:
2708 // return true to prevent the action from happening
2709 *result
= !event
.IsAllowed();
2712 case TVN_ITEMEXPANDED
:
2713 // the item is not refreshed properly after expansion when it has
2714 // an image depending on the expanded/collapsed state - bug in
2715 // comctl32.dll or our code?
2717 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2718 wxTreeItemId
id(tv
->itemNew
.hItem
);
2720 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2728 case TVN_GETDISPINFO
:
2729 // NB: so far the user can't set the image himself anyhow, so do it
2730 // anyway - but this may change later
2731 //if ( /* !processed && */ )
2733 wxTreeItemId item
= event
.m_item
;
2734 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2736 const wxTreeItemParam
* const param
= GetItemParam(item
);
2740 if ( info
->item
.mask
& TVIF_IMAGE
)
2745 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2746 : wxTreeItemIcon_Normal
2749 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2751 info
->item
.iSelectedImage
=
2754 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2755 : wxTreeItemIcon_Selected
2762 // for the other messages the return value is ignored and there is
2763 // nothing special to do
2768 // ----------------------------------------------------------------------------
2770 // ----------------------------------------------------------------------------
2772 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2773 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2775 void wxTreeCtrl
::SetState(const wxTreeItemId
& node
, int state
)
2778 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2779 tvi
.mask
= TVIF_STATE
;
2780 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2782 // Select the specified state, or -1 == cycle to the next one.
2785 TreeView_GetItem(GetHwnd(), &tvi
);
2787 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2788 if ( state
== m_imageListState
->GetImageCount() )
2792 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2793 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2795 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2797 TreeView_SetItem(GetHwnd(), &tvi
);
2800 int wxTreeCtrl
::GetState(const wxTreeItemId
& node
)
2803 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2804 tvi
.mask
= TVIF_STATE
;
2805 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2806 TreeView_GetItem(GetHwnd(), &tvi
);
2808 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2811 #endif // wxUSE_TREECTRL