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 // ----------------------------------------------------------------------------
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
39 // Mingw32 is a bit mental even though this is done in winundef
48 #if defined(__WIN95__)
51 #include "wx/dynarray.h"
52 #include "wx/imaglist.h"
53 #include "wx/settings.h"
54 #include "wx/msw/treectrl.h"
55 #include "wx/msw/dragimag.h"
57 #ifdef __GNUWIN32_OLD__
58 #include "wx/msw/gnuwin32/extra.h"
61 #if defined(__WIN95__) && !((defined(__GNUWIN32_OLD__) || defined(__TWIN32__)) && !defined(__CYGWIN10__))
65 // Bug in headers, sometimes
67 #define TVIS_FOCUSED 0x0001
71 #define TV_FIRST 0x1100
74 #ifndef TVS_CHECKBOXES
75 #define TVS_CHECKBOXES 0x0100
78 // old headers might miss these messages (comctl32.dll 4.71+ only)
79 #ifndef TVM_SETBKCOLOR
80 #define TVM_SETBKCOLOR (TV_FIRST + 29)
81 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
84 // a macro to hide the ugliness of nested casts
85 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
87 // the native control doesn't support multiple selections under MSW and we
88 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
89 // checkboxes be the selection status (checked == selected) or by really
90 // emulating everything, i.e. intercepting mouse and key events &c. The first
91 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
93 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 // wrapper for TreeView_HitTest
101 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
107 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
110 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
112 // wrappers for TreeView_GetItem/TreeView_SetItem
113 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
116 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
117 tvi
.stateMask
= TVIS_SELECTED
;
120 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
122 wxLogLastError(wxT("TreeView_GetItem"));
125 return (tvi
.state
& TVIS_SELECTED
) != 0;
128 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
131 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
132 tvi
.stateMask
= TVIS_SELECTED
;
133 tvi
.state
= select
? TVIS_SELECTED
: 0;
136 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
138 wxLogLastError(wxT("TreeView_SetItem"));
142 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
144 SelectItem(hwndTV
, htItem
, FALSE
);
147 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
149 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
152 // helper function which selects all items in a range and, optionally,
153 // unselects all others
154 static void SelectRange(HWND hwndTV
,
157 bool unselectOthers
= TRUE
)
159 // find the first (or last) item and select it
161 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
162 while ( htItem
&& cont
)
164 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
166 if ( !IsItemSelected(hwndTV
, htItem
) )
168 SelectItem(hwndTV
, htItem
);
175 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
177 UnselectItem(hwndTV
, htItem
);
181 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
184 // select the items in range
185 cont
= htFirst
!= htLast
;
186 while ( htItem
&& cont
)
188 if ( !IsItemSelected(hwndTV
, htItem
) )
190 SelectItem(hwndTV
, htItem
);
193 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
195 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
199 if ( unselectOthers
)
203 if ( IsItemSelected(hwndTV
, htItem
) )
205 UnselectItem(hwndTV
, htItem
);
208 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
212 // seems to be necessary - otherwise the just selected items don't always
213 // appear as selected
214 UpdateWindow(hwndTV
);
217 // helper function which tricks the standard control into changing the focused
218 // item without changing anything else (if someone knows why Microsoft doesn't
219 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
220 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
223 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
228 if ( htItem
!= htFocus
)
230 // remember the selection state of the item
231 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
233 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
235 // prevent the tree from unselecting the old focus which it
236 // would do by default (TreeView_SelectItem unselects the
238 TreeView_SelectItem(hwndTV
, 0);
239 SelectItem(hwndTV
, htFocus
);
242 TreeView_SelectItem(hwndTV
, htItem
);
246 // need to clear the selection which TreeView_SelectItem() gave
248 UnselectItem(hwndTV
, htItem
);
250 //else: was selected, still selected - ok
252 //else: nothing to do, focus already there
258 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
260 // just clear the focus
261 TreeView_SelectItem(hwndTV
, 0);
263 if ( wasFocusSelected
)
265 // restore the selection state
266 SelectItem(hwndTV
, htFocus
);
269 //else: nothing to do, no focus already
273 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
275 // ----------------------------------------------------------------------------
277 // ----------------------------------------------------------------------------
279 // a convenient wrapper around TV_ITEM struct which adds a ctor
281 #pragma warning( disable : 4097 ) // inheriting from typedef
284 struct wxTreeViewItem
: public TV_ITEM
286 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
287 UINT mask_
, // fields which are valid
288 UINT stateMask_
= 0) // for TVIF_STATE only
290 // hItem member is always valid
291 mask
= mask_
| TVIF_HANDLE
;
292 stateMask
= stateMask_
;
298 #pragma warning( default : 4097 )
301 // a class which encapsulates the tree traversal logic: it vists all (unless
302 // OnVisit() returns FALSE) items under the given one
303 class wxTreeTraversal
306 wxTreeTraversal(const wxTreeCtrl
*tree
)
311 // do traverse the tree: visit all items (recursively by default) under the
312 // given one; return TRUE if all items were traversed or FALSE if the
313 // traversal was aborted because OnVisit returned FALSE
314 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
316 // override this function to do whatever is needed for each item, return
317 // FALSE to stop traversing
318 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
321 const wxTreeCtrl
*GetTree() const { return m_tree
; }
324 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
326 const wxTreeCtrl
*m_tree
;
329 // internal class for getting the selected items
330 class TraverseSelections
: public wxTreeTraversal
333 TraverseSelections(const wxTreeCtrl
*tree
,
334 wxArrayTreeItemIds
& selections
)
335 : wxTreeTraversal(tree
), m_selections(selections
)
337 m_selections
.Empty();
339 DoTraverse(tree
->GetRootItem());
342 virtual bool OnVisit(const wxTreeItemId
& item
)
344 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
345 if ( GetTree()->IsItemChecked(item
) )
347 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
350 m_selections
.Add(item
);
356 size_t GetCount() const { return m_selections
.GetCount(); }
359 wxArrayTreeItemIds
& m_selections
;
362 // internal class for counting tree items
363 class TraverseCounter
: public wxTreeTraversal
366 TraverseCounter(const wxTreeCtrl
*tree
,
367 const wxTreeItemId
& root
,
369 : wxTreeTraversal(tree
)
373 DoTraverse(root
, recursively
);
376 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
383 size_t GetCount() const { return m_count
; }
389 // ----------------------------------------------------------------------------
390 // This class is needed for support of different images: the Win32 common
391 // control natively supports only 2 images (the normal one and another for the
392 // selected state). We wish to provide support for 2 more of them for folder
393 // items (i.e. those which have children): for expanded state and for expanded
394 // selected state. For this we use this structure to store the additional items
397 // There is only one problem with this: when we retrieve the item's data, we
398 // don't know whether we get a pointer to wxTreeItemData or
399 // wxTreeItemIndirectData. So we have to maintain a list of all items which
400 // have indirect data inside the listctrl itself.
401 // ----------------------------------------------------------------------------
403 class wxTreeItemIndirectData
406 // ctor associates this data with the item and the real item data becomes
407 // available through our GetData() method
408 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
410 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
416 m_data
= tree
->GetItemData(item
);
418 // and set ourselves as the new one
419 tree
->SetIndirectItemData(item
, this);
422 // dtor deletes the associated data as well
423 ~wxTreeItemIndirectData() { delete m_data
; }
426 // get the real data associated with the item
427 wxTreeItemData
*GetData() const { return m_data
; }
429 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
431 // do we have such image?
432 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
434 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
436 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
439 // all the images associated with the item
440 int m_images
[wxTreeItemIcon_Max
];
442 wxTreeItemData
*m_data
;
445 // ----------------------------------------------------------------------------
447 // ----------------------------------------------------------------------------
449 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
451 // ----------------------------------------------------------------------------
453 // ----------------------------------------------------------------------------
455 // handy table for sending events
456 static wxEventType g_events
[2][2] =
458 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
459 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
462 // ============================================================================
464 // ============================================================================
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
470 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
472 if ( !OnVisit(root
) )
475 return Traverse(root
, recursively
);
478 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
481 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
482 while ( child
.IsOk() )
484 // depth first traversal
485 if ( recursively
&& !Traverse(child
, TRUE
) )
488 if ( !OnVisit(child
) )
491 child
= m_tree
->GetNextChild(root
, cookie
);
497 // ----------------------------------------------------------------------------
498 // construction and destruction
499 // ----------------------------------------------------------------------------
501 void wxTreeCtrl::Init()
503 m_imageListNormal
= NULL
;
504 m_imageListState
= NULL
;
505 m_ownsImageListNormal
= m_ownsImageListState
= FALSE
;
507 m_hasAnyAttr
= FALSE
;
511 // Initialize static array of events, because with the new event system,
512 // they may not be initialized yet.
514 g_events
[0][0] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
515 g_events
[0][1] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
516 g_events
[1][0] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
517 g_events
[1][1] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
520 bool wxTreeCtrl::Create(wxWindow
*parent
,
525 const wxValidator
& validator
,
526 const wxString
& name
)
530 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
533 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
536 if ( m_windowStyle
& wxCLIP_SIBLINGS
)
537 wstyle
|= WS_CLIPSIBLINGS
;
539 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
540 wstyle
|= TVS_HASLINES
;
541 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
542 wstyle
|= TVS_HASBUTTONS
;
544 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
545 wstyle
|= TVS_EDITLABELS
;
547 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
548 wstyle
|= TVS_LINESATROOT
;
550 // using TVS_CHECKBOXES for emulation of a multiselection tree control
551 // doesn't work without the new enough headers
552 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
553 !defined( __GNUWIN32_OLD__ ) && \
554 !defined( __BORLANDC__ ) && \
555 !defined( __WATCOMC__ ) && \
556 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
558 // we emulate the multiple selection tree controls by using checkboxes: set
559 // up the image list we need for this if we do have multiple selections
560 if ( m_windowStyle
& wxTR_MULTIPLE
)
561 wstyle
|= TVS_CHECKBOXES
;
562 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
564 // Create the tree control.
565 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
568 #if wxUSE_COMCTL32_SAFELY
569 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
570 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
572 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
573 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
575 // This works around a bug in the Windows tree control whereby for some versions
576 // of comctrl32, setting any colour actually draws the background in black.
577 // This will initialise the background to the system colour.
578 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
579 // Assume the user has an updated comctl32.dll.
580 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
581 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
582 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
586 // VZ: this is some experimental code which may be used to get the
587 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
588 // AFAIK, the standard DLL does about the same thing anyhow.
590 if ( m_windowStyle
& wxTR_MULTIPLE
)
594 // create the DC compatible with the current screen
595 HDC hdcMem
= CreateCompatibleDC(NULL
);
597 // create a mono bitmap of the standard size
598 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
599 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
600 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
601 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
602 1, // # of color planes
603 1, // # bits needed for one pixel
604 0); // array containing colour data
605 SelectObject(hdcMem
, hbmpCheck
);
607 // then draw a check mark into it
608 RECT rect
= { 0, 0, x
, y
};
609 if ( !::DrawFrameControl(hdcMem
, &rect
,
611 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
613 wxLogLastError(wxT("DrawFrameControl(check)"));
616 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
617 imagelistCheckboxes
.Add(bmp
);
619 if ( !::DrawFrameControl(hdcMem
, &rect
,
623 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
626 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
627 imagelistCheckboxes
.Add(bmp
);
633 SetStateImageList(&imagelistCheckboxes
);
637 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
642 wxTreeCtrl::~wxTreeCtrl()
644 // delete any attributes
647 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
649 delete (wxTreeItemAttr
*)node
->Data();
652 // prevent TVN_DELETEITEM handler from deleting the attributes again!
653 m_hasAnyAttr
= FALSE
;
658 // delete user data to prevent memory leaks
661 if (m_ownsImageListNormal
) delete m_imageListNormal
;
662 if (m_ownsImageListState
) delete m_imageListState
;
665 // ----------------------------------------------------------------------------
667 // ----------------------------------------------------------------------------
669 // simple wrappers which add error checking in debug mode
671 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
673 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
675 wxLogLastError(wxT("TreeView_GetItem"));
683 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
685 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
687 wxLogLastError(wxT("TreeView_SetItem"));
691 size_t wxTreeCtrl::GetCount() const
693 return (size_t)TreeView_GetCount(GetHwnd());
696 unsigned int wxTreeCtrl::GetIndent() const
698 return TreeView_GetIndent(GetHwnd());
701 void wxTreeCtrl::SetIndent(unsigned int indent
)
703 TreeView_SetIndent(GetHwnd(), indent
);
706 wxImageList
*wxTreeCtrl::GetImageList() const
708 return m_imageListNormal
;
711 wxImageList
*wxTreeCtrl::GetStateImageList() const
713 return m_imageListNormal
;
716 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
719 TreeView_SetImageList(GetHwnd(),
720 imageList
? imageList
->GetHIMAGELIST() : 0,
724 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
726 if (m_ownsImageListNormal
) delete m_imageListNormal
;
727 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
728 m_ownsImageListNormal
= FALSE
;
731 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
733 if (m_ownsImageListState
) delete m_imageListState
;
734 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
735 m_ownsImageListState
= FALSE
;
738 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
740 SetImageList(imageList
);
741 m_ownsImageListNormal
= TRUE
;
744 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
746 SetStateImageList(imageList
);
747 m_ownsImageListState
= TRUE
;
750 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
751 bool recursively
) const
753 TraverseCounter
counter(this, item
, recursively
);
755 return counter
.GetCount() - 1;
758 // ----------------------------------------------------------------------------
760 // ----------------------------------------------------------------------------
762 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
764 #if !wxUSE_COMCTL32_SAFELY
765 if ( !wxWindowBase::SetBackgroundColour(colour
) )
768 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
774 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
776 #if !wxUSE_COMCTL32_SAFELY
777 if ( !wxWindowBase::SetForegroundColour(colour
) )
780 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
786 // ----------------------------------------------------------------------------
788 // ----------------------------------------------------------------------------
790 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
792 wxChar buf
[512]; // the size is arbitrary...
794 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
795 tvItem
.pszText
= buf
;
796 tvItem
.cchTextMax
= WXSIZEOF(buf
);
797 if ( !DoGetItem(&tvItem
) )
799 // don't return some garbage which was on stack, but an empty string
803 return wxString(buf
);
806 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
808 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
809 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
813 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
814 wxTreeItemIcon which
) const
816 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
817 if ( !DoGetItem(&tvItem
) )
822 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
825 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
827 wxTreeItemIcon which
) const
829 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
830 if ( !DoGetItem(&tvItem
) )
835 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
837 data
->SetImage(image
, which
);
839 // make sure that we have selected images as well
840 if ( which
== wxTreeItemIcon_Normal
&&
841 !data
->HasImage(wxTreeItemIcon_Selected
) )
843 data
->SetImage(image
, wxTreeItemIcon_Selected
);
846 if ( which
== wxTreeItemIcon_Expanded
&&
847 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
849 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
853 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
857 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
858 tvItem
.iSelectedImage
= imageSel
;
859 tvItem
.iImage
= image
;
863 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
864 wxTreeItemIcon which
) const
866 if ( HasIndirectData(item
) )
868 return DoGetItemImageFromData(item
, which
);
875 wxFAIL_MSG( wxT("unknown tree item image type") );
877 case wxTreeItemIcon_Normal
:
881 case wxTreeItemIcon_Selected
:
882 mask
= TVIF_SELECTEDIMAGE
;
885 case wxTreeItemIcon_Expanded
:
886 case wxTreeItemIcon_SelectedExpanded
:
890 wxTreeViewItem
tvItem(item
, mask
);
893 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
896 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
897 wxTreeItemIcon which
)
899 int imageNormal
, imageSel
;
903 wxFAIL_MSG( wxT("unknown tree item image type") );
905 case wxTreeItemIcon_Normal
:
907 imageSel
= GetItemSelectedImage(item
);
910 case wxTreeItemIcon_Selected
:
911 imageNormal
= GetItemImage(item
);
915 case wxTreeItemIcon_Expanded
:
916 case wxTreeItemIcon_SelectedExpanded
:
917 if ( !HasIndirectData(item
) )
919 // we need to get the old images first, because after we create
920 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
922 imageNormal
= GetItemImage(item
);
923 imageSel
= GetItemSelectedImage(item
);
925 // if it doesn't have it yet, add it
926 wxTreeItemIndirectData
*data
= new
927 wxTreeItemIndirectData(this, item
);
929 // copy the data to the new location
930 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
931 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
934 DoSetItemImageFromData(item
, image
, which
);
936 // reset the normal/selected images because we won't use them any
937 // more - now they're stored inside the indirect data
939 imageSel
= I_IMAGECALLBACK
;
943 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
944 // change both normal and selected image - otherwise the change simply
945 // doesn't take place!
946 DoSetItemImages(item
, imageNormal
, imageSel
);
949 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
951 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
952 if ( !DoGetItem(&tvItem
) )
957 if ( HasIndirectData(item
) )
959 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
963 return (wxTreeItemData
*)tvItem
.lParam
;
967 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
969 // first, associate this piece of data with this item
975 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
977 if ( HasIndirectData(item
) )
979 if ( DoGetItem(&tvItem
) )
981 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
985 wxFAIL_MSG( wxT("failed to change tree items data") );
990 tvItem
.lParam
= (LPARAM
)data
;
995 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
996 wxTreeItemIndirectData
*data
)
998 // this should never happen because it's unnecessary and will probably lead
999 // to crash too because the code elsewhere supposes that the pointer the
1000 // wxTreeItemIndirectData has is a real wxItemData and not
1001 // wxTreeItemIndirectData as well
1002 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1004 SetItemData(item
, (wxTreeItemData
*)data
);
1006 m_itemsWithIndirectData
.Add(item
);
1009 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1011 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
1014 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1016 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1017 tvItem
.cChildren
= (int)has
;
1021 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1023 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1024 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1028 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1030 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1031 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1035 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1036 const wxColour
& col
)
1038 m_hasAnyAttr
= TRUE
;
1040 long id
= (long)(WXHTREEITEM
)item
;
1041 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1044 attr
= new wxTreeItemAttr
;
1045 m_attrs
.Put(id
, (wxObject
*)attr
);
1048 attr
->SetTextColour(col
);
1052 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1053 const wxColour
& col
)
1055 m_hasAnyAttr
= TRUE
;
1057 long id
= (long)(WXHTREEITEM
)item
;
1058 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1061 attr
= new wxTreeItemAttr
;
1062 m_attrs
.Put(id
, (wxObject
*)attr
);
1065 attr
->SetBackgroundColour(col
);
1069 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1071 m_hasAnyAttr
= TRUE
;
1073 long id
= (long)(WXHTREEITEM
)item
;
1074 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1077 attr
= new wxTreeItemAttr
;
1078 m_attrs
.Put(id
, (wxObject
*)attr
);
1081 attr
->SetFont(font
);
1085 // ----------------------------------------------------------------------------
1087 // ----------------------------------------------------------------------------
1089 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1091 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1094 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1095 // the HTREEITEM with TVM_GETITEMRECT
1096 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1098 // FALSE means get item rect for the whole item, not only text
1099 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1103 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1105 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1108 return tvItem
.cChildren
!= 0;
1111 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1113 // probably not a good idea to put it here
1114 //wxASSERT( ItemHasChildren(item) );
1116 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1119 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1122 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1124 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1127 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1130 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1132 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1135 return (tvItem
.state
& TVIS_BOLD
) != 0;
1138 // ----------------------------------------------------------------------------
1140 // ----------------------------------------------------------------------------
1142 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1144 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1147 wxTreeItemId
wxTreeCtrl::GetSelection() const
1149 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1150 wxT("this only works with single selection controls") );
1152 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1155 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1157 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1160 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1161 long& _cookie
) const
1163 // remember the last child returned in 'cookie'
1164 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1166 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1169 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1170 long& _cookie
) const
1172 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1179 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1181 // can this be done more efficiently?
1184 wxTreeItemId childLast
,
1185 child
= GetFirstChild(item
, cookie
);
1186 while ( child
.IsOk() )
1189 child
= GetNextChild(item
, cookie
);
1195 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1197 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1200 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1202 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1205 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1207 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1210 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1212 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1214 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1217 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1219 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1221 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1224 // ----------------------------------------------------------------------------
1225 // multiple selections emulation
1226 // ----------------------------------------------------------------------------
1228 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1230 // receive the desired information.
1231 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1234 // state image indices are 1 based
1235 return ((tvItem
.state
>> 12) - 1) == 1;
1238 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1240 // receive the desired information.
1241 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1243 // state images are one-based
1244 tvItem
.state
= (check
? 2 : 1) << 12;
1249 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1251 TraverseSelections
selector(this, selections
);
1253 return selector
.GetCount();
1256 // ----------------------------------------------------------------------------
1258 // ----------------------------------------------------------------------------
1260 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1261 wxTreeItemId hInsertAfter
,
1262 const wxString
& text
,
1263 int image
, int selectedImage
,
1264 wxTreeItemData
*data
)
1266 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1268 _T("can't have more than one root in the tree") );
1270 TV_INSERTSTRUCT tvIns
;
1271 tvIns
.hParent
= HITEM(parent
);
1272 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1274 // this is how we insert the item as the first child: supply a NULL
1276 if ( !tvIns
.hInsertAfter
)
1278 tvIns
.hInsertAfter
= TVI_FIRST
;
1282 if ( !text
.IsEmpty() )
1285 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1289 tvIns
.item
.pszText
= NULL
;
1290 tvIns
.item
.cchTextMax
= 0;
1296 tvIns
.item
.iImage
= image
;
1298 if ( selectedImage
== -1 )
1300 // take the same image for selected icon if not specified
1301 selectedImage
= image
;
1305 if ( selectedImage
!= -1 )
1307 mask
|= TVIF_SELECTEDIMAGE
;
1308 tvIns
.item
.iSelectedImage
= selectedImage
;
1314 tvIns
.item
.lParam
= (LPARAM
)data
;
1317 tvIns
.item
.mask
= mask
;
1319 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1322 wxLogLastError(wxT("TreeView_InsertItem"));
1327 // associate the application tree item with Win32 tree item handle
1328 data
->SetId((WXHTREEITEM
)id
);
1331 return wxTreeItemId((WXHTREEITEM
)id
);
1334 // for compatibility only
1335 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1336 const wxString
& text
,
1337 int image
, int selImage
,
1340 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1341 image
, selImage
, NULL
);
1344 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1345 int image
, int selectedImage
,
1346 wxTreeItemData
*data
)
1348 return DoInsertItem(wxTreeItemId((long) (WXHTREEITEM
) 0), (long)(WXHTREEITEM
) 0,
1349 text
, image
, selectedImage
, data
);
1352 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1353 const wxString
& text
,
1354 int image
, int selectedImage
,
1355 wxTreeItemData
*data
)
1357 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1358 text
, image
, selectedImage
, data
);
1361 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1362 const wxTreeItemId
& idPrevious
,
1363 const wxString
& text
,
1364 int image
, int selectedImage
,
1365 wxTreeItemData
*data
)
1367 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1370 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1372 const wxString
& text
,
1373 int image
, int selectedImage
,
1374 wxTreeItemData
*data
)
1376 // find the item from index
1378 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1379 while ( index
!= 0 && idCur
.IsOk() )
1384 idCur
= GetNextChild(parent
, cookie
);
1387 // assert, not check: if the index is invalid, we will append the item
1389 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1391 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1394 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1395 const wxString
& text
,
1396 int image
, int selectedImage
,
1397 wxTreeItemData
*data
)
1399 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1400 text
, image
, selectedImage
, data
);
1403 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1405 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1407 wxLogLastError(wxT("TreeView_DeleteItem"));
1411 // delete all children (but don't delete the item itself)
1412 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1416 wxArrayLong children
;
1417 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1418 while ( child
.IsOk() )
1420 children
.Add((long)(WXHTREEITEM
)child
);
1422 child
= GetNextChild(item
, cookie
);
1425 size_t nCount
= children
.Count();
1426 for ( size_t n
= 0; n
< nCount
; n
++ )
1428 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1430 wxLogLastError(wxT("TreeView_DeleteItem"));
1435 void wxTreeCtrl::DeleteAllItems()
1437 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1439 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1443 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1445 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1446 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1447 flag
== TVE_EXPAND
||
1449 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1451 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1452 // emulate them. This behaviour has changed slightly with comctl32.dll
1453 // v 4.70 - now it does send them but only the first time. To maintain
1454 // compatible behaviour and also in order to not have surprises with the
1455 // future versions, don't rely on this and still do everything ourselves.
1456 // To avoid that the messages be sent twice when the item is expanded for
1457 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1459 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1463 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1465 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1466 event
.m_item
= item
;
1468 bool isExpanded
= IsExpanded(item
);
1470 event
.SetEventObject(this);
1472 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1473 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1474 GetEventHandler()->ProcessEvent(event
);
1476 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1477 GetEventHandler()->ProcessEvent(event
);
1479 //else: change didn't took place, so do nothing at all
1482 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1484 DoExpand(item
, TVE_EXPAND
);
1487 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1489 DoExpand(item
, TVE_COLLAPSE
);
1492 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1494 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1497 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1499 DoExpand(item
, TVE_TOGGLE
);
1502 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1504 DoExpand(item
, action
);
1507 void wxTreeCtrl::Unselect()
1509 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1510 wxT("doesn't make sense, may be you want UnselectAll()?") );
1512 // just remove the selection
1513 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1516 void wxTreeCtrl::UnselectAll()
1518 if ( m_windowStyle
& wxTR_MULTIPLE
)
1520 wxArrayTreeItemIds selections
;
1521 size_t count
= GetSelections(selections
);
1522 for ( size_t n
= 0; n
< count
; n
++ )
1524 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1525 SetItemCheck(selections
[n
], FALSE
);
1526 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1527 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1528 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1533 // just remove the selection
1538 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1540 if ( m_windowStyle
& wxTR_MULTIPLE
)
1542 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1543 // selecting the item means checking it
1545 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1546 ::SelectItem(GetHwnd(), HITEM(item
));
1547 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1551 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1552 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1553 // send them ourselves
1555 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1556 event
.m_item
= item
;
1557 event
.SetEventObject(this);
1559 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1560 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1562 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1564 wxLogLastError(wxT("TreeView_SelectItem"));
1568 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1569 (void)GetEventHandler()->ProcessEvent(event
);
1572 //else: program vetoed the change
1576 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1579 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1582 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1584 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1586 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1590 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1592 // normally, we could try to do something like this to return something
1593 // even when the editing was started by the user and not by calling
1594 // EditLabel() - but as nobody has asked for this so far and there might be
1595 // problems in the code below, I leave it disabled for now (VZ)
1599 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1602 m_textCtrl
= new wxTextCtrl(this, -1);
1604 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1606 //else: not editing label right now
1613 void wxTreeCtrl::DeleteTextCtrl()
1617 // the HWND corresponding to this control is deleted by the tree
1618 // control itself and we don't know when exactly this happens, so check
1619 // if the window still exists before calling UnsubclassWin()
1620 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1622 m_textCtrl
->SetHWND(0);
1625 m_textCtrl
->UnsubclassWin();
1626 m_textCtrl
->SetHWND(0);
1632 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1633 wxClassInfo
* textControlClass
)
1635 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1639 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1641 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1648 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1649 m_textCtrl
->SetParent(this);
1650 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1651 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1656 // End label editing, optionally cancelling the edit
1657 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1659 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1664 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1666 TV_HITTESTINFO hitTestInfo
;
1667 hitTestInfo
.pt
.x
= (int)point
.x
;
1668 hitTestInfo
.pt
.y
= (int)point
.y
;
1670 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1675 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1676 flags |= wxTREE_HITTEST_##flag
1678 TRANSLATE_FLAG(ABOVE
);
1679 TRANSLATE_FLAG(BELOW
);
1680 TRANSLATE_FLAG(NOWHERE
);
1681 TRANSLATE_FLAG(ONITEMBUTTON
);
1682 TRANSLATE_FLAG(ONITEMICON
);
1683 TRANSLATE_FLAG(ONITEMINDENT
);
1684 TRANSLATE_FLAG(ONITEMLABEL
);
1685 TRANSLATE_FLAG(ONITEMRIGHT
);
1686 TRANSLATE_FLAG(ONITEMSTATEICON
);
1687 TRANSLATE_FLAG(TOLEFT
);
1688 TRANSLATE_FLAG(TORIGHT
);
1690 #undef TRANSLATE_FLAG
1692 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1695 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1697 bool textOnly
) const
1700 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1703 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1709 // couldn't retrieve rect: for example, item isn't visible
1714 // ----------------------------------------------------------------------------
1716 // ----------------------------------------------------------------------------
1718 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1719 wxTreeItemData
*pItem2
,
1722 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1723 wxT("sorting tree without data doesn't make sense") );
1725 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1728 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1729 const wxTreeItemId
& item2
)
1731 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1734 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1736 // rely on the fact that TreeView_SortChildren does the same thing as our
1737 // default behaviour, i.e. sorts items alphabetically and so call it
1738 // directly if we're not in derived class (much more efficient!)
1739 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1741 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1746 tvSort
.hParent
= HITEM(item
);
1747 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1748 tvSort
.lParam
= (LPARAM
)this;
1749 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1753 // ----------------------------------------------------------------------------
1755 // ----------------------------------------------------------------------------
1757 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1759 if ( cmd
== EN_UPDATE
)
1761 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1762 event
.SetEventObject( this );
1763 ProcessCommand(event
);
1765 else if ( cmd
== EN_KILLFOCUS
)
1767 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1768 event
.SetEventObject( this );
1769 ProcessCommand(event
);
1777 // command processed
1781 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1782 // only do it during dragging, minimize wxWin overhead (this is important for
1783 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1784 // instead of passing by wxWin events
1785 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1787 bool processed
= FALSE
;
1790 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1792 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1794 // we only process mouse messages here and these parameters have the same
1795 // meaning for all of them
1796 int x
= GET_X_LPARAM(lParam
),
1797 y
= GET_Y_LPARAM(lParam
);
1798 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1802 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1803 case WM_LBUTTONDOWN
:
1804 if ( htItem
&& isMultiple
)
1806 if ( wParam
& MK_CONTROL
)
1810 // toggle selected state
1811 ToggleItemSelection(GetHwnd(), htItem
);
1813 ::SetFocus(GetHwnd(), htItem
);
1815 // reset on any click without Shift
1820 else if ( wParam
& MK_SHIFT
)
1822 // this selects all items between the starting one and
1825 if ( !m_htSelStart
)
1827 // take the focused item
1828 m_htSelStart
= (WXHTREEITEM
)
1829 TreeView_GetSelection(GetHwnd());
1832 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1833 !(wParam
& MK_CONTROL
));
1835 ::SetFocus(GetHwnd(), htItem
);
1839 else // normal click
1841 // clear the selection and then let the default handler
1845 // prevent the click from starting in-place editing
1846 // when there was no selection in the control
1847 TreeView_SelectItem(GetHwnd(), 0);
1849 // reset on any click without Shift
1854 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1859 m_dragImage
->Move(wxPoint(x
, y
));
1862 // highlight the item as target (hiding drag image is
1863 // necessary - otherwise the display will be corrupted)
1864 m_dragImage
->Hide();
1865 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1866 m_dragImage
->Show();
1875 m_dragImage
->EndDrag();
1879 // generate the drag end event
1880 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1882 event
.m_item
= (WXHTREEITEM
)htItem
;
1883 event
.m_pointDrag
= wxPoint(x
, y
);
1884 event
.SetEventObject(this);
1886 (void)GetEventHandler()->ProcessEvent(event
);
1888 // if we don't do it, the tree seems to think that 2 items
1889 // are selected simultaneously which is quite weird
1890 TreeView_SelectDropTarget(GetHwnd(), 0);
1895 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1896 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1898 // the tree control greys out the selected item when it loses focus and
1899 // paints it as selected again when it regains it, but it won't do it
1900 // for the other items itself - help it
1901 wxArrayTreeItemIds selections
;
1902 size_t count
= GetSelections(selections
);
1904 for ( size_t n
= 0; n
< count
; n
++ )
1906 // TreeView_GetItemRect() will return FALSE if item is not visible,
1907 // which may happen perfectly well
1908 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1911 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1915 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1917 bool bCtrl
= wxIsCtrlDown(),
1918 bShift
= wxIsShiftDown();
1920 // we handle.arrows and space, but not page up/down and home/end: the
1921 // latter should be easy, but not the former
1923 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1924 if ( !m_htSelStart
)
1926 m_htSelStart
= (WXHTREEITEM
)htSel
;
1929 if ( wParam
== VK_SPACE
)
1933 ToggleItemSelection(GetHwnd(), htSel
);
1939 ::SelectItem(GetHwnd(), htSel
);
1944 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1946 if ( !bCtrl
&& !bShift
)
1948 // no modifiers, just clear selection and then let the default
1949 // processing to take place
1954 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1956 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
1957 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1958 : TreeView_GetNextVisible(GetHwnd(), htSel
));
1962 // at the top/bottom
1968 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1972 // without changing selection
1973 ::SetFocus(GetHwnd(), htNext
);
1980 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1983 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1988 // process WM_NOTIFY Windows message
1989 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1991 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1992 wxEventType eventType
= wxEVT_NULL
;
1993 NMHDR
*hdr
= (NMHDR
*)lParam
;
1995 switch ( hdr
->code
)
1998 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2001 case TVN_BEGINRDRAG
:
2003 if ( eventType
== wxEVT_NULL
)
2004 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2005 //else: left drag, already set above
2007 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2009 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2010 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2012 // don't allow dragging by default: the user code must
2013 // explicitly say that it wants to allow it to avoid breaking
2019 case TVN_BEGINLABELEDIT
:
2021 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2022 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2024 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2025 event
.m_label
= info
->item
.pszText
;
2029 case TVN_DELETEITEM
:
2031 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2032 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2034 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2038 delete (wxTreeItemAttr
*)m_attrs
.
2039 Delete((long)tv
->itemOld
.hItem
);
2044 case TVN_ENDLABELEDIT
:
2046 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2047 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2049 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2050 event
.m_label
= info
->item
.pszText
;
2051 if (info
->item
.pszText
== NULL
)
2056 case TVN_GETDISPINFO
:
2057 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2060 case TVN_SETDISPINFO
:
2062 if ( eventType
== wxEVT_NULL
)
2063 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2064 //else: get, already set above
2066 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2068 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2072 case TVN_ITEMEXPANDING
:
2073 event
.m_code
= FALSE
;
2076 case TVN_ITEMEXPANDED
:
2078 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2080 bool expand
= FALSE
;
2081 switch ( tv
->action
)
2092 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2095 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2096 eventType
= g_events
[expand
][ing
];
2098 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2104 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2105 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2107 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
2109 // a separate event for Space/Return
2110 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2111 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2113 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2115 event2
.SetEventObject(this);
2116 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2118 event2
.m_item
= GetSelection();
2120 //else: don't know how to get it
2122 (void)GetEventHandler()->ProcessEvent(event2
);
2127 case TVN_SELCHANGED
:
2128 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2131 case TVN_SELCHANGING
:
2133 if ( eventType
== wxEVT_NULL
)
2134 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2135 //else: already set above
2137 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2139 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2140 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2144 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY && !defined(__GNUWIN32__)
2147 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2148 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2149 switch ( nmcd
.dwDrawStage
)
2152 // if we've got any items with non standard attributes,
2153 // notify us before painting each item
2154 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2158 case CDDS_ITEMPREPAINT
:
2160 wxTreeItemAttr
*attr
=
2161 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2165 // nothing to do for this item
2166 *result
= CDRF_DODEFAULT
;
2171 wxColour colText
, colBack
;
2172 if ( attr
->HasFont() )
2174 wxFont font
= attr
->GetFont();
2175 hFont
= (HFONT
)font
.GetResourceHandle();
2182 if ( attr
->HasTextColour() )
2184 colText
= attr
->GetTextColour();
2188 colText
= GetForegroundColour();
2191 // selection colours should override ours
2192 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2194 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2195 lptvcd
->clrTextBk
= clrBk
;
2197 // try to make the text visible
2198 lptvcd
->clrText
= wxColourToRGB(colText
);
2199 lptvcd
->clrText
|= ~clrBk
;
2200 lptvcd
->clrText
&= 0x00ffffff;
2204 if ( attr
->HasBackgroundColour() )
2206 colBack
= attr
->GetBackgroundColour();
2210 colBack
= GetBackgroundColour();
2213 lptvcd
->clrText
= wxColourToRGB(colText
);
2214 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2217 // note that if we wanted to set colours for
2218 // individual columns (subitems), we would have
2219 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2222 ::SelectObject(nmcd
.hdc
, hFont
);
2224 *result
= CDRF_NEWFONT
;
2228 *result
= CDRF_DODEFAULT
;
2234 *result
= CDRF_DODEFAULT
;
2238 // we always process it
2240 #endif // _WIN32_IE >= 0x300
2245 TV_HITTESTINFO tvhti
;
2246 ::GetCursorPos(&tvhti
.pt
);
2247 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2248 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2250 if ( tvhti
.flags
& TVHT_ONITEM
)
2252 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2253 eventType
= (int)hdr
->code
== NM_DBLCLK
2254 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2255 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2257 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2258 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2267 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2270 event
.SetEventObject(this);
2271 event
.SetEventType(eventType
);
2273 bool processed
= GetEventHandler()->ProcessEvent(event
);
2276 switch ( hdr
->code
)
2279 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2280 // the return code of this event handler as the return value for
2281 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2282 // expanded status would never work
2287 case TVN_BEGINRDRAG
:
2288 if ( event
.IsAllowed() )
2290 // normally this is impossible because the m_dragImage is
2291 // deleted once the drag operation is over
2292 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2294 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2295 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2296 m_dragImage
->Show();
2300 case TVN_DELETEITEM
:
2302 // NB: we might process this message using wxWindows event
2303 // tables, but due to overhead of wxWin event system we
2304 // prefer to do it here ourself (otherwise deleting a tree
2305 // with many items is just too slow)
2306 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2308 wxTreeItemId item
= event
.m_item
;
2309 if ( HasIndirectData(item
) )
2311 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2313 delete data
; // can't be NULL here
2315 m_itemsWithIndirectData
.Remove(item
);
2317 int iIndex
= m_itemsWithIndirectData
.Index(item
);
2318 wxASSERT( iIndex
!= wxNOT_FOUND
) ;
2319 m_itemsWithIndirectData
.wxBaseArray::RemoveAt((size_t)iIndex
);
2324 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2325 delete data
; // may be NULL, ok
2328 processed
= TRUE
; // Make sure we don't get called twice
2332 case TVN_BEGINLABELEDIT
:
2333 // return TRUE to cancel label editing
2334 *result
= !event
.IsAllowed();
2337 case TVN_ENDLABELEDIT
:
2338 // return TRUE to set the label to the new string: note that we
2339 // also must pretend that we did process the message or it is going
2340 // to be passed to DefWindowProc() which will happily return FALSE
2341 // cancelling the label change
2342 *result
= event
.IsAllowed();
2345 // ensure that we don't have the text ctrl which is going to be
2350 case TVN_SELCHANGING
:
2351 case TVN_ITEMEXPANDING
:
2352 // return TRUE to prevent the action from happening
2353 *result
= !event
.IsAllowed();
2356 case TVN_GETDISPINFO
:
2357 // NB: so far the user can't set the image himself anyhow, so do it
2358 // anyway - but this may change later
2359 // if ( /* !processed && */ 1 )
2361 wxTreeItemId item
= event
.m_item
;
2362 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2363 if ( info
->item
.mask
& TVIF_IMAGE
)
2366 DoGetItemImageFromData
2369 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2370 : wxTreeItemIcon_Normal
2373 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2375 info
->item
.iSelectedImage
=
2376 DoGetItemImageFromData
2379 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2380 : wxTreeItemIcon_Selected
2387 // for the other messages the return value is ignored and there is
2388 // nothing special to do
2396 #endif // wxUSE_TREECTRL