Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wxFileDialog::GetCurrentlySelectedFilterIndex() and wxEVT_UPDATE_UI event notification when the selected file type filter changes #1310

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/wx/filedlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class WXDLLIMPEXP_CORE wxFileDialogBase: public wxDialog
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*);
Expand Down Expand Up @@ -153,6 +156,12 @@ class WXDLLIMPEXP_CORE wxFileDialogBase: public wxDialog
// GetCurrentlySelectedFilename().
wxString m_currentlySelectedFilename;

// Currently selected, but not yet necessarily accepted by the user, file type / 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)
Expand Down
3 changes: 3 additions & 0 deletions include/wx/msw/filedlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class WXDLLIMPEXP_CORE wxFileDialog: public wxFileDialogBase
// 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;
Expand Down
22 changes: 22 additions & 0 deletions interface/wx/filedlg.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,28 @@ class wxFileDialog : public wxDialog
*/
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 under MSW only and
returns an undefined value 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.
*/
Expand Down
18 changes: 16 additions & 2 deletions src/msw/filedlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "wx/scopeguard.h"
#include "wx/tokenzr.h"
#include "wx/modalhook.h"
#include <wx/scopeguard.h>

// ----------------------------------------------------------------------------
// constants
Expand Down Expand Up @@ -185,6 +186,10 @@ wxFileDialogHookFunction(HWND hDlg,
case CDN_SELCHANGE:
dialog->MSWOnSelChange((WXHWND)hDlg);
break;

case CDN_TYPECHANGE:
dialog->MSWOnTypeChange((WXHWND)hDlg, pNotifyCode->lpOFN->nFilterIndex -1);
break;
}
}
}
Expand Down Expand Up @@ -330,7 +335,7 @@ void wxFileDialog::MSWOnInitDone(WXHWND hDlg)
SetPosition(gs_rectDialog.GetPosition());
}

// Call selection change handler so that update handler will be
// Call selection change handlers so that update handlers will be
// called once with no selection.
MSWOnSelChange(hDlg);
}
Expand All @@ -350,6 +355,14 @@ void wxFileDialog::MSWOnSelChange(WXHWND hDlg)
m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE);
}

void wxFileDialog::MSWOnTypeChange(WXHWND hDlg, int nFilterIndex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vadz hDlg causes a warning because it is not used. Could you add WXUNUSED?

{
m_currentlySelectedFilterIndex = nFilterIndex;

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
Expand Down Expand Up @@ -566,6 +579,7 @@ int wxFileDialog::ShowModal()

of.lpstrFilter = filterBuffer.t_str();
of.nFilterIndex = m_filterIndex + 1;
m_currentlySelectedFilterIndex = m_filterIndex;

//=== Setting defaultFileName >>=========================================

Expand Down Expand Up @@ -619,7 +633,7 @@ int wxFileDialog::ShowModal()

if ( !ShowCommFileDialog(&of, m_windowStyle) )
return wxID_CANCEL;

m_fileNames.Empty();

if ( ( HasFdFlag(wxFD_MULTIPLE) ) &&
Expand Down