]> git.saurik.com Git - wxWidgets.git/blob - src/generic/preferencesg.cpp
Return raw text, not markup, from wxWebViewWebKit::GetSelectedText().
[wxWidgets.git] / src / generic / preferencesg.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/preferencesg.cpp
3 // Purpose: Implementation of wxPreferencesEditor.
4 // Author: Vaclav Slavik
5 // Created: 2013-02-19
6 // RCS-ID: $Id$
7 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_PREFERENCES_EDITOR
27
28 #include "wx/private/preferences.h"
29
30 #ifndef wxHAS_PREF_EDITOR_NATIVE
31
32 #include "wx/app.h"
33 #include "wx/dialog.h"
34 #include "wx/notebook.h"
35 #include "wx/sizer.h"
36 #include "wx/sharedptr.h"
37 #include "wx/scopedptr.h"
38 #include "wx/scopeguard.h"
39 #include "wx/vector.h"
40
41 namespace
42 {
43
44 class wxGenericPrefsDialog : public wxDialog
45 {
46 public:
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))
51 {
52 SetExtraStyle(wxWS_EX_VALIDATE_RECURSIVELY);
53
54 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
55
56 m_notebook = new wxNotebook(this, wxID_ANY);
57 sizer->Add(m_notebook, wxSizerFlags(1).Expand().DoubleBorder());
58
59 #ifdef __WXGTK__
60 SetEscapeId(wxID_CLOSE);
61 sizer->Add(CreateButtonSizer(wxCLOSE), wxSizerFlags().Expand().DoubleBorder(wxBOTTOM));
62 #else
63 sizer->Add(CreateButtonSizer(wxOK | wxCANCEL),
64 wxSizerFlags().Expand().DoubleBorder(wxLEFT|wxRIGHT|wxBOTTOM));
65 #endif
66 SetSizer(sizer);
67 }
68
69 void AddPage(wxPreferencesPage *page)
70 {
71 wxWindow *win = page->CreateWindow(m_notebook);
72 m_notebook->AddPage(win, page->GetName());
73 }
74
75 int GetSelectedPage() const
76 {
77 return m_notebook->GetSelection();
78 }
79
80 void SelectPage(int page)
81 {
82 m_notebook->ChangeSelection(page);
83 }
84
85 private:
86 wxNotebook *m_notebook;
87 };
88
89
90 class wxGenericPreferencesEditorImplBase : public wxPreferencesEditorImpl
91 {
92 public:
93 void SetTitle(const wxString& title)
94 {
95 m_title = title;
96 }
97
98 virtual void AddPage(wxPreferencesPage* page)
99 {
100 m_pages.push_back(wxSharedPtr<wxPreferencesPage>(page));
101 }
102
103 protected:
104 wxGenericPrefsDialog *CreateDialog(wxWindow *parent)
105 {
106 if ( m_title.empty() )
107 {
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());
112 }
113
114 wxGenericPrefsDialog *dlg = new wxGenericPrefsDialog(parent, m_title);
115
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).
118 //
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();
124 i != m_pages.end();
125 ++i )
126 {
127 dlg->AddPage(i->get());
128 }
129
130 dlg->Fit();
131
132 return dlg;
133 }
134
135 typedef wxVector< wxSharedPtr<wxPreferencesPage> > Pages;
136 Pages m_pages;
137
138 private:
139 wxString m_title;
140 };
141
142
143 #ifdef wxHAS_PREF_EDITOR_MODELESS
144
145 class wxModelessPreferencesEditorImpl : public wxGenericPreferencesEditorImplBase
146 {
147 public:
148 virtual ~wxModelessPreferencesEditorImpl()
149 {
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.
153 if ( m_win )
154 m_win->Destroy();
155 }
156
157 virtual void Show(wxWindow* parent)
158 {
159 if ( !m_win )
160 {
161 wxWindow *win = CreateDialog(parent);
162 win->Show();
163 m_win = win;
164 }
165 else
166 {
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
170 // m_win cleared.
171 m_win->Raise();
172 }
173 }
174
175 virtual void Dismiss()
176 {
177 if ( m_win )
178 {
179 m_win->Close(/*force=*/true);
180 m_win = NULL;
181 }
182 }
183
184 private:
185 wxWeakRef<wxWindow> m_win;
186 };
187
188 inline
189 wxGenericPreferencesEditorImplBase* NewGenericImpl()
190 {
191 return new wxModelessPreferencesEditorImpl;
192 }
193
194 #else // !wxHAS_PREF_EDITOR_MODELESS
195
196 class wxModalPreferencesEditorImpl : public wxGenericPreferencesEditorImplBase
197 {
198 public:
199 wxModalPreferencesEditorImpl()
200 {
201 m_dlg = NULL;
202 m_currentPage = -1;
203 }
204
205 virtual void Show(wxWindow* parent)
206 {
207 wxScopedPtr<wxGenericPrefsDialog> dlg(CreateDialog(parent));
208
209 // Store it for Dismiss() but ensure that the pointer is reset to NULL
210 // when the dialog is destroyed on leaving this function.
211 m_dlg = dlg.get();
212 wxON_BLOCK_EXIT_NULL(m_dlg);
213
214 // Restore the previously selected page, if any.
215 if ( m_currentPage != -1 )
216 dlg->SelectPage(m_currentPage);
217
218 // Don't remember the last selected page if the dialog was cancelled.
219 if ( dlg->ShowModal() != wxID_CANCEL )
220 m_currentPage = dlg->GetSelectedPage();
221 }
222
223 virtual void Dismiss()
224 {
225 if ( m_dlg )
226 {
227 m_dlg->EndModal(wxID_CANCEL);
228 m_dlg = NULL;
229 }
230 }
231
232 private:
233 wxGenericPrefsDialog* m_dlg;
234 int m_currentPage;
235
236 wxDECLARE_NO_COPY_CLASS(wxModalPreferencesEditorImpl);
237 };
238
239 inline
240 wxGenericPreferencesEditorImplBase* NewGenericImpl()
241 {
242 return new wxModalPreferencesEditorImpl;
243 }
244
245 #endif // !wxHAS_PREF_EDITOR_MODELESS
246
247 } // anonymous namespace
248
249 /*static*/
250 wxPreferencesEditorImpl* wxPreferencesEditorImpl::Create(const wxString& title)
251 {
252 wxGenericPreferencesEditorImplBase* const impl = NewGenericImpl();
253
254 impl->SetTitle(title);
255
256 return impl;
257 }
258
259 #endif // !wxHAS_PREF_EDITOR_NATIVE
260
261 #endif // wxUSE_PREFERENCES_EDITOR