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;
622 m_pVirtualRoot
= NULL
;
624 // initialize the global array of events now as it can't be done statically
625 // with the wxEVT_XXX values being allocated during run-time only
626 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
627 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
628 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
629 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
632 bool wxTreeCtrl::Create(wxWindow
*parent
,
637 const wxValidator
& validator
,
638 const wxString
& name
)
642 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
643 style
|= wxBORDER_SUNKEN
;
645 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
649 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
650 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
652 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
653 wstyle
|= TVS_HASLINES
;
654 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
655 wstyle
|= TVS_HASBUTTONS
;
657 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
658 wstyle
|= TVS_EDITLABELS
;
660 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
661 wstyle
|= TVS_LINESATROOT
;
663 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
665 if ( wxApp::GetComCtl32Version() >= 471 )
666 wstyle
|= TVS_FULLROWSELECT
;
669 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
670 // Need so that TVN_GETINFOTIP messages will be sent
671 wstyle
|= TVS_INFOTIP
;
674 // Create the tree control.
675 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
678 #if wxUSE_COMCTL32_SAFELY
679 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
680 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
682 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
683 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
685 // This works around a bug in the Windows tree control whereby for some versions
686 // of comctrl32, setting any colour actually draws the background in black.
687 // This will initialise the background to the system colour.
688 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
689 // Assume the user has an updated comctl32.dll.
690 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
691 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
692 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
696 // VZ: this is some experimental code which may be used to get the
697 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
698 // AFAIK, the standard DLL does about the same thing anyhow.
700 if ( m_windowStyle
& wxTR_MULTIPLE
)
704 // create the DC compatible with the current screen
705 HDC hdcMem
= CreateCompatibleDC(NULL
);
707 // create a mono bitmap of the standard size
708 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
709 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
710 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
711 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
712 1, // # of color planes
713 1, // # bits needed for one pixel
714 0); // array containing colour data
715 SelectObject(hdcMem
, hbmpCheck
);
717 // then draw a check mark into it
718 RECT rect
= { 0, 0, x
, y
};
719 if ( !::DrawFrameControl(hdcMem
, &rect
,
721 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
723 wxLogLastError(wxT("DrawFrameControl(check)"));
726 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
727 imagelistCheckboxes
.Add(bmp
);
729 if ( !::DrawFrameControl(hdcMem
, &rect
,
733 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
736 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
737 imagelistCheckboxes
.Add(bmp
);
743 SetStateImageList(&imagelistCheckboxes
);
747 wxSetCCUnicodeFormat(GetHwnd());
752 wxTreeCtrl::~wxTreeCtrl()
754 // delete any attributes
757 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
759 // prevent TVN_DELETEITEM handler from deleting the attributes again!
760 m_hasAnyAttr
= false;
765 // delete user data to prevent memory leaks
766 // also deletes hidden root node storage.
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
774 /* static */ wxVisualAttributes
775 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
777 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
779 // common controls have their own default font
780 attrs
.font
= wxGetCCDefaultFont();
786 // simple wrappers which add error checking in debug mode
788 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
790 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
791 _T("can't retrieve virtual root item") );
793 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
795 wxLogLastError(wxT("TreeView_GetItem"));
803 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
805 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
807 wxLogLastError(wxT("TreeView_SetItem"));
811 unsigned int wxTreeCtrl::GetCount() const
813 return (unsigned int)TreeView_GetCount(GetHwnd());
816 unsigned int wxTreeCtrl::GetIndent() const
818 return TreeView_GetIndent(GetHwnd());
821 void wxTreeCtrl::SetIndent(unsigned int indent
)
823 TreeView_SetIndent(GetHwnd(), indent
);
826 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
829 (void) TreeView_SetImageList(GetHwnd(),
830 imageList
? imageList
->GetHIMAGELIST() : 0,
834 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
836 if (m_ownsImageListNormal
)
837 delete m_imageListNormal
;
839 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
840 m_ownsImageListNormal
= false;
843 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
845 if (m_ownsImageListState
) delete m_imageListState
;
846 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
847 m_ownsImageListState
= false;
850 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
851 bool recursively
) const
853 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
855 TraverseCounter
counter(this, item
, recursively
);
856 return counter
.GetCount() - 1;
859 // ----------------------------------------------------------------------------
861 // ----------------------------------------------------------------------------
863 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
865 #if !wxUSE_COMCTL32_SAFELY
866 if ( !wxWindowBase::SetBackgroundColour(colour
) )
869 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
875 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
877 #if !wxUSE_COMCTL32_SAFELY
878 if ( !wxWindowBase::SetForegroundColour(colour
) )
881 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
887 // ----------------------------------------------------------------------------
889 // ----------------------------------------------------------------------------
891 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
893 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
896 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
898 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
900 wxChar buf
[512]; // the size is arbitrary...
902 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
903 tvItem
.pszText
= buf
;
904 tvItem
.cchTextMax
= WXSIZEOF(buf
);
905 if ( !DoGetItem(&tvItem
) )
907 // don't return some garbage which was on stack, but an empty string
911 return wxString(buf
);
914 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
916 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
918 if ( IS_VIRTUAL_ROOT(item
) )
921 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
922 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
925 // when setting the text of the item being edited, the text control should
926 // be updated to reflect the new text as well, otherwise calling
927 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
929 // don't use GetEditControl() here because m_textCtrl is not set yet
930 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
933 if ( item
== m_idEdited
)
935 ::SetWindowText(hwndEdit
, text
);
940 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
941 wxTreeItemIcon which
) const
943 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
945 if ( IsHiddenRoot(item
) )
947 // no images for hidden root item
951 wxTreeItemParam
*param
= GetItemParam(item
);
953 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
956 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
957 wxTreeItemIcon which
)
959 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
960 wxCHECK_RET( which
>= 0 &&
961 which
< wxTreeItemIcon_Max
,
962 wxT("invalid image index"));
965 if ( IsHiddenRoot(item
) )
967 // no images for hidden root item
971 wxTreeItemParam
*data
= GetItemParam(item
);
975 data
->SetImage(image
, which
);
980 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
982 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
984 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
986 // hidden root may still have data.
987 if ( IS_VIRTUAL_ROOT(item
) )
989 return GET_VIRTUAL_ROOT()->GetParam();
993 if ( !DoGetItem(&tvItem
) )
998 return (wxTreeItemParam
*)tvItem
.lParam
;
1001 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1003 wxTreeItemParam
*data
= GetItemParam(item
);
1005 return data
? data
->GetData() : NULL
;
1008 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1010 // first, associate this piece of data with this item
1016 wxTreeItemParam
*param
= GetItemParam(item
);
1018 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1020 param
->SetData(data
);
1023 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1025 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1027 if ( IS_VIRTUAL_ROOT(item
) )
1030 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1031 tvItem
.cChildren
= (int)has
;
1035 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1037 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1039 if ( IS_VIRTUAL_ROOT(item
) )
1042 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1043 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1047 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1049 if ( IS_VIRTUAL_ROOT(item
) )
1052 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1053 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1057 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1059 if ( IS_VIRTUAL_ROOT(item
) )
1063 if ( GetBoundingRect(item
, rect
) )
1069 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1071 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1073 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1074 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1077 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1079 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1081 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1082 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1085 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1087 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1089 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1090 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1093 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1094 const wxColour
& col
)
1096 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1098 wxTreeItemAttr
*attr
;
1099 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1100 if ( it
== m_attrs
.end() )
1102 m_hasAnyAttr
= true;
1104 m_attrs
[item
.m_pItem
] =
1105 attr
= new wxTreeItemAttr
;
1112 attr
->SetTextColour(col
);
1117 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1118 const wxColour
& col
)
1120 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1122 wxTreeItemAttr
*attr
;
1123 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1124 if ( it
== m_attrs
.end() )
1126 m_hasAnyAttr
= true;
1128 m_attrs
[item
.m_pItem
] =
1129 attr
= new wxTreeItemAttr
;
1131 else // already in the hash
1136 attr
->SetBackgroundColour(col
);
1141 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1143 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1145 wxTreeItemAttr
*attr
;
1146 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1147 if ( it
== m_attrs
.end() )
1149 m_hasAnyAttr
= true;
1151 m_attrs
[item
.m_pItem
] =
1152 attr
= new wxTreeItemAttr
;
1154 else // already in the hash
1159 attr
->SetFont(font
);
1161 // Reset the item's text to ensure that the bounding rect will be adjusted
1162 // for the new font.
1163 SetItemText(item
, GetItemText(item
));
1168 // ----------------------------------------------------------------------------
1170 // ----------------------------------------------------------------------------
1172 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1174 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1176 if ( item
== wxTreeItemId(TVI_ROOT
) )
1178 // virtual (hidden) root is never visible
1182 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1185 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1186 // the HTREEITEM with TVM_GETITEMRECT
1187 *(HTREEITEM
*)&rect
= HITEM(item
);
1189 // true means to get rect for just the text, not the whole line
1190 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1192 // if TVM_GETITEMRECT returned false, then the item is definitely not
1193 // visible (because its parent is not expanded)
1197 // however if it returned true, the item might still be outside the
1198 // currently visible part of the tree, test for it (notice that partly
1199 // visible means visible here)
1200 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1203 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1205 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1207 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1210 return tvItem
.cChildren
!= 0;
1213 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1215 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1217 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1220 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1223 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1225 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1227 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1230 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1233 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1235 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1237 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1240 return (tvItem
.state
& TVIS_BOLD
) != 0;
1243 // ----------------------------------------------------------------------------
1245 // ----------------------------------------------------------------------------
1247 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1249 // Root may be real (visible) or virtual (hidden).
1250 if ( GET_VIRTUAL_ROOT() )
1253 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1256 wxTreeItemId
wxTreeCtrl::GetSelection() const
1258 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1259 wxT("this only works with single selection controls") );
1261 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1264 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1266 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1270 if ( IS_VIRTUAL_ROOT(item
) )
1272 // no parent for the virtual root
1277 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1278 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1280 // the top level items should have the virtual root as their parent
1285 return wxTreeItemId(hItem
);
1288 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1289 wxTreeItemIdValue
& cookie
) const
1291 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1293 // remember the last child returned in 'cookie'
1294 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1296 return wxTreeItemId(cookie
);
1299 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1300 wxTreeItemIdValue
& cookie
) const
1302 wxTreeItemId
fromCookie(cookie
);
1304 HTREEITEM hitem
= HITEM(fromCookie
);
1306 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1308 wxTreeItemId
item(hitem
);
1310 cookie
= item
.m_pItem
;
1315 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1317 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1319 // can this be done more efficiently?
1320 wxTreeItemIdValue cookie
;
1322 wxTreeItemId childLast
,
1323 child
= GetFirstChild(item
, cookie
);
1324 while ( child
.IsOk() )
1327 child
= GetNextChild(item
, cookie
);
1333 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1335 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1336 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1339 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1341 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1342 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1345 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1347 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1350 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1352 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1353 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1355 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1358 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1360 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1361 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1363 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1366 // ----------------------------------------------------------------------------
1367 // multiple selections emulation
1368 // ----------------------------------------------------------------------------
1370 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1372 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1374 // receive the desired information.
1375 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1378 // state image indices are 1 based
1379 return ((tvItem
.state
>> 12) - 1) == 1;
1382 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1384 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1386 // receive the desired information.
1387 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1391 // state images are one-based
1392 tvItem
.state
= (check
? 2 : 1) << 12;
1397 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1399 TraverseSelections
selector(this, selections
);
1401 return selector
.GetCount();
1404 // ----------------------------------------------------------------------------
1406 // ----------------------------------------------------------------------------
1408 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1409 const wxTreeItemId
& hInsertAfter
,
1410 const wxString
& text
,
1411 int image
, int selectedImage
,
1412 wxTreeItemData
*data
)
1414 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1416 _T("can't have more than one root in the tree") );
1418 TV_INSERTSTRUCT tvIns
;
1419 tvIns
.hParent
= HITEM(parent
);
1420 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1422 // this is how we insert the item as the first child: supply a NULL
1424 if ( !tvIns
.hInsertAfter
)
1426 tvIns
.hInsertAfter
= TVI_FIRST
;
1430 if ( !text
.empty() )
1433 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1437 tvIns
.item
.pszText
= NULL
;
1438 tvIns
.item
.cchTextMax
= 0;
1441 // create the param which will store the other item parameters
1442 wxTreeItemParam
*param
= new wxTreeItemParam
;
1444 // we return the images on demand as they depend on whether the item is
1445 // expanded or collapsed too in our case
1446 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1447 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1448 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1450 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1451 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1454 tvIns
.item
.lParam
= (LPARAM
)param
;
1455 tvIns
.item
.mask
= mask
;
1457 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1460 wxLogLastError(wxT("TreeView_InsertItem"));
1463 // associate the application tree item with Win32 tree item handle
1466 // setup wxTreeItemData
1469 param
->SetData(data
);
1473 return wxTreeItemId(id
);
1476 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1477 int image
, int selectedImage
,
1478 wxTreeItemData
*data
)
1480 if ( HasFlag(wxTR_HIDE_ROOT
) )
1482 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1484 // create a virtual root item, the parent for all the others
1485 wxTreeItemParam
*param
= new wxTreeItemParam
;
1486 param
->SetData(data
);
1488 m_pVirtualRoot
= new wxVirtualNode(param
);
1493 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1494 text
, image
, selectedImage
, data
);
1497 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1499 const wxString
& text
,
1500 int image
, int selectedImage
,
1501 wxTreeItemData
*data
)
1503 wxTreeItemId idPrev
;
1504 if ( index
== (size_t)-1 )
1506 // special value: append to the end
1509 else // find the item from index
1511 wxTreeItemIdValue cookie
;
1512 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1513 while ( index
!= 0 && idCur
.IsOk() )
1518 idCur
= GetNextChild(parent
, cookie
);
1521 // assert, not check: if the index is invalid, we will append the item
1523 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1526 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1529 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1531 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1533 wxLogLastError(wxT("TreeView_DeleteItem"));
1537 // delete all children (but don't delete the item itself)
1538 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1540 wxTreeItemIdValue cookie
;
1542 wxArrayTreeItemIds children
;
1543 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1544 while ( child
.IsOk() )
1546 children
.Add(child
);
1548 child
= GetNextChild(item
, cookie
);
1551 size_t nCount
= children
.Count();
1552 for ( size_t n
= 0; n
< nCount
; n
++ )
1554 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1556 wxLogLastError(wxT("TreeView_DeleteItem"));
1561 void wxTreeCtrl::DeleteAllItems()
1563 // delete the "virtual" root item.
1564 if ( GET_VIRTUAL_ROOT() )
1566 delete GET_VIRTUAL_ROOT();
1567 m_pVirtualRoot
= NULL
;
1570 // and all the real items
1572 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1574 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1578 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1580 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1581 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1582 flag
== TVE_EXPAND
||
1584 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1586 // A hidden root can be neither expanded nor collapsed.
1587 wxCHECK_RET( !IsHiddenRoot(item
),
1588 wxT("Can't expand/collapse hidden root node!") );
1590 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1591 // emulate them. This behaviour has changed slightly with comctl32.dll
1592 // v 4.70 - now it does send them but only the first time. To maintain
1593 // compatible behaviour and also in order to not have surprises with the
1594 // future versions, don't rely on this and still do everything ourselves.
1595 // To avoid that the messages be sent twice when the item is expanded for
1596 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1598 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1602 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1604 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1606 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1610 (void)GetEventHandler()->ProcessEvent(event
);
1612 //else: change didn't took place, so do nothing at all
1615 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1617 DoExpand(item
, TVE_EXPAND
);
1620 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1622 DoExpand(item
, TVE_COLLAPSE
);
1625 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1627 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1630 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1632 DoExpand(item
, TVE_TOGGLE
);
1635 void wxTreeCtrl::Unselect()
1637 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1638 wxT("doesn't make sense, may be you want UnselectAll()?") );
1640 // just remove the selection
1641 SelectItem(wxTreeItemId());
1644 void wxTreeCtrl::UnselectAll()
1646 if ( m_windowStyle
& wxTR_MULTIPLE
)
1648 wxArrayTreeItemIds selections
;
1649 size_t count
= GetSelections(selections
);
1650 for ( size_t n
= 0; n
< count
; n
++ )
1652 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1655 m_htSelStart
.Unset();
1659 // just remove the selection
1664 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1666 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1668 wxASSERT_MSG( select
|| HasFlag(wxTR_MULTIPLE
),
1669 _T("SelectItem(false) works only for multiselect") );
1671 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1672 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1674 if ( HasFlag(wxTR_MULTIPLE
) )
1676 if ( !::SelectItem(GetHwnd(), HITEM(item
), select
) )
1678 wxLogLastError(wxT("TreeView_SelectItem"));
1682 else // single selection
1684 // use TreeView_SelectItem() to deselect the previous selection
1685 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1687 wxLogLastError(wxT("TreeView_SelectItem"));
1692 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1693 (void)GetEventHandler()->ProcessEvent(event
);
1695 //else: program vetoed the change
1698 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1700 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1703 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1706 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1708 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1710 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1714 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1719 void wxTreeCtrl::DeleteTextCtrl()
1723 // the HWND corresponding to this control is deleted by the tree
1724 // control itself and we don't know when exactly this happens, so check
1725 // if the window still exists before calling UnsubclassWin()
1726 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1728 m_textCtrl
->SetHWND(0);
1731 m_textCtrl
->UnsubclassWin();
1732 m_textCtrl
->SetHWND(0);
1740 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1741 wxClassInfo
*textControlClass
)
1743 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1748 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1749 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1751 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1760 // textctrl is subclassed in MSWOnNotify
1764 // End label editing, optionally cancelling the edit
1765 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1767 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1772 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1774 TV_HITTESTINFO hitTestInfo
;
1775 hitTestInfo
.pt
.x
= (int)point
.x
;
1776 hitTestInfo
.pt
.y
= (int)point
.y
;
1778 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1783 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1784 flags |= wxTREE_HITTEST_##flag
1786 TRANSLATE_FLAG(ABOVE
);
1787 TRANSLATE_FLAG(BELOW
);
1788 TRANSLATE_FLAG(NOWHERE
);
1789 TRANSLATE_FLAG(ONITEMBUTTON
);
1790 TRANSLATE_FLAG(ONITEMICON
);
1791 TRANSLATE_FLAG(ONITEMINDENT
);
1792 TRANSLATE_FLAG(ONITEMLABEL
);
1793 TRANSLATE_FLAG(ONITEMRIGHT
);
1794 TRANSLATE_FLAG(ONITEMSTATEICON
);
1795 TRANSLATE_FLAG(TOLEFT
);
1796 TRANSLATE_FLAG(TORIGHT
);
1798 #undef TRANSLATE_FLAG
1800 return wxTreeItemId(hitTestInfo
.hItem
);
1803 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1805 bool textOnly
) const
1809 // Virtual root items have no bounding rectangle
1810 if ( IS_VIRTUAL_ROOT(item
) )
1815 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1818 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1824 // couldn't retrieve rect: for example, item isn't visible
1829 // ----------------------------------------------------------------------------
1831 // ----------------------------------------------------------------------------
1833 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1834 // functions such as IsDataIndirect()
1835 class wxTreeSortHelper
1838 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1841 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1843 return ((wxTreeItemParam
*)lParam
)->GetItem();
1847 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1851 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1852 wxT("sorting tree without data doesn't make sense") );
1854 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1856 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1857 GetIdFromData(pItem2
));
1860 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1862 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1864 // rely on the fact that TreeView_SortChildren does the same thing as our
1865 // default behaviour, i.e. sorts items alphabetically and so call it
1866 // directly if we're not in derived class (much more efficient!)
1867 // RN: Note that if you find you're code doesn't sort as expected this
1868 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1869 // combo for your derived wxTreeCtrl if will sort without
1871 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1873 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1878 tvSort
.hParent
= HITEM(item
);
1879 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1880 tvSort
.lParam
= (LPARAM
)this;
1881 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1885 // ----------------------------------------------------------------------------
1887 // ----------------------------------------------------------------------------
1889 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1891 if ( msg
->message
== WM_KEYDOWN
)
1893 if ( msg
->wParam
== VK_RETURN
)
1895 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1900 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
1903 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1905 if ( cmd
== EN_UPDATE
)
1907 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1908 event
.SetEventObject( this );
1909 ProcessCommand(event
);
1911 else if ( cmd
== EN_KILLFOCUS
)
1913 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1914 event
.SetEventObject( this );
1915 ProcessCommand(event
);
1923 // command processed
1927 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1928 // only do it during dragging, minimize wxWin overhead (this is important for
1929 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1930 // instead of passing by wxWin events
1931 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1933 bool processed
= false;
1935 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
1937 // This message is sent after a right-click, or when the "menu" key is pressed
1938 if ( nMsg
== WM_CONTEXTMENU
)
1940 int x
= GET_X_LPARAM(lParam
),
1941 y
= GET_Y_LPARAM(lParam
);
1943 // the item for which the menu should be shown
1946 // the position where the menu should be shown in client coordinates
1947 // (so that it can be passed directly to PopupMenu())
1950 if ( x
== -1 || y
== -1 )
1952 // this means that the event was generated from keyboard (e.g. with
1953 // Shift-F10 or special Windows menu key)
1955 // use the Explorer standard of putting the menu at the left edge
1956 // of the text, in the vertical middle of the text
1957 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1960 // Use the bounding rectangle of only the text part
1962 GetBoundingRect(item
, rect
, true);
1963 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
1966 else // event from mouse, use mouse position
1968 pt
= ScreenToClient(wxPoint(x
, y
));
1970 TV_HITTESTINFO tvhti
;
1973 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
1974 item
= wxTreeItemId(tvhti
.hItem
);
1978 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
1980 event
.m_pointDrag
= pt
;
1982 if ( GetEventHandler()->ProcessEvent(event
) )
1984 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
1986 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1988 // we only process mouse messages here and these parameters have the
1989 // same meaning for all of them
1990 int x
= GET_X_LPARAM(lParam
),
1991 y
= GET_Y_LPARAM(lParam
);
1993 TV_HITTESTINFO tvht
;
1997 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2001 case WM_LBUTTONDOWN
:
2002 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2004 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2005 m_ptClick
= wxPoint(x
, y
);
2007 if ( wParam
& MK_CONTROL
)
2011 // toggle selected state
2012 ToggleItemSelection(htItem
);
2014 ::SetFocus(GetHwnd(), htItem
);
2016 // reset on any click without Shift
2017 m_htSelStart
.Unset();
2021 else if ( wParam
& MK_SHIFT
)
2023 // this selects all items between the starting one and
2026 if ( !m_htSelStart
)
2028 // take the focused item
2029 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2033 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2034 !(wParam
& MK_CONTROL
));
2036 ::SelectItem(GetHwnd(), htItem
);
2038 ::SetFocus(GetHwnd(), htItem
);
2042 else // normal click
2044 // avoid doing anything if we click on the only
2045 // currently selected item
2049 wxArrayTreeItemIds selections
;
2050 size_t count
= GetSelections(selections
);
2053 HITEM(selections
[0]) != htItem
)
2055 // clear the previously selected items, if the
2056 // user clicked outside of the present selection.
2057 // otherwise, perform the deselection on mouse-up.
2058 // this allows multiple drag and drop to work.
2060 if (!IsItemSelected(GetHwnd(), htItem
))
2064 // prevent the click from starting in-place editing
2065 // which should only happen if we click on the
2066 // already selected item (and nothing else is
2069 TreeView_SelectItem(GetHwnd(), 0);
2070 ::SelectItem(GetHwnd(), htItem
);
2072 ::SetFocus(GetHwnd(), htItem
);
2075 else // click on a single selected item
2077 // don't interfere with the default processing in
2078 // WM_MOUSEMOVE handler below as the default window
2079 // proc will start the drag itself if we let have
2081 m_htClickedItem
.Unset();
2084 // reset on any click without Shift
2085 m_htSelStart
.Unset();
2092 if ( m_htClickedItem
)
2094 int cx
= abs(m_ptClick
.x
- x
);
2095 int cy
= abs(m_ptClick
.y
- y
);
2097 if ( cx
> ::GetSystemMetrics(SM_CXDRAG
) ||
2098 cy
> ::GetSystemMetrics(SM_CYDRAG
) )
2103 tv
.hdr
.hwndFrom
= GetHwnd();
2104 tv
.hdr
.idFrom
= ::GetWindowLong(GetHwnd(), GWL_ID
);
2105 tv
.hdr
.code
= TVN_BEGINDRAG
;
2107 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2111 wxZeroMemory(tviAux
);
2113 tviAux
.hItem
= HITEM(m_htClickedItem
);
2114 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2115 tviAux
.stateMask
= 0xffffffff;
2116 TreeView_GetItem(GetHwnd(), &tviAux
);
2118 tv
.itemNew
.state
= tviAux
.state
;
2119 tv
.itemNew
.lParam
= tviAux
.lParam
;
2124 // do it before SendMessage() call below to avoid
2125 // reentrancies here if there is another WM_MOUSEMOVE
2126 // in the queue already
2127 m_htClickedItem
.Unset();
2129 ::SendMessage(GetHwndOf(GetParent()), WM_NOTIFY
,
2130 tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2132 // don't pass it to the default window proc, it would
2133 // start dragging again
2137 #endif // __WXWINCE__
2141 m_dragImage
->Move(wxPoint(x
, y
));
2144 // highlight the item as target (hiding drag image is
2145 // necessary - otherwise the display will be corrupted)
2146 m_dragImage
->Hide();
2147 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2148 m_dragImage
->Show();
2155 // facilitates multiple drag-and-drop
2156 if (htItem
&& isMultiple
)
2158 wxArrayTreeItemIds selections
;
2159 size_t count
= GetSelections(selections
);
2162 !(wParam
& MK_CONTROL
) &&
2163 !(wParam
& MK_SHIFT
))
2166 TreeView_SelectItem(GetHwnd(), htItem
);
2167 ::SelectItem(GetHwnd(), htItem
);
2168 ::SetFocus(GetHwnd(), htItem
);
2170 m_htClickedItem
.Unset();
2178 m_dragImage
->EndDrag();
2182 // generate the drag end event
2183 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2184 event
.m_pointDrag
= wxPoint(x
, y
);
2186 (void)GetEventHandler()->ProcessEvent(event
);
2188 // if we don't do it, the tree seems to think that 2 items
2189 // are selected simultaneously which is quite weird
2190 TreeView_SelectDropTarget(GetHwnd(), 0);
2195 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2197 // the tree control greys out the selected item when it loses focus and
2198 // paints it as selected again when it regains it, but it won't do it
2199 // for the other items itself - help it
2200 wxArrayTreeItemIds selections
;
2201 size_t count
= GetSelections(selections
);
2203 for ( size_t n
= 0; n
< count
; n
++ )
2205 // TreeView_GetItemRect() will return false if item is not visible,
2206 // which may happen perfectly well
2207 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2210 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2214 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2216 bool bCtrl
= wxIsCtrlDown(),
2217 bShift
= wxIsShiftDown();
2219 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2225 ToggleItemSelection(htSel
);
2231 ::SelectItem(GetHwnd(), htSel
);
2239 if ( !bCtrl
&& !bShift
)
2241 // no modifiers, just clear selection and then let the default
2242 // processing to take place
2247 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2249 HTREEITEM htNext
= (HTREEITEM
)
2250 TreeView_GetNextItem
2254 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2260 // at the top/bottom
2266 if ( !m_htSelStart
)
2267 m_htSelStart
= htSel
;
2269 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2273 // without changing selection
2274 ::SetFocus(GetHwnd(), htNext
);
2285 // TODO: handle Shift/Ctrl with these keys
2286 if ( !bCtrl
&& !bShift
)
2290 m_htSelStart
.Unset();
2294 else if ( nMsg
== WM_COMMAND
)
2296 // if we receive a EN_KILLFOCUS command from the in-place edit control
2297 // used for label editing, make sure to end editing
2300 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2302 if ( cmd
== EN_KILLFOCUS
)
2304 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2314 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2320 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2322 if ( nMsg
== WM_CHAR
)
2324 // don't let the control process Space and Return keys because it
2325 // doesn't do anything useful with them anyhow but always beeps
2326 // annoyingly when it receives them and there is no way to turn it off
2327 // simply if you just process TREEITEM_ACTIVATED event to which Space
2328 // and Enter presses are mapped in your code
2329 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2333 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2336 // process WM_NOTIFY Windows message
2337 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2339 wxTreeEvent
event(wxEVT_NULL
, this);
2340 wxEventType eventType
= wxEVT_NULL
;
2341 NMHDR
*hdr
= (NMHDR
*)lParam
;
2343 switch ( hdr
->code
)
2346 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2349 case TVN_BEGINRDRAG
:
2351 if ( eventType
== wxEVT_NULL
)
2352 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2353 //else: left drag, already set above
2355 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2357 event
.m_item
= tv
->itemNew
.hItem
;
2358 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2360 // don't allow dragging by default: the user code must
2361 // explicitly say that it wants to allow it to avoid breaking
2367 case TVN_BEGINLABELEDIT
:
2369 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2370 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2372 // although the user event handler may still veto it, it is
2373 // important to set it now so that calls to SetItemText() from
2374 // the event handler would change the text controls contents
2376 event
.m_item
= info
->item
.hItem
;
2377 event
.m_label
= info
->item
.pszText
;
2378 event
.m_editCancelled
= false;
2382 case TVN_DELETEITEM
:
2384 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2385 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2387 event
.m_item
= tv
->itemOld
.hItem
;
2391 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2392 if ( it
!= m_attrs
.end() )
2401 case TVN_ENDLABELEDIT
:
2403 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2404 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2406 event
.m_item
= info
->item
.hItem
;
2407 event
.m_label
= info
->item
.pszText
;
2408 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2413 // These *must* not be removed or TVN_GETINFOTIP will
2414 // not be processed each time the mouse is moved
2415 // and the tooltip will only ever update once.
2424 #ifdef TVN_GETINFOTIP
2425 case TVN_GETINFOTIP
:
2427 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2428 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2430 // Which item are we trying to get a tooltip for?
2431 event
.m_item
= info
->hItem
;
2435 #endif // TVN_GETINFOTIP
2436 #endif // !__WXWINCE__
2438 case TVN_GETDISPINFO
:
2439 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2442 case TVN_SETDISPINFO
:
2444 if ( eventType
== wxEVT_NULL
)
2445 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2446 //else: get, already set above
2448 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2450 event
.m_item
= info
->item
.hItem
;
2454 case TVN_ITEMEXPANDING
:
2455 case TVN_ITEMEXPANDED
:
2457 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2460 switch ( tv
->action
)
2463 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2471 what
= IDX_COLLAPSE
;
2475 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2478 eventType
= gs_expandEvents
[what
][how
];
2480 event
.m_item
= tv
->itemNew
.hItem
;
2486 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2487 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2489 // fabricate the lParam and wParam parameters sufficiently
2490 // similar to the ones from a "real" WM_KEYDOWN so that
2491 // CreateKeyEvent() works correctly
2492 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2493 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2495 WXWPARAM wParam
= info
->wVKey
;
2497 int keyCode
= wxCharCodeMSWToWX(wParam
);
2500 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2505 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2510 // a separate event for Space/Return
2511 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2512 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2515 if ( !HasFlag(wxTR_MULTIPLE
) )
2516 item
= GetSelection();
2518 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2520 (void)GetEventHandler()->ProcessEvent(event2
);
2525 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2526 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2527 // we have to handle both messages:
2528 case TVN_SELCHANGEDA
:
2529 case TVN_SELCHANGEDW
:
2530 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2533 case TVN_SELCHANGINGA
:
2534 case TVN_SELCHANGINGW
:
2536 if ( eventType
== wxEVT_NULL
)
2537 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2538 //else: already set above
2540 if (hdr
->code
== TVN_SELCHANGINGW
||
2541 hdr
->code
== TVN_SELCHANGEDW
)
2543 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2544 event
.m_item
= tv
->itemNew
.hItem
;
2545 event
.m_itemOld
= tv
->itemOld
.hItem
;
2549 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2550 event
.m_item
= tv
->itemNew
.hItem
;
2551 event
.m_itemOld
= tv
->itemOld
.hItem
;
2556 // instead of explicitly checking for _WIN32_IE, check if the
2557 // required symbols are available in the headers
2558 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2561 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2562 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2563 switch ( nmcd
.dwDrawStage
)
2566 // if we've got any items with non standard attributes,
2567 // notify us before painting each item
2568 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2572 case CDDS_ITEMPREPAINT
:
2574 wxMapTreeAttr::iterator
2575 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2577 if ( it
== m_attrs
.end() )
2579 // nothing to do for this item
2580 *result
= CDRF_DODEFAULT
;
2584 wxTreeItemAttr
* const attr
= it
->second
;
2586 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2587 TVIF_STATE
, TVIS_DROPHILITED
);
2589 const UINT tvItemState
= tvItem
.state
;
2591 // selection colours should override ours,
2592 // otherwise it is too confusing to the user
2593 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2594 !(tvItemState
& TVIS_DROPHILITED
) )
2597 if ( attr
->HasBackgroundColour() )
2599 colBack
= attr
->GetBackgroundColour();
2600 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2604 // but we still want to keep the special foreground
2605 // colour when we don't have focus (we can't keep
2606 // it when we do, it would usually be unreadable on
2607 // the almost inverted bg colour...)
2608 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2609 FindFocus() != this ) &&
2610 !(tvItemState
& TVIS_DROPHILITED
) )
2613 if ( attr
->HasTextColour() )
2615 colText
= attr
->GetTextColour();
2616 lptvcd
->clrText
= wxColourToRGB(colText
);
2620 if ( attr
->HasFont() )
2622 HFONT hFont
= GetHfontOf(attr
->GetFont());
2624 ::SelectObject(nmcd
.hdc
, hFont
);
2626 *result
= CDRF_NEWFONT
;
2628 else // no specific font
2630 *result
= CDRF_DODEFAULT
;
2636 *result
= CDRF_DODEFAULT
;
2640 // we always process it
2642 #endif // have owner drawn support in headers
2646 DWORD pos
= GetMessagePos();
2648 point
.x
= LOWORD(pos
);
2649 point
.y
= HIWORD(pos
);
2650 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2652 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2653 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2655 event
.m_item
= item
;
2656 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2664 TV_HITTESTINFO tvhti
;
2665 ::GetCursorPos(&tvhti
.pt
);
2666 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2667 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2669 if ( tvhti
.flags
& TVHT_ONITEM
)
2671 event
.m_item
= tvhti
.hItem
;
2672 eventType
= (int)hdr
->code
== NM_DBLCLK
2673 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2674 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2676 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2677 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2686 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2689 event
.SetEventType(eventType
);
2691 if ( event
.m_item
.IsOk() )
2692 event
.SetClientObject(GetItemData(event
.m_item
));
2694 bool processed
= GetEventHandler()->ProcessEvent(event
);
2697 switch ( hdr
->code
)
2700 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2701 // the return code of this event handler as the return value for
2702 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2703 // expanded status would never work
2708 // prevent tree control from sending WM_CONTEXTMENU to our parent
2709 // (which it does if NM_RCLICK is not handled) because we want to
2710 // send it to the control itself
2714 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
2715 (WPARAM
)GetHwnd(), ::GetMessagePos());
2719 case TVN_BEGINRDRAG
:
2720 if ( event
.IsAllowed() )
2722 // normally this is impossible because the m_dragImage is
2723 // deleted once the drag operation is over
2724 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2726 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2727 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2728 m_dragImage
->Show();
2732 case TVN_DELETEITEM
:
2734 // NB: we might process this message using wxWidgets event
2735 // tables, but due to overhead of wxWin event system we
2736 // prefer to do it here ourself (otherwise deleting a tree
2737 // with many items is just too slow)
2738 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2740 wxTreeItemParam
*param
=
2741 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2744 processed
= true; // Make sure we don't get called twice
2748 case TVN_BEGINLABELEDIT
:
2749 // return true to cancel label editing
2750 *result
= !event
.IsAllowed();
2752 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2753 if ( event
.IsAllowed() )
2755 HWND hText
= TreeView_GetEditControl(GetHwnd());
2758 // MBN: if m_textCtrl already has an HWND, it is a stale
2759 // pointer from a previous edit (because the user
2760 // didn't modify the label before dismissing the control,
2761 // and TVN_ENDLABELEDIT was not sent), so delete it
2762 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2765 m_textCtrl
= new wxTextCtrl();
2766 m_textCtrl
->SetParent(this);
2767 m_textCtrl
->SetHWND((WXHWND
)hText
);
2768 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2770 // set wxTE_PROCESS_ENTER style for the text control to
2771 // force it to process the Enter presses itself, otherwise
2772 // they could be stolen from it by the dialog
2774 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2775 | wxTE_PROCESS_ENTER
);
2778 else // we had set m_idEdited before
2784 case TVN_ENDLABELEDIT
:
2785 // return true to set the label to the new string: note that we
2786 // also must pretend that we did process the message or it is going
2787 // to be passed to DefWindowProc() which will happily return false
2788 // cancelling the label change
2789 *result
= event
.IsAllowed();
2792 // ensure that we don't have the text ctrl which is going to be
2798 #ifdef TVN_GETINFOTIP
2799 case TVN_GETINFOTIP
:
2801 // If the user permitted a tooltip change, change it
2802 if (event
.IsAllowed())
2804 SetToolTip(event
.m_label
);
2811 case TVN_SELCHANGING
:
2812 case TVN_ITEMEXPANDING
:
2813 // return true to prevent the action from happening
2814 *result
= !event
.IsAllowed();
2817 case TVN_ITEMEXPANDED
:
2818 // the item is not refreshed properly after expansion when it has
2819 // an image depending on the expanded/collapsed state - bug in
2820 // comctl32.dll or our code?
2822 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2823 wxTreeItemId
id(tv
->itemNew
.hItem
);
2825 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2833 case TVN_GETDISPINFO
:
2834 // NB: so far the user can't set the image himself anyhow, so do it
2835 // anyway - but this may change later
2836 //if ( /* !processed && */ )
2838 wxTreeItemId item
= event
.m_item
;
2839 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2841 const wxTreeItemParam
* const param
= GetItemParam(item
);
2845 if ( info
->item
.mask
& TVIF_IMAGE
)
2850 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2851 : wxTreeItemIcon_Normal
2854 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2856 info
->item
.iSelectedImage
=
2859 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2860 : wxTreeItemIcon_Selected
2867 // for the other messages the return value is ignored and there is
2868 // nothing special to do
2873 // ----------------------------------------------------------------------------
2875 // ----------------------------------------------------------------------------
2877 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2878 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2880 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2883 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2884 tvi
.mask
= TVIF_STATE
;
2885 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2887 // Select the specified state, or -1 == cycle to the next one.
2890 TreeView_GetItem(GetHwnd(), &tvi
);
2892 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2893 if ( state
== m_imageListState
->GetImageCount() )
2897 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2898 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2900 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2902 TreeView_SetItem(GetHwnd(), &tvi
);
2905 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2908 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2909 tvi
.mask
= TVIF_STATE
;
2910 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2911 TreeView_GetItem(GetHwnd(), &tvi
);
2913 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2916 #endif // wxUSE_TREECTRL