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