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)))
56 // older SDKs are missing these
57 #ifndef TVN_ITEMCHANGINGA
59 #define TVN_ITEMCHANGINGA (TVN_FIRST-16)
60 #define TVN_ITEMCHANGINGW (TVN_FIRST-17)
62 typedef struct tagNMTVITEMCHANGE
75 // this global variable is used on vista systems for preventing unwanted
76 // item state changes in the vista tree control. It is only used in
77 // multi-select mode on vista systems.
79 static HTREEITEM gs_unlockItem
= NULL
;
81 class TreeItemUnlocker
84 TreeItemUnlocker(HTREEITEM item
) { gs_unlockItem
= item
; }
85 ~TreeItemUnlocker() { gs_unlockItem
= NULL
; }
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 // wrappers for TreeView_GetItem/TreeView_SetItem
94 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
98 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
99 tvi
.stateMask
= TVIS_SELECTED
;
102 TreeItemUnlocker
unlocker(hItem
);
104 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
106 wxLogLastError(wxT("TreeView_GetItem"));
109 return (tvi
.state
& TVIS_SELECTED
) != 0;
112 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
115 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
116 tvi
.stateMask
= TVIS_SELECTED
;
117 tvi
.state
= select
? TVIS_SELECTED
: 0;
120 TreeItemUnlocker
unlocker(hItem
);
122 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
124 wxLogLastError(wxT("TreeView_SetItem"));
131 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
133 SelectItem(hwndTV
, htItem
, false);
136 // helper function which selects all items in a range and, optionally,
137 // unselects all others
138 static void SelectRange(HWND hwndTV
,
141 bool unselectOthers
= true)
143 // find the first (or last) item and select it
145 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
146 while ( htItem
&& cont
)
148 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
150 if ( !IsItemSelected(hwndTV
, htItem
) )
152 SelectItem(hwndTV
, htItem
);
159 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
161 UnselectItem(hwndTV
, htItem
);
165 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
168 // select the items in range
169 cont
= htFirst
!= htLast
;
170 while ( htItem
&& cont
)
172 if ( !IsItemSelected(hwndTV
, htItem
) )
174 SelectItem(hwndTV
, htItem
);
177 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
179 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
183 if ( unselectOthers
)
187 if ( IsItemSelected(hwndTV
, htItem
) )
189 UnselectItem(hwndTV
, htItem
);
192 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
196 // seems to be necessary - otherwise the just selected items don't always
197 // appear as selected
198 UpdateWindow(hwndTV
);
201 // helper function which tricks the standard control into changing the focused
202 // item without changing anything else (if someone knows why Microsoft doesn't
203 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
205 // returns true if the focus was changed, false if the given item was already
207 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
210 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
212 if ( htItem
== htFocus
)
217 // remember the selection state of the item
218 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
220 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
222 // prevent the tree from unselecting the old focus which it
223 // would do by default (TreeView_SelectItem unselects the
225 TreeView_SelectItem(hwndTV
, 0);
226 SelectItem(hwndTV
, htFocus
);
229 TreeView_SelectItem(hwndTV
, htItem
);
233 // need to clear the selection which TreeView_SelectItem() gave
235 UnselectItem(hwndTV
, htItem
);
237 //else: was selected, still selected - ok
241 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
243 // just clear the focus
244 TreeView_SelectItem(hwndTV
, 0);
246 if ( wasFocusSelected
)
248 // restore the selection state
249 SelectItem(hwndTV
, htFocus
);
256 // ----------------------------------------------------------------------------
258 // ----------------------------------------------------------------------------
260 // a convenient wrapper around TV_ITEM struct which adds a ctor
262 #pragma warning( disable : 4097 ) // inheriting from typedef
265 struct wxTreeViewItem
: public TV_ITEM
267 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
268 UINT mask_
, // fields which are valid
269 UINT stateMask_
= 0) // for TVIF_STATE only
273 // hItem member is always valid
274 mask
= mask_
| TVIF_HANDLE
;
275 stateMask
= stateMask_
;
280 // ----------------------------------------------------------------------------
281 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
283 // We need this for a couple of reasons:
285 // 1) This class is needed for support of different images: the Win32 common
286 // control natively supports only 2 images (the normal one and another for the
287 // selected state). We wish to provide support for 2 more of them for folder
288 // items (i.e. those which have children): for expanded state and for expanded
289 // selected state. For this we use this structure to store the additional items
292 // 2) This class is also needed to hold the HITEM so that we can sort
293 // it correctly in the MSW sort callback.
295 // In addition it makes other workarounds such as this easier and helps
296 // simplify the code.
297 // ----------------------------------------------------------------------------
299 class wxTreeItemParam
306 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
312 // dtor deletes the associated data as well
313 virtual ~wxTreeItemParam() { delete m_data
; }
316 // get the real data associated with the item
317 wxTreeItemData
*GetData() const { return m_data
; }
319 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
321 // do we have such image?
322 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
323 // get image, falling back to the other images if this one is not
325 int GetImage(wxTreeItemIcon which
) const
327 int image
= m_images
[which
];
332 case wxTreeItemIcon_SelectedExpanded
:
333 image
= GetImage(wxTreeItemIcon_Expanded
);
338 case wxTreeItemIcon_Selected
:
339 case wxTreeItemIcon_Expanded
:
340 image
= GetImage(wxTreeItemIcon_Normal
);
343 case wxTreeItemIcon_Normal
:
348 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
354 // change the given image
355 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
358 const wxTreeItemId
& GetItem() const { return m_item
; }
360 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
363 // all the images associated with the item
364 int m_images
[wxTreeItemIcon_Max
];
366 // item for sort callbacks
369 // the real client data
370 wxTreeItemData
*m_data
;
372 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
375 // wxVirutalNode is used in place of a single root when 'hidden' root is
377 class wxVirtualNode
: public wxTreeViewItem
380 wxVirtualNode(wxTreeItemParam
*param
)
381 : wxTreeViewItem(TVI_ROOT
, 0)
391 wxTreeItemParam
*GetParam() const { return m_param
; }
392 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
395 wxTreeItemParam
*m_param
;
397 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
401 #pragma warning( default : 4097 )
404 // a macro to get the virtual root, returns NULL if none
405 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
407 // returns true if the item is the virtual root
408 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
410 // a class which encapsulates the tree traversal logic: it vists all (unless
411 // OnVisit() returns false) items under the given one
412 class wxTreeTraversal
415 wxTreeTraversal(const wxTreeCtrl
*tree
)
420 // give it a virtual dtor: not really needed as the class is never used
421 // polymorphically and not even allocated on heap at all, but this is safer
422 // (in case it ever is) and silences the compiler warnings for now
423 virtual ~wxTreeTraversal() { }
425 // do traverse the tree: visit all items (recursively by default) under the
426 // given one; return true if all items were traversed or false if the
427 // traversal was aborted because OnVisit returned false
428 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
430 // override this function to do whatever is needed for each item, return
431 // false to stop traversing
432 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
435 const wxTreeCtrl
*GetTree() const { return m_tree
; }
438 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
440 const wxTreeCtrl
*m_tree
;
442 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
445 // internal class for getting the selected items
446 class TraverseSelections
: public wxTreeTraversal
449 TraverseSelections(const wxTreeCtrl
*tree
,
450 wxArrayTreeItemIds
& selections
)
451 : wxTreeTraversal(tree
), m_selections(selections
)
453 m_selections
.Empty();
455 if (tree
->GetCount() > 0)
456 DoTraverse(tree
->GetRootItem());
459 virtual bool OnVisit(const wxTreeItemId
& item
)
461 const wxTreeCtrl
* const tree
= GetTree();
463 // can't visit a virtual node.
464 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
469 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
471 m_selections
.Add(item
);
477 size_t GetCount() const { return m_selections
.GetCount(); }
480 wxArrayTreeItemIds
& m_selections
;
482 DECLARE_NO_COPY_CLASS(TraverseSelections
)
485 // internal class for counting tree items
486 class TraverseCounter
: public wxTreeTraversal
489 TraverseCounter(const wxTreeCtrl
*tree
,
490 const wxTreeItemId
& root
,
492 : wxTreeTraversal(tree
)
496 DoTraverse(root
, recursively
);
499 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
506 size_t GetCount() const { return m_count
; }
511 DECLARE_NO_COPY_CLASS(TraverseCounter
)
514 // ----------------------------------------------------------------------------
516 // ----------------------------------------------------------------------------
518 #if wxUSE_EXTENDED_RTTI
519 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
521 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
522 // new style border flags, we put them first to
523 // use them for streaming out
524 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
525 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
526 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
527 wxFLAGS_MEMBER(wxBORDER_RAISED
)
528 wxFLAGS_MEMBER(wxBORDER_STATIC
)
529 wxFLAGS_MEMBER(wxBORDER_NONE
)
531 // old style border flags
532 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
533 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
534 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
535 wxFLAGS_MEMBER(wxRAISED_BORDER
)
536 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
537 wxFLAGS_MEMBER(wxBORDER
)
539 // standard window styles
540 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
541 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
542 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
543 wxFLAGS_MEMBER(wxWANTS_CHARS
)
544 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
545 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
546 wxFLAGS_MEMBER(wxVSCROLL
)
547 wxFLAGS_MEMBER(wxHSCROLL
)
549 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
550 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
551 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
552 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
553 wxFLAGS_MEMBER(wxTR_NO_LINES
)
554 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
555 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
556 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
557 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
558 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
559 wxFLAGS_MEMBER(wxTR_SINGLE
)
560 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
561 #if WXWIN_COMPATIBILITY_2_8
562 wxFLAGS_MEMBER(wxTR_EXTENDED
)
564 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
566 wxEND_FLAGS( wxTreeCtrlStyle
)
568 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
570 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
571 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
572 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
573 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
574 wxEND_PROPERTIES_TABLE()
576 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
577 wxEND_HANDLERS_TABLE()
579 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
581 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 // indices in gs_expandEvents table below
603 // handy table for sending events - it has to be initialized during run-time
604 // now so can't be const any more
605 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
608 but logically it's a const table with the following entries:
611 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
612 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
616 // ============================================================================
618 // ============================================================================
620 // ----------------------------------------------------------------------------
622 // ----------------------------------------------------------------------------
624 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
626 if ( !OnVisit(root
) )
629 return Traverse(root
, recursively
);
632 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
634 wxTreeItemIdValue cookie
;
635 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
636 while ( child
.IsOk() )
638 // depth first traversal
639 if ( recursively
&& !Traverse(child
, true) )
642 if ( !OnVisit(child
) )
645 child
= m_tree
->GetNextChild(root
, cookie
);
651 // ----------------------------------------------------------------------------
652 // construction and destruction
653 // ----------------------------------------------------------------------------
655 void wxTreeCtrl::Init()
658 m_hasAnyAttr
= false;
662 m_pVirtualRoot
= NULL
;
664 // initialize the global array of events now as it can't be done statically
665 // with the wxEVT_XXX values being allocated during run-time only
666 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
667 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
668 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
669 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
672 bool wxTreeCtrl::Create(wxWindow
*parent
,
677 const wxValidator
& validator
,
678 const wxString
& name
)
682 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
683 style
|= wxBORDER_SUNKEN
;
685 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
689 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
690 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
692 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
693 wstyle
|= TVS_HASLINES
;
694 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
695 wstyle
|= TVS_HASBUTTONS
;
697 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
698 wstyle
|= TVS_EDITLABELS
;
700 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
701 wstyle
|= TVS_LINESATROOT
;
703 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
705 if ( wxApp::GetComCtl32Version() >= 471 )
706 wstyle
|= TVS_FULLROWSELECT
;
709 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
710 // Need so that TVN_GETINFOTIP messages will be sent
711 wstyle
|= TVS_INFOTIP
;
714 // Create the tree control.
715 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
718 #if wxUSE_COMCTL32_SAFELY
719 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
720 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
722 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
723 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
725 // This works around a bug in the Windows tree control whereby for some versions
726 // of comctrl32, setting any colour actually draws the background in black.
727 // This will initialise the background to the system colour.
728 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
729 // Assume the user has an updated comctl32.dll.
730 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
731 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
732 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
736 // VZ: this is some experimental code which may be used to get the
737 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
738 // AFAIK, the standard DLL does about the same thing anyhow.
740 if ( m_windowStyle
& wxTR_MULTIPLE
)
744 // create the DC compatible with the current screen
745 HDC hdcMem
= CreateCompatibleDC(NULL
);
747 // create a mono bitmap of the standard size
748 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
749 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
750 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
751 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
752 1, // # of color planes
753 1, // # bits needed for one pixel
754 0); // array containing colour data
755 SelectObject(hdcMem
, hbmpCheck
);
757 // then draw a check mark into it
758 RECT rect
= { 0, 0, x
, y
};
759 if ( !::DrawFrameControl(hdcMem
, &rect
,
761 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
763 wxLogLastError(wxT("DrawFrameControl(check)"));
766 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
767 imagelistCheckboxes
.Add(bmp
);
769 if ( !::DrawFrameControl(hdcMem
, &rect
,
773 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
776 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
777 imagelistCheckboxes
.Add(bmp
);
783 SetStateImageList(&imagelistCheckboxes
);
787 wxSetCCUnicodeFormat(GetHwnd());
792 wxTreeCtrl::~wxTreeCtrl()
794 // delete any attributes
797 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
799 // prevent TVN_DELETEITEM handler from deleting the attributes again!
800 m_hasAnyAttr
= false;
805 // delete user data to prevent memory leaks
806 // also deletes hidden root node storage.
810 // ----------------------------------------------------------------------------
812 // ----------------------------------------------------------------------------
814 /* static */ wxVisualAttributes
815 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
817 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
819 // common controls have their own default font
820 attrs
.font
= wxGetCCDefaultFont();
826 // simple wrappers which add error checking in debug mode
828 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
830 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
831 _T("can't retrieve virtual root item") );
833 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
835 wxLogLastError(wxT("TreeView_GetItem"));
843 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
845 TreeItemUnlocker
unlocker(tvItem
->hItem
);
847 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
849 wxLogLastError(wxT("TreeView_SetItem"));
853 unsigned int wxTreeCtrl::GetCount() const
855 return (unsigned int)TreeView_GetCount(GetHwnd());
858 unsigned int wxTreeCtrl::GetIndent() const
860 return TreeView_GetIndent(GetHwnd());
863 void wxTreeCtrl::SetIndent(unsigned int indent
)
865 TreeView_SetIndent(GetHwnd(), indent
);
868 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
871 (void) TreeView_SetImageList(GetHwnd(),
872 imageList
? imageList
->GetHIMAGELIST() : 0,
876 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
878 if (m_ownsImageListNormal
)
879 delete m_imageListNormal
;
881 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
882 m_ownsImageListNormal
= false;
885 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
887 if (m_ownsImageListState
) delete m_imageListState
;
888 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
889 m_ownsImageListState
= false;
892 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
893 bool recursively
) const
895 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
897 TraverseCounter
counter(this, item
, recursively
);
898 return counter
.GetCount() - 1;
901 // ----------------------------------------------------------------------------
903 // ----------------------------------------------------------------------------
905 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
907 #if !wxUSE_COMCTL32_SAFELY
908 if ( !wxWindowBase::SetBackgroundColour(colour
) )
911 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
917 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
919 #if !wxUSE_COMCTL32_SAFELY
920 if ( !wxWindowBase::SetForegroundColour(colour
) )
923 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
929 // ----------------------------------------------------------------------------
931 // ----------------------------------------------------------------------------
933 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
935 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
938 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
940 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
942 wxChar buf
[512]; // the size is arbitrary...
944 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
945 tvItem
.pszText
= buf
;
946 tvItem
.cchTextMax
= WXSIZEOF(buf
);
947 if ( !DoGetItem(&tvItem
) )
949 // don't return some garbage which was on stack, but an empty string
953 return wxString(buf
);
956 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
958 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
960 if ( IS_VIRTUAL_ROOT(item
) )
963 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
964 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
967 // when setting the text of the item being edited, the text control should
968 // be updated to reflect the new text as well, otherwise calling
969 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
971 // don't use GetEditControl() here because m_textCtrl is not set yet
972 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
975 if ( item
== m_idEdited
)
977 ::SetWindowText(hwndEdit
, text
.wx_str());
982 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
983 wxTreeItemIcon which
) const
985 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
987 if ( IsHiddenRoot(item
) )
989 // no images for hidden root item
993 wxTreeItemParam
*param
= GetItemParam(item
);
995 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
998 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
999 wxTreeItemIcon which
)
1001 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1002 wxCHECK_RET( which
>= 0 &&
1003 which
< wxTreeItemIcon_Max
,
1004 wxT("invalid image index"));
1007 if ( IsHiddenRoot(item
) )
1009 // no images for hidden root item
1013 wxTreeItemParam
*data
= GetItemParam(item
);
1017 data
->SetImage(image
, which
);
1022 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1024 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1026 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1028 // hidden root may still have data.
1029 if ( IS_VIRTUAL_ROOT(item
) )
1031 return GET_VIRTUAL_ROOT()->GetParam();
1035 if ( !DoGetItem(&tvItem
) )
1040 return (wxTreeItemParam
*)tvItem
.lParam
;
1043 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1045 wxTreeItemParam
*data
= GetItemParam(item
);
1047 return data
? data
->GetData() : NULL
;
1050 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1052 // first, associate this piece of data with this item
1058 wxTreeItemParam
*param
= GetItemParam(item
);
1060 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1062 param
->SetData(data
);
1065 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1067 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1069 if ( IS_VIRTUAL_ROOT(item
) )
1072 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1073 tvItem
.cChildren
= (int)has
;
1077 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1079 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1081 if ( IS_VIRTUAL_ROOT(item
) )
1084 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1085 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1089 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1091 if ( IS_VIRTUAL_ROOT(item
) )
1094 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1095 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1099 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1101 if ( IS_VIRTUAL_ROOT(item
) )
1105 if ( GetBoundingRect(item
, rect
) )
1111 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1113 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1115 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1116 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1119 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1121 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1123 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1124 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1127 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1129 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1131 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1132 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1135 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1136 const wxColour
& col
)
1138 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1140 wxTreeItemAttr
*attr
;
1141 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1142 if ( it
== m_attrs
.end() )
1144 m_hasAnyAttr
= true;
1146 m_attrs
[item
.m_pItem
] =
1147 attr
= new wxTreeItemAttr
;
1154 attr
->SetTextColour(col
);
1159 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1160 const wxColour
& col
)
1162 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1164 wxTreeItemAttr
*attr
;
1165 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1166 if ( it
== m_attrs
.end() )
1168 m_hasAnyAttr
= true;
1170 m_attrs
[item
.m_pItem
] =
1171 attr
= new wxTreeItemAttr
;
1173 else // already in the hash
1178 attr
->SetBackgroundColour(col
);
1183 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1185 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1187 wxTreeItemAttr
*attr
;
1188 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1189 if ( it
== m_attrs
.end() )
1191 m_hasAnyAttr
= true;
1193 m_attrs
[item
.m_pItem
] =
1194 attr
= new wxTreeItemAttr
;
1196 else // already in the hash
1201 attr
->SetFont(font
);
1203 // Reset the item's text to ensure that the bounding rect will be adjusted
1204 // for the new font.
1205 SetItemText(item
, GetItemText(item
));
1210 // ----------------------------------------------------------------------------
1212 // ----------------------------------------------------------------------------
1214 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1216 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1218 if ( item
== wxTreeItemId(TVI_ROOT
) )
1220 // virtual (hidden) root is never visible
1224 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1227 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1228 // the HTREEITEM with TVM_GETITEMRECT
1229 *(HTREEITEM
*)&rect
= HITEM(item
);
1231 // true means to get rect for just the text, not the whole line
1232 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1234 // if TVM_GETITEMRECT returned false, then the item is definitely not
1235 // visible (because its parent is not expanded)
1239 // however if it returned true, the item might still be outside the
1240 // currently visible part of the tree, test for it (notice that partly
1241 // visible means visible here)
1242 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1245 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1247 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1249 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1252 return tvItem
.cChildren
!= 0;
1255 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1257 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1259 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1262 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1265 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1267 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1269 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1272 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1275 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1277 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1279 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1282 return (tvItem
.state
& TVIS_BOLD
) != 0;
1285 // ----------------------------------------------------------------------------
1287 // ----------------------------------------------------------------------------
1289 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1291 // Root may be real (visible) or virtual (hidden).
1292 if ( GET_VIRTUAL_ROOT() )
1295 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1298 wxTreeItemId
wxTreeCtrl::GetSelection() const
1300 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1301 wxT("this only works with single selection controls") );
1303 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1306 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1308 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1312 if ( IS_VIRTUAL_ROOT(item
) )
1314 // no parent for the virtual root
1319 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1320 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1322 // the top level items should have the virtual root as their parent
1327 return wxTreeItemId(hItem
);
1330 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1331 wxTreeItemIdValue
& cookie
) const
1333 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1335 // remember the last child returned in 'cookie'
1336 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1338 return wxTreeItemId(cookie
);
1341 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1342 wxTreeItemIdValue
& cookie
) const
1344 wxTreeItemId
fromCookie(cookie
);
1346 HTREEITEM hitem
= HITEM(fromCookie
);
1348 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1350 wxTreeItemId
item(hitem
);
1352 cookie
= item
.m_pItem
;
1357 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1359 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1361 // can this be done more efficiently?
1362 wxTreeItemIdValue cookie
;
1364 wxTreeItemId childLast
,
1365 child
= GetFirstChild(item
, cookie
);
1366 while ( child
.IsOk() )
1369 child
= GetNextChild(item
, cookie
);
1375 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1377 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1378 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1381 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1383 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1384 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1387 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1389 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1392 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1394 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1395 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1397 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1398 if ( next
.IsOk() && !IsVisible(next
) )
1400 // Win32 considers that any non-collapsed item is visible while we want
1401 // to return only really visible items
1408 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1410 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1411 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1413 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1414 if ( prev
.IsOk() && !IsVisible(prev
) )
1416 // just as above, Win32 function will happily return the previous item
1417 // in the tree for the first visible item too
1424 // ----------------------------------------------------------------------------
1425 // multiple selections emulation
1426 // ----------------------------------------------------------------------------
1428 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1430 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1432 // receive the desired information.
1433 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1436 // state image indices are 1 based
1437 return ((tvItem
.state
>> 12) - 1) == 1;
1440 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1442 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1444 // receive the desired information.
1445 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1449 // state images are one-based
1450 tvItem
.state
= (check
? 2 : 1) << 12;
1455 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1457 TraverseSelections
selector(this, selections
);
1459 return selector
.GetCount();
1462 // ----------------------------------------------------------------------------
1464 // ----------------------------------------------------------------------------
1466 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1467 const wxTreeItemId
& hInsertAfter
,
1468 const wxString
& text
,
1469 int image
, int selectedImage
,
1470 wxTreeItemData
*data
)
1472 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1474 _T("can't have more than one root in the tree") );
1476 TV_INSERTSTRUCT tvIns
;
1477 tvIns
.hParent
= HITEM(parent
);
1478 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1480 // this is how we insert the item as the first child: supply a NULL
1482 if ( !tvIns
.hInsertAfter
)
1484 tvIns
.hInsertAfter
= TVI_FIRST
;
1488 if ( !text
.empty() )
1491 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1495 tvIns
.item
.pszText
= NULL
;
1496 tvIns
.item
.cchTextMax
= 0;
1499 // create the param which will store the other item parameters
1500 wxTreeItemParam
*param
= new wxTreeItemParam
;
1502 // we return the images on demand as they depend on whether the item is
1503 // expanded or collapsed too in our case
1504 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1505 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1506 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1508 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1509 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1512 tvIns
.item
.lParam
= (LPARAM
)param
;
1513 tvIns
.item
.mask
= mask
;
1515 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1518 wxLogLastError(wxT("TreeView_InsertItem"));
1521 // associate the application tree item with Win32 tree item handle
1524 // setup wxTreeItemData
1527 param
->SetData(data
);
1531 return wxTreeItemId(id
);
1534 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1535 int image
, int selectedImage
,
1536 wxTreeItemData
*data
)
1538 if ( HasFlag(wxTR_HIDE_ROOT
) )
1540 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1542 // create a virtual root item, the parent for all the others
1543 wxTreeItemParam
*param
= new wxTreeItemParam
;
1544 param
->SetData(data
);
1546 m_pVirtualRoot
= new wxVirtualNode(param
);
1551 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1552 text
, image
, selectedImage
, data
);
1555 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1557 const wxString
& text
,
1558 int image
, int selectedImage
,
1559 wxTreeItemData
*data
)
1561 wxTreeItemId idPrev
;
1562 if ( index
== (size_t)-1 )
1564 // special value: append to the end
1567 else // find the item from index
1569 wxTreeItemIdValue cookie
;
1570 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1571 while ( index
!= 0 && idCur
.IsOk() )
1576 idCur
= GetNextChild(parent
, cookie
);
1579 // assert, not check: if the index is invalid, we will append the item
1581 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1584 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1587 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1589 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1591 wxLogLastError(wxT("TreeView_DeleteItem"));
1595 // delete all children (but don't delete the item itself)
1596 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1598 wxTreeItemIdValue cookie
;
1600 wxArrayTreeItemIds children
;
1601 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1602 while ( child
.IsOk() )
1604 children
.Add(child
);
1606 child
= GetNextChild(item
, cookie
);
1609 size_t nCount
= children
.Count();
1610 for ( size_t n
= 0; n
< nCount
; n
++ )
1612 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1614 wxLogLastError(wxT("TreeView_DeleteItem"));
1619 void wxTreeCtrl::DeleteAllItems()
1621 // delete the "virtual" root item.
1622 if ( GET_VIRTUAL_ROOT() )
1624 delete GET_VIRTUAL_ROOT();
1625 m_pVirtualRoot
= NULL
;
1628 // and all the real items
1630 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1632 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1636 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1638 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1639 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1640 flag
== TVE_EXPAND
||
1642 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1644 // A hidden root can be neither expanded nor collapsed.
1645 wxCHECK_RET( !IsHiddenRoot(item
),
1646 wxT("Can't expand/collapse hidden root node!") );
1648 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1649 // emulate them. This behaviour has changed slightly with comctl32.dll
1650 // v 4.70 - now it does send them but only the first time. To maintain
1651 // compatible behaviour and also in order to not have surprises with the
1652 // future versions, don't rely on this and still do everything ourselves.
1653 // To avoid that the messages be sent twice when the item is expanded for
1654 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1656 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1660 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1662 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1664 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1668 (void)GetEventHandler()->ProcessEvent(event
);
1670 //else: change didn't took place, so do nothing at all
1673 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1675 DoExpand(item
, TVE_EXPAND
);
1678 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1680 DoExpand(item
, TVE_COLLAPSE
);
1683 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1685 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1688 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1690 DoExpand(item
, TVE_TOGGLE
);
1693 void wxTreeCtrl::Unselect()
1695 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1696 wxT("doesn't make sense, may be you want UnselectAll()?") );
1698 // just remove the selection
1699 SelectItem(wxTreeItemId());
1702 void wxTreeCtrl::UnselectAll()
1704 if ( m_windowStyle
& wxTR_MULTIPLE
)
1706 wxArrayTreeItemIds selections
;
1707 size_t count
= GetSelections(selections
);
1708 for ( size_t n
= 0; n
< count
; n
++ )
1710 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1713 m_htSelStart
.Unset();
1717 // just remove the selection
1722 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1724 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1726 wxASSERT_MSG( select
|| HasFlag(wxTR_MULTIPLE
),
1727 _T("SelectItem(false) works only for multiselect") );
1729 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1730 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1732 if ( HasFlag(wxTR_MULTIPLE
) )
1734 if ( !::SelectItem(GetHwnd(), HITEM(item
), select
) )
1736 wxLogLastError(wxT("TreeView_SelectItem"));
1740 else // single selection
1742 // use TreeView_SelectItem() to deselect the previous selection
1743 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1745 wxLogLastError(wxT("TreeView_SelectItem"));
1750 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1751 (void)GetEventHandler()->ProcessEvent(event
);
1753 //else: program vetoed the change
1756 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1758 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1761 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1764 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1766 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1768 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1772 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1777 void wxTreeCtrl::DeleteTextCtrl()
1781 // the HWND corresponding to this control is deleted by the tree
1782 // control itself and we don't know when exactly this happens, so check
1783 // if the window still exists before calling UnsubclassWin()
1784 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1786 m_textCtrl
->SetHWND(0);
1789 m_textCtrl
->UnsubclassWin();
1790 m_textCtrl
->SetHWND(0);
1798 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1799 wxClassInfo
*textControlClass
)
1801 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1806 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1807 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1809 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1818 // textctrl is subclassed in MSWOnNotify
1822 // End label editing, optionally cancelling the edit
1823 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1825 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1830 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1832 TV_HITTESTINFO hitTestInfo
;
1833 hitTestInfo
.pt
.x
= (int)point
.x
;
1834 hitTestInfo
.pt
.y
= (int)point
.y
;
1836 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1841 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1842 flags |= wxTREE_HITTEST_##flag
1844 TRANSLATE_FLAG(ABOVE
);
1845 TRANSLATE_FLAG(BELOW
);
1846 TRANSLATE_FLAG(NOWHERE
);
1847 TRANSLATE_FLAG(ONITEMBUTTON
);
1848 TRANSLATE_FLAG(ONITEMICON
);
1849 TRANSLATE_FLAG(ONITEMINDENT
);
1850 TRANSLATE_FLAG(ONITEMLABEL
);
1851 TRANSLATE_FLAG(ONITEMRIGHT
);
1852 TRANSLATE_FLAG(ONITEMSTATEICON
);
1853 TRANSLATE_FLAG(TOLEFT
);
1854 TRANSLATE_FLAG(TORIGHT
);
1856 #undef TRANSLATE_FLAG
1858 return wxTreeItemId(hitTestInfo
.hItem
);
1861 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1863 bool textOnly
) const
1867 // Virtual root items have no bounding rectangle
1868 if ( IS_VIRTUAL_ROOT(item
) )
1873 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1876 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1882 // couldn't retrieve rect: for example, item isn't visible
1887 // ----------------------------------------------------------------------------
1889 // ----------------------------------------------------------------------------
1891 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1892 // functions such as IsDataIndirect()
1893 class wxTreeSortHelper
1896 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1899 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1901 return ((wxTreeItemParam
*)lParam
)->GetItem();
1905 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1909 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1910 wxT("sorting tree without data doesn't make sense") );
1912 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1914 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1915 GetIdFromData(pItem2
));
1918 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1920 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1922 // rely on the fact that TreeView_SortChildren does the same thing as our
1923 // default behaviour, i.e. sorts items alphabetically and so call it
1924 // directly if we're not in derived class (much more efficient!)
1925 // RN: Note that if you find you're code doesn't sort as expected this
1926 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1927 // combo for your derived wxTreeCtrl if will sort without
1929 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1931 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1936 tvSort
.hParent
= HITEM(item
);
1937 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1938 tvSort
.lParam
= (LPARAM
)this;
1939 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1943 // ----------------------------------------------------------------------------
1945 // ----------------------------------------------------------------------------
1947 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1949 if ( msg
->message
== WM_KEYDOWN
)
1951 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
1953 // Only eat VK_RETURN if not being used by the application in conjunction with
1955 if ( msg
->wParam
== VK_RETURN
&& !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
)
1957 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1962 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
1965 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
1967 const int id
= (signed short)id_
;
1969 if ( cmd
== EN_UPDATE
)
1971 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1972 event
.SetEventObject( this );
1973 ProcessCommand(event
);
1975 else if ( cmd
== EN_KILLFOCUS
)
1977 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1978 event
.SetEventObject( this );
1979 ProcessCommand(event
);
1987 // command processed
1991 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1992 // only do it during dragging, minimize wxWin overhead (this is important for
1993 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1994 // instead of passing by wxWin events
1995 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1997 bool processed
= false;
1999 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2001 // This message is sent after a right-click, or when the "menu" key is pressed
2002 if ( nMsg
== WM_CONTEXTMENU
)
2004 int x
= GET_X_LPARAM(lParam
),
2005 y
= GET_Y_LPARAM(lParam
);
2007 // the item for which the menu should be shown
2010 // the position where the menu should be shown in client coordinates
2011 // (so that it can be passed directly to PopupMenu())
2014 if ( x
== -1 || y
== -1 )
2016 // this means that the event was generated from keyboard (e.g. with
2017 // Shift-F10 or special Windows menu key)
2019 // use the Explorer standard of putting the menu at the left edge
2020 // of the text, in the vertical middle of the text
2021 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2024 // Use the bounding rectangle of only the text part
2026 GetBoundingRect(item
, rect
, true);
2027 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2030 else // event from mouse, use mouse position
2032 pt
= ScreenToClient(wxPoint(x
, y
));
2034 TV_HITTESTINFO tvhti
;
2037 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2038 item
= wxTreeItemId(tvhti
.hItem
);
2042 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2044 event
.m_pointDrag
= pt
;
2046 if ( GetEventHandler()->ProcessEvent(event
) )
2048 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2050 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2052 // we only process mouse messages here and these parameters have the
2053 // same meaning for all of them
2054 int x
= GET_X_LPARAM(lParam
),
2055 y
= GET_Y_LPARAM(lParam
);
2057 TV_HITTESTINFO tvht
;
2061 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2065 case WM_LBUTTONDOWN
:
2066 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2068 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2069 m_ptClick
= wxPoint(x
, y
);
2071 if ( wParam
& MK_CONTROL
)
2075 // toggle selected state
2076 ToggleItemSelection(htItem
);
2078 ::SetFocus(GetHwnd(), htItem
);
2080 // reset on any click without Shift
2081 m_htSelStart
.Unset();
2085 else if ( wParam
& MK_SHIFT
)
2087 // this selects all items between the starting one and
2090 if ( !m_htSelStart
)
2092 // take the focused item
2093 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2097 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2098 !(wParam
& MK_CONTROL
));
2100 ::SelectItem(GetHwnd(), htItem
);
2102 ::SetFocus(GetHwnd(), htItem
);
2106 else // normal click
2108 // avoid doing anything if we click on the only
2109 // currently selected item
2113 wxArrayTreeItemIds selections
;
2114 size_t count
= GetSelections(selections
);
2117 HITEM(selections
[0]) != htItem
)
2119 // clear the previously selected items, if the
2120 // user clicked outside of the present selection.
2121 // otherwise, perform the deselection on mouse-up.
2122 // this allows multiple drag and drop to work.
2124 if (!IsItemSelected(GetHwnd(), htItem
))
2128 // prevent the click from starting in-place editing
2129 // which should only happen if we click on the
2130 // already selected item (and nothing else is
2133 TreeView_SelectItem(GetHwnd(), 0);
2134 ::SelectItem(GetHwnd(), htItem
);
2136 ::SetFocus(GetHwnd(), htItem
);
2139 else // click on a single selected item
2141 // don't interfere with the default processing in
2142 // WM_MOUSEMOVE handler below as the default window
2143 // proc will start the drag itself if we let have
2145 m_htClickedItem
.Unset();
2148 // reset on any click without Shift
2149 m_htSelStart
.Unset();
2154 case WM_RBUTTONDOWN
:
2155 // default handler removes the highlight from the currently
2156 // focused item when right mouse button is pressed on another
2157 // one but keeps the remaining items highlighted, which is
2158 // confusing, so override this default behaviour for tree with
2159 // multiple selections
2162 if ( !IsItemSelected(GetHwnd(), htItem
) )
2166 ::SetFocus(GetHwnd(), htItem
);
2169 // fire EVT_RIGHT_DOWN
2170 HandleMouseEvent(nMsg
, x
, y
, wParam
);
2174 nmhdr
.hwndFrom
= GetHwnd();
2175 nmhdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2176 nmhdr
.code
= NM_RCLICK
;
2177 ::SendMessage(::GetParent(GetHwnd()), WM_NOTIFY
,
2178 nmhdr
.idFrom
, (LPARAM
)&nmhdr
);
2180 // prevent tree control default processing, as we've
2181 // already done everything
2188 if ( m_htClickedItem
)
2190 int cx
= abs(m_ptClick
.x
- x
);
2191 int cy
= abs(m_ptClick
.y
- y
);
2193 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2194 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2199 tv
.hdr
.hwndFrom
= GetHwnd();
2200 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2201 tv
.hdr
.code
= TVN_BEGINDRAG
;
2203 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2207 wxZeroMemory(tviAux
);
2209 tviAux
.hItem
= HITEM(m_htClickedItem
);
2210 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2211 tviAux
.stateMask
= 0xffffffff;
2212 TreeView_GetItem(GetHwnd(), &tviAux
);
2214 tv
.itemNew
.state
= tviAux
.state
;
2215 tv
.itemNew
.lParam
= tviAux
.lParam
;
2220 // do it before SendMessage() call below to avoid
2221 // reentrancies here if there is another WM_MOUSEMOVE
2222 // in the queue already
2223 m_htClickedItem
.Unset();
2225 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2226 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2228 // don't pass it to the default window proc, it would
2229 // start dragging again
2233 #endif // __WXWINCE__
2238 m_dragImage
->Move(wxPoint(x
, y
));
2241 // highlight the item as target (hiding drag image is
2242 // necessary - otherwise the display will be corrupted)
2243 m_dragImage
->Hide();
2244 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2245 m_dragImage
->Show();
2248 #endif // wxUSE_DRAGIMAGE
2253 // facilitates multiple drag-and-drop
2254 if (htItem
&& isMultiple
)
2256 wxArrayTreeItemIds selections
;
2257 size_t count
= GetSelections(selections
);
2260 !(wParam
& MK_CONTROL
) &&
2261 !(wParam
& MK_SHIFT
))
2264 TreeView_SelectItem(GetHwnd(), htItem
);
2265 ::SelectItem(GetHwnd(), htItem
);
2266 ::SetFocus(GetHwnd(), htItem
);
2268 m_htClickedItem
.Unset();
2277 m_dragImage
->EndDrag();
2281 // generate the drag end event
2282 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2283 event
.m_pointDrag
= wxPoint(x
, y
);
2285 (void)GetEventHandler()->ProcessEvent(event
);
2287 // if we don't do it, the tree seems to think that 2 items
2288 // are selected simultaneously which is quite weird
2289 TreeView_SelectDropTarget(GetHwnd(), 0);
2291 #endif // wxUSE_DRAGIMAGE
2295 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2297 // the tree control greys out the selected item when it loses focus and
2298 // paints it as selected again when it regains it, but it won't do it
2299 // for the other items itself - help it
2300 wxArrayTreeItemIds selections
;
2301 size_t count
= GetSelections(selections
);
2303 for ( size_t n
= 0; n
< count
; n
++ )
2305 // TreeView_GetItemRect() will return false if item is not visible,
2306 // which may happen perfectly well
2307 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2310 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2314 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2316 bool bCtrl
= wxIsCtrlDown(),
2317 bShift
= wxIsShiftDown();
2319 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2325 ToggleItemSelection(htSel
);
2331 ::SelectItem(GetHwnd(), htSel
);
2339 if ( !bCtrl
&& !bShift
)
2341 // no modifiers, just clear selection and then let the default
2342 // processing to take place
2347 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2349 HTREEITEM htNext
= (HTREEITEM
)
2350 TreeView_GetNextItem
2354 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2360 // at the top/bottom
2366 if ( !m_htSelStart
)
2367 m_htSelStart
= htSel
;
2369 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2373 // without changing selection
2374 ::SetFocus(GetHwnd(), htNext
);
2385 // TODO: handle Shift/Ctrl with these keys
2386 if ( !bCtrl
&& !bShift
)
2390 m_htSelStart
.Unset();
2394 else if ( nMsg
== WM_COMMAND
)
2396 // if we receive a EN_KILLFOCUS command from the in-place edit control
2397 // used for label editing, make sure to end editing
2400 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2402 if ( cmd
== EN_KILLFOCUS
)
2404 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2414 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2420 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2422 if ( nMsg
== WM_CHAR
)
2424 // don't let the control process Space and Return keys because it
2425 // doesn't do anything useful with them anyhow but always beeps
2426 // annoyingly when it receives them and there is no way to turn it off
2427 // simply if you just process TREEITEM_ACTIVATED event to which Space
2428 // and Enter presses are mapped in your code
2429 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2433 else if ( nMsg
== WM_KEYDOWN
)
2435 if ( wParam
== VK_ESCAPE
)
2439 m_dragImage
->EndDrag();
2443 // if we don't do it, the tree seems to think that 2 items
2444 // are selected simultaneously which is quite weird
2445 TreeView_SelectDropTarget(GetHwnd(), 0);
2449 #endif // wxUSE_DRAGIMAGE
2451 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2454 // process WM_NOTIFY Windows message
2455 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2457 wxTreeEvent
event(wxEVT_NULL
, this);
2458 wxEventType eventType
= wxEVT_NULL
;
2459 NMHDR
*hdr
= (NMHDR
*)lParam
;
2461 switch ( hdr
->code
)
2464 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2467 case TVN_BEGINRDRAG
:
2469 if ( eventType
== wxEVT_NULL
)
2470 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2471 //else: left drag, already set above
2473 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2475 event
.m_item
= tv
->itemNew
.hItem
;
2476 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2478 // don't allow dragging by default: the user code must
2479 // explicitly say that it wants to allow it to avoid breaking
2485 case TVN_BEGINLABELEDIT
:
2487 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2488 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2490 // although the user event handler may still veto it, it is
2491 // important to set it now so that calls to SetItemText() from
2492 // the event handler would change the text controls contents
2494 event
.m_item
= info
->item
.hItem
;
2495 event
.m_label
= info
->item
.pszText
;
2496 event
.m_editCancelled
= false;
2500 case TVN_DELETEITEM
:
2502 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2503 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2505 event
.m_item
= tv
->itemOld
.hItem
;
2509 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2510 if ( it
!= m_attrs
.end() )
2519 case TVN_ENDLABELEDIT
:
2521 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2522 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2524 event
.m_item
= info
->item
.hItem
;
2525 event
.m_label
= info
->item
.pszText
;
2526 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2531 // These *must* not be removed or TVN_GETINFOTIP will
2532 // not be processed each time the mouse is moved
2533 // and the tooltip will only ever update once.
2542 #ifdef TVN_GETINFOTIP
2543 case TVN_GETINFOTIP
:
2545 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2546 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2548 // Which item are we trying to get a tooltip for?
2549 event
.m_item
= info
->hItem
;
2553 #endif // TVN_GETINFOTIP
2554 #endif // !__WXWINCE__
2556 case TVN_GETDISPINFO
:
2557 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2560 case TVN_SETDISPINFO
:
2562 if ( eventType
== wxEVT_NULL
)
2563 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2564 //else: get, already set above
2566 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2568 event
.m_item
= info
->item
.hItem
;
2572 case TVN_ITEMEXPANDING
:
2573 case TVN_ITEMEXPANDED
:
2575 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2578 switch ( tv
->action
)
2581 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2589 what
= IDX_COLLAPSE
;
2593 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2596 eventType
= gs_expandEvents
[what
][how
];
2598 event
.m_item
= tv
->itemNew
.hItem
;
2604 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2605 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2607 // fabricate the lParam and wParam parameters sufficiently
2608 // similar to the ones from a "real" WM_KEYDOWN so that
2609 // CreateKeyEvent() works correctly
2610 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2611 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2613 WXWPARAM wParam
= info
->wVKey
;
2615 int keyCode
= wxCharCodeMSWToWX(wParam
);
2618 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2623 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2628 // a separate event for Space/Return
2629 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2630 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2633 if ( !HasFlag(wxTR_MULTIPLE
) )
2634 item
= GetSelection();
2636 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2638 (void)GetEventHandler()->ProcessEvent(event2
);
2644 // Vista's tree control has introduced some problems with our
2645 // multi-selection tree. When TreeView_SelectItem() is called,
2646 // the wrong items are deselected.
2648 // Fortunately, Vista provides a new notification, TVN_ITEMCHANGING
2649 // that can be used to regulate this incorrect behavior. The
2650 // following messages will allow only the unlocked item's selection
2652 case TVN_ITEMCHANGINGA
:
2653 case TVN_ITEMCHANGINGW
:
2655 // we only need to handles these in multi-select trees
2656 if ( HasFlag(wxTR_MULTIPLE
) )
2658 // get info about the item about to be changed
2659 NMTVITEMCHANGE
* info
= (NMTVITEMCHANGE
*)lParam
;
2660 if (info
->hItem
!= gs_unlockItem
)
2662 // item's state is locked, don't allow the change
2663 // returning 1 will disallow the change
2669 // allow the state change
2673 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2674 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2675 // we have to handle both messages:
2676 case TVN_SELCHANGEDA
:
2677 case TVN_SELCHANGEDW
:
2678 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2681 case TVN_SELCHANGINGA
:
2682 case TVN_SELCHANGINGW
:
2684 if ( eventType
== wxEVT_NULL
)
2685 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2686 //else: already set above
2688 if (hdr
->code
== TVN_SELCHANGINGW
||
2689 hdr
->code
== TVN_SELCHANGEDW
)
2691 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2692 event
.m_item
= tv
->itemNew
.hItem
;
2693 event
.m_itemOld
= tv
->itemOld
.hItem
;
2697 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2698 event
.m_item
= tv
->itemNew
.hItem
;
2699 event
.m_itemOld
= tv
->itemOld
.hItem
;
2703 // we receive this message from WM_LBUTTONDOWN handler inside
2704 // comctl32.dll and so before the click is passed to
2705 // DefWindowProc() which sets the focus to the window which was
2706 // clicked and this can lead to unexpected event sequences: for
2707 // example, we may get a "selection change" event from the tree
2708 // before getting a "kill focus" event for the text control which
2709 // had the focus previously, thus breaking user code doing input
2712 // to avoid such surprises, we force the generation of focus events
2713 // now, before we generate the selection change ones
2717 // instead of explicitly checking for _WIN32_IE, check if the
2718 // required symbols are available in the headers
2719 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2722 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2723 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2724 switch ( nmcd
.dwDrawStage
)
2727 // if we've got any items with non standard attributes,
2728 // notify us before painting each item
2729 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2733 case CDDS_ITEMPREPAINT
:
2735 wxMapTreeAttr::iterator
2736 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2738 if ( it
== m_attrs
.end() )
2740 // nothing to do for this item
2741 *result
= CDRF_DODEFAULT
;
2745 wxTreeItemAttr
* const attr
= it
->second
;
2747 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2748 TVIF_STATE
, TVIS_DROPHILITED
);
2750 const UINT tvItemState
= tvItem
.state
;
2752 // selection colours should override ours,
2753 // otherwise it is too confusing to the user
2754 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2755 !(tvItemState
& TVIS_DROPHILITED
) )
2758 if ( attr
->HasBackgroundColour() )
2760 colBack
= attr
->GetBackgroundColour();
2761 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2765 // but we still want to keep the special foreground
2766 // colour when we don't have focus (we can't keep
2767 // it when we do, it would usually be unreadable on
2768 // the almost inverted bg colour...)
2769 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2770 FindFocus() != this ) &&
2771 !(tvItemState
& TVIS_DROPHILITED
) )
2774 if ( attr
->HasTextColour() )
2776 colText
= attr
->GetTextColour();
2777 lptvcd
->clrText
= wxColourToRGB(colText
);
2781 if ( attr
->HasFont() )
2783 HFONT hFont
= GetHfontOf(attr
->GetFont());
2785 ::SelectObject(nmcd
.hdc
, hFont
);
2787 *result
= CDRF_NEWFONT
;
2789 else // no specific font
2791 *result
= CDRF_DODEFAULT
;
2797 *result
= CDRF_DODEFAULT
;
2801 // we always process it
2803 #endif // have owner drawn support in headers
2807 DWORD pos
= GetMessagePos();
2809 point
.x
= LOWORD(pos
);
2810 point
.y
= HIWORD(pos
);
2811 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2813 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2814 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2816 event
.m_item
= item
;
2817 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2825 TV_HITTESTINFO tvhti
;
2826 ::GetCursorPos(&tvhti
.pt
);
2827 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2828 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2830 if ( tvhti
.flags
& TVHT_ONITEM
)
2832 event
.m_item
= tvhti
.hItem
;
2833 eventType
= (int)hdr
->code
== NM_DBLCLK
2834 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2835 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2837 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2838 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2847 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2850 event
.SetEventType(eventType
);
2852 if ( event
.m_item
.IsOk() )
2853 event
.SetClientObject(GetItemData(event
.m_item
));
2855 bool processed
= GetEventHandler()->ProcessEvent(event
);
2858 switch ( hdr
->code
)
2861 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2862 // the return code of this event handler as the return value for
2863 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2864 // expanded status would never work
2869 // prevent tree control from sending WM_CONTEXTMENU to our parent
2870 // (which it does if NM_RCLICK is not handled) because we want to
2871 // send it to the control itself
2875 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
2876 (WPARAM
)GetHwnd(), ::GetMessagePos());
2880 case TVN_BEGINRDRAG
:
2882 if ( event
.IsAllowed() )
2884 // normally this is impossible because the m_dragImage is
2885 // deleted once the drag operation is over
2886 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2888 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2889 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2890 m_dragImage
->Show();
2892 #endif // wxUSE_DRAGIMAGE
2895 case TVN_DELETEITEM
:
2897 // NB: we might process this message using wxWidgets event
2898 // tables, but due to overhead of wxWin event system we
2899 // prefer to do it here ourself (otherwise deleting a tree
2900 // with many items is just too slow)
2901 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2903 wxTreeItemParam
*param
=
2904 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2907 processed
= true; // Make sure we don't get called twice
2911 case TVN_BEGINLABELEDIT
:
2912 // return true to cancel label editing
2913 *result
= !event
.IsAllowed();
2915 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2916 if ( event
.IsAllowed() )
2918 HWND hText
= TreeView_GetEditControl(GetHwnd());
2921 // MBN: if m_textCtrl already has an HWND, it is a stale
2922 // pointer from a previous edit (because the user
2923 // didn't modify the label before dismissing the control,
2924 // and TVN_ENDLABELEDIT was not sent), so delete it
2925 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2928 m_textCtrl
= new wxTextCtrl();
2929 m_textCtrl
->SetParent(this);
2930 m_textCtrl
->SetHWND((WXHWND
)hText
);
2931 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2933 // set wxTE_PROCESS_ENTER style for the text control to
2934 // force it to process the Enter presses itself, otherwise
2935 // they could be stolen from it by the dialog
2937 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2938 | wxTE_PROCESS_ENTER
);
2941 else // we had set m_idEdited before
2947 case TVN_ENDLABELEDIT
:
2948 // return true to set the label to the new string: note that we
2949 // also must pretend that we did process the message or it is going
2950 // to be passed to DefWindowProc() which will happily return false
2951 // cancelling the label change
2952 *result
= event
.IsAllowed();
2955 // ensure that we don't have the text ctrl which is going to be
2961 #ifdef TVN_GETINFOTIP
2962 case TVN_GETINFOTIP
:
2964 // If the user permitted a tooltip change, change it
2965 if (event
.IsAllowed())
2967 SetToolTip(event
.m_label
);
2974 case TVN_SELCHANGING
:
2975 case TVN_ITEMEXPANDING
:
2976 // return true to prevent the action from happening
2977 *result
= !event
.IsAllowed();
2980 case TVN_ITEMEXPANDED
:
2982 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2983 const wxTreeItemId
id(tv
->itemNew
.hItem
);
2985 if ( tv
->action
== TVE_COLLAPSE
)
2987 if ( wxApp::GetComCtl32Version() >= 600 )
2989 // for some reason the item selection rectangle depends
2990 // on whether it is expanded or collapsed (at least
2991 // with comctl32.dll v6): it is wider (by 3 pixels) in
2992 // the expanded state, so when the item collapses and
2993 // then is deselected the rightmost 3 pixels of the
2994 // previously drawn selection are left on the screen
2996 // it's not clear if it's a bug in comctl32.dll or in
2997 // our code (because it does not happen in Explorer but
2998 // OTOH we don't do anything which could result in this
2999 // AFAICS) but we do need to work around it to avoid
3006 // the item is also not refreshed properly after expansion when
3007 // it has an image depending on the expanded/collapsed state:
3008 // again, it's not clear if the bug is in comctl32.dll or our
3010 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3019 case TVN_GETDISPINFO
:
3020 // NB: so far the user can't set the image himself anyhow, so do it
3021 // anyway - but this may change later
3022 //if ( /* !processed && */ )
3024 wxTreeItemId item
= event
.m_item
;
3025 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3027 const wxTreeItemParam
* const param
= GetItemParam(item
);
3031 if ( info
->item
.mask
& TVIF_IMAGE
)
3036 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3037 : wxTreeItemIcon_Normal
3040 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3042 info
->item
.iSelectedImage
=
3045 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3046 : wxTreeItemIcon_Selected
3053 // for the other messages the return value is ignored and there is
3054 // nothing special to do
3059 // ----------------------------------------------------------------------------
3061 // ----------------------------------------------------------------------------
3063 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3064 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3066 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
3069 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3070 tvi
.mask
= TVIF_STATE
;
3071 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3073 // Select the specified state, or -1 == cycle to the next one.
3076 TreeView_GetItem(GetHwnd(), &tvi
);
3078 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
3079 if ( state
== m_imageListState
->GetImageCount() )
3083 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3084 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3086 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3088 TreeView_SetItem(GetHwnd(), &tvi
);
3091 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3094 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3095 tvi
.mask
= TVIF_STATE
;
3096 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3097 TreeView_GetItem(GetHwnd(), &tvi
);
3099 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3102 #endif // wxUSE_TREECTRL