Advertisement
Guest User

wxAppConsole overridden OnRun()

a guest
Feb 10th, 2011
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.88 KB | None | 0 0
  1. /** @file
  2.  * Demonstration of lack of cleanup when using an event loop with wxAppConsole.
  3.  */
  4.  
  5. // For compilers that support precompilation, includes "wx/wx.h".
  6. #include "wx/wxprec.h"
  7.  
  8. #ifdef __BORLANDC__
  9.     #pragma hdrstop
  10. #endif
  11.  
  12. // for all others, include the necessary headers (this file is usually all you
  13. // need because it includes almost all "standard" wxWidgets headers)
  14. #ifndef WX_PRECOMP
  15.     #include "wx/wx.h"
  16. #endif
  17.  
  18. #include <wx/app.h>
  19. #include <wx/wfstream.h>
  20. #include <wx/txtstrm.h>
  21.  
  22. // Settings I use, not sure if they are needed.
  23. #define __WXMSW__
  24. #define __WXDEBUG__
  25. #ifdef wxUSE_GUI
  26. #undef wxUSE_GUI
  27. #define wxUSE_GUI 0
  28. #endif //#ifdef wxUSE_GUI
  29.  
  30.  
  31. /**
  32.  * Use these to determine program flow.
  33.  */
  34. #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).
  35. #ifdef OVERRIDE_ON_RUN_METHOD
  36.   //#define NO_EVENT_LOOP  // Don't call base class wxAppConsole::OnRun(), return 0 from overridden wxAppConsole::OnRun(). Comment out to get default event loop.
  37.   #ifndef NO_EVENT_LOOP
  38.     #define DEFAULT_EVENT_LOOP // Use the default loop in wxAppConsole::OnRun(). Comment out to get custom event loop.
  39.     #ifndef DEFAULT_EVENT_LOOP
  40.       #define CUSTOM_EVENT_LOOP // Use custom loop in overridden wxAppConsole::OnRun().
  41.     #endif
  42.   #endif
  43. #endif //#ifdef OVERRIDE_ON_RUN_METHOD
  44.  
  45.  
  46. class MyAppConsole : public wxAppConsole
  47. {
  48. public:
  49.   MyAppConsole();
  50.   virtual ~MyAppConsole();
  51.  
  52. protected:
  53.   // Event Handlers
  54.   void OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop));
  55. #ifdef OVERRIDE_ON_RUN_METHOD
  56.   virtual int OnRun();
  57. #endif //#ifdef OVERRIDE_ON_RUN_METHOD
  58.   virtual int OnExit();
  59.   virtual int FilterEvent(wxEvent& rEvent);
  60.  
  61.   wxString* m_pString;
  62. };
  63.  
  64. /**
  65.  * Creates a forward declaration of CAppConsole, so that other files
  66.  *  may use wxGetApp() to get a refernce to this object.
  67.  */
  68. wxDECLARE_APP(MyAppConsole);
  69.  
  70. /**
  71.  * Tell wxWidgets to make the entry point to this program based off of this
  72.  * wxAppConsole.
  73.  */
  74. wxIMPLEMENT_APP_CONSOLE(MyAppConsole);
  75.  
  76. void myAtExitFunction(void)
  77. {
  78.   wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
  79.   wxTextOutputStream fout(fFileStream);
  80.   fout << wxT("atexit event called") << "\n";
  81. }
  82.  
  83. MyAppConsole::MyAppConsole() :
  84.   wxAppConsole()
  85. {
  86.   this->m_pString = new wxString(wxT("Hello World"));
  87.   // Clean up the "log" files.
  88.   wxRemoveFile(wxT("Events.txt"));
  89.   wxFFileOutputStream fFileStream(wxT("Events.txt"));
  90.   wxTextOutputStream fout(fFileStream);
  91.   fout << wxT("Constructor called") << "\n";
  92.   atexit(myAtExitFunction);
  93. }
  94.  
  95. MyAppConsole::~MyAppConsole()
  96. {
  97.   // Not reached if no return value from an overridden wxAppConsole::OnRun()
  98.   wxDELETE(this->m_pString);
  99.   wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
  100.   wxTextOutputStream fout(fFileStream);
  101.   fout << wxT("Destructor called") << "\n";
  102. }
  103.  
  104. void MyAppConsole::OnEventLoopEnter(wxEventLoopBase* WXUNUSED(loop))
  105. {
  106.   // Set breakpoint here to see if an event loop started.  Or watch for file.
  107.   (*this->m_pString) = wxT("An event loop started!");
  108.   wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
  109.   wxTextOutputStream fout(fFileStream);
  110.   fout << wxT("An Event Loop was entered") << "\n";
  111. }
  112.  
  113. #ifdef OVERRIDE_ON_RUN_METHOD
  114. int MyAppConsole::OnRun()
  115. {
  116. #ifdef CUSTOM_EVENT_LOOP
  117.   wxEventLoop* loop = new wxEventLoop();
  118.   ProcessPendingEvents();
  119.   loop->Run();
  120.   return 0; // Not reached if custom event loop used.
  121. #endif //#ifdef CUSTOM_EVENT_LOOP
  122.  
  123. #ifdef DEFAULT_EVENT_LOOP
  124.   return wxAppConsole::OnRun(); // Default event loop.
  125. #endif //#ifndef CUSTOM_EVENT_LOOP
  126.  
  127. #ifdef NO_EVENT_LOOP
  128.   return 0; // Don't use an event loop.  Exit program.
  129. #endif //#ifdef NO_EVENT_LOOP
  130. }
  131. #endif //#ifdef OVERRIDE_ON_RUN_METHOD
  132.  
  133. /**
  134.  * Override this member function for any processing which needs to be done as
  135.  *  the application is about to exit.
  136.  * OnExit is called after destroying all application windows and controls, but
  137.  *  before wxWidgets cleanup. Note that it is not called at all if OnInit()
  138.  *  failed.
  139.  * The return value of this function is currently ignored, return the same
  140.  *  value as returned by the base class method if you override it.
  141.  */
  142. int MyAppConsole::OnExit()
  143. {
  144.   wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
  145.   wxTextOutputStream fout(fFileStream);
  146.   fout << wxT("OnExit() called") << "\n";
  147.   return wxAppConsole::OnExit();
  148. }
  149.  
  150. /**
  151.  * See: http://docs.wxwidgets.org/trunk/overview_events.html#overview_events_processing
  152.  */
  153. int MyAppConsole::FilterEvent(wxEvent& rEvent)
  154. {
  155.   // Set breakpoint here if interested in what events occur.
  156.   rEvent.Skip();
  157.   wxFFileOutputStream fFileStream(wxT("Events.txt"), wxT("a"));
  158.   wxTextOutputStream fout(fFileStream);
  159.   fout << wxT("EventName: ") << rEvent.GetClassInfo()->GetClassName() << "\n";
  160.   return false;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement