1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
37 #include "wx/settings.h"
40 #include "wx/msw/private.h"
42 // Set this to 1 to be _absolutely_ sure that repainting will work for all
43 // comctl32.dll versions
44 #define wxUSE_COMCTL32_SAFELY 0
46 #include "wx/imaglist.h"
47 #include "wx/msw/dragimag.h"
49 // macros to hide the cast ugliness
50 // --------------------------------
52 // get HTREEITEM from wxTreeItemId
53 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // wrapper for TreeView_HitTest
60 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
66 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
69 // wrappers for TreeView_GetItem/TreeView_SetItem
70 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
74 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
75 tvi
.stateMask
= TVIS_SELECTED
;
78 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
80 wxLogLastError(wxT("TreeView_GetItem"));
83 return (tvi
.state
& TVIS_SELECTED
) != 0;
86 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
89 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
90 tvi
.stateMask
= TVIS_SELECTED
;
91 tvi
.state
= select
? TVIS_SELECTED
: 0;
94 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
96 wxLogLastError(wxT("TreeView_SetItem"));
103 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
105 SelectItem(hwndTV
, htItem
, false);
108 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
110 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
113 // helper function which selects all items in a range and, optionally,
114 // unselects all others
115 static void SelectRange(HWND hwndTV
,
118 bool unselectOthers
= true)
120 // find the first (or last) item and select it
122 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
123 while ( htItem
&& cont
)
125 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
127 if ( !IsItemSelected(hwndTV
, htItem
) )
129 SelectItem(hwndTV
, htItem
);
136 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
138 UnselectItem(hwndTV
, htItem
);
142 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
145 // select the items in range
146 cont
= htFirst
!= htLast
;
147 while ( htItem
&& cont
)
149 if ( !IsItemSelected(hwndTV
, htItem
) )
151 SelectItem(hwndTV
, htItem
);
154 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
156 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
160 if ( unselectOthers
)
164 if ( IsItemSelected(hwndTV
, htItem
) )
166 UnselectItem(hwndTV
, htItem
);
169 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
173 // seems to be necessary - otherwise the just selected items don't always
174 // appear as selected
175 UpdateWindow(hwndTV
);
178 // helper function which tricks the standard control into changing the focused
179 // item without changing anything else (if someone knows why Microsoft doesn't
180 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
181 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
184 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
189 if ( htItem
!= htFocus
)
191 // remember the selection state of the item
192 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
194 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
196 // prevent the tree from unselecting the old focus which it
197 // would do by default (TreeView_SelectItem unselects the
199 TreeView_SelectItem(hwndTV
, 0);
200 SelectItem(hwndTV
, htFocus
);
203 TreeView_SelectItem(hwndTV
, htItem
);
207 // need to clear the selection which TreeView_SelectItem() gave
209 UnselectItem(hwndTV
, htItem
);
211 //else: was selected, still selected - ok
213 //else: nothing to do, focus already there
219 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
221 // just clear the focus
222 TreeView_SelectItem(hwndTV
, 0);
224 if ( wasFocusSelected
)
226 // restore the selection state
227 SelectItem(hwndTV
, htFocus
);
230 //else: nothing to do, no focus already
234 // ----------------------------------------------------------------------------
236 // ----------------------------------------------------------------------------
238 // a convenient wrapper around TV_ITEM struct which adds a ctor
240 #pragma warning( disable : 4097 ) // inheriting from typedef
243 struct wxTreeViewItem
: public TV_ITEM
245 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
246 UINT mask_
, // fields which are valid
247 UINT stateMask_
= 0) // for TVIF_STATE only
251 // hItem member is always valid
252 mask
= mask_
| TVIF_HANDLE
;
253 stateMask
= stateMask_
;
258 // ----------------------------------------------------------------------------
259 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
261 // We need this for a couple of reasons:
263 // 1) This class is needed for support of different images: the Win32 common
264 // control natively supports only 2 images (the normal one and another for the
265 // selected state). We wish to provide support for 2 more of them for folder
266 // items (i.e. those which have children): for expanded state and for expanded
267 // selected state. For this we use this structure to store the additional items
270 // 2) This class is also needed to hold the HITEM so that we can sort
271 // it correctly in the MSW sort callback.
273 // In addition it makes other workarounds such as this easier and helps
274 // simplify the code.
275 // ----------------------------------------------------------------------------
277 class wxTreeItemParam
284 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
290 // dtor deletes the associated data as well
291 virtual ~wxTreeItemParam() { delete m_data
; }
294 // get the real data associated with the item
295 wxTreeItemData
*GetData() const { return m_data
; }
297 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
299 // do we have such image?
300 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
301 // get image, falling back to the other images if this one is not
303 int GetImage(wxTreeItemIcon which
) const
305 int image
= m_images
[which
];
310 case wxTreeItemIcon_SelectedExpanded
:
311 image
= GetImage(wxTreeItemIcon_Expanded
);
316 case wxTreeItemIcon_Selected
:
317 case wxTreeItemIcon_Expanded
:
318 image
= GetImage(wxTreeItemIcon_Normal
);
321 case wxTreeItemIcon_Normal
:
326 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
332 // change the given image
333 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
336 const wxTreeItemId
& GetItem() const { return m_item
; }
338 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
341 // all the images associated with the item
342 int m_images
[wxTreeItemIcon_Max
];
344 // item for sort callbacks
347 // the real client data
348 wxTreeItemData
*m_data
;
350 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
353 // wxVirutalNode is used in place of a single root when 'hidden' root is
355 class wxVirtualNode
: public wxTreeViewItem
358 wxVirtualNode(wxTreeItemParam
*param
)
359 : wxTreeViewItem(TVI_ROOT
, 0)
369 wxTreeItemParam
*GetParam() const { return m_param
; }
370 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
373 wxTreeItemParam
*m_param
;
375 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
379 #pragma warning( default : 4097 )
382 // a macro to get the virtual root, returns NULL if none
383 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
385 // returns true if the item is the virtual root
386 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
388 // a class which encapsulates the tree traversal logic: it vists all (unless
389 // OnVisit() returns false) items under the given one
390 class wxTreeTraversal
393 wxTreeTraversal(const wxTreeCtrl
*tree
)
398 // give it a virtual dtor: not really needed as the class is never used
399 // polymorphically and not even allocated on heap at all, but this is safer
400 // (in case it ever is) and silences the compiler warnings for now
401 virtual ~wxTreeTraversal() { }
403 // do traverse the tree: visit all items (recursively by default) under the
404 // given one; return true if all items were traversed or false if the
405 // traversal was aborted because OnVisit returned false
406 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
408 // override this function to do whatever is needed for each item, return
409 // false to stop traversing
410 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
413 const wxTreeCtrl
*GetTree() const { return m_tree
; }
416 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
418 const wxTreeCtrl
*m_tree
;
420 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
423 // internal class for getting the selected items
424 class TraverseSelections
: public wxTreeTraversal
427 TraverseSelections(const wxTreeCtrl
*tree
,
428 wxArrayTreeItemIds
& selections
)
429 : wxTreeTraversal(tree
), m_selections(selections
)
431 m_selections
.Empty();
433 if (tree
->GetCount() > 0)
434 DoTraverse(tree
->GetRootItem());
437 virtual bool OnVisit(const wxTreeItemId
& item
)
439 const wxTreeCtrl
* const tree
= GetTree();
441 // can't visit a virtual node.
442 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
447 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
449 m_selections
.Add(item
);
455 size_t GetCount() const { return m_selections
.GetCount(); }
458 wxArrayTreeItemIds
& m_selections
;
460 DECLARE_NO_COPY_CLASS(TraverseSelections
)
463 // internal class for counting tree items
464 class TraverseCounter
: public wxTreeTraversal
467 TraverseCounter(const wxTreeCtrl
*tree
,
468 const wxTreeItemId
& root
,
470 : wxTreeTraversal(tree
)
474 DoTraverse(root
, recursively
);
477 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
484 size_t GetCount() const { return m_count
; }
489 DECLARE_NO_COPY_CLASS(TraverseCounter
)
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 #if wxUSE_EXTENDED_RTTI
497 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
499 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
500 // new style border flags, we put them first to
501 // use them for streaming out
502 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
503 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
504 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
505 wxFLAGS_MEMBER(wxBORDER_RAISED
)
506 wxFLAGS_MEMBER(wxBORDER_STATIC
)
507 wxFLAGS_MEMBER(wxBORDER_NONE
)
509 // old style border flags
510 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
511 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
512 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
513 wxFLAGS_MEMBER(wxRAISED_BORDER
)
514 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
515 wxFLAGS_MEMBER(wxBORDER
)
517 // standard window styles
518 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
519 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
520 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
521 wxFLAGS_MEMBER(wxWANTS_CHARS
)
522 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
523 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
524 wxFLAGS_MEMBER(wxVSCROLL
)
525 wxFLAGS_MEMBER(wxHSCROLL
)
527 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
528 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
529 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
530 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
531 wxFLAGS_MEMBER(wxTR_NO_LINES
)
532 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
533 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
534 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
535 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
536 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
537 wxFLAGS_MEMBER(wxTR_SINGLE
)
538 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
539 wxFLAGS_MEMBER(wxTR_EXTENDED
)
540 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
542 wxEND_FLAGS( wxTreeCtrlStyle
)
544 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
546 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
547 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
548 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
549 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
550 wxEND_PROPERTIES_TABLE()
552 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
553 wxEND_HANDLERS_TABLE()
555 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
557 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
560 // ----------------------------------------------------------------------------
562 // ----------------------------------------------------------------------------
564 // indices in gs_expandEvents table below
579 // handy table for sending events - it has to be initialized during run-time
580 // now so can't be const any more
581 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
584 but logically it's a const table with the following entries:
587 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
588 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
592 // ============================================================================
594 // ============================================================================
596 // ----------------------------------------------------------------------------
598 // ----------------------------------------------------------------------------
600 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
602 if ( !OnVisit(root
) )
605 return Traverse(root
, recursively
);
608 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
610 wxTreeItemIdValue cookie
;
611 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
612 while ( child
.IsOk() )
614 // depth first traversal
615 if ( recursively
&& !Traverse(child
, true) )
618 if ( !OnVisit(child
) )
621 child
= m_tree
->GetNextChild(root
, cookie
);
627 // ----------------------------------------------------------------------------
628 // construction and destruction
629 // ----------------------------------------------------------------------------
631 void wxTreeCtrl::Init()
634 m_hasAnyAttr
= false;
636 m_pVirtualRoot
= NULL
;
638 // initialize the global array of events now as it can't be done statically
639 // with the wxEVT_XXX values being allocated during run-time only
640 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
641 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
642 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
643 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
646 bool wxTreeCtrl::Create(wxWindow
*parent
,
651 const wxValidator
& validator
,
652 const wxString
& name
)
656 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
657 style
|= wxBORDER_SUNKEN
;
659 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
663 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
664 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
666 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
667 wstyle
|= TVS_HASLINES
;
668 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
669 wstyle
|= TVS_HASBUTTONS
;
671 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
672 wstyle
|= TVS_EDITLABELS
;
674 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
675 wstyle
|= TVS_LINESATROOT
;
677 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
679 if ( wxApp::GetComCtl32Version() >= 471 )
680 wstyle
|= TVS_FULLROWSELECT
;
683 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
684 // Need so that TVN_GETINFOTIP messages will be sent
685 wstyle
|= TVS_INFOTIP
;
688 // Create the tree control.
689 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
692 #if wxUSE_COMCTL32_SAFELY
693 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
694 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
696 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
697 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
699 // This works around a bug in the Windows tree control whereby for some versions
700 // of comctrl32, setting any colour actually draws the background in black.
701 // This will initialise the background to the system colour.
702 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
703 // Assume the user has an updated comctl32.dll.
704 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
705 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
706 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
710 // VZ: this is some experimental code which may be used to get the
711 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
712 // AFAIK, the standard DLL does about the same thing anyhow.
714 if ( m_windowStyle
& wxTR_MULTIPLE
)
718 // create the DC compatible with the current screen
719 HDC hdcMem
= CreateCompatibleDC(NULL
);
721 // create a mono bitmap of the standard size
722 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
723 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
724 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
725 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
726 1, // # of color planes
727 1, // # bits needed for one pixel
728 0); // array containing colour data
729 SelectObject(hdcMem
, hbmpCheck
);
731 // then draw a check mark into it
732 RECT rect
= { 0, 0, x
, y
};
733 if ( !::DrawFrameControl(hdcMem
, &rect
,
735 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
737 wxLogLastError(wxT("DrawFrameControl(check)"));
740 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
741 imagelistCheckboxes
.Add(bmp
);
743 if ( !::DrawFrameControl(hdcMem
, &rect
,
747 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
750 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
751 imagelistCheckboxes
.Add(bmp
);
757 SetStateImageList(&imagelistCheckboxes
);
761 wxSetCCUnicodeFormat(GetHwnd());
766 wxTreeCtrl::~wxTreeCtrl()
768 // delete any attributes
771 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
773 // prevent TVN_DELETEITEM handler from deleting the attributes again!
774 m_hasAnyAttr
= false;
779 // delete user data to prevent memory leaks
780 // also deletes hidden root node storage.
784 // ----------------------------------------------------------------------------
786 // ----------------------------------------------------------------------------
788 /* static */ wxVisualAttributes
789 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
791 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
793 // common controls have their own default font
794 attrs
.font
= wxGetCCDefaultFont();
800 // simple wrappers which add error checking in debug mode
802 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
804 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
805 _T("can't retrieve virtual root item") );
807 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
809 wxLogLastError(wxT("TreeView_GetItem"));
817 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
819 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
821 wxLogLastError(wxT("TreeView_SetItem"));
825 unsigned int wxTreeCtrl::GetCount() const
827 return (unsigned int)TreeView_GetCount(GetHwnd());
830 unsigned int wxTreeCtrl::GetIndent() const
832 return TreeView_GetIndent(GetHwnd());
835 void wxTreeCtrl::SetIndent(unsigned int indent
)
837 TreeView_SetIndent(GetHwnd(), indent
);
840 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
843 (void) TreeView_SetImageList(GetHwnd(),
844 imageList
? imageList
->GetHIMAGELIST() : 0,
848 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
850 if (m_ownsImageListNormal
)
851 delete m_imageListNormal
;
853 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
854 m_ownsImageListNormal
= false;
857 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
859 if (m_ownsImageListState
) delete m_imageListState
;
860 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
861 m_ownsImageListState
= false;
864 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
865 bool recursively
) const
867 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
869 TraverseCounter
counter(this, item
, recursively
);
870 return counter
.GetCount() - 1;
873 // ----------------------------------------------------------------------------
875 // ----------------------------------------------------------------------------
877 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
879 #if !wxUSE_COMCTL32_SAFELY
880 if ( !wxWindowBase::SetBackgroundColour(colour
) )
883 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
889 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
891 #if !wxUSE_COMCTL32_SAFELY
892 if ( !wxWindowBase::SetForegroundColour(colour
) )
895 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
901 // ----------------------------------------------------------------------------
903 // ----------------------------------------------------------------------------
905 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
907 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
910 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
912 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
914 wxChar buf
[512]; // the size is arbitrary...
916 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
917 tvItem
.pszText
= buf
;
918 tvItem
.cchTextMax
= WXSIZEOF(buf
);
919 if ( !DoGetItem(&tvItem
) )
921 // don't return some garbage which was on stack, but an empty string
925 return wxString(buf
);
928 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
930 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
932 if ( IS_VIRTUAL_ROOT(item
) )
935 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
936 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
939 // when setting the text of the item being edited, the text control should
940 // be updated to reflect the new text as well, otherwise calling
941 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
943 // don't use GetEditControl() here because m_textCtrl is not set yet
944 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
947 if ( item
== m_idEdited
)
949 ::SetWindowText(hwndEdit
, text
);
954 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
955 wxTreeItemIcon which
) const
957 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
959 if ( IsHiddenRoot(item
) )
961 // no images for hidden root item
965 wxTreeItemParam
*param
= GetItemParam(item
);
967 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
970 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
971 wxTreeItemIcon which
)
973 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
974 wxCHECK_RET( which
>= 0 &&
975 which
< wxTreeItemIcon_Max
,
976 wxT("invalid image index"));
979 if ( IsHiddenRoot(item
) )
981 // no images for hidden root item
985 wxTreeItemParam
*data
= GetItemParam(item
);
989 data
->SetImage(image
, which
);
994 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
996 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
998 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1000 // hidden root may still have data.
1001 if ( IS_VIRTUAL_ROOT(item
) )
1003 return GET_VIRTUAL_ROOT()->GetParam();
1007 if ( !DoGetItem(&tvItem
) )
1012 return (wxTreeItemParam
*)tvItem
.lParam
;
1015 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1017 wxTreeItemParam
*data
= GetItemParam(item
);
1019 return data
? data
->GetData() : NULL
;
1022 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1024 // first, associate this piece of data with this item
1030 wxTreeItemParam
*param
= GetItemParam(item
);
1032 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1034 param
->SetData(data
);
1037 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1039 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1041 if ( IS_VIRTUAL_ROOT(item
) )
1044 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1045 tvItem
.cChildren
= (int)has
;
1049 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1051 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1053 if ( IS_VIRTUAL_ROOT(item
) )
1056 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1057 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1061 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1063 if ( IS_VIRTUAL_ROOT(item
) )
1066 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1067 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1071 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1073 if ( IS_VIRTUAL_ROOT(item
) )
1077 if ( GetBoundingRect(item
, rect
) )
1083 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1085 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1087 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1088 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1091 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1093 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1095 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1096 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1099 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1101 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1103 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1104 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1107 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1108 const wxColour
& col
)
1110 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1112 wxTreeItemAttr
*attr
;
1113 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1114 if ( it
== m_attrs
.end() )
1116 m_hasAnyAttr
= true;
1118 m_attrs
[item
.m_pItem
] =
1119 attr
= new wxTreeItemAttr
;
1126 attr
->SetTextColour(col
);
1131 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1132 const wxColour
& col
)
1134 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1136 wxTreeItemAttr
*attr
;
1137 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1138 if ( it
== m_attrs
.end() )
1140 m_hasAnyAttr
= true;
1142 m_attrs
[item
.m_pItem
] =
1143 attr
= new wxTreeItemAttr
;
1145 else // already in the hash
1150 attr
->SetBackgroundColour(col
);
1155 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1157 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1159 wxTreeItemAttr
*attr
;
1160 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1161 if ( it
== m_attrs
.end() )
1163 m_hasAnyAttr
= true;
1165 m_attrs
[item
.m_pItem
] =
1166 attr
= new wxTreeItemAttr
;
1168 else // already in the hash
1173 attr
->SetFont(font
);
1175 // Reset the item's text to ensure that the bounding rect will be adjusted
1176 // for the new font.
1177 SetItemText(item
, GetItemText(item
));
1182 // ----------------------------------------------------------------------------
1184 // ----------------------------------------------------------------------------
1186 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1188 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1190 if ( item
== wxTreeItemId(TVI_ROOT
) )
1192 // virtual (hidden) root is never visible
1196 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1199 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1200 // the HTREEITEM with TVM_GETITEMRECT
1201 *(HTREEITEM
*)&rect
= HITEM(item
);
1203 // true means to get rect for just the text, not the whole line
1204 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1206 // if TVM_GETITEMRECT returned false, then the item is definitely not
1207 // visible (because its parent is not expanded)
1211 // however if it returned true, the item might still be outside the
1212 // currently visible part of the tree, test for it (notice that partly
1213 // visible means visible here)
1214 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1217 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1219 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1221 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1224 return tvItem
.cChildren
!= 0;
1227 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1229 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1231 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1234 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1237 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1239 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1241 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1244 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1247 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1249 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1251 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1254 return (tvItem
.state
& TVIS_BOLD
) != 0;
1257 // ----------------------------------------------------------------------------
1259 // ----------------------------------------------------------------------------
1261 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1263 // Root may be real (visible) or virtual (hidden).
1264 if ( GET_VIRTUAL_ROOT() )
1267 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1270 wxTreeItemId
wxTreeCtrl::GetSelection() const
1272 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1273 wxT("this only works with single selection controls") );
1275 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1278 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1280 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1284 if ( IS_VIRTUAL_ROOT(item
) )
1286 // no parent for the virtual root
1291 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1292 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1294 // the top level items should have the virtual root as their parent
1299 return wxTreeItemId(hItem
);
1302 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1303 wxTreeItemIdValue
& cookie
) const
1305 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1307 // remember the last child returned in 'cookie'
1308 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1310 return wxTreeItemId(cookie
);
1313 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1314 wxTreeItemIdValue
& cookie
) const
1316 wxTreeItemId
fromCookie(cookie
);
1318 HTREEITEM hitem
= HITEM(fromCookie
);
1320 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1322 wxTreeItemId
item(hitem
);
1324 cookie
= item
.m_pItem
;
1329 #if WXWIN_COMPATIBILITY_2_4
1331 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1334 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1336 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1338 return wxTreeItemId((void *)cookie
);
1341 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1344 wxTreeItemId
fromCookie((void *)cookie
);
1346 HTREEITEM hitem
= HITEM(fromCookie
);
1348 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1350 wxTreeItemId
item(hitem
);
1352 cookie
= (long)item
.m_pItem
;
1357 #endif // WXWIN_COMPATIBILITY_2_4
1359 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1361 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1363 // can this be done more efficiently?
1364 wxTreeItemIdValue cookie
;
1366 wxTreeItemId childLast
,
1367 child
= GetFirstChild(item
, cookie
);
1368 while ( child
.IsOk() )
1371 child
= GetNextChild(item
, cookie
);
1377 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1379 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1380 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1383 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1385 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1386 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1389 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1391 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1394 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1396 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1397 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1399 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1402 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1404 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1405 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1407 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1410 // ----------------------------------------------------------------------------
1411 // multiple selections emulation
1412 // ----------------------------------------------------------------------------
1414 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1416 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1418 // receive the desired information.
1419 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1422 // state image indices are 1 based
1423 return ((tvItem
.state
>> 12) - 1) == 1;
1426 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1428 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1430 // receive the desired information.
1431 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1435 // state images are one-based
1436 tvItem
.state
= (check
? 2 : 1) << 12;
1441 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1443 TraverseSelections
selector(this, selections
);
1445 return selector
.GetCount();
1448 // ----------------------------------------------------------------------------
1450 // ----------------------------------------------------------------------------
1452 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1453 const wxTreeItemId
& hInsertAfter
,
1454 const wxString
& text
,
1455 int image
, int selectedImage
,
1456 wxTreeItemData
*data
)
1458 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1460 _T("can't have more than one root in the tree") );
1462 TV_INSERTSTRUCT tvIns
;
1463 tvIns
.hParent
= HITEM(parent
);
1464 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1466 // this is how we insert the item as the first child: supply a NULL
1468 if ( !tvIns
.hInsertAfter
)
1470 tvIns
.hInsertAfter
= TVI_FIRST
;
1474 if ( !text
.empty() )
1477 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1481 tvIns
.item
.pszText
= NULL
;
1482 tvIns
.item
.cchTextMax
= 0;
1485 // create the param which will store the other item parameters
1486 wxTreeItemParam
*param
= new wxTreeItemParam
;
1488 // we return the images on demand as they depend on whether the item is
1489 // expanded or collapsed too in our case
1490 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1491 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1492 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1494 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1495 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1498 tvIns
.item
.lParam
= (LPARAM
)param
;
1499 tvIns
.item
.mask
= mask
;
1501 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1504 wxLogLastError(wxT("TreeView_InsertItem"));
1507 // associate the application tree item with Win32 tree item handle
1510 // setup wxTreeItemData
1513 param
->SetData(data
);
1517 return wxTreeItemId(id
);
1520 // for compatibility only
1521 #if WXWIN_COMPATIBILITY_2_4
1523 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1525 SetImageList(imageList
);
1528 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1530 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1533 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1535 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1538 #endif // WXWIN_COMPATIBILITY_2_4
1540 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1541 int image
, int selectedImage
,
1542 wxTreeItemData
*data
)
1545 if ( HasFlag(wxTR_HIDE_ROOT
) )
1547 // create a virtual root item, the parent for all the others
1548 wxTreeItemParam
*param
= new wxTreeItemParam
;
1549 param
->SetData(data
);
1551 m_pVirtualRoot
= new wxVirtualNode(param
);
1556 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1557 text
, image
, selectedImage
, data
);
1560 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1562 const wxString
& text
,
1563 int image
, int selectedImage
,
1564 wxTreeItemData
*data
)
1566 wxTreeItemId idPrev
;
1567 if ( index
== (size_t)-1 )
1569 // special value: append to the end
1572 else // find the item from index
1574 wxTreeItemIdValue cookie
;
1575 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1576 while ( index
!= 0 && idCur
.IsOk() )
1581 idCur
= GetNextChild(parent
, cookie
);
1584 // assert, not check: if the index is invalid, we will append the item
1586 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1589 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1592 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1594 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1596 wxLogLastError(wxT("TreeView_DeleteItem"));
1600 // delete all children (but don't delete the item itself)
1601 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1603 wxTreeItemIdValue cookie
;
1605 wxArrayTreeItemIds children
;
1606 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1607 while ( child
.IsOk() )
1609 children
.Add(child
);
1611 child
= GetNextChild(item
, cookie
);
1614 size_t nCount
= children
.Count();
1615 for ( size_t n
= 0; n
< nCount
; n
++ )
1617 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1619 wxLogLastError(wxT("TreeView_DeleteItem"));
1624 void wxTreeCtrl::DeleteAllItems()
1626 // delete the "virtual" root item.
1627 if ( GET_VIRTUAL_ROOT() )
1629 delete GET_VIRTUAL_ROOT();
1630 m_pVirtualRoot
= NULL
;
1633 // and all the real items
1635 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1637 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1641 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1643 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1644 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1645 flag
== TVE_EXPAND
||
1647 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1649 // A hidden root can be neither expanded nor collapsed.
1650 wxCHECK_RET( !IsHiddenRoot(item
),
1651 wxT("Can't expand/collapse hidden root node!") );
1653 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1654 // emulate them. This behaviour has changed slightly with comctl32.dll
1655 // v 4.70 - now it does send them but only the first time. To maintain
1656 // compatible behaviour and also in order to not have surprises with the
1657 // future versions, don't rely on this and still do everything ourselves.
1658 // To avoid that the messages be sent twice when the item is expanded for
1659 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1661 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1665 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1667 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1669 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1673 (void)GetEventHandler()->ProcessEvent(event
);
1675 //else: change didn't took place, so do nothing at all
1678 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1680 DoExpand(item
, TVE_EXPAND
);
1683 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1685 DoExpand(item
, TVE_COLLAPSE
);
1688 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1690 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1693 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1695 DoExpand(item
, TVE_TOGGLE
);
1698 #if WXWIN_COMPATIBILITY_2_4
1700 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1702 DoExpand(item
, action
);
1707 void wxTreeCtrl::Unselect()
1709 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1710 wxT("doesn't make sense, may be you want UnselectAll()?") );
1712 // just remove the selection
1713 SelectItem(wxTreeItemId());
1716 void wxTreeCtrl::UnselectAll()
1718 if ( m_windowStyle
& wxTR_MULTIPLE
)
1720 wxArrayTreeItemIds selections
;
1721 size_t count
= GetSelections(selections
);
1722 for ( size_t n
= 0; n
< count
; n
++ )
1724 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1727 m_htSelStart
.Unset();
1731 // just remove the selection
1736 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1738 if ( m_windowStyle
& wxTR_MULTIPLE
)
1740 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1744 wxASSERT_MSG( select
,
1745 _T("SelectItem(false) works only for multiselect") );
1747 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1748 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1749 // send them ourselves
1751 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1752 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1754 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1756 wxLogLastError(wxT("TreeView_SelectItem"));
1760 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1761 (void)GetEventHandler()->ProcessEvent(event
);
1764 //else: program vetoed the change
1768 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1770 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1773 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1776 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1778 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1780 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1784 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1789 void wxTreeCtrl::DeleteTextCtrl()
1793 // the HWND corresponding to this control is deleted by the tree
1794 // control itself and we don't know when exactly this happens, so check
1795 // if the window still exists before calling UnsubclassWin()
1796 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1798 m_textCtrl
->SetHWND(0);
1801 m_textCtrl
->UnsubclassWin();
1802 m_textCtrl
->SetHWND(0);
1810 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1811 wxClassInfo
*textControlClass
)
1813 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1818 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1819 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1821 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1830 // textctrl is subclassed in MSWOnNotify
1834 // End label editing, optionally cancelling the edit
1835 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1837 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1842 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1844 TV_HITTESTINFO hitTestInfo
;
1845 hitTestInfo
.pt
.x
= (int)point
.x
;
1846 hitTestInfo
.pt
.y
= (int)point
.y
;
1848 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1853 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1854 flags |= wxTREE_HITTEST_##flag
1856 TRANSLATE_FLAG(ABOVE
);
1857 TRANSLATE_FLAG(BELOW
);
1858 TRANSLATE_FLAG(NOWHERE
);
1859 TRANSLATE_FLAG(ONITEMBUTTON
);
1860 TRANSLATE_FLAG(ONITEMICON
);
1861 TRANSLATE_FLAG(ONITEMINDENT
);
1862 TRANSLATE_FLAG(ONITEMLABEL
);
1863 TRANSLATE_FLAG(ONITEMRIGHT
);
1864 TRANSLATE_FLAG(ONITEMSTATEICON
);
1865 TRANSLATE_FLAG(TOLEFT
);
1866 TRANSLATE_FLAG(TORIGHT
);
1868 #undef TRANSLATE_FLAG
1870 return wxTreeItemId(hitTestInfo
.hItem
);
1873 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1875 bool textOnly
) const
1879 // Virtual root items have no bounding rectangle
1880 if ( IS_VIRTUAL_ROOT(item
) )
1885 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1888 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1894 // couldn't retrieve rect: for example, item isn't visible
1899 // ----------------------------------------------------------------------------
1901 // ----------------------------------------------------------------------------
1903 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1904 // functions such as IsDataIndirect()
1905 class wxTreeSortHelper
1908 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1911 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1913 return ((wxTreeItemParam
*)lParam
)->GetItem();
1917 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1921 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1922 wxT("sorting tree without data doesn't make sense") );
1924 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1926 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1927 GetIdFromData(pItem2
));
1930 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1932 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1934 // rely on the fact that TreeView_SortChildren does the same thing as our
1935 // default behaviour, i.e. sorts items alphabetically and so call it
1936 // directly if we're not in derived class (much more efficient!)
1937 // RN: Note that if you find you're code doesn't sort as expected this
1938 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1939 // combo for your derived wxTreeCtrl if will sort without
1941 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1943 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1948 tvSort
.hParent
= HITEM(item
);
1949 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1950 tvSort
.lParam
= (LPARAM
)this;
1951 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1955 // ----------------------------------------------------------------------------
1957 // ----------------------------------------------------------------------------
1959 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1961 if ( msg
->message
== WM_KEYDOWN
)
1963 if ( msg
->wParam
== VK_RETURN
)
1965 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1970 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
1973 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1975 if ( cmd
== EN_UPDATE
)
1977 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1978 event
.SetEventObject( this );
1979 ProcessCommand(event
);
1981 else if ( cmd
== EN_KILLFOCUS
)
1983 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1984 event
.SetEventObject( this );
1985 ProcessCommand(event
);
1993 // command processed
1997 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1998 // only do it during dragging, minimize wxWin overhead (this is important for
1999 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2000 // instead of passing by wxWin events
2001 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2003 bool processed
= false;
2005 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2007 // This message is sent after a right-click, or when the "menu" key is pressed
2008 if ( nMsg
== WM_CONTEXTMENU
)
2010 int x
= GET_X_LPARAM(lParam
),
2011 y
= GET_Y_LPARAM(lParam
);
2013 // the item for which the menu should be shown
2016 // the position where the menu should be shown in client coordinates
2017 // (so that it can be passed directly to PopupMenu())
2020 if ( x
== -1 || y
== -1 )
2022 // this means that the event was generated from keyboard (e.g. with
2023 // Shift-F10 or special Windows menu key)
2025 // use the Explorer standard of putting the menu at the left edge
2026 // of the text, in the vertical middle of the text
2027 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2030 // Use the bounding rectangle of only the text part
2032 GetBoundingRect(item
, rect
, true);
2033 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
2036 else // event from mouse, use mouse position
2038 pt
= ScreenToClient(wxPoint(x
, y
));
2040 TV_HITTESTINFO tvhti
;
2043 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2044 item
= wxTreeItemId(tvhti
.hItem
);
2048 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
2050 event
.m_pointDrag
= pt
;
2052 if ( GetEventHandler()->ProcessEvent(event
) )
2054 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2056 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2058 // we only process mouse messages here and these parameters have the
2059 // same meaning for all of them
2060 int x
= GET_X_LPARAM(lParam
),
2061 y
= GET_Y_LPARAM(lParam
);
2062 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2064 TV_HITTESTINFO tvht
;
2068 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2072 case WM_LBUTTONDOWN
:
2073 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2075 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2076 m_ptClick
= wxPoint(x
, y
);
2078 if ( wParam
& MK_CONTROL
)
2082 // toggle selected state
2083 ::ToggleItemSelection(GetHwnd(), htItem
);
2085 ::SetFocus(GetHwnd(), htItem
);
2087 // reset on any click without Shift
2088 m_htSelStart
.Unset();
2092 else if ( wParam
& MK_SHIFT
)
2094 // this selects all items between the starting one and
2097 if ( !m_htSelStart
)
2099 // take the focused item
2100 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2104 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2105 !(wParam
& MK_CONTROL
));
2107 ::SelectItem(GetHwnd(), htItem
);
2109 ::SetFocus(GetHwnd(), htItem
);
2113 else // normal click
2115 // avoid doing anything if we click on the only
2116 // currently selected item
2120 wxArrayTreeItemIds selections
;
2121 size_t count
= GetSelections(selections
);
2124 HITEM(selections
[0]) != htItem
)
2126 // clear the previously selected items, if the
2127 // user clicked outside of the present selection.
2128 // otherwise, perform the deselection on mouse-up.
2129 // this allows multiple drag and drop to work.
2131 if (!IsItemSelected(GetHwnd(), htItem
))
2135 // prevent the click from starting in-place editing
2136 // which should only happen if we click on the
2137 // already selected item (and nothing else is
2140 TreeView_SelectItem(GetHwnd(), 0);
2141 ::SelectItem(GetHwnd(), htItem
);
2143 ::SetFocus(GetHwnd(), htItem
);
2147 // reset on any click without Shift
2148 m_htSelStart
.Unset();
2155 if ( m_htClickedItem
)
2157 int cx
= abs(m_ptClick
.x
- x
);
2158 int cy
= abs(m_ptClick
.y
- y
);
2160 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2162 HWND pWnd
= ::GetParent( GetHwnd() );
2167 tv
.hdr
.hwndFrom
= GetHwnd();
2168 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2169 tv
.hdr
.code
= TVN_BEGINDRAG
;
2171 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2174 ZeroMemory(&tviAux
, sizeof(tviAux
));
2175 tviAux
.hItem
= HITEM(m_htClickedItem
);
2176 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2177 tviAux
.stateMask
= 0xffffffff;
2178 TreeView_GetItem( GetHwnd(), &tviAux
);
2180 tv
.itemNew
.state
= tviAux
.state
;
2181 tv
.itemNew
.lParam
= tviAux
.lParam
;
2186 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2188 m_htClickedItem
.Unset();
2191 #endif // __WXWINCE__
2195 m_dragImage
->Move(wxPoint(x
, y
));
2198 // highlight the item as target (hiding drag image is
2199 // necessary - otherwise the display will be corrupted)
2200 m_dragImage
->Hide();
2201 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2202 m_dragImage
->Show();
2209 // facilitates multiple drag-and-drop
2210 if (htItem
&& isMultiple
)
2212 wxArrayTreeItemIds selections
;
2213 size_t count
= GetSelections(selections
);
2216 !(wParam
& MK_CONTROL
) &&
2217 !(wParam
& MK_SHIFT
))
2220 TreeView_SelectItem(GetHwnd(), htItem
);
2221 ::SelectItem(GetHwnd(), htItem
);
2222 ::SetFocus(GetHwnd(), htItem
);
2224 m_htClickedItem
.Unset();
2232 m_dragImage
->EndDrag();
2236 // generate the drag end event
2237 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2238 (void)GetEventHandler()->ProcessEvent(event
);
2240 // if we don't do it, the tree seems to think that 2 items
2241 // are selected simultaneously which is quite weird
2242 TreeView_SelectDropTarget(GetHwnd(), 0);
2247 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2249 // the tree control greys out the selected item when it loses focus and
2250 // paints it as selected again when it regains it, but it won't do it
2251 // for the other items itself - help it
2252 wxArrayTreeItemIds selections
;
2253 size_t count
= GetSelections(selections
);
2255 for ( size_t n
= 0; n
< count
; n
++ )
2257 // TreeView_GetItemRect() will return false if item is not visible,
2258 // which may happen perfectly well
2259 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2262 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2266 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2268 bool bCtrl
= wxIsCtrlDown(),
2269 bShift
= wxIsShiftDown();
2271 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2277 ::ToggleItemSelection(GetHwnd(), htSel
);
2283 ::SelectItem(GetHwnd(), htSel
);
2291 if ( !bCtrl
&& !bShift
)
2293 // no modifiers, just clear selection and then let the default
2294 // processing to take place
2299 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2301 HTREEITEM htNext
= (HTREEITEM
)
2302 TreeView_GetNextItem
2306 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2312 // at the top/bottom
2318 if ( !m_htSelStart
)
2319 m_htSelStart
= htSel
;
2321 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2325 // without changing selection
2326 ::SetFocus(GetHwnd(), htNext
);
2337 // TODO: handle Shift/Ctrl with these keys
2338 if ( !bCtrl
&& !bShift
)
2342 m_htSelStart
.Unset();
2346 else if ( nMsg
== WM_COMMAND
)
2348 // if we receive a EN_KILLFOCUS command from the in-place edit control
2349 // used for label editing, make sure to end editing
2352 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2354 if ( cmd
== EN_KILLFOCUS
)
2356 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2366 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2372 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2374 if ( nMsg
== WM_CHAR
)
2376 // don't let the control process Space and Return keys because it
2377 // doesn't do anything useful with them anyhow but always beeps
2378 // annoyingly when it receives them and there is no way to turn it off
2379 // simply if you just process TREEITEM_ACTIVATED event to which Space
2380 // and Enter presses are mapped in your code
2381 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2385 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2388 // process WM_NOTIFY Windows message
2389 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2391 wxTreeEvent
event(wxEVT_NULL
, this);
2392 wxEventType eventType
= wxEVT_NULL
;
2393 NMHDR
*hdr
= (NMHDR
*)lParam
;
2395 switch ( hdr
->code
)
2398 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2401 case TVN_BEGINRDRAG
:
2403 if ( eventType
== wxEVT_NULL
)
2404 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2405 //else: left drag, already set above
2407 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2409 event
.m_item
= tv
->itemNew
.hItem
;
2410 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2412 // don't allow dragging by default: the user code must
2413 // explicitly say that it wants to allow it to avoid breaking
2419 case TVN_BEGINLABELEDIT
:
2421 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2422 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2424 // although the user event handler may still veto it, it is
2425 // important to set it now so that calls to SetItemText() from
2426 // the event handler would change the text controls contents
2428 event
.m_item
= info
->item
.hItem
;
2429 event
.m_label
= info
->item
.pszText
;
2430 event
.m_editCancelled
= false;
2434 case TVN_DELETEITEM
:
2436 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2437 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2439 event
.m_item
= tv
->itemOld
.hItem
;
2443 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2444 if ( it
!= m_attrs
.end() )
2453 case TVN_ENDLABELEDIT
:
2455 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2456 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2458 event
.m_item
= info
->item
.hItem
;
2459 event
.m_label
= info
->item
.pszText
;
2460 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2465 // These *must* not be removed or TVN_GETINFOTIP will
2466 // not be processed each time the mouse is moved
2467 // and the tooltip will only ever update once.
2476 #ifdef TVN_GETINFOTIP
2477 case TVN_GETINFOTIP
:
2479 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2480 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2482 // Which item are we trying to get a tooltip for?
2483 event
.m_item
= info
->hItem
;
2487 #endif // TVN_GETINFOTIP
2488 #endif // !__WXWINCE__
2490 case TVN_GETDISPINFO
:
2491 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2494 case TVN_SETDISPINFO
:
2496 if ( eventType
== wxEVT_NULL
)
2497 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2498 //else: get, already set above
2500 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2502 event
.m_item
= info
->item
.hItem
;
2506 case TVN_ITEMEXPANDING
:
2507 case TVN_ITEMEXPANDED
:
2509 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2512 switch ( tv
->action
)
2515 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2523 what
= IDX_COLLAPSE
;
2527 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2530 eventType
= gs_expandEvents
[what
][how
];
2532 event
.m_item
= tv
->itemNew
.hItem
;
2538 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2539 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2541 // fabricate the lParam and wParam parameters sufficiently
2542 // similar to the ones from a "real" WM_KEYDOWN so that
2543 // CreateKeyEvent() works correctly
2544 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2545 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2547 WXWPARAM wParam
= info
->wVKey
;
2549 int keyCode
= wxCharCodeMSWToWX(wParam
);
2552 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2557 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2562 // a separate event for Space/Return
2563 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2564 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2567 if ( !HasFlag(wxTR_MULTIPLE
) )
2568 item
= GetSelection();
2570 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2572 (void)GetEventHandler()->ProcessEvent(event2
);
2577 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2578 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2579 // we have to handle both messages:
2580 case TVN_SELCHANGEDA
:
2581 case TVN_SELCHANGEDW
:
2582 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2585 case TVN_SELCHANGINGA
:
2586 case TVN_SELCHANGINGW
:
2588 if ( eventType
== wxEVT_NULL
)
2589 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2590 //else: already set above
2592 if (hdr
->code
== TVN_SELCHANGINGW
||
2593 hdr
->code
== TVN_SELCHANGEDW
)
2595 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2596 event
.m_item
= tv
->itemNew
.hItem
;
2597 event
.m_itemOld
= tv
->itemOld
.hItem
;
2601 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2602 event
.m_item
= tv
->itemNew
.hItem
;
2603 event
.m_itemOld
= tv
->itemOld
.hItem
;
2608 // instead of explicitly checking for _WIN32_IE, check if the
2609 // required symbols are available in the headers
2610 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2613 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2614 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2615 switch ( nmcd
.dwDrawStage
)
2618 // if we've got any items with non standard attributes,
2619 // notify us before painting each item
2620 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2624 case CDDS_ITEMPREPAINT
:
2626 wxMapTreeAttr::iterator
2627 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2629 if ( it
== m_attrs
.end() )
2631 // nothing to do for this item
2632 *result
= CDRF_DODEFAULT
;
2636 wxTreeItemAttr
* const attr
= it
->second
;
2638 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2639 TVIF_STATE
, TVIS_DROPHILITED
);
2641 const UINT tvItemState
= tvItem
.state
;
2643 // selection colours should override ours,
2644 // otherwise it is too confusing to the user
2645 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2646 !(tvItemState
& TVIS_DROPHILITED
) )
2649 if ( attr
->HasBackgroundColour() )
2651 colBack
= attr
->GetBackgroundColour();
2652 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2656 // but we still want to keep the special foreground
2657 // colour when we don't have focus (we can't keep
2658 // it when we do, it would usually be unreadable on
2659 // the almost inverted bg colour...)
2660 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2661 FindFocus() != this ) &&
2662 !(tvItemState
& TVIS_DROPHILITED
) )
2665 if ( attr
->HasTextColour() )
2667 colText
= attr
->GetTextColour();
2668 lptvcd
->clrText
= wxColourToRGB(colText
);
2672 if ( attr
->HasFont() )
2674 HFONT hFont
= GetHfontOf(attr
->GetFont());
2676 ::SelectObject(nmcd
.hdc
, hFont
);
2678 *result
= CDRF_NEWFONT
;
2680 else // no specific font
2682 *result
= CDRF_DODEFAULT
;
2688 *result
= CDRF_DODEFAULT
;
2692 // we always process it
2694 #endif // have owner drawn support in headers
2698 DWORD pos
= GetMessagePos();
2700 point
.x
= LOWORD(pos
);
2701 point
.y
= HIWORD(pos
);
2702 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2704 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2705 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2707 event
.m_item
= item
;
2708 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2716 TV_HITTESTINFO tvhti
;
2717 ::GetCursorPos(&tvhti
.pt
);
2718 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2719 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2721 if ( tvhti
.flags
& TVHT_ONITEM
)
2723 event
.m_item
= tvhti
.hItem
;
2724 eventType
= (int)hdr
->code
== NM_DBLCLK
2725 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2726 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2728 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2729 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2738 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2741 event
.SetEventType(eventType
);
2743 if ( event
.m_item
.IsOk() )
2744 event
.SetClientObject(GetItemData(event
.m_item
));
2746 bool processed
= GetEventHandler()->ProcessEvent(event
);
2749 switch ( hdr
->code
)
2752 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2753 // the return code of this event handler as the return value for
2754 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2755 // expanded status would never work
2760 // prevent tree control from sending WM_CONTEXTMENU to our parent
2761 // (which it does if NM_RCLICK is not handled) because we want to
2762 // send it to the control itself
2766 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
2767 (WPARAM
)GetHwnd(), ::GetMessagePos());
2771 case TVN_BEGINRDRAG
:
2772 if ( event
.IsAllowed() )
2774 // normally this is impossible because the m_dragImage is
2775 // deleted once the drag operation is over
2776 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2778 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2779 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2780 m_dragImage
->Show();
2784 case TVN_DELETEITEM
:
2786 // NB: we might process this message using wxWidgets event
2787 // tables, but due to overhead of wxWin event system we
2788 // prefer to do it here ourself (otherwise deleting a tree
2789 // with many items is just too slow)
2790 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2792 wxTreeItemParam
*param
=
2793 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2796 processed
= true; // Make sure we don't get called twice
2800 case TVN_BEGINLABELEDIT
:
2801 // return true to cancel label editing
2802 *result
= !event
.IsAllowed();
2804 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2805 if ( event
.IsAllowed() )
2807 HWND hText
= TreeView_GetEditControl(GetHwnd());
2810 // MBN: if m_textCtrl already has an HWND, it is a stale
2811 // pointer from a previous edit (because the user
2812 // didn't modify the label before dismissing the control,
2813 // and TVN_ENDLABELEDIT was not sent), so delete it
2814 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2817 m_textCtrl
= new wxTextCtrl();
2818 m_textCtrl
->SetParent(this);
2819 m_textCtrl
->SetHWND((WXHWND
)hText
);
2820 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2822 // set wxTE_PROCESS_ENTER style for the text control to
2823 // force it to process the Enter presses itself, otherwise
2824 // they could be stolen from it by the dialog
2826 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2827 | wxTE_PROCESS_ENTER
);
2830 else // we had set m_idEdited before
2836 case TVN_ENDLABELEDIT
:
2837 // return true to set the label to the new string: note that we
2838 // also must pretend that we did process the message or it is going
2839 // to be passed to DefWindowProc() which will happily return false
2840 // cancelling the label change
2841 *result
= event
.IsAllowed();
2844 // ensure that we don't have the text ctrl which is going to be
2850 #ifdef TVN_GETINFOTIP
2851 case TVN_GETINFOTIP
:
2853 // If the user permitted a tooltip change, change it
2854 if (event
.IsAllowed())
2856 SetToolTip(event
.m_label
);
2863 case TVN_SELCHANGING
:
2864 case TVN_ITEMEXPANDING
:
2865 // return true to prevent the action from happening
2866 *result
= !event
.IsAllowed();
2869 case TVN_ITEMEXPANDED
:
2870 // the item is not refreshed properly after expansion when it has
2871 // an image depending on the expanded/collapsed state - bug in
2872 // comctl32.dll or our code?
2874 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2875 wxTreeItemId
id(tv
->itemNew
.hItem
);
2877 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2885 case TVN_GETDISPINFO
:
2886 // NB: so far the user can't set the image himself anyhow, so do it
2887 // anyway - but this may change later
2888 //if ( /* !processed && */ )
2890 wxTreeItemId item
= event
.m_item
;
2891 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2893 const wxTreeItemParam
* const param
= GetItemParam(item
);
2897 if ( info
->item
.mask
& TVIF_IMAGE
)
2902 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2903 : wxTreeItemIcon_Normal
2906 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2908 info
->item
.iSelectedImage
=
2911 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2912 : wxTreeItemIcon_Selected
2919 // for the other messages the return value is ignored and there is
2920 // nothing special to do
2925 // ----------------------------------------------------------------------------
2927 // ----------------------------------------------------------------------------
2929 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2930 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2932 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2935 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2936 tvi
.mask
= TVIF_STATE
;
2937 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2939 // Select the specified state, or -1 == cycle to the next one.
2942 TreeView_GetItem(GetHwnd(), &tvi
);
2944 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2945 if ( state
== m_imageListState
->GetImageCount() )
2949 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2950 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2952 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2954 TreeView_SetItem(GetHwnd(), &tvi
);
2957 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2960 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2961 tvi
.mask
= TVIF_STATE
;
2962 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2963 TreeView_GetItem(GetHwnd(), &tvi
);
2965 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2968 #endif // wxUSE_TREECTRL