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
;
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 static bool gs_inYield
= FALSE
;
64 bool wxApp::Yield(bool onlyIfNeeded
)
70 wxFAIL_MSG( wxT("wxYield called recursively" ) );
77 if ( !wxThread::IsMain() )
79 // can't process events from other threads, MGL is thread-unsafe
82 #endif // wxUSE_THREADS
88 if ( wxEventLoop::GetActive() )
90 while (wxEventLoop::GetActive()->Pending())
91 wxEventLoop::GetActive()->Dispatch();
94 /* it's necessary to call ProcessIdle() to update the frames sizes which
95 might have been changed (it also will update other things set from
96 OnUpdateUI() which is a nice (and desired) side effect) */
97 while (wxTheApp
->ProcessIdle()) { }
107 //-----------------------------------------------------------------------------
109 //-----------------------------------------------------------------------------
114 if (!wxThread::IsMain())
118 while (wxTheApp
->ProcessIdle()) {}
121 if (!wxThread::IsMain())
126 //-----------------------------------------------------------------------------
128 //-----------------------------------------------------------------------------
130 class wxRootWindow
: public wxWindow
133 wxRootWindow() : wxWindow(NULL
, -1)
135 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
136 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
140 // we don't want to delete MGL_WM's rootWnd
144 virtual bool AcceptsFocus() const { return FALSE
; }
146 DECLARE_DYNAMIC_CLASS(wxRootWindow
)
149 IMPLEMENT_DYNAMIC_CLASS(wxRootWindow
, wxWindow
)
151 static wxRootWindow
*gs_rootWindow
= NULL
;
153 //-----------------------------------------------------------------------------
154 // MGL initialization
155 //-----------------------------------------------------------------------------
157 static bool wxCreateMGL_WM(const wxDisplayModeInfo
& displayMode
)
160 int refresh
= MGL_DEFAULT_REFRESH
;
162 #if wxUSE_SYSTEM_OPTIONS
163 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
164 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
167 mode
= MGL_findMode(displayMode
.GetWidth(),
168 displayMode
.GetHeight(),
169 displayMode
.GetDepth());
172 wxLogError(_("Mode %ix%i-%i not available."),
173 displayMode
.GetWidth(),
174 displayMode
.GetHeight(),
175 displayMode
.GetDepth());
178 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
179 if ( !g_displayDC
->isValid() )
186 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
193 static void wxDestroyMGL_WM()
197 MGL_wmDestroy(g_winMng
);
207 //-----------------------------------------------------------------------------
209 //-----------------------------------------------------------------------------
211 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
213 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
214 EVT_IDLE(wxApp::OnIdle
)
218 wxApp::wxApp() : m_mainLoop(NULL
)
226 wxDisplayModeInfo
wxGetDefaultDisplayMode()
231 if ( !wxGetEnv(wxT("WXMODE"), &mode
) ||
232 (wxSscanf(mode
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3) )
234 w
= 640, h
= 480, bpp
= 16;
237 return wxDisplayModeInfo(w
, h
, bpp
);
240 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
246 if ( g_displayDC
!= NULL
)
248 // FIXME_MGL -- we currently don't allow to switch video mode
249 // more than once. This can hopefully be changed...
250 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
254 if ( !wxCreateMGL_WM(mode
) )
256 gs_rootWindow
= new wxRootWindow
;
258 m_displayMode
= mode
;
263 bool wxApp::OnInitGui()
265 if ( !wxAppBase::OnInitGui() )
269 // MGL redirects stdout and stderr to physical console, so lets redirect
270 // it to file. Do it only when WXDEBUG environment variable is set
272 if ( wxGetEnv(wxT("WXSTDERR"), &redirect
) )
273 freopen(redirect
.mb_str(), "wt", stderr
);
276 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
277 if ( oldLog
) delete oldLog
;
282 bool wxApp::ProcessIdle()
285 event
.SetEventObject(this);
288 return event
.MoreRequested();
291 void wxApp::OnIdle(wxIdleEvent
&event
)
293 static bool s_inOnIdle
= FALSE
;
295 /* Avoid recursion (via ProcessEvent default case) */
301 /* Resend in the main thread events which have been prepared in other
303 ProcessPendingEvents();
305 // 'Garbage' collection of windows deleted with Close().
306 DeletePendingObjects();
309 // flush the logged messages if any
310 wxLog::FlushActive();
313 // Send OnIdle events to all windows
314 if ( SendIdleEvents() )
315 event
.RequestMore(TRUE
);
320 bool wxApp::SendIdleEvents()
322 bool needMore
= FALSE
;
324 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
327 wxWindow
* win
= node
->GetData();
328 if ( SendIdleEvents(win
) )
330 node
= node
->GetNext();
336 bool wxApp::SendIdleEvents(wxWindow
* win
)
338 bool needMore
= FALSE
;
341 event
.SetEventObject(win
);
343 win
->GetEventHandler()->ProcessEvent(event
);
345 if ( event
.MoreRequested() )
348 wxNode
* node
= win
->GetChildren().First();
351 wxWindow
* win
= (wxWindow
*) node
->Data();
352 if ( SendIdleEvents(win
) )
360 int wxApp::MainLoop()
363 m_mainLoop
= new wxEventLoop
;
365 rt
= m_mainLoop
->Run();
372 void wxApp::ExitMainLoop()
378 bool wxApp::Initialized()
380 return (wxTopLevelWindows
.GetCount() != 0);
383 bool wxApp::Pending()
385 return wxEventLoop::GetActive()->Pending();
388 void wxApp::Dispatch()
390 wxEventLoop::GetActive()->Dispatch();
393 void wxApp::DeletePendingObjects()
395 wxNode
*node
= wxPendingDelete
.First();
398 wxObject
*obj
= (wxObject
*)node
->Data();
402 if ( wxPendingDelete
.Find(obj
) )
405 node
= wxPendingDelete
.First();
409 bool wxApp::Initialize()
411 if ( MGL_init(".", NULL
) == 0 )
413 wxLogError(_("Cannot initialize SciTech MGL!"));
417 wxClassInfo::InitializeClasses();
420 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
423 // GL: I'm annoyed ... I don't know where to put this and I don't want to
424 // create a module for that as it's part of the core.
426 wxPendingEvents
= new wxList
;
427 wxPendingEventsLocker
= new wxCriticalSection
;
430 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
431 wxTheColourDatabase
->Initialize();
433 // Can't do this in wxModule, because fonts are needed by stock lists
434 wxTheFontsManager
= new wxFontsManager
;
436 wxInitializeStockLists();
437 wxInitializeStockObjects();
439 wxModule::RegisterModules();
440 if (!wxModule::InitializeModules()) return FALSE
;
445 void wxApp::CleanUp()
448 // continuing to use user defined log target is unsafe from now on because
449 // some resources may be already unavailable, so replace it by something
451 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
456 delete gs_rootWindow
;
458 wxModule::CleanUpModules();
460 if (wxTheColourDatabase
)
461 delete wxTheColourDatabase
;
463 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
465 wxDeleteStockObjects();
466 wxDeleteStockLists();
469 wxTheApp
= (wxApp
*) NULL
;
472 // GL: I'm annoyed ... I don't know where to put this and I don't want to
473 // create a module for that as it's part of the core.
475 delete wxPendingEvents
;
476 delete wxPendingEventsLocker
;
479 wxClassInfo::CleanUpClasses();
481 // Can't do this in wxModule, because fonts are needed by stock lists
482 // (do it after deleting wxTheApp and cleaning modules up, since somebody
483 // may be deleting fonts that lately)
484 delete wxTheFontsManager
;
485 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
[])
533 // VS: disable long filenames under DJGPP as the very first thing,
534 // since SciTech MGL doesn't like them much...
535 wxSetEnv(wxT("LFN"), wxT("N"));
538 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
539 // This seems to be necessary since there are 'rogue'
540 // objects present at this point (perhaps global objects?)
541 // Setting a checkpoint will ignore them as far as the
542 // memory checking facility is concerned.
543 // Of course you may argue that memory allocated in globals should be
544 // checked, but this is a reasonable compromise.
545 wxDebugContext::SetCheckpoint();
547 int err
= wxEntryStart(argc
, argv
);
553 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
554 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
556 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
558 wxObject
*test_app
= app_ini();
560 wxTheApp
= (wxApp
*) test_app
;
563 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
565 wxTheApp
->argc
= argc
;
567 wxTheApp
->argv
= new wxChar
*[argc
+1];
569 while (mb_argc
< argc
)
571 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
574 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
576 wxTheApp
->argv
= argv
;
579 wxString
name(wxFileNameFromPath(argv
[0]));
580 wxStripExtension(name
);
581 wxTheApp
->SetAppName(name
);
584 retValue
= wxEntryInitGui();
586 // Here frames insert themselves automatically into wxTopLevelWindows by
587 // getting created in OnInit().
590 if ( !wxTheApp
->OnInit() )
596 /* delete pending toplevel windows (typically a single
597 dialog) so that, if there isn't any left, we don't
599 wxTheApp
->DeletePendingObjects();
601 if ( wxTheApp
->Initialized() )
605 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
608 /* Forcibly delete the window. */
609 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
610 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
612 topWindow
->Close(TRUE
);
613 wxTheApp
->DeletePendingObjects();
618 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
623 // flush the logged messages if any
624 wxLog
*log
= wxLog::GetActiveTarget();
625 if (log
!= NULL
&& log
->HasPendingMessages())
628 retValue
= wxTheApp
->OnExit();