1 /////////////////////////////////////////////////////////////////////////////
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 #pragma implementation "treectrl.h"
23 // For compilers that support precompilation, includes "wx.h".
24 #include "wx/wxprec.h"
30 #include "wx/msw/private.h"
32 // Set this to 1 to be _absolutely_ sure that repainting will work for all comctl32.dll versions
33 #define wxUSE_COMCTL32_SAFELY 0
35 // Mingw32 is a bit mental even though this is done in winundef
44 #if defined(__WIN95__)
47 #include "wx/dynarray.h"
48 #include "wx/imaglist.h"
49 #include "wx/settings.h"
50 #include "wx/msw/treectrl.h"
51 #include "wx/msw/dragimag.h"
53 #ifdef __GNUWIN32_OLD__
54 #include "wx/msw/gnuwin32/extra.h"
57 #if defined(__WIN95__) && !(defined(__GNUWIN32_OLD__) || defined(__TWIN32__))
61 // Bug in headers, sometimes
63 #define TVIS_FOCUSED 0x0001
67 #define TV_FIRST 0x1100
70 #ifndef TVS_CHECKBOXES
71 #define TVS_CHECKBOXES 0x0100
74 // old headers might miss these messages (comctl32.dll 4.71+ only)
75 #ifndef TVM_SETBKCOLOR
76 #define TVM_SETBKCOLOR (TV_FIRST + 29)
77 #define TVM_SETTEXTCOLOR (TV_FIRST + 30)
80 // a macro to hide the ugliness of nested casts
81 #define HITEM(item) (HTREEITEM)(WXHTREEITEM)(item)
83 // the native control doesn't support multiple selections under MSW and we
84 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
85 // checkboxes be the selection status (checked == selected) or by really
86 // emulating everything, i.e. intercepting mouse and key events &c. The first
87 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
89 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 // wrapper for TreeView_HitTest
96 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
102 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
105 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
107 // wrappers for TreeView_GetItem/TreeView_SetItem
108 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
111 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
112 tvi
.stateMask
= TVIS_SELECTED
;
115 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
117 wxLogLastError(wxT("TreeView_GetItem"));
120 return (tvi
.state
& TVIS_SELECTED
) != 0;
123 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= TRUE
)
126 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
127 tvi
.stateMask
= TVIS_SELECTED
;
128 tvi
.state
= select
? TVIS_SELECTED
: 0;
131 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
133 wxLogLastError(wxT("TreeView_SetItem"));
137 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
139 SelectItem(hwndTV
, htItem
, FALSE
);
142 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
144 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
147 // helper function which selects all items in a range and, optionally,
148 // unselects all others
149 static void SelectRange(HWND hwndTV
,
152 bool unselectOthers
= TRUE
)
154 // find the first (or last) item and select it
156 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
157 while ( htItem
&& cont
)
159 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
161 if ( !IsItemSelected(hwndTV
, htItem
) )
163 SelectItem(hwndTV
, htItem
);
170 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
172 UnselectItem(hwndTV
, htItem
);
176 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
179 // select the items in range
180 cont
= htFirst
!= htLast
;
181 while ( htItem
&& cont
)
183 if ( !IsItemSelected(hwndTV
, htItem
) )
185 SelectItem(hwndTV
, htItem
);
188 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
190 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
194 if ( unselectOthers
)
198 if ( IsItemSelected(hwndTV
, htItem
) )
200 UnselectItem(hwndTV
, htItem
);
203 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
207 // seems to be necessary - otherwise the just selected items don't always
208 // appear as selected
209 UpdateWindow(hwndTV
);
212 // helper function which tricks the standard control into changing the focused
213 // item without changing anything else (if someone knows why Microsoft doesn't
214 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
215 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
218 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
223 if ( htItem
!= htFocus
)
225 // remember the selection state of the item
226 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
228 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
230 // prevent the tree from unselecting the old focus which it
231 // would do by default (TreeView_SelectItem unselects the
233 TreeView_SelectItem(hwndTV
, 0);
234 SelectItem(hwndTV
, htFocus
);
237 TreeView_SelectItem(hwndTV
, htItem
);
241 // need to clear the selection which TreeView_SelectItem() gave
243 UnselectItem(hwndTV
, htItem
);
245 //else: was selected, still selected - ok
247 //else: nothing to do, focus already there
253 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
255 // just clear the focus
256 TreeView_SelectItem(hwndTV
, 0);
258 if ( wasFocusSelected
)
260 // restore the selection state
261 SelectItem(hwndTV
, htFocus
);
264 //else: nothing to do, no focus already
268 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
270 // ----------------------------------------------------------------------------
272 // ----------------------------------------------------------------------------
274 // a convenient wrapper around TV_ITEM struct which adds a ctor
276 #pragma warning( disable : 4097 ) // inheriting from typedef
279 struct wxTreeViewItem
: public TV_ITEM
281 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
282 UINT mask_
, // fields which are valid
283 UINT stateMask_
= 0) // for TVIF_STATE only
285 // hItem member is always valid
286 mask
= mask_
| TVIF_HANDLE
;
287 stateMask
= stateMask_
;
293 #pragma warning( default : 4097 )
296 // a class which encapsulates the tree traversal logic: it vists all (unless
297 // OnVisit() returns FALSE) items under the given one
298 class wxTreeTraversal
301 wxTreeTraversal(const wxTreeCtrl
*tree
)
306 // do traverse the tree: visit all items (recursively by default) under the
307 // given one; return TRUE if all items were traversed or FALSE if the
308 // traversal was aborted because OnVisit returned FALSE
309 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= TRUE
);
311 // override this function to do whatever is needed for each item, return
312 // FALSE to stop traversing
313 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
316 const wxTreeCtrl
*GetTree() const { return m_tree
; }
319 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
321 const wxTreeCtrl
*m_tree
;
324 // internal class for getting the selected items
325 class TraverseSelections
: public wxTreeTraversal
328 TraverseSelections(const wxTreeCtrl
*tree
,
329 wxArrayTreeItemIds
& selections
)
330 : wxTreeTraversal(tree
), m_selections(selections
)
332 m_selections
.Empty();
334 DoTraverse(tree
->GetRootItem());
337 virtual bool OnVisit(const wxTreeItemId
& item
)
339 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
340 if ( GetTree()->IsItemChecked(item
) )
342 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
345 m_selections
.Add(item
);
351 size_t GetCount() const { return m_selections
.GetCount(); }
354 wxArrayTreeItemIds
& m_selections
;
357 // internal class for counting tree items
358 class TraverseCounter
: public wxTreeTraversal
361 TraverseCounter(const wxTreeCtrl
*tree
,
362 const wxTreeItemId
& root
,
364 : wxTreeTraversal(tree
)
368 DoTraverse(root
, recursively
);
371 virtual bool OnVisit(const wxTreeItemId
& item
)
378 size_t GetCount() const { return m_count
; }
384 // ----------------------------------------------------------------------------
385 // This class is needed for support of different images: the Win32 common
386 // control natively supports only 2 images (the normal one and another for the
387 // selected state). We wish to provide support for 2 more of them for folder
388 // items (i.e. those which have children): for expanded state and for expanded
389 // selected state. For this we use this structure to store the additional items
392 // There is only one problem with this: when we retrieve the item's data, we
393 // don't know whether we get a pointer to wxTreeItemData or
394 // wxTreeItemIndirectData. So we have to maintain a list of all items which
395 // have indirect data inside the listctrl itself.
396 // ----------------------------------------------------------------------------
398 class wxTreeItemIndirectData
401 // ctor associates this data with the item and the real item data becomes
402 // available through our GetData() method
403 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
405 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
411 m_data
= tree
->GetItemData(item
);
413 // and set ourselves as the new one
414 tree
->SetIndirectItemData(item
, this);
417 // dtor deletes the associated data as well
418 ~wxTreeItemIndirectData() { delete m_data
; }
421 // get the real data associated with the item
422 wxTreeItemData
*GetData() const { return m_data
; }
424 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
426 // do we have such image?
427 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
429 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
431 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
434 // all the images associated with the item
435 int m_images
[wxTreeItemIcon_Max
];
437 wxTreeItemData
*m_data
;
440 // ----------------------------------------------------------------------------
442 // ----------------------------------------------------------------------------
444 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 // handy table for sending events
451 static const wxEventType g_events
[2][2] =
453 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED
, wxEVT_COMMAND_TREE_ITEM_COLLAPSING
},
454 { wxEVT_COMMAND_TREE_ITEM_EXPANDED
, wxEVT_COMMAND_TREE_ITEM_EXPANDING
}
457 // ============================================================================
459 // ============================================================================
461 // ----------------------------------------------------------------------------
463 // ----------------------------------------------------------------------------
465 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
467 if ( !OnVisit(root
) )
470 return Traverse(root
, recursively
);
473 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
476 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
477 while ( child
.IsOk() )
479 // depth first traversal
480 if ( recursively
&& !Traverse(child
, TRUE
) )
483 if ( !OnVisit(child
) )
486 child
= m_tree
->GetNextChild(root
, cookie
);
492 // ----------------------------------------------------------------------------
493 // construction and destruction
494 // ----------------------------------------------------------------------------
496 void wxTreeCtrl::Init()
498 m_imageListNormal
= NULL
;
499 m_imageListState
= NULL
;
500 m_ownsImageListNormal
= m_ownsImageListState
= FALSE
;
502 m_hasAnyAttr
= FALSE
;
508 bool wxTreeCtrl::Create(wxWindow
*parent
,
513 const wxValidator
& validator
,
514 const wxString
& name
)
518 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
521 DWORD wstyle
= WS_VISIBLE
| WS_CHILD
| WS_TABSTOP
|
522 TVS_SHOWSELALWAYS
/* | WS_CLIPSIBLINGS */;
524 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
525 wstyle
|= TVS_HASLINES
;
526 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
527 wstyle
|= TVS_HASBUTTONS
;
529 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
530 wstyle
|= TVS_EDITLABELS
;
532 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
533 wstyle
|= TVS_LINESATROOT
;
535 // using TVS_CHECKBOXES for emulation of a multiselection tree control
536 // doesn't work without the new enough headers
537 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
538 !defined( __GNUWIN32_OLD__ ) && \
539 !defined( __BORLANDC__ ) && \
540 !defined( __WATCOMC__ ) && \
541 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
543 // we emulate the multiple selection tree controls by using checkboxes: set
544 // up the image list we need for this if we do have multiple selections
545 if ( m_windowStyle
& wxTR_MULTIPLE
)
546 wstyle
|= TVS_CHECKBOXES
;
547 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
549 // Create the tree control.
550 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
553 #if wxUSE_COMCTL32_SAFELY
554 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
555 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
557 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
558 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
560 // This works around a bug in the Windows tree control whereby for some versions
561 // of comctrl32, setting any colour actually draws the background in black.
562 // This will initialise the background to the system colour.
563 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
564 // Assume the user has an updated comctl32.dll.
565 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
566 wxWindow::SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_WINDOW
));
567 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
571 // VZ: this is some experimental code which may be used to get the
572 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
573 // AFAIK, the standard DLL does about the same thing anyhow.
575 if ( m_windowStyle
& wxTR_MULTIPLE
)
579 // create the DC compatible with the current screen
580 HDC hdcMem
= CreateCompatibleDC(NULL
);
582 // create a mono bitmap of the standard size
583 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
584 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
585 wxImageList
imagelistCheckboxes(x
, y
, FALSE
, 2);
586 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
587 1, // # of color planes
588 1, // # bits needed for one pixel
589 0); // array containing colour data
590 SelectObject(hdcMem
, hbmpCheck
);
592 // then draw a check mark into it
593 RECT rect
= { 0, 0, x
, y
};
594 if ( !::DrawFrameControl(hdcMem
, &rect
,
596 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
598 wxLogLastError(wxT("DrawFrameControl(check)"));
601 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
602 imagelistCheckboxes
.Add(bmp
);
604 if ( !::DrawFrameControl(hdcMem
, &rect
,
608 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
611 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
612 imagelistCheckboxes
.Add(bmp
);
618 SetStateImageList(&imagelistCheckboxes
);
622 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
627 wxTreeCtrl::~wxTreeCtrl()
629 // delete any attributes
632 for ( wxNode
*node
= m_attrs
.Next(); node
; node
= m_attrs
.Next() )
634 delete (wxTreeItemAttr
*)node
->Data();
637 // prevent TVN_DELETEITEM handler from deleting the attributes again!
638 m_hasAnyAttr
= FALSE
;
643 // delete user data to prevent memory leaks
646 if (m_ownsImageListNormal
) delete m_imageListNormal
;
647 if (m_ownsImageListState
) delete m_imageListState
;
650 // ----------------------------------------------------------------------------
652 // ----------------------------------------------------------------------------
654 // simple wrappers which add error checking in debug mode
656 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
658 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
660 wxLogLastError(wxT("TreeView_GetItem"));
668 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
670 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
672 wxLogLastError(wxT("TreeView_SetItem"));
676 size_t wxTreeCtrl::GetCount() const
678 return (size_t)TreeView_GetCount(GetHwnd());
681 unsigned int wxTreeCtrl::GetIndent() const
683 return TreeView_GetIndent(GetHwnd());
686 void wxTreeCtrl::SetIndent(unsigned int indent
)
688 TreeView_SetIndent(GetHwnd(), indent
);
691 wxImageList
*wxTreeCtrl::GetImageList() const
693 return m_imageListNormal
;
696 wxImageList
*wxTreeCtrl::GetStateImageList() const
698 return m_imageListNormal
;
701 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
704 TreeView_SetImageList(GetHwnd(),
705 imageList
? imageList
->GetHIMAGELIST() : 0,
709 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
711 if (m_ownsImageListNormal
) delete m_imageListNormal
;
712 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
713 m_ownsImageListNormal
= FALSE
;
716 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
718 if (m_ownsImageListState
) delete m_imageListState
;
719 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
720 m_ownsImageListState
= FALSE
;
723 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
725 SetImageList(imageList
);
726 m_ownsImageListNormal
= TRUE
;
729 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
731 SetStateImageList(imageList
);
732 m_ownsImageListState
= TRUE
;
735 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
736 bool recursively
) const
738 TraverseCounter
counter(this, item
, recursively
);
740 return counter
.GetCount() - 1;
743 // ----------------------------------------------------------------------------
745 // ----------------------------------------------------------------------------
747 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
749 #if !wxUSE_COMCTL32_SAFELY
750 if ( !wxWindowBase::SetBackgroundColour(colour
) )
753 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
759 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
761 #if !wxUSE_COMCTL32_SAFELY
762 if ( !wxWindowBase::SetForegroundColour(colour
) )
765 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
771 // ----------------------------------------------------------------------------
773 // ----------------------------------------------------------------------------
775 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
777 wxChar buf
[512]; // the size is arbitrary...
779 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
780 tvItem
.pszText
= buf
;
781 tvItem
.cchTextMax
= WXSIZEOF(buf
);
782 if ( !DoGetItem(&tvItem
) )
784 // don't return some garbage which was on stack, but an empty string
788 return wxString(buf
);
791 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
793 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
794 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
798 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
799 wxTreeItemIcon which
) const
801 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
802 if ( !DoGetItem(&tvItem
) )
807 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
810 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
812 wxTreeItemIcon which
) const
814 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
815 if ( !DoGetItem(&tvItem
) )
820 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
822 data
->SetImage(image
, which
);
824 // make sure that we have selected images as well
825 if ( which
== wxTreeItemIcon_Normal
&&
826 !data
->HasImage(wxTreeItemIcon_Selected
) )
828 data
->SetImage(image
, wxTreeItemIcon_Selected
);
831 if ( which
== wxTreeItemIcon_Expanded
&&
832 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
834 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
838 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
842 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
843 tvItem
.iSelectedImage
= imageSel
;
844 tvItem
.iImage
= image
;
848 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
849 wxTreeItemIcon which
) const
851 if ( HasIndirectData(item
) )
853 return DoGetItemImageFromData(item
, which
);
860 wxFAIL_MSG( wxT("unknown tree item image type") );
862 case wxTreeItemIcon_Normal
:
866 case wxTreeItemIcon_Selected
:
867 mask
= TVIF_SELECTEDIMAGE
;
870 case wxTreeItemIcon_Expanded
:
871 case wxTreeItemIcon_SelectedExpanded
:
875 wxTreeViewItem
tvItem(item
, mask
);
878 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
881 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
882 wxTreeItemIcon which
)
884 int imageNormal
, imageSel
;
888 wxFAIL_MSG( wxT("unknown tree item image type") );
890 case wxTreeItemIcon_Normal
:
892 imageSel
= GetItemSelectedImage(item
);
895 case wxTreeItemIcon_Selected
:
896 imageNormal
= GetItemImage(item
);
900 case wxTreeItemIcon_Expanded
:
901 case wxTreeItemIcon_SelectedExpanded
:
902 if ( !HasIndirectData(item
) )
904 // we need to get the old images first, because after we create
905 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
907 imageNormal
= GetItemImage(item
);
908 imageSel
= GetItemSelectedImage(item
);
910 // if it doesn't have it yet, add it
911 wxTreeItemIndirectData
*data
= new
912 wxTreeItemIndirectData(this, item
);
914 // copy the data to the new location
915 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
916 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
919 DoSetItemImageFromData(item
, image
, which
);
921 // reset the normal/selected images because we won't use them any
922 // more - now they're stored inside the indirect data
924 imageSel
= I_IMAGECALLBACK
;
928 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
929 // change both normal and selected image - otherwise the change simply
930 // doesn't take place!
931 DoSetItemImages(item
, imageNormal
, imageSel
);
934 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
936 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
937 if ( !DoGetItem(&tvItem
) )
942 if ( HasIndirectData(item
) )
944 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetData();
948 return (wxTreeItemData
*)tvItem
.lParam
;
952 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
954 // first, associate this piece of data with this item
960 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
962 if ( HasIndirectData(item
) )
964 if ( DoGetItem(&tvItem
) )
966 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
970 wxFAIL_MSG( wxT("failed to change tree items data") );
975 tvItem
.lParam
= (LPARAM
)data
;
980 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
981 wxTreeItemIndirectData
*data
)
983 // this should never happen because it's unnecessary and will probably lead
984 // to crash too because the code elsewhere supposes that the pointer the
985 // wxTreeItemIndirectData has is a real wxItemData and not
986 // wxTreeItemIndirectData as well
987 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
989 SetItemData(item
, (wxTreeItemData
*)data
);
991 m_itemsWithIndirectData
.Add(item
);
994 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
996 return m_itemsWithIndirectData
.Index(item
) != wxNOT_FOUND
;
999 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1001 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1002 tvItem
.cChildren
= (int)has
;
1006 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1008 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1009 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1013 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1015 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1016 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1020 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1021 const wxColour
& col
)
1023 m_hasAnyAttr
= TRUE
;
1025 long id
= (long)(WXHTREEITEM
)item
;
1026 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1029 attr
= new wxTreeItemAttr
;
1030 m_attrs
.Put(id
, (wxObject
*)attr
);
1033 attr
->SetTextColour(col
);
1036 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1037 const wxColour
& col
)
1039 m_hasAnyAttr
= TRUE
;
1041 long id
= (long)(WXHTREEITEM
)item
;
1042 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1045 attr
= new wxTreeItemAttr
;
1046 m_attrs
.Put(id
, (wxObject
*)attr
);
1049 attr
->SetBackgroundColour(col
);
1052 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1054 m_hasAnyAttr
= TRUE
;
1056 long id
= (long)(WXHTREEITEM
)item
;
1057 wxTreeItemAttr
*attr
= (wxTreeItemAttr
*)m_attrs
.Get(id
);
1060 attr
= new wxTreeItemAttr
;
1061 m_attrs
.Put(id
, (wxObject
*)attr
);
1064 attr
->SetFont(font
);
1067 // ----------------------------------------------------------------------------
1069 // ----------------------------------------------------------------------------
1071 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1073 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1076 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1077 // the HTREEITEM with TVM_GETITEMRECT
1078 *(WXHTREEITEM
*)&rect
= (WXHTREEITEM
)item
;
1080 // FALSE means get item rect for the whole item, not only text
1081 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, FALSE
, (LPARAM
)&rect
) != 0;
1085 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1087 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1090 return tvItem
.cChildren
!= 0;
1093 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1095 // probably not a good idea to put it here
1096 //wxASSERT( ItemHasChildren(item) );
1098 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1101 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1104 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1106 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1109 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1112 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1114 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1117 return (tvItem
.state
& TVIS_BOLD
) != 0;
1120 // ----------------------------------------------------------------------------
1122 // ----------------------------------------------------------------------------
1124 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1126 return wxTreeItemId((WXHTREEITEM
) TreeView_GetRoot(GetHwnd()));
1129 wxTreeItemId
wxTreeCtrl::GetSelection() const
1131 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), (long)(WXHTREEITEM
)0,
1132 wxT("this only works with single selection controls") );
1134 return wxTreeItemId((WXHTREEITEM
) TreeView_GetSelection(GetHwnd()));
1137 wxTreeItemId
wxTreeCtrl::GetParent(const wxTreeItemId
& item
) const
1139 return wxTreeItemId((WXHTREEITEM
) TreeView_GetParent(GetHwnd(), HITEM(item
)));
1142 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1143 long& _cookie
) const
1145 // remember the last child returned in 'cookie'
1146 _cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1148 return wxTreeItemId((WXHTREEITEM
)_cookie
);
1151 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1152 long& _cookie
) const
1154 wxTreeItemId l
= wxTreeItemId((WXHTREEITEM
)TreeView_GetNextSibling(GetHwnd(),
1161 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1163 // can this be done more efficiently?
1166 wxTreeItemId childLast
,
1167 child
= GetFirstChild(item
, cookie
);
1168 while ( child
.IsOk() )
1171 child
= GetNextChild(item
, cookie
);
1177 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1179 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1182 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1184 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1187 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1189 return wxTreeItemId((WXHTREEITEM
) TreeView_GetFirstVisible(GetHwnd()));
1192 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1194 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1196 return wxTreeItemId((WXHTREEITEM
) TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1199 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1201 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1203 return wxTreeItemId((WXHTREEITEM
) TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1206 // ----------------------------------------------------------------------------
1207 // multiple selections emulation
1208 // ----------------------------------------------------------------------------
1210 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1212 // receive the desired information.
1213 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1216 // state image indices are 1 based
1217 return ((tvItem
.state
>> 12) - 1) == 1;
1220 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1222 // receive the desired information.
1223 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1225 // state images are one-based
1226 tvItem
.state
= (check
? 2 : 1) << 12;
1231 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1233 TraverseSelections
selector(this, selections
);
1235 return selector
.GetCount();
1238 // ----------------------------------------------------------------------------
1240 // ----------------------------------------------------------------------------
1242 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1243 wxTreeItemId hInsertAfter
,
1244 const wxString
& text
,
1245 int image
, int selectedImage
,
1246 wxTreeItemData
*data
)
1248 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1250 _T("can't have more than one root in the tree") );
1252 TV_INSERTSTRUCT tvIns
;
1253 tvIns
.hParent
= HITEM(parent
);
1254 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1256 // this is how we insert the item as the first child: supply a NULL
1258 if ( !tvIns
.hInsertAfter
)
1260 tvIns
.hInsertAfter
= TVI_FIRST
;
1264 if ( !text
.IsEmpty() )
1267 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1271 tvIns
.item
.pszText
= NULL
;
1272 tvIns
.item
.cchTextMax
= 0;
1278 tvIns
.item
.iImage
= image
;
1280 if ( selectedImage
== -1 )
1282 // take the same image for selected icon if not specified
1283 selectedImage
= image
;
1287 if ( selectedImage
!= -1 )
1289 mask
|= TVIF_SELECTEDIMAGE
;
1290 tvIns
.item
.iSelectedImage
= selectedImage
;
1296 tvIns
.item
.lParam
= (LPARAM
)data
;
1299 tvIns
.item
.mask
= mask
;
1301 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1304 wxLogLastError(wxT("TreeView_InsertItem"));
1309 // associate the application tree item with Win32 tree item handle
1310 data
->SetId((WXHTREEITEM
)id
);
1313 return wxTreeItemId((WXHTREEITEM
)id
);
1316 // for compatibility only
1317 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1318 const wxString
& text
,
1319 int image
, int selImage
,
1322 return DoInsertItem(parent
, (WXHTREEITEM
)insertAfter
, text
,
1323 image
, selImage
, NULL
);
1326 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1327 int image
, int selectedImage
,
1328 wxTreeItemData
*data
)
1330 return DoInsertItem(wxTreeItemId((long) (WXHTREEITEM
) 0), (long)(WXHTREEITEM
) 0,
1331 text
, image
, selectedImage
, data
);
1334 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1335 const wxString
& text
,
1336 int image
, int selectedImage
,
1337 wxTreeItemData
*data
)
1339 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_FIRST
,
1340 text
, image
, selectedImage
, data
);
1343 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1344 const wxTreeItemId
& idPrevious
,
1345 const wxString
& text
,
1346 int image
, int selectedImage
,
1347 wxTreeItemData
*data
)
1349 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1352 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1354 const wxString
& text
,
1355 int image
, int selectedImage
,
1356 wxTreeItemData
*data
)
1358 // find the item from index
1360 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1361 while ( index
!= 0 && idCur
.IsOk() )
1366 idCur
= GetNextChild(parent
, cookie
);
1369 // assert, not check: if the index is invalid, we will append the item
1371 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1373 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1376 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1377 const wxString
& text
,
1378 int image
, int selectedImage
,
1379 wxTreeItemData
*data
)
1381 return DoInsertItem(parent
, (WXHTREEITEM
) TVI_LAST
,
1382 text
, image
, selectedImage
, data
);
1385 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1387 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1389 wxLogLastError(wxT("TreeView_DeleteItem"));
1393 // delete all children (but don't delete the item itself)
1394 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1398 wxArrayLong children
;
1399 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1400 while ( child
.IsOk() )
1402 children
.Add((long)(WXHTREEITEM
)child
);
1404 child
= GetNextChild(item
, cookie
);
1407 size_t nCount
= children
.Count();
1408 for ( size_t n
= 0; n
< nCount
; n
++ )
1410 if ( !TreeView_DeleteItem(GetHwnd(), (HTREEITEM
)children
[n
]) )
1412 wxLogLastError(wxT("TreeView_DeleteItem"));
1417 void wxTreeCtrl::DeleteAllItems()
1419 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1421 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1425 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1427 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1428 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1429 flag
== TVE_EXPAND
||
1431 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1433 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1434 // emulate them. This behaviour has changed slightly with comctl32.dll
1435 // v 4.70 - now it does send them but only the first time. To maintain
1436 // compatible behaviour and also in order to not have surprises with the
1437 // future versions, don't rely on this and still do everything ourselves.
1438 // To avoid that the messages be sent twice when the item is expanded for
1439 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1441 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1445 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1447 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1448 event
.m_item
= item
;
1450 bool isExpanded
= IsExpanded(item
);
1452 event
.SetEventObject(this);
1454 // FIXME return value of {EXPAND|COLLAPS}ING event handler is discarded
1455 event
.SetEventType(g_events
[isExpanded
][TRUE
]);
1456 GetEventHandler()->ProcessEvent(event
);
1458 event
.SetEventType(g_events
[isExpanded
][FALSE
]);
1459 GetEventHandler()->ProcessEvent(event
);
1461 //else: change didn't took place, so do nothing at all
1464 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1466 DoExpand(item
, TVE_EXPAND
);
1469 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1471 DoExpand(item
, TVE_COLLAPSE
);
1474 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1476 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1479 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1481 DoExpand(item
, TVE_TOGGLE
);
1484 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1486 DoExpand(item
, action
);
1489 void wxTreeCtrl::Unselect()
1491 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1492 wxT("doesn't make sense, may be you want UnselectAll()?") );
1494 // just remove the selection
1495 SelectItem(wxTreeItemId((long) (WXHTREEITEM
) 0));
1498 void wxTreeCtrl::UnselectAll()
1500 if ( m_windowStyle
& wxTR_MULTIPLE
)
1502 wxArrayTreeItemIds selections
;
1503 size_t count
= GetSelections(selections
);
1504 for ( size_t n
= 0; n
< count
; n
++ )
1506 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1507 SetItemCheck(selections
[n
], FALSE
);
1508 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1509 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1510 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1515 // just remove the selection
1520 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1522 if ( m_windowStyle
& wxTR_MULTIPLE
)
1524 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1525 // selecting the item means checking it
1527 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1528 ::SelectItem(GetHwnd(), HITEM(item
));
1529 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1533 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1534 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1535 // send them ourselves
1537 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1538 event
.m_item
= item
;
1539 event
.SetEventObject(this);
1541 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1542 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1544 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1546 wxLogLastError(wxT("TreeView_SelectItem"));
1550 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1551 (void)GetEventHandler()->ProcessEvent(event
);
1554 //else: program vetoed the change
1558 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1561 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1564 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1566 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1568 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1572 wxTextCtrl
* wxTreeCtrl::GetEditControl() const
1574 // normally, we could try to do something like this to return something
1575 // even when the editing was started by the user and not by calling
1576 // EditLabel() - but as nobody has asked for this so far and there might be
1577 // problems in the code below, I leave it disabled for now (VZ)
1581 HWND hwndText
= TreeView_GetEditControl(GetHwnd());
1584 m_textCtrl
= new wxTextCtrl(this, -1);
1586 m_textCtrl
->SetHWND((WXHWND
)hwndText
);
1588 //else: not editing label right now
1595 void wxTreeCtrl::DeleteTextCtrl()
1599 // the HWND corresponding to this control is deleted by the tree
1600 // control itself and we don't know when exactly this happens, so check
1601 // if the window still exists before calling UnsubclassWin()
1602 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1604 m_textCtrl
->SetHWND(0);
1607 m_textCtrl
->UnsubclassWin();
1608 m_textCtrl
->SetHWND(0);
1614 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1615 wxClassInfo
* textControlClass
)
1617 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1621 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1623 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1630 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1631 m_textCtrl
->SetHWND((WXHWND
)hWnd
);
1632 m_textCtrl
->SubclassWin((WXHWND
)hWnd
);
1637 // End label editing, optionally cancelling the edit
1638 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& item
, bool discardChanges
)
1640 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1645 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1647 TV_HITTESTINFO hitTestInfo
;
1648 hitTestInfo
.pt
.x
= (int)point
.x
;
1649 hitTestInfo
.pt
.y
= (int)point
.y
;
1651 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1656 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1657 flags |= wxTREE_HITTEST_##flag
1659 TRANSLATE_FLAG(ABOVE
);
1660 TRANSLATE_FLAG(BELOW
);
1661 TRANSLATE_FLAG(NOWHERE
);
1662 TRANSLATE_FLAG(ONITEMBUTTON
);
1663 TRANSLATE_FLAG(ONITEMICON
);
1664 TRANSLATE_FLAG(ONITEMINDENT
);
1665 TRANSLATE_FLAG(ONITEMLABEL
);
1666 TRANSLATE_FLAG(ONITEMRIGHT
);
1667 TRANSLATE_FLAG(ONITEMSTATEICON
);
1668 TRANSLATE_FLAG(TOLEFT
);
1669 TRANSLATE_FLAG(TORIGHT
);
1671 #undef TRANSLATE_FLAG
1673 return wxTreeItemId((WXHTREEITEM
) hitTestInfo
.hItem
);
1676 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1678 bool textOnly
) const
1681 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1684 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1690 // couldn't retrieve rect: for example, item isn't visible
1695 // ----------------------------------------------------------------------------
1697 // ----------------------------------------------------------------------------
1699 static int CALLBACK
TreeView_CompareCallback(wxTreeItemData
*pItem1
,
1700 wxTreeItemData
*pItem2
,
1703 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1704 wxT("sorting tree without data doesn't make sense") );
1706 return tree
->OnCompareItems(pItem1
->GetId(), pItem2
->GetId());
1709 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
1710 const wxTreeItemId
& item2
)
1712 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
1715 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1717 // rely on the fact that TreeView_SortChildren does the same thing as our
1718 // default behaviour, i.e. sorts items alphabetically and so call it
1719 // directly if we're not in derived class (much more efficient!)
1720 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1722 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1727 tvSort
.hParent
= HITEM(item
);
1728 tvSort
.lpfnCompare
= (PFNTVCOMPARE
)TreeView_CompareCallback
;
1729 tvSort
.lParam
= (LPARAM
)this;
1730 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1734 // ----------------------------------------------------------------------------
1736 // ----------------------------------------------------------------------------
1738 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
1740 if ( cmd
== EN_UPDATE
)
1742 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
1743 event
.SetEventObject( this );
1744 ProcessCommand(event
);
1746 else if ( cmd
== EN_KILLFOCUS
)
1748 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
1749 event
.SetEventObject( this );
1750 ProcessCommand(event
);
1758 // command processed
1762 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
1763 // only do it during dragging, minimize wxWin overhead (this is important for
1764 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
1765 // instead of passing by wxWin events
1766 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
1768 bool processed
= FALSE
;
1771 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
1773 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
1775 // we only process mouse messages here and these parameters have the same
1776 // meaning for all of them
1777 int x
= GET_X_LPARAM(lParam
),
1778 y
= GET_Y_LPARAM(lParam
);
1779 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
1783 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1784 case WM_LBUTTONDOWN
:
1785 if ( htItem
&& isMultiple
)
1787 if ( wParam
& MK_CONTROL
)
1791 // toggle selected state
1792 ToggleItemSelection(GetHwnd(), htItem
);
1794 ::SetFocus(GetHwnd(), htItem
);
1796 // reset on any click without Shift
1801 else if ( wParam
& MK_SHIFT
)
1803 // this selects all items between the starting one and
1806 if ( !m_htSelStart
)
1808 // take the focused item
1809 m_htSelStart
= (WXHTREEITEM
)
1810 TreeView_GetSelection(GetHwnd());
1813 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
1814 !(wParam
& MK_CONTROL
));
1816 ::SetFocus(GetHwnd(), htItem
);
1820 else // normal click
1822 // clear the selection and then let the default handler
1826 // prevent the click from starting in-place editing
1827 // when there was no selection in the control
1828 TreeView_SelectItem(GetHwnd(), 0);
1830 // reset on any click without Shift
1835 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1840 m_dragImage
->Move(wxPoint(x
, y
));
1843 // highlight the item as target (hiding drag image is
1844 // necessary - otherwise the display will be corrupted)
1845 m_dragImage
->Hide();
1846 TreeView_SelectDropTarget(GetHwnd(), htItem
);
1847 m_dragImage
->Show();
1856 m_dragImage
->EndDrag();
1860 // generate the drag end event
1861 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
1863 event
.m_item
= (WXHTREEITEM
)htItem
;
1864 event
.m_pointDrag
= wxPoint(x
, y
);
1865 event
.SetEventObject(this);
1867 (void)GetEventHandler()->ProcessEvent(event
);
1869 // if we don't do it, the tree seems to think that 2 items
1870 // are selected simultaneously which is quite weird
1871 TreeView_SelectDropTarget(GetHwnd(), 0);
1876 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1877 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
1879 // the tree control greys out the selected item when it loses focus and
1880 // paints it as selected again when it regains it, but it won't do it
1881 // for the other items itself - help it
1882 wxArrayTreeItemIds selections
;
1883 size_t count
= GetSelections(selections
);
1885 for ( size_t n
= 0; n
< count
; n
++ )
1887 // TreeView_GetItemRect() will return FALSE if item is not visible,
1888 // which may happen perfectly well
1889 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
1892 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
1896 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
1898 bool bCtrl
= wxIsCtrlDown(),
1899 bShift
= wxIsShiftDown();
1901 // we handle.arrows and space, but not page up/down and home/end: the
1902 // latter should be easy, but not the former
1904 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
1905 if ( !m_htSelStart
)
1907 m_htSelStart
= (WXHTREEITEM
)htSel
;
1910 if ( wParam
== VK_SPACE
)
1914 ToggleItemSelection(GetHwnd(), htSel
);
1920 ::SelectItem(GetHwnd(), htSel
);
1925 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
1927 if ( !bCtrl
&& !bShift
)
1929 // no modifiers, just clear selection and then let the default
1930 // processing to take place
1935 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1937 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
1938 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
1939 : TreeView_GetNextVisible(GetHwnd(), htSel
));
1943 // at the top/bottom
1949 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
1953 // without changing selection
1954 ::SetFocus(GetHwnd(), htNext
);
1961 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1964 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
1969 // process WM_NOTIFY Windows message
1970 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
1972 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1973 wxEventType eventType
= wxEVT_NULL
;
1974 NMHDR
*hdr
= (NMHDR
*)lParam
;
1976 switch ( hdr
->code
)
1979 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
1982 case TVN_BEGINRDRAG
:
1984 if ( eventType
== wxEVT_NULL
)
1985 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
1986 //else: left drag, already set above
1988 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
1990 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
1991 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
1993 // don't allow dragging by default: the user code must
1994 // explicitly say that it wants to allow it to avoid breaking
2000 case TVN_BEGINLABELEDIT
:
2002 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2003 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2005 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2006 event
.m_label
= info
->item
.pszText
;
2010 case TVN_DELETEITEM
:
2012 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2013 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2015 event
.m_item
= (WXHTREEITEM
)tv
->itemOld
.hItem
;
2019 delete (wxTreeItemAttr
*)m_attrs
.
2020 Delete((long)tv
->itemOld
.hItem
);
2025 case TVN_ENDLABELEDIT
:
2027 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2028 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2030 event
.m_item
= (WXHTREEITEM
)info
->item
.hItem
;
2031 event
.m_label
= info
->item
.pszText
;
2032 if (info
->item
.pszText
== NULL
)
2037 case TVN_GETDISPINFO
:
2038 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2041 case TVN_SETDISPINFO
:
2043 if ( eventType
== wxEVT_NULL
)
2044 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2045 //else: get, already set above
2047 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2049 event
.m_item
= (WXHTREEITEM
) info
->item
.hItem
;
2053 case TVN_ITEMEXPANDING
:
2054 event
.m_code
= FALSE
;
2057 case TVN_ITEMEXPANDED
:
2059 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2061 bool expand
= FALSE
;
2062 switch ( tv
->action
)
2073 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2076 bool ing
= ((int)hdr
->code
== TVN_ITEMEXPANDING
);
2077 eventType
= g_events
[expand
][ing
];
2079 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2085 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2086 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2088 event
.m_code
= wxCharCodeMSWToWX(info
->wVKey
);
2090 // a separate event for Space/Return
2091 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2092 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2094 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2096 event2
.SetEventObject(this);
2097 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2099 event2
.m_item
= GetSelection();
2101 //else: don't know how to get it
2103 (void)GetEventHandler()->ProcessEvent(event2
);
2108 case TVN_SELCHANGED
:
2109 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2112 case TVN_SELCHANGING
:
2114 if ( eventType
== wxEVT_NULL
)
2115 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2116 //else: already set above
2118 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2120 event
.m_item
= (WXHTREEITEM
) tv
->itemNew
.hItem
;
2121 event
.m_itemOld
= (WXHTREEITEM
) tv
->itemOld
.hItem
;
2125 #if defined(_WIN32_IE) && _WIN32_IE >= 0x300 && !wxUSE_COMCTL32_SAFELY
2128 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2129 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2130 switch( nmcd
.dwDrawStage
)
2133 // if we've got any items with non standard attributes,
2134 // notify us before painting each item
2135 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2139 case CDDS_ITEMPREPAINT
:
2141 wxTreeItemAttr
*attr
=
2142 (wxTreeItemAttr
*)m_attrs
.Get(nmcd
.dwItemSpec
);
2146 // nothing to do for this item
2147 return CDRF_DODEFAULT
;
2151 wxColour colText
, colBack
;
2152 if ( attr
->HasFont() )
2154 wxFont font
= attr
->GetFont();
2155 hFont
= (HFONT
)font
.GetResourceHandle();
2162 if ( attr
->HasTextColour() )
2164 colText
= attr
->GetTextColour();
2168 colText
= GetForegroundColour();
2171 // selection colours should override ours
2172 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2174 DWORD clrBk
= ::GetSysColor(COLOR_HIGHLIGHT
);
2175 lptvcd
->clrTextBk
= clrBk
;
2177 // try to make the text visible
2178 lptvcd
->clrText
= wxColourToRGB(colText
);
2179 lptvcd
->clrText
|= ~clrBk
;
2180 lptvcd
->clrText
&= 0x00ffffff;
2184 if ( attr
->HasBackgroundColour() )
2186 colBack
= attr
->GetBackgroundColour();
2190 colBack
= GetBackgroundColour();
2193 lptvcd
->clrText
= wxColourToRGB(colText
);
2194 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2197 // note that if we wanted to set colours for
2198 // individual columns (subitems), we would have
2199 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2202 ::SelectObject(nmcd
.hdc
, hFont
);
2204 *result
= CDRF_NEWFONT
;
2208 *result
= CDRF_DODEFAULT
;
2215 *result
= CDRF_DODEFAULT
;
2220 #endif // _WIN32_IE >= 0x300
2225 TV_HITTESTINFO tvhti
;
2226 ::GetCursorPos(&tvhti
.pt
);
2227 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2228 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2230 if ( tvhti
.flags
& TVHT_ONITEM
)
2232 event
.m_item
= (WXHTREEITEM
) tvhti
.hItem
;
2233 eventType
= (int)hdr
->code
== NM_DBLCLK
2234 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2235 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2237 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2238 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2247 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2250 event
.SetEventObject(this);
2251 event
.SetEventType(eventType
);
2253 bool processed
= GetEventHandler()->ProcessEvent(event
);
2256 switch ( hdr
->code
)
2259 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2260 // the return code of this event handler as the return value for
2261 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2262 // expanded status would never work
2267 case TVN_BEGINRDRAG
:
2268 if ( event
.IsAllowed() )
2270 // normally this is impossible because the m_dragImage is
2271 // deleted once the drag operation is over
2272 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2274 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2275 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2276 m_dragImage
->Show();
2280 case TVN_DELETEITEM
:
2282 // NB: we might process this message using wxWindows event
2283 // tables, but due to overhead of wxWin event system we
2284 // prefer to do it here ourself (otherwise deleting a tree
2285 // with many items is just too slow)
2286 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2288 wxTreeItemId item
= event
.m_item
;
2289 if ( HasIndirectData(item
) )
2291 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2293 delete data
; // can't be NULL here
2295 m_itemsWithIndirectData
.Remove(item
);
2297 int iIndex
= m_itemsWithIndirectData
.Index(item
);
2298 wxASSERT( iIndex
!= wxNOT_FOUND
) ;
2299 m_itemsWithIndirectData
.wxBaseArray::RemoveAt((size_t)iIndex
);
2304 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2305 delete data
; // may be NULL, ok
2308 processed
= TRUE
; // Make sure we don't get called twice
2312 case TVN_BEGINLABELEDIT
:
2313 // return TRUE to cancel label editing
2314 *result
= !event
.IsAllowed();
2317 case TVN_ENDLABELEDIT
:
2318 // return TRUE to set the label to the new string: note that we
2319 // also must pretend that we did process the message or it is going
2320 // to be passed to DefWindowProc() which will happily return FALSE
2321 // cancelling the label change
2322 *result
= event
.IsAllowed();
2325 // ensure that we don't have the text ctrl which is going to be
2330 case TVN_SELCHANGING
:
2331 case TVN_ITEMEXPANDING
:
2332 // return TRUE to prevent the action from happening
2333 *result
= !event
.IsAllowed();
2336 case TVN_GETDISPINFO
:
2337 // NB: so far the user can't set the image himself anyhow, so do it
2338 // anyway - but this may change later
2339 if ( /* !processed && */ 1 )
2341 wxTreeItemId item
= event
.m_item
;
2342 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2343 if ( info
->item
.mask
& TVIF_IMAGE
)
2346 DoGetItemImageFromData
2349 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2350 : wxTreeItemIcon_Normal
2353 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2355 info
->item
.iSelectedImage
=
2356 DoGetItemImageFromData
2359 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2360 : wxTreeItemIcon_Selected
2367 // for the other messages the return value is ignored and there is
2368 // nothing special to do