preferences.cpp 7.0 KB

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