1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/preferencesg.cpp
3 // Purpose: Implementation of wxPreferencesEditor.
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/private/preferences.h"
28 #ifndef wxHAS_PREF_EDITOR_NATIVE
31 #include "wx/dialog.h"
32 #include "wx/notebook.h"
34 #include "wx/sharedptr.h"
35 #include "wx/scopedptr.h"
36 #include "wx/scopeguard.h"
37 #include "wx/vector.h"
42 class wxGenericPrefsDialog
: public wxDialog
45 wxGenericPrefsDialog(wxWindow
*parent
, const wxString
& title
)
46 : wxDialog(parent
, wxID_ANY
, title
,
47 wxDefaultPosition
, wxDefaultSize
,
48 wxDEFAULT_FRAME_STYLE
& ~(wxRESIZE_BORDER
| wxMAXIMIZE_BOX
| wxMINIMIZE_BOX
))
50 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
52 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
54 m_notebook
= new wxNotebook(this, wxID_ANY
);
55 sizer
->Add(m_notebook
, wxSizerFlags(1).Expand().DoubleBorder());
58 SetEscapeId(wxID_CLOSE
);
59 sizer
->Add(CreateButtonSizer(wxCLOSE
), wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
61 sizer
->Add(CreateButtonSizer(wxOK
| wxCANCEL
),
62 wxSizerFlags().Expand().DoubleBorder(wxLEFT
|wxRIGHT
|wxBOTTOM
));
67 void AddPage(wxPreferencesPage
*page
)
69 wxWindow
*win
= page
->CreateWindow(m_notebook
);
70 m_notebook
->AddPage(win
, page
->GetName());
73 int GetSelectedPage() const
75 return m_notebook
->GetSelection();
78 void SelectPage(int page
)
80 m_notebook
->ChangeSelection(page
);
84 wxNotebook
*m_notebook
;
88 class wxGenericPreferencesEditorImplBase
: public wxPreferencesEditorImpl
91 void SetTitle(const wxString
& title
)
96 virtual void AddPage(wxPreferencesPage
* page
)
98 m_pages
.push_back(wxSharedPtr
<wxPreferencesPage
>(page
));
102 wxGenericPrefsDialog
*CreateDialog(wxWindow
*parent
)
104 if ( m_title
.empty() )
106 // Use the default title, which should include the application name
107 // under both MSW and GTK (and OSX uses its own native
108 // implementation anyhow).
109 m_title
.Printf(_("%s Preferences"), wxTheApp
->GetAppDisplayName());
112 wxGenericPrefsDialog
*dlg
= new wxGenericPrefsDialog(parent
, m_title
);
114 // TODO: Don't create all pages immediately like this, do it on demand
115 // when a page is selected in the notebook (as is done on OS X).
117 // Currently, creating all pages is necessary so that the notebook
118 // can determine its best size. We'll need to extend
119 // wxPreferencesPage with a GetBestSize() virtual method to make
120 // it possible to defer the creation.
121 for ( Pages::const_iterator i
= m_pages
.begin();
125 dlg
->AddPage(i
->get());
133 typedef wxVector
< wxSharedPtr
<wxPreferencesPage
> > Pages
;
141 #ifdef wxHAS_PREF_EDITOR_MODELESS
143 class wxModelessPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
146 virtual ~wxModelessPreferencesEditorImpl()
148 // m_win may already be destroyed if this destructor is called from
149 // wxApp's destructor. In that case, all windows -- including this
150 // one -- would already be destroyed by now.
155 virtual void Show(wxWindow
* parent
)
159 wxWindow
*win
= CreateDialog(parent
);
165 // Ideally, we'd reparent the dialog under 'parent', but it's
166 // probably not worth the hassle. We know the old parent is still
167 // valid, because otherwise Dismiss() would have been called and
173 virtual void Dismiss()
177 m_win
->Close(/*force=*/true);
183 wxWeakRef
<wxWindow
> m_win
;
187 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
189 return new wxModelessPreferencesEditorImpl
;
192 #else // !wxHAS_PREF_EDITOR_MODELESS
194 class wxModalPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
197 wxModalPreferencesEditorImpl()
203 virtual void Show(wxWindow
* parent
)
205 wxScopedPtr
<wxGenericPrefsDialog
> dlg(CreateDialog(parent
));
207 // Store it for Dismiss() but ensure that the pointer is reset to NULL
208 // when the dialog is destroyed on leaving this function.
210 wxON_BLOCK_EXIT_NULL(m_dlg
);
212 // Restore the previously selected page, if any.
213 if ( m_currentPage
!= -1 )
214 dlg
->SelectPage(m_currentPage
);
216 // Don't remember the last selected page if the dialog was cancelled.
217 if ( dlg
->ShowModal() != wxID_CANCEL
)
218 m_currentPage
= dlg
->GetSelectedPage();
221 virtual void Dismiss()
225 m_dlg
->EndModal(wxID_CANCEL
);
231 wxGenericPrefsDialog
* m_dlg
;
234 wxDECLARE_NO_COPY_CLASS(wxModalPreferencesEditorImpl
);
238 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
240 return new wxModalPreferencesEditorImpl
;
243 #endif // !wxHAS_PREF_EDITOR_MODELESS
245 } // anonymous namespace
248 wxPreferencesEditorImpl
* wxPreferencesEditorImpl::Create(const wxString
& title
)
250 wxGenericPreferencesEditorImplBase
* const impl
= NewGenericImpl();
252 impl
->SetTitle(title
);
257 #endif // !wxHAS_PREF_EDITOR_NATIVE