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/msw/private.h"
31 // include <commctrl.h> "properly"
32 #include "wx/msw/wrapcctl.h"
34 #include "wx/msw/missing.h"
36 // Set this to 1 to be _absolutely_ sure that repainting will work for all
37 // comctl32.dll versions
38 #define wxUSE_COMCTL32_SAFELY 0
42 #include "wx/dynarray.h"
43 #include "wx/imaglist.h"
44 #include "wx/settings.h"
45 #include "wx/treectrl.h"
46 #include "wx/msw/dragimag.h"
48 // macros to hide the cast ugliness
49 // --------------------------------
51 // get HTREEITEM from wxTreeItemId
52 #define HITEM(item) ((HTREEITEM)(((item).m_pItem)))
54 // the native control doesn't support multiple selections under MSW and we
55 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
56 // checkboxes be the selection status (checked == selected) or by really
57 // emulating everything, i.e. intercepting mouse and key events &c. The first
58 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
60 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 // wrapper for TreeView_HitTest
67 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
73 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
76 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
78 // wrappers for TreeView_GetItem/TreeView_SetItem
79 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
83 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
84 tvi
.stateMask
= TVIS_SELECTED
;
87 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
89 wxLogLastError(wxT("TreeView_GetItem"));
92 return (tvi
.state
& TVIS_SELECTED
) != 0;
95 static bool SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
98 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
99 tvi
.stateMask
= TVIS_SELECTED
;
100 tvi
.state
= select
? TVIS_SELECTED
: 0;
103 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
105 wxLogLastError(wxT("TreeView_SetItem"));
112 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
114 SelectItem(hwndTV
, htItem
, false);
117 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
119 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
122 // helper function which selects all items in a range and, optionally,
123 // unselects all others
124 static void SelectRange(HWND hwndTV
,
127 bool unselectOthers
= true)
129 // find the first (or last) item and select it
131 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
132 while ( htItem
&& cont
)
134 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
136 if ( !IsItemSelected(hwndTV
, htItem
) )
138 SelectItem(hwndTV
, htItem
);
145 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
147 UnselectItem(hwndTV
, htItem
);
151 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
154 // select the items in range
155 cont
= htFirst
!= htLast
;
156 while ( htItem
&& cont
)
158 if ( !IsItemSelected(hwndTV
, htItem
) )
160 SelectItem(hwndTV
, htItem
);
163 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
165 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
169 if ( unselectOthers
)
173 if ( IsItemSelected(hwndTV
, htItem
) )
175 UnselectItem(hwndTV
, htItem
);
178 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
182 // seems to be necessary - otherwise the just selected items don't always
183 // appear as selected
184 UpdateWindow(hwndTV
);
187 // helper function which tricks the standard control into changing the focused
188 // item without changing anything else (if someone knows why Microsoft doesn't
189 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
190 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
193 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
198 if ( htItem
!= htFocus
)
200 // remember the selection state of the item
201 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
203 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
205 // prevent the tree from unselecting the old focus which it
206 // would do by default (TreeView_SelectItem unselects the
208 TreeView_SelectItem(hwndTV
, 0);
209 SelectItem(hwndTV
, htFocus
);
212 TreeView_SelectItem(hwndTV
, htItem
);
216 // need to clear the selection which TreeView_SelectItem() gave
218 UnselectItem(hwndTV
, htItem
);
220 //else: was selected, still selected - ok
222 //else: nothing to do, focus already there
228 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
230 // just clear the focus
231 TreeView_SelectItem(hwndTV
, 0);
233 if ( wasFocusSelected
)
235 // restore the selection state
236 SelectItem(hwndTV
, htFocus
);
239 //else: nothing to do, no focus already
243 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 // a convenient wrapper around TV_ITEM struct which adds a ctor
251 #pragma warning( disable : 4097 ) // inheriting from typedef
254 struct wxTreeViewItem
: public TV_ITEM
256 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
257 UINT mask_
, // fields which are valid
258 UINT stateMask_
= 0) // for TVIF_STATE only
262 // hItem member is always valid
263 mask
= mask_
| TVIF_HANDLE
;
264 stateMask
= stateMask_
;
269 // wxVirutalNode is used in place of a single root when 'hidden' root is
271 class wxVirtualNode
: public wxTreeViewItem
274 wxVirtualNode(wxTreeItemData
*data
)
275 : wxTreeViewItem(TVI_ROOT
, 0)
285 wxTreeItemData
*GetData() const { return m_data
; }
286 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
289 wxTreeItemData
*m_data
;
291 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
295 #pragma warning( default : 4097 )
298 // a macro to get the virtual root, returns NULL if none
299 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
301 // returns true if the item is the virtual root
302 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
304 // a class which encapsulates the tree traversal logic: it vists all (unless
305 // OnVisit() returns false) items under the given one
306 class wxTreeTraversal
309 wxTreeTraversal(const wxTreeCtrl
*tree
)
314 // give it a virtual dtor: not really needed as the class is never used
315 // polymorphically and not even allocated on heap at all, but this is safer
316 // (in case it ever is) and silences the compiler warnings for now
317 virtual ~wxTreeTraversal() { }
319 // do traverse the tree: visit all items (recursively by default) under the
320 // given one; return true if all items were traversed or false if the
321 // traversal was aborted because OnVisit returned false
322 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
324 // override this function to do whatever is needed for each item, return
325 // false to stop traversing
326 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
329 const wxTreeCtrl
*GetTree() const { return m_tree
; }
332 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
334 const wxTreeCtrl
*m_tree
;
336 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
339 // internal class for getting the selected items
340 class TraverseSelections
: public wxTreeTraversal
343 TraverseSelections(const wxTreeCtrl
*tree
,
344 wxArrayTreeItemIds
& selections
)
345 : wxTreeTraversal(tree
), m_selections(selections
)
347 m_selections
.Empty();
349 if (tree
->GetCount() > 0)
350 DoTraverse(tree
->GetRootItem());
353 virtual bool OnVisit(const wxTreeItemId
& item
)
355 // can't visit a virtual node.
356 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
361 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
362 if ( GetTree()->IsItemChecked(item
) )
364 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
367 m_selections
.Add(item
);
373 size_t GetCount() const { return m_selections
.GetCount(); }
376 wxArrayTreeItemIds
& m_selections
;
378 DECLARE_NO_COPY_CLASS(TraverseSelections
)
381 // internal class for counting tree items
382 class TraverseCounter
: public wxTreeTraversal
385 TraverseCounter(const wxTreeCtrl
*tree
,
386 const wxTreeItemId
& root
,
388 : wxTreeTraversal(tree
)
392 DoTraverse(root
, recursively
);
395 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
402 size_t GetCount() const { return m_count
; }
407 DECLARE_NO_COPY_CLASS(TraverseCounter
)
410 // ----------------------------------------------------------------------------
411 // This class is needed for support of different images: the Win32 common
412 // control natively supports only 2 images (the normal one and another for the
413 // selected state). We wish to provide support for 2 more of them for folder
414 // items (i.e. those which have children): for expanded state and for expanded
415 // selected state. For this we use this structure to store the additional items
418 // There is only one problem with this: when we retrieve the item's data, we
419 // don't know whether we get a pointer to wxTreeItemData or
420 // wxTreeItemIndirectData. So we always set the item id to an invalid value
421 // in this class and the code using the client data checks for it and retrieves
422 // the real client data in this case.
423 // ----------------------------------------------------------------------------
425 class wxTreeItemIndirectData
: public wxTreeItemData
428 // ctor associates this data with the item and the real item data becomes
429 // available through our GetData() method
430 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
432 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
438 m_data
= tree
->GetItemData(item
);
440 // and set ourselves as the new one
441 tree
->SetIndirectItemData(item
, this);
443 // we must have the invalid value for the item
447 // dtor deletes the associated data as well
448 virtual ~wxTreeItemIndirectData() { delete m_data
; }
451 // get the real data associated with the item
452 wxTreeItemData
*GetData() const { return m_data
; }
454 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
456 // do we have such image?
457 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
459 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
461 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
464 // all the images associated with the item
465 int m_images
[wxTreeItemIcon_Max
];
467 // the real client data
468 wxTreeItemData
*m_data
;
470 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
473 // ----------------------------------------------------------------------------
475 // ----------------------------------------------------------------------------
477 #if wxUSE_EXTENDED_RTTI
478 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
480 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
481 // new style border flags, we put them first to
482 // use them for streaming out
483 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
484 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
485 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
486 wxFLAGS_MEMBER(wxBORDER_RAISED
)
487 wxFLAGS_MEMBER(wxBORDER_STATIC
)
488 wxFLAGS_MEMBER(wxBORDER_NONE
)
490 // old style border flags
491 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
492 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
493 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
494 wxFLAGS_MEMBER(wxRAISED_BORDER
)
495 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
496 wxFLAGS_MEMBER(wxBORDER
)
498 // standard window styles
499 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
500 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
501 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
502 wxFLAGS_MEMBER(wxWANTS_CHARS
)
503 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
504 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
505 wxFLAGS_MEMBER(wxVSCROLL
)
506 wxFLAGS_MEMBER(wxHSCROLL
)
508 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
509 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
510 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
511 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
512 wxFLAGS_MEMBER(wxTR_NO_LINES
)
513 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
514 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
515 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
516 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
517 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
518 wxFLAGS_MEMBER(wxTR_SINGLE
)
519 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
520 wxFLAGS_MEMBER(wxTR_EXTENDED
)
521 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
523 wxEND_FLAGS( wxTreeCtrlStyle
)
525 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
527 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
528 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
529 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
530 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
531 wxEND_PROPERTIES_TABLE()
533 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
534 wxEND_HANDLERS_TABLE()
536 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
538 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
541 // ----------------------------------------------------------------------------
543 // ----------------------------------------------------------------------------
545 // indices in gs_expandEvents table below
560 // handy table for sending events - it has to be initialized during run-time
561 // now so can't be const any more
562 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
565 but logically it's a const table with the following entries:
568 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
569 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
573 // ============================================================================
575 // ============================================================================
577 // ----------------------------------------------------------------------------
579 // ----------------------------------------------------------------------------
581 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
583 if ( !OnVisit(root
) )
586 return Traverse(root
, recursively
);
589 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
591 wxTreeItemIdValue cookie
;
592 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
593 while ( child
.IsOk() )
595 // depth first traversal
596 if ( recursively
&& !Traverse(child
, true) )
599 if ( !OnVisit(child
) )
602 child
= m_tree
->GetNextChild(root
, cookie
);
608 // ----------------------------------------------------------------------------
609 // construction and destruction
610 // ----------------------------------------------------------------------------
612 void wxTreeCtrl::Init()
615 m_hasAnyAttr
= false;
617 m_pVirtualRoot
= NULL
;
619 // initialize the global array of events now as it can't be done statically
620 // with the wxEVT_XXX values being allocated during run-time only
621 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
622 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
623 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
624 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
627 bool wxTreeCtrl::Create(wxWindow
*parent
,
632 const wxValidator
& validator
,
633 const wxString
& name
)
637 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
638 style
|= wxBORDER_SUNKEN
;
640 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
644 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
645 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
647 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
648 wstyle
|= TVS_HASLINES
;
649 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
650 wstyle
|= TVS_HASBUTTONS
;
652 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
653 wstyle
|= TVS_EDITLABELS
;
655 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
656 wstyle
|= TVS_LINESATROOT
;
658 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
660 if ( wxApp::GetComCtl32Version() >= 471 )
661 wstyle
|= TVS_FULLROWSELECT
;
664 // using TVS_CHECKBOXES for emulation of a multiselection tree control
665 // doesn't work without the new enough headers
666 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
667 !defined( __GNUWIN32_OLD__ ) && \
668 !defined( __BORLANDC__ ) && \
669 !defined( __WATCOMC__ ) && \
670 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
672 // we emulate the multiple selection tree controls by using checkboxes: set
673 // up the image list we need for this if we do have multiple selections
674 if ( m_windowStyle
& wxTR_MULTIPLE
)
675 wstyle
|= TVS_CHECKBOXES
;
676 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
678 #if !defined(__WXWINCE__) && defined(TVS_INFOTIP)
679 // Need so that TVN_GETINFOTIP messages will be sent
680 wstyle
|= TVS_INFOTIP
;
683 // Create the tree control.
684 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
, pos
, size
) )
687 #if wxUSE_COMCTL32_SAFELY
688 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
689 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
691 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
692 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
694 // This works around a bug in the Windows tree control whereby for some versions
695 // of comctrl32, setting any colour actually draws the background in black.
696 // This will initialise the background to the system colour.
697 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
698 // Assume the user has an updated comctl32.dll.
699 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
700 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
701 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
705 // VZ: this is some experimental code which may be used to get the
706 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
707 // AFAIK, the standard DLL does about the same thing anyhow.
709 if ( m_windowStyle
& wxTR_MULTIPLE
)
713 // create the DC compatible with the current screen
714 HDC hdcMem
= CreateCompatibleDC(NULL
);
716 // create a mono bitmap of the standard size
717 int x
= ::GetSystemMetrics(SM_CXMENUCHECK
);
718 int y
= ::GetSystemMetrics(SM_CYMENUCHECK
);
719 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
720 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
721 1, // # of color planes
722 1, // # bits needed for one pixel
723 0); // array containing colour data
724 SelectObject(hdcMem
, hbmpCheck
);
726 // then draw a check mark into it
727 RECT rect
= { 0, 0, x
, y
};
728 if ( !::DrawFrameControl(hdcMem
, &rect
,
730 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
732 wxLogLastError(wxT("DrawFrameControl(check)"));
735 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
736 imagelistCheckboxes
.Add(bmp
);
738 if ( !::DrawFrameControl(hdcMem
, &rect
,
742 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
745 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
746 imagelistCheckboxes
.Add(bmp
);
752 SetStateImageList(&imagelistCheckboxes
);
756 wxSetCCUnicodeFormat(GetHwnd());
761 wxTreeCtrl::~wxTreeCtrl()
763 // delete any attributes
766 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
768 // prevent TVN_DELETEITEM handler from deleting the attributes again!
769 m_hasAnyAttr
= false;
774 // delete user data to prevent memory leaks
775 // also deletes hidden root node storage.
779 // ----------------------------------------------------------------------------
781 // ----------------------------------------------------------------------------
783 /* static */ wxVisualAttributes
784 wxTreeCtrl::GetClassDefaultAttributes(wxWindowVariant variant
)
786 wxVisualAttributes attrs
= GetCompositeControlsDefaultAttributes(variant
);
788 // common controls have their own default font
789 attrs
.font
= wxGetCCDefaultFont();
795 // simple wrappers which add error checking in debug mode
797 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
799 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
800 _T("can't retrieve virtual root item") );
802 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
804 wxLogLastError(wxT("TreeView_GetItem"));
812 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
814 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
816 wxLogLastError(wxT("TreeView_SetItem"));
820 size_t wxTreeCtrl::GetCount() const
822 return (size_t)TreeView_GetCount(GetHwnd());
825 unsigned int wxTreeCtrl::GetIndent() const
827 return TreeView_GetIndent(GetHwnd());
830 void wxTreeCtrl::SetIndent(unsigned int indent
)
832 TreeView_SetIndent(GetHwnd(), indent
);
835 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
838 (void) TreeView_SetImageList(GetHwnd(),
839 imageList
? imageList
->GetHIMAGELIST() : 0,
843 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
845 if (m_ownsImageListNormal
)
846 delete m_imageListNormal
;
848 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
849 m_ownsImageListNormal
= false;
852 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
854 if (m_ownsImageListState
) delete m_imageListState
;
855 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
856 m_ownsImageListState
= false;
859 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
860 bool recursively
) const
862 wxCHECK_MSG( item
.IsOk(), 0u, wxT("invalid tree item") );
864 TraverseCounter
counter(this, item
, recursively
);
865 return counter
.GetCount() - 1;
868 // ----------------------------------------------------------------------------
870 // ----------------------------------------------------------------------------
872 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
874 #if !wxUSE_COMCTL32_SAFELY
875 if ( !wxWindowBase::SetBackgroundColour(colour
) )
878 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
884 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
886 #if !wxUSE_COMCTL32_SAFELY
887 if ( !wxWindowBase::SetForegroundColour(colour
) )
890 ::SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
896 // ----------------------------------------------------------------------------
898 // ----------------------------------------------------------------------------
900 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
902 wxCHECK_MSG( item
.IsOk(), wxEmptyString
, wxT("invalid tree item") );
904 wxChar buf
[512]; // the size is arbitrary...
906 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
907 tvItem
.pszText
= buf
;
908 tvItem
.cchTextMax
= WXSIZEOF(buf
);
909 if ( !DoGetItem(&tvItem
) )
911 // don't return some garbage which was on stack, but an empty string
915 return wxString(buf
);
918 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
920 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
922 if ( IS_VIRTUAL_ROOT(item
) )
925 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
926 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
929 // when setting the text of the item being edited, the text control should
930 // be updated to reflect the new text as well, otherwise calling
931 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
933 // don't use GetEditControl() here because m_textCtrl is not set yet
934 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
937 if ( item
== m_idEdited
)
939 ::SetWindowText(hwndEdit
, text
);
944 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
945 wxTreeItemIcon which
) const
947 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
948 if ( !DoGetItem(&tvItem
) )
953 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
956 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
958 wxTreeItemIcon which
) const
960 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
961 if ( !DoGetItem(&tvItem
) )
966 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
968 data
->SetImage(image
, which
);
970 // make sure that we have selected images as well
971 if ( which
== wxTreeItemIcon_Normal
&&
972 !data
->HasImage(wxTreeItemIcon_Selected
) )
974 data
->SetImage(image
, wxTreeItemIcon_Selected
);
977 if ( which
== wxTreeItemIcon_Expanded
&&
978 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
980 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
984 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
988 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
989 tvItem
.iSelectedImage
= imageSel
;
990 tvItem
.iImage
= image
;
994 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
995 wxTreeItemIcon which
) const
997 wxCHECK_MSG( item
.IsOk(), -1, wxT("invalid tree item") );
999 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1001 // TODO: Maybe a hidden root can still provide images?
1005 if ( HasIndirectData(item
) )
1007 return DoGetItemImageFromData(item
, which
);
1014 wxFAIL_MSG( wxT("unknown tree item image type") );
1016 case wxTreeItemIcon_Normal
:
1020 case wxTreeItemIcon_Selected
:
1021 mask
= TVIF_SELECTEDIMAGE
;
1024 case wxTreeItemIcon_Expanded
:
1025 case wxTreeItemIcon_SelectedExpanded
:
1029 wxTreeViewItem
tvItem(item
, mask
);
1032 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1035 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1036 wxTreeItemIcon which
)
1038 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1040 if ( IS_VIRTUAL_ROOT(item
) )
1042 // TODO: Maybe a hidden root can still store images?
1052 wxFAIL_MSG( wxT("unknown tree item image type") );
1055 case wxTreeItemIcon_Normal
:
1057 const int imageNormalOld
= GetItemImage(item
);
1058 const int imageSelOld
=
1059 GetItemImage(item
, wxTreeItemIcon_Selected
);
1061 // always set the normal image
1062 imageNormal
= image
;
1064 // if the selected and normal images were the same, they should
1065 // be the same after the update, otherwise leave the selected
1067 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1071 case wxTreeItemIcon_Selected
:
1072 imageNormal
= GetItemImage(item
);
1076 case wxTreeItemIcon_Expanded
:
1077 case wxTreeItemIcon_SelectedExpanded
:
1078 if ( !HasIndirectData(item
) )
1080 // we need to get the old images first, because after we create
1081 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1083 imageNormal
= GetItemImage(item
);
1084 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1086 // if it doesn't have it yet, add it
1087 wxTreeItemIndirectData
*data
= new
1088 wxTreeItemIndirectData(this, item
);
1090 // copy the data to the new location
1091 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1092 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1095 DoSetItemImageFromData(item
, image
, which
);
1097 // reset the normal/selected images because we won't use them any
1098 // more - now they're stored inside the indirect data
1100 imageSel
= I_IMAGECALLBACK
;
1104 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1105 // change both normal and selected image - otherwise the change simply
1106 // doesn't take place!
1107 DoSetItemImages(item
, imageNormal
, imageSel
);
1110 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1112 wxCHECK_MSG( item
.IsOk(), NULL
, wxT("invalid tree item") );
1114 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1116 // Hidden root may have data.
1117 if ( IS_VIRTUAL_ROOT(item
) )
1119 return GET_VIRTUAL_ROOT()->GetData();
1123 if ( !DoGetItem(&tvItem
) )
1128 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1129 if ( IsDataIndirect(data
) )
1131 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1137 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1139 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1141 if ( IS_VIRTUAL_ROOT(item
) )
1143 GET_VIRTUAL_ROOT()->SetData(data
);
1146 // first, associate this piece of data with this item
1152 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1154 if ( HasIndirectData(item
) )
1156 if ( DoGetItem(&tvItem
) )
1158 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1162 wxFAIL_MSG( wxT("failed to change tree items data") );
1167 tvItem
.lParam
= (LPARAM
)data
;
1172 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1173 wxTreeItemIndirectData
*data
)
1175 // this should never happen because it's unnecessary and will probably lead
1176 // to crash too because the code elsewhere supposes that the pointer the
1177 // wxTreeItemIndirectData has is a real wxItemData and not
1178 // wxTreeItemIndirectData as well
1179 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1181 SetItemData(item
, data
);
1184 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1186 // query the item itself
1187 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1188 if ( !DoGetItem(&tvItem
) )
1193 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1195 return data
&& IsDataIndirect(data
);
1198 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1200 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1202 if ( IS_VIRTUAL_ROOT(item
) )
1205 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1206 tvItem
.cChildren
= (int)has
;
1210 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1212 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1214 if ( IS_VIRTUAL_ROOT(item
) )
1217 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1218 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1222 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1224 if ( IS_VIRTUAL_ROOT(item
) )
1227 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1228 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1232 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1234 if ( IS_VIRTUAL_ROOT(item
) )
1238 if ( GetBoundingRect(item
, rect
) )
1244 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1246 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1248 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1249 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1252 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1254 wxCHECK_MSG( item
.IsOk(), wxNullColour
, wxT("invalid tree item") );
1256 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1257 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1260 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1262 wxCHECK_MSG( item
.IsOk(), wxNullFont
, wxT("invalid tree item") );
1264 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1265 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1268 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1269 const wxColour
& col
)
1271 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1273 wxTreeItemAttr
*attr
;
1274 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1275 if ( it
== m_attrs
.end() )
1277 m_hasAnyAttr
= true;
1279 m_attrs
[item
.m_pItem
] =
1280 attr
= new wxTreeItemAttr
;
1287 attr
->SetTextColour(col
);
1292 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1293 const wxColour
& col
)
1295 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1297 wxTreeItemAttr
*attr
;
1298 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1299 if ( it
== m_attrs
.end() )
1301 m_hasAnyAttr
= true;
1303 m_attrs
[item
.m_pItem
] =
1304 attr
= new wxTreeItemAttr
;
1306 else // already in the hash
1311 attr
->SetBackgroundColour(col
);
1316 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1318 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1320 wxTreeItemAttr
*attr
;
1321 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1322 if ( it
== m_attrs
.end() )
1324 m_hasAnyAttr
= true;
1326 m_attrs
[item
.m_pItem
] =
1327 attr
= new wxTreeItemAttr
;
1329 else // already in the hash
1334 attr
->SetFont(font
);
1339 // ----------------------------------------------------------------------------
1341 // ----------------------------------------------------------------------------
1343 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1345 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1347 if ( item
== wxTreeItemId(TVI_ROOT
) )
1349 // virtual (hidden) root is never visible
1353 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1356 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1357 // the HTREEITEM with TVM_GETITEMRECT
1358 *(HTREEITEM
*)&rect
= HITEM(item
);
1360 // true means to get rect for just the text, not the whole line
1361 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1363 // if TVM_GETITEMRECT returned false, then the item is definitely not
1364 // visible (because its parent is not expanded)
1368 // however if it returned true, the item might still be outside the
1369 // currently visible part of the tree, test for it (notice that partly
1370 // visible means visible here)
1371 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1374 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1376 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1378 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1381 return tvItem
.cChildren
!= 0;
1384 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1386 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1388 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1391 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1394 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1396 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1398 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1401 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1404 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1406 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1408 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1411 return (tvItem
.state
& TVIS_BOLD
) != 0;
1414 // ----------------------------------------------------------------------------
1416 // ----------------------------------------------------------------------------
1418 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1420 // Root may be real (visible) or virtual (hidden).
1421 if ( GET_VIRTUAL_ROOT() )
1424 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1427 wxTreeItemId
wxTreeCtrl::GetSelection() const
1429 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1430 wxT("this only works with single selection controls") );
1432 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1435 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1437 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1441 if ( IS_VIRTUAL_ROOT(item
) )
1443 // no parent for the virtual root
1448 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1449 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1451 // the top level items should have the virtual root as their parent
1456 return wxTreeItemId(hItem
);
1459 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1460 wxTreeItemIdValue
& cookie
) const
1462 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1464 // remember the last child returned in 'cookie'
1465 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1467 return wxTreeItemId(cookie
);
1470 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1471 wxTreeItemIdValue
& cookie
) const
1473 wxTreeItemId
fromCookie(cookie
);
1475 HTREEITEM hitem
= HITEM(fromCookie
);
1477 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1479 wxTreeItemId
item(hitem
);
1481 cookie
= item
.m_pItem
;
1486 #if WXWIN_COMPATIBILITY_2_4
1488 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1491 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1493 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1495 return wxTreeItemId((void *)cookie
);
1498 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1501 wxTreeItemId
fromCookie((void *)cookie
);
1503 HTREEITEM hitem
= HITEM(fromCookie
);
1505 hitem
= TreeView_GetNextSibling(GetHwnd(), hitem
);
1507 wxTreeItemId
item(hitem
);
1509 cookie
= (long)item
.m_pItem
;
1514 #endif // WXWIN_COMPATIBILITY_2_4
1516 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1518 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1520 // can this be done more efficiently?
1521 wxTreeItemIdValue cookie
;
1523 wxTreeItemId childLast
,
1524 child
= GetFirstChild(item
, cookie
);
1525 while ( child
.IsOk() )
1528 child
= GetNextChild(item
, cookie
);
1534 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1536 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1537 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1540 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1542 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1543 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1546 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1548 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1551 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1553 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1554 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1556 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1559 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1561 wxCHECK_MSG( item
.IsOk(), wxTreeItemId(), wxT("invalid tree item") );
1562 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1564 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1567 // ----------------------------------------------------------------------------
1568 // multiple selections emulation
1569 // ----------------------------------------------------------------------------
1571 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1573 wxCHECK_MSG( item
.IsOk(), false, wxT("invalid tree item") );
1575 // receive the desired information.
1576 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1579 // state image indices are 1 based
1580 return ((tvItem
.state
>> 12) - 1) == 1;
1583 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1585 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
1587 // receive the desired information.
1588 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1592 // state images are one-based
1593 tvItem
.state
= (check
? 2 : 1) << 12;
1598 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1600 TraverseSelections
selector(this, selections
);
1602 return selector
.GetCount();
1605 // ----------------------------------------------------------------------------
1607 // ----------------------------------------------------------------------------
1609 wxTreeItemId
wxTreeCtrl::DoInsertAfter(const wxTreeItemId
& parent
,
1610 const wxTreeItemId
& hInsertAfter
,
1611 const wxString
& text
,
1612 int image
, int selectedImage
,
1613 wxTreeItemData
*data
)
1615 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1617 _T("can't have more than one root in the tree") );
1619 TV_INSERTSTRUCT tvIns
;
1620 tvIns
.hParent
= HITEM(parent
);
1621 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1623 // this is how we insert the item as the first child: supply a NULL
1625 if ( !tvIns
.hInsertAfter
)
1627 tvIns
.hInsertAfter
= TVI_FIRST
;
1631 if ( !text
.empty() )
1634 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1638 tvIns
.item
.pszText
= NULL
;
1639 tvIns
.item
.cchTextMax
= 0;
1645 tvIns
.item
.iImage
= image
;
1647 if ( selectedImage
== -1 )
1649 // take the same image for selected icon if not specified
1650 selectedImage
= image
;
1654 if ( selectedImage
!= -1 )
1656 mask
|= TVIF_SELECTEDIMAGE
;
1657 tvIns
.item
.iSelectedImage
= selectedImage
;
1663 tvIns
.item
.lParam
= (LPARAM
)data
;
1666 tvIns
.item
.mask
= mask
;
1668 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1671 wxLogLastError(wxT("TreeView_InsertItem"));
1676 // associate the application tree item with Win32 tree item handle
1680 return wxTreeItemId(id
);
1683 // for compatibility only
1684 #if WXWIN_COMPATIBILITY_2_4
1686 void wxTreeCtrl::SetImageList(wxImageList
*imageList
, int)
1688 SetImageList(imageList
);
1691 int wxTreeCtrl::GetItemSelectedImage(const wxTreeItemId
& item
) const
1693 return GetItemImage(item
, wxTreeItemIcon_Selected
);
1696 void wxTreeCtrl::SetItemSelectedImage(const wxTreeItemId
& item
, int image
)
1698 SetItemImage(item
, image
, wxTreeItemIcon_Selected
);
1701 #endif // WXWIN_COMPATIBILITY_2_4
1703 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1704 int image
, int selectedImage
,
1705 wxTreeItemData
*data
)
1708 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1710 // create a virtual root item, the parent for all the others
1711 m_pVirtualRoot
= new wxVirtualNode(data
);
1716 return DoInsertAfter(wxTreeItemId(), wxTreeItemId(),
1717 text
, image
, selectedImage
, data
);
1720 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1722 const wxString
& text
,
1723 int image
, int selectedImage
,
1724 wxTreeItemData
*data
)
1726 wxTreeItemId idPrev
;
1727 if ( index
== (size_t)-1 )
1729 // special value: append to the end
1732 else // find the item from index
1734 wxTreeItemIdValue cookie
;
1735 wxTreeItemId idCur
= GetFirstChild(parent
, cookie
);
1736 while ( index
!= 0 && idCur
.IsOk() )
1741 idCur
= GetNextChild(parent
, cookie
);
1744 // assert, not check: if the index is invalid, we will append the item
1746 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1749 return DoInsertAfter(parent
, idPrev
, text
, image
, selectedImage
, data
);
1752 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1754 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1756 wxLogLastError(wxT("TreeView_DeleteItem"));
1760 // delete all children (but don't delete the item itself)
1761 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1763 wxTreeItemIdValue cookie
;
1765 wxArrayTreeItemIds children
;
1766 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1767 while ( child
.IsOk() )
1769 children
.Add(child
);
1771 child
= GetNextChild(item
, cookie
);
1774 size_t nCount
= children
.Count();
1775 for ( size_t n
= 0; n
< nCount
; n
++ )
1777 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(children
[n
])) )
1779 wxLogLastError(wxT("TreeView_DeleteItem"));
1784 void wxTreeCtrl::DeleteAllItems()
1786 // delete the "virtual" root item.
1787 if ( GET_VIRTUAL_ROOT() )
1789 delete GET_VIRTUAL_ROOT();
1790 m_pVirtualRoot
= NULL
;
1793 // and all the real items
1795 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1797 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1801 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1803 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1804 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1805 flag
== TVE_EXPAND
||
1807 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1809 // A hidden root can be neither expanded nor collapsed.
1810 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1811 wxT("Can't expand/collapse hidden root node!") );
1813 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1814 // emulate them. This behaviour has changed slightly with comctl32.dll
1815 // v 4.70 - now it does send them but only the first time. To maintain
1816 // compatible behaviour and also in order to not have surprises with the
1817 // future versions, don't rely on this and still do everything ourselves.
1818 // To avoid that the messages be sent twice when the item is expanded for
1819 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1821 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1825 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1827 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1828 event
.m_item
= item
;
1829 event
.SetEventObject(this);
1831 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1833 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1837 (void)GetEventHandler()->ProcessEvent(event
);
1839 //else: change didn't took place, so do nothing at all
1842 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1844 DoExpand(item
, TVE_EXPAND
);
1847 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1849 DoExpand(item
, TVE_COLLAPSE
);
1852 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1854 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1857 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1859 DoExpand(item
, TVE_TOGGLE
);
1862 #if WXWIN_COMPATIBILITY_2_4
1864 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1866 DoExpand(item
, action
);
1871 void wxTreeCtrl::Unselect()
1873 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1874 wxT("doesn't make sense, may be you want UnselectAll()?") );
1876 // just remove the selection
1877 SelectItem(wxTreeItemId());
1880 void wxTreeCtrl::UnselectAll()
1882 if ( m_windowStyle
& wxTR_MULTIPLE
)
1884 wxArrayTreeItemIds selections
;
1885 size_t count
= GetSelections(selections
);
1886 for ( size_t n
= 0; n
< count
; n
++ )
1888 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1889 SetItemCheck(HITEM(selections
[n
]), false);
1890 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1891 ::UnselectItem(GetHwnd(), HITEM(selections
[n
]));
1892 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1895 m_htSelStart
.Unset();
1899 // just remove the selection
1904 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
, bool select
)
1906 if ( m_windowStyle
& wxTR_MULTIPLE
)
1908 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1909 // selecting the item means checking it
1910 SetItemCheck(item
, select
);
1911 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1912 ::SelectItem(GetHwnd(), HITEM(item
), select
);
1913 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1917 wxASSERT_MSG( select
,
1918 _T("SelectItem(false) works only for multiselect") );
1920 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1921 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1922 // send them ourselves
1924 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1925 event
.m_item
= item
;
1926 event
.SetEventObject(this);
1928 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1929 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1931 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1933 wxLogLastError(wxT("TreeView_SelectItem"));
1937 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1938 (void)GetEventHandler()->ProcessEvent(event
);
1941 //else: program vetoed the change
1945 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1948 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1951 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1953 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1955 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1959 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1964 void wxTreeCtrl::DeleteTextCtrl()
1968 // the HWND corresponding to this control is deleted by the tree
1969 // control itself and we don't know when exactly this happens, so check
1970 // if the window still exists before calling UnsubclassWin()
1971 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1973 m_textCtrl
->SetHWND(0);
1976 m_textCtrl
->UnsubclassWin();
1977 m_textCtrl
->SetHWND(0);
1985 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1986 wxClassInfo
* textControlClass
)
1988 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1993 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1994 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1996 // this is not an error - the TVN_BEGINLABELEDIT handler might have
2005 // textctrl is subclassed in MSWOnNotify
2009 // End label editing, optionally cancelling the edit
2010 void wxTreeCtrl::DoEndEditLabel(bool discardChanges
)
2012 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
2017 wxTreeItemId
wxTreeCtrl::DoTreeHitTest(const wxPoint
& point
, int& flags
)
2019 TV_HITTESTINFO hitTestInfo
;
2020 hitTestInfo
.pt
.x
= (int)point
.x
;
2021 hitTestInfo
.pt
.y
= (int)point
.y
;
2023 (void) TreeView_HitTest(GetHwnd(), &hitTestInfo
);
2028 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
2029 flags |= wxTREE_HITTEST_##flag
2031 TRANSLATE_FLAG(ABOVE
);
2032 TRANSLATE_FLAG(BELOW
);
2033 TRANSLATE_FLAG(NOWHERE
);
2034 TRANSLATE_FLAG(ONITEMBUTTON
);
2035 TRANSLATE_FLAG(ONITEMICON
);
2036 TRANSLATE_FLAG(ONITEMINDENT
);
2037 TRANSLATE_FLAG(ONITEMLABEL
);
2038 TRANSLATE_FLAG(ONITEMRIGHT
);
2039 TRANSLATE_FLAG(ONITEMSTATEICON
);
2040 TRANSLATE_FLAG(TOLEFT
);
2041 TRANSLATE_FLAG(TORIGHT
);
2043 #undef TRANSLATE_FLAG
2045 return wxTreeItemId(hitTestInfo
.hItem
);
2048 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2050 bool textOnly
) const
2054 // Virtual root items have no bounding rectangle
2055 if ( IS_VIRTUAL_ROOT(item
) )
2060 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2063 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2069 // couldn't retrieve rect: for example, item isn't visible
2074 // ----------------------------------------------------------------------------
2076 // ----------------------------------------------------------------------------
2078 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2079 // functions such as IsDataIndirect()
2080 class wxTreeSortHelper
2083 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2086 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
2088 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
2089 if ( tree
->IsDataIndirect(data
) )
2091 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2094 return data
->GetId();
2098 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2102 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2103 wxT("sorting tree without data doesn't make sense") );
2105 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2107 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2108 GetIdFromData(tree
, pItem2
));
2111 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2113 wxCHECK_RET( item
.IsOk(), wxT("invalid tree item") );
2115 // rely on the fact that TreeView_SortChildren does the same thing as our
2116 // default behaviour, i.e. sorts items alphabetically and so call it
2117 // directly if we're not in derived class (much more efficient!)
2118 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2120 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2125 tvSort
.hParent
= HITEM(item
);
2126 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2127 tvSort
.lParam
= (LPARAM
)this;
2128 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2132 // ----------------------------------------------------------------------------
2134 // ----------------------------------------------------------------------------
2136 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2138 if ( cmd
== EN_UPDATE
)
2140 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2141 event
.SetEventObject( this );
2142 ProcessCommand(event
);
2144 else if ( cmd
== EN_KILLFOCUS
)
2146 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2147 event
.SetEventObject( this );
2148 ProcessCommand(event
);
2156 // command processed
2160 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2161 // only do it during dragging, minimize wxWin overhead (this is important for
2162 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2163 // instead of passing by wxWin events
2164 WXLRESULT
wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2166 bool processed
= false;
2168 bool isMultiple
= HasFlag(wxTR_MULTIPLE
);
2170 // This message is sent after a right-click, or when the "menu" key is pressed
2171 if ( nMsg
== WM_CONTEXTMENU
)
2173 int x
= GET_X_LPARAM(lParam
),
2174 y
= GET_Y_LPARAM(lParam
);
2175 // Convert the screen point to a client point
2176 wxPoint MenuPoint
= ScreenToClient(wxPoint(x
, y
));
2178 wxTreeEvent
event( wxEVT_COMMAND_TREE_ITEM_MENU
, GetId() );
2180 // can't use GetSelection() here as it would assert in multiselect mode
2181 event
.m_item
= wxTreeItemId(TreeView_GetSelection(GetHwnd()));
2182 event
.SetEventObject( this );
2184 // Get the bounding rectangle for the item, including the non-text areas
2186 GetBoundingRect(event
.m_item
, ItemRect
, false);
2187 // If the point is inside the bounding rectangle, use it as the click position.
2188 // This should be the case for WM_CONTEXTMENU as the result of a right-click
2189 if (ItemRect
.Inside(MenuPoint
))
2191 event
.m_pointDrag
= MenuPoint
;
2193 // Use the Explorer standard of putting the menu at the left edge of the text,
2194 // in the vertical middle of the text. Should be the case for the "menu" key
2197 // Use the bounding rectangle of only the text part
2198 GetBoundingRect(event
.m_item
, ItemRect
, true);
2199 event
.m_pointDrag
= wxPoint(ItemRect
.GetX(), ItemRect
.GetY() + ItemRect
.GetHeight() / 2);
2202 if ( GetEventHandler()->ProcessEvent(event
) )
2204 //else: continue with generating wxEVT_CONTEXT_MENU in base class code
2206 else if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2208 // we only process mouse messages here and these parameters have the
2209 // same meaning for all of them
2210 int x
= GET_X_LPARAM(lParam
),
2211 y
= GET_Y_LPARAM(lParam
);
2212 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2214 TV_HITTESTINFO tvht
;
2218 (void) TreeView_HitTest(GetHwnd(), &tvht
);
2222 case WM_RBUTTONDOWN
:
2223 // if the item we are about to right click on is not already
2224 // selected or if we click outside of any item, remove the
2225 // entire previous selection
2226 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2231 // select item and set the focus to the
2232 // newly selected item
2233 ::SelectItem(GetHwnd(), htItem
);
2234 ::SetFocus(GetHwnd(), htItem
);
2237 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2238 case WM_LBUTTONDOWN
:
2239 if ( htItem
&& isMultiple
&& (tvht
.flags
& TVHT_ONITEM
) != 0 )
2241 m_htClickedItem
= (WXHTREEITEM
) htItem
;
2242 m_ptClick
= wxPoint(x
, y
);
2244 if ( wParam
& MK_CONTROL
)
2248 // toggle selected state
2249 ::ToggleItemSelection(GetHwnd(), htItem
);
2251 ::SetFocus(GetHwnd(), htItem
);
2253 // reset on any click without Shift
2254 m_htSelStart
.Unset();
2258 else if ( wParam
& MK_SHIFT
)
2260 // this selects all items between the starting one and
2263 if ( !m_htSelStart
)
2265 // take the focused item
2266 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2270 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2271 !(wParam
& MK_CONTROL
));
2273 ::SelectItem(GetHwnd(), htItem
);
2275 ::SetFocus(GetHwnd(), htItem
);
2279 else // normal click
2281 // avoid doing anything if we click on the only
2282 // currently selected item
2286 wxArrayTreeItemIds selections
;
2287 size_t count
= GetSelections(selections
);
2290 HITEM(selections
[0]) != htItem
)
2292 // clear the previously selected items, if the
2293 // user clicked outside of the present selection.
2294 // otherwise, perform the deselection on mouse-up.
2295 // this allows multiple drag and drop to work.
2297 if (!IsItemSelected(GetHwnd(), htItem
))
2301 // prevent the click from starting in-place editing
2302 // which should only happen if we click on the
2303 // already selected item (and nothing else is
2306 TreeView_SelectItem(GetHwnd(), 0);
2307 ::SelectItem(GetHwnd(), htItem
);
2309 ::SetFocus(GetHwnd(), htItem
);
2313 // reset on any click without Shift
2314 m_htSelStart
.Unset();
2318 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2322 if ( m_htClickedItem
)
2324 int cx
= abs(m_ptClick
.x
- x
);
2325 int cy
= abs(m_ptClick
.y
- y
);
2327 if ( cx
> GetSystemMetrics( SM_CXDRAG
) || cy
> GetSystemMetrics( SM_CYDRAG
) )
2329 HWND pWnd
= ::GetParent( GetHwnd() );
2334 tv
.hdr
.hwndFrom
= GetHwnd();
2335 tv
.hdr
.idFrom
= ::GetWindowLong( GetHwnd(), GWL_ID
);
2336 tv
.hdr
.code
= TVN_BEGINDRAG
;
2338 tv
.itemNew
.hItem
= HITEM(m_htClickedItem
);
2341 ZeroMemory(&tviAux
, sizeof(tviAux
));
2342 tviAux
.hItem
= HITEM(m_htClickedItem
);
2343 tviAux
.mask
= TVIF_STATE
| TVIF_PARAM
;
2344 tviAux
.stateMask
= 0xffffffff;
2345 TreeView_GetItem( GetHwnd(), &tviAux
);
2347 tv
.itemNew
.state
= tviAux
.state
;
2348 tv
.itemNew
.lParam
= tviAux
.lParam
;
2353 ::SendMessage( pWnd
, WM_NOTIFY
, tv
.hdr
.idFrom
, (LPARAM
)&tv
);
2355 m_htClickedItem
.Unset();
2358 #endif // __WXWINCE__
2362 m_dragImage
->Move(wxPoint(x
, y
));
2365 // highlight the item as target (hiding drag image is
2366 // necessary - otherwise the display will be corrupted)
2367 m_dragImage
->Hide();
2368 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2369 m_dragImage
->Show();
2376 // facilitates multiple drag-and-drop
2377 if (htItem
&& isMultiple
)
2379 wxArrayTreeItemIds selections
;
2380 size_t count
= GetSelections(selections
);
2383 !(wParam
& MK_CONTROL
) &&
2384 !(wParam
& MK_SHIFT
))
2387 TreeView_SelectItem(GetHwnd(), htItem
);
2388 ::SelectItem(GetHwnd(), htItem
);
2389 ::SetFocus(GetHwnd(), htItem
);
2391 m_htClickedItem
.Unset();
2399 m_dragImage
->EndDrag();
2403 // generate the drag end event
2404 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2406 event
.m_item
= htItem
;
2407 event
.m_pointDrag
= wxPoint(x
, y
);
2408 event
.SetEventObject(this);
2410 (void)GetEventHandler()->ProcessEvent(event
);
2412 // if we don't do it, the tree seems to think that 2 items
2413 // are selected simultaneously which is quite weird
2414 TreeView_SelectDropTarget(GetHwnd(), 0);
2419 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2420 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2422 // the tree control greys out the selected item when it loses focus and
2423 // paints it as selected again when it regains it, but it won't do it
2424 // for the other items itself - help it
2425 wxArrayTreeItemIds selections
;
2426 size_t count
= GetSelections(selections
);
2428 for ( size_t n
= 0; n
< count
; n
++ )
2430 // TreeView_GetItemRect() will return false if item is not visible,
2431 // which may happen perfectly well
2432 if ( TreeView_GetItemRect(GetHwnd(), HITEM(selections
[n
]),
2435 ::InvalidateRect(GetHwnd(), &rect
, FALSE
);
2439 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2441 bool bCtrl
= wxIsCtrlDown(),
2442 bShift
= wxIsShiftDown();
2444 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2450 ::ToggleItemSelection(GetHwnd(), htSel
);
2456 ::SelectItem(GetHwnd(), htSel
);
2464 if ( !bCtrl
&& !bShift
)
2466 // no modifiers, just clear selection and then let the default
2467 // processing to take place
2472 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2474 HTREEITEM htNext
= (HTREEITEM
)
2475 TreeView_GetNextItem
2479 wParam
== VK_UP
? TVGN_PREVIOUSVISIBLE
2485 // at the top/bottom
2491 if ( !m_htSelStart
)
2492 m_htSelStart
= htSel
;
2494 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2498 // without changing selection
2499 ::SetFocus(GetHwnd(), htNext
);
2510 // TODO: handle Shift/Ctrl with these keys
2511 if ( !bCtrl
&& !bShift
)
2515 m_htSelStart
.Unset();
2519 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2520 else if ( nMsg
== WM_COMMAND
)
2522 // if we receive a EN_KILLFOCUS command from the in-place edit control
2523 // used for label editing, make sure to end editing
2526 UnpackCommand(wParam
, lParam
, &id
, &hwnd
, &cmd
);
2528 if ( cmd
== EN_KILLFOCUS
)
2530 if ( m_textCtrl
&& m_textCtrl
->GetHandle() == hwnd
)
2540 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2546 wxTreeCtrl::MSWDefWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2548 // default WM_RBUTTONDOWN handler enters modal loop inside DefWindowProc()
2549 // waiting for WM_RBUTTONUP and then sends the resulting WM_CONTEXTMENU to
2550 // the parent window, not us, which completely breaks everything so simply
2551 // don't let it see this message at all
2552 if ( nMsg
== WM_RBUTTONDOWN
)
2555 // but because of the above we don't get NM_RCLICK which is normally
2556 // generated by tree window proc when the modal loop mentioned above ends
2557 // because the mouse is released -- synthesize it ourselves instead
2558 if ( nMsg
== WM_RBUTTONUP
)
2561 hdr
.hwndFrom
= GetHwnd();
2562 hdr
.idFrom
= GetId();
2563 hdr
.code
= NM_RCLICK
;
2566 MSWOnNotify(GetId(), (LPARAM
)&hdr
, &rc
);
2568 // continue as usual
2571 if ( nMsg
== WM_CHAR
)
2573 // also don't let the control process Space and Return keys because it
2574 // doesn't do anything useful with them anyhow but always beeps
2575 // annoyingly when it receives them and there is no way to turn it off
2576 // simply if you just process TREEITEM_ACTIVATED event to which Space
2577 // and Enter presses are mapped in your code
2578 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2582 return wxControl::MSWDefWindowProc(nMsg
, wParam
, lParam
);
2585 // process WM_NOTIFY Windows message
2586 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2588 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2589 wxEventType eventType
= wxEVT_NULL
;
2590 NMHDR
*hdr
= (NMHDR
*)lParam
;
2592 switch ( hdr
->code
)
2595 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2598 case TVN_BEGINRDRAG
:
2600 if ( eventType
== wxEVT_NULL
)
2601 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2602 //else: left drag, already set above
2604 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2606 event
.m_item
= tv
->itemNew
.hItem
;
2607 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2609 // don't allow dragging by default: the user code must
2610 // explicitly say that it wants to allow it to avoid breaking
2616 case TVN_BEGINLABELEDIT
:
2618 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2619 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2621 // although the user event handler may still veto it, it is
2622 // important to set it now so that calls to SetItemText() from
2623 // the event handler would change the text controls contents
2625 event
.m_item
= info
->item
.hItem
;
2626 event
.m_label
= info
->item
.pszText
;
2627 event
.m_editCancelled
= false;
2631 case TVN_DELETEITEM
:
2633 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2634 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2636 event
.m_item
= tv
->itemOld
.hItem
;
2640 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2641 if ( it
!= m_attrs
.end() )
2650 case TVN_ENDLABELEDIT
:
2652 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2653 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2655 event
.m_item
= info
->item
.hItem
;
2656 event
.m_label
= info
->item
.pszText
;
2657 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2662 // These *must* not be removed or TVN_GETINFOTIP will
2663 // not be processed each time the mouse is moved
2664 // and the tooltip will only ever update once.
2673 #ifdef TVN_GETINFOTIP
2674 case TVN_GETINFOTIP
:
2676 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2677 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2679 // Which item are we trying to get a tooltip for?
2680 event
.m_item
= info
->hItem
;
2687 case TVN_GETDISPINFO
:
2688 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2691 case TVN_SETDISPINFO
:
2693 if ( eventType
== wxEVT_NULL
)
2694 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2695 //else: get, already set above
2697 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2699 event
.m_item
= info
->item
.hItem
;
2703 case TVN_ITEMEXPANDING
:
2704 case TVN_ITEMEXPANDED
:
2706 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2709 switch ( tv
->action
)
2712 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2720 what
= IDX_COLLAPSE
;
2724 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2727 eventType
= gs_expandEvents
[what
][how
];
2729 event
.m_item
= tv
->itemNew
.hItem
;
2735 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2736 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2738 // fabricate the lParam and wParam parameters sufficiently
2739 // similar to the ones from a "real" WM_KEYDOWN so that
2740 // CreateKeyEvent() works correctly
2741 const bool isAltDown
= ::GetKeyState(VK_MENU
) < 0;
2742 WXLPARAM lParam
= (isAltDown
? KF_ALTDOWN
: 0) << 16;
2744 WXWPARAM wParam
= info
->wVKey
;
2746 int keyCode
= wxCharCodeMSWToWX(wParam
);
2749 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2754 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2759 // a separate event for Space/Return
2760 if ( !wxIsCtrlDown() && !wxIsShiftDown() && !isAltDown
&&
2761 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2763 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2765 event2
.SetEventObject(this);
2766 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2768 event2
.m_item
= GetSelection();
2770 //else: don't know how to get it
2772 (void)GetEventHandler()->ProcessEvent(event2
);
2777 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2778 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2779 // we have to handle both messages:
2780 case TVN_SELCHANGEDA
:
2781 case TVN_SELCHANGEDW
:
2782 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2785 case TVN_SELCHANGINGA
:
2786 case TVN_SELCHANGINGW
:
2788 if ( eventType
== wxEVT_NULL
)
2789 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2790 //else: already set above
2792 if (hdr
->code
== TVN_SELCHANGINGW
||
2793 hdr
->code
== TVN_SELCHANGEDW
)
2795 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2796 event
.m_item
= tv
->itemNew
.hItem
;
2797 event
.m_itemOld
= tv
->itemOld
.hItem
;
2801 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2802 event
.m_item
= tv
->itemNew
.hItem
;
2803 event
.m_itemOld
= tv
->itemOld
.hItem
;
2808 // instead of explicitly checking for _WIN32_IE, check if the
2809 // required symbols are available in the headers
2810 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2813 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2814 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2815 switch ( nmcd
.dwDrawStage
)
2818 // if we've got any items with non standard attributes,
2819 // notify us before painting each item
2820 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2824 case CDDS_ITEMPREPAINT
:
2826 wxMapTreeAttr::iterator
2827 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2829 if ( it
== m_attrs
.end() )
2831 // nothing to do for this item
2832 *result
= CDRF_DODEFAULT
;
2836 wxTreeItemAttr
* const attr
= it
->second
;
2838 // selection colours should override ours,
2839 // otherwise it is too confusing ot the user
2840 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) )
2843 if ( attr
->HasBackgroundColour() )
2845 colBack
= attr
->GetBackgroundColour();
2846 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2850 // but we still want to keep the special foreground
2851 // colour when we don't have focus (we can't keep
2852 // it when we do, it would usually be unreadable on
2853 // the almost inverted bg colour...)
2854 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2855 FindFocus() != this )
2858 if ( attr
->HasTextColour() )
2860 colText
= attr
->GetTextColour();
2861 lptvcd
->clrText
= wxColourToRGB(colText
);
2865 if ( attr
->HasFont() )
2867 HFONT hFont
= GetHfontOf(attr
->GetFont());
2869 ::SelectObject(nmcd
.hdc
, hFont
);
2871 *result
= CDRF_NEWFONT
;
2873 else // no specific font
2875 *result
= CDRF_DODEFAULT
;
2881 *result
= CDRF_DODEFAULT
;
2885 // we always process it
2887 #endif // have owner drawn support in headers
2891 DWORD pos
= GetMessagePos();
2893 point
.x
= LOWORD(pos
);
2894 point
.y
= HIWORD(pos
);
2895 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2897 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2898 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2900 event
.m_item
= item
;
2901 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2909 TV_HITTESTINFO tvhti
;
2910 ::GetCursorPos(&tvhti
.pt
);
2911 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2912 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2914 if ( tvhti
.flags
& TVHT_ONITEM
)
2916 event
.m_item
= tvhti
.hItem
;
2917 eventType
= (int)hdr
->code
== NM_DBLCLK
2918 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2919 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2921 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2922 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2931 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2934 event
.SetEventObject(this);
2935 event
.SetEventType(eventType
);
2937 bool processed
= GetEventHandler()->ProcessEvent(event
);
2940 switch ( hdr
->code
)
2943 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2944 // the return code of this event handler as the return value for
2945 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2946 // expanded status would never work
2951 case TVN_BEGINRDRAG
:
2952 if ( event
.IsAllowed() )
2954 // normally this is impossible because the m_dragImage is
2955 // deleted once the drag operation is over
2956 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2958 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2959 m_dragImage
->BeginDrag(wxPoint(0,0), this);
2960 m_dragImage
->Show();
2964 case TVN_DELETEITEM
:
2966 // NB: we might process this message using wxWidgets event
2967 // tables, but due to overhead of wxWin event system we
2968 // prefer to do it here ourself (otherwise deleting a tree
2969 // with many items is just too slow)
2970 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2972 wxTreeItemId item
= event
.m_item
;
2973 if ( HasIndirectData(item
) )
2975 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2977 delete data
; // can't be NULL here
2981 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2982 delete data
; // may be NULL, ok
2985 processed
= true; // Make sure we don't get called twice
2989 case TVN_BEGINLABELEDIT
:
2990 // return true to cancel label editing
2991 *result
= !event
.IsAllowed();
2993 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2994 if ( event
.IsAllowed() )
2996 HWND hText
= TreeView_GetEditControl(GetHwnd());
2999 // MBN: if m_textCtrl already has an HWND, it is a stale
3000 // pointer from a previous edit (because the user
3001 // didn't modify the label before dismissing the control,
3002 // and TVN_ENDLABELEDIT was not sent), so delete it
3003 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
3006 m_textCtrl
= new wxTextCtrl();
3007 m_textCtrl
->SetParent(this);
3008 m_textCtrl
->SetHWND((WXHWND
)hText
);
3009 m_textCtrl
->SubclassWin((WXHWND
)hText
);
3011 // set wxTE_PROCESS_ENTER style for the text control to
3012 // force it to process the Enter presses itself, otherwise
3013 // they could be stolen from it by the dialog
3015 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
3016 | wxTE_PROCESS_ENTER
);
3019 else // we had set m_idEdited before
3025 case TVN_ENDLABELEDIT
:
3026 // return true to set the label to the new string: note that we
3027 // also must pretend that we did process the message or it is going
3028 // to be passed to DefWindowProc() which will happily return false
3029 // cancelling the label change
3030 *result
= event
.IsAllowed();
3033 // ensure that we don't have the text ctrl which is going to be
3039 #ifdef TVN_GETINFOTIP
3040 case TVN_GETINFOTIP
:
3042 // If the user permitted a tooltip change, change it
3043 if (event
.IsAllowed())
3045 SetToolTip(event
.m_label
);
3052 case TVN_SELCHANGING
:
3053 case TVN_ITEMEXPANDING
:
3054 // return true to prevent the action from happening
3055 *result
= !event
.IsAllowed();
3058 case TVN_ITEMEXPANDED
:
3059 // the item is not refreshed properly after expansion when it has
3060 // an image depending on the expanded/collapsed state - bug in
3061 // comctl32.dll or our code?
3063 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
3064 wxTreeItemId
id(tv
->itemNew
.hItem
);
3066 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
3074 case TVN_GETDISPINFO
:
3075 // NB: so far the user can't set the image himself anyhow, so do it
3076 // anyway - but this may change later
3077 //if ( /* !processed && */ 1 )
3079 wxTreeItemId item
= event
.m_item
;
3080 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
3081 if ( info
->item
.mask
& TVIF_IMAGE
)
3084 DoGetItemImageFromData
3087 IsExpanded(item
) ? wxTreeItemIcon_Expanded
3088 : wxTreeItemIcon_Normal
3091 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
3093 info
->item
.iSelectedImage
=
3094 DoGetItemImageFromData
3097 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
3098 : wxTreeItemIcon_Selected
3105 // for the other messages the return value is ignored and there is
3106 // nothing special to do
3111 // ----------------------------------------------------------------------------
3113 // ----------------------------------------------------------------------------
3115 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
3116 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
3118 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
3121 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3122 tvi
.mask
= TVIF_STATE
;
3123 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3125 // Select the specified state, or -1 == cycle to the next one.
3128 TreeView_GetItem(GetHwnd(), &tvi
);
3130 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
3131 if ( state
== m_imageListState
->GetImageCount() )
3135 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
3136 _T("wxTreeCtrl::SetState(): item index out of bounds") );
3138 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
3140 TreeView_SetItem(GetHwnd(), &tvi
);
3143 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
3146 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
3147 tvi
.mask
= TVIF_STATE
;
3148 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
3149 TreeView_GetItem(GetHwnd(), &tvi
);
3151 return STATEIMAGEMASKTOINDEX(tvi
.state
);
3154 #endif // wxUSE_TREECTRL