1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
4 // based on GTK and MSW implementations
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "app.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
23 #include "wx/settings.h"
24 #include "wx/module.h"
25 #include "wx/evtloop.h"
27 #include "wx/dialog.h"
33 #include "wx/fontutil.h"
34 #include "wx/univ/theme.h"
35 #include "wx/univ/renderer.h"
36 #include "wx/univ/colschem.h"
37 #include "wx/mgl/private.h"
41 #if defined(MGL_DEBUG) && !defined(__WXDEBUG__)
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 wxApp
*wxTheApp
= NULL
;
50 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
67 static bool gs_inYield
= FALSE
;
69 bool wxApp::Yield(bool onlyIfNeeded
)
75 wxFAIL_MSG( wxT("wxYield called recursively" ) );
82 if ( !wxThread::IsMain() )
84 // can't process events from other threads, MGL is thread-unsafe
87 #endif // wxUSE_THREADS
93 if ( wxEventLoop::GetActive() )
95 while (wxEventLoop::GetActive()->Pending())
96 wxEventLoop::GetActive()->Dispatch();
99 /* it's necessary to call ProcessIdle() to update the frames sizes which
100 might have been changed (it also will update other things set from
101 OnUpdateUI() which is a nice (and desired) side effect) */
102 while (wxTheApp
->ProcessIdle()) { }
112 //-----------------------------------------------------------------------------
114 //-----------------------------------------------------------------------------
119 if (!wxThread::IsMain())
123 while (wxTheApp
->ProcessIdle()) {}
126 if (!wxThread::IsMain())
131 //-----------------------------------------------------------------------------
133 //-----------------------------------------------------------------------------
135 class wxRootWindow
: public wxWindow
138 wxRootWindow() : wxWindow(NULL
, -1)
140 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
141 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
145 // we don't want to delete MGL_WM's rootWnd
149 virtual bool AcceptsFocus() { return FALSE
; }
152 static wxRootWindow
*gs_rootWindow
= NULL
;
154 //-----------------------------------------------------------------------------
155 // MGL initialization
156 //-----------------------------------------------------------------------------
158 static bool wxCreateMGL_WM()
161 int width
= 640, height
= 480, depth
= 16;
162 int refresh
= MGL_DEFAULT_REFRESH
;
164 #if wxUSE_SYSTEM_OPTIONS
165 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh") )
166 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
169 mode
= MGL_findMode(width
, height
, depth
);
172 wxLogWarning(_("Mode %ix%i-%i not available, falling back to default mode."), width
, height
, depth
);
173 mode
= 0; // always available
175 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
176 if ( !g_displayDC
->isValid() )
183 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
190 static void wxDestroyMGL_WM()
194 MGL_wmDestroy(g_winMng
);
204 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------
208 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
210 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
211 EVT_IDLE(wxApp::OnIdle
)
215 wxApp::wxApp() : m_mainLoop(NULL
)
223 bool wxApp::OnInitGui()
225 if ( !wxCreateMGL_WM() )
228 // This has to be done *after* wxCreateMGL_WM() because it initializes
230 if ( !wxAppBase::OnInitGui() )
233 // ...and this has to be done after wxUniv themes were initialized
234 gs_rootWindow
= new wxRootWindow
;
237 // That damn MGL redirects stdin and stdout to physical console
238 FILE *file
= fopen("stderr", "wt");
239 wxLog::SetActiveTarget(new wxLogStderr(file
));
245 bool wxApp::ProcessIdle()
248 event
.SetEventObject(this);
251 return event
.MoreRequested();
254 void wxApp::OnIdle(wxIdleEvent
&event
)
256 static bool s_inOnIdle
= FALSE
;
258 /* Avoid recursion (via ProcessEvent default case) */
264 /* Resend in the main thread events which have been prepared in other
266 ProcessPendingEvents();
268 // 'Garbage' collection of windows deleted with Close().
269 DeletePendingObjects();
271 // Send OnIdle events to all windows
272 if ( SendIdleEvents() )
273 event
.RequestMore(TRUE
);
278 bool wxApp::SendIdleEvents()
280 bool needMore
= FALSE
;
282 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
285 wxWindow
* win
= node
->GetData();
286 if ( SendIdleEvents(win
) )
288 node
= node
->GetNext();
294 bool wxApp::SendIdleEvents(wxWindow
* win
)
296 bool needMore
= FALSE
;
299 event
.SetEventObject(win
);
301 win
->GetEventHandler()->ProcessEvent(event
);
303 if ( event
.MoreRequested() )
306 wxNode
* node
= win
->GetChildren().First();
309 wxWindow
* win
= (wxWindow
*) node
->Data();
310 if ( SendIdleEvents(win
) )
318 int wxApp::MainLoop()
321 m_mainLoop
= new wxEventLoop
;
323 rt
= m_mainLoop
->Run();
330 void wxApp::ExitMainLoop()
336 bool wxApp::Initialized()
338 return (wxTopLevelWindows
.GetCount() != 0);
341 bool wxApp::Pending()
343 return wxEventLoop::GetActive()->Pending();
346 void wxApp::Dispatch()
348 wxEventLoop::GetActive()->Dispatch();
351 void wxApp::DeletePendingObjects()
353 wxNode
*node
= wxPendingDelete
.First();
356 wxObject
*obj
= (wxObject
*)node
->Data();
360 if ( wxPendingDelete
.Find(obj
) )
363 node
= wxPendingDelete
.First();
367 bool wxApp::Initialize()
369 if ( MGL_init(".", NULL
) == 0 )
372 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
374 wxClassInfo::InitializeClasses();
376 wxSystemSettings::Init();
379 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
382 // GL: I'm annoyed ... I don't know where to put this and I don't want to
383 // create a module for that as it's part of the core.
385 wxPendingEvents
= new wxList
;
386 wxPendingEventsLocker
= new wxCriticalSection
;
389 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
390 wxTheColourDatabase
->Initialize();
392 // Can't do this in wxModule, because fonts are needed by stock lists
393 wxTheFontsManager
= new wxFontsManager
;
395 wxInitializeStockLists();
396 wxInitializeStockObjects();
398 #if wxUSE_WX_RESOURCES
399 wxInitializeResourceSystem();
402 wxModule::RegisterModules();
403 if (!wxModule::InitializeModules()) return FALSE
;
408 wxIcon
wxApp::GetStdIcon(int which
) const
410 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
413 void wxApp::CleanUp()
415 delete gs_rootWindow
;
418 // flush the logged messages if any
419 wxLog
*log
= wxLog::GetActiveTarget();
420 if (log
!= NULL
&& log
->HasPendingMessages())
423 // continuing to use user defined log target is unsafe from now on because
424 // some resources may be already unavailable, so replace it by something
426 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
431 wxModule::CleanUpModules();
433 #if wxUSE_WX_RESOURCES
434 wxCleanUpResourceSystem();
437 if (wxTheColourDatabase
)
438 delete wxTheColourDatabase
;
440 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
442 wxDeleteStockObjects();
443 wxDeleteStockLists();
445 // Can't do this in wxModule, because fonts are needed by stock lists
446 delete wxTheFontsManager
;
447 wxTheFontsManager
= (wxFontsManager
*) NULL
;
450 wxTheApp
= (wxApp
*) NULL
;
452 // GL: I'm annoyed ... I don't know where to put this and I don't want to
453 // create a module for that as it's part of the core.
455 delete wxPendingEvents
;
456 delete wxPendingEventsLocker
;
459 wxSystemSettings::Done();
463 wxClassInfo::CleanUpClasses();
465 // check for memory leaks
466 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
467 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
469 wxLogDebug(wxT("There were memory leaks.\n"));
470 wxDebugContext::Dump();
471 wxDebugContext::PrintStatistics();
476 // do this as the very last thing because everything else can log messages
477 wxLog::DontCreateOnDemand();
479 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
489 int wxEntryStart(int argc
, char *argv
[])
491 return wxApp::Initialize() ? 0 : -1;
497 return wxTheApp
->OnInitGui() ? 0 : -1;
501 void wxEntryCleanup()
508 int wxEntry(int argc
, char *argv
[])
510 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
511 // This seems to be necessary since there are 'rogue'
512 // objects present at this point (perhaps global objects?)
513 // Setting a checkpoint will ignore them as far as the
514 // memory checking facility is concerned.
515 // Of course you may argue that memory allocated in globals should be
516 // checked, but this is a reasonable compromise.
517 wxDebugContext::SetCheckpoint();
519 int err
= wxEntryStart(argc
, argv
);
525 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
526 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
528 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
530 wxObject
*test_app
= app_ini();
532 wxTheApp
= (wxApp
*) test_app
;
535 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
537 wxTheApp
->argc
= argc
;
539 wxTheApp
->argv
= new wxChar
*[argc
+1];
541 while (mb_argc
< argc
)
543 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
546 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
548 wxTheApp
->argv
= argv
;
551 wxString
name(wxFileNameFromPath(argv
[0]));
552 wxStripExtension(name
);
553 wxTheApp
->SetAppName(name
);
556 retValue
= wxEntryInitGui();
558 // Here frames insert themselves automatically into wxTopLevelWindows by
559 // getting created in OnInit().
562 if ( !wxTheApp
->OnInit() )
568 /* delete pending toplevel windows (typically a single
569 dialog) so that, if there isn't any left, we don't
571 wxTheApp
->DeletePendingObjects();
573 if ( wxTheApp
->Initialized() )
577 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
580 /* Forcibly delete the window. */
581 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
582 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
584 topWindow
->Close(TRUE
);
585 wxTheApp
->DeletePendingObjects();
590 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
594 retValue
= wxTheApp
->OnExit();