1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/dynarray.h"
35 #include "wx/settings.h"
38 #include "wx/msw/private.h"
40 // include <commctrl.h> "properly"
41 #include "wx/msw/wrapcctl.h"
43 #include "wx/msw/missing.h"
45 // Set this to 1 to be _absolutely_ sure that repainting will work for all
46 // comctl32.dll versions
47 #define wxUSE_COMCTL32_SAFELY 0
49 #include "wx/imaglist.h"
50 #include "wx/msw/dragimag.h"
52 // macros to hide the cast ugliness
53 // --------------------------------
55 // get HTREEITEM from wxTreeItemId
56 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
58 // the native control doesn't support multiple selections under MSW and we
59 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
60 // checkboxes be the selection status (checked == selected) or by really
61 // emulating everything, i.e. intercepting mouse and key events &c. The first
62 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
64 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // wrapper for TreeView_HitTest
71 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
77 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
80 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
82 // wrappers for TreeView_GetItem/TreeView_SetItem
83 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
87 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
88 tvi
.stateMask
= TVIS_SELECTED
;
91 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
93 wxLogLastError(wxT("TreeView_GetItem"));
96 return (tvi
.state
& TVIS_SELECTED
) != 0;
99 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
102 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
103 tvi
.stateMask
= TVIS_SELECTED
;
104 tvi
.state
= select
? TVIS_SELECTED
: 0;
107 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
109 wxLogLastError(wxT("TreeView_SetItem"));
116 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
118 SelectItem(hwndTV
, htItem
, false);
121 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
123 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
126 // helper function which selects all items in a range and, optionally,
127 // unselects all others
128 static void SelectRange(HWND hwndTV
,
131 bool unselectOthers
= true)
133 // find the first (or last) item and select it
135 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
136 while ( htItem
&& cont
)
138 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
140 if ( !IsItemSelected(hwndTV
, htItem
) )
142 SelectItem(hwndTV
, htItem
);
149 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
151 UnselectItem(hwndTV
, htItem
);
155 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
158 // select the items in range
159 cont
= htFirst
!= htLast
;
160 while ( htItem
&& cont
)
162 if ( !IsItemSelected(hwndTV
, htItem
) )
164 SelectItem(hwndTV
, htItem
);
167 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
169 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
173 if ( unselectOthers
)
177 if ( IsItemSelected(hwndTV
, htItem
) )
179 UnselectItem(hwndTV
, htItem
);
182 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
186 // seems to be necessary - otherwise the just selected items don't always
187 // appear as selected
188 UpdateWindow(hwndTV
);
191 // helper function which tricks the standard control into changing the focused
192 // item without changing anything else (if someone knows why Microsoft doesn't
193 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
194 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
197 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
202 if ( htItem
!= htFocus
)
204 // remember the selection state of the item
205 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
207 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
209 // prevent the tree from unselecting the old focus which it
210 // would do by default (TreeView_SelectItem unselects the
212 TreeView_SelectItem(hwndTV
, 0);
213 SelectItem(hwndTV
, htFocus
);
216 TreeView_SelectItem(hwndTV
, htItem
);
220 // need to clear the selection which TreeView_SelectItem() gave
222 UnselectItem(hwndTV
, htItem
);
224 //else: was selected, still selected - ok
226 //else: nothing to do, focus already there
232 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
234 // just clear the focus
235 TreeView_SelectItem(hwndTV
, 0);
237 if ( wasFocusSelected
)
239 // restore the selection state
240 SelectItem(hwndTV
, htFocus
);
243 //else: nothing to do, no focus already
247 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 // a convenient wrapper around TV_ITEM struct which adds a ctor
255 #pragma warning( disable : 4097 ) // inheriting from typedef
258 struct wxTreeViewItem
: public TV_ITEM
260 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
261 UINT mask_
, // fields which are valid
262 UINT stateMask_
= 0) // for TVIF_STATE only
266 // hItem member is always valid
267 mask
= mask_
| TVIF_HANDLE
;
268 stateMask
= stateMask_
;
273 // ----------------------------------------------------------------------------
274 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
276 // We need this for a couple of reasons:
278 // 1) This class is needed for support of different images: the Win32 common
279 // control natively supports only 2 images (the normal one and another for the
280 // selected state). We wish to provide support for 2 more of them for folder
281 // items (i.e. those which have children): for expanded state and for expanded
282 // selected state. For this we use this structure to store the additional items
285 // 2) This class is also needed to hold the HITEM so that we can sort
286 // it correctly in the MSW sort callback.
288 // In addition it makes other workarounds such as this easier and helps
289 // simplify the code.
290 // ----------------------------------------------------------------------------
292 class wxTreeItemParam
299 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
305 // dtor deletes the associated data as well
306 virtual ~wxTreeItemParam() { delete m_data
; }
309 // get the real data associated with the item
310 wxTreeItemData
*GetData() const { return m_data
; }
312 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
314 // do we have such image?
315 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
316 // get image, falling back to the other images if this one is not
318 int GetImage(wxTreeItemIcon which
) const
320 int image
= m_images
[which
];
325 case wxTreeItemIcon_SelectedExpanded
:
326 image
= GetImage(wxTreeItemIcon_Expanded
);
331 case wxTreeItemIcon_Selected
:
332 case wxTreeItemIcon_Expanded
:
333 image
= GetImage(wxTreeItemIcon_Normal
);
336 case wxTreeItemIcon_Normal
:
341 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
347 // change the given image
348 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
351 const wxTreeItemId
& GetItem() const { return m_item
; }
353 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
356 // all the images associated with the item
357 int m_images
[wxTreeItemIcon_Max
];
359 // item for sort callbacks
362 // the real client data
363 wxTreeItemData
*m_data
;
365 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
368 // wxVirutalNode is used in place of a single root when 'hidden' root is
370 class wxVirtualNode
: public wxTreeViewItem
373 wxVirtualNode(wxTreeItemParam
*param
)
374 : wxTreeViewItem(TVI_ROOT
, 0)
384 wxTreeItemParam
*GetParam() const { return m_param
; }
385 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
388 wxTreeItemParam
*m_param
;
390 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
394 #pragma warning( default : 4097 )
397 // a macro to get the virtual root, returns NULL if none
398 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
400 // returns true if the item is the virtual root
401 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
403 // a class which encapsulates the tree traversal logic: it vists all (unless
404 // OnVisit() returns false) items under the given one
405 class wxTreeTraversal
408 wxTreeTraversal(const wxTreeCtrl
*tree
)
413 // give it a virtual dtor: not really needed as the class is never used
414 // polymorphically and not even allocated on heap at all, but this is safer
415 // (in case it ever is) and silences the compiler warnings for now
416 virtual ~wxTreeTraversal() { }
418 // do traverse the tree: visit all items (recursively by default) under the
419 // given one; return true if all items were traversed or false if the
420 // traversal was aborted because OnVisit returned false
421 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
423 // override this function to do whatever is needed for each item, return
424 // false to stop traversing
425 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
428 const wxTreeCtrl
*GetTree() const { return m_tree
; }
431 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
433 const wxTreeCtrl
*m_tree
;
435 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
438 // internal class for getting the selected items
439 class TraverseSelections
: public wxTreeTraversal
442 TraverseSelections(const wxTreeCtrl
*tree
,
443 wxArrayTreeItemIds
& selections
)
444 : wxTreeTraversal(tree
), m_selections(selections
)
446 m_selections
.Empty();
448 if (tree
->GetCount() > 0)
449 DoTraverse(tree
->GetRootItem());
452 virtual bool OnVisit(const wxTreeItemId
& item
)
454 // can't visit a virtual node.
455 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
460 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
461 if ( GetTree()->IsItemChecked(item
) )
463 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
466 m_selections
.Add(item
);
472 size_t GetCount() const { return m_selections
.GetCount(); }
475 wxArrayTreeItemIds
& m_selections
;
477 DECLARE_NO_COPY_CLASS(TraverseSelections
)
480 // internal class for counting tree items
481 class TraverseCounter
: public wxTreeTraversal
484 TraverseCounter(const wxTreeCtrl
*tree
,
485 const wxTreeItemId
& root
,
487 : wxTreeTraversal(tree
)
491 DoTraverse(root
, recursively
);
494 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
501 size_t GetCount() const { return m_count
; }
506 DECLARE_NO_COPY_CLASS(TraverseCounter
)
509 // ----------------------------------------------------------------------------
511 // ----------------------------------------------------------------------------
513 #if wxUSE_EXTENDED_RTTI
514 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
516 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
517 // new style border flags, we put them first to
518 // use them for streaming out
519 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
520 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
521 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
522 wxFLAGS_MEMBER(wxBORDER_RAISED
)
523 wxFLAGS_MEMBER(wxBORDER_STATIC
)
524 wxFLAGS_MEMBER(wxBORDER_NONE
)
526 // old style border flags
527 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
528 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
529 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
530 wxFLAGS_MEMBER(wxRAISED_BORDER
)
531 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
532 wxFLAGS_MEMBER(wxBORDER
)
534 // standard window styles
535 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
536 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
537 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
538 wxFLAGS_MEMBER(wxWANTS_CHARS
)
539 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
540 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
541 wxFLAGS_MEMBER(wxVSCROLL
)
542 wxFLAGS_MEMBER(wxHSCROLL
)
544 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
545 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
546 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
547 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
548 wxFLAGS_MEMBER(wxTR_NO_LINES
)
549 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
550 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
551 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
552 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
553 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
554 wxFLAGS_MEMBER(wxTR_SINGLE
)
555 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
556 wxFLAGS_MEMBER(wxTR_EXTENDED
)
557 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
559 wxEND_FLAGS( wxTreeCtrlStyle
)
561 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
563 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
564 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
565 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
566 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
567 wxEND_PROPERTIES_TABLE()
569 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
570 wxEND_HANDLERS_TABLE()
572 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
574 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 // indices in gs_expandEvents table below
596 // handy table for sending events - it has to be initialized during run-time
597 // now so can't be const any more
598 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
601 but logically it's a const table with the following entries:
604 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
605 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
609 // ============================================================================
611 // ============================================================================
613 // ----------------------------------------------------------------------------
615 // ----------------------------------------------------------------------------
617 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
619 if ( !OnVisit(root
) )
622 return Traverse(root
, recursively
);
625 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
627 wxTreeItemIdValue cookie
;
628 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
629 while ( child
.IsOk() )
631 // depth first traversal
632 if ( recursively
&& !Traverse(child
, true) )
635 if ( !OnVisit(child
) )
638 child
= m_tree
->GetNextChild(root
, cookie
);
644 // ----------------------------------------------------------------------------
645 // construction and destruction
646 // ----------------------------------------------------------------------------
648 void wxTreeCtrl::Init()
651 m_hasAnyAttr
= false;
653 m_pVirtualRoot
= NULL
;
655 // initialize the global array of events now as it can't be done statically
656 // with the wxEVT_XXX values being allocated during run-time only
657 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
658 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
659 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
660 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
663 bool wxTreeCtrl::Create(wxWindow
*parent
,
668 const wxValidator
& validator
,
669 const wxString
& name
)
673 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
674 style
|= wxBORDER_SUNKEN
;
676 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
680 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
681 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
683 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
684 wstyle
|= TVS_HASLINES
;
685 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
686 wstyle
|= TVS_HASBUTTONS
;
688 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
689 wstyle
|= TVS_EDITLABELS
;
691 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
692 wstyle
|= TVS_LINESATROOT
;
694 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
696 if ( wxApp::GetComCtl32Version() >= 471 )
697 wstyle
|= TVS_FULLROWSELECT
;
700 // using TVS_CHECKBOXES for emulation of a multiselection tree control
701 // doesn't work without the new enough headers
702 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
703 !defined( __GNUWIN32_OLD__ ) && \
704 !defined( __BORLANDC__ ) && \
705 !defined( __WATCOMC__ ) && \
706 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
708 // we emulate the multiple selection tree controls by using checkboxes: set
709 // up the image list we need for this if we do have multiple selections
710 if ( m_windowStyle
& wxTR_MULTIPLE
)
711 wstyle
|= TVS_CHECKBOXES
;
712 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
714 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
715 // Need so that TVN_GETINFOTIP messages will be sent
716 wstyle
|= TVS_INFOTIP
;
719 // Create the tree control.
720 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
723 #if wxUSE_COMCTL32_SAFELY
724 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
725 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
727 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
728 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
730 // This works around a bug in the Windows tree control whereby for some versions
731 // of comctrl32, setting any colour actually draws the background in black.
732 // This will initialise the background to the system colour.
733 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
734 // Assume the user has an updated comctl32.dll.
735 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
736 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
737 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
741 // VZ: this is some experimental code which may be used to get the
742 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
743 // AFAIK, the standard DLL does about the same thing anyhow.
745 if ( m_windowStyle
& wxTR_MULTIPLE
)
749 // create the DC compatible with the current screen
750 HDC hdcMem
= CreateCompatibleDC(NULL
);
752 // create a mono bitmap of the standard size
753 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
754 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
755 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
756 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
757 1, // # of color planes
758 1, // # bits needed for one pixel
759 0); // array containing colour data
760 SelectObject(hdcMem
, hbmpCheck
);
762 // then draw a check mark into it
763 RECT rect
= { 0, 0, x
, y
};
764 if ( !::DrawFrameControl(hdcMem
, &rect
,
766 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
768 wxLogLastError(wxT("DrawFrameControl(check)"));
771 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
772 imagelistCheckboxes
.Add(bmp
);
774 if ( !::DrawFrameControl(hdcMem
, &rect
,
778 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
781 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
782 imagelistCheckboxes
.Add(bmp
);
788 SetStateImageList(&imagelistCheckboxes
);
792 wxSetCCUnicodeFormat(GetHwnd());
797 wxTreeCtrl::~wxTreeCtrl()
799 // delete any attributes
802 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
804 // prevent TVN_DELETEITEM handler from deleting the attributes again!
805 m_hasAnyAttr
= false;
810 // delete user data to prevent memory leaks
811 // also deletes hidden root node storage.
815 // ----------------------------------------------------------------------------
817 // ----------------------------------------------------------------------------
819 /* static */ wxVisualAttributes
820 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
822 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
824 // common controls have their own default font
825 attrs
.font
= wxGetCCDefaultFont();
831 // simple wrappers which add error checking in debug mode
833 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
835 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
836 _T("can't retrieve virtual root item") );
838 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
840 wxLogLastError(wxT("TreeView_GetItem"));
848 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
850 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
852 wxLogLastError(wxT("TreeView_SetItem"));
856 unsigned int wxTreeCtrl::GetCount() const
858 return (unsigned int)TreeView_GetCount(GetHwnd());
861 unsigned int wxTreeCtrl::GetIndent() const
863 return TreeView_GetIndent(GetHwnd());
866 void wxTreeCtrl::SetIndent(unsigned int indent
)
868 TreeView_SetIndent(GetHwnd(), indent
);
871 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
874 (void) TreeView_SetImageList(GetHwnd(),
875 imageList
? imageList
->GetHIMAGELIST() : 0,
879 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
881 if (m_ownsImageListNormal
)
882 delete m_imageListNormal
;
884 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
885 m_ownsImageListNormal
= false;
888 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
890 if (m_ownsImageListState
) delete m_imageListState
;
891 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
892 m_ownsImageListState
= false;
895 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
896 bool recursively
) const
898 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
900 TraverseCounter
counter(this, item
, recursively
);
901 return counter
.GetCount() - 1;
904 // ----------------------------------------------------------------------------
906 // ----------------------------------------------------------------------------
908 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
910 #if !wxUSE_COMCTL32_SAFELY
911 if ( !wxWindowBase::SetBackgroundColour(colour
) )
914 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
920 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
922 #if !wxUSE_COMCTL32_SAFELY
923 if ( !wxWindowBase::SetForegroundColour(colour
) )
926 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
932 // ----------------------------------------------------------------------------
934 // ----------------------------------------------------------------------------
936 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
938 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
940 wxChar buf
[512]; // the size is arbitrary...
942 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
943 tvItem
.pszText
= buf
;
944 tvItem
.cchTextMax
= WXSIZEOF(buf
);
945 if ( !DoGetItem(&tvItem
) )
947 // don't return some garbage which was on stack, but an empty string
951 return wxString(buf
);
954 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
956 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
958 if ( IS_VIRTUAL_ROOT(item
) )
961 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
962 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
965 // when setting the text of the item being edited, the text control should
966 // be updated to reflect the new text as well, otherwise calling
967 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
969 // don't use GetEditControl() here because m_textCtrl is not set yet
970 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
973 if ( item
== m_idEdited
)
975 ::SetWindowText(hwndEdit
, text
);
980 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
981 wxTreeItemIcon which
) const
983 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
985 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
987 // no images for hidden root item
991 wxTreeItemParam
*param
= GetItemParam(item
);
993 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
996 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
997 wxTreeItemIcon which
)
999 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1000 wxCHECK_RET( which
>= 0 &&
1001 which
< wxTreeItemIcon_Max
,
1002 wxT("invalid image index"));
1005 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1007 // no images for hidden root item
1011 wxTreeItemParam
*data
= GetItemParam(item
);
1015 data
->SetImage(image
, which
);
1020 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1022 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1024 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1026 // hidden root may still have data.
1027 if ( IS_VIRTUAL_ROOT(item
) )
1029 return GET_VIRTUAL_ROOT()->GetParam();
1033 if ( !DoGetItem(&tvItem
) )
1038 return (wxTreeItemParam
*)tvItem
.lParam
;
1041 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1043 wxTreeItemParam
*data
= GetItemParam(item
);
1045 return data
? data
->GetData() : NULL
;
1048 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1050 // first, associate this piece of data with this item
1056 wxTreeItemParam
*param
= GetItemParam(item
);
1058 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1060 param
->SetData(data
);
1063 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1065 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1067 if ( IS_VIRTUAL_ROOT(item
) )
1070 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1071 tvItem
.cChildren
= (int)has
;
1075 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1077 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1079 if ( IS_VIRTUAL_ROOT(item
) )
1082 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1083 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1087 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1089 if ( IS_VIRTUAL_ROOT(item
) )
1092 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1093 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1097 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1099 if ( IS_VIRTUAL_ROOT(item
) )
1103 if ( GetBoundingRect(item
, rect
) )
1109 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1111 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1113 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1114 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1117 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1119 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1121 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1122 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1125 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1127 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1129 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1130 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1133 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1134 const wxColour
& col
)
1136 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1138 wxTreeItemAttr
*attr
;
1139 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1140 if ( it
== m_attrs
.end() )
1142 m_hasAnyAttr
= true;
1144 m_attrs
[item
.m_pItem
] =
1145 attr
= new wxTreeItemAttr
;
1152 attr
->SetTextColour(col
);
1157 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1158 const wxColour
& col
)
1160 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1162 wxTreeItemAttr
*attr
;
1163 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1164 if ( it
== m_attrs
.end() )
1166 m_hasAnyAttr
= true;
1168 m_attrs
[item
.m_pItem
] =
1169 attr
= new wxTreeItemAttr
;
1171 else // already in the hash
1176 attr
->SetBackgroundColour(col
);
1181 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1183 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1185 wxTreeItemAttr
*attr
;
1186 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1187 if ( it
== m_attrs
.end() )
1189 m_hasAnyAttr
= true;
1191 m_attrs
[item
.m_pItem
] =
1192 attr
= new wxTreeItemAttr
;
1194 else // already in the hash
1199 attr
->SetFont(font
);
1204 // ----------------------------------------------------------------------------
1206 // ----------------------------------------------------------------------------
1208 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1210 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1212 if ( item
== wxTreeItemId(TVI_ROOT
) )
1214 // virtual (hidden) root is never visible
1218 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1221 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1222 // the HTREEITEM with TVM_GETITEMRECT
1223 *(HTREEITEM
*)&rect
= HITEM(item
);
1225 // true means to get rect for just the text, not the whole line
1226 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1228 // if TVM_GETITEMRECT returned false, then the item is definitely not
1229 // visible (because its parent is not expanded)
1233 // however if it returned true, the item might still be outside the
1234 // currently visible part of the tree, test for it (notice that partly
1235 // visible means visible here)
1236 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1239 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1241 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1243 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1246 return tvItem
.cChildren
!= 0;
1249 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1251 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1253 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1256 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1259 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1261 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1263 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1266 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1269 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1271 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1273 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1276 return (tvItem
.state
& TVIS_BOLD
) != 0;
1279 // ----------------------------------------------------------------------------
1281 // ----------------------------------------------------------------------------
1283 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1285 // Root may be real (visible) or virtual (hidden).
1286 if ( GET_VIRTUAL_ROOT() )
1289 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1292 wxTreeItemId
wxTreeCtrl::GetSelection() const
1294 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1295 wxT("this only works with single selection controls") );
1297 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1300 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1302 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1306 if ( IS_VIRTUAL_ROOT(item
) )
1308 // no parent for the virtual root
1313 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1314 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1316 // the top level items should have the virtual root as their parent
1321 return wxTreeItemId(hItem
);
1324 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1325 wxTreeItemIdValue
& cookie
) const
1327 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1329 // remember the last child returned in 'cookie'
1330 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1332 return wxTreeItemId(cookie
);
1335 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1336 wxTreeItemIdValue
& cookie
) const
1338 wxTreeItemId
fromCookie(cookie
);
1340 HTREEITEM hitem
= HITEM(fromCookie
);
1342 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1344 wxTreeItemId
item(hitem
);
1346 cookie
= item
.m_pItem
;
1351 #if WXWIN_COMPATIBILITY_2_4
1353 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1356 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1358 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1360 return wxTreeItemId((void *)cookie
);
1363 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1366 wxTreeItemId
fromCookie((void *)cookie
);
1368 HTREEITEM hitem
= HITEM(fromCookie
);
1370 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1372 wxTreeItemId
item(hitem
);
1374 cookie
= (long)item
.m_pItem
;
1379 #endif // WXWIN_COMPATIBILITY_2_4
1381 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1383 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1385 // can this be done more efficiently?
1386 wxTreeItemIdValue cookie
;
1388 wxTreeItemId childLast
,
1389 child
= GetFirstChild(item
, cookie
);
1390 while ( child
.IsOk() )
1393 child
= GetNextChild(item
, cookie
);
1399 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1401 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1402 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1405 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1407 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1408 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1411 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1413 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1416 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1418 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1419 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1421 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1424 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1426 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1427 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1429 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1432 // ----------------------------------------------------------------------------
1433 // multiple selections emulation
1434 // ----------------------------------------------------------------------------
1436 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1438 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1440 // receive the desired information.
1441 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1444 // state image indices are 1 based
1445 return ((tvItem
.state
>> 12) - 1) == 1;
1448 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1450 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1452 // receive the desired information.
1453 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1457 // state images are one-based
1458 tvItem
.state
= (check
? 2 : 1) << 12;
1463 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1465 TraverseSelections
selector(this, selections
);
1467 return selector
.GetCount();
1470 // ----------------------------------------------------------------------------
1472 // ----------------------------------------------------------------------------
1474 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1475 const wxTreeItemId
& hInsertAfter
,
1476 const wxString
& text
,
1477 int image
, int selectedImage
,
1478 wxTreeItemData
*data
)
1480 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1482 _T("can't have more than one root in the tree") );
1484 TV_INSERTSTRUCT tvIns
;
1485 tvIns
.hParent
= HITEM(parent
);
1486 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1488 // this is how we insert the item as the first child: supply a NULL
1490 if ( !tvIns
.hInsertAfter
)
1492 tvIns
.hInsertAfter
= TVI_FIRST
;
1496 if ( !text
.empty() )
1499 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1503 tvIns
.item
.pszText
= NULL
;
1504 tvIns
.item
.cchTextMax
= 0;
1507 // create the param which will store the other item parameters
1508 wxTreeItemParam
*param
= new wxTreeItemParam
;
1510 // we return the images on demand as they depend on whether the item is
1511 // expanded or collapsed too in our case
1512 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1513 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1514 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1516 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1517 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1520 tvIns
.item
.lParam
= (LPARAM
)param
;
1521 tvIns
.item
.mask
= mask
;
1523 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1526 wxLogLastError(wxT("TreeView_InsertItem"));
1529 // associate the application tree item with Win32 tree item handle
1532 // setup wxTreeItemData
1535 param
->SetData(data
);
1539 return wxTreeItemId(id
);
1542 // for compatibility only
1543 #if WXWIN_COMPATIBILITY_2_4
1545 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1547 SetImageList(imageList
);
1550 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1552 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1555 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1557 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1560 #endif // WXWIN_COMPATIBILITY_2_4
1562 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1563 int image
, int selectedImage
,
1564 wxTreeItemData
*data
)
1567 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1569 // create a virtual root item, the parent for all the others
1570 wxTreeItemParam
*param
= new wxTreeItemParam
;
1571 param
->SetData(data
);
1573 m_pVirtualRoot
= new wxVirtualNode(param
);
1578 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1579 text
, image
, selectedImage
, data
);
1582 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1584 const wxString
& text
,
1585 int image
, int selectedImage
,
1586 wxTreeItemData
*data
)
1588 wxTreeItemId idPrev
;
1589 if ( index
== (size_t)-1 )
1591 // special value: append to the end
1594 else // find the item from index
1596 wxTreeItemIdValue cookie
;
1597 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1598 while ( index
!= 0 && idCur
.IsOk() )
1603 idCur
= GetNextChild(parent
, cookie
);
1606 // assert, not check: if the index is invalid, we will append the item
1608 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1611 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1614 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1616 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1618 wxLogLastError(wxT("TreeView_DeleteItem"));
1622 // delete all children (but don't delete the item itself)
1623 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1625 wxTreeItemIdValue cookie
;
1627 wxArrayTreeItemIds children
;
1628 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1629 while ( child
.IsOk() )
1631 children
.Add(child
);
1633 child
= GetNextChild(item
, cookie
);
1636 size_t nCount
= children
.Count();
1637 for ( size_t n
= 0; n
< nCount
; n
++ )
1639 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1641 wxLogLastError(wxT("TreeView_DeleteItem"));
1646 void wxTreeCtrl::DeleteAllItems()
1648 // delete the "virtual" root item.
1649 if ( GET_VIRTUAL_ROOT() )
1651 delete GET_VIRTUAL_ROOT();
1652 m_pVirtualRoot
= NULL
;
1655 // and all the real items
1657 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1659 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1663 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1665 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1666 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1667 flag
== TVE_EXPAND
||
1669 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1671 // A hidden root can be neither expanded nor collapsed.
1672 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1673 wxT("Can't expand/collapse hidden root node!") );
1675 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1676 // emulate them. This behaviour has changed slightly with comctl32.dll
1677 // v 4.70 - now it does send them but only the first time. To maintain
1678 // compatible behaviour and also in order to not have surprises with the
1679 // future versions, don't rely on this and still do everything ourselves.
1680 // To avoid that the messages be sent twice when the item is expanded for
1681 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1683 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1687 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1689 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1691 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1695 (void)GetEventHandler()->ProcessEvent(event
);
1697 //else: change didn't took place, so do nothing at all
1700 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1702 DoExpand(item
, TVE_EXPAND
);
1705 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1707 DoExpand(item
, TVE_COLLAPSE
);
1710 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1712 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1715 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1717 DoExpand(item
, TVE_TOGGLE
);
1720 #if WXWIN_COMPATIBILITY_2_4
1722 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1724 DoExpand(item
, action
);
1729 void wxTreeCtrl::Unselect()
1731 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1732 wxT("doesn't make sense, may be you want UnselectAll()?") );
1734 // just remove the selection
1735 SelectItem(wxTreeItemId());
1738 void wxTreeCtrl::UnselectAll()
1740 if ( m_windowStyle
& wxTR_MULTIPLE
)
1742 wxArrayTreeItemIds selections
;
1743 size_t count
= GetSelections(selections
);
1744 for ( size_t n
= 0; n
< count
; n
++ )
1746 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1747 SetItemCheck(HITEM(selections
[n
]), false);
1748 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1749 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1750 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1753 m_htSelStart
.Unset();
1757 // just remove the selection
1762 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1764 if ( m_windowStyle
& wxTR_MULTIPLE
)
1766 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1767 // selecting the item means checking it
1768 SetItemCheck(item
, select
);
1769 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1770 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1771 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1775 wxASSERT_MSG( select
,
1776 _T("SelectItem(false) works only for multiselect") );
1778 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1779 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1780 // send them ourselves
1782 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1783 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1785 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1787 wxLogLastError(wxT("TreeView_SelectItem"));
1791 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1792 (void)GetEventHandler()->ProcessEvent(event
);
1795 //else: program vetoed the change
1799 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1802 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1805 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1807 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1809 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1813 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1818 void wxTreeCtrl::DeleteTextCtrl()
1822 // the HWND corresponding to this control is deleted by the tree
1823 // control itself and we don't know when exactly this happens, so check
1824 // if the window still exists before calling UnsubclassWin()
1825 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1827 m_textCtrl
->SetHWND(0);
1830 m_textCtrl
->UnsubclassWin();
1831 m_textCtrl
->SetHWND(0);
1839 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1840 wxClassInfo
*textControlClass
)
1842 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1847 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1848 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1850 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1859 // textctrl is subclassed in MSWOnNotify
1863 // End label editing, optionally cancelling the edit
1864 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1866 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1871 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1873 TV_HITTESTINFO hitTestInfo
;
1874 hitTestInfo
.pt
.x
= (int)point
.x
;
1875 hitTestInfo
.pt
.y
= (int)point
.y
;
1877 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1882 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1883 flags |= wxTREE_HITTEST_##flag
1885 TRANSLATE_FLAG(ABOVE
);
1886 TRANSLATE_FLAG(BELOW
);
1887 TRANSLATE_FLAG(NOWHERE
);
1888 TRANSLATE_FLAG(ONITEMBUTTON
);
1889 TRANSLATE_FLAG(ONITEMICON
);
1890 TRANSLATE_FLAG(ONITEMINDENT
);
1891 TRANSLATE_FLAG(ONITEMLABEL
);
1892 TRANSLATE_FLAG(ONITEMRIGHT
);
1893 TRANSLATE_FLAG(ONITEMSTATEICON
);
1894 TRANSLATE_FLAG(TOLEFT
);
1895 TRANSLATE_FLAG(TORIGHT
);
1897 #undef TRANSLATE_FLAG
1899 return wxTreeItemId(hitTestInfo
.hItem
);
1902 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1904 bool textOnly
) const
1908 // Virtual root items have no bounding rectangle
1909 if ( IS_VIRTUAL_ROOT(item
) )
1914 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1917 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1923 // couldn't retrieve rect: for example, item isn't visible
1928 // ----------------------------------------------------------------------------
1930 // ----------------------------------------------------------------------------
1932 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1933 // functions such as IsDataIndirect()
1934 class wxTreeSortHelper
1937 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1940 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1942 return ((wxTreeItemParam
*)lParam
)->GetItem();
1946 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1950 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1951 wxT("sorting tree without data doesn't make sense") );
1953 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1955 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1956 GetIdFromData(pItem2
));
1959 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1961 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1963 // rely on the fact that TreeView_SortChildren does the same thing as our
1964 // default behaviour, i.e. sorts items alphabetically and so call it
1965 // directly if we're not in derived class (much more efficient!)
1966 // RN: Note that if you find you're code doesn't sort as expected this
1967 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1968 // combo for your derived wxTreeCtrl if will sort without
1970 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1972 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1977 tvSort
.hParent
= HITEM(item
);
1978 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1979 tvSort
.lParam
= (LPARAM
)this;
1980 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1984 // ----------------------------------------------------------------------------
1986 // ----------------------------------------------------------------------------
1988 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1990 if ( msg
->message
== WM_KEYDOWN
)
1992 if ( msg
->wParam
== VK_RETURN
)
1994 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1999 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2002 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2004 if ( cmd
== EN_UPDATE
)
2006 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2007 event
.SetEventObject( this );
2008 ProcessCommand(event
);
2010 else if ( cmd
== EN_KILLFOCUS
)
2012 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2013 event
.SetEventObject( this );
2014 ProcessCommand(event
);
2022 // command processed
2026 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2027 // only do it during dragging, minimize wxWin overhead (this is important for
2028 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2029 // instead of passing by wxWin events
2030 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2032 bool processed
= false;
2034 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2036 // This message is sent after a right-click, or when the "menu" key is pressed
2037 if ( nMsg
== WM_CONTEXTMENU
)
2039 int x
= GET_X_LPARAM(lParam
),
2040 y
= GET_Y_LPARAM(lParam
);
2041 // Convert the screen point to a client point
2042 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2044 // can't use GetSelection() here as it would assert in multiselect mode
2045 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this,
2046 wxTreeItemId(TreeView_GetSelection(GetHwnd())));
2048 // Get the bounding rectangle for the item, including the non-text areas
2050 GetBoundingRect(event
.m_item
, ItemRect
, false);
2051 // If the point is inside the bounding rectangle, use it as the click position.
2052 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2053 if (ItemRect
.Inside(MenuPoint
))
2055 event
.m_pointDrag
= MenuPoint
;
2057 // Use the Explorer standard of putting the menu at the left edge of the text,
2058 // in the vertical middle of the text. Should be the case for the "menu" key
2061 // Use the bounding rectangle of only the text part
2062 GetBoundingRect(event
.m_item
, ItemRect
, true);
2063 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2066 if ( GetEventHandler()->ProcessEvent(event
) )
2068 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2070 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2072 // we only process mouse messages here and these parameters have the
2073 // same meaning for all of them
2074 int x
= GET_X_LPARAM(lParam
),
2075 y
= GET_Y_LPARAM(lParam
);
2076 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2078 TV_HITTESTINFO tvht
;
2082 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2086 case WM_RBUTTONDOWN
:
2087 // if the item we are about to right click on is not already
2088 // selected or if we click outside of any item, remove the
2089 // entire previous selection
2090 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2095 // select item and set the focus to the
2096 // newly selected item
2097 ::SelectItem(GetHwnd(), htItem
);
2098 ::SetFocus(GetHwnd(), htItem
);
2101 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2102 case WM_LBUTTONDOWN
:
2103 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2105 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2106 m_ptClick
= wxPoint(x
, y
);
2108 if ( wParam
& MK_CONTROL
)
2112 // toggle selected state
2113 ::ToggleItemSelection(GetHwnd(), htItem
);
2115 ::SetFocus(GetHwnd(), htItem
);
2117 // reset on any click without Shift
2118 m_htSelStart
.Unset();
2122 else if ( wParam
& MK_SHIFT
)
2124 // this selects all items between the starting one and
2127 if ( !m_htSelStart
)
2129 // take the focused item
2130 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2134 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2135 !(wParam
& MK_CONTROL
));
2137 ::SelectItem(GetHwnd(), htItem
);
2139 ::SetFocus(GetHwnd(), htItem
);
2143 else // normal click
2145 // avoid doing anything if we click on the only
2146 // currently selected item
2150 wxArrayTreeItemIds selections
;
2151 size_t count
= GetSelections(selections
);
2154 HITEM(selections
[0]) != htItem
)
2156 // clear the previously selected items, if the
2157 // user clicked outside of the present selection.
2158 // otherwise, perform the deselection on mouse-up.
2159 // this allows multiple drag and drop to work.
2161 if (!IsItemSelected(GetHwnd(), htItem
))
2165 // prevent the click from starting in-place editing
2166 // which should only happen if we click on the
2167 // already selected item (and nothing else is
2170 TreeView_SelectItem(GetHwnd(), 0);
2171 ::SelectItem(GetHwnd(), htItem
);
2173 ::SetFocus(GetHwnd(), htItem
);
2177 // reset on any click without Shift
2178 m_htSelStart
.Unset();
2182 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2186 if ( m_htClickedItem
)
2188 int cx
= abs(m_ptClick
.x
- x
);
2189 int cy
= abs(m_ptClick
.y
- y
);
2191 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2193 HWND pWnd
= ::GetParent( GetHwnd() );
2198 tv
.hdr
.hwndFrom
= GetHwnd();
2199 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2200 tv
.hdr
.code
= TVN_BEGINDRAG
;
2202 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2205 ZeroMemory(&tviAux
, sizeof(tviAux
));
2206 tviAux
.hItem
= HITEM(m_htClickedItem
);
2207 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2208 tviAux
.stateMask
= 0xffffffff;
2209 TreeView_GetItem( GetHwnd(), &tviAux
);
2211 tv
.itemNew
.state
= tviAux
.state
;
2212 tv
.itemNew
.lParam
= tviAux
.lParam
;
2217 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2219 m_htClickedItem
.Unset();
2222 #endif // __WXWINCE__
2226 m_dragImage
->Move(wxPoint(x
, y
));
2229 // highlight the item as target (hiding drag image is
2230 // necessary - otherwise the display will be corrupted)
2231 m_dragImage
->Hide();
2232 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2233 m_dragImage
->Show();
2240 // facilitates multiple drag-and-drop
2241 if (htItem
&& isMultiple
)
2243 wxArrayTreeItemIds selections
;
2244 size_t count
= GetSelections(selections
);
2247 !(wParam
& MK_CONTROL
) &&
2248 !(wParam
& MK_SHIFT
))
2251 TreeView_SelectItem(GetHwnd(), htItem
);
2252 ::SelectItem(GetHwnd(), htItem
);
2253 ::SetFocus(GetHwnd(), htItem
);
2255 m_htClickedItem
.Unset();
2263 m_dragImage
->EndDrag();
2267 // generate the drag end event
2268 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2269 (void)GetEventHandler()->ProcessEvent(event
);
2271 // if we don't do it, the tree seems to think that 2 items
2272 // are selected simultaneously which is quite weird
2273 TreeView_SelectDropTarget(GetHwnd(), 0);
2278 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2279 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2281 // the tree control greys out the selected item when it loses focus and
2282 // paints it as selected again when it regains it, but it won't do it
2283 // for the other items itself - help it
2284 wxArrayTreeItemIds selections
;
2285 size_t count
= GetSelections(selections
);
2287 for ( size_t n
= 0; n
< count
; n
++ )
2289 // TreeView_GetItemRect() will return false if item is not visible,
2290 // which may happen perfectly well
2291 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2294 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2298 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2300 bool bCtrl
= wxIsCtrlDown(),
2301 bShift
= wxIsShiftDown();
2303 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2309 ::ToggleItemSelection(GetHwnd(), htSel
);
2315 ::SelectItem(GetHwnd(), htSel
);
2323 if ( !bCtrl
&& !bShift
)
2325 // no modifiers, just clear selection and then let the default
2326 // processing to take place
2331 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2333 HTREEITEM htNext
= (HTREEITEM
)
2334 TreeView_GetNextItem
2338 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2344 // at the top/bottom
2350 if ( !m_htSelStart
)
2351 m_htSelStart
= htSel
;
2353 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2357 // without changing selection
2358 ::SetFocus(GetHwnd(), htNext
);
2369 // TODO: handle Shift/Ctrl with these keys
2370 if ( !bCtrl
&& !bShift
)
2374 m_htSelStart
.Unset();
2378 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2379 else if ( nMsg
== WM_COMMAND
)
2381 // if we receive a EN_KILLFOCUS command from the in-place edit control
2382 // used for label editing, make sure to end editing
2385 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2387 if ( cmd
== EN_KILLFOCUS
)
2389 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2399 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2405 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2407 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2408 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2409 // the parent window, not us, which completely breaks everything so simply
2410 // don't let it see this message at all
2411 if ( nMsg
== WM_RBUTTONDOWN
)
2414 // but because of the above we don't get NM_RCLICK which is normally
2415 // generated by tree window proc when the modal loop mentioned above ends
2416 // because the mouse is released -- synthesize it ourselves instead
2417 if ( nMsg
== WM_RBUTTONUP
)
2420 hdr
.hwndFrom
= GetHwnd();
2421 hdr
.idFrom
= GetId();
2422 hdr
.code
= NM_RCLICK
;
2425 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2427 // continue as usual
2430 if ( nMsg
== WM_CHAR
)
2432 // also don't let the control process Space and Return keys because it
2433 // doesn't do anything useful with them anyhow but always beeps
2434 // annoyingly when it receives them and there is no way to turn it off
2435 // simply if you just process TREEITEM_ACTIVATED event to which Space
2436 // and Enter presses are mapped in your code
2437 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2441 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2444 // process WM_NOTIFY Windows message
2445 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2447 wxTreeEvent
event(wxEVT_NULL
, this);
2448 wxEventType eventType
= wxEVT_NULL
;
2449 NMHDR
*hdr
= (NMHDR
*)lParam
;
2451 switch ( hdr
->code
)
2454 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2457 case TVN_BEGINRDRAG
:
2459 if ( eventType
== wxEVT_NULL
)
2460 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2461 //else: left drag, already set above
2463 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2465 event
.m_item
= tv
->itemNew
.hItem
;
2466 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2468 // don't allow dragging by default: the user code must
2469 // explicitly say that it wants to allow it to avoid breaking
2475 case TVN_BEGINLABELEDIT
:
2477 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2478 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2480 // although the user event handler may still veto it, it is
2481 // important to set it now so that calls to SetItemText() from
2482 // the event handler would change the text controls contents
2484 event
.m_item
= info
->item
.hItem
;
2485 event
.m_label
= info
->item
.pszText
;
2486 event
.m_editCancelled
= false;
2490 case TVN_DELETEITEM
:
2492 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2493 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2495 event
.m_item
= tv
->itemOld
.hItem
;
2499 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2500 if ( it
!= m_attrs
.end() )
2509 case TVN_ENDLABELEDIT
:
2511 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2512 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2514 event
.m_item
= info
->item
.hItem
;
2515 event
.m_label
= info
->item
.pszText
;
2516 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2521 // These *must* not be removed or TVN_GETINFOTIP will
2522 // not be processed each time the mouse is moved
2523 // and the tooltip will only ever update once.
2532 #ifdef TVN_GETINFOTIP
2533 case TVN_GETINFOTIP
:
2535 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2536 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2538 // Which item are we trying to get a tooltip for?
2539 event
.m_item
= info
->hItem
;
2546 case TVN_GETDISPINFO
:
2547 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2550 case TVN_SETDISPINFO
:
2552 if ( eventType
== wxEVT_NULL
)
2553 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2554 //else: get, already set above
2556 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2558 event
.m_item
= info
->item
.hItem
;
2562 case TVN_ITEMEXPANDING
:
2563 case TVN_ITEMEXPANDED
:
2565 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2568 switch ( tv
->action
)
2571 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2579 what
= IDX_COLLAPSE
;
2583 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2586 eventType
= gs_expandEvents
[what
][how
];
2588 event
.m_item
= tv
->itemNew
.hItem
;
2594 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2595 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2597 // fabricate the lParam and wParam parameters sufficiently
2598 // similar to the ones from a "real" WM_KEYDOWN so that
2599 // CreateKeyEvent() works correctly
2600 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2601 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2603 WXWPARAM wParam
= info
->wVKey
;
2605 int keyCode
= wxCharCodeMSWToWX(wParam
);
2608 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2613 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2618 // a separate event for Space/Return
2619 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2620 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2623 if ( !HasFlag(wxTR_MULTIPLE
) )
2624 item
= GetSelection();
2626 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2628 (void)GetEventHandler()->ProcessEvent(event2
);
2633 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2634 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2635 // we have to handle both messages:
2636 case TVN_SELCHANGEDA
:
2637 case TVN_SELCHANGEDW
:
2638 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2641 case TVN_SELCHANGINGA
:
2642 case TVN_SELCHANGINGW
:
2644 if ( eventType
== wxEVT_NULL
)
2645 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2646 //else: already set above
2648 if (hdr
->code
== TVN_SELCHANGINGW
||
2649 hdr
->code
== TVN_SELCHANGEDW
)
2651 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2652 event
.m_item
= tv
->itemNew
.hItem
;
2653 event
.m_itemOld
= tv
->itemOld
.hItem
;
2657 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2658 event
.m_item
= tv
->itemNew
.hItem
;
2659 event
.m_itemOld
= tv
->itemOld
.hItem
;
2664 // instead of explicitly checking for _WIN32_IE, check if the
2665 // required symbols are available in the headers
2666 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2669 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2670 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2671 switch ( nmcd
.dwDrawStage
)
2674 // if we've got any items with non standard attributes,
2675 // notify us before painting each item
2676 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2680 case CDDS_ITEMPREPAINT
:
2682 wxMapTreeAttr::iterator
2683 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2685 if ( it
== m_attrs
.end() )
2687 // nothing to do for this item
2688 *result
= CDRF_DODEFAULT
;
2692 wxTreeItemAttr
* const attr
= it
->second
;
2694 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2695 TVIF_STATE
, TVIS_DROPHILITED
);
2697 const UINT tvItemState
= tvItem
.state
;
2699 // selection colours should override ours,
2700 // otherwise it is too confusing to the user
2701 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2702 !(tvItemState
& TVIS_DROPHILITED
) )
2705 if ( attr
->HasBackgroundColour() )
2707 colBack
= attr
->GetBackgroundColour();
2708 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2712 // but we still want to keep the special foreground
2713 // colour when we don't have focus (we can't keep
2714 // it when we do, it would usually be unreadable on
2715 // the almost inverted bg colour...)
2716 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2717 FindFocus() != this ) &&
2718 !(tvItemState
& TVIS_DROPHILITED
) )
2721 if ( attr
->HasTextColour() )
2723 colText
= attr
->GetTextColour();
2724 lptvcd
->clrText
= wxColourToRGB(colText
);
2728 if ( attr
->HasFont() )
2730 HFONT hFont
= GetHfontOf(attr
->GetFont());
2732 ::SelectObject(nmcd
.hdc
, hFont
);
2734 *result
= CDRF_NEWFONT
;
2736 else // no specific font
2738 *result
= CDRF_DODEFAULT
;
2744 *result
= CDRF_DODEFAULT
;
2748 // we always process it
2750 #endif // have owner drawn support in headers
2754 DWORD pos
= GetMessagePos();
2756 point
.x
= LOWORD(pos
);
2757 point
.y
= HIWORD(pos
);
2758 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2760 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2761 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2763 event
.m_item
= item
;
2764 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2772 TV_HITTESTINFO tvhti
;
2773 ::GetCursorPos(&tvhti
.pt
);
2774 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2775 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2777 if ( tvhti
.flags
& TVHT_ONITEM
)
2779 event
.m_item
= tvhti
.hItem
;
2780 eventType
= (int)hdr
->code
== NM_DBLCLK
2781 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2782 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2784 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2785 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2794 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2797 event
.SetEventType(eventType
);
2799 if ( event
.m_item
.IsOk() )
2800 event
.SetClientObject(GetItemData(event
.m_item
));
2802 bool processed
= GetEventHandler()->ProcessEvent(event
);
2805 switch ( hdr
->code
)
2808 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2809 // the return code of this event handler as the return value for
2810 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2811 // expanded status would never work
2816 case TVN_BEGINRDRAG
:
2817 if ( event
.IsAllowed() )
2819 // normally this is impossible because the m_dragImage is
2820 // deleted once the drag operation is over
2821 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2823 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2824 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2825 m_dragImage
->Show();
2829 case TVN_DELETEITEM
:
2831 // NB: we might process this message using wxWidgets event
2832 // tables, but due to overhead of wxWin event system we
2833 // prefer to do it here ourself (otherwise deleting a tree
2834 // with many items is just too slow)
2835 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2837 wxTreeItemParam
*param
=
2838 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2841 processed
= true; // Make sure we don't get called twice
2845 case TVN_BEGINLABELEDIT
:
2846 // return true to cancel label editing
2847 *result
= !event
.IsAllowed();
2849 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2850 if ( event
.IsAllowed() )
2852 HWND hText
= TreeView_GetEditControl(GetHwnd());
2855 // MBN: if m_textCtrl already has an HWND, it is a stale
2856 // pointer from a previous edit (because the user
2857 // didn't modify the label before dismissing the control,
2858 // and TVN_ENDLABELEDIT was not sent), so delete it
2859 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2862 m_textCtrl
= new wxTextCtrl();
2863 m_textCtrl
->SetParent(this);
2864 m_textCtrl
->SetHWND((WXHWND
)hText
);
2865 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2867 // set wxTE_PROCESS_ENTER style for the text control to
2868 // force it to process the Enter presses itself, otherwise
2869 // they could be stolen from it by the dialog
2871 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2872 | wxTE_PROCESS_ENTER
);
2875 else // we had set m_idEdited before
2881 case TVN_ENDLABELEDIT
:
2882 // return true to set the label to the new string: note that we
2883 // also must pretend that we did process the message or it is going
2884 // to be passed to DefWindowProc() which will happily return false
2885 // cancelling the label change
2886 *result
= event
.IsAllowed();
2889 // ensure that we don't have the text ctrl which is going to be
2895 #ifdef TVN_GETINFOTIP
2896 case TVN_GETINFOTIP
:
2898 // If the user permitted a tooltip change, change it
2899 if (event
.IsAllowed())
2901 SetToolTip(event
.m_label
);
2908 case TVN_SELCHANGING
:
2909 case TVN_ITEMEXPANDING
:
2910 // return true to prevent the action from happening
2911 *result
= !event
.IsAllowed();
2914 case TVN_ITEMEXPANDED
:
2915 // the item is not refreshed properly after expansion when it has
2916 // an image depending on the expanded/collapsed state - bug in
2917 // comctl32.dll or our code?
2919 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2920 wxTreeItemId
id(tv
->itemNew
.hItem
);
2922 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2930 case TVN_GETDISPINFO
:
2931 // NB: so far the user can't set the image himself anyhow, so do it
2932 // anyway - but this may change later
2933 //if ( /* !processed && */ )
2935 wxTreeItemId item
= event
.m_item
;
2936 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2938 const wxTreeItemParam
* const param
= GetItemParam(item
);
2942 if ( info
->item
.mask
& TVIF_IMAGE
)
2947 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2948 : wxTreeItemIcon_Normal
2951 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2953 info
->item
.iSelectedImage
=
2956 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2957 : wxTreeItemIcon_Selected
2964 // for the other messages the return value is ignored and there is
2965 // nothing special to do
2970 // ----------------------------------------------------------------------------
2972 // ----------------------------------------------------------------------------
2974 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2975 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2977 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2980 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2981 tvi
.mask
= TVIF_STATE
;
2982 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2984 // Select the specified state, or -1 == cycle to the next one.
2987 TreeView_GetItem(GetHwnd(), &tvi
);
2989 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2990 if ( state
== m_imageListState
->GetImageCount() )
2994 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2995 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2997 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2999 TreeView_SetItem(GetHwnd(), &tvi
);
3002 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3005 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3006 tvi
.mask
= TVIF_STATE
;
3007 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3008 TreeView_GetItem(GetHwnd(), &tvi
);
3010 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3013 #endif // wxUSE_TREECTRL