]>
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
7 // Copyright: (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/preferences.h"
21 #include "wx/config.h"
23 #include "wx/scopedptr.h"
25 #include "wx/checkbox.h"
26 #include "wx/listbox.h"
27 #include "wx/stattext.h"
29 #include "wx/artprov.h"
32 class MyApp
: public wxApp
35 virtual bool OnInit();
37 void ShowPreferencesEditor(wxWindow
* parent
);
38 void DismissPreferencesEditor();
41 wxScopedPtr
<wxPreferencesEditor
> m_prefEditor
;
47 class MyFrame
: public wxFrame
50 MyFrame() : wxFrame(NULL
, wxID_ANY
, "Preferences sample")
52 wxMenu
*fileMenu
= new wxMenu
;
53 fileMenu
->Append(wxID_PREFERENCES
);
54 fileMenu
->Append(wxID_EXIT
);
56 wxMenuBar
*menuBar
= new wxMenuBar();
57 menuBar
->Append(fileMenu
, "&File");
60 Connect(wxID_PREFERENCES
,
62 wxCommandEventHandler(MyFrame::OnPref
), NULL
, this);
65 wxCommandEventHandler(MyFrame::OnExit
), NULL
, this);
66 Connect(wxEVT_CLOSE_WINDOW
,
67 wxCloseEventHandler(MyFrame::OnClose
), NULL
, this);
71 void OnPref(wxCommandEvent
&)
73 wxGetApp().ShowPreferencesEditor(this);
76 void OnExit(wxCommandEvent
&)
81 void OnClose(wxCloseEvent
& e
)
83 wxGetApp().DismissPreferencesEditor();
89 class PrefsPageGeneralPanel
: public wxPanel
92 PrefsPageGeneralPanel(wxWindow
*parent
) : wxPanel(parent
)
94 m_useMarkdown
= new wxCheckBox(this, wxID_ANY
, "User Markdown syntax");
95 m_spellcheck
= new wxCheckBox(this, wxID_ANY
, "Check spelling");
97 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
98 sizer
->Add(m_useMarkdown
, wxSizerFlags().Border());
99 sizer
->Add(m_spellcheck
, wxSizerFlags().Border());
101 SetSizerAndFit(sizer
);
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() )
107 m_useMarkdown
->Connect(wxEVT_CHECKBOX
,
108 wxCommandEventHandler(PrefsPageGeneralPanel::ChangedUseMarkdown
),
110 m_spellcheck
->Connect(wxEVT_CHECKBOX
,
111 wxCommandEventHandler(PrefsPageGeneralPanel::ChangedSpellcheck
),
116 virtual bool TransferDataToWindow()
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);
125 virtual bool TransferDataFromWindow()
127 // Called on platforms with modal preferences dialog to save and apply
129 wxCommandEvent dummy
;
130 ChangedUseMarkdown(dummy
);
131 ChangedSpellcheck(dummy
);
136 void ChangedUseMarkdown(wxCommandEvent
& WXUNUSED(e
))
138 // save new m_useMarkdown value and apply the change to the app
141 void ChangedSpellcheck(wxCommandEvent
& WXUNUSED(e
))
143 // save new m_spellcheck value and apply the change to the app
146 wxCheckBox
*m_useMarkdown
;
147 wxCheckBox
*m_spellcheck
;
150 class PrefsPageGeneral
: public wxStockPreferencesPage
153 PrefsPageGeneral() : wxStockPreferencesPage(Kind_General
) {}
154 virtual wxWindow
*CreateWindow(wxWindow
*parent
)
155 { return new PrefsPageGeneralPanel(parent
); }
159 class PrefsPageTopicsPanel
: public wxPanel
162 PrefsPageTopicsPanel(wxWindow
*parent
) : wxPanel(parent
)
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());
172 SetSizerAndFit(sizer
);
174 if ( wxPreferencesEditor::ShouldApplyChangesImmediately() )
176 m_fulltext
->Connect(wxEVT_CHECKBOX
,
177 wxCommandEventHandler(PrefsPageTopicsPanel::ChangedFulltext
),
182 virtual bool TransferDataToWindow()
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
191 virtual bool TransferDataFromWindow()
193 // Called on platforms with modal preferences dialog to save and apply
195 wxCommandEvent dummy
;
196 ChangedFulltext(dummy
);
197 // TODO: handle the listbox
202 void ChangedFulltext(wxCommandEvent
& WXUNUSED(e
))
204 // save new m_fulltext value and apply the change to the app
207 wxCheckBox
*m_fulltext
;
210 class PrefsPageTopics
: public wxPreferencesPage
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
); }
224 if ( !wxApp::OnInit() )
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");
232 MyFrame
*frame
= new MyFrame();
238 void MyApp::ShowPreferencesEditor(wxWindow
* parent
)
242 m_prefEditor
.reset(new wxPreferencesEditor
);
243 m_prefEditor
->AddPage(new PrefsPageGeneral());
244 m_prefEditor
->AddPage(new PrefsPageTopics());
247 m_prefEditor
->Show(parent
);
250 void MyApp::DismissPreferencesEditor()
253 m_prefEditor
->Dismiss();