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"
30 #include "wx/resource.h"
34 #include "wx/fontutil.h"
35 #include "wx/univ/theme.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/colschem.h"
38 #include "wx/mgl/private.h"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 wxApp
*wxTheApp
= NULL
;
45 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 static bool gs_inYield
= FALSE
;
64 bool wxApp::Yield(bool onlyIfNeeded
)
70 wxFAIL_MSG( wxT("wxYield called recursively" ) );
77 if ( !wxThread::IsMain() )
79 // can't process events from other threads, MGL is thread-unsafe
82 #endif // wxUSE_THREADS
88 if ( wxEventLoop::GetActive() )
90 while (wxEventLoop::GetActive()->Pending())
91 wxEventLoop::GetActive()->Dispatch();
94 /* it's necessary to call ProcessIdle() to update the frames sizes which
95 might have been changed (it also will update other things set from
96 OnUpdateUI() which is a nice (and desired) side effect) */
97 while (wxTheApp
->ProcessIdle()) { }
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
114 if (!wxThread::IsMain())
118 while (wxTheApp
->ProcessIdle()) {}
121 if (!wxThread::IsMain())
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 class wxRootWindow
: public wxWindow
133 wxRootWindow() : wxWindow(NULL
, -1)
135 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
136 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
140 // we don't want to delete MGL_WM's rootWnd
144 virtual bool AcceptsFocus() { return FALSE
; }
147 static wxRootWindow
*gs_rootWindow
= NULL
;
149 //-----------------------------------------------------------------------------
150 // MGL initialization
151 //-----------------------------------------------------------------------------
153 static bool wxCreateMGL_WM(const wxDisplayModeInfo
& displayMode
)
156 int refresh
= MGL_DEFAULT_REFRESH
;
158 #if wxUSE_SYSTEM_OPTIONS
159 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh") )
160 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
163 mode
= MGL_findMode(displayMode
.GetWidth(),
164 displayMode
.GetHeight(),
165 displayMode
.GetDepth());
168 wxLogError(_("Mode %ix%i-%i not available."),
169 displayMode
.GetWidth(),
170 displayMode
.GetHeight(),
171 displayMode
.GetDepth());
174 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
175 if ( !g_displayDC
->isValid() )
182 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
189 static void wxDestroyMGL_WM()
193 MGL_wmDestroy(g_winMng
);
203 //-----------------------------------------------------------------------------
205 //-----------------------------------------------------------------------------
207 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
209 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
210 EVT_IDLE(wxApp::OnIdle
)
214 wxApp::wxApp() : m_mainLoop(NULL
)
222 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
228 if ( g_displayDC
!= NULL
)
230 // FIXME_MGL -- we currently don't allow to switch video mode
231 // more than once. This can hopefully be changed...
232 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
236 if ( !wxCreateMGL_WM(mode
) )
238 gs_rootWindow
= new wxRootWindow
;
240 m_displayMode
= mode
;
245 bool wxApp::OnInitGui()
247 if ( !wxAppBase::OnInitGui() )
251 // MGL redirects stdout and stderr to physical console, so lets redirect
252 // it to file. Do it only when WXDEBUG environment variable is set
253 if ( wxGetEnv(wxT("WXDEBUG"), NULL
) )
254 freopen("output.err", "wt", stderr
);
257 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
258 if ( oldLog
) delete oldLog
;
263 bool wxApp::ProcessIdle()
266 event
.SetEventObject(this);
269 return event
.MoreRequested();
272 void wxApp::OnIdle(wxIdleEvent
&event
)
274 static bool s_inOnIdle
= FALSE
;
276 /* Avoid recursion (via ProcessEvent default case) */
282 /* Resend in the main thread events which have been prepared in other
284 ProcessPendingEvents();
286 // 'Garbage' collection of windows deleted with Close().
287 DeletePendingObjects();
290 // flush the logged messages if any
291 wxLog::FlushActive();
294 // Send OnIdle events to all windows
295 if ( SendIdleEvents() )
296 event
.RequestMore(TRUE
);
301 bool wxApp::SendIdleEvents()
303 bool needMore
= FALSE
;
305 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
308 wxWindow
* win
= node
->GetData();
309 if ( SendIdleEvents(win
) )
311 node
= node
->GetNext();
317 bool wxApp::SendIdleEvents(wxWindow
* win
)
319 bool needMore
= FALSE
;
322 event
.SetEventObject(win
);
324 win
->GetEventHandler()->ProcessEvent(event
);
326 if ( event
.MoreRequested() )
329 wxNode
* node
= win
->GetChildren().First();
332 wxWindow
* win
= (wxWindow
*) node
->Data();
333 if ( SendIdleEvents(win
) )
341 int wxApp::MainLoop()
344 m_mainLoop
= new wxEventLoop
;
346 rt
= m_mainLoop
->Run();
353 void wxApp::ExitMainLoop()
359 bool wxApp::Initialized()
361 return (wxTopLevelWindows
.GetCount() != 0);
364 bool wxApp::Pending()
366 return wxEventLoop::GetActive()->Pending();
369 void wxApp::Dispatch()
371 wxEventLoop::GetActive()->Dispatch();
374 void wxApp::DeletePendingObjects()
376 wxNode
*node
= wxPendingDelete
.First();
379 wxObject
*obj
= (wxObject
*)node
->Data();
383 if ( wxPendingDelete
.Find(obj
) )
386 node
= wxPendingDelete
.First();
390 bool wxApp::Initialize()
392 if ( MGL_init(".", NULL
) == 0 )
395 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
397 wxClassInfo::InitializeClasses();
399 wxSystemSettings::Init();
402 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
405 // GL: I'm annoyed ... I don't know where to put this and I don't want to
406 // create a module for that as it's part of the core.
408 wxPendingEvents
= new wxList
;
409 wxPendingEventsLocker
= new wxCriticalSection
;
412 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
413 wxTheColourDatabase
->Initialize();
415 // Can't do this in wxModule, because fonts are needed by stock lists
416 wxTheFontsManager
= new wxFontsManager
;
418 wxInitializeStockLists();
419 wxInitializeStockObjects();
421 #if wxUSE_WX_RESOURCES
422 wxInitializeResourceSystem();
425 wxModule::RegisterModules();
426 if (!wxModule::InitializeModules()) return FALSE
;
431 wxIcon
wxApp::GetStdIcon(int which
) const
433 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
436 void wxApp::CleanUp()
439 // continuing to use user defined log target is unsafe from now on because
440 // some resources may be already unavailable, so replace it by something
442 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
447 delete gs_rootWindow
;
449 wxModule::CleanUpModules();
451 #if wxUSE_WX_RESOURCES
452 wxCleanUpResourceSystem();
455 if (wxTheColourDatabase
)
456 delete wxTheColourDatabase
;
458 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
460 wxDeleteStockObjects();
461 wxDeleteStockLists();
463 // Can't do this in wxModule, because fonts are needed by stock lists
464 delete wxTheFontsManager
;
465 wxTheFontsManager
= (wxFontsManager
*) NULL
;
468 wxTheApp
= (wxApp
*) NULL
;
470 // GL: I'm annoyed ... I don't know where to put this and I don't want to
471 // create a module for that as it's part of the core.
473 delete wxPendingEvents
;
474 delete wxPendingEventsLocker
;
477 wxSystemSettings::Done();
481 wxClassInfo::CleanUpClasses();
483 // check for memory leaks
484 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
485 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
487 wxLogDebug(wxT("There were memory leaks.\n"));
488 wxDebugContext::Dump();
489 wxDebugContext::PrintStatistics();
494 // do this as the very last thing because everything else can log messages
495 wxLog::DontCreateOnDemand();
497 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
507 int wxEntryStart(int argc
, char *argv
[])
509 return wxApp::Initialize() ? 0 : -1;
515 return wxTheApp
->OnInitGui() ? 0 : -1;
519 void wxEntryCleanup()
526 int wxEntry(int argc
, char *argv
[])
528 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
529 // This seems to be necessary since there are 'rogue'
530 // objects present at this point (perhaps global objects?)
531 // Setting a checkpoint will ignore them as far as the
532 // memory checking facility is concerned.
533 // Of course you may argue that memory allocated in globals should be
534 // checked, but this is a reasonable compromise.
535 wxDebugContext::SetCheckpoint();
537 int err
= wxEntryStart(argc
, argv
);
543 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
544 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
546 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
548 wxObject
*test_app
= app_ini();
550 wxTheApp
= (wxApp
*) test_app
;
553 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
555 wxTheApp
->argc
= argc
;
557 wxTheApp
->argv
= new wxChar
*[argc
+1];
559 while (mb_argc
< argc
)
561 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
564 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
566 wxTheApp
->argv
= argv
;
569 wxString
name(wxFileNameFromPath(argv
[0]));
570 wxStripExtension(name
);
571 wxTheApp
->SetAppName(name
);
574 retValue
= wxEntryInitGui();
576 // Here frames insert themselves automatically into wxTopLevelWindows by
577 // getting created in OnInit().
580 if ( !wxTheApp
->OnInit() )
586 /* delete pending toplevel windows (typically a single
587 dialog) so that, if there isn't any left, we don't
589 wxTheApp
->DeletePendingObjects();
591 if ( wxTheApp
->Initialized() )
595 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
598 /* Forcibly delete the window. */
599 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
600 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
602 topWindow
->Close(TRUE
);
603 wxTheApp
->DeletePendingObjects();
608 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
613 // flush the logged messages if any
614 wxLog
*log
= wxLog::GetActiveTarget();
615 if (log
!= NULL
&& log
->HasPendingMessages())
618 retValue
= wxTheApp
->OnExit();