1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/treectrl.cpp
4 // Author: Julian Smart
5 // Modified by: Vadim Zeitlin to be less MSW-specific on 10.10.98
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/treectrl.h"
32 #include "wx/msw/wrapcctl.h" // include <commctrl.h> "properly"
33 #include "wx/msw/missing.h"
34 #include "wx/dynarray.h"
37 #include "wx/settings.h"
40 #include "wx/msw/private.h"
42 // Set this to 1 to be _absolutely_ sure that repainting will work for all
43 // comctl32.dll versions
44 #define wxUSE_COMCTL32_SAFELY 0
46 #include "wx/imaglist.h"
47 #include "wx/msw/dragimag.h"
49 // macros to hide the cast ugliness
50 // --------------------------------
52 // get HTREEITEM from wxTreeItemId
53 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
55 // the native control doesn't support multiple selections under MSW and we
56 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
57 // checkboxes be the selection status (checked == selected) or by really
58 // emulating everything, i.e. intercepting mouse and key events &c. The first
59 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
61 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 // wrapper for TreeView_HitTest
68 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
74 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
77 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
79 // wrappers for TreeView_GetItem/TreeView_SetItem
80 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
84 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
85 tvi
.stateMask
= TVIS_SELECTED
;
88 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
90 wxLogLastError(wxT("TreeView_GetItem"));
93 return (tvi
.state
& TVIS_SELECTED
) != 0;
96 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
99 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
100 tvi
.stateMask
= TVIS_SELECTED
;
101 tvi
.state
= select
? TVIS_SELECTED
: 0;
104 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
106 wxLogLastError(wxT("TreeView_SetItem"));
113 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
115 SelectItem(hwndTV
, htItem
, false);
118 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
120 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
123 // helper function which selects all items in a range and, optionally,
124 // unselects all others
125 static void SelectRange(HWND hwndTV
,
128 bool unselectOthers
= true)
130 // find the first (or last) item and select it
132 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
133 while ( htItem
&& cont
)
135 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
137 if ( !IsItemSelected(hwndTV
, htItem
) )
139 SelectItem(hwndTV
, htItem
);
146 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
148 UnselectItem(hwndTV
, htItem
);
152 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
155 // select the items in range
156 cont
= htFirst
!= htLast
;
157 while ( htItem
&& cont
)
159 if ( !IsItemSelected(hwndTV
, htItem
) )
161 SelectItem(hwndTV
, htItem
);
164 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
166 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
170 if ( unselectOthers
)
174 if ( IsItemSelected(hwndTV
, htItem
) )
176 UnselectItem(hwndTV
, htItem
);
179 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
183 // seems to be necessary - otherwise the just selected items don't always
184 // appear as selected
185 UpdateWindow(hwndTV
);
188 // helper function which tricks the standard control into changing the focused
189 // item without changing anything else (if someone knows why Microsoft doesn't
190 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
191 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
194 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
199 if ( htItem
!= htFocus
)
201 // remember the selection state of the item
202 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
204 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
206 // prevent the tree from unselecting the old focus which it
207 // would do by default (TreeView_SelectItem unselects the
209 TreeView_SelectItem(hwndTV
, 0);
210 SelectItem(hwndTV
, htFocus
);
213 TreeView_SelectItem(hwndTV
, htItem
);
217 // need to clear the selection which TreeView_SelectItem() gave
219 UnselectItem(hwndTV
, htItem
);
221 //else: was selected, still selected - ok
223 //else: nothing to do, focus already there
229 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
231 // just clear the focus
232 TreeView_SelectItem(hwndTV
, 0);
234 if ( wasFocusSelected
)
236 // restore the selection state
237 SelectItem(hwndTV
, htFocus
);
240 //else: nothing to do, no focus already
244 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
250 // a convenient wrapper around TV_ITEM struct which adds a ctor
252 #pragma warning( disable : 4097 ) // inheriting from typedef
255 struct wxTreeViewItem
: public TV_ITEM
257 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
258 UINT mask_
, // fields which are valid
259 UINT stateMask_
= 0) // for TVIF_STATE only
263 // hItem member is always valid
264 mask
= mask_
| TVIF_HANDLE
;
265 stateMask
= stateMask_
;
270 // ----------------------------------------------------------------------------
271 // This class is our userdata/lParam for the TV_ITEMs stored in the treeview.
273 // We need this for a couple of reasons:
275 // 1) This class is needed for support of different images: the Win32 common
276 // control natively supports only 2 images (the normal one and another for the
277 // selected state). We wish to provide support for 2 more of them for folder
278 // items (i.e. those which have children): for expanded state and for expanded
279 // selected state. For this we use this structure to store the additional items
282 // 2) This class is also needed to hold the HITEM so that we can sort
283 // it correctly in the MSW sort callback.
285 // In addition it makes other workarounds such as this easier and helps
286 // simplify the code.
287 // ----------------------------------------------------------------------------
289 class wxTreeItemParam
296 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
302 // dtor deletes the associated data as well
303 virtual ~wxTreeItemParam() { delete m_data
; }
306 // get the real data associated with the item
307 wxTreeItemData
*GetData() const { return m_data
; }
309 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
311 // do we have such image?
312 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
313 // get image, falling back to the other images if this one is not
315 int GetImage(wxTreeItemIcon which
) const
317 int image
= m_images
[which
];
322 case wxTreeItemIcon_SelectedExpanded
:
323 image
= GetImage(wxTreeItemIcon_Expanded
);
328 case wxTreeItemIcon_Selected
:
329 case wxTreeItemIcon_Expanded
:
330 image
= GetImage(wxTreeItemIcon_Normal
);
333 case wxTreeItemIcon_Normal
:
338 wxFAIL_MSG( _T("unsupported wxTreeItemIcon value") );
344 // change the given image
345 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
348 const wxTreeItemId
& GetItem() const { return m_item
; }
350 void SetItem(const wxTreeItemId
& item
) { m_item
= item
; }
353 // all the images associated with the item
354 int m_images
[wxTreeItemIcon_Max
];
356 // item for sort callbacks
359 // the real client data
360 wxTreeItemData
*m_data
;
362 DECLARE_NO_COPY_CLASS(wxTreeItemParam
)
365 // wxVirutalNode is used in place of a single root when 'hidden' root is
367 class wxVirtualNode
: public wxTreeViewItem
370 wxVirtualNode(wxTreeItemParam
*param
)
371 : wxTreeViewItem(TVI_ROOT
, 0)
381 wxTreeItemParam
*GetParam() const { return m_param
; }
382 void SetParam(wxTreeItemParam
*param
) { delete m_param
; m_param
= param
; }
385 wxTreeItemParam
*m_param
;
387 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
391 #pragma warning( default : 4097 )
394 // a macro to get the virtual root, returns NULL if none
395 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
397 // returns true if the item is the virtual root
398 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
400 // a class which encapsulates the tree traversal logic: it vists all (unless
401 // OnVisit() returns false) items under the given one
402 class wxTreeTraversal
405 wxTreeTraversal(const wxTreeCtrl
*tree
)
410 // give it a virtual dtor: not really needed as the class is never used
411 // polymorphically and not even allocated on heap at all, but this is safer
412 // (in case it ever is) and silences the compiler warnings for now
413 virtual ~wxTreeTraversal() { }
415 // do traverse the tree: visit all items (recursively by default) under the
416 // given one; return true if all items were traversed or false if the
417 // traversal was aborted because OnVisit returned false
418 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
420 // override this function to do whatever is needed for each item, return
421 // false to stop traversing
422 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
425 const wxTreeCtrl
*GetTree() const { return m_tree
; }
428 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
430 const wxTreeCtrl
*m_tree
;
432 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
435 // internal class for getting the selected items
436 class TraverseSelections
: public wxTreeTraversal
439 TraverseSelections(const wxTreeCtrl
*tree
,
440 wxArrayTreeItemIds
& selections
)
441 : wxTreeTraversal(tree
), m_selections(selections
)
443 m_selections
.Empty();
445 if (tree
->GetCount() > 0)
446 DoTraverse(tree
->GetRootItem());
449 virtual bool OnVisit(const wxTreeItemId
& item
)
451 const wxTreeCtrl
* const tree
= GetTree();
453 // can't visit a virtual node.
454 if ( (tree
->GetRootItem() == item
) && tree
->HasFlag(wxTR_HIDE_ROOT
) )
459 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
460 if ( tree
->IsItemChecked(item
) )
462 if ( ::IsItemSelected(GetHwndOf(tree
), HITEM(item
)) )
465 m_selections
.Add(item
);
471 size_t GetCount() const { return m_selections
.GetCount(); }
474 wxArrayTreeItemIds
& m_selections
;
476 DECLARE_NO_COPY_CLASS(TraverseSelections
)
479 // internal class for counting tree items
480 class TraverseCounter
: public wxTreeTraversal
483 TraverseCounter(const wxTreeCtrl
*tree
,
484 const wxTreeItemId
& root
,
486 : wxTreeTraversal(tree
)
490 DoTraverse(root
, recursively
);
493 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
500 size_t GetCount() const { return m_count
; }
505 DECLARE_NO_COPY_CLASS(TraverseCounter
)
508 // ----------------------------------------------------------------------------
510 // ----------------------------------------------------------------------------
512 #if wxUSE_EXTENDED_RTTI
513 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
515 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
516 // new style border flags, we put them first to
517 // use them for streaming out
518 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
519 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
520 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
521 wxFLAGS_MEMBER(wxBORDER_RAISED
)
522 wxFLAGS_MEMBER(wxBORDER_STATIC
)
523 wxFLAGS_MEMBER(wxBORDER_NONE
)
525 // old style border flags
526 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
527 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
528 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
529 wxFLAGS_MEMBER(wxRAISED_BORDER
)
530 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
531 wxFLAGS_MEMBER(wxBORDER
)
533 // standard window styles
534 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
535 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
536 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
537 wxFLAGS_MEMBER(wxWANTS_CHARS
)
538 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
539 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
540 wxFLAGS_MEMBER(wxVSCROLL
)
541 wxFLAGS_MEMBER(wxHSCROLL
)
543 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
544 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
545 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
546 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
547 wxFLAGS_MEMBER(wxTR_NO_LINES
)
548 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
549 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
550 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
551 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
552 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
553 wxFLAGS_MEMBER(wxTR_SINGLE
)
554 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
555 wxFLAGS_MEMBER(wxTR_EXTENDED
)
556 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
558 wxEND_FLAGS( wxTreeCtrlStyle
)
560 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
562 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
563 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
564 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
565 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
566 wxEND_PROPERTIES_TABLE()
568 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
569 wxEND_HANDLERS_TABLE()
571 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
573 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
576 // ----------------------------------------------------------------------------
578 // ----------------------------------------------------------------------------
580 // indices in gs_expandEvents table below
595 // handy table for sending events - it has to be initialized during run-time
596 // now so can't be const any more
597 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
600 but logically it's a const table with the following entries:
603 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
604 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
608 // ============================================================================
610 // ============================================================================
612 // ----------------------------------------------------------------------------
614 // ----------------------------------------------------------------------------
616 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
618 if ( !OnVisit(root
) )
621 return Traverse(root
, recursively
);
624 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
626 wxTreeItemIdValue cookie
;
627 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
628 while ( child
.IsOk() )
630 // depth first traversal
631 if ( recursively
&& !Traverse(child
, true) )
634 if ( !OnVisit(child
) )
637 child
= m_tree
->GetNextChild(root
, cookie
);
643 // ----------------------------------------------------------------------------
644 // construction and destruction
645 // ----------------------------------------------------------------------------
647 void wxTreeCtrl::Init()
650 m_hasAnyAttr
= false;
652 m_pVirtualRoot
= NULL
;
654 // initialize the global array of events now as it can't be done statically
655 // with the wxEVT_XXX values being allocated during run-time only
656 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
657 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
658 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
659 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
662 bool wxTreeCtrl::Create(wxWindow
*parent
,
667 const wxValidator
& validator
,
668 const wxString
& name
)
672 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
673 style
|= wxBORDER_SUNKEN
;
675 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
679 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
680 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
682 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
683 wstyle
|= TVS_HASLINES
;
684 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
685 wstyle
|= TVS_HASBUTTONS
;
687 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
688 wstyle
|= TVS_EDITLABELS
;
690 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
691 wstyle
|= TVS_LINESATROOT
;
693 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
695 if ( wxApp::GetComCtl32Version() >= 471 )
696 wstyle
|= TVS_FULLROWSELECT
;
699 // using TVS_CHECKBOXES for emulation of a multiselection tree control
700 // doesn't work without the new enough headers
701 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
702 !defined( __GNUWIN32_OLD__ ) && \
703 !defined( __BORLANDC__ ) && \
704 !defined( __WATCOMC__ ) && \
705 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
707 // we emulate the multiple selection tree controls by using checkboxes: set
708 // up the image list we need for this if we do have multiple selections
709 if ( m_windowStyle
& wxTR_MULTIPLE
)
710 wstyle
|= TVS_CHECKBOXES
;
711 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
713 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
714 // Need so that TVN_GETINFOTIP messages will be sent
715 wstyle
|= TVS_INFOTIP
;
718 // Create the tree control.
719 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
722 #if wxUSE_COMCTL32_SAFELY
723 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
724 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
726 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
727 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
729 // This works around a bug in the Windows tree control whereby for some versions
730 // of comctrl32, setting any colour actually draws the background in black.
731 // This will initialise the background to the system colour.
732 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
733 // Assume the user has an updated comctl32.dll.
734 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
735 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
736 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
740 // VZ: this is some experimental code which may be used to get the
741 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
742 // AFAIK, the standard DLL does about the same thing anyhow.
744 if ( m_windowStyle
& wxTR_MULTIPLE
)
748 // create the DC compatible with the current screen
749 HDC hdcMem
= CreateCompatibleDC(NULL
);
751 // create a mono bitmap of the standard size
752 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
753 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
754 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
755 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
756 1, // # of color planes
757 1, // # bits needed for one pixel
758 0); // array containing colour data
759 SelectObject(hdcMem
, hbmpCheck
);
761 // then draw a check mark into it
762 RECT rect
= { 0, 0, x
, y
};
763 if ( !::DrawFrameControl(hdcMem
, &rect
,
765 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
767 wxLogLastError(wxT("DrawFrameControl(check)"));
770 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
771 imagelistCheckboxes
.Add(bmp
);
773 if ( !::DrawFrameControl(hdcMem
, &rect
,
777 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
780 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
781 imagelistCheckboxes
.Add(bmp
);
787 SetStateImageList(&imagelistCheckboxes
);
791 wxSetCCUnicodeFormat(GetHwnd());
796 wxTreeCtrl::~wxTreeCtrl()
798 // delete any attributes
801 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
803 // prevent TVN_DELETEITEM handler from deleting the attributes again!
804 m_hasAnyAttr
= false;
809 // delete user data to prevent memory leaks
810 // also deletes hidden root node storage.
814 // ----------------------------------------------------------------------------
816 // ----------------------------------------------------------------------------
818 /* static */ wxVisualAttributes
819 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
821 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
823 // common controls have their own default font
824 attrs
.font
= wxGetCCDefaultFont();
830 // simple wrappers which add error checking in debug mode
832 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
*tvItem
) const
834 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
835 _T("can't retrieve virtual root item") );
837 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
839 wxLogLastError(wxT("TreeView_GetItem"));
847 void wxTreeCtrl::DoSetItem(wxTreeViewItem
*tvItem
)
849 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
851 wxLogLastError(wxT("TreeView_SetItem"));
855 unsigned int wxTreeCtrl::GetCount() const
857 return (unsigned int)TreeView_GetCount(GetHwnd());
860 unsigned int wxTreeCtrl::GetIndent() const
862 return TreeView_GetIndent(GetHwnd());
865 void wxTreeCtrl::SetIndent(unsigned int indent
)
867 TreeView_SetIndent(GetHwnd(), indent
);
870 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
873 (void) TreeView_SetImageList(GetHwnd(),
874 imageList
? imageList
->GetHIMAGELIST() : 0,
878 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
880 if (m_ownsImageListNormal
)
881 delete m_imageListNormal
;
883 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
884 m_ownsImageListNormal
= false;
887 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
889 if (m_ownsImageListState
) delete m_imageListState
;
890 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
891 m_ownsImageListState
= false;
894 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
895 bool recursively
) const
897 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
899 TraverseCounter
counter(this, item
, recursively
);
900 return counter
.GetCount() - 1;
903 // ----------------------------------------------------------------------------
905 // ----------------------------------------------------------------------------
907 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
909 #if !wxUSE_COMCTL32_SAFELY
910 if ( !wxWindowBase::SetBackgroundColour(colour
) )
913 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
919 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
921 #if !wxUSE_COMCTL32_SAFELY
922 if ( !wxWindowBase::SetForegroundColour(colour
) )
925 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
931 // ----------------------------------------------------------------------------
933 // ----------------------------------------------------------------------------
935 bool wxTreeCtrl::IsHiddenRoot(const wxTreeItemId
& item
) const
937 return HITEM(item
) == TVI_ROOT
&& HasFlag(wxTR_HIDE_ROOT
);
940 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
942 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
944 wxChar buf
[512]; // the size is arbitrary...
946 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
947 tvItem
.pszText
= buf
;
948 tvItem
.cchTextMax
= WXSIZEOF(buf
);
949 if ( !DoGetItem(&tvItem
) )
951 // don't return some garbage which was on stack, but an empty string
955 return wxString(buf
);
958 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
960 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
962 if ( IS_VIRTUAL_ROOT(item
) )
965 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
966 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
969 // when setting the text of the item being edited, the text control should
970 // be updated to reflect the new text as well, otherwise calling
971 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
973 // don't use GetEditControl() here because m_textCtrl is not set yet
974 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
977 if ( item
== m_idEdited
)
979 ::SetWindowText(hwndEdit
, text
);
984 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
985 wxTreeItemIcon which
) const
987 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
989 if ( IsHiddenRoot(item
) )
991 // no images for hidden root item
995 wxTreeItemParam
*param
= GetItemParam(item
);
997 return param
&& param
->HasImage(which
) ? param
->GetImage(which
) : -1;
1000 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1001 wxTreeItemIcon which
)
1003 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1004 wxCHECK_RET( which
>= 0 &&
1005 which
< wxTreeItemIcon_Max
,
1006 wxT("invalid image index"));
1009 if ( IsHiddenRoot(item
) )
1011 // no images for hidden root item
1015 wxTreeItemParam
*data
= GetItemParam(item
);
1019 data
->SetImage(image
, which
);
1024 wxTreeItemParam
*wxTreeCtrl::GetItemParam(const wxTreeItemId
& item
) const
1026 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1028 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1030 // hidden root may still have data.
1031 if ( IS_VIRTUAL_ROOT(item
) )
1033 return GET_VIRTUAL_ROOT()->GetParam();
1037 if ( !DoGetItem(&tvItem
) )
1042 return (wxTreeItemParam
*)tvItem
.lParam
;
1045 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1047 wxTreeItemParam
*data
= GetItemParam(item
);
1049 return data
? data
->GetData() : NULL
;
1052 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1054 // first, associate this piece of data with this item
1060 wxTreeItemParam
*param
= GetItemParam(item
);
1062 wxCHECK_RET( param
, wxT("failed to change tree items data") );
1064 param
->SetData(data
);
1067 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1069 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1071 if ( IS_VIRTUAL_ROOT(item
) )
1074 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1075 tvItem
.cChildren
= (int)has
;
1079 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1081 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1083 if ( IS_VIRTUAL_ROOT(item
) )
1086 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1087 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1091 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1093 if ( IS_VIRTUAL_ROOT(item
) )
1096 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1097 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1101 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1103 if ( IS_VIRTUAL_ROOT(item
) )
1107 if ( GetBoundingRect(item
, rect
) )
1113 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1115 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1117 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1118 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1121 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1123 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1125 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1126 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1129 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1131 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1133 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1134 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1137 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1138 const wxColour
& col
)
1140 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1142 wxTreeItemAttr
*attr
;
1143 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1144 if ( it
== m_attrs
.end() )
1146 m_hasAnyAttr
= true;
1148 m_attrs
[item
.m_pItem
] =
1149 attr
= new wxTreeItemAttr
;
1156 attr
->SetTextColour(col
);
1161 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1162 const wxColour
& col
)
1164 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1166 wxTreeItemAttr
*attr
;
1167 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1168 if ( it
== m_attrs
.end() )
1170 m_hasAnyAttr
= true;
1172 m_attrs
[item
.m_pItem
] =
1173 attr
= new wxTreeItemAttr
;
1175 else // already in the hash
1180 attr
->SetBackgroundColour(col
);
1185 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1187 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1189 wxTreeItemAttr
*attr
;
1190 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1191 if ( it
== m_attrs
.end() )
1193 m_hasAnyAttr
= true;
1195 m_attrs
[item
.m_pItem
] =
1196 attr
= new wxTreeItemAttr
;
1198 else // already in the hash
1203 attr
->SetFont(font
);
1208 // ----------------------------------------------------------------------------
1210 // ----------------------------------------------------------------------------
1212 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1214 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1216 if ( item
== wxTreeItemId(TVI_ROOT
) )
1218 // virtual (hidden) root is never visible
1222 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1225 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1226 // the HTREEITEM with TVM_GETITEMRECT
1227 *(HTREEITEM
*)&rect
= HITEM(item
);
1229 // true means to get rect for just the text, not the whole line
1230 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1232 // if TVM_GETITEMRECT returned false, then the item is definitely not
1233 // visible (because its parent is not expanded)
1237 // however if it returned true, the item might still be outside the
1238 // currently visible part of the tree, test for it (notice that partly
1239 // visible means visible here)
1240 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1243 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1245 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1247 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1250 return tvItem
.cChildren
!= 0;
1253 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1255 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1257 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1260 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1263 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1265 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1267 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1270 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1273 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1275 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1277 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1280 return (tvItem
.state
& TVIS_BOLD
) != 0;
1283 // ----------------------------------------------------------------------------
1285 // ----------------------------------------------------------------------------
1287 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1289 // Root may be real (visible) or virtual (hidden).
1290 if ( GET_VIRTUAL_ROOT() )
1293 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1296 wxTreeItemId
wxTreeCtrl::GetSelection() const
1298 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1299 wxT("this only works with single selection controls") );
1301 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1304 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1306 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1310 if ( IS_VIRTUAL_ROOT(item
) )
1312 // no parent for the virtual root
1317 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1318 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1320 // the top level items should have the virtual root as their parent
1325 return wxTreeItemId(hItem
);
1328 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1329 wxTreeItemIdValue
& cookie
) const
1331 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1333 // remember the last child returned in 'cookie'
1334 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1336 return wxTreeItemId(cookie
);
1339 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1340 wxTreeItemIdValue
& cookie
) const
1342 wxTreeItemId
fromCookie(cookie
);
1344 HTREEITEM hitem
= HITEM(fromCookie
);
1346 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1348 wxTreeItemId
item(hitem
);
1350 cookie
= item
.m_pItem
;
1355 #if WXWIN_COMPATIBILITY_2_4
1357 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1360 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1362 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1364 return wxTreeItemId((void *)cookie
);
1367 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1370 wxTreeItemId
fromCookie((void *)cookie
);
1372 HTREEITEM hitem
= HITEM(fromCookie
);
1374 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1376 wxTreeItemId
item(hitem
);
1378 cookie
= (long)item
.m_pItem
;
1383 #endif // WXWIN_COMPATIBILITY_2_4
1385 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1387 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1389 // can this be done more efficiently?
1390 wxTreeItemIdValue cookie
;
1392 wxTreeItemId childLast
,
1393 child
= GetFirstChild(item
, cookie
);
1394 while ( child
.IsOk() )
1397 child
= GetNextChild(item
, cookie
);
1403 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1405 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1406 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1409 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1411 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1412 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1415 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1417 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1420 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1422 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1423 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1425 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1428 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1430 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1431 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1433 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1436 // ----------------------------------------------------------------------------
1437 // multiple selections emulation
1438 // ----------------------------------------------------------------------------
1440 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1442 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1444 // receive the desired information.
1445 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1448 // state image indices are 1 based
1449 return ((tvItem
.state
>> 12) - 1) == 1;
1452 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1454 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1456 // receive the desired information.
1457 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1461 // state images are one-based
1462 tvItem
.state
= (check
? 2 : 1) << 12;
1467 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1469 TraverseSelections
selector(this, selections
);
1471 return selector
.GetCount();
1474 // ----------------------------------------------------------------------------
1476 // ----------------------------------------------------------------------------
1478 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1479 const wxTreeItemId
& hInsertAfter
,
1480 const wxString
& text
,
1481 int image
, int selectedImage
,
1482 wxTreeItemData
*data
)
1484 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1486 _T("can't have more than one root in the tree") );
1488 TV_INSERTSTRUCT tvIns
;
1489 tvIns
.hParent
= HITEM(parent
);
1490 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1492 // this is how we insert the item as the first child: supply a NULL
1494 if ( !tvIns
.hInsertAfter
)
1496 tvIns
.hInsertAfter
= TVI_FIRST
;
1500 if ( !text
.empty() )
1503 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1507 tvIns
.item
.pszText
= NULL
;
1508 tvIns
.item
.cchTextMax
= 0;
1511 // create the param which will store the other item parameters
1512 wxTreeItemParam
*param
= new wxTreeItemParam
;
1514 // we return the images on demand as they depend on whether the item is
1515 // expanded or collapsed too in our case
1516 mask
|= TVIF_IMAGE
| TVIF_SELECTEDIMAGE
;
1517 tvIns
.item
.iImage
= I_IMAGECALLBACK
;
1518 tvIns
.item
.iSelectedImage
= I_IMAGECALLBACK
;
1520 param
->SetImage(image
, wxTreeItemIcon_Normal
);
1521 param
->SetImage(selectedImage
, wxTreeItemIcon_Selected
);
1524 tvIns
.item
.lParam
= (LPARAM
)param
;
1525 tvIns
.item
.mask
= mask
;
1527 HTREEITEM id
= TreeView_InsertItem(GetHwnd(), &tvIns
);
1530 wxLogLastError(wxT("TreeView_InsertItem"));
1533 // associate the application tree item with Win32 tree item handle
1536 // setup wxTreeItemData
1539 param
->SetData(data
);
1543 return wxTreeItemId(id
);
1546 // for compatibility only
1547 #if WXWIN_COMPATIBILITY_2_4
1549 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1551 SetImageList(imageList
);
1554 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1556 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1559 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1561 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1564 #endif // WXWIN_COMPATIBILITY_2_4
1566 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1567 int image
, int selectedImage
,
1568 wxTreeItemData
*data
)
1571 if ( HasFlag(wxTR_HIDE_ROOT
) )
1573 // create a virtual root item, the parent for all the others
1574 wxTreeItemParam
*param
= new wxTreeItemParam
;
1575 param
->SetData(data
);
1577 m_pVirtualRoot
= new wxVirtualNode(param
);
1582 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1583 text
, image
, selectedImage
, data
);
1586 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1588 const wxString
& text
,
1589 int image
, int selectedImage
,
1590 wxTreeItemData
*data
)
1592 wxTreeItemId idPrev
;
1593 if ( index
== (size_t)-1 )
1595 // special value: append to the end
1598 else // find the item from index
1600 wxTreeItemIdValue cookie
;
1601 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1602 while ( index
!= 0 && idCur
.IsOk() )
1607 idCur
= GetNextChild(parent
, cookie
);
1610 // assert, not check: if the index is invalid, we will append the item
1612 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1615 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1618 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1620 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1622 wxLogLastError(wxT("TreeView_DeleteItem"));
1626 // delete all children (but don't delete the item itself)
1627 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1629 wxTreeItemIdValue cookie
;
1631 wxArrayTreeItemIds children
;
1632 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1633 while ( child
.IsOk() )
1635 children
.Add(child
);
1637 child
= GetNextChild(item
, cookie
);
1640 size_t nCount
= children
.Count();
1641 for ( size_t n
= 0; n
< nCount
; n
++ )
1643 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1645 wxLogLastError(wxT("TreeView_DeleteItem"));
1650 void wxTreeCtrl::DeleteAllItems()
1652 // delete the "virtual" root item.
1653 if ( GET_VIRTUAL_ROOT() )
1655 delete GET_VIRTUAL_ROOT();
1656 m_pVirtualRoot
= NULL
;
1659 // and all the real items
1661 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1663 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1667 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1669 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1670 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1671 flag
== TVE_EXPAND
||
1673 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1675 // A hidden root can be neither expanded nor collapsed.
1676 wxCHECK_RET( !IsHiddenRoot(item
),
1677 wxT("Can't expand/collapse hidden root node!") );
1679 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1680 // emulate them. This behaviour has changed slightly with comctl32.dll
1681 // v 4.70 - now it does send them but only the first time. To maintain
1682 // compatible behaviour and also in order to not have surprises with the
1683 // future versions, don't rely on this and still do everything ourselves.
1684 // To avoid that the messages be sent twice when the item is expanded for
1685 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1687 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1691 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1693 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1695 wxTreeEvent
event(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1699 (void)GetEventHandler()->ProcessEvent(event
);
1701 //else: change didn't took place, so do nothing at all
1704 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1706 DoExpand(item
, TVE_EXPAND
);
1709 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1711 DoExpand(item
, TVE_COLLAPSE
);
1714 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1716 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1719 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1721 DoExpand(item
, TVE_TOGGLE
);
1724 #if WXWIN_COMPATIBILITY_2_4
1726 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1728 DoExpand(item
, action
);
1733 void wxTreeCtrl::Unselect()
1735 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1736 wxT("doesn't make sense, may be you want UnselectAll()?") );
1738 // just remove the selection
1739 SelectItem(wxTreeItemId());
1742 void wxTreeCtrl::UnselectAll()
1744 if ( m_windowStyle
& wxTR_MULTIPLE
)
1746 wxArrayTreeItemIds selections
;
1747 size_t count
= GetSelections(selections
);
1748 for ( size_t n
= 0; n
< count
; n
++ )
1750 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1751 SetItemCheck(HITEM(selections
[n
]), false);
1752 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1753 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1754 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1757 m_htSelStart
.Unset();
1761 // just remove the selection
1766 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1768 if ( m_windowStyle
& wxTR_MULTIPLE
)
1770 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1771 // selecting the item means checking it
1772 SetItemCheck(item
, select
);
1773 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1774 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1775 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1779 wxASSERT_MSG( select
,
1780 _T("SelectItem(false) works only for multiselect") );
1782 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1783 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1784 // send them ourselves
1786 wxTreeEvent
event(wxEVT_COMMAND_TREE_SEL_CHANGING
, this, item
);
1787 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1789 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1791 wxLogLastError(wxT("TreeView_SelectItem"));
1795 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1796 (void)GetEventHandler()->ProcessEvent(event
);
1799 //else: program vetoed the change
1803 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1805 wxCHECK_RET( !IsHiddenRoot(item
), _T("can't show hidden root item") );
1808 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1811 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1813 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1815 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1819 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1824 void wxTreeCtrl::DeleteTextCtrl()
1828 // the HWND corresponding to this control is deleted by the tree
1829 // control itself and we don't know when exactly this happens, so check
1830 // if the window still exists before calling UnsubclassWin()
1831 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1833 m_textCtrl
->SetHWND(0);
1836 m_textCtrl
->UnsubclassWin();
1837 m_textCtrl
->SetHWND(0);
1845 wxTextCtrl
*wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1846 wxClassInfo
*textControlClass
)
1848 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1853 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1854 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1856 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1865 // textctrl is subclassed in MSWOnNotify
1869 // End label editing, optionally cancelling the edit
1870 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
1872 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1877 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
) const
1879 TV_HITTESTINFO hitTestInfo
;
1880 hitTestInfo
.pt
.x
= (int)point
.x
;
1881 hitTestInfo
.pt
.y
= (int)point
.y
;
1883 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1888 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1889 flags |= wxTREE_HITTEST_##flag
1891 TRANSLATE_FLAG(ABOVE
);
1892 TRANSLATE_FLAG(BELOW
);
1893 TRANSLATE_FLAG(NOWHERE
);
1894 TRANSLATE_FLAG(ONITEMBUTTON
);
1895 TRANSLATE_FLAG(ONITEMICON
);
1896 TRANSLATE_FLAG(ONITEMINDENT
);
1897 TRANSLATE_FLAG(ONITEMLABEL
);
1898 TRANSLATE_FLAG(ONITEMRIGHT
);
1899 TRANSLATE_FLAG(ONITEMSTATEICON
);
1900 TRANSLATE_FLAG(TOLEFT
);
1901 TRANSLATE_FLAG(TORIGHT
);
1903 #undef TRANSLATE_FLAG
1905 return wxTreeItemId(hitTestInfo
.hItem
);
1908 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1910 bool textOnly
) const
1914 // Virtual root items have no bounding rectangle
1915 if ( IS_VIRTUAL_ROOT(item
) )
1920 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
1923 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
1929 // couldn't retrieve rect: for example, item isn't visible
1934 // ----------------------------------------------------------------------------
1936 // ----------------------------------------------------------------------------
1938 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
1939 // functions such as IsDataIndirect()
1940 class wxTreeSortHelper
1943 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
1946 static wxTreeItemId
GetIdFromData(LPARAM lParam
)
1948 return ((wxTreeItemParam
*)lParam
)->GetItem();
1952 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
1956 wxCHECK_MSG( pItem1
&& pItem2
, 0,
1957 wxT("sorting tree without data doesn't make sense") );
1959 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
1961 return tree
->OnCompareItems(GetIdFromData(pItem1
),
1962 GetIdFromData(pItem2
));
1965 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
1967 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1969 // rely on the fact that TreeView_SortChildren does the same thing as our
1970 // default behaviour, i.e. sorts items alphabetically and so call it
1971 // directly if we're not in derived class (much more efficient!)
1972 // RN: Note that if you find you're code doesn't sort as expected this
1973 // may be why as if you don't use the DECLARE_CLASS/IMPLEMENT_CLASS
1974 // combo for your derived wxTreeCtrl if will sort without
1976 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
1978 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
1983 tvSort
.hParent
= HITEM(item
);
1984 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
1985 tvSort
.lParam
= (LPARAM
)this;
1986 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
1990 // ----------------------------------------------------------------------------
1992 // ----------------------------------------------------------------------------
1994 bool wxTreeCtrl::MSWShouldPreProcessMessage(WXMSG
* msg
)
1996 if ( msg
->message
== WM_KEYDOWN
)
1998 if ( msg
->wParam
== VK_RETURN
)
2000 // we need VK_RETURN to generate wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2005 return wxTreeCtrlBase::MSWShouldPreProcessMessage(msg
);
2008 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2010 if ( cmd
== EN_UPDATE
)
2012 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2013 event
.SetEventObject( this );
2014 ProcessCommand(event
);
2016 else if ( cmd
== EN_KILLFOCUS
)
2018 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2019 event
.SetEventObject( this );
2020 ProcessCommand(event
);
2028 // command processed
2032 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2033 // only do it during dragging, minimize wxWin overhead (this is important for
2034 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2035 // instead of passing by wxWin events
2036 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2038 bool processed
= false;
2040 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2042 // This message is sent after a right-click, or when the "menu" key is pressed
2043 if ( nMsg
== WM_CONTEXTMENU
)
2045 int x
= GET_X_LPARAM(lParam
),
2046 y
= GET_Y_LPARAM(lParam
);
2047 // Convert the screen point to a client point
2048 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2050 // can't use GetSelection() here as it would assert in multiselect mode
2051 wxTreeEvent
event(wxEVT_COMMAND_TREE_ITEM_MENU
, this,
2052 wxTreeItemId(TreeView_GetSelection(GetHwnd())));
2054 // Get the bounding rectangle for the item, including the non-text areas
2056 GetBoundingRect(event
.m_item
, ItemRect
, false);
2057 // If the point is inside the bounding rectangle, use it as the click position.
2058 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2059 if (ItemRect
.Contains(MenuPoint
))
2061 event
.m_pointDrag
= MenuPoint
;
2063 // Use the Explorer standard of putting the menu at the left edge of the text,
2064 // in the vertical middle of the text. Should be the case for the "menu" key
2067 // Use the bounding rectangle of only the text part
2068 GetBoundingRect(event
.m_item
, ItemRect
, true);
2069 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2072 if ( GetEventHandler()->ProcessEvent(event
) )
2074 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2076 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2078 // we only process mouse messages here and these parameters have the
2079 // same meaning for all of them
2080 int x
= GET_X_LPARAM(lParam
),
2081 y
= GET_Y_LPARAM(lParam
);
2082 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2084 TV_HITTESTINFO tvht
;
2088 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2092 case WM_RBUTTONDOWN
:
2093 // if the item we are about to right click on is not already
2094 // selected or if we click outside of any item, remove the
2095 // entire previous selection
2096 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2101 // select item and set the focus to the
2102 // newly selected item
2103 ::SelectItem(GetHwnd(), htItem
);
2104 ::SetFocus(GetHwnd(), htItem
);
2107 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2108 case WM_LBUTTONDOWN
:
2109 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2111 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2112 m_ptClick
= wxPoint(x
, y
);
2114 if ( wParam
& MK_CONTROL
)
2118 // toggle selected state
2119 ::ToggleItemSelection(GetHwnd(), htItem
);
2121 ::SetFocus(GetHwnd(), htItem
);
2123 // reset on any click without Shift
2124 m_htSelStart
.Unset();
2128 else if ( wParam
& MK_SHIFT
)
2130 // this selects all items between the starting one and
2133 if ( !m_htSelStart
)
2135 // take the focused item
2136 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2140 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2141 !(wParam
& MK_CONTROL
));
2143 ::SelectItem(GetHwnd(), htItem
);
2145 ::SetFocus(GetHwnd(), htItem
);
2149 else // normal click
2151 // avoid doing anything if we click on the only
2152 // currently selected item
2156 wxArrayTreeItemIds selections
;
2157 size_t count
= GetSelections(selections
);
2160 HITEM(selections
[0]) != htItem
)
2162 // clear the previously selected items, if the
2163 // user clicked outside of the present selection.
2164 // otherwise, perform the deselection on mouse-up.
2165 // this allows multiple drag and drop to work.
2167 if (!IsItemSelected(GetHwnd(), htItem
))
2171 // prevent the click from starting in-place editing
2172 // which should only happen if we click on the
2173 // already selected item (and nothing else is
2176 TreeView_SelectItem(GetHwnd(), 0);
2177 ::SelectItem(GetHwnd(), htItem
);
2179 ::SetFocus(GetHwnd(), htItem
);
2183 // reset on any click without Shift
2184 m_htSelStart
.Unset();
2188 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2192 if ( m_htClickedItem
)
2194 int cx
= abs(m_ptClick
.x
- x
);
2195 int cy
= abs(m_ptClick
.y
- y
);
2197 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2199 HWND pWnd
= ::GetParent( GetHwnd() );
2204 tv
.hdr
.hwndFrom
= GetHwnd();
2205 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2206 tv
.hdr
.code
= TVN_BEGINDRAG
;
2208 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2211 ZeroMemory(&tviAux
, sizeof(tviAux
));
2212 tviAux
.hItem
= HITEM(m_htClickedItem
);
2213 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2214 tviAux
.stateMask
= 0xffffffff;
2215 TreeView_GetItem( GetHwnd(), &tviAux
);
2217 tv
.itemNew
.state
= tviAux
.state
;
2218 tv
.itemNew
.lParam
= tviAux
.lParam
;
2223 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2225 m_htClickedItem
.Unset();
2228 #endif // __WXWINCE__
2232 m_dragImage
->Move(wxPoint(x
, y
));
2235 // highlight the item as target (hiding drag image is
2236 // necessary - otherwise the display will be corrupted)
2237 m_dragImage
->Hide();
2238 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2239 m_dragImage
->Show();
2246 // facilitates multiple drag-and-drop
2247 if (htItem
&& isMultiple
)
2249 wxArrayTreeItemIds selections
;
2250 size_t count
= GetSelections(selections
);
2253 !(wParam
& MK_CONTROL
) &&
2254 !(wParam
& MK_SHIFT
))
2257 TreeView_SelectItem(GetHwnd(), htItem
);
2258 ::SelectItem(GetHwnd(), htItem
);
2259 ::SetFocus(GetHwnd(), htItem
);
2261 m_htClickedItem
.Unset();
2269 m_dragImage
->EndDrag();
2273 // generate the drag end event
2274 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, this, htItem
);
2275 (void)GetEventHandler()->ProcessEvent(event
);
2277 // if we don't do it, the tree seems to think that 2 items
2278 // are selected simultaneously which is quite weird
2279 TreeView_SelectDropTarget(GetHwnd(), 0);
2284 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2285 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2287 // the tree control greys out the selected item when it loses focus and
2288 // paints it as selected again when it regains it, but it won't do it
2289 // for the other items itself - help it
2290 wxArrayTreeItemIds selections
;
2291 size_t count
= GetSelections(selections
);
2293 for ( size_t n
= 0; n
< count
; n
++ )
2295 // TreeView_GetItemRect() will return false if item is not visible,
2296 // which may happen perfectly well
2297 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2300 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2304 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2306 bool bCtrl
= wxIsCtrlDown(),
2307 bShift
= wxIsShiftDown();
2309 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2315 ::ToggleItemSelection(GetHwnd(), htSel
);
2321 ::SelectItem(GetHwnd(), htSel
);
2329 if ( !bCtrl
&& !bShift
)
2331 // no modifiers, just clear selection and then let the default
2332 // processing to take place
2337 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2339 HTREEITEM htNext
= (HTREEITEM
)
2340 TreeView_GetNextItem
2344 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2350 // at the top/bottom
2356 if ( !m_htSelStart
)
2357 m_htSelStart
= htSel
;
2359 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2363 // without changing selection
2364 ::SetFocus(GetHwnd(), htNext
);
2375 // TODO: handle Shift/Ctrl with these keys
2376 if ( !bCtrl
&& !bShift
)
2380 m_htSelStart
.Unset();
2384 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2385 else if ( nMsg
== WM_COMMAND
)
2387 // if we receive a EN_KILLFOCUS command from the in-place edit control
2388 // used for label editing, make sure to end editing
2391 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2393 if ( cmd
== EN_KILLFOCUS
)
2395 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2405 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2411 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2413 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2414 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2415 // the parent window, not us, which completely breaks everything so simply
2416 // don't let it see this message at all
2417 if ( nMsg
== WM_RBUTTONDOWN
)
2420 // but because of the above we don't get NM_RCLICK which is normally
2421 // generated by tree window proc when the modal loop mentioned above ends
2422 // because the mouse is released -- synthesize it ourselves instead
2423 if ( nMsg
== WM_RBUTTONUP
)
2426 hdr
.hwndFrom
= GetHwnd();
2427 hdr
.idFrom
= GetId();
2428 hdr
.code
= NM_RCLICK
;
2431 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2433 // continue as usual
2436 if ( nMsg
== WM_CHAR
)
2438 // also don't let the control process Space and Return keys because it
2439 // doesn't do anything useful with them anyhow but always beeps
2440 // annoyingly when it receives them and there is no way to turn it off
2441 // simply if you just process TREEITEM_ACTIVATED event to which Space
2442 // and Enter presses are mapped in your code
2443 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2447 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2450 // process WM_NOTIFY Windows message
2451 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2453 wxTreeEvent
event(wxEVT_NULL
, this);
2454 wxEventType eventType
= wxEVT_NULL
;
2455 NMHDR
*hdr
= (NMHDR
*)lParam
;
2457 switch ( hdr
->code
)
2460 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2463 case TVN_BEGINRDRAG
:
2465 if ( eventType
== wxEVT_NULL
)
2466 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2467 //else: left drag, already set above
2469 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2471 event
.m_item
= tv
->itemNew
.hItem
;
2472 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2474 // don't allow dragging by default: the user code must
2475 // explicitly say that it wants to allow it to avoid breaking
2481 case TVN_BEGINLABELEDIT
:
2483 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2484 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2486 // although the user event handler may still veto it, it is
2487 // important to set it now so that calls to SetItemText() from
2488 // the event handler would change the text controls contents
2490 event
.m_item
= info
->item
.hItem
;
2491 event
.m_label
= info
->item
.pszText
;
2492 event
.m_editCancelled
= false;
2496 case TVN_DELETEITEM
:
2498 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2499 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2501 event
.m_item
= tv
->itemOld
.hItem
;
2505 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2506 if ( it
!= m_attrs
.end() )
2515 case TVN_ENDLABELEDIT
:
2517 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2518 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2520 event
.m_item
= info
->item
.hItem
;
2521 event
.m_label
= info
->item
.pszText
;
2522 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2527 // These *must* not be removed or TVN_GETINFOTIP will
2528 // not be processed each time the mouse is moved
2529 // and the tooltip will only ever update once.
2538 #ifdef TVN_GETINFOTIP
2539 case TVN_GETINFOTIP
:
2541 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2542 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2544 // Which item are we trying to get a tooltip for?
2545 event
.m_item
= info
->hItem
;
2552 case TVN_GETDISPINFO
:
2553 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2556 case TVN_SETDISPINFO
:
2558 if ( eventType
== wxEVT_NULL
)
2559 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2560 //else: get, already set above
2562 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2564 event
.m_item
= info
->item
.hItem
;
2568 case TVN_ITEMEXPANDING
:
2569 case TVN_ITEMEXPANDED
:
2571 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2574 switch ( tv
->action
)
2577 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2585 what
= IDX_COLLAPSE
;
2589 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2592 eventType
= gs_expandEvents
[what
][how
];
2594 event
.m_item
= tv
->itemNew
.hItem
;
2600 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2601 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2603 // fabricate the lParam and wParam parameters sufficiently
2604 // similar to the ones from a "real" WM_KEYDOWN so that
2605 // CreateKeyEvent() works correctly
2606 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2607 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2609 WXWPARAM wParam
= info
->wVKey
;
2611 int keyCode
= wxCharCodeMSWToWX(wParam
);
2614 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2619 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2624 // a separate event for Space/Return
2625 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2626 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2629 if ( !HasFlag(wxTR_MULTIPLE
) )
2630 item
= GetSelection();
2632 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2634 (void)GetEventHandler()->ProcessEvent(event2
);
2639 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2640 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2641 // we have to handle both messages:
2642 case TVN_SELCHANGEDA
:
2643 case TVN_SELCHANGEDW
:
2644 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2647 case TVN_SELCHANGINGA
:
2648 case TVN_SELCHANGINGW
:
2650 if ( eventType
== wxEVT_NULL
)
2651 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2652 //else: already set above
2654 if (hdr
->code
== TVN_SELCHANGINGW
||
2655 hdr
->code
== TVN_SELCHANGEDW
)
2657 NM_TREEVIEWW
*tv
= (NM_TREEVIEWW
*)lParam
;
2658 event
.m_item
= tv
->itemNew
.hItem
;
2659 event
.m_itemOld
= tv
->itemOld
.hItem
;
2663 NM_TREEVIEWA
*tv
= (NM_TREEVIEWA
*)lParam
;
2664 event
.m_item
= tv
->itemNew
.hItem
;
2665 event
.m_itemOld
= tv
->itemOld
.hItem
;
2670 // instead of explicitly checking for _WIN32_IE, check if the
2671 // required symbols are available in the headers
2672 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2675 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2676 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2677 switch ( nmcd
.dwDrawStage
)
2680 // if we've got any items with non standard attributes,
2681 // notify us before painting each item
2682 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2686 case CDDS_ITEMPREPAINT
:
2688 wxMapTreeAttr::iterator
2689 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2691 if ( it
== m_attrs
.end() )
2693 // nothing to do for this item
2694 *result
= CDRF_DODEFAULT
;
2698 wxTreeItemAttr
* const attr
= it
->second
;
2700 wxTreeViewItem
tvItem((void *)nmcd
.dwItemSpec
,
2701 TVIF_STATE
, TVIS_DROPHILITED
);
2703 const UINT tvItemState
= tvItem
.state
;
2705 // selection colours should override ours,
2706 // otherwise it is too confusing to the user
2707 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) &&
2708 !(tvItemState
& TVIS_DROPHILITED
) )
2711 if ( attr
->HasBackgroundColour() )
2713 colBack
= attr
->GetBackgroundColour();
2714 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2718 // but we still want to keep the special foreground
2719 // colour when we don't have focus (we can't keep
2720 // it when we do, it would usually be unreadable on
2721 // the almost inverted bg colour...)
2722 if ( ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2723 FindFocus() != this ) &&
2724 !(tvItemState
& TVIS_DROPHILITED
) )
2727 if ( attr
->HasTextColour() )
2729 colText
= attr
->GetTextColour();
2730 lptvcd
->clrText
= wxColourToRGB(colText
);
2734 if ( attr
->HasFont() )
2736 HFONT hFont
= GetHfontOf(attr
->GetFont());
2738 ::SelectObject(nmcd
.hdc
, hFont
);
2740 *result
= CDRF_NEWFONT
;
2742 else // no specific font
2744 *result
= CDRF_DODEFAULT
;
2750 *result
= CDRF_DODEFAULT
;
2754 // we always process it
2756 #endif // have owner drawn support in headers
2760 DWORD pos
= GetMessagePos();
2762 point
.x
= LOWORD(pos
);
2763 point
.y
= HIWORD(pos
);
2764 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2766 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2767 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2769 event
.m_item
= item
;
2770 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2778 TV_HITTESTINFO tvhti
;
2779 ::GetCursorPos(&tvhti
.pt
);
2780 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2781 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2783 if ( tvhti
.flags
& TVHT_ONITEM
)
2785 event
.m_item
= tvhti
.hItem
;
2786 eventType
= (int)hdr
->code
== NM_DBLCLK
2787 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2788 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2790 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2791 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2800 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2803 event
.SetEventType(eventType
);
2805 if ( event
.m_item
.IsOk() )
2806 event
.SetClientObject(GetItemData(event
.m_item
));
2808 bool processed
= GetEventHandler()->ProcessEvent(event
);
2811 switch ( hdr
->code
)
2814 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2815 // the return code of this event handler as the return value for
2816 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2817 // expanded status would never work
2822 case TVN_BEGINRDRAG
:
2823 if ( event
.IsAllowed() )
2825 // normally this is impossible because the m_dragImage is
2826 // deleted once the drag operation is over
2827 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2829 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2830 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2831 m_dragImage
->Show();
2835 case TVN_DELETEITEM
:
2837 // NB: we might process this message using wxWidgets event
2838 // tables, but due to overhead of wxWin event system we
2839 // prefer to do it here ourself (otherwise deleting a tree
2840 // with many items is just too slow)
2841 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2843 wxTreeItemParam
*param
=
2844 (wxTreeItemParam
*)tv
->itemOld
.lParam
;
2847 processed
= true; // Make sure we don't get called twice
2851 case TVN_BEGINLABELEDIT
:
2852 // return true to cancel label editing
2853 *result
= !event
.IsAllowed();
2855 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2856 if ( event
.IsAllowed() )
2858 HWND hText
= TreeView_GetEditControl(GetHwnd());
2861 // MBN: if m_textCtrl already has an HWND, it is a stale
2862 // pointer from a previous edit (because the user
2863 // didn't modify the label before dismissing the control,
2864 // and TVN_ENDLABELEDIT was not sent), so delete it
2865 if ( m_textCtrl
&& m_textCtrl
->GetHWND() )
2868 m_textCtrl
= new wxTextCtrl();
2869 m_textCtrl
->SetParent(this);
2870 m_textCtrl
->SetHWND((WXHWND
)hText
);
2871 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2873 // set wxTE_PROCESS_ENTER style for the text control to
2874 // force it to process the Enter presses itself, otherwise
2875 // they could be stolen from it by the dialog
2877 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2878 | wxTE_PROCESS_ENTER
);
2881 else // we had set m_idEdited before
2887 case TVN_ENDLABELEDIT
:
2888 // return true to set the label to the new string: note that we
2889 // also must pretend that we did process the message or it is going
2890 // to be passed to DefWindowProc() which will happily return false
2891 // cancelling the label change
2892 *result
= event
.IsAllowed();
2895 // ensure that we don't have the text ctrl which is going to be
2901 #ifdef TVN_GETINFOTIP
2902 case TVN_GETINFOTIP
:
2904 // If the user permitted a tooltip change, change it
2905 if (event
.IsAllowed())
2907 SetToolTip(event
.m_label
);
2914 case TVN_SELCHANGING
:
2915 case TVN_ITEMEXPANDING
:
2916 // return true to prevent the action from happening
2917 *result
= !event
.IsAllowed();
2920 case TVN_ITEMEXPANDED
:
2921 // the item is not refreshed properly after expansion when it has
2922 // an image depending on the expanded/collapsed state - bug in
2923 // comctl32.dll or our code?
2925 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2926 wxTreeItemId
id(tv
->itemNew
.hItem
);
2928 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2936 case TVN_GETDISPINFO
:
2937 // NB: so far the user can't set the image himself anyhow, so do it
2938 // anyway - but this may change later
2939 //if ( /* !processed && */ )
2941 wxTreeItemId item
= event
.m_item
;
2942 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2944 const wxTreeItemParam
* const param
= GetItemParam(item
);
2948 if ( info
->item
.mask
& TVIF_IMAGE
)
2953 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2954 : wxTreeItemIcon_Normal
2957 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2959 info
->item
.iSelectedImage
=
2962 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2963 : wxTreeItemIcon_Selected
2970 // for the other messages the return value is ignored and there is
2971 // nothing special to do
2976 // ----------------------------------------------------------------------------
2978 // ----------------------------------------------------------------------------
2980 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2981 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2983 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2986 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2987 tvi
.mask
= TVIF_STATE
;
2988 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2990 // Select the specified state, or -1 == cycle to the next one.
2993 TreeView_GetItem(GetHwnd(), &tvi
);
2995 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2996 if ( state
== m_imageListState
->GetImageCount() )
3000 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3001 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3003 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3005 TreeView_SetItem(GetHwnd(), &tvi
);
3008 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3011 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3012 tvi
.mask
= TVIF_STATE
;
3013 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3014 TreeView_GetItem(GetHwnd(), &tvi
);
3016 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3019 #endif // wxUSE_TREECTRL