Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / samples / preferences / preferences.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/preferences/preferences.cpp
3 // Purpose: Sample demonstrating wxPreferencesEditor use.
4 // Author: Vaclav Slavik
5 // Created: 2013-02-19
6 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
9
10
11 #include "wx/wxprec.h"
12
13 #ifdef __BORLANDC__
14 #pragma hdrstop
15 #endif
16
17 #include "wx/preferences.h"
18
19 #include "wx/app.h"
20 #include "wx/config.h"
21 #include "wx/panel.h"
22 #include "wx/scopedptr.h"
23 #include "wx/menu.h"
24 #include "wx/checkbox.h"
25 #include "wx/listbox.h"
26 #include "wx/stattext.h"
27 #include "wx/sizer.h"
28 #include "wx/artprov.h"
29 #include "wx/frame.h"
30
31 class MyApp : public wxApp
32 {
33 public:
34 virtual bool OnInit();
35
36 void ShowPreferencesEditor(wxWindow* parent);
37 void DismissPreferencesEditor();
38
39 private:
40 wxScopedPtr<wxPreferencesEditor> m_prefEditor;
41 };
42
43 IMPLEMENT_APP(MyApp)
44
45
46 class MyFrame : public wxFrame
47 {
48 public:
49 MyFrame() : wxFrame(NULL, wxID_ANY, "Preferences sample")
50 {
51 wxMenu *fileMenu = new wxMenu;
52 fileMenu->Append(wxID_PREFERENCES);
53 fileMenu->Append(wxID_EXIT);
54
55 wxMenuBar *menuBar = new wxMenuBar();
56 menuBar->Append(fileMenu, "&File");
57 SetMenuBar(menuBar);
58
59 Connect(wxID_PREFERENCES,
60 wxEVT_MENU,
61 wxCommandEventHandler(MyFrame::OnPref), NULL, this);
62 Connect(wxID_EXIT,
63 wxEVT_MENU,
64 wxCommandEventHandler(MyFrame::OnExit), NULL, this);
65 Connect(wxEVT_CLOSE_WINDOW,
66 wxCloseEventHandler(MyFrame::OnClose), NULL, this);
67 }
68
69 private:
70 void OnPref(wxCommandEvent&)
71 {
72 wxGetApp().ShowPreferencesEditor(this);
73 }
74
75 void OnExit(wxCommandEvent&)
76 {
77 Close();
78 }
79
80 void OnClose(wxCloseEvent& e)
81 {
82 wxGetApp().DismissPreferencesEditor();
83 e.Skip();
84 }
85 };
86
87
88 class PrefsPageGeneralPanel : public wxPanel
89 {
90 public:
91 PrefsPageGeneralPanel(wxWindow *parent) : wxPanel(parent)
92 {
93 m_useMarkdown = new wxCheckBox(this, wxID_ANY, "User Markdown syntax");
94 m_spellcheck = new wxCheckBox(this, wxID_ANY, "Check spelling");
95
96 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
97 sizer->Add(m_useMarkdown, wxSizerFlags().Border());
98 sizer->Add(m_spellcheck, wxSizerFlags().Border());
99
100 SetSizerAndFit(sizer);
101
102 // On some platforms (OS X, GNOME), changes to preferences are applied
103 // immediately rather than after the OK or Apply button is pressed.
104 if ( wxPreferencesEditor::ShouldApplyChangesImmediately() )
105 {
106 m_useMarkdown->Connect(wxEVT_CHECKBOX,
107 wxCommandEventHandler(PrefsPageGeneralPanel::ChangedUseMarkdown),
108 NULL, this);
109 m_spellcheck->Connect(wxEVT_CHECKBOX,
110 wxCommandEventHandler(PrefsPageGeneralPanel::ChangedSpellcheck),
111 NULL, this);
112 }
113 }
114
115 virtual bool TransferDataToWindow()
116 {
117 // This is the place where you can initialize values, e.g. from wxConfig.
118 // For demonstration purposes, we just set hardcoded values.
119 m_useMarkdown->SetValue(true);
120 m_spellcheck->SetValue(false);
121 return true;
122 }
123
124 virtual bool TransferDataFromWindow()
125 {
126 // Called on platforms with modal preferences dialog to save and apply
127 // the changes.
128 wxCommandEvent dummy;
129 ChangedUseMarkdown(dummy);
130 ChangedSpellcheck(dummy);
131 return true;
132 }
133
134 private:
135 void ChangedUseMarkdown(wxCommandEvent& WXUNUSED(e))
136 {
137 // save new m_useMarkdown value and apply the change to the app
138 }
139
140 void ChangedSpellcheck(wxCommandEvent& WXUNUSED(e))
141 {
142 // save new m_spellcheck value and apply the change to the app
143 }
144
145 wxCheckBox *m_useMarkdown;
146 wxCheckBox *m_spellcheck;
147 };
148
149 class PrefsPageGeneral : public wxStockPreferencesPage
150 {
151 public:
152 PrefsPageGeneral() : wxStockPreferencesPage(Kind_General) {}
153 virtual wxWindow *CreateWindow(wxWindow *parent)
154 { return new PrefsPageGeneralPanel(parent); }
155 };
156
157
158 class PrefsPageTopicsPanel : public wxPanel
159 {
160 public:
161 PrefsPageTopicsPanel(wxWindow *parent) : wxPanel(parent)
162 {
163 wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
164 sizer->Add(new wxStaticText(this, wxID_ANY, "Search in these topics:"), wxSizerFlags().Border());
165 wxListBox *box = new wxListBox(this, wxID_ANY);
166 box->SetMinSize(wxSize(400, 300));
167 sizer->Add(box, wxSizerFlags(1).Border().Expand());
168 m_fulltext = new wxCheckBox(this, wxID_ANY, "Automatically build fulltext index");
169 sizer->Add(m_fulltext, wxSizerFlags().Border());
170
171 SetSizerAndFit(sizer);
172
173 if ( wxPreferencesEditor::ShouldApplyChangesImmediately() )
174 {
175 m_fulltext->Connect(wxEVT_CHECKBOX,
176 wxCommandEventHandler(PrefsPageTopicsPanel::ChangedFulltext),
177 NULL, this);
178 }
179 }
180
181 virtual bool TransferDataToWindow()
182 {
183 // This is the place where you can initialize values, e.g. from wxConfig.
184 // For demonstration purposes, we just set hardcoded values.
185 m_fulltext->SetValue(true);
186 // TODO: handle the listbox
187 return true;
188 }
189
190 virtual bool TransferDataFromWindow()
191 {
192 // Called on platforms with modal preferences dialog to save and apply
193 // the changes.
194 wxCommandEvent dummy;
195 ChangedFulltext(dummy);
196 // TODO: handle the listbox
197 return true;
198 }
199
200 private:
201 void ChangedFulltext(wxCommandEvent& WXUNUSED(e))
202 {
203 // save new m_fulltext value and apply the change to the app
204 }
205
206 wxCheckBox *m_fulltext;
207 };
208
209 class PrefsPageTopics : public wxPreferencesPage
210 {
211 public:
212 virtual wxString GetName() const { return "Topics"; }
213 virtual wxBitmap GetLargeIcon() const
214 { return wxArtProvider::GetBitmap(wxART_HELP, wxART_TOOLBAR); }
215 virtual wxWindow *CreateWindow(wxWindow *parent)
216 { return new PrefsPageTopicsPanel(parent); }
217 };
218
219
220
221 bool MyApp::OnInit()
222 {
223 if ( !wxApp::OnInit() )
224 return false;
225
226 // This will be used in the title of the preferences dialog under some
227 // platforms, don't leave it as default "Preferences" because this would
228 // result in rather strange "Preferences Preferences" title.
229 SetAppDisplayName("wxWidgets Sample");
230
231 MyFrame *frame = new MyFrame();
232 frame->Show(true);
233
234 return true;
235 }
236
237 void MyApp::ShowPreferencesEditor(wxWindow* parent)
238 {
239 if ( !m_prefEditor )
240 {
241 m_prefEditor.reset(new wxPreferencesEditor);
242 m_prefEditor->AddPage(new PrefsPageGeneral());
243 m_prefEditor->AddPage(new PrefsPageTopics());
244 }
245
246 m_prefEditor->Show(parent);
247 }
248
249 void MyApp::DismissPreferencesEditor()
250 {
251 if ( m_prefEditor )
252 m_prefEditor->Dismiss();
253 }