]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
No change
[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$
c41c20a5 6// Copyright: (c) 2001-2002 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
b7d5acd0
VS
145 virtual bool AcceptsFocus() const { return FALSE; }
146
147 DECLARE_DYNAMIC_CLASS(wxRootWindow)
58061670
VS
148};
149
b7d5acd0
VS
150IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow)
151
58061670
VS
152static wxRootWindow *gs_rootWindow = NULL;
153
154//-----------------------------------------------------------------------------
155// MGL initialization
156//-----------------------------------------------------------------------------
157
634f6a1f 158static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode)
58061670
VS
159{
160 int mode;
58061670
VS
161 int refresh = MGL_DEFAULT_REFRESH;
162
163#if wxUSE_SYSTEM_OPTIONS
05c9ccbe 164 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
58061670
VS
165 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
166#endif
167
d76048f5
VS
168 mode = MGL_findMode(displayMode.GetWidth(),
169 displayMode.GetHeight(),
634f6a1f 170 displayMode.GetDepth());
58061670
VS
171 if ( mode == -1 )
172 {
1f43b5c9 173 wxLogError(_("Mode %ix%i-%i not available."),
d76048f5
VS
174 displayMode.GetWidth(),
175 displayMode.GetHeight(),
634f6a1f 176 displayMode.GetDepth());
1f43b5c9 177 return FALSE;
58061670
VS
178 }
179 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
180 if ( !g_displayDC->isValid() )
181 {
182 delete g_displayDC;
183 g_displayDC = NULL;
184 return FALSE;
185 }
186
187 g_winMng = MGL_wmCreate(g_displayDC->getDC());
188 if (!g_winMng)
189 return FALSE;
1f43b5c9 190
58061670
VS
191 return TRUE;
192}
193
194static void wxDestroyMGL_WM()
195{
196 if ( g_winMng )
197 {
198 MGL_wmDestroy(g_winMng);
199 g_winMng = NULL;
200 }
201 if ( g_displayDC )
202 {
203 delete g_displayDC;
204 g_displayDC = NULL;
205 }
206}
207
32b8ec41
VZ
208//-----------------------------------------------------------------------------
209// wxApp
210//-----------------------------------------------------------------------------
211
212IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
213
214BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
215 EVT_IDLE(wxApp::OnIdle)
216END_EVENT_TABLE()
217
218
ef344ff8 219wxApp::wxApp() : m_mainLoop(NULL)
7bdc1879
VS
220{
221}
222
223wxApp::~wxApp()
224{
225}
226
a3e76614
VS
227wxDisplayModeInfo wxGetDefaultDisplayMode()
228{
229 wxString mode;
230 unsigned w, h, bpp;
231
232 if ( !wxGetEnv(wxT("WXMODE"), &mode) ||
233 (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) )
234 {
235 w = 640, h = 480, bpp = 16;
236 }
237
238 return wxDisplayModeInfo(w, h, bpp);
239}
240
634f6a1f
VS
241bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode)
242{
243 if ( !mode.IsOk() )
244 {
245 return FALSE;
246 }
247 if ( g_displayDC != NULL )
248 {
249 // FIXME_MGL -- we currently don't allow to switch video mode
1f43b5c9 250 // more than once. This can hopefully be changed...
634f6a1f
VS
251 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
252 return FALSE;
253 }
1f43b5c9
VS
254
255 if ( !wxCreateMGL_WM(mode) )
256 return FALSE;
257 gs_rootWindow = new wxRootWindow;
258
634f6a1f 259 m_displayMode = mode;
1f43b5c9 260
634f6a1f
VS
261 return TRUE;
262}
263
7bdc1879
VS
264bool wxApp::OnInitGui()
265{
7bdc1879
VS
266 if ( !wxAppBase::OnInitGui() )
267 return FALSE;
268
d76048f5
VS
269#ifdef __WXDEBUG__
270 // MGL redirects stdout and stderr to physical console, so lets redirect
271 // it to file. Do it only when WXDEBUG environment variable is set
343e418c
VS
272 wxString redirect;
273 if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
274 freopen(redirect.mb_str(), "wt", stderr);
2ec3892d
VS
275#endif
276
d76048f5
VS
277 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
278 if ( oldLog ) delete oldLog;
279
7bdc1879
VS
280 return TRUE;
281}
282
283bool wxApp::ProcessIdle()
284{
285 wxIdleEvent event;
286 event.SetEventObject(this);
287 ProcessEvent(event);
288
289 return event.MoreRequested();
290}
291
292void wxApp::OnIdle(wxIdleEvent &event)
293{
294 static bool s_inOnIdle = FALSE;
295
296 /* Avoid recursion (via ProcessEvent default case) */
297 if (s_inOnIdle)
298 return;
299
300 s_inOnIdle = TRUE;
301
302 /* Resend in the main thread events which have been prepared in other
303 threads */
304 ProcessPendingEvents();
305
306 // 'Garbage' collection of windows deleted with Close().
307 DeletePendingObjects();
308
d76048f5
VS
309#if wxUSE_LOG
310 // flush the logged messages if any
311 wxLog::FlushActive();
312#endif // wxUSE_LOG
313
7bdc1879
VS
314 // Send OnIdle events to all windows
315 if ( SendIdleEvents() )
316 event.RequestMore(TRUE);
317
318 s_inOnIdle = FALSE;
319}
320
321bool wxApp::SendIdleEvents()
322{
323 bool needMore = FALSE;
324
325 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
326 while (node)
327 {
328 wxWindow* win = node->GetData();
329 if ( SendIdleEvents(win) )
330 needMore = TRUE;
331 node = node->GetNext();
332 }
333
334 return needMore;
335}
336
337bool wxApp::SendIdleEvents(wxWindow* win)
338{
339 bool needMore = FALSE;
340
341 wxIdleEvent event;
342 event.SetEventObject(win);
343
344 win->GetEventHandler()->ProcessEvent(event);
345
7bdc1879
VS
346 if ( event.MoreRequested() )
347 needMore = TRUE;
7bdc1879
VS
348
349 wxNode* node = win->GetChildren().First();
350 while (node)
351 {
352 wxWindow* win = (wxWindow*) node->Data();
353 if ( SendIdleEvents(win) )
354 needMore = TRUE;
355
356 node = node->Next();
357 }
358 return needMore;
359}
360
361int wxApp::MainLoop()
362{
fd495ab3 363 int rt;
ef344ff8
VS
364 m_mainLoop = new wxEventLoop;
365
366 rt = m_mainLoop->Run();
367
368 delete m_mainLoop;
369 m_mainLoop = NULL;
fd495ab3 370 return rt;
7bdc1879
VS
371}
372
373void wxApp::ExitMainLoop()
374{
ef344ff8
VS
375 if ( m_mainLoop )
376 m_mainLoop->Exit(0);
7bdc1879
VS
377}
378
379bool wxApp::Initialized()
380{
bd73ba41 381 return (wxTopLevelWindows.GetCount() != 0);
7bdc1879
VS
382}
383
384bool wxApp::Pending()
385{
ef344ff8 386 return wxEventLoop::GetActive()->Pending();
7bdc1879
VS
387}
388
389void wxApp::Dispatch()
32b8ec41 390{
ef344ff8 391 wxEventLoop::GetActive()->Dispatch();
32b8ec41
VZ
392}
393
7bdc1879
VS
394void wxApp::DeletePendingObjects()
395{
396 wxNode *node = wxPendingDelete.First();
397 while (node)
398 {
399 wxObject *obj = (wxObject *)node->Data();
400
401 delete obj;
402
403 if ( wxPendingDelete.Find(obj) )
404 delete node;
32b8ec41 405
7bdc1879
VS
406 node = wxPendingDelete.First();
407 }
408}
409
410bool wxApp::Initialize()
32b8ec41 411{
bd73ba41
VS
412 if ( MGL_init(".", NULL) == 0 )
413 return FALSE;
414
32b8ec41 415 wxClassInfo::InitializeClasses();
7bdc1879 416
7bdc1879
VS
417#if wxUSE_INTL
418 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
419#endif
420
421 // GL: I'm annoyed ... I don't know where to put this and I don't want to
422 // create a module for that as it's part of the core.
423#if wxUSE_THREADS
424 wxPendingEvents = new wxList;
425 wxPendingEventsLocker = new wxCriticalSection;
426#endif
427
428 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
32b8ec41 429 wxTheColourDatabase->Initialize();
ef344ff8
VS
430
431 // Can't do this in wxModule, because fonts are needed by stock lists
432 wxTheFontsManager = new wxFontsManager;
7bdc1879 433
32b8ec41
VZ
434 wxInitializeStockLists();
435 wxInitializeStockObjects();
7bdc1879
VS
436
437#if wxUSE_WX_RESOURCES
438 wxInitializeResourceSystem();
439#endif
440
32b8ec41
VZ
441 wxModule::RegisterModules();
442 if (!wxModule::InitializeModules()) return FALSE;
7bdc1879 443
32b8ec41
VZ
444 return TRUE;
445}
446
7bdc1879
VS
447void wxApp::CleanUp()
448{
449#if wxUSE_LOG
7bdc1879
VS
450 // continuing to use user defined log target is unsafe from now on because
451 // some resources may be already unavailable, so replace it by something
452 // more safe
453 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
454 if ( oldlog )
455 delete oldlog;
456#endif // wxUSE_LOG
457
d76048f5
VS
458 delete gs_rootWindow;
459
7bdc1879
VS
460 wxModule::CleanUpModules();
461
462#if wxUSE_WX_RESOURCES
463 wxCleanUpResourceSystem();
464#endif
465
466 if (wxTheColourDatabase)
467 delete wxTheColourDatabase;
468
469 wxTheColourDatabase = (wxColourDatabase*) NULL;
470
471 wxDeleteStockObjects();
7bdc1879
VS
472 wxDeleteStockLists();
473
474 delete wxTheApp;
475 wxTheApp = (wxApp*) NULL;
476
df16a53e 477
7bdc1879
VS
478 // GL: I'm annoyed ... I don't know where to put this and I don't want to
479 // create a module for that as it's part of the core.
480#if wxUSE_THREADS
481 delete wxPendingEvents;
482 delete wxPendingEventsLocker;
483#endif
484
7bdc1879
VS
485 wxClassInfo::CleanUpClasses();
486
df16a53e
VS
487 // Can't do this in wxModule, because fonts are needed by stock lists
488 // (do it after deleting wxTheApp and cleaning modules up, since somebody
489 // may be deleting fonts that lately)
490 delete wxTheFontsManager;
491 wxTheFontsManager = (wxFontsManager*) NULL;
492
7bdc1879
VS
493 // check for memory leaks
494#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
495 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
496 {
497 wxLogDebug(wxT("There were memory leaks.\n"));
498 wxDebugContext::Dump();
499 wxDebugContext::PrintStatistics();
500 }
501#endif // Debug
502
503#if wxUSE_LOG
504 // do this as the very last thing because everything else can log messages
505 wxLog::DontCreateOnDemand();
506
507 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
508 if (oldLog)
509 delete oldLog;
510#endif // wxUSE_LOG
511
512 wxDestroyMGL_WM();
513 MGL_exit();
514}
515
516
517int wxEntryStart(int argc, char *argv[])
518{
519 return wxApp::Initialize() ? 0 : -1;
520}
521
522
523int wxEntryInitGui()
524{
525 return wxTheApp->OnInitGui() ? 0 : -1;
526}
527
528
529void wxEntryCleanup()
530{
531 wxApp::CleanUp();
532}
533
534
535
536int wxEntry(int argc, char *argv[])
537{
6ed89758
VS
538#ifdef __DJGPP__
539 // VS: disable long filenames under DJGPP as the very first thing,
540 // since SciTech MGL doesn't like them much...
541 wxSetEnv(wxT("LFN"), wxT("N"));
542#endif
543
7bdc1879
VS
544#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
545 // This seems to be necessary since there are 'rogue'
546 // objects present at this point (perhaps global objects?)
547 // Setting a checkpoint will ignore them as far as the
548 // memory checking facility is concerned.
549 // Of course you may argue that memory allocated in globals should be
550 // checked, but this is a reasonable compromise.
551 wxDebugContext::SetCheckpoint();
552#endif
553 int err = wxEntryStart(argc, argv);
554 if ( err )
555 return err;
556
557 if ( !wxTheApp )
558 {
559 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
560 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
561
562 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
563
564 wxObject *test_app = app_ini();
565
566 wxTheApp = (wxApp*) test_app;
567 }
568
569 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
570
571 wxTheApp->argc = argc;
572#if wxUSE_UNICODE
573 wxTheApp->argv = new wxChar*[argc+1];
574 int mb_argc = 0;
575 while (mb_argc < argc)
576 {
577 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
578 mb_argc++;
579 }
580 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
581#else
582 wxTheApp->argv = argv;
583#endif
584
585 wxString name(wxFileNameFromPath(argv[0]));
586 wxStripExtension(name);
587 wxTheApp->SetAppName(name);
588
589 int retValue;
590 retValue = wxEntryInitGui();
591
592 // Here frames insert themselves automatically into wxTopLevelWindows by
593 // getting created in OnInit().
594 if ( retValue == 0 )
595 {
596 if ( !wxTheApp->OnInit() )
597 retValue = -1;
598 }
599
600 if ( retValue == 0 )
601 {
602 /* delete pending toplevel windows (typically a single
603 dialog) so that, if there isn't any left, we don't
604 call OnRun() */
605 wxTheApp->DeletePendingObjects();
606
fd495ab3 607 if ( wxTheApp->Initialized() )
7bdc1879
VS
608 {
609 wxTheApp->OnRun();
610
611 wxWindow *topWindow = wxTheApp->GetTopWindow();
612 if ( topWindow )
613 {
614 /* Forcibly delete the window. */
615 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
616 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
617 {
618 topWindow->Close(TRUE);
619 wxTheApp->DeletePendingObjects();
620 }
621 else
622 {
623 delete topWindow;
624 wxTheApp->SetTopWindow((wxWindow*) NULL);
625 }
626 }
627
d76048f5
VS
628#if wxUSE_LOG
629 // flush the logged messages if any
630 wxLog *log = wxLog::GetActiveTarget();
631 if (log != NULL && log->HasPendingMessages())
632 log->Flush();
633#endif // wxUSE_LOG
7bdc1879
VS
634 retValue = wxTheApp->OnExit();
635 }
636 }
637
638 wxEntryCleanup();
639
640 return retValue;
641}