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 wxFLAGS_MEMBER(wxTR_EXTENDED
)
524 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
526 wxEND_FLAGS( wxTreeCtrlStyle
)
528 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
530 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
531 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
532 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
533 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
534 wxEND_PROPERTIES_TABLE()
536 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
537 wxEND_HANDLERS_TABLE()
539 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
541 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 // indices in gs_expandEvents table below
563 // handy table for sending events - it has to be initialized during run-time
564 // now so can't be const any more
565 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
568 but logically it's a const table with the following entries:
571 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
572 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
576 // ============================================================================
578 // ============================================================================
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
586 if ( !OnVisit(root
) )
589 return Traverse(root
, recursively
);
592 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
594 wxTreeItemIdValue cookie
;
595 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
596 while ( child
.IsOk() )
598 // depth first traversal
599 if ( recursively
&& !Traverse(child
, true) )
602 if ( !OnVisit(child
) )
605 child
= m_tree
->GetNextChild(root
, cookie
);
611 // ----------------------------------------------------------------------------
612 // construction and destruction
613 // ----------------------------------------------------------------------------
615 void wxTreeCtrl::Init()
618 m_hasAnyAttr
= false;
620 m_pVirtualRoot
= NULL
;
622 // initialize the global array of events now as it can't be done statically
623 // with the wxEVT_XXX values being allocated during run-time only
624 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
625 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
626 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
627 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
630 bool wxTreeCtrl::Create(wxWindow
*parent
,
635 const wxValidator
& validator
,
636 const wxString
& name
)
640 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
641 style
|= wxBORDER_SUNKEN
;
643 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
647 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
648 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
650 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
651 wstyle
|= TVS_HASLINES
;
652 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
653 wstyle
|= TVS_HASBUTTONS
;
655 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
656 wstyle
|= TVS_EDITLABELS
;
658 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
659 wstyle
|= TVS_LINESATROOT
;
661 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
663 if ( wxApp::GetComCtl32Version() >= 471 )
664 wstyle
|= TVS_FULLROWSELECT
;
667 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
668 // Need so that TVN_GETINFOTIP messages will be sent
669 wstyle
|= TVS_INFOTIP
;
672 // Create the tree control.
673 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
676 #if wxUSE_COMCTL32_SAFELY
677 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
678 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
680 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
681 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
683 // This works around a bug in the Windows tree control whereby for some versions
684 // of comctrl32, setting any colour actually draws the background in black.
685 // This will initialise the background to the system colour.
686 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
687 // Assume the user has an updated comctl32.dll.
688 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
689 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
690 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
694 // VZ: this is some experimental code which may be used to get the
695 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
696 // AFAIK, the standard DLL does about the same thing anyhow.
698 if ( m_windowStyle
& wxTR_MULTIPLE
)
702 // create the DC compatible with the current screen
703 HDC hdcMem
= CreateCompatibleDC(NULL
);
705 // create a mono bitmap of the standard size
706 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
707 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
708 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
709 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
710 1, // # of color planes
711 1, // # bits needed for one pixel
712 0); // array containing colour data
713 SelectObject(hdcMem
, hbmpCheck
);
715 // then draw a check mark into it
716 RECT rect
= { 0, 0, x
, y
};
717 if ( !::DrawFrameControl(hdcMem
, &rect
,
719 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
721 wxLogLastError(wxT("DrawFrameControl(check)"));
724 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
725 imagelistCheckboxes
.Add(bmp
);
727 if ( !::DrawFrameControl(hdcMem
, &rect
,
731 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
734 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
735 imagelistCheckboxes
.Add(bmp
);
741 SetStateImageList(&imagelistCheckboxes
);
745 wxSetCCUnicodeFormat(GetHwnd());
750 wxTreeCtrl::~wxTreeCtrl()
752 // delete any attributes
755 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
757 // prevent TVN_DELETEITEM handler from deleting the attributes again!
758 m_hasAnyAttr
= false;
763 // delete user data to prevent memory leaks
764 // also deletes hidden root node storage.
768 // ----------------------------------------------------------------------------
770 // ----------------------------------------------------------------------------
772 /* static */ wxVisualAttributes
773 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
775 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
777 // common controls have their own default font
778 attrs
.font
= wxGetCCDefaultFont();
784 // simple wrappers which add error checking in debug mode
786 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
788 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
789 _T("can't retrieve virtual root item") );
791 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
793 wxLogLastError(wxT("TreeView_GetItem"));
801 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
803 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
805 wxLogLastError(wxT("TreeView_SetItem"));
809 unsigned int wxTreeCtrl::GetCount() const
811 return (unsigned int)TreeView_GetCount(GetHwnd());
814 unsigned int wxTreeCtrl::GetIndent() const
816 return TreeView_GetIndent(GetHwnd());
819 void wxTreeCtrl::SetIndent(unsigned int indent
)
821 TreeView_SetIndent(GetHwnd(), indent
);
824 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
827 (void) TreeView_SetImageList(GetHwnd(),
828 imageList
? imageList
->GetHIMAGELIST() : 0,
832 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
834 if (m_ownsImageListNormal
)
835 delete m_imageListNormal
;
837 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
838 m_ownsImageListNormal
= false;
841 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
843 if (m_ownsImageListState
) delete m_imageListState
;
844 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
845 m_ownsImageListState
= false;
848 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
849 bool recursively
) const
851 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
853 TraverseCounter
counter(this, item
, recursively
);
854 return counter
.GetCount() - 1;
857 // ----------------------------------------------------------------------------
859 // ----------------------------------------------------------------------------
861 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
863 #if !wxUSE_COMCTL32_SAFELY
864 if ( !wxWindowBase::SetBackgroundColour(colour
) )
867 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
873 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
875 #if !wxUSE_COMCTL32_SAFELY
876 if ( !wxWindowBase::SetForegroundColour(colour
) )
879 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
885 // ----------------------------------------------------------------------------
887 // ----------------------------------------------------------------------------
889 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
891 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
894 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
896 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
898 wxChar buf
[512]; // the size is arbitrary...
900 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
901 tvItem
.pszText
= buf
;
902 tvItem
.cchTextMax
= WXSIZEOF(buf
);
903 if ( !DoGetItem(&tvItem
) )
905 // don't return some garbage which was on stack, but an empty string
909 return wxString(buf
);
912 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
914 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
916 if ( IS_VIRTUAL_ROOT(item
) )
919 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
920 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
923 // when setting the text of the item being edited, the text control should
924 // be updated to reflect the new text as well, otherwise calling
925 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
927 // don't use GetEditControl() here because m_textCtrl is not set yet
928 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
931 if ( item
== m_idEdited
)
933 ::SetWindowText(hwndEdit
, text
);
938 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
939 wxTreeItemIcon which
) const
941 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
943 if ( IsHiddenRoot(item
) )
945 // no images for hidden root item
949 wxTreeItemParam
*param
= GetItemParam(item
);
951 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
954 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
955 wxTreeItemIcon which
)
957 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
958 wxCHECK_RET( which
>= 0 &&
959 which
< wxTreeItemIcon_Max
,
960 wxT("invalid image index"));
963 if ( IsHiddenRoot(item
) )
965 // no images for hidden root item
969 wxTreeItemParam
*data
= GetItemParam(item
);
973 data
->SetImage(image
, which
);
978 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
980 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
982 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
984 // hidden root may still have data.
985 if ( IS_VIRTUAL_ROOT(item
) )
987 return GET_VIRTUAL_ROOT()->GetParam();
991 if ( !DoGetItem(&tvItem
) )
996 return (wxTreeItemParam
*)tvItem
.lParam
;
999 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1001 wxTreeItemParam
*data
= GetItemParam(item
);
1003 return data
? data
->GetData() : NULL
;
1006 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1008 // first, associate this piece of data with this item
1014 wxTreeItemParam
*param
= GetItemParam(item
);
1016 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1018 param
->SetData(data
);
1021 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1023 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1025 if ( IS_VIRTUAL_ROOT(item
) )
1028 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1029 tvItem
.cChildren
= (int)has
;
1033 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1035 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1037 if ( IS_VIRTUAL_ROOT(item
) )
1040 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1041 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1045 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1047 if ( IS_VIRTUAL_ROOT(item
) )
1050 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1051 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1055 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1057 if ( IS_VIRTUAL_ROOT(item
) )
1061 if ( GetBoundingRect(item
, rect
) )
1067 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1069 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1071 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1072 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1075 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1077 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1079 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1080 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1083 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1085 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1087 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1088 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1091 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1092 const wxColour
& col
)
1094 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1096 wxTreeItemAttr
*attr
;
1097 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1098 if ( it
== m_attrs
.end() )
1100 m_hasAnyAttr
= true;
1102 m_attrs
[item
.m_pItem
] =
1103 attr
= new wxTreeItemAttr
;
1110 attr
->SetTextColour(col
);
1115 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1116 const wxColour
& col
)
1118 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1120 wxTreeItemAttr
*attr
;
1121 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1122 if ( it
== m_attrs
.end() )
1124 m_hasAnyAttr
= true;
1126 m_attrs
[item
.m_pItem
] =
1127 attr
= new wxTreeItemAttr
;
1129 else // already in the hash
1134 attr
->SetBackgroundColour(col
);
1139 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1141 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1143 wxTreeItemAttr
*attr
;
1144 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1145 if ( it
== m_attrs
.end() )
1147 m_hasAnyAttr
= true;
1149 m_attrs
[item
.m_pItem
] =
1150 attr
= new wxTreeItemAttr
;
1152 else // already in the hash
1157 attr
->SetFont(font
);
1159 // Reset the item's text to ensure that the bounding rect will be adjusted
1160 // for the new font.
1161 SetItemText(item
, GetItemText(item
));
1166 // ----------------------------------------------------------------------------
1168 // ----------------------------------------------------------------------------
1170 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1172 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1174 if ( item
== wxTreeItemId(TVI_ROOT
) )
1176 // virtual (hidden) root is never visible
1180 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1183 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1184 // the HTREEITEM with TVM_GETITEMRECT
1185 *(HTREEITEM
*)&rect
= HITEM(item
);
1187 // true means to get rect for just the text, not the whole line
1188 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1190 // if TVM_GETITEMRECT returned false, then the item is definitely not
1191 // visible (because its parent is not expanded)
1195 // however if it returned true, the item might still be outside the
1196 // currently visible part of the tree, test for it (notice that partly
1197 // visible means visible here)
1198 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1201 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1203 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1205 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1208 return tvItem
.cChildren
!= 0;
1211 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1213 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1215 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1218 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1221 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1223 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1225 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1228 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1231 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1233 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1235 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1238 return (tvItem
.state
& TVIS_BOLD
) != 0;
1241 // ----------------------------------------------------------------------------
1243 // ----------------------------------------------------------------------------
1245 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1247 // Root may be real (visible) or virtual (hidden).
1248 if ( GET_VIRTUAL_ROOT() )
1251 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1254 wxTreeItemId
wxTreeCtrl::GetSelection() const
1256 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1257 wxT("this only works with single selection controls") );
1259 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1262 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1264 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1268 if ( IS_VIRTUAL_ROOT(item
) )
1270 // no parent for the virtual root
1275 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1276 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1278 // the top level items should have the virtual root as their parent
1283 return wxTreeItemId(hItem
);
1286 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1287 wxTreeItemIdValue
& cookie
) const
1289 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1291 // remember the last child returned in 'cookie'
1292 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1294 return wxTreeItemId(cookie
);
1297 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1298 wxTreeItemIdValue
& cookie
) const
1300 wxTreeItemId
fromCookie(cookie
);
1302 HTREEITEM hitem
= HITEM(fromCookie
);
1304 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1306 wxTreeItemId
item(hitem
);
1308 cookie
= item
.m_pItem
;
1313 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1315 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1317 // can this be done more efficiently?
1318 wxTreeItemIdValue cookie
;
1320 wxTreeItemId childLast
,
1321 child
= GetFirstChild(item
, cookie
);
1322 while ( child
.IsOk() )
1325 child
= GetNextChild(item
, cookie
);
1331 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1333 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1334 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1337 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1339 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1340 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1343 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1345 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1348 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1350 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1351 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1353 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1356 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1358 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1359 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1361 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1364 // ----------------------------------------------------------------------------
1365 // multiple selections emulation
1366 // ----------------------------------------------------------------------------
1368 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1370 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1372 // receive the desired information.
1373 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1376 // state image indices are 1 based
1377 return ((tvItem
.state
>> 12) - 1) == 1;
1380 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1382 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1384 // receive the desired information.
1385 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1389 // state images are one-based
1390 tvItem
.state
= (check
? 2 : 1) << 12;
1395 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1397 TraverseSelections
selector(this, selections
);
1399 return selector
.GetCount();
1402 // ----------------------------------------------------------------------------
1404 // ----------------------------------------------------------------------------
1406 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1407 const wxTreeItemId
& hInsertAfter
,
1408 const wxString
& text
,
1409 int image
, int selectedImage
,
1410 wxTreeItemData
*data
)
1412 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1414 _T("can't have more than one root in the tree") );
1416 TV_INSERTSTRUCT tvIns
;
1417 tvIns
.hParent
= HITEM(parent
);
1418 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1420 // this is how we insert the item as the first child: supply a NULL
1422 if ( !tvIns
.hInsertAfter
)
1424 tvIns
.hInsertAfter
= TVI_FIRST
;
1428 if ( !text
.empty() )
1431 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1435 tvIns
.item
.pszText
= NULL
;
1436 tvIns
.item
.cchTextMax
= 0;
1439 // create the param which will store the other item parameters
1440 wxTreeItemParam
*param
= new wxTreeItemParam
;
1442 // we return the images on demand as they depend on whether the item is
1443 // expanded or collapsed too in our case
1444 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1445 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1446 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1448 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1449 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1452 tvIns
.item
.lParam
= (LPARAM
)param
;
1453 tvIns
.item
.mask
= mask
;
1455 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1458 wxLogLastError(wxT("TreeView_InsertItem"));
1461 // associate the application tree item with Win32 tree item handle
1464 // setup wxTreeItemData
1467 param
->SetData(data
);
1471 return wxTreeItemId(id
);
1474 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1475 int image
, int selectedImage
,
1476 wxTreeItemData
*data
)
1478 if ( HasFlag(wxTR_HIDE_ROOT
) )
1480 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1482 // create a virtual root item, the parent for all the others
1483 wxTreeItemParam
*param
= new wxTreeItemParam
;
1484 param
->SetData(data
);
1486 m_pVirtualRoot
= new wxVirtualNode(param
);
1491 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1492 text
, image
, selectedImage
, data
);
1495 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1497 const wxString
& text
,
1498 int image
, int selectedImage
,
1499 wxTreeItemData
*data
)
1501 wxTreeItemId idPrev
;
1502 if ( index
== (size_t)-1 )
1504 // special value: append to the end
1507 else // find the item from index
1509 wxTreeItemIdValue cookie
;
1510 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1511 while ( index
!= 0 && idCur
.IsOk() )
1516 idCur
= GetNextChild(parent
, cookie
);
1519 // assert, not check: if the index is invalid, we will append the item
1521 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1524 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1527 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1529 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1531 wxLogLastError(wxT("TreeView_DeleteItem"));
1535 // delete all children (but don't delete the item itself)
1536 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1538 wxTreeItemIdValue cookie
;
1540 wxArrayTreeItemIds children
;
1541 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1542 while ( child
.IsOk() )
1544 children
.Add(child
);
1546 child
= GetNextChild(item
, cookie
);
1549 size_t nCount
= children
.Count();
1550 for ( size_t n
= 0; n
< nCount
; n
++ )
1552 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1554 wxLogLastError(wxT("TreeView_DeleteItem"));
1559 void wxTreeCtrl::DeleteAllItems()
1561 // delete the "virtual" root item.
1562 if ( GET_VIRTUAL_ROOT() )
1564 delete GET_VIRTUAL_ROOT();
1565 m_pVirtualRoot
= NULL
;
1568 // and all the real items
1570 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1572 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1576 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1578 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1579 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1580 flag
== TVE_EXPAND
||
1582 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1584 // A hidden root can be neither expanded nor collapsed.
1585 wxCHECK_RET( !IsHiddenRoot(item
),
1586 wxT("Can't expand/collapse hidden root node!") );
1588 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1589 // emulate them. This behaviour has changed slightly with comctl32.dll
1590 // v 4.70 - now it does send them but only the first time. To maintain
1591 // compatible behaviour and also in order to not have surprises with the
1592 // future versions, don't rely on this and still do everything ourselves.
1593 // To avoid that the messages be sent twice when the item is expanded for
1594 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1596 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1600 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1602 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1604 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1608 (void)GetEventHandler()->ProcessEvent(event
);
1610 //else: change didn't took place, so do nothing at all
1613 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1615 DoExpand(item
, TVE_EXPAND
);
1618 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1620 DoExpand(item
, TVE_COLLAPSE
);
1623 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1625 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1628 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1630 DoExpand(item
, TVE_TOGGLE
);
1633 void wxTreeCtrl::Unselect()
1635 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1636 wxT("doesn't make sense, may be you want UnselectAll()?") );
1638 // just remove the selection
1639 SelectItem(wxTreeItemId());
1642 void wxTreeCtrl::UnselectAll()
1644 if ( m_windowStyle
& wxTR_MULTIPLE
)
1646 wxArrayTreeItemIds selections
;
1647 size_t count
= GetSelections(selections
);
1648 for ( size_t n
= 0; n
< count
; n
++ )
1650 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1653 m_htSelStart
.Unset();
1657 // just remove the selection
1662 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1664 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1666 wxASSERT_MSG( select
|| HasFlag(wxTR_MULTIPLE
),
1667 _T("SelectItem(false) works only for multiselect") );
1669 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1670 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1672 if ( !::SelectItem(GetHwnd(), HITEM(item
), select
) )
1674 wxLogLastError(wxT("TreeView_SelectItem"));
1678 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1679 (void)GetEventHandler()->ProcessEvent(event
);
1682 //else: program vetoed the change
1685 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1687 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1690 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1693 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1695 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1697 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1701 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1706 void wxTreeCtrl::DeleteTextCtrl()
1710 // the HWND corresponding to this control is deleted by the tree
1711 // control itself and we don't know when exactly this happens, so check
1712 // if the window still exists before calling UnsubclassWin()
1713 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1715 m_textCtrl
->SetHWND(0);
1718 m_textCtrl
->UnsubclassWin();
1719 m_textCtrl
->SetHWND(0);
1727 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1728 wxClassInfo
*textControlClass
)
1730 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1735 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1736 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1738 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1747 // textctrl is subclassed in MSWOnNotify
1751 // End label editing, optionally cancelling the edit
1752 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1754 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1759 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1761 TV_HITTESTINFO hitTestInfo
;
1762 hitTestInfo
.pt
.x
= (int)point
.x
;
1763 hitTestInfo
.pt
.y
= (int)point
.y
;
1765 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1770 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1771 flags |= wxTREE_HITTEST_##flag
1773 TRANSLATE_FLAG(ABOVE
);
1774 TRANSLATE_FLAG(BELOW
);
1775 TRANSLATE_FLAG(NOWHERE
);
1776 TRANSLATE_FLAG(ONITEMBUTTON
);
1777 TRANSLATE_FLAG(ONITEMICON
);
1778 TRANSLATE_FLAG(ONITEMINDENT
);
1779 TRANSLATE_FLAG(ONITEMLABEL
);
1780 TRANSLATE_FLAG(ONITEMRIGHT
);
1781 TRANSLATE_FLAG(ONITEMSTATEICON
);
1782 TRANSLATE_FLAG(TOLEFT
);
1783 TRANSLATE_FLAG(TORIGHT
);
1785 #undef TRANSLATE_FLAG
1787 return wxTreeItemId(hitTestInfo
.hItem
);
1790 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1792 bool textOnly
) const
1796 // Virtual root items have no bounding rectangle
1797 if ( IS_VIRTUAL_ROOT(item
) )
1802 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1805 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1811 // couldn't retrieve rect: for example, item isn't visible
1816 // ----------------------------------------------------------------------------
1818 // ----------------------------------------------------------------------------
1820 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1821 // functions such as IsDataIndirect()
1822 class wxTreeSortHelper
1825 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1828 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1830 return ((wxTreeItemParam
*)lParam
)->GetItem();
1834 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1838 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1839 wxT("sorting tree without data doesn't make sense") );
1841 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1843 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1844 GetIdFromData(pItem2
));
1847 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1849 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1851 // rely on the fact that TreeView_SortChildren does the same thing as our
1852 // default behaviour, i.e. sorts items alphabetically and so call it
1853 // directly if we're not in derived class (much more efficient!)
1854 // RN: Note that if you find you're code doesn't sort as expected this
1855 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1856 // combo for your derived wxTreeCtrl if will sort without
1858 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1860 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1865 tvSort
.hParent
= HITEM(item
);
1866 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1867 tvSort
.lParam
= (LPARAM
)this;
1868 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1872 // ----------------------------------------------------------------------------
1874 // ----------------------------------------------------------------------------
1876 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1878 if ( msg
->message
== WM_KEYDOWN
)
1880 if ( msg
->wParam
== VK_RETURN
)
1882 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1887 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
1890 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1892 if ( cmd
== EN_UPDATE
)
1894 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1895 event
.SetEventObject( this );
1896 ProcessCommand(event
);
1898 else if ( cmd
== EN_KILLFOCUS
)
1900 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1901 event
.SetEventObject( this );
1902 ProcessCommand(event
);
1910 // command processed
1914 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1915 // only do it during dragging, minimize wxWin overhead (this is important for
1916 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1917 // instead of passing by wxWin events
1918 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1920 bool processed
= false;
1922 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
1924 // This message is sent after a right-click, or when the "menu" key is pressed
1925 if ( nMsg
== WM_CONTEXTMENU
)
1927 int x
= GET_X_LPARAM(lParam
),
1928 y
= GET_Y_LPARAM(lParam
);
1930 // the item for which the menu should be shown
1933 // the position where the menu should be shown in client coordinates
1934 // (so that it can be passed directly to PopupMenu())
1937 if ( x
== -1 || y
== -1 )
1939 // this means that the event was generated from keyboard (e.g. with
1940 // Shift-F10 or special Windows menu key)
1942 // use the Explorer standard of putting the menu at the left edge
1943 // of the text, in the vertical middle of the text
1944 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1947 // Use the bounding rectangle of only the text part
1949 GetBoundingRect(item
, rect
, true);
1950 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
1953 else // event from mouse, use mouse position
1955 pt
= ScreenToClient(wxPoint(x
, y
));
1957 TV_HITTESTINFO tvhti
;
1960 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
1961 item
= wxTreeItemId(tvhti
.hItem
);
1965 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
1967 event
.m_pointDrag
= pt
;
1969 if ( GetEventHandler()->ProcessEvent(event
) )
1971 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
1973 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1975 // we only process mouse messages here and these parameters have the
1976 // same meaning for all of them
1977 int x
= GET_X_LPARAM(lParam
),
1978 y
= GET_Y_LPARAM(lParam
);
1980 TV_HITTESTINFO tvht
;
1984 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
1988 case WM_LBUTTONDOWN
:
1989 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
1991 m_htClickedItem
= (WXHTREEITEM
) htItem
;
1992 m_ptClick
= wxPoint(x
, y
);
1994 if ( wParam
& MK_CONTROL
)
1998 // toggle selected state
1999 ToggleItemSelection(htItem
);
2001 ::SetFocus(GetHwnd(), htItem
);
2003 // reset on any click without Shift
2004 m_htSelStart
.Unset();
2008 else if ( wParam
& MK_SHIFT
)
2010 // this selects all items between the starting one and
2013 if ( !m_htSelStart
)
2015 // take the focused item
2016 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2020 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2021 !(wParam
& MK_CONTROL
));
2023 ::SelectItem(GetHwnd(), htItem
);
2025 ::SetFocus(GetHwnd(), htItem
);
2029 else // normal click
2031 // avoid doing anything if we click on the only
2032 // currently selected item
2036 wxArrayTreeItemIds selections
;
2037 size_t count
= GetSelections(selections
);
2040 HITEM(selections
[0]) != htItem
)
2042 // clear the previously selected items, if the
2043 // user clicked outside of the present selection.
2044 // otherwise, perform the deselection on mouse-up.
2045 // this allows multiple drag and drop to work.
2047 if (!IsItemSelected(GetHwnd(), htItem
))
2051 // prevent the click from starting in-place editing
2052 // which should only happen if we click on the
2053 // already selected item (and nothing else is
2056 TreeView_SelectItem(GetHwnd(), 0);
2057 ::SelectItem(GetHwnd(), htItem
);
2059 ::SetFocus(GetHwnd(), htItem
);
2063 // reset on any click without Shift
2064 m_htSelStart
.Unset();
2071 if ( m_htClickedItem
)
2073 int cx
= abs(m_ptClick
.x
- x
);
2074 int cy
= abs(m_ptClick
.y
- y
);
2076 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2078 HWND pWnd
= ::GetParent( GetHwnd() );
2083 tv
.hdr
.hwndFrom
= GetHwnd();
2084 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2085 tv
.hdr
.code
= TVN_BEGINDRAG
;
2087 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2090 ZeroMemory(&tviAux
, sizeof(tviAux
));
2091 tviAux
.hItem
= HITEM(m_htClickedItem
);
2092 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2093 tviAux
.stateMask
= 0xffffffff;
2094 TreeView_GetItem( GetHwnd(), &tviAux
);
2096 tv
.itemNew
.state
= tviAux
.state
;
2097 tv
.itemNew
.lParam
= tviAux
.lParam
;
2102 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2104 m_htClickedItem
.Unset();
2107 #endif // __WXWINCE__
2111 m_dragImage
->Move(wxPoint(x
, y
));
2114 // highlight the item as target (hiding drag image is
2115 // necessary - otherwise the display will be corrupted)
2116 m_dragImage
->Hide();
2117 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2118 m_dragImage
->Show();
2125 // facilitates multiple drag-and-drop
2126 if (htItem
&& isMultiple
)
2128 wxArrayTreeItemIds selections
;
2129 size_t count
= GetSelections(selections
);
2132 !(wParam
& MK_CONTROL
) &&
2133 !(wParam
& MK_SHIFT
))
2136 TreeView_SelectItem(GetHwnd(), htItem
);
2137 ::SelectItem(GetHwnd(), htItem
);
2138 ::SetFocus(GetHwnd(), htItem
);
2140 m_htClickedItem
.Unset();
2148 m_dragImage
->EndDrag();
2152 // generate the drag end event
2153 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2154 event
.m_pointDrag
= wxPoint(x
, y
);
2156 (void)GetEventHandler()->ProcessEvent(event
);
2158 // if we don't do it, the tree seems to think that 2 items
2159 // are selected simultaneously which is quite weird
2160 TreeView_SelectDropTarget(GetHwnd(), 0);
2165 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2167 // the tree control greys out the selected item when it loses focus and
2168 // paints it as selected again when it regains it, but it won't do it
2169 // for the other items itself - help it
2170 wxArrayTreeItemIds selections
;
2171 size_t count
= GetSelections(selections
);
2173 for ( size_t n
= 0; n
< count
; n
++ )
2175 // TreeView_GetItemRect() will return false if item is not visible,
2176 // which may happen perfectly well
2177 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2180 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2184 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2186 bool bCtrl
= wxIsCtrlDown(),
2187 bShift
= wxIsShiftDown();
2189 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2195 ToggleItemSelection(htSel
);
2201 ::SelectItem(GetHwnd(), htSel
);
2209 if ( !bCtrl
&& !bShift
)
2211 // no modifiers, just clear selection and then let the default
2212 // processing to take place
2217 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2219 HTREEITEM htNext
= (HTREEITEM
)
2220 TreeView_GetNextItem
2224 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2230 // at the top/bottom
2236 if ( !m_htSelStart
)
2237 m_htSelStart
= htSel
;
2239 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2243 // without changing selection
2244 ::SetFocus(GetHwnd(), htNext
);
2255 // TODO: handle Shift/Ctrl with these keys
2256 if ( !bCtrl
&& !bShift
)
2260 m_htSelStart
.Unset();
2264 else if ( nMsg
== WM_COMMAND
)
2266 // if we receive a EN_KILLFOCUS command from the in-place edit control
2267 // used for label editing, make sure to end editing
2270 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2272 if ( cmd
== EN_KILLFOCUS
)
2274 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2284 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2290 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2292 if ( nMsg
== WM_CHAR
)
2294 // don't let the control process Space and Return keys because it
2295 // doesn't do anything useful with them anyhow but always beeps
2296 // annoyingly when it receives them and there is no way to turn it off
2297 // simply if you just process TREEITEM_ACTIVATED event to which Space
2298 // and Enter presses are mapped in your code
2299 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2303 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2306 // process WM_NOTIFY Windows message
2307 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2309 wxTreeEvent
event(wxEVT_NULL
, this);
2310 wxEventType eventType
= wxEVT_NULL
;
2311 NMHDR
*hdr
= (NMHDR
*)lParam
;
2313 switch ( hdr
->code
)
2316 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2319 case TVN_BEGINRDRAG
:
2321 if ( eventType
== wxEVT_NULL
)
2322 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2323 //else: left drag, already set above
2325 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2327 event
.m_item
= tv
->itemNew
.hItem
;
2328 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2330 // don't allow dragging by default: the user code must
2331 // explicitly say that it wants to allow it to avoid breaking
2337 case TVN_BEGINLABELEDIT
:
2339 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2340 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2342 // although the user event handler may still veto it, it is
2343 // important to set it now so that calls to SetItemText() from
2344 // the event handler would change the text controls contents
2346 event
.m_item
= info
->item
.hItem
;
2347 event
.m_label
= info
->item
.pszText
;
2348 event
.m_editCancelled
= false;
2352 case TVN_DELETEITEM
:
2354 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2355 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2357 event
.m_item
= tv
->itemOld
.hItem
;
2361 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2362 if ( it
!= m_attrs
.end() )
2371 case TVN_ENDLABELEDIT
:
2373 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2374 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2376 event
.m_item
= info
->item
.hItem
;
2377 event
.m_label
= info
->item
.pszText
;
2378 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2383 // These *must* not be removed or TVN_GETINFOTIP will
2384 // not be processed each time the mouse is moved
2385 // and the tooltip will only ever update once.
2394 #ifdef TVN_GETINFOTIP
2395 case TVN_GETINFOTIP
:
2397 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2398 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2400 // Which item are we trying to get a tooltip for?
2401 event
.m_item
= info
->hItem
;
2405 #endif // TVN_GETINFOTIP
2406 #endif // !__WXWINCE__
2408 case TVN_GETDISPINFO
:
2409 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2412 case TVN_SETDISPINFO
:
2414 if ( eventType
== wxEVT_NULL
)
2415 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2416 //else: get, already set above
2418 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2420 event
.m_item
= info
->item
.hItem
;
2424 case TVN_ITEMEXPANDING
:
2425 case TVN_ITEMEXPANDED
:
2427 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2430 switch ( tv
->action
)
2433 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2441 what
= IDX_COLLAPSE
;
2445 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2448 eventType
= gs_expandEvents
[what
][how
];
2450 event
.m_item
= tv
->itemNew
.hItem
;
2456 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2457 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2459 // fabricate the lParam and wParam parameters sufficiently
2460 // similar to the ones from a "real" WM_KEYDOWN so that
2461 // CreateKeyEvent() works correctly
2462 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2463 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2465 WXWPARAM wParam
= info
->wVKey
;
2467 int keyCode
= wxCharCodeMSWToWX(wParam
);
2470 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2475 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2480 // a separate event for Space/Return
2481 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2482 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2485 if ( !HasFlag(wxTR_MULTIPLE
) )
2486 item
= GetSelection();
2488 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2490 (void)GetEventHandler()->ProcessEvent(event2
);
2495 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2496 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2497 // we have to handle both messages:
2498 case TVN_SELCHANGEDA
:
2499 case TVN_SELCHANGEDW
:
2500 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2503 case TVN_SELCHANGINGA
:
2504 case TVN_SELCHANGINGW
:
2506 if ( eventType
== wxEVT_NULL
)
2507 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2508 //else: already set above
2510 if (hdr
->code
== TVN_SELCHANGINGW
||
2511 hdr
->code
== TVN_SELCHANGEDW
)
2513 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2514 event
.m_item
= tv
->itemNew
.hItem
;
2515 event
.m_itemOld
= tv
->itemOld
.hItem
;
2519 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2520 event
.m_item
= tv
->itemNew
.hItem
;
2521 event
.m_itemOld
= tv
->itemOld
.hItem
;
2526 // instead of explicitly checking for _WIN32_IE, check if the
2527 // required symbols are available in the headers
2528 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2531 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2532 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2533 switch ( nmcd
.dwDrawStage
)
2536 // if we've got any items with non standard attributes,
2537 // notify us before painting each item
2538 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2542 case CDDS_ITEMPREPAINT
:
2544 wxMapTreeAttr::iterator
2545 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2547 if ( it
== m_attrs
.end() )
2549 // nothing to do for this item
2550 *result
= CDRF_DODEFAULT
;
2554 wxTreeItemAttr
* const attr
= it
->second
;
2556 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2557 TVIF_STATE
, TVIS_DROPHILITED
);
2559 const UINT tvItemState
= tvItem
.state
;
2561 // selection colours should override ours,
2562 // otherwise it is too confusing to the user
2563 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2564 !(tvItemState
& TVIS_DROPHILITED
) )
2567 if ( attr
->HasBackgroundColour() )
2569 colBack
= attr
->GetBackgroundColour();
2570 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2574 // but we still want to keep the special foreground
2575 // colour when we don't have focus (we can't keep
2576 // it when we do, it would usually be unreadable on
2577 // the almost inverted bg colour...)
2578 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2579 FindFocus() != this ) &&
2580 !(tvItemState
& TVIS_DROPHILITED
) )
2583 if ( attr
->HasTextColour() )
2585 colText
= attr
->GetTextColour();
2586 lptvcd
->clrText
= wxColourToRGB(colText
);
2590 if ( attr
->HasFont() )
2592 HFONT hFont
= GetHfontOf(attr
->GetFont());
2594 ::SelectObject(nmcd
.hdc
, hFont
);
2596 *result
= CDRF_NEWFONT
;
2598 else // no specific font
2600 *result
= CDRF_DODEFAULT
;
2606 *result
= CDRF_DODEFAULT
;
2610 // we always process it
2612 #endif // have owner drawn support in headers
2616 DWORD pos
= GetMessagePos();
2618 point
.x
= LOWORD(pos
);
2619 point
.y
= HIWORD(pos
);
2620 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2622 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2623 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2625 event
.m_item
= item
;
2626 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2634 TV_HITTESTINFO tvhti
;
2635 ::GetCursorPos(&tvhti
.pt
);
2636 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2637 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2639 if ( tvhti
.flags
& TVHT_ONITEM
)
2641 event
.m_item
= tvhti
.hItem
;
2642 eventType
= (int)hdr
->code
== NM_DBLCLK
2643 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2644 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2646 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2647 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2656 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2659 event
.SetEventType(eventType
);
2661 if ( event
.m_item
.IsOk() )
2662 event
.SetClientObject(GetItemData(event
.m_item
));
2664 bool processed
= GetEventHandler()->ProcessEvent(event
);
2667 switch ( hdr
->code
)
2670 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2671 // the return code of this event handler as the return value for
2672 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2673 // expanded status would never work
2678 // prevent tree control from sending WM_CONTEXTMENU to our parent
2679 // (which it does if NM_RCLICK is not handled) because we want to
2680 // send it to the control itself
2684 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
2685 (WPARAM
)GetHwnd(), ::GetMessagePos());
2689 case TVN_BEGINRDRAG
:
2690 if ( event
.IsAllowed() )
2692 // normally this is impossible because the m_dragImage is
2693 // deleted once the drag operation is over
2694 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2696 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2697 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2698 m_dragImage
->Show();
2702 case TVN_DELETEITEM
:
2704 // NB: we might process this message using wxWidgets event
2705 // tables, but due to overhead of wxWin event system we
2706 // prefer to do it here ourself (otherwise deleting a tree
2707 // with many items is just too slow)
2708 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2710 wxTreeItemParam
*param
=
2711 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2714 processed
= true; // Make sure we don't get called twice
2718 case TVN_BEGINLABELEDIT
:
2719 // return true to cancel label editing
2720 *result
= !event
.IsAllowed();
2722 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2723 if ( event
.IsAllowed() )
2725 HWND hText
= TreeView_GetEditControl(GetHwnd());
2728 // MBN: if m_textCtrl already has an HWND, it is a stale
2729 // pointer from a previous edit (because the user
2730 // didn't modify the label before dismissing the control,
2731 // and TVN_ENDLABELEDIT was not sent), so delete it
2732 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2735 m_textCtrl
= new wxTextCtrl();
2736 m_textCtrl
->SetParent(this);
2737 m_textCtrl
->SetHWND((WXHWND
)hText
);
2738 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2740 // set wxTE_PROCESS_ENTER style for the text control to
2741 // force it to process the Enter presses itself, otherwise
2742 // they could be stolen from it by the dialog
2744 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2745 | wxTE_PROCESS_ENTER
);
2748 else // we had set m_idEdited before
2754 case TVN_ENDLABELEDIT
:
2755 // return true to set the label to the new string: note that we
2756 // also must pretend that we did process the message or it is going
2757 // to be passed to DefWindowProc() which will happily return false
2758 // cancelling the label change
2759 *result
= event
.IsAllowed();
2762 // ensure that we don't have the text ctrl which is going to be
2768 #ifdef TVN_GETINFOTIP
2769 case TVN_GETINFOTIP
:
2771 // If the user permitted a tooltip change, change it
2772 if (event
.IsAllowed())
2774 SetToolTip(event
.m_label
);
2781 case TVN_SELCHANGING
:
2782 case TVN_ITEMEXPANDING
:
2783 // return true to prevent the action from happening
2784 *result
= !event
.IsAllowed();
2787 case TVN_ITEMEXPANDED
:
2788 // the item is not refreshed properly after expansion when it has
2789 // an image depending on the expanded/collapsed state - bug in
2790 // comctl32.dll or our code?
2792 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2793 wxTreeItemId
id(tv
->itemNew
.hItem
);
2795 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2803 case TVN_GETDISPINFO
:
2804 // NB: so far the user can't set the image himself anyhow, so do it
2805 // anyway - but this may change later
2806 //if ( /* !processed && */ )
2808 wxTreeItemId item
= event
.m_item
;
2809 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2811 const wxTreeItemParam
* const param
= GetItemParam(item
);
2815 if ( info
->item
.mask
& TVIF_IMAGE
)
2820 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2821 : wxTreeItemIcon_Normal
2824 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2826 info
->item
.iSelectedImage
=
2829 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2830 : wxTreeItemIcon_Selected
2837 // for the other messages the return value is ignored and there is
2838 // nothing special to do
2843 // ----------------------------------------------------------------------------
2845 // ----------------------------------------------------------------------------
2847 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2848 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2850 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2853 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2854 tvi
.mask
= TVIF_STATE
;
2855 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2857 // Select the specified state, or -1 == cycle to the next one.
2860 TreeView_GetItem(GetHwnd(), &tvi
);
2862 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2863 if ( state
== m_imageListState
->GetImageCount() )
2867 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2868 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2870 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2872 TreeView_SetItem(GetHwnd(), &tvi
);
2875 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2878 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2879 tvi
.mask
= TVIF_STATE
;
2880 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2881 TreeView_GetItem(GetHwnd(), &tvi
);
2883 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2886 #endif // wxUSE_TREECTRL