X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7a4b8f27709c3dd59a5b447850389ca25f178047..b9efe021b554fa3967d1442cf758435c5cd5ae8f:/samples/config/conftest.cpp diff --git a/samples/config/conftest.cpp b/samples/config/conftest.cpp index 0dbc6cd148..112e79056d 100644 --- a/samples/config/conftest.cpp +++ b/samples/config/conftest.cpp @@ -28,6 +28,7 @@ // ---------------------------------------------------------------------------- // classes // ---------------------------------------------------------------------------- + class MyApp: public wxApp { public: @@ -46,7 +47,6 @@ public: void OnQuit(wxCommandEvent& event); void OnAbout(wxCommandEvent& event); void OnDelete(wxCommandEvent& event); - void OnCloseWindow(wxCloseEvent& event); private: wxTextCtrl *m_text; @@ -57,19 +57,19 @@ private: enum { - Minimal_Quit, - Minimal_About, - Minimal_Delete + ConfTest_Quit, + ConfTest_About, + ConfTest_Delete }; // ---------------------------------------------------------------------------- // event tables // ---------------------------------------------------------------------------- + BEGIN_EVENT_TABLE(MyFrame, wxFrame) - EVT_MENU(Minimal_Quit, MyFrame::OnQuit) - EVT_MENU(Minimal_About, MyFrame::OnAbout) - EVT_MENU(Minimal_Delete, MyFrame::OnDelete) - EVT_CLOSE(MyFrame::OnCloseWindow) + EVT_MENU(ConfTest_Quit, MyFrame::OnQuit) + EVT_MENU(ConfTest_About, MyFrame::OnAbout) + EVT_MENU(ConfTest_Delete, MyFrame::OnDelete) END_EVENT_TABLE() // ============================================================================ @@ -79,6 +79,7 @@ END_EVENT_TABLE() // ---------------------------------------------------------------------------- // application // ---------------------------------------------------------------------------- + IMPLEMENT_APP(MyApp) // `Main program' equivalent, creating windows and returning main app frame @@ -94,11 +95,16 @@ bool MyApp::OnInit() // of the config file/registry key and must be set before the first call // to Get() if you want to override the default values (the application // name is the name of the executable and the vendor name is the same) - SetVendorName("wxWindows"); - SetAppName("conftest"); // not needed, it's the default value + SetVendorName(_T("wxWidgets")); + SetAppName(_T("conftest")); // not needed, it's the default value wxConfigBase *pConfig = wxConfigBase::Get(); + // uncomment this to force writing back of the defaults for all values + // if they're not present in the config - this can give the user an idea + // of all possible settings for this program + pConfig->SetRecordDefaults(); + // or you could also write something like this: // wxFileConfig *pConfig = new wxFileConfig("conftest"); // wxConfigBase::Set(pConfig); @@ -108,19 +114,19 @@ bool MyApp::OnInit() // create the main program window MyFrame *frame = new MyFrame; - frame->Show(TRUE); + frame->Show(true); SetTopWindow(frame); // use our config object... - if ( pConfig->Read("/Controls/Check", 1l) != 0 ) { - wxMessageBox("You can disable this message box by unchecking\n" - "the checkbox in the main window (of course, a real\n" - "program would have a checkbox right here but we\n" - "keep it simple)", "Welcome to wxConfig demo", + if ( pConfig->Read(_T("/Controls/Check"), 1l) != 0 ) { + wxMessageBox(_T("You can disable this message box by unchecking\n") + _T("the checkbox in the main window (of course, a real\n") + _T("program would have a checkbox right here but we\n") + _T("keep it simple)"), _T("Welcome to wxConfig demo"), wxICON_INFORMATION | wxOK); } - return TRUE; + return true; } int MyApp::OnExit() @@ -139,34 +145,30 @@ int MyApp::OnExit() // main frame ctor MyFrame::MyFrame() - : wxFrame((wxFrame *) NULL, -1, "wxConfig Demo") + : wxFrame((wxFrame *) NULL, wxID_ANY, _T("wxConfig Demo")) { - // submenu - wxMenu *sub_menu = new wxMenu( wxEmptyString, wxMENU_TEAROFF ); - sub_menu->Append(Minimal_About, "&About", "About this sample"); - sub_menu->Append(Minimal_About, "&About", "About this sample"); - sub_menu->Append(Minimal_About, "&About", "About this sample"); - // menu wxMenu *file_menu = new wxMenu; - file_menu->Append(Minimal_Delete, "&Delete", "Delete config file"); + file_menu->Append(ConfTest_Delete, _T("&Delete"), _T("Delete config file")); file_menu->AppendSeparator(); - file_menu->Append(Minimal_About, "&About", "About this sample"); + file_menu->Append(ConfTest_About, _T("&About\tF1"), _T("About this sample")); file_menu->AppendSeparator(); - file_menu->Append(Minimal_Quit, "E&xit", "Exit the program"); + file_menu->Append(ConfTest_Quit, _T("E&xit\tAlt-X"), _T("Exit the program")); wxMenuBar *menu_bar = new wxMenuBar; - menu_bar->Append(file_menu, "&File"); + menu_bar->Append(file_menu, _T("&File")); SetMenuBar(menu_bar); +#if wxUSE_STATUSBAR CreateStatusBar(); +#endif // wxUSE_STATUSBAR // child controls wxPanel *panel = new wxPanel(this); - (void)new wxStaticText(panel, -1, "These controls remember their values!", + (void)new wxStaticText(panel, wxID_ANY, _T("These controls remember their values!"), wxPoint(10, 10), wxSize(300, 20)); - m_text = new wxTextCtrl(panel, -1, "", wxPoint(10, 40), wxSize(300, 20)); - m_check = new wxCheckBox(panel, -1, "show welcome message box at startup", + m_text = new wxTextCtrl(panel, wxID_ANY, _T(""), wxPoint(10, 40), wxSize(300, 20)); + m_check = new wxCheckBox(panel, wxID_ANY, _T("show welcome message box at startup"), wxPoint(10, 70), wxSize(300, 20)); // restore the control's values from the config @@ -179,45 +181,59 @@ MyFrame::MyFrame() wxConfigBase *pConfig = wxConfigBase::Get(); // we could write Read("/Controls/Text") as well, it's just to show SetPath() - pConfig->SetPath("/Controls"); + pConfig->SetPath(_T("/Controls")); - m_text->SetValue(pConfig->Read("Text", "")); - m_check->SetValue(pConfig->Read("Check", 1l) != 0); + m_text->SetValue(pConfig->Read(_T("Text"), _T(""))); + m_check->SetValue(pConfig->Read(_T("Check"), 1l) != 0); // SetPath() understands ".." - pConfig->SetPath("../MainFrame"); + pConfig->SetPath(_T("../MainFrame")); // restore frame position and size - int x = pConfig->Read("x", 50), - y = pConfig->Read("y", 50), - w = pConfig->Read("w", 350), - h = pConfig->Read("h", 200); + int x = pConfig->Read(_T("x"), 50), + y = pConfig->Read(_T("y"), 50), + w = pConfig->Read(_T("w"), 350), + h = pConfig->Read(_T("h"), 200); Move(x, y); SetClientSize(w, h); -} -void MyFrame::OnCloseWindow(wxCloseEvent& event) -{ - this->Destroy(); + pConfig->SetPath(_T("/")); + wxString s; + if ( pConfig->Read(_T("TestValue"), &s) ) + { + wxLogStatus(this, wxT("TestValue from config is '%s'"), s.c_str()); + } + else + { + wxLogStatus(this, wxT("TestValue not found in the config")); + } } void MyFrame::OnQuit(wxCommandEvent&) { - Close(TRUE); + Close(true); } void MyFrame::OnAbout(wxCommandEvent&) { - wxMessageBox(_T("wxConfig demo\n© Vadim Zeitlin 1998"), _T("About"), + wxMessageBox(_T("wxConfig demo\n(c) 1998-2001 Vadim Zeitlin"), _T("About"), wxICON_INFORMATION | wxOK); } void MyFrame::OnDelete(wxCommandEvent&) { - if ( wxConfigBase::Get()->DeleteAll() ) { + wxConfigBase *pConfig = wxConfigBase::Get(); + if ( pConfig == NULL ) + { + wxLogError(_T("No config to delete!")); + return; + } + + if ( pConfig->DeleteAll() ) + { wxLogMessage(_T("Config file/registry key successfully deleted.")); - delete wxConfigBase::Set((wxConfigBase *) NULL); + delete wxConfigBase::Set(NULL); wxConfigBase::DontCreateOnDemand(); } else @@ -228,19 +244,23 @@ void MyFrame::OnDelete(wxCommandEvent&) MyFrame::~MyFrame() { - // save the control's values to the config wxConfigBase *pConfig = wxConfigBase::Get(); if ( pConfig == NULL ) return; - pConfig->Write("/Controls/Text", m_text->GetValue()); - pConfig->Write("/Controls/Check", m_check->GetValue()); + + // save the control's values to the config + pConfig->Write(_T("/Controls/Text"), m_text->GetValue()); + pConfig->Write(_T("/Controls/Check"), m_check->GetValue()); // save the frame position int x, y, w, h; GetClientSize(&w, &h); GetPosition(&x, &y); - pConfig->Write("/MainFrame/x", (long) x); - pConfig->Write("/MainFrame/y", (long) y); - pConfig->Write("/MainFrame/w", (long) w); - pConfig->Write("/MainFrame/h", (long) h); + pConfig->Write(_T("/MainFrame/x"), (long) x); + pConfig->Write(_T("/MainFrame/y"), (long) y); + pConfig->Write(_T("/MainFrame/w"), (long) w); + pConfig->Write(_T("/MainFrame/h"), (long) h); + + pConfig->Write(_T("/TestValue"), wxT("A test value")); } +