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 #if wxUSE_PREFERENCES_EDITOR
28 #include "wx/private/preferences.h"
30 #ifndef wxHAS_PREF_EDITOR_NATIVE
33 #include "wx/dialog.h"
34 #include "wx/notebook.h"
36 #include "wx/sharedptr.h"
37 #include "wx/scopedptr.h"
38 #include "wx/scopeguard.h"
39 #include "wx/vector.h"
44 class wxGenericPrefsDialog
: public wxDialog
47 wxGenericPrefsDialog(wxWindow
*parent
, const wxString
& title
)
48 : wxDialog(parent
, wxID_ANY
, title
,
49 wxDefaultPosition
, wxDefaultSize
,
50 wxDEFAULT_FRAME_STYLE
& ~(wxRESIZE_BORDER
| wxMAXIMIZE_BOX
| wxMINIMIZE_BOX
))
52 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
54 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
56 m_notebook
= new wxNotebook(this, wxID_ANY
);
57 sizer
->Add(m_notebook
, wxSizerFlags(1).Expand().DoubleBorder());
60 SetEscapeId(wxID_CLOSE
);
61 sizer
->Add(CreateButtonSizer(wxCLOSE
), wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
63 sizer
->Add(CreateButtonSizer(wxOK
| wxCANCEL
),
64 wxSizerFlags().Expand().DoubleBorder(wxLEFT
|wxRIGHT
|wxBOTTOM
));
69 void AddPage(wxPreferencesPage
*page
)
71 wxWindow
*win
= page
->CreateWindow(m_notebook
);
72 m_notebook
->AddPage(win
, page
->GetName());
75 int GetSelectedPage() const
77 return m_notebook
->GetSelection();
80 void SelectPage(int page
)
82 m_notebook
->ChangeSelection(page
);
86 wxNotebook
*m_notebook
;
90 class wxGenericPreferencesEditorImplBase
: public wxPreferencesEditorImpl
93 void SetTitle(const wxString
& title
)
98 virtual void AddPage(wxPreferencesPage
* page
)
100 m_pages
.push_back(wxSharedPtr
<wxPreferencesPage
>(page
));
104 wxGenericPrefsDialog
*CreateDialog(wxWindow
*parent
)
106 if ( m_title
.empty() )
108 // Use the default title, which should include the application name
109 // under both MSW and GTK (and OSX uses its own native
110 // implementation anyhow).
111 m_title
.Printf(_("%s Preferences"), wxTheApp
->GetAppDisplayName());
114 wxGenericPrefsDialog
*dlg
= new wxGenericPrefsDialog(parent
, m_title
);
116 // TODO: Don't create all pages immediately like this, do it on demand
117 // when a page is selected in the notebook (as is done on OS X).
119 // Currently, creating all pages is necessary so that the notebook
120 // can determine its best size. We'll need to extend
121 // wxPreferencesPage with a GetBestSize() virtual method to make
122 // it possible to defer the creation.
123 for ( Pages::const_iterator i
= m_pages
.begin();
127 dlg
->AddPage(i
->get());
135 typedef wxVector
< wxSharedPtr
<wxPreferencesPage
> > Pages
;
143 #ifdef wxHAS_PREF_EDITOR_MODELESS
145 class wxModelessPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
148 virtual ~wxModelessPreferencesEditorImpl()
150 // m_win may already be destroyed if this destructor is called from
151 // wxApp's destructor. In that case, all windows -- including this
152 // one -- would already be destroyed by now.
157 virtual void Show(wxWindow
* parent
)
161 wxWindow
*win
= CreateDialog(parent
);
167 // Ideally, we'd reparent the dialog under 'parent', but it's
168 // probably not worth the hassle. We know the old parent is still
169 // valid, because otherwise Dismiss() would have been called and
175 virtual void Dismiss()
179 m_win
->Close(/*force=*/true);
185 wxWeakRef
<wxWindow
> m_win
;
189 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
191 return new wxModelessPreferencesEditorImpl
;
194 #else // !wxHAS_PREF_EDITOR_MODELESS
196 class wxModalPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
199 wxModalPreferencesEditorImpl()
205 virtual void Show(wxWindow
* parent
)
207 wxScopedPtr
<wxGenericPrefsDialog
> dlg(CreateDialog(parent
));
209 // Store it for Dismiss() but ensure that the pointer is reset to NULL
210 // when the dialog is destroyed on leaving this function.
212 wxON_BLOCK_EXIT_NULL(m_dlg
);
214 // Restore the previously selected page, if any.
215 if ( m_currentPage
!= -1 )
216 dlg
->SelectPage(m_currentPage
);
218 // Don't remember the last selected page if the dialog was cancelled.
219 if ( dlg
->ShowModal() != wxID_CANCEL
)
220 m_currentPage
= dlg
->GetSelectedPage();
223 virtual void Dismiss()
227 m_dlg
->EndModal(wxID_CANCEL
);
233 wxGenericPrefsDialog
* m_dlg
;
236 wxDECLARE_NO_COPY_CLASS(wxModalPreferencesEditorImpl
);
240 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
242 return new wxModalPreferencesEditorImpl
;
245 #endif // !wxHAS_PREF_EDITOR_MODELESS
247 } // anonymous namespace
250 wxPreferencesEditorImpl
* wxPreferencesEditorImpl::Create(const wxString
& title
)
252 wxGenericPreferencesEditorImplBase
* const impl
= NewGenericImpl();
254 impl
->SetTitle(title
);
259 #endif // !wxHAS_PREF_EDITOR_NATIVE
261 #endif // wxUSE_PREFERENCES_EDITOR