1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/msw/private.h"
35 // Set this to 1 to be _absolutely_ sure that repainting will work for all
36 // comctl32.dll versions
37 #define wxUSE_COMCTL32_SAFELY 0
41 #include "wx/dynarray.h"
42 #include "wx/imaglist.h"
43 #include "wx/settings.h"
44 #include "wx/msw/treectrl.h"
45 #include "wx/msw/dragimag.h"
47 // include <commctrl.h> "properly"
48 #include "wx/msw/wrapcctl.h"
50 // macros to hide the cast ugliness
51 // --------------------------------
53 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
54 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
56 // item here is a wxTreeItemId
57 #define HITEM(item) HITEM_PTR((item).m_pItem)
59 // the native control doesn't support multiple selections under MSW and we
60 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
61 // checkboxes be the selection status (checked == selected) or by really
62 // emulating everything, i.e. intercepting mouse and key events &c. The first
63 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
65 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // wrapper for TreeView_HitTest
72 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
78 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
81 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
83 // wrappers for TreeView_GetItem/TreeView_SetItem
84 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
88 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
89 tvi
.stateMask
= TVIS_SELECTED
;
92 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
94 wxLogLastError(wxT("TreeView_GetItem"));
97 return (tvi
.state
& TVIS_SELECTED
) != 0;
100 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
103 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
104 tvi
.stateMask
= TVIS_SELECTED
;
105 tvi
.state
= select
? TVIS_SELECTED
: 0;
108 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
110 wxLogLastError(wxT("TreeView_SetItem"));
114 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
116 SelectItem(hwndTV
, htItem
, false);
119 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
121 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
124 // helper function which selects all items in a range and, optionally,
125 // unselects all others
126 static void SelectRange(HWND hwndTV
,
129 bool unselectOthers
= true)
131 // find the first (or last) item and select it
133 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
134 while ( htItem
&& cont
)
136 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
138 if ( !IsItemSelected(hwndTV
, htItem
) )
140 SelectItem(hwndTV
, htItem
);
147 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
149 UnselectItem(hwndTV
, htItem
);
153 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
156 // select the items in range
157 cont
= htFirst
!= htLast
;
158 while ( htItem
&& cont
)
160 if ( !IsItemSelected(hwndTV
, htItem
) )
162 SelectItem(hwndTV
, htItem
);
165 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
167 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
171 if ( unselectOthers
)
175 if ( IsItemSelected(hwndTV
, htItem
) )
177 UnselectItem(hwndTV
, htItem
);
180 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
184 // seems to be necessary - otherwise the just selected items don't always
185 // appear as selected
186 UpdateWindow(hwndTV
);
189 // helper function which tricks the standard control into changing the focused
190 // item without changing anything else (if someone knows why Microsoft doesn't
191 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
192 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
195 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
200 if ( htItem
!= htFocus
)
202 // remember the selection state of the item
203 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
205 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
207 // prevent the tree from unselecting the old focus which it
208 // would do by default (TreeView_SelectItem unselects the
210 TreeView_SelectItem(hwndTV
, 0);
211 SelectItem(hwndTV
, htFocus
);
214 TreeView_SelectItem(hwndTV
, htItem
);
218 // need to clear the selection which TreeView_SelectItem() gave
220 UnselectItem(hwndTV
, htItem
);
222 //else: was selected, still selected - ok
224 //else: nothing to do, focus already there
230 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
232 // just clear the focus
233 TreeView_SelectItem(hwndTV
, 0);
235 if ( wasFocusSelected
)
237 // restore the selection state
238 SelectItem(hwndTV
, htFocus
);
241 //else: nothing to do, no focus already
245 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // a convenient wrapper around TV_ITEM struct which adds a ctor
253 #pragma warning( disable : 4097 ) // inheriting from typedef
256 struct wxTreeViewItem
: public TV_ITEM
258 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
259 UINT mask_
, // fields which are valid
260 UINT stateMask_
= 0) // for TVIF_STATE only
264 // hItem member is always valid
265 mask
= mask_
| TVIF_HANDLE
;
266 stateMask
= stateMask_
;
271 // wxVirutalNode is used in place of a single root when 'hidden' root is
273 class wxVirtualNode
: public wxTreeViewItem
276 wxVirtualNode(wxTreeItemData
*data
)
277 : wxTreeViewItem(TVI_ROOT
, 0)
287 wxTreeItemData
*GetData() const { return m_data
; }
288 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
291 wxTreeItemData
*m_data
;
293 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
297 #pragma warning( default : 4097 )
300 // a macro to get the virtual root, returns NULL if none
301 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
303 // returns true if the item is the virtual root
304 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
306 // a class which encapsulates the tree traversal logic: it vists all (unless
307 // OnVisit() returns false) items under the given one
308 class wxTreeTraversal
311 wxTreeTraversal(const wxTreeCtrl
*tree
)
316 // do traverse the tree: visit all items (recursively by default) under the
317 // given one; return true if all items were traversed or false if the
318 // traversal was aborted because OnVisit returned false
319 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
321 // override this function to do whatever is needed for each item, return
322 // false to stop traversing
323 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
326 const wxTreeCtrl
*GetTree() const { return m_tree
; }
329 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
331 const wxTreeCtrl
*m_tree
;
333 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
336 // internal class for getting the selected items
337 class TraverseSelections
: public wxTreeTraversal
340 TraverseSelections(const wxTreeCtrl
*tree
,
341 wxArrayTreeItemIds
& selections
)
342 : wxTreeTraversal(tree
), m_selections(selections
)
344 m_selections
.Empty();
346 DoTraverse(tree
->GetRootItem());
349 virtual bool OnVisit(const wxTreeItemId
& item
)
351 // can't visit a virtual node.
352 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
357 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
358 if ( GetTree()->IsItemChecked(item
) )
360 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
363 m_selections
.Add(item
);
369 size_t GetCount() const { return m_selections
.GetCount(); }
372 wxArrayTreeItemIds
& m_selections
;
374 DECLARE_NO_COPY_CLASS(TraverseSelections
)
377 // internal class for counting tree items
378 class TraverseCounter
: public wxTreeTraversal
381 TraverseCounter(const wxTreeCtrl
*tree
,
382 const wxTreeItemId
& root
,
384 : wxTreeTraversal(tree
)
388 DoTraverse(root
, recursively
);
391 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
398 size_t GetCount() const { return m_count
; }
403 DECLARE_NO_COPY_CLASS(TraverseCounter
)
406 // ----------------------------------------------------------------------------
407 // This class is needed for support of different images: the Win32 common
408 // control natively supports only 2 images (the normal one and another for the
409 // selected state). We wish to provide support for 2 more of them for folder
410 // items (i.e. those which have children): for expanded state and for expanded
411 // selected state. For this we use this structure to store the additional items
414 // There is only one problem with this: when we retrieve the item's data, we
415 // don't know whether we get a pointer to wxTreeItemData or
416 // wxTreeItemIndirectData. So we always set the item id to an invalid value
417 // in this class and the code using the client data checks for it and retrieves
418 // the real client data in this case.
419 // ----------------------------------------------------------------------------
421 class wxTreeItemIndirectData
: public wxTreeItemData
424 // ctor associates this data with the item and the real item data becomes
425 // available through our GetData() method
426 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
428 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
434 m_data
= tree
->GetItemData(item
);
436 // and set ourselves as the new one
437 tree
->SetIndirectItemData(item
, this);
439 // we must have the invalid value for the item
443 // dtor deletes the associated data as well
444 virtual ~wxTreeItemIndirectData() { delete m_data
; }
447 // get the real data associated with the item
448 wxTreeItemData
*GetData() const { return m_data
; }
450 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
452 // do we have such image?
453 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
455 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
457 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
460 // all the images associated with the item
461 int m_images
[wxTreeItemIcon_Max
];
463 // the real client data
464 wxTreeItemData
*m_data
;
466 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 #if wxUSE_EXTENDED_RTTI
474 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
476 WX_BEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
477 WX_END_PROPERTIES_TABLE()
479 WX_BEGIN_HANDLERS_TABLE(wxTreeCtrl
)
480 WX_END_HANDLERS_TABLE()
482 WX_CONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
484 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 // indices in gs_expandEvents table below
506 // handy table for sending events - it has to be initialized during run-time
507 // now so can't be const any more
508 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
511 but logically it's a const table with the following entries:
514 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
515 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
519 // ============================================================================
521 // ============================================================================
523 // ----------------------------------------------------------------------------
525 // ----------------------------------------------------------------------------
527 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
529 if ( !OnVisit(root
) )
532 return Traverse(root
, recursively
);
535 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
537 wxTreeItemIdValue cookie
;
538 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
539 while ( child
.IsOk() )
541 // depth first traversal
542 if ( recursively
&& !Traverse(child
, true) )
545 if ( !OnVisit(child
) )
548 child
= m_tree
->GetNextChild(root
, cookie
);
554 // ----------------------------------------------------------------------------
555 // construction and destruction
556 // ----------------------------------------------------------------------------
558 void wxTreeCtrl::Init()
560 m_imageListNormal
= NULL
;
561 m_imageListState
= NULL
;
562 m_ownsImageListNormal
= m_ownsImageListState
= false;
564 m_hasAnyAttr
= false;
566 m_pVirtualRoot
= NULL
;
568 // initialize the global array of events now as it can't be done statically
569 // with the wxEVT_XXX values being allocated during run-time only
570 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
571 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
572 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
573 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
576 bool wxTreeCtrl::Create(wxWindow
*parent
,
581 const wxValidator
& validator
,
582 const wxString
& name
)
586 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
587 style
|= wxBORDER_SUNKEN
;
589 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
593 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
594 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
596 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
597 wstyle
|= TVS_HASLINES
;
598 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
599 wstyle
|= TVS_HASBUTTONS
;
601 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
602 wstyle
|= TVS_EDITLABELS
;
604 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
605 wstyle
|= TVS_LINESATROOT
;
607 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
609 if ( wxTheApp
->GetComCtl32Version() >= 471 )
610 wstyle
|= TVS_FULLROWSELECT
;
613 // using TVS_CHECKBOXES for emulation of a multiselection tree control
614 // doesn't work without the new enough headers
615 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
616 !defined( __GNUWIN32_OLD__ ) && \
617 !defined( __BORLANDC__ ) && \
618 !defined( __WATCOMC__ ) && \
619 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
621 // we emulate the multiple selection tree controls by using checkboxes: set
622 // up the image list we need for this if we do have multiple selections
623 if ( m_windowStyle
& wxTR_MULTIPLE
)
624 wstyle
|= TVS_CHECKBOXES
;
625 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
627 // Create the tree control.
628 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
631 #if wxUSE_COMCTL32_SAFELY
632 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
633 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
635 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
636 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
638 // This works around a bug in the Windows tree control whereby for some versions
639 // of comctrl32, setting any colour actually draws the background in black.
640 // This will initialise the background to the system colour.
641 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
642 // Assume the user has an updated comctl32.dll.
643 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
644 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
645 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
649 // VZ: this is some experimental code which may be used to get the
650 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
651 // AFAIK, the standard DLL does about the same thing anyhow.
653 if ( m_windowStyle
& wxTR_MULTIPLE
)
657 // create the DC compatible with the current screen
658 HDC hdcMem
= CreateCompatibleDC(NULL
);
660 // create a mono bitmap of the standard size
661 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
662 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
663 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
664 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
665 1, // # of color planes
666 1, // # bits needed for one pixel
667 0); // array containing colour data
668 SelectObject(hdcMem
, hbmpCheck
);
670 // then draw a check mark into it
671 RECT rect
= { 0, 0, x
, y
};
672 if ( !::DrawFrameControl(hdcMem
, &rect
,
674 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
676 wxLogLastError(wxT("DrawFrameControl(check)"));
679 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
680 imagelistCheckboxes
.Add(bmp
);
682 if ( !::DrawFrameControl(hdcMem
, &rect
,
686 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
689 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
690 imagelistCheckboxes
.Add(bmp
);
696 SetStateImageList(&imagelistCheckboxes
);
700 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
705 wxTreeCtrl::~wxTreeCtrl()
707 // delete any attributes
710 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
712 // prevent TVN_DELETEITEM handler from deleting the attributes again!
713 m_hasAnyAttr
= false;
718 // delete user data to prevent memory leaks
719 // also deletes hidden root node storage.
722 if (m_ownsImageListNormal
) delete m_imageListNormal
;
723 if (m_ownsImageListState
) delete m_imageListState
;
726 // ----------------------------------------------------------------------------
728 // ----------------------------------------------------------------------------
730 // simple wrappers which add error checking in debug mode
732 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
734 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
735 _T("can't retrieve virtual root item") );
737 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
739 wxLogLastError(wxT("TreeView_GetItem"));
747 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
749 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
751 wxLogLastError(wxT("TreeView_SetItem"));
755 size_t wxTreeCtrl::GetCount() const
757 return (size_t)TreeView_GetCount(GetHwnd());
760 unsigned int wxTreeCtrl::GetIndent() const
762 return TreeView_GetIndent(GetHwnd());
765 void wxTreeCtrl::SetIndent(unsigned int indent
)
767 TreeView_SetIndent(GetHwnd(), indent
);
770 wxImageList
*wxTreeCtrl::GetImageList() const
772 return m_imageListNormal
;
775 wxImageList
*wxTreeCtrl::GetStateImageList() const
777 return m_imageListState
;
780 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
783 TreeView_SetImageList(GetHwnd(),
784 imageList
? imageList
->GetHIMAGELIST() : 0,
788 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
790 if (m_ownsImageListNormal
)
791 delete m_imageListNormal
;
793 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
794 m_ownsImageListNormal
= false;
797 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
799 if (m_ownsImageListState
) delete m_imageListState
;
800 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
801 m_ownsImageListState
= false;
804 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
806 SetImageList(imageList
);
807 m_ownsImageListNormal
= true;
810 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
812 SetStateImageList(imageList
);
813 m_ownsImageListState
= true;
816 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
817 bool recursively
) const
819 TraverseCounter
counter(this, item
, recursively
);
821 return counter
.GetCount() - 1;
824 // ----------------------------------------------------------------------------
826 // ----------------------------------------------------------------------------
828 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
830 #if !wxUSE_COMCTL32_SAFELY
831 if ( !wxWindowBase::SetBackgroundColour(colour
) )
834 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
840 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
842 #if !wxUSE_COMCTL32_SAFELY
843 if ( !wxWindowBase::SetForegroundColour(colour
) )
846 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
852 // ----------------------------------------------------------------------------
854 // ----------------------------------------------------------------------------
856 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
858 wxChar buf
[512]; // the size is arbitrary...
860 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
861 tvItem
.pszText
= buf
;
862 tvItem
.cchTextMax
= WXSIZEOF(buf
);
863 if ( !DoGetItem(&tvItem
) )
865 // don't return some garbage which was on stack, but an empty string
869 return wxString(buf
);
872 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
874 if ( IS_VIRTUAL_ROOT(item
) )
877 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
878 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
881 // when setting the text of the item being edited, the text control should
882 // be updated to reflect the new text as well, otherwise calling
883 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
885 // don't use GetEditControl() here because m_textCtrl is not set yet
886 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
889 if ( item
== GetSelection() )
891 ::SetWindowText(hwndEdit
, text
);
896 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
897 wxTreeItemIcon which
) const
899 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
900 if ( !DoGetItem(&tvItem
) )
905 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
908 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
910 wxTreeItemIcon which
) const
912 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
913 if ( !DoGetItem(&tvItem
) )
918 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
920 data
->SetImage(image
, which
);
922 // make sure that we have selected images as well
923 if ( which
== wxTreeItemIcon_Normal
&&
924 !data
->HasImage(wxTreeItemIcon_Selected
) )
926 data
->SetImage(image
, wxTreeItemIcon_Selected
);
929 if ( which
== wxTreeItemIcon_Expanded
&&
930 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
932 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
936 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
940 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
941 tvItem
.iSelectedImage
= imageSel
;
942 tvItem
.iImage
= image
;
946 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
947 wxTreeItemIcon which
) const
949 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
951 // TODO: Maybe a hidden root can still provide images?
955 if ( HasIndirectData(item
) )
957 return DoGetItemImageFromData(item
, which
);
964 wxFAIL_MSG( wxT("unknown tree item image type") );
966 case wxTreeItemIcon_Normal
:
970 case wxTreeItemIcon_Selected
:
971 mask
= TVIF_SELECTEDIMAGE
;
974 case wxTreeItemIcon_Expanded
:
975 case wxTreeItemIcon_SelectedExpanded
:
979 wxTreeViewItem
tvItem(item
, mask
);
982 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
985 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
986 wxTreeItemIcon which
)
988 if ( IS_VIRTUAL_ROOT(item
) )
990 // TODO: Maybe a hidden root can still store images?
1000 wxFAIL_MSG( wxT("unknown tree item image type") );
1003 case wxTreeItemIcon_Normal
:
1005 const int imageNormalOld
= GetItemImage(item
);
1006 const int imageSelOld
=
1007 GetItemImage(item
, wxTreeItemIcon_Selected
);
1009 // always set the normal image
1010 imageNormal
= image
;
1012 // if the selected and normal images were the same, they should
1013 // be the same after the update, otherwise leave the selected
1015 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1019 case wxTreeItemIcon_Selected
:
1020 imageNormal
= GetItemImage(item
);
1024 case wxTreeItemIcon_Expanded
:
1025 case wxTreeItemIcon_SelectedExpanded
:
1026 if ( !HasIndirectData(item
) )
1028 // we need to get the old images first, because after we create
1029 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1031 imageNormal
= GetItemImage(item
);
1032 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1034 // if it doesn't have it yet, add it
1035 wxTreeItemIndirectData
*data
= new
1036 wxTreeItemIndirectData(this, item
);
1038 // copy the data to the new location
1039 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1040 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1043 DoSetItemImageFromData(item
, image
, which
);
1045 // reset the normal/selected images because we won't use them any
1046 // more - now they're stored inside the indirect data
1048 imageSel
= I_IMAGECALLBACK
;
1052 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1053 // change both normal and selected image - otherwise the change simply
1054 // doesn't take place!
1055 DoSetItemImages(item
, imageNormal
, imageSel
);
1058 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1060 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1062 // Hidden root may have data.
1063 if ( IS_VIRTUAL_ROOT(item
) )
1065 return GET_VIRTUAL_ROOT()->GetData();
1069 if ( !DoGetItem(&tvItem
) )
1074 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1075 if ( IsDataIndirect(data
) )
1077 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1083 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1085 if ( IS_VIRTUAL_ROOT(item
) )
1087 GET_VIRTUAL_ROOT()->SetData(data
);
1090 // first, associate this piece of data with this item
1096 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1098 if ( HasIndirectData(item
) )
1100 if ( DoGetItem(&tvItem
) )
1102 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1106 wxFAIL_MSG( wxT("failed to change tree items data") );
1111 tvItem
.lParam
= (LPARAM
)data
;
1116 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1117 wxTreeItemIndirectData
*data
)
1119 // this should never happen because it's unnecessary and will probably lead
1120 // to crash too because the code elsewhere supposes that the pointer the
1121 // wxTreeItemIndirectData has is a real wxItemData and not
1122 // wxTreeItemIndirectData as well
1123 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1125 SetItemData(item
, data
);
1128 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1130 // query the item itself
1131 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1132 if ( !DoGetItem(&tvItem
) )
1137 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1139 return data
&& IsDataIndirect(data
);
1142 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1144 if ( IS_VIRTUAL_ROOT(item
) )
1147 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1148 tvItem
.cChildren
= (int)has
;
1152 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1154 if ( IS_VIRTUAL_ROOT(item
) )
1157 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1158 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1162 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1164 if ( IS_VIRTUAL_ROOT(item
) )
1167 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1168 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1172 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1174 if ( IS_VIRTUAL_ROOT(item
) )
1178 if ( GetBoundingRect(item
, rect
) )
1184 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1186 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1188 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1191 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1193 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1195 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1198 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1200 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1202 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1205 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1206 const wxColour
& col
)
1208 wxTreeItemAttr
*attr
;
1209 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1210 if ( it
== m_attrs
.end() )
1212 m_hasAnyAttr
= true;
1214 m_attrs
[item
.m_pItem
] =
1215 attr
= new wxTreeItemAttr
;
1222 attr
->SetTextColour(col
);
1227 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1228 const wxColour
& col
)
1230 wxTreeItemAttr
*attr
;
1231 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1232 if ( it
== m_attrs
.end() )
1234 m_hasAnyAttr
= true;
1236 m_attrs
[item
.m_pItem
] =
1237 attr
= new wxTreeItemAttr
;
1239 else // already in the hash
1244 attr
->SetBackgroundColour(col
);
1249 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1251 wxTreeItemAttr
*attr
;
1252 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1253 if ( it
== m_attrs
.end() )
1255 m_hasAnyAttr
= true;
1257 m_attrs
[item
.m_pItem
] =
1258 attr
= new wxTreeItemAttr
;
1260 else // already in the hash
1265 attr
->SetFont(font
);
1270 // ----------------------------------------------------------------------------
1272 // ----------------------------------------------------------------------------
1274 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1276 if ( item
== wxTreeItemId(TVI_ROOT
) )
1278 // virtual (hidden) root is never visible
1282 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1285 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1286 // the HTREEITEM with TVM_GETITEMRECT
1287 *(HTREEITEM
*)&rect
= HITEM(item
);
1289 // false means get item rect for the whole item, not only text
1290 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1293 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1295 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1298 return tvItem
.cChildren
!= 0;
1301 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1303 // probably not a good idea to put it here
1304 //wxASSERT( ItemHasChildren(item) );
1306 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1309 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1312 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1314 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1317 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1320 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1322 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1325 return (tvItem
.state
& TVIS_BOLD
) != 0;
1328 // ----------------------------------------------------------------------------
1330 // ----------------------------------------------------------------------------
1332 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1334 // Root may be real (visible) or virtual (hidden).
1335 if ( GET_VIRTUAL_ROOT() )
1338 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1341 wxTreeItemId
wxTreeCtrl::GetSelection() const
1343 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1344 wxT("this only works with single selection controls") );
1346 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1349 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1353 if ( IS_VIRTUAL_ROOT(item
) )
1355 // no parent for the virtual root
1360 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1361 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1363 // the top level items should have the virtual root as their parent
1368 return wxTreeItemId(hItem
);
1371 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1372 wxTreeItemIdValue
& cookie
) const
1374 // remember the last child returned in 'cookie'
1375 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1377 return wxTreeItemId(cookie
);
1380 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1381 wxTreeItemIdValue
& cookie
) const
1383 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1384 HITEM(wxTreeItemId(cookie
))));
1385 cookie
= item
.m_pItem
;
1390 #if WXWIN_COMPATIBILITY_2_4
1392 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1395 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1397 return wxTreeItemId((void *)cookie
);
1400 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1403 wxTreeItemId
item(TreeView_GetNextSibling
1406 HITEM(wxTreeItemId((void *)cookie
)
1408 cookie
= (long)item
.m_pItem
;
1413 #endif // WXWIN_COMPATIBILITY_2_4
1415 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1417 // can this be done more efficiently?
1418 wxTreeItemIdValue cookie
;
1420 wxTreeItemId childLast
,
1421 child
= GetFirstChild(item
, cookie
);
1422 while ( child
.IsOk() )
1425 child
= GetNextChild(item
, cookie
);
1431 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1433 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1436 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1438 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1441 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1443 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1446 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1448 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1450 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1453 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1455 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1457 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1460 // ----------------------------------------------------------------------------
1461 // multiple selections emulation
1462 // ----------------------------------------------------------------------------
1464 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1466 // receive the desired information.
1467 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1470 // state image indices are 1 based
1471 return ((tvItem
.state
>> 12) - 1) == 1;
1474 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1476 // receive the desired information.
1477 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1481 // state images are one-based
1482 tvItem
.state
= (check
? 2 : 1) << 12;
1487 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1489 TraverseSelections
selector(this, selections
);
1491 return selector
.GetCount();
1494 // ----------------------------------------------------------------------------
1496 // ----------------------------------------------------------------------------
1498 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1499 wxTreeItemId hInsertAfter
,
1500 const wxString
& text
,
1501 int image
, int selectedImage
,
1502 wxTreeItemData
*data
)
1504 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1506 _T("can't have more than one root in the tree") );
1508 TV_INSERTSTRUCT tvIns
;
1509 tvIns
.hParent
= HITEM(parent
);
1510 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1512 // this is how we insert the item as the first child: supply a NULL
1514 if ( !tvIns
.hInsertAfter
)
1516 tvIns
.hInsertAfter
= TVI_FIRST
;
1520 if ( !text
.IsEmpty() )
1523 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1527 tvIns
.item
.pszText
= NULL
;
1528 tvIns
.item
.cchTextMax
= 0;
1534 tvIns
.item
.iImage
= image
;
1536 if ( selectedImage
== -1 )
1538 // take the same image for selected icon if not specified
1539 selectedImage
= image
;
1543 if ( selectedImage
!= -1 )
1545 mask
|= TVIF_SELECTEDIMAGE
;
1546 tvIns
.item
.iSelectedImage
= selectedImage
;
1552 tvIns
.item
.lParam
= (LPARAM
)data
;
1555 tvIns
.item
.mask
= mask
;
1557 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1560 wxLogLastError(wxT("TreeView_InsertItem"));
1565 // associate the application tree item with Win32 tree item handle
1569 return wxTreeItemId(id
);
1572 // for compatibility only
1573 #if WXWIN_COMPATIBILITY_2_4
1575 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1576 const wxString
& text
,
1577 int image
, int selImage
,
1580 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1581 image
, selImage
, NULL
);
1584 #endif // WXWIN_COMPATIBILITY_2_4
1586 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1587 int image
, int selectedImage
,
1588 wxTreeItemData
*data
)
1591 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1593 // create a virtual root item, the parent for all the others
1594 m_pVirtualRoot
= new wxVirtualNode(data
);
1599 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1600 text
, image
, selectedImage
, data
);
1603 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1604 const wxString
& text
,
1605 int image
, int selectedImage
,
1606 wxTreeItemData
*data
)
1608 return DoInsertItem(parent
, TVI_FIRST
,
1609 text
, image
, selectedImage
, data
);
1612 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1613 const wxTreeItemId
& idPrevious
,
1614 const wxString
& text
,
1615 int image
, int selectedImage
,
1616 wxTreeItemData
*data
)
1618 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1621 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1623 const wxString
& text
,
1624 int image
, int selectedImage
,
1625 wxTreeItemData
*data
)
1627 // find the item from index
1628 wxTreeItemIdValue cookie
;
1629 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1630 while ( index
!= 0 && idCur
.IsOk() )
1635 idCur
= GetNextChild(parent
, cookie
);
1638 // assert, not check: if the index is invalid, we will append the item
1640 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1642 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1645 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1646 const wxString
& text
,
1647 int image
, int selectedImage
,
1648 wxTreeItemData
*data
)
1650 return DoInsertItem(parent
, TVI_LAST
,
1651 text
, image
, selectedImage
, data
);
1654 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1656 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1658 wxLogLastError(wxT("TreeView_DeleteItem"));
1662 // delete all children (but don't delete the item itself)
1663 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1665 wxTreeItemIdValue cookie
;
1667 wxArrayTreeItemIds children
;
1668 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1669 while ( child
.IsOk() )
1671 children
.Add(child
);
1673 child
= GetNextChild(item
, cookie
);
1676 size_t nCount
= children
.Count();
1677 for ( size_t n
= 0; n
< nCount
; n
++ )
1679 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1681 wxLogLastError(wxT("TreeView_DeleteItem"));
1686 void wxTreeCtrl::DeleteAllItems()
1688 // delete the "virtual" root item.
1689 if ( GET_VIRTUAL_ROOT() )
1691 delete GET_VIRTUAL_ROOT();
1692 m_pVirtualRoot
= NULL
;
1695 // and all the real items
1697 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1699 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1703 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1705 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1706 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1707 flag
== TVE_EXPAND
||
1709 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1711 // A hidden root can be neither expanded nor collapsed.
1712 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1713 wxT("Can't expand/collapse hidden root node!") )
1715 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1716 // emulate them. This behaviour has changed slightly with comctl32.dll
1717 // v 4.70 - now it does send them but only the first time. To maintain
1718 // compatible behaviour and also in order to not have surprises with the
1719 // future versions, don't rely on this and still do everything ourselves.
1720 // To avoid that the messages be sent twice when the item is expanded for
1721 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1723 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1727 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1729 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1730 event
.m_item
= item
;
1731 event
.SetEventObject(this);
1733 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1735 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1739 (void)GetEventHandler()->ProcessEvent(event
);
1741 //else: change didn't took place, so do nothing at all
1744 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1746 DoExpand(item
, TVE_EXPAND
);
1749 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1751 DoExpand(item
, TVE_COLLAPSE
);
1754 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1756 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1759 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1761 DoExpand(item
, TVE_TOGGLE
);
1764 #if WXWIN_COMPATIBILITY_2_4
1765 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1767 DoExpand(item
, action
);
1771 void wxTreeCtrl::Unselect()
1773 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1774 wxT("doesn't make sense, may be you want UnselectAll()?") );
1776 // just remove the selection
1777 SelectItem(wxTreeItemId());
1780 void wxTreeCtrl::UnselectAll()
1782 if ( m_windowStyle
& wxTR_MULTIPLE
)
1784 wxArrayTreeItemIds selections
;
1785 size_t count
= GetSelections(selections
);
1786 for ( size_t n
= 0; n
< count
; n
++ )
1788 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1789 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1790 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1791 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1792 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1797 // just remove the selection
1802 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1804 if ( m_windowStyle
& wxTR_MULTIPLE
)
1806 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1807 // selecting the item means checking it
1809 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1810 ::SelectItem(GetHwnd(), HITEM(item
));
1811 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1815 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1816 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1817 // send them ourselves
1819 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1820 event
.m_item
= item
;
1821 event
.SetEventObject(this);
1823 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1824 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1826 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1828 wxLogLastError(wxT("TreeView_SelectItem"));
1832 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1833 (void)GetEventHandler()->ProcessEvent(event
);
1836 //else: program vetoed the change
1840 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1843 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1846 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1848 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1850 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1854 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1859 void wxTreeCtrl::DeleteTextCtrl()
1863 // the HWND corresponding to this control is deleted by the tree
1864 // control itself and we don't know when exactly this happens, so check
1865 // if the window still exists before calling UnsubclassWin()
1866 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1868 m_textCtrl
->SetHWND(0);
1871 m_textCtrl
->UnsubclassWin();
1872 m_textCtrl
->SetHWND(0);
1878 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1879 wxClassInfo
* textControlClass
)
1881 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1885 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1886 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1888 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1897 // textctrl is subclassed in MSWOnNotify
1901 // End label editing, optionally cancelling the edit
1902 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1904 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1909 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1911 TV_HITTESTINFO hitTestInfo
;
1912 hitTestInfo
.pt
.x
= (int)point
.x
;
1913 hitTestInfo
.pt
.y
= (int)point
.y
;
1915 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1920 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1921 flags |= wxTREE_HITTEST_##flag
1923 TRANSLATE_FLAG(ABOVE
);
1924 TRANSLATE_FLAG(BELOW
);
1925 TRANSLATE_FLAG(NOWHERE
);
1926 TRANSLATE_FLAG(ONITEMBUTTON
);
1927 TRANSLATE_FLAG(ONITEMICON
);
1928 TRANSLATE_FLAG(ONITEMINDENT
);
1929 TRANSLATE_FLAG(ONITEMLABEL
);
1930 TRANSLATE_FLAG(ONITEMRIGHT
);
1931 TRANSLATE_FLAG(ONITEMSTATEICON
);
1932 TRANSLATE_FLAG(TOLEFT
);
1933 TRANSLATE_FLAG(TORIGHT
);
1935 #undef TRANSLATE_FLAG
1937 return wxTreeItemId(hitTestInfo
.hItem
);
1940 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1942 bool textOnly
) const
1946 // Virtual root items have no bounding rectangle
1947 if ( IS_VIRTUAL_ROOT(item
) )
1952 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1955 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1961 // couldn't retrieve rect: for example, item isn't visible
1966 // ----------------------------------------------------------------------------
1968 // ----------------------------------------------------------------------------
1970 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1971 // functions such as IsDataIndirect()
1972 class wxTreeSortHelper
1975 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1978 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
1980 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
1981 if ( tree
->IsDataIndirect(data
) )
1983 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1986 return data
->GetId();
1990 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1994 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1995 wxT("sorting tree without data doesn't make sense") );
1997 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1999 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2000 GetIdFromData(tree
, pItem2
));
2003 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2004 const wxTreeItemId
& item2
)
2006 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2009 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2011 // rely on the fact that TreeView_SortChildren does the same thing as our
2012 // default behaviour, i.e. sorts items alphabetically and so call it
2013 // directly if we're not in derived class (much more efficient!)
2014 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2016 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2021 tvSort
.hParent
= HITEM(item
);
2022 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2023 tvSort
.lParam
= (LPARAM
)this;
2024 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2028 // ----------------------------------------------------------------------------
2030 // ----------------------------------------------------------------------------
2032 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2034 if ( cmd
== EN_UPDATE
)
2036 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2037 event
.SetEventObject( this );
2038 ProcessCommand(event
);
2040 else if ( cmd
== EN_KILLFOCUS
)
2042 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2043 event
.SetEventObject( this );
2044 ProcessCommand(event
);
2052 // command processed
2056 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2057 // only do it during dragging, minimize wxWin overhead (this is important for
2058 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2059 // instead of passing by wxWin events
2060 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2062 bool processed
= false;
2064 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2066 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2068 // we only process mouse messages here and these parameters have the
2069 // same meaning for all of them
2070 int x
= GET_X_LPARAM(lParam
),
2071 y
= GET_Y_LPARAM(lParam
);
2072 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2076 case WM_RBUTTONDOWN
:
2077 // if the item we are about to right click on
2078 // is not already select, remove the entire
2079 // previous selection
2080 if (!::IsItemSelected(GetHwnd(), htItem
))
2085 // select item and set the focus to the
2086 // newly selected item
2087 ::SelectItem(GetHwnd(), htItem
);
2088 ::SetFocus(GetHwnd(), htItem
);
2091 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2092 case WM_LBUTTONDOWN
:
2093 if ( htItem
&& isMultiple
)
2095 if ( wParam
& MK_CONTROL
)
2099 // toggle selected state
2100 ToggleItemSelection(GetHwnd(), htItem
);
2102 ::SetFocus(GetHwnd(), htItem
);
2104 // reset on any click without Shift
2105 m_htSelStart
.Unset();
2109 else if ( wParam
& MK_SHIFT
)
2111 // this selects all items between the starting one and
2114 if ( !m_htSelStart
)
2116 // take the focused item
2117 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2120 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2121 !(wParam
& MK_CONTROL
));
2123 ::SetFocus(GetHwnd(), htItem
);
2127 else // normal click
2129 // avoid doing anything if we click on the only
2130 // currently selected item
2132 wxArrayTreeItemIds selections
;
2133 size_t count
= GetSelections(selections
);
2136 HITEM_PTR(selections
[0]) != htItem
)
2138 // clear the previously selected items, if the
2139 // user clicked outside of the present selection.
2140 // otherwise, perform the deselection on mouse-up.
2141 // this allows multiple drag and drop to work.
2143 if (IsItemSelected(GetHwnd(), htItem
))
2145 ::SetFocus(GetHwnd(), htItem
);
2151 // prevent the click from starting in-place editing
2152 // which should only happen if we click on the
2153 // already selected item (and nothing else is
2156 TreeView_SelectItem(GetHwnd(), 0);
2157 ::SelectItem(GetHwnd(), htItem
);
2161 // reset on any click without Shift
2162 m_htSelStart
.Unset();
2166 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2171 m_dragImage
->Move(wxPoint(x
, y
));
2174 // highlight the item as target (hiding drag image is
2175 // necessary - otherwise the display will be corrupted)
2176 m_dragImage
->Hide();
2177 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2178 m_dragImage
->Show();
2185 // facilitates multiple drag-and-drop
2186 if (htItem
&& isMultiple
)
2188 wxArrayTreeItemIds selections
;
2189 size_t count
= GetSelections(selections
);
2192 !(wParam
& MK_CONTROL
) &&
2193 !(wParam
& MK_SHIFT
))
2196 TreeView_SelectItem(GetHwnd(), htItem
);
2205 m_dragImage
->EndDrag();
2209 // generate the drag end event
2210 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2212 event
.m_item
= htItem
;
2213 event
.m_pointDrag
= wxPoint(x
, y
);
2214 event
.SetEventObject(this);
2216 (void)GetEventHandler()->ProcessEvent(event
);
2218 // if we don't do it, the tree seems to think that 2 items
2219 // are selected simultaneously which is quite weird
2220 TreeView_SelectDropTarget(GetHwnd(), 0);
2225 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2226 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2228 // the tree control greys out the selected item when it loses focus and
2229 // paints it as selected again when it regains it, but it won't do it
2230 // for the other items itself - help it
2231 wxArrayTreeItemIds selections
;
2232 size_t count
= GetSelections(selections
);
2234 for ( size_t n
= 0; n
< count
; n
++ )
2236 // TreeView_GetItemRect() will return false if item is not visible,
2237 // which may happen perfectly well
2238 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2241 ::InvalidateRect(GetHwnd(), &rect
, false);
2245 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2247 bool bCtrl
= wxIsCtrlDown(),
2248 bShift
= wxIsShiftDown();
2250 // we handle.arrows and space, but not page up/down and home/end: the
2251 // latter should be easy, but not the former
2253 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2254 if ( !m_htSelStart
)
2256 m_htSelStart
= htSel
;
2259 if ( wParam
== VK_SPACE
)
2263 ToggleItemSelection(GetHwnd(), htSel
);
2269 ::SelectItem(GetHwnd(), htSel
);
2274 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2276 if ( !bCtrl
&& !bShift
)
2278 // no modifiers, just clear selection and then let the default
2279 // processing to take place
2284 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2286 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2287 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2288 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2292 // at the top/bottom
2298 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2302 // without changing selection
2303 ::SetFocus(GetHwnd(), htNext
);
2310 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2311 else if ( nMsg
== WM_CHAR
)
2313 // don't let the control process Space and Return keys because it
2314 // doesn't do anything useful with them anyhow but always beeps
2315 // annoyingly when it receives them and there is no way to turn it off
2316 // simply if you just process TREEITEM_ACTIVATED event to which Space
2317 // and Enter presses are mapped in your code
2318 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2325 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2330 // process WM_NOTIFY Windows message
2331 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2333 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2334 wxEventType eventType
= wxEVT_NULL
;
2335 NMHDR
*hdr
= (NMHDR
*)lParam
;
2337 switch ( hdr
->code
)
2340 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2343 case TVN_BEGINRDRAG
:
2345 if ( eventType
== wxEVT_NULL
)
2346 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2347 //else: left drag, already set above
2349 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2351 event
.m_item
= tv
->itemNew
.hItem
;
2352 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2354 // don't allow dragging by default: the user code must
2355 // explicitly say that it wants to allow it to avoid breaking
2361 case TVN_BEGINLABELEDIT
:
2363 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2364 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2366 event
.m_item
= info
->item
.hItem
;
2367 event
.m_label
= info
->item
.pszText
;
2368 event
.m_editCancelled
= false;
2372 case TVN_DELETEITEM
:
2374 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2375 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2377 event
.m_item
= tv
->itemOld
.hItem
;
2381 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2382 if ( it
!= m_attrs
.end() )
2391 case TVN_ENDLABELEDIT
:
2393 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2394 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2396 event
.m_item
= info
->item
.hItem
;
2397 event
.m_label
= info
->item
.pszText
;
2398 if (info
->item
.pszText
== NULL
)
2400 event
.m_editCancelled
= true;
2404 event
.m_editCancelled
= false;
2409 case TVN_GETDISPINFO
:
2410 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2413 case TVN_SETDISPINFO
:
2415 if ( eventType
== wxEVT_NULL
)
2416 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2417 //else: get, already set above
2419 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2421 event
.m_item
= info
->item
.hItem
;
2425 case TVN_ITEMEXPANDING
:
2426 case TVN_ITEMEXPANDED
:
2428 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2431 switch ( tv
->action
)
2434 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2442 what
= IDX_COLLAPSE
;
2446 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2449 eventType
= gs_expandEvents
[what
][how
];
2451 event
.m_item
= tv
->itemNew
.hItem
;
2457 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2458 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2460 // fabricate the lParam and wParam parameters sufficiently
2461 // similar to the ones from a "real" WM_KEYDOWN so that
2462 // CreateKeyEvent() works correctly
2464 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2466 WXWPARAM wParam
= info
->wVKey
;
2468 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2471 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2476 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2481 // a separate event for Space/Return
2482 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2483 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2485 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2487 event2
.SetEventObject(this);
2488 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2490 event2
.m_item
= GetSelection();
2492 //else: don't know how to get it
2494 (void)GetEventHandler()->ProcessEvent(event2
);
2499 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2500 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2501 // we have to handle both messages:
2502 case TVN_SELCHANGEDA
:
2503 case TVN_SELCHANGEDW
:
2504 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2507 case TVN_SELCHANGINGA
:
2508 case TVN_SELCHANGINGW
:
2510 if ( eventType
== wxEVT_NULL
)
2511 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2512 //else: already set above
2514 if (hdr
->code
== TVN_SELCHANGINGW
||
2515 hdr
->code
== TVN_SELCHANGEDW
)
2517 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2518 event
.m_item
= tv
->itemNew
.hItem
;
2519 event
.m_itemOld
= tv
->itemOld
.hItem
;
2523 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2524 event
.m_item
= tv
->itemNew
.hItem
;
2525 event
.m_itemOld
= tv
->itemOld
.hItem
;
2530 // instead of explicitly checking for _WIN32_IE, check if the
2531 // required symbols are available in the headers
2532 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2535 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2536 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2537 switch ( nmcd
.dwDrawStage
)
2540 // if we've got any items with non standard attributes,
2541 // notify us before painting each item
2542 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2546 case CDDS_ITEMPREPAINT
:
2548 wxMapTreeAttr::iterator
2549 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2551 if ( it
== m_attrs
.end() )
2553 // nothing to do for this item
2554 *result
= CDRF_DODEFAULT
;
2558 wxTreeItemAttr
* const attr
= it
->second
;
2561 if ( attr
->HasFont() )
2563 hFont
= GetHfontOf(attr
->GetFont());
2571 if ( attr
->HasTextColour() )
2573 colText
= attr
->GetTextColour();
2577 colText
= GetForegroundColour();
2580 // selection colours should override ours
2581 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2584 ::GetSysColor(COLOR_HIGHLIGHT
);
2586 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2591 if ( attr
->HasBackgroundColour() )
2593 colBack
= attr
->GetBackgroundColour();
2597 colBack
= GetBackgroundColour();
2600 lptvcd
->clrText
= wxColourToRGB(colText
);
2601 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2604 // note that if we wanted to set colours for
2605 // individual columns (subitems), we would have
2606 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2609 ::SelectObject(nmcd
.hdc
, hFont
);
2611 *result
= CDRF_NEWFONT
;
2615 *result
= CDRF_DODEFAULT
;
2621 *result
= CDRF_DODEFAULT
;
2625 // we always process it
2627 #endif // have owner drawn support in headers
2631 DWORD pos
= GetMessagePos();
2633 point
.x
= LOWORD(pos
);
2634 point
.y
= HIWORD(pos
);
2635 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2637 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2638 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2640 event
.m_item
= item
;
2641 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2649 TV_HITTESTINFO tvhti
;
2650 ::GetCursorPos(&tvhti
.pt
);
2651 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2652 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2654 if ( tvhti
.flags
& TVHT_ONITEM
)
2656 event
.m_item
= tvhti
.hItem
;
2657 eventType
= (int)hdr
->code
== NM_DBLCLK
2658 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2659 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2661 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2662 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2671 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2674 event
.SetEventObject(this);
2675 event
.SetEventType(eventType
);
2677 bool processed
= GetEventHandler()->ProcessEvent(event
);
2680 switch ( hdr
->code
)
2683 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2684 // the return code of this event handler as the return value for
2685 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2686 // expanded status would never work
2691 case TVN_BEGINRDRAG
:
2692 if ( event
.IsAllowed() )
2694 // normally this is impossible because the m_dragImage is
2695 // deleted once the drag operation is over
2696 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2698 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2699 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2700 m_dragImage
->Show();
2704 case TVN_DELETEITEM
:
2706 // NB: we might process this message using wxWindows event
2707 // tables, but due to overhead of wxWin event system we
2708 // prefer to do it here ourself (otherwise deleting a tree
2709 // with many items is just too slow)
2710 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2712 wxTreeItemId item
= event
.m_item
;
2713 if ( HasIndirectData(item
) )
2715 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2717 delete data
; // can't be NULL here
2721 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2722 delete data
; // may be NULL, ok
2725 processed
= true; // Make sure we don't get called twice
2729 case TVN_BEGINLABELEDIT
:
2730 // return true to cancel label editing
2731 *result
= !event
.IsAllowed();
2732 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2733 if(event
.IsAllowed())
2735 HWND hText
= TreeView_GetEditControl(GetHwnd());
2738 // MBN: if m_textCtrl already has an HWND, it is a stale
2739 // pointer from a previous edit (because the user
2740 // didn't modify the label before dismissing the control,
2741 // and TVN_ENDLABELEDIT was not sent), so delete it
2742 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2745 m_textCtrl
= new wxTextCtrl();
2746 m_textCtrl
->SetParent(this);
2747 m_textCtrl
->SetHWND((WXHWND
)hText
);
2748 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2750 // set wxTE_PROCESS_ENTER style for the text control to
2751 // force it to process the Enter presses itself, otherwise
2752 // they could be stolen from it by the dialog
2754 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2755 | wxTE_PROCESS_ENTER
);
2760 case TVN_ENDLABELEDIT
:
2761 // return true to set the label to the new string: note that we
2762 // also must pretend that we did process the message or it is going
2763 // to be passed to DefWindowProc() which will happily return false
2764 // cancelling the label change
2765 *result
= event
.IsAllowed();
2768 // ensure that we don't have the text ctrl which is going to be
2773 case TVN_SELCHANGING
:
2774 case TVN_ITEMEXPANDING
:
2775 // return true to prevent the action from happening
2776 *result
= !event
.IsAllowed();
2779 case TVN_ITEMEXPANDED
:
2780 // the item is not refreshed properly after expansion when it has
2781 // an image depending on the expanded/collapsed state - bug in
2782 // comctl32.dll or our code?
2784 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2785 wxTreeItemId
id(tv
->itemNew
.hItem
);
2787 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2795 case TVN_GETDISPINFO
:
2796 // NB: so far the user can't set the image himself anyhow, so do it
2797 // anyway - but this may change later
2798 //if ( /* !processed && */ 1 )
2800 wxTreeItemId item
= event
.m_item
;
2801 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2802 if ( info
->item
.mask
& TVIF_IMAGE
)
2805 DoGetItemImageFromData
2808 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2809 : wxTreeItemIcon_Normal
2812 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2814 info
->item
.iSelectedImage
=
2815 DoGetItemImageFromData
2818 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2819 : wxTreeItemIcon_Selected
2826 // for the other messages the return value is ignored and there is
2827 // nothing special to do
2832 // ----------------------------------------------------------------------------
2834 // ----------------------------------------------------------------------------
2836 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2837 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2839 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2842 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2843 tvi
.mask
= TVIF_STATE
;
2844 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2846 // Select the specified state, or -1 == cycle to the next one.
2849 TreeView_GetItem(GetHwnd(), &tvi
);
2851 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2852 if ( state
== m_imageListState
->GetImageCount() )
2856 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2857 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2859 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2861 TreeView_SetItem(GetHwnd(), &tvi
);
2864 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2867 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2868 tvi
.mask
= TVIF_STATE
;
2869 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2870 TreeView_GetItem(GetHwnd(), &tvi
);
2872 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2875 #endif // wxUSE_TREECTRL