1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
37 #include "wx/settings.h"
40 #include "wx/msw/private.h"
42 // Set this to 1 to be _absolutely_ sure that repainting will work for all
43 // comctl32.dll versions
44 #define wxUSE_COMCTL32_SAFELY 0
46 #include "wx/imaglist.h"
47 #include "wx/msw/dragimag.h"
49 // macros to hide the cast ugliness
50 // --------------------------------
52 // get HTREEITEM from wxTreeItemId
53 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // wrappers for TreeView_GetItem/TreeView_SetItem
60 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
64 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
65 tvi
.stateMask
= TVIS_SELECTED
;
68 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
70 wxLogLastError(wxT("TreeView_GetItem"));
73 return (tvi
.state
& TVIS_SELECTED
) != 0;
76 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
79 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
80 tvi
.stateMask
= TVIS_SELECTED
;
81 tvi
.state
= select
? TVIS_SELECTED
: 0;
84 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
86 wxLogLastError(wxT("TreeView_SetItem"));
93 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
95 SelectItem(hwndTV
, htItem
, false);
98 // helper function which selects all items in a range and, optionally,
99 // unselects all others
100 static void SelectRange(HWND hwndTV
,
103 bool unselectOthers
= true)
105 // find the first (or last) item and select it
107 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
108 while ( htItem
&& cont
)
110 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
112 if ( !IsItemSelected(hwndTV
, htItem
) )
114 SelectItem(hwndTV
, htItem
);
121 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
123 UnselectItem(hwndTV
, htItem
);
127 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
130 // select the items in range
131 cont
= htFirst
!= htLast
;
132 while ( htItem
&& cont
)
134 if ( !IsItemSelected(hwndTV
, htItem
) )
136 SelectItem(hwndTV
, htItem
);
139 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
141 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
145 if ( unselectOthers
)
149 if ( IsItemSelected(hwndTV
, htItem
) )
151 UnselectItem(hwndTV
, htItem
);
154 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
158 // seems to be necessary - otherwise the just selected items don't always
159 // appear as selected
160 UpdateWindow(hwndTV
);
163 // helper function which tricks the standard control into changing the focused
164 // item without changing anything else (if someone knows why Microsoft doesn't
165 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
167 // returns true if the focus was changed, false if the given item was already
169 static bool SetFocus(HWND hwndTV
, HTREEITEM htItem
)
172 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
174 if ( htItem
== htFocus
)
179 // remember the selection state of the item
180 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
182 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
184 // prevent the tree from unselecting the old focus which it
185 // would do by default (TreeView_SelectItem unselects the
187 TreeView_SelectItem(hwndTV
, 0);
188 SelectItem(hwndTV
, htFocus
);
191 TreeView_SelectItem(hwndTV
, htItem
);
195 // need to clear the selection which TreeView_SelectItem() gave
197 UnselectItem(hwndTV
, htItem
);
199 //else: was selected, still selected - ok
203 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
205 // just clear the focus
206 TreeView_SelectItem(hwndTV
, 0);
208 if ( wasFocusSelected
)
210 // restore the selection state
211 SelectItem(hwndTV
, htFocus
);
218 // ----------------------------------------------------------------------------
220 // ----------------------------------------------------------------------------
222 // a convenient wrapper around TV_ITEM struct which adds a ctor
224 #pragma warning( disable : 4097 ) // inheriting from typedef
227 struct wxTreeViewItem
: public TV_ITEM
229 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
230 UINT mask_
, // fields which are valid
231 UINT stateMask_
= 0) // for TVIF_STATE only
235 // hItem member is always valid
236 mask
= mask_
| TVIF_HANDLE
;
237 stateMask
= stateMask_
;
242 // ----------------------------------------------------------------------------
243 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
245 // We need this for a couple of reasons:
247 // 1) This class is needed for support of different images: the Win32 common
248 // control natively supports only 2 images (the normal one and another for the
249 // selected state). We wish to provide support for 2 more of them for folder
250 // items (i.e. those which have children): for expanded state and for expanded
251 // selected state. For this we use this structure to store the additional items
254 // 2) This class is also needed to hold the HITEM so that we can sort
255 // it correctly in the MSW sort callback.
257 // In addition it makes other workarounds such as this easier and helps
258 // simplify the code.
259 // ----------------------------------------------------------------------------
261 class wxTreeItemParam
268 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
274 // dtor deletes the associated data as well
275 virtual ~wxTreeItemParam() { delete m_data
; }
278 // get the real data associated with the item
279 wxTreeItemData
*GetData() const { return m_data
; }
281 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
283 // do we have such image?
284 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
285 // get image, falling back to the other images if this one is not
287 int GetImage(wxTreeItemIcon which
) const
289 int image
= m_images
[which
];
294 case wxTreeItemIcon_SelectedExpanded
:
295 image
= GetImage(wxTreeItemIcon_Expanded
);
300 case wxTreeItemIcon_Selected
:
301 case wxTreeItemIcon_Expanded
:
302 image
= GetImage(wxTreeItemIcon_Normal
);
305 case wxTreeItemIcon_Normal
:
310 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
316 // change the given image
317 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
320 const wxTreeItemId
& GetItem() const { return m_item
; }
322 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
325 // all the images associated with the item
326 int m_images
[wxTreeItemIcon_Max
];
328 // item for sort callbacks
331 // the real client data
332 wxTreeItemData
*m_data
;
334 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
337 // wxVirutalNode is used in place of a single root when 'hidden' root is
339 class wxVirtualNode
: public wxTreeViewItem
342 wxVirtualNode(wxTreeItemParam
*param
)
343 : wxTreeViewItem(TVI_ROOT
, 0)
353 wxTreeItemParam
*GetParam() const { return m_param
; }
354 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
357 wxTreeItemParam
*m_param
;
359 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
363 #pragma warning( default : 4097 )
366 // a macro to get the virtual root, returns NULL if none
367 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
369 // returns true if the item is the virtual root
370 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
372 // a class which encapsulates the tree traversal logic: it vists all (unless
373 // OnVisit() returns false) items under the given one
374 class wxTreeTraversal
377 wxTreeTraversal(const wxTreeCtrl
*tree
)
382 // give it a virtual dtor: not really needed as the class is never used
383 // polymorphically and not even allocated on heap at all, but this is safer
384 // (in case it ever is) and silences the compiler warnings for now
385 virtual ~wxTreeTraversal() { }
387 // do traverse the tree: visit all items (recursively by default) under the
388 // given one; return true if all items were traversed or false if the
389 // traversal was aborted because OnVisit returned false
390 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
392 // override this function to do whatever is needed for each item, return
393 // false to stop traversing
394 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
397 const wxTreeCtrl
*GetTree() const { return m_tree
; }
400 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
402 const wxTreeCtrl
*m_tree
;
404 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
407 // internal class for getting the selected items
408 class TraverseSelections
: public wxTreeTraversal
411 TraverseSelections(const wxTreeCtrl
*tree
,
412 wxArrayTreeItemIds
& selections
)
413 : wxTreeTraversal(tree
), m_selections(selections
)
415 m_selections
.Empty();
417 if (tree
->GetCount() > 0)
418 DoTraverse(tree
->GetRootItem());
421 virtual bool OnVisit(const wxTreeItemId
& item
)
423 const wxTreeCtrl
* const tree
= GetTree();
425 // can't visit a virtual node.
426 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
431 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
433 m_selections
.Add(item
);
439 size_t GetCount() const { return m_selections
.GetCount(); }
442 wxArrayTreeItemIds
& m_selections
;
444 DECLARE_NO_COPY_CLASS(TraverseSelections
)
447 // internal class for counting tree items
448 class TraverseCounter
: public wxTreeTraversal
451 TraverseCounter(const wxTreeCtrl
*tree
,
452 const wxTreeItemId
& root
,
454 : wxTreeTraversal(tree
)
458 DoTraverse(root
, recursively
);
461 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
468 size_t GetCount() const { return m_count
; }
473 DECLARE_NO_COPY_CLASS(TraverseCounter
)
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 #if wxUSE_EXTENDED_RTTI
481 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
483 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
484 // new style border flags, we put them first to
485 // use them for streaming out
486 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
487 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
488 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
489 wxFLAGS_MEMBER(wxBORDER_RAISED
)
490 wxFLAGS_MEMBER(wxBORDER_STATIC
)
491 wxFLAGS_MEMBER(wxBORDER_NONE
)
493 // old style border flags
494 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
495 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
496 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
497 wxFLAGS_MEMBER(wxRAISED_BORDER
)
498 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
499 wxFLAGS_MEMBER(wxBORDER
)
501 // standard window styles
502 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
503 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
504 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
505 wxFLAGS_MEMBER(wxWANTS_CHARS
)
506 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
507 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
508 wxFLAGS_MEMBER(wxVSCROLL
)
509 wxFLAGS_MEMBER(wxHSCROLL
)
511 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
512 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
513 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
514 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
515 wxFLAGS_MEMBER(wxTR_NO_LINES
)
516 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
517 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
518 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
519 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
520 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
521 wxFLAGS_MEMBER(wxTR_SINGLE
)
522 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
523 #if WXWIN_COMPATIBILITY_2_8
524 wxFLAGS_MEMBER(wxTR_EXTENDED
)
526 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
528 wxEND_FLAGS( wxTreeCtrlStyle
)
530 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
532 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
533 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
534 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
535 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
536 wxEND_PROPERTIES_TABLE()
538 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
539 wxEND_HANDLERS_TABLE()
541 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
543 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
546 // ----------------------------------------------------------------------------
548 // ----------------------------------------------------------------------------
550 // indices in gs_expandEvents table below
565 // handy table for sending events - it has to be initialized during run-time
566 // now so can't be const any more
567 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
570 but logically it's a const table with the following entries:
573 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
574 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
578 // ============================================================================
580 // ============================================================================
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
588 if ( !OnVisit(root
) )
591 return Traverse(root
, recursively
);
594 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
596 wxTreeItemIdValue cookie
;
597 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
598 while ( child
.IsOk() )
600 // depth first traversal
601 if ( recursively
&& !Traverse(child
, true) )
604 if ( !OnVisit(child
) )
607 child
= m_tree
->GetNextChild(root
, cookie
);
613 // ----------------------------------------------------------------------------
614 // construction and destruction
615 // ----------------------------------------------------------------------------
617 void wxTreeCtrl::Init()
620 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 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
672 // Need so that TVN_GETINFOTIP messages will be sent
673 wstyle
|= TVS_INFOTIP
;
676 // Create the tree control.
677 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
680 #if wxUSE_COMCTL32_SAFELY
681 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
682 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
684 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
685 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
687 // This works around a bug in the Windows tree control whereby for some versions
688 // of comctrl32, setting any colour actually draws the background in black.
689 // This will initialise the background to the system colour.
690 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
691 // Assume the user has an updated comctl32.dll.
692 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
693 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
694 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
698 // VZ: this is some experimental code which may be used to get the
699 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
700 // AFAIK, the standard DLL does about the same thing anyhow.
702 if ( m_windowStyle
& wxTR_MULTIPLE
)
706 // create the DC compatible with the current screen
707 HDC hdcMem
= CreateCompatibleDC(NULL
);
709 // create a mono bitmap of the standard size
710 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
711 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
712 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
713 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
714 1, // # of color planes
715 1, // # bits needed for one pixel
716 0); // array containing colour data
717 SelectObject(hdcMem
, hbmpCheck
);
719 // then draw a check mark into it
720 RECT rect
= { 0, 0, x
, y
};
721 if ( !::DrawFrameControl(hdcMem
, &rect
,
723 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
725 wxLogLastError(wxT("DrawFrameControl(check)"));
728 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
729 imagelistCheckboxes
.Add(bmp
);
731 if ( !::DrawFrameControl(hdcMem
, &rect
,
735 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
738 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
739 imagelistCheckboxes
.Add(bmp
);
745 SetStateImageList(&imagelistCheckboxes
);
749 wxSetCCUnicodeFormat(GetHwnd());
754 wxTreeCtrl::~wxTreeCtrl()
756 // delete any attributes
759 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
761 // prevent TVN_DELETEITEM handler from deleting the attributes again!
762 m_hasAnyAttr
= false;
767 // delete user data to prevent memory leaks
768 // also deletes hidden root node storage.
772 // ----------------------------------------------------------------------------
774 // ----------------------------------------------------------------------------
776 /* static */ wxVisualAttributes
777 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
779 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
781 // common controls have their own default font
782 attrs
.font
= wxGetCCDefaultFont();
788 // simple wrappers which add error checking in debug mode
790 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
792 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
793 _T("can't retrieve virtual root item") );
795 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
797 wxLogLastError(wxT("TreeView_GetItem"));
805 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
807 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
809 wxLogLastError(wxT("TreeView_SetItem"));
813 unsigned int wxTreeCtrl::GetCount() const
815 return (unsigned int)TreeView_GetCount(GetHwnd());
818 unsigned int wxTreeCtrl::GetIndent() const
820 return TreeView_GetIndent(GetHwnd());
823 void wxTreeCtrl::SetIndent(unsigned int indent
)
825 TreeView_SetIndent(GetHwnd(), indent
);
828 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
831 (void) TreeView_SetImageList(GetHwnd(),
832 imageList
? imageList
->GetHIMAGELIST() : 0,
836 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
838 if (m_ownsImageListNormal
)
839 delete m_imageListNormal
;
841 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
842 m_ownsImageListNormal
= false;
845 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
847 if (m_ownsImageListState
) delete m_imageListState
;
848 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
849 m_ownsImageListState
= false;
852 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
853 bool recursively
) const
855 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
857 TraverseCounter
counter(this, item
, recursively
);
858 return counter
.GetCount() - 1;
861 // ----------------------------------------------------------------------------
863 // ----------------------------------------------------------------------------
865 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
867 #if !wxUSE_COMCTL32_SAFELY
868 if ( !wxWindowBase::SetBackgroundColour(colour
) )
871 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
877 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
879 #if !wxUSE_COMCTL32_SAFELY
880 if ( !wxWindowBase::SetForegroundColour(colour
) )
883 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
889 // ----------------------------------------------------------------------------
891 // ----------------------------------------------------------------------------
893 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
895 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
898 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
900 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
902 wxChar buf
[512]; // the size is arbitrary...
904 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
905 tvItem
.pszText
= buf
;
906 tvItem
.cchTextMax
= WXSIZEOF(buf
);
907 if ( !DoGetItem(&tvItem
) )
909 // don't return some garbage which was on stack, but an empty string
913 return wxString(buf
);
916 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
918 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
920 if ( IS_VIRTUAL_ROOT(item
) )
923 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
924 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
927 // when setting the text of the item being edited, the text control should
928 // be updated to reflect the new text as well, otherwise calling
929 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
931 // don't use GetEditControl() here because m_textCtrl is not set yet
932 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
935 if ( item
== m_idEdited
)
937 ::SetWindowText(hwndEdit
, text
.wx_str());
942 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
943 wxTreeItemIcon which
) const
945 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
947 if ( IsHiddenRoot(item
) )
949 // no images for hidden root item
953 wxTreeItemParam
*param
= GetItemParam(item
);
955 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
958 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
959 wxTreeItemIcon which
)
961 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
962 wxCHECK_RET( which
>= 0 &&
963 which
< wxTreeItemIcon_Max
,
964 wxT("invalid image index"));
967 if ( IsHiddenRoot(item
) )
969 // no images for hidden root item
973 wxTreeItemParam
*data
= GetItemParam(item
);
977 data
->SetImage(image
, which
);
982 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
984 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
986 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
988 // hidden root may still have data.
989 if ( IS_VIRTUAL_ROOT(item
) )
991 return GET_VIRTUAL_ROOT()->GetParam();
995 if ( !DoGetItem(&tvItem
) )
1000 return (wxTreeItemParam
*)tvItem
.lParam
;
1003 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1005 wxTreeItemParam
*data
= GetItemParam(item
);
1007 return data
? data
->GetData() : NULL
;
1010 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1012 // first, associate this piece of data with this item
1018 wxTreeItemParam
*param
= GetItemParam(item
);
1020 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1022 param
->SetData(data
);
1025 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1027 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1029 if ( IS_VIRTUAL_ROOT(item
) )
1032 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1033 tvItem
.cChildren
= (int)has
;
1037 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1039 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1041 if ( IS_VIRTUAL_ROOT(item
) )
1044 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1045 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1049 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1051 if ( IS_VIRTUAL_ROOT(item
) )
1054 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1055 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1059 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1061 if ( IS_VIRTUAL_ROOT(item
) )
1065 if ( GetBoundingRect(item
, rect
) )
1071 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1073 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1075 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1076 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1079 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1081 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1083 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1084 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1087 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1089 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1091 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1092 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1095 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1096 const wxColour
& col
)
1098 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1100 wxTreeItemAttr
*attr
;
1101 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1102 if ( it
== m_attrs
.end() )
1104 m_hasAnyAttr
= true;
1106 m_attrs
[item
.m_pItem
] =
1107 attr
= new wxTreeItemAttr
;
1114 attr
->SetTextColour(col
);
1119 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1120 const wxColour
& col
)
1122 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1124 wxTreeItemAttr
*attr
;
1125 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1126 if ( it
== m_attrs
.end() )
1128 m_hasAnyAttr
= true;
1130 m_attrs
[item
.m_pItem
] =
1131 attr
= new wxTreeItemAttr
;
1133 else // already in the hash
1138 attr
->SetBackgroundColour(col
);
1143 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1145 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1147 wxTreeItemAttr
*attr
;
1148 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1149 if ( it
== m_attrs
.end() )
1151 m_hasAnyAttr
= true;
1153 m_attrs
[item
.m_pItem
] =
1154 attr
= new wxTreeItemAttr
;
1156 else // already in the hash
1161 attr
->SetFont(font
);
1163 // Reset the item's text to ensure that the bounding rect will be adjusted
1164 // for the new font.
1165 SetItemText(item
, GetItemText(item
));
1170 // ----------------------------------------------------------------------------
1172 // ----------------------------------------------------------------------------
1174 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1176 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1178 if ( item
== wxTreeItemId(TVI_ROOT
) )
1180 // virtual (hidden) root is never visible
1184 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1187 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1188 // the HTREEITEM with TVM_GETITEMRECT
1189 *(HTREEITEM
*)&rect
= HITEM(item
);
1191 // true means to get rect for just the text, not the whole line
1192 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1194 // if TVM_GETITEMRECT returned false, then the item is definitely not
1195 // visible (because its parent is not expanded)
1199 // however if it returned true, the item might still be outside the
1200 // currently visible part of the tree, test for it (notice that partly
1201 // visible means visible here)
1202 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1205 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1207 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1209 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1212 return tvItem
.cChildren
!= 0;
1215 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1217 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1219 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1222 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1225 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1227 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1229 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1232 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1235 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1237 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1239 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1242 return (tvItem
.state
& TVIS_BOLD
) != 0;
1245 // ----------------------------------------------------------------------------
1247 // ----------------------------------------------------------------------------
1249 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1251 // Root may be real (visible) or virtual (hidden).
1252 if ( GET_VIRTUAL_ROOT() )
1255 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1258 wxTreeItemId
wxTreeCtrl::GetSelection() const
1260 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1261 wxT("this only works with single selection controls") );
1263 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1266 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1268 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1272 if ( IS_VIRTUAL_ROOT(item
) )
1274 // no parent for the virtual root
1279 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1280 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1282 // the top level items should have the virtual root as their parent
1287 return wxTreeItemId(hItem
);
1290 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1291 wxTreeItemIdValue
& cookie
) const
1293 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1295 // remember the last child returned in 'cookie'
1296 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1298 return wxTreeItemId(cookie
);
1301 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1302 wxTreeItemIdValue
& cookie
) const
1304 wxTreeItemId
fromCookie(cookie
);
1306 HTREEITEM hitem
= HITEM(fromCookie
);
1308 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1310 wxTreeItemId
item(hitem
);
1312 cookie
= item
.m_pItem
;
1317 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1319 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1321 // can this be done more efficiently?
1322 wxTreeItemIdValue cookie
;
1324 wxTreeItemId childLast
,
1325 child
= GetFirstChild(item
, cookie
);
1326 while ( child
.IsOk() )
1329 child
= GetNextChild(item
, cookie
);
1335 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1337 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1338 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1341 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1343 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1344 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1347 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1349 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1352 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1354 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1355 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1357 wxTreeItemId
next(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1358 if ( next
.IsOk() && !IsVisible(next
) )
1360 // Win32 considers that any non-collapsed item is visible while we want
1361 // to return only really visible items
1368 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1370 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1371 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1373 wxTreeItemId
prev(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1374 if ( prev
.IsOk() && !IsVisible(prev
) )
1376 // just as above, Win32 function will happily return the previous item
1377 // in the tree for the first visible item too
1384 // ----------------------------------------------------------------------------
1385 // multiple selections emulation
1386 // ----------------------------------------------------------------------------
1388 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1390 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1392 // receive the desired information.
1393 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1396 // state image indices are 1 based
1397 return ((tvItem
.state
>> 12) - 1) == 1;
1400 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1402 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1404 // receive the desired information.
1405 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1409 // state images are one-based
1410 tvItem
.state
= (check
? 2 : 1) << 12;
1415 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1417 TraverseSelections
selector(this, selections
);
1419 return selector
.GetCount();
1422 // ----------------------------------------------------------------------------
1424 // ----------------------------------------------------------------------------
1426 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1427 const wxTreeItemId
& hInsertAfter
,
1428 const wxString
& text
,
1429 int image
, int selectedImage
,
1430 wxTreeItemData
*data
)
1432 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1434 _T("can't have more than one root in the tree") );
1436 TV_INSERTSTRUCT tvIns
;
1437 tvIns
.hParent
= HITEM(parent
);
1438 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1440 // this is how we insert the item as the first child: supply a NULL
1442 if ( !tvIns
.hInsertAfter
)
1444 tvIns
.hInsertAfter
= TVI_FIRST
;
1448 if ( !text
.empty() )
1451 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1455 tvIns
.item
.pszText
= NULL
;
1456 tvIns
.item
.cchTextMax
= 0;
1459 // create the param which will store the other item parameters
1460 wxTreeItemParam
*param
= new wxTreeItemParam
;
1462 // we return the images on demand as they depend on whether the item is
1463 // expanded or collapsed too in our case
1464 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1465 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1466 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1468 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1469 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1472 tvIns
.item
.lParam
= (LPARAM
)param
;
1473 tvIns
.item
.mask
= mask
;
1475 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1478 wxLogLastError(wxT("TreeView_InsertItem"));
1481 // associate the application tree item with Win32 tree item handle
1484 // setup wxTreeItemData
1487 param
->SetData(data
);
1491 return wxTreeItemId(id
);
1494 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1495 int image
, int selectedImage
,
1496 wxTreeItemData
*data
)
1498 if ( HasFlag(wxTR_HIDE_ROOT
) )
1500 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1502 // create a virtual root item, the parent for all the others
1503 wxTreeItemParam
*param
= new wxTreeItemParam
;
1504 param
->SetData(data
);
1506 m_pVirtualRoot
= new wxVirtualNode(param
);
1511 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1512 text
, image
, selectedImage
, data
);
1515 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1517 const wxString
& text
,
1518 int image
, int selectedImage
,
1519 wxTreeItemData
*data
)
1521 wxTreeItemId idPrev
;
1522 if ( index
== (size_t)-1 )
1524 // special value: append to the end
1527 else // find the item from index
1529 wxTreeItemIdValue cookie
;
1530 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1531 while ( index
!= 0 && idCur
.IsOk() )
1536 idCur
= GetNextChild(parent
, cookie
);
1539 // assert, not check: if the index is invalid, we will append the item
1541 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1544 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1547 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1549 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1551 wxLogLastError(wxT("TreeView_DeleteItem"));
1555 // delete all children (but don't delete the item itself)
1556 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1558 wxTreeItemIdValue cookie
;
1560 wxArrayTreeItemIds children
;
1561 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1562 while ( child
.IsOk() )
1564 children
.Add(child
);
1566 child
= GetNextChild(item
, cookie
);
1569 size_t nCount
= children
.Count();
1570 for ( size_t n
= 0; n
< nCount
; n
++ )
1572 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1574 wxLogLastError(wxT("TreeView_DeleteItem"));
1579 void wxTreeCtrl::DeleteAllItems()
1581 // delete the "virtual" root item.
1582 if ( GET_VIRTUAL_ROOT() )
1584 delete GET_VIRTUAL_ROOT();
1585 m_pVirtualRoot
= NULL
;
1588 // and all the real items
1590 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1592 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1596 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1598 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1599 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1600 flag
== TVE_EXPAND
||
1602 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1604 // A hidden root can be neither expanded nor collapsed.
1605 wxCHECK_RET( !IsHiddenRoot(item
),
1606 wxT("Can't expand/collapse hidden root node!") );
1608 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1609 // emulate them. This behaviour has changed slightly with comctl32.dll
1610 // v 4.70 - now it does send them but only the first time. To maintain
1611 // compatible behaviour and also in order to not have surprises with the
1612 // future versions, don't rely on this and still do everything ourselves.
1613 // To avoid that the messages be sent twice when the item is expanded for
1614 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1616 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1620 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1622 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1624 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1628 (void)GetEventHandler()->ProcessEvent(event
);
1630 //else: change didn't took place, so do nothing at all
1633 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1635 DoExpand(item
, TVE_EXPAND
);
1638 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1640 DoExpand(item
, TVE_COLLAPSE
);
1643 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1645 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1648 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1650 DoExpand(item
, TVE_TOGGLE
);
1653 void wxTreeCtrl::Unselect()
1655 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1656 wxT("doesn't make sense, may be you want UnselectAll()?") );
1658 // just remove the selection
1659 SelectItem(wxTreeItemId());
1662 void wxTreeCtrl::UnselectAll()
1664 if ( m_windowStyle
& wxTR_MULTIPLE
)
1666 wxArrayTreeItemIds selections
;
1667 size_t count
= GetSelections(selections
);
1668 for ( size_t n
= 0; n
< count
; n
++ )
1670 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1673 m_htSelStart
.Unset();
1677 // just remove the selection
1682 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1684 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1686 wxASSERT_MSG( select
|| HasFlag(wxTR_MULTIPLE
),
1687 _T("SelectItem(false) works only for multiselect") );
1689 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1690 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1692 if ( HasFlag(wxTR_MULTIPLE
) )
1694 if ( !::SelectItem(GetHwnd(), HITEM(item
), select
) )
1696 wxLogLastError(wxT("TreeView_SelectItem"));
1700 else // single selection
1702 // use TreeView_SelectItem() to deselect the previous selection
1703 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1705 wxLogLastError(wxT("TreeView_SelectItem"));
1710 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1711 (void)GetEventHandler()->ProcessEvent(event
);
1713 //else: program vetoed the change
1716 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1718 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1721 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1724 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1726 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1728 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1732 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1737 void wxTreeCtrl::DeleteTextCtrl()
1741 // the HWND corresponding to this control is deleted by the tree
1742 // control itself and we don't know when exactly this happens, so check
1743 // if the window still exists before calling UnsubclassWin()
1744 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1746 m_textCtrl
->SetHWND(0);
1749 m_textCtrl
->UnsubclassWin();
1750 m_textCtrl
->SetHWND(0);
1758 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1759 wxClassInfo
*textControlClass
)
1761 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1766 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1767 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1769 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1778 // textctrl is subclassed in MSWOnNotify
1782 // End label editing, optionally cancelling the edit
1783 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1785 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1790 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1792 TV_HITTESTINFO hitTestInfo
;
1793 hitTestInfo
.pt
.x
= (int)point
.x
;
1794 hitTestInfo
.pt
.y
= (int)point
.y
;
1796 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1801 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1802 flags |= wxTREE_HITTEST_##flag
1804 TRANSLATE_FLAG(ABOVE
);
1805 TRANSLATE_FLAG(BELOW
);
1806 TRANSLATE_FLAG(NOWHERE
);
1807 TRANSLATE_FLAG(ONITEMBUTTON
);
1808 TRANSLATE_FLAG(ONITEMICON
);
1809 TRANSLATE_FLAG(ONITEMINDENT
);
1810 TRANSLATE_FLAG(ONITEMLABEL
);
1811 TRANSLATE_FLAG(ONITEMRIGHT
);
1812 TRANSLATE_FLAG(ONITEMSTATEICON
);
1813 TRANSLATE_FLAG(TOLEFT
);
1814 TRANSLATE_FLAG(TORIGHT
);
1816 #undef TRANSLATE_FLAG
1818 return wxTreeItemId(hitTestInfo
.hItem
);
1821 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1823 bool textOnly
) const
1827 // Virtual root items have no bounding rectangle
1828 if ( IS_VIRTUAL_ROOT(item
) )
1833 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1836 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1842 // couldn't retrieve rect: for example, item isn't visible
1847 // ----------------------------------------------------------------------------
1849 // ----------------------------------------------------------------------------
1851 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1852 // functions such as IsDataIndirect()
1853 class wxTreeSortHelper
1856 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1859 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1861 return ((wxTreeItemParam
*)lParam
)->GetItem();
1865 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1869 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1870 wxT("sorting tree without data doesn't make sense") );
1872 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1874 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1875 GetIdFromData(pItem2
));
1878 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1880 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1882 // rely on the fact that TreeView_SortChildren does the same thing as our
1883 // default behaviour, i.e. sorts items alphabetically and so call it
1884 // directly if we're not in derived class (much more efficient!)
1885 // RN: Note that if you find you're code doesn't sort as expected this
1886 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1887 // combo for your derived wxTreeCtrl if will sort without
1889 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1891 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1896 tvSort
.hParent
= HITEM(item
);
1897 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1898 tvSort
.lParam
= (LPARAM
)this;
1899 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1903 // ----------------------------------------------------------------------------
1905 // ----------------------------------------------------------------------------
1907 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1909 if ( msg
->message
== WM_KEYDOWN
)
1911 if ( msg
->wParam
== VK_RETURN
)
1913 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1918 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
1921 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id_
)
1923 const int id
= (signed short)id_
;
1925 if ( cmd
== EN_UPDATE
)
1927 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1928 event
.SetEventObject( this );
1929 ProcessCommand(event
);
1931 else if ( cmd
== EN_KILLFOCUS
)
1933 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1934 event
.SetEventObject( this );
1935 ProcessCommand(event
);
1943 // command processed
1947 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1948 // only do it during dragging, minimize wxWin overhead (this is important for
1949 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1950 // instead of passing by wxWin events
1951 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1953 bool processed
= false;
1955 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
1957 // This message is sent after a right-click, or when the "menu" key is pressed
1958 if ( nMsg
== WM_CONTEXTMENU
)
1960 int x
= GET_X_LPARAM(lParam
),
1961 y
= GET_Y_LPARAM(lParam
);
1963 // the item for which the menu should be shown
1966 // the position where the menu should be shown in client coordinates
1967 // (so that it can be passed directly to PopupMenu())
1970 if ( x
== -1 || y
== -1 )
1972 // this means that the event was generated from keyboard (e.g. with
1973 // Shift-F10 or special Windows menu key)
1975 // use the Explorer standard of putting the menu at the left edge
1976 // of the text, in the vertical middle of the text
1977 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1980 // Use the bounding rectangle of only the text part
1982 GetBoundingRect(item
, rect
, true);
1983 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
1986 else // event from mouse, use mouse position
1988 pt
= ScreenToClient(wxPoint(x
, y
));
1990 TV_HITTESTINFO tvhti
;
1993 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
1994 item
= wxTreeItemId(tvhti
.hItem
);
1998 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2000 event
.m_pointDrag
= pt
;
2002 if ( GetEventHandler()->ProcessEvent(event
) )
2004 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2006 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2008 // we only process mouse messages here and these parameters have the
2009 // same meaning for all of them
2010 int x
= GET_X_LPARAM(lParam
),
2011 y
= GET_Y_LPARAM(lParam
);
2013 TV_HITTESTINFO tvht
;
2017 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2021 case WM_LBUTTONDOWN
:
2022 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2024 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2025 m_ptClick
= wxPoint(x
, y
);
2027 if ( wParam
& MK_CONTROL
)
2031 // toggle selected state
2032 ToggleItemSelection(htItem
);
2034 ::SetFocus(GetHwnd(), htItem
);
2036 // reset on any click without Shift
2037 m_htSelStart
.Unset();
2041 else if ( wParam
& MK_SHIFT
)
2043 // this selects all items between the starting one and
2046 if ( !m_htSelStart
)
2048 // take the focused item
2049 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2053 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2054 !(wParam
& MK_CONTROL
));
2056 ::SelectItem(GetHwnd(), htItem
);
2058 ::SetFocus(GetHwnd(), htItem
);
2062 else // normal click
2064 // avoid doing anything if we click on the only
2065 // currently selected item
2069 wxArrayTreeItemIds selections
;
2070 size_t count
= GetSelections(selections
);
2073 HITEM(selections
[0]) != htItem
)
2075 // clear the previously selected items, if the
2076 // user clicked outside of the present selection.
2077 // otherwise, perform the deselection on mouse-up.
2078 // this allows multiple drag and drop to work.
2080 if (!IsItemSelected(GetHwnd(), htItem
))
2084 // prevent the click from starting in-place editing
2085 // which should only happen if we click on the
2086 // already selected item (and nothing else is
2089 TreeView_SelectItem(GetHwnd(), 0);
2090 ::SelectItem(GetHwnd(), htItem
);
2092 ::SetFocus(GetHwnd(), htItem
);
2095 else // click on a single selected item
2097 // don't interfere with the default processing in
2098 // WM_MOUSEMOVE handler below as the default window
2099 // proc will start the drag itself if we let have
2101 m_htClickedItem
.Unset();
2104 // reset on any click without Shift
2105 m_htSelStart
.Unset();
2112 if ( m_htClickedItem
)
2114 int cx
= abs(m_ptClick
.x
- x
);
2115 int cy
= abs(m_ptClick
.y
- y
);
2117 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2118 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2123 tv
.hdr
.hwndFrom
= GetHwnd();
2124 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2125 tv
.hdr
.code
= TVN_BEGINDRAG
;
2127 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2131 wxZeroMemory(tviAux
);
2133 tviAux
.hItem
= HITEM(m_htClickedItem
);
2134 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2135 tviAux
.stateMask
= 0xffffffff;
2136 TreeView_GetItem(GetHwnd(), &tviAux
);
2138 tv
.itemNew
.state
= tviAux
.state
;
2139 tv
.itemNew
.lParam
= tviAux
.lParam
;
2144 // do it before SendMessage() call below to avoid
2145 // reentrancies here if there is another WM_MOUSEMOVE
2146 // in the queue already
2147 m_htClickedItem
.Unset();
2149 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2150 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2152 // don't pass it to the default window proc, it would
2153 // start dragging again
2157 #endif // __WXWINCE__
2162 m_dragImage
->Move(wxPoint(x
, y
));
2165 // highlight the item as target (hiding drag image is
2166 // necessary - otherwise the display will be corrupted)
2167 m_dragImage
->Hide();
2168 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2169 m_dragImage
->Show();
2172 #endif // wxUSE_DRAGIMAGE
2177 // facilitates multiple drag-and-drop
2178 if (htItem
&& isMultiple
)
2180 wxArrayTreeItemIds selections
;
2181 size_t count
= GetSelections(selections
);
2184 !(wParam
& MK_CONTROL
) &&
2185 !(wParam
& MK_SHIFT
))
2188 TreeView_SelectItem(GetHwnd(), htItem
);
2189 ::SelectItem(GetHwnd(), htItem
);
2190 ::SetFocus(GetHwnd(), htItem
);
2192 m_htClickedItem
.Unset();
2201 m_dragImage
->EndDrag();
2205 // generate the drag end event
2206 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2207 event
.m_pointDrag
= wxPoint(x
, y
);
2209 (void)GetEventHandler()->ProcessEvent(event
);
2211 // if we don't do it, the tree seems to think that 2 items
2212 // are selected simultaneously which is quite weird
2213 TreeView_SelectDropTarget(GetHwnd(), 0);
2215 #endif // wxUSE_DRAGIMAGE
2219 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2221 // the tree control greys out the selected item when it loses focus and
2222 // paints it as selected again when it regains it, but it won't do it
2223 // for the other items itself - help it
2224 wxArrayTreeItemIds selections
;
2225 size_t count
= GetSelections(selections
);
2227 for ( size_t n
= 0; n
< count
; n
++ )
2229 // TreeView_GetItemRect() will return false if item is not visible,
2230 // which may happen perfectly well
2231 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2234 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2238 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2240 bool bCtrl
= wxIsCtrlDown(),
2241 bShift
= wxIsShiftDown();
2243 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2249 ToggleItemSelection(htSel
);
2255 ::SelectItem(GetHwnd(), htSel
);
2263 if ( !bCtrl
&& !bShift
)
2265 // no modifiers, just clear selection and then let the default
2266 // processing to take place
2271 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2273 HTREEITEM htNext
= (HTREEITEM
)
2274 TreeView_GetNextItem
2278 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2284 // at the top/bottom
2290 if ( !m_htSelStart
)
2291 m_htSelStart
= htSel
;
2293 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2297 // without changing selection
2298 ::SetFocus(GetHwnd(), htNext
);
2309 // TODO: handle Shift/Ctrl with these keys
2310 if ( !bCtrl
&& !bShift
)
2314 m_htSelStart
.Unset();
2318 else if ( nMsg
== WM_COMMAND
)
2320 // if we receive a EN_KILLFOCUS command from the in-place edit control
2321 // used for label editing, make sure to end editing
2324 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2326 if ( cmd
== EN_KILLFOCUS
)
2328 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2338 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2344 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2346 if ( nMsg
== WM_CHAR
)
2348 // don't let the control process Space and Return keys because it
2349 // doesn't do anything useful with them anyhow but always beeps
2350 // annoyingly when it receives them and there is no way to turn it off
2351 // simply if you just process TREEITEM_ACTIVATED event to which Space
2352 // and Enter presses are mapped in your code
2353 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2357 else if ( nMsg
== WM_KEYDOWN
)
2359 if ( wParam
== VK_ESCAPE
)
2363 m_dragImage
->EndDrag();
2367 // if we don't do it, the tree seems to think that 2 items
2368 // are selected simultaneously which is quite weird
2369 TreeView_SelectDropTarget(GetHwnd(), 0);
2373 #endif // wxUSE_DRAGIMAGE
2375 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2378 // process WM_NOTIFY Windows message
2379 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2381 wxTreeEvent
event(wxEVT_NULL
, this);
2382 wxEventType eventType
= wxEVT_NULL
;
2383 NMHDR
*hdr
= (NMHDR
*)lParam
;
2385 switch ( hdr
->code
)
2388 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2391 case TVN_BEGINRDRAG
:
2393 if ( eventType
== wxEVT_NULL
)
2394 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2395 //else: left drag, already set above
2397 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2399 event
.m_item
= tv
->itemNew
.hItem
;
2400 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2402 // don't allow dragging by default: the user code must
2403 // explicitly say that it wants to allow it to avoid breaking
2409 case TVN_BEGINLABELEDIT
:
2411 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2412 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2414 // although the user event handler may still veto it, it is
2415 // important to set it now so that calls to SetItemText() from
2416 // the event handler would change the text controls contents
2418 event
.m_item
= info
->item
.hItem
;
2419 event
.m_label
= info
->item
.pszText
;
2420 event
.m_editCancelled
= false;
2424 case TVN_DELETEITEM
:
2426 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2427 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2429 event
.m_item
= tv
->itemOld
.hItem
;
2433 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2434 if ( it
!= m_attrs
.end() )
2443 case TVN_ENDLABELEDIT
:
2445 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2446 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2448 event
.m_item
= info
->item
.hItem
;
2449 event
.m_label
= info
->item
.pszText
;
2450 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2455 // These *must* not be removed or TVN_GETINFOTIP will
2456 // not be processed each time the mouse is moved
2457 // and the tooltip will only ever update once.
2466 #ifdef TVN_GETINFOTIP
2467 case TVN_GETINFOTIP
:
2469 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2470 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2472 // Which item are we trying to get a tooltip for?
2473 event
.m_item
= info
->hItem
;
2477 #endif // TVN_GETINFOTIP
2478 #endif // !__WXWINCE__
2480 case TVN_GETDISPINFO
:
2481 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2484 case TVN_SETDISPINFO
:
2486 if ( eventType
== wxEVT_NULL
)
2487 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2488 //else: get, already set above
2490 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2492 event
.m_item
= info
->item
.hItem
;
2496 case TVN_ITEMEXPANDING
:
2497 case TVN_ITEMEXPANDED
:
2499 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2502 switch ( tv
->action
)
2505 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2513 what
= IDX_COLLAPSE
;
2517 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2520 eventType
= gs_expandEvents
[what
][how
];
2522 event
.m_item
= tv
->itemNew
.hItem
;
2528 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2529 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2531 // fabricate the lParam and wParam parameters sufficiently
2532 // similar to the ones from a "real" WM_KEYDOWN so that
2533 // CreateKeyEvent() works correctly
2534 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2535 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2537 WXWPARAM wParam
= info
->wVKey
;
2539 int keyCode
= wxCharCodeMSWToWX(wParam
);
2542 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2547 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2552 // a separate event for Space/Return
2553 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2554 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2557 if ( !HasFlag(wxTR_MULTIPLE
) )
2558 item
= GetSelection();
2560 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2562 (void)GetEventHandler()->ProcessEvent(event2
);
2567 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2568 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2569 // we have to handle both messages:
2570 case TVN_SELCHANGEDA
:
2571 case TVN_SELCHANGEDW
:
2572 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2575 case TVN_SELCHANGINGA
:
2576 case TVN_SELCHANGINGW
:
2578 if ( eventType
== wxEVT_NULL
)
2579 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2580 //else: already set above
2582 if (hdr
->code
== TVN_SELCHANGINGW
||
2583 hdr
->code
== TVN_SELCHANGEDW
)
2585 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2586 event
.m_item
= tv
->itemNew
.hItem
;
2587 event
.m_itemOld
= tv
->itemOld
.hItem
;
2591 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2592 event
.m_item
= tv
->itemNew
.hItem
;
2593 event
.m_itemOld
= tv
->itemOld
.hItem
;
2598 // instead of explicitly checking for _WIN32_IE, check if the
2599 // required symbols are available in the headers
2600 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2603 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2604 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2605 switch ( nmcd
.dwDrawStage
)
2608 // if we've got any items with non standard attributes,
2609 // notify us before painting each item
2610 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2614 case CDDS_ITEMPREPAINT
:
2616 wxMapTreeAttr::iterator
2617 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2619 if ( it
== m_attrs
.end() )
2621 // nothing to do for this item
2622 *result
= CDRF_DODEFAULT
;
2626 wxTreeItemAttr
* const attr
= it
->second
;
2628 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2629 TVIF_STATE
, TVIS_DROPHILITED
);
2631 const UINT tvItemState
= tvItem
.state
;
2633 // selection colours should override ours,
2634 // otherwise it is too confusing to the user
2635 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2636 !(tvItemState
& TVIS_DROPHILITED
) )
2639 if ( attr
->HasBackgroundColour() )
2641 colBack
= attr
->GetBackgroundColour();
2642 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2646 // but we still want to keep the special foreground
2647 // colour when we don't have focus (we can't keep
2648 // it when we do, it would usually be unreadable on
2649 // the almost inverted bg colour...)
2650 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2651 FindFocus() != this ) &&
2652 !(tvItemState
& TVIS_DROPHILITED
) )
2655 if ( attr
->HasTextColour() )
2657 colText
= attr
->GetTextColour();
2658 lptvcd
->clrText
= wxColourToRGB(colText
);
2662 if ( attr
->HasFont() )
2664 HFONT hFont
= GetHfontOf(attr
->GetFont());
2666 ::SelectObject(nmcd
.hdc
, hFont
);
2668 *result
= CDRF_NEWFONT
;
2670 else // no specific font
2672 *result
= CDRF_DODEFAULT
;
2678 *result
= CDRF_DODEFAULT
;
2682 // we always process it
2684 #endif // have owner drawn support in headers
2688 DWORD pos
= GetMessagePos();
2690 point
.x
= LOWORD(pos
);
2691 point
.y
= HIWORD(pos
);
2692 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2694 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2695 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2697 event
.m_item
= item
;
2698 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2706 TV_HITTESTINFO tvhti
;
2707 ::GetCursorPos(&tvhti
.pt
);
2708 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2709 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2711 if ( tvhti
.flags
& TVHT_ONITEM
)
2713 event
.m_item
= tvhti
.hItem
;
2714 eventType
= (int)hdr
->code
== NM_DBLCLK
2715 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2716 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2718 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2719 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2728 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2731 event
.SetEventType(eventType
);
2733 if ( event
.m_item
.IsOk() )
2734 event
.SetClientObject(GetItemData(event
.m_item
));
2736 bool processed
= GetEventHandler()->ProcessEvent(event
);
2739 switch ( hdr
->code
)
2742 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2743 // the return code of this event handler as the return value for
2744 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2745 // expanded status would never work
2750 // prevent tree control from sending WM_CONTEXTMENU to our parent
2751 // (which it does if NM_RCLICK is not handled) because we want to
2752 // send it to the control itself
2756 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
2757 (WPARAM
)GetHwnd(), ::GetMessagePos());
2761 case TVN_BEGINRDRAG
:
2763 if ( event
.IsAllowed() )
2765 // normally this is impossible because the m_dragImage is
2766 // deleted once the drag operation is over
2767 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2769 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2770 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2771 m_dragImage
->Show();
2773 #endif // wxUSE_DRAGIMAGE
2776 case TVN_DELETEITEM
:
2778 // NB: we might process this message using wxWidgets event
2779 // tables, but due to overhead of wxWin event system we
2780 // prefer to do it here ourself (otherwise deleting a tree
2781 // with many items is just too slow)
2782 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2784 wxTreeItemParam
*param
=
2785 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2788 processed
= true; // Make sure we don't get called twice
2792 case TVN_BEGINLABELEDIT
:
2793 // return true to cancel label editing
2794 *result
= !event
.IsAllowed();
2796 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2797 if ( event
.IsAllowed() )
2799 HWND hText
= TreeView_GetEditControl(GetHwnd());
2802 // MBN: if m_textCtrl already has an HWND, it is a stale
2803 // pointer from a previous edit (because the user
2804 // didn't modify the label before dismissing the control,
2805 // and TVN_ENDLABELEDIT was not sent), so delete it
2806 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2809 m_textCtrl
= new wxTextCtrl();
2810 m_textCtrl
->SetParent(this);
2811 m_textCtrl
->SetHWND((WXHWND
)hText
);
2812 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2814 // set wxTE_PROCESS_ENTER style for the text control to
2815 // force it to process the Enter presses itself, otherwise
2816 // they could be stolen from it by the dialog
2818 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2819 | wxTE_PROCESS_ENTER
);
2822 else // we had set m_idEdited before
2828 case TVN_ENDLABELEDIT
:
2829 // return true to set the label to the new string: note that we
2830 // also must pretend that we did process the message or it is going
2831 // to be passed to DefWindowProc() which will happily return false
2832 // cancelling the label change
2833 *result
= event
.IsAllowed();
2836 // ensure that we don't have the text ctrl which is going to be
2842 #ifdef TVN_GETINFOTIP
2843 case TVN_GETINFOTIP
:
2845 // If the user permitted a tooltip change, change it
2846 if (event
.IsAllowed())
2848 SetToolTip(event
.m_label
);
2855 case TVN_SELCHANGING
:
2856 case TVN_ITEMEXPANDING
:
2857 // return true to prevent the action from happening
2858 *result
= !event
.IsAllowed();
2861 case TVN_ITEMEXPANDED
:
2863 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2864 const wxTreeItemId
id(tv
->itemNew
.hItem
);
2866 if ( tv
->action
== TVE_COLLAPSE
)
2868 if ( wxApp::GetComCtl32Version() >= 600 )
2870 // for some reason the item selection rectangle depends
2871 // on whether it is expanded or collapsed (at least
2872 // with comctl32.dll v6): it is wider (by 3 pixels) in
2873 // the expanded state, so when the item collapses and
2874 // then is deselected the rightmost 3 pixels of the
2875 // previously drawn selection are left on the screen
2877 // it's not clear if it's a bug in comctl32.dll or in
2878 // our code (because it does not happen in Explorer but
2879 // OTOH we don't do anything which could result in this
2880 // AFAICS) but we do need to work around it to avoid
2887 // the item is also not refreshed properly after expansion when
2888 // it has an image depending on the expanded/collapsed state:
2889 // again, it's not clear if the bug is in comctl32.dll or our
2891 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2900 case TVN_GETDISPINFO
:
2901 // NB: so far the user can't set the image himself anyhow, so do it
2902 // anyway - but this may change later
2903 //if ( /* !processed && */ )
2905 wxTreeItemId item
= event
.m_item
;
2906 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2908 const wxTreeItemParam
* const param
= GetItemParam(item
);
2912 if ( info
->item
.mask
& TVIF_IMAGE
)
2917 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2918 : wxTreeItemIcon_Normal
2921 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2923 info
->item
.iSelectedImage
=
2926 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2927 : wxTreeItemIcon_Selected
2934 // for the other messages the return value is ignored and there is
2935 // nothing special to do
2940 // ----------------------------------------------------------------------------
2942 // ----------------------------------------------------------------------------
2944 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2945 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2947 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2950 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2951 tvi
.mask
= TVIF_STATE
;
2952 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2954 // Select the specified state, or -1 == cycle to the next one.
2957 TreeView_GetItem(GetHwnd(), &tvi
);
2959 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2960 if ( state
== m_imageListState
->GetImageCount() )
2964 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2965 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2967 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2969 TreeView_SetItem(GetHwnd(), &tvi
);
2972 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2975 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2976 tvi
.mask
= TVIF_STATE
;
2977 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2978 TreeView_GetItem(GetHwnd(), &tvi
);
2980 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2983 #endif // wxUSE_TREECTRL