From 0be4095abb8ef117e63e1db7f9d80831353a9e7c Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 3 Aug 1998 22:37:42 +0000 Subject: [PATCH] wxConfig sample + wxGTK makefiles git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@422 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- samples/config/Makefile | 1 + samples/config/Makefile.in | 24 ++++ samples/config/conftest.cpp | 232 ++++++++++++++++++++++++++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 samples/config/Makefile create mode 100644 samples/config/Makefile.in create mode 100644 samples/config/conftest.cpp diff --git a/samples/config/Makefile b/samples/config/Makefile new file mode 100644 index 0000000000..027d82ae19 --- /dev/null +++ b/samples/config/Makefile @@ -0,0 +1 @@ +include ../../src/gtk/setup/general/makeapp diff --git a/samples/config/Makefile.in b/samples/config/Makefile.in new file mode 100644 index 0000000000..95613d0b42 --- /dev/null +++ b/samples/config/Makefile.in @@ -0,0 +1,24 @@ +# WXXT base directory +WXBASEDIR=@WXBASEDIR@ + +# set the OS type for compilation +OS=@OS@ +# compile a library only +RULE=bin + +# define library name +BIN_TARGET=conftest +# define library sources +BIN_SRC=conftest.cpp + +#define library objects +BIN_OBJ=conftest.o + +# additional things needed to link +BIN_LINK= + +# additional things needed to compile +ADD_COMPILE= + +# include the definitions now +include ../../../template.mak diff --git a/samples/config/conftest.cpp b/samples/config/conftest.cpp new file mode 100644 index 0000000000..b44a5b33c4 --- /dev/null +++ b/samples/config/conftest.cpp @@ -0,0 +1,232 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: conftest.cpp +// Purpose: demo of wxConfig and related classes +// Author: Vadim Zeitlin +// Modified by: +// Created: 03.08.98 +// RCS-ID: $Id$ +// Copyright: (c) 1998 Vadim Zeitlin +// Licence: wxWindows license +/////////////////////////////////////////////////////////////////////////////// + +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- +#include "wx/wxprec.h" + +#ifndef WX_PRECOMP + #include "wx/wx.h" +#endif //precompiled headers + +#include "wx/log.h" +#include "wx/config.h" + +// ---------------------------------------------------------------------------- +// classes +// ---------------------------------------------------------------------------- +class MyApp: public wxApp +{ +public: + // implement base class virtuals + virtual bool OnInit(); + virtual int OnExit(); +}; + +class MyFrame: public wxFrame +{ +public: + MyFrame(); + virtual ~MyFrame(); + + // callbacks + void OnQuit(wxCommandEvent& event); + void OnAbout(wxCommandEvent& event); + void OnDelete(wxCommandEvent& event); + bool OnClose() { return TRUE; } + +private: + wxTextCtrl *m_text; + wxCheckBox *m_check; + + DECLARE_EVENT_TABLE() +}; + +enum +{ + Minimal_Quit, + Minimal_About, + Minimal_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) +END_EVENT_TABLE() + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// application +// ---------------------------------------------------------------------------- +IMPLEMENT_APP(MyApp) + +// `Main program' equivalent, creating windows and returning main app frame +bool MyApp::OnInit() +{ + // we're using wxConfig's "create-on-demand" feature: it will create the + // config object when it's used for the first time. It has a number of + // advantages compared with explicitly creating our wxConfig: + // 1) we don't pay for it if we don't use it + // 2) there is no danger to create it twice + + // application and vendor name are used by wxConfig to construct the name + // 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 + + wxConfigBase *pConfig = wxConfigBase::Get(); + + // or you could also write something like this: + // wxFileConfig *pConfig = new wxFileConfig("conftest"); + // wxConfigBase::Set(pConfig); + // where you can also specify the file names explicitly if you wish. + // Of course, calling Set() is optional and you only must do it if + // you want to later retrieve this pointer with Get(). + + // create the main program window + MyFrame *frame = new MyFrame; + 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", + wxICON_INFORMATION | wxOK); + } + + return TRUE; +} + +int MyApp::OnExit() +{ + // clean up: Set() returns the active config object as Get() does, but unlike + // Get() it doesn't try to create one if there is none (definitely not what + // we want here!) + delete wxConfigBase::Set(NULL); + + return 0; +} + +// ---------------------------------------------------------------------------- +// frame +// ---------------------------------------------------------------------------- + +// main frame ctor +MyFrame::MyFrame() + : wxFrame(NULL, -1, "wxConfig Demo") +{ + // menu + wxMenu *file_menu = new wxMenu; + + file_menu->Append(Minimal_Delete, "&Delete", "Delete config file"); + file_menu->AppendSeparator(); + file_menu->Append(Minimal_About, "&About", "About this sample"); + file_menu->AppendSeparator(); + file_menu->Append(Minimal_Quit, "E&xit", "Exit the program"); + wxMenuBar *menu_bar = new wxMenuBar; + menu_bar->Append(file_menu, "&File"); + SetMenuBar(menu_bar); + + CreateStatusBar(); + + // child controls + wxPanel *panel = new wxPanel(this); + (void)new wxStaticText(panel, -1, "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", + wxPoint(10, 70), wxSize(300, 20)); + + // restore the control's values from the config + + // NB: in this program, the config object is already created at this moment + // because we had called Get() from MyApp::OnInit(). However, if you later + // change the code and don't create it before this line, it won't break + // anything - unlike if you manually create wxConfig object with Create() + // or in any other way (then you must be sure to create it before using it!). + wxConfigBase *pConfig = wxConfigBase::Get(); + + // we could write Read("/Controls/Text") as well, it's just to show SetPath() + pConfig->SetPath("/Controls"); + + m_text->SetValue(pConfig->Read("Text", "")); + m_check->SetValue(pConfig->Read("Check", 1l) != 0); + + // SetPath() understands ".." + pConfig->SetPath("../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); + Move(x, y); + SetClientSize(w, h); +} + +void MyFrame::OnQuit(wxCommandEvent&) +{ + Close(TRUE); +} + +void MyFrame::OnAbout(wxCommandEvent&) +{ + wxMessageBox("wxConfig demo\n© Vadim Zeitlin 1998", "About", + wxICON_INFORMATION | wxOK); +} + +void MyFrame::OnDelete(wxCommandEvent&) +{ + if ( wxConfigBase::Get()->DeleteAll() ) { + wxLogMessage("Config file/registry key successfully deleted."); + + delete wxConfigBase::Set(NULL); + wxConfigBase::DontCreateOnDemand(); + } + else + wxLogError("Deleting config file/registry key failed."); +} + +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 frame position + int x, y, w, h; + GetClientSize(&w, &h); + GetPosition(&x, &y); + pConfig->Write("/MainFrame/x", x); + pConfig->Write("/MainFrame/y", y); + pConfig->Write("/MainFrame/w", w); + pConfig->Write("/MainFrame/h", h); +} -- 2.49.0