File: preferences.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (317 lines) | stat: -rw-r--r-- 9,533 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
///////////////////////////////////////////////////////////////////////////////
// Name:        samples/preferences/preferences.cpp
// Purpose:     Sample demonstrating wxPreferencesEditor use.
// Author:      Vaclav Slavik
// Created:     2013-02-19
// Copyright:   (c) 2013 Vaclav Slavik <vslavik@fastmail.fm>
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////


#include "wx/wxprec.h"


#include "wx/preferences.h"

#include "wx/app.h"
#include "wx/config.h"
#include "wx/panel.h"
#include "wx/scopedptr.h"
#include "wx/menu.h"
#include "wx/checkbox.h"
#include "wx/listbox.h"
#include "wx/stattext.h"
#include "wx/sizer.h"
#include "wx/artprov.h"
#include "wx/frame.h"

// This struct combines the settings edited in the preferences dialog.
struct MySettings
{
    MySettings()
    {
        // Normally we would initialize values by loading them from some
        // persistent storage, e.g. using wxConfig.
        // For demonstration purposes, we just set hardcoded values here.
        m_useMarkdown = true;
        m_spellcheck = false;
    }

    bool m_useMarkdown;
    bool m_spellcheck;

    // We don't do the same thing for the second preferences page fields, but
    // we would have included them in a real application.
};

class MyApp : public wxApp
{
public:
    virtual bool OnInit() wxOVERRIDE;

    void ShowPreferencesEditor(wxWindow* parent);
    void DismissPreferencesEditor();

    const MySettings& GetSettings() const { return m_settings; }
    void UpdateSettings(const MySettings& settings);

private:
    class MyFrame* m_frame;
    wxScopedPtr<wxPreferencesEditor> m_prefEditor;
    MySettings m_settings;
};

wxIMPLEMENT_APP(MyApp);


class MyFrame : public wxFrame
{
public:
    MyFrame() : wxFrame(NULL, wxID_ANY, "Preferences sample")
    {
        wxMenu *fileMenu = new wxMenu;
        fileMenu->Append(wxID_PREFERENCES);
        fileMenu->Append(wxID_EXIT);

        wxMenuBar *menuBar = new wxMenuBar();
        menuBar->Append(fileMenu, "&File");
        SetMenuBar(menuBar);

        Bind(wxEVT_MENU, &MyFrame::OnPref, this, wxID_PREFERENCES);
        Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
        Bind(wxEVT_CLOSE_WINDOW, &MyFrame::OnClose, this);

        wxPanel* const panel = new wxPanel(this);
        m_textMarkdownSyntax = new wxStaticText(panel, wxID_ANY, "");
        m_textSpellcheck = new wxStaticText(panel, wxID_ANY, "");

        wxSizer* const sizer = new wxFlexGridSizer(2, wxSize(5, 5));
        sizer->Add(new wxStaticText(panel, wxID_ANY, "Markdown syntax:"),
                   wxSizerFlags().Center().Right());
        sizer->Add(m_textMarkdownSyntax,
                   wxSizerFlags().Center());
        sizer->Add(new wxStaticText(panel, wxID_ANY, "Spell checking:"),
                   wxSizerFlags().Center().Right());
        sizer->Add(m_textSpellcheck,
                   wxSizerFlags().Center());
        panel->SetSizer(sizer);

        // Show the initial values.
        UpdateSettings();
    }

    void UpdateSettings()
    {
        // Here we should update the settings we use. As we don't actually do
        // anything in this sample, just update their values shown on screen.
        const MySettings& settings = wxGetApp().GetSettings();
        m_textMarkdownSyntax->SetLabel(settings.m_useMarkdown ? "yes" : "no");
        m_textSpellcheck->SetLabel(settings.m_spellcheck ? "on" : "off");
    }

private:
    void OnPref(wxCommandEvent&)
    {
        wxGetApp().ShowPreferencesEditor(this);
    }

    void OnExit(wxCommandEvent&)
    {
        Close();
    }

    void OnClose(wxCloseEvent& e)
    {
        wxGetApp().DismissPreferencesEditor();
        e.Skip();
    }

    wxStaticText* m_textMarkdownSyntax;
    wxStaticText* m_textSpellcheck;
};


class PrefsPageGeneralPanel : public wxPanel
{
public:
    PrefsPageGeneralPanel(wxWindow *parent) : wxPanel(parent)
    {
        m_useMarkdown = new wxCheckBox(this, wxID_ANY, "Use Markdown syntax");
        m_spellcheck = new wxCheckBox(this, wxID_ANY, "Check spelling");

        wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
        sizer->Add(m_useMarkdown, wxSizerFlags().Border());
        sizer->Add(m_spellcheck, wxSizerFlags().Border());

        SetSizerAndFit(sizer);

        m_useMarkdown->Bind(wxEVT_CHECKBOX,
                            &PrefsPageGeneralPanel::ChangedUseMarkdown,
                            this);
        m_spellcheck->Bind(wxEVT_CHECKBOX,
                           &PrefsPageGeneralPanel::ChangedSpellcheck,
                           this);
    }

    virtual bool TransferDataToWindow() wxOVERRIDE
    {
        m_settingsCurrent = wxGetApp().GetSettings();
        m_useMarkdown->SetValue(m_settingsCurrent.m_useMarkdown);
        m_spellcheck->SetValue(m_settingsCurrent.m_spellcheck);
        return true;
    }

    virtual bool TransferDataFromWindow() wxOVERRIDE
    {
        // Called on platforms with modal preferences dialog to save and apply
        // the changes.
        wxGetApp().UpdateSettings(m_settingsCurrent);
        return true;
    }

private:
    void UpdateSettingsIfNecessary()
    {
        // On some platforms (OS X, GNOME), changes to preferences are applied
        // immediately rather than after the OK or Apply button is pressed, so
        // we need to take them into account as soon as they happen. On others
        // (MSW), we need to wait until the changes are accepted by the user by
        // pressing the "OK" button. To reuse the same code for both cases, we
        // always update m_settingsCurrent object under all platforms, but only
        // update the real application settings if necessary here.
        if ( wxPreferencesEditor::ShouldApplyChangesImmediately() )
        {
            wxGetApp().UpdateSettings(m_settingsCurrent);
        }
    }

    void ChangedUseMarkdown(wxCommandEvent& e)
    {
        m_settingsCurrent.m_useMarkdown = e.IsChecked();
        UpdateSettingsIfNecessary();
    }

    void ChangedSpellcheck(wxCommandEvent& e)
    {
        m_settingsCurrent.m_spellcheck = e.IsChecked();
        UpdateSettingsIfNecessary();
    }

    wxCheckBox *m_useMarkdown;
    wxCheckBox *m_spellcheck;

    // Settings corresponding to the current values in this dialog.
    MySettings m_settingsCurrent;
};

class PrefsPageGeneral : public wxStockPreferencesPage
{
public:
    PrefsPageGeneral() : wxStockPreferencesPage(Kind_General) {}
    virtual wxWindow *CreateWindow(wxWindow *parent) wxOVERRIDE
        { return new PrefsPageGeneralPanel(parent); }
};


class PrefsPageTopicsPanel : public wxPanel
{
public:
    PrefsPageTopicsPanel(wxWindow *parent) : wxPanel(parent)
    {
        wxSizer *sizer = new wxBoxSizer(wxVERTICAL);
        sizer->Add(new wxStaticText(this, wxID_ANY, "Search in these topics:"), wxSizerFlags().Border());
        wxListBox *box = new wxListBox(this, wxID_ANY);
        box->SetMinSize(wxSize(400, 300));
        sizer->Add(box, wxSizerFlags(1).Border().Expand());
        m_fulltext = new wxCheckBox(this, wxID_ANY, "Automatically build fulltext index");
        sizer->Add(m_fulltext, wxSizerFlags().Border());

        SetSizerAndFit(sizer);

        if ( wxPreferencesEditor::ShouldApplyChangesImmediately() )
        {
            m_fulltext->Bind(wxEVT_CHECKBOX,
                             &PrefsPageTopicsPanel::ChangedFulltext,
                             this);
        }
    }

    virtual bool TransferDataToWindow() wxOVERRIDE
    {
        // This is the place where you can initialize values, e.g. from wxConfig.
        // For demonstration purposes, we just set hardcoded values.
        m_fulltext->SetValue(true);
        // TODO: handle the listbox
        return true;
    }

    virtual bool TransferDataFromWindow() wxOVERRIDE
    {
        // Called on platforms with modal preferences dialog to save and apply
        // the changes.
        wxCommandEvent dummy;
        ChangedFulltext(dummy);
        // TODO: handle the listbox
        return true;
    }

private:
    void ChangedFulltext(wxCommandEvent& WXUNUSED(e))
    {
        // save new m_fulltext value and apply the change to the app
    }

    wxCheckBox *m_fulltext;
};

class PrefsPageTopics : public wxPreferencesPage
{
public:
    virtual wxString GetName() const wxOVERRIDE { return "Topics"; }
    virtual wxBitmapBundle GetIcon() const wxOVERRIDE
        { return wxArtProvider::GetBitmapBundle(wxART_HELP, wxART_TOOLBAR); }
    virtual wxWindow *CreateWindow(wxWindow *parent) wxOVERRIDE
        { return new PrefsPageTopicsPanel(parent); }
};



bool MyApp::OnInit()
{
    if ( !wxApp::OnInit() )
        return false;

    // This will be used in the title of the preferences dialog under some
    // platforms, don't leave it as default "Preferences" because this would
    // result in rather strange "Preferences Preferences" title.
    SetAppDisplayName("wxWidgets Sample");

    m_frame = new MyFrame();
    m_frame->Show(true);

    return true;
}

void MyApp::ShowPreferencesEditor(wxWindow* parent)
{
    if ( !m_prefEditor )
    {
        m_prefEditor.reset(new wxPreferencesEditor);
        m_prefEditor->AddPage(new PrefsPageGeneral());
        m_prefEditor->AddPage(new PrefsPageTopics());
    }

    m_prefEditor->Show(parent);
}

void MyApp::DismissPreferencesEditor()
{
    if ( m_prefEditor )
        m_prefEditor->Dismiss();
}

void MyApp::UpdateSettings(const MySettings& settings)
{
    m_settings = settings;
    m_frame->UpdateSettings();
}