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 wxBEGIN_FLAGS( wxTreeCtrlStyle
)
477 // new style border flags, we put them first to
478 // use them for streaming out
479 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
480 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
481 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
482 wxFLAGS_MEMBER(wxBORDER_RAISED
)
483 wxFLAGS_MEMBER(wxBORDER_STATIC
)
484 wxFLAGS_MEMBER(wxBORDER_NONE
)
486 // old style border flags
487 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
488 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
489 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
490 wxFLAGS_MEMBER(wxRAISED_BORDER
)
491 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
492 wxFLAGS_MEMBER(wxBORDER
)
494 // standard window styles
495 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
496 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
497 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
498 wxFLAGS_MEMBER(wxWANTS_CHARS
)
499 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
500 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
501 wxFLAGS_MEMBER(wxVSCROLL
)
502 wxFLAGS_MEMBER(wxHSCROLL
)
504 wxFLAGS_MEMBER(wxTR_EDIT_LABELS
)
505 wxFLAGS_MEMBER(wxTR_NO_BUTTONS
)
506 wxFLAGS_MEMBER(wxTR_HAS_BUTTONS
)
507 wxFLAGS_MEMBER(wxTR_TWIST_BUTTONS
)
508 wxFLAGS_MEMBER(wxTR_NO_LINES
)
509 wxFLAGS_MEMBER(wxTR_FULL_ROW_HIGHLIGHT
)
510 wxFLAGS_MEMBER(wxTR_LINES_AT_ROOT
)
511 wxFLAGS_MEMBER(wxTR_HIDE_ROOT
)
512 wxFLAGS_MEMBER(wxTR_ROW_LINES
)
513 wxFLAGS_MEMBER(wxTR_HAS_VARIABLE_ROW_HEIGHT
)
514 wxFLAGS_MEMBER(wxTR_SINGLE
)
515 wxFLAGS_MEMBER(wxTR_MULTIPLE
)
516 wxFLAGS_MEMBER(wxTR_EXTENDED
)
517 wxFLAGS_MEMBER(wxTR_DEFAULT_STYLE
)
519 wxEND_FLAGS( wxTreeCtrlStyle
)
521 IMPLEMENT_DYNAMIC_CLASS_XTI(wxTreeCtrl
, wxControl
,"wx/treectrl.h")
523 wxBEGIN_PROPERTIES_TABLE(wxTreeCtrl
)
524 wxEVENT_PROPERTY( TextUpdated
, wxEVT_COMMAND_TEXT_UPDATED
, wxCommandEvent
)
525 wxEVENT_RANGE_PROPERTY( TreeEvent
, wxEVT_COMMAND_TREE_BEGIN_DRAG
, wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
, wxTreeEvent
)
526 wxPROPERTY_FLAGS( WindowStyle
, wxTreeCtrlStyle
, long , SetWindowStyleFlag
, GetWindowStyleFlag
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group")) // style
527 wxEND_PROPERTIES_TABLE()
529 wxBEGIN_HANDLERS_TABLE(wxTreeCtrl
)
530 wxEND_HANDLERS_TABLE()
532 wxCONSTRUCTOR_5( wxTreeCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
)
534 IMPLEMENT_DYNAMIC_CLASS(wxTreeCtrl
, wxControl
)
537 // ----------------------------------------------------------------------------
539 // ----------------------------------------------------------------------------
541 // indices in gs_expandEvents table below
556 // handy table for sending events - it has to be initialized during run-time
557 // now so can't be const any more
558 static /* const */ wxEventType gs_expandEvents
[IDX_WHAT_MAX
][IDX_HOW_MAX
];
561 but logically it's a const table with the following entries:
564 { wxEVT_COMMAND_TREE_ITEM_COLLAPSED, wxEVT_COMMAND_TREE_ITEM_COLLAPSING },
565 { wxEVT_COMMAND_TREE_ITEM_EXPANDED, wxEVT_COMMAND_TREE_ITEM_EXPANDING }
569 // ============================================================================
571 // ============================================================================
573 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
577 bool wxTreeTraversal::DoTraverse(const wxTreeItemId
& root
, bool recursively
)
579 if ( !OnVisit(root
) )
582 return Traverse(root
, recursively
);
585 bool wxTreeTraversal::Traverse(const wxTreeItemId
& root
, bool recursively
)
587 wxTreeItemIdValue cookie
;
588 wxTreeItemId child
= m_tree
->GetFirstChild(root
, cookie
);
589 while ( child
.IsOk() )
591 // depth first traversal
592 if ( recursively
&& !Traverse(child
, true) )
595 if ( !OnVisit(child
) )
598 child
= m_tree
->GetNextChild(root
, cookie
);
604 // ----------------------------------------------------------------------------
605 // construction and destruction
606 // ----------------------------------------------------------------------------
608 void wxTreeCtrl::Init()
610 m_imageListNormal
= NULL
;
611 m_imageListState
= NULL
;
612 m_ownsImageListNormal
= m_ownsImageListState
= false;
614 m_hasAnyAttr
= false;
616 m_pVirtualRoot
= NULL
;
618 // initialize the global array of events now as it can't be done statically
619 // with the wxEVT_XXX values being allocated during run-time only
620 gs_expandEvents
[IDX_COLLAPSE
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSED
;
621 gs_expandEvents
[IDX_COLLAPSE
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_COLLAPSING
;
622 gs_expandEvents
[IDX_EXPAND
][IDX_DONE
] = wxEVT_COMMAND_TREE_ITEM_EXPANDED
;
623 gs_expandEvents
[IDX_EXPAND
][IDX_DOING
] = wxEVT_COMMAND_TREE_ITEM_EXPANDING
;
626 bool wxTreeCtrl::Create(wxWindow
*parent
,
631 const wxValidator
& validator
,
632 const wxString
& name
)
636 if ( (style
& wxBORDER_MASK
) == wxBORDER_DEFAULT
)
637 style
|= wxBORDER_SUNKEN
;
639 if ( !CreateControl(parent
, id
, pos
, size
, style
, validator
, name
) )
643 DWORD wstyle
= MSWGetStyle(m_windowStyle
, & exStyle
);
644 wstyle
|= WS_TABSTOP
| TVS_SHOWSELALWAYS
;
646 if ((m_windowStyle
& wxTR_NO_LINES
) == 0)
647 wstyle
|= TVS_HASLINES
;
648 if ( m_windowStyle
& wxTR_HAS_BUTTONS
)
649 wstyle
|= TVS_HASBUTTONS
;
651 if ( m_windowStyle
& wxTR_EDIT_LABELS
)
652 wstyle
|= TVS_EDITLABELS
;
654 if ( m_windowStyle
& wxTR_LINES_AT_ROOT
)
655 wstyle
|= TVS_LINESATROOT
;
657 if ( m_windowStyle
& wxTR_FULL_ROW_HIGHLIGHT
)
659 if ( wxTheApp
->GetComCtl32Version() >= 471 )
660 wstyle
|= TVS_FULLROWSELECT
;
663 // using TVS_CHECKBOXES for emulation of a multiselection tree control
664 // doesn't work without the new enough headers
665 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE && \
666 !defined( __GNUWIN32_OLD__ ) && \
667 !defined( __BORLANDC__ ) && \
668 !defined( __WATCOMC__ ) && \
669 (!defined(__VISUALC__) || (__VISUALC__ > 1010))
671 // we emulate the multiple selection tree controls by using checkboxes: set
672 // up the image list we need for this if we do have multiple selections
673 if ( m_windowStyle
& wxTR_MULTIPLE
)
674 wstyle
|= TVS_CHECKBOXES
;
675 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
677 // Need so that TVN_GETINFOTIP messages will be sent
678 wstyle
|= TVS_INFOTIP
;
680 // Create the tree control.
681 if ( !MSWCreateControl(WC_TREEVIEW
, wstyle
) )
684 #if wxUSE_COMCTL32_SAFELY
685 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
686 wxWindow::SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
688 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
689 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
691 // This works around a bug in the Windows tree control whereby for some versions
692 // of comctrl32, setting any colour actually draws the background in black.
693 // This will initialise the background to the system colour.
694 // THIS FIX NOW REVERTED since it caused problems on _other_ systems.
695 // Assume the user has an updated comctl32.dll.
696 ::SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0,-1);
697 wxWindow::SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOW
));
698 SetForegroundColour(wxWindow::GetParent()->GetForegroundColour());
702 // VZ: this is some experimental code which may be used to get the
703 // TVS_CHECKBOXES style functionality for comctl32.dll < 4.71.
704 // AFAIK, the standard DLL does about the same thing anyhow.
706 if ( m_windowStyle
& wxTR_MULTIPLE
)
710 // create the DC compatible with the current screen
711 HDC hdcMem
= CreateCompatibleDC(NULL
);
713 // create a mono bitmap of the standard size
714 int x
= GetSystemMetrics(SM_CXMENUCHECK
);
715 int y
= GetSystemMetrics(SM_CYMENUCHECK
);
716 wxImageList
imagelistCheckboxes(x
, y
, false, 2);
717 HBITMAP hbmpCheck
= CreateBitmap(x
, y
, // bitmap size
718 1, // # of color planes
719 1, // # bits needed for one pixel
720 0); // array containing colour data
721 SelectObject(hdcMem
, hbmpCheck
);
723 // then draw a check mark into it
724 RECT rect
= { 0, 0, x
, y
};
725 if ( !::DrawFrameControl(hdcMem
, &rect
,
727 DFCS_BUTTONCHECK
| DFCS_CHECKED
) )
729 wxLogLastError(wxT("DrawFrameControl(check)"));
732 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
733 imagelistCheckboxes
.Add(bmp
);
735 if ( !::DrawFrameControl(hdcMem
, &rect
,
739 wxLogLastError(wxT("DrawFrameControl(uncheck)"));
742 bmp
.SetHBITMAP((WXHBITMAP
)hbmpCheck
);
743 imagelistCheckboxes
.Add(bmp
);
749 SetStateImageList(&imagelistCheckboxes
);
753 SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
);
758 wxTreeCtrl::~wxTreeCtrl()
760 // delete any attributes
763 WX_CLEAR_HASH_MAP(wxMapTreeAttr
, m_attrs
);
765 // prevent TVN_DELETEITEM handler from deleting the attributes again!
766 m_hasAnyAttr
= false;
771 // delete user data to prevent memory leaks
772 // also deletes hidden root node storage.
775 if (m_ownsImageListNormal
) delete m_imageListNormal
;
776 if (m_ownsImageListState
) delete m_imageListState
;
779 // ----------------------------------------------------------------------------
781 // ----------------------------------------------------------------------------
783 // simple wrappers which add error checking in debug mode
785 bool wxTreeCtrl::DoGetItem(wxTreeViewItem
* tvItem
) const
787 wxCHECK_MSG( tvItem
->hItem
!= TVI_ROOT
, false,
788 _T("can't retrieve virtual root item") );
790 if ( !TreeView_GetItem(GetHwnd(), tvItem
) )
792 wxLogLastError(wxT("TreeView_GetItem"));
800 void wxTreeCtrl::DoSetItem(wxTreeViewItem
* tvItem
)
802 if ( TreeView_SetItem(GetHwnd(), tvItem
) == -1 )
804 wxLogLastError(wxT("TreeView_SetItem"));
808 size_t wxTreeCtrl::GetCount() const
810 return (size_t)TreeView_GetCount(GetHwnd());
813 unsigned int wxTreeCtrl::GetIndent() const
815 return TreeView_GetIndent(GetHwnd());
818 void wxTreeCtrl::SetIndent(unsigned int indent
)
820 TreeView_SetIndent(GetHwnd(), indent
);
823 wxImageList
*wxTreeCtrl::GetImageList() const
825 return m_imageListNormal
;
828 wxImageList
*wxTreeCtrl::GetStateImageList() const
830 return m_imageListState
;
833 void wxTreeCtrl::SetAnyImageList(wxImageList
*imageList
, int which
)
836 TreeView_SetImageList(GetHwnd(),
837 imageList
? imageList
->GetHIMAGELIST() : 0,
841 void wxTreeCtrl::SetImageList(wxImageList
*imageList
)
843 if (m_ownsImageListNormal
)
844 delete m_imageListNormal
;
846 SetAnyImageList(m_imageListNormal
= imageList
, TVSIL_NORMAL
);
847 m_ownsImageListNormal
= false;
850 void wxTreeCtrl::SetStateImageList(wxImageList
*imageList
)
852 if (m_ownsImageListState
) delete m_imageListState
;
853 SetAnyImageList(m_imageListState
= imageList
, TVSIL_STATE
);
854 m_ownsImageListState
= false;
857 void wxTreeCtrl::AssignImageList(wxImageList
*imageList
)
859 SetImageList(imageList
);
860 m_ownsImageListNormal
= true;
863 void wxTreeCtrl::AssignStateImageList(wxImageList
*imageList
)
865 SetStateImageList(imageList
);
866 m_ownsImageListState
= true;
869 size_t wxTreeCtrl::GetChildrenCount(const wxTreeItemId
& item
,
870 bool recursively
) const
872 TraverseCounter
counter(this, item
, recursively
);
874 return counter
.GetCount() - 1;
877 // ----------------------------------------------------------------------------
879 // ----------------------------------------------------------------------------
881 bool wxTreeCtrl::SetBackgroundColour(const wxColour
&colour
)
883 #if !wxUSE_COMCTL32_SAFELY
884 if ( !wxWindowBase::SetBackgroundColour(colour
) )
887 SendMessage(GetHwnd(), TVM_SETBKCOLOR
, 0, colour
.GetPixel());
893 bool wxTreeCtrl::SetForegroundColour(const wxColour
&colour
)
895 #if !wxUSE_COMCTL32_SAFELY
896 if ( !wxWindowBase::SetForegroundColour(colour
) )
899 SendMessage(GetHwnd(), TVM_SETTEXTCOLOR
, 0, colour
.GetPixel());
905 // ----------------------------------------------------------------------------
907 // ----------------------------------------------------------------------------
909 wxString
wxTreeCtrl::GetItemText(const wxTreeItemId
& item
) const
911 wxChar buf
[512]; // the size is arbitrary...
913 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
914 tvItem
.pszText
= buf
;
915 tvItem
.cchTextMax
= WXSIZEOF(buf
);
916 if ( !DoGetItem(&tvItem
) )
918 // don't return some garbage which was on stack, but an empty string
922 return wxString(buf
);
925 void wxTreeCtrl::SetItemText(const wxTreeItemId
& item
, const wxString
& text
)
927 if ( IS_VIRTUAL_ROOT(item
) )
930 wxTreeViewItem
tvItem(item
, TVIF_TEXT
);
931 tvItem
.pszText
= (wxChar
*)text
.c_str(); // conversion is ok
934 // when setting the text of the item being edited, the text control should
935 // be updated to reflect the new text as well, otherwise calling
936 // SetItemText() in the OnBeginLabelEdit() handler doesn't have any effect
938 // don't use GetEditControl() here because m_textCtrl is not set yet
939 HWND hwndEdit
= TreeView_GetEditControl(GetHwnd());
942 if ( item
== GetSelection() )
944 ::SetWindowText(hwndEdit
, text
);
949 int wxTreeCtrl::DoGetItemImageFromData(const wxTreeItemId
& item
,
950 wxTreeItemIcon which
) const
952 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
953 if ( !DoGetItem(&tvItem
) )
958 return ((wxTreeItemIndirectData
*)tvItem
.lParam
)->GetImage(which
);
961 void wxTreeCtrl::DoSetItemImageFromData(const wxTreeItemId
& item
,
963 wxTreeItemIcon which
) const
965 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
966 if ( !DoGetItem(&tvItem
) )
971 wxTreeItemIndirectData
*data
= ((wxTreeItemIndirectData
*)tvItem
.lParam
);
973 data
->SetImage(image
, which
);
975 // make sure that we have selected images as well
976 if ( which
== wxTreeItemIcon_Normal
&&
977 !data
->HasImage(wxTreeItemIcon_Selected
) )
979 data
->SetImage(image
, wxTreeItemIcon_Selected
);
982 if ( which
== wxTreeItemIcon_Expanded
&&
983 !data
->HasImage(wxTreeItemIcon_SelectedExpanded
) )
985 data
->SetImage(image
, wxTreeItemIcon_SelectedExpanded
);
989 void wxTreeCtrl::DoSetItemImages(const wxTreeItemId
& item
,
993 wxTreeViewItem
tvItem(item
, TVIF_IMAGE
| TVIF_SELECTEDIMAGE
);
994 tvItem
.iSelectedImage
= imageSel
;
995 tvItem
.iImage
= image
;
999 int wxTreeCtrl::GetItemImage(const wxTreeItemId
& item
,
1000 wxTreeItemIcon which
) const
1002 if ( (HITEM(item
) == TVI_ROOT
) && (m_windowStyle
& wxTR_HIDE_ROOT
) )
1004 // TODO: Maybe a hidden root can still provide images?
1008 if ( HasIndirectData(item
) )
1010 return DoGetItemImageFromData(item
, which
);
1017 wxFAIL_MSG( wxT("unknown tree item image type") );
1019 case wxTreeItemIcon_Normal
:
1023 case wxTreeItemIcon_Selected
:
1024 mask
= TVIF_SELECTEDIMAGE
;
1027 case wxTreeItemIcon_Expanded
:
1028 case wxTreeItemIcon_SelectedExpanded
:
1032 wxTreeViewItem
tvItem(item
, mask
);
1035 return mask
== TVIF_IMAGE
? tvItem
.iImage
: tvItem
.iSelectedImage
;
1038 void wxTreeCtrl::SetItemImage(const wxTreeItemId
& item
, int image
,
1039 wxTreeItemIcon which
)
1041 if ( IS_VIRTUAL_ROOT(item
) )
1043 // TODO: Maybe a hidden root can still store images?
1053 wxFAIL_MSG( wxT("unknown tree item image type") );
1056 case wxTreeItemIcon_Normal
:
1058 const int imageNormalOld
= GetItemImage(item
);
1059 const int imageSelOld
=
1060 GetItemImage(item
, wxTreeItemIcon_Selected
);
1062 // always set the normal image
1063 imageNormal
= image
;
1065 // if the selected and normal images were the same, they should
1066 // be the same after the update, otherwise leave the selected
1068 imageSel
= imageNormalOld
== imageSelOld
? image
: imageSelOld
;
1072 case wxTreeItemIcon_Selected
:
1073 imageNormal
= GetItemImage(item
);
1077 case wxTreeItemIcon_Expanded
:
1078 case wxTreeItemIcon_SelectedExpanded
:
1079 if ( !HasIndirectData(item
) )
1081 // we need to get the old images first, because after we create
1082 // the wxTreeItemIndirectData GetItemXXXImage() will use it to
1084 imageNormal
= GetItemImage(item
);
1085 imageSel
= GetItemImage(item
, wxTreeItemIcon_Selected
);
1087 // if it doesn't have it yet, add it
1088 wxTreeItemIndirectData
*data
= new
1089 wxTreeItemIndirectData(this, item
);
1091 // copy the data to the new location
1092 data
->SetImage(imageNormal
, wxTreeItemIcon_Normal
);
1093 data
->SetImage(imageSel
, wxTreeItemIcon_Selected
);
1096 DoSetItemImageFromData(item
, image
, which
);
1098 // reset the normal/selected images because we won't use them any
1099 // more - now they're stored inside the indirect data
1101 imageSel
= I_IMAGECALLBACK
;
1105 // NB: at least in version 5.00.0518.9 of comctl32.dll we need to always
1106 // change both normal and selected image - otherwise the change simply
1107 // doesn't take place!
1108 DoSetItemImages(item
, imageNormal
, imageSel
);
1111 wxTreeItemData
*wxTreeCtrl::GetItemData(const wxTreeItemId
& item
) const
1113 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1115 // Hidden root may have data.
1116 if ( IS_VIRTUAL_ROOT(item
) )
1118 return GET_VIRTUAL_ROOT()->GetData();
1122 if ( !DoGetItem(&tvItem
) )
1127 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1128 if ( IsDataIndirect(data
) )
1130 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
1136 void wxTreeCtrl::SetItemData(const wxTreeItemId
& item
, wxTreeItemData
*data
)
1138 if ( IS_VIRTUAL_ROOT(item
) )
1140 GET_VIRTUAL_ROOT()->SetData(data
);
1143 // first, associate this piece of data with this item
1149 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1151 if ( HasIndirectData(item
) )
1153 if ( DoGetItem(&tvItem
) )
1155 ((wxTreeItemIndirectData
*)tvItem
.lParam
)->SetData(data
);
1159 wxFAIL_MSG( wxT("failed to change tree items data") );
1164 tvItem
.lParam
= (LPARAM
)data
;
1169 void wxTreeCtrl::SetIndirectItemData(const wxTreeItemId
& item
,
1170 wxTreeItemIndirectData
*data
)
1172 // this should never happen because it's unnecessary and will probably lead
1173 // to crash too because the code elsewhere supposes that the pointer the
1174 // wxTreeItemIndirectData has is a real wxItemData and not
1175 // wxTreeItemIndirectData as well
1176 wxASSERT_MSG( !HasIndirectData(item
), wxT("setting indirect data twice?") );
1178 SetItemData(item
, data
);
1181 bool wxTreeCtrl::HasIndirectData(const wxTreeItemId
& item
) const
1183 // query the item itself
1184 wxTreeViewItem
tvItem(item
, TVIF_PARAM
);
1185 if ( !DoGetItem(&tvItem
) )
1190 wxTreeItemData
*data
= (wxTreeItemData
*)tvItem
.lParam
;
1192 return data
&& IsDataIndirect(data
);
1195 void wxTreeCtrl::SetItemHasChildren(const wxTreeItemId
& item
, bool has
)
1197 if ( IS_VIRTUAL_ROOT(item
) )
1200 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1201 tvItem
.cChildren
= (int)has
;
1205 void wxTreeCtrl::SetItemBold(const wxTreeItemId
& item
, bool bold
)
1207 if ( IS_VIRTUAL_ROOT(item
) )
1210 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1211 tvItem
.state
= bold
? TVIS_BOLD
: 0;
1215 void wxTreeCtrl::SetItemDropHighlight(const wxTreeItemId
& item
, bool highlight
)
1217 if ( IS_VIRTUAL_ROOT(item
) )
1220 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_DROPHILITED
);
1221 tvItem
.state
= highlight
? TVIS_DROPHILITED
: 0;
1225 void wxTreeCtrl::RefreshItem(const wxTreeItemId
& item
)
1227 if ( IS_VIRTUAL_ROOT(item
) )
1231 if ( GetBoundingRect(item
, rect
) )
1237 wxColour
wxTreeCtrl::GetItemTextColour(const wxTreeItemId
& item
) const
1239 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1241 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetTextColour();
1244 wxColour
wxTreeCtrl::GetItemBackgroundColour(const wxTreeItemId
& item
) const
1246 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1248 return it
== m_attrs
.end() ? wxNullColour
: it
->second
->GetBackgroundColour();
1251 wxFont
wxTreeCtrl::GetItemFont(const wxTreeItemId
& item
) const
1253 wxMapTreeAttr::const_iterator it
= m_attrs
.find(item
.m_pItem
);
1255 return it
== m_attrs
.end() ? wxNullFont
: it
->second
->GetFont();
1258 void wxTreeCtrl::SetItemTextColour(const wxTreeItemId
& item
,
1259 const wxColour
& col
)
1261 wxTreeItemAttr
*attr
;
1262 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1263 if ( it
== m_attrs
.end() )
1265 m_hasAnyAttr
= true;
1267 m_attrs
[item
.m_pItem
] =
1268 attr
= new wxTreeItemAttr
;
1275 attr
->SetTextColour(col
);
1280 void wxTreeCtrl::SetItemBackgroundColour(const wxTreeItemId
& item
,
1281 const wxColour
& col
)
1283 wxTreeItemAttr
*attr
;
1284 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1285 if ( it
== m_attrs
.end() )
1287 m_hasAnyAttr
= true;
1289 m_attrs
[item
.m_pItem
] =
1290 attr
= new wxTreeItemAttr
;
1292 else // already in the hash
1297 attr
->SetBackgroundColour(col
);
1302 void wxTreeCtrl::SetItemFont(const wxTreeItemId
& item
, const wxFont
& font
)
1304 wxTreeItemAttr
*attr
;
1305 wxMapTreeAttr::iterator it
= m_attrs
.find(item
.m_pItem
);
1306 if ( it
== m_attrs
.end() )
1308 m_hasAnyAttr
= true;
1310 m_attrs
[item
.m_pItem
] =
1311 attr
= new wxTreeItemAttr
;
1313 else // already in the hash
1318 attr
->SetFont(font
);
1323 // ----------------------------------------------------------------------------
1325 // ----------------------------------------------------------------------------
1327 bool wxTreeCtrl::IsVisible(const wxTreeItemId
& item
) const
1329 if ( item
== wxTreeItemId(TVI_ROOT
) )
1331 // virtual (hidden) root is never visible
1335 // Bug in Gnu-Win32 headers, so don't use the macro TreeView_GetItemRect
1338 // this ugliness comes directly from MSDN - it *is* the correct way to pass
1339 // the HTREEITEM with TVM_GETITEMRECT
1340 *(HTREEITEM
*)&rect
= HITEM(item
);
1342 // true means to get rect for just the text, not the whole line
1343 if ( !::SendMessage(GetHwnd(), TVM_GETITEMRECT
, true, (LPARAM
)&rect
) )
1345 // if TVM_GETITEMRECT returned false, then the item is definitely not
1346 // visible (because its parent is not expanded)
1350 // however if it returned true, the item might still be outside the
1351 // currently visible part of the tree, test for it (notice that partly
1352 // visible means visible here)
1353 return rect
.bottom
> 0 && rect
.top
< GetClientSize().y
;
1356 bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId
& item
) const
1358 wxTreeViewItem
tvItem(item
, TVIF_CHILDREN
);
1361 return tvItem
.cChildren
!= 0;
1364 bool wxTreeCtrl::IsExpanded(const wxTreeItemId
& item
) const
1366 // probably not a good idea to put it here
1367 //wxASSERT( ItemHasChildren(item) );
1369 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDED
);
1372 return (tvItem
.state
& TVIS_EXPANDED
) != 0;
1375 bool wxTreeCtrl::IsSelected(const wxTreeItemId
& item
) const
1377 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_SELECTED
);
1380 return (tvItem
.state
& TVIS_SELECTED
) != 0;
1383 bool wxTreeCtrl::IsBold(const wxTreeItemId
& item
) const
1385 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_BOLD
);
1388 return (tvItem
.state
& TVIS_BOLD
) != 0;
1391 // ----------------------------------------------------------------------------
1393 // ----------------------------------------------------------------------------
1395 wxTreeItemId
wxTreeCtrl::GetRootItem() const
1397 // Root may be real (visible) or virtual (hidden).
1398 if ( GET_VIRTUAL_ROOT() )
1401 return wxTreeItemId(TreeView_GetRoot(GetHwnd()));
1404 wxTreeItemId
wxTreeCtrl::GetSelection() const
1406 wxCHECK_MSG( !(m_windowStyle
& wxTR_MULTIPLE
), wxTreeItemId(),
1407 wxT("this only works with single selection controls") );
1409 return wxTreeItemId(TreeView_GetSelection(GetHwnd()));
1412 wxTreeItemId
wxTreeCtrl::GetItemParent(const wxTreeItemId
& item
) const
1416 if ( IS_VIRTUAL_ROOT(item
) )
1418 // no parent for the virtual root
1423 hItem
= TreeView_GetParent(GetHwnd(), HITEM(item
));
1424 if ( !hItem
&& HasFlag(wxTR_HIDE_ROOT
) )
1426 // the top level items should have the virtual root as their parent
1431 return wxTreeItemId(hItem
);
1434 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1435 wxTreeItemIdValue
& cookie
) const
1437 // remember the last child returned in 'cookie'
1438 cookie
= TreeView_GetChild(GetHwnd(), HITEM(item
));
1440 return wxTreeItemId(cookie
);
1443 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1444 wxTreeItemIdValue
& cookie
) const
1446 wxTreeItemId
item(TreeView_GetNextSibling(GetHwnd(),
1447 HITEM(wxTreeItemId(cookie
))));
1448 cookie
= item
.m_pItem
;
1453 #if WXWIN_COMPATIBILITY_2_4
1455 wxTreeItemId
wxTreeCtrl::GetFirstChild(const wxTreeItemId
& item
,
1458 cookie
= (long)TreeView_GetChild(GetHwnd(), HITEM(item
));
1460 return wxTreeItemId((void *)cookie
);
1463 wxTreeItemId
wxTreeCtrl::GetNextChild(const wxTreeItemId
& WXUNUSED(item
),
1466 wxTreeItemId
item(TreeView_GetNextSibling
1469 HITEM(wxTreeItemId((void *)cookie
)
1471 cookie
= (long)item
.m_pItem
;
1476 #endif // WXWIN_COMPATIBILITY_2_4
1478 wxTreeItemId
wxTreeCtrl::GetLastChild(const wxTreeItemId
& item
) const
1480 // can this be done more efficiently?
1481 wxTreeItemIdValue cookie
;
1483 wxTreeItemId childLast
,
1484 child
= GetFirstChild(item
, cookie
);
1485 while ( child
.IsOk() )
1488 child
= GetNextChild(item
, cookie
);
1494 wxTreeItemId
wxTreeCtrl::GetNextSibling(const wxTreeItemId
& item
) const
1496 return wxTreeItemId(TreeView_GetNextSibling(GetHwnd(), HITEM(item
)));
1499 wxTreeItemId
wxTreeCtrl::GetPrevSibling(const wxTreeItemId
& item
) const
1501 return wxTreeItemId(TreeView_GetPrevSibling(GetHwnd(), HITEM(item
)));
1504 wxTreeItemId
wxTreeCtrl::GetFirstVisibleItem() const
1506 return wxTreeItemId(TreeView_GetFirstVisible(GetHwnd()));
1509 wxTreeItemId
wxTreeCtrl::GetNextVisible(const wxTreeItemId
& item
) const
1511 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetNextVisible() for must be visible itself!"));
1513 return wxTreeItemId(TreeView_GetNextVisible(GetHwnd(), HITEM(item
)));
1516 wxTreeItemId
wxTreeCtrl::GetPrevVisible(const wxTreeItemId
& item
) const
1518 wxASSERT_MSG( IsVisible(item
), wxT("The item you call GetPrevVisible() for must be visible itself!"));
1520 return wxTreeItemId(TreeView_GetPrevVisible(GetHwnd(), HITEM(item
)));
1523 // ----------------------------------------------------------------------------
1524 // multiple selections emulation
1525 // ----------------------------------------------------------------------------
1527 bool wxTreeCtrl::IsItemChecked(const wxTreeItemId
& item
) const
1529 // receive the desired information.
1530 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1533 // state image indices are 1 based
1534 return ((tvItem
.state
>> 12) - 1) == 1;
1537 void wxTreeCtrl::SetItemCheck(const wxTreeItemId
& item
, bool check
)
1539 // receive the desired information.
1540 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_STATEIMAGEMASK
);
1544 // state images are one-based
1545 tvItem
.state
= (check
? 2 : 1) << 12;
1550 size_t wxTreeCtrl::GetSelections(wxArrayTreeItemIds
& selections
) const
1552 TraverseSelections
selector(this, selections
);
1554 return selector
.GetCount();
1557 // ----------------------------------------------------------------------------
1559 // ----------------------------------------------------------------------------
1561 wxTreeItemId
wxTreeCtrl::DoInsertItem(const wxTreeItemId
& parent
,
1562 wxTreeItemId hInsertAfter
,
1563 const wxString
& text
,
1564 int image
, int selectedImage
,
1565 wxTreeItemData
*data
)
1567 wxCHECK_MSG( parent
.IsOk() || !TreeView_GetRoot(GetHwnd()),
1569 _T("can't have more than one root in the tree") );
1571 TV_INSERTSTRUCT tvIns
;
1572 tvIns
.hParent
= HITEM(parent
);
1573 tvIns
.hInsertAfter
= HITEM(hInsertAfter
);
1575 // this is how we insert the item as the first child: supply a NULL
1577 if ( !tvIns
.hInsertAfter
)
1579 tvIns
.hInsertAfter
= TVI_FIRST
;
1583 if ( !text
.IsEmpty() )
1586 tvIns
.item
.pszText
= (wxChar
*)text
.c_str(); // cast is ok
1590 tvIns
.item
.pszText
= NULL
;
1591 tvIns
.item
.cchTextMax
= 0;
1597 tvIns
.item
.iImage
= image
;
1599 if ( selectedImage
== -1 )
1601 // take the same image for selected icon if not specified
1602 selectedImage
= image
;
1606 if ( selectedImage
!= -1 )
1608 mask
|= TVIF_SELECTEDIMAGE
;
1609 tvIns
.item
.iSelectedImage
= selectedImage
;
1615 tvIns
.item
.lParam
= (LPARAM
)data
;
1618 tvIns
.item
.mask
= mask
;
1620 HTREEITEM id
= (HTREEITEM
) TreeView_InsertItem(GetHwnd(), &tvIns
);
1623 wxLogLastError(wxT("TreeView_InsertItem"));
1628 // associate the application tree item with Win32 tree item handle
1632 return wxTreeItemId(id
);
1635 // for compatibility only
1636 #if WXWIN_COMPATIBILITY_2_4
1638 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1639 const wxString
& text
,
1640 int image
, int selImage
,
1643 return DoInsertItem(parent
, wxTreeItemId((void *)insertAfter
), text
,
1644 image
, selImage
, NULL
);
1647 #endif // WXWIN_COMPATIBILITY_2_4
1649 wxTreeItemId
wxTreeCtrl::AddRoot(const wxString
& text
,
1650 int image
, int selectedImage
,
1651 wxTreeItemData
*data
)
1654 if ( m_windowStyle
& wxTR_HIDE_ROOT
)
1656 // create a virtual root item, the parent for all the others
1657 m_pVirtualRoot
= new wxVirtualNode(data
);
1662 return DoInsertItem(wxTreeItemId(), wxTreeItemId(),
1663 text
, image
, selectedImage
, data
);
1666 wxTreeItemId
wxTreeCtrl::PrependItem(const wxTreeItemId
& parent
,
1667 const wxString
& text
,
1668 int image
, int selectedImage
,
1669 wxTreeItemData
*data
)
1671 return DoInsertItem(parent
, TVI_FIRST
,
1672 text
, image
, selectedImage
, data
);
1675 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1676 const wxTreeItemId
& idPrevious
,
1677 const wxString
& text
,
1678 int image
, int selectedImage
,
1679 wxTreeItemData
*data
)
1681 return DoInsertItem(parent
, idPrevious
, text
, image
, selectedImage
, data
);
1684 wxTreeItemId
wxTreeCtrl::InsertItem(const wxTreeItemId
& parent
,
1686 const wxString
& text
,
1687 int image
, int selectedImage
,
1688 wxTreeItemData
*data
)
1690 // find the item from index
1691 wxTreeItemIdValue cookie
;
1692 wxTreeItemId idPrev
, idCur
= GetFirstChild(parent
, cookie
);
1693 while ( index
!= 0 && idCur
.IsOk() )
1698 idCur
= GetNextChild(parent
, cookie
);
1701 // assert, not check: if the index is invalid, we will append the item
1703 wxASSERT_MSG( index
== 0, _T("bad index in wxTreeCtrl::InsertItem") );
1705 return DoInsertItem(parent
, idPrev
, text
, image
, selectedImage
, data
);
1708 wxTreeItemId
wxTreeCtrl::AppendItem(const wxTreeItemId
& parent
,
1709 const wxString
& text
,
1710 int image
, int selectedImage
,
1711 wxTreeItemData
*data
)
1713 return DoInsertItem(parent
, TVI_LAST
,
1714 text
, image
, selectedImage
, data
);
1717 void wxTreeCtrl::Delete(const wxTreeItemId
& item
)
1719 if ( !TreeView_DeleteItem(GetHwnd(), HITEM(item
)) )
1721 wxLogLastError(wxT("TreeView_DeleteItem"));
1725 // delete all children (but don't delete the item itself)
1726 void wxTreeCtrl::DeleteChildren(const wxTreeItemId
& item
)
1728 wxTreeItemIdValue cookie
;
1730 wxArrayTreeItemIds children
;
1731 wxTreeItemId child
= GetFirstChild(item
, cookie
);
1732 while ( child
.IsOk() )
1734 children
.Add(child
);
1736 child
= GetNextChild(item
, cookie
);
1739 size_t nCount
= children
.Count();
1740 for ( size_t n
= 0; n
< nCount
; n
++ )
1742 if ( !TreeView_DeleteItem(GetHwnd(), HITEM_PTR(children
[n
])) )
1744 wxLogLastError(wxT("TreeView_DeleteItem"));
1749 void wxTreeCtrl::DeleteAllItems()
1751 // delete the "virtual" root item.
1752 if ( GET_VIRTUAL_ROOT() )
1754 delete GET_VIRTUAL_ROOT();
1755 m_pVirtualRoot
= NULL
;
1758 // and all the real items
1760 if ( !TreeView_DeleteAllItems(GetHwnd()) )
1762 wxLogLastError(wxT("TreeView_DeleteAllItems"));
1766 void wxTreeCtrl::DoExpand(const wxTreeItemId
& item
, int flag
)
1768 wxASSERT_MSG( flag
== TVE_COLLAPSE
||
1769 flag
== (TVE_COLLAPSE
| TVE_COLLAPSERESET
) ||
1770 flag
== TVE_EXPAND
||
1772 wxT("Unknown flag in wxTreeCtrl::DoExpand") );
1774 // A hidden root can be neither expanded nor collapsed.
1775 wxCHECK_RET( !(m_windowStyle
& wxTR_HIDE_ROOT
) || (HITEM(item
) != TVI_ROOT
),
1776 wxT("Can't expand/collapse hidden root node!") )
1778 // TreeView_Expand doesn't send TVN_ITEMEXPAND(ING) messages, so we must
1779 // emulate them. This behaviour has changed slightly with comctl32.dll
1780 // v 4.70 - now it does send them but only the first time. To maintain
1781 // compatible behaviour and also in order to not have surprises with the
1782 // future versions, don't rely on this and still do everything ourselves.
1783 // To avoid that the messages be sent twice when the item is expanded for
1784 // the first time we must clear TVIS_EXPANDEDONCE style manually.
1786 wxTreeViewItem
tvItem(item
, TVIF_STATE
, TVIS_EXPANDEDONCE
);
1790 if ( TreeView_Expand(GetHwnd(), HITEM(item
), flag
) != 0 )
1792 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1793 event
.m_item
= item
;
1794 event
.SetEventObject(this);
1796 // note that the {EXPAND|COLLAPS}ING event is sent by TreeView_Expand()
1798 event
.SetEventType(gs_expandEvents
[IsExpanded(item
) ? IDX_EXPAND
1802 (void)GetEventHandler()->ProcessEvent(event
);
1804 //else: change didn't took place, so do nothing at all
1807 void wxTreeCtrl::Expand(const wxTreeItemId
& item
)
1809 DoExpand(item
, TVE_EXPAND
);
1812 void wxTreeCtrl::Collapse(const wxTreeItemId
& item
)
1814 DoExpand(item
, TVE_COLLAPSE
);
1817 void wxTreeCtrl::CollapseAndReset(const wxTreeItemId
& item
)
1819 DoExpand(item
, TVE_COLLAPSE
| TVE_COLLAPSERESET
);
1822 void wxTreeCtrl::Toggle(const wxTreeItemId
& item
)
1824 DoExpand(item
, TVE_TOGGLE
);
1827 #if WXWIN_COMPATIBILITY_2_4
1828 void wxTreeCtrl::ExpandItem(const wxTreeItemId
& item
, int action
)
1830 DoExpand(item
, action
);
1834 void wxTreeCtrl::Unselect()
1836 wxASSERT_MSG( !(m_windowStyle
& wxTR_MULTIPLE
),
1837 wxT("doesn't make sense, may be you want UnselectAll()?") );
1839 // just remove the selection
1840 SelectItem(wxTreeItemId());
1843 void wxTreeCtrl::UnselectAll()
1845 if ( m_windowStyle
& wxTR_MULTIPLE
)
1847 wxArrayTreeItemIds selections
;
1848 size_t count
= GetSelections(selections
);
1849 for ( size_t n
= 0; n
< count
; n
++ )
1851 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1852 SetItemCheck(HITEM_PTR(selections
[n
]), false);
1853 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1854 ::UnselectItem(GetHwnd(), HITEM_PTR(selections
[n
]));
1855 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1860 // just remove the selection
1865 void wxTreeCtrl::SelectItem(const wxTreeItemId
& item
)
1867 if ( m_windowStyle
& wxTR_MULTIPLE
)
1869 #if wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1870 // selecting the item means checking it
1872 #else // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1873 ::SelectItem(GetHwnd(), HITEM(item
));
1874 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE/!wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
1878 // inspite of the docs (MSDN Jan 99 edition), we don't seem to receive
1879 // the notification from the control (i.e. TVN_SELCHANG{ED|ING}), so
1880 // send them ourselves
1882 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
1883 event
.m_item
= item
;
1884 event
.SetEventObject(this);
1886 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGING
);
1887 if ( !GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
1889 if ( !TreeView_SelectItem(GetHwnd(), HITEM(item
)) )
1891 wxLogLastError(wxT("TreeView_SelectItem"));
1895 event
.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED
);
1896 (void)GetEventHandler()->ProcessEvent(event
);
1899 //else: program vetoed the change
1903 void wxTreeCtrl::EnsureVisible(const wxTreeItemId
& item
)
1906 TreeView_EnsureVisible(GetHwnd(), HITEM(item
));
1909 void wxTreeCtrl::ScrollTo(const wxTreeItemId
& item
)
1911 if ( !TreeView_SelectSetFirstVisible(GetHwnd(), HITEM(item
)) )
1913 wxLogLastError(wxT("TreeView_SelectSetFirstVisible"));
1917 wxTextCtrl
*wxTreeCtrl::GetEditControl() const
1922 void wxTreeCtrl::DeleteTextCtrl()
1926 // the HWND corresponding to this control is deleted by the tree
1927 // control itself and we don't know when exactly this happens, so check
1928 // if the window still exists before calling UnsubclassWin()
1929 if ( !::IsWindow(GetHwndOf(m_textCtrl
)) )
1931 m_textCtrl
->SetHWND(0);
1934 m_textCtrl
->UnsubclassWin();
1935 m_textCtrl
->SetHWND(0);
1941 wxTextCtrl
* wxTreeCtrl::EditLabel(const wxTreeItemId
& item
,
1942 wxClassInfo
* textControlClass
)
1944 wxASSERT( textControlClass
->IsKindOf(CLASSINFO(wxTextCtrl
)) );
1948 m_textCtrl
= (wxTextCtrl
*)textControlClass
->CreateObject();
1949 HWND hWnd
= (HWND
) TreeView_EditLabel(GetHwnd(), HITEM(item
));
1951 // this is not an error - the TVN_BEGINLABELEDIT handler might have
1960 // textctrl is subclassed in MSWOnNotify
1964 // End label editing, optionally cancelling the edit
1965 void wxTreeCtrl::EndEditLabel(const wxTreeItemId
& WXUNUSED(item
), bool discardChanges
)
1967 TreeView_EndEditLabelNow(GetHwnd(), discardChanges
);
1972 wxTreeItemId
wxTreeCtrl::HitTest(const wxPoint
& point
, int& flags
)
1974 TV_HITTESTINFO hitTestInfo
;
1975 hitTestInfo
.pt
.x
= (int)point
.x
;
1976 hitTestInfo
.pt
.y
= (int)point
.y
;
1978 TreeView_HitTest(GetHwnd(), &hitTestInfo
);
1983 #define TRANSLATE_FLAG(flag) if ( hitTestInfo.flags & TVHT_##flag ) \
1984 flags |= wxTREE_HITTEST_##flag
1986 TRANSLATE_FLAG(ABOVE
);
1987 TRANSLATE_FLAG(BELOW
);
1988 TRANSLATE_FLAG(NOWHERE
);
1989 TRANSLATE_FLAG(ONITEMBUTTON
);
1990 TRANSLATE_FLAG(ONITEMICON
);
1991 TRANSLATE_FLAG(ONITEMINDENT
);
1992 TRANSLATE_FLAG(ONITEMLABEL
);
1993 TRANSLATE_FLAG(ONITEMRIGHT
);
1994 TRANSLATE_FLAG(ONITEMSTATEICON
);
1995 TRANSLATE_FLAG(TOLEFT
);
1996 TRANSLATE_FLAG(TORIGHT
);
1998 #undef TRANSLATE_FLAG
2000 return wxTreeItemId(hitTestInfo
.hItem
);
2003 bool wxTreeCtrl::GetBoundingRect(const wxTreeItemId
& item
,
2005 bool textOnly
) const
2009 // Virtual root items have no bounding rectangle
2010 if ( IS_VIRTUAL_ROOT(item
) )
2015 if ( TreeView_GetItemRect(GetHwnd(), HITEM(item
),
2018 rect
= wxRect(wxPoint(rc
.left
, rc
.top
), wxPoint(rc
.right
, rc
.bottom
));
2024 // couldn't retrieve rect: for example, item isn't visible
2029 // ----------------------------------------------------------------------------
2031 // ----------------------------------------------------------------------------
2033 // this is just a tiny namespace which is friend to wxTreeCtrl and so can use
2034 // functions such as IsDataIndirect()
2035 class wxTreeSortHelper
2038 static int CALLBACK
Compare(LPARAM data1
, LPARAM data2
, LPARAM tree
);
2041 static wxTreeItemId
GetIdFromData(wxTreeCtrl
*tree
, LPARAM item
)
2043 wxTreeItemData
*data
= (wxTreeItemData
*)item
;
2044 if ( tree
->IsDataIndirect(data
) )
2046 data
= ((wxTreeItemIndirectData
*)data
)->GetData();
2049 return data
->GetId();
2053 int CALLBACK
wxTreeSortHelper::Compare(LPARAM pItem1
,
2057 wxCHECK_MSG( pItem1
&& pItem2
, 0,
2058 wxT("sorting tree without data doesn't make sense") );
2060 wxTreeCtrl
*tree
= (wxTreeCtrl
*)htree
;
2062 return tree
->OnCompareItems(GetIdFromData(tree
, pItem1
),
2063 GetIdFromData(tree
, pItem2
));
2066 int wxTreeCtrl::OnCompareItems(const wxTreeItemId
& item1
,
2067 const wxTreeItemId
& item2
)
2069 return wxStrcmp(GetItemText(item1
), GetItemText(item2
));
2072 void wxTreeCtrl::SortChildren(const wxTreeItemId
& item
)
2074 // rely on the fact that TreeView_SortChildren does the same thing as our
2075 // default behaviour, i.e. sorts items alphabetically and so call it
2076 // directly if we're not in derived class (much more efficient!)
2077 if ( GetClassInfo() == CLASSINFO(wxTreeCtrl
) )
2079 TreeView_SortChildren(GetHwnd(), HITEM(item
), 0);
2084 tvSort
.hParent
= HITEM(item
);
2085 tvSort
.lpfnCompare
= wxTreeSortHelper::Compare
;
2086 tvSort
.lParam
= (LPARAM
)this;
2087 TreeView_SortChildrenCB(GetHwnd(), &tvSort
, 0 /* reserved */);
2091 // ----------------------------------------------------------------------------
2093 // ----------------------------------------------------------------------------
2095 bool wxTreeCtrl::MSWCommand(WXUINT cmd
, WXWORD id
)
2097 if ( cmd
== EN_UPDATE
)
2099 wxCommandEvent
event(wxEVT_COMMAND_TEXT_UPDATED
, id
);
2100 event
.SetEventObject( this );
2101 ProcessCommand(event
);
2103 else if ( cmd
== EN_KILLFOCUS
)
2105 wxCommandEvent
event(wxEVT_KILL_FOCUS
, id
);
2106 event
.SetEventObject( this );
2107 ProcessCommand(event
);
2115 // command processed
2119 // we hook into WndProc to process WM_MOUSEMOVE/WM_BUTTONUP messages - as we
2120 // only do it during dragging, minimize wxWin overhead (this is important for
2121 // WM_MOUSEMOVE as they're a lot of them) by catching Windows messages directly
2122 // instead of passing by wxWin events
2123 long wxTreeCtrl::MSWWindowProc(WXUINT nMsg
, WXWPARAM wParam
, WXLPARAM lParam
)
2125 bool processed
= false;
2127 bool isMultiple
= (GetWindowStyle() & wxTR_MULTIPLE
) != 0;
2129 if ( (nMsg
>= WM_MOUSEFIRST
) && (nMsg
<= WM_MOUSELAST
) )
2131 // we only process mouse messages here and these parameters have the
2132 // same meaning for all of them
2133 int x
= GET_X_LPARAM(lParam
),
2134 y
= GET_Y_LPARAM(lParam
);
2135 HTREEITEM htItem
= GetItemFromPoint(GetHwnd(), x
, y
);
2139 case WM_RBUTTONDOWN
:
2140 // if the item we are about to right click on is not already
2141 // selected or if we click outside of any item, remove the
2142 // entire previous selection
2143 if ( !htItem
|| !::IsItemSelected(GetHwnd(), htItem
) )
2148 // select item and set the focus to the
2149 // newly selected item
2150 ::SelectItem(GetHwnd(), htItem
);
2151 ::SetFocus(GetHwnd(), htItem
);
2154 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2155 case WM_LBUTTONDOWN
:
2156 if ( htItem
&& isMultiple
)
2158 if ( wParam
& MK_CONTROL
)
2162 // toggle selected state
2163 ToggleItemSelection(GetHwnd(), htItem
);
2165 ::SetFocus(GetHwnd(), htItem
);
2167 // reset on any click without Shift
2168 m_htSelStart
.Unset();
2172 else if ( wParam
& MK_SHIFT
)
2174 // this selects all items between the starting one and
2177 if ( !m_htSelStart
)
2179 // take the focused item
2180 m_htSelStart
= TreeView_GetSelection(GetHwnd());
2183 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htItem
,
2184 !(wParam
& MK_CONTROL
));
2186 ::SetFocus(GetHwnd(), htItem
);
2190 else // normal click
2192 // avoid doing anything if we click on the only
2193 // currently selected item
2195 wxArrayTreeItemIds selections
;
2196 size_t count
= GetSelections(selections
);
2199 HITEM_PTR(selections
[0]) != htItem
)
2201 // clear the previously selected items, if the
2202 // user clicked outside of the present selection.
2203 // otherwise, perform the deselection on mouse-up.
2204 // this allows multiple drag and drop to work.
2206 if (IsItemSelected(GetHwnd(), htItem
))
2208 ::SetFocus(GetHwnd(), htItem
);
2214 // prevent the click from starting in-place editing
2215 // which should only happen if we click on the
2216 // already selected item (and nothing else is
2219 TreeView_SelectItem(GetHwnd(), 0);
2220 ::SelectItem(GetHwnd(), htItem
);
2224 // reset on any click without Shift
2225 m_htSelStart
.Unset();
2229 #endif // wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2234 m_dragImage
->Move(wxPoint(x
, y
));
2237 // highlight the item as target (hiding drag image is
2238 // necessary - otherwise the display will be corrupted)
2239 m_dragImage
->Hide();
2240 TreeView_SelectDropTarget(GetHwnd(), htItem
);
2241 m_dragImage
->Show();
2248 // facilitates multiple drag-and-drop
2249 if (htItem
&& isMultiple
)
2251 wxArrayTreeItemIds selections
;
2252 size_t count
= GetSelections(selections
);
2255 !(wParam
& MK_CONTROL
) &&
2256 !(wParam
& MK_SHIFT
))
2259 TreeView_SelectItem(GetHwnd(), htItem
);
2268 m_dragImage
->EndDrag();
2272 // generate the drag end event
2273 wxTreeEvent
event(wxEVT_COMMAND_TREE_END_DRAG
, m_windowId
);
2275 event
.m_item
= htItem
;
2276 event
.m_pointDrag
= wxPoint(x
, y
);
2277 event
.SetEventObject(this);
2279 (void)GetEventHandler()->ProcessEvent(event
);
2281 // if we don't do it, the tree seems to think that 2 items
2282 // are selected simultaneously which is quite weird
2283 TreeView_SelectDropTarget(GetHwnd(), 0);
2288 #if !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2289 else if ( (nMsg
== WM_SETFOCUS
|| nMsg
== WM_KILLFOCUS
) && isMultiple
)
2291 // the tree control greys out the selected item when it loses focus and
2292 // paints it as selected again when it regains it, but it won't do it
2293 // for the other items itself - help it
2294 wxArrayTreeItemIds selections
;
2295 size_t count
= GetSelections(selections
);
2297 for ( size_t n
= 0; n
< count
; n
++ )
2299 // TreeView_GetItemRect() will return false if item is not visible,
2300 // which may happen perfectly well
2301 if ( TreeView_GetItemRect(GetHwnd(), HITEM_PTR(selections
[n
]),
2304 ::InvalidateRect(GetHwnd(), &rect
, false);
2308 else if ( nMsg
== WM_KEYDOWN
&& isMultiple
)
2310 bool bCtrl
= wxIsCtrlDown(),
2311 bShift
= wxIsShiftDown();
2313 // we handle.arrows and space, but not page up/down and home/end: the
2314 // latter should be easy, but not the former
2316 HTREEITEM htSel
= (HTREEITEM
)TreeView_GetSelection(GetHwnd());
2317 if ( !m_htSelStart
)
2319 m_htSelStart
= htSel
;
2322 if ( wParam
== VK_SPACE
)
2326 ToggleItemSelection(GetHwnd(), htSel
);
2332 ::SelectItem(GetHwnd(), htSel
);
2337 else if ( wParam
== VK_UP
|| wParam
== VK_DOWN
)
2339 if ( !bCtrl
&& !bShift
)
2341 // no modifiers, just clear selection and then let the default
2342 // processing to take place
2347 (void)wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2349 HTREEITEM htNext
= (HTREEITEM
)(wParam
== VK_UP
2350 ? TreeView_GetPrevVisible(GetHwnd(), htSel
)
2351 : TreeView_GetNextVisible(GetHwnd(), htSel
));
2355 // at the top/bottom
2361 SelectRange(GetHwnd(), HITEM(m_htSelStart
), htNext
);
2365 // without changing selection
2366 ::SetFocus(GetHwnd(), htNext
);
2373 #endif // !wxUSE_CHECKBOXES_IN_MULTI_SEL_TREE
2374 else if ( nMsg
== WM_CHAR
)
2376 // don't let the control process Space and Return keys because it
2377 // doesn't do anything useful with them anyhow but always beeps
2378 // annoyingly when it receives them and there is no way to turn it off
2379 // simply if you just process TREEITEM_ACTIVATED event to which Space
2380 // and Enter presses are mapped in your code
2381 if ( wParam
== VK_SPACE
|| wParam
== VK_RETURN
)
2388 rc
= wxControl::MSWWindowProc(nMsg
, wParam
, lParam
);
2393 // process WM_NOTIFY Windows message
2394 bool wxTreeCtrl::MSWOnNotify(int idCtrl
, WXLPARAM lParam
, WXLPARAM
*result
)
2396 wxTreeEvent
event(wxEVT_NULL
, m_windowId
);
2397 wxEventType eventType
= wxEVT_NULL
;
2398 NMHDR
*hdr
= (NMHDR
*)lParam
;
2400 switch ( hdr
->code
)
2403 eventType
= wxEVT_COMMAND_TREE_BEGIN_DRAG
;
2406 case TVN_BEGINRDRAG
:
2408 if ( eventType
== wxEVT_NULL
)
2409 eventType
= wxEVT_COMMAND_TREE_BEGIN_RDRAG
;
2410 //else: left drag, already set above
2412 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2414 event
.m_item
= tv
->itemNew
.hItem
;
2415 event
.m_pointDrag
= wxPoint(tv
->ptDrag
.x
, tv
->ptDrag
.y
);
2417 // don't allow dragging by default: the user code must
2418 // explicitly say that it wants to allow it to avoid breaking
2424 case TVN_BEGINLABELEDIT
:
2426 eventType
= wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT
;
2427 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2429 event
.m_item
= info
->item
.hItem
;
2430 event
.m_label
= info
->item
.pszText
;
2431 event
.m_editCancelled
= false;
2435 case TVN_DELETEITEM
:
2437 eventType
= wxEVT_COMMAND_TREE_DELETE_ITEM
;
2438 NM_TREEVIEW
*tv
= (NM_TREEVIEW
*)lParam
;
2440 event
.m_item
= tv
->itemOld
.hItem
;
2444 wxMapTreeAttr::iterator it
= m_attrs
.find(tv
->itemOld
.hItem
);
2445 if ( it
!= m_attrs
.end() )
2454 case TVN_ENDLABELEDIT
:
2456 eventType
= wxEVT_COMMAND_TREE_END_LABEL_EDIT
;
2457 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2459 event
.m_item
= info
->item
.hItem
;
2460 event
.m_label
= info
->item
.pszText
;
2461 event
.m_editCancelled
= info
->item
.pszText
== NULL
;
2466 // These *must* not be removed or TVN_GETINFOTIP will
2467 // not be processed each time the mouse is moved
2468 // and the tooltip will only ever update once.
2477 case TVN_GETINFOTIP
:
2479 eventType
= wxEVT_COMMAND_TREE_ITEM_GETTOOLTIP
;
2480 NMTVGETINFOTIP
*info
= (NMTVGETINFOTIP
*)lParam
;
2482 // Which item are we trying to get a tooltip for?
2483 event
.m_item
= info
->hItem
;
2488 case TVN_GETDISPINFO
:
2489 eventType
= wxEVT_COMMAND_TREE_GET_INFO
;
2492 case TVN_SETDISPINFO
:
2494 if ( eventType
== wxEVT_NULL
)
2495 eventType
= wxEVT_COMMAND_TREE_SET_INFO
;
2496 //else: get, already set above
2498 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2500 event
.m_item
= info
->item
.hItem
;
2504 case TVN_ITEMEXPANDING
:
2505 case TVN_ITEMEXPANDED
:
2507 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2510 switch ( tv
->action
)
2513 wxLogDebug(wxT("unexpected code %d in TVN_ITEMEXPAND message"), tv
->action
);
2521 what
= IDX_COLLAPSE
;
2525 int how
= hdr
->code
== TVN_ITEMEXPANDING
? IDX_DOING
2528 eventType
= gs_expandEvents
[what
][how
];
2530 event
.m_item
= tv
->itemNew
.hItem
;
2536 eventType
= wxEVT_COMMAND_TREE_KEY_DOWN
;
2537 TV_KEYDOWN
*info
= (TV_KEYDOWN
*)lParam
;
2539 // fabricate the lParam and wParam parameters sufficiently
2540 // similar to the ones from a "real" WM_KEYDOWN so that
2541 // CreateKeyEvent() works correctly
2543 (::GetKeyState(VK_MENU
) < 0 ? KF_ALTDOWN
: 0) << 16;
2545 WXWPARAM wParam
= info
->wVKey
;
2547 int keyCode
= wxCharCodeMSWToWX(info
->wVKey
);
2550 // wxCharCodeMSWToWX() returns 0 to indicate that this is a
2555 event
.m_evtKey
= CreateKeyEvent(wxEVT_KEY_DOWN
,
2560 // a separate event for Space/Return
2561 if ( !wxIsCtrlDown() && !wxIsShiftDown() &&
2562 ((info
->wVKey
== VK_SPACE
) || (info
->wVKey
== VK_RETURN
)) )
2564 wxTreeEvent
event2(wxEVT_COMMAND_TREE_ITEM_ACTIVATED
,
2566 event2
.SetEventObject(this);
2567 if ( !(GetWindowStyle() & wxTR_MULTIPLE
) )
2569 event2
.m_item
= GetSelection();
2571 //else: don't know how to get it
2573 (void)GetEventHandler()->ProcessEvent(event2
);
2578 // NB: MSLU is broken and sends TVN_SELCHANGEDA instead of
2579 // TVN_SELCHANGEDW in Unicode mode under Win98. Therefore
2580 // we have to handle both messages:
2581 case TVN_SELCHANGEDA
:
2582 case TVN_SELCHANGEDW
:
2583 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGED
;
2586 case TVN_SELCHANGINGA
:
2587 case TVN_SELCHANGINGW
:
2589 if ( eventType
== wxEVT_NULL
)
2590 eventType
= wxEVT_COMMAND_TREE_SEL_CHANGING
;
2591 //else: already set above
2593 if (hdr
->code
== TVN_SELCHANGINGW
||
2594 hdr
->code
== TVN_SELCHANGEDW
)
2596 NM_TREEVIEWW
* tv
= (NM_TREEVIEWW
*)lParam
;
2597 event
.m_item
= tv
->itemNew
.hItem
;
2598 event
.m_itemOld
= tv
->itemOld
.hItem
;
2602 NM_TREEVIEWA
* tv
= (NM_TREEVIEWA
*)lParam
;
2603 event
.m_item
= tv
->itemNew
.hItem
;
2604 event
.m_itemOld
= tv
->itemOld
.hItem
;
2609 // instead of explicitly checking for _WIN32_IE, check if the
2610 // required symbols are available in the headers
2611 #if defined(CDDS_PREPAINT) && !wxUSE_COMCTL32_SAFELY
2614 LPNMTVCUSTOMDRAW lptvcd
= (LPNMTVCUSTOMDRAW
)lParam
;
2615 NMCUSTOMDRAW
& nmcd
= lptvcd
->nmcd
;
2616 switch ( nmcd
.dwDrawStage
)
2619 // if we've got any items with non standard attributes,
2620 // notify us before painting each item
2621 *result
= m_hasAnyAttr
? CDRF_NOTIFYITEMDRAW
2625 case CDDS_ITEMPREPAINT
:
2627 wxMapTreeAttr::iterator
2628 it
= m_attrs
.find((void *)nmcd
.dwItemSpec
);
2630 if ( it
== m_attrs
.end() )
2632 // nothing to do for this item
2633 *result
= CDRF_DODEFAULT
;
2637 wxTreeItemAttr
* const attr
= it
->second
;
2639 // selection colours should override ours,
2640 // otherwise it is too confusing ot the user
2641 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) )
2644 if ( attr
->HasBackgroundColour() )
2646 colBack
= attr
->GetBackgroundColour();
2647 lptvcd
->clrTextBk
= wxColourToRGB(colBack
);
2651 // but we still want to keep the special foreground
2652 // colour when we don't have focus (we can't keep
2653 // it when we do, it would usually be unreadable on
2654 // the almost inverted bg colour...)
2655 if ( !(nmcd
.uItemState
& CDIS_SELECTED
) ||
2656 FindFocus() != this )
2659 if ( attr
->HasTextColour() )
2661 colText
= attr
->GetTextColour();
2662 lptvcd
->clrText
= wxColourToRGB(colText
);
2666 if ( attr
->HasFont() )
2668 HFONT hFont
= GetHfontOf(attr
->GetFont());
2670 ::SelectObject(nmcd
.hdc
, hFont
);
2672 *result
= CDRF_NEWFONT
;
2674 else // no specific font
2676 *result
= CDRF_DODEFAULT
;
2682 *result
= CDRF_DODEFAULT
;
2686 // we always process it
2688 #endif // have owner drawn support in headers
2692 DWORD pos
= GetMessagePos();
2694 point
.x
= LOWORD(pos
);
2695 point
.y
= HIWORD(pos
);
2696 ::MapWindowPoints(HWND_DESKTOP
, GetHwnd(), &point
, 1);
2698 wxTreeItemId item
= HitTest(wxPoint(point
.x
, point
.y
), flags
);
2699 if (flags
& wxTREE_HITTEST_ONITEMSTATEICON
)
2701 event
.m_item
= item
;
2702 eventType
= wxEVT_COMMAND_TREE_STATE_IMAGE_CLICK
;
2710 TV_HITTESTINFO tvhti
;
2711 ::GetCursorPos(&tvhti
.pt
);
2712 ::ScreenToClient(GetHwnd(), &tvhti
.pt
);
2713 if ( TreeView_HitTest(GetHwnd(), &tvhti
) )
2715 if ( tvhti
.flags
& TVHT_ONITEM
)
2717 event
.m_item
= tvhti
.hItem
;
2718 eventType
= (int)hdr
->code
== NM_DBLCLK
2719 ? wxEVT_COMMAND_TREE_ITEM_ACTIVATED
2720 : wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK
;
2722 event
.m_pointDrag
.x
= tvhti
.pt
.x
;
2723 event
.m_pointDrag
.y
= tvhti
.pt
.y
;
2732 return wxControl::MSWOnNotify(idCtrl
, lParam
, result
);
2735 event
.SetEventObject(this);
2736 event
.SetEventType(eventType
);
2738 bool processed
= GetEventHandler()->ProcessEvent(event
);
2741 switch ( hdr
->code
)
2744 // we translate NM_DBLCLK into ACTIVATED event, so don't interpret
2745 // the return code of this event handler as the return value for
2746 // NM_DBLCLK - otherwise, double clicking the item to toggle its
2747 // expanded status would never work
2752 case TVN_BEGINRDRAG
:
2753 if ( event
.IsAllowed() )
2755 // normally this is impossible because the m_dragImage is
2756 // deleted once the drag operation is over
2757 wxASSERT_MSG( !m_dragImage
, _T("starting to drag once again?") );
2759 m_dragImage
= new wxDragImage(*this, event
.m_item
);
2760 m_dragImage
->BeginDrag(wxPoint(0, 0), this);
2761 m_dragImage
->Show();
2765 case TVN_DELETEITEM
:
2767 // NB: we might process this message using wxWindows event
2768 // tables, but due to overhead of wxWin event system we
2769 // prefer to do it here ourself (otherwise deleting a tree
2770 // with many items is just too slow)
2771 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2773 wxTreeItemId item
= event
.m_item
;
2774 if ( HasIndirectData(item
) )
2776 wxTreeItemIndirectData
*data
= (wxTreeItemIndirectData
*)
2778 delete data
; // can't be NULL here
2782 wxTreeItemData
*data
= (wxTreeItemData
*)tv
->itemOld
.lParam
;
2783 delete data
; // may be NULL, ok
2786 processed
= true; // Make sure we don't get called twice
2790 case TVN_BEGINLABELEDIT
:
2791 // return true to cancel label editing
2792 *result
= !event
.IsAllowed();
2793 // set ES_WANTRETURN ( like we do in BeginLabelEdit )
2794 if(event
.IsAllowed())
2796 HWND hText
= TreeView_GetEditControl(GetHwnd());
2799 // MBN: if m_textCtrl already has an HWND, it is a stale
2800 // pointer from a previous edit (because the user
2801 // didn't modify the label before dismissing the control,
2802 // and TVN_ENDLABELEDIT was not sent), so delete it
2803 if(m_textCtrl
&& m_textCtrl
->GetHWND() != 0)
2806 m_textCtrl
= new wxTextCtrl();
2807 m_textCtrl
->SetParent(this);
2808 m_textCtrl
->SetHWND((WXHWND
)hText
);
2809 m_textCtrl
->SubclassWin((WXHWND
)hText
);
2811 // set wxTE_PROCESS_ENTER style for the text control to
2812 // force it to process the Enter presses itself, otherwise
2813 // they could be stolen from it by the dialog
2815 m_textCtrl
->SetWindowStyle(m_textCtrl
->GetWindowStyle()
2816 | wxTE_PROCESS_ENTER
);
2821 case TVN_ENDLABELEDIT
:
2822 // return true to set the label to the new string: note that we
2823 // also must pretend that we did process the message or it is going
2824 // to be passed to DefWindowProc() which will happily return false
2825 // cancelling the label change
2826 *result
= event
.IsAllowed();
2829 // ensure that we don't have the text ctrl which is going to be
2834 case TVN_GETINFOTIP
:
2836 // If the user permitted a tooltip change, change it
2837 if (event
.IsAllowed())
2839 SetToolTip(event
.m_label
);
2844 case TVN_SELCHANGING
:
2845 case TVN_ITEMEXPANDING
:
2846 // return true to prevent the action from happening
2847 *result
= !event
.IsAllowed();
2850 case TVN_ITEMEXPANDED
:
2851 // the item is not refreshed properly after expansion when it has
2852 // an image depending on the expanded/collapsed state - bug in
2853 // comctl32.dll or our code?
2855 NM_TREEVIEW
* tv
= (NM_TREEVIEW
*)lParam
;
2856 wxTreeItemId
id(tv
->itemNew
.hItem
);
2858 int image
= GetItemImage(id
, wxTreeItemIcon_Expanded
);
2866 case TVN_GETDISPINFO
:
2867 // NB: so far the user can't set the image himself anyhow, so do it
2868 // anyway - but this may change later
2869 //if ( /* !processed && */ 1 )
2871 wxTreeItemId item
= event
.m_item
;
2872 TV_DISPINFO
*info
= (TV_DISPINFO
*)lParam
;
2873 if ( info
->item
.mask
& TVIF_IMAGE
)
2876 DoGetItemImageFromData
2879 IsExpanded(item
) ? wxTreeItemIcon_Expanded
2880 : wxTreeItemIcon_Normal
2883 if ( info
->item
.mask
& TVIF_SELECTEDIMAGE
)
2885 info
->item
.iSelectedImage
=
2886 DoGetItemImageFromData
2889 IsExpanded(item
) ? wxTreeItemIcon_SelectedExpanded
2890 : wxTreeItemIcon_Selected
2897 // for the other messages the return value is ignored and there is
2898 // nothing special to do
2903 // ----------------------------------------------------------------------------
2905 // ----------------------------------------------------------------------------
2907 // why do they define INDEXTOSTATEIMAGEMASK but not the inverse?
2908 #define STATEIMAGEMASKTOINDEX(state) (((state) & TVIS_STATEIMAGEMASK) >> 12)
2910 void wxTreeCtrl::SetState(const wxTreeItemId
& node
, int state
)
2913 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2914 tvi
.mask
= TVIF_STATE
;
2915 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2917 // Select the specified state, or -1 == cycle to the next one.
2920 TreeView_GetItem(GetHwnd(), &tvi
);
2922 state
= STATEIMAGEMASKTOINDEX(tvi
.state
) + 1;
2923 if ( state
== m_imageListState
->GetImageCount() )
2927 wxCHECK_RET( state
< m_imageListState
->GetImageCount(),
2928 _T("wxTreeCtrl::SetState(): item index out of bounds") );
2930 tvi
.state
= INDEXTOSTATEIMAGEMASK(state
);
2932 TreeView_SetItem(GetHwnd(), &tvi
);
2935 int wxTreeCtrl::GetState(const wxTreeItemId
& node
)
2938 tvi
.hItem
= (HTREEITEM
)node
.m_pItem
;
2939 tvi
.mask
= TVIF_STATE
;
2940 tvi
.stateMask
= TVIS_STATEIMAGEMASK
;
2941 TreeView_GetItem(GetHwnd(), &tvi
);
2943 return STATEIMAGEMASKTOINDEX(tvi
.state
);
2946 #endif // wxUSE_TREECTRL