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/dynarray.h"
37 #include "wx/msw/private.h"
39 // include <commctrl.h> "properly"
40 #include "wx/msw/wrapcctl.h"
42 #include "wx/msw/missing.h"
44 // Set this to 1 to be _absolutely_ sure that repainting will work for all
45 // comctl32.dll versions
46 #define wxUSE_COMCTL32_SAFELY 0
48 #include "wx/imaglist.h"
49 #include "wx/settings.h"
50 #include "wx/msw/dragimag.h"
52 // macros to hide the cast ugliness
53 // --------------------------------
55 // get HTREEITEM from wxTreeItemId
56 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
58 // the native control doesn't support multiple selections under MSW and we
59 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
60 // checkboxes be the selection status (checked == selected) or by really
61 // emulating everything, i.e. intercepting mouse and key events &c. The first
62 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
64 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // wrapper for TreeView_HitTest
71 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
77 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
80 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
82 // wrappers for TreeView_GetItem/TreeView_SetItem
83 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
87 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
88 tvi
.stateMask
= TVIS_SELECTED
;
91 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
93 wxLogLastError(wxT("TreeView_GetItem"));
96 return (tvi
.state
& TVIS_SELECTED
) != 0;
99 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
102 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
103 tvi
.stateMask
= TVIS_SELECTED
;
104 tvi
.state
= select
? TVIS_SELECTED
: 0;
107 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
109 wxLogLastError(wxT("TreeView_SetItem"));
116 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
118 SelectItem(hwndTV
, htItem
, false);
121 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
123 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
126 // helper function which selects all items in a range and, optionally,
127 // unselects all others
128 static void SelectRange(HWND hwndTV
,
131 bool unselectOthers
= true)
133 // find the first (or last) item and select it
135 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
136 while ( htItem
&& cont
)
138 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
140 if ( !IsItemSelected(hwndTV
, htItem
) )
142 SelectItem(hwndTV
, htItem
);
149 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
151 UnselectItem(hwndTV
, htItem
);
155 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
158 // select the items in range
159 cont
= htFirst
!= htLast
;
160 while ( htItem
&& cont
)
162 if ( !IsItemSelected(hwndTV
, htItem
) )
164 SelectItem(hwndTV
, htItem
);
167 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
169 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
173 if ( unselectOthers
)
177 if ( IsItemSelected(hwndTV
, htItem
) )
179 UnselectItem(hwndTV
, htItem
);
182 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
186 // seems to be necessary - otherwise the just selected items don't always
187 // appear as selected
188 UpdateWindow(hwndTV
);
191 // helper function which tricks the standard control into changing the focused
192 // item without changing anything else (if someone knows why Microsoft doesn't
193 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
194 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
197 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
202 if ( htItem
!= htFocus
)
204 // remember the selection state of the item
205 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
207 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
209 // prevent the tree from unselecting the old focus which it
210 // would do by default (TreeView_SelectItem unselects the
212 TreeView_SelectItem(hwndTV
, 0);
213 SelectItem(hwndTV
, htFocus
);
216 TreeView_SelectItem(hwndTV
, htItem
);
220 // need to clear the selection which TreeView_SelectItem() gave
222 UnselectItem(hwndTV
, htItem
);
224 //else: was selected, still selected - ok
226 //else: nothing to do, focus already there
232 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
234 // just clear the focus
235 TreeView_SelectItem(hwndTV
, 0);
237 if ( wasFocusSelected
)
239 // restore the selection state
240 SelectItem(hwndTV
, htFocus
);
243 //else: nothing to do, no focus already
247 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 // a convenient wrapper around TV_ITEM struct which adds a ctor
255 #pragma warning( disable : 4097 ) // inheriting from typedef
258 struct wxTreeViewItem
: public TV_ITEM
260 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
261 UINT mask_
, // fields which are valid
262 UINT stateMask_
= 0) // for TVIF_STATE only
266 // hItem member is always valid
267 mask
= mask_
| TVIF_HANDLE
;
268 stateMask
= stateMask_
;
273 // ----------------------------------------------------------------------------
274 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
276 // We need this for a couple of reasons:
278 // 1) This class is needed for support of different images: the Win32 common
279 // control natively supports only 2 images (the normal one and another for the
280 // selected state). We wish to provide support for 2 more of them for folder
281 // items (i.e. those which have children): for expanded state and for expanded
282 // selected state. For this we use this structure to store the additional items
285 // 2) This class is also needed to hold the HITEM so that we can sort
286 // it correctly in the MSW sort callback.
288 // In addition it makes other workarounds such as this easier and helps
289 // simplify the code.
290 // ----------------------------------------------------------------------------
292 class wxTreeItemParam
299 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
305 // dtor deletes the associated data as well
306 virtual ~wxTreeItemParam() { delete m_data
; }
309 // get the real data associated with the item
310 wxTreeItemData
*GetData() const { return m_data
; }
312 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
314 // do we have such image?
315 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
317 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
319 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
322 const wxTreeItemId
& GetItem() const { return m_item
; }
324 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
327 // all the images associated with the item
328 int m_images
[wxTreeItemIcon_Max
];
330 // item for sort callbacks
333 // the real client data
334 wxTreeItemData
*m_data
;
336 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
339 // wxVirutalNode is used in place of a single root when 'hidden' root is
341 class wxVirtualNode
: public wxTreeViewItem
344 wxVirtualNode(wxTreeItemParam
*param
)
345 : wxTreeViewItem(TVI_ROOT
, 0)
355 wxTreeItemParam
*GetParam() const { return m_param
; }
356 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
359 wxTreeItemParam
*m_param
;
361 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
365 #pragma warning( default : 4097 )
368 // a macro to get the virtual root, returns NULL if none
369 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
371 // returns true if the item is the virtual root
372 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
374 // a class which encapsulates the tree traversal logic: it vists all (unless
375 // OnVisit() returns false) items under the given one
376 class wxTreeTraversal
379 wxTreeTraversal(const wxTreeCtrl
*tree
)
384 // give it a virtual dtor: not really needed as the class is never used
385 // polymorphically and not even allocated on heap at all, but this is safer
386 // (in case it ever is) and silences the compiler warnings for now
387 virtual ~wxTreeTraversal() { }
389 // do traverse the tree: visit all items (recursively by default) under the
390 // given one; return true if all items were traversed or false if the
391 // traversal was aborted because OnVisit returned false
392 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
394 // override this function to do whatever is needed for each item, return
395 // false to stop traversing
396 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
399 const wxTreeCtrl
*GetTree() const { return m_tree
; }
402 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
404 const wxTreeCtrl
*m_tree
;
406 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
409 // internal class for getting the selected items
410 class TraverseSelections
: public wxTreeTraversal
413 TraverseSelections(const wxTreeCtrl
*tree
,
414 wxArrayTreeItemIds
& selections
)
415 : wxTreeTraversal(tree
), m_selections(selections
)
417 m_selections
.Empty();
419 if (tree
->GetCount() > 0)
420 DoTraverse(tree
->GetRootItem());
423 virtual bool OnVisit(const wxTreeItemId
& item
)
425 // can't visit a virtual node.
426 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
431 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
432 if ( GetTree()->IsItemChecked(item
) )
434 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
437 m_selections
.Add(item
);
443 size_t GetCount() const { return m_selections
.GetCount(); }
446 wxArrayTreeItemIds
& m_selections
;
448 DECLARE_NO_COPY_CLASS(TraverseSelections
)
451 // internal class for counting tree items
452 class TraverseCounter
: public wxTreeTraversal
455 TraverseCounter(const wxTreeCtrl
*tree
,
456 const wxTreeItemId
& root
,
458 : wxTreeTraversal(tree
)
462 DoTraverse(root
, recursively
);
465 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
472 size_t GetCount() const { return m_count
; }
477 DECLARE_NO_COPY_CLASS(TraverseCounter
)
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 #if wxUSE_EXTENDED_RTTI
485 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
487 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
488 // new style border flags, we put them first to
489 // use them for streaming out
490 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
491 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
492 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
493 wxFLAGS_MEMBER(wxBORDER_RAISED
)
494 wxFLAGS_MEMBER(wxBORDER_STATIC
)
495 wxFLAGS_MEMBER(wxBORDER_NONE
)
497 // old style border flags
498 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
499 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
500 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
501 wxFLAGS_MEMBER(wxRAISED_BORDER
)
502 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
503 wxFLAGS_MEMBER(wxBORDER
)
505 // standard window styles
506 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
507 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
508 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
509 wxFLAGS_MEMBER(wxWANTS_CHARS
)
510 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
511 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
512 wxFLAGS_MEMBER(wxVSCROLL
)
513 wxFLAGS_MEMBER(wxHSCROLL
)
515 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
516 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
517 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
518 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
519 wxFLAGS_MEMBER(wxTR_NO_LINES
)
520 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
521 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
522 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
523 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
524 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
525 wxFLAGS_MEMBER(wxTR_SINGLE
)
526 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
527 wxFLAGS_MEMBER(wxTR_EXTENDED
)
528 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
530 wxEND_FLAGS( wxTreeCtrlStyle
)
532 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
534 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
535 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
536 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
537 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
538 wxEND_PROPERTIES_TABLE()
540 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
541 wxEND_HANDLERS_TABLE()
543 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
545 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
552 // indices in gs_expandEvents table below
567 // handy table for sending events - it has to be initialized during run-time
568 // now so can't be const any more
569 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
572 but logically it's a const table with the following entries:
575 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
576 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
580 // ============================================================================
582 // ============================================================================
584 // ----------------------------------------------------------------------------
586 // ----------------------------------------------------------------------------
588 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
590 if ( !OnVisit(root
) )
593 return Traverse(root
, recursively
);
596 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
598 wxTreeItemIdValue cookie
;
599 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
600 while ( child
.IsOk() )
602 // depth first traversal
603 if ( recursively
&& !Traverse(child
, true) )
606 if ( !OnVisit(child
) )
609 child
= m_tree
->GetNextChild(root
, cookie
);
615 // ----------------------------------------------------------------------------
616 // construction and destruction
617 // ----------------------------------------------------------------------------
619 void wxTreeCtrl::Init()
622 m_hasAnyAttr
= false;
624 m_pVirtualRoot
= NULL
;
626 // initialize the global array of events now as it can't be done statically
627 // with the wxEVT_XXX values being allocated during run-time only
628 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
629 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
630 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
631 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
634 bool wxTreeCtrl::Create(wxWindow
*parent
,
639 const wxValidator
& validator
,
640 const wxString
& name
)
644 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
645 style
|= wxBORDER_SUNKEN
;
647 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
651 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
652 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
654 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
655 wstyle
|= TVS_HASLINES
;
656 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
657 wstyle
|= TVS_HASBUTTONS
;
659 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
660 wstyle
|= TVS_EDITLABELS
;
662 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
663 wstyle
|= TVS_LINESATROOT
;
665 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
667 if ( wxApp::GetComCtl32Version() >= 471 )
668 wstyle
|= TVS_FULLROWSELECT
;
671 // using TVS_CHECKBOXES for emulation of a multiselection tree control
672 // doesn't work without the new enough headers
673 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
674 !defined( __GNUWIN32_OLD__ ) && \
675 !defined( __BORLANDC__ ) && \
676 !defined( __WATCOMC__ ) && \
677 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
679 // we emulate the multiple selection tree controls by using checkboxes: set
680 // up the image list we need for this if we do have multiple selections
681 if ( m_windowStyle
& wxTR_MULTIPLE
)
682 wstyle
|= TVS_CHECKBOXES
;
683 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
685 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
686 // Need so that TVN_GETINFOTIP messages will be sent
687 wstyle
|= TVS_INFOTIP
;
690 // Create the tree control.
691 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
694 #if wxUSE_COMCTL32_SAFELY
695 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
696 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
698 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
699 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
701 // This works around a bug in the Windows tree control whereby for some versions
702 // of comctrl32, setting any colour actually draws the background in black.
703 // This will initialise the background to the system colour.
704 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
705 // Assume the user has an updated comctl32.dll.
706 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
707 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
708 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
712 // VZ: this is some experimental code which may be used to get the
713 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
714 // AFAIK, the standard DLL does about the same thing anyhow.
716 if ( m_windowStyle
& wxTR_MULTIPLE
)
720 // create the DC compatible with the current screen
721 HDC hdcMem
= CreateCompatibleDC(NULL
);
723 // create a mono bitmap of the standard size
724 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
725 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
726 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
727 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
728 1, // # of color planes
729 1, // # bits needed for one pixel
730 0); // array containing colour data
731 SelectObject(hdcMem
, hbmpCheck
);
733 // then draw a check mark into it
734 RECT rect
= { 0, 0, x
, y
};
735 if ( !::DrawFrameControl(hdcMem
, &rect
,
737 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
739 wxLogLastError(wxT("DrawFrameControl(check)"));
742 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
743 imagelistCheckboxes
.Add(bmp
);
745 if ( !::DrawFrameControl(hdcMem
, &rect
,
749 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
752 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
753 imagelistCheckboxes
.Add(bmp
);
759 SetStateImageList(&imagelistCheckboxes
);
763 wxSetCCUnicodeFormat(GetHwnd());
768 wxTreeCtrl::~wxTreeCtrl()
770 // delete any attributes
773 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
775 // prevent TVN_DELETEITEM handler from deleting the attributes again!
776 m_hasAnyAttr
= false;
781 // delete user data to prevent memory leaks
782 // also deletes hidden root node storage.
786 // ----------------------------------------------------------------------------
788 // ----------------------------------------------------------------------------
790 /* static */ wxVisualAttributes
791 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
793 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
795 // common controls have their own default font
796 attrs
.font
= wxGetCCDefaultFont();
802 // simple wrappers which add error checking in debug mode
804 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
806 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
807 _T("can't retrieve virtual root item") );
809 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
811 wxLogLastError(wxT("TreeView_GetItem"));
819 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
821 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
823 wxLogLastError(wxT("TreeView_SetItem"));
827 unsigned int wxTreeCtrl::GetCount() const
829 return (unsigned int)TreeView_GetCount(GetHwnd());
832 unsigned int wxTreeCtrl::GetIndent() const
834 return TreeView_GetIndent(GetHwnd());
837 void wxTreeCtrl::SetIndent(unsigned int indent
)
839 TreeView_SetIndent(GetHwnd(), indent
);
842 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
845 (void) TreeView_SetImageList(GetHwnd(),
846 imageList
? imageList
->GetHIMAGELIST() : 0,
850 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
852 if (m_ownsImageListNormal
)
853 delete m_imageListNormal
;
855 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
856 m_ownsImageListNormal
= false;
859 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
861 if (m_ownsImageListState
) delete m_imageListState
;
862 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
863 m_ownsImageListState
= false;
866 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
867 bool recursively
) const
869 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
871 TraverseCounter
counter(this, item
, recursively
);
872 return counter
.GetCount() - 1;
875 // ----------------------------------------------------------------------------
877 // ----------------------------------------------------------------------------
879 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
881 #if !wxUSE_COMCTL32_SAFELY
882 if ( !wxWindowBase::SetBackgroundColour(colour
) )
885 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
891 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
893 #if !wxUSE_COMCTL32_SAFELY
894 if ( !wxWindowBase::SetForegroundColour(colour
) )
897 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
903 // ----------------------------------------------------------------------------
905 // ----------------------------------------------------------------------------
907 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
909 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
911 wxChar buf
[512]; // the size is arbitrary...
913 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
914 tvItem
.pszText
= buf
;
915 tvItem
.cchTextMax
= WXSIZEOF(buf
);
916 if ( !DoGetItem(&tvItem
) )
918 // don't return some garbage which was on stack, but an empty string
922 return wxString(buf
);
925 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
927 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
929 if ( IS_VIRTUAL_ROOT(item
) )
932 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
933 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
936 // when setting the text of the item being edited, the text control should
937 // be updated to reflect the new text as well, otherwise calling
938 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
940 // don't use GetEditControl() here because m_textCtrl is not set yet
941 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
944 if ( item
== m_idEdited
)
946 ::SetWindowText(hwndEdit
, text
);
951 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
952 wxTreeItemIcon which
) const
954 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
956 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
958 // no images for hidden root item
962 wxTreeItemParam
*param
= GetItemParam(item
);
964 return param
? param
->GetImage(which
) : -1;
967 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
968 wxTreeItemIcon which
)
970 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
971 wxCHECK_RET( which
>= 0 &&
972 which
< wxTreeItemIcon_Max
,
973 wxT("invalid image index"));
976 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
978 // no images for hidden root item
982 wxTreeItemParam
*data
= GetItemParam(item
);
986 data
->SetImage(image
, which
);
988 // make sure that we have selected images as well
989 if ( which
== wxTreeItemIcon_Normal
&&
990 !data
->HasImage(wxTreeItemIcon_Selected
) )
992 data
->SetImage(image
, wxTreeItemIcon_Selected
);
995 if ( which
== wxTreeItemIcon_Expanded
&&
996 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
998 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
1002 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1004 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1006 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1008 // hidden root may still have data.
1009 if ( IS_VIRTUAL_ROOT(item
) )
1011 return GET_VIRTUAL_ROOT()->GetParam();
1015 if ( !DoGetItem(&tvItem
) )
1020 return (wxTreeItemParam
*)tvItem
.lParam
;
1023 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1025 wxTreeItemParam
*data
= GetItemParam(item
);
1027 return data
? data
->GetData() : NULL
;
1030 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1032 // first, associate this piece of data with this item
1038 wxTreeItemParam
*param
= GetItemParam(item
);
1040 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1042 param
->SetData(data
);
1045 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1047 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1049 if ( IS_VIRTUAL_ROOT(item
) )
1052 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1053 tvItem
.cChildren
= (int)has
;
1057 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1059 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1061 if ( IS_VIRTUAL_ROOT(item
) )
1064 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1065 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1069 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1071 if ( IS_VIRTUAL_ROOT(item
) )
1074 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1075 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1079 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1081 if ( IS_VIRTUAL_ROOT(item
) )
1085 if ( GetBoundingRect(item
, rect
) )
1091 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1093 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1095 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1096 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1099 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1101 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1103 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1104 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1107 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1109 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1111 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1112 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1115 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1116 const wxColour
& col
)
1118 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1120 wxTreeItemAttr
*attr
;
1121 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1122 if ( it
== m_attrs
.end() )
1124 m_hasAnyAttr
= true;
1126 m_attrs
[item
.m_pItem
] =
1127 attr
= new wxTreeItemAttr
;
1134 attr
->SetTextColour(col
);
1139 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1140 const wxColour
& col
)
1142 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1144 wxTreeItemAttr
*attr
;
1145 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1146 if ( it
== m_attrs
.end() )
1148 m_hasAnyAttr
= true;
1150 m_attrs
[item
.m_pItem
] =
1151 attr
= new wxTreeItemAttr
;
1153 else // already in the hash
1158 attr
->SetBackgroundColour(col
);
1163 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1165 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1167 wxTreeItemAttr
*attr
;
1168 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1169 if ( it
== m_attrs
.end() )
1171 m_hasAnyAttr
= true;
1173 m_attrs
[item
.m_pItem
] =
1174 attr
= new wxTreeItemAttr
;
1176 else // already in the hash
1181 attr
->SetFont(font
);
1186 // ----------------------------------------------------------------------------
1188 // ----------------------------------------------------------------------------
1190 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1192 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1194 if ( item
== wxTreeItemId(TVI_ROOT
) )
1196 // virtual (hidden) root is never visible
1200 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1203 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1204 // the HTREEITEM with TVM_GETITEMRECT
1205 *(HTREEITEM
*)&rect
= HITEM(item
);
1207 // true means to get rect for just the text, not the whole line
1208 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1210 // if TVM_GETITEMRECT returned false, then the item is definitely not
1211 // visible (because its parent is not expanded)
1215 // however if it returned true, the item might still be outside the
1216 // currently visible part of the tree, test for it (notice that partly
1217 // visible means visible here)
1218 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1221 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1223 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1225 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1228 return tvItem
.cChildren
!= 0;
1231 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1233 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1235 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1238 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1241 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1243 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1245 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1248 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1251 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1253 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1255 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1258 return (tvItem
.state
& TVIS_BOLD
) != 0;
1261 // ----------------------------------------------------------------------------
1263 // ----------------------------------------------------------------------------
1265 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1267 // Root may be real (visible) or virtual (hidden).
1268 if ( GET_VIRTUAL_ROOT() )
1271 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1274 wxTreeItemId
wxTreeCtrl::GetSelection() const
1276 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1277 wxT("this only works with single selection controls") );
1279 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1282 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1284 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1288 if ( IS_VIRTUAL_ROOT(item
) )
1290 // no parent for the virtual root
1295 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1296 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1298 // the top level items should have the virtual root as their parent
1303 return wxTreeItemId(hItem
);
1306 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1307 wxTreeItemIdValue
& cookie
) const
1309 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1311 // remember the last child returned in 'cookie'
1312 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1314 return wxTreeItemId(cookie
);
1317 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1318 wxTreeItemIdValue
& cookie
) const
1320 wxTreeItemId
fromCookie(cookie
);
1322 HTREEITEM hitem
= HITEM(fromCookie
);
1324 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1326 wxTreeItemId
item(hitem
);
1328 cookie
= item
.m_pItem
;
1333 #if WXWIN_COMPATIBILITY_2_4
1335 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1338 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1340 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1342 return wxTreeItemId((void *)cookie
);
1345 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1348 wxTreeItemId
fromCookie((void *)cookie
);
1350 HTREEITEM hitem
= HITEM(fromCookie
);
1352 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1354 wxTreeItemId
item(hitem
);
1356 cookie
= (long)item
.m_pItem
;
1361 #endif // WXWIN_COMPATIBILITY_2_4
1363 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1365 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1367 // can this be done more efficiently?
1368 wxTreeItemIdValue cookie
;
1370 wxTreeItemId childLast
,
1371 child
= GetFirstChild(item
, cookie
);
1372 while ( child
.IsOk() )
1375 child
= GetNextChild(item
, cookie
);
1381 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1383 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1384 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1387 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1389 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1390 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1393 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1395 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1398 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1400 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1401 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1403 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1406 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1408 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1409 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1411 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1414 // ----------------------------------------------------------------------------
1415 // multiple selections emulation
1416 // ----------------------------------------------------------------------------
1418 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1420 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1422 // receive the desired information.
1423 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1426 // state image indices are 1 based
1427 return ((tvItem
.state
>> 12) - 1) == 1;
1430 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1432 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1434 // receive the desired information.
1435 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1439 // state images are one-based
1440 tvItem
.state
= (check
? 2 : 1) << 12;
1445 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1447 TraverseSelections
selector(this, selections
);
1449 return selector
.GetCount();
1452 // ----------------------------------------------------------------------------
1454 // ----------------------------------------------------------------------------
1456 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1457 const wxTreeItemId
& hInsertAfter
,
1458 const wxString
& text
,
1459 int image
, int selectedImage
,
1460 wxTreeItemData
*data
)
1462 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1464 _T("can't have more than one root in the tree") );
1466 TV_INSERTSTRUCT tvIns
;
1467 tvIns
.hParent
= HITEM(parent
);
1468 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1470 // this is how we insert the item as the first child: supply a NULL
1472 if ( !tvIns
.hInsertAfter
)
1474 tvIns
.hInsertAfter
= TVI_FIRST
;
1478 if ( !text
.empty() )
1481 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1485 tvIns
.item
.pszText
= NULL
;
1486 tvIns
.item
.cchTextMax
= 0;
1489 // we use the wxTreeItemParam of the LPARAM to return the image
1490 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1491 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1492 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1494 // create the param and setup the initial image numbers
1495 wxTreeItemParam
*param
= new wxTreeItemParam
;
1497 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1499 // take the same image for selected icon if not specified
1500 if ( selectedImage
== -1 )
1501 param
->SetImage(image
, wxTreeItemIcon_Selected
);
1503 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1506 tvIns
.item
.lParam
= (LPARAM
)param
;
1507 tvIns
.item
.mask
= mask
;
1509 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1512 wxLogLastError(wxT("TreeView_InsertItem"));
1515 // associate the application tree item with Win32 tree item handle
1518 // setup wxTreeItemData
1521 param
->SetData(data
);
1525 return wxTreeItemId(id
);
1528 // for compatibility only
1529 #if WXWIN_COMPATIBILITY_2_4
1531 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1533 SetImageList(imageList
);
1536 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1538 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1541 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1543 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1546 #endif // WXWIN_COMPATIBILITY_2_4
1548 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1549 int image
, int selectedImage
,
1550 wxTreeItemData
*data
)
1553 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1555 // create a virtual root item, the parent for all the others
1556 wxTreeItemParam
*param
= new wxTreeItemParam
;
1557 param
->SetData(data
);
1559 m_pVirtualRoot
= new wxVirtualNode(param
);
1564 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1565 text
, image
, selectedImage
, data
);
1568 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1570 const wxString
& text
,
1571 int image
, int selectedImage
,
1572 wxTreeItemData
*data
)
1574 wxTreeItemId idPrev
;
1575 if ( index
== (size_t)-1 )
1577 // special value: append to the end
1580 else // find the item from index
1582 wxTreeItemIdValue cookie
;
1583 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1584 while ( index
!= 0 && idCur
.IsOk() )
1589 idCur
= GetNextChild(parent
, cookie
);
1592 // assert, not check: if the index is invalid, we will append the item
1594 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1597 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1600 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1602 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1604 wxLogLastError(wxT("TreeView_DeleteItem"));
1608 // delete all children (but don't delete the item itself)
1609 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1611 wxTreeItemIdValue cookie
;
1613 wxArrayTreeItemIds children
;
1614 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1615 while ( child
.IsOk() )
1617 children
.Add(child
);
1619 child
= GetNextChild(item
, cookie
);
1622 size_t nCount
= children
.Count();
1623 for ( size_t n
= 0; n
< nCount
; n
++ )
1625 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1627 wxLogLastError(wxT("TreeView_DeleteItem"));
1632 void wxTreeCtrl::DeleteAllItems()
1634 // delete the "virtual" root item.
1635 if ( GET_VIRTUAL_ROOT() )
1637 delete GET_VIRTUAL_ROOT();
1638 m_pVirtualRoot
= NULL
;
1641 // and all the real items
1643 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1645 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1649 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1651 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1652 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1653 flag
== TVE_EXPAND
||
1655 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1657 // A hidden root can be neither expanded nor collapsed.
1658 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1659 wxT("Can't expand/collapse hidden root node!") );
1661 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1662 // emulate them. This behaviour has changed slightly with comctl32.dll
1663 // v 4.70 - now it does send them but only the first time. To maintain
1664 // compatible behaviour and also in order to not have surprises with the
1665 // future versions, don't rely on this and still do everything ourselves.
1666 // To avoid that the messages be sent twice when the item is expanded for
1667 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1669 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1673 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1675 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1676 event
.m_item
= item
;
1677 event
.SetEventObject(this);
1679 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1681 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1685 (void)GetEventHandler()->ProcessEvent(event
);
1687 //else: change didn't took place, so do nothing at all
1690 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1692 DoExpand(item
, TVE_EXPAND
);
1695 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1697 DoExpand(item
, TVE_COLLAPSE
);
1700 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1702 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1705 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1707 DoExpand(item
, TVE_TOGGLE
);
1710 #if WXWIN_COMPATIBILITY_2_4
1712 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1714 DoExpand(item
, action
);
1719 void wxTreeCtrl::Unselect()
1721 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1722 wxT("doesn't make sense, may be you want UnselectAll()?") );
1724 // just remove the selection
1725 SelectItem(wxTreeItemId());
1728 void wxTreeCtrl::UnselectAll()
1730 if ( m_windowStyle
& wxTR_MULTIPLE
)
1732 wxArrayTreeItemIds selections
;
1733 size_t count
= GetSelections(selections
);
1734 for ( size_t n
= 0; n
< count
; n
++ )
1736 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1737 SetItemCheck(HITEM(selections
[n
]), false);
1738 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1739 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1740 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1743 m_htSelStart
.Unset();
1747 // just remove the selection
1752 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1754 if ( m_windowStyle
& wxTR_MULTIPLE
)
1756 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1757 // selecting the item means checking it
1758 SetItemCheck(item
, select
);
1759 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1760 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1761 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1765 wxASSERT_MSG( select
,
1766 _T("SelectItem(false) works only for multiselect") );
1768 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1769 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1770 // send them ourselves
1772 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1773 event
.m_item
= item
;
1774 event
.SetEventObject(this);
1776 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1777 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1779 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1781 wxLogLastError(wxT("TreeView_SelectItem"));
1785 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1786 (void)GetEventHandler()->ProcessEvent(event
);
1789 //else: program vetoed the change
1793 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1796 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1799 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1801 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1803 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1807 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1812 void wxTreeCtrl::DeleteTextCtrl()
1816 // the HWND corresponding to this control is deleted by the tree
1817 // control itself and we don't know when exactly this happens, so check
1818 // if the window still exists before calling UnsubclassWin()
1819 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1821 m_textCtrl
->SetHWND(0);
1824 m_textCtrl
->UnsubclassWin();
1825 m_textCtrl
->SetHWND(0);
1833 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1834 wxClassInfo
*textControlClass
)
1836 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1841 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1842 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1844 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1853 // textctrl is subclassed in MSWOnNotify
1857 // End label editing, optionally cancelling the edit
1858 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1860 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1865 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
)
1867 TV_HITTESTINFO hitTestInfo
;
1868 hitTestInfo
.pt
.x
= (int)point
.x
;
1869 hitTestInfo
.pt
.y
= (int)point
.y
;
1871 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1876 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1877 flags |= wxTREE_HITTEST_##flag
1879 TRANSLATE_FLAG(ABOVE
);
1880 TRANSLATE_FLAG(BELOW
);
1881 TRANSLATE_FLAG(NOWHERE
);
1882 TRANSLATE_FLAG(ONITEMBUTTON
);
1883 TRANSLATE_FLAG(ONITEMICON
);
1884 TRANSLATE_FLAG(ONITEMINDENT
);
1885 TRANSLATE_FLAG(ONITEMLABEL
);
1886 TRANSLATE_FLAG(ONITEMRIGHT
);
1887 TRANSLATE_FLAG(ONITEMSTATEICON
);
1888 TRANSLATE_FLAG(TOLEFT
);
1889 TRANSLATE_FLAG(TORIGHT
);
1891 #undef TRANSLATE_FLAG
1893 return wxTreeItemId(hitTestInfo
.hItem
);
1896 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1898 bool textOnly
) const
1902 // Virtual root items have no bounding rectangle
1903 if ( IS_VIRTUAL_ROOT(item
) )
1908 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1911 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1917 // couldn't retrieve rect: for example, item isn't visible
1922 // ----------------------------------------------------------------------------
1924 // ----------------------------------------------------------------------------
1926 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1927 // functions such as IsDataIndirect()
1928 class wxTreeSortHelper
1931 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1934 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1936 return ((wxTreeItemParam
*)lParam
)->GetItem();
1940 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1944 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1945 wxT("sorting tree without data doesn't make sense") );
1947 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1949 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1950 GetIdFromData(pItem2
));
1953 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1955 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1957 // rely on the fact that TreeView_SortChildren does the same thing as our
1958 // default behaviour, i.e. sorts items alphabetically and so call it
1959 // directly if we're not in derived class (much more efficient!)
1960 // RN: Note that if you find you're code doesn't sort as expected this
1961 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1962 // combo for your derived wxTreeCtrl if will sort without
1964 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1966 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1971 tvSort
.hParent
= HITEM(item
);
1972 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1973 tvSort
.lParam
= (LPARAM
)this;
1974 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1978 // ----------------------------------------------------------------------------
1980 // ----------------------------------------------------------------------------
1982 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1984 if ( cmd
== EN_UPDATE
)
1986 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1987 event
.SetEventObject( this );
1988 ProcessCommand(event
);
1990 else if ( cmd
== EN_KILLFOCUS
)
1992 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1993 event
.SetEventObject( this );
1994 ProcessCommand(event
);
2002 // command processed
2006 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2007 // only do it during dragging, minimize wxWin overhead (this is important for
2008 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2009 // instead of passing by wxWin events
2010 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2012 bool processed
= false;
2014 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2016 // This message is sent after a right-click, or when the "menu" key is pressed
2017 if ( nMsg
== WM_CONTEXTMENU
)
2019 int x
= GET_X_LPARAM(lParam
),
2020 y
= GET_Y_LPARAM(lParam
);
2021 // Convert the screen point to a client point
2022 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2024 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2026 // can't use GetSelection() here as it would assert in multiselect mode
2027 event
.m_item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2028 event
.SetEventObject( this );
2030 // Get the bounding rectangle for the item, including the non-text areas
2032 GetBoundingRect(event
.m_item
, ItemRect
, false);
2033 // If the point is inside the bounding rectangle, use it as the click position.
2034 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2035 if (ItemRect
.Inside(MenuPoint
))
2037 event
.m_pointDrag
= MenuPoint
;
2039 // Use the Explorer standard of putting the menu at the left edge of the text,
2040 // in the vertical middle of the text. Should be the case for the "menu" key
2043 // Use the bounding rectangle of only the text part
2044 GetBoundingRect(event
.m_item
, ItemRect
, true);
2045 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2048 if ( GetEventHandler()->ProcessEvent(event
) )
2050 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2052 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2054 // we only process mouse messages here and these parameters have the
2055 // same meaning for all of them
2056 int x
= GET_X_LPARAM(lParam
),
2057 y
= GET_Y_LPARAM(lParam
);
2058 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2060 TV_HITTESTINFO tvht
;
2064 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2068 case WM_RBUTTONDOWN
:
2069 // if the item we are about to right click on is not already
2070 // selected or if we click outside of any item, remove the
2071 // entire previous selection
2072 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2077 // select item and set the focus to the
2078 // newly selected item
2079 ::SelectItem(GetHwnd(), htItem
);
2080 ::SetFocus(GetHwnd(), htItem
);
2083 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2084 case WM_LBUTTONDOWN
:
2085 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2087 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2088 m_ptClick
= wxPoint(x
, y
);
2090 if ( wParam
& MK_CONTROL
)
2094 // toggle selected state
2095 ::ToggleItemSelection(GetHwnd(), htItem
);
2097 ::SetFocus(GetHwnd(), htItem
);
2099 // reset on any click without Shift
2100 m_htSelStart
.Unset();
2104 else if ( wParam
& MK_SHIFT
)
2106 // this selects all items between the starting one and
2109 if ( !m_htSelStart
)
2111 // take the focused item
2112 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2116 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2117 !(wParam
& MK_CONTROL
));
2119 ::SelectItem(GetHwnd(), htItem
);
2121 ::SetFocus(GetHwnd(), htItem
);
2125 else // normal click
2127 // avoid doing anything if we click on the only
2128 // currently selected item
2132 wxArrayTreeItemIds selections
;
2133 size_t count
= GetSelections(selections
);
2136 HITEM(selections
[0]) != htItem
)
2138 // clear the previously selected items, if the
2139 // user clicked outside of the present selection.
2140 // otherwise, perform the deselection on mouse-up.
2141 // this allows multiple drag and drop to work.
2143 if (!IsItemSelected(GetHwnd(), htItem
))
2147 // prevent the click from starting in-place editing
2148 // which should only happen if we click on the
2149 // already selected item (and nothing else is
2152 TreeView_SelectItem(GetHwnd(), 0);
2153 ::SelectItem(GetHwnd(), htItem
);
2155 ::SetFocus(GetHwnd(), htItem
);
2159 // reset on any click without Shift
2160 m_htSelStart
.Unset();
2164 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2168 if ( m_htClickedItem
)
2170 int cx
= abs(m_ptClick
.x
- x
);
2171 int cy
= abs(m_ptClick
.y
- y
);
2173 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2175 HWND pWnd
= ::GetParent( GetHwnd() );
2180 tv
.hdr
.hwndFrom
= GetHwnd();
2181 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2182 tv
.hdr
.code
= TVN_BEGINDRAG
;
2184 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2187 ZeroMemory(&tviAux
, sizeof(tviAux
));
2188 tviAux
.hItem
= HITEM(m_htClickedItem
);
2189 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2190 tviAux
.stateMask
= 0xffffffff;
2191 TreeView_GetItem( GetHwnd(), &tviAux
);
2193 tv
.itemNew
.state
= tviAux
.state
;
2194 tv
.itemNew
.lParam
= tviAux
.lParam
;
2199 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2201 m_htClickedItem
.Unset();
2204 #endif // __WXWINCE__
2208 m_dragImage
->Move(wxPoint(x
, y
));
2211 // highlight the item as target (hiding drag image is
2212 // necessary - otherwise the display will be corrupted)
2213 m_dragImage
->Hide();
2214 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2215 m_dragImage
->Show();
2222 // facilitates multiple drag-and-drop
2223 if (htItem
&& isMultiple
)
2225 wxArrayTreeItemIds selections
;
2226 size_t count
= GetSelections(selections
);
2229 !(wParam
& MK_CONTROL
) &&
2230 !(wParam
& MK_SHIFT
))
2233 TreeView_SelectItem(GetHwnd(), htItem
);
2234 ::SelectItem(GetHwnd(), htItem
);
2235 ::SetFocus(GetHwnd(), htItem
);
2237 m_htClickedItem
.Unset();
2245 m_dragImage
->EndDrag();
2249 // generate the drag end event
2250 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2252 event
.m_item
= htItem
;
2253 event
.m_pointDrag
= wxPoint(x
, y
);
2254 event
.SetEventObject(this);
2256 (void)GetEventHandler()->ProcessEvent(event
);
2258 // if we don't do it, the tree seems to think that 2 items
2259 // are selected simultaneously which is quite weird
2260 TreeView_SelectDropTarget(GetHwnd(), 0);
2265 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2266 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2268 // the tree control greys out the selected item when it loses focus and
2269 // paints it as selected again when it regains it, but it won't do it
2270 // for the other items itself - help it
2271 wxArrayTreeItemIds selections
;
2272 size_t count
= GetSelections(selections
);
2274 for ( size_t n
= 0; n
< count
; n
++ )
2276 // TreeView_GetItemRect() will return false if item is not visible,
2277 // which may happen perfectly well
2278 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2281 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2285 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2287 bool bCtrl
= wxIsCtrlDown(),
2288 bShift
= wxIsShiftDown();
2290 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2296 ::ToggleItemSelection(GetHwnd(), htSel
);
2302 ::SelectItem(GetHwnd(), htSel
);
2310 if ( !bCtrl
&& !bShift
)
2312 // no modifiers, just clear selection and then let the default
2313 // processing to take place
2318 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2320 HTREEITEM htNext
= (HTREEITEM
)
2321 TreeView_GetNextItem
2325 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2331 // at the top/bottom
2337 if ( !m_htSelStart
)
2338 m_htSelStart
= htSel
;
2340 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2344 // without changing selection
2345 ::SetFocus(GetHwnd(), htNext
);
2356 // TODO: handle Shift/Ctrl with these keys
2357 if ( !bCtrl
&& !bShift
)
2361 m_htSelStart
.Unset();
2365 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2366 else if ( nMsg
== WM_COMMAND
)
2368 // if we receive a EN_KILLFOCUS command from the in-place edit control
2369 // used for label editing, make sure to end editing
2372 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2374 if ( cmd
== EN_KILLFOCUS
)
2376 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2386 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2392 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2394 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2395 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2396 // the parent window, not us, which completely breaks everything so simply
2397 // don't let it see this message at all
2398 if ( nMsg
== WM_RBUTTONDOWN
)
2401 // but because of the above we don't get NM_RCLICK which is normally
2402 // generated by tree window proc when the modal loop mentioned above ends
2403 // because the mouse is released -- synthesize it ourselves instead
2404 if ( nMsg
== WM_RBUTTONUP
)
2407 hdr
.hwndFrom
= GetHwnd();
2408 hdr
.idFrom
= GetId();
2409 hdr
.code
= NM_RCLICK
;
2412 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2414 // continue as usual
2417 if ( nMsg
== WM_CHAR
)
2419 // also don't let the control process Space and Return keys because it
2420 // doesn't do anything useful with them anyhow but always beeps
2421 // annoyingly when it receives them and there is no way to turn it off
2422 // simply if you just process TREEITEM_ACTIVATED event to which Space
2423 // and Enter presses are mapped in your code
2424 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2428 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2431 // process WM_NOTIFY Windows message
2432 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2434 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2435 wxEventType eventType
= wxEVT_NULL
;
2436 NMHDR
*hdr
= (NMHDR
*)lParam
;
2438 switch ( hdr
->code
)
2441 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2444 case TVN_BEGINRDRAG
:
2446 if ( eventType
== wxEVT_NULL
)
2447 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2448 //else: left drag, already set above
2450 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2452 event
.m_item
= tv
->itemNew
.hItem
;
2453 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2455 // don't allow dragging by default: the user code must
2456 // explicitly say that it wants to allow it to avoid breaking
2462 case TVN_BEGINLABELEDIT
:
2464 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2465 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2467 // although the user event handler may still veto it, it is
2468 // important to set it now so that calls to SetItemText() from
2469 // the event handler would change the text controls contents
2471 event
.m_item
= info
->item
.hItem
;
2472 event
.m_label
= info
->item
.pszText
;
2473 event
.m_editCancelled
= false;
2477 case TVN_DELETEITEM
:
2479 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2480 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2482 event
.m_item
= tv
->itemOld
.hItem
;
2486 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2487 if ( it
!= m_attrs
.end() )
2496 case TVN_ENDLABELEDIT
:
2498 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2499 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2501 event
.m_item
= info
->item
.hItem
;
2502 event
.m_label
= info
->item
.pszText
;
2503 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2508 // These *must* not be removed or TVN_GETINFOTIP will
2509 // not be processed each time the mouse is moved
2510 // and the tooltip will only ever update once.
2519 #ifdef TVN_GETINFOTIP
2520 case TVN_GETINFOTIP
:
2522 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2523 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2525 // Which item are we trying to get a tooltip for?
2526 event
.m_item
= info
->hItem
;
2533 case TVN_GETDISPINFO
:
2534 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2537 case TVN_SETDISPINFO
:
2539 if ( eventType
== wxEVT_NULL
)
2540 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2541 //else: get, already set above
2543 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2545 event
.m_item
= info
->item
.hItem
;
2549 case TVN_ITEMEXPANDING
:
2550 case TVN_ITEMEXPANDED
:
2552 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2555 switch ( tv
->action
)
2558 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2566 what
= IDX_COLLAPSE
;
2570 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2573 eventType
= gs_expandEvents
[what
][how
];
2575 event
.m_item
= tv
->itemNew
.hItem
;
2581 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2582 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2584 // fabricate the lParam and wParam parameters sufficiently
2585 // similar to the ones from a "real" WM_KEYDOWN so that
2586 // CreateKeyEvent() works correctly
2587 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2588 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2590 WXWPARAM wParam
= info
->wVKey
;
2592 int keyCode
= wxCharCodeMSWToWX(wParam
);
2595 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2600 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2605 // a separate event for Space/Return
2606 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2607 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2609 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2611 event2
.SetEventObject(this);
2612 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2614 event2
.m_item
= GetSelection();
2616 //else: don't know how to get it
2618 (void)GetEventHandler()->ProcessEvent(event2
);
2623 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2624 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2625 // we have to handle both messages:
2626 case TVN_SELCHANGEDA
:
2627 case TVN_SELCHANGEDW
:
2628 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2631 case TVN_SELCHANGINGA
:
2632 case TVN_SELCHANGINGW
:
2634 if ( eventType
== wxEVT_NULL
)
2635 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2636 //else: already set above
2638 if (hdr
->code
== TVN_SELCHANGINGW
||
2639 hdr
->code
== TVN_SELCHANGEDW
)
2641 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2642 event
.m_item
= tv
->itemNew
.hItem
;
2643 event
.m_itemOld
= tv
->itemOld
.hItem
;
2647 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2648 event
.m_item
= tv
->itemNew
.hItem
;
2649 event
.m_itemOld
= tv
->itemOld
.hItem
;
2654 // instead of explicitly checking for _WIN32_IE, check if the
2655 // required symbols are available in the headers
2656 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2659 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2660 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2661 switch ( nmcd
.dwDrawStage
)
2664 // if we've got any items with non standard attributes,
2665 // notify us before painting each item
2666 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2670 case CDDS_ITEMPREPAINT
:
2672 wxMapTreeAttr::iterator
2673 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2675 if ( it
== m_attrs
.end() )
2677 // nothing to do for this item
2678 *result
= CDRF_DODEFAULT
;
2682 wxTreeItemAttr
* const attr
= it
->second
;
2684 // selection colours should override ours,
2685 // otherwise it is too confusing ot the user
2686 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) )
2689 if ( attr
->HasBackgroundColour() )
2691 colBack
= attr
->GetBackgroundColour();
2692 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2696 // but we still want to keep the special foreground
2697 // colour when we don't have focus (we can't keep
2698 // it when we do, it would usually be unreadable on
2699 // the almost inverted bg colour...)
2700 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2701 FindFocus() != this )
2704 if ( attr
->HasTextColour() )
2706 colText
= attr
->GetTextColour();
2707 lptvcd
->clrText
= wxColourToRGB(colText
);
2711 if ( attr
->HasFont() )
2713 HFONT hFont
= GetHfontOf(attr
->GetFont());
2715 ::SelectObject(nmcd
.hdc
, hFont
);
2717 *result
= CDRF_NEWFONT
;
2719 else // no specific font
2721 *result
= CDRF_DODEFAULT
;
2727 *result
= CDRF_DODEFAULT
;
2731 // we always process it
2733 #endif // have owner drawn support in headers
2737 DWORD pos
= GetMessagePos();
2739 point
.x
= LOWORD(pos
);
2740 point
.y
= HIWORD(pos
);
2741 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2743 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2744 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2746 event
.m_item
= item
;
2747 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2755 TV_HITTESTINFO tvhti
;
2756 ::GetCursorPos(&tvhti
.pt
);
2757 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2758 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2760 if ( tvhti
.flags
& TVHT_ONITEM
)
2762 event
.m_item
= tvhti
.hItem
;
2763 eventType
= (int)hdr
->code
== NM_DBLCLK
2764 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2765 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2767 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2768 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2777 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2780 event
.SetEventObject(this);
2781 event
.SetEventType(eventType
);
2783 bool processed
= GetEventHandler()->ProcessEvent(event
);
2786 switch ( hdr
->code
)
2789 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2790 // the return code of this event handler as the return value for
2791 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2792 // expanded status would never work
2797 case TVN_BEGINRDRAG
:
2798 if ( event
.IsAllowed() )
2800 // normally this is impossible because the m_dragImage is
2801 // deleted once the drag operation is over
2802 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2804 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2805 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2806 m_dragImage
->Show();
2810 case TVN_DELETEITEM
:
2812 // NB: we might process this message using wxWidgets event
2813 // tables, but due to overhead of wxWin event system we
2814 // prefer to do it here ourself (otherwise deleting a tree
2815 // with many items is just too slow)
2816 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2818 wxTreeItemParam
*param
=
2819 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2822 processed
= true; // Make sure we don't get called twice
2826 case TVN_BEGINLABELEDIT
:
2827 // return true to cancel label editing
2828 *result
= !event
.IsAllowed();
2830 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2831 if ( event
.IsAllowed() )
2833 HWND hText
= TreeView_GetEditControl(GetHwnd());
2836 // MBN: if m_textCtrl already has an HWND, it is a stale
2837 // pointer from a previous edit (because the user
2838 // didn't modify the label before dismissing the control,
2839 // and TVN_ENDLABELEDIT was not sent), so delete it
2840 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2843 m_textCtrl
= new wxTextCtrl();
2844 m_textCtrl
->SetParent(this);
2845 m_textCtrl
->SetHWND((WXHWND
)hText
);
2846 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2848 // set wxTE_PROCESS_ENTER style for the text control to
2849 // force it to process the Enter presses itself, otherwise
2850 // they could be stolen from it by the dialog
2852 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2853 | wxTE_PROCESS_ENTER
);
2856 else // we had set m_idEdited before
2862 case TVN_ENDLABELEDIT
:
2863 // return true to set the label to the new string: note that we
2864 // also must pretend that we did process the message or it is going
2865 // to be passed to DefWindowProc() which will happily return false
2866 // cancelling the label change
2867 *result
= event
.IsAllowed();
2870 // ensure that we don't have the text ctrl which is going to be
2876 #ifdef TVN_GETINFOTIP
2877 case TVN_GETINFOTIP
:
2879 // If the user permitted a tooltip change, change it
2880 if (event
.IsAllowed())
2882 SetToolTip(event
.m_label
);
2889 case TVN_SELCHANGING
:
2890 case TVN_ITEMEXPANDING
:
2891 // return true to prevent the action from happening
2892 *result
= !event
.IsAllowed();
2895 case TVN_ITEMEXPANDED
:
2896 // the item is not refreshed properly after expansion when it has
2897 // an image depending on the expanded/collapsed state - bug in
2898 // comctl32.dll or our code?
2900 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2901 wxTreeItemId
id(tv
->itemNew
.hItem
);
2903 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2911 case TVN_GETDISPINFO
:
2912 // NB: so far the user can't set the image himself anyhow, so do it
2913 // anyway - but this may change later
2914 //if ( /* !processed && */ )
2916 wxTreeItemId item
= event
.m_item
;
2917 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2919 wxTreeItemParam
*param
= GetItemParam(item
);
2923 if ( info
->item
.mask
& TVIF_IMAGE
)
2928 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2929 : wxTreeItemIcon_Normal
2932 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2934 info
->item
.iSelectedImage
=
2937 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2938 : wxTreeItemIcon_Selected
2945 // for the other messages the return value is ignored and there is
2946 // nothing special to do
2951 // ----------------------------------------------------------------------------
2953 // ----------------------------------------------------------------------------
2955 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2956 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2958 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2961 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2962 tvi
.mask
= TVIF_STATE
;
2963 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2965 // Select the specified state, or -1 == cycle to the next one.
2968 TreeView_GetItem(GetHwnd(), &tvi
);
2970 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2971 if ( state
== m_imageListState
->GetImageCount() )
2975 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2976 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2978 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2980 TreeView_SetItem(GetHwnd(), &tvi
);
2983 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2986 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2987 tvi
.mask
= TVIF_STATE
;
2988 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2989 TreeView_GetItem(GetHwnd(), &tvi
);
2991 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2994 #endif // wxUSE_TREECTRL