conftest.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Name: conftest.cpp
  3. // Purpose: demo of wxConfig and related classes
  4. // Author: Vadim Zeitlin
  5. // Modified by:
  6. // Created: 03.08.98
  7. // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
  8. // Licence: wxWindows licence
  9. ///////////////////////////////////////////////////////////////////////////////
  10. // ============================================================================
  11. // declarations
  12. // ============================================================================
  13. // ----------------------------------------------------------------------------
  14. // headers
  15. // ----------------------------------------------------------------------------
  16. #include "wx/wxprec.h"
  17. #ifdef __BORLANDC__
  18. #pragma hdrstop
  19. #endif
  20. #ifndef WX_PRECOMP
  21. #include "wx/wx.h"
  22. #endif //precompiled headers
  23. #include "wx/log.h"
  24. #include "wx/config.h"
  25. #ifndef wxHAS_IMAGES_IN_RESOURCES
  26. #include "../sample.xpm"
  27. #endif
  28. // ----------------------------------------------------------------------------
  29. // classes
  30. // ----------------------------------------------------------------------------
  31. class MyApp: public wxApp
  32. {
  33. public:
  34. // implement base class virtuals
  35. virtual bool OnInit();
  36. virtual int OnExit();
  37. };
  38. class MyFrame: public wxFrame
  39. {
  40. public:
  41. MyFrame();
  42. virtual ~MyFrame();
  43. // callbacks
  44. void OnQuit(wxCommandEvent& event);
  45. void OnAbout(wxCommandEvent& event);
  46. void OnDelete(wxCommandEvent& event);
  47. private:
  48. wxTextCtrl *m_text;
  49. wxCheckBox *m_check;
  50. wxDECLARE_EVENT_TABLE();
  51. };
  52. // ----------------------------------------------------------------------------
  53. // event tables
  54. // ----------------------------------------------------------------------------
  55. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  56. EVT_MENU(wxID_EXIT, MyFrame::OnQuit)
  57. EVT_MENU(wxID_ABOUT, MyFrame::OnAbout)
  58. EVT_MENU(wxID_DELETE, MyFrame::OnDelete)
  59. wxEND_EVENT_TABLE()
  60. // ============================================================================
  61. // implementation
  62. // ============================================================================
  63. // ----------------------------------------------------------------------------
  64. // application
  65. // ----------------------------------------------------------------------------
  66. IMPLEMENT_APP(MyApp)
  67. // `Main program' equivalent, creating windows and returning main app frame
  68. bool MyApp::OnInit()
  69. {
  70. if ( !wxApp::OnInit() )
  71. return false;
  72. // we're using wxConfig's "create-on-demand" feature: it will create the
  73. // config object when it's used for the first time. It has a number of
  74. // advantages compared with explicitly creating our wxConfig:
  75. // 1) we don't pay for it if we don't use it
  76. // 2) there is no danger to create it twice
  77. // application and vendor name are used by wxConfig to construct the name
  78. // of the config file/registry key and must be set before the first call
  79. // to Get() if you want to override the default values (the application
  80. // name is the name of the executable and the vendor name is the same)
  81. SetVendorName(wxT("wxWidgets"));
  82. SetAppName(wxT("conftest")); // not needed, it's the default value
  83. wxConfigBase *pConfig = wxConfigBase::Get();
  84. // uncomment this to force writing back of the defaults for all values
  85. // if they're not present in the config - this can give the user an idea
  86. // of all possible settings for this program
  87. pConfig->SetRecordDefaults();
  88. // or you could also write something like this:
  89. // wxFileConfig *pConfig = new wxFileConfig("conftest");
  90. // wxConfigBase::Set(pConfig);
  91. // where you can also specify the file names explicitly if you wish.
  92. // Of course, calling Set() is optional and you only must do it if
  93. // you want to later retrieve this pointer with Get().
  94. // create the main program window
  95. MyFrame *frame = new MyFrame;
  96. frame->Show(true);
  97. // use our config object...
  98. if ( pConfig->Read(wxT("/Controls/Check"), 1l) != 0 ) {
  99. wxMessageBox(wxT("You can disable this message box by unchecking\n")
  100. wxT("the checkbox in the main window (of course, a real\n")
  101. wxT("program would have a checkbox right here but we\n")
  102. wxT("keep it simple)"), wxT("Welcome to wxConfig demo"),
  103. wxICON_INFORMATION | wxOK);
  104. }
  105. return true;
  106. }
  107. int MyApp::OnExit()
  108. {
  109. // clean up: Set() returns the active config object as Get() does, but unlike
  110. // Get() it doesn't try to create one if there is none (definitely not what
  111. // we want here!)
  112. delete wxConfigBase::Set((wxConfigBase *) NULL);
  113. return 0;
  114. }
  115. // ----------------------------------------------------------------------------
  116. // frame
  117. // ----------------------------------------------------------------------------
  118. // main frame ctor
  119. MyFrame::MyFrame()
  120. : wxFrame((wxFrame *) NULL, wxID_ANY, wxT("wxConfig Demo"))
  121. {
  122. SetIcon(wxICON(sample));
  123. // menu
  124. wxMenu *file_menu = new wxMenu;
  125. file_menu->Append(wxID_DELETE, wxT("&Delete"), wxT("Delete config file"));
  126. file_menu->AppendSeparator();
  127. file_menu->Append(wxID_ABOUT, wxT("&About\tF1"), wxT("About this sample"));
  128. file_menu->AppendSeparator();
  129. file_menu->Append(wxID_EXIT, wxT("E&xit\tAlt-X"), wxT("Exit the program"));
  130. wxMenuBar *menu_bar = new wxMenuBar;
  131. menu_bar->Append(file_menu, wxT("&File"));
  132. SetMenuBar(menu_bar);
  133. #if wxUSE_STATUSBAR
  134. CreateStatusBar();
  135. #endif // wxUSE_STATUSBAR
  136. // child controls
  137. wxPanel *panel = new wxPanel(this);
  138. (void)new wxStaticText(panel, wxID_ANY, wxT("These controls remember their values!"),
  139. wxPoint(10, 10), wxSize(300, 20));
  140. m_text = new wxTextCtrl(panel, wxID_ANY, wxT(""), wxPoint(10, 40), wxSize(300, 20));
  141. m_check = new wxCheckBox(panel, wxID_ANY, wxT("show welcome message box at startup"),
  142. wxPoint(10, 70), wxSize(300, 20));
  143. // restore the control's values from the config
  144. // NB: in this program, the config object is already created at this moment
  145. // because we had called Get() from MyApp::OnInit(). However, if you later
  146. // change the code and don't create it before this line, it won't break
  147. // anything - unlike if you manually create wxConfig object with Create()
  148. // or in any other way (then you must be sure to create it before using it!).
  149. wxConfigBase *pConfig = wxConfigBase::Get();
  150. // we could write Read("/Controls/Text") as well, it's just to show SetPath()
  151. pConfig->SetPath(wxT("/Controls"));
  152. m_text->SetValue(pConfig->Read(wxT("Text"), wxT("")));
  153. m_check->SetValue(pConfig->Read(wxT("Check"), 1l) != 0);
  154. // SetPath() understands ".."
  155. pConfig->SetPath(wxT("../MainFrame"));
  156. // restore frame position and size
  157. int x = pConfig->Read(wxT("x"), 50),
  158. y = pConfig->Read(wxT("y"), 50),
  159. w = pConfig->Read(wxT("w"), 350),
  160. h = pConfig->Read(wxT("h"), 200);
  161. Move(x, y);
  162. SetClientSize(w, h);
  163. pConfig->SetPath(wxT("/"));
  164. wxString s;
  165. if ( pConfig->Read(wxT("TestValue"), &s) )
  166. {
  167. wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str());
  168. }
  169. else
  170. {
  171. wxLogStatus(this, wxT("TestValue not found in the config"));
  172. }
  173. }
  174. void MyFrame::OnQuit(wxCommandEvent&)
  175. {
  176. Close(true);
  177. }
  178. void MyFrame::OnAbout(wxCommandEvent&)
  179. {
  180. wxMessageBox(wxT("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin"), wxT("About"),
  181. wxICON_INFORMATION | wxOK);
  182. }
  183. void MyFrame::OnDelete(wxCommandEvent&)
  184. {
  185. wxConfigBase *pConfig = wxConfigBase::Get();
  186. if ( pConfig == NULL )
  187. {
  188. wxLogError(wxT("No config to delete!"));
  189. return;
  190. }
  191. if ( pConfig->DeleteAll() )
  192. {
  193. wxLogMessage(wxT("Config file/registry key successfully deleted."));
  194. delete wxConfigBase::Set(NULL);
  195. wxConfigBase::DontCreateOnDemand();
  196. }
  197. else
  198. {
  199. wxLogError(wxT("Deleting config file/registry key failed."));
  200. }
  201. }
  202. MyFrame::~MyFrame()
  203. {
  204. wxConfigBase *pConfig = wxConfigBase::Get();
  205. if ( pConfig == NULL )
  206. return;
  207. // save the control's values to the config
  208. pConfig->Write(wxT("/Controls/Text"), m_text->GetValue());
  209. pConfig->Write(wxT("/Controls/Check"), m_check->GetValue());
  210. // save the frame position
  211. int x, y, w, h;
  212. GetClientSize(&w, &h);
  213. GetPosition(&x, &y);
  214. pConfig->Write(wxT("/MainFrame/x"), (long) x);
  215. pConfig->Write(wxT("/MainFrame/y"), (long) y);
  216. pConfig->Write(wxT("/MainFrame/w"), (long) w);
  217. pConfig->Write(wxT("/MainFrame/h"), (long) h);
  218. pConfig->Write(wxT("/TestValue"), wxT("A test value"));
  219. }