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 wxLogError(_("Mode %ix%i-%i not available."),
174 displayMode
.GetScreenSize().x
,
175 displayMode
.GetScreenSize().y
,
176 displayMode
.GetDepth());
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
)
227 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
233 if ( g_displayDC
!= NULL
)
235 // FIXME_MGL -- we currently don't allow to switch video mode
236 // more than once. This can hopefully be changed...
237 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
241 if ( !wxCreateMGL_WM(mode
) )
243 gs_rootWindow
= new wxRootWindow
;
245 m_displayMode
= mode
;
250 bool wxApp::OnInitGui()
252 if ( !wxAppBase::OnInitGui() )
256 // That damn MGL redirects stdin and stdout to physical console
257 FILE *file
= fopen("stderr", "wt");
258 wxLog::SetActiveTarget(new wxLogStderr(file
));
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();
290 // Send OnIdle events to all windows
291 if ( SendIdleEvents() )
292 event
.RequestMore(TRUE
);
297 bool wxApp::SendIdleEvents()
299 bool needMore
= FALSE
;
301 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
304 wxWindow
* win
= node
->GetData();
305 if ( SendIdleEvents(win
) )
307 node
= node
->GetNext();
313 bool wxApp::SendIdleEvents(wxWindow
* win
)
315 bool needMore
= FALSE
;
318 event
.SetEventObject(win
);
320 win
->GetEventHandler()->ProcessEvent(event
);
322 if ( event
.MoreRequested() )
325 wxNode
* node
= win
->GetChildren().First();
328 wxWindow
* win
= (wxWindow
*) node
->Data();
329 if ( SendIdleEvents(win
) )
337 int wxApp::MainLoop()
340 m_mainLoop
= new wxEventLoop
;
342 rt
= m_mainLoop
->Run();
349 void wxApp::ExitMainLoop()
355 bool wxApp::Initialized()
357 return (wxTopLevelWindows
.GetCount() != 0);
360 bool wxApp::Pending()
362 return wxEventLoop::GetActive()->Pending();
365 void wxApp::Dispatch()
367 wxEventLoop::GetActive()->Dispatch();
370 void wxApp::DeletePendingObjects()
372 wxNode
*node
= wxPendingDelete
.First();
375 wxObject
*obj
= (wxObject
*)node
->Data();
379 if ( wxPendingDelete
.Find(obj
) )
382 node
= wxPendingDelete
.First();
386 bool wxApp::Initialize()
388 if ( MGL_init(".", NULL
) == 0 )
391 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
393 wxClassInfo::InitializeClasses();
395 wxSystemSettings::Init();
398 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
401 // GL: I'm annoyed ... I don't know where to put this and I don't want to
402 // create a module for that as it's part of the core.
404 wxPendingEvents
= new wxList
;
405 wxPendingEventsLocker
= new wxCriticalSection
;
408 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
409 wxTheColourDatabase
->Initialize();
411 // Can't do this in wxModule, because fonts are needed by stock lists
412 wxTheFontsManager
= new wxFontsManager
;
414 wxInitializeStockLists();
415 wxInitializeStockObjects();
417 #if wxUSE_WX_RESOURCES
418 wxInitializeResourceSystem();
421 wxModule::RegisterModules();
422 if (!wxModule::InitializeModules()) return FALSE
;
427 wxIcon
wxApp::GetStdIcon(int which
) const
429 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
432 void wxApp::CleanUp()
434 delete gs_rootWindow
;
437 // flush the logged messages if any
438 wxLog
*log
= wxLog::GetActiveTarget();
439 if (log
!= NULL
&& log
->HasPendingMessages())
442 // continuing to use user defined log target is unsafe from now on because
443 // some resources may be already unavailable, so replace it by something
445 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
450 wxModule::CleanUpModules();
452 #if wxUSE_WX_RESOURCES
453 wxCleanUpResourceSystem();
456 if (wxTheColourDatabase
)
457 delete wxTheColourDatabase
;
459 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
461 wxDeleteStockObjects();
462 wxDeleteStockLists();
464 // Can't do this in wxModule, because fonts are needed by stock lists
465 delete wxTheFontsManager
;
466 wxTheFontsManager
= (wxFontsManager
*) NULL
;
469 wxTheApp
= (wxApp
*) NULL
;
471 // GL: I'm annoyed ... I don't know where to put this and I don't want to
472 // create a module for that as it's part of the core.
474 delete wxPendingEvents
;
475 delete wxPendingEventsLocker
;
478 wxSystemSettings::Done();
482 wxClassInfo::CleanUpClasses();
484 // check for memory leaks
485 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
486 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
488 wxLogDebug(wxT("There were memory leaks.\n"));
489 wxDebugContext::Dump();
490 wxDebugContext::PrintStatistics();
495 // do this as the very last thing because everything else can log messages
496 wxLog::DontCreateOnDemand();
498 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
508 int wxEntryStart(int argc
, char *argv
[])
510 return wxApp::Initialize() ? 0 : -1;
516 return wxTheApp
->OnInitGui() ? 0 : -1;
520 void wxEntryCleanup()
527 int wxEntry(int argc
, char *argv
[])
529 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
530 // This seems to be necessary since there are 'rogue'
531 // objects present at this point (perhaps global objects?)
532 // Setting a checkpoint will ignore them as far as the
533 // memory checking facility is concerned.
534 // Of course you may argue that memory allocated in globals should be
535 // checked, but this is a reasonable compromise.
536 wxDebugContext::SetCheckpoint();
538 int err
= wxEntryStart(argc
, argv
);
544 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
545 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
547 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
549 wxObject
*test_app
= app_ini();
551 wxTheApp
= (wxApp
*) test_app
;
554 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
556 wxTheApp
->argc
= argc
;
558 wxTheApp
->argv
= new wxChar
*[argc
+1];
560 while (mb_argc
< argc
)
562 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
565 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
567 wxTheApp
->argv
= argv
;
570 wxString
name(wxFileNameFromPath(argv
[0]));
571 wxStripExtension(name
);
572 wxTheApp
->SetAppName(name
);
575 retValue
= wxEntryInitGui();
577 // Here frames insert themselves automatically into wxTopLevelWindows by
578 // getting created in OnInit().
581 if ( !wxTheApp
->OnInit() )
587 /* delete pending toplevel windows (typically a single
588 dialog) so that, if there isn't any left, we don't
590 wxTheApp
->DeletePendingObjects();
592 if ( wxTheApp
->Initialized() )
596 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
599 /* Forcibly delete the window. */
600 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
601 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
603 topWindow
->Close(TRUE
);
604 wxTheApp
->DeletePendingObjects();
609 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
613 retValue
= wxTheApp
->OnExit();