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"
30 #include "wx/dynarray.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/imaglist.h"
47 #include "wx/settings.h"
48 #include "wx/treectrl.h"
49 #include "wx/msw/dragimag.h"
51 // macros to hide the cast ugliness
52 // --------------------------------
54 // get HTREEITEM from wxTreeItemId
55 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
57 // the native control doesn't support multiple selections under MSW and we
58 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
59 // checkboxes be the selection status (checked == selected) or by really
60 // emulating everything, i.e. intercepting mouse and key events &c. The first
61 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
63 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // wrapper for TreeView_HitTest
70 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
76 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
79 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
81 // wrappers for TreeView_GetItem/TreeView_SetItem
82 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
86 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
87 tvi
.stateMask
= TVIS_SELECTED
;
90 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
92 wxLogLastError(wxT("TreeView_GetItem"));
95 return (tvi
.state
& TVIS_SELECTED
) != 0;
98 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
101 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
102 tvi
.stateMask
= TVIS_SELECTED
;
103 tvi
.state
= select
? TVIS_SELECTED
: 0;
106 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
108 wxLogLastError(wxT("TreeView_SetItem"));
115 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
117 SelectItem(hwndTV
, htItem
, false);
120 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
122 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
125 // helper function which selects all items in a range and, optionally,
126 // unselects all others
127 static void SelectRange(HWND hwndTV
,
130 bool unselectOthers
= true)
132 // find the first (or last) item and select it
134 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
135 while ( htItem
&& cont
)
137 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
139 if ( !IsItemSelected(hwndTV
, htItem
) )
141 SelectItem(hwndTV
, htItem
);
148 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
150 UnselectItem(hwndTV
, htItem
);
154 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
157 // select the items in range
158 cont
= htFirst
!= htLast
;
159 while ( htItem
&& cont
)
161 if ( !IsItemSelected(hwndTV
, htItem
) )
163 SelectItem(hwndTV
, htItem
);
166 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
168 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
172 if ( unselectOthers
)
176 if ( IsItemSelected(hwndTV
, htItem
) )
178 UnselectItem(hwndTV
, htItem
);
181 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
185 // seems to be necessary - otherwise the just selected items don't always
186 // appear as selected
187 UpdateWindow(hwndTV
);
190 // helper function which tricks the standard control into changing the focused
191 // item without changing anything else (if someone knows why Microsoft doesn't
192 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
193 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
196 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
201 if ( htItem
!= htFocus
)
203 // remember the selection state of the item
204 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
206 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
208 // prevent the tree from unselecting the old focus which it
209 // would do by default (TreeView_SelectItem unselects the
211 TreeView_SelectItem(hwndTV
, 0);
212 SelectItem(hwndTV
, htFocus
);
215 TreeView_SelectItem(hwndTV
, htItem
);
219 // need to clear the selection which TreeView_SelectItem() gave
221 UnselectItem(hwndTV
, htItem
);
223 //else: was selected, still selected - ok
225 //else: nothing to do, focus already there
231 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
233 // just clear the focus
234 TreeView_SelectItem(hwndTV
, 0);
236 if ( wasFocusSelected
)
238 // restore the selection state
239 SelectItem(hwndTV
, htFocus
);
242 //else: nothing to do, no focus already
246 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
252 // a convenient wrapper around TV_ITEM struct which adds a ctor
254 #pragma warning( disable : 4097 ) // inheriting from typedef
257 struct wxTreeViewItem
: public TV_ITEM
259 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
260 UINT mask_
, // fields which are valid
261 UINT stateMask_
= 0) // for TVIF_STATE only
265 // hItem member is always valid
266 mask
= mask_
| TVIF_HANDLE
;
267 stateMask
= stateMask_
;
272 // wxVirutalNode is used in place of a single root when 'hidden' root is
274 class wxVirtualNode
: public wxTreeViewItem
277 wxVirtualNode(wxTreeItemData
*data
)
278 : wxTreeViewItem(TVI_ROOT
, 0)
288 wxTreeItemData
*GetData() const { return m_data
; }
289 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
292 wxTreeItemData
*m_data
;
294 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
298 #pragma warning( default : 4097 )
301 // a macro to get the virtual root, returns NULL if none
302 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
304 // returns true if the item is the virtual root
305 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
307 // a class which encapsulates the tree traversal logic: it vists all (unless
308 // OnVisit() returns false) items under the given one
309 class wxTreeTraversal
312 wxTreeTraversal(const wxTreeCtrl
*tree
)
317 // give it a virtual dtor: not really needed as the class is never used
318 // polymorphically and not even allocated on heap at all, but this is safer
319 // (in case it ever is) and silences the compiler warnings for now
320 virtual ~wxTreeTraversal() { }
322 // do traverse the tree: visit all items (recursively by default) under the
323 // given one; return true if all items were traversed or false if the
324 // traversal was aborted because OnVisit returned false
325 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
327 // override this function to do whatever is needed for each item, return
328 // false to stop traversing
329 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
332 const wxTreeCtrl
*GetTree() const { return m_tree
; }
335 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
337 const wxTreeCtrl
*m_tree
;
339 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
342 // internal class for getting the selected items
343 class TraverseSelections
: public wxTreeTraversal
346 TraverseSelections(const wxTreeCtrl
*tree
,
347 wxArrayTreeItemIds
& selections
)
348 : wxTreeTraversal(tree
), m_selections(selections
)
350 m_selections
.Empty();
352 if (tree
->GetCount() > 0)
353 DoTraverse(tree
->GetRootItem());
356 virtual bool OnVisit(const wxTreeItemId
& item
)
358 // can't visit a virtual node.
359 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
364 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
365 if ( GetTree()->IsItemChecked(item
) )
367 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
370 m_selections
.Add(item
);
376 size_t GetCount() const { return m_selections
.GetCount(); }
379 wxArrayTreeItemIds
& m_selections
;
381 DECLARE_NO_COPY_CLASS(TraverseSelections
)
384 // internal class for counting tree items
385 class TraverseCounter
: public wxTreeTraversal
388 TraverseCounter(const wxTreeCtrl
*tree
,
389 const wxTreeItemId
& root
,
391 : wxTreeTraversal(tree
)
395 DoTraverse(root
, recursively
);
398 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
405 size_t GetCount() const { return m_count
; }
410 DECLARE_NO_COPY_CLASS(TraverseCounter
)
413 // ----------------------------------------------------------------------------
414 // This class is needed for support of different images: the Win32 common
415 // control natively supports only 2 images (the normal one and another for the
416 // selected state). We wish to provide support for 2 more of them for folder
417 // items (i.e. those which have children): for expanded state and for expanded
418 // selected state. For this we use this structure to store the additional items
421 // There is only one problem with this: when we retrieve the item's data, we
422 // don't know whether we get a pointer to wxTreeItemData or
423 // wxTreeItemIndirectData. So we always set the item id to an invalid value
424 // in this class and the code using the client data checks for it and retrieves
425 // the real client data in this case.
426 // ----------------------------------------------------------------------------
428 class wxTreeItemIndirectData
: public wxTreeItemData
431 // ctor associates this data with the item and the real item data becomes
432 // available through our GetData() method
433 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
435 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
441 m_data
= tree
->GetItemData(item
);
443 // and set ourselves as the new one
444 tree
->SetIndirectItemData(item
, this);
446 // we must have the invalid value for the item
450 // dtor deletes the associated data as well
451 virtual ~wxTreeItemIndirectData() { delete m_data
; }
454 // get the real data associated with the item
455 wxTreeItemData
*GetData() const { return m_data
; }
457 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
459 // do we have such image?
460 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
462 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
464 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
467 // all the images associated with the item
468 int m_images
[wxTreeItemIcon_Max
];
470 // the real client data
471 wxTreeItemData
*m_data
;
473 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
476 // ----------------------------------------------------------------------------
478 // ----------------------------------------------------------------------------
480 #if wxUSE_EXTENDED_RTTI
481 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
483 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
484 // new style border flags, we put them first to
485 // use them for streaming out
486 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
487 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
488 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
489 wxFLAGS_MEMBER(wxBORDER_RAISED
)
490 wxFLAGS_MEMBER(wxBORDER_STATIC
)
491 wxFLAGS_MEMBER(wxBORDER_NONE
)
493 // old style border flags
494 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
495 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
496 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
497 wxFLAGS_MEMBER(wxRAISED_BORDER
)
498 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
499 wxFLAGS_MEMBER(wxBORDER
)
501 // standard window styles
502 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
503 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
504 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
505 wxFLAGS_MEMBER(wxWANTS_CHARS
)
506 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
507 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
508 wxFLAGS_MEMBER(wxVSCROLL
)
509 wxFLAGS_MEMBER(wxHSCROLL
)
511 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
512 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
513 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
514 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
515 wxFLAGS_MEMBER(wxTR_NO_LINES
)
516 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
517 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
518 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
519 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
520 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
521 wxFLAGS_MEMBER(wxTR_SINGLE
)
522 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
523 wxFLAGS_MEMBER(wxTR_EXTENDED
)
524 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
526 wxEND_FLAGS( wxTreeCtrlStyle
)
528 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
530 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
531 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
532 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
533 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
534 wxEND_PROPERTIES_TABLE()
536 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
537 wxEND_HANDLERS_TABLE()
539 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
541 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 // indices in gs_expandEvents table below
563 // handy table for sending events - it has to be initialized during run-time
564 // now so can't be const any more
565 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
568 but logically it's a const table with the following entries:
571 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
572 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
576 // ============================================================================
578 // ============================================================================
580 // ----------------------------------------------------------------------------
582 // ----------------------------------------------------------------------------
584 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
586 if ( !OnVisit(root
) )
589 return Traverse(root
, recursively
);
592 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
594 wxTreeItemIdValue cookie
;
595 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
596 while ( child
.IsOk() )
598 // depth first traversal
599 if ( recursively
&& !Traverse(child
, true) )
602 if ( !OnVisit(child
) )
605 child
= m_tree
->GetNextChild(root
, cookie
);
611 // ----------------------------------------------------------------------------
612 // construction and destruction
613 // ----------------------------------------------------------------------------
615 void wxTreeCtrl::Init()
618 m_hasAnyAttr
= false;
620 m_pVirtualRoot
= NULL
;
622 // initialize the global array of events now as it can't be done statically
623 // with the wxEVT_XXX values being allocated during run-time only
624 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
625 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
626 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
627 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
630 bool wxTreeCtrl::Create(wxWindow
*parent
,
635 const wxValidator
& validator
,
636 const wxString
& name
)
640 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
641 style
|= wxBORDER_SUNKEN
;
643 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
647 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
648 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
650 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
651 wstyle
|= TVS_HASLINES
;
652 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
653 wstyle
|= TVS_HASBUTTONS
;
655 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
656 wstyle
|= TVS_EDITLABELS
;
658 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
659 wstyle
|= TVS_LINESATROOT
;
661 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
663 if ( wxApp::GetComCtl32Version() >= 471 )
664 wstyle
|= TVS_FULLROWSELECT
;
667 // using TVS_CHECKBOXES for emulation of a multiselection tree control
668 // doesn't work without the new enough headers
669 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
670 !defined( __GNUWIN32_OLD__ ) && \
671 !defined( __BORLANDC__ ) && \
672 !defined( __WATCOMC__ ) && \
673 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
675 // we emulate the multiple selection tree controls by using checkboxes: set
676 // up the image list we need for this if we do have multiple selections
677 if ( m_windowStyle
& wxTR_MULTIPLE
)
678 wstyle
|= TVS_CHECKBOXES
;
679 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
681 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
682 // Need so that TVN_GETINFOTIP messages will be sent
683 wstyle
|= TVS_INFOTIP
;
686 // Create the tree control.
687 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
690 #if wxUSE_COMCTL32_SAFELY
691 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
692 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
694 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
695 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
697 // This works around a bug in the Windows tree control whereby for some versions
698 // of comctrl32, setting any colour actually draws the background in black.
699 // This will initialise the background to the system colour.
700 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
701 // Assume the user has an updated comctl32.dll.
702 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
703 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
704 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
708 // VZ: this is some experimental code which may be used to get the
709 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
710 // AFAIK, the standard DLL does about the same thing anyhow.
712 if ( m_windowStyle
& wxTR_MULTIPLE
)
716 // create the DC compatible with the current screen
717 HDC hdcMem
= CreateCompatibleDC(NULL
);
719 // create a mono bitmap of the standard size
720 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
721 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
722 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
723 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
724 1, // # of color planes
725 1, // # bits needed for one pixel
726 0); // array containing colour data
727 SelectObject(hdcMem
, hbmpCheck
);
729 // then draw a check mark into it
730 RECT rect
= { 0, 0, x
, y
};
731 if ( !::DrawFrameControl(hdcMem
, &rect
,
733 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
735 wxLogLastError(wxT("DrawFrameControl(check)"));
738 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
739 imagelistCheckboxes
.Add(bmp
);
741 if ( !::DrawFrameControl(hdcMem
, &rect
,
745 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
748 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
749 imagelistCheckboxes
.Add(bmp
);
755 SetStateImageList(&imagelistCheckboxes
);
759 wxSetCCUnicodeFormat(GetHwnd());
764 wxTreeCtrl::~wxTreeCtrl()
766 // delete any attributes
769 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
771 // prevent TVN_DELETEITEM handler from deleting the attributes again!
772 m_hasAnyAttr
= false;
777 // delete user data to prevent memory leaks
778 // also deletes hidden root node storage.
782 // ----------------------------------------------------------------------------
784 // ----------------------------------------------------------------------------
786 /* static */ wxVisualAttributes
787 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
789 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
791 // common controls have their own default font
792 attrs
.font
= wxGetCCDefaultFont();
798 // simple wrappers which add error checking in debug mode
800 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
802 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
803 _T("can't retrieve virtual root item") );
805 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
807 wxLogLastError(wxT("TreeView_GetItem"));
815 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
817 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
819 wxLogLastError(wxT("TreeView_SetItem"));
823 unsigned int wxTreeCtrl::GetCount() const
825 return (unsigned int)TreeView_GetCount(GetHwnd());
828 unsigned int wxTreeCtrl::GetIndent() const
830 return TreeView_GetIndent(GetHwnd());
833 void wxTreeCtrl::SetIndent(unsigned int indent
)
835 TreeView_SetIndent(GetHwnd(), indent
);
838 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
841 (void) TreeView_SetImageList(GetHwnd(),
842 imageList
? imageList
->GetHIMAGELIST() : 0,
846 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
848 if (m_ownsImageListNormal
)
849 delete m_imageListNormal
;
851 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
852 m_ownsImageListNormal
= false;
855 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
857 if (m_ownsImageListState
) delete m_imageListState
;
858 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
859 m_ownsImageListState
= false;
862 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
863 bool recursively
) const
865 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
867 TraverseCounter
counter(this, item
, recursively
);
868 return counter
.GetCount() - 1;
871 // ----------------------------------------------------------------------------
873 // ----------------------------------------------------------------------------
875 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
877 #if !wxUSE_COMCTL32_SAFELY
878 if ( !wxWindowBase::SetBackgroundColour(colour
) )
881 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
887 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
889 #if !wxUSE_COMCTL32_SAFELY
890 if ( !wxWindowBase::SetForegroundColour(colour
) )
893 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
899 // ----------------------------------------------------------------------------
901 // ----------------------------------------------------------------------------
903 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
905 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
907 wxChar buf
[512]; // the size is arbitrary...
909 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
910 tvItem
.pszText
= buf
;
911 tvItem
.cchTextMax
= WXSIZEOF(buf
);
912 if ( !DoGetItem(&tvItem
) )
914 // don't return some garbage which was on stack, but an empty string
918 return wxString(buf
);
921 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
923 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
925 if ( IS_VIRTUAL_ROOT(item
) )
928 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
929 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
932 // when setting the text of the item being edited, the text control should
933 // be updated to reflect the new text as well, otherwise calling
934 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
936 // don't use GetEditControl() here because m_textCtrl is not set yet
937 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
940 if ( item
== m_idEdited
)
942 ::SetWindowText(hwndEdit
, text
);
947 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
948 wxTreeItemIcon which
) const
950 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
951 if ( !DoGetItem(&tvItem
) )
956 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
959 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
961 wxTreeItemIcon which
) const
963 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
964 if ( !DoGetItem(&tvItem
) )
969 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
971 data
->SetImage(image
, which
);
973 // make sure that we have selected images as well
974 if ( which
== wxTreeItemIcon_Normal
&&
975 !data
->HasImage(wxTreeItemIcon_Selected
) )
977 data
->SetImage(image
, wxTreeItemIcon_Selected
);
980 if ( which
== wxTreeItemIcon_Expanded
&&
981 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
983 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
987 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
991 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
992 tvItem
.iSelectedImage
= imageSel
;
993 tvItem
.iImage
= image
;
997 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
998 wxTreeItemIcon which
) const
1000 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1002 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1004 // TODO: Maybe a hidden root can still provide images?
1008 if ( HasIndirectData(item
) )
1010 return DoGetItemImageFromData(item
, which
);
1017 wxFAIL_MSG( wxT("unknown tree item image type") );
1019 case wxTreeItemIcon_Normal
:
1023 case wxTreeItemIcon_Selected
:
1024 mask
= TVIF_SELECTEDIMAGE
;
1027 case wxTreeItemIcon_Expanded
:
1028 case wxTreeItemIcon_SelectedExpanded
:
1032 wxTreeViewItem
tvItem(item
, mask
);
1035 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1038 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1039 wxTreeItemIcon which
)
1041 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1043 if ( IS_VIRTUAL_ROOT(item
) )
1045 // TODO: Maybe a hidden root can still store images?
1055 wxFAIL_MSG( wxT("unknown tree item image type") );
1058 case wxTreeItemIcon_Normal
:
1060 const int imageNormalOld
= GetItemImage(item
);
1061 const int imageSelOld
=
1062 GetItemImage(item
, wxTreeItemIcon_Selected
);
1064 // always set the normal image
1065 imageNormal
= image
;
1067 // if the selected and normal images were the same, they should
1068 // be the same after the update, otherwise leave the selected
1070 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1074 case wxTreeItemIcon_Selected
:
1075 imageNormal
= GetItemImage(item
);
1079 case wxTreeItemIcon_Expanded
:
1080 case wxTreeItemIcon_SelectedExpanded
:
1081 if ( !HasIndirectData(item
) )
1083 // we need to get the old images first, because after we create
1084 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1086 imageNormal
= GetItemImage(item
);
1087 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1089 // if it doesn't have it yet, add it
1090 wxTreeItemIndirectData
*data
= new
1091 wxTreeItemIndirectData(this, item
);
1093 // copy the data to the new location
1094 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1095 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1098 DoSetItemImageFromData(item
, image
, which
);
1100 // reset the normal/selected images because we won't use them any
1101 // more - now they're stored inside the indirect data
1103 imageSel
= I_IMAGECALLBACK
;
1107 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1108 // change both normal and selected image - otherwise the change simply
1109 // doesn't take place!
1110 DoSetItemImages(item
, imageNormal
, imageSel
);
1113 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1115 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1117 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1119 // Hidden root may have data.
1120 if ( IS_VIRTUAL_ROOT(item
) )
1122 return GET_VIRTUAL_ROOT()->GetData();
1126 if ( !DoGetItem(&tvItem
) )
1131 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1132 if ( IsDataIndirect(data
) )
1134 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1140 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1142 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1144 if ( IS_VIRTUAL_ROOT(item
) )
1146 GET_VIRTUAL_ROOT()->SetData(data
);
1149 // first, associate this piece of data with this item
1155 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1157 if ( HasIndirectData(item
) )
1159 if ( DoGetItem(&tvItem
) )
1161 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1165 wxFAIL_MSG( wxT("failed to change tree items data") );
1170 tvItem
.lParam
= (LPARAM
)data
;
1175 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1176 wxTreeItemIndirectData
*data
)
1178 // this should never happen because it's unnecessary and will probably lead
1179 // to crash too because the code elsewhere supposes that the pointer the
1180 // wxTreeItemIndirectData has is a real wxItemData and not
1181 // wxTreeItemIndirectData as well
1182 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1184 SetItemData(item
, data
);
1187 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1189 // query the item itself
1190 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1191 if ( !DoGetItem(&tvItem
) )
1196 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1198 return data
&& IsDataIndirect(data
);
1201 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1203 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1205 if ( IS_VIRTUAL_ROOT(item
) )
1208 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1209 tvItem
.cChildren
= (int)has
;
1213 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1215 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1217 if ( IS_VIRTUAL_ROOT(item
) )
1220 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1221 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1225 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1227 if ( IS_VIRTUAL_ROOT(item
) )
1230 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1231 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1235 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1237 if ( IS_VIRTUAL_ROOT(item
) )
1241 if ( GetBoundingRect(item
, rect
) )
1247 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1249 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1251 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1252 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1255 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1257 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1259 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1260 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1263 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1265 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1267 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1268 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1271 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1272 const wxColour
& col
)
1274 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1276 wxTreeItemAttr
*attr
;
1277 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1278 if ( it
== m_attrs
.end() )
1280 m_hasAnyAttr
= true;
1282 m_attrs
[item
.m_pItem
] =
1283 attr
= new wxTreeItemAttr
;
1290 attr
->SetTextColour(col
);
1295 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1296 const wxColour
& col
)
1298 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1300 wxTreeItemAttr
*attr
;
1301 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1302 if ( it
== m_attrs
.end() )
1304 m_hasAnyAttr
= true;
1306 m_attrs
[item
.m_pItem
] =
1307 attr
= new wxTreeItemAttr
;
1309 else // already in the hash
1314 attr
->SetBackgroundColour(col
);
1319 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1321 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1323 wxTreeItemAttr
*attr
;
1324 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1325 if ( it
== m_attrs
.end() )
1327 m_hasAnyAttr
= true;
1329 m_attrs
[item
.m_pItem
] =
1330 attr
= new wxTreeItemAttr
;
1332 else // already in the hash
1337 attr
->SetFont(font
);
1342 // ----------------------------------------------------------------------------
1344 // ----------------------------------------------------------------------------
1346 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1348 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1350 if ( item
== wxTreeItemId(TVI_ROOT
) )
1352 // virtual (hidden) root is never visible
1356 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1359 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1360 // the HTREEITEM with TVM_GETITEMRECT
1361 *(HTREEITEM
*)&rect
= HITEM(item
);
1363 // true means to get rect for just the text, not the whole line
1364 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1366 // if TVM_GETITEMRECT returned false, then the item is definitely not
1367 // visible (because its parent is not expanded)
1371 // however if it returned true, the item might still be outside the
1372 // currently visible part of the tree, test for it (notice that partly
1373 // visible means visible here)
1374 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1377 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1379 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1381 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1384 return tvItem
.cChildren
!= 0;
1387 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1389 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1391 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1394 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1397 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1399 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1401 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1404 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1407 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1409 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1411 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1414 return (tvItem
.state
& TVIS_BOLD
) != 0;
1417 // ----------------------------------------------------------------------------
1419 // ----------------------------------------------------------------------------
1421 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1423 // Root may be real (visible) or virtual (hidden).
1424 if ( GET_VIRTUAL_ROOT() )
1427 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1430 wxTreeItemId
wxTreeCtrl::GetSelection() const
1432 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1433 wxT("this only works with single selection controls") );
1435 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1438 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1440 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1444 if ( IS_VIRTUAL_ROOT(item
) )
1446 // no parent for the virtual root
1451 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1452 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1454 // the top level items should have the virtual root as their parent
1459 return wxTreeItemId(hItem
);
1462 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1463 wxTreeItemIdValue
& cookie
) const
1465 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1467 // remember the last child returned in 'cookie'
1468 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1470 return wxTreeItemId(cookie
);
1473 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1474 wxTreeItemIdValue
& cookie
) const
1476 wxTreeItemId
fromCookie(cookie
);
1478 HTREEITEM hitem
= HITEM(fromCookie
);
1480 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1482 wxTreeItemId
item(hitem
);
1484 cookie
= item
.m_pItem
;
1489 #if WXWIN_COMPATIBILITY_2_4
1491 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1494 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1496 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1498 return wxTreeItemId((void *)cookie
);
1501 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1504 wxTreeItemId
fromCookie((void *)cookie
);
1506 HTREEITEM hitem
= HITEM(fromCookie
);
1508 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1510 wxTreeItemId
item(hitem
);
1512 cookie
= (long)item
.m_pItem
;
1517 #endif // WXWIN_COMPATIBILITY_2_4
1519 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1521 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1523 // can this be done more efficiently?
1524 wxTreeItemIdValue cookie
;
1526 wxTreeItemId childLast
,
1527 child
= GetFirstChild(item
, cookie
);
1528 while ( child
.IsOk() )
1531 child
= GetNextChild(item
, cookie
);
1537 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1539 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1540 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1543 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1545 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1546 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1549 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1551 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1554 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1556 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1557 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1559 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1562 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1564 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1565 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1567 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1570 // ----------------------------------------------------------------------------
1571 // multiple selections emulation
1572 // ----------------------------------------------------------------------------
1574 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1576 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1578 // receive the desired information.
1579 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1582 // state image indices are 1 based
1583 return ((tvItem
.state
>> 12) - 1) == 1;
1586 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1588 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1590 // receive the desired information.
1591 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1595 // state images are one-based
1596 tvItem
.state
= (check
? 2 : 1) << 12;
1601 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1603 TraverseSelections
selector(this, selections
);
1605 return selector
.GetCount();
1608 // ----------------------------------------------------------------------------
1610 // ----------------------------------------------------------------------------
1612 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1613 const wxTreeItemId
& hInsertAfter
,
1614 const wxString
& text
,
1615 int image
, int selectedImage
,
1616 wxTreeItemData
*data
)
1618 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1620 _T("can't have more than one root in the tree") );
1622 TV_INSERTSTRUCT tvIns
;
1623 tvIns
.hParent
= HITEM(parent
);
1624 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1626 // this is how we insert the item as the first child: supply a NULL
1628 if ( !tvIns
.hInsertAfter
)
1630 tvIns
.hInsertAfter
= TVI_FIRST
;
1634 if ( !text
.empty() )
1637 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1641 tvIns
.item
.pszText
= NULL
;
1642 tvIns
.item
.cchTextMax
= 0;
1648 tvIns
.item
.iImage
= image
;
1650 if ( selectedImage
== -1 )
1652 // take the same image for selected icon if not specified
1653 selectedImage
= image
;
1657 if ( selectedImage
!= -1 )
1659 mask
|= TVIF_SELECTEDIMAGE
;
1660 tvIns
.item
.iSelectedImage
= selectedImage
;
1666 tvIns
.item
.lParam
= (LPARAM
)data
;
1669 tvIns
.item
.mask
= mask
;
1671 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1674 wxLogLastError(wxT("TreeView_InsertItem"));
1679 // associate the application tree item with Win32 tree item handle
1683 return wxTreeItemId(id
);
1686 // for compatibility only
1687 #if WXWIN_COMPATIBILITY_2_4
1689 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1691 SetImageList(imageList
);
1694 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1696 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1699 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1701 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1704 #endif // WXWIN_COMPATIBILITY_2_4
1706 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1707 int image
, int selectedImage
,
1708 wxTreeItemData
*data
)
1711 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1713 // create a virtual root item, the parent for all the others
1714 m_pVirtualRoot
= new wxVirtualNode(data
);
1719 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1720 text
, image
, selectedImage
, data
);
1723 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1725 const wxString
& text
,
1726 int image
, int selectedImage
,
1727 wxTreeItemData
*data
)
1729 wxTreeItemId idPrev
;
1730 if ( index
== (size_t)-1 )
1732 // special value: append to the end
1735 else // find the item from index
1737 wxTreeItemIdValue cookie
;
1738 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1739 while ( index
!= 0 && idCur
.IsOk() )
1744 idCur
= GetNextChild(parent
, cookie
);
1747 // assert, not check: if the index is invalid, we will append the item
1749 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1752 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1755 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1757 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1759 wxLogLastError(wxT("TreeView_DeleteItem"));
1763 // delete all children (but don't delete the item itself)
1764 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1766 wxTreeItemIdValue cookie
;
1768 wxArrayTreeItemIds children
;
1769 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1770 while ( child
.IsOk() )
1772 children
.Add(child
);
1774 child
= GetNextChild(item
, cookie
);
1777 size_t nCount
= children
.Count();
1778 for ( size_t n
= 0; n
< nCount
; n
++ )
1780 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1782 wxLogLastError(wxT("TreeView_DeleteItem"));
1787 void wxTreeCtrl::DeleteAllItems()
1789 // delete the "virtual" root item.
1790 if ( GET_VIRTUAL_ROOT() )
1792 delete GET_VIRTUAL_ROOT();
1793 m_pVirtualRoot
= NULL
;
1796 // and all the real items
1798 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1800 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1804 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1806 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1807 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1808 flag
== TVE_EXPAND
||
1810 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1812 // A hidden root can be neither expanded nor collapsed.
1813 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1814 wxT("Can't expand/collapse hidden root node!") );
1816 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1817 // emulate them. This behaviour has changed slightly with comctl32.dll
1818 // v 4.70 - now it does send them but only the first time. To maintain
1819 // compatible behaviour and also in order to not have surprises with the
1820 // future versions, don't rely on this and still do everything ourselves.
1821 // To avoid that the messages be sent twice when the item is expanded for
1822 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1824 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1828 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1830 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1831 event
.m_item
= item
;
1832 event
.SetEventObject(this);
1834 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1836 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1840 (void)GetEventHandler()->ProcessEvent(event
);
1842 //else: change didn't took place, so do nothing at all
1845 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1847 DoExpand(item
, TVE_EXPAND
);
1850 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1852 DoExpand(item
, TVE_COLLAPSE
);
1855 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1857 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1860 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1862 DoExpand(item
, TVE_TOGGLE
);
1865 #if WXWIN_COMPATIBILITY_2_4
1867 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1869 DoExpand(item
, action
);
1874 void wxTreeCtrl::Unselect()
1876 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1877 wxT("doesn't make sense, may be you want UnselectAll()?") );
1879 // just remove the selection
1880 SelectItem(wxTreeItemId());
1883 void wxTreeCtrl::UnselectAll()
1885 if ( m_windowStyle
& wxTR_MULTIPLE
)
1887 wxArrayTreeItemIds selections
;
1888 size_t count
= GetSelections(selections
);
1889 for ( size_t n
= 0; n
< count
; n
++ )
1891 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1892 SetItemCheck(HITEM(selections
[n
]), false);
1893 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1894 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1895 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1898 m_htSelStart
.Unset();
1902 // just remove the selection
1907 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1909 if ( m_windowStyle
& wxTR_MULTIPLE
)
1911 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1912 // selecting the item means checking it
1913 SetItemCheck(item
, select
);
1914 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1915 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1916 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1920 wxASSERT_MSG( select
,
1921 _T("SelectItem(false) works only for multiselect") );
1923 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1924 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1925 // send them ourselves
1927 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1928 event
.m_item
= item
;
1929 event
.SetEventObject(this);
1931 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1932 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1934 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1936 wxLogLastError(wxT("TreeView_SelectItem"));
1940 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1941 (void)GetEventHandler()->ProcessEvent(event
);
1944 //else: program vetoed the change
1948 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1951 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1954 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1956 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1958 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1962 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1967 void wxTreeCtrl::DeleteTextCtrl()
1971 // the HWND corresponding to this control is deleted by the tree
1972 // control itself and we don't know when exactly this happens, so check
1973 // if the window still exists before calling UnsubclassWin()
1974 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1976 m_textCtrl
->SetHWND(0);
1979 m_textCtrl
->UnsubclassWin();
1980 m_textCtrl
->SetHWND(0);
1988 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1989 wxClassInfo
* textControlClass
)
1991 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1996 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1997 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1999 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2008 // textctrl is subclassed in MSWOnNotify
2012 // End label editing, optionally cancelling the edit
2013 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2015 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2020 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
)
2022 TV_HITTESTINFO hitTestInfo
;
2023 hitTestInfo
.pt
.x
= (int)point
.x
;
2024 hitTestInfo
.pt
.y
= (int)point
.y
;
2026 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2031 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2032 flags |= wxTREE_HITTEST_##flag
2034 TRANSLATE_FLAG(ABOVE
);
2035 TRANSLATE_FLAG(BELOW
);
2036 TRANSLATE_FLAG(NOWHERE
);
2037 TRANSLATE_FLAG(ONITEMBUTTON
);
2038 TRANSLATE_FLAG(ONITEMICON
);
2039 TRANSLATE_FLAG(ONITEMINDENT
);
2040 TRANSLATE_FLAG(ONITEMLABEL
);
2041 TRANSLATE_FLAG(ONITEMRIGHT
);
2042 TRANSLATE_FLAG(ONITEMSTATEICON
);
2043 TRANSLATE_FLAG(TOLEFT
);
2044 TRANSLATE_FLAG(TORIGHT
);
2046 #undef TRANSLATE_FLAG
2048 return wxTreeItemId(hitTestInfo
.hItem
);
2051 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2053 bool textOnly
) const
2057 // Virtual root items have no bounding rectangle
2058 if ( IS_VIRTUAL_ROOT(item
) )
2063 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2066 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2072 // couldn't retrieve rect: for example, item isn't visible
2077 // ----------------------------------------------------------------------------
2079 // ----------------------------------------------------------------------------
2081 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2082 // functions such as IsDataIndirect()
2083 class wxTreeSortHelper
2086 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2089 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
2091 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
2092 if ( tree
->IsDataIndirect(data
) )
2094 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2097 return data
->GetId();
2101 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2105 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2106 wxT("sorting tree without data doesn't make sense") );
2108 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2110 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2111 GetIdFromData(tree
, pItem2
));
2114 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2116 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2118 // rely on the fact that TreeView_SortChildren does the same thing as our
2119 // default behaviour, i.e. sorts items alphabetically and so call it
2120 // directly if we're not in derived class (much more efficient!)
2121 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2123 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2128 tvSort
.hParent
= HITEM(item
);
2129 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2130 tvSort
.lParam
= (LPARAM
)this;
2131 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2135 // ----------------------------------------------------------------------------
2137 // ----------------------------------------------------------------------------
2139 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2141 if ( cmd
== EN_UPDATE
)
2143 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2144 event
.SetEventObject( this );
2145 ProcessCommand(event
);
2147 else if ( cmd
== EN_KILLFOCUS
)
2149 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2150 event
.SetEventObject( this );
2151 ProcessCommand(event
);
2159 // command processed
2163 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2164 // only do it during dragging, minimize wxWin overhead (this is important for
2165 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2166 // instead of passing by wxWin events
2167 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2169 bool processed
= false;
2171 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2173 // This message is sent after a right-click, or when the "menu" key is pressed
2174 if ( nMsg
== WM_CONTEXTMENU
)
2176 int x
= GET_X_LPARAM(lParam
),
2177 y
= GET_Y_LPARAM(lParam
);
2178 // Convert the screen point to a client point
2179 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2181 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2183 // can't use GetSelection() here as it would assert in multiselect mode
2184 event
.m_item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2185 event
.SetEventObject( this );
2187 // Get the bounding rectangle for the item, including the non-text areas
2189 GetBoundingRect(event
.m_item
, ItemRect
, false);
2190 // If the point is inside the bounding rectangle, use it as the click position.
2191 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2192 if (ItemRect
.Inside(MenuPoint
))
2194 event
.m_pointDrag
= MenuPoint
;
2196 // Use the Explorer standard of putting the menu at the left edge of the text,
2197 // in the vertical middle of the text. Should be the case for the "menu" key
2200 // Use the bounding rectangle of only the text part
2201 GetBoundingRect(event
.m_item
, ItemRect
, true);
2202 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2205 if ( GetEventHandler()->ProcessEvent(event
) )
2207 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2209 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2211 // we only process mouse messages here and these parameters have the
2212 // same meaning for all of them
2213 int x
= GET_X_LPARAM(lParam
),
2214 y
= GET_Y_LPARAM(lParam
);
2215 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2217 TV_HITTESTINFO tvht
;
2221 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2225 case WM_RBUTTONDOWN
:
2226 // if the item we are about to right click on is not already
2227 // selected or if we click outside of any item, remove the
2228 // entire previous selection
2229 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2234 // select item and set the focus to the
2235 // newly selected item
2236 ::SelectItem(GetHwnd(), htItem
);
2237 ::SetFocus(GetHwnd(), htItem
);
2240 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2241 case WM_LBUTTONDOWN
:
2242 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2244 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2245 m_ptClick
= wxPoint(x
, y
);
2247 if ( wParam
& MK_CONTROL
)
2251 // toggle selected state
2252 ::ToggleItemSelection(GetHwnd(), htItem
);
2254 ::SetFocus(GetHwnd(), htItem
);
2256 // reset on any click without Shift
2257 m_htSelStart
.Unset();
2261 else if ( wParam
& MK_SHIFT
)
2263 // this selects all items between the starting one and
2266 if ( !m_htSelStart
)
2268 // take the focused item
2269 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2273 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2274 !(wParam
& MK_CONTROL
));
2276 ::SelectItem(GetHwnd(), htItem
);
2278 ::SetFocus(GetHwnd(), htItem
);
2282 else // normal click
2284 // avoid doing anything if we click on the only
2285 // currently selected item
2289 wxArrayTreeItemIds selections
;
2290 size_t count
= GetSelections(selections
);
2293 HITEM(selections
[0]) != htItem
)
2295 // clear the previously selected items, if the
2296 // user clicked outside of the present selection.
2297 // otherwise, perform the deselection on mouse-up.
2298 // this allows multiple drag and drop to work.
2300 if (!IsItemSelected(GetHwnd(), htItem
))
2304 // prevent the click from starting in-place editing
2305 // which should only happen if we click on the
2306 // already selected item (and nothing else is
2309 TreeView_SelectItem(GetHwnd(), 0);
2310 ::SelectItem(GetHwnd(), htItem
);
2312 ::SetFocus(GetHwnd(), htItem
);
2316 // reset on any click without Shift
2317 m_htSelStart
.Unset();
2321 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2325 if ( m_htClickedItem
)
2327 int cx
= abs(m_ptClick
.x
- x
);
2328 int cy
= abs(m_ptClick
.y
- y
);
2330 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2332 HWND pWnd
= ::GetParent( GetHwnd() );
2337 tv
.hdr
.hwndFrom
= GetHwnd();
2338 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2339 tv
.hdr
.code
= TVN_BEGINDRAG
;
2341 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2344 ZeroMemory(&tviAux
, sizeof(tviAux
));
2345 tviAux
.hItem
= HITEM(m_htClickedItem
);
2346 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2347 tviAux
.stateMask
= 0xffffffff;
2348 TreeView_GetItem( GetHwnd(), &tviAux
);
2350 tv
.itemNew
.state
= tviAux
.state
;
2351 tv
.itemNew
.lParam
= tviAux
.lParam
;
2356 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2358 m_htClickedItem
.Unset();
2361 #endif // __WXWINCE__
2365 m_dragImage
->Move(wxPoint(x
, y
));
2368 // highlight the item as target (hiding drag image is
2369 // necessary - otherwise the display will be corrupted)
2370 m_dragImage
->Hide();
2371 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2372 m_dragImage
->Show();
2379 // facilitates multiple drag-and-drop
2380 if (htItem
&& isMultiple
)
2382 wxArrayTreeItemIds selections
;
2383 size_t count
= GetSelections(selections
);
2386 !(wParam
& MK_CONTROL
) &&
2387 !(wParam
& MK_SHIFT
))
2390 TreeView_SelectItem(GetHwnd(), htItem
);
2391 ::SelectItem(GetHwnd(), htItem
);
2392 ::SetFocus(GetHwnd(), htItem
);
2394 m_htClickedItem
.Unset();
2402 m_dragImage
->EndDrag();
2406 // generate the drag end event
2407 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2409 event
.m_item
= htItem
;
2410 event
.m_pointDrag
= wxPoint(x
, y
);
2411 event
.SetEventObject(this);
2413 (void)GetEventHandler()->ProcessEvent(event
);
2415 // if we don't do it, the tree seems to think that 2 items
2416 // are selected simultaneously which is quite weird
2417 TreeView_SelectDropTarget(GetHwnd(), 0);
2422 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2423 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2425 // the tree control greys out the selected item when it loses focus and
2426 // paints it as selected again when it regains it, but it won't do it
2427 // for the other items itself - help it
2428 wxArrayTreeItemIds selections
;
2429 size_t count
= GetSelections(selections
);
2431 for ( size_t n
= 0; n
< count
; n
++ )
2433 // TreeView_GetItemRect() will return false if item is not visible,
2434 // which may happen perfectly well
2435 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2438 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2442 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2444 bool bCtrl
= wxIsCtrlDown(),
2445 bShift
= wxIsShiftDown();
2447 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2453 ::ToggleItemSelection(GetHwnd(), htSel
);
2459 ::SelectItem(GetHwnd(), htSel
);
2467 if ( !bCtrl
&& !bShift
)
2469 // no modifiers, just clear selection and then let the default
2470 // processing to take place
2475 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2477 HTREEITEM htNext
= (HTREEITEM
)
2478 TreeView_GetNextItem
2482 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2488 // at the top/bottom
2494 if ( !m_htSelStart
)
2495 m_htSelStart
= htSel
;
2497 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2501 // without changing selection
2502 ::SetFocus(GetHwnd(), htNext
);
2513 // TODO: handle Shift/Ctrl with these keys
2514 if ( !bCtrl
&& !bShift
)
2518 m_htSelStart
.Unset();
2522 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2523 else if ( nMsg
== WM_COMMAND
)
2525 // if we receive a EN_KILLFOCUS command from the in-place edit control
2526 // used for label editing, make sure to end editing
2529 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2531 if ( cmd
== EN_KILLFOCUS
)
2533 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2543 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2549 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2551 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2552 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2553 // the parent window, not us, which completely breaks everything so simply
2554 // don't let it see this message at all
2555 if ( nMsg
== WM_RBUTTONDOWN
)
2558 // but because of the above we don't get NM_RCLICK which is normally
2559 // generated by tree window proc when the modal loop mentioned above ends
2560 // because the mouse is released -- synthesize it ourselves instead
2561 if ( nMsg
== WM_RBUTTONUP
)
2564 hdr
.hwndFrom
= GetHwnd();
2565 hdr
.idFrom
= GetId();
2566 hdr
.code
= NM_RCLICK
;
2569 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2571 // continue as usual
2574 if ( nMsg
== WM_CHAR
)
2576 // also don't let the control process Space and Return keys because it
2577 // doesn't do anything useful with them anyhow but always beeps
2578 // annoyingly when it receives them and there is no way to turn it off
2579 // simply if you just process TREEITEM_ACTIVATED event to which Space
2580 // and Enter presses are mapped in your code
2581 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2585 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2588 // process WM_NOTIFY Windows message
2589 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2591 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2592 wxEventType eventType
= wxEVT_NULL
;
2593 NMHDR
*hdr
= (NMHDR
*)lParam
;
2595 switch ( hdr
->code
)
2598 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2601 case TVN_BEGINRDRAG
:
2603 if ( eventType
== wxEVT_NULL
)
2604 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2605 //else: left drag, already set above
2607 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2609 event
.m_item
= tv
->itemNew
.hItem
;
2610 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2612 // don't allow dragging by default: the user code must
2613 // explicitly say that it wants to allow it to avoid breaking
2619 case TVN_BEGINLABELEDIT
:
2621 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2622 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2624 // although the user event handler may still veto it, it is
2625 // important to set it now so that calls to SetItemText() from
2626 // the event handler would change the text controls contents
2628 event
.m_item
= info
->item
.hItem
;
2629 event
.m_label
= info
->item
.pszText
;
2630 event
.m_editCancelled
= false;
2634 case TVN_DELETEITEM
:
2636 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2637 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2639 event
.m_item
= tv
->itemOld
.hItem
;
2643 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2644 if ( it
!= m_attrs
.end() )
2653 case TVN_ENDLABELEDIT
:
2655 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2656 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2658 event
.m_item
= info
->item
.hItem
;
2659 event
.m_label
= info
->item
.pszText
;
2660 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2665 // These *must* not be removed or TVN_GETINFOTIP will
2666 // not be processed each time the mouse is moved
2667 // and the tooltip will only ever update once.
2676 #ifdef TVN_GETINFOTIP
2677 case TVN_GETINFOTIP
:
2679 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2680 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2682 // Which item are we trying to get a tooltip for?
2683 event
.m_item
= info
->hItem
;
2690 case TVN_GETDISPINFO
:
2691 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2694 case TVN_SETDISPINFO
:
2696 if ( eventType
== wxEVT_NULL
)
2697 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2698 //else: get, already set above
2700 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2702 event
.m_item
= info
->item
.hItem
;
2706 case TVN_ITEMEXPANDING
:
2707 case TVN_ITEMEXPANDED
:
2709 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2712 switch ( tv
->action
)
2715 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2723 what
= IDX_COLLAPSE
;
2727 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2730 eventType
= gs_expandEvents
[what
][how
];
2732 event
.m_item
= tv
->itemNew
.hItem
;
2738 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2739 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2741 // fabricate the lParam and wParam parameters sufficiently
2742 // similar to the ones from a "real" WM_KEYDOWN so that
2743 // CreateKeyEvent() works correctly
2744 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2745 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2747 WXWPARAM wParam
= info
->wVKey
;
2749 int keyCode
= wxCharCodeMSWToWX(wParam
);
2752 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2757 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2762 // a separate event for Space/Return
2763 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2764 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2766 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2768 event2
.SetEventObject(this);
2769 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2771 event2
.m_item
= GetSelection();
2773 //else: don't know how to get it
2775 (void)GetEventHandler()->ProcessEvent(event2
);
2780 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2781 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2782 // we have to handle both messages:
2783 case TVN_SELCHANGEDA
:
2784 case TVN_SELCHANGEDW
:
2785 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2788 case TVN_SELCHANGINGA
:
2789 case TVN_SELCHANGINGW
:
2791 if ( eventType
== wxEVT_NULL
)
2792 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2793 //else: already set above
2795 if (hdr
->code
== TVN_SELCHANGINGW
||
2796 hdr
->code
== TVN_SELCHANGEDW
)
2798 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2799 event
.m_item
= tv
->itemNew
.hItem
;
2800 event
.m_itemOld
= tv
->itemOld
.hItem
;
2804 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2805 event
.m_item
= tv
->itemNew
.hItem
;
2806 event
.m_itemOld
= tv
->itemOld
.hItem
;
2811 // instead of explicitly checking for _WIN32_IE, check if the
2812 // required symbols are available in the headers
2813 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2816 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2817 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2818 switch ( nmcd
.dwDrawStage
)
2821 // if we've got any items with non standard attributes,
2822 // notify us before painting each item
2823 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2827 case CDDS_ITEMPREPAINT
:
2829 wxMapTreeAttr::iterator
2830 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2832 if ( it
== m_attrs
.end() )
2834 // nothing to do for this item
2835 *result
= CDRF_DODEFAULT
;
2839 wxTreeItemAttr
* const attr
= it
->second
;
2841 // selection colours should override ours,
2842 // otherwise it is too confusing ot the user
2843 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) )
2846 if ( attr
->HasBackgroundColour() )
2848 colBack
= attr
->GetBackgroundColour();
2849 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2853 // but we still want to keep the special foreground
2854 // colour when we don't have focus (we can't keep
2855 // it when we do, it would usually be unreadable on
2856 // the almost inverted bg colour...)
2857 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2858 FindFocus() != this )
2861 if ( attr
->HasTextColour() )
2863 colText
= attr
->GetTextColour();
2864 lptvcd
->clrText
= wxColourToRGB(colText
);
2868 if ( attr
->HasFont() )
2870 HFONT hFont
= GetHfontOf(attr
->GetFont());
2872 ::SelectObject(nmcd
.hdc
, hFont
);
2874 *result
= CDRF_NEWFONT
;
2876 else // no specific font
2878 *result
= CDRF_DODEFAULT
;
2884 *result
= CDRF_DODEFAULT
;
2888 // we always process it
2890 #endif // have owner drawn support in headers
2894 DWORD pos
= GetMessagePos();
2896 point
.x
= LOWORD(pos
);
2897 point
.y
= HIWORD(pos
);
2898 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2900 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2901 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2903 event
.m_item
= item
;
2904 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2912 TV_HITTESTINFO tvhti
;
2913 ::GetCursorPos(&tvhti
.pt
);
2914 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2915 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2917 if ( tvhti
.flags
& TVHT_ONITEM
)
2919 event
.m_item
= tvhti
.hItem
;
2920 eventType
= (int)hdr
->code
== NM_DBLCLK
2921 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2922 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2924 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2925 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2934 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2937 event
.SetEventObject(this);
2938 event
.SetEventType(eventType
);
2940 bool processed
= GetEventHandler()->ProcessEvent(event
);
2943 switch ( hdr
->code
)
2946 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2947 // the return code of this event handler as the return value for
2948 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2949 // expanded status would never work
2954 case TVN_BEGINRDRAG
:
2955 if ( event
.IsAllowed() )
2957 // normally this is impossible because the m_dragImage is
2958 // deleted once the drag operation is over
2959 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2961 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2962 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2963 m_dragImage
->Show();
2967 case TVN_DELETEITEM
:
2969 // NB: we might process this message using wxWidgets event
2970 // tables, but due to overhead of wxWin event system we
2971 // prefer to do it here ourself (otherwise deleting a tree
2972 // with many items is just too slow)
2973 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2975 wxTreeItemId item
= event
.m_item
;
2976 if ( HasIndirectData(item
) )
2978 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2980 delete data
; // can't be NULL here
2984 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2985 delete data
; // may be NULL, ok
2988 processed
= true; // Make sure we don't get called twice
2992 case TVN_BEGINLABELEDIT
:
2993 // return true to cancel label editing
2994 *result
= !event
.IsAllowed();
2996 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2997 if ( event
.IsAllowed() )
2999 HWND hText
= TreeView_GetEditControl(GetHwnd());
3002 // MBN: if m_textCtrl already has an HWND, it is a stale
3003 // pointer from a previous edit (because the user
3004 // didn't modify the label before dismissing the control,
3005 // and TVN_ENDLABELEDIT was not sent), so delete it
3006 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
3009 m_textCtrl
= new wxTextCtrl();
3010 m_textCtrl
->SetParent(this);
3011 m_textCtrl
->SetHWND((WXHWND
)hText
);
3012 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3014 // set wxTE_PROCESS_ENTER style for the text control to
3015 // force it to process the Enter presses itself, otherwise
3016 // they could be stolen from it by the dialog
3018 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3019 | wxTE_PROCESS_ENTER
);
3022 else // we had set m_idEdited before
3028 case TVN_ENDLABELEDIT
:
3029 // return true to set the label to the new string: note that we
3030 // also must pretend that we did process the message or it is going
3031 // to be passed to DefWindowProc() which will happily return false
3032 // cancelling the label change
3033 *result
= event
.IsAllowed();
3036 // ensure that we don't have the text ctrl which is going to be
3042 #ifdef TVN_GETINFOTIP
3043 case TVN_GETINFOTIP
:
3045 // If the user permitted a tooltip change, change it
3046 if (event
.IsAllowed())
3048 SetToolTip(event
.m_label
);
3055 case TVN_SELCHANGING
:
3056 case TVN_ITEMEXPANDING
:
3057 // return true to prevent the action from happening
3058 *result
= !event
.IsAllowed();
3061 case TVN_ITEMEXPANDED
:
3062 // the item is not refreshed properly after expansion when it has
3063 // an image depending on the expanded/collapsed state - bug in
3064 // comctl32.dll or our code?
3066 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
3067 wxTreeItemId
id(tv
->itemNew
.hItem
);
3069 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3077 case TVN_GETDISPINFO
:
3078 // NB: so far the user can't set the image himself anyhow, so do it
3079 // anyway - but this may change later
3080 //if ( /* !processed && */ 1 )
3082 wxTreeItemId item
= event
.m_item
;
3083 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3084 if ( info
->item
.mask
& TVIF_IMAGE
)
3087 DoGetItemImageFromData
3090 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3091 : wxTreeItemIcon_Normal
3094 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3096 info
->item
.iSelectedImage
=
3097 DoGetItemImageFromData
3100 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3101 : wxTreeItemIcon_Selected
3108 // for the other messages the return value is ignored and there is
3109 // nothing special to do
3114 // ----------------------------------------------------------------------------
3116 // ----------------------------------------------------------------------------
3118 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3119 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3121 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
3124 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3125 tvi
.mask
= TVIF_STATE
;
3126 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3128 // Select the specified state, or -1 == cycle to the next one.
3131 TreeView_GetItem(GetHwnd(), &tvi
);
3133 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
3134 if ( state
== m_imageListState
->GetImageCount() )
3138 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3139 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3141 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3143 TreeView_SetItem(GetHwnd(), &tvi
);
3146 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3149 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3150 tvi
.mask
= TVIF_STATE
;
3151 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3152 TreeView_GetItem(GetHwnd(), &tvi
);
3154 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3157 #endif // wxUSE_TREECTRL