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(const wxDisplayModeInfo
& displayMode
)
161 int refresh
= MGL_DEFAULT_REFRESH
;
163 #if wxUSE_SYSTEM_OPTIONS
164 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh") )
165 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
168 mode
= MGL_findMode(displayMode
.GetScreenSize().x
,
169 displayMode
.GetScreenSize().y
,
170 displayMode
.GetDepth());
173 wxLogWarning(_("Mode %ix%i-%i not available, falling back to default mode."),
174 displayMode
.GetScreenSize().x
,
175 displayMode
.GetScreenSize().y
,
176 displayMode
.GetDepth());
177 mode
= 0; // always available
179 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
180 if ( !g_displayDC
->isValid() )
187 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
194 static void wxDestroyMGL_WM()
198 MGL_wmDestroy(g_winMng
);
208 //-----------------------------------------------------------------------------
210 //-----------------------------------------------------------------------------
212 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
214 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
215 EVT_IDLE(wxApp::OnIdle
)
219 wxApp::wxApp() : m_mainLoop(NULL
)
221 m_displayMode
= wxDisplayModeInfo(wxSize(640, 480), 16);
228 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
234 if ( g_displayDC
!= NULL
)
236 // FIXME_MGL -- we currently don't allow to switch video mode
237 // at runtime. This can hopefully be changed...
238 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
241 m_displayMode
= mode
;
245 bool wxApp::OnInitGui()
247 if ( !wxCreateMGL_WM(m_displayMode
) )
250 // This has to be done *after* wxCreateMGL_WM() because it initializes
252 if ( !wxAppBase::OnInitGui() )
255 // ...and this has to be done after wxUniv themes were initialized
256 gs_rootWindow
= new wxRootWindow
;
259 // That damn MGL redirects stdin and stdout to physical console
260 FILE *file
= fopen("stderr", "wt");
261 wxLog::SetActiveTarget(new wxLogStderr(file
));
267 bool wxApp::ProcessIdle()
270 event
.SetEventObject(this);
273 return event
.MoreRequested();
276 void wxApp::OnIdle(wxIdleEvent
&event
)
278 static bool s_inOnIdle
= FALSE
;
280 /* Avoid recursion (via ProcessEvent default case) */
286 /* Resend in the main thread events which have been prepared in other
288 ProcessPendingEvents();
290 // 'Garbage' collection of windows deleted with Close().
291 DeletePendingObjects();
293 // Send OnIdle events to all windows
294 if ( SendIdleEvents() )
295 event
.RequestMore(TRUE
);
300 bool wxApp::SendIdleEvents()
302 bool needMore
= FALSE
;
304 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
307 wxWindow
* win
= node
->GetData();
308 if ( SendIdleEvents(win
) )
310 node
= node
->GetNext();
316 bool wxApp::SendIdleEvents(wxWindow
* win
)
318 bool needMore
= FALSE
;
321 event
.SetEventObject(win
);
323 win
->GetEventHandler()->ProcessEvent(event
);
325 if ( event
.MoreRequested() )
328 wxNode
* node
= win
->GetChildren().First();
331 wxWindow
* win
= (wxWindow
*) node
->Data();
332 if ( SendIdleEvents(win
) )
340 int wxApp::MainLoop()
343 m_mainLoop
= new wxEventLoop
;
345 rt
= m_mainLoop
->Run();
352 void wxApp::ExitMainLoop()
358 bool wxApp::Initialized()
360 return (wxTopLevelWindows
.GetCount() != 0);
363 bool wxApp::Pending()
365 return wxEventLoop::GetActive()->Pending();
368 void wxApp::Dispatch()
370 wxEventLoop::GetActive()->Dispatch();
373 void wxApp::DeletePendingObjects()
375 wxNode
*node
= wxPendingDelete
.First();
378 wxObject
*obj
= (wxObject
*)node
->Data();
382 if ( wxPendingDelete
.Find(obj
) )
385 node
= wxPendingDelete
.First();
389 bool wxApp::Initialize()
391 if ( MGL_init(".", NULL
) == 0 )
394 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
396 wxClassInfo::InitializeClasses();
398 wxSystemSettings::Init();
401 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
404 // GL: I'm annoyed ... I don't know where to put this and I don't want to
405 // create a module for that as it's part of the core.
407 wxPendingEvents
= new wxList
;
408 wxPendingEventsLocker
= new wxCriticalSection
;
411 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
412 wxTheColourDatabase
->Initialize();
414 // Can't do this in wxModule, because fonts are needed by stock lists
415 wxTheFontsManager
= new wxFontsManager
;
417 wxInitializeStockLists();
418 wxInitializeStockObjects();
420 #if wxUSE_WX_RESOURCES
421 wxInitializeResourceSystem();
424 wxModule::RegisterModules();
425 if (!wxModule::InitializeModules()) return FALSE
;
430 wxIcon
wxApp::GetStdIcon(int which
) const
432 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
435 void wxApp::CleanUp()
437 delete gs_rootWindow
;
440 // flush the logged messages if any
441 wxLog
*log
= wxLog::GetActiveTarget();
442 if (log
!= NULL
&& log
->HasPendingMessages())
445 // continuing to use user defined log target is unsafe from now on because
446 // some resources may be already unavailable, so replace it by something
448 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
453 wxModule::CleanUpModules();
455 #if wxUSE_WX_RESOURCES
456 wxCleanUpResourceSystem();
459 if (wxTheColourDatabase
)
460 delete wxTheColourDatabase
;
462 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
464 wxDeleteStockObjects();
465 wxDeleteStockLists();
467 // Can't do this in wxModule, because fonts are needed by stock lists
468 delete wxTheFontsManager
;
469 wxTheFontsManager
= (wxFontsManager
*) NULL
;
472 wxTheApp
= (wxApp
*) NULL
;
474 // GL: I'm annoyed ... I don't know where to put this and I don't want to
475 // create a module for that as it's part of the core.
477 delete wxPendingEvents
;
478 delete wxPendingEventsLocker
;
481 wxSystemSettings::Done();
485 wxClassInfo::CleanUpClasses();
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
);
616 retValue
= wxTheApp
->OnExit();