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
This commit is contained in:
QuentinC
2019-04-30 21:55:15 +02:00
committed by Vadim Zeitlin
parent 1916442956
commit 413abb066e
5 changed files with 56 additions and 0 deletions

View File

@@ -114,6 +114,9 @@ public:
virtual wxString GetCurrentlySelectedFilename() const virtual wxString GetCurrentlySelectedFilename() const
{ return m_currentlySelectedFilename; } { return m_currentlySelectedFilename; }
virtual int GetCurrentlySelectedFilterIndex () const
{ return m_currentlySelectedFilterIndex; }
// this function is called with wxFileDialog as parameter and should // this function is called with wxFileDialog as parameter and should
// create the window containing the extra controls we want to show in it // create the window containing the extra controls we want to show in it
typedef wxWindow *(*ExtraControlCreatorFunction)(wxWindow*); typedef wxWindow *(*ExtraControlCreatorFunction)(wxWindow*);
@@ -153,6 +156,12 @@ protected:
// GetCurrentlySelectedFilename(). // GetCurrentlySelectedFilename().
wxString m_currentlySelectedFilename; 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; wxWindow* m_extraControl;
// returns true if control is created (if it already exists returns false) // returns true if control is created (if it already exists returns false)

View File

@@ -44,6 +44,9 @@ public:
// called from the hook procedure on CDN_SELCHANGE. // called from the hook procedure on CDN_SELCHANGE.
void MSWOnSelChange(WXHWND hDlg); void MSWOnSelChange(WXHWND hDlg);
// called from the hook procedure on CDN_TYPECHANGE.
void MSWOnTypeChange(WXHWND hDlg, int nFilterIndex);
protected: protected:
virtual void DoMoveWindow(int x, int y, int width, int height) wxOVERRIDE; virtual void DoMoveWindow(int x, int y, int width, int height) wxOVERRIDE;

View File

@@ -210,6 +210,29 @@ public:
*/ */
virtual wxString GetCurrentlySelectedFilename() const; 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. Returns the default directory.
*/ */

View File

@@ -46,6 +46,7 @@ wxIMPLEMENT_DYNAMIC_CLASS(wxFileDialogBase, wxDialog);
void wxFileDialogBase::Init() void wxFileDialogBase::Init()
{ {
m_filterIndex = 0; m_filterIndex = 0;
m_currentlySelectedFilterIndex = wxNOT_FOUND;
m_windowStyle = 0; m_windowStyle = 0;
m_extraControl = NULL; m_extraControl = NULL;
m_extraControlCreator = NULL; m_extraControlCreator = NULL;

View File

@@ -185,6 +185,14 @@ wxFileDialogHookFunction(HWND hDlg,
case CDN_SELCHANGE: case CDN_SELCHANGE:
dialog->MSWOnSelChange((WXHWND)hDlg); dialog->MSWOnSelChange((WXHWND)hDlg);
break; 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); 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 // helper used below in ShowCommFileDialog(): style is used to determine
// whether to show the "Save file" dialog (if it contains wxFD_SAVE bit) or // 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 // "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.lpstrFilter = filterBuffer.t_str();
of.nFilterIndex = m_filterIndex + 1; of.nFilterIndex = m_filterIndex + 1;
m_currentlySelectedFilterIndex = m_filterIndex;
//=== Setting defaultFileName >>========================================= //=== Setting defaultFileName >>=========================================