2013-02-28 11:29:12 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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"
|
|
|
|
|
2017-07-11 17:05:28 -04:00
|
|
|
// 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.
|
|
|
|
};
|
|
|
|
|
2013-02-28 11:29:12 -05:00
|
|
|
class MyApp : public wxApp
|
|
|
|
{
|
|
|
|
public:
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual bool OnInit() wxOVERRIDE;
|
2013-02-28 11:29:12 -05:00
|
|
|
|
|
|
|
void ShowPreferencesEditor(wxWindow* parent);
|
|
|
|
void DismissPreferencesEditor();
|
2017-07-11 17:05:28 -04:00
|
|
|
|
2017-07-11 17:34:37 -04:00
|
|
|
const MySettings& GetSettings() const { return m_settings; }
|
|
|
|
void UpdateSettings(const MySettings& settings);
|
2013-02-28 11:29:12 -05:00
|
|
|
|
|
|
|
private:
|
2017-07-11 17:05:28 -04:00
|
|
|
class MyFrame* m_frame;
|
2013-02-28 11:29:12 -05:00
|
|
|
wxScopedPtr<wxPreferencesEditor> m_prefEditor;
|
2017-07-11 17:34:37 -04:00
|
|
|
MySettings m_settings;
|
2013-02-28 11:29:12 -05:00
|
|
|
};
|
|
|
|
|
2015-04-23 07:49:01 -04:00
|
|
|
wxIMPLEMENT_APP(MyApp);
|
2013-02-28 11:29:12 -05:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2017-07-11 16:47:32 -04:00
|
|
|
Bind(wxEVT_MENU, &MyFrame::OnPref, this, wxID_PREFERENCES);
|
|
|
|
Bind(wxEVT_MENU, &MyFrame::OnExit, this, wxID_EXIT);
|
|
|
|
Bind(wxEVT_CLOSE_WINDOW, &MyFrame::OnClose, this);
|
2017-07-11 17:05:28 -04:00
|
|
|
|
|
|
|
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.
|
2017-07-11 17:34:37 -04:00
|
|
|
const MySettings& settings = wxGetApp().GetSettings();
|
2017-07-11 17:05:28 -04:00
|
|
|
m_textMarkdownSyntax->SetLabel(settings.m_useMarkdown ? "yes" : "no");
|
|
|
|
m_textSpellcheck->SetLabel(settings.m_spellcheck ? "on" : "off");
|
2013-02-28 11:29:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void OnPref(wxCommandEvent&)
|
|
|
|
{
|
|
|
|
wxGetApp().ShowPreferencesEditor(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnExit(wxCommandEvent&)
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnClose(wxCloseEvent& e)
|
|
|
|
{
|
|
|
|
wxGetApp().DismissPreferencesEditor();
|
|
|
|
e.Skip();
|
|
|
|
}
|
2017-07-11 17:05:28 -04:00
|
|
|
|
|
|
|
wxStaticText* m_textMarkdownSyntax;
|
|
|
|
wxStaticText* m_textSpellcheck;
|
2013-02-28 11:29:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class PrefsPageGeneralPanel : public wxPanel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PrefsPageGeneralPanel(wxWindow *parent) : wxPanel(parent)
|
|
|
|
{
|
2017-07-11 16:45:18 -04:00
|
|
|
m_useMarkdown = new wxCheckBox(this, wxID_ANY, "Use Markdown syntax");
|
2013-02-28 11:29:12 -05:00
|
|
|
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);
|
|
|
|
|
2017-07-11 17:34:37 -04:00
|
|
|
m_useMarkdown->Bind(wxEVT_CHECKBOX,
|
|
|
|
&PrefsPageGeneralPanel::ChangedUseMarkdown,
|
|
|
|
this);
|
|
|
|
m_spellcheck->Bind(wxEVT_CHECKBOX,
|
|
|
|
&PrefsPageGeneralPanel::ChangedSpellcheck,
|
|
|
|
this);
|
2013-02-28 11:29:12 -05:00
|
|
|
}
|
|
|
|
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual bool TransferDataToWindow() wxOVERRIDE
|
2013-02-28 11:29:12 -05:00
|
|
|
{
|
2017-07-11 17:34:37 -04:00
|
|
|
m_settingsCurrent = wxGetApp().GetSettings();
|
|
|
|
m_useMarkdown->SetValue(m_settingsCurrent.m_useMarkdown);
|
|
|
|
m_spellcheck->SetValue(m_settingsCurrent.m_spellcheck);
|
2013-02-28 11:29:12 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual bool TransferDataFromWindow() wxOVERRIDE
|
2013-02-28 11:29:12 -05:00
|
|
|
{
|
|
|
|
// Called on platforms with modal preferences dialog to save and apply
|
|
|
|
// the changes.
|
2017-07-11 17:34:37 -04:00
|
|
|
wxGetApp().UpdateSettings(m_settingsCurrent);
|
2013-02-28 11:29:12 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-07-11 17:34:37 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 17:05:28 -04:00
|
|
|
void ChangedUseMarkdown(wxCommandEvent& e)
|
2013-02-28 11:29:12 -05:00
|
|
|
{
|
2017-07-11 17:34:37 -04:00
|
|
|
m_settingsCurrent.m_useMarkdown = e.IsChecked();
|
|
|
|
UpdateSettingsIfNecessary();
|
2013-02-28 11:29:12 -05:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:05:28 -04:00
|
|
|
void ChangedSpellcheck(wxCommandEvent& e)
|
2013-02-28 11:29:12 -05:00
|
|
|
{
|
2017-07-11 17:34:37 -04:00
|
|
|
m_settingsCurrent.m_spellcheck = e.IsChecked();
|
|
|
|
UpdateSettingsIfNecessary();
|
2013-02-28 11:29:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
wxCheckBox *m_useMarkdown;
|
|
|
|
wxCheckBox *m_spellcheck;
|
2017-07-11 17:34:37 -04:00
|
|
|
|
|
|
|
// Settings corresponding to the current values in this dialog.
|
|
|
|
MySettings m_settingsCurrent;
|
2013-02-28 11:29:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
class PrefsPageGeneral : public wxStockPreferencesPage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PrefsPageGeneral() : wxStockPreferencesPage(Kind_General) {}
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual wxWindow *CreateWindow(wxWindow *parent) wxOVERRIDE
|
2013-02-28 11:29:12 -05:00
|
|
|
{ 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() )
|
|
|
|
{
|
2017-07-11 16:47:32 -04:00
|
|
|
m_fulltext->Bind(wxEVT_CHECKBOX,
|
|
|
|
&PrefsPageTopicsPanel::ChangedFulltext,
|
|
|
|
this);
|
2013-02-28 11:29:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual bool TransferDataToWindow() wxOVERRIDE
|
2013-02-28 11:29:12 -05:00
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual bool TransferDataFromWindow() wxOVERRIDE
|
2013-02-28 11:29:12 -05:00
|
|
|
{
|
|
|
|
// 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:
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual wxString GetName() const wxOVERRIDE { return "Topics"; }
|
2022-03-10 13:54:58 -05:00
|
|
|
virtual wxBitmapBundle GetIcon() const wxOVERRIDE
|
|
|
|
{ return wxArtProvider::GetBitmapBundle(wxART_HELP, wxART_TOOLBAR); }
|
2018-03-06 17:12:19 -05:00
|
|
|
virtual wxWindow *CreateWindow(wxWindow *parent) wxOVERRIDE
|
2013-02-28 11:29:12 -05:00
|
|
|
{ return new PrefsPageTopicsPanel(parent); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
|
|
|
if ( !wxApp::OnInit() )
|
|
|
|
return false;
|
|
|
|
|
2013-05-16 10:42:59 -04:00
|
|
|
// 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");
|
|
|
|
|
2017-07-11 17:05:28 -04:00
|
|
|
m_frame = new MyFrame();
|
|
|
|
m_frame->Show(true);
|
2013-02-28 11:29:12 -05:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
2017-07-11 17:05:28 -04:00
|
|
|
|
2017-07-11 17:34:37 -04:00
|
|
|
void MyApp::UpdateSettings(const MySettings& settings)
|
2017-07-11 17:05:28 -04:00
|
|
|
{
|
2017-07-11 17:34:37 -04:00
|
|
|
m_settings = settings;
|
2017-07-11 17:05:28 -04:00
|
|
|
m_frame->UpdateSettings();
|
|
|
|
}
|