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"
37 class wxGenericPrefsDialog
: public wxDialog
40 wxGenericPrefsDialog(wxWindow
*parent
)
41 : wxDialog(parent
, wxID_ANY
, _("Preferences"),
42 wxDefaultPosition
, wxDefaultSize
,
43 wxDEFAULT_FRAME_STYLE
& ~(wxRESIZE_BORDER
| wxMAXIMIZE_BOX
| wxMINIMIZE_BOX
))
45 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY
);
47 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
49 m_notebook
= new wxNotebook(this, wxID_ANY
);
50 sizer
->Add(m_notebook
, wxSizerFlags(1).Expand().DoubleBorder());
53 SetEscapeId(wxID_CLOSE
);
54 sizer
->Add(CreateButtonSizer(wxCLOSE
), wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
56 sizer
->Add(CreateButtonSizer(wxOK
| wxCANCEL
),
57 wxSizerFlags().Expand().DoubleBorder(wxLEFT
|wxRIGHT
|wxBOTTOM
));
62 void AddPage(wxPreferencesPage
*page
)
64 wxWindow
*win
= page
->CreateWindow(m_notebook
);
65 m_notebook
->AddPage(win
, page
->GetName());
69 wxNotebook
*m_notebook
;
73 class wxGenericPreferencesEditorImplBase
: public wxPreferencesEditorImpl
76 virtual void AddPage(wxPreferencesPage
* page
)
78 m_pages
.push_back(wxSharedPtr
<wxPreferencesPage
>(page
));
82 virtual wxDialog
*CreateWindow(wxWindow
*parent
)
84 wxGenericPrefsDialog
*dlg
= new wxGenericPrefsDialog(parent
);
86 // TODO: Don't create all pages immediately like this, do it on demand
87 // when a page is selected in the notebook (as is done on OS X).
89 // Currently, creating all pages is necessary so that the notebook
90 // can determine its best size. We'll need to extend
91 // wxPreferencesPage with a GetBestSize() virtual method to make
92 // it possible to defer the creation.
93 for ( Pages::const_iterator i
= m_pages
.begin();
97 dlg
->AddPage(i
->get());
103 typedef wxVector
< wxSharedPtr
<wxPreferencesPage
> > Pages
;
109 #ifdef wxHAS_PREF_EDITOR_MODELESS
111 class wxModelessPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
114 virtual ~wxModelessPreferencesEditorImpl()
116 // m_win may already be destroyed if this destructor is called from
117 // wxApp's destructor. In that case, all windows -- including this
118 // one -- would already be destroyed by now.
123 virtual void Show(wxWindow
* parent
)
127 wxWindow
*win
= CreateWindow(parent
);
133 // Ideally, we'd reparent the dialog under 'parent', but it's
134 // probably not worth the hassle. We know the old parent is still
135 // valid, because otherwise Dismiss() would have been called and
141 virtual void Dismiss()
145 m_win
->Close(/*force=*/true);
151 wxWeakRef
<wxWindow
> m_win
;
154 #else // !wxHAS_PREF_EDITOR_MODELESS
156 class wxModalPreferencesEditorImpl
: public wxGenericPreferencesEditorImplBase
159 virtual void Show(wxWindow
* parent
)
161 wxScopedPtr
<wxDialog
> dlg(CreateWindow(parent
));
166 virtual void Dismiss()
172 #endif // !wxHAS_PREF_EDITOR_MODELESS
176 wxPreferencesEditorImpl
* wxPreferencesEditorImpl::Create()
178 #ifdef wxHAS_PREF_EDITOR_MODELESS
179 return new wxModelessPreferencesEditorImpl();
181 return new wxModalPreferencesEditorImpl();
185 #endif // !wxHAS_PREF_EDITOR_NATIVE