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 // wrappers for TreeView_GetItem/TreeView_SetItem
60 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
64 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
65 tvi
.stateMask
= TVIS_SELECTED
;
68 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
70 wxLogLastError(wxT("TreeView_GetItem"));
73 return (tvi
.state
& TVIS_SELECTED
) != 0;
76 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
79 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
80 tvi
.stateMask
= TVIS_SELECTED
;
81 tvi
.state
= select
? TVIS_SELECTED
: 0;
84 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
86 wxLogLastError(wxT("TreeView_SetItem"));
93 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
95 SelectItem(hwndTV
, htItem
, false);
98 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
100 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
103 // helper function which selects all items in a range and, optionally,
104 // unselects all others
105 static void SelectRange(HWND hwndTV
,
108 bool unselectOthers
= true)
110 // find the first (or last) item and select it
112 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
113 while ( htItem
&& cont
)
115 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
117 if ( !IsItemSelected(hwndTV
, htItem
) )
119 SelectItem(hwndTV
, htItem
);
126 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
128 UnselectItem(hwndTV
, htItem
);
132 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
135 // select the items in range
136 cont
= htFirst
!= htLast
;
137 while ( htItem
&& cont
)
139 if ( !IsItemSelected(hwndTV
, htItem
) )
141 SelectItem(hwndTV
, htItem
);
144 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
146 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
150 if ( unselectOthers
)
154 if ( IsItemSelected(hwndTV
, htItem
) )
156 UnselectItem(hwndTV
, htItem
);
159 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
163 // seems to be necessary - otherwise the just selected items don't always
164 // appear as selected
165 UpdateWindow(hwndTV
);
168 // helper function which tricks the standard control into changing the focused
169 // item without changing anything else (if someone knows why Microsoft doesn't
170 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
171 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
174 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
179 if ( htItem
!= htFocus
)
181 // remember the selection state of the item
182 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
184 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
186 // prevent the tree from unselecting the old focus which it
187 // would do by default (TreeView_SelectItem unselects the
189 TreeView_SelectItem(hwndTV
, 0);
190 SelectItem(hwndTV
, htFocus
);
193 TreeView_SelectItem(hwndTV
, htItem
);
197 // need to clear the selection which TreeView_SelectItem() gave
199 UnselectItem(hwndTV
, htItem
);
201 //else: was selected, still selected - ok
203 //else: nothing to do, focus already there
209 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
211 // just clear the focus
212 TreeView_SelectItem(hwndTV
, 0);
214 if ( wasFocusSelected
)
216 // restore the selection state
217 SelectItem(hwndTV
, htFocus
);
220 //else: nothing to do, no focus already
224 // ----------------------------------------------------------------------------
226 // ----------------------------------------------------------------------------
228 // a convenient wrapper around TV_ITEM struct which adds a ctor
230 #pragma warning( disable : 4097 ) // inheriting from typedef
233 struct wxTreeViewItem
: public TV_ITEM
235 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
236 UINT mask_
, // fields which are valid
237 UINT stateMask_
= 0) // for TVIF_STATE only
241 // hItem member is always valid
242 mask
= mask_
| TVIF_HANDLE
;
243 stateMask
= stateMask_
;
248 // ----------------------------------------------------------------------------
249 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
251 // We need this for a couple of reasons:
253 // 1) This class is needed for support of different images: the Win32 common
254 // control natively supports only 2 images (the normal one and another for the
255 // selected state). We wish to provide support for 2 more of them for folder
256 // items (i.e. those which have children): for expanded state and for expanded
257 // selected state. For this we use this structure to store the additional items
260 // 2) This class is also needed to hold the HITEM so that we can sort
261 // it correctly in the MSW sort callback.
263 // In addition it makes other workarounds such as this easier and helps
264 // simplify the code.
265 // ----------------------------------------------------------------------------
267 class wxTreeItemParam
274 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
280 // dtor deletes the associated data as well
281 virtual ~wxTreeItemParam() { delete m_data
; }
284 // get the real data associated with the item
285 wxTreeItemData
*GetData() const { return m_data
; }
287 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
289 // do we have such image?
290 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
291 // get image, falling back to the other images if this one is not
293 int GetImage(wxTreeItemIcon which
) const
295 int image
= m_images
[which
];
300 case wxTreeItemIcon_SelectedExpanded
:
301 image
= GetImage(wxTreeItemIcon_Expanded
);
306 case wxTreeItemIcon_Selected
:
307 case wxTreeItemIcon_Expanded
:
308 image
= GetImage(wxTreeItemIcon_Normal
);
311 case wxTreeItemIcon_Normal
:
316 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
322 // change the given image
323 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
326 const wxTreeItemId
& GetItem() const { return m_item
; }
328 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
331 // all the images associated with the item
332 int m_images
[wxTreeItemIcon_Max
];
334 // item for sort callbacks
337 // the real client data
338 wxTreeItemData
*m_data
;
340 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
343 // wxVirutalNode is used in place of a single root when 'hidden' root is
345 class wxVirtualNode
: public wxTreeViewItem
348 wxVirtualNode(wxTreeItemParam
*param
)
349 : wxTreeViewItem(TVI_ROOT
, 0)
359 wxTreeItemParam
*GetParam() const { return m_param
; }
360 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
363 wxTreeItemParam
*m_param
;
365 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
369 #pragma warning( default : 4097 )
372 // a macro to get the virtual root, returns NULL if none
373 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
375 // returns true if the item is the virtual root
376 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
378 // a class which encapsulates the tree traversal logic: it vists all (unless
379 // OnVisit() returns false) items under the given one
380 class wxTreeTraversal
383 wxTreeTraversal(const wxTreeCtrl
*tree
)
388 // give it a virtual dtor: not really needed as the class is never used
389 // polymorphically and not even allocated on heap at all, but this is safer
390 // (in case it ever is) and silences the compiler warnings for now
391 virtual ~wxTreeTraversal() { }
393 // do traverse the tree: visit all items (recursively by default) under the
394 // given one; return true if all items were traversed or false if the
395 // traversal was aborted because OnVisit returned false
396 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
398 // override this function to do whatever is needed for each item, return
399 // false to stop traversing
400 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
403 const wxTreeCtrl
*GetTree() const { return m_tree
; }
406 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
408 const wxTreeCtrl
*m_tree
;
410 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
413 // internal class for getting the selected items
414 class TraverseSelections
: public wxTreeTraversal
417 TraverseSelections(const wxTreeCtrl
*tree
,
418 wxArrayTreeItemIds
& selections
)
419 : wxTreeTraversal(tree
), m_selections(selections
)
421 m_selections
.Empty();
423 if (tree
->GetCount() > 0)
424 DoTraverse(tree
->GetRootItem());
427 virtual bool OnVisit(const wxTreeItemId
& item
)
429 const wxTreeCtrl
* const tree
= GetTree();
431 // can't visit a virtual node.
432 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
437 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
439 m_selections
.Add(item
);
445 size_t GetCount() const { return m_selections
.GetCount(); }
448 wxArrayTreeItemIds
& m_selections
;
450 DECLARE_NO_COPY_CLASS(TraverseSelections
)
453 // internal class for counting tree items
454 class TraverseCounter
: public wxTreeTraversal
457 TraverseCounter(const wxTreeCtrl
*tree
,
458 const wxTreeItemId
& root
,
460 : wxTreeTraversal(tree
)
464 DoTraverse(root
, recursively
);
467 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
474 size_t GetCount() const { return m_count
; }
479 DECLARE_NO_COPY_CLASS(TraverseCounter
)
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 #if wxUSE_EXTENDED_RTTI
487 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
489 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
490 // new style border flags, we put them first to
491 // use them for streaming out
492 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
493 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
494 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
495 wxFLAGS_MEMBER(wxBORDER_RAISED
)
496 wxFLAGS_MEMBER(wxBORDER_STATIC
)
497 wxFLAGS_MEMBER(wxBORDER_NONE
)
499 // old style border flags
500 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
501 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
502 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
503 wxFLAGS_MEMBER(wxRAISED_BORDER
)
504 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
505 wxFLAGS_MEMBER(wxBORDER
)
507 // standard window styles
508 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
509 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
510 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
511 wxFLAGS_MEMBER(wxWANTS_CHARS
)
512 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
513 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
514 wxFLAGS_MEMBER(wxVSCROLL
)
515 wxFLAGS_MEMBER(wxHSCROLL
)
517 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
518 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
519 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
520 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
521 wxFLAGS_MEMBER(wxTR_NO_LINES
)
522 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
523 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
524 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
525 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
526 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
527 wxFLAGS_MEMBER(wxTR_SINGLE
)
528 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
529 wxFLAGS_MEMBER(wxTR_EXTENDED
)
530 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
532 wxEND_FLAGS( wxTreeCtrlStyle
)
534 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
536 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
537 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
538 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
539 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
540 wxEND_PROPERTIES_TABLE()
542 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
543 wxEND_HANDLERS_TABLE()
545 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
547 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
554 // indices in gs_expandEvents table below
569 // handy table for sending events - it has to be initialized during run-time
570 // now so can't be const any more
571 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
574 but logically it's a const table with the following entries:
577 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
578 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
582 // ============================================================================
584 // ============================================================================
586 // ----------------------------------------------------------------------------
588 // ----------------------------------------------------------------------------
590 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
592 if ( !OnVisit(root
) )
595 return Traverse(root
, recursively
);
598 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
600 wxTreeItemIdValue cookie
;
601 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
602 while ( child
.IsOk() )
604 // depth first traversal
605 if ( recursively
&& !Traverse(child
, true) )
608 if ( !OnVisit(child
) )
611 child
= m_tree
->GetNextChild(root
, cookie
);
617 // ----------------------------------------------------------------------------
618 // construction and destruction
619 // ----------------------------------------------------------------------------
621 void wxTreeCtrl::Init()
624 m_hasAnyAttr
= false;
626 m_pVirtualRoot
= NULL
;
628 // initialize the global array of events now as it can't be done statically
629 // with the wxEVT_XXX values being allocated during run-time only
630 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
631 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
632 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
633 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
636 bool wxTreeCtrl::Create(wxWindow
*parent
,
641 const wxValidator
& validator
,
642 const wxString
& name
)
646 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
647 style
|= wxBORDER_SUNKEN
;
649 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
653 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
654 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
656 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
657 wstyle
|= TVS_HASLINES
;
658 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
659 wstyle
|= TVS_HASBUTTONS
;
661 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
662 wstyle
|= TVS_EDITLABELS
;
664 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
665 wstyle
|= TVS_LINESATROOT
;
667 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
669 if ( wxApp::GetComCtl32Version() >= 471 )
670 wstyle
|= TVS_FULLROWSELECT
;
673 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
674 // Need so that TVN_GETINFOTIP messages will be sent
675 wstyle
|= TVS_INFOTIP
;
678 // Create the tree control.
679 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
682 #if wxUSE_COMCTL32_SAFELY
683 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
684 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
686 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
687 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
689 // This works around a bug in the Windows tree control whereby for some versions
690 // of comctrl32, setting any colour actually draws the background in black.
691 // This will initialise the background to the system colour.
692 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
693 // Assume the user has an updated comctl32.dll.
694 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
695 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
696 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
700 // VZ: this is some experimental code which may be used to get the
701 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
702 // AFAIK, the standard DLL does about the same thing anyhow.
704 if ( m_windowStyle
& wxTR_MULTIPLE
)
708 // create the DC compatible with the current screen
709 HDC hdcMem
= CreateCompatibleDC(NULL
);
711 // create a mono bitmap of the standard size
712 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
713 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
714 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
715 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
716 1, // # of color planes
717 1, // # bits needed for one pixel
718 0); // array containing colour data
719 SelectObject(hdcMem
, hbmpCheck
);
721 // then draw a check mark into it
722 RECT rect
= { 0, 0, x
, y
};
723 if ( !::DrawFrameControl(hdcMem
, &rect
,
725 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
727 wxLogLastError(wxT("DrawFrameControl(check)"));
730 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
731 imagelistCheckboxes
.Add(bmp
);
733 if ( !::DrawFrameControl(hdcMem
, &rect
,
737 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
740 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
741 imagelistCheckboxes
.Add(bmp
);
747 SetStateImageList(&imagelistCheckboxes
);
751 wxSetCCUnicodeFormat(GetHwnd());
756 wxTreeCtrl::~wxTreeCtrl()
758 // delete any attributes
761 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
763 // prevent TVN_DELETEITEM handler from deleting the attributes again!
764 m_hasAnyAttr
= false;
769 // delete user data to prevent memory leaks
770 // also deletes hidden root node storage.
774 // ----------------------------------------------------------------------------
776 // ----------------------------------------------------------------------------
778 /* static */ wxVisualAttributes
779 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
781 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
783 // common controls have their own default font
784 attrs
.font
= wxGetCCDefaultFont();
790 // simple wrappers which add error checking in debug mode
792 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
794 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
795 _T("can't retrieve virtual root item") );
797 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
799 wxLogLastError(wxT("TreeView_GetItem"));
807 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
809 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
811 wxLogLastError(wxT("TreeView_SetItem"));
815 unsigned int wxTreeCtrl::GetCount() const
817 return (unsigned int)TreeView_GetCount(GetHwnd());
820 unsigned int wxTreeCtrl::GetIndent() const
822 return TreeView_GetIndent(GetHwnd());
825 void wxTreeCtrl::SetIndent(unsigned int indent
)
827 TreeView_SetIndent(GetHwnd(), indent
);
830 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
833 (void) TreeView_SetImageList(GetHwnd(),
834 imageList
? imageList
->GetHIMAGELIST() : 0,
838 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
840 if (m_ownsImageListNormal
)
841 delete m_imageListNormal
;
843 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
844 m_ownsImageListNormal
= false;
847 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
849 if (m_ownsImageListState
) delete m_imageListState
;
850 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
851 m_ownsImageListState
= false;
854 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
855 bool recursively
) const
857 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
859 TraverseCounter
counter(this, item
, recursively
);
860 return counter
.GetCount() - 1;
863 // ----------------------------------------------------------------------------
865 // ----------------------------------------------------------------------------
867 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
869 #if !wxUSE_COMCTL32_SAFELY
870 if ( !wxWindowBase::SetBackgroundColour(colour
) )
873 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
879 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
881 #if !wxUSE_COMCTL32_SAFELY
882 if ( !wxWindowBase::SetForegroundColour(colour
) )
885 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
891 // ----------------------------------------------------------------------------
893 // ----------------------------------------------------------------------------
895 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
897 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
900 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
902 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
904 wxChar buf
[512]; // the size is arbitrary...
906 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
907 tvItem
.pszText
= buf
;
908 tvItem
.cchTextMax
= WXSIZEOF(buf
);
909 if ( !DoGetItem(&tvItem
) )
911 // don't return some garbage which was on stack, but an empty string
915 return wxString(buf
);
918 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
920 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
922 if ( IS_VIRTUAL_ROOT(item
) )
925 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
926 tvItem
.pszText
= (wxChar
*)text
.wx_str(); // conversion is ok
929 // when setting the text of the item being edited, the text control should
930 // be updated to reflect the new text as well, otherwise calling
931 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
933 // don't use GetEditControl() here because m_textCtrl is not set yet
934 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
937 if ( item
== m_idEdited
)
939 ::SetWindowText(hwndEdit
, text
);
944 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
945 wxTreeItemIcon which
) const
947 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
949 if ( IsHiddenRoot(item
) )
951 // no images for hidden root item
955 wxTreeItemParam
*param
= GetItemParam(item
);
957 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
960 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
961 wxTreeItemIcon which
)
963 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
964 wxCHECK_RET( which
>= 0 &&
965 which
< wxTreeItemIcon_Max
,
966 wxT("invalid image index"));
969 if ( IsHiddenRoot(item
) )
971 // no images for hidden root item
975 wxTreeItemParam
*data
= GetItemParam(item
);
979 data
->SetImage(image
, which
);
984 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
986 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
988 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
990 // hidden root may still have data.
991 if ( IS_VIRTUAL_ROOT(item
) )
993 return GET_VIRTUAL_ROOT()->GetParam();
997 if ( !DoGetItem(&tvItem
) )
1002 return (wxTreeItemParam
*)tvItem
.lParam
;
1005 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1007 wxTreeItemParam
*data
= GetItemParam(item
);
1009 return data
? data
->GetData() : NULL
;
1012 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1014 // first, associate this piece of data with this item
1020 wxTreeItemParam
*param
= GetItemParam(item
);
1022 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1024 param
->SetData(data
);
1027 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1029 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1031 if ( IS_VIRTUAL_ROOT(item
) )
1034 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1035 tvItem
.cChildren
= (int)has
;
1039 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1041 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1043 if ( IS_VIRTUAL_ROOT(item
) )
1046 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1047 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1051 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1053 if ( IS_VIRTUAL_ROOT(item
) )
1056 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1057 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1061 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1063 if ( IS_VIRTUAL_ROOT(item
) )
1067 if ( GetBoundingRect(item
, rect
) )
1073 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1075 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1077 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1078 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1081 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1083 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1085 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1086 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1089 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1091 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1093 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1094 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1097 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1098 const wxColour
& col
)
1100 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1102 wxTreeItemAttr
*attr
;
1103 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1104 if ( it
== m_attrs
.end() )
1106 m_hasAnyAttr
= true;
1108 m_attrs
[item
.m_pItem
] =
1109 attr
= new wxTreeItemAttr
;
1116 attr
->SetTextColour(col
);
1121 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1122 const wxColour
& col
)
1124 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1126 wxTreeItemAttr
*attr
;
1127 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1128 if ( it
== m_attrs
.end() )
1130 m_hasAnyAttr
= true;
1132 m_attrs
[item
.m_pItem
] =
1133 attr
= new wxTreeItemAttr
;
1135 else // already in the hash
1140 attr
->SetBackgroundColour(col
);
1145 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1147 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1149 wxTreeItemAttr
*attr
;
1150 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1151 if ( it
== m_attrs
.end() )
1153 m_hasAnyAttr
= true;
1155 m_attrs
[item
.m_pItem
] =
1156 attr
= new wxTreeItemAttr
;
1158 else // already in the hash
1163 attr
->SetFont(font
);
1165 // Reset the item's text to ensure that the bounding rect will be adjusted
1166 // for the new font.
1167 SetItemText(item
, GetItemText(item
));
1172 // ----------------------------------------------------------------------------
1174 // ----------------------------------------------------------------------------
1176 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1178 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1180 if ( item
== wxTreeItemId(TVI_ROOT
) )
1182 // virtual (hidden) root is never visible
1186 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1189 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1190 // the HTREEITEM with TVM_GETITEMRECT
1191 *(HTREEITEM
*)&rect
= HITEM(item
);
1193 // true means to get rect for just the text, not the whole line
1194 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1196 // if TVM_GETITEMRECT returned false, then the item is definitely not
1197 // visible (because its parent is not expanded)
1201 // however if it returned true, the item might still be outside the
1202 // currently visible part of the tree, test for it (notice that partly
1203 // visible means visible here)
1204 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1207 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1209 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1211 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1214 return tvItem
.cChildren
!= 0;
1217 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1219 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1221 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1224 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1227 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1229 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1231 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1234 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1237 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1239 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1241 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1244 return (tvItem
.state
& TVIS_BOLD
) != 0;
1247 // ----------------------------------------------------------------------------
1249 // ----------------------------------------------------------------------------
1251 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1253 // Root may be real (visible) or virtual (hidden).
1254 if ( GET_VIRTUAL_ROOT() )
1257 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1260 wxTreeItemId
wxTreeCtrl::GetSelection() const
1262 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1263 wxT("this only works with single selection controls") );
1265 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1268 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1270 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1274 if ( IS_VIRTUAL_ROOT(item
) )
1276 // no parent for the virtual root
1281 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1282 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1284 // the top level items should have the virtual root as their parent
1289 return wxTreeItemId(hItem
);
1292 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1293 wxTreeItemIdValue
& cookie
) const
1295 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1297 // remember the last child returned in 'cookie'
1298 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1300 return wxTreeItemId(cookie
);
1303 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1304 wxTreeItemIdValue
& cookie
) const
1306 wxTreeItemId
fromCookie(cookie
);
1308 HTREEITEM hitem
= HITEM(fromCookie
);
1310 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1312 wxTreeItemId
item(hitem
);
1314 cookie
= item
.m_pItem
;
1319 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1321 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1323 // can this be done more efficiently?
1324 wxTreeItemIdValue cookie
;
1326 wxTreeItemId childLast
,
1327 child
= GetFirstChild(item
, cookie
);
1328 while ( child
.IsOk() )
1331 child
= GetNextChild(item
, cookie
);
1337 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1339 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1340 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1343 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1345 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1346 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1349 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1351 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1354 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1356 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1357 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1359 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1362 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1364 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1365 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1367 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1370 // ----------------------------------------------------------------------------
1371 // multiple selections emulation
1372 // ----------------------------------------------------------------------------
1374 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1376 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1378 // receive the desired information.
1379 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1382 // state image indices are 1 based
1383 return ((tvItem
.state
>> 12) - 1) == 1;
1386 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1388 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1390 // receive the desired information.
1391 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1395 // state images are one-based
1396 tvItem
.state
= (check
? 2 : 1) << 12;
1401 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1403 TraverseSelections
selector(this, selections
);
1405 return selector
.GetCount();
1408 // ----------------------------------------------------------------------------
1410 // ----------------------------------------------------------------------------
1412 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1413 const wxTreeItemId
& hInsertAfter
,
1414 const wxString
& text
,
1415 int image
, int selectedImage
,
1416 wxTreeItemData
*data
)
1418 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1420 _T("can't have more than one root in the tree") );
1422 TV_INSERTSTRUCT tvIns
;
1423 tvIns
.hParent
= HITEM(parent
);
1424 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1426 // this is how we insert the item as the first child: supply a NULL
1428 if ( !tvIns
.hInsertAfter
)
1430 tvIns
.hInsertAfter
= TVI_FIRST
;
1434 if ( !text
.empty() )
1437 tvIns
.item
.pszText
= (wxChar
*)text
.wx_str(); // cast is ok
1441 tvIns
.item
.pszText
= NULL
;
1442 tvIns
.item
.cchTextMax
= 0;
1445 // create the param which will store the other item parameters
1446 wxTreeItemParam
*param
= new wxTreeItemParam
;
1448 // we return the images on demand as they depend on whether the item is
1449 // expanded or collapsed too in our case
1450 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1451 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1452 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1454 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1455 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1458 tvIns
.item
.lParam
= (LPARAM
)param
;
1459 tvIns
.item
.mask
= mask
;
1461 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1464 wxLogLastError(wxT("TreeView_InsertItem"));
1467 // associate the application tree item with Win32 tree item handle
1470 // setup wxTreeItemData
1473 param
->SetData(data
);
1477 return wxTreeItemId(id
);
1480 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1481 int image
, int selectedImage
,
1482 wxTreeItemData
*data
)
1484 if ( HasFlag(wxTR_HIDE_ROOT
) )
1486 wxASSERT_MSG( !m_pVirtualRoot
, _T("tree can have only a single root") );
1488 // create a virtual root item, the parent for all the others
1489 wxTreeItemParam
*param
= new wxTreeItemParam
;
1490 param
->SetData(data
);
1492 m_pVirtualRoot
= new wxVirtualNode(param
);
1497 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1498 text
, image
, selectedImage
, data
);
1501 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1503 const wxString
& text
,
1504 int image
, int selectedImage
,
1505 wxTreeItemData
*data
)
1507 wxTreeItemId idPrev
;
1508 if ( index
== (size_t)-1 )
1510 // special value: append to the end
1513 else // find the item from index
1515 wxTreeItemIdValue cookie
;
1516 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1517 while ( index
!= 0 && idCur
.IsOk() )
1522 idCur
= GetNextChild(parent
, cookie
);
1525 // assert, not check: if the index is invalid, we will append the item
1527 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1530 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1533 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1535 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1537 wxLogLastError(wxT("TreeView_DeleteItem"));
1541 // delete all children (but don't delete the item itself)
1542 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1544 wxTreeItemIdValue cookie
;
1546 wxArrayTreeItemIds children
;
1547 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1548 while ( child
.IsOk() )
1550 children
.Add(child
);
1552 child
= GetNextChild(item
, cookie
);
1555 size_t nCount
= children
.Count();
1556 for ( size_t n
= 0; n
< nCount
; n
++ )
1558 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1560 wxLogLastError(wxT("TreeView_DeleteItem"));
1565 void wxTreeCtrl::DeleteAllItems()
1567 // delete the "virtual" root item.
1568 if ( GET_VIRTUAL_ROOT() )
1570 delete GET_VIRTUAL_ROOT();
1571 m_pVirtualRoot
= NULL
;
1574 // and all the real items
1576 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1578 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1582 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1584 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1585 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1586 flag
== TVE_EXPAND
||
1588 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1590 // A hidden root can be neither expanded nor collapsed.
1591 wxCHECK_RET( !IsHiddenRoot(item
),
1592 wxT("Can't expand/collapse hidden root node!") );
1594 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1595 // emulate them. This behaviour has changed slightly with comctl32.dll
1596 // v 4.70 - now it does send them but only the first time. To maintain
1597 // compatible behaviour and also in order to not have surprises with the
1598 // future versions, don't rely on this and still do everything ourselves.
1599 // To avoid that the messages be sent twice when the item is expanded for
1600 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1602 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1606 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1608 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1610 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1614 (void)GetEventHandler()->ProcessEvent(event
);
1616 //else: change didn't took place, so do nothing at all
1619 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1621 DoExpand(item
, TVE_EXPAND
);
1624 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1626 DoExpand(item
, TVE_COLLAPSE
);
1629 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1631 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1634 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1636 DoExpand(item
, TVE_TOGGLE
);
1639 void wxTreeCtrl::Unselect()
1641 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1642 wxT("doesn't make sense, may be you want UnselectAll()?") );
1644 // just remove the selection
1645 SelectItem(wxTreeItemId());
1648 void wxTreeCtrl::UnselectAll()
1650 if ( m_windowStyle
& wxTR_MULTIPLE
)
1652 wxArrayTreeItemIds selections
;
1653 size_t count
= GetSelections(selections
);
1654 for ( size_t n
= 0; n
< count
; n
++ )
1656 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1659 m_htSelStart
.Unset();
1663 // just remove the selection
1668 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1670 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't select hidden root item") );
1672 if ( m_windowStyle
& wxTR_MULTIPLE
)
1674 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1678 wxASSERT_MSG( select
,
1679 _T("SelectItem(false) works only for multiselect") );
1681 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1682 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1683 // send them ourselves
1685 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1686 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1688 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1690 wxLogLastError(wxT("TreeView_SelectItem"));
1694 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1695 (void)GetEventHandler()->ProcessEvent(event
);
1698 //else: program vetoed the change
1702 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1704 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1707 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1710 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1712 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1714 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1718 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1723 void wxTreeCtrl::DeleteTextCtrl()
1727 // the HWND corresponding to this control is deleted by the tree
1728 // control itself and we don't know when exactly this happens, so check
1729 // if the window still exists before calling UnsubclassWin()
1730 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1732 m_textCtrl
->SetHWND(0);
1735 m_textCtrl
->UnsubclassWin();
1736 m_textCtrl
->SetHWND(0);
1744 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1745 wxClassInfo
*textControlClass
)
1747 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1752 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1753 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1755 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1764 // textctrl is subclassed in MSWOnNotify
1768 // End label editing, optionally cancelling the edit
1769 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1771 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1776 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1778 TV_HITTESTINFO hitTestInfo
;
1779 hitTestInfo
.pt
.x
= (int)point
.x
;
1780 hitTestInfo
.pt
.y
= (int)point
.y
;
1782 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1787 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1788 flags |= wxTREE_HITTEST_##flag
1790 TRANSLATE_FLAG(ABOVE
);
1791 TRANSLATE_FLAG(BELOW
);
1792 TRANSLATE_FLAG(NOWHERE
);
1793 TRANSLATE_FLAG(ONITEMBUTTON
);
1794 TRANSLATE_FLAG(ONITEMICON
);
1795 TRANSLATE_FLAG(ONITEMINDENT
);
1796 TRANSLATE_FLAG(ONITEMLABEL
);
1797 TRANSLATE_FLAG(ONITEMRIGHT
);
1798 TRANSLATE_FLAG(ONITEMSTATEICON
);
1799 TRANSLATE_FLAG(TOLEFT
);
1800 TRANSLATE_FLAG(TORIGHT
);
1802 #undef TRANSLATE_FLAG
1804 return wxTreeItemId(hitTestInfo
.hItem
);
1807 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1809 bool textOnly
) const
1813 // Virtual root items have no bounding rectangle
1814 if ( IS_VIRTUAL_ROOT(item
) )
1819 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1822 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1828 // couldn't retrieve rect: for example, item isn't visible
1833 // ----------------------------------------------------------------------------
1835 // ----------------------------------------------------------------------------
1837 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1838 // functions such as IsDataIndirect()
1839 class wxTreeSortHelper
1842 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1845 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1847 return ((wxTreeItemParam
*)lParam
)->GetItem();
1851 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1855 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1856 wxT("sorting tree without data doesn't make sense") );
1858 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1860 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1861 GetIdFromData(pItem2
));
1864 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1866 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1868 // rely on the fact that TreeView_SortChildren does the same thing as our
1869 // default behaviour, i.e. sorts items alphabetically and so call it
1870 // directly if we're not in derived class (much more efficient!)
1871 // RN: Note that if you find you're code doesn't sort as expected this
1872 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1873 // combo for your derived wxTreeCtrl if will sort without
1875 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1877 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1882 tvSort
.hParent
= HITEM(item
);
1883 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1884 tvSort
.lParam
= (LPARAM
)this;
1885 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1889 // ----------------------------------------------------------------------------
1891 // ----------------------------------------------------------------------------
1893 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1895 if ( msg
->message
== WM_KEYDOWN
)
1897 if ( msg
->wParam
== VK_RETURN
)
1899 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
1904 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
1907 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1909 if ( cmd
== EN_UPDATE
)
1911 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1912 event
.SetEventObject( this );
1913 ProcessCommand(event
);
1915 else if ( cmd
== EN_KILLFOCUS
)
1917 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1918 event
.SetEventObject( this );
1919 ProcessCommand(event
);
1927 // command processed
1931 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1932 // only do it during dragging, minimize wxWin overhead (this is important for
1933 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1934 // instead of passing by wxWin events
1935 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1937 bool processed
= false;
1939 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
1941 // This message is sent after a right-click, or when the "menu" key is pressed
1942 if ( nMsg
== WM_CONTEXTMENU
)
1944 int x
= GET_X_LPARAM(lParam
),
1945 y
= GET_Y_LPARAM(lParam
);
1947 // the item for which the menu should be shown
1950 // the position where the menu should be shown in client coordinates
1951 // (so that it can be passed directly to PopupMenu())
1954 if ( x
== -1 || y
== -1 )
1956 // this means that the event was generated from keyboard (e.g. with
1957 // Shift-F10 or special Windows menu key)
1959 // use the Explorer standard of putting the menu at the left edge
1960 // of the text, in the vertical middle of the text
1961 item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1964 // Use the bounding rectangle of only the text part
1966 GetBoundingRect(item
, rect
, true);
1967 pt
= wxPoint(rect
.GetX(), rect
.GetY() + rect
.GetHeight() / 2);
1970 else // event from mouse, use mouse position
1972 pt
= ScreenToClient(wxPoint(x
, y
));
1974 TV_HITTESTINFO tvhti
;
1977 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
1978 item
= wxTreeItemId(tvhti
.hItem
);
1982 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this, item
);
1984 event
.m_pointDrag
= pt
;
1986 if ( GetEventHandler()->ProcessEvent(event
) )
1988 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
1990 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1992 // we only process mouse messages here and these parameters have the
1993 // same meaning for all of them
1994 int x
= GET_X_LPARAM(lParam
),
1995 y
= GET_Y_LPARAM(lParam
);
1997 TV_HITTESTINFO tvht
;
2001 HTREEITEM htItem
= TreeView_HitTest(GetHwnd(), &tvht
);
2005 case WM_LBUTTONDOWN
:
2006 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2008 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2009 m_ptClick
= wxPoint(x
, y
);
2011 if ( wParam
& MK_CONTROL
)
2015 // toggle selected state
2016 ::ToggleItemSelection(GetHwnd(), htItem
);
2018 ::SetFocus(GetHwnd(), htItem
);
2020 // reset on any click without Shift
2021 m_htSelStart
.Unset();
2025 else if ( wParam
& MK_SHIFT
)
2027 // this selects all items between the starting one and
2030 if ( !m_htSelStart
)
2032 // take the focused item
2033 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2037 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2038 !(wParam
& MK_CONTROL
));
2040 ::SelectItem(GetHwnd(), htItem
);
2042 ::SetFocus(GetHwnd(), htItem
);
2046 else // normal click
2048 // avoid doing anything if we click on the only
2049 // currently selected item
2053 wxArrayTreeItemIds selections
;
2054 size_t count
= GetSelections(selections
);
2057 HITEM(selections
[0]) != htItem
)
2059 // clear the previously selected items, if the
2060 // user clicked outside of the present selection.
2061 // otherwise, perform the deselection on mouse-up.
2062 // this allows multiple drag and drop to work.
2064 if (!IsItemSelected(GetHwnd(), htItem
))
2068 // prevent the click from starting in-place editing
2069 // which should only happen if we click on the
2070 // already selected item (and nothing else is
2073 TreeView_SelectItem(GetHwnd(), 0);
2074 ::SelectItem(GetHwnd(), htItem
);
2076 ::SetFocus(GetHwnd(), htItem
);
2080 // reset on any click without Shift
2081 m_htSelStart
.Unset();
2088 if ( m_htClickedItem
)
2090 int cx
= abs(m_ptClick
.x
- x
);
2091 int cy
= abs(m_ptClick
.y
- y
);
2093 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2095 HWND pWnd
= ::GetParent( GetHwnd() );
2100 tv
.hdr
.hwndFrom
= GetHwnd();
2101 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2102 tv
.hdr
.code
= TVN_BEGINDRAG
;
2104 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2107 ZeroMemory(&tviAux
, sizeof(tviAux
));
2108 tviAux
.hItem
= HITEM(m_htClickedItem
);
2109 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2110 tviAux
.stateMask
= 0xffffffff;
2111 TreeView_GetItem( GetHwnd(), &tviAux
);
2113 tv
.itemNew
.state
= tviAux
.state
;
2114 tv
.itemNew
.lParam
= tviAux
.lParam
;
2119 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2121 m_htClickedItem
.Unset();
2124 #endif // __WXWINCE__
2128 m_dragImage
->Move(wxPoint(x
, y
));
2131 // highlight the item as target (hiding drag image is
2132 // necessary - otherwise the display will be corrupted)
2133 m_dragImage
->Hide();
2134 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2135 m_dragImage
->Show();
2142 // facilitates multiple drag-and-drop
2143 if (htItem
&& isMultiple
)
2145 wxArrayTreeItemIds selections
;
2146 size_t count
= GetSelections(selections
);
2149 !(wParam
& MK_CONTROL
) &&
2150 !(wParam
& MK_SHIFT
))
2153 TreeView_SelectItem(GetHwnd(), htItem
);
2154 ::SelectItem(GetHwnd(), htItem
);
2155 ::SetFocus(GetHwnd(), htItem
);
2157 m_htClickedItem
.Unset();
2165 m_dragImage
->EndDrag();
2169 // generate the drag end event
2170 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2171 event
.m_pointDrag
= wxPoint(x
, y
);
2173 (void)GetEventHandler()->ProcessEvent(event
);
2175 // if we don't do it, the tree seems to think that 2 items
2176 // are selected simultaneously which is quite weird
2177 TreeView_SelectDropTarget(GetHwnd(), 0);
2182 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2184 // the tree control greys out the selected item when it loses focus and
2185 // paints it as selected again when it regains it, but it won't do it
2186 // for the other items itself - help it
2187 wxArrayTreeItemIds selections
;
2188 size_t count
= GetSelections(selections
);
2190 for ( size_t n
= 0; n
< count
; n
++ )
2192 // TreeView_GetItemRect() will return false if item is not visible,
2193 // which may happen perfectly well
2194 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2197 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2201 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2203 bool bCtrl
= wxIsCtrlDown(),
2204 bShift
= wxIsShiftDown();
2206 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2212 ::ToggleItemSelection(GetHwnd(), htSel
);
2218 ::SelectItem(GetHwnd(), htSel
);
2226 if ( !bCtrl
&& !bShift
)
2228 // no modifiers, just clear selection and then let the default
2229 // processing to take place
2234 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2236 HTREEITEM htNext
= (HTREEITEM
)
2237 TreeView_GetNextItem
2241 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2247 // at the top/bottom
2253 if ( !m_htSelStart
)
2254 m_htSelStart
= htSel
;
2256 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2260 // without changing selection
2261 ::SetFocus(GetHwnd(), htNext
);
2272 // TODO: handle Shift/Ctrl with these keys
2273 if ( !bCtrl
&& !bShift
)
2277 m_htSelStart
.Unset();
2281 else if ( nMsg
== WM_COMMAND
)
2283 // if we receive a EN_KILLFOCUS command from the in-place edit control
2284 // used for label editing, make sure to end editing
2287 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2289 if ( cmd
== EN_KILLFOCUS
)
2291 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2301 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2307 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2309 if ( nMsg
== WM_CHAR
)
2311 // don't let the control process Space and Return keys because it
2312 // doesn't do anything useful with them anyhow but always beeps
2313 // annoyingly when it receives them and there is no way to turn it off
2314 // simply if you just process TREEITEM_ACTIVATED event to which Space
2315 // and Enter presses are mapped in your code
2316 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2320 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2323 // process WM_NOTIFY Windows message
2324 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2326 wxTreeEvent
event(wxEVT_NULL
, this);
2327 wxEventType eventType
= wxEVT_NULL
;
2328 NMHDR
*hdr
= (NMHDR
*)lParam
;
2330 switch ( hdr
->code
)
2333 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2336 case TVN_BEGINRDRAG
:
2338 if ( eventType
== wxEVT_NULL
)
2339 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2340 //else: left drag, already set above
2342 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2344 event
.m_item
= tv
->itemNew
.hItem
;
2345 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2347 // don't allow dragging by default: the user code must
2348 // explicitly say that it wants to allow it to avoid breaking
2354 case TVN_BEGINLABELEDIT
:
2356 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2357 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2359 // although the user event handler may still veto it, it is
2360 // important to set it now so that calls to SetItemText() from
2361 // the event handler would change the text controls contents
2363 event
.m_item
= info
->item
.hItem
;
2364 event
.m_label
= info
->item
.pszText
;
2365 event
.m_editCancelled
= false;
2369 case TVN_DELETEITEM
:
2371 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2372 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2374 event
.m_item
= tv
->itemOld
.hItem
;
2378 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2379 if ( it
!= m_attrs
.end() )
2388 case TVN_ENDLABELEDIT
:
2390 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2391 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2393 event
.m_item
= info
->item
.hItem
;
2394 event
.m_label
= info
->item
.pszText
;
2395 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2400 // These *must* not be removed or TVN_GETINFOTIP will
2401 // not be processed each time the mouse is moved
2402 // and the tooltip will only ever update once.
2411 #ifdef TVN_GETINFOTIP
2412 case TVN_GETINFOTIP
:
2414 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2415 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2417 // Which item are we trying to get a tooltip for?
2418 event
.m_item
= info
->hItem
;
2422 #endif // TVN_GETINFOTIP
2423 #endif // !__WXWINCE__
2425 case TVN_GETDISPINFO
:
2426 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2429 case TVN_SETDISPINFO
:
2431 if ( eventType
== wxEVT_NULL
)
2432 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2433 //else: get, already set above
2435 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2437 event
.m_item
= info
->item
.hItem
;
2441 case TVN_ITEMEXPANDING
:
2442 case TVN_ITEMEXPANDED
:
2444 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2447 switch ( tv
->action
)
2450 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2458 what
= IDX_COLLAPSE
;
2462 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2465 eventType
= gs_expandEvents
[what
][how
];
2467 event
.m_item
= tv
->itemNew
.hItem
;
2473 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2474 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2476 // fabricate the lParam and wParam parameters sufficiently
2477 // similar to the ones from a "real" WM_KEYDOWN so that
2478 // CreateKeyEvent() works correctly
2479 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2480 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2482 WXWPARAM wParam
= info
->wVKey
;
2484 int keyCode
= wxCharCodeMSWToWX(wParam
);
2487 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2492 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2497 // a separate event for Space/Return
2498 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2499 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2502 if ( !HasFlag(wxTR_MULTIPLE
) )
2503 item
= GetSelection();
2505 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2507 (void)GetEventHandler()->ProcessEvent(event2
);
2512 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2513 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2514 // we have to handle both messages:
2515 case TVN_SELCHANGEDA
:
2516 case TVN_SELCHANGEDW
:
2517 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2520 case TVN_SELCHANGINGA
:
2521 case TVN_SELCHANGINGW
:
2523 if ( eventType
== wxEVT_NULL
)
2524 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2525 //else: already set above
2527 if (hdr
->code
== TVN_SELCHANGINGW
||
2528 hdr
->code
== TVN_SELCHANGEDW
)
2530 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2531 event
.m_item
= tv
->itemNew
.hItem
;
2532 event
.m_itemOld
= tv
->itemOld
.hItem
;
2536 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2537 event
.m_item
= tv
->itemNew
.hItem
;
2538 event
.m_itemOld
= tv
->itemOld
.hItem
;
2543 // instead of explicitly checking for _WIN32_IE, check if the
2544 // required symbols are available in the headers
2545 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2548 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2549 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2550 switch ( nmcd
.dwDrawStage
)
2553 // if we've got any items with non standard attributes,
2554 // notify us before painting each item
2555 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2559 case CDDS_ITEMPREPAINT
:
2561 wxMapTreeAttr::iterator
2562 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2564 if ( it
== m_attrs
.end() )
2566 // nothing to do for this item
2567 *result
= CDRF_DODEFAULT
;
2571 wxTreeItemAttr
* const attr
= it
->second
;
2573 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2574 TVIF_STATE
, TVIS_DROPHILITED
);
2576 const UINT tvItemState
= tvItem
.state
;
2578 // selection colours should override ours,
2579 // otherwise it is too confusing to the user
2580 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2581 !(tvItemState
& TVIS_DROPHILITED
) )
2584 if ( attr
->HasBackgroundColour() )
2586 colBack
= attr
->GetBackgroundColour();
2587 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2591 // but we still want to keep the special foreground
2592 // colour when we don't have focus (we can't keep
2593 // it when we do, it would usually be unreadable on
2594 // the almost inverted bg colour...)
2595 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2596 FindFocus() != this ) &&
2597 !(tvItemState
& TVIS_DROPHILITED
) )
2600 if ( attr
->HasTextColour() )
2602 colText
= attr
->GetTextColour();
2603 lptvcd
->clrText
= wxColourToRGB(colText
);
2607 if ( attr
->HasFont() )
2609 HFONT hFont
= GetHfontOf(attr
->GetFont());
2611 ::SelectObject(nmcd
.hdc
, hFont
);
2613 *result
= CDRF_NEWFONT
;
2615 else // no specific font
2617 *result
= CDRF_DODEFAULT
;
2623 *result
= CDRF_DODEFAULT
;
2627 // we always process it
2629 #endif // have owner drawn support in headers
2633 DWORD pos
= GetMessagePos();
2635 point
.x
= LOWORD(pos
);
2636 point
.y
= HIWORD(pos
);
2637 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2639 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2640 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2642 event
.m_item
= item
;
2643 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2651 TV_HITTESTINFO tvhti
;
2652 ::GetCursorPos(&tvhti
.pt
);
2653 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2654 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2656 if ( tvhti
.flags
& TVHT_ONITEM
)
2658 event
.m_item
= tvhti
.hItem
;
2659 eventType
= (int)hdr
->code
== NM_DBLCLK
2660 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2661 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2663 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2664 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2673 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2676 event
.SetEventType(eventType
);
2678 if ( event
.m_item
.IsOk() )
2679 event
.SetClientObject(GetItemData(event
.m_item
));
2681 bool processed
= GetEventHandler()->ProcessEvent(event
);
2684 switch ( hdr
->code
)
2687 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2688 // the return code of this event handler as the return value for
2689 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2690 // expanded status would never work
2695 // prevent tree control from sending WM_CONTEXTMENU to our parent
2696 // (which it does if NM_RCLICK is not handled) because we want to
2697 // send it to the control itself
2701 ::SendMessage(GetHwnd(), WM_CONTEXTMENU
,
2702 (WPARAM
)GetHwnd(), ::GetMessagePos());
2706 case TVN_BEGINRDRAG
:
2707 if ( event
.IsAllowed() )
2709 // normally this is impossible because the m_dragImage is
2710 // deleted once the drag operation is over
2711 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2713 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2714 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2715 m_dragImage
->Show();
2719 case TVN_DELETEITEM
:
2721 // NB: we might process this message using wxWidgets event
2722 // tables, but due to overhead of wxWin event system we
2723 // prefer to do it here ourself (otherwise deleting a tree
2724 // with many items is just too slow)
2725 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2727 wxTreeItemParam
*param
=
2728 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2731 processed
= true; // Make sure we don't get called twice
2735 case TVN_BEGINLABELEDIT
:
2736 // return true to cancel label editing
2737 *result
= !event
.IsAllowed();
2739 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2740 if ( event
.IsAllowed() )
2742 HWND hText
= TreeView_GetEditControl(GetHwnd());
2745 // MBN: if m_textCtrl already has an HWND, it is a stale
2746 // pointer from a previous edit (because the user
2747 // didn't modify the label before dismissing the control,
2748 // and TVN_ENDLABELEDIT was not sent), so delete it
2749 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2752 m_textCtrl
= new wxTextCtrl();
2753 m_textCtrl
->SetParent(this);
2754 m_textCtrl
->SetHWND((WXHWND
)hText
);
2755 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2757 // set wxTE_PROCESS_ENTER style for the text control to
2758 // force it to process the Enter presses itself, otherwise
2759 // they could be stolen from it by the dialog
2761 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2762 | wxTE_PROCESS_ENTER
);
2765 else // we had set m_idEdited before
2771 case TVN_ENDLABELEDIT
:
2772 // return true to set the label to the new string: note that we
2773 // also must pretend that we did process the message or it is going
2774 // to be passed to DefWindowProc() which will happily return false
2775 // cancelling the label change
2776 *result
= event
.IsAllowed();
2779 // ensure that we don't have the text ctrl which is going to be
2785 #ifdef TVN_GETINFOTIP
2786 case TVN_GETINFOTIP
:
2788 // If the user permitted a tooltip change, change it
2789 if (event
.IsAllowed())
2791 SetToolTip(event
.m_label
);
2798 case TVN_SELCHANGING
:
2799 case TVN_ITEMEXPANDING
:
2800 // return true to prevent the action from happening
2801 *result
= !event
.IsAllowed();
2804 case TVN_ITEMEXPANDED
:
2805 // the item is not refreshed properly after expansion when it has
2806 // an image depending on the expanded/collapsed state - bug in
2807 // comctl32.dll or our code?
2809 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2810 wxTreeItemId
id(tv
->itemNew
.hItem
);
2812 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2820 case TVN_GETDISPINFO
:
2821 // NB: so far the user can't set the image himself anyhow, so do it
2822 // anyway - but this may change later
2823 //if ( /* !processed && */ )
2825 wxTreeItemId item
= event
.m_item
;
2826 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2828 const wxTreeItemParam
* const param
= GetItemParam(item
);
2832 if ( info
->item
.mask
& TVIF_IMAGE
)
2837 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2838 : wxTreeItemIcon_Normal
2841 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2843 info
->item
.iSelectedImage
=
2846 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2847 : wxTreeItemIcon_Selected
2854 // for the other messages the return value is ignored and there is
2855 // nothing special to do
2860 // ----------------------------------------------------------------------------
2862 // ----------------------------------------------------------------------------
2864 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2865 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2867 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2870 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2871 tvi
.mask
= TVIF_STATE
;
2872 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2874 // Select the specified state, or -1 == cycle to the next one.
2877 TreeView_GetItem(GetHwnd(), &tvi
);
2879 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2880 if ( state
== m_imageListState
->GetImageCount() )
2884 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2885 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2887 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2889 TreeView_SetItem(GetHwnd(), &tvi
);
2892 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2895 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2896 tvi
.mask
= TVIF_STATE
;
2897 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2898 TreeView_GetItem(GetHwnd(), &tvi
);
2900 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2903 #endif // wxUSE_TREECTRL