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/dynarray.h"
36 #include "wx/msw/private.h"
38 // include <commctrl.h> "properly"
39 #include "wx/msw/wrapcctl.h"
41 #include "wx/msw/missing.h"
43 // Set this to 1 to be _absolutely_ sure that repainting will work for all
44 // comctl32.dll versions
45 #define wxUSE_COMCTL32_SAFELY 0
48 #include "wx/imaglist.h"
49 #include "wx/settings.h"
50 #include "wx/msw/dragimag.h"
52 // macros to hide the cast ugliness
53 // --------------------------------
55 // get HTREEITEM from wxTreeItemId
56 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
58 // the native control doesn't support multiple selections under MSW and we
59 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
60 // checkboxes be the selection status (checked == selected) or by really
61 // emulating everything, i.e. intercepting mouse and key events &c. The first
62 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
64 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // wrapper for TreeView_HitTest
71 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
77 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
80 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
82 // wrappers for TreeView_GetItem/TreeView_SetItem
83 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
87 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
88 tvi
.stateMask
= TVIS_SELECTED
;
91 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
93 wxLogLastError(wxT("TreeView_GetItem"));
96 return (tvi
.state
& TVIS_SELECTED
) != 0;
99 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
102 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
103 tvi
.stateMask
= TVIS_SELECTED
;
104 tvi
.state
= select
? TVIS_SELECTED
: 0;
107 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
109 wxLogLastError(wxT("TreeView_SetItem"));
116 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
118 SelectItem(hwndTV
, htItem
, false);
121 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
123 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
126 // helper function which selects all items in a range and, optionally,
127 // unselects all others
128 static void SelectRange(HWND hwndTV
,
131 bool unselectOthers
= true)
133 // find the first (or last) item and select it
135 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
136 while ( htItem
&& cont
)
138 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
140 if ( !IsItemSelected(hwndTV
, htItem
) )
142 SelectItem(hwndTV
, htItem
);
149 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
151 UnselectItem(hwndTV
, htItem
);
155 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
158 // select the items in range
159 cont
= htFirst
!= htLast
;
160 while ( htItem
&& cont
)
162 if ( !IsItemSelected(hwndTV
, htItem
) )
164 SelectItem(hwndTV
, htItem
);
167 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
169 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
173 if ( unselectOthers
)
177 if ( IsItemSelected(hwndTV
, htItem
) )
179 UnselectItem(hwndTV
, htItem
);
182 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
186 // seems to be necessary - otherwise the just selected items don't always
187 // appear as selected
188 UpdateWindow(hwndTV
);
191 // helper function which tricks the standard control into changing the focused
192 // item without changing anything else (if someone knows why Microsoft doesn't
193 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
194 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
197 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
202 if ( htItem
!= htFocus
)
204 // remember the selection state of the item
205 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
207 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
209 // prevent the tree from unselecting the old focus which it
210 // would do by default (TreeView_SelectItem unselects the
212 TreeView_SelectItem(hwndTV
, 0);
213 SelectItem(hwndTV
, htFocus
);
216 TreeView_SelectItem(hwndTV
, htItem
);
220 // need to clear the selection which TreeView_SelectItem() gave
222 UnselectItem(hwndTV
, htItem
);
224 //else: was selected, still selected - ok
226 //else: nothing to do, focus already there
232 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
234 // just clear the focus
235 TreeView_SelectItem(hwndTV
, 0);
237 if ( wasFocusSelected
)
239 // restore the selection state
240 SelectItem(hwndTV
, htFocus
);
243 //else: nothing to do, no focus already
247 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
249 // ----------------------------------------------------------------------------
251 // ----------------------------------------------------------------------------
253 // a convenient wrapper around TV_ITEM struct which adds a ctor
255 #pragma warning( disable : 4097 ) // inheriting from typedef
258 struct wxTreeViewItem
: public TV_ITEM
260 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
261 UINT mask_
, // fields which are valid
262 UINT stateMask_
= 0) // for TVIF_STATE only
266 // hItem member is always valid
267 mask
= mask_
| TVIF_HANDLE
;
268 stateMask
= stateMask_
;
273 // wxVirutalNode is used in place of a single root when 'hidden' root is
275 class wxVirtualNode
: public wxTreeViewItem
278 wxVirtualNode(wxTreeItemData
*data
)
279 : wxTreeViewItem(TVI_ROOT
, 0)
289 wxTreeItemData
*GetData() const { return m_data
; }
290 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
293 wxTreeItemData
*m_data
;
295 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
299 #pragma warning( default : 4097 )
302 // a macro to get the virtual root, returns NULL if none
303 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
305 // returns true if the item is the virtual root
306 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
308 // a class which encapsulates the tree traversal logic: it vists all (unless
309 // OnVisit() returns false) items under the given one
310 class wxTreeTraversal
313 wxTreeTraversal(const wxTreeCtrl
*tree
)
318 // give it a virtual dtor: not really needed as the class is never used
319 // polymorphically and not even allocated on heap at all, but this is safer
320 // (in case it ever is) and silences the compiler warnings for now
321 virtual ~wxTreeTraversal() { }
323 // do traverse the tree: visit all items (recursively by default) under the
324 // given one; return true if all items were traversed or false if the
325 // traversal was aborted because OnVisit returned false
326 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
328 // override this function to do whatever is needed for each item, return
329 // false to stop traversing
330 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
333 const wxTreeCtrl
*GetTree() const { return m_tree
; }
336 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
338 const wxTreeCtrl
*m_tree
;
340 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
343 // internal class for getting the selected items
344 class TraverseSelections
: public wxTreeTraversal
347 TraverseSelections(const wxTreeCtrl
*tree
,
348 wxArrayTreeItemIds
& selections
)
349 : wxTreeTraversal(tree
), m_selections(selections
)
351 m_selections
.Empty();
353 if (tree
->GetCount() > 0)
354 DoTraverse(tree
->GetRootItem());
357 virtual bool OnVisit(const wxTreeItemId
& item
)
359 // can't visit a virtual node.
360 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
365 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
366 if ( GetTree()->IsItemChecked(item
) )
368 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
371 m_selections
.Add(item
);
377 size_t GetCount() const { return m_selections
.GetCount(); }
380 wxArrayTreeItemIds
& m_selections
;
382 DECLARE_NO_COPY_CLASS(TraverseSelections
)
385 // internal class for counting tree items
386 class TraverseCounter
: public wxTreeTraversal
389 TraverseCounter(const wxTreeCtrl
*tree
,
390 const wxTreeItemId
& root
,
392 : wxTreeTraversal(tree
)
396 DoTraverse(root
, recursively
);
399 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
406 size_t GetCount() const { return m_count
; }
411 DECLARE_NO_COPY_CLASS(TraverseCounter
)
414 // ----------------------------------------------------------------------------
415 // This class is needed for support of different images: the Win32 common
416 // control natively supports only 2 images (the normal one and another for the
417 // selected state). We wish to provide support for 2 more of them for folder
418 // items (i.e. those which have children): for expanded state and for expanded
419 // selected state. For this we use this structure to store the additional items
422 // There is only one problem with this: when we retrieve the item's data, we
423 // don't know whether we get a pointer to wxTreeItemData or
424 // wxTreeItemIndirectData. So we always set the item id to an invalid value
425 // in this class and the code using the client data checks for it and retrieves
426 // the real client data in this case.
427 // ----------------------------------------------------------------------------
429 class wxTreeItemIndirectData
: public wxTreeItemData
432 // ctor associates this data with the item and the real item data becomes
433 // available through our GetData() method
434 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
436 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
442 m_data
= tree
->GetItemData(item
);
444 // and set ourselves as the new one
445 tree
->SetIndirectItemData(item
, this);
447 // we must have the invalid value for the item
451 // dtor deletes the associated data as well
452 virtual ~wxTreeItemIndirectData() { delete m_data
; }
455 // get the real data associated with the item
456 wxTreeItemData
*GetData() const { return m_data
; }
458 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
460 // do we have such image?
461 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
463 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
465 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
468 // all the images associated with the item
469 int m_images
[wxTreeItemIcon_Max
];
471 // the real client data
472 wxTreeItemData
*m_data
;
474 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
477 // ----------------------------------------------------------------------------
479 // ----------------------------------------------------------------------------
481 #if wxUSE_EXTENDED_RTTI
482 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
484 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
485 // new style border flags, we put them first to
486 // use them for streaming out
487 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
488 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
489 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
490 wxFLAGS_MEMBER(wxBORDER_RAISED
)
491 wxFLAGS_MEMBER(wxBORDER_STATIC
)
492 wxFLAGS_MEMBER(wxBORDER_NONE
)
494 // old style border flags
495 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
496 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
497 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
498 wxFLAGS_MEMBER(wxRAISED_BORDER
)
499 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
500 wxFLAGS_MEMBER(wxBORDER
)
502 // standard window styles
503 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
504 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
505 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
506 wxFLAGS_MEMBER(wxWANTS_CHARS
)
507 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
508 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
509 wxFLAGS_MEMBER(wxVSCROLL
)
510 wxFLAGS_MEMBER(wxHSCROLL
)
512 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
513 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
514 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
515 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
516 wxFLAGS_MEMBER(wxTR_NO_LINES
)
517 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
518 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
519 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
520 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
521 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
522 wxFLAGS_MEMBER(wxTR_SINGLE
)
523 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
524 wxFLAGS_MEMBER(wxTR_EXTENDED
)
525 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
527 wxEND_FLAGS( wxTreeCtrlStyle
)
529 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
531 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
532 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
533 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
534 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
535 wxEND_PROPERTIES_TABLE()
537 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
538 wxEND_HANDLERS_TABLE()
540 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
542 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
545 // ----------------------------------------------------------------------------
547 // ----------------------------------------------------------------------------
549 // indices in gs_expandEvents table below
564 // handy table for sending events - it has to be initialized during run-time
565 // now so can't be const any more
566 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
569 but logically it's a const table with the following entries:
572 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
573 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
577 // ============================================================================
579 // ============================================================================
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
587 if ( !OnVisit(root
) )
590 return Traverse(root
, recursively
);
593 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
595 wxTreeItemIdValue cookie
;
596 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
597 while ( child
.IsOk() )
599 // depth first traversal
600 if ( recursively
&& !Traverse(child
, true) )
603 if ( !OnVisit(child
) )
606 child
= m_tree
->GetNextChild(root
, cookie
);
612 // ----------------------------------------------------------------------------
613 // construction and destruction
614 // ----------------------------------------------------------------------------
616 void wxTreeCtrl::Init()
619 m_hasAnyAttr
= false;
621 m_pVirtualRoot
= NULL
;
623 // initialize the global array of events now as it can't be done statically
624 // with the wxEVT_XXX values being allocated during run-time only
625 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
626 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
627 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
628 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
631 bool wxTreeCtrl::Create(wxWindow
*parent
,
636 const wxValidator
& validator
,
637 const wxString
& name
)
641 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
642 style
|= wxBORDER_SUNKEN
;
644 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
648 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
649 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
651 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
652 wstyle
|= TVS_HASLINES
;
653 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
654 wstyle
|= TVS_HASBUTTONS
;
656 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
657 wstyle
|= TVS_EDITLABELS
;
659 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
660 wstyle
|= TVS_LINESATROOT
;
662 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
664 if ( wxApp::GetComCtl32Version() >= 471 )
665 wstyle
|= TVS_FULLROWSELECT
;
668 // using TVS_CHECKBOXES for emulation of a multiselection tree control
669 // doesn't work without the new enough headers
670 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
671 !defined( __GNUWIN32_OLD__ ) && \
672 !defined( __BORLANDC__ ) && \
673 !defined( __WATCOMC__ ) && \
674 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
676 // we emulate the multiple selection tree controls by using checkboxes: set
677 // up the image list we need for this if we do have multiple selections
678 if ( m_windowStyle
& wxTR_MULTIPLE
)
679 wstyle
|= TVS_CHECKBOXES
;
680 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
682 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
683 // Need so that TVN_GETINFOTIP messages will be sent
684 wstyle
|= TVS_INFOTIP
;
687 // Create the tree control.
688 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
691 #if wxUSE_COMCTL32_SAFELY
692 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
693 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
695 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
696 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
698 // This works around a bug in the Windows tree control whereby for some versions
699 // of comctrl32, setting any colour actually draws the background in black.
700 // This will initialise the background to the system colour.
701 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
702 // Assume the user has an updated comctl32.dll.
703 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
704 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
705 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
709 // VZ: this is some experimental code which may be used to get the
710 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
711 // AFAIK, the standard DLL does about the same thing anyhow.
713 if ( m_windowStyle
& wxTR_MULTIPLE
)
717 // create the DC compatible with the current screen
718 HDC hdcMem
= CreateCompatibleDC(NULL
);
720 // create a mono bitmap of the standard size
721 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
722 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
723 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
724 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
725 1, // # of color planes
726 1, // # bits needed for one pixel
727 0); // array containing colour data
728 SelectObject(hdcMem
, hbmpCheck
);
730 // then draw a check mark into it
731 RECT rect
= { 0, 0, x
, y
};
732 if ( !::DrawFrameControl(hdcMem
, &rect
,
734 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
736 wxLogLastError(wxT("DrawFrameControl(check)"));
739 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
740 imagelistCheckboxes
.Add(bmp
);
742 if ( !::DrawFrameControl(hdcMem
, &rect
,
746 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
749 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
750 imagelistCheckboxes
.Add(bmp
);
756 SetStateImageList(&imagelistCheckboxes
);
760 wxSetCCUnicodeFormat(GetHwnd());
765 wxTreeCtrl::~wxTreeCtrl()
767 // delete any attributes
770 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
772 // prevent TVN_DELETEITEM handler from deleting the attributes again!
773 m_hasAnyAttr
= false;
778 // delete user data to prevent memory leaks
779 // also deletes hidden root node storage.
783 // ----------------------------------------------------------------------------
785 // ----------------------------------------------------------------------------
787 /* static */ wxVisualAttributes
788 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
790 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
792 // common controls have their own default font
793 attrs
.font
= wxGetCCDefaultFont();
799 // simple wrappers which add error checking in debug mode
801 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
803 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
804 _T("can't retrieve virtual root item") );
806 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
808 wxLogLastError(wxT("TreeView_GetItem"));
816 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
818 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
820 wxLogLastError(wxT("TreeView_SetItem"));
824 unsigned int wxTreeCtrl::GetCount() const
826 return (unsigned int)TreeView_GetCount(GetHwnd());
829 unsigned int wxTreeCtrl::GetIndent() const
831 return TreeView_GetIndent(GetHwnd());
834 void wxTreeCtrl::SetIndent(unsigned int indent
)
836 TreeView_SetIndent(GetHwnd(), indent
);
839 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
842 (void) TreeView_SetImageList(GetHwnd(),
843 imageList
? imageList
->GetHIMAGELIST() : 0,
847 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
849 if (m_ownsImageListNormal
)
850 delete m_imageListNormal
;
852 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
853 m_ownsImageListNormal
= false;
856 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
858 if (m_ownsImageListState
) delete m_imageListState
;
859 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
860 m_ownsImageListState
= false;
863 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
864 bool recursively
) const
866 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
868 TraverseCounter
counter(this, item
, recursively
);
869 return counter
.GetCount() - 1;
872 // ----------------------------------------------------------------------------
874 // ----------------------------------------------------------------------------
876 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
878 #if !wxUSE_COMCTL32_SAFELY
879 if ( !wxWindowBase::SetBackgroundColour(colour
) )
882 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
888 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
890 #if !wxUSE_COMCTL32_SAFELY
891 if ( !wxWindowBase::SetForegroundColour(colour
) )
894 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
900 // ----------------------------------------------------------------------------
902 // ----------------------------------------------------------------------------
904 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
906 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
908 wxChar buf
[512]; // the size is arbitrary...
910 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
911 tvItem
.pszText
= buf
;
912 tvItem
.cchTextMax
= WXSIZEOF(buf
);
913 if ( !DoGetItem(&tvItem
) )
915 // don't return some garbage which was on stack, but an empty string
919 return wxString(buf
);
922 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
924 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
926 if ( IS_VIRTUAL_ROOT(item
) )
929 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
930 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
933 // when setting the text of the item being edited, the text control should
934 // be updated to reflect the new text as well, otherwise calling
935 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
937 // don't use GetEditControl() here because m_textCtrl is not set yet
938 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
941 if ( item
== m_idEdited
)
943 ::SetWindowText(hwndEdit
, text
);
948 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
949 wxTreeItemIcon which
) const
951 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
952 if ( !DoGetItem(&tvItem
) )
957 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
960 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
962 wxTreeItemIcon which
) const
964 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
965 if ( !DoGetItem(&tvItem
) )
970 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
972 data
->SetImage(image
, which
);
974 // make sure that we have selected images as well
975 if ( which
== wxTreeItemIcon_Normal
&&
976 !data
->HasImage(wxTreeItemIcon_Selected
) )
978 data
->SetImage(image
, wxTreeItemIcon_Selected
);
981 if ( which
== wxTreeItemIcon_Expanded
&&
982 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
984 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
988 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
992 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
993 tvItem
.iSelectedImage
= imageSel
;
994 tvItem
.iImage
= image
;
998 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
999 wxTreeItemIcon which
) const
1001 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
1003 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1005 // TODO: Maybe a hidden root can still provide images?
1009 if ( HasIndirectData(item
) )
1011 return DoGetItemImageFromData(item
, which
);
1018 wxFAIL_MSG( wxT("unknown tree item image type") );
1020 case wxTreeItemIcon_Normal
:
1024 case wxTreeItemIcon_Selected
:
1025 mask
= TVIF_SELECTEDIMAGE
;
1028 case wxTreeItemIcon_Expanded
:
1029 case wxTreeItemIcon_SelectedExpanded
:
1033 wxTreeViewItem
tvItem(item
, mask
);
1036 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1039 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1040 wxTreeItemIcon which
)
1042 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1044 if ( IS_VIRTUAL_ROOT(item
) )
1046 // TODO: Maybe a hidden root can still store images?
1056 wxFAIL_MSG( wxT("unknown tree item image type") );
1059 case wxTreeItemIcon_Normal
:
1061 const int imageNormalOld
= GetItemImage(item
);
1062 const int imageSelOld
=
1063 GetItemImage(item
, wxTreeItemIcon_Selected
);
1065 // always set the normal image
1066 imageNormal
= image
;
1068 // if the selected and normal images were the same, they should
1069 // be the same after the update, otherwise leave the selected
1071 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1075 case wxTreeItemIcon_Selected
:
1076 imageNormal
= GetItemImage(item
);
1080 case wxTreeItemIcon_Expanded
:
1081 case wxTreeItemIcon_SelectedExpanded
:
1082 if ( !HasIndirectData(item
) )
1084 // we need to get the old images first, because after we create
1085 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1087 imageNormal
= GetItemImage(item
);
1088 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1090 // if it doesn't have it yet, add it
1091 wxTreeItemIndirectData
*data
= new
1092 wxTreeItemIndirectData(this, item
);
1094 // copy the data to the new location
1095 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1096 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1099 DoSetItemImageFromData(item
, image
, which
);
1101 // reset the normal/selected images because we won't use them any
1102 // more - now they're stored inside the indirect data
1104 imageSel
= I_IMAGECALLBACK
;
1108 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1109 // change both normal and selected image - otherwise the change simply
1110 // doesn't take place!
1111 DoSetItemImages(item
, imageNormal
, imageSel
);
1114 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1116 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1118 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1120 // Hidden root may have data.
1121 if ( IS_VIRTUAL_ROOT(item
) )
1123 return GET_VIRTUAL_ROOT()->GetData();
1127 if ( !DoGetItem(&tvItem
) )
1132 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1133 if ( IsDataIndirect(data
) )
1135 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1141 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1143 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1145 if ( IS_VIRTUAL_ROOT(item
) )
1147 GET_VIRTUAL_ROOT()->SetData(data
);
1150 // first, associate this piece of data with this item
1156 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1158 if ( HasIndirectData(item
) )
1160 if ( DoGetItem(&tvItem
) )
1162 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1166 wxFAIL_MSG( wxT("failed to change tree items data") );
1171 tvItem
.lParam
= (LPARAM
)data
;
1176 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1177 wxTreeItemIndirectData
*data
)
1179 // this should never happen because it's unnecessary and will probably lead
1180 // to crash too because the code elsewhere supposes that the pointer the
1181 // wxTreeItemIndirectData has is a real wxItemData and not
1182 // wxTreeItemIndirectData as well
1183 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1185 SetItemData(item
, data
);
1188 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1190 // query the item itself
1191 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1192 if ( !DoGetItem(&tvItem
) )
1197 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1199 return data
&& IsDataIndirect(data
);
1202 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1204 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1206 if ( IS_VIRTUAL_ROOT(item
) )
1209 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1210 tvItem
.cChildren
= (int)has
;
1214 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1216 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1218 if ( IS_VIRTUAL_ROOT(item
) )
1221 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1222 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1226 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1228 if ( IS_VIRTUAL_ROOT(item
) )
1231 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1232 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1236 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1238 if ( IS_VIRTUAL_ROOT(item
) )
1242 if ( GetBoundingRect(item
, rect
) )
1248 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1250 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1252 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1253 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1256 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1258 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1260 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1261 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1264 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1266 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1268 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1269 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1272 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1273 const wxColour
& col
)
1275 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1277 wxTreeItemAttr
*attr
;
1278 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1279 if ( it
== m_attrs
.end() )
1281 m_hasAnyAttr
= true;
1283 m_attrs
[item
.m_pItem
] =
1284 attr
= new wxTreeItemAttr
;
1291 attr
->SetTextColour(col
);
1296 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1297 const wxColour
& col
)
1299 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1301 wxTreeItemAttr
*attr
;
1302 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1303 if ( it
== m_attrs
.end() )
1305 m_hasAnyAttr
= true;
1307 m_attrs
[item
.m_pItem
] =
1308 attr
= new wxTreeItemAttr
;
1310 else // already in the hash
1315 attr
->SetBackgroundColour(col
);
1320 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1322 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1324 wxTreeItemAttr
*attr
;
1325 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1326 if ( it
== m_attrs
.end() )
1328 m_hasAnyAttr
= true;
1330 m_attrs
[item
.m_pItem
] =
1331 attr
= new wxTreeItemAttr
;
1333 else // already in the hash
1338 attr
->SetFont(font
);
1343 // ----------------------------------------------------------------------------
1345 // ----------------------------------------------------------------------------
1347 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1349 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1351 if ( item
== wxTreeItemId(TVI_ROOT
) )
1353 // virtual (hidden) root is never visible
1357 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1360 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1361 // the HTREEITEM with TVM_GETITEMRECT
1362 *(HTREEITEM
*)&rect
= HITEM(item
);
1364 // true means to get rect for just the text, not the whole line
1365 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1367 // if TVM_GETITEMRECT returned false, then the item is definitely not
1368 // visible (because its parent is not expanded)
1372 // however if it returned true, the item might still be outside the
1373 // currently visible part of the tree, test for it (notice that partly
1374 // visible means visible here)
1375 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1378 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1380 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1382 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1385 return tvItem
.cChildren
!= 0;
1388 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1390 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1392 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1395 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1398 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1400 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1402 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1405 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1408 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1410 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1412 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1415 return (tvItem
.state
& TVIS_BOLD
) != 0;
1418 // ----------------------------------------------------------------------------
1420 // ----------------------------------------------------------------------------
1422 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1424 // Root may be real (visible) or virtual (hidden).
1425 if ( GET_VIRTUAL_ROOT() )
1428 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1431 wxTreeItemId
wxTreeCtrl::GetSelection() const
1433 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1434 wxT("this only works with single selection controls") );
1436 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1439 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1441 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1445 if ( IS_VIRTUAL_ROOT(item
) )
1447 // no parent for the virtual root
1452 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1453 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1455 // the top level items should have the virtual root as their parent
1460 return wxTreeItemId(hItem
);
1463 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1464 wxTreeItemIdValue
& cookie
) const
1466 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1468 // remember the last child returned in 'cookie'
1469 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1471 return wxTreeItemId(cookie
);
1474 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1475 wxTreeItemIdValue
& cookie
) const
1477 wxTreeItemId
fromCookie(cookie
);
1479 HTREEITEM hitem
= HITEM(fromCookie
);
1481 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1483 wxTreeItemId
item(hitem
);
1485 cookie
= item
.m_pItem
;
1490 #if WXWIN_COMPATIBILITY_2_4
1492 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1495 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1497 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1499 return wxTreeItemId((void *)cookie
);
1502 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1505 wxTreeItemId
fromCookie((void *)cookie
);
1507 HTREEITEM hitem
= HITEM(fromCookie
);
1509 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1511 wxTreeItemId
item(hitem
);
1513 cookie
= (long)item
.m_pItem
;
1518 #endif // WXWIN_COMPATIBILITY_2_4
1520 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1522 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1524 // can this be done more efficiently?
1525 wxTreeItemIdValue cookie
;
1527 wxTreeItemId childLast
,
1528 child
= GetFirstChild(item
, cookie
);
1529 while ( child
.IsOk() )
1532 child
= GetNextChild(item
, cookie
);
1538 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1540 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1541 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1544 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1546 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1547 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1550 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1552 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1555 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1557 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1558 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1560 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1563 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1565 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1566 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1568 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1571 // ----------------------------------------------------------------------------
1572 // multiple selections emulation
1573 // ----------------------------------------------------------------------------
1575 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1577 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1579 // receive the desired information.
1580 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1583 // state image indices are 1 based
1584 return ((tvItem
.state
>> 12) - 1) == 1;
1587 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1589 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1591 // receive the desired information.
1592 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1596 // state images are one-based
1597 tvItem
.state
= (check
? 2 : 1) << 12;
1602 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1604 TraverseSelections
selector(this, selections
);
1606 return selector
.GetCount();
1609 // ----------------------------------------------------------------------------
1611 // ----------------------------------------------------------------------------
1613 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1614 const wxTreeItemId
& hInsertAfter
,
1615 const wxString
& text
,
1616 int image
, int selectedImage
,
1617 wxTreeItemData
*data
)
1619 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1621 _T("can't have more than one root in the tree") );
1623 TV_INSERTSTRUCT tvIns
;
1624 tvIns
.hParent
= HITEM(parent
);
1625 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1627 // this is how we insert the item as the first child: supply a NULL
1629 if ( !tvIns
.hInsertAfter
)
1631 tvIns
.hInsertAfter
= TVI_FIRST
;
1635 if ( !text
.empty() )
1638 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1642 tvIns
.item
.pszText
= NULL
;
1643 tvIns
.item
.cchTextMax
= 0;
1649 tvIns
.item
.iImage
= image
;
1651 if ( selectedImage
== -1 )
1653 // take the same image for selected icon if not specified
1654 selectedImage
= image
;
1658 if ( selectedImage
!= -1 )
1660 mask
|= TVIF_SELECTEDIMAGE
;
1661 tvIns
.item
.iSelectedImage
= selectedImage
;
1667 tvIns
.item
.lParam
= (LPARAM
)data
;
1670 tvIns
.item
.mask
= mask
;
1672 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1675 wxLogLastError(wxT("TreeView_InsertItem"));
1680 // associate the application tree item with Win32 tree item handle
1684 return wxTreeItemId(id
);
1687 // for compatibility only
1688 #if WXWIN_COMPATIBILITY_2_4
1690 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1692 SetImageList(imageList
);
1695 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1697 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1700 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1702 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1705 #endif // WXWIN_COMPATIBILITY_2_4
1707 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1708 int image
, int selectedImage
,
1709 wxTreeItemData
*data
)
1712 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1714 // create a virtual root item, the parent for all the others
1715 m_pVirtualRoot
= new wxVirtualNode(data
);
1720 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1721 text
, image
, selectedImage
, data
);
1724 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1726 const wxString
& text
,
1727 int image
, int selectedImage
,
1728 wxTreeItemData
*data
)
1730 wxTreeItemId idPrev
;
1731 if ( index
== (size_t)-1 )
1733 // special value: append to the end
1736 else // find the item from index
1738 wxTreeItemIdValue cookie
;
1739 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1740 while ( index
!= 0 && idCur
.IsOk() )
1745 idCur
= GetNextChild(parent
, cookie
);
1748 // assert, not check: if the index is invalid, we will append the item
1750 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1753 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1756 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1758 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1760 wxLogLastError(wxT("TreeView_DeleteItem"));
1764 // delete all children (but don't delete the item itself)
1765 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1767 wxTreeItemIdValue cookie
;
1769 wxArrayTreeItemIds children
;
1770 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1771 while ( child
.IsOk() )
1773 children
.Add(child
);
1775 child
= GetNextChild(item
, cookie
);
1778 size_t nCount
= children
.Count();
1779 for ( size_t n
= 0; n
< nCount
; n
++ )
1781 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1783 wxLogLastError(wxT("TreeView_DeleteItem"));
1788 void wxTreeCtrl::DeleteAllItems()
1790 // delete the "virtual" root item.
1791 if ( GET_VIRTUAL_ROOT() )
1793 delete GET_VIRTUAL_ROOT();
1794 m_pVirtualRoot
= NULL
;
1797 // and all the real items
1799 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1801 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1805 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1807 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1808 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1809 flag
== TVE_EXPAND
||
1811 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1813 // A hidden root can be neither expanded nor collapsed.
1814 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1815 wxT("Can't expand/collapse hidden root node!") );
1817 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1818 // emulate them. This behaviour has changed slightly with comctl32.dll
1819 // v 4.70 - now it does send them but only the first time. To maintain
1820 // compatible behaviour and also in order to not have surprises with the
1821 // future versions, don't rely on this and still do everything ourselves.
1822 // To avoid that the messages be sent twice when the item is expanded for
1823 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1825 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1829 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1831 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1832 event
.m_item
= item
;
1833 event
.SetEventObject(this);
1835 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1837 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1841 (void)GetEventHandler()->ProcessEvent(event
);
1843 //else: change didn't took place, so do nothing at all
1846 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1848 DoExpand(item
, TVE_EXPAND
);
1851 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1853 DoExpand(item
, TVE_COLLAPSE
);
1856 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1858 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1861 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1863 DoExpand(item
, TVE_TOGGLE
);
1866 #if WXWIN_COMPATIBILITY_2_4
1868 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1870 DoExpand(item
, action
);
1875 void wxTreeCtrl::Unselect()
1877 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1878 wxT("doesn't make sense, may be you want UnselectAll()?") );
1880 // just remove the selection
1881 SelectItem(wxTreeItemId());
1884 void wxTreeCtrl::UnselectAll()
1886 if ( m_windowStyle
& wxTR_MULTIPLE
)
1888 wxArrayTreeItemIds selections
;
1889 size_t count
= GetSelections(selections
);
1890 for ( size_t n
= 0; n
< count
; n
++ )
1892 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1893 SetItemCheck(HITEM(selections
[n
]), false);
1894 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1895 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1896 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1899 m_htSelStart
.Unset();
1903 // just remove the selection
1908 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1910 if ( m_windowStyle
& wxTR_MULTIPLE
)
1912 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1913 // selecting the item means checking it
1914 SetItemCheck(item
, select
);
1915 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1916 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1917 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1921 wxASSERT_MSG( select
,
1922 _T("SelectItem(false) works only for multiselect") );
1924 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1925 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1926 // send them ourselves
1928 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1929 event
.m_item
= item
;
1930 event
.SetEventObject(this);
1932 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1933 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1935 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1937 wxLogLastError(wxT("TreeView_SelectItem"));
1941 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1942 (void)GetEventHandler()->ProcessEvent(event
);
1945 //else: program vetoed the change
1949 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1952 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1955 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1957 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1959 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1963 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1968 void wxTreeCtrl::DeleteTextCtrl()
1972 // the HWND corresponding to this control is deleted by the tree
1973 // control itself and we don't know when exactly this happens, so check
1974 // if the window still exists before calling UnsubclassWin()
1975 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1977 m_textCtrl
->SetHWND(0);
1980 m_textCtrl
->UnsubclassWin();
1981 m_textCtrl
->SetHWND(0);
1989 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1990 wxClassInfo
* textControlClass
)
1992 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1997 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1998 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
2000 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2009 // textctrl is subclassed in MSWOnNotify
2013 // End label editing, optionally cancelling the edit
2014 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2016 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2021 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
)
2023 TV_HITTESTINFO hitTestInfo
;
2024 hitTestInfo
.pt
.x
= (int)point
.x
;
2025 hitTestInfo
.pt
.y
= (int)point
.y
;
2027 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2032 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2033 flags |= wxTREE_HITTEST_##flag
2035 TRANSLATE_FLAG(ABOVE
);
2036 TRANSLATE_FLAG(BELOW
);
2037 TRANSLATE_FLAG(NOWHERE
);
2038 TRANSLATE_FLAG(ONITEMBUTTON
);
2039 TRANSLATE_FLAG(ONITEMICON
);
2040 TRANSLATE_FLAG(ONITEMINDENT
);
2041 TRANSLATE_FLAG(ONITEMLABEL
);
2042 TRANSLATE_FLAG(ONITEMRIGHT
);
2043 TRANSLATE_FLAG(ONITEMSTATEICON
);
2044 TRANSLATE_FLAG(TOLEFT
);
2045 TRANSLATE_FLAG(TORIGHT
);
2047 #undef TRANSLATE_FLAG
2049 return wxTreeItemId(hitTestInfo
.hItem
);
2052 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2054 bool textOnly
) const
2058 // Virtual root items have no bounding rectangle
2059 if ( IS_VIRTUAL_ROOT(item
) )
2064 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2067 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2073 // couldn't retrieve rect: for example, item isn't visible
2078 // ----------------------------------------------------------------------------
2080 // ----------------------------------------------------------------------------
2082 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2083 // functions such as IsDataIndirect()
2084 class wxTreeSortHelper
2087 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2090 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
2092 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
2093 if ( tree
->IsDataIndirect(data
) )
2095 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2098 return data
->GetId();
2102 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2106 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2107 wxT("sorting tree without data doesn't make sense") );
2109 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2111 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2112 GetIdFromData(tree
, pItem2
));
2115 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2117 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2119 // rely on the fact that TreeView_SortChildren does the same thing as our
2120 // default behaviour, i.e. sorts items alphabetically and so call it
2121 // directly if we're not in derived class (much more efficient!)
2122 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2124 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2129 tvSort
.hParent
= HITEM(item
);
2130 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2131 tvSort
.lParam
= (LPARAM
)this;
2132 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2136 // ----------------------------------------------------------------------------
2138 // ----------------------------------------------------------------------------
2140 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2142 if ( cmd
== EN_UPDATE
)
2144 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2145 event
.SetEventObject( this );
2146 ProcessCommand(event
);
2148 else if ( cmd
== EN_KILLFOCUS
)
2150 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2151 event
.SetEventObject( this );
2152 ProcessCommand(event
);
2160 // command processed
2164 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2165 // only do it during dragging, minimize wxWin overhead (this is important for
2166 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2167 // instead of passing by wxWin events
2168 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2170 bool processed
= false;
2172 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2174 // This message is sent after a right-click, or when the "menu" key is pressed
2175 if ( nMsg
== WM_CONTEXTMENU
)
2177 int x
= GET_X_LPARAM(lParam
),
2178 y
= GET_Y_LPARAM(lParam
);
2179 // Convert the screen point to a client point
2180 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2182 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2184 // can't use GetSelection() here as it would assert in multiselect mode
2185 event
.m_item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2186 event
.SetEventObject( this );
2188 // Get the bounding rectangle for the item, including the non-text areas
2190 GetBoundingRect(event
.m_item
, ItemRect
, false);
2191 // If the point is inside the bounding rectangle, use it as the click position.
2192 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2193 if (ItemRect
.Inside(MenuPoint
))
2195 event
.m_pointDrag
= MenuPoint
;
2197 // Use the Explorer standard of putting the menu at the left edge of the text,
2198 // in the vertical middle of the text. Should be the case for the "menu" key
2201 // Use the bounding rectangle of only the text part
2202 GetBoundingRect(event
.m_item
, ItemRect
, true);
2203 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2206 if ( GetEventHandler()->ProcessEvent(event
) )
2208 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2210 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2212 // we only process mouse messages here and these parameters have the
2213 // same meaning for all of them
2214 int x
= GET_X_LPARAM(lParam
),
2215 y
= GET_Y_LPARAM(lParam
);
2216 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2218 TV_HITTESTINFO tvht
;
2222 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2226 case WM_RBUTTONDOWN
:
2227 // if the item we are about to right click on is not already
2228 // selected or if we click outside of any item, remove the
2229 // entire previous selection
2230 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2235 // select item and set the focus to the
2236 // newly selected item
2237 ::SelectItem(GetHwnd(), htItem
);
2238 ::SetFocus(GetHwnd(), htItem
);
2241 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2242 case WM_LBUTTONDOWN
:
2243 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2245 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2246 m_ptClick
= wxPoint(x
, y
);
2248 if ( wParam
& MK_CONTROL
)
2252 // toggle selected state
2253 ::ToggleItemSelection(GetHwnd(), htItem
);
2255 ::SetFocus(GetHwnd(), htItem
);
2257 // reset on any click without Shift
2258 m_htSelStart
.Unset();
2262 else if ( wParam
& MK_SHIFT
)
2264 // this selects all items between the starting one and
2267 if ( !m_htSelStart
)
2269 // take the focused item
2270 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2274 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2275 !(wParam
& MK_CONTROL
));
2277 ::SelectItem(GetHwnd(), htItem
);
2279 ::SetFocus(GetHwnd(), htItem
);
2283 else // normal click
2285 // avoid doing anything if we click on the only
2286 // currently selected item
2290 wxArrayTreeItemIds selections
;
2291 size_t count
= GetSelections(selections
);
2294 HITEM(selections
[0]) != htItem
)
2296 // clear the previously selected items, if the
2297 // user clicked outside of the present selection.
2298 // otherwise, perform the deselection on mouse-up.
2299 // this allows multiple drag and drop to work.
2301 if (!IsItemSelected(GetHwnd(), htItem
))
2305 // prevent the click from starting in-place editing
2306 // which should only happen if we click on the
2307 // already selected item (and nothing else is
2310 TreeView_SelectItem(GetHwnd(), 0);
2311 ::SelectItem(GetHwnd(), htItem
);
2313 ::SetFocus(GetHwnd(), htItem
);
2317 // reset on any click without Shift
2318 m_htSelStart
.Unset();
2322 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2326 if ( m_htClickedItem
)
2328 int cx
= abs(m_ptClick
.x
- x
);
2329 int cy
= abs(m_ptClick
.y
- y
);
2331 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2333 HWND pWnd
= ::GetParent( GetHwnd() );
2338 tv
.hdr
.hwndFrom
= GetHwnd();
2339 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2340 tv
.hdr
.code
= TVN_BEGINDRAG
;
2342 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2345 ZeroMemory(&tviAux
, sizeof(tviAux
));
2346 tviAux
.hItem
= HITEM(m_htClickedItem
);
2347 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2348 tviAux
.stateMask
= 0xffffffff;
2349 TreeView_GetItem( GetHwnd(), &tviAux
);
2351 tv
.itemNew
.state
= tviAux
.state
;
2352 tv
.itemNew
.lParam
= tviAux
.lParam
;
2357 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2359 m_htClickedItem
.Unset();
2362 #endif // __WXWINCE__
2366 m_dragImage
->Move(wxPoint(x
, y
));
2369 // highlight the item as target (hiding drag image is
2370 // necessary - otherwise the display will be corrupted)
2371 m_dragImage
->Hide();
2372 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2373 m_dragImage
->Show();
2380 // facilitates multiple drag-and-drop
2381 if (htItem
&& isMultiple
)
2383 wxArrayTreeItemIds selections
;
2384 size_t count
= GetSelections(selections
);
2387 !(wParam
& MK_CONTROL
) &&
2388 !(wParam
& MK_SHIFT
))
2391 TreeView_SelectItem(GetHwnd(), htItem
);
2392 ::SelectItem(GetHwnd(), htItem
);
2393 ::SetFocus(GetHwnd(), htItem
);
2395 m_htClickedItem
.Unset();
2403 m_dragImage
->EndDrag();
2407 // generate the drag end event
2408 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2410 event
.m_item
= htItem
;
2411 event
.m_pointDrag
= wxPoint(x
, y
);
2412 event
.SetEventObject(this);
2414 (void)GetEventHandler()->ProcessEvent(event
);
2416 // if we don't do it, the tree seems to think that 2 items
2417 // are selected simultaneously which is quite weird
2418 TreeView_SelectDropTarget(GetHwnd(), 0);
2423 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2424 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2426 // the tree control greys out the selected item when it loses focus and
2427 // paints it as selected again when it regains it, but it won't do it
2428 // for the other items itself - help it
2429 wxArrayTreeItemIds selections
;
2430 size_t count
= GetSelections(selections
);
2432 for ( size_t n
= 0; n
< count
; n
++ )
2434 // TreeView_GetItemRect() will return false if item is not visible,
2435 // which may happen perfectly well
2436 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2439 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2443 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2445 bool bCtrl
= wxIsCtrlDown(),
2446 bShift
= wxIsShiftDown();
2448 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2454 ::ToggleItemSelection(GetHwnd(), htSel
);
2460 ::SelectItem(GetHwnd(), htSel
);
2468 if ( !bCtrl
&& !bShift
)
2470 // no modifiers, just clear selection and then let the default
2471 // processing to take place
2476 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2478 HTREEITEM htNext
= (HTREEITEM
)
2479 TreeView_GetNextItem
2483 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2489 // at the top/bottom
2495 if ( !m_htSelStart
)
2496 m_htSelStart
= htSel
;
2498 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2502 // without changing selection
2503 ::SetFocus(GetHwnd(), htNext
);
2514 // TODO: handle Shift/Ctrl with these keys
2515 if ( !bCtrl
&& !bShift
)
2519 m_htSelStart
.Unset();
2523 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2524 else if ( nMsg
== WM_COMMAND
)
2526 // if we receive a EN_KILLFOCUS command from the in-place edit control
2527 // used for label editing, make sure to end editing
2530 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2532 if ( cmd
== EN_KILLFOCUS
)
2534 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2544 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2550 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2552 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2553 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2554 // the parent window, not us, which completely breaks everything so simply
2555 // don't let it see this message at all
2556 if ( nMsg
== WM_RBUTTONDOWN
)
2559 // but because of the above we don't get NM_RCLICK which is normally
2560 // generated by tree window proc when the modal loop mentioned above ends
2561 // because the mouse is released -- synthesize it ourselves instead
2562 if ( nMsg
== WM_RBUTTONUP
)
2565 hdr
.hwndFrom
= GetHwnd();
2566 hdr
.idFrom
= GetId();
2567 hdr
.code
= NM_RCLICK
;
2570 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2572 // continue as usual
2575 if ( nMsg
== WM_CHAR
)
2577 // also don't let the control process Space and Return keys because it
2578 // doesn't do anything useful with them anyhow but always beeps
2579 // annoyingly when it receives them and there is no way to turn it off
2580 // simply if you just process TREEITEM_ACTIVATED event to which Space
2581 // and Enter presses are mapped in your code
2582 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2586 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2589 // process WM_NOTIFY Windows message
2590 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2592 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2593 wxEventType eventType
= wxEVT_NULL
;
2594 NMHDR
*hdr
= (NMHDR
*)lParam
;
2596 switch ( hdr
->code
)
2599 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2602 case TVN_BEGINRDRAG
:
2604 if ( eventType
== wxEVT_NULL
)
2605 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2606 //else: left drag, already set above
2608 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2610 event
.m_item
= tv
->itemNew
.hItem
;
2611 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2613 // don't allow dragging by default: the user code must
2614 // explicitly say that it wants to allow it to avoid breaking
2620 case TVN_BEGINLABELEDIT
:
2622 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2623 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2625 // although the user event handler may still veto it, it is
2626 // important to set it now so that calls to SetItemText() from
2627 // the event handler would change the text controls contents
2629 event
.m_item
= info
->item
.hItem
;
2630 event
.m_label
= info
->item
.pszText
;
2631 event
.m_editCancelled
= false;
2635 case TVN_DELETEITEM
:
2637 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2638 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2640 event
.m_item
= tv
->itemOld
.hItem
;
2644 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2645 if ( it
!= m_attrs
.end() )
2654 case TVN_ENDLABELEDIT
:
2656 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2657 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2659 event
.m_item
= info
->item
.hItem
;
2660 event
.m_label
= info
->item
.pszText
;
2661 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2666 // These *must* not be removed or TVN_GETINFOTIP will
2667 // not be processed each time the mouse is moved
2668 // and the tooltip will only ever update once.
2677 #ifdef TVN_GETINFOTIP
2678 case TVN_GETINFOTIP
:
2680 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2681 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2683 // Which item are we trying to get a tooltip for?
2684 event
.m_item
= info
->hItem
;
2691 case TVN_GETDISPINFO
:
2692 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2695 case TVN_SETDISPINFO
:
2697 if ( eventType
== wxEVT_NULL
)
2698 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2699 //else: get, already set above
2701 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2703 event
.m_item
= info
->item
.hItem
;
2707 case TVN_ITEMEXPANDING
:
2708 case TVN_ITEMEXPANDED
:
2710 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2713 switch ( tv
->action
)
2716 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2724 what
= IDX_COLLAPSE
;
2728 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2731 eventType
= gs_expandEvents
[what
][how
];
2733 event
.m_item
= tv
->itemNew
.hItem
;
2739 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2740 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2742 // fabricate the lParam and wParam parameters sufficiently
2743 // similar to the ones from a "real" WM_KEYDOWN so that
2744 // CreateKeyEvent() works correctly
2745 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2746 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2748 WXWPARAM wParam
= info
->wVKey
;
2750 int keyCode
= wxCharCodeMSWToWX(wParam
);
2753 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2758 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2763 // a separate event for Space/Return
2764 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2765 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2767 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2769 event2
.SetEventObject(this);
2770 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2772 event2
.m_item
= GetSelection();
2774 //else: don't know how to get it
2776 (void)GetEventHandler()->ProcessEvent(event2
);
2781 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2782 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2783 // we have to handle both messages:
2784 case TVN_SELCHANGEDA
:
2785 case TVN_SELCHANGEDW
:
2786 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2789 case TVN_SELCHANGINGA
:
2790 case TVN_SELCHANGINGW
:
2792 if ( eventType
== wxEVT_NULL
)
2793 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2794 //else: already set above
2796 if (hdr
->code
== TVN_SELCHANGINGW
||
2797 hdr
->code
== TVN_SELCHANGEDW
)
2799 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2800 event
.m_item
= tv
->itemNew
.hItem
;
2801 event
.m_itemOld
= tv
->itemOld
.hItem
;
2805 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2806 event
.m_item
= tv
->itemNew
.hItem
;
2807 event
.m_itemOld
= tv
->itemOld
.hItem
;
2812 // instead of explicitly checking for _WIN32_IE, check if the
2813 // required symbols are available in the headers
2814 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2817 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2818 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2819 switch ( nmcd
.dwDrawStage
)
2822 // if we've got any items with non standard attributes,
2823 // notify us before painting each item
2824 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2828 case CDDS_ITEMPREPAINT
:
2830 wxMapTreeAttr::iterator
2831 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2833 if ( it
== m_attrs
.end() )
2835 // nothing to do for this item
2836 *result
= CDRF_DODEFAULT
;
2840 wxTreeItemAttr
* const attr
= it
->second
;
2842 // selection colours should override ours,
2843 // otherwise it is too confusing ot the user
2844 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) )
2847 if ( attr
->HasBackgroundColour() )
2849 colBack
= attr
->GetBackgroundColour();
2850 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2854 // but we still want to keep the special foreground
2855 // colour when we don't have focus (we can't keep
2856 // it when we do, it would usually be unreadable on
2857 // the almost inverted bg colour...)
2858 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2859 FindFocus() != this )
2862 if ( attr
->HasTextColour() )
2864 colText
= attr
->GetTextColour();
2865 lptvcd
->clrText
= wxColourToRGB(colText
);
2869 if ( attr
->HasFont() )
2871 HFONT hFont
= GetHfontOf(attr
->GetFont());
2873 ::SelectObject(nmcd
.hdc
, hFont
);
2875 *result
= CDRF_NEWFONT
;
2877 else // no specific font
2879 *result
= CDRF_DODEFAULT
;
2885 *result
= CDRF_DODEFAULT
;
2889 // we always process it
2891 #endif // have owner drawn support in headers
2895 DWORD pos
= GetMessagePos();
2897 point
.x
= LOWORD(pos
);
2898 point
.y
= HIWORD(pos
);
2899 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2901 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2902 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2904 event
.m_item
= item
;
2905 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2913 TV_HITTESTINFO tvhti
;
2914 ::GetCursorPos(&tvhti
.pt
);
2915 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2916 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2918 if ( tvhti
.flags
& TVHT_ONITEM
)
2920 event
.m_item
= tvhti
.hItem
;
2921 eventType
= (int)hdr
->code
== NM_DBLCLK
2922 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2923 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2925 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2926 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2935 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2938 event
.SetEventObject(this);
2939 event
.SetEventType(eventType
);
2941 bool processed
= GetEventHandler()->ProcessEvent(event
);
2944 switch ( hdr
->code
)
2947 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2948 // the return code of this event handler as the return value for
2949 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2950 // expanded status would never work
2955 case TVN_BEGINRDRAG
:
2956 if ( event
.IsAllowed() )
2958 // normally this is impossible because the m_dragImage is
2959 // deleted once the drag operation is over
2960 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2962 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2963 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2964 m_dragImage
->Show();
2968 case TVN_DELETEITEM
:
2970 // NB: we might process this message using wxWidgets event
2971 // tables, but due to overhead of wxWin event system we
2972 // prefer to do it here ourself (otherwise deleting a tree
2973 // with many items is just too slow)
2974 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2976 wxTreeItemId item
= event
.m_item
;
2977 if ( HasIndirectData(item
) )
2979 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2981 delete data
; // can't be NULL here
2985 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2986 delete data
; // may be NULL, ok
2989 processed
= true; // Make sure we don't get called twice
2993 case TVN_BEGINLABELEDIT
:
2994 // return true to cancel label editing
2995 *result
= !event
.IsAllowed();
2997 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2998 if ( event
.IsAllowed() )
3000 HWND hText
= TreeView_GetEditControl(GetHwnd());
3003 // MBN: if m_textCtrl already has an HWND, it is a stale
3004 // pointer from a previous edit (because the user
3005 // didn't modify the label before dismissing the control,
3006 // and TVN_ENDLABELEDIT was not sent), so delete it
3007 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
3010 m_textCtrl
= new wxTextCtrl();
3011 m_textCtrl
->SetParent(this);
3012 m_textCtrl
->SetHWND((WXHWND
)hText
);
3013 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3015 // set wxTE_PROCESS_ENTER style for the text control to
3016 // force it to process the Enter presses itself, otherwise
3017 // they could be stolen from it by the dialog
3019 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3020 | wxTE_PROCESS_ENTER
);
3023 else // we had set m_idEdited before
3029 case TVN_ENDLABELEDIT
:
3030 // return true to set the label to the new string: note that we
3031 // also must pretend that we did process the message or it is going
3032 // to be passed to DefWindowProc() which will happily return false
3033 // cancelling the label change
3034 *result
= event
.IsAllowed();
3037 // ensure that we don't have the text ctrl which is going to be
3043 #ifdef TVN_GETINFOTIP
3044 case TVN_GETINFOTIP
:
3046 // If the user permitted a tooltip change, change it
3047 if (event
.IsAllowed())
3049 SetToolTip(event
.m_label
);
3056 case TVN_SELCHANGING
:
3057 case TVN_ITEMEXPANDING
:
3058 // return true to prevent the action from happening
3059 *result
= !event
.IsAllowed();
3062 case TVN_ITEMEXPANDED
:
3063 // the item is not refreshed properly after expansion when it has
3064 // an image depending on the expanded/collapsed state - bug in
3065 // comctl32.dll or our code?
3067 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
3068 wxTreeItemId
id(tv
->itemNew
.hItem
);
3070 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3078 case TVN_GETDISPINFO
:
3079 // NB: so far the user can't set the image himself anyhow, so do it
3080 // anyway - but this may change later
3081 //if ( /* !processed && */ 1 )
3083 wxTreeItemId item
= event
.m_item
;
3084 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3085 if ( info
->item
.mask
& TVIF_IMAGE
)
3088 DoGetItemImageFromData
3091 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3092 : wxTreeItemIcon_Normal
3095 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3097 info
->item
.iSelectedImage
=
3098 DoGetItemImageFromData
3101 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3102 : wxTreeItemIcon_Selected
3109 // for the other messages the return value is ignored and there is
3110 // nothing special to do
3115 // ----------------------------------------------------------------------------
3117 // ----------------------------------------------------------------------------
3119 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3120 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3122 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
3125 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3126 tvi
.mask
= TVIF_STATE
;
3127 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3129 // Select the specified state, or -1 == cycle to the next one.
3132 TreeView_GetItem(GetHwnd(), &tvi
);
3134 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
3135 if ( state
== m_imageListState
->GetImageCount() )
3139 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3140 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3142 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3144 TreeView_SetItem(GetHwnd(), &tvi
);
3147 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3150 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3151 tvi
.mask
= TVIF_STATE
;
3152 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3153 TreeView_GetItem(GetHwnd(), &tvi
);
3155 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3158 #endif // wxUSE_TREECTRL