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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/msw/private.h"
35 // include <commctrl.h> "properly"
36 #include "wx/msw/wrapcctl.h"
38 #include "wx/msw/missing.h"
40 // Set this to 1 to be _absolutely_ sure that repainting will work for all
41 // comctl32.dll versions
42 #define wxUSE_COMCTL32_SAFELY 0
46 #include "wx/dynarray.h"
47 #include "wx/imaglist.h"
48 #include "wx/settings.h"
49 #include "wx/msw/treectrl.h"
50 #include "wx/msw/dragimag.h"
52 // macros to hide the cast ugliness
53 // --------------------------------
55 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
56 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
58 // item here is a wxTreeItemId
59 #define HITEM(item) HITEM_PTR((item).m_pItem)
61 // the native control doesn't support multiple selections under MSW and we
62 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
63 // checkboxes be the selection status (checked == selected) or by really
64 // emulating everything, i.e. intercepting mouse and key events &c. The first
65 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
67 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // wrapper for TreeView_HitTest
74 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
80 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
83 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
85 // wrappers for TreeView_GetItem/TreeView_SetItem
86 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
90 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
91 tvi
.stateMask
= TVIS_SELECTED
;
94 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
96 wxLogLastError(wxT("TreeView_GetItem"));
99 return (tvi
.state
& TVIS_SELECTED
) != 0;
102 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
105 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
106 tvi
.stateMask
= TVIS_SELECTED
;
107 tvi
.state
= select
? TVIS_SELECTED
: 0;
110 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
112 wxLogLastError(wxT("TreeView_SetItem"));
119 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
121 SelectItem(hwndTV
, htItem
, false);
124 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
126 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
129 // helper function which selects all items in a range and, optionally,
130 // unselects all others
131 static void SelectRange(HWND hwndTV
,
134 bool unselectOthers
= true)
136 // find the first (or last) item and select it
138 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
139 while ( htItem
&& cont
)
141 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
143 if ( !IsItemSelected(hwndTV
, htItem
) )
145 SelectItem(hwndTV
, htItem
);
152 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
154 UnselectItem(hwndTV
, htItem
);
158 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
161 // select the items in range
162 cont
= htFirst
!= htLast
;
163 while ( htItem
&& cont
)
165 if ( !IsItemSelected(hwndTV
, htItem
) )
167 SelectItem(hwndTV
, htItem
);
170 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
172 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
176 if ( unselectOthers
)
180 if ( IsItemSelected(hwndTV
, htItem
) )
182 UnselectItem(hwndTV
, htItem
);
185 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
189 // seems to be necessary - otherwise the just selected items don't always
190 // appear as selected
191 UpdateWindow(hwndTV
);
194 // helper function which tricks the standard control into changing the focused
195 // item without changing anything else (if someone knows why Microsoft doesn't
196 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
197 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
200 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
205 if ( htItem
!= htFocus
)
207 // remember the selection state of the item
208 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
210 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
212 // prevent the tree from unselecting the old focus which it
213 // would do by default (TreeView_SelectItem unselects the
215 TreeView_SelectItem(hwndTV
, 0);
216 SelectItem(hwndTV
, htFocus
);
219 TreeView_SelectItem(hwndTV
, htItem
);
223 // need to clear the selection which TreeView_SelectItem() gave
225 UnselectItem(hwndTV
, htItem
);
227 //else: was selected, still selected - ok
229 //else: nothing to do, focus already there
235 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
237 // just clear the focus
238 TreeView_SelectItem(hwndTV
, 0);
240 if ( wasFocusSelected
)
242 // restore the selection state
243 SelectItem(hwndTV
, htFocus
);
246 //else: nothing to do, no focus already
250 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 // a convenient wrapper around TV_ITEM struct which adds a ctor
258 #pragma warning( disable : 4097 ) // inheriting from typedef
261 struct wxTreeViewItem
: public TV_ITEM
263 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
264 UINT mask_
, // fields which are valid
265 UINT stateMask_
= 0) // for TVIF_STATE only
269 // hItem member is always valid
270 mask
= mask_
| TVIF_HANDLE
;
271 stateMask
= stateMask_
;
276 // wxVirutalNode is used in place of a single root when 'hidden' root is
278 class wxVirtualNode
: public wxTreeViewItem
281 wxVirtualNode(wxTreeItemData
*data
)
282 : wxTreeViewItem(TVI_ROOT
, 0)
292 wxTreeItemData
*GetData() const { return m_data
; }
293 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
296 wxTreeItemData
*m_data
;
298 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
302 #pragma warning( default : 4097 )
305 // a macro to get the virtual root, returns NULL if none
306 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
308 // returns true if the item is the virtual root
309 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
311 // a class which encapsulates the tree traversal logic: it vists all (unless
312 // OnVisit() returns false) items under the given one
313 class wxTreeTraversal
316 wxTreeTraversal(const wxTreeCtrl
*tree
)
321 // do traverse the tree: visit all items (recursively by default) under the
322 // given one; return true if all items were traversed or false if the
323 // traversal was aborted because OnVisit returned false
324 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
326 // override this function to do whatever is needed for each item, return
327 // false to stop traversing
328 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
331 const wxTreeCtrl
*GetTree() const { return m_tree
; }
334 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
336 const wxTreeCtrl
*m_tree
;
338 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
341 // internal class for getting the selected items
342 class TraverseSelections
: public wxTreeTraversal
345 TraverseSelections(const wxTreeCtrl
*tree
,
346 wxArrayTreeItemIds
& selections
)
347 : wxTreeTraversal(tree
), m_selections(selections
)
349 m_selections
.Empty();
351 DoTraverse(tree
->GetRootItem());
354 virtual bool OnVisit(const wxTreeItemId
& item
)
356 // can't visit a virtual node.
357 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
362 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
363 if ( GetTree()->IsItemChecked(item
) )
365 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
368 m_selections
.Add(item
);
374 size_t GetCount() const { return m_selections
.GetCount(); }
377 wxArrayTreeItemIds
& m_selections
;
379 DECLARE_NO_COPY_CLASS(TraverseSelections
)
382 // internal class for counting tree items
383 class TraverseCounter
: public wxTreeTraversal
386 TraverseCounter(const wxTreeCtrl
*tree
,
387 const wxTreeItemId
& root
,
389 : wxTreeTraversal(tree
)
393 DoTraverse(root
, recursively
);
396 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
403 size_t GetCount() const { return m_count
; }
408 DECLARE_NO_COPY_CLASS(TraverseCounter
)
411 // ----------------------------------------------------------------------------
412 // This class is needed for support of different images: the Win32 common
413 // control natively supports only 2 images (the normal one and another for the
414 // selected state). We wish to provide support for 2 more of them for folder
415 // items (i.e. those which have children): for expanded state and for expanded
416 // selected state. For this we use this structure to store the additional items
419 // There is only one problem with this: when we retrieve the item's data, we
420 // don't know whether we get a pointer to wxTreeItemData or
421 // wxTreeItemIndirectData. So we always set the item id to an invalid value
422 // in this class and the code using the client data checks for it and retrieves
423 // the real client data in this case.
424 // ----------------------------------------------------------------------------
426 class wxTreeItemIndirectData
: public wxTreeItemData
429 // ctor associates this data with the item and the real item data becomes
430 // available through our GetData() method
431 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
433 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
439 m_data
= tree
->GetItemData(item
);
441 // and set ourselves as the new one
442 tree
->SetIndirectItemData(item
, this);
444 // we must have the invalid value for the item
448 // dtor deletes the associated data as well
449 virtual ~wxTreeItemIndirectData() { delete m_data
; }
452 // get the real data associated with the item
453 wxTreeItemData
*GetData() const { return m_data
; }
455 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
457 // do we have such image?
458 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
460 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
462 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
465 // all the images associated with the item
466 int m_images
[wxTreeItemIcon_Max
];
468 // the real client data
469 wxTreeItemData
*m_data
;
471 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
474 // ----------------------------------------------------------------------------
476 // ----------------------------------------------------------------------------
478 #if wxUSE_EXTENDED_RTTI
479 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
481 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
482 // new style border flags, we put them first to
483 // use them for streaming out
484 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
485 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
486 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
487 wxFLAGS_MEMBER(wxBORDER_RAISED
)
488 wxFLAGS_MEMBER(wxBORDER_STATIC
)
489 wxFLAGS_MEMBER(wxBORDER_NONE
)
491 // old style border flags
492 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
493 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
494 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
495 wxFLAGS_MEMBER(wxRAISED_BORDER
)
496 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
497 wxFLAGS_MEMBER(wxBORDER
)
499 // standard window styles
500 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
501 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
502 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
503 wxFLAGS_MEMBER(wxWANTS_CHARS
)
504 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
505 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
506 wxFLAGS_MEMBER(wxVSCROLL
)
507 wxFLAGS_MEMBER(wxHSCROLL
)
509 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
510 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
511 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
512 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
513 wxFLAGS_MEMBER(wxTR_NO_LINES
)
514 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
515 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
516 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
517 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
518 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
519 wxFLAGS_MEMBER(wxTR_SINGLE
)
520 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
521 wxFLAGS_MEMBER(wxTR_EXTENDED
)
522 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
524 wxEND_FLAGS( wxTreeCtrlStyle
)
526 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
528 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
529 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
530 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
531 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
532 wxEND_PROPERTIES_TABLE()
534 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
535 wxEND_HANDLERS_TABLE()
537 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
539 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
542 // ----------------------------------------------------------------------------
544 // ----------------------------------------------------------------------------
546 // indices in gs_expandEvents table below
561 // handy table for sending events - it has to be initialized during run-time
562 // now so can't be const any more
563 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
566 but logically it's a const table with the following entries:
569 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
570 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
574 // ============================================================================
576 // ============================================================================
578 // ----------------------------------------------------------------------------
580 // ----------------------------------------------------------------------------
582 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
584 if ( !OnVisit(root
) )
587 return Traverse(root
, recursively
);
590 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
592 wxTreeItemIdValue cookie
;
593 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
594 while ( child
.IsOk() )
596 // depth first traversal
597 if ( recursively
&& !Traverse(child
, true) )
600 if ( !OnVisit(child
) )
603 child
= m_tree
->GetNextChild(root
, cookie
);
609 // ----------------------------------------------------------------------------
610 // construction and destruction
611 // ----------------------------------------------------------------------------
613 void wxTreeCtrl::Init()
615 m_imageListNormal
= NULL
;
616 m_imageListState
= NULL
;
617 m_ownsImageListNormal
= m_ownsImageListState
= false;
619 m_hasAnyAttr
= false;
621 m_pVirtualRoot
= NULL
;
623 // initialize the global array of events now as it can't be done statically
624 // with the wxEVT_XXX values being allocated during run-time only
625 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
626 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
627 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
628 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
631 bool wxTreeCtrl::Create(wxWindow
*parent
,
636 const wxValidator
& validator
,
637 const wxString
& name
)
641 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
642 style
|= wxBORDER_SUNKEN
;
644 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
648 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
649 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
651 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
652 wstyle
|= TVS_HASLINES
;
653 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
654 wstyle
|= TVS_HASBUTTONS
;
656 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
657 wstyle
|= TVS_EDITLABELS
;
659 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
660 wstyle
|= TVS_LINESATROOT
;
662 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
664 if ( wxApp::GetComCtl32Version() >= 471 )
665 wstyle
|= TVS_FULLROWSELECT
;
668 // using TVS_CHECKBOXES for emulation of a multiselection tree control
669 // doesn't work without the new enough headers
670 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
671 !defined( __GNUWIN32_OLD__ ) && \
672 !defined( __BORLANDC__ ) && \
673 !defined( __WATCOMC__ ) && \
674 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
676 // we emulate the multiple selection tree controls by using checkboxes: set
677 // up the image list we need for this if we do have multiple selections
678 if ( m_windowStyle
& wxTR_MULTIPLE
)
679 wstyle
|= TVS_CHECKBOXES
;
680 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
682 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
683 // Need so that TVN_GETINFOTIP messages will be sent
684 wstyle
|= TVS_INFOTIP
;
687 // Create the tree control.
688 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
691 #if wxUSE_COMCTL32_SAFELY
692 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
693 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
695 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
696 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
698 // This works around a bug in the Windows tree control whereby for some versions
699 // of comctrl32, setting any colour actually draws the background in black.
700 // This will initialise the background to the system colour.
701 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
702 // Assume the user has an updated comctl32.dll.
703 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
704 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
705 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
709 // VZ: this is some experimental code which may be used to get the
710 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
711 // AFAIK, the standard DLL does about the same thing anyhow.
713 if ( m_windowStyle
& wxTR_MULTIPLE
)
717 // create the DC compatible with the current screen
718 HDC hdcMem
= CreateCompatibleDC(NULL
);
720 // create a mono bitmap of the standard size
721 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
722 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
723 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
724 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
725 1, // # of color planes
726 1, // # bits needed for one pixel
727 0); // array containing colour data
728 SelectObject(hdcMem
, hbmpCheck
);
730 // then draw a check mark into it
731 RECT rect
= { 0, 0, x
, y
};
732 if ( !::DrawFrameControl(hdcMem
, &rect
,
734 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
736 wxLogLastError(wxT("DrawFrameControl(check)"));
739 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
740 imagelistCheckboxes
.Add(bmp
);
742 if ( !::DrawFrameControl(hdcMem
, &rect
,
746 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
749 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
750 imagelistCheckboxes
.Add(bmp
);
756 SetStateImageList(&imagelistCheckboxes
);
760 wxSetCCUnicodeFormat(GetHwnd());
765 wxTreeCtrl::~wxTreeCtrl()
767 // delete any attributes
770 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
772 // prevent TVN_DELETEITEM handler from deleting the attributes again!
773 m_hasAnyAttr
= false;
778 // delete user data to prevent memory leaks
779 // also deletes hidden root node storage.
782 if (m_ownsImageListNormal
) delete m_imageListNormal
;
783 if (m_ownsImageListState
) delete m_imageListState
;
786 // ----------------------------------------------------------------------------
788 // ----------------------------------------------------------------------------
790 /* static */ wxVisualAttributes
791 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
793 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
795 // common controls have their own default font
796 attrs
.font
= wxGetCCDefaultFont();
802 // simple wrappers which add error checking in debug mode
804 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
806 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
807 _T("can't retrieve virtual root item") );
809 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
811 wxLogLastError(wxT("TreeView_GetItem"));
819 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
821 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
823 wxLogLastError(wxT("TreeView_SetItem"));
827 size_t wxTreeCtrl::GetCount() const
829 return (size_t)TreeView_GetCount(GetHwnd());
832 unsigned int wxTreeCtrl::GetIndent() const
834 return TreeView_GetIndent(GetHwnd());
837 void wxTreeCtrl::SetIndent(unsigned int indent
)
839 TreeView_SetIndent(GetHwnd(), indent
);
842 wxImageList
*wxTreeCtrl::GetImageList() const
844 return m_imageListNormal
;
847 wxImageList
*wxTreeCtrl::GetStateImageList() const
849 return m_imageListState
;
852 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
855 TreeView_SetImageList(GetHwnd(),
856 imageList
? imageList
->GetHIMAGELIST() : 0,
860 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
862 if (m_ownsImageListNormal
)
863 delete m_imageListNormal
;
865 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
866 m_ownsImageListNormal
= false;
869 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
871 if (m_ownsImageListState
) delete m_imageListState
;
872 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
873 m_ownsImageListState
= false;
876 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
878 SetImageList(imageList
);
879 m_ownsImageListNormal
= true;
882 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
884 SetStateImageList(imageList
);
885 m_ownsImageListState
= true;
888 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
889 bool recursively
) const
891 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
893 TraverseCounter
counter(this, item
, recursively
);
894 return counter
.GetCount() - 1;
897 // ----------------------------------------------------------------------------
899 // ----------------------------------------------------------------------------
901 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
903 #if !wxUSE_COMCTL32_SAFELY
904 if ( !wxWindowBase::SetBackgroundColour(colour
) )
907 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
913 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
915 #if !wxUSE_COMCTL32_SAFELY
916 if ( !wxWindowBase::SetForegroundColour(colour
) )
919 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
925 // ----------------------------------------------------------------------------
927 // ----------------------------------------------------------------------------
929 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
931 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
933 wxChar buf
[512]; // the size is arbitrary...
935 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
936 tvItem
.pszText
= buf
;
937 tvItem
.cchTextMax
= WXSIZEOF(buf
);
938 if ( !DoGetItem(&tvItem
) )
940 // don't return some garbage which was on stack, but an empty string
944 return wxString(buf
);
947 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
949 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
951 if ( IS_VIRTUAL_ROOT(item
) )
954 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
955 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
958 // when setting the text of the item being edited, the text control should
959 // be updated to reflect the new text as well, otherwise calling
960 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
962 // don't use GetEditControl() here because m_textCtrl is not set yet
963 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
966 if ( item
== m_idEdited
)
968 ::SetWindowText(hwndEdit
, text
);
973 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
974 wxTreeItemIcon which
) const
976 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
977 if ( !DoGetItem(&tvItem
) )
982 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
985 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
987 wxTreeItemIcon which
) const
989 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
990 if ( !DoGetItem(&tvItem
) )
995 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
997 data
->SetImage(image
, which
);
999 // make sure that we have selected images as well
1000 if ( which
== wxTreeItemIcon_Normal
&&
1001 !data
->HasImage(wxTreeItemIcon_Selected
) )
1003 data
->SetImage(image
, wxTreeItemIcon_Selected
);
1006 if ( which
== wxTreeItemIcon_Expanded
&&
1007 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
1009 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
1013 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
1017 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
1018 tvItem
.iSelectedImage
= imageSel
;
1019 tvItem
.iImage
= image
;
1023 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1024 wxTreeItemIcon which
) const
1026 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1028 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1030 // TODO: Maybe a hidden root can still provide images?
1034 if ( HasIndirectData(item
) )
1036 return DoGetItemImageFromData(item
, which
);
1043 wxFAIL_MSG( wxT("unknown tree item image type") );
1045 case wxTreeItemIcon_Normal
:
1049 case wxTreeItemIcon_Selected
:
1050 mask
= TVIF_SELECTEDIMAGE
;
1053 case wxTreeItemIcon_Expanded
:
1054 case wxTreeItemIcon_SelectedExpanded
:
1058 wxTreeViewItem
tvItem(item
, mask
);
1061 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1064 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1065 wxTreeItemIcon which
)
1067 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1069 if ( IS_VIRTUAL_ROOT(item
) )
1071 // TODO: Maybe a hidden root can still store images?
1081 wxFAIL_MSG( wxT("unknown tree item image type") );
1084 case wxTreeItemIcon_Normal
:
1086 const int imageNormalOld
= GetItemImage(item
);
1087 const int imageSelOld
=
1088 GetItemImage(item
, wxTreeItemIcon_Selected
);
1090 // always set the normal image
1091 imageNormal
= image
;
1093 // if the selected and normal images were the same, they should
1094 // be the same after the update, otherwise leave the selected
1096 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1100 case wxTreeItemIcon_Selected
:
1101 imageNormal
= GetItemImage(item
);
1105 case wxTreeItemIcon_Expanded
:
1106 case wxTreeItemIcon_SelectedExpanded
:
1107 if ( !HasIndirectData(item
) )
1109 // we need to get the old images first, because after we create
1110 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1112 imageNormal
= GetItemImage(item
);
1113 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1115 // if it doesn't have it yet, add it
1116 wxTreeItemIndirectData
*data
= new
1117 wxTreeItemIndirectData(this, item
);
1119 // copy the data to the new location
1120 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1121 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1124 DoSetItemImageFromData(item
, image
, which
);
1126 // reset the normal/selected images because we won't use them any
1127 // more - now they're stored inside the indirect data
1129 imageSel
= I_IMAGECALLBACK
;
1133 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1134 // change both normal and selected image - otherwise the change simply
1135 // doesn't take place!
1136 DoSetItemImages(item
, imageNormal
, imageSel
);
1139 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1141 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1143 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1145 // Hidden root may have data.
1146 if ( IS_VIRTUAL_ROOT(item
) )
1148 return GET_VIRTUAL_ROOT()->GetData();
1152 if ( !DoGetItem(&tvItem
) )
1157 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1158 if ( IsDataIndirect(data
) )
1160 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1166 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1168 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1170 if ( IS_VIRTUAL_ROOT(item
) )
1172 GET_VIRTUAL_ROOT()->SetData(data
);
1175 // first, associate this piece of data with this item
1181 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1183 if ( HasIndirectData(item
) )
1185 if ( DoGetItem(&tvItem
) )
1187 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1191 wxFAIL_MSG( wxT("failed to change tree items data") );
1196 tvItem
.lParam
= (LPARAM
)data
;
1201 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1202 wxTreeItemIndirectData
*data
)
1204 // this should never happen because it's unnecessary and will probably lead
1205 // to crash too because the code elsewhere supposes that the pointer the
1206 // wxTreeItemIndirectData has is a real wxItemData and not
1207 // wxTreeItemIndirectData as well
1208 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1210 SetItemData(item
, data
);
1213 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1215 // query the item itself
1216 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1217 if ( !DoGetItem(&tvItem
) )
1222 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1224 return data
&& IsDataIndirect(data
);
1227 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1229 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1231 if ( IS_VIRTUAL_ROOT(item
) )
1234 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1235 tvItem
.cChildren
= (int)has
;
1239 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1241 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1243 if ( IS_VIRTUAL_ROOT(item
) )
1246 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1247 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1251 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1253 if ( IS_VIRTUAL_ROOT(item
) )
1256 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1257 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1261 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1263 if ( IS_VIRTUAL_ROOT(item
) )
1267 if ( GetBoundingRect(item
, rect
) )
1273 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1275 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1277 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1278 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1281 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1283 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1285 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1286 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1289 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1291 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1293 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1294 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1297 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1298 const wxColour
& col
)
1300 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1302 wxTreeItemAttr
*attr
;
1303 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1304 if ( it
== m_attrs
.end() )
1306 m_hasAnyAttr
= true;
1308 m_attrs
[item
.m_pItem
] =
1309 attr
= new wxTreeItemAttr
;
1316 attr
->SetTextColour(col
);
1321 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1322 const wxColour
& col
)
1324 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1326 wxTreeItemAttr
*attr
;
1327 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1328 if ( it
== m_attrs
.end() )
1330 m_hasAnyAttr
= true;
1332 m_attrs
[item
.m_pItem
] =
1333 attr
= new wxTreeItemAttr
;
1335 else // already in the hash
1340 attr
->SetBackgroundColour(col
);
1345 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1347 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1349 wxTreeItemAttr
*attr
;
1350 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1351 if ( it
== m_attrs
.end() )
1353 m_hasAnyAttr
= true;
1355 m_attrs
[item
.m_pItem
] =
1356 attr
= new wxTreeItemAttr
;
1358 else // already in the hash
1363 attr
->SetFont(font
);
1368 // ----------------------------------------------------------------------------
1370 // ----------------------------------------------------------------------------
1372 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1374 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1376 if ( item
== wxTreeItemId(TVI_ROOT
) )
1378 // virtual (hidden) root is never visible
1382 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1385 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1386 // the HTREEITEM with TVM_GETITEMRECT
1387 *(HTREEITEM
*)&rect
= HITEM(item
);
1389 // true means to get rect for just the text, not the whole line
1390 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1392 // if TVM_GETITEMRECT returned false, then the item is definitely not
1393 // visible (because its parent is not expanded)
1397 // however if it returned true, the item might still be outside the
1398 // currently visible part of the tree, test for it (notice that partly
1399 // visible means visible here)
1400 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1403 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1405 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1407 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1410 return tvItem
.cChildren
!= 0;
1413 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1415 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1417 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1420 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1423 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1425 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1427 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1430 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1433 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1435 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1437 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1440 return (tvItem
.state
& TVIS_BOLD
) != 0;
1443 // ----------------------------------------------------------------------------
1445 // ----------------------------------------------------------------------------
1447 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1449 // Root may be real (visible) or virtual (hidden).
1450 if ( GET_VIRTUAL_ROOT() )
1453 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1456 wxTreeItemId
wxTreeCtrl::GetSelection() const
1458 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1459 wxT("this only works with single selection controls") );
1461 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1464 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1466 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1470 if ( IS_VIRTUAL_ROOT(item
) )
1472 // no parent for the virtual root
1477 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1478 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1480 // the top level items should have the virtual root as their parent
1485 return wxTreeItemId(hItem
);
1488 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1489 wxTreeItemIdValue
& cookie
) const
1491 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1493 // remember the last child returned in 'cookie'
1494 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1496 return wxTreeItemId(cookie
);
1499 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1500 wxTreeItemIdValue
& cookie
) const
1502 wxTreeItemId
fromCookie(cookie
);
1504 HTREEITEM hitem
= HITEM(fromCookie
);
1506 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1508 wxTreeItemId
item(hitem
);
1510 cookie
= item
.m_pItem
;
1515 #if WXWIN_COMPATIBILITY_2_4
1517 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1520 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1522 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1524 return wxTreeItemId((void *)cookie
);
1527 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1530 wxTreeItemId
fromCookie((void *)cookie
);
1532 HTREEITEM hitem
= HITEM(fromCookie
);
1534 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1536 wxTreeItemId
item(hitem
);
1538 cookie
= (long)item
.m_pItem
;
1543 #endif // WXWIN_COMPATIBILITY_2_4
1545 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1547 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1549 // can this be done more efficiently?
1550 wxTreeItemIdValue cookie
;
1552 wxTreeItemId childLast
,
1553 child
= GetFirstChild(item
, cookie
);
1554 while ( child
.IsOk() )
1557 child
= GetNextChild(item
, cookie
);
1563 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1565 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1566 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1569 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1571 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1572 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1575 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1577 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1580 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1582 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1583 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1585 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1588 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1590 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1591 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1593 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1596 // ----------------------------------------------------------------------------
1597 // multiple selections emulation
1598 // ----------------------------------------------------------------------------
1600 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1602 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1604 // receive the desired information.
1605 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1608 // state image indices are 1 based
1609 return ((tvItem
.state
>> 12) - 1) == 1;
1612 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1614 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1616 // receive the desired information.
1617 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1621 // state images are one-based
1622 tvItem
.state
= (check
? 2 : 1) << 12;
1627 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1629 TraverseSelections
selector(this, selections
);
1631 return selector
.GetCount();
1634 // ----------------------------------------------------------------------------
1636 // ----------------------------------------------------------------------------
1638 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1639 wxTreeItemId hInsertAfter
,
1640 const wxString
& text
,
1641 int image
, int selectedImage
,
1642 wxTreeItemData
*data
)
1644 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1646 _T("can't have more than one root in the tree") );
1648 TV_INSERTSTRUCT tvIns
;
1649 tvIns
.hParent
= HITEM(parent
);
1650 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1652 // this is how we insert the item as the first child: supply a NULL
1654 if ( !tvIns
.hInsertAfter
)
1656 tvIns
.hInsertAfter
= TVI_FIRST
;
1660 if ( !text
.empty() )
1663 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1667 tvIns
.item
.pszText
= NULL
;
1668 tvIns
.item
.cchTextMax
= 0;
1674 tvIns
.item
.iImage
= image
;
1676 if ( selectedImage
== -1 )
1678 // take the same image for selected icon if not specified
1679 selectedImage
= image
;
1683 if ( selectedImage
!= -1 )
1685 mask
|= TVIF_SELECTEDIMAGE
;
1686 tvIns
.item
.iSelectedImage
= selectedImage
;
1692 tvIns
.item
.lParam
= (LPARAM
)data
;
1695 tvIns
.item
.mask
= mask
;
1697 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1700 wxLogLastError(wxT("TreeView_InsertItem"));
1705 // associate the application tree item with Win32 tree item handle
1709 return wxTreeItemId(id
);
1712 // for compatibility only
1713 #if WXWIN_COMPATIBILITY_2_4
1715 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1716 const wxString
& text
,
1717 int image
, int selImage
,
1720 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1721 image
, selImage
, NULL
);
1724 wxImageList
*wxTreeCtrl::GetImageList(int) const
1726 return GetImageList();
1729 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1731 SetImageList(imageList
);
1734 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1736 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1739 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1741 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1744 #endif // WXWIN_COMPATIBILITY_2_4
1746 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1747 int image
, int selectedImage
,
1748 wxTreeItemData
*data
)
1751 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1753 // create a virtual root item, the parent for all the others
1754 m_pVirtualRoot
= new wxVirtualNode(data
);
1759 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1760 text
, image
, selectedImage
, data
);
1763 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1764 const wxString
& text
,
1765 int image
, int selectedImage
,
1766 wxTreeItemData
*data
)
1768 return DoInsertItem(parent
, TVI_FIRST
,
1769 text
, image
, selectedImage
, data
);
1772 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1773 const wxTreeItemId
& idPrevious
,
1774 const wxString
& text
,
1775 int image
, int selectedImage
,
1776 wxTreeItemData
*data
)
1778 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1781 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1783 const wxString
& text
,
1784 int image
, int selectedImage
,
1785 wxTreeItemData
*data
)
1787 // find the item from index
1788 wxTreeItemIdValue cookie
;
1789 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1790 while ( index
!= 0 && idCur
.IsOk() )
1795 idCur
= GetNextChild(parent
, cookie
);
1798 // assert, not check: if the index is invalid, we will append the item
1800 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1802 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1805 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1806 const wxString
& text
,
1807 int image
, int selectedImage
,
1808 wxTreeItemData
*data
)
1810 return DoInsertItem(parent
, TVI_LAST
,
1811 text
, image
, selectedImage
, data
);
1814 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1816 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1818 wxLogLastError(wxT("TreeView_DeleteItem"));
1822 // delete all children (but don't delete the item itself)
1823 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1825 wxTreeItemIdValue cookie
;
1827 wxArrayTreeItemIds children
;
1828 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1829 while ( child
.IsOk() )
1831 children
.Add(child
);
1833 child
= GetNextChild(item
, cookie
);
1836 size_t nCount
= children
.Count();
1837 for ( size_t n
= 0; n
< nCount
; n
++ )
1839 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1841 wxLogLastError(wxT("TreeView_DeleteItem"));
1846 void wxTreeCtrl::DeleteAllItems()
1848 // delete the "virtual" root item.
1849 if ( GET_VIRTUAL_ROOT() )
1851 delete GET_VIRTUAL_ROOT();
1852 m_pVirtualRoot
= NULL
;
1855 // and all the real items
1857 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1859 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1863 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1865 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1866 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1867 flag
== TVE_EXPAND
||
1869 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1871 // A hidden root can be neither expanded nor collapsed.
1872 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1873 wxT("Can't expand/collapse hidden root node!") )
1875 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1876 // emulate them. This behaviour has changed slightly with comctl32.dll
1877 // v 4.70 - now it does send them but only the first time. To maintain
1878 // compatible behaviour and also in order to not have surprises with the
1879 // future versions, don't rely on this and still do everything ourselves.
1880 // To avoid that the messages be sent twice when the item is expanded for
1881 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1883 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1887 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1889 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1890 event
.m_item
= item
;
1891 event
.SetEventObject(this);
1893 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1895 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1899 (void)GetEventHandler()->ProcessEvent(event
);
1901 //else: change didn't took place, so do nothing at all
1904 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1906 DoExpand(item
, TVE_EXPAND
);
1909 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1911 DoExpand(item
, TVE_COLLAPSE
);
1914 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1916 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1919 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1921 DoExpand(item
, TVE_TOGGLE
);
1924 #if WXWIN_COMPATIBILITY_2_4
1926 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1928 DoExpand(item
, action
);
1933 void wxTreeCtrl::Unselect()
1935 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1936 wxT("doesn't make sense, may be you want UnselectAll()?") );
1938 // just remove the selection
1939 SelectItem(wxTreeItemId());
1942 void wxTreeCtrl::UnselectAll()
1944 if ( m_windowStyle
& wxTR_MULTIPLE
)
1946 wxArrayTreeItemIds selections
;
1947 size_t count
= GetSelections(selections
);
1948 for ( size_t n
= 0; n
< count
; n
++ )
1950 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1951 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1952 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1953 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1954 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1957 m_htSelStart
.Unset();
1961 // just remove the selection
1966 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1968 if ( m_windowStyle
& wxTR_MULTIPLE
)
1970 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1971 // selecting the item means checking it
1972 SetItemCheck(item
, select
);
1973 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1974 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1975 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1979 wxASSERT_MSG( select
,
1980 _T("SelectItem(false) works only for multiselect") );
1982 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1983 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1984 // send them ourselves
1986 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1987 event
.m_item
= item
;
1988 event
.SetEventObject(this);
1990 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1991 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1993 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1995 wxLogLastError(wxT("TreeView_SelectItem"));
1999 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
2000 (void)GetEventHandler()->ProcessEvent(event
);
2003 //else: program vetoed the change
2007 void wxTreeCtrl::UnselectItem(const wxTreeItemId
& item
)
2009 SelectItem(item
, false);
2012 void wxTreeCtrl::ToggleItemSelection(const wxTreeItemId
& item
)
2014 SelectItem(item
, !IsSelected(item
));
2017 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2020 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
2023 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
2025 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
2027 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2031 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
2036 void wxTreeCtrl::DeleteTextCtrl()
2040 // the HWND corresponding to this control is deleted by the tree
2041 // control itself and we don't know when exactly this happens, so check
2042 // if the window still exists before calling UnsubclassWin()
2043 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
2045 m_textCtrl
->SetHWND(0);
2048 m_textCtrl
->UnsubclassWin();
2049 m_textCtrl
->SetHWND(0);
2057 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2058 wxClassInfo
* textControlClass
)
2060 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
2065 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
2066 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2068 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2077 // textctrl is subclassed in MSWOnNotify
2081 // End label editing, optionally cancelling the edit
2082 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2084 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2089 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2091 TV_HITTESTINFO hitTestInfo
;
2092 hitTestInfo
.pt
.x
= (int)point
.x
;
2093 hitTestInfo
.pt
.y
= (int)point
.y
;
2095 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2100 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2101 flags |= wxTREE_HITTEST_##flag
2103 TRANSLATE_FLAG(ABOVE
);
2104 TRANSLATE_FLAG(BELOW
);
2105 TRANSLATE_FLAG(NOWHERE
);
2106 TRANSLATE_FLAG(ONITEMBUTTON
);
2107 TRANSLATE_FLAG(ONITEMICON
);
2108 TRANSLATE_FLAG(ONITEMINDENT
);
2109 TRANSLATE_FLAG(ONITEMLABEL
);
2110 TRANSLATE_FLAG(ONITEMRIGHT
);
2111 TRANSLATE_FLAG(ONITEMSTATEICON
);
2112 TRANSLATE_FLAG(TOLEFT
);
2113 TRANSLATE_FLAG(TORIGHT
);
2115 #undef TRANSLATE_FLAG
2117 return wxTreeItemId(hitTestInfo
.hItem
);
2120 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2122 bool textOnly
) const
2126 // Virtual root items have no bounding rectangle
2127 if ( IS_VIRTUAL_ROOT(item
) )
2132 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2135 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2141 // couldn't retrieve rect: for example, item isn't visible
2146 // ----------------------------------------------------------------------------
2148 // ----------------------------------------------------------------------------
2150 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2151 // functions such as IsDataIndirect()
2152 class wxTreeSortHelper
2155 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2158 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
2160 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
2161 if ( tree
->IsDataIndirect(data
) )
2163 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2166 return data
->GetId();
2170 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2174 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2175 wxT("sorting tree without data doesn't make sense") );
2177 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2179 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2180 GetIdFromData(tree
, pItem2
));
2183 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2184 const wxTreeItemId
& item2
)
2186 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2189 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2191 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2193 // rely on the fact that TreeView_SortChildren does the same thing as our
2194 // default behaviour, i.e. sorts items alphabetically and so call it
2195 // directly if we're not in derived class (much more efficient!)
2196 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2198 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2203 tvSort
.hParent
= HITEM(item
);
2204 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2205 tvSort
.lParam
= (LPARAM
)this;
2206 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2210 // ----------------------------------------------------------------------------
2212 // ----------------------------------------------------------------------------
2214 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2216 if ( cmd
== EN_UPDATE
)
2218 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2219 event
.SetEventObject( this );
2220 ProcessCommand(event
);
2222 else if ( cmd
== EN_KILLFOCUS
)
2224 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2225 event
.SetEventObject( this );
2226 ProcessCommand(event
);
2234 // command processed
2238 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2239 // only do it during dragging, minimize wxWin overhead (this is important for
2240 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2241 // instead of passing by wxWin events
2242 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2244 bool processed
= false;
2246 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2248 // This message is sent after a right-click, or when the "menu" key is pressed
2249 if ( nMsg
== WM_CONTEXTMENU
)
2251 int x
= GET_X_LPARAM(lParam
),
2252 y
= GET_Y_LPARAM(lParam
);
2253 // Convert the screen point to a client point
2254 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2256 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2258 // can't use GetSelection() here as it would assert in multiselect mode
2259 event
.m_item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2260 event
.SetEventObject( this );
2262 // Get the bounding rectangle for the item, including the non-text areas
2264 GetBoundingRect(event
.m_item
, ItemRect
, false);
2265 // If the point is inside the bounding rectangle, use it as the click position.
2266 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2267 if (ItemRect
.Inside(MenuPoint
))
2269 event
.m_pointDrag
= MenuPoint
;
2271 // Use the Explorer standard of putting the menu at the left edge of the text,
2272 // in the vertical middle of the text. Should be the case for the "menu" key
2275 // Use the bounding rectangle of only the text part
2276 GetBoundingRect(event
.m_item
, ItemRect
, true);
2277 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2280 if ( GetEventHandler()->ProcessEvent(event
) )
2282 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2284 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2286 // we only process mouse messages here and these parameters have the
2287 // same meaning for all of them
2288 int x
= GET_X_LPARAM(lParam
),
2289 y
= GET_Y_LPARAM(lParam
);
2290 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2294 case WM_RBUTTONDOWN
:
2295 // if the item we are about to right click on is not already
2296 // selected or if we click outside of any item, remove the
2297 // entire previous selection
2298 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2303 // select item and set the focus to the
2304 // newly selected item
2305 ::SelectItem(GetHwnd(), htItem
);
2306 ::SetFocus(GetHwnd(), htItem
);
2309 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2310 case WM_LBUTTONDOWN
:
2311 if ( htItem
&& isMultiple
)
2313 if ( wParam
& MK_CONTROL
)
2317 // toggle selected state
2318 ::ToggleItemSelection(GetHwnd(), htItem
);
2320 ::SetFocus(GetHwnd(), htItem
);
2322 // reset on any click without Shift
2323 m_htSelStart
.Unset();
2327 else if ( wParam
& MK_SHIFT
)
2329 // this selects all items between the starting one and
2332 if ( !m_htSelStart
)
2334 // take the focused item
2335 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2338 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2339 !(wParam
& MK_CONTROL
));
2341 ::SetFocus(GetHwnd(), htItem
);
2345 else // normal click
2347 // avoid doing anything if we click on the only
2348 // currently selected item
2350 wxArrayTreeItemIds selections
;
2351 size_t count
= GetSelections(selections
);
2354 HITEM_PTR(selections
[0]) != htItem
)
2356 // clear the previously selected items, if the
2357 // user clicked outside of the present selection.
2358 // otherwise, perform the deselection on mouse-up.
2359 // this allows multiple drag and drop to work.
2361 if (IsItemSelected(GetHwnd(), htItem
))
2363 ::SetFocus(GetHwnd(), htItem
);
2369 // prevent the click from starting in-place editing
2370 // which should only happen if we click on the
2371 // already selected item (and nothing else is
2374 TreeView_SelectItem(GetHwnd(), 0);
2375 ::SelectItem(GetHwnd(), htItem
);
2379 // reset on any click without Shift
2380 m_htSelStart
.Unset();
2384 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2389 m_dragImage
->Move(wxPoint(x
, y
));
2392 // highlight the item as target (hiding drag image is
2393 // necessary - otherwise the display will be corrupted)
2394 m_dragImage
->Hide();
2395 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2396 m_dragImage
->Show();
2403 // facilitates multiple drag-and-drop
2404 if (htItem
&& isMultiple
)
2406 wxArrayTreeItemIds selections
;
2407 size_t count
= GetSelections(selections
);
2410 !(wParam
& MK_CONTROL
) &&
2411 !(wParam
& MK_SHIFT
))
2414 TreeView_SelectItem(GetHwnd(), htItem
);
2423 m_dragImage
->EndDrag();
2427 // generate the drag end event
2428 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2430 event
.m_item
= htItem
;
2431 event
.m_pointDrag
= wxPoint(x
, y
);
2432 event
.SetEventObject(this);
2434 (void)GetEventHandler()->ProcessEvent(event
);
2436 // if we don't do it, the tree seems to think that 2 items
2437 // are selected simultaneously which is quite weird
2438 TreeView_SelectDropTarget(GetHwnd(), 0);
2443 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2444 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2446 // the tree control greys out the selected item when it loses focus and
2447 // paints it as selected again when it regains it, but it won't do it
2448 // for the other items itself - help it
2449 wxArrayTreeItemIds selections
;
2450 size_t count
= GetSelections(selections
);
2452 for ( size_t n
= 0; n
< count
; n
++ )
2454 // TreeView_GetItemRect() will return false if item is not visible,
2455 // which may happen perfectly well
2456 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2459 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2463 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2465 bool bCtrl
= wxIsCtrlDown(),
2466 bShift
= wxIsShiftDown();
2468 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2474 ::ToggleItemSelection(GetHwnd(), htSel
);
2480 ::SelectItem(GetHwnd(), htSel
);
2488 if ( !bCtrl
&& !bShift
)
2490 // no modifiers, just clear selection and then let the default
2491 // processing to take place
2496 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2498 HTREEITEM htNext
= (HTREEITEM
)
2499 TreeView_GetNextItem
2503 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2509 // at the top/bottom
2515 if ( !m_htSelStart
)
2516 m_htSelStart
= htSel
;
2518 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2522 // without changing selection
2523 ::SetFocus(GetHwnd(), htNext
);
2534 // TODO: handle Shift/Ctrl with these keys
2535 if ( !bCtrl
&& !bShift
)
2539 m_htSelStart
.Unset();
2543 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2544 else if ( nMsg
== WM_COMMAND
)
2546 // if we receive a EN_KILLFOCUS command from the in-place edit control
2547 // used for label editing, make sure to end editing
2550 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2552 if ( cmd
== EN_KILLFOCUS
)
2554 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2564 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2570 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2572 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2573 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2574 // the parent window, not us, which completely breaks everything so simply
2575 // don't let it see this message at all
2576 if ( nMsg
== WM_RBUTTONDOWN
)
2579 // but because of the above we don't get NM_RCLICK which is normally
2580 // generated by tree window proc when the modal loop mentioned above ends
2581 // because the mouse is released -- synthesize it ourselves instead
2582 if ( nMsg
== WM_RBUTTONUP
)
2585 hdr
.hwndFrom
= GetHwnd();
2586 hdr
.idFrom
= GetId();
2587 hdr
.code
= NM_RCLICK
;
2590 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2592 // continue as usual
2595 if ( nMsg
== WM_CHAR
)
2597 // also don't let the control process Space and Return keys because it
2598 // doesn't do anything useful with them anyhow but always beeps
2599 // annoyingly when it receives them and there is no way to turn it off
2600 // simply if you just process TREEITEM_ACTIVATED event to which Space
2601 // and Enter presses are mapped in your code
2602 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2606 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2609 // process WM_NOTIFY Windows message
2610 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2612 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2613 wxEventType eventType
= wxEVT_NULL
;
2614 NMHDR
*hdr
= (NMHDR
*)lParam
;
2616 switch ( hdr
->code
)
2619 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2622 case TVN_BEGINRDRAG
:
2624 if ( eventType
== wxEVT_NULL
)
2625 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2626 //else: left drag, already set above
2628 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2630 event
.m_item
= tv
->itemNew
.hItem
;
2631 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2633 // don't allow dragging by default: the user code must
2634 // explicitly say that it wants to allow it to avoid breaking
2640 case TVN_BEGINLABELEDIT
:
2642 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2643 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2645 // although the user event handler may still veto it, it is
2646 // important to set it now so that calls to SetItemText() from
2647 // the event handler would change the text controls contents
2649 event
.m_item
= info
->item
.hItem
;
2650 event
.m_label
= info
->item
.pszText
;
2651 event
.m_editCancelled
= false;
2655 case TVN_DELETEITEM
:
2657 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2658 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2660 event
.m_item
= tv
->itemOld
.hItem
;
2664 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2665 if ( it
!= m_attrs
.end() )
2674 case TVN_ENDLABELEDIT
:
2676 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2677 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2679 event
.m_item
= info
->item
.hItem
;
2680 event
.m_label
= info
->item
.pszText
;
2681 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2686 // These *must* not be removed or TVN_GETINFOTIP will
2687 // not be processed each time the mouse is moved
2688 // and the tooltip will only ever update once.
2697 #ifdef TVN_GETINFOTIP
2698 case TVN_GETINFOTIP
:
2700 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2701 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2703 // Which item are we trying to get a tooltip for?
2704 event
.m_item
= info
->hItem
;
2711 case TVN_GETDISPINFO
:
2712 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2715 case TVN_SETDISPINFO
:
2717 if ( eventType
== wxEVT_NULL
)
2718 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2719 //else: get, already set above
2721 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2723 event
.m_item
= info
->item
.hItem
;
2727 case TVN_ITEMEXPANDING
:
2728 case TVN_ITEMEXPANDED
:
2730 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2733 switch ( tv
->action
)
2736 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2744 what
= IDX_COLLAPSE
;
2748 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2751 eventType
= gs_expandEvents
[what
][how
];
2753 event
.m_item
= tv
->itemNew
.hItem
;
2759 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2760 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2762 // fabricate the lParam and wParam parameters sufficiently
2763 // similar to the ones from a "real" WM_KEYDOWN so that
2764 // CreateKeyEvent() works correctly
2766 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2768 WXWPARAM wParam
= info
->wVKey
;
2770 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2773 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2778 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2783 // a separate event for Space/Return
2784 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2785 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2787 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2789 event2
.SetEventObject(this);
2790 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2792 event2
.m_item
= GetSelection();
2794 //else: don't know how to get it
2796 (void)GetEventHandler()->ProcessEvent(event2
);
2801 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2802 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2803 // we have to handle both messages:
2804 case TVN_SELCHANGEDA
:
2805 case TVN_SELCHANGEDW
:
2806 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2809 case TVN_SELCHANGINGA
:
2810 case TVN_SELCHANGINGW
:
2812 if ( eventType
== wxEVT_NULL
)
2813 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2814 //else: already set above
2816 if (hdr
->code
== TVN_SELCHANGINGW
||
2817 hdr
->code
== TVN_SELCHANGEDW
)
2819 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2820 event
.m_item
= tv
->itemNew
.hItem
;
2821 event
.m_itemOld
= tv
->itemOld
.hItem
;
2825 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2826 event
.m_item
= tv
->itemNew
.hItem
;
2827 event
.m_itemOld
= tv
->itemOld
.hItem
;
2832 // instead of explicitly checking for _WIN32_IE, check if the
2833 // required symbols are available in the headers
2834 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2837 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2838 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2839 switch ( nmcd
.dwDrawStage
)
2842 // if we've got any items with non standard attributes,
2843 // notify us before painting each item
2844 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2848 case CDDS_ITEMPREPAINT
:
2850 wxMapTreeAttr::iterator
2851 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2853 if ( it
== m_attrs
.end() )
2855 // nothing to do for this item
2856 *result
= CDRF_DODEFAULT
;
2860 wxTreeItemAttr
* const attr
= it
->second
;
2862 // selection colours should override ours,
2863 // otherwise it is too confusing ot the user
2864 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) )
2867 if ( attr
->HasBackgroundColour() )
2869 colBack
= attr
->GetBackgroundColour();
2870 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2874 // but we still want to keep the special foreground
2875 // colour when we don't have focus (we can't keep
2876 // it when we do, it would usually be unreadable on
2877 // the almost inverted bg colour...)
2878 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2879 FindFocus() != this )
2882 if ( attr
->HasTextColour() )
2884 colText
= attr
->GetTextColour();
2885 lptvcd
->clrText
= wxColourToRGB(colText
);
2889 if ( attr
->HasFont() )
2891 HFONT hFont
= GetHfontOf(attr
->GetFont());
2893 ::SelectObject(nmcd
.hdc
, hFont
);
2895 *result
= CDRF_NEWFONT
;
2897 else // no specific font
2899 *result
= CDRF_DODEFAULT
;
2905 *result
= CDRF_DODEFAULT
;
2909 // we always process it
2911 #endif // have owner drawn support in headers
2915 DWORD pos
= GetMessagePos();
2917 point
.x
= LOWORD(pos
);
2918 point
.y
= HIWORD(pos
);
2919 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2921 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2922 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2924 event
.m_item
= item
;
2925 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2933 TV_HITTESTINFO tvhti
;
2934 ::GetCursorPos(&tvhti
.pt
);
2935 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2936 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2938 if ( tvhti
.flags
& TVHT_ONITEM
)
2940 event
.m_item
= tvhti
.hItem
;
2941 eventType
= (int)hdr
->code
== NM_DBLCLK
2942 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2943 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2945 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2946 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2955 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2958 event
.SetEventObject(this);
2959 event
.SetEventType(eventType
);
2961 bool processed
= GetEventHandler()->ProcessEvent(event
);
2964 switch ( hdr
->code
)
2967 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2968 // the return code of this event handler as the return value for
2969 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2970 // expanded status would never work
2975 case TVN_BEGINRDRAG
:
2976 if ( event
.IsAllowed() )
2978 // normally this is impossible because the m_dragImage is
2979 // deleted once the drag operation is over
2980 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2982 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2983 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2984 m_dragImage
->Show();
2988 case TVN_DELETEITEM
:
2990 // NB: we might process this message using wxWidgets event
2991 // tables, but due to overhead of wxWin event system we
2992 // prefer to do it here ourself (otherwise deleting a tree
2993 // with many items is just too slow)
2994 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2996 wxTreeItemId item
= event
.m_item
;
2997 if ( HasIndirectData(item
) )
2999 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
3001 delete data
; // can't be NULL here
3005 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
3006 delete data
; // may be NULL, ok
3009 processed
= true; // Make sure we don't get called twice
3013 case TVN_BEGINLABELEDIT
:
3014 // return true to cancel label editing
3015 *result
= !event
.IsAllowed();
3017 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3018 if ( event
.IsAllowed() )
3020 HWND hText
= TreeView_GetEditControl(GetHwnd());
3023 // MBN: if m_textCtrl already has an HWND, it is a stale
3024 // pointer from a previous edit (because the user
3025 // didn't modify the label before dismissing the control,
3026 // and TVN_ENDLABELEDIT was not sent), so delete it
3027 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
3030 m_textCtrl
= new wxTextCtrl();
3031 m_textCtrl
->SetParent(this);
3032 m_textCtrl
->SetHWND((WXHWND
)hText
);
3033 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3035 // set wxTE_PROCESS_ENTER style for the text control to
3036 // force it to process the Enter presses itself, otherwise
3037 // they could be stolen from it by the dialog
3039 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3040 | wxTE_PROCESS_ENTER
);
3043 else // we had set m_idEdited before
3049 case TVN_ENDLABELEDIT
:
3050 // return true to set the label to the new string: note that we
3051 // also must pretend that we did process the message or it is going
3052 // to be passed to DefWindowProc() which will happily return false
3053 // cancelling the label change
3054 *result
= event
.IsAllowed();
3057 // ensure that we don't have the text ctrl which is going to be
3063 #ifdef TVN_GETINFOTIP
3064 case TVN_GETINFOTIP
:
3066 // If the user permitted a tooltip change, change it
3067 if (event
.IsAllowed())
3069 SetToolTip(event
.m_label
);
3076 case TVN_SELCHANGING
:
3077 case TVN_ITEMEXPANDING
:
3078 // return true to prevent the action from happening
3079 *result
= !event
.IsAllowed();
3082 case TVN_ITEMEXPANDED
:
3083 // the item is not refreshed properly after expansion when it has
3084 // an image depending on the expanded/collapsed state - bug in
3085 // comctl32.dll or our code?
3087 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
3088 wxTreeItemId
id(tv
->itemNew
.hItem
);
3090 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3098 case TVN_GETDISPINFO
:
3099 // NB: so far the user can't set the image himself anyhow, so do it
3100 // anyway - but this may change later
3101 //if ( /* !processed && */ 1 )
3103 wxTreeItemId item
= event
.m_item
;
3104 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3105 if ( info
->item
.mask
& TVIF_IMAGE
)
3108 DoGetItemImageFromData
3111 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3112 : wxTreeItemIcon_Normal
3115 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3117 info
->item
.iSelectedImage
=
3118 DoGetItemImageFromData
3121 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3122 : wxTreeItemIcon_Selected
3129 // for the other messages the return value is ignored and there is
3130 // nothing special to do
3135 // ----------------------------------------------------------------------------
3137 // ----------------------------------------------------------------------------
3139 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3140 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3142 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
3145 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3146 tvi
.mask
= TVIF_STATE
;
3147 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3149 // Select the specified state, or -1 == cycle to the next one.
3152 TreeView_GetItem(GetHwnd(), &tvi
);
3154 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
3155 if ( state
== m_imageListState
->GetImageCount() )
3159 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3160 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3162 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3164 TreeView_SetItem(GetHwnd(), &tvi
);
3167 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3170 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3171 tvi
.mask
= TVIF_STATE
;
3172 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3173 TreeView_GetItem(GetHwnd(), &tvi
);
3175 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3178 #if WXWIN_COMPATIBILITY_2_2
3180 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
3182 return GetItemParent( item
);
3185 #endif // WXWIN_COMPATIBILITY_2_2
3187 #endif // wxUSE_TREECTRL