]>
git.saurik.com Git - wxWidgets.git/blob - samples/preferences/preferences.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: samples/preferences/preferences.cpp
3 // Purpose: Sample demonstrating wxPreferencesEditor use.
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
17 #include "wx/preferences.h"
20 #include "wx/config.h"
22 #include "wx/scopedptr.h"
24 #include "wx/checkbox.h"
25 #include "wx/listbox.h"
26 #include "wx/stattext.h"
28 #include "wx/artprov.h"
31 class MyApp
: public wxApp
34 virtual bool OnInit();
36 void ShowPreferencesEditor(wxWindow
* parent
);
37 void DismissPreferencesEditor();
40 wxScopedPtr
<wxPreferencesEditor
> m_prefEditor
;
46 class MyFrame
: public wxFrame
49 MyFrame() : wxFrame(NULL
, wxID_ANY
, "Preferences sample")
51 wxMenu
*fileMenu
= new wxMenu
;
52 fileMenu
->Append(wxID_PREFERENCES
);
53 fileMenu
->Append(wxID_EXIT
);
55 wxMenuBar
*menuBar
= new wxMenuBar();
56 menuBar
->Append(fileMenu
, "&File");
59 Connect(wxID_PREFERENCES
,
61 wxCommandEventHandler(MyFrame::OnPref
), NULL
, this);
64 wxCommandEventHandler(MyFrame::OnExit
), NULL
, this);
65 Connect(wxEVT_CLOSE_WINDOW
,
66 wxCloseEventHandler(MyFrame::OnClose
), NULL
, this);
70 void OnPref(wxCommandEvent
&)
72 wxGetApp().ShowPreferencesEditor(this);
75 void OnExit(wxCommandEvent
&)
80 void OnClose(wxCloseEvent
& e
)
82 wxGetApp().DismissPreferencesEditor();
88 class PrefsPageGeneralPanel
: public wxPanel
91 PrefsPageGeneralPanel(wxWindow
*parent
) : wxPanel(parent
)
93 m_useMarkdown
= new wxCheckBox(this, wxID_ANY
, "User Markdown syntax");
94 m_spellcheck
= new wxCheckBox(this, wxID_ANY
, "Check spelling");
96 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
97 sizer
->Add(m_useMarkdown
, wxSizerFlags().Border());
98 sizer
->Add(m_spellcheck
, wxSizerFlags().Border());
100 SetSizerAndFit(sizer
);
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() )
106 m_useMarkdown
->Connect(wxEVT_CHECKBOX
,
107 wxCommandEventHandler(PrefsPageGeneralPanel::ChangedUseMarkdown
),
109 m_spellcheck
->Connect(wxEVT_CHECKBOX
,
110 wxCommandEventHandler(PrefsPageGeneralPanel::ChangedSpellcheck
),
115 virtual bool TransferDataToWindow()
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);
124 virtual bool TransferDataFromWindow()
126 // Called on platforms with modal preferences dialog to save and apply
128 wxCommandEvent dummy
;
129 ChangedUseMarkdown(dummy
);
130 ChangedSpellcheck(dummy
);
135 void ChangedUseMarkdown(wxCommandEvent
& WXUNUSED(e
))
137 // save new m_useMarkdown value and apply the change to the app
140 void ChangedSpellcheck(wxCommandEvent
& WXUNUSED(e
))
142 // save new m_spellcheck value and apply the change to the app
145 wxCheckBox
*m_useMarkdown
;
146 wxCheckBox
*m_spellcheck
;
149 class PrefsPageGeneral
: public wxStockPreferencesPage
152 PrefsPageGeneral() : wxStockPreferencesPage(Kind_General
) {}
153 virtual wxWindow
*CreateWindow(wxWindow
*parent
)
154 { return new PrefsPageGeneralPanel(parent
); }
158 class PrefsPageTopicsPanel
: public wxPanel
161 PrefsPageTopicsPanel(wxWindow
*parent
) : wxPanel(parent
)
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());
171 SetSizerAndFit(sizer
);
173 if ( wxPreferencesEditor::ShouldApplyChangesImmediately() )
175 m_fulltext
->Connect(wxEVT_CHECKBOX
,
176 wxCommandEventHandler(PrefsPageTopicsPanel::ChangedFulltext
),
181 virtual bool TransferDataToWindow()
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
190 virtual bool TransferDataFromWindow()
192 // Called on platforms with modal preferences dialog to save and apply
194 wxCommandEvent dummy
;
195 ChangedFulltext(dummy
);
196 // TODO: handle the listbox
201 void ChangedFulltext(wxCommandEvent
& WXUNUSED(e
))
203 // save new m_fulltext value and apply the change to the app
206 wxCheckBox
*m_fulltext
;
209 class PrefsPageTopics
: public wxPreferencesPage
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
); }
223 if ( !wxApp::OnInit() )
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");
231 MyFrame
*frame
= new MyFrame();
237 void MyApp::ShowPreferencesEditor(wxWindow
* parent
)
241 m_prefEditor
.reset(new wxPreferencesEditor
);
242 m_prefEditor
->AddPage(new PrefsPageGeneral());
243 m_prefEditor
->AddPage(new PrefsPageTopics());
246 m_prefEditor
->Show(parent
);
249 void MyApp::DismissPreferencesEditor()
252 m_prefEditor
->Dismiss();