1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/msw/private.h"
31 // include <commctrl.h> "properly"
32 #include "wx/msw/wrapcctl.h"
34 #include "wx/msw/missing.h"
36 // Set this to 1 to be _absolutely_ sure that repainting will work for all
37 // comctl32.dll versions
38 #define wxUSE_COMCTL32_SAFELY 0
42 #include "wx/dynarray.h"
43 #include "wx/imaglist.h"
44 #include "wx/settings.h"
45 #include "wx/msw/treectrl.h"
46 #include "wx/msw/dragimag.h"
48 // macros to hide the cast ugliness
49 // --------------------------------
51 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
52 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
54 // item here is a wxTreeItemId
55 #define HITEM(item) HITEM_PTR((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 // do traverse the tree: visit all items (recursively by default) under the
318 // given one; return true if all items were traversed or false if the
319 // traversal was aborted because OnVisit returned false
320 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
322 // override this function to do whatever is needed for each item, return
323 // false to stop traversing
324 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
327 const wxTreeCtrl
*GetTree() const { return m_tree
; }
330 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
332 const wxTreeCtrl
*m_tree
;
334 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
337 // internal class for getting the selected items
338 class TraverseSelections
: public wxTreeTraversal
341 TraverseSelections(const wxTreeCtrl
*tree
,
342 wxArrayTreeItemIds
& selections
)
343 : wxTreeTraversal(tree
), m_selections(selections
)
345 m_selections
.Empty();
347 if (tree
->GetCount() > 0)
348 DoTraverse(tree
->GetRootItem());
351 virtual bool OnVisit(const wxTreeItemId
& item
)
353 // can't visit a virtual node.
354 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
359 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
360 if ( GetTree()->IsItemChecked(item
) )
362 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
365 m_selections
.Add(item
);
371 size_t GetCount() const { return m_selections
.GetCount(); }
374 wxArrayTreeItemIds
& m_selections
;
376 DECLARE_NO_COPY_CLASS(TraverseSelections
)
379 // internal class for counting tree items
380 class TraverseCounter
: public wxTreeTraversal
383 TraverseCounter(const wxTreeCtrl
*tree
,
384 const wxTreeItemId
& root
,
386 : wxTreeTraversal(tree
)
390 DoTraverse(root
, recursively
);
393 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
400 size_t GetCount() const { return m_count
; }
405 DECLARE_NO_COPY_CLASS(TraverseCounter
)
408 // ----------------------------------------------------------------------------
409 // This class is needed for support of different images: the Win32 common
410 // control natively supports only 2 images (the normal one and another for the
411 // selected state). We wish to provide support for 2 more of them for folder
412 // items (i.e. those which have children): for expanded state and for expanded
413 // selected state. For this we use this structure to store the additional items
416 // There is only one problem with this: when we retrieve the item's data, we
417 // don't know whether we get a pointer to wxTreeItemData or
418 // wxTreeItemIndirectData. So we always set the item id to an invalid value
419 // in this class and the code using the client data checks for it and retrieves
420 // the real client data in this case.
421 // ----------------------------------------------------------------------------
423 class wxTreeItemIndirectData
: public wxTreeItemData
426 // ctor associates this data with the item and the real item data becomes
427 // available through our GetData() method
428 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
430 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
436 m_data
= tree
->GetItemData(item
);
438 // and set ourselves as the new one
439 tree
->SetIndirectItemData(item
, this);
441 // we must have the invalid value for the item
445 // dtor deletes the associated data as well
446 virtual ~wxTreeItemIndirectData() { delete m_data
; }
449 // get the real data associated with the item
450 wxTreeItemData
*GetData() const { return m_data
; }
452 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
454 // do we have such image?
455 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
457 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
459 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
462 // all the images associated with the item
463 int m_images
[wxTreeItemIcon_Max
];
465 // the real client data
466 wxTreeItemData
*m_data
;
468 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
471 // ----------------------------------------------------------------------------
473 // ----------------------------------------------------------------------------
475 #if wxUSE_EXTENDED_RTTI
476 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
478 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
479 // new style border flags, we put them first to
480 // use them for streaming out
481 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
482 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
483 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
484 wxFLAGS_MEMBER(wxBORDER_RAISED
)
485 wxFLAGS_MEMBER(wxBORDER_STATIC
)
486 wxFLAGS_MEMBER(wxBORDER_NONE
)
488 // old style border flags
489 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
490 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
491 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
492 wxFLAGS_MEMBER(wxRAISED_BORDER
)
493 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
494 wxFLAGS_MEMBER(wxBORDER
)
496 // standard window styles
497 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
498 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
499 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
500 wxFLAGS_MEMBER(wxWANTS_CHARS
)
501 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
502 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
503 wxFLAGS_MEMBER(wxVSCROLL
)
504 wxFLAGS_MEMBER(wxHSCROLL
)
506 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
507 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
508 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
509 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
510 wxFLAGS_MEMBER(wxTR_NO_LINES
)
511 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
512 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
513 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
514 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
515 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
516 wxFLAGS_MEMBER(wxTR_SINGLE
)
517 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
518 wxFLAGS_MEMBER(wxTR_EXTENDED
)
519 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
521 wxEND_FLAGS( wxTreeCtrlStyle
)
523 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
525 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
526 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
527 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
528 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
529 wxEND_PROPERTIES_TABLE()
531 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
532 wxEND_HANDLERS_TABLE()
534 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
536 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 // indices in gs_expandEvents table below
558 // handy table for sending events - it has to be initialized during run-time
559 // now so can't be const any more
560 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
563 but logically it's a const table with the following entries:
566 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
567 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
571 // ============================================================================
573 // ============================================================================
575 // ----------------------------------------------------------------------------
577 // ----------------------------------------------------------------------------
579 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
581 if ( !OnVisit(root
) )
584 return Traverse(root
, recursively
);
587 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
589 wxTreeItemIdValue cookie
;
590 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
591 while ( child
.IsOk() )
593 // depth first traversal
594 if ( recursively
&& !Traverse(child
, true) )
597 if ( !OnVisit(child
) )
600 child
= m_tree
->GetNextChild(root
, cookie
);
606 // ----------------------------------------------------------------------------
607 // construction and destruction
608 // ----------------------------------------------------------------------------
610 void wxTreeCtrl::Init()
612 m_imageListNormal
= NULL
;
613 m_imageListState
= NULL
;
614 m_ownsImageListNormal
= m_ownsImageListState
= false;
616 m_hasAnyAttr
= false;
618 m_pVirtualRoot
= NULL
;
620 // initialize the global array of events now as it can't be done statically
621 // with the wxEVT_XXX values being allocated during run-time only
622 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
623 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
624 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
625 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
628 bool wxTreeCtrl::Create(wxWindow
*parent
,
633 const wxValidator
& validator
,
634 const wxString
& name
)
638 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
639 style
|= wxBORDER_SUNKEN
;
641 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
645 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
646 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
648 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
649 wstyle
|= TVS_HASLINES
;
650 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
651 wstyle
|= TVS_HASBUTTONS
;
653 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
654 wstyle
|= TVS_EDITLABELS
;
656 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
657 wstyle
|= TVS_LINESATROOT
;
659 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
661 if ( wxApp::GetComCtl32Version() >= 471 )
662 wstyle
|= TVS_FULLROWSELECT
;
665 // using TVS_CHECKBOXES for emulation of a multiselection tree control
666 // doesn't work without the new enough headers
667 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
668 !defined( __GNUWIN32_OLD__ ) && \
669 !defined( __BORLANDC__ ) && \
670 !defined( __WATCOMC__ ) && \
671 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
673 // we emulate the multiple selection tree controls by using checkboxes: set
674 // up the image list we need for this if we do have multiple selections
675 if ( m_windowStyle
& wxTR_MULTIPLE
)
676 wstyle
|= TVS_CHECKBOXES
;
677 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
679 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
680 // Need so that TVN_GETINFOTIP messages will be sent
681 wstyle
|= TVS_INFOTIP
;
684 // Create the tree control.
685 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
688 #if wxUSE_COMCTL32_SAFELY
689 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
690 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
692 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
693 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
695 // This works around a bug in the Windows tree control whereby for some versions
696 // of comctrl32, setting any colour actually draws the background in black.
697 // This will initialise the background to the system colour.
698 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
699 // Assume the user has an updated comctl32.dll.
700 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
701 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
702 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
706 // VZ: this is some experimental code which may be used to get the
707 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
708 // AFAIK, the standard DLL does about the same thing anyhow.
710 if ( m_windowStyle
& wxTR_MULTIPLE
)
714 // create the DC compatible with the current screen
715 HDC hdcMem
= CreateCompatibleDC(NULL
);
717 // create a mono bitmap of the standard size
718 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
719 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
720 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
721 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
722 1, // # of color planes
723 1, // # bits needed for one pixel
724 0); // array containing colour data
725 SelectObject(hdcMem
, hbmpCheck
);
727 // then draw a check mark into it
728 RECT rect
= { 0, 0, x
, y
};
729 if ( !::DrawFrameControl(hdcMem
, &rect
,
731 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
733 wxLogLastError(wxT("DrawFrameControl(check)"));
736 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
737 imagelistCheckboxes
.Add(bmp
);
739 if ( !::DrawFrameControl(hdcMem
, &rect
,
743 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
746 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
747 imagelistCheckboxes
.Add(bmp
);
753 SetStateImageList(&imagelistCheckboxes
);
757 wxSetCCUnicodeFormat(GetHwnd());
762 wxTreeCtrl::~wxTreeCtrl()
764 // delete any attributes
767 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
769 // prevent TVN_DELETEITEM handler from deleting the attributes again!
770 m_hasAnyAttr
= false;
775 // delete user data to prevent memory leaks
776 // also deletes hidden root node storage.
779 if (m_ownsImageListNormal
) delete m_imageListNormal
;
780 if (m_ownsImageListState
) delete m_imageListState
;
783 // ----------------------------------------------------------------------------
785 // ----------------------------------------------------------------------------
787 /* static */ wxVisualAttributes
788 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
790 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
792 // common controls have their own default font
793 attrs
.font
= wxGetCCDefaultFont();
799 // simple wrappers which add error checking in debug mode
801 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
803 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
804 _T("can't retrieve virtual root item") );
806 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
808 wxLogLastError(wxT("TreeView_GetItem"));
816 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
818 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
820 wxLogLastError(wxT("TreeView_SetItem"));
824 size_t wxTreeCtrl::GetCount() const
826 return (size_t)TreeView_GetCount(GetHwnd());
829 unsigned int wxTreeCtrl::GetIndent() const
831 return TreeView_GetIndent(GetHwnd());
834 void wxTreeCtrl::SetIndent(unsigned int indent
)
836 TreeView_SetIndent(GetHwnd(), indent
);
839 wxImageList
*wxTreeCtrl::GetImageList() const
841 return m_imageListNormal
;
844 wxImageList
*wxTreeCtrl::GetStateImageList() const
846 return m_imageListState
;
849 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
852 TreeView_SetImageList(GetHwnd(),
853 imageList
? imageList
->GetHIMAGELIST() : 0,
857 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
859 if (m_ownsImageListNormal
)
860 delete m_imageListNormal
;
862 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
863 m_ownsImageListNormal
= false;
866 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
868 if (m_ownsImageListState
) delete m_imageListState
;
869 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
870 m_ownsImageListState
= false;
873 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
875 SetImageList(imageList
);
876 m_ownsImageListNormal
= true;
879 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
881 SetStateImageList(imageList
);
882 m_ownsImageListState
= true;
885 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
886 bool recursively
) const
888 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
890 TraverseCounter
counter(this, item
, recursively
);
891 return counter
.GetCount() - 1;
894 // ----------------------------------------------------------------------------
896 // ----------------------------------------------------------------------------
898 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
900 #if !wxUSE_COMCTL32_SAFELY
901 if ( !wxWindowBase::SetBackgroundColour(colour
) )
904 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
910 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
912 #if !wxUSE_COMCTL32_SAFELY
913 if ( !wxWindowBase::SetForegroundColour(colour
) )
916 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
922 // ----------------------------------------------------------------------------
924 // ----------------------------------------------------------------------------
926 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
928 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
930 wxChar buf
[512]; // the size is arbitrary...
932 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
933 tvItem
.pszText
= buf
;
934 tvItem
.cchTextMax
= WXSIZEOF(buf
);
935 if ( !DoGetItem(&tvItem
) )
937 // don't return some garbage which was on stack, but an empty string
941 return wxString(buf
);
944 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
946 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
948 if ( IS_VIRTUAL_ROOT(item
) )
951 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
952 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
955 // when setting the text of the item being edited, the text control should
956 // be updated to reflect the new text as well, otherwise calling
957 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
959 // don't use GetEditControl() here because m_textCtrl is not set yet
960 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
963 if ( item
== m_idEdited
)
965 ::SetWindowText(hwndEdit
, text
);
970 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
971 wxTreeItemIcon which
) const
973 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
974 if ( !DoGetItem(&tvItem
) )
979 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
982 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
984 wxTreeItemIcon which
) const
986 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
987 if ( !DoGetItem(&tvItem
) )
992 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
994 data
->SetImage(image
, which
);
996 // make sure that we have selected images as well
997 if ( which
== wxTreeItemIcon_Normal
&&
998 !data
->HasImage(wxTreeItemIcon_Selected
) )
1000 data
->SetImage(image
, wxTreeItemIcon_Selected
);
1003 if ( which
== wxTreeItemIcon_Expanded
&&
1004 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
1006 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
1010 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
1014 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
1015 tvItem
.iSelectedImage
= imageSel
;
1016 tvItem
.iImage
= image
;
1020 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1021 wxTreeItemIcon which
) const
1023 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1025 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1027 // TODO: Maybe a hidden root can still provide images?
1031 if ( HasIndirectData(item
) )
1033 return DoGetItemImageFromData(item
, which
);
1040 wxFAIL_MSG( wxT("unknown tree item image type") );
1042 case wxTreeItemIcon_Normal
:
1046 case wxTreeItemIcon_Selected
:
1047 mask
= TVIF_SELECTEDIMAGE
;
1050 case wxTreeItemIcon_Expanded
:
1051 case wxTreeItemIcon_SelectedExpanded
:
1055 wxTreeViewItem
tvItem(item
, mask
);
1058 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1061 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1062 wxTreeItemIcon which
)
1064 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1066 if ( IS_VIRTUAL_ROOT(item
) )
1068 // TODO: Maybe a hidden root can still store images?
1078 wxFAIL_MSG( wxT("unknown tree item image type") );
1081 case wxTreeItemIcon_Normal
:
1083 const int imageNormalOld
= GetItemImage(item
);
1084 const int imageSelOld
=
1085 GetItemImage(item
, wxTreeItemIcon_Selected
);
1087 // always set the normal image
1088 imageNormal
= image
;
1090 // if the selected and normal images were the same, they should
1091 // be the same after the update, otherwise leave the selected
1093 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1097 case wxTreeItemIcon_Selected
:
1098 imageNormal
= GetItemImage(item
);
1102 case wxTreeItemIcon_Expanded
:
1103 case wxTreeItemIcon_SelectedExpanded
:
1104 if ( !HasIndirectData(item
) )
1106 // we need to get the old images first, because after we create
1107 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1109 imageNormal
= GetItemImage(item
);
1110 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1112 // if it doesn't have it yet, add it
1113 wxTreeItemIndirectData
*data
= new
1114 wxTreeItemIndirectData(this, item
);
1116 // copy the data to the new location
1117 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1118 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1121 DoSetItemImageFromData(item
, image
, which
);
1123 // reset the normal/selected images because we won't use them any
1124 // more - now they're stored inside the indirect data
1126 imageSel
= I_IMAGECALLBACK
;
1130 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1131 // change both normal and selected image - otherwise the change simply
1132 // doesn't take place!
1133 DoSetItemImages(item
, imageNormal
, imageSel
);
1136 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1138 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1140 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1142 // Hidden root may have data.
1143 if ( IS_VIRTUAL_ROOT(item
) )
1145 return GET_VIRTUAL_ROOT()->GetData();
1149 if ( !DoGetItem(&tvItem
) )
1154 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1155 if ( IsDataIndirect(data
) )
1157 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1163 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1165 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1167 if ( IS_VIRTUAL_ROOT(item
) )
1169 GET_VIRTUAL_ROOT()->SetData(data
);
1172 // first, associate this piece of data with this item
1178 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1180 if ( HasIndirectData(item
) )
1182 if ( DoGetItem(&tvItem
) )
1184 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1188 wxFAIL_MSG( wxT("failed to change tree items data") );
1193 tvItem
.lParam
= (LPARAM
)data
;
1198 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1199 wxTreeItemIndirectData
*data
)
1201 // this should never happen because it's unnecessary and will probably lead
1202 // to crash too because the code elsewhere supposes that the pointer the
1203 // wxTreeItemIndirectData has is a real wxItemData and not
1204 // wxTreeItemIndirectData as well
1205 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1207 SetItemData(item
, data
);
1210 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1212 // query the item itself
1213 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1214 if ( !DoGetItem(&tvItem
) )
1219 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1221 return data
&& IsDataIndirect(data
);
1224 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1226 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1228 if ( IS_VIRTUAL_ROOT(item
) )
1231 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1232 tvItem
.cChildren
= (int)has
;
1236 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1238 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1240 if ( IS_VIRTUAL_ROOT(item
) )
1243 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1244 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1248 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1250 if ( IS_VIRTUAL_ROOT(item
) )
1253 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1254 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1258 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1260 if ( IS_VIRTUAL_ROOT(item
) )
1264 if ( GetBoundingRect(item
, rect
) )
1270 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1272 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1274 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1275 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1278 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1280 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1282 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1283 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1286 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1288 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1290 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1291 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1294 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1295 const wxColour
& col
)
1297 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1299 wxTreeItemAttr
*attr
;
1300 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1301 if ( it
== m_attrs
.end() )
1303 m_hasAnyAttr
= true;
1305 m_attrs
[item
.m_pItem
] =
1306 attr
= new wxTreeItemAttr
;
1313 attr
->SetTextColour(col
);
1318 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1319 const wxColour
& col
)
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
->SetBackgroundColour(col
);
1342 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1344 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1346 wxTreeItemAttr
*attr
;
1347 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1348 if ( it
== m_attrs
.end() )
1350 m_hasAnyAttr
= true;
1352 m_attrs
[item
.m_pItem
] =
1353 attr
= new wxTreeItemAttr
;
1355 else // already in the hash
1360 attr
->SetFont(font
);
1365 // ----------------------------------------------------------------------------
1367 // ----------------------------------------------------------------------------
1369 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1371 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1373 if ( item
== wxTreeItemId(TVI_ROOT
) )
1375 // virtual (hidden) root is never visible
1379 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1382 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1383 // the HTREEITEM with TVM_GETITEMRECT
1384 *(HTREEITEM
*)&rect
= HITEM(item
);
1386 // true means to get rect for just the text, not the whole line
1387 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1389 // if TVM_GETITEMRECT returned false, then the item is definitely not
1390 // visible (because its parent is not expanded)
1394 // however if it returned true, the item might still be outside the
1395 // currently visible part of the tree, test for it (notice that partly
1396 // visible means visible here)
1397 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1400 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1402 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1404 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1407 return tvItem
.cChildren
!= 0;
1410 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1412 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1414 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1417 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1420 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1422 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1424 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1427 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1430 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1432 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1434 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1437 return (tvItem
.state
& TVIS_BOLD
) != 0;
1440 // ----------------------------------------------------------------------------
1442 // ----------------------------------------------------------------------------
1444 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1446 // Root may be real (visible) or virtual (hidden).
1447 if ( GET_VIRTUAL_ROOT() )
1450 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1453 wxTreeItemId
wxTreeCtrl::GetSelection() const
1455 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1456 wxT("this only works with single selection controls") );
1458 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1461 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1463 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1467 if ( IS_VIRTUAL_ROOT(item
) )
1469 // no parent for the virtual root
1474 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1475 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1477 // the top level items should have the virtual root as their parent
1482 return wxTreeItemId(hItem
);
1485 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1486 wxTreeItemIdValue
& cookie
) const
1488 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1490 // remember the last child returned in 'cookie'
1491 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1493 return wxTreeItemId(cookie
);
1496 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1497 wxTreeItemIdValue
& cookie
) const
1499 wxTreeItemId
fromCookie(cookie
);
1501 HTREEITEM hitem
= HITEM(fromCookie
);
1503 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1505 wxTreeItemId
item(hitem
);
1507 cookie
= item
.m_pItem
;
1512 #if WXWIN_COMPATIBILITY_2_4
1514 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1517 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1519 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1521 return wxTreeItemId((void *)cookie
);
1524 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1527 wxTreeItemId
fromCookie((void *)cookie
);
1529 HTREEITEM hitem
= HITEM(fromCookie
);
1531 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1533 wxTreeItemId
item(hitem
);
1535 cookie
= (long)item
.m_pItem
;
1540 #endif // WXWIN_COMPATIBILITY_2_4
1542 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1544 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1546 // can this be done more efficiently?
1547 wxTreeItemIdValue cookie
;
1549 wxTreeItemId childLast
,
1550 child
= GetFirstChild(item
, cookie
);
1551 while ( child
.IsOk() )
1554 child
= GetNextChild(item
, cookie
);
1560 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1562 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1563 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1566 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1568 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1569 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1572 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1574 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1577 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1579 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1580 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1582 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1585 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1587 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1588 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1590 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1593 // ----------------------------------------------------------------------------
1594 // multiple selections emulation
1595 // ----------------------------------------------------------------------------
1597 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1599 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1601 // receive the desired information.
1602 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1605 // state image indices are 1 based
1606 return ((tvItem
.state
>> 12) - 1) == 1;
1609 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1611 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1613 // receive the desired information.
1614 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1618 // state images are one-based
1619 tvItem
.state
= (check
? 2 : 1) << 12;
1624 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1626 TraverseSelections
selector(this, selections
);
1628 return selector
.GetCount();
1631 // ----------------------------------------------------------------------------
1633 // ----------------------------------------------------------------------------
1635 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1636 wxTreeItemId hInsertAfter
,
1637 const wxString
& text
,
1638 int image
, int selectedImage
,
1639 wxTreeItemData
*data
)
1641 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1643 _T("can't have more than one root in the tree") );
1645 TV_INSERTSTRUCT tvIns
;
1646 tvIns
.hParent
= HITEM(parent
);
1647 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1649 // this is how we insert the item as the first child: supply a NULL
1651 if ( !tvIns
.hInsertAfter
)
1653 tvIns
.hInsertAfter
= TVI_FIRST
;
1657 if ( !text
.empty() )
1660 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1664 tvIns
.item
.pszText
= NULL
;
1665 tvIns
.item
.cchTextMax
= 0;
1671 tvIns
.item
.iImage
= image
;
1673 if ( selectedImage
== -1 )
1675 // take the same image for selected icon if not specified
1676 selectedImage
= image
;
1680 if ( selectedImage
!= -1 )
1682 mask
|= TVIF_SELECTEDIMAGE
;
1683 tvIns
.item
.iSelectedImage
= selectedImage
;
1689 tvIns
.item
.lParam
= (LPARAM
)data
;
1692 tvIns
.item
.mask
= mask
;
1694 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1697 wxLogLastError(wxT("TreeView_InsertItem"));
1702 // associate the application tree item with Win32 tree item handle
1706 return wxTreeItemId(id
);
1709 // for compatibility only
1710 #if WXWIN_COMPATIBILITY_2_4
1712 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1713 const wxString
& text
,
1714 int image
, int selImage
,
1717 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1718 image
, selImage
, NULL
);
1721 wxImageList
*wxTreeCtrl::GetImageList(int) const
1723 return GetImageList();
1726 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1728 SetImageList(imageList
);
1731 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1733 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1736 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1738 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1741 #endif // WXWIN_COMPATIBILITY_2_4
1743 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1744 int image
, int selectedImage
,
1745 wxTreeItemData
*data
)
1748 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1750 // create a virtual root item, the parent for all the others
1751 m_pVirtualRoot
= new wxVirtualNode(data
);
1756 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1757 text
, image
, selectedImage
, data
);
1760 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1761 const wxString
& text
,
1762 int image
, int selectedImage
,
1763 wxTreeItemData
*data
)
1765 return DoInsertItem(parent
, TVI_FIRST
,
1766 text
, image
, selectedImage
, data
);
1769 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1770 const wxTreeItemId
& idPrevious
,
1771 const wxString
& text
,
1772 int image
, int selectedImage
,
1773 wxTreeItemData
*data
)
1775 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1778 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1780 const wxString
& text
,
1781 int image
, int selectedImage
,
1782 wxTreeItemData
*data
)
1784 // find the item from index
1785 wxTreeItemIdValue cookie
;
1786 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1787 while ( index
!= 0 && idCur
.IsOk() )
1792 idCur
= GetNextChild(parent
, cookie
);
1795 // assert, not check: if the index is invalid, we will append the item
1797 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1799 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1802 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1803 const wxString
& text
,
1804 int image
, int selectedImage
,
1805 wxTreeItemData
*data
)
1807 return DoInsertItem(parent
, TVI_LAST
,
1808 text
, image
, selectedImage
, data
);
1811 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1813 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1815 wxLogLastError(wxT("TreeView_DeleteItem"));
1819 // delete all children (but don't delete the item itself)
1820 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1822 wxTreeItemIdValue cookie
;
1824 wxArrayTreeItemIds children
;
1825 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1826 while ( child
.IsOk() )
1828 children
.Add(child
);
1830 child
= GetNextChild(item
, cookie
);
1833 size_t nCount
= children
.Count();
1834 for ( size_t n
= 0; n
< nCount
; n
++ )
1836 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1838 wxLogLastError(wxT("TreeView_DeleteItem"));
1843 void wxTreeCtrl::DeleteAllItems()
1845 // delete the "virtual" root item.
1846 if ( GET_VIRTUAL_ROOT() )
1848 delete GET_VIRTUAL_ROOT();
1849 m_pVirtualRoot
= NULL
;
1852 // and all the real items
1854 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1856 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1860 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1862 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1863 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1864 flag
== TVE_EXPAND
||
1866 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1868 // A hidden root can be neither expanded nor collapsed.
1869 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1870 wxT("Can't expand/collapse hidden root node!") )
1872 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1873 // emulate them. This behaviour has changed slightly with comctl32.dll
1874 // v 4.70 - now it does send them but only the first time. To maintain
1875 // compatible behaviour and also in order to not have surprises with the
1876 // future versions, don't rely on this and still do everything ourselves.
1877 // To avoid that the messages be sent twice when the item is expanded for
1878 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1880 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1884 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1886 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1887 event
.m_item
= item
;
1888 event
.SetEventObject(this);
1890 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1892 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1896 (void)GetEventHandler()->ProcessEvent(event
);
1898 //else: change didn't took place, so do nothing at all
1901 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1903 DoExpand(item
, TVE_EXPAND
);
1906 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1908 DoExpand(item
, TVE_COLLAPSE
);
1911 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1913 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1916 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1918 DoExpand(item
, TVE_TOGGLE
);
1921 #if WXWIN_COMPATIBILITY_2_4
1923 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1925 DoExpand(item
, action
);
1930 void wxTreeCtrl::Unselect()
1932 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1933 wxT("doesn't make sense, may be you want UnselectAll()?") );
1935 // just remove the selection
1936 SelectItem(wxTreeItemId());
1939 void wxTreeCtrl::UnselectAll()
1941 if ( m_windowStyle
& wxTR_MULTIPLE
)
1943 wxArrayTreeItemIds selections
;
1944 size_t count
= GetSelections(selections
);
1945 for ( size_t n
= 0; n
< count
; n
++ )
1947 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1948 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1949 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1950 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1951 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1954 m_htSelStart
.Unset();
1958 // just remove the selection
1963 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1965 if ( m_windowStyle
& wxTR_MULTIPLE
)
1967 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1968 // selecting the item means checking it
1969 SetItemCheck(item
, select
);
1970 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1971 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1972 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1976 wxASSERT_MSG( select
,
1977 _T("SelectItem(false) works only for multiselect") );
1979 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1980 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1981 // send them ourselves
1983 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1984 event
.m_item
= item
;
1985 event
.SetEventObject(this);
1987 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1988 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1990 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1992 wxLogLastError(wxT("TreeView_SelectItem"));
1996 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1997 (void)GetEventHandler()->ProcessEvent(event
);
2000 //else: program vetoed the change
2004 void wxTreeCtrl::UnselectItem(const wxTreeItemId
& item
)
2006 SelectItem(item
, false);
2009 void wxTreeCtrl::ToggleItemSelection(const wxTreeItemId
& item
)
2011 SelectItem(item
, !IsSelected(item
));
2014 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
2017 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
2020 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
2022 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
2024 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
2028 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
2033 void wxTreeCtrl::DeleteTextCtrl()
2037 // the HWND corresponding to this control is deleted by the tree
2038 // control itself and we don't know when exactly this happens, so check
2039 // if the window still exists before calling UnsubclassWin()
2040 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
2042 m_textCtrl
->SetHWND(0);
2045 m_textCtrl
->UnsubclassWin();
2046 m_textCtrl
->SetHWND(0);
2054 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
2055 wxClassInfo
* textControlClass
)
2057 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
2062 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
2063 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2065 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2074 // textctrl is subclassed in MSWOnNotify
2078 // End label editing, optionally cancelling the edit
2079 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2081 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2086 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
2088 TV_HITTESTINFO hitTestInfo
;
2089 hitTestInfo
.pt
.x
= (int)point
.x
;
2090 hitTestInfo
.pt
.y
= (int)point
.y
;
2092 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2097 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2098 flags |= wxTREE_HITTEST_##flag
2100 TRANSLATE_FLAG(ABOVE
);
2101 TRANSLATE_FLAG(BELOW
);
2102 TRANSLATE_FLAG(NOWHERE
);
2103 TRANSLATE_FLAG(ONITEMBUTTON
);
2104 TRANSLATE_FLAG(ONITEMICON
);
2105 TRANSLATE_FLAG(ONITEMINDENT
);
2106 TRANSLATE_FLAG(ONITEMLABEL
);
2107 TRANSLATE_FLAG(ONITEMRIGHT
);
2108 TRANSLATE_FLAG(ONITEMSTATEICON
);
2109 TRANSLATE_FLAG(TOLEFT
);
2110 TRANSLATE_FLAG(TORIGHT
);
2112 #undef TRANSLATE_FLAG
2114 return wxTreeItemId(hitTestInfo
.hItem
);
2117 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2119 bool textOnly
) const
2123 // Virtual root items have no bounding rectangle
2124 if ( IS_VIRTUAL_ROOT(item
) )
2129 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2132 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2138 // couldn't retrieve rect: for example, item isn't visible
2143 // ----------------------------------------------------------------------------
2145 // ----------------------------------------------------------------------------
2147 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2148 // functions such as IsDataIndirect()
2149 class wxTreeSortHelper
2152 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2155 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
2157 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
2158 if ( tree
->IsDataIndirect(data
) )
2160 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2163 return data
->GetId();
2167 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2171 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2172 wxT("sorting tree without data doesn't make sense") );
2174 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2176 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2177 GetIdFromData(tree
, pItem2
));
2180 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2181 const wxTreeItemId
& item2
)
2183 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2186 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2188 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2190 // rely on the fact that TreeView_SortChildren does the same thing as our
2191 // default behaviour, i.e. sorts items alphabetically and so call it
2192 // directly if we're not in derived class (much more efficient!)
2193 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2195 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2200 tvSort
.hParent
= HITEM(item
);
2201 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2202 tvSort
.lParam
= (LPARAM
)this;
2203 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2207 // ----------------------------------------------------------------------------
2209 // ----------------------------------------------------------------------------
2211 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2213 if ( cmd
== EN_UPDATE
)
2215 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2216 event
.SetEventObject( this );
2217 ProcessCommand(event
);
2219 else if ( cmd
== EN_KILLFOCUS
)
2221 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2222 event
.SetEventObject( this );
2223 ProcessCommand(event
);
2231 // command processed
2235 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2236 // only do it during dragging, minimize wxWin overhead (this is important for
2237 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2238 // instead of passing by wxWin events
2239 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2241 bool processed
= false;
2243 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2245 // This message is sent after a right-click, or when the "menu" key is pressed
2246 if ( nMsg
== WM_CONTEXTMENU
)
2248 int x
= GET_X_LPARAM(lParam
),
2249 y
= GET_Y_LPARAM(lParam
);
2250 // Convert the screen point to a client point
2251 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2253 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2255 // can't use GetSelection() here as it would assert in multiselect mode
2256 event
.m_item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2257 event
.SetEventObject( this );
2259 // Get the bounding rectangle for the item, including the non-text areas
2261 GetBoundingRect(event
.m_item
, ItemRect
, false);
2262 // If the point is inside the bounding rectangle, use it as the click position.
2263 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2264 if (ItemRect
.Inside(MenuPoint
))
2266 event
.m_pointDrag
= MenuPoint
;
2268 // Use the Explorer standard of putting the menu at the left edge of the text,
2269 // in the vertical middle of the text. Should be the case for the "menu" key
2272 // Use the bounding rectangle of only the text part
2273 GetBoundingRect(event
.m_item
, ItemRect
, true);
2274 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2277 if ( GetEventHandler()->ProcessEvent(event
) )
2279 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2281 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2283 // we only process mouse messages here and these parameters have the
2284 // same meaning for all of them
2285 int x
= GET_X_LPARAM(lParam
),
2286 y
= GET_Y_LPARAM(lParam
);
2287 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2289 TV_HITTESTINFO tvht
;
2293 TreeView_HitTest(GetHwnd(), &tvht
);
2297 case WM_RBUTTONDOWN
:
2298 // if the item we are about to right click on is not already
2299 // selected or if we click outside of any item, remove the
2300 // entire previous selection
2301 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2306 // select item and set the focus to the
2307 // newly selected item
2308 ::SelectItem(GetHwnd(), htItem
);
2309 ::SetFocus(GetHwnd(), htItem
);
2312 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2313 case WM_LBUTTONDOWN
:
2314 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2316 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2317 m_ptClick
= wxPoint(x
, y
);
2319 if ( wParam
& MK_CONTROL
)
2323 // toggle selected state
2324 ::ToggleItemSelection(GetHwnd(), htItem
);
2326 ::SetFocus(GetHwnd(), htItem
);
2328 // reset on any click without Shift
2329 m_htSelStart
.Unset();
2333 else if ( wParam
& MK_SHIFT
)
2335 // this selects all items between the starting one and
2338 if ( !m_htSelStart
)
2340 // take the focused item
2341 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2345 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2346 !(wParam
& MK_CONTROL
));
2348 ::SelectItem(GetHwnd(), htItem
);
2350 ::SetFocus(GetHwnd(), htItem
);
2354 else // normal click
2356 // avoid doing anything if we click on the only
2357 // currently selected item
2361 wxArrayTreeItemIds selections
;
2362 size_t count
= GetSelections(selections
);
2365 HITEM_PTR(selections
[0]) != htItem
)
2367 // clear the previously selected items, if the
2368 // user clicked outside of the present selection.
2369 // otherwise, perform the deselection on mouse-up.
2370 // this allows multiple drag and drop to work.
2372 if (!IsItemSelected(GetHwnd(), htItem
))
2376 // prevent the click from starting in-place editing
2377 // which should only happen if we click on the
2378 // already selected item (and nothing else is
2381 TreeView_SelectItem(GetHwnd(), 0);
2382 ::SelectItem(GetHwnd(), htItem
);
2384 ::SetFocus(GetHwnd(), htItem
);
2388 // reset on any click without Shift
2389 m_htSelStart
.Unset();
2393 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2397 if ( m_htClickedItem
)
2399 int cx
= abs(m_ptClick
.x
- x
);
2400 int cy
= abs(m_ptClick
.y
- y
);
2402 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2404 HWND pWnd
= ::GetParent( GetHwnd() );
2409 tv
.hdr
.hwndFrom
= GetHwnd();
2410 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2411 tv
.hdr
.code
= TVN_BEGINDRAG
;
2413 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2416 ZeroMemory(&tviAux
, sizeof(tviAux
));
2417 tviAux
.hItem
= HITEM(m_htClickedItem
);
2418 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2419 tviAux
.stateMask
= 0xffffffff;
2420 TreeView_GetItem( GetHwnd(), &tviAux
);
2422 tv
.itemNew
.state
= tviAux
.state
;
2423 tv
.itemNew
.lParam
= tviAux
.lParam
;
2428 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2430 m_htClickedItem
.Unset();
2433 #endif // __WXWINCE__
2437 m_dragImage
->Move(wxPoint(x
, y
));
2440 // highlight the item as target (hiding drag image is
2441 // necessary - otherwise the display will be corrupted)
2442 m_dragImage
->Hide();
2443 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2444 m_dragImage
->Show();
2451 // facilitates multiple drag-and-drop
2452 if (htItem
&& isMultiple
)
2454 wxArrayTreeItemIds selections
;
2455 size_t count
= GetSelections(selections
);
2458 !(wParam
& MK_CONTROL
) &&
2459 !(wParam
& MK_SHIFT
))
2462 TreeView_SelectItem(GetHwnd(), htItem
);
2463 ::SelectItem(GetHwnd(), htItem
);
2464 ::SetFocus(GetHwnd(), htItem
);
2466 m_htClickedItem
.Unset();
2474 m_dragImage
->EndDrag();
2478 // generate the drag end event
2479 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2481 event
.m_item
= htItem
;
2482 event
.m_pointDrag
= wxPoint(x
, y
);
2483 event
.SetEventObject(this);
2485 (void)GetEventHandler()->ProcessEvent(event
);
2487 // if we don't do it, the tree seems to think that 2 items
2488 // are selected simultaneously which is quite weird
2489 TreeView_SelectDropTarget(GetHwnd(), 0);
2494 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2495 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2497 // the tree control greys out the selected item when it loses focus and
2498 // paints it as selected again when it regains it, but it won't do it
2499 // for the other items itself - help it
2500 wxArrayTreeItemIds selections
;
2501 size_t count
= GetSelections(selections
);
2503 for ( size_t n
= 0; n
< count
; n
++ )
2505 // TreeView_GetItemRect() will return false if item is not visible,
2506 // which may happen perfectly well
2507 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2510 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2514 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2516 bool bCtrl
= wxIsCtrlDown(),
2517 bShift
= wxIsShiftDown();
2519 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2525 ::ToggleItemSelection(GetHwnd(), htSel
);
2531 ::SelectItem(GetHwnd(), htSel
);
2539 if ( !bCtrl
&& !bShift
)
2541 // no modifiers, just clear selection and then let the default
2542 // processing to take place
2547 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2549 HTREEITEM htNext
= (HTREEITEM
)
2550 TreeView_GetNextItem
2554 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2560 // at the top/bottom
2566 if ( !m_htSelStart
)
2567 m_htSelStart
= htSel
;
2569 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2573 // without changing selection
2574 ::SetFocus(GetHwnd(), htNext
);
2585 // TODO: handle Shift/Ctrl with these keys
2586 if ( !bCtrl
&& !bShift
)
2590 m_htSelStart
.Unset();
2594 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2595 else if ( nMsg
== WM_COMMAND
)
2597 // if we receive a EN_KILLFOCUS command from the in-place edit control
2598 // used for label editing, make sure to end editing
2601 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2603 if ( cmd
== EN_KILLFOCUS
)
2605 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2615 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2621 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2623 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2624 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2625 // the parent window, not us, which completely breaks everything so simply
2626 // don't let it see this message at all
2627 if ( nMsg
== WM_RBUTTONDOWN
)
2630 // but because of the above we don't get NM_RCLICK which is normally
2631 // generated by tree window proc when the modal loop mentioned above ends
2632 // because the mouse is released -- synthesize it ourselves instead
2633 if ( nMsg
== WM_RBUTTONUP
)
2636 hdr
.hwndFrom
= GetHwnd();
2637 hdr
.idFrom
= GetId();
2638 hdr
.code
= NM_RCLICK
;
2641 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2643 // continue as usual
2646 if ( nMsg
== WM_CHAR
)
2648 // also don't let the control process Space and Return keys because it
2649 // doesn't do anything useful with them anyhow but always beeps
2650 // annoyingly when it receives them and there is no way to turn it off
2651 // simply if you just process TREEITEM_ACTIVATED event to which Space
2652 // and Enter presses are mapped in your code
2653 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2657 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2660 // process WM_NOTIFY Windows message
2661 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2663 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2664 wxEventType eventType
= wxEVT_NULL
;
2665 NMHDR
*hdr
= (NMHDR
*)lParam
;
2667 switch ( hdr
->code
)
2670 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2673 case TVN_BEGINRDRAG
:
2675 if ( eventType
== wxEVT_NULL
)
2676 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2677 //else: left drag, already set above
2679 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2681 event
.m_item
= tv
->itemNew
.hItem
;
2682 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2684 // don't allow dragging by default: the user code must
2685 // explicitly say that it wants to allow it to avoid breaking
2691 case TVN_BEGINLABELEDIT
:
2693 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2694 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2696 // although the user event handler may still veto it, it is
2697 // important to set it now so that calls to SetItemText() from
2698 // the event handler would change the text controls contents
2700 event
.m_item
= info
->item
.hItem
;
2701 event
.m_label
= info
->item
.pszText
;
2702 event
.m_editCancelled
= false;
2706 case TVN_DELETEITEM
:
2708 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2709 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2711 event
.m_item
= tv
->itemOld
.hItem
;
2715 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2716 if ( it
!= m_attrs
.end() )
2725 case TVN_ENDLABELEDIT
:
2727 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2728 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2730 event
.m_item
= info
->item
.hItem
;
2731 event
.m_label
= info
->item
.pszText
;
2732 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2737 // These *must* not be removed or TVN_GETINFOTIP will
2738 // not be processed each time the mouse is moved
2739 // and the tooltip will only ever update once.
2748 #ifdef TVN_GETINFOTIP
2749 case TVN_GETINFOTIP
:
2751 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2752 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2754 // Which item are we trying to get a tooltip for?
2755 event
.m_item
= info
->hItem
;
2762 case TVN_GETDISPINFO
:
2763 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2766 case TVN_SETDISPINFO
:
2768 if ( eventType
== wxEVT_NULL
)
2769 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2770 //else: get, already set above
2772 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2774 event
.m_item
= info
->item
.hItem
;
2778 case TVN_ITEMEXPANDING
:
2779 case TVN_ITEMEXPANDED
:
2781 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2784 switch ( tv
->action
)
2787 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2795 what
= IDX_COLLAPSE
;
2799 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2802 eventType
= gs_expandEvents
[what
][how
];
2804 event
.m_item
= tv
->itemNew
.hItem
;
2810 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2811 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2813 // fabricate the lParam and wParam parameters sufficiently
2814 // similar to the ones from a "real" WM_KEYDOWN so that
2815 // CreateKeyEvent() works correctly
2816 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2817 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2819 WXWPARAM wParam
= info
->wVKey
;
2821 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2824 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2829 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2834 // a separate event for Space/Return
2835 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2836 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2838 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2840 event2
.SetEventObject(this);
2841 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2843 event2
.m_item
= GetSelection();
2845 //else: don't know how to get it
2847 (void)GetEventHandler()->ProcessEvent(event2
);
2852 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2853 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2854 // we have to handle both messages:
2855 case TVN_SELCHANGEDA
:
2856 case TVN_SELCHANGEDW
:
2857 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2860 case TVN_SELCHANGINGA
:
2861 case TVN_SELCHANGINGW
:
2863 if ( eventType
== wxEVT_NULL
)
2864 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2865 //else: already set above
2867 if (hdr
->code
== TVN_SELCHANGINGW
||
2868 hdr
->code
== TVN_SELCHANGEDW
)
2870 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2871 event
.m_item
= tv
->itemNew
.hItem
;
2872 event
.m_itemOld
= tv
->itemOld
.hItem
;
2876 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2877 event
.m_item
= tv
->itemNew
.hItem
;
2878 event
.m_itemOld
= tv
->itemOld
.hItem
;
2883 // instead of explicitly checking for _WIN32_IE, check if the
2884 // required symbols are available in the headers
2885 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2888 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2889 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2890 switch ( nmcd
.dwDrawStage
)
2893 // if we've got any items with non standard attributes,
2894 // notify us before painting each item
2895 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2899 case CDDS_ITEMPREPAINT
:
2901 wxMapTreeAttr::iterator
2902 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2904 if ( it
== m_attrs
.end() )
2906 // nothing to do for this item
2907 *result
= CDRF_DODEFAULT
;
2911 wxTreeItemAttr
* const attr
= it
->second
;
2913 // selection colours should override ours,
2914 // otherwise it is too confusing ot the user
2915 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) )
2918 if ( attr
->HasBackgroundColour() )
2920 colBack
= attr
->GetBackgroundColour();
2921 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2925 // but we still want to keep the special foreground
2926 // colour when we don't have focus (we can't keep
2927 // it when we do, it would usually be unreadable on
2928 // the almost inverted bg colour...)
2929 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2930 FindFocus() != this )
2933 if ( attr
->HasTextColour() )
2935 colText
= attr
->GetTextColour();
2936 lptvcd
->clrText
= wxColourToRGB(colText
);
2940 if ( attr
->HasFont() )
2942 HFONT hFont
= GetHfontOf(attr
->GetFont());
2944 ::SelectObject(nmcd
.hdc
, hFont
);
2946 *result
= CDRF_NEWFONT
;
2948 else // no specific font
2950 *result
= CDRF_DODEFAULT
;
2956 *result
= CDRF_DODEFAULT
;
2960 // we always process it
2962 #endif // have owner drawn support in headers
2966 DWORD pos
= GetMessagePos();
2968 point
.x
= LOWORD(pos
);
2969 point
.y
= HIWORD(pos
);
2970 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2972 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2973 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2975 event
.m_item
= item
;
2976 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2984 TV_HITTESTINFO tvhti
;
2985 ::GetCursorPos(&tvhti
.pt
);
2986 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2987 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2989 if ( tvhti
.flags
& TVHT_ONITEM
)
2991 event
.m_item
= tvhti
.hItem
;
2992 eventType
= (int)hdr
->code
== NM_DBLCLK
2993 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2994 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2996 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2997 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
3006 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
3009 event
.SetEventObject(this);
3010 event
.SetEventType(eventType
);
3012 bool processed
= GetEventHandler()->ProcessEvent(event
);
3015 switch ( hdr
->code
)
3018 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
3019 // the return code of this event handler as the return value for
3020 // NM_DBLCLK - otherwise, double clicking the item to toggle its
3021 // expanded status would never work
3026 case TVN_BEGINRDRAG
:
3027 if ( event
.IsAllowed() )
3029 // normally this is impossible because the m_dragImage is
3030 // deleted once the drag operation is over
3031 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
3033 m_dragImage
= new wxDragImage(*this, event
.m_item
);
3034 m_dragImage
->BeginDrag(wxPoint(0,0), this);
3035 m_dragImage
->Show();
3039 case TVN_DELETEITEM
:
3041 // NB: we might process this message using wxWidgets event
3042 // tables, but due to overhead of wxWin event system we
3043 // prefer to do it here ourself (otherwise deleting a tree
3044 // with many items is just too slow)
3045 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
3047 wxTreeItemId item
= event
.m_item
;
3048 if ( HasIndirectData(item
) )
3050 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
3052 delete data
; // can't be NULL here
3056 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
3057 delete data
; // may be NULL, ok
3060 processed
= true; // Make sure we don't get called twice
3064 case TVN_BEGINLABELEDIT
:
3065 // return true to cancel label editing
3066 *result
= !event
.IsAllowed();
3068 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
3069 if ( event
.IsAllowed() )
3071 HWND hText
= TreeView_GetEditControl(GetHwnd());
3074 // MBN: if m_textCtrl already has an HWND, it is a stale
3075 // pointer from a previous edit (because the user
3076 // didn't modify the label before dismissing the control,
3077 // and TVN_ENDLABELEDIT was not sent), so delete it
3078 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
3081 m_textCtrl
= new wxTextCtrl();
3082 m_textCtrl
->SetParent(this);
3083 m_textCtrl
->SetHWND((WXHWND
)hText
);
3084 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3086 // set wxTE_PROCESS_ENTER style for the text control to
3087 // force it to process the Enter presses itself, otherwise
3088 // they could be stolen from it by the dialog
3090 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3091 | wxTE_PROCESS_ENTER
);
3094 else // we had set m_idEdited before
3100 case TVN_ENDLABELEDIT
:
3101 // return true to set the label to the new string: note that we
3102 // also must pretend that we did process the message or it is going
3103 // to be passed to DefWindowProc() which will happily return false
3104 // cancelling the label change
3105 *result
= event
.IsAllowed();
3108 // ensure that we don't have the text ctrl which is going to be
3114 #ifdef TVN_GETINFOTIP
3115 case TVN_GETINFOTIP
:
3117 // If the user permitted a tooltip change, change it
3118 if (event
.IsAllowed())
3120 SetToolTip(event
.m_label
);
3127 case TVN_SELCHANGING
:
3128 case TVN_ITEMEXPANDING
:
3129 // return true to prevent the action from happening
3130 *result
= !event
.IsAllowed();
3133 case TVN_ITEMEXPANDED
:
3134 // the item is not refreshed properly after expansion when it has
3135 // an image depending on the expanded/collapsed state - bug in
3136 // comctl32.dll or our code?
3138 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
3139 wxTreeItemId
id(tv
->itemNew
.hItem
);
3141 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3149 case TVN_GETDISPINFO
:
3150 // NB: so far the user can't set the image himself anyhow, so do it
3151 // anyway - but this may change later
3152 //if ( /* !processed && */ 1 )
3154 wxTreeItemId item
= event
.m_item
;
3155 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3156 if ( info
->item
.mask
& TVIF_IMAGE
)
3159 DoGetItemImageFromData
3162 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3163 : wxTreeItemIcon_Normal
3166 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3168 info
->item
.iSelectedImage
=
3169 DoGetItemImageFromData
3172 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3173 : wxTreeItemIcon_Selected
3180 // for the other messages the return value is ignored and there is
3181 // nothing special to do
3186 // ----------------------------------------------------------------------------
3188 // ----------------------------------------------------------------------------
3190 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3191 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3193 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
3196 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3197 tvi
.mask
= TVIF_STATE
;
3198 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3200 // Select the specified state, or -1 == cycle to the next one.
3203 TreeView_GetItem(GetHwnd(), &tvi
);
3205 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
3206 if ( state
== m_imageListState
->GetImageCount() )
3210 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3211 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3213 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3215 TreeView_SetItem(GetHwnd(), &tvi
);
3218 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3221 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3222 tvi
.mask
= TVIF_STATE
;
3223 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3224 TreeView_GetItem(GetHwnd(), &tvi
);
3226 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3229 #if WXWIN_COMPATIBILITY_2_2
3231 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
3233 return GetItemParent( item
);
3236 #endif // WXWIN_COMPATIBILITY_2_2
3238 #endif // wxUSE_TREECTRL