From: Vadim Zeitlin Date: Thu, 1 Nov 2012 17:15:08 +0000 (+0000) Subject: Added wxEVT_COMMAND_DIRCTRL_CHANGED for wxDirCtrl selection changes. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/84605707d116d42c3fc3dba931786bc55bbdc1d8 Added wxEVT_COMMAND_DIRCTRL_CHANGED for wxDirCtrl selection changes. This makes it much simpler to react to the changes in the control, update the sample to show it. Closes #14792. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@72842 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/changes.txt b/docs/changes.txt index 2eca888372..0d329b6876 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -581,6 +581,7 @@ All (GUI): - Close wxLogWindow automatically if it's the last remaining top level window. - Implement clipping for wxSVGFileDC (Steve Benbow). - Added wxDirCtrl::GetPath() (troelsk). +- Added wxEVT_COMMAND_DIRCTRL_CHANGED event (troelsk). wxGTK: diff --git a/include/wx/generic/dirctrlg.h b/include/wx/generic/dirctrlg.h index 450c8624d7..af6ab5de58 100644 --- a/include/wx/generic/dirctrlg.h +++ b/include/wx/generic/dirctrlg.h @@ -111,6 +111,7 @@ public: void OnCollapseItem(wxTreeEvent &event ); void OnBeginEditItem(wxTreeEvent &event ); void OnEndEditItem(wxTreeEvent &event ); + void OnTreeSelChange(wxTreeEvent &event); void OnSize(wxSizeEvent &event ); // Try to expand as much of the given path as possible. @@ -210,6 +211,13 @@ private: wxDECLARE_NO_COPY_CLASS(wxGenericDirCtrl); }; +wxDECLARE_EXPORTED_EVENT( WXDLLIMPEXP_CORE, wxEVT_COMMAND_DIRCTRL_CHANGED, wxTreeEvent ); + +#define wx__DECLARE_DIRCTRL_EVT(evt, id, fn) \ + wx__DECLARE_EVT1(wxEVT_COMMAND_DIRCTRL_ ## evt, id, wxTreeEventHandler(fn)) + +#define EVT_DIRCTRL_CHANGED(id, fn) wx__DECLARE_DIRCTRL_EVT(CHANGED, id, fn) + //----------------------------------------------------------------------------- // wxDirFilterListCtrl //----------------------------------------------------------------------------- diff --git a/interface/wx/dirctrl.h b/interface/wx/dirctrl.h index 9fe8b21fff..34560ed1e3 100644 --- a/interface/wx/dirctrl.h +++ b/interface/wx/dirctrl.h @@ -47,6 +47,12 @@ enum @library{wxcore} @category{ctrl} @appearance{genericdirctrl.png} + @event{EVT_DIRCTRL_CHANGED(id, func)} + Selected directory has changed. + Processes a @c wxEVT_COMMAND_DIRCTRL_CHANGED event type. + Notice that this event is generated even for the changes done by the + program itself and not only those done by the user. + @since 2.9.5 */ class wxGenericDirCtrl : public wxControl { diff --git a/samples/widgets/dirctrl.cpp b/samples/widgets/dirctrl.cpp index 0698b926de..6a55991d55 100644 --- a/samples/widgets/dirctrl.cpp +++ b/samples/widgets/dirctrl.cpp @@ -112,6 +112,7 @@ protected: void OnStdPath(wxCommandEvent& event); void OnCheckBox(wxCommandEvent& event); void OnRadioBox(wxCommandEvent& event); + void OnSelChanged(wxTreeEvent& event); // reset the control parameters void Reset(); @@ -154,6 +155,7 @@ BEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage) EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath) EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox) EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox) + EVT_DIRCTRL_CHANGED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnSelChanged) END_EVENT_TABLE() // ============================================================================ @@ -168,6 +170,7 @@ DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist) :WidgetsPage(book, imaglist, dirctrl_xpm) { + m_dirCtrl = NULL; } void DirCtrlWidgetsPage::CreateContent() @@ -360,4 +363,15 @@ void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event)) } } +void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent& event) +{ + if ( m_dirCtrl ) + { + wxLogMessage("Selection changed to \"%s\"", + m_dirCtrl->GetPath(event.GetItem())); + } + + event.Skip(); +} + #endif // wxUSE_DIRDLG diff --git a/src/generic/dirctrlg.cpp b/src/generic/dirctrlg.cpp index 4b25f874a2..7eba8af2f9 100644 --- a/src/generic/dirctrlg.cpp +++ b/src/generic/dirctrlg.cpp @@ -102,6 +102,12 @@ extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr[]; bool wxIsDriveAvailable(const wxString& dirName); +// ---------------------------------------------------------------------------- +// events +// ---------------------------------------------------------------------------- + +wxDEFINE_EVENT( wxEVT_COMMAND_DIRCTRL_CHANGED, wxTreeEvent ); + // ---------------------------------------------------------------------------- // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/") // ---------------------------------------------------------------------------- @@ -441,6 +447,7 @@ BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl) EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem) EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem) EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem) + EVT_TREE_SEL_CHANGED (wxID_TREECTRL, wxGenericDirCtrl::OnTreeSelChange) EVT_SIZE (wxGenericDirCtrl::OnSize) END_EVENT_TABLE() @@ -694,6 +701,17 @@ void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event) } } +void wxGenericDirCtrl::OnTreeSelChange(wxTreeEvent &event) +{ + wxTreeEvent changedEvent(wxEVT_COMMAND_DIRCTRL_CHANGED, GetId()); + + changedEvent.SetItem(event.GetItem()); + changedEvent.SetClientObject(m_treeCtrl->GetItemData(event.GetItem())); + + GetEventHandler()->SafelyProcessEvent(changedEvent); + event.Skip(); +} + void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event) { wxTreeItemId parentId = event.GetItem();