1 /////////////////////////////////////////////////////////////////////////////
3 // Author: Vaclav Slavik
4 // based on GTK and MSW implementations
6 // Copyright: (c) 2001-2002 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/sysopt.h"
38 #include "wx/mgl/private.h"
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 wxApp
*wxTheApp
= NULL
;
45 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 static bool gs_inYield
= FALSE
;
60 bool wxApp::Yield(bool onlyIfNeeded
)
66 wxFAIL_MSG( wxT("wxYield called recursively" ) );
73 if ( !wxThread::IsMain() )
75 // can't process events from other threads, MGL is thread-unsafe
78 #endif // wxUSE_THREADS
84 if ( wxEventLoop::GetActive() )
86 while (wxEventLoop::GetActive()->Pending())
87 wxEventLoop::GetActive()->Dispatch();
90 /* it's necessary to call ProcessIdle() to update the frames sizes which
91 might have been changed (it also will update other things set from
92 OnUpdateUI() which is a nice (and desired) side effect) */
93 while (wxTheApp
->ProcessIdle()) { }
103 //-----------------------------------------------------------------------------
105 //-----------------------------------------------------------------------------
107 void wxApp::WakeUpIdle()
110 if (!wxThread::IsMain())
114 while (wxTheApp
->ProcessIdle())
118 if (!wxThread::IsMain())
123 //-----------------------------------------------------------------------------
125 //-----------------------------------------------------------------------------
127 class wxRootWindow
: public wxWindow
130 wxRootWindow() : wxWindow(NULL
, -1)
132 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
133 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
137 // we don't want to delete MGL_WM's rootWnd
141 virtual bool AcceptsFocus() const { return FALSE
; }
143 DECLARE_DYNAMIC_CLASS(wxRootWindow
)
146 IMPLEMENT_DYNAMIC_CLASS(wxRootWindow
, wxWindow
)
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
269 if ( wxGetEnv(wxT("WXSTDERR"), &redirect
) )
270 freopen(redirect
.mb_str(), "wt", stderr
);
273 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
274 if ( oldLog
) delete oldLog
;
279 bool wxApp::ProcessIdle()
282 event
.SetEventObject(this);
285 return event
.MoreRequested();
288 void wxApp::OnIdle(wxIdleEvent
&event
)
290 static bool s_inOnIdle
= FALSE
;
292 /* Avoid recursion (via ProcessEvent default case) */
298 /* Resend in the main thread events which have been prepared in other
300 ProcessPendingEvents();
302 // 'Garbage' collection of windows deleted with Close().
303 DeletePendingObjects();
306 // flush the logged messages if any
307 wxLog::FlushActive();
310 // Send OnIdle events to all windows
311 if ( SendIdleEvents() )
312 event
.RequestMore(TRUE
);
317 bool wxApp::SendIdleEvents()
319 bool needMore
= FALSE
;
321 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
324 wxWindow
* win
= node
->GetData();
325 if ( SendIdleEvents(win
) )
327 node
= node
->GetNext();
333 bool wxApp::SendIdleEvents(wxWindow
* win
)
335 bool needMore
= FALSE
;
338 event
.SetEventObject(win
);
340 win
->GetEventHandler()->ProcessEvent(event
);
342 if ( event
.MoreRequested() )
345 wxNode
* node
= win
->GetChildren().First();
348 wxWindow
* win
= (wxWindow
*) node
->Data();
349 if ( SendIdleEvents(win
) )
357 int wxApp::MainLoop()
360 m_mainLoop
= new wxEventLoop
;
362 rt
= m_mainLoop
->Run();
369 void wxApp::ExitMainLoop()
375 bool wxApp::Initialized()
377 return (wxTopLevelWindows
.GetCount() != 0);
380 bool wxApp::Pending()
382 return wxEventLoop::GetActive()->Pending();
385 void wxApp::Dispatch()
387 wxEventLoop::GetActive()->Dispatch();
390 void wxApp::DeletePendingObjects()
392 wxNode
*node
= wxPendingDelete
.First();
395 wxObject
*obj
= (wxObject
*)node
->Data();
399 if ( wxPendingDelete
.Find(obj
) )
402 node
= wxPendingDelete
.First();
406 bool wxApp::Initialize()
408 if ( MGL_init(".", NULL
) == 0 )
410 wxLogError(_("Cannot initialize SciTech MGL!"));
414 wxClassInfo::InitializeClasses();
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 wxModule::RegisterModules();
437 if (!wxModule::InitializeModules()) return FALSE
;
442 void wxApp::CleanUp()
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 delete gs_rootWindow
;
455 wxModule::CleanUpModules();
457 if (wxTheColourDatabase
)
458 delete wxTheColourDatabase
;
460 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
462 wxDeleteStockObjects();
463 wxDeleteStockLists();
466 wxTheApp
= (wxApp
*) NULL
;
469 // GL: I'm annoyed ... I don't know where to put this and I don't want to
470 // create a module for that as it's part of the core.
472 delete wxPendingEvents
;
473 delete wxPendingEventsLocker
;
476 wxClassInfo::CleanUpClasses();
478 // Can't do this in wxModule, because fonts are needed by stock lists
479 // (do it after deleting wxTheApp and cleaning modules up, since somebody
480 // may be deleting fonts that lately)
481 delete wxTheFontsManager
;
482 wxTheFontsManager
= (wxFontsManager
*) NULL
;
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
[])
530 // VS: disable long filenames under DJGPP as the very first thing,
531 // since SciTech MGL doesn't like them much...
532 wxSetEnv(wxT("LFN"), wxT("N"));
535 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
536 // This seems to be necessary since there are 'rogue'
537 // objects present at this point (perhaps global objects?)
538 // Setting a checkpoint will ignore them as far as the
539 // memory checking facility is concerned.
540 // Of course you may argue that memory allocated in globals should be
541 // checked, but this is a reasonable compromise.
542 wxDebugContext::SetCheckpoint();
544 int err
= wxEntryStart(argc
, argv
);
550 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
551 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
553 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
555 wxObject
*test_app
= app_ini();
557 wxTheApp
= (wxApp
*) test_app
;
560 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
562 wxTheApp
->argc
= argc
;
564 wxTheApp
->argv
= new wxChar
*[argc
+1];
566 while (mb_argc
< argc
)
568 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
571 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
573 wxTheApp
->argv
= argv
;
576 wxString
name(wxFileNameFromPath(argv
[0]));
577 wxStripExtension(name
);
578 wxTheApp
->SetAppName(name
);
581 retValue
= wxEntryInitGui();
583 // Here frames insert themselves automatically into wxTopLevelWindows by
584 // getting created in OnInit().
587 if ( !wxTheApp
->OnInit() )
593 /* delete pending toplevel windows (typically a single
594 dialog) so that, if there isn't any left, we don't
596 wxTheApp
->DeletePendingObjects();
598 if ( wxTheApp
->Initialized() )
602 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
605 /* Forcibly delete the window. */
606 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
607 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
609 topWindow
->Close(TRUE
);
610 wxTheApp
->DeletePendingObjects();
615 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
620 // flush the logged messages if any
621 wxLog
*log
= wxLog::GetActiveTarget();
622 if (log
!= NULL
&& log
->HasPendingMessages())
625 retValue
= wxTheApp
->OnExit();