From 413abb066ea678c73c6ff1db961fab175c68d479 Mon Sep 17 00:00:00 2001 From: QuentinC Date: Tue, 30 Apr 2019 21:55:15 +0200 Subject: [PATCH] Allow retrieving current filter from wxFileDialog extra controls Add wxFileDialog::GetCurrentlySelectedFilterIndex() similar to the existing GetCurrentlySelectedFilename(), which can be used to retrieve the index of the currently selected filter and update the state of the extra controls in wxFileDialog accordingly. Implement this for wxMSW only by updating the internally stored value from the native CDN_TYPECHANGE notification and also generating a wxEVT_UPDATE_UI event to allow the extra controls to update themselves. Closes https://github.com/wxWidgets/wxWidgets/pull/1310 --- include/wx/filedlg.h | 9 +++++++++ include/wx/msw/filedlg.h | 3 +++ interface/wx/filedlg.h | 23 +++++++++++++++++++++++ src/common/fldlgcmn.cpp | 1 + src/msw/filedlg.cpp | 20 ++++++++++++++++++++ 5 files changed, 56 insertions(+) diff --git a/include/wx/filedlg.h b/include/wx/filedlg.h index b068129ae2..14f667fa68 100644 --- a/include/wx/filedlg.h +++ b/include/wx/filedlg.h @@ -114,6 +114,9 @@ public: virtual wxString GetCurrentlySelectedFilename() const { return m_currentlySelectedFilename; } + virtual int GetCurrentlySelectedFilterIndex () const + { return m_currentlySelectedFilterIndex; } + // this function is called with wxFileDialog as parameter and should // create the window containing the extra controls we want to show in it typedef wxWindow *(*ExtraControlCreatorFunction)(wxWindow*); @@ -153,6 +156,12 @@ protected: // GetCurrentlySelectedFilename(). wxString m_currentlySelectedFilename; + // Currently selected, but not yet necessarily accepted by the user, file + // type (a.k.a. filter) index. This should be updated whenever the + // selection in the control changes by the platform-specific code to + // provide a useful implementation of GetCurrentlySelectedFilterIndex(). + int m_currentlySelectedFilterIndex; + wxWindow* m_extraControl; // returns true if control is created (if it already exists returns false) diff --git a/include/wx/msw/filedlg.h b/include/wx/msw/filedlg.h index 0b3b780d2a..a83192b6e9 100644 --- a/include/wx/msw/filedlg.h +++ b/include/wx/msw/filedlg.h @@ -44,6 +44,9 @@ public: // called from the hook procedure on CDN_SELCHANGE. void MSWOnSelChange(WXHWND hDlg); + // called from the hook procedure on CDN_TYPECHANGE. + void MSWOnTypeChange(WXHWND hDlg, int nFilterIndex); + protected: virtual void DoMoveWindow(int x, int y, int width, int height) wxOVERRIDE; diff --git a/interface/wx/filedlg.h b/interface/wx/filedlg.h index 719ac18b7c..10715cb330 100644 --- a/interface/wx/filedlg.h +++ b/interface/wx/filedlg.h @@ -210,6 +210,29 @@ public: */ virtual wxString GetCurrentlySelectedFilename() const; + /** + Returns the file type filter index currently selected in dialog. + + Notice that this file type filter is not necessarily going to be the + one finally accepted by the user, so calling this function mostly makes + sense from an update UI event handler of a custom file dialog extra + control to update its state depending on the currently selected file + type filter. + + Currently this function is fully implemented only under MSW and + always returns @c wxNOT_FOUND elsewhere. + + @since 3.1.3 + + @return The 0-based index of the currently selected file type filter or + wxNOT_FOUND if nothing is selected. + + @see SetExtraControlCreator() + @see GetFilterIndex() + @see SetFilterIndex() + */ + virtual int GetCurrentlySelectedFilterIndex () const; + /** Returns the default directory. */ diff --git a/src/common/fldlgcmn.cpp b/src/common/fldlgcmn.cpp index 69f28d5320..2084c050ab 100644 --- a/src/common/fldlgcmn.cpp +++ b/src/common/fldlgcmn.cpp @@ -46,6 +46,7 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog); void wxFileDialogBase::Init() { m_filterIndex = 0; + m_currentlySelectedFilterIndex = wxNOT_FOUND; m_windowStyle = 0; m_extraControl = NULL; m_extraControlCreator = NULL; diff --git a/src/msw/filedlg.cpp b/src/msw/filedlg.cpp index 2e96f37d5c..7dc4741a45 100644 --- a/src/msw/filedlg.cpp +++ b/src/msw/filedlg.cpp @@ -185,6 +185,14 @@ wxFileDialogHookFunction(HWND hDlg, case CDN_SELCHANGE: dialog->MSWOnSelChange((WXHWND)hDlg); break; + + case CDN_TYPECHANGE: + dialog->MSWOnTypeChange + ( + (WXHWND)hDlg, + pNotifyCode->lpOFN->nFilterIndex + ); + break; } } } @@ -350,6 +358,17 @@ void wxFileDialog::MSWOnSelChange(WXHWND hDlg) m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE); } +void wxFileDialog::MSWOnTypeChange(WXHWND hDlg, int nFilterIndex) +{ + // Filter indices are 1-based, while we want to use 0-based index, as + // usual. However the input index can apparently also be 0 in some + // circumstances, so take care before decrementing it. + m_currentlySelectedFilterIndex = nFilterIndex ? nFilterIndex - 1 : 0; + + if ( m_extraControl ) + m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE); +} + // helper used below in ShowCommFileDialog(): style is used to determine // whether to show the "Save file" dialog (if it contains wxFD_SAVE bit) or // "Open file" one; returns true on success or false on failure in which case @@ -566,6 +585,7 @@ int wxFileDialog::ShowModal() of.lpstrFilter = filterBuffer.t_str(); of.nFilterIndex = m_filterIndex + 1; + m_currentlySelectedFilterIndex = m_filterIndex; //=== Setting defaultFileName >>=========================================