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 wxDisplayModeInfo
wxGetDefaultDisplayMode()
228 if ( !wxGetEnv(wxT("WXMODE"), &mode
) ||
229 (wxSscanf(mode
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3) )
231 w
= 640, h
= 480, bpp
= 16;
234 return wxDisplayModeInfo(w
, h
, bpp
);
237 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
243 if ( g_displayDC
!= NULL
)
245 // FIXME_MGL -- we currently don't allow to switch video mode
246 // more than once. This can hopefully be changed...
247 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
251 if ( !wxCreateMGL_WM(mode
) )
253 gs_rootWindow
= new wxRootWindow
;
255 m_displayMode
= mode
;
260 bool wxApp::OnInitGui()
262 if ( !wxAppBase::OnInitGui() )
266 // MGL redirects stdout and stderr to physical console, so lets redirect
267 // it to file. Do it only when WXDEBUG environment variable is set
268 if ( wxGetEnv(wxT("WXDEBUG"), NULL
) )
269 freopen("output.err", "wt", stderr
);
272 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
273 if ( oldLog
) delete oldLog
;
278 bool wxApp::ProcessIdle()
281 event
.SetEventObject(this);
284 return event
.MoreRequested();
287 void wxApp::OnIdle(wxIdleEvent
&event
)
289 static bool s_inOnIdle
= FALSE
;
291 /* Avoid recursion (via ProcessEvent default case) */
297 /* Resend in the main thread events which have been prepared in other
299 ProcessPendingEvents();
301 // 'Garbage' collection of windows deleted with Close().
302 DeletePendingObjects();
305 // flush the logged messages if any
306 wxLog::FlushActive();
309 // Send OnIdle events to all windows
310 if ( SendIdleEvents() )
311 event
.RequestMore(TRUE
);
316 bool wxApp::SendIdleEvents()
318 bool needMore
= FALSE
;
320 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
323 wxWindow
* win
= node
->GetData();
324 if ( SendIdleEvents(win
) )
326 node
= node
->GetNext();
332 bool wxApp::SendIdleEvents(wxWindow
* win
)
334 bool needMore
= FALSE
;
337 event
.SetEventObject(win
);
339 win
->GetEventHandler()->ProcessEvent(event
);
341 if ( event
.MoreRequested() )
344 wxNode
* node
= win
->GetChildren().First();
347 wxWindow
* win
= (wxWindow
*) node
->Data();
348 if ( SendIdleEvents(win
) )
356 int wxApp::MainLoop()
359 m_mainLoop
= new wxEventLoop
;
361 rt
= m_mainLoop
->Run();
368 void wxApp::ExitMainLoop()
374 bool wxApp::Initialized()
376 return (wxTopLevelWindows
.GetCount() != 0);
379 bool wxApp::Pending()
381 return wxEventLoop::GetActive()->Pending();
384 void wxApp::Dispatch()
386 wxEventLoop::GetActive()->Dispatch();
389 void wxApp::DeletePendingObjects()
391 wxNode
*node
= wxPendingDelete
.First();
394 wxObject
*obj
= (wxObject
*)node
->Data();
398 if ( wxPendingDelete
.Find(obj
) )
401 node
= wxPendingDelete
.First();
405 bool wxApp::Initialize()
407 if ( MGL_init(".", NULL
) == 0 )
410 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
412 wxClassInfo::InitializeClasses();
414 wxSystemSettings::Init();
417 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
420 // GL: I'm annoyed ... I don't know where to put this and I don't want to
421 // create a module for that as it's part of the core.
423 wxPendingEvents
= new wxList
;
424 wxPendingEventsLocker
= new wxCriticalSection
;
427 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
428 wxTheColourDatabase
->Initialize();
430 // Can't do this in wxModule, because fonts are needed by stock lists
431 wxTheFontsManager
= new wxFontsManager
;
433 wxInitializeStockLists();
434 wxInitializeStockObjects();
436 #if wxUSE_WX_RESOURCES
437 wxInitializeResourceSystem();
440 wxModule::RegisterModules();
441 if (!wxModule::InitializeModules()) return FALSE
;
446 wxIcon
wxApp::GetStdIcon(int which
) const
448 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
451 void wxApp::CleanUp()
454 // continuing to use user defined log target is unsafe from now on because
455 // some resources may be already unavailable, so replace it by something
457 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
462 delete gs_rootWindow
;
464 wxModule::CleanUpModules();
466 #if wxUSE_WX_RESOURCES
467 wxCleanUpResourceSystem();
470 if (wxTheColourDatabase
)
471 delete wxTheColourDatabase
;
473 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
475 wxDeleteStockObjects();
476 wxDeleteStockLists();
479 wxTheApp
= (wxApp
*) NULL
;
482 // GL: I'm annoyed ... I don't know where to put this and I don't want to
483 // create a module for that as it's part of the core.
485 delete wxPendingEvents
;
486 delete wxPendingEventsLocker
;
489 wxSystemSettings::Done();
491 wxClassInfo::CleanUpClasses();
493 // Can't do this in wxModule, because fonts are needed by stock lists
494 // (do it after deleting wxTheApp and cleaning modules up, since somebody
495 // may be deleting fonts that lately)
496 delete wxTheFontsManager
;
497 wxTheFontsManager
= (wxFontsManager
*) NULL
;
501 // check for memory leaks
502 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
503 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
505 wxLogDebug(wxT("There were memory leaks.\n"));
506 wxDebugContext::Dump();
507 wxDebugContext::PrintStatistics();
512 // do this as the very last thing because everything else can log messages
513 wxLog::DontCreateOnDemand();
515 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
525 int wxEntryStart(int argc
, char *argv
[])
527 return wxApp::Initialize() ? 0 : -1;
533 return wxTheApp
->OnInitGui() ? 0 : -1;
537 void wxEntryCleanup()
544 int wxEntry(int argc
, char *argv
[])
546 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
547 // This seems to be necessary since there are 'rogue'
548 // objects present at this point (perhaps global objects?)
549 // Setting a checkpoint will ignore them as far as the
550 // memory checking facility is concerned.
551 // Of course you may argue that memory allocated in globals should be
552 // checked, but this is a reasonable compromise.
553 wxDebugContext::SetCheckpoint();
555 int err
= wxEntryStart(argc
, argv
);
561 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
562 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
564 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
566 wxObject
*test_app
= app_ini();
568 wxTheApp
= (wxApp
*) test_app
;
571 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
573 wxTheApp
->argc
= argc
;
575 wxTheApp
->argv
= new wxChar
*[argc
+1];
577 while (mb_argc
< argc
)
579 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
582 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
584 wxTheApp
->argv
= argv
;
587 wxString
name(wxFileNameFromPath(argv
[0]));
588 wxStripExtension(name
);
589 wxTheApp
->SetAppName(name
);
592 retValue
= wxEntryInitGui();
594 // Here frames insert themselves automatically into wxTopLevelWindows by
595 // getting created in OnInit().
598 if ( !wxTheApp
->OnInit() )
604 /* delete pending toplevel windows (typically a single
605 dialog) so that, if there isn't any left, we don't
607 wxTheApp
->DeletePendingObjects();
609 if ( wxTheApp
->Initialized() )
613 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
616 /* Forcibly delete the window. */
617 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
618 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
620 topWindow
->Close(TRUE
);
621 wxTheApp
->DeletePendingObjects();
626 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
631 // flush the logged messages if any
632 wxLog
*log
= wxLog::GetActiveTarget();
633 if (log
!= NULL
&& log
->HasPendingMessages())
636 retValue
= wxTheApp
->OnExit();