Merge branch 'mac-filedlg-improvements' of https://github.com/discnl/wxWidgets

macOS wxFileDialog improvements.

See https://github.com/wxWidgets/wxWidgets/pull/2234
This commit is contained in:
Vadim Zeitlin
2021-03-06 12:18:43 +01:00
8 changed files with 77 additions and 19 deletions

View File

@@ -181,6 +181,9 @@ protected:
{ return m_extraControlCreator != NULL; } { return m_extraControlCreator != NULL; }
// get the size of the extra control by creating and deleting it // get the size of the extra control by creating and deleting it
wxSize GetExtraControlSize(); wxSize GetExtraControlSize();
// Helper function for native file dialog usage where no wx events
// are processed.
void UpdateExtraControlUI();
private: private:
ExtraControlCreatorFunction m_extraControlCreator; ExtraControlCreatorFunction m_extraControlCreator;

View File

@@ -236,10 +236,11 @@ public:
control to update its state depending on the currently selected file control to update its state depending on the currently selected file
type filter. type filter.
Currently this function is fully implemented only under MSW and Currently this function is fully implemented under macOS and MSW and
always returns @c wxNOT_FOUND elsewhere. always returns @c wxNOT_FOUND elsewhere.
@since 3.1.3 @since 3.1.3 - MSW
@since 3.1.5 - macOS
@return The 0-based index of the currently selected file type filter or @return The 0-based index of the currently selected file type filter or
wxNOT_FOUND if nothing is selected. wxNOT_FOUND if nothing is selected.

View File

@@ -203,6 +203,7 @@ wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(DIALOGS_FILES_OPEN_WINDOW_MODAL, MyFrame::FilesOpenWindowModal) EVT_MENU(DIALOGS_FILES_OPEN_WINDOW_MODAL, MyFrame::FilesOpenWindowModal)
EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave) EVT_MENU(DIALOGS_FILE_SAVE, MyFrame::FileSave)
EVT_MENU(DIALOGS_FILE_SAVE_WINDOW_MODAL, MyFrame::FileSaveWindowModal) EVT_MENU(DIALOGS_FILE_SAVE_WINDOW_MODAL, MyFrame::FileSaveWindowModal)
EVT_MENU(DIALOGS_MAC_TOGGLE_ALWAYS_SHOW_TYPES, MyFrame::MacToggleAlwaysShowTypes)
#endif // wxUSE_FILEDLG #endif // wxUSE_FILEDLG
#if USE_FILEDLG_GENERIC #if USE_FILEDLG_GENERIC
@@ -504,6 +505,13 @@ bool MyApp::OnInit()
filedlg_menu->Append(DIALOGS_FILE_SAVE_GENERIC, "Sa&ve file (generic)"); filedlg_menu->Append(DIALOGS_FILE_SAVE_GENERIC, "Sa&ve file (generic)");
#endif // USE_FILEDLG_GENERIC #endif // USE_FILEDLG_GENERIC
#ifdef __WXOSX_COCOA__
filedlg_menu->AppendSeparator();
filedlg_menu->AppendCheckItem(DIALOGS_MAC_TOGGLE_ALWAYS_SHOW_TYPES,
"macOS only: Toggle open file "
"\"Always show types\"\tRawCtrl+Ctrl+S");
#endif
menuDlg->Append(wxID_ANY,"&File operations",filedlg_menu); menuDlg->Append(wxID_ANY,"&File operations",filedlg_menu);
#endif // wxUSE_FILEDLG #endif // wxUSE_FILEDLG
@@ -1853,6 +1861,16 @@ void MyFrame::FileSaveWindowModalClosed(wxWindowModalDialogEvent& event)
#endif // wxUSE_FILEDLG #endif // wxUSE_FILEDLG
void MyFrame::MacToggleAlwaysShowTypes(wxCommandEvent& event)
{
#ifdef wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES
wxSystemOptions::SetOption(wxOSX_FILEDIALOG_ALWAYS_SHOW_TYPES,
event.IsChecked());
#else
wxUnusedVar(event);
#endif
}
#if USE_FILEDLG_GENERIC #if USE_FILEDLG_GENERIC
void MyFrame::FileOpenGeneric(wxCommandEvent& WXUNUSED(event) ) void MyFrame::FileOpenGeneric(wxCommandEvent& WXUNUSED(event) )
{ {

View File

@@ -433,6 +433,7 @@ public:
void FileSave(wxCommandEvent& event); void FileSave(wxCommandEvent& event);
void FileSaveWindowModal(wxCommandEvent& event); void FileSaveWindowModal(wxCommandEvent& event);
void FileSaveWindowModalClosed(wxWindowModalDialogEvent& event); void FileSaveWindowModalClosed(wxWindowModalDialogEvent& event);
void MacToggleAlwaysShowTypes(wxCommandEvent& event);
#endif // wxUSE_FILEDLG #endif // wxUSE_FILEDLG
#if USE_FILEDLG_GENERIC #if USE_FILEDLG_GENERIC
@@ -616,6 +617,7 @@ enum
DIALOGS_FILE_OPEN_GENERIC, DIALOGS_FILE_OPEN_GENERIC,
DIALOGS_FILES_OPEN_GENERIC, DIALOGS_FILES_OPEN_GENERIC,
DIALOGS_FILE_SAVE_GENERIC, DIALOGS_FILE_SAVE_GENERIC,
DIALOGS_MAC_TOGGLE_ALWAYS_SHOW_TYPES,
DIALOGS_DIR_CHOOSE, DIALOGS_DIR_CHOOSE,
DIALOGS_DIR_CHOOSE_WINDOW_MODAL, DIALOGS_DIR_CHOOSE_WINDOW_MODAL,
DIALOGS_DIRNEW_CHOOSE, DIALOGS_DIRNEW_CHOOSE,

View File

@@ -192,6 +192,12 @@ wxSize wxFileDialogBase::GetExtraControlSize()
return (*m_extraControlCreator)(&dlg)->GetSize(); return (*m_extraControlCreator)(&dlg)->GetSize();
} }
void wxFileDialogBase::UpdateExtraControlUI()
{
if ( m_extraControl )
m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE);
}
void wxFileDialogBase::SetPath(const wxString& path) void wxFileDialogBase::SetPath(const wxString& path)
{ {
wxString ext; wxString ext;

View File

@@ -496,8 +496,7 @@ void wxFileDialog::GTKSelectionChanged(const wxString& filename)
{ {
m_currentlySelectedFilename = filename; m_currentlySelectedFilename = filename;
if (m_extraControl) UpdateExtraControlUI();
m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE);
} }
#endif // wxUSE_FILEDLG #endif // wxUSE_FILEDLG

View File

@@ -353,8 +353,7 @@ void wxFileDialog::MSWOnSelChange(WXHWND hDlg)
else else
m_currentlySelectedFilename.clear(); m_currentlySelectedFilename.clear();
if ( m_extraControl ) UpdateExtraControlUI();
m_extraControl->UpdateWindowUI(wxUPDATE_UI_RECURSE);
} }
void wxFileDialog::MSWOnTypeChange(WXHWND WXUNUSED(hDlg), int nFilterIndex) void wxFileDialog::MSWOnTypeChange(WXHWND WXUNUSED(hDlg), int nFilterIndex)
@@ -364,8 +363,7 @@ void wxFileDialog::MSWOnTypeChange(WXHWND WXUNUSED(hDlg), int nFilterIndex)
// circumstances, so take care before decrementing it. // circumstances, so take care before decrementing it.
m_currentlySelectedFilterIndex = nFilterIndex ? nFilterIndex - 1 : 0; m_currentlySelectedFilterIndex = nFilterIndex ? nFilterIndex - 1 : 0;
if ( m_extraControl ) UpdateExtraControlUI();
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

View File

@@ -246,25 +246,36 @@ void wxFileDialog::ShowWindowModal()
} }
// Create a panel with the file type drop down list // Fill a new or existing panel with the file type drop down list.
// If extra controls need to be added (see wxFileDialog::SetExtraControlCreator), add // If extra controls need to be added (see wxFileDialog::SetExtraControlCreator),
// them to the panel as well // use that as a panel if possible, otherwise add them to a new panel.
// Returns the newly created wxPanel // Returns a newly created wxPanel or extracontrol if it's suitable as a filter
// panel.
wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol) wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol)
{ {
wxPanel *extrapanel = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize); // Try to use extracontrol as a filter panel instead of creating a new one
// and then reparenting extracontrol. Reparenting is less desired as user
// code may expect the parent to be a wxFileDialog as on other platforms.
const bool useExtraControlAsPanel = extracontrol &&
wxDynamicCast(extracontrol, wxPanel) != NULL;
wxWindow* extrapanel = useExtraControlAsPanel
? extracontrol
: static_cast<wxWindow*>(new wxPanel(this));
wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL); wxBoxSizer *verticalSizer = new wxBoxSizer(wxVERTICAL);
extrapanel->SetSizer(verticalSizer);
// the file type control // the file type control
{ {
wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL); wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0); verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
horizontalSizer->AddStretchSpacer();
wxStaticText *stattext = new wxStaticText( extrapanel, wxID_ANY, _("File type:") ); wxStaticText *stattext = new wxStaticText( extrapanel, wxID_ANY, _("File type:") );
horizontalSizer->Add(stattext, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5); horizontalSizer->Add(stattext, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
m_filterChoice = new wxChoice(extrapanel, wxID_ANY); m_filterChoice = new wxChoice(extrapanel, wxID_ANY);
horizontalSizer->Add(m_filterChoice, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5); horizontalSizer->Add(m_filterChoice, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);
horizontalSizer->AddStretchSpacer();
m_filterChoice->Append(m_filterNames); m_filterChoice->Append(m_filterNames);
if( m_filterNames.GetCount() > 0) if( m_filterNames.GetCount() > 0)
{ {
@@ -276,13 +287,29 @@ wxWindow* wxFileDialog::CreateFilterPanel(wxWindow *extracontrol)
if(extracontrol) if(extracontrol)
{ {
wxBoxSizer *horizontalSizer = new wxBoxSizer(wxHORIZONTAL); // Either use an extra control's existing sizer or the extra control
verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0); // itself, to be in the vertical sizer.
extracontrol->Reparent(extrapanel); wxSizer* existingSizer = extracontrol->GetSizer();
horizontalSizer->Add(extracontrol); if ( useExtraControlAsPanel && existingSizer )
{
// Move extra control's sizer to verticalSizer.
extracontrol->SetSizer(NULL, /* deleteOld = */ false);
verticalSizer->Add(existingSizer);
}
else
{
wxBoxSizer* horizontalSizer = new wxBoxSizer(wxHORIZONTAL);
verticalSizer->Add(horizontalSizer, 0, wxEXPAND, 0);
if ( !useExtraControlAsPanel )
extracontrol->Reparent(extrapanel);
horizontalSizer->Add(extracontrol);
}
} }
extrapanel->SetSizer(verticalSizer);
verticalSizer->Layout(); verticalSizer->Layout();
verticalSizer->SetSizeHints(extrapanel); verticalSizer->SetSizeHints(extrapanel);
return extrapanel; return extrapanel;
@@ -293,6 +320,10 @@ void wxFileDialog::DoOnFilterSelected(int index)
NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions); NSArray* types = GetTypesFromExtension(m_filterExtensions[index],m_currentExtensions);
NSSavePanel* panel = (NSSavePanel*) GetWXWindow(); NSSavePanel* panel = (NSSavePanel*) GetWXWindow();
[panel setAllowedFileTypes:types]; [panel setAllowedFileTypes:types];
m_currentlySelectedFilterIndex = index;
UpdateExtraControlUI();
} }
// An item has been selected in the file filter wxChoice: // An item has been selected in the file filter wxChoice: