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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "treectrl.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/msw/private.h"
35 // Set this to 1 to be _absolutely_ sure that repainting will work for all
36 // comctl32.dll versions
37 #define wxUSE_COMCTL32_SAFELY 0
41 #include "wx/dynarray.h"
42 #include "wx/imaglist.h"
43 #include "wx/settings.h"
44 #include "wx/msw/treectrl.h"
45 #include "wx/msw/dragimag.h"
47 // include <commctrl.h> "properly"
48 #include "wx/msw/wrapcctl.h"
50 // macros to hide the cast ugliness
51 // --------------------------------
53 // ptr is the real item id, i.e. wxTreeItemId::m_pItem
54 #define HITEM_PTR(ptr) (HTREEITEM)(ptr)
56 // item here is a wxTreeItemId
57 #define HITEM(item) HITEM_PTR((item).m_pItem)
59 // the native control doesn't support multiple selections under MSW and we
60 // have 2 ways to emulate them: either using TVS_CHECKBOXES style and let
61 // checkboxes be the selection status (checked == selected) or by really
62 // emulating everything, i.e. intercepting mouse and key events &c. The first
63 // approach is much easier but doesn't work with comctl32.dll < 4.71 and also
65 #define wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE 0
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // wrapper for TreeView_HitTest
72 static HTREEITEM
GetItemFromPoint(HWND hwndTV
, int x
, int y
)
78 return (HTREEITEM
)TreeView_HitTest(hwndTV
, &tvht
);
81 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
83 // wrappers for TreeView_GetItem/TreeView_SetItem
84 static bool IsItemSelected(HWND hwndTV
, HTREEITEM hItem
)
88 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
89 tvi
.stateMask
= TVIS_SELECTED
;
92 if ( !TreeView_GetItem(hwndTV
, &tvi
) )
94 wxLogLastError(wxT("TreeView_GetItem"));
97 return (tvi
.state
& TVIS_SELECTED
) != 0;
100 static void SelectItem(HWND hwndTV
, HTREEITEM hItem
, bool select
= true)
103 tvi
.mask
= TVIF_STATE
| TVIF_HANDLE
;
104 tvi
.stateMask
= TVIS_SELECTED
;
105 tvi
.state
= select
? TVIS_SELECTED
: 0;
108 if ( TreeView_SetItem(hwndTV
, &tvi
) == -1 )
110 wxLogLastError(wxT("TreeView_SetItem"));
114 static inline void UnselectItem(HWND hwndTV
, HTREEITEM htItem
)
116 SelectItem(hwndTV
, htItem
, false);
119 static inline void ToggleItemSelection(HWND hwndTV
, HTREEITEM htItem
)
121 SelectItem(hwndTV
, htItem
, !IsItemSelected(hwndTV
, htItem
));
124 // helper function which selects all items in a range and, optionally,
125 // unselects all others
126 static void SelectRange(HWND hwndTV
,
129 bool unselectOthers
= true)
131 // find the first (or last) item and select it
133 HTREEITEM htItem
= (HTREEITEM
)TreeView_GetRoot(hwndTV
);
134 while ( htItem
&& cont
)
136 if ( (htItem
== htFirst
) || (htItem
== htLast
) )
138 if ( !IsItemSelected(hwndTV
, htItem
) )
140 SelectItem(hwndTV
, htItem
);
147 if ( unselectOthers
&& IsItemSelected(hwndTV
, htItem
) )
149 UnselectItem(hwndTV
, htItem
);
153 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
156 // select the items in range
157 cont
= htFirst
!= htLast
;
158 while ( htItem
&& cont
)
160 if ( !IsItemSelected(hwndTV
, htItem
) )
162 SelectItem(hwndTV
, htItem
);
165 cont
= (htItem
!= htFirst
) && (htItem
!= htLast
);
167 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
171 if ( unselectOthers
)
175 if ( IsItemSelected(hwndTV
, htItem
) )
177 UnselectItem(hwndTV
, htItem
);
180 htItem
= (HTREEITEM
)TreeView_GetNextVisible(hwndTV
, htItem
);
184 // seems to be necessary - otherwise the just selected items don't always
185 // appear as selected
186 UpdateWindow(hwndTV
);
189 // helper function which tricks the standard control into changing the focused
190 // item without changing anything else (if someone knows why Microsoft doesn't
191 // allow to do it by just setting TVIS_FOCUSED flag, please tell me!)
192 static void SetFocus(HWND hwndTV
, HTREEITEM htItem
)
195 HTREEITEM htFocus
= (HTREEITEM
)TreeView_GetSelection(hwndTV
);
200 if ( htItem
!= htFocus
)
202 // remember the selection state of the item
203 bool wasSelected
= IsItemSelected(hwndTV
, htItem
);
205 if ( htFocus
&& IsItemSelected(hwndTV
, htFocus
) )
207 // prevent the tree from unselecting the old focus which it
208 // would do by default (TreeView_SelectItem unselects the
210 TreeView_SelectItem(hwndTV
, 0);
211 SelectItem(hwndTV
, htFocus
);
214 TreeView_SelectItem(hwndTV
, htItem
);
218 // need to clear the selection which TreeView_SelectItem() gave
220 UnselectItem(hwndTV
, htItem
);
222 //else: was selected, still selected - ok
224 //else: nothing to do, focus already there
230 bool wasFocusSelected
= IsItemSelected(hwndTV
, htFocus
);
232 // just clear the focus
233 TreeView_SelectItem(hwndTV
, 0);
235 if ( wasFocusSelected
)
237 // restore the selection state
238 SelectItem(hwndTV
, htFocus
);
241 //else: nothing to do, no focus already
245 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
251 // a convenient wrapper around TV_ITEM struct which adds a ctor
253 #pragma warning( disable : 4097 ) // inheriting from typedef
256 struct wxTreeViewItem
: public TV_ITEM
258 wxTreeViewItem(const wxTreeItemId
& item
, // the item handle
259 UINT mask_
, // fields which are valid
260 UINT stateMask_
= 0) // for TVIF_STATE only
264 // hItem member is always valid
265 mask
= mask_
| TVIF_HANDLE
;
266 stateMask
= stateMask_
;
271 // wxVirutalNode is used in place of a single root when 'hidden' root is
273 class wxVirtualNode
: public wxTreeViewItem
276 wxVirtualNode(wxTreeItemData
*data
)
277 : wxTreeViewItem(TVI_ROOT
, 0)
287 wxTreeItemData
*GetData() const { return m_data
; }
288 void SetData(wxTreeItemData
*data
) { delete m_data
; m_data
= data
; }
291 wxTreeItemData
*m_data
;
293 DECLARE_NO_COPY_CLASS(wxVirtualNode
)
297 #pragma warning( default : 4097 )
300 // a macro to get the virtual root, returns NULL if none
301 #define GET_VIRTUAL_ROOT() ((wxVirtualNode *)m_pVirtualRoot)
303 // returns true if the item is the virtual root
304 #define IS_VIRTUAL_ROOT(item) (HITEM(item) == TVI_ROOT)
306 // a class which encapsulates the tree traversal logic: it vists all (unless
307 // OnVisit() returns false) items under the given one
308 class wxTreeTraversal
311 wxTreeTraversal(const wxTreeCtrl
*tree
)
316 // do traverse the tree: visit all items (recursively by default) under the
317 // given one; return true if all items were traversed or false if the
318 // traversal was aborted because OnVisit returned false
319 bool DoTraverse(const wxTreeItemId
& root
, bool recursively
= true);
321 // override this function to do whatever is needed for each item, return
322 // false to stop traversing
323 virtual bool OnVisit(const wxTreeItemId
& item
) = 0;
326 const wxTreeCtrl
*GetTree() const { return m_tree
; }
329 bool Traverse(const wxTreeItemId
& root
, bool recursively
);
331 const wxTreeCtrl
*m_tree
;
333 DECLARE_NO_COPY_CLASS(wxTreeTraversal
)
336 // internal class for getting the selected items
337 class TraverseSelections
: public wxTreeTraversal
340 TraverseSelections(const wxTreeCtrl
*tree
,
341 wxArrayTreeItemIds
& selections
)
342 : wxTreeTraversal(tree
), m_selections(selections
)
344 m_selections
.Empty();
346 DoTraverse(tree
->GetRootItem());
349 virtual bool OnVisit(const wxTreeItemId
& item
)
351 // can't visit a virtual node.
352 if ( (GetTree()->GetRootItem() == item
) && (GetTree()->GetWindowStyle() & wxTR_HIDE_ROOT
))
357 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
358 if ( GetTree()->IsItemChecked(item
) )
360 if ( ::IsItemSelected(GetHwndOf(GetTree()), HITEM(item
)) )
363 m_selections
.Add(item
);
369 size_t GetCount() const { return m_selections
.GetCount(); }
372 wxArrayTreeItemIds
& m_selections
;
374 DECLARE_NO_COPY_CLASS(TraverseSelections
)
377 // internal class for counting tree items
378 class TraverseCounter
: public wxTreeTraversal
381 TraverseCounter(const wxTreeCtrl
*tree
,
382 const wxTreeItemId
& root
,
384 : wxTreeTraversal(tree
)
388 DoTraverse(root
, recursively
);
391 virtual bool OnVisit(const wxTreeItemId
& WXUNUSED(item
))
398 size_t GetCount() const { return m_count
; }
403 DECLARE_NO_COPY_CLASS(TraverseCounter
)
406 // ----------------------------------------------------------------------------
407 // This class is needed for support of different images: the Win32 common
408 // control natively supports only 2 images (the normal one and another for the
409 // selected state). We wish to provide support for 2 more of them for folder
410 // items (i.e. those which have children): for expanded state and for expanded
411 // selected state. For this we use this structure to store the additional items
414 // There is only one problem with this: when we retrieve the item's data, we
415 // don't know whether we get a pointer to wxTreeItemData or
416 // wxTreeItemIndirectData. So we always set the item id to an invalid value
417 // in this class and the code using the client data checks for it and retrieves
418 // the real client data in this case.
419 // ----------------------------------------------------------------------------
421 class wxTreeItemIndirectData
: public wxTreeItemData
424 // ctor associates this data with the item and the real item data becomes
425 // available through our GetData() method
426 wxTreeItemIndirectData(wxTreeCtrl
*tree
, const wxTreeItemId
& item
)
428 for ( size_t n
= 0; n
< WXSIZEOF(m_images
); n
++ )
434 m_data
= tree
->GetItemData(item
);
436 // and set ourselves as the new one
437 tree
->SetIndirectItemData(item
, this);
439 // we must have the invalid value for the item
443 // dtor deletes the associated data as well
444 virtual ~wxTreeItemIndirectData() { delete m_data
; }
447 // get the real data associated with the item
448 wxTreeItemData
*GetData() const { return m_data
; }
450 void SetData(wxTreeItemData
*data
) { m_data
= data
; }
452 // do we have such image?
453 bool HasImage(wxTreeItemIcon which
) const { return m_images
[which
] != -1; }
455 int GetImage(wxTreeItemIcon which
) const { return m_images
[which
]; }
457 void SetImage(int image
, wxTreeItemIcon which
) { m_images
[which
] = image
; }
460 // all the images associated with the item
461 int m_images
[wxTreeItemIcon_Max
];
463 // the real client data
464 wxTreeItemData
*m_data
;
466 DECLARE_NO_COPY_CLASS(wxTreeItemIndirectData
)
469 // ----------------------------------------------------------------------------
471 // ----------------------------------------------------------------------------
473 #if wxUSE_EXTENDED_RTTI
474 WX_DEFINE_FLAGS( wxTreeCtrlStyle
)
476 WX_BEGIN_FLAGS( wxTreeCtrlStyle
)
477 // new style border flags, we put them first to
478 // use them for streaming out
479 WX_FLAGS_MEMBER(wxBORDER_SIMPLE
)
480 WX_FLAGS_MEMBER(wxBORDER_SUNKEN
)
481 WX_FLAGS_MEMBER(wxBORDER_DOUBLE
)
482 WX_FLAGS_MEMBER(wxBORDER_RAISED
)
483 WX_FLAGS_MEMBER(wxBORDER_STATIC
)
484 WX_FLAGS_MEMBER(wxBORDER_NONE
)
486 // old style border flags
487 WX_FLAGS_MEMBER(wxSIMPLE_BORDER
)
488 WX_FLAGS_MEMBER(wxSUNKEN_BORDER
)
489 WX_FLAGS_MEMBER(wxDOUBLE_BORDER
)
490 WX_FLAGS_MEMBER(wxRAISED_BORDER
)
491 WX_FLAGS_MEMBER(wxSTATIC_BORDER
)
492 WX_FLAGS_MEMBER(wxNO_BORDER
)
494 // standard window styles
495 WX_FLAGS_MEMBER(wxTAB_TRAVERSAL
)
496 WX_FLAGS_MEMBER(wxCLIP_CHILDREN
)
497 WX_FLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
498 WX_FLAGS_MEMBER(wxWANTS_CHARS
)
499 WX_FLAGS_MEMBER(wxNO_FULL_REPAINT_ON_RESIZE
)
500 WX_FLAGS_MEMBER(wxALWAYS_SHOW_SB
)
501 WX_FLAGS_MEMBER(wxVSCROLL
)
502 WX_FLAGS_MEMBER(wxHSCROLL
)
504 WX_FLAGS_MEMBER(wxTR_EDIT_LABELS
)
505 WX_FLAGS_MEMBER(wxTR_NO_BUTTONS
)
506 WX_FLAGS_MEMBER(wxTR_HAS_BUTTONS
)
507 WX_FLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
508 WX_FLAGS_MEMBER(wxTR_NO_LINES
)
509 WX_FLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
510 WX_FLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
511 WX_FLAGS_MEMBER(wxTR_HIDE_ROOT
)
512 WX_FLAGS_MEMBER(wxTR_ROW_LINES
)
513 WX_FLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
514 WX_FLAGS_MEMBER(wxTR_SINGLE
)
515 WX_FLAGS_MEMBER(wxTR_MULTIPLE
)
516 WX_FLAGS_MEMBER(wxTR_EXTENDED
)
517 WX_FLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
519 WX_END_FLAGS( wxTreeCtrlStyle
)
521 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
523 WX_BEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
524 WX_PROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
525 WX_END_PROPERTIES_TABLE()
527 WX_BEGIN_HANDLERS_TABLE(wxTreeCtrl
)
528 WX_END_HANDLERS_TABLE()
530 WX_CONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
532 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
535 // ----------------------------------------------------------------------------
537 // ----------------------------------------------------------------------------
539 // indices in gs_expandEvents table below
554 // handy table for sending events - it has to be initialized during run-time
555 // now so can't be const any more
556 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
559 but logically it's a const table with the following entries:
562 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
563 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
567 // ============================================================================
569 // ============================================================================
571 // ----------------------------------------------------------------------------
573 // ----------------------------------------------------------------------------
575 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
577 if ( !OnVisit(root
) )
580 return Traverse(root
, recursively
);
583 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
585 wxTreeItemIdValue cookie
;
586 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
587 while ( child
.IsOk() )
589 // depth first traversal
590 if ( recursively
&& !Traverse(child
, true) )
593 if ( !OnVisit(child
) )
596 child
= m_tree
->GetNextChild(root
, cookie
);
602 // ----------------------------------------------------------------------------
603 // construction and destruction
604 // ----------------------------------------------------------------------------
606 void wxTreeCtrl::Init()
608 m_imageListNormal
= NULL
;
609 m_imageListState
= NULL
;
610 m_ownsImageListNormal
= m_ownsImageListState
= false;
612 m_hasAnyAttr
= false;
614 m_pVirtualRoot
= NULL
;
616 // initialize the global array of events now as it can't be done statically
617 // with the wxEVT_XXX values being allocated during run-time only
618 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
619 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
620 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
621 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
624 bool wxTreeCtrl::Create(wxWindow
*parent
,
629 const wxValidator
& validator
,
630 const wxString
& name
)
634 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
635 style
|= wxBORDER_SUNKEN
;
637 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
641 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
642 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
644 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
645 wstyle
|= TVS_HASLINES
;
646 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
647 wstyle
|= TVS_HASBUTTONS
;
649 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
650 wstyle
|= TVS_EDITLABELS
;
652 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
653 wstyle
|= TVS_LINESATROOT
;
655 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
657 if ( wxTheApp
->GetComCtl32Version() >= 471 )
658 wstyle
|= TVS_FULLROWSELECT
;
661 // using TVS_CHECKBOXES for emulation of a multiselection tree control
662 // doesn't work without the new enough headers
663 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
664 !defined( __GNUWIN32_OLD__ ) && \
665 !defined( __BORLANDC__ ) && \
666 !defined( __WATCOMC__ ) && \
667 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
669 // we emulate the multiple selection tree controls by using checkboxes: set
670 // up the image list we need for this if we do have multiple selections
671 if ( m_windowStyle
& wxTR_MULTIPLE
)
672 wstyle
|= TVS_CHECKBOXES
;
673 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
675 // Create the tree control.
676 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
679 #if wxUSE_COMCTL32_SAFELY
680 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
681 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
683 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
684 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
686 // This works around a bug in the Windows tree control whereby for some versions
687 // of comctrl32, setting any colour actually draws the background in black.
688 // This will initialise the background to the system colour.
689 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
690 // Assume the user has an updated comctl32.dll.
691 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
692 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
693 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
697 // VZ: this is some experimental code which may be used to get the
698 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
699 // AFAIK, the standard DLL does about the same thing anyhow.
701 if ( m_windowStyle
& wxTR_MULTIPLE
)
705 // create the DC compatible with the current screen
706 HDC hdcMem
= CreateCompatibleDC(NULL
);
708 // create a mono bitmap of the standard size
709 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
710 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
711 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
712 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
713 1, // # of color planes
714 1, // # bits needed for one pixel
715 0); // array containing colour data
716 SelectObject(hdcMem
, hbmpCheck
);
718 // then draw a check mark into it
719 RECT rect
= { 0, 0, x
, y
};
720 if ( !::DrawFrameControl(hdcMem
, &rect
,
722 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
724 wxLogLastError(wxT("DrawFrameControl(check)"));
727 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
728 imagelistCheckboxes
.Add(bmp
);
730 if ( !::DrawFrameControl(hdcMem
, &rect
,
734 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
737 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
738 imagelistCheckboxes
.Add(bmp
);
744 SetStateImageList(&imagelistCheckboxes
);
748 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
753 wxTreeCtrl::~wxTreeCtrl()
755 // delete any attributes
758 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
760 // prevent TVN_DELETEITEM handler from deleting the attributes again!
761 m_hasAnyAttr
= false;
766 // delete user data to prevent memory leaks
767 // also deletes hidden root node storage.
770 if (m_ownsImageListNormal
) delete m_imageListNormal
;
771 if (m_ownsImageListState
) delete m_imageListState
;
774 // ----------------------------------------------------------------------------
776 // ----------------------------------------------------------------------------
778 // simple wrappers which add error checking in debug mode
780 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
782 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
783 _T("can't retrieve virtual root item") );
785 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
787 wxLogLastError(wxT("TreeView_GetItem"));
795 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
797 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
799 wxLogLastError(wxT("TreeView_SetItem"));
803 size_t wxTreeCtrl::GetCount() const
805 return (size_t)TreeView_GetCount(GetHwnd());
808 unsigned int wxTreeCtrl::GetIndent() const
810 return TreeView_GetIndent(GetHwnd());
813 void wxTreeCtrl::SetIndent(unsigned int indent
)
815 TreeView_SetIndent(GetHwnd(), indent
);
818 wxImageList
*wxTreeCtrl::GetImageList() const
820 return m_imageListNormal
;
823 wxImageList
*wxTreeCtrl::GetStateImageList() const
825 return m_imageListState
;
828 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
831 TreeView_SetImageList(GetHwnd(),
832 imageList
? imageList
->GetHIMAGELIST() : 0,
836 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
838 if (m_ownsImageListNormal
)
839 delete m_imageListNormal
;
841 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
842 m_ownsImageListNormal
= false;
845 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
847 if (m_ownsImageListState
) delete m_imageListState
;
848 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
849 m_ownsImageListState
= false;
852 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
854 SetImageList(imageList
);
855 m_ownsImageListNormal
= true;
858 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
860 SetStateImageList(imageList
);
861 m_ownsImageListState
= true;
864 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
865 bool recursively
) const
867 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 wxChar buf
[512]; // the size is arbitrary...
908 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
909 tvItem
.pszText
= buf
;
910 tvItem
.cchTextMax
= WXSIZEOF(buf
);
911 if ( !DoGetItem(&tvItem
) )
913 // don't return some garbage which was on stack, but an empty string
917 return wxString(buf
);
920 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
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
== GetSelection() )
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 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
999 // TODO: Maybe a hidden root can still provide images?
1003 if ( HasIndirectData(item
) )
1005 return DoGetItemImageFromData(item
, which
);
1012 wxFAIL_MSG( wxT("unknown tree item image type") );
1014 case wxTreeItemIcon_Normal
:
1018 case wxTreeItemIcon_Selected
:
1019 mask
= TVIF_SELECTEDIMAGE
;
1022 case wxTreeItemIcon_Expanded
:
1023 case wxTreeItemIcon_SelectedExpanded
:
1027 wxTreeViewItem
tvItem(item
, mask
);
1030 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1033 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1034 wxTreeItemIcon which
)
1036 if ( IS_VIRTUAL_ROOT(item
) )
1038 // TODO: Maybe a hidden root can still store images?
1048 wxFAIL_MSG( wxT("unknown tree item image type") );
1051 case wxTreeItemIcon_Normal
:
1053 const int imageNormalOld
= GetItemImage(item
);
1054 const int imageSelOld
=
1055 GetItemImage(item
, wxTreeItemIcon_Selected
);
1057 // always set the normal image
1058 imageNormal
= image
;
1060 // if the selected and normal images were the same, they should
1061 // be the same after the update, otherwise leave the selected
1063 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1067 case wxTreeItemIcon_Selected
:
1068 imageNormal
= GetItemImage(item
);
1072 case wxTreeItemIcon_Expanded
:
1073 case wxTreeItemIcon_SelectedExpanded
:
1074 if ( !HasIndirectData(item
) )
1076 // we need to get the old images first, because after we create
1077 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1079 imageNormal
= GetItemImage(item
);
1080 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1082 // if it doesn't have it yet, add it
1083 wxTreeItemIndirectData
*data
= new
1084 wxTreeItemIndirectData(this, item
);
1086 // copy the data to the new location
1087 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1088 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1091 DoSetItemImageFromData(item
, image
, which
);
1093 // reset the normal/selected images because we won't use them any
1094 // more - now they're stored inside the indirect data
1096 imageSel
= I_IMAGECALLBACK
;
1100 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1101 // change both normal and selected image - otherwise the change simply
1102 // doesn't take place!
1103 DoSetItemImages(item
, imageNormal
, imageSel
);
1106 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1108 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1110 // Hidden root may have data.
1111 if ( IS_VIRTUAL_ROOT(item
) )
1113 return GET_VIRTUAL_ROOT()->GetData();
1117 if ( !DoGetItem(&tvItem
) )
1122 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1123 if ( IsDataIndirect(data
) )
1125 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1131 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1133 if ( IS_VIRTUAL_ROOT(item
) )
1135 GET_VIRTUAL_ROOT()->SetData(data
);
1138 // first, associate this piece of data with this item
1144 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1146 if ( HasIndirectData(item
) )
1148 if ( DoGetItem(&tvItem
) )
1150 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1154 wxFAIL_MSG( wxT("failed to change tree items data") );
1159 tvItem
.lParam
= (LPARAM
)data
;
1164 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1165 wxTreeItemIndirectData
*data
)
1167 // this should never happen because it's unnecessary and will probably lead
1168 // to crash too because the code elsewhere supposes that the pointer the
1169 // wxTreeItemIndirectData has is a real wxItemData and not
1170 // wxTreeItemIndirectData as well
1171 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1173 SetItemData(item
, data
);
1176 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1178 // query the item itself
1179 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1180 if ( !DoGetItem(&tvItem
) )
1185 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1187 return data
&& IsDataIndirect(data
);
1190 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1192 if ( IS_VIRTUAL_ROOT(item
) )
1195 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1196 tvItem
.cChildren
= (int)has
;
1200 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1202 if ( IS_VIRTUAL_ROOT(item
) )
1205 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1206 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1210 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1212 if ( IS_VIRTUAL_ROOT(item
) )
1215 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1216 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1220 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1222 if ( IS_VIRTUAL_ROOT(item
) )
1226 if ( GetBoundingRect(item
, rect
) )
1232 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1234 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1236 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1239 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1241 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1243 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1246 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1248 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1250 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1253 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1254 const wxColour
& col
)
1256 wxTreeItemAttr
*attr
;
1257 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1258 if ( it
== m_attrs
.end() )
1260 m_hasAnyAttr
= true;
1262 m_attrs
[item
.m_pItem
] =
1263 attr
= new wxTreeItemAttr
;
1270 attr
->SetTextColour(col
);
1275 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1276 const wxColour
& col
)
1278 wxTreeItemAttr
*attr
;
1279 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1280 if ( it
== m_attrs
.end() )
1282 m_hasAnyAttr
= true;
1284 m_attrs
[item
.m_pItem
] =
1285 attr
= new wxTreeItemAttr
;
1287 else // already in the hash
1292 attr
->SetBackgroundColour(col
);
1297 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1299 wxTreeItemAttr
*attr
;
1300 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1301 if ( it
== m_attrs
.end() )
1303 m_hasAnyAttr
= true;
1305 m_attrs
[item
.m_pItem
] =
1306 attr
= new wxTreeItemAttr
;
1308 else // already in the hash
1313 attr
->SetFont(font
);
1318 // ----------------------------------------------------------------------------
1320 // ----------------------------------------------------------------------------
1322 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1324 if ( item
== wxTreeItemId(TVI_ROOT
) )
1326 // virtual (hidden) root is never visible
1330 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1333 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1334 // the HTREEITEM with TVM_GETITEMRECT
1335 *(HTREEITEM
*)&rect
= HITEM(item
);
1337 // false means get item rect for the whole item, not only text
1338 return SendMessage(GetHwnd(), TVM_GETITEMRECT
, false, (LPARAM
)&rect
) != 0;
1341 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1343 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1346 return tvItem
.cChildren
!= 0;
1349 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1351 // probably not a good idea to put it here
1352 //wxASSERT( ItemHasChildren(item) );
1354 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1357 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1360 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1362 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1365 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1368 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1370 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1373 return (tvItem
.state
& TVIS_BOLD
) != 0;
1376 // ----------------------------------------------------------------------------
1378 // ----------------------------------------------------------------------------
1380 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1382 // Root may be real (visible) or virtual (hidden).
1383 if ( GET_VIRTUAL_ROOT() )
1386 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1389 wxTreeItemId
wxTreeCtrl::GetSelection() const
1391 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1392 wxT("this only works with single selection controls") );
1394 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1397 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1401 if ( IS_VIRTUAL_ROOT(item
) )
1403 // no parent for the virtual root
1408 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1409 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1411 // the top level items should have the virtual root as their parent
1416 return wxTreeItemId(hItem
);
1419 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1420 wxTreeItemIdValue
& cookie
) const
1422 // remember the last child returned in 'cookie'
1423 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1425 return wxTreeItemId(cookie
);
1428 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1429 wxTreeItemIdValue
& cookie
) const
1431 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1432 HITEM(wxTreeItemId(cookie
))));
1433 cookie
= item
.m_pItem
;
1438 #if WXWIN_COMPATIBILITY_2_4
1440 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1443 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1445 return wxTreeItemId((void *)cookie
);
1448 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1451 wxTreeItemId
item(TreeView_GetNextSibling
1454 HITEM(wxTreeItemId((void *)cookie
)
1456 cookie
= (long)item
.m_pItem
;
1461 #endif // WXWIN_COMPATIBILITY_2_4
1463 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1465 // can this be done more efficiently?
1466 wxTreeItemIdValue cookie
;
1468 wxTreeItemId childLast
,
1469 child
= GetFirstChild(item
, cookie
);
1470 while ( child
.IsOk() )
1473 child
= GetNextChild(item
, cookie
);
1479 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1481 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1484 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1486 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1489 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1491 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1494 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1496 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1498 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1501 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1503 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1505 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1508 // ----------------------------------------------------------------------------
1509 // multiple selections emulation
1510 // ----------------------------------------------------------------------------
1512 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1514 // receive the desired information.
1515 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1518 // state image indices are 1 based
1519 return ((tvItem
.state
>> 12) - 1) == 1;
1522 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1524 // receive the desired information.
1525 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1529 // state images are one-based
1530 tvItem
.state
= (check
? 2 : 1) << 12;
1535 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1537 TraverseSelections
selector(this, selections
);
1539 return selector
.GetCount();
1542 // ----------------------------------------------------------------------------
1544 // ----------------------------------------------------------------------------
1546 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1547 wxTreeItemId hInsertAfter
,
1548 const wxString
& text
,
1549 int image
, int selectedImage
,
1550 wxTreeItemData
*data
)
1552 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1554 _T("can't have more than one root in the tree") );
1556 TV_INSERTSTRUCT tvIns
;
1557 tvIns
.hParent
= HITEM(parent
);
1558 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1560 // this is how we insert the item as the first child: supply a NULL
1562 if ( !tvIns
.hInsertAfter
)
1564 tvIns
.hInsertAfter
= TVI_FIRST
;
1568 if ( !text
.IsEmpty() )
1571 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1575 tvIns
.item
.pszText
= NULL
;
1576 tvIns
.item
.cchTextMax
= 0;
1582 tvIns
.item
.iImage
= image
;
1584 if ( selectedImage
== -1 )
1586 // take the same image for selected icon if not specified
1587 selectedImage
= image
;
1591 if ( selectedImage
!= -1 )
1593 mask
|= TVIF_SELECTEDIMAGE
;
1594 tvIns
.item
.iSelectedImage
= selectedImage
;
1600 tvIns
.item
.lParam
= (LPARAM
)data
;
1603 tvIns
.item
.mask
= mask
;
1605 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1608 wxLogLastError(wxT("TreeView_InsertItem"));
1613 // associate the application tree item with Win32 tree item handle
1617 return wxTreeItemId(id
);
1620 // for compatibility only
1621 #if WXWIN_COMPATIBILITY_2_4
1623 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1624 const wxString
& text
,
1625 int image
, int selImage
,
1628 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1629 image
, selImage
, NULL
);
1632 #endif // WXWIN_COMPATIBILITY_2_4
1634 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1635 int image
, int selectedImage
,
1636 wxTreeItemData
*data
)
1639 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1641 // create a virtual root item, the parent for all the others
1642 m_pVirtualRoot
= new wxVirtualNode(data
);
1647 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1648 text
, image
, selectedImage
, data
);
1651 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1652 const wxString
& text
,
1653 int image
, int selectedImage
,
1654 wxTreeItemData
*data
)
1656 return DoInsertItem(parent
, TVI_FIRST
,
1657 text
, image
, selectedImage
, data
);
1660 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1661 const wxTreeItemId
& idPrevious
,
1662 const wxString
& text
,
1663 int image
, int selectedImage
,
1664 wxTreeItemData
*data
)
1666 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1669 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1671 const wxString
& text
,
1672 int image
, int selectedImage
,
1673 wxTreeItemData
*data
)
1675 // find the item from index
1676 wxTreeItemIdValue cookie
;
1677 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1678 while ( index
!= 0 && idCur
.IsOk() )
1683 idCur
= GetNextChild(parent
, cookie
);
1686 // assert, not check: if the index is invalid, we will append the item
1688 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1690 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1693 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1694 const wxString
& text
,
1695 int image
, int selectedImage
,
1696 wxTreeItemData
*data
)
1698 return DoInsertItem(parent
, TVI_LAST
,
1699 text
, image
, selectedImage
, data
);
1702 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1704 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1706 wxLogLastError(wxT("TreeView_DeleteItem"));
1710 // delete all children (but don't delete the item itself)
1711 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1713 wxTreeItemIdValue cookie
;
1715 wxArrayTreeItemIds children
;
1716 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1717 while ( child
.IsOk() )
1719 children
.Add(child
);
1721 child
= GetNextChild(item
, cookie
);
1724 size_t nCount
= children
.Count();
1725 for ( size_t n
= 0; n
< nCount
; n
++ )
1727 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1729 wxLogLastError(wxT("TreeView_DeleteItem"));
1734 void wxTreeCtrl::DeleteAllItems()
1736 // delete the "virtual" root item.
1737 if ( GET_VIRTUAL_ROOT() )
1739 delete GET_VIRTUAL_ROOT();
1740 m_pVirtualRoot
= NULL
;
1743 // and all the real items
1745 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1747 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1751 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1753 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1754 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1755 flag
== TVE_EXPAND
||
1757 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1759 // A hidden root can be neither expanded nor collapsed.
1760 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1761 wxT("Can't expand/collapse hidden root node!") )
1763 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1764 // emulate them. This behaviour has changed slightly with comctl32.dll
1765 // v 4.70 - now it does send them but only the first time. To maintain
1766 // compatible behaviour and also in order to not have surprises with the
1767 // future versions, don't rely on this and still do everything ourselves.
1768 // To avoid that the messages be sent twice when the item is expanded for
1769 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1771 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1775 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1777 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1778 event
.m_item
= item
;
1779 event
.SetEventObject(this);
1781 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1783 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1787 (void)GetEventHandler()->ProcessEvent(event
);
1789 //else: change didn't took place, so do nothing at all
1792 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1794 DoExpand(item
, TVE_EXPAND
);
1797 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1799 DoExpand(item
, TVE_COLLAPSE
);
1802 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1804 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1807 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1809 DoExpand(item
, TVE_TOGGLE
);
1812 #if WXWIN_COMPATIBILITY_2_4
1813 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1815 DoExpand(item
, action
);
1819 void wxTreeCtrl::Unselect()
1821 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1822 wxT("doesn't make sense, may be you want UnselectAll()?") );
1824 // just remove the selection
1825 SelectItem(wxTreeItemId());
1828 void wxTreeCtrl::UnselectAll()
1830 if ( m_windowStyle
& wxTR_MULTIPLE
)
1832 wxArrayTreeItemIds selections
;
1833 size_t count
= GetSelections(selections
);
1834 for ( size_t n
= 0; n
< count
; n
++ )
1836 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1837 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1838 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1839 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1840 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1845 // just remove the selection
1850 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1852 if ( m_windowStyle
& wxTR_MULTIPLE
)
1854 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1855 // selecting the item means checking it
1857 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1858 ::SelectItem(GetHwnd(), HITEM(item
));
1859 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1863 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1864 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1865 // send them ourselves
1867 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1868 event
.m_item
= item
;
1869 event
.SetEventObject(this);
1871 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1872 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1874 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1876 wxLogLastError(wxT("TreeView_SelectItem"));
1880 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1881 (void)GetEventHandler()->ProcessEvent(event
);
1884 //else: program vetoed the change
1888 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1891 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1894 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1896 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1898 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1902 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1907 void wxTreeCtrl::DeleteTextCtrl()
1911 // the HWND corresponding to this control is deleted by the tree
1912 // control itself and we don't know when exactly this happens, so check
1913 // if the window still exists before calling UnsubclassWin()
1914 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1916 m_textCtrl
->SetHWND(0);
1919 m_textCtrl
->UnsubclassWin();
1920 m_textCtrl
->SetHWND(0);
1926 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1927 wxClassInfo
* textControlClass
)
1929 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1933 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1934 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1936 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1945 // textctrl is subclassed in MSWOnNotify
1949 // End label editing, optionally cancelling the edit
1950 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1952 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1957 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1959 TV_HITTESTINFO hitTestInfo
;
1960 hitTestInfo
.pt
.x
= (int)point
.x
;
1961 hitTestInfo
.pt
.y
= (int)point
.y
;
1963 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1968 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1969 flags |= wxTREE_HITTEST_##flag
1971 TRANSLATE_FLAG(ABOVE
);
1972 TRANSLATE_FLAG(BELOW
);
1973 TRANSLATE_FLAG(NOWHERE
);
1974 TRANSLATE_FLAG(ONITEMBUTTON
);
1975 TRANSLATE_FLAG(ONITEMICON
);
1976 TRANSLATE_FLAG(ONITEMINDENT
);
1977 TRANSLATE_FLAG(ONITEMLABEL
);
1978 TRANSLATE_FLAG(ONITEMRIGHT
);
1979 TRANSLATE_FLAG(ONITEMSTATEICON
);
1980 TRANSLATE_FLAG(TOLEFT
);
1981 TRANSLATE_FLAG(TORIGHT
);
1983 #undef TRANSLATE_FLAG
1985 return wxTreeItemId(hitTestInfo
.hItem
);
1988 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
1990 bool textOnly
) const
1994 // Virtual root items have no bounding rectangle
1995 if ( IS_VIRTUAL_ROOT(item
) )
2000 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2003 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2009 // couldn't retrieve rect: for example, item isn't visible
2014 // ----------------------------------------------------------------------------
2016 // ----------------------------------------------------------------------------
2018 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2019 // functions such as IsDataIndirect()
2020 class wxTreeSortHelper
2023 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2026 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
2028 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
2029 if ( tree
->IsDataIndirect(data
) )
2031 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2034 return data
->GetId();
2038 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2042 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2043 wxT("sorting tree without data doesn't make sense") );
2045 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2047 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2048 GetIdFromData(tree
, pItem2
));
2051 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2052 const wxTreeItemId
& item2
)
2054 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2057 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2059 // rely on the fact that TreeView_SortChildren does the same thing as our
2060 // default behaviour, i.e. sorts items alphabetically and so call it
2061 // directly if we're not in derived class (much more efficient!)
2062 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2064 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2069 tvSort
.hParent
= HITEM(item
);
2070 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2071 tvSort
.lParam
= (LPARAM
)this;
2072 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2076 // ----------------------------------------------------------------------------
2078 // ----------------------------------------------------------------------------
2080 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2082 if ( cmd
== EN_UPDATE
)
2084 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2085 event
.SetEventObject( this );
2086 ProcessCommand(event
);
2088 else if ( cmd
== EN_KILLFOCUS
)
2090 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2091 event
.SetEventObject( this );
2092 ProcessCommand(event
);
2100 // command processed
2104 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2105 // only do it during dragging, minimize wxWin overhead (this is important for
2106 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2107 // instead of passing by wxWin events
2108 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2110 bool processed
= false;
2112 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2114 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2116 // we only process mouse messages here and these parameters have the
2117 // same meaning for all of them
2118 int x
= GET_X_LPARAM(lParam
),
2119 y
= GET_Y_LPARAM(lParam
);
2120 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2124 case WM_RBUTTONDOWN
:
2125 // if the item we are about to right click on
2126 // is not already select, remove the entire
2127 // previous selection
2128 if (!::IsItemSelected(GetHwnd(), htItem
))
2133 // select item and set the focus to the
2134 // newly selected item
2135 ::SelectItem(GetHwnd(), htItem
);
2136 ::SetFocus(GetHwnd(), htItem
);
2139 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2140 case WM_LBUTTONDOWN
:
2141 if ( htItem
&& isMultiple
)
2143 if ( wParam
& MK_CONTROL
)
2147 // toggle selected state
2148 ToggleItemSelection(GetHwnd(), htItem
);
2150 ::SetFocus(GetHwnd(), htItem
);
2152 // reset on any click without Shift
2153 m_htSelStart
.Unset();
2157 else if ( wParam
& MK_SHIFT
)
2159 // this selects all items between the starting one and
2162 if ( !m_htSelStart
)
2164 // take the focused item
2165 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2168 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2169 !(wParam
& MK_CONTROL
));
2171 ::SetFocus(GetHwnd(), htItem
);
2175 else // normal click
2177 // avoid doing anything if we click on the only
2178 // currently selected item
2180 wxArrayTreeItemIds selections
;
2181 size_t count
= GetSelections(selections
);
2184 HITEM_PTR(selections
[0]) != htItem
)
2186 // clear the previously selected items, if the
2187 // user clicked outside of the present selection.
2188 // otherwise, perform the deselection on mouse-up.
2189 // this allows multiple drag and drop to work.
2191 if (IsItemSelected(GetHwnd(), htItem
))
2193 ::SetFocus(GetHwnd(), htItem
);
2199 // prevent the click from starting in-place editing
2200 // which should only happen if we click on the
2201 // already selected item (and nothing else is
2204 TreeView_SelectItem(GetHwnd(), 0);
2205 ::SelectItem(GetHwnd(), htItem
);
2209 // reset on any click without Shift
2210 m_htSelStart
.Unset();
2214 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2219 m_dragImage
->Move(wxPoint(x
, y
));
2222 // highlight the item as target (hiding drag image is
2223 // necessary - otherwise the display will be corrupted)
2224 m_dragImage
->Hide();
2225 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2226 m_dragImage
->Show();
2233 // facilitates multiple drag-and-drop
2234 if (htItem
&& isMultiple
)
2236 wxArrayTreeItemIds selections
;
2237 size_t count
= GetSelections(selections
);
2240 !(wParam
& MK_CONTROL
) &&
2241 !(wParam
& MK_SHIFT
))
2244 TreeView_SelectItem(GetHwnd(), htItem
);
2253 m_dragImage
->EndDrag();
2257 // generate the drag end event
2258 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2260 event
.m_item
= htItem
;
2261 event
.m_pointDrag
= wxPoint(x
, y
);
2262 event
.SetEventObject(this);
2264 (void)GetEventHandler()->ProcessEvent(event
);
2266 // if we don't do it, the tree seems to think that 2 items
2267 // are selected simultaneously which is quite weird
2268 TreeView_SelectDropTarget(GetHwnd(), 0);
2273 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2274 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2276 // the tree control greys out the selected item when it loses focus and
2277 // paints it as selected again when it regains it, but it won't do it
2278 // for the other items itself - help it
2279 wxArrayTreeItemIds selections
;
2280 size_t count
= GetSelections(selections
);
2282 for ( size_t n
= 0; n
< count
; n
++ )
2284 // TreeView_GetItemRect() will return false if item is not visible,
2285 // which may happen perfectly well
2286 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2289 ::InvalidateRect(GetHwnd(), &rect
, false);
2293 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2295 bool bCtrl
= wxIsCtrlDown(),
2296 bShift
= wxIsShiftDown();
2298 // we handle.arrows and space, but not page up/down and home/end: the
2299 // latter should be easy, but not the former
2301 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2302 if ( !m_htSelStart
)
2304 m_htSelStart
= htSel
;
2307 if ( wParam
== VK_SPACE
)
2311 ToggleItemSelection(GetHwnd(), htSel
);
2317 ::SelectItem(GetHwnd(), htSel
);
2322 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2324 if ( !bCtrl
&& !bShift
)
2326 // no modifiers, just clear selection and then let the default
2327 // processing to take place
2332 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2334 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2335 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2336 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2340 // at the top/bottom
2346 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2350 // without changing selection
2351 ::SetFocus(GetHwnd(), htNext
);
2358 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2359 else if ( nMsg
== WM_CHAR
)
2361 // don't let the control process Space and Return keys because it
2362 // doesn't do anything useful with them anyhow but always beeps
2363 // annoyingly when it receives them and there is no way to turn it off
2364 // simply if you just process TREEITEM_ACTIVATED event to which Space
2365 // and Enter presses are mapped in your code
2366 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2373 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2378 // process WM_NOTIFY Windows message
2379 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2381 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2382 wxEventType eventType
= wxEVT_NULL
;
2383 NMHDR
*hdr
= (NMHDR
*)lParam
;
2385 switch ( hdr
->code
)
2388 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2391 case TVN_BEGINRDRAG
:
2393 if ( eventType
== wxEVT_NULL
)
2394 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2395 //else: left drag, already set above
2397 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2399 event
.m_item
= tv
->itemNew
.hItem
;
2400 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2402 // don't allow dragging by default: the user code must
2403 // explicitly say that it wants to allow it to avoid breaking
2409 case TVN_BEGINLABELEDIT
:
2411 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2412 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2414 event
.m_item
= info
->item
.hItem
;
2415 event
.m_label
= info
->item
.pszText
;
2416 event
.m_editCancelled
= false;
2420 case TVN_DELETEITEM
:
2422 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2423 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2425 event
.m_item
= tv
->itemOld
.hItem
;
2429 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2430 if ( it
!= m_attrs
.end() )
2439 case TVN_ENDLABELEDIT
:
2441 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2442 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2444 event
.m_item
= info
->item
.hItem
;
2445 event
.m_label
= info
->item
.pszText
;
2446 if (info
->item
.pszText
== NULL
)
2448 event
.m_editCancelled
= true;
2452 event
.m_editCancelled
= false;
2457 case TVN_GETDISPINFO
:
2458 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2461 case TVN_SETDISPINFO
:
2463 if ( eventType
== wxEVT_NULL
)
2464 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2465 //else: get, already set above
2467 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2469 event
.m_item
= info
->item
.hItem
;
2473 case TVN_ITEMEXPANDING
:
2474 case TVN_ITEMEXPANDED
:
2476 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2479 switch ( tv
->action
)
2482 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2490 what
= IDX_COLLAPSE
;
2494 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2497 eventType
= gs_expandEvents
[what
][how
];
2499 event
.m_item
= tv
->itemNew
.hItem
;
2505 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2506 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2508 // fabricate the lParam and wParam parameters sufficiently
2509 // similar to the ones from a "real" WM_KEYDOWN so that
2510 // CreateKeyEvent() works correctly
2512 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2514 WXWPARAM wParam
= info
->wVKey
;
2516 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2519 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2524 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2529 // a separate event for Space/Return
2530 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2531 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2533 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2535 event2
.SetEventObject(this);
2536 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2538 event2
.m_item
= GetSelection();
2540 //else: don't know how to get it
2542 (void)GetEventHandler()->ProcessEvent(event2
);
2547 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2548 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2549 // we have to handle both messages:
2550 case TVN_SELCHANGEDA
:
2551 case TVN_SELCHANGEDW
:
2552 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2555 case TVN_SELCHANGINGA
:
2556 case TVN_SELCHANGINGW
:
2558 if ( eventType
== wxEVT_NULL
)
2559 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2560 //else: already set above
2562 if (hdr
->code
== TVN_SELCHANGINGW
||
2563 hdr
->code
== TVN_SELCHANGEDW
)
2565 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2566 event
.m_item
= tv
->itemNew
.hItem
;
2567 event
.m_itemOld
= tv
->itemOld
.hItem
;
2571 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2572 event
.m_item
= tv
->itemNew
.hItem
;
2573 event
.m_itemOld
= tv
->itemOld
.hItem
;
2578 // instead of explicitly checking for _WIN32_IE, check if the
2579 // required symbols are available in the headers
2580 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2583 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2584 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2585 switch ( nmcd
.dwDrawStage
)
2588 // if we've got any items with non standard attributes,
2589 // notify us before painting each item
2590 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2594 case CDDS_ITEMPREPAINT
:
2596 wxMapTreeAttr::iterator
2597 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2599 if ( it
== m_attrs
.end() )
2601 // nothing to do for this item
2602 *result
= CDRF_DODEFAULT
;
2606 wxTreeItemAttr
* const attr
= it
->second
;
2609 if ( attr
->HasFont() )
2611 hFont
= GetHfontOf(attr
->GetFont());
2619 if ( attr
->HasTextColour() )
2621 colText
= attr
->GetTextColour();
2625 colText
= GetForegroundColour();
2628 // selection colours should override ours
2629 if ( nmcd
.uItemState
& CDIS_SELECTED
)
2632 ::GetSysColor(COLOR_HIGHLIGHT
);
2634 ::GetSysColor(COLOR_HIGHLIGHTTEXT
);
2639 if ( attr
->HasBackgroundColour() )
2641 colBack
= attr
->GetBackgroundColour();
2645 colBack
= GetBackgroundColour();
2648 lptvcd
->clrText
= wxColourToRGB(colText
);
2649 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2652 // note that if we wanted to set colours for
2653 // individual columns (subitems), we would have
2654 // returned CDRF_NOTIFYSUBITEMREDRAW from here
2657 ::SelectObject(nmcd
.hdc
, hFont
);
2659 *result
= CDRF_NEWFONT
;
2663 *result
= CDRF_DODEFAULT
;
2669 *result
= CDRF_DODEFAULT
;
2673 // we always process it
2675 #endif // have owner drawn support in headers
2679 DWORD pos
= GetMessagePos();
2681 point
.x
= LOWORD(pos
);
2682 point
.y
= HIWORD(pos
);
2683 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2685 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2686 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2688 event
.m_item
= item
;
2689 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2697 TV_HITTESTINFO tvhti
;
2698 ::GetCursorPos(&tvhti
.pt
);
2699 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2700 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2702 if ( tvhti
.flags
& TVHT_ONITEM
)
2704 event
.m_item
= tvhti
.hItem
;
2705 eventType
= (int)hdr
->code
== NM_DBLCLK
2706 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2707 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2709 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2710 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2719 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2722 event
.SetEventObject(this);
2723 event
.SetEventType(eventType
);
2725 bool processed
= GetEventHandler()->ProcessEvent(event
);
2728 switch ( hdr
->code
)
2731 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2732 // the return code of this event handler as the return value for
2733 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2734 // expanded status would never work
2739 case TVN_BEGINRDRAG
:
2740 if ( event
.IsAllowed() )
2742 // normally this is impossible because the m_dragImage is
2743 // deleted once the drag operation is over
2744 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2746 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2747 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2748 m_dragImage
->Show();
2752 case TVN_DELETEITEM
:
2754 // NB: we might process this message using wxWindows event
2755 // tables, but due to overhead of wxWin event system we
2756 // prefer to do it here ourself (otherwise deleting a tree
2757 // with many items is just too slow)
2758 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2760 wxTreeItemId item
= event
.m_item
;
2761 if ( HasIndirectData(item
) )
2763 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2765 delete data
; // can't be NULL here
2769 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2770 delete data
; // may be NULL, ok
2773 processed
= true; // Make sure we don't get called twice
2777 case TVN_BEGINLABELEDIT
:
2778 // return true to cancel label editing
2779 *result
= !event
.IsAllowed();
2780 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2781 if(event
.IsAllowed())
2783 HWND hText
= TreeView_GetEditControl(GetHwnd());
2786 // MBN: if m_textCtrl already has an HWND, it is a stale
2787 // pointer from a previous edit (because the user
2788 // didn't modify the label before dismissing the control,
2789 // and TVN_ENDLABELEDIT was not sent), so delete it
2790 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2793 m_textCtrl
= new wxTextCtrl();
2794 m_textCtrl
->SetParent(this);
2795 m_textCtrl
->SetHWND((WXHWND
)hText
);
2796 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2798 // set wxTE_PROCESS_ENTER style for the text control to
2799 // force it to process the Enter presses itself, otherwise
2800 // they could be stolen from it by the dialog
2802 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2803 | wxTE_PROCESS_ENTER
);
2808 case TVN_ENDLABELEDIT
:
2809 // return true to set the label to the new string: note that we
2810 // also must pretend that we did process the message or it is going
2811 // to be passed to DefWindowProc() which will happily return false
2812 // cancelling the label change
2813 *result
= event
.IsAllowed();
2816 // ensure that we don't have the text ctrl which is going to be
2821 case TVN_SELCHANGING
:
2822 case TVN_ITEMEXPANDING
:
2823 // return true to prevent the action from happening
2824 *result
= !event
.IsAllowed();
2827 case TVN_ITEMEXPANDED
:
2828 // the item is not refreshed properly after expansion when it has
2829 // an image depending on the expanded/collapsed state - bug in
2830 // comctl32.dll or our code?
2832 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2833 wxTreeItemId
id(tv
->itemNew
.hItem
);
2835 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2843 case TVN_GETDISPINFO
:
2844 // NB: so far the user can't set the image himself anyhow, so do it
2845 // anyway - but this may change later
2846 //if ( /* !processed && */ 1 )
2848 wxTreeItemId item
= event
.m_item
;
2849 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2850 if ( info
->item
.mask
& TVIF_IMAGE
)
2853 DoGetItemImageFromData
2856 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2857 : wxTreeItemIcon_Normal
2860 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2862 info
->item
.iSelectedImage
=
2863 DoGetItemImageFromData
2866 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2867 : wxTreeItemIcon_Selected
2874 // for the other messages the return value is ignored and there is
2875 // nothing special to do
2880 // ----------------------------------------------------------------------------
2882 // ----------------------------------------------------------------------------
2884 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2885 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2887 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2890 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2891 tvi
.mask
= TVIF_STATE
;
2892 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2894 // Select the specified state, or -1 == cycle to the next one.
2897 TreeView_GetItem(GetHwnd(), &tvi
);
2899 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2900 if ( state
== m_imageListState
->GetImageCount() )
2904 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2905 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2907 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2909 TreeView_SetItem(GetHwnd(), &tvi
);
2912 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2915 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2916 tvi
.mask
= TVIF_STATE
;
2917 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2918 TreeView_GetItem(GetHwnd(), &tvi
);
2920 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2923 #endif // wxUSE_TREECTRL