]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
Latest updates for compiling.
[wxWidgets.git] / src / mgl / app.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
32b8ec41 3// Author: Vaclav Slavik
7bdc1879 4// based on GTK and MSW implementations
32b8ec41 5// Id: $Id$
8f7b34a8 6// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
32b8ec41
VZ
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11 #pragma implementation "app.h"
12#endif
13
7bdc1879
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
2ec3892d 21
7bdc1879
VS
22#ifndef WX_PRECOMP
23 #include "wx/settings.h"
24 #include "wx/module.h"
25 #include "wx/evtloop.h"
26 #include "wx/frame.h"
27 #include "wx/dialog.h"
2ec3892d 28 #include "wx/log.h"
7bdc1879 29 #include "wx/intl.h"
1acd70f9 30 #include "wx/resource.h"
7bdc1879 31#endif
32b8ec41 32
7bdc1879 33#include "wx/app.h"
ef344ff8 34#include "wx/fontutil.h"
df028524
VS
35#include "wx/univ/theme.h"
36#include "wx/univ/renderer.h"
58061670 37#include "wx/univ/colschem.h"
05c9ccbe 38#include "wx/sysopt.h"
7bdc1879 39#include "wx/mgl/private.h"
32b8ec41
VZ
40
41//-----------------------------------------------------------------------------
42// Global data
43//-----------------------------------------------------------------------------
44
7bdc1879 45wxApp *wxTheApp = NULL;
32b8ec41
VZ
46wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
47
32b8ec41 48
7bdc1879
VS
49//-----------------------------------------------------------------------------
50// wxExit
51//-----------------------------------------------------------------------------
32b8ec41
VZ
52
53void wxExit()
54{
7bdc1879 55 MGL_exit();
32b8ec41
VZ
56 exit(0);
57}
58
59//-----------------------------------------------------------------------------
60// wxYield
61//-----------------------------------------------------------------------------
62
7bdc1879
VS
63static bool gs_inYield = FALSE;
64
e1218bd6 65bool wxApp::Yield(bool onlyIfNeeded)
32b8ec41 66{
e1218bd6
VS
67 if ( gs_inYield )
68 {
69 if ( !onlyIfNeeded )
70 {
71 wxFAIL_MSG( wxT("wxYield called recursively" ) );
72 }
73
74 return FALSE;
75 }
76
7bdc1879
VS
77#if wxUSE_THREADS
78 if ( !wxThread::IsMain() )
79 {
80 // can't process events from other threads, MGL is thread-unsafe
81 return TRUE;
82 }
83#endif // wxUSE_THREADS
84
85 gs_inYield = TRUE;
86
87 wxLog::Suspend();
88
ef344ff8
VS
89 if ( wxEventLoop::GetActive() )
90 {
91 while (wxEventLoop::GetActive()->Pending())
92 wxEventLoop::GetActive()->Dispatch();
93 }
7bdc1879
VS
94
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()) { }
99
100 wxLog::Resume();
101
102 gs_inYield = FALSE;
103
32b8ec41
VZ
104 return TRUE;
105}
106
7bdc1879 107
32b8ec41
VZ
108//-----------------------------------------------------------------------------
109// wxWakeUpIdle
110//-----------------------------------------------------------------------------
111
112void wxWakeUpIdle()
113{
7bdc1879
VS
114#if wxUSE_THREADS
115 if (!wxThread::IsMain())
116 wxMutexGuiEnter();
117#endif
118
119 while (wxTheApp->ProcessIdle()) {}
120
121#if wxUSE_THREADS
122 if (!wxThread::IsMain())
123 wxMutexGuiLeave();
124#endif
32b8ec41
VZ
125}
126
58061670
VS
127//-----------------------------------------------------------------------------
128// Root window
129//-----------------------------------------------------------------------------
130
131class wxRootWindow : public wxWindow
132{
133 public:
134 wxRootWindow() : wxWindow(NULL, -1)
135 {
136 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng));
137 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP));
138 }
139 ~wxRootWindow()
140 {
141 // we don't want to delete MGL_WM's rootWnd
142 m_wnd = NULL;
143 }
144
145 virtual bool AcceptsFocus() { return FALSE; }
146};
147
148static wxRootWindow *gs_rootWindow = NULL;
149
150//-----------------------------------------------------------------------------
151// MGL initialization
152//-----------------------------------------------------------------------------
153
634f6a1f 154static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode)
58061670
VS
155{
156 int mode;
58061670
VS
157 int refresh = MGL_DEFAULT_REFRESH;
158
159#if wxUSE_SYSTEM_OPTIONS
05c9ccbe 160 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
58061670
VS
161 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
162#endif
163
d76048f5
VS
164 mode = MGL_findMode(displayMode.GetWidth(),
165 displayMode.GetHeight(),
634f6a1f 166 displayMode.GetDepth());
58061670
VS
167 if ( mode == -1 )
168 {
1f43b5c9 169 wxLogError(_("Mode %ix%i-%i not available."),
d76048f5
VS
170 displayMode.GetWidth(),
171 displayMode.GetHeight(),
634f6a1f 172 displayMode.GetDepth());
1f43b5c9 173 return FALSE;
58061670
VS
174 }
175 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
176 if ( !g_displayDC->isValid() )
177 {
178 delete g_displayDC;
179 g_displayDC = NULL;
180 return FALSE;
181 }
182
183 g_winMng = MGL_wmCreate(g_displayDC->getDC());
184 if (!g_winMng)
185 return FALSE;
1f43b5c9 186
58061670
VS
187 return TRUE;
188}
189
190static void wxDestroyMGL_WM()
191{
192 if ( g_winMng )
193 {
194 MGL_wmDestroy(g_winMng);
195 g_winMng = NULL;
196 }
197 if ( g_displayDC )
198 {
199 delete g_displayDC;
200 g_displayDC = NULL;
201 }
202}
203
32b8ec41
VZ
204//-----------------------------------------------------------------------------
205// wxApp
206//-----------------------------------------------------------------------------
207
208IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
209
210BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
211 EVT_IDLE(wxApp::OnIdle)
212END_EVENT_TABLE()
213
214
ef344ff8 215wxApp::wxApp() : m_mainLoop(NULL)
7bdc1879
VS
216{
217}
218
219wxApp::~wxApp()
220{
221}
222
634f6a1f
VS
223bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode)
224{
225 if ( !mode.IsOk() )
226 {
227 return FALSE;
228 }
229 if ( g_displayDC != NULL )
230 {
231 // FIXME_MGL -- we currently don't allow to switch video mode
1f43b5c9 232 // more than once. This can hopefully be changed...
634f6a1f
VS
233 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
234 return FALSE;
235 }
1f43b5c9
VS
236
237 if ( !wxCreateMGL_WM(mode) )
238 return FALSE;
239 gs_rootWindow = new wxRootWindow;
240
634f6a1f 241 m_displayMode = mode;
1f43b5c9 242
634f6a1f
VS
243 return TRUE;
244}
245
7bdc1879
VS
246bool wxApp::OnInitGui()
247{
7bdc1879
VS
248 if ( !wxAppBase::OnInitGui() )
249 return FALSE;
250
d76048f5
VS
251#ifdef __WXDEBUG__
252 // MGL redirects stdout and stderr to physical console, so lets redirect
253 // it to file. Do it only when WXDEBUG environment variable is set
254 if ( wxGetEnv(wxT("WXDEBUG"), NULL) )
255 freopen("output.err", "wt", stderr);
2ec3892d
VS
256#endif
257
d76048f5
VS
258 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
259 if ( oldLog ) delete oldLog;
260
7bdc1879
VS
261 return TRUE;
262}
263
264bool wxApp::ProcessIdle()
265{
266 wxIdleEvent event;
267 event.SetEventObject(this);
268 ProcessEvent(event);
269
270 return event.MoreRequested();
271}
272
273void wxApp::OnIdle(wxIdleEvent &event)
274{
275 static bool s_inOnIdle = FALSE;
276
277 /* Avoid recursion (via ProcessEvent default case) */
278 if (s_inOnIdle)
279 return;
280
281 s_inOnIdle = TRUE;
282
283 /* Resend in the main thread events which have been prepared in other
284 threads */
285 ProcessPendingEvents();
286
287 // 'Garbage' collection of windows deleted with Close().
288 DeletePendingObjects();
289
d76048f5
VS
290#if wxUSE_LOG
291 // flush the logged messages if any
292 wxLog::FlushActive();
293#endif // wxUSE_LOG
294
7bdc1879
VS
295 // Send OnIdle events to all windows
296 if ( SendIdleEvents() )
297 event.RequestMore(TRUE);
298
299 s_inOnIdle = FALSE;
300}
301
302bool wxApp::SendIdleEvents()
303{
304 bool needMore = FALSE;
305
306 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
307 while (node)
308 {
309 wxWindow* win = node->GetData();
310 if ( SendIdleEvents(win) )
311 needMore = TRUE;
312 node = node->GetNext();
313 }
314
315 return needMore;
316}
317
318bool wxApp::SendIdleEvents(wxWindow* win)
319{
320 bool needMore = FALSE;
321
322 wxIdleEvent event;
323 event.SetEventObject(win);
324
325 win->GetEventHandler()->ProcessEvent(event);
326
7bdc1879
VS
327 if ( event.MoreRequested() )
328 needMore = TRUE;
7bdc1879
VS
329
330 wxNode* node = win->GetChildren().First();
331 while (node)
332 {
333 wxWindow* win = (wxWindow*) node->Data();
334 if ( SendIdleEvents(win) )
335 needMore = TRUE;
336
337 node = node->Next();
338 }
339 return needMore;
340}
341
342int wxApp::MainLoop()
343{
fd495ab3 344 int rt;
ef344ff8
VS
345 m_mainLoop = new wxEventLoop;
346
347 rt = m_mainLoop->Run();
348
349 delete m_mainLoop;
350 m_mainLoop = NULL;
fd495ab3 351 return rt;
7bdc1879
VS
352}
353
354void wxApp::ExitMainLoop()
355{
ef344ff8
VS
356 if ( m_mainLoop )
357 m_mainLoop->Exit(0);
7bdc1879
VS
358}
359
360bool wxApp::Initialized()
361{
bd73ba41 362 return (wxTopLevelWindows.GetCount() != 0);
7bdc1879
VS
363}
364
365bool wxApp::Pending()
366{
ef344ff8 367 return wxEventLoop::GetActive()->Pending();
7bdc1879
VS
368}
369
370void wxApp::Dispatch()
32b8ec41 371{
ef344ff8 372 wxEventLoop::GetActive()->Dispatch();
32b8ec41
VZ
373}
374
7bdc1879
VS
375void wxApp::DeletePendingObjects()
376{
377 wxNode *node = wxPendingDelete.First();
378 while (node)
379 {
380 wxObject *obj = (wxObject *)node->Data();
381
382 delete obj;
383
384 if ( wxPendingDelete.Find(obj) )
385 delete node;
32b8ec41 386
7bdc1879
VS
387 node = wxPendingDelete.First();
388 }
389}
390
391bool wxApp::Initialize()
32b8ec41 392{
bd73ba41
VS
393 if ( MGL_init(".", NULL) == 0 )
394 return FALSE;
395
32b8ec41
VZ
396 wxBuffer = new wxChar[BUFSIZ + 512];
397
398 wxClassInfo::InitializeClasses();
7bdc1879 399
32b8ec41 400 wxSystemSettings::Init();
7bdc1879
VS
401
402#if wxUSE_INTL
403 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
404#endif
405
406 // GL: I'm annoyed ... I don't know where to put this and I don't want to
407 // create a module for that as it's part of the core.
408#if wxUSE_THREADS
409 wxPendingEvents = new wxList;
410 wxPendingEventsLocker = new wxCriticalSection;
411#endif
412
413 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
32b8ec41 414 wxTheColourDatabase->Initialize();
ef344ff8
VS
415
416 // Can't do this in wxModule, because fonts are needed by stock lists
417 wxTheFontsManager = new wxFontsManager;
7bdc1879 418
32b8ec41
VZ
419 wxInitializeStockLists();
420 wxInitializeStockObjects();
7bdc1879
VS
421
422#if wxUSE_WX_RESOURCES
423 wxInitializeResourceSystem();
424#endif
425
32b8ec41
VZ
426 wxModule::RegisterModules();
427 if (!wxModule::InitializeModules()) return FALSE;
7bdc1879 428
32b8ec41
VZ
429 return TRUE;
430}
431
7bdc1879
VS
432wxIcon wxApp::GetStdIcon(int which) const
433{
df028524 434 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
7bdc1879
VS
435}
436
437void wxApp::CleanUp()
438{
439#if wxUSE_LOG
7bdc1879
VS
440 // continuing to use user defined log target is unsafe from now on because
441 // some resources may be already unavailable, so replace it by something
442 // more safe
443 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
444 if ( oldlog )
445 delete oldlog;
446#endif // wxUSE_LOG
447
d76048f5
VS
448 delete gs_rootWindow;
449
7bdc1879
VS
450 wxModule::CleanUpModules();
451
452#if wxUSE_WX_RESOURCES
453 wxCleanUpResourceSystem();
454#endif
455
456 if (wxTheColourDatabase)
457 delete wxTheColourDatabase;
458
459 wxTheColourDatabase = (wxColourDatabase*) NULL;
460
461 wxDeleteStockObjects();
7bdc1879
VS
462 wxDeleteStockLists();
463
464 delete wxTheApp;
465 wxTheApp = (wxApp*) NULL;
466
df16a53e 467
7bdc1879
VS
468 // GL: I'm annoyed ... I don't know where to put this and I don't want to
469 // create a module for that as it's part of the core.
470#if wxUSE_THREADS
471 delete wxPendingEvents;
472 delete wxPendingEventsLocker;
473#endif
474
475 wxSystemSettings::Done();
476
7bdc1879
VS
477 wxClassInfo::CleanUpClasses();
478
df16a53e
VS
479 // Can't do this in wxModule, because fonts are needed by stock lists
480 // (do it after deleting wxTheApp and cleaning modules up, since somebody
481 // may be deleting fonts that lately)
482 delete wxTheFontsManager;
483 wxTheFontsManager = (wxFontsManager*) NULL;
484
485 delete[] wxBuffer;
486
7bdc1879
VS
487 // check for memory leaks
488#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
489 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
490 {
491 wxLogDebug(wxT("There were memory leaks.\n"));
492 wxDebugContext::Dump();
493 wxDebugContext::PrintStatistics();
494 }
495#endif // Debug
496
497#if wxUSE_LOG
498 // do this as the very last thing because everything else can log messages
499 wxLog::DontCreateOnDemand();
500
501 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
502 if (oldLog)
503 delete oldLog;
504#endif // wxUSE_LOG
505
506 wxDestroyMGL_WM();
507 MGL_exit();
508}
509
510
511int wxEntryStart(int argc, char *argv[])
512{
513 return wxApp::Initialize() ? 0 : -1;
514}
515
516
517int wxEntryInitGui()
518{
519 return wxTheApp->OnInitGui() ? 0 : -1;
520}
521
522
523void wxEntryCleanup()
524{
525 wxApp::CleanUp();
526}
527
528
529
530int wxEntry(int argc, char *argv[])
531{
532#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
533 // This seems to be necessary since there are 'rogue'
534 // objects present at this point (perhaps global objects?)
535 // Setting a checkpoint will ignore them as far as the
536 // memory checking facility is concerned.
537 // Of course you may argue that memory allocated in globals should be
538 // checked, but this is a reasonable compromise.
539 wxDebugContext::SetCheckpoint();
540#endif
541 int err = wxEntryStart(argc, argv);
542 if ( err )
543 return err;
544
545 if ( !wxTheApp )
546 {
547 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
548 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
549
550 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
551
552 wxObject *test_app = app_ini();
553
554 wxTheApp = (wxApp*) test_app;
555 }
556
557 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
558
559 wxTheApp->argc = argc;
560#if wxUSE_UNICODE
561 wxTheApp->argv = new wxChar*[argc+1];
562 int mb_argc = 0;
563 while (mb_argc < argc)
564 {
565 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
566 mb_argc++;
567 }
568 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
569#else
570 wxTheApp->argv = argv;
571#endif
572
573 wxString name(wxFileNameFromPath(argv[0]));
574 wxStripExtension(name);
575 wxTheApp->SetAppName(name);
576
577 int retValue;
578 retValue = wxEntryInitGui();
579
580 // Here frames insert themselves automatically into wxTopLevelWindows by
581 // getting created in OnInit().
582 if ( retValue == 0 )
583 {
584 if ( !wxTheApp->OnInit() )
585 retValue = -1;
586 }
587
588 if ( retValue == 0 )
589 {
590 /* delete pending toplevel windows (typically a single
591 dialog) so that, if there isn't any left, we don't
592 call OnRun() */
593 wxTheApp->DeletePendingObjects();
594
fd495ab3 595 if ( wxTheApp->Initialized() )
7bdc1879
VS
596 {
597 wxTheApp->OnRun();
598
599 wxWindow *topWindow = wxTheApp->GetTopWindow();
600 if ( topWindow )
601 {
602 /* Forcibly delete the window. */
603 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
604 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
605 {
606 topWindow->Close(TRUE);
607 wxTheApp->DeletePendingObjects();
608 }
609 else
610 {
611 delete topWindow;
612 wxTheApp->SetTopWindow((wxWindow*) NULL);
613 }
614 }
615
d76048f5
VS
616#if wxUSE_LOG
617 // flush the logged messages if any
618 wxLog *log = wxLog::GetActiveTarget();
619 if (log != NULL && log->HasPendingMessages())
620 log->Flush();
621#endif // wxUSE_LOG
7bdc1879
VS
622 retValue = wxTheApp->OnExit();
623 }
624 }
625
626 wxEntryCleanup();
627
628 return retValue;
629}