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 const wxTreeCtrl
* const tree
= GetTree();
456 // can't visit a virtual node.
457 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
462 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
463 if ( tree
->IsItemChecked(item
) )
465 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
468 m_selections
.Add(item
);
474 size_t GetCount() const { return m_selections
.GetCount(); }
477 wxArrayTreeItemIds
& m_selections
;
479 DECLARE_NO_COPY_CLASS(TraverseSelections
)
482 // internal class for counting tree items
483 class TraverseCounter
: public wxTreeTraversal
486 TraverseCounter(const wxTreeCtrl
*tree
,
487 const wxTreeItemId
& root
,
489 : wxTreeTraversal(tree
)
493 DoTraverse(root
, recursively
);
496 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
503 size_t GetCount() const { return m_count
; }
508 DECLARE_NO_COPY_CLASS(TraverseCounter
)
511 // ----------------------------------------------------------------------------
513 // ----------------------------------------------------------------------------
515 #if wxUSE_EXTENDED_RTTI
516 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
518 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
519 // new style border flags, we put them first to
520 // use them for streaming out
521 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
522 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
523 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
524 wxFLAGS_MEMBER(wxBORDER_RAISED
)
525 wxFLAGS_MEMBER(wxBORDER_STATIC
)
526 wxFLAGS_MEMBER(wxBORDER_NONE
)
528 // old style border flags
529 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
530 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
531 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
532 wxFLAGS_MEMBER(wxRAISED_BORDER
)
533 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
534 wxFLAGS_MEMBER(wxBORDER
)
536 // standard window styles
537 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
538 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
539 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
540 wxFLAGS_MEMBER(wxWANTS_CHARS
)
541 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
542 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
543 wxFLAGS_MEMBER(wxVSCROLL
)
544 wxFLAGS_MEMBER(wxHSCROLL
)
546 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
547 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
548 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
549 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
550 wxFLAGS_MEMBER(wxTR_NO_LINES
)
551 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
552 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
553 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
554 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
555 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
556 wxFLAGS_MEMBER(wxTR_SINGLE
)
557 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
558 wxFLAGS_MEMBER(wxTR_EXTENDED
)
559 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
561 wxEND_FLAGS( wxTreeCtrlStyle
)
563 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
565 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
566 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
567 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
568 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
569 wxEND_PROPERTIES_TABLE()
571 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
572 wxEND_HANDLERS_TABLE()
574 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
576 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
579 // ----------------------------------------------------------------------------
581 // ----------------------------------------------------------------------------
583 // indices in gs_expandEvents table below
598 // handy table for sending events - it has to be initialized during run-time
599 // now so can't be const any more
600 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
603 but logically it's a const table with the following entries:
606 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
607 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
611 // ============================================================================
613 // ============================================================================
615 // ----------------------------------------------------------------------------
617 // ----------------------------------------------------------------------------
619 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
621 if ( !OnVisit(root
) )
624 return Traverse(root
, recursively
);
627 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
629 wxTreeItemIdValue cookie
;
630 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
631 while ( child
.IsOk() )
633 // depth first traversal
634 if ( recursively
&& !Traverse(child
, true) )
637 if ( !OnVisit(child
) )
640 child
= m_tree
->GetNextChild(root
, cookie
);
646 // ----------------------------------------------------------------------------
647 // construction and destruction
648 // ----------------------------------------------------------------------------
650 void wxTreeCtrl::Init()
653 m_hasAnyAttr
= false;
655 m_pVirtualRoot
= NULL
;
657 // initialize the global array of events now as it can't be done statically
658 // with the wxEVT_XXX values being allocated during run-time only
659 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
660 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
661 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
662 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
665 bool wxTreeCtrl::Create(wxWindow
*parent
,
670 const wxValidator
& validator
,
671 const wxString
& name
)
675 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
676 style
|= wxBORDER_SUNKEN
;
678 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
682 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
683 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
685 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
686 wstyle
|= TVS_HASLINES
;
687 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
688 wstyle
|= TVS_HASBUTTONS
;
690 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
691 wstyle
|= TVS_EDITLABELS
;
693 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
694 wstyle
|= TVS_LINESATROOT
;
696 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
698 if ( wxApp::GetComCtl32Version() >= 471 )
699 wstyle
|= TVS_FULLROWSELECT
;
702 // using TVS_CHECKBOXES for emulation of a multiselection tree control
703 // doesn't work without the new enough headers
704 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
705 !defined( __GNUWIN32_OLD__ ) && \
706 !defined( __BORLANDC__ ) && \
707 !defined( __WATCOMC__ ) && \
708 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
710 // we emulate the multiple selection tree controls by using checkboxes: set
711 // up the image list we need for this if we do have multiple selections
712 if ( m_windowStyle
& wxTR_MULTIPLE
)
713 wstyle
|= TVS_CHECKBOXES
;
714 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
716 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
717 // Need so that TVN_GETINFOTIP messages will be sent
718 wstyle
|= TVS_INFOTIP
;
721 // Create the tree control.
722 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
725 #if wxUSE_COMCTL32_SAFELY
726 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
727 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
729 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
730 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
732 // This works around a bug in the Windows tree control whereby for some versions
733 // of comctrl32, setting any colour actually draws the background in black.
734 // This will initialise the background to the system colour.
735 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
736 // Assume the user has an updated comctl32.dll.
737 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
738 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
739 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
743 // VZ: this is some experimental code which may be used to get the
744 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
745 // AFAIK, the standard DLL does about the same thing anyhow.
747 if ( m_windowStyle
& wxTR_MULTIPLE
)
751 // create the DC compatible with the current screen
752 HDC hdcMem
= CreateCompatibleDC(NULL
);
754 // create a mono bitmap of the standard size
755 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
756 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
757 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
758 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
759 1, // # of color planes
760 1, // # bits needed for one pixel
761 0); // array containing colour data
762 SelectObject(hdcMem
, hbmpCheck
);
764 // then draw a check mark into it
765 RECT rect
= { 0, 0, x
, y
};
766 if ( !::DrawFrameControl(hdcMem
, &rect
,
768 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
770 wxLogLastError(wxT("DrawFrameControl(check)"));
773 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
774 imagelistCheckboxes
.Add(bmp
);
776 if ( !::DrawFrameControl(hdcMem
, &rect
,
780 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
783 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
784 imagelistCheckboxes
.Add(bmp
);
790 SetStateImageList(&imagelistCheckboxes
);
794 wxSetCCUnicodeFormat(GetHwnd());
799 wxTreeCtrl::~wxTreeCtrl()
801 // delete any attributes
804 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
806 // prevent TVN_DELETEITEM handler from deleting the attributes again!
807 m_hasAnyAttr
= false;
812 // delete user data to prevent memory leaks
813 // also deletes hidden root node storage.
817 // ----------------------------------------------------------------------------
819 // ----------------------------------------------------------------------------
821 /* static */ wxVisualAttributes
822 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
824 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
826 // common controls have their own default font
827 attrs
.font
= wxGetCCDefaultFont();
833 // simple wrappers which add error checking in debug mode
835 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
837 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
838 _T("can't retrieve virtual root item") );
840 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
842 wxLogLastError(wxT("TreeView_GetItem"));
850 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
852 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
854 wxLogLastError(wxT("TreeView_SetItem"));
858 unsigned int wxTreeCtrl::GetCount() const
860 return (unsigned int)TreeView_GetCount(GetHwnd());
863 unsigned int wxTreeCtrl::GetIndent() const
865 return TreeView_GetIndent(GetHwnd());
868 void wxTreeCtrl::SetIndent(unsigned int indent
)
870 TreeView_SetIndent(GetHwnd(), indent
);
873 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
876 (void) TreeView_SetImageList(GetHwnd(),
877 imageList
? imageList
->GetHIMAGELIST() : 0,
881 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
883 if (m_ownsImageListNormal
)
884 delete m_imageListNormal
;
886 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
887 m_ownsImageListNormal
= false;
890 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
892 if (m_ownsImageListState
) delete m_imageListState
;
893 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
894 m_ownsImageListState
= false;
897 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
898 bool recursively
) const
900 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
902 TraverseCounter
counter(this, item
, recursively
);
903 return counter
.GetCount() - 1;
906 // ----------------------------------------------------------------------------
908 // ----------------------------------------------------------------------------
910 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
912 #if !wxUSE_COMCTL32_SAFELY
913 if ( !wxWindowBase::SetBackgroundColour(colour
) )
916 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
922 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
924 #if !wxUSE_COMCTL32_SAFELY
925 if ( !wxWindowBase::SetForegroundColour(colour
) )
928 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
934 // ----------------------------------------------------------------------------
936 // ----------------------------------------------------------------------------
938 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
940 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
943 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
945 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
947 wxChar buf
[512]; // the size is arbitrary...
949 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
950 tvItem
.pszText
= buf
;
951 tvItem
.cchTextMax
= WXSIZEOF(buf
);
952 if ( !DoGetItem(&tvItem
) )
954 // don't return some garbage which was on stack, but an empty string
958 return wxString(buf
);
961 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
963 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
965 if ( IS_VIRTUAL_ROOT(item
) )
968 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
969 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
972 // when setting the text of the item being edited, the text control should
973 // be updated to reflect the new text as well, otherwise calling
974 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
976 // don't use GetEditControl() here because m_textCtrl is not set yet
977 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
980 if ( item
== m_idEdited
)
982 ::SetWindowText(hwndEdit
, text
);
987 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
988 wxTreeItemIcon which
) const
990 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
992 if ( IsHiddenRoot(item
) )
994 // no images for hidden root item
998 wxTreeItemParam
*param
= GetItemParam(item
);
1000 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
1003 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1004 wxTreeItemIcon which
)
1006 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1007 wxCHECK_RET( which
>= 0 &&
1008 which
< wxTreeItemIcon_Max
,
1009 wxT("invalid image index"));
1012 if ( IsHiddenRoot(item
) )
1014 // no images for hidden root item
1018 wxTreeItemParam
*data
= GetItemParam(item
);
1022 data
->SetImage(image
, which
);
1027 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1029 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1031 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1033 // hidden root may still have data.
1034 if ( IS_VIRTUAL_ROOT(item
) )
1036 return GET_VIRTUAL_ROOT()->GetParam();
1040 if ( !DoGetItem(&tvItem
) )
1045 return (wxTreeItemParam
*)tvItem
.lParam
;
1048 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1050 wxTreeItemParam
*data
= GetItemParam(item
);
1052 return data
? data
->GetData() : NULL
;
1055 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1057 // first, associate this piece of data with this item
1063 wxTreeItemParam
*param
= GetItemParam(item
);
1065 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1067 param
->SetData(data
);
1070 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1072 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1074 if ( IS_VIRTUAL_ROOT(item
) )
1077 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1078 tvItem
.cChildren
= (int)has
;
1082 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1084 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1086 if ( IS_VIRTUAL_ROOT(item
) )
1089 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1090 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1094 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1096 if ( IS_VIRTUAL_ROOT(item
) )
1099 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1100 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1104 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1106 if ( IS_VIRTUAL_ROOT(item
) )
1110 if ( GetBoundingRect(item
, rect
) )
1116 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1118 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1120 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1121 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1124 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1126 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1128 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1129 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1132 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1134 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1136 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1137 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1140 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1141 const wxColour
& col
)
1143 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1145 wxTreeItemAttr
*attr
;
1146 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1147 if ( it
== m_attrs
.end() )
1149 m_hasAnyAttr
= true;
1151 m_attrs
[item
.m_pItem
] =
1152 attr
= new wxTreeItemAttr
;
1159 attr
->SetTextColour(col
);
1164 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1165 const wxColour
& col
)
1167 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1169 wxTreeItemAttr
*attr
;
1170 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1171 if ( it
== m_attrs
.end() )
1173 m_hasAnyAttr
= true;
1175 m_attrs
[item
.m_pItem
] =
1176 attr
= new wxTreeItemAttr
;
1178 else // already in the hash
1183 attr
->SetBackgroundColour(col
);
1188 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1190 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1192 wxTreeItemAttr
*attr
;
1193 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1194 if ( it
== m_attrs
.end() )
1196 m_hasAnyAttr
= true;
1198 m_attrs
[item
.m_pItem
] =
1199 attr
= new wxTreeItemAttr
;
1201 else // already in the hash
1206 attr
->SetFont(font
);
1211 // ----------------------------------------------------------------------------
1213 // ----------------------------------------------------------------------------
1215 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1217 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1219 if ( item
== wxTreeItemId(TVI_ROOT
) )
1221 // virtual (hidden) root is never visible
1225 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1228 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1229 // the HTREEITEM with TVM_GETITEMRECT
1230 *(HTREEITEM
*)&rect
= HITEM(item
);
1232 // true means to get rect for just the text, not the whole line
1233 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1235 // if TVM_GETITEMRECT returned false, then the item is definitely not
1236 // visible (because its parent is not expanded)
1240 // however if it returned true, the item might still be outside the
1241 // currently visible part of the tree, test for it (notice that partly
1242 // visible means visible here)
1243 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1246 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1248 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1250 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1253 return tvItem
.cChildren
!= 0;
1256 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1258 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1260 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1263 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1266 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1268 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1270 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1273 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1276 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1278 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1280 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1283 return (tvItem
.state
& TVIS_BOLD
) != 0;
1286 // ----------------------------------------------------------------------------
1288 // ----------------------------------------------------------------------------
1290 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1292 // Root may be real (visible) or virtual (hidden).
1293 if ( GET_VIRTUAL_ROOT() )
1296 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1299 wxTreeItemId
wxTreeCtrl::GetSelection() const
1301 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1302 wxT("this only works with single selection controls") );
1304 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1307 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1309 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1313 if ( IS_VIRTUAL_ROOT(item
) )
1315 // no parent for the virtual root
1320 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1321 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1323 // the top level items should have the virtual root as their parent
1328 return wxTreeItemId(hItem
);
1331 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1332 wxTreeItemIdValue
& cookie
) const
1334 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1336 // remember the last child returned in 'cookie'
1337 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1339 return wxTreeItemId(cookie
);
1342 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1343 wxTreeItemIdValue
& cookie
) const
1345 wxTreeItemId
fromCookie(cookie
);
1347 HTREEITEM hitem
= HITEM(fromCookie
);
1349 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1351 wxTreeItemId
item(hitem
);
1353 cookie
= item
.m_pItem
;
1358 #if WXWIN_COMPATIBILITY_2_4
1360 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1363 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1365 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1367 return wxTreeItemId((void *)cookie
);
1370 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1373 wxTreeItemId
fromCookie((void *)cookie
);
1375 HTREEITEM hitem
= HITEM(fromCookie
);
1377 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1379 wxTreeItemId
item(hitem
);
1381 cookie
= (long)item
.m_pItem
;
1386 #endif // WXWIN_COMPATIBILITY_2_4
1388 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1390 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1392 // can this be done more efficiently?
1393 wxTreeItemIdValue cookie
;
1395 wxTreeItemId childLast
,
1396 child
= GetFirstChild(item
, cookie
);
1397 while ( child
.IsOk() )
1400 child
= GetNextChild(item
, cookie
);
1406 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1408 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1409 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1412 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1414 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1415 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1418 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1420 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1423 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1425 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1426 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1428 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1431 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1433 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1434 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1436 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1439 // ----------------------------------------------------------------------------
1440 // multiple selections emulation
1441 // ----------------------------------------------------------------------------
1443 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1445 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1447 // receive the desired information.
1448 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1451 // state image indices are 1 based
1452 return ((tvItem
.state
>> 12) - 1) == 1;
1455 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1457 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1459 // receive the desired information.
1460 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1464 // state images are one-based
1465 tvItem
.state
= (check
? 2 : 1) << 12;
1470 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1472 TraverseSelections
selector(this, selections
);
1474 return selector
.GetCount();
1477 // ----------------------------------------------------------------------------
1479 // ----------------------------------------------------------------------------
1481 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1482 const wxTreeItemId
& hInsertAfter
,
1483 const wxString
& text
,
1484 int image
, int selectedImage
,
1485 wxTreeItemData
*data
)
1487 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1489 _T("can't have more than one root in the tree") );
1491 TV_INSERTSTRUCT tvIns
;
1492 tvIns
.hParent
= HITEM(parent
);
1493 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1495 // this is how we insert the item as the first child: supply a NULL
1497 if ( !tvIns
.hInsertAfter
)
1499 tvIns
.hInsertAfter
= TVI_FIRST
;
1503 if ( !text
.empty() )
1506 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1510 tvIns
.item
.pszText
= NULL
;
1511 tvIns
.item
.cchTextMax
= 0;
1514 // create the param which will store the other item parameters
1515 wxTreeItemParam
*param
= new wxTreeItemParam
;
1517 // we return the images on demand as they depend on whether the item is
1518 // expanded or collapsed too in our case
1519 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1520 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1521 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1523 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1524 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1527 tvIns
.item
.lParam
= (LPARAM
)param
;
1528 tvIns
.item
.mask
= mask
;
1530 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1533 wxLogLastError(wxT("TreeView_InsertItem"));
1536 // associate the application tree item with Win32 tree item handle
1539 // setup wxTreeItemData
1542 param
->SetData(data
);
1546 return wxTreeItemId(id
);
1549 // for compatibility only
1550 #if WXWIN_COMPATIBILITY_2_4
1552 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1554 SetImageList(imageList
);
1557 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1559 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1562 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1564 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1567 #endif // WXWIN_COMPATIBILITY_2_4
1569 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1570 int image
, int selectedImage
,
1571 wxTreeItemData
*data
)
1574 if ( HasFlag(wxTR_HIDE_ROOT
) )
1576 // create a virtual root item, the parent for all the others
1577 wxTreeItemParam
*param
= new wxTreeItemParam
;
1578 param
->SetData(data
);
1580 m_pVirtualRoot
= new wxVirtualNode(param
);
1585 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1586 text
, image
, selectedImage
, data
);
1589 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1591 const wxString
& text
,
1592 int image
, int selectedImage
,
1593 wxTreeItemData
*data
)
1595 wxTreeItemId idPrev
;
1596 if ( index
== (size_t)-1 )
1598 // special value: append to the end
1601 else // find the item from index
1603 wxTreeItemIdValue cookie
;
1604 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1605 while ( index
!= 0 && idCur
.IsOk() )
1610 idCur
= GetNextChild(parent
, cookie
);
1613 // assert, not check: if the index is invalid, we will append the item
1615 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1618 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1621 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1623 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1625 wxLogLastError(wxT("TreeView_DeleteItem"));
1629 // delete all children (but don't delete the item itself)
1630 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1632 wxTreeItemIdValue cookie
;
1634 wxArrayTreeItemIds children
;
1635 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1636 while ( child
.IsOk() )
1638 children
.Add(child
);
1640 child
= GetNextChild(item
, cookie
);
1643 size_t nCount
= children
.Count();
1644 for ( size_t n
= 0; n
< nCount
; n
++ )
1646 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1648 wxLogLastError(wxT("TreeView_DeleteItem"));
1653 void wxTreeCtrl::DeleteAllItems()
1655 // delete the "virtual" root item.
1656 if ( GET_VIRTUAL_ROOT() )
1658 delete GET_VIRTUAL_ROOT();
1659 m_pVirtualRoot
= NULL
;
1662 // and all the real items
1664 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1666 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1670 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1672 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1673 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1674 flag
== TVE_EXPAND
||
1676 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1678 // A hidden root can be neither expanded nor collapsed.
1679 wxCHECK_RET( !IsHiddenRoot(item
),
1680 wxT("Can't expand/collapse hidden root node!") );
1682 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1683 // emulate them. This behaviour has changed slightly with comctl32.dll
1684 // v 4.70 - now it does send them but only the first time. To maintain
1685 // compatible behaviour and also in order to not have surprises with the
1686 // future versions, don't rely on this and still do everything ourselves.
1687 // To avoid that the messages be sent twice when the item is expanded for
1688 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1690 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1694 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1696 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1698 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1702 (void)GetEventHandler()->ProcessEvent(event
);
1704 //else: change didn't took place, so do nothing at all
1707 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1709 DoExpand(item
, TVE_EXPAND
);
1712 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1714 DoExpand(item
, TVE_COLLAPSE
);
1717 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1719 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1722 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1724 DoExpand(item
, TVE_TOGGLE
);
1727 #if WXWIN_COMPATIBILITY_2_4
1729 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1731 DoExpand(item
, action
);
1736 void wxTreeCtrl::Unselect()
1738 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1739 wxT("doesn't make sense, may be you want UnselectAll()?") );
1741 // just remove the selection
1742 SelectItem(wxTreeItemId());
1745 void wxTreeCtrl::UnselectAll()
1747 if ( m_windowStyle
& wxTR_MULTIPLE
)
1749 wxArrayTreeItemIds selections
;
1750 size_t count
= GetSelections(selections
);
1751 for ( size_t n
= 0; n
< count
; n
++ )
1753 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1754 SetItemCheck(HITEM(selections
[n
]), false);
1755 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1756 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1757 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1760 m_htSelStart
.Unset();
1764 // just remove the selection
1769 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1771 if ( m_windowStyle
& wxTR_MULTIPLE
)
1773 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1774 // selecting the item means checking it
1775 SetItemCheck(item
, select
);
1776 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1777 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1778 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1782 wxASSERT_MSG( select
,
1783 _T("SelectItem(false) works only for multiselect") );
1785 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1786 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1787 // send them ourselves
1789 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1790 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1792 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1794 wxLogLastError(wxT("TreeView_SelectItem"));
1798 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1799 (void)GetEventHandler()->ProcessEvent(event
);
1802 //else: program vetoed the change
1806 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1808 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1811 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1814 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1816 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1818 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1822 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1827 void wxTreeCtrl::DeleteTextCtrl()
1831 // the HWND corresponding to this control is deleted by the tree
1832 // control itself and we don't know when exactly this happens, so check
1833 // if the window still exists before calling UnsubclassWin()
1834 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1836 m_textCtrl
->SetHWND(0);
1839 m_textCtrl
->UnsubclassWin();
1840 m_textCtrl
->SetHWND(0);
1848 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1849 wxClassInfo
*textControlClass
)
1851 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1856 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1857 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1859 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1868 // textctrl is subclassed in MSWOnNotify
1872 // End label editing, optionally cancelling the edit
1873 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1875 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1880 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1882 TV_HITTESTINFO hitTestInfo
;
1883 hitTestInfo
.pt
.x
= (int)point
.x
;
1884 hitTestInfo
.pt
.y
= (int)point
.y
;
1886 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1891 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1892 flags |= wxTREE_HITTEST_##flag
1894 TRANSLATE_FLAG(ABOVE
);
1895 TRANSLATE_FLAG(BELOW
);
1896 TRANSLATE_FLAG(NOWHERE
);
1897 TRANSLATE_FLAG(ONITEMBUTTON
);
1898 TRANSLATE_FLAG(ONITEMICON
);
1899 TRANSLATE_FLAG(ONITEMINDENT
);
1900 TRANSLATE_FLAG(ONITEMLABEL
);
1901 TRANSLATE_FLAG(ONITEMRIGHT
);
1902 TRANSLATE_FLAG(ONITEMSTATEICON
);
1903 TRANSLATE_FLAG(TOLEFT
);
1904 TRANSLATE_FLAG(TORIGHT
);
1906 #undef TRANSLATE_FLAG
1908 return wxTreeItemId(hitTestInfo
.hItem
);
1911 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1913 bool textOnly
) const
1917 // Virtual root items have no bounding rectangle
1918 if ( IS_VIRTUAL_ROOT(item
) )
1923 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1926 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1932 // couldn't retrieve rect: for example, item isn't visible
1937 // ----------------------------------------------------------------------------
1939 // ----------------------------------------------------------------------------
1941 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1942 // functions such as IsDataIndirect()
1943 class wxTreeSortHelper
1946 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1949 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1951 return ((wxTreeItemParam
*)lParam
)->GetItem();
1955 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1959 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1960 wxT("sorting tree without data doesn't make sense") );
1962 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1964 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1965 GetIdFromData(pItem2
));
1968 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1970 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1972 // rely on the fact that TreeView_SortChildren does the same thing as our
1973 // default behaviour, i.e. sorts items alphabetically and so call it
1974 // directly if we're not in derived class (much more efficient!)
1975 // RN: Note that if you find you're code doesn't sort as expected this
1976 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1977 // combo for your derived wxTreeCtrl if will sort without
1979 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1981 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1986 tvSort
.hParent
= HITEM(item
);
1987 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1988 tvSort
.lParam
= (LPARAM
)this;
1989 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1993 // ----------------------------------------------------------------------------
1995 // ----------------------------------------------------------------------------
1997 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1999 if ( msg
->message
== WM_KEYDOWN
)
2001 if ( msg
->wParam
== VK_RETURN
)
2003 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2008 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2011 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2013 if ( cmd
== EN_UPDATE
)
2015 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2016 event
.SetEventObject( this );
2017 ProcessCommand(event
);
2019 else if ( cmd
== EN_KILLFOCUS
)
2021 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2022 event
.SetEventObject( this );
2023 ProcessCommand(event
);
2031 // command processed
2035 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2036 // only do it during dragging, minimize wxWin overhead (this is important for
2037 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2038 // instead of passing by wxWin events
2039 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2041 bool processed
= false;
2043 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2045 // This message is sent after a right-click, or when the "menu" key is pressed
2046 if ( nMsg
== WM_CONTEXTMENU
)
2048 int x
= GET_X_LPARAM(lParam
),
2049 y
= GET_Y_LPARAM(lParam
);
2050 // Convert the screen point to a client point
2051 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2053 // can't use GetSelection() here as it would assert in multiselect mode
2054 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this,
2055 wxTreeItemId(TreeView_GetSelection(GetHwnd())));
2057 // Get the bounding rectangle for the item, including the non-text areas
2059 GetBoundingRect(event
.m_item
, ItemRect
, false);
2060 // If the point is inside the bounding rectangle, use it as the click position.
2061 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2062 if (ItemRect
.Inside(MenuPoint
))
2064 event
.m_pointDrag
= MenuPoint
;
2066 // Use the Explorer standard of putting the menu at the left edge of the text,
2067 // in the vertical middle of the text. Should be the case for the "menu" key
2070 // Use the bounding rectangle of only the text part
2071 GetBoundingRect(event
.m_item
, ItemRect
, true);
2072 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2075 if ( GetEventHandler()->ProcessEvent(event
) )
2077 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2079 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2081 // we only process mouse messages here and these parameters have the
2082 // same meaning for all of them
2083 int x
= GET_X_LPARAM(lParam
),
2084 y
= GET_Y_LPARAM(lParam
);
2085 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2087 TV_HITTESTINFO tvht
;
2091 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2095 case WM_RBUTTONDOWN
:
2096 // if the item we are about to right click on is not already
2097 // selected or if we click outside of any item, remove the
2098 // entire previous selection
2099 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2104 // select item and set the focus to the
2105 // newly selected item
2106 ::SelectItem(GetHwnd(), htItem
);
2107 ::SetFocus(GetHwnd(), htItem
);
2110 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2111 case WM_LBUTTONDOWN
:
2112 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2114 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2115 m_ptClick
= wxPoint(x
, y
);
2117 if ( wParam
& MK_CONTROL
)
2121 // toggle selected state
2122 ::ToggleItemSelection(GetHwnd(), htItem
);
2124 ::SetFocus(GetHwnd(), htItem
);
2126 // reset on any click without Shift
2127 m_htSelStart
.Unset();
2131 else if ( wParam
& MK_SHIFT
)
2133 // this selects all items between the starting one and
2136 if ( !m_htSelStart
)
2138 // take the focused item
2139 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2143 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2144 !(wParam
& MK_CONTROL
));
2146 ::SelectItem(GetHwnd(), htItem
);
2148 ::SetFocus(GetHwnd(), htItem
);
2152 else // normal click
2154 // avoid doing anything if we click on the only
2155 // currently selected item
2159 wxArrayTreeItemIds selections
;
2160 size_t count
= GetSelections(selections
);
2163 HITEM(selections
[0]) != htItem
)
2165 // clear the previously selected items, if the
2166 // user clicked outside of the present selection.
2167 // otherwise, perform the deselection on mouse-up.
2168 // this allows multiple drag and drop to work.
2170 if (!IsItemSelected(GetHwnd(), htItem
))
2174 // prevent the click from starting in-place editing
2175 // which should only happen if we click on the
2176 // already selected item (and nothing else is
2179 TreeView_SelectItem(GetHwnd(), 0);
2180 ::SelectItem(GetHwnd(), htItem
);
2182 ::SetFocus(GetHwnd(), htItem
);
2186 // reset on any click without Shift
2187 m_htSelStart
.Unset();
2191 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2195 if ( m_htClickedItem
)
2197 int cx
= abs(m_ptClick
.x
- x
);
2198 int cy
= abs(m_ptClick
.y
- y
);
2200 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2202 HWND pWnd
= ::GetParent( GetHwnd() );
2207 tv
.hdr
.hwndFrom
= GetHwnd();
2208 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2209 tv
.hdr
.code
= TVN_BEGINDRAG
;
2211 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2214 ZeroMemory(&tviAux
, sizeof(tviAux
));
2215 tviAux
.hItem
= HITEM(m_htClickedItem
);
2216 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2217 tviAux
.stateMask
= 0xffffffff;
2218 TreeView_GetItem( GetHwnd(), &tviAux
);
2220 tv
.itemNew
.state
= tviAux
.state
;
2221 tv
.itemNew
.lParam
= tviAux
.lParam
;
2226 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2228 m_htClickedItem
.Unset();
2231 #endif // __WXWINCE__
2235 m_dragImage
->Move(wxPoint(x
, y
));
2238 // highlight the item as target (hiding drag image is
2239 // necessary - otherwise the display will be corrupted)
2240 m_dragImage
->Hide();
2241 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2242 m_dragImage
->Show();
2249 // facilitates multiple drag-and-drop
2250 if (htItem
&& isMultiple
)
2252 wxArrayTreeItemIds selections
;
2253 size_t count
= GetSelections(selections
);
2256 !(wParam
& MK_CONTROL
) &&
2257 !(wParam
& MK_SHIFT
))
2260 TreeView_SelectItem(GetHwnd(), htItem
);
2261 ::SelectItem(GetHwnd(), htItem
);
2262 ::SetFocus(GetHwnd(), htItem
);
2264 m_htClickedItem
.Unset();
2272 m_dragImage
->EndDrag();
2276 // generate the drag end event
2277 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2278 (void)GetEventHandler()->ProcessEvent(event
);
2280 // if we don't do it, the tree seems to think that 2 items
2281 // are selected simultaneously which is quite weird
2282 TreeView_SelectDropTarget(GetHwnd(), 0);
2287 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2288 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2290 // the tree control greys out the selected item when it loses focus and
2291 // paints it as selected again when it regains it, but it won't do it
2292 // for the other items itself - help it
2293 wxArrayTreeItemIds selections
;
2294 size_t count
= GetSelections(selections
);
2296 for ( size_t n
= 0; n
< count
; n
++ )
2298 // TreeView_GetItemRect() will return false if item is not visible,
2299 // which may happen perfectly well
2300 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2303 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2307 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2309 bool bCtrl
= wxIsCtrlDown(),
2310 bShift
= wxIsShiftDown();
2312 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2318 ::ToggleItemSelection(GetHwnd(), htSel
);
2324 ::SelectItem(GetHwnd(), htSel
);
2332 if ( !bCtrl
&& !bShift
)
2334 // no modifiers, just clear selection and then let the default
2335 // processing to take place
2340 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2342 HTREEITEM htNext
= (HTREEITEM
)
2343 TreeView_GetNextItem
2347 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2353 // at the top/bottom
2359 if ( !m_htSelStart
)
2360 m_htSelStart
= htSel
;
2362 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2366 // without changing selection
2367 ::SetFocus(GetHwnd(), htNext
);
2378 // TODO: handle Shift/Ctrl with these keys
2379 if ( !bCtrl
&& !bShift
)
2383 m_htSelStart
.Unset();
2387 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2388 else if ( nMsg
== WM_COMMAND
)
2390 // if we receive a EN_KILLFOCUS command from the in-place edit control
2391 // used for label editing, make sure to end editing
2394 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2396 if ( cmd
== EN_KILLFOCUS
)
2398 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2408 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2414 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2416 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2417 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2418 // the parent window, not us, which completely breaks everything so simply
2419 // don't let it see this message at all
2420 if ( nMsg
== WM_RBUTTONDOWN
)
2423 // but because of the above we don't get NM_RCLICK which is normally
2424 // generated by tree window proc when the modal loop mentioned above ends
2425 // because the mouse is released -- synthesize it ourselves instead
2426 if ( nMsg
== WM_RBUTTONUP
)
2429 hdr
.hwndFrom
= GetHwnd();
2430 hdr
.idFrom
= GetId();
2431 hdr
.code
= NM_RCLICK
;
2434 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2436 // continue as usual
2439 if ( nMsg
== WM_CHAR
)
2441 // also don't let the control process Space and Return keys because it
2442 // doesn't do anything useful with them anyhow but always beeps
2443 // annoyingly when it receives them and there is no way to turn it off
2444 // simply if you just process TREEITEM_ACTIVATED event to which Space
2445 // and Enter presses are mapped in your code
2446 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2450 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2453 // process WM_NOTIFY Windows message
2454 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2456 wxTreeEvent
event(wxEVT_NULL
, this);
2457 wxEventType eventType
= wxEVT_NULL
;
2458 NMHDR
*hdr
= (NMHDR
*)lParam
;
2460 switch ( hdr
->code
)
2463 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2466 case TVN_BEGINRDRAG
:
2468 if ( eventType
== wxEVT_NULL
)
2469 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2470 //else: left drag, already set above
2472 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2474 event
.m_item
= tv
->itemNew
.hItem
;
2475 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2477 // don't allow dragging by default: the user code must
2478 // explicitly say that it wants to allow it to avoid breaking
2484 case TVN_BEGINLABELEDIT
:
2486 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2487 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2489 // although the user event handler may still veto it, it is
2490 // important to set it now so that calls to SetItemText() from
2491 // the event handler would change the text controls contents
2493 event
.m_item
= info
->item
.hItem
;
2494 event
.m_label
= info
->item
.pszText
;
2495 event
.m_editCancelled
= false;
2499 case TVN_DELETEITEM
:
2501 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2502 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2504 event
.m_item
= tv
->itemOld
.hItem
;
2508 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2509 if ( it
!= m_attrs
.end() )
2518 case TVN_ENDLABELEDIT
:
2520 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2521 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2523 event
.m_item
= info
->item
.hItem
;
2524 event
.m_label
= info
->item
.pszText
;
2525 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2530 // These *must* not be removed or TVN_GETINFOTIP will
2531 // not be processed each time the mouse is moved
2532 // and the tooltip will only ever update once.
2541 #ifdef TVN_GETINFOTIP
2542 case TVN_GETINFOTIP
:
2544 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2545 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2547 // Which item are we trying to get a tooltip for?
2548 event
.m_item
= info
->hItem
;
2555 case TVN_GETDISPINFO
:
2556 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2559 case TVN_SETDISPINFO
:
2561 if ( eventType
== wxEVT_NULL
)
2562 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2563 //else: get, already set above
2565 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2567 event
.m_item
= info
->item
.hItem
;
2571 case TVN_ITEMEXPANDING
:
2572 case TVN_ITEMEXPANDED
:
2574 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2577 switch ( tv
->action
)
2580 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2588 what
= IDX_COLLAPSE
;
2592 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2595 eventType
= gs_expandEvents
[what
][how
];
2597 event
.m_item
= tv
->itemNew
.hItem
;
2603 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2604 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2606 // fabricate the lParam and wParam parameters sufficiently
2607 // similar to the ones from a "real" WM_KEYDOWN so that
2608 // CreateKeyEvent() works correctly
2609 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2610 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2612 WXWPARAM wParam
= info
->wVKey
;
2614 int keyCode
= wxCharCodeMSWToWX(wParam
);
2617 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2622 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2627 // a separate event for Space/Return
2628 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2629 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2632 if ( !HasFlag(wxTR_MULTIPLE
) )
2633 item
= GetSelection();
2635 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2637 (void)GetEventHandler()->ProcessEvent(event2
);
2642 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2643 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2644 // we have to handle both messages:
2645 case TVN_SELCHANGEDA
:
2646 case TVN_SELCHANGEDW
:
2647 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2650 case TVN_SELCHANGINGA
:
2651 case TVN_SELCHANGINGW
:
2653 if ( eventType
== wxEVT_NULL
)
2654 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2655 //else: already set above
2657 if (hdr
->code
== TVN_SELCHANGINGW
||
2658 hdr
->code
== TVN_SELCHANGEDW
)
2660 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2661 event
.m_item
= tv
->itemNew
.hItem
;
2662 event
.m_itemOld
= tv
->itemOld
.hItem
;
2666 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2667 event
.m_item
= tv
->itemNew
.hItem
;
2668 event
.m_itemOld
= tv
->itemOld
.hItem
;
2673 // instead of explicitly checking for _WIN32_IE, check if the
2674 // required symbols are available in the headers
2675 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2678 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2679 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2680 switch ( nmcd
.dwDrawStage
)
2683 // if we've got any items with non standard attributes,
2684 // notify us before painting each item
2685 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2689 case CDDS_ITEMPREPAINT
:
2691 wxMapTreeAttr::iterator
2692 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2694 if ( it
== m_attrs
.end() )
2696 // nothing to do for this item
2697 *result
= CDRF_DODEFAULT
;
2701 wxTreeItemAttr
* const attr
= it
->second
;
2703 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2704 TVIF_STATE
, TVIS_DROPHILITED
);
2706 const UINT tvItemState
= tvItem
.state
;
2708 // selection colours should override ours,
2709 // otherwise it is too confusing to the user
2710 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2711 !(tvItemState
& TVIS_DROPHILITED
) )
2714 if ( attr
->HasBackgroundColour() )
2716 colBack
= attr
->GetBackgroundColour();
2717 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2721 // but we still want to keep the special foreground
2722 // colour when we don't have focus (we can't keep
2723 // it when we do, it would usually be unreadable on
2724 // the almost inverted bg colour...)
2725 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2726 FindFocus() != this ) &&
2727 !(tvItemState
& TVIS_DROPHILITED
) )
2730 if ( attr
->HasTextColour() )
2732 colText
= attr
->GetTextColour();
2733 lptvcd
->clrText
= wxColourToRGB(colText
);
2737 if ( attr
->HasFont() )
2739 HFONT hFont
= GetHfontOf(attr
->GetFont());
2741 ::SelectObject(nmcd
.hdc
, hFont
);
2743 *result
= CDRF_NEWFONT
;
2745 else // no specific font
2747 *result
= CDRF_DODEFAULT
;
2753 *result
= CDRF_DODEFAULT
;
2757 // we always process it
2759 #endif // have owner drawn support in headers
2763 DWORD pos
= GetMessagePos();
2765 point
.x
= LOWORD(pos
);
2766 point
.y
= HIWORD(pos
);
2767 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2769 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2770 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2772 event
.m_item
= item
;
2773 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2781 TV_HITTESTINFO tvhti
;
2782 ::GetCursorPos(&tvhti
.pt
);
2783 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2784 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2786 if ( tvhti
.flags
& TVHT_ONITEM
)
2788 event
.m_item
= tvhti
.hItem
;
2789 eventType
= (int)hdr
->code
== NM_DBLCLK
2790 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2791 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2793 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2794 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2803 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2806 event
.SetEventType(eventType
);
2808 if ( event
.m_item
.IsOk() )
2809 event
.SetClientObject(GetItemData(event
.m_item
));
2811 bool processed
= GetEventHandler()->ProcessEvent(event
);
2814 switch ( hdr
->code
)
2817 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2818 // the return code of this event handler as the return value for
2819 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2820 // expanded status would never work
2825 case TVN_BEGINRDRAG
:
2826 if ( event
.IsAllowed() )
2828 // normally this is impossible because the m_dragImage is
2829 // deleted once the drag operation is over
2830 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2832 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2833 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2834 m_dragImage
->Show();
2838 case TVN_DELETEITEM
:
2840 // NB: we might process this message using wxWidgets event
2841 // tables, but due to overhead of wxWin event system we
2842 // prefer to do it here ourself (otherwise deleting a tree
2843 // with many items is just too slow)
2844 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2846 wxTreeItemParam
*param
=
2847 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2850 processed
= true; // Make sure we don't get called twice
2854 case TVN_BEGINLABELEDIT
:
2855 // return true to cancel label editing
2856 *result
= !event
.IsAllowed();
2858 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2859 if ( event
.IsAllowed() )
2861 HWND hText
= TreeView_GetEditControl(GetHwnd());
2864 // MBN: if m_textCtrl already has an HWND, it is a stale
2865 // pointer from a previous edit (because the user
2866 // didn't modify the label before dismissing the control,
2867 // and TVN_ENDLABELEDIT was not sent), so delete it
2868 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2871 m_textCtrl
= new wxTextCtrl();
2872 m_textCtrl
->SetParent(this);
2873 m_textCtrl
->SetHWND((WXHWND
)hText
);
2874 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2876 // set wxTE_PROCESS_ENTER style for the text control to
2877 // force it to process the Enter presses itself, otherwise
2878 // they could be stolen from it by the dialog
2880 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2881 | wxTE_PROCESS_ENTER
);
2884 else // we had set m_idEdited before
2890 case TVN_ENDLABELEDIT
:
2891 // return true to set the label to the new string: note that we
2892 // also must pretend that we did process the message or it is going
2893 // to be passed to DefWindowProc() which will happily return false
2894 // cancelling the label change
2895 *result
= event
.IsAllowed();
2898 // ensure that we don't have the text ctrl which is going to be
2904 #ifdef TVN_GETINFOTIP
2905 case TVN_GETINFOTIP
:
2907 // If the user permitted a tooltip change, change it
2908 if (event
.IsAllowed())
2910 SetToolTip(event
.m_label
);
2917 case TVN_SELCHANGING
:
2918 case TVN_ITEMEXPANDING
:
2919 // return true to prevent the action from happening
2920 *result
= !event
.IsAllowed();
2923 case TVN_ITEMEXPANDED
:
2924 // the item is not refreshed properly after expansion when it has
2925 // an image depending on the expanded/collapsed state - bug in
2926 // comctl32.dll or our code?
2928 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2929 wxTreeItemId
id(tv
->itemNew
.hItem
);
2931 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2939 case TVN_GETDISPINFO
:
2940 // NB: so far the user can't set the image himself anyhow, so do it
2941 // anyway - but this may change later
2942 //if ( /* !processed && */ )
2944 wxTreeItemId item
= event
.m_item
;
2945 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2947 const wxTreeItemParam
* const param
= GetItemParam(item
);
2951 if ( info
->item
.mask
& TVIF_IMAGE
)
2956 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2957 : wxTreeItemIcon_Normal
2960 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2962 info
->item
.iSelectedImage
=
2965 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2966 : wxTreeItemIcon_Selected
2973 // for the other messages the return value is ignored and there is
2974 // nothing special to do
2979 // ----------------------------------------------------------------------------
2981 // ----------------------------------------------------------------------------
2983 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2984 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2986 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2989 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2990 tvi
.mask
= TVIF_STATE
;
2991 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2993 // Select the specified state, or -1 == cycle to the next one.
2996 TreeView_GetItem(GetHwnd(), &tvi
);
2998 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2999 if ( state
== m_imageListState
->GetImageCount() )
3003 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3004 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3006 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3008 TreeView_SetItem(GetHwnd(), &tvi
);
3011 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3014 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3015 tvi
.mask
= TVIF_STATE
;
3016 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3017 TreeView_GetItem(GetHwnd(), &tvi
);
3019 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3022 #endif // wxUSE_TREECTRL