Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @file
- * Demonstration of lack of cleanup when using an event loop with wxAppConsole.
- */
- // For compilers that support precompilation, includes "wx/wx.h".
- #include "wx/wxprec.h"
- #ifdef __BORLANDC__
- #pragma hdrstop
- #endif
- // for all others, include the necessary headers (this file is usually all you
- // need because it includes almost all "standard" wxWidgets headers)
- #ifndef WX_PRECOMP
- #include "wx/wx.h"
- #endif
- #include <wx/app.h>
- #include <wx/wfstream.h>
- #include <wx/txtstrm.h>
- // Settings I use, not sure if they are needed.
- #define __WXMSW__
- #define __WXDEBUG__
- #ifdef wxUSE_GUI
- #undef wxUSE_GUI
- #define wxUSE_GUI 0
- #endif //#ifdef wxUSE_GUI
- /**
- * Use these to determine program flow.
- */
- #define OVERRIDE_ON_RUN_METHOD // Can choose not to override wxAppConsole::OnRun(). Base class's wxAppConsole::OnRun() is run (which contains the default event loop).
- #ifdef OVERRIDE_ON_RUN_METHOD
- //#define NO_EVENT_LOOP // Don't call base class wxAppConsole::OnRun(), return 0 from overridden wxAppConsole::OnRun(). Comment out to get default event loop.
- #ifndef NO_EVENT_LOOP
- #define DEFAULT_EVENT_LOOP // Use the default loop in wxAppConsole::OnRun(). Comment out to get custom event loop.
- #ifndef DEFAULT_EVENT_LOOP
- #define CUSTOM_EVENT_LOOP // Use custom loop in overridden wxAppConsole::OnRun().
- #endif
- #endif
- #endif //#ifdef OVERRIDE_ON_RUN_METHOD
- class MyAppConsole : public wxAppConsole
- {
- public:
- MyAppConsole();
- virtual ~MyAppConsole();
- protected:
- // Event Handlers
- void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop));
- #ifdef OVERRIDE_ON_RUN_METHOD
- virtual int OnRun();
- #endif //#ifdef OVERRIDE_ON_RUN_METHOD
- virtual int OnExit();
- virtual int FilterEvent(wxEvent& rEvent);
- wxString* m_pString;
- };
- /**
- * Creates a forward declaration of CAppConsole, so that other files
- * may use wxGetApp() to get a refernce to this object.
- */
- wxDECLARE_APP(MyAppConsole);
- /**
- * Tell wxWidgets to make the entry point to this program based off of this
- * wxAppConsole.
- */
- wxIMPLEMENT_APP_CONSOLE(MyAppConsole);
- void myAtExitFunction(void)
- {
- wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
- wxTextOutputStream fout(fFileStream);
- fout << wxT("atexit event called") << "\n";
- }
- MyAppConsole::MyAppConsole() :
- wxAppConsole()
- {
- this->m_pString = new wxString(wxT("Hello World"));
- // Clean up the "log" files.
- wxRemoveFile(wxT("Events.txt"));
- wxFFileOutputStream fFileStream(wxT("Events.txt"));
- wxTextOutputStream fout(fFileStream);
- fout << wxT("Constructor called") << "\n";
- atexit(myAtExitFunction);
- }
- MyAppConsole::~MyAppConsole()
- {
- // Not reached if no return value from an overridden wxAppConsole::OnRun()
- wxDELETE(this->m_pString);
- wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
- wxTextOutputStream fout(fFileStream);
- fout << wxT("Destructor called") << "\n";
- }
- void MyAppConsole::OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
- {
- // Set breakpoint here to see if an event loop started. Or watch for file.
- (*this->m_pString) = wxT("An event loop started!");
- wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
- wxTextOutputStream fout(fFileStream);
- fout << wxT("An Event Loop was entered") << "\n";
- }
- #ifdef OVERRIDE_ON_RUN_METHOD
- int MyAppConsole::OnRun()
- {
- #ifdef CUSTOM_EVENT_LOOP
- wxEventLoop* loop = new wxEventLoop();
- ProcessPendingEvents();
- loop->Run();
- return 0; // Not reached if custom event loop used.
- #endif //#ifdef CUSTOM_EVENT_LOOP
- #ifdef DEFAULT_EVENT_LOOP
- return wxAppConsole::OnRun(); // Default event loop.
- #endif //#ifndef CUSTOM_EVENT_LOOP
- #ifdef NO_EVENT_LOOP
- return 0; // Don't use an event loop. Exit program.
- #endif //#ifdef NO_EVENT_LOOP
- }
- #endif //#ifdef OVERRIDE_ON_RUN_METHOD
- /**
- * Override this member function for any processing which needs to be done as
- * the application is about to exit.
- * OnExit is called after destroying all application windows and controls, but
- * before wxWidgets cleanup. Note that it is not called at all if OnInit()
- * failed.
- * The return value of this function is currently ignored, return the same
- * value as returned by the base class method if you override it.
- */
- int MyAppConsole::OnExit()
- {
- wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
- wxTextOutputStream fout(fFileStream);
- fout << wxT("OnExit() called") << "\n";
- return wxAppConsole::OnExit();
- }
- /**
- * See: http://docs.wxwidgets.org/trunk/overview_events.html#overview_events_processing
- */
- int MyAppConsole::FilterEvent(wxEvent& rEvent)
- {
- // Set breakpoint here if interested in what events occur.
- rEvent.Skip();
- wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
- wxTextOutputStream fout(fFileStream);
- fout << wxT("EventName: ") << rEvent.GetClassInfo()->GetClassName() << "\n";
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement