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 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 static bool gs_inYield
= FALSE
;
56 bool wxApp::Yield(bool onlyIfNeeded
)
62 wxFAIL_MSG( wxT("wxYield called recursively" ) );
69 if ( !wxThread::IsMain() )
71 // can't process events from other threads, MGL is thread-unsafe
74 #endif // wxUSE_THREADS
80 if ( wxEventLoop::GetActive() )
82 while (wxEventLoop::GetActive()->Pending())
83 wxEventLoop::GetActive()->Dispatch();
86 /* it's necessary to call ProcessIdle() to update the frames sizes which
87 might have been changed (it also will update other things set from
88 OnUpdateUI() which is a nice (and desired) side effect) */
89 while (wxTheApp
->ProcessIdle()) { }
99 //-----------------------------------------------------------------------------
101 //-----------------------------------------------------------------------------
103 void wxApp::WakeUpIdle()
106 if (!wxThread::IsMain())
110 while (wxTheApp
->ProcessIdle())
114 if (!wxThread::IsMain())
119 //-----------------------------------------------------------------------------
121 //-----------------------------------------------------------------------------
123 class wxRootWindow
: public wxWindow
126 wxRootWindow() : wxWindow(NULL
, -1)
128 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
129 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
133 // we don't want to delete MGL_WM's rootWnd
137 virtual bool AcceptsFocus() const { return FALSE
; }
139 DECLARE_DYNAMIC_CLASS(wxRootWindow
)
142 IMPLEMENT_DYNAMIC_CLASS(wxRootWindow
, wxWindow
)
144 static wxRootWindow
*gs_rootWindow
= NULL
;
146 //-----------------------------------------------------------------------------
147 // MGL initialization
148 //-----------------------------------------------------------------------------
150 static bool wxCreateMGL_WM(const wxDisplayModeInfo
& displayMode
)
153 int refresh
= MGL_DEFAULT_REFRESH
;
155 #if wxUSE_SYSTEM_OPTIONS
156 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
157 refresh
= wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
160 mode
= MGL_findMode(displayMode
.GetWidth(),
161 displayMode
.GetHeight(),
162 displayMode
.GetDepth());
165 wxLogError(_("Mode %ix%i-%i not available."),
166 displayMode
.GetWidth(),
167 displayMode
.GetHeight(),
168 displayMode
.GetDepth());
171 g_displayDC
= new MGLDisplayDC(mode
, 1, refresh
);
172 if ( !g_displayDC
->isValid() )
179 g_winMng
= MGL_wmCreate(g_displayDC
->getDC());
186 static void wxDestroyMGL_WM()
190 MGL_wmDestroy(g_winMng
);
200 //-----------------------------------------------------------------------------
202 //-----------------------------------------------------------------------------
204 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
206 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
207 EVT_IDLE(wxApp::OnIdle
)
211 wxApp::wxApp() : m_mainLoop(NULL
)
219 wxDisplayModeInfo
wxGetDefaultDisplayMode()
224 if ( !wxGetEnv(wxT("WXMODE"), &mode
) ||
225 (wxSscanf(mode
.c_str(), _T("%ux%u-%u"), &w
, &h
, &bpp
) != 3) )
227 w
= 640, h
= 480, bpp
= 16;
230 return wxDisplayModeInfo(w
, h
, bpp
);
233 bool wxApp::SetDisplayMode(const wxDisplayModeInfo
& mode
)
239 if ( g_displayDC
!= NULL
)
241 // FIXME_MGL -- we currently don't allow to switch video mode
242 // more than once. This can hopefully be changed...
243 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
247 if ( !wxCreateMGL_WM(mode
) )
249 gs_rootWindow
= new wxRootWindow
;
251 m_displayMode
= mode
;
256 bool wxApp::OnInitGui()
258 if ( !wxAppBase::OnInitGui() )
262 // MGL redirects stdout and stderr to physical console, so lets redirect
263 // it to file. Do it only when WXDEBUG environment variable is set
265 if ( wxGetEnv(wxT("WXSTDERR"), &redirect
) )
266 freopen(redirect
.mb_str(), "wt", stderr
);
269 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
270 if ( oldLog
) delete oldLog
;
275 bool wxApp::ProcessIdle()
278 event
.SetEventObject(this);
281 return event
.MoreRequested();
284 void wxApp::OnIdle(wxIdleEvent
&event
)
286 static bool s_inOnIdle
= FALSE
;
288 /* Avoid recursion (via ProcessEvent default case) */
294 /* Resend in the main thread events which have been prepared in other
296 ProcessPendingEvents();
298 // 'Garbage' collection of windows deleted with Close().
299 DeletePendingObjects();
302 // flush the logged messages if any
303 wxLog::FlushActive();
306 // Send OnIdle events to all windows
307 if ( SendIdleEvents() )
308 event
.RequestMore(TRUE
);
313 bool wxApp::SendIdleEvents()
315 bool needMore
= FALSE
;
317 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
320 wxWindow
* win
= node
->GetData();
321 if ( SendIdleEvents(win
) )
323 node
= node
->GetNext();
329 bool wxApp::SendIdleEvents(wxWindow
* win
)
331 bool needMore
= FALSE
;
334 event
.SetEventObject(win
);
336 win
->GetEventHandler()->ProcessEvent(event
);
338 if ( event
.MoreRequested() )
341 wxNode
* node
= win
->GetChildren().First();
344 wxWindow
* win
= (wxWindow
*) node
->Data();
345 if ( SendIdleEvents(win
) )
353 int wxApp::MainLoop()
356 m_mainLoop
= new wxEventLoop
;
358 rt
= m_mainLoop
->Run();
365 void wxApp::ExitMainLoop()
371 bool wxApp::Initialized()
373 return (wxTopLevelWindows
.GetCount() != 0);
376 bool wxApp::Pending()
378 return wxEventLoop::GetActive()->Pending();
381 void wxApp::Dispatch()
383 wxEventLoop::GetActive()->Dispatch();
386 void wxApp::DeletePendingObjects()
388 wxNode
*node
= wxPendingDelete
.First();
391 wxObject
*obj
= (wxObject
*)node
->Data();
395 if ( wxPendingDelete
.Find(obj
) )
398 node
= wxPendingDelete
.First();
402 bool wxApp::Initialize()
404 if ( MGL_init(".", NULL
) == 0 )
406 wxLogError(_("Cannot initialize SciTech MGL!"));
410 wxClassInfo::InitializeClasses();
413 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
416 // GL: I'm annoyed ... I don't know where to put this and I don't want to
417 // create a module for that as it's part of the core.
419 wxPendingEvents
= new wxList
;
420 wxPendingEventsLocker
= new wxCriticalSection
;
423 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
424 wxTheColourDatabase
->Initialize();
426 // Can't do this in wxModule, because fonts are needed by stock lists
427 wxTheFontsManager
= new wxFontsManager
;
429 wxInitializeStockLists();
430 wxInitializeStockObjects();
432 wxModule::RegisterModules();
433 if (!wxModule::InitializeModules()) return FALSE
;
438 void wxApp::CleanUp()
441 // continuing to use user defined log target is unsafe from now on because
442 // some resources may be already unavailable, so replace it by something
444 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
449 delete gs_rootWindow
;
451 wxModule::CleanUpModules();
453 if (wxTheColourDatabase
)
454 delete wxTheColourDatabase
;
456 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
458 wxDeleteStockObjects();
459 wxDeleteStockLists();
462 wxTheApp
= (wxApp
*) NULL
;
465 // GL: I'm annoyed ... I don't know where to put this and I don't want to
466 // create a module for that as it's part of the core.
468 delete wxPendingEvents
;
469 delete wxPendingEventsLocker
;
472 wxClassInfo::CleanUpClasses();
474 // Can't do this in wxModule, because fonts are needed by stock lists
475 // (do it after deleting wxTheApp and cleaning modules up, since somebody
476 // may be deleting fonts that lately)
477 delete wxTheFontsManager
;
478 wxTheFontsManager
= (wxFontsManager
*) NULL
;
480 // check for memory leaks
481 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
482 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
484 wxLogDebug(wxT("There were memory leaks.\n"));
485 wxDebugContext::Dump();
486 wxDebugContext::PrintStatistics();
491 // do this as the very last thing because everything else can log messages
492 wxLog::DontCreateOnDemand();
494 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
504 int wxEntryStart(int argc
, char *argv
[])
506 return wxApp::Initialize() ? 0 : -1;
512 return wxTheApp
->OnInitGui() ? 0 : -1;
516 void wxEntryCleanup()
523 int wxEntry(int argc
, char *argv
[])
526 // VS: disable long filenames under DJGPP as the very first thing,
527 // since SciTech MGL doesn't like them much...
528 wxSetEnv(wxT("LFN"), wxT("N"));
531 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
532 // This seems to be necessary since there are 'rogue'
533 // objects present at this point (perhaps global objects?)
534 // Setting a checkpoint will ignore them as far as the
535 // memory checking facility is concerned.
536 // Of course you may argue that memory allocated in globals should be
537 // checked, but this is a reasonable compromise.
538 wxDebugContext::SetCheckpoint();
540 int err
= wxEntryStart(argc
, argv
);
546 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
547 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
549 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
551 wxObject
*test_app
= app_ini();
553 wxTheApp
= (wxApp
*) test_app
;
556 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
558 wxTheApp
->argc
= argc
;
560 wxTheApp
->argv
= new wxChar
*[argc
+1];
562 while (mb_argc
< argc
)
564 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
567 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
569 wxTheApp
->argv
= argv
;
572 wxString
name(wxFileNameFromPath(argv
[0]));
573 wxStripExtension(name
);
574 wxTheApp
->SetAppName(name
);
577 retValue
= wxEntryInitGui();
579 // Here frames insert themselves automatically into wxTopLevelWindows by
580 // getting created in OnInit().
583 if ( !wxTheApp
->OnInit() )
589 /* delete pending toplevel windows (typically a single
590 dialog) so that, if there isn't any left, we don't
592 wxTheApp
->DeletePendingObjects();
594 if ( wxTheApp
->Initialized() )
598 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
601 /* Forcibly delete the window. */
602 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
603 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
605 topWindow
->Close(TRUE
);
606 wxTheApp
->DeletePendingObjects();
611 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
616 // flush the logged messages if any
617 wxLog
*log
= wxLog::GetActiveTarget();
618 if (log
!= NULL
&& log
->HasPendingMessages())
621 retValue
= wxTheApp
->OnExit();