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"
30 #include "wx/resource.h"
34 #include "wx/fontutil.h"
35 #include "wx/univ/theme.h"
36 #include "wx/univ/renderer.h"
37 #include "wx/univ/colschem.h"
38 #include "wx/sysopt.h"
39 #include "wx/mgl/private.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 wxApp
*wxTheApp
= NULL
;
46 wxAppInitializerFunction
wxAppBase::m_appInitFn
= (wxAppInitializerFunction
) NULL
;
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 static bool gs_inYield
= FALSE
;
65 bool wxApp::Yield(bool onlyIfNeeded
)
71 wxFAIL_MSG( wxT("wxYield called recursively" ) );
78 if ( !wxThread::IsMain() )
80 // can't process events from other threads, MGL is thread-unsafe
83 #endif // wxUSE_THREADS
89 if ( wxEventLoop::GetActive() )
91 while (wxEventLoop::GetActive()->Pending())
92 wxEventLoop::GetActive()->Dispatch();
95 /* it's necessary to call ProcessIdle() to update the frames sizes which
96 might have been changed (it also will update other things set from
97 OnUpdateUI() which is a nice (and desired) side effect) */
98 while (wxTheApp
->ProcessIdle()) { }
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
115 if (!wxThread::IsMain())
119 while (wxTheApp
->ProcessIdle()) {}
122 if (!wxThread::IsMain())
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
131 class wxRootWindow
: public wxWindow
134 wxRootWindow() : wxWindow(NULL
, -1)
136 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng
));
137 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP
));
141 // we don't want to delete MGL_WM's rootWnd
145 virtual bool AcceptsFocus() { return FALSE
; }
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
268 if ( wxGetEnv(wxT("WXDEBUG"), NULL
) )
269 freopen("output.err", "wt", stderr
);
272 wxLog
*oldLog
= wxLog::SetActiveTarget(new wxLogGui
);
273 if ( oldLog
) delete oldLog
;
278 bool wxApp::ProcessIdle()
281 event
.SetEventObject(this);
284 return event
.MoreRequested();
287 void wxApp::OnIdle(wxIdleEvent
&event
)
289 static bool s_inOnIdle
= FALSE
;
291 /* Avoid recursion (via ProcessEvent default case) */
297 /* Resend in the main thread events which have been prepared in other
299 ProcessPendingEvents();
301 // 'Garbage' collection of windows deleted with Close().
302 DeletePendingObjects();
305 // flush the logged messages if any
306 wxLog::FlushActive();
309 // Send OnIdle events to all windows
310 if ( SendIdleEvents() )
311 event
.RequestMore(TRUE
);
316 bool wxApp::SendIdleEvents()
318 bool needMore
= FALSE
;
320 wxWindowList::Node
* node
= wxTopLevelWindows
.GetFirst();
323 wxWindow
* win
= node
->GetData();
324 if ( SendIdleEvents(win
) )
326 node
= node
->GetNext();
332 bool wxApp::SendIdleEvents(wxWindow
* win
)
334 bool needMore
= FALSE
;
337 event
.SetEventObject(win
);
339 win
->GetEventHandler()->ProcessEvent(event
);
341 if ( event
.MoreRequested() )
344 wxNode
* node
= win
->GetChildren().First();
347 wxWindow
* win
= (wxWindow
*) node
->Data();
348 if ( SendIdleEvents(win
) )
356 int wxApp::MainLoop()
359 m_mainLoop
= new wxEventLoop
;
361 rt
= m_mainLoop
->Run();
368 void wxApp::ExitMainLoop()
374 bool wxApp::Initialized()
376 return (wxTopLevelWindows
.GetCount() != 0);
379 bool wxApp::Pending()
381 return wxEventLoop::GetActive()->Pending();
384 void wxApp::Dispatch()
386 wxEventLoop::GetActive()->Dispatch();
389 void wxApp::DeletePendingObjects()
391 wxNode
*node
= wxPendingDelete
.First();
394 wxObject
*obj
= (wxObject
*)node
->Data();
398 if ( wxPendingDelete
.Find(obj
) )
401 node
= wxPendingDelete
.First();
405 bool wxApp::Initialize()
407 if ( MGL_init(".", NULL
) == 0 )
410 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
412 wxClassInfo::InitializeClasses();
415 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
418 // GL: I'm annoyed ... I don't know where to put this and I don't want to
419 // create a module for that as it's part of the core.
421 wxPendingEvents
= new wxList
;
422 wxPendingEventsLocker
= new wxCriticalSection
;
425 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
426 wxTheColourDatabase
->Initialize();
428 // Can't do this in wxModule, because fonts are needed by stock lists
429 wxTheFontsManager
= new wxFontsManager
;
431 wxInitializeStockLists();
432 wxInitializeStockObjects();
434 #if wxUSE_WX_RESOURCES
435 wxInitializeResourceSystem();
438 wxModule::RegisterModules();
439 if (!wxModule::InitializeModules()) return FALSE
;
444 wxIcon
wxApp::GetStdIcon(int which
) const
446 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
449 void wxApp::CleanUp()
452 // continuing to use user defined log target is unsafe from now on because
453 // some resources may be already unavailable, so replace it by something
455 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
460 delete gs_rootWindow
;
462 wxModule::CleanUpModules();
464 #if wxUSE_WX_RESOURCES
465 wxCleanUpResourceSystem();
468 if (wxTheColourDatabase
)
469 delete wxTheColourDatabase
;
471 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
473 wxDeleteStockObjects();
474 wxDeleteStockLists();
477 wxTheApp
= (wxApp
*) NULL
;
480 // GL: I'm annoyed ... I don't know where to put this and I don't want to
481 // create a module for that as it's part of the core.
483 delete wxPendingEvents
;
484 delete wxPendingEventsLocker
;
487 wxClassInfo::CleanUpClasses();
489 // Can't do this in wxModule, because fonts are needed by stock lists
490 // (do it after deleting wxTheApp and cleaning modules up, since somebody
491 // may be deleting fonts that lately)
492 delete wxTheFontsManager
;
493 wxTheFontsManager
= (wxFontsManager
*) NULL
;
497 // check for memory leaks
498 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
499 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
501 wxLogDebug(wxT("There were memory leaks.\n"));
502 wxDebugContext::Dump();
503 wxDebugContext::PrintStatistics();
508 // do this as the very last thing because everything else can log messages
509 wxLog::DontCreateOnDemand();
511 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
521 int wxEntryStart(int argc
, char *argv
[])
523 return wxApp::Initialize() ? 0 : -1;
529 return wxTheApp
->OnInitGui() ? 0 : -1;
533 void wxEntryCleanup()
540 int wxEntry(int argc
, char *argv
[])
543 // VS: disable long filenames under DJGPP as the very first thing,
544 // since SciTech MGL doesn't like them much...
545 wxSetEnv(wxT("LFN"), wxT("N"));
548 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
549 // This seems to be necessary since there are 'rogue'
550 // objects present at this point (perhaps global objects?)
551 // Setting a checkpoint will ignore them as far as the
552 // memory checking facility is concerned.
553 // Of course you may argue that memory allocated in globals should be
554 // checked, but this is a reasonable compromise.
555 wxDebugContext::SetCheckpoint();
557 int err
= wxEntryStart(argc
, argv
);
563 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
564 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
566 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
568 wxObject
*test_app
= app_ini();
570 wxTheApp
= (wxApp
*) test_app
;
573 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
575 wxTheApp
->argc
= argc
;
577 wxTheApp
->argv
= new wxChar
*[argc
+1];
579 while (mb_argc
< argc
)
581 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
584 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
586 wxTheApp
->argv
= argv
;
589 wxString
name(wxFileNameFromPath(argv
[0]));
590 wxStripExtension(name
);
591 wxTheApp
->SetAppName(name
);
594 retValue
= wxEntryInitGui();
596 // Here frames insert themselves automatically into wxTopLevelWindows by
597 // getting created in OnInit().
600 if ( !wxTheApp
->OnInit() )
606 /* delete pending toplevel windows (typically a single
607 dialog) so that, if there isn't any left, we don't
609 wxTheApp
->DeletePendingObjects();
611 if ( wxTheApp
->Initialized() )
615 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
618 /* Forcibly delete the window. */
619 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
620 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
622 topWindow
->Close(TRUE
);
623 wxTheApp
->DeletePendingObjects();
628 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
633 // flush the logged messages if any
634 wxLog
*log
= wxLog::GetActiveTarget();
635 if (log
!= NULL
&& log
->HasPendingMessages())
638 retValue
= wxTheApp
->OnExit();