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/vector.h"
41 class wxGenericPrefsDialog
: public wxDialog
44 wxGenericPrefsDialog(wxWindow
*parent
, const wxString
& title
)
45 : wxDialog(parent
, wxID_ANY
, title
,
46 wxDefaultPosition
, wxDefaultSize
,
47 wxDEFAULT_FRAME_STYLE
& ~(wxRESIZE_BORDER
| wxMAXIMIZE_BOX
| wxMINIMIZE_BOX
))
49 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
51 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
53 m_notebook
= new wxNotebook(this, wxID_ANY
);
54 sizer
->Add(m_notebook
, wxSizerFlags(1).Expand().DoubleBorder());
57 SetEscapeId(wxID_CLOSE
);
58 sizer
->Add(CreateButtonSizer(wxCLOSE
), wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
60 sizer
->Add(CreateButtonSizer(wxOK
| wxCANCEL
),
61 wxSizerFlags().Expand().DoubleBorder(wxLEFT
|wxRIGHT
|wxBOTTOM
));
66 void AddPage(wxPreferencesPage
*page
)
68 wxWindow
*win
= page
->CreateWindow(m_notebook
);
69 m_notebook
->AddPage(win
, page
->GetName());
72 int GetSelectedPage() const
74 return m_notebook
->GetSelection();
77 void SelectPage(int page
)
79 m_notebook
->ChangeSelection(page
);
83 wxNotebook
*m_notebook
;
87 class wxGenericPreferencesEditorImplBase
: public wxPreferencesEditorImpl
90 void SetTitle(const wxString
& title
)
95 virtual void AddPage(wxPreferencesPage
* page
)
97 m_pages
.push_back(wxSharedPtr
<wxPreferencesPage
>(page
));
101 wxGenericPrefsDialog
*CreateDialog(wxWindow
*parent
)
103 if ( m_title
.empty() )
105 // Use the default title, which should include the application name
106 // under both MSW and GTK (and OSX uses its own native
107 // implementation anyhow).
108 m_title
.Printf(_("%s Preferences"), wxTheApp
->GetAppDisplayName());
111 wxGenericPrefsDialog
*dlg
= new wxGenericPrefsDialog(parent
, m_title
);
113 // TODO: Don't create all pages immediately like this, do it on demand
114 // when a page is selected in the notebook (as is done on OS X).
116 // Currently, creating all pages is necessary so that the notebook
117 // can determine its best size. We'll need to extend
118 // wxPreferencesPage with a GetBestSize() virtual method to make
119 // it possible to defer the creation.
120 for ( Pages::const_iterator i
= m_pages
.begin();
124 dlg
->AddPage(i
->get());
130 typedef wxVector
< wxSharedPtr
<wxPreferencesPage
> > Pages
;
138 #ifdef wxHAS_PREF_EDITOR_MODELESS
140 class wxModelessPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
143 virtual ~wxModelessPreferencesEditorImpl()
145 // m_win may already be destroyed if this destructor is called from
146 // wxApp's destructor. In that case, all windows -- including this
147 // one -- would already be destroyed by now.
152 virtual void Show(wxWindow
* parent
)
156 wxWindow
*win
= CreateDialog(parent
);
162 // Ideally, we'd reparent the dialog under 'parent', but it's
163 // probably not worth the hassle. We know the old parent is still
164 // valid, because otherwise Dismiss() would have been called and
170 virtual void Dismiss()
174 m_win
->Close(/*force=*/true);
180 wxWeakRef
<wxWindow
> m_win
;
184 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
186 return new wxModelessPreferencesEditorImpl
;
189 #else // !wxHAS_PREF_EDITOR_MODELESS
191 class wxModalPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
194 wxModalPreferencesEditorImpl()
199 virtual void Show(wxWindow
* parent
)
201 wxScopedPtr
<wxGenericPrefsDialog
> dlg(CreateDialog(parent
));
204 // Restore the previously selected page, if any.
205 if ( m_currentPage
!= -1 )
206 dlg
->SelectPage(m_currentPage
);
208 // Don't remember the last selected page if the dialog was cancelled.
209 if ( dlg
->ShowModal() != wxID_CANCEL
)
210 m_currentPage
= dlg
->GetSelectedPage();
213 virtual void Dismiss()
223 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
225 return new wxModalPreferencesEditorImpl
;
228 #endif // !wxHAS_PREF_EDITOR_MODELESS
230 } // anonymous namespace
233 wxPreferencesEditorImpl
* wxPreferencesEditorImpl::Create(const wxString
& title
)
235 wxGenericPreferencesEditorImplBase
* const impl
= NewGenericImpl();
237 impl
->SetTitle(title
);
242 #endif // !wxHAS_PREF_EDITOR_NATIVE