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
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 )
411 wxBuffer
= new wxChar
[BUFSIZ
+ 512];
413 wxClassInfo::InitializeClasses();
416 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
419 // GL: I'm annoyed ... I don't know where to put this and I don't want to
420 // create a module for that as it's part of the core.
422 wxPendingEvents
= new wxList
;
423 wxPendingEventsLocker
= new wxCriticalSection
;
426 wxTheColourDatabase
= new wxColourDatabase(wxKEY_STRING
);
427 wxTheColourDatabase
->Initialize();
429 // Can't do this in wxModule, because fonts are needed by stock lists
430 wxTheFontsManager
= new wxFontsManager
;
432 wxInitializeStockLists();
433 wxInitializeStockObjects();
435 #if wxUSE_WX_RESOURCES
436 wxInitializeResourceSystem();
439 wxModule::RegisterModules();
440 if (!wxModule::InitializeModules()) return FALSE
;
445 wxIcon
wxApp::GetStdIcon(int which
) const
447 return wxTheme::Get()->GetRenderer()->GetStdIcon(which
);
450 void wxApp::CleanUp()
453 // continuing to use user defined log target is unsafe from now on because
454 // some resources may be already unavailable, so replace it by something
456 wxLog
*oldlog
= wxLog::SetActiveTarget(new wxLogStderr
);
461 delete gs_rootWindow
;
463 wxModule::CleanUpModules();
465 #if wxUSE_WX_RESOURCES
466 wxCleanUpResourceSystem();
469 if (wxTheColourDatabase
)
470 delete wxTheColourDatabase
;
472 wxTheColourDatabase
= (wxColourDatabase
*) NULL
;
474 wxDeleteStockObjects();
475 wxDeleteStockLists();
478 wxTheApp
= (wxApp
*) NULL
;
481 // GL: I'm annoyed ... I don't know where to put this and I don't want to
482 // create a module for that as it's part of the core.
484 delete wxPendingEvents
;
485 delete wxPendingEventsLocker
;
488 wxClassInfo::CleanUpClasses();
490 // Can't do this in wxModule, because fonts are needed by stock lists
491 // (do it after deleting wxTheApp and cleaning modules up, since somebody
492 // may be deleting fonts that lately)
493 delete wxTheFontsManager
;
494 wxTheFontsManager
= (wxFontsManager
*) NULL
;
498 // check for memory leaks
499 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
500 if (wxDebugContext::CountObjectsLeft(TRUE
) > 0)
502 wxLogDebug(wxT("There were memory leaks.\n"));
503 wxDebugContext::Dump();
504 wxDebugContext::PrintStatistics();
509 // do this as the very last thing because everything else can log messages
510 wxLog::DontCreateOnDemand();
512 wxLog
*oldLog
= wxLog::SetActiveTarget( (wxLog
*) NULL
);
522 int wxEntryStart(int argc
, char *argv
[])
524 return wxApp::Initialize() ? 0 : -1;
530 return wxTheApp
->OnInitGui() ? 0 : -1;
534 void wxEntryCleanup()
541 int wxEntry(int argc
, char *argv
[])
544 // VS: disable long filenames under DJGPP as the very first thing,
545 // since SciTech MGL doesn't like them much...
546 wxSetEnv(wxT("LFN"), wxT("N"));
549 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
550 // This seems to be necessary since there are 'rogue'
551 // objects present at this point (perhaps global objects?)
552 // Setting a checkpoint will ignore them as far as the
553 // memory checking facility is concerned.
554 // Of course you may argue that memory allocated in globals should be
555 // checked, but this is a reasonable compromise.
556 wxDebugContext::SetCheckpoint();
558 int err
= wxEntryStart(argc
, argv
);
564 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
565 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
567 wxAppInitializerFunction app_ini
= wxApp::GetInitializerFunction();
569 wxObject
*test_app
= app_ini();
571 wxTheApp
= (wxApp
*) test_app
;
574 wxCHECK_MSG( wxTheApp
, -1, wxT("wxWindows error: no application object") );
576 wxTheApp
->argc
= argc
;
578 wxTheApp
->argv
= new wxChar
*[argc
+1];
580 while (mb_argc
< argc
)
582 wxTheApp
->argv
[mb_argc
] = wxStrdup(wxConvLibc
.cMB2WX(argv
[mb_argc
]));
585 wxTheApp
->argv
[mb_argc
] = (wxChar
*)NULL
;
587 wxTheApp
->argv
= argv
;
590 wxString
name(wxFileNameFromPath(argv
[0]));
591 wxStripExtension(name
);
592 wxTheApp
->SetAppName(name
);
595 retValue
= wxEntryInitGui();
597 // Here frames insert themselves automatically into wxTopLevelWindows by
598 // getting created in OnInit().
601 if ( !wxTheApp
->OnInit() )
607 /* delete pending toplevel windows (typically a single
608 dialog) so that, if there isn't any left, we don't
610 wxTheApp
->DeletePendingObjects();
612 if ( wxTheApp
->Initialized() )
616 wxWindow
*topWindow
= wxTheApp
->GetTopWindow();
619 /* Forcibly delete the window. */
620 if (topWindow
->IsKindOf(CLASSINFO(wxFrame
)) ||
621 topWindow
->IsKindOf(CLASSINFO(wxDialog
)) )
623 topWindow
->Close(TRUE
);
624 wxTheApp
->DeletePendingObjects();
629 wxTheApp
->SetTopWindow((wxWindow
*) NULL
);
634 // flush the logged messages if any
635 wxLog
*log
= wxLog::GetActiveTarget();
636 if (log
!= NULL
&& log
->HasPendingMessages())
639 retValue
= wxTheApp
->OnExit();