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/sysopt.h"
39 #include "wx/mgl/private.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 wxApp
*wxTheApp
= NULL
;
46 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 static bool gs_inYield
= FALSE
;
65 bool wxApp::Yield(bool onlyIfNeeded
)
71 wxFAIL_MSG( wxT("wxYield called recursively" ) );
78 if ( !wxThread::IsMain() )
80 // can't process events from other threads, MGL is thread-unsafe
83 #endif // wxUSE_THREADS
89 if ( wxEventLoop::GetActive() )
91 while (wxEventLoop::GetActive()->Pending())
92 wxEventLoop::GetActive()->Dispatch();
95 /* it's necessary to call ProcessIdle() to update the frames sizes which
96 might have been changed (it also will update other things set from
97 OnUpdateUI() which is a nice (and desired) side effect) */
98 while (wxTheApp
->ProcessIdle()) { }
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
115 if (!wxThread::IsMain())
119 while (wxTheApp
->ProcessIdle()) {}
122 if (!wxThread::IsMain())
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 class wxRootWindow
: public wxWindow
134 wxRootWindow() : wxWindow(NULL
, -1)
136 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
137 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
141 // we don't want to delete MGL_WM's rootWnd
145 virtual bool AcceptsFocus() { return FALSE
; }
148 static wxRootWindow
*gs_rootWindow
= NULL
;
150 //-----------------------------------------------------------------------------
151 // MGL initialization
152 //-----------------------------------------------------------------------------
154 static bool wxCreateMGL_WM(const wxDisplayModeInfo
& displayMode
)
157 int refresh
= MGL_DEFAULT_REFRESH
;
159 #if wxUSE_SYSTEM_OPTIONS
160 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
161 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
164 mode
= MGL_findMode(displayMode
.GetWidth(),
165 displayMode
.GetHeight(),
166 displayMode
.GetDepth());
169 wxLogError(_("Mode %ix%i-%i not available."),
170 displayMode
.GetWidth(),
171 displayMode
.GetHeight(),
172 displayMode
.GetDepth());
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::SetDisplayMode(const wxDisplayModeInfo
& mode
)
229 if ( g_displayDC
!= NULL
)
231 // FIXME_MGL -- we currently don't allow to switch video mode
232 // more than once. This can hopefully be changed...
233 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
237 if ( !wxCreateMGL_WM(mode
) )
239 gs_rootWindow
= new wxRootWindow
;
241 m_displayMode
= mode
;
246 bool wxApp::OnInitGui()
248 if ( !wxAppBase::OnInitGui() )
252 // MGL redirects stdout and stderr to physical console, so lets redirect
253 // it to file. Do it only when WXDEBUG environment variable is set
254 if ( wxGetEnv(wxT("WXDEBUG"), NULL
) )
255 freopen("output.err", "wt", stderr
);
258 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
259 if ( oldLog
) delete oldLog
;
264 bool wxApp::ProcessIdle()
267 event
.SetEventObject(this);
270 return event
.MoreRequested();
273 void wxApp::OnIdle(wxIdleEvent
&event
)
275 static bool s_inOnIdle
= FALSE
;
277 /* Avoid recursion (via ProcessEvent default case) */
283 /* Resend in the main thread events which have been prepared in other
285 ProcessPendingEvents();
287 // 'Garbage' collection of windows deleted with Close().
288 DeletePendingObjects();
291 // flush the logged messages if any
292 wxLog::FlushActive();
295 // Send OnIdle events to all windows
296 if ( SendIdleEvents() )
297 event
.RequestMore(TRUE
);
302 bool wxApp::SendIdleEvents()
304 bool needMore
= FALSE
;
306 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
309 wxWindow
* win
= node
->GetData();
310 if ( SendIdleEvents(win
) )
312 node
= node
->GetNext();
318 bool wxApp::SendIdleEvents(wxWindow
* win
)
320 bool needMore
= FALSE
;
323 event
.SetEventObject(win
);
325 win
->GetEventHandler()->ProcessEvent(event
);
327 if ( event
.MoreRequested() )
330 wxNode
* node
= win
->GetChildren().First();
333 wxWindow
* win
= (wxWindow
*) node
->Data();
334 if ( SendIdleEvents(win
) )
342 int wxApp::MainLoop()
345 m_mainLoop
= new wxEventLoop
;
347 rt
= m_mainLoop
->Run();
354 void wxApp::ExitMainLoop()
360 bool wxApp::Initialized()
362 return (wxTopLevelWindows
.GetCount() != 0);
365 bool wxApp::Pending()
367 return wxEventLoop::GetActive()->Pending();
370 void wxApp::Dispatch()
372 wxEventLoop::GetActive()->Dispatch();
375 void wxApp::DeletePendingObjects()
377 wxNode
*node
= wxPendingDelete
.First();
380 wxObject
*obj
= (wxObject
*)node
->Data();
384 if ( wxPendingDelete
.Find(obj
) )
387 node
= wxPendingDelete
.First();
391 bool wxApp::Initialize()
393 if ( MGL_init(".", NULL
) == 0 )
396 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
398 wxClassInfo::InitializeClasses();
400 wxSystemSettings::Init();
403 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
406 // GL: I'm annoyed ... I don't know where to put this and I don't want to
407 // create a module for that as it's part of the core.
409 wxPendingEvents
= new wxList
;
410 wxPendingEventsLocker
= new wxCriticalSection
;
413 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
414 wxTheColourDatabase
->Initialize();
416 // Can't do this in wxModule, because fonts are needed by stock lists
417 wxTheFontsManager
= new wxFontsManager
;
419 wxInitializeStockLists();
420 wxInitializeStockObjects();
422 #if wxUSE_WX_RESOURCES
423 wxInitializeResourceSystem();
426 wxModule::RegisterModules();
427 if (!wxModule::InitializeModules()) return FALSE
;
432 wxIcon
wxApp::GetStdIcon(int which
) const
434 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
437 void wxApp::CleanUp()
440 // continuing to use user defined log target is unsafe from now on because
441 // some resources may be already unavailable, so replace it by something
443 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
448 delete gs_rootWindow
;
450 wxModule::CleanUpModules();
452 #if wxUSE_WX_RESOURCES
453 wxCleanUpResourceSystem();
456 if (wxTheColourDatabase
)
457 delete wxTheColourDatabase
;
459 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
461 wxDeleteStockObjects();
462 wxDeleteStockLists();
465 wxTheApp
= (wxApp
*) NULL
;
468 // GL: I'm annoyed ... I don't know where to put this and I don't want to
469 // create a module for that as it's part of the core.
471 delete wxPendingEvents
;
472 delete wxPendingEventsLocker
;
475 wxSystemSettings::Done();
477 wxClassInfo::CleanUpClasses();
479 // Can't do this in wxModule, because fonts are needed by stock lists
480 // (do it after deleting wxTheApp and cleaning modules up, since somebody
481 // may be deleting fonts that lately)
482 delete wxTheFontsManager
;
483 wxTheFontsManager
= (wxFontsManager
*) NULL
;
487 // check for memory leaks
488 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
489 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
491 wxLogDebug(wxT("There were memory leaks.\n"));
492 wxDebugContext::Dump();
493 wxDebugContext::PrintStatistics();
498 // do this as the very last thing because everything else can log messages
499 wxLog::DontCreateOnDemand();
501 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
511 int wxEntryStart(int argc
, char *argv
[])
513 return wxApp::Initialize() ? 0 : -1;
519 return wxTheApp
->OnInitGui() ? 0 : -1;
523 void wxEntryCleanup()
530 int wxEntry(int argc
, char *argv
[])
532 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
533 // This seems to be necessary since there are 'rogue'
534 // objects present at this point (perhaps global objects?)
535 // Setting a checkpoint will ignore them as far as the
536 // memory checking facility is concerned.
537 // Of course you may argue that memory allocated in globals should be
538 // checked, but this is a reasonable compromise.
539 wxDebugContext::SetCheckpoint();
541 int err
= wxEntryStart(argc
, argv
);
547 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
548 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
550 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
552 wxObject
*test_app
= app_ini();
554 wxTheApp
= (wxApp
*) test_app
;
557 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
559 wxTheApp
->argc
= argc
;
561 wxTheApp
->argv
= new wxChar
*[argc
+1];
563 while (mb_argc
< argc
)
565 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
568 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
570 wxTheApp
->argv
= argv
;
573 wxString
name(wxFileNameFromPath(argv
[0]));
574 wxStripExtension(name
);
575 wxTheApp
->SetAppName(name
);
578 retValue
= wxEntryInitGui();
580 // Here frames insert themselves automatically into wxTopLevelWindows by
581 // getting created in OnInit().
584 if ( !wxTheApp
->OnInit() )
590 /* delete pending toplevel windows (typically a single
591 dialog) so that, if there isn't any left, we don't
593 wxTheApp
->DeletePendingObjects();
595 if ( wxTheApp
->Initialized() )
599 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
602 /* Forcibly delete the window. */
603 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
604 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
606 topWindow
->Close(TRUE
);
607 wxTheApp
->DeletePendingObjects();
612 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
617 // flush the logged messages if any
618 wxLog
*log
= wxLog::GetActiveTarget();
619 if (log
!= NULL
&& log
->HasPendingMessages())
622 retValue
= wxTheApp
->OnExit();