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
30 #include "wx/dialog.h"
31 #include "wx/notebook.h"
33 #include "wx/sharedptr.h"
34 #include "wx/scopedptr.h"
35 #include "wx/vector.h"
40 class wxGenericPrefsDialog
: public wxDialog
43 wxGenericPrefsDialog(wxWindow
*parent
, const wxString
& title
)
44 : wxDialog(parent
, wxID_ANY
, title
,
45 wxDefaultPosition
, wxDefaultSize
,
46 wxDEFAULT_FRAME_STYLE
& ~(wxRESIZE_BORDER
| wxMAXIMIZE_BOX
| wxMINIMIZE_BOX
))
48 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
50 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
52 m_notebook
= new wxNotebook(this, wxID_ANY
);
53 sizer
->Add(m_notebook
, wxSizerFlags(1).Expand().DoubleBorder());
56 SetEscapeId(wxID_CLOSE
);
57 sizer
->Add(CreateButtonSizer(wxCLOSE
), wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
59 sizer
->Add(CreateButtonSizer(wxOK
| wxCANCEL
),
60 wxSizerFlags().Expand().DoubleBorder(wxLEFT
|wxRIGHT
|wxBOTTOM
));
65 void AddPage(wxPreferencesPage
*page
)
67 wxWindow
*win
= page
->CreateWindow(m_notebook
);
68 m_notebook
->AddPage(win
, page
->GetName());
71 int GetSelectedPage() const
73 return m_notebook
->GetSelection();
76 void SelectPage(int page
)
78 m_notebook
->ChangeSelection(page
);
82 wxNotebook
*m_notebook
;
86 class wxGenericPreferencesEditorImplBase
: public wxPreferencesEditorImpl
89 void SetTitle(const wxString
& title
)
94 virtual void AddPage(wxPreferencesPage
* page
)
96 m_pages
.push_back(wxSharedPtr
<wxPreferencesPage
>(page
));
100 wxGenericPrefsDialog
*CreateDialog(wxWindow
*parent
)
102 if ( m_title
.empty() )
103 m_title
= _("Preferences");
105 wxGenericPrefsDialog
*dlg
= new wxGenericPrefsDialog(parent
, m_title
);
107 // TODO: Don't create all pages immediately like this, do it on demand
108 // when a page is selected in the notebook (as is done on OS X).
110 // Currently, creating all pages is necessary so that the notebook
111 // can determine its best size. We'll need to extend
112 // wxPreferencesPage with a GetBestSize() virtual method to make
113 // it possible to defer the creation.
114 for ( Pages::const_iterator i
= m_pages
.begin();
118 dlg
->AddPage(i
->get());
124 typedef wxVector
< wxSharedPtr
<wxPreferencesPage
> > Pages
;
132 #ifdef wxHAS_PREF_EDITOR_MODELESS
134 class wxModelessPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
137 virtual ~wxModelessPreferencesEditorImpl()
139 // m_win may already be destroyed if this destructor is called from
140 // wxApp's destructor. In that case, all windows -- including this
141 // one -- would already be destroyed by now.
146 virtual void Show(wxWindow
* parent
)
150 wxWindow
*win
= CreateDialog(parent
);
156 // Ideally, we'd reparent the dialog under 'parent', but it's
157 // probably not worth the hassle. We know the old parent is still
158 // valid, because otherwise Dismiss() would have been called and
164 virtual void Dismiss()
168 m_win
->Close(/*force=*/true);
174 wxWeakRef
<wxWindow
> m_win
;
178 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
180 return new wxModelessPreferencesEditorImpl
;
183 #else // !wxHAS_PREF_EDITOR_MODELESS
185 class wxModalPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
188 wxModalPreferencesEditorImpl()
193 virtual void Show(wxWindow
* parent
)
195 wxScopedPtr
<wxGenericPrefsDialog
> dlg(CreateDialog(parent
));
198 // Restore the previously selected page, if any.
199 if ( m_currentPage
!= -1 )
200 dlg
->SelectPage(m_currentPage
);
202 // Don't remember the last selected page if the dialog was cancelled.
203 if ( dlg
->ShowModal() != wxID_CANCEL
)
204 m_currentPage
= dlg
->GetSelectedPage();
207 virtual void Dismiss()
217 wxGenericPreferencesEditorImplBase
* NewGenericImpl()
219 return new wxModalPreferencesEditorImpl
;
222 #endif // !wxHAS_PREF_EDITOR_MODELESS
224 } // anonymous namespace
227 wxPreferencesEditorImpl
* wxPreferencesEditorImpl::Create(const wxString
& title
)
229 wxGenericPreferencesEditorImplBase
* const impl
= NewGenericImpl();
231 impl
->SetTitle(title
);
236 #endif // !wxHAS_PREF_EDITOR_NATIVE