X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/2c544b402610082d2a6193c5e7a364cbfb515942..85d2dec9f022748a678a3534851b8dcf76c56fa8:/src/generic/preferencesg.cpp diff --git a/src/generic/preferencesg.cpp b/src/generic/preferencesg.cpp index 98170cf071..97236af305 100644 --- a/src/generic/preferencesg.cpp +++ b/src/generic/preferencesg.cpp @@ -3,7 +3,6 @@ // Purpose: Implementation of wxPreferencesEditor. // Author: Vaclav Slavik // Created: 2013-02-19 -// RCS-ID: $Id$ // Copyright: (c) 2013 Vaclav Slavik // Licence: wxWindows licence /////////////////////////////////////////////////////////////////////////////// @@ -23,22 +22,29 @@ #pragma hdrstop #endif +#if wxUSE_PREFERENCES_EDITOR + #include "wx/private/preferences.h" #ifndef wxHAS_PREF_EDITOR_NATIVE +#include "wx/app.h" #include "wx/dialog.h" #include "wx/notebook.h" #include "wx/sizer.h" #include "wx/sharedptr.h" #include "wx/scopedptr.h" +#include "wx/scopeguard.h" #include "wx/vector.h" +namespace +{ + class wxGenericPrefsDialog : public wxDialog { public: - wxGenericPrefsDialog(wxWindow *parent) - : wxDialog(parent, wxID_ANY, _("Preferences"), + wxGenericPrefsDialog(wxWindow *parent, const wxString& title) + : wxDialog(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE & ~(wxRESIZE_BORDER | wxMAXIMIZE_BOX | wxMINIMIZE_BOX)) { @@ -65,6 +71,16 @@ public: m_notebook->AddPage(win, page->GetName()); } + int GetSelectedPage() const + { + return m_notebook->GetSelection(); + } + + void SelectPage(int page) + { + m_notebook->ChangeSelection(page); + } + private: wxNotebook *m_notebook; }; @@ -73,6 +89,11 @@ private: class wxGenericPreferencesEditorImplBase : public wxPreferencesEditorImpl { public: + void SetTitle(const wxString& title) + { + m_title = title; + } + virtual void AddPage(wxPreferencesPage* page) { m_pages.push_back(wxSharedPtr(page)); @@ -81,7 +102,15 @@ public: protected: wxGenericPrefsDialog *CreateDialog(wxWindow *parent) { - wxGenericPrefsDialog *dlg = new wxGenericPrefsDialog(parent); + if ( m_title.empty() ) + { + // Use the default title, which should include the application name + // under both MSW and GTK (and OSX uses its own native + // implementation anyhow). + m_title.Printf(_("%s Preferences"), wxTheApp->GetAppDisplayName()); + } + + wxGenericPrefsDialog *dlg = new wxGenericPrefsDialog(parent, m_title); // TODO: Don't create all pages immediately like this, do it on demand // when a page is selected in the notebook (as is done on OS X). @@ -97,12 +126,16 @@ protected: dlg->AddPage(i->get()); } + dlg->Fit(); + return dlg; } typedef wxVector< wxSharedPtr > Pages; Pages m_pages; +private: + wxString m_title; }; @@ -151,35 +184,77 @@ private: wxWeakRef m_win; }; +inline +wxGenericPreferencesEditorImplBase* NewGenericImpl() +{ + return new wxModelessPreferencesEditorImpl; +} + #else // !wxHAS_PREF_EDITOR_MODELESS class wxModalPreferencesEditorImpl : public wxGenericPreferencesEditorImplBase { public: + wxModalPreferencesEditorImpl() + { + m_dlg = NULL; + m_currentPage = -1; + } + virtual void Show(wxWindow* parent) { wxScopedPtr dlg(CreateDialog(parent)); - dlg->Fit(); - dlg->ShowModal(); + + // Store it for Dismiss() but ensure that the pointer is reset to NULL + // when the dialog is destroyed on leaving this function. + m_dlg = dlg.get(); + wxON_BLOCK_EXIT_NULL(m_dlg); + + // Restore the previously selected page, if any. + if ( m_currentPage != -1 ) + dlg->SelectPage(m_currentPage); + + // Don't remember the last selected page if the dialog was cancelled. + if ( dlg->ShowModal() != wxID_CANCEL ) + m_currentPage = dlg->GetSelectedPage(); } virtual void Dismiss() { - // nothing to do + if ( m_dlg ) + { + m_dlg->EndModal(wxID_CANCEL); + m_dlg = NULL; + } } + +private: + wxGenericPrefsDialog* m_dlg; + int m_currentPage; + + wxDECLARE_NO_COPY_CLASS(wxModalPreferencesEditorImpl); }; +inline +wxGenericPreferencesEditorImplBase* NewGenericImpl() +{ + return new wxModalPreferencesEditorImpl; +} + #endif // !wxHAS_PREF_EDITOR_MODELESS +} // anonymous namespace /*static*/ -wxPreferencesEditorImpl* wxPreferencesEditorImpl::Create() +wxPreferencesEditorImpl* wxPreferencesEditorImpl::Create(const wxString& title) { -#ifdef wxHAS_PREF_EDITOR_MODELESS - return new wxModelessPreferencesEditorImpl(); -#else - return new wxModalPreferencesEditorImpl(); -#endif + wxGenericPreferencesEditorImplBase* const impl = NewGenericImpl(); + + impl->SetTitle(title); + + return impl; } #endif // !wxHAS_PREF_EDITOR_NATIVE + +#endif // wxUSE_PREFERENCES_EDITOR