]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/app.cpp
implemented display mode setting
[wxWidgets.git] / src / mgl / app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: app.cpp
3 // Author: Vaclav Slavik
4 // based on GTK and MSW implementations
5 // Id: $Id$
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "app.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21
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"
28 #include "wx/log.h"
29 #include "wx/intl.h"
30 #endif
31
32 #include "wx/app.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/mgl/private.h"
38
39 #define MGL_DEBUG
40
41 #if defined(MGL_DEBUG) && !defined(__WXDEBUG__)
42 #undef MGL_DEBUG
43 #endif
44
45 //-----------------------------------------------------------------------------
46 // Global data
47 //-----------------------------------------------------------------------------
48
49 wxApp *wxTheApp = NULL;
50 wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
51
52
53 //-----------------------------------------------------------------------------
54 // wxExit
55 //-----------------------------------------------------------------------------
56
57 void wxExit()
58 {
59 MGL_exit();
60 exit(0);
61 }
62
63 //-----------------------------------------------------------------------------
64 // wxYield
65 //-----------------------------------------------------------------------------
66
67 static bool gs_inYield = FALSE;
68
69 bool wxApp::Yield(bool onlyIfNeeded)
70 {
71 if ( gs_inYield )
72 {
73 if ( !onlyIfNeeded )
74 {
75 wxFAIL_MSG( wxT("wxYield called recursively" ) );
76 }
77
78 return FALSE;
79 }
80
81 #if wxUSE_THREADS
82 if ( !wxThread::IsMain() )
83 {
84 // can't process events from other threads, MGL is thread-unsafe
85 return TRUE;
86 }
87 #endif // wxUSE_THREADS
88
89 gs_inYield = TRUE;
90
91 wxLog::Suspend();
92
93 if ( wxEventLoop::GetActive() )
94 {
95 while (wxEventLoop::GetActive()->Pending())
96 wxEventLoop::GetActive()->Dispatch();
97 }
98
99 /* it's necessary to call ProcessIdle() to update the frames sizes which
100 might have been changed (it also will update other things set from
101 OnUpdateUI() which is a nice (and desired) side effect) */
102 while (wxTheApp->ProcessIdle()) { }
103
104 wxLog::Resume();
105
106 gs_inYield = FALSE;
107
108 return TRUE;
109 }
110
111
112 //-----------------------------------------------------------------------------
113 // wxWakeUpIdle
114 //-----------------------------------------------------------------------------
115
116 void wxWakeUpIdle()
117 {
118 #if wxUSE_THREADS
119 if (!wxThread::IsMain())
120 wxMutexGuiEnter();
121 #endif
122
123 while (wxTheApp->ProcessIdle()) {}
124
125 #if wxUSE_THREADS
126 if (!wxThread::IsMain())
127 wxMutexGuiLeave();
128 #endif
129 }
130
131 //-----------------------------------------------------------------------------
132 // Root window
133 //-----------------------------------------------------------------------------
134
135 class wxRootWindow : public wxWindow
136 {
137 public:
138 wxRootWindow() : wxWindow(NULL, -1)
139 {
140 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng));
141 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP));
142 }
143 ~wxRootWindow()
144 {
145 // we don't want to delete MGL_WM's rootWnd
146 m_wnd = NULL;
147 }
148
149 virtual bool AcceptsFocus() { return FALSE; }
150 };
151
152 static wxRootWindow *gs_rootWindow = NULL;
153
154 //-----------------------------------------------------------------------------
155 // MGL initialization
156 //-----------------------------------------------------------------------------
157
158 static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode)
159 {
160 int mode;
161 int refresh = MGL_DEFAULT_REFRESH;
162
163 #if wxUSE_SYSTEM_OPTIONS
164 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh") )
165 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
166 #endif
167
168 mode = MGL_findMode(displayMode.GetScreenSize().x,
169 displayMode.GetScreenSize().y,
170 displayMode.GetDepth());
171 if ( mode == -1 )
172 {
173 wxLogWarning(_("Mode %ix%i-%i not available, falling back to default mode."),
174 displayMode.GetScreenSize().x,
175 displayMode.GetScreenSize().y,
176 displayMode.GetDepth());
177 mode = 0; // always available
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;
190
191 return TRUE;
192 }
193
194 static 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
208 //-----------------------------------------------------------------------------
209 // wxApp
210 //-----------------------------------------------------------------------------
211
212 IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
213
214 BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
215 EVT_IDLE(wxApp::OnIdle)
216 END_EVENT_TABLE()
217
218
219 wxApp::wxApp() : m_mainLoop(NULL)
220 {
221 m_displayMode = wxDisplayModeInfo(wxSize(640, 480), 16);
222 }
223
224 wxApp::~wxApp()
225 {
226 }
227
228 bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode)
229 {
230 if ( !mode.IsOk() )
231 {
232 return FALSE;
233 }
234 if ( g_displayDC != NULL )
235 {
236 // FIXME_MGL -- we currently don't allow to switch video mode
237 // at runtime. This can hopefully be changed...
238 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
239 return FALSE;
240 }
241 m_displayMode = mode;
242 return TRUE;
243 }
244
245 bool wxApp::OnInitGui()
246 {
247 if ( !wxCreateMGL_WM(m_displayMode) )
248 return FALSE;
249
250 // This has to be done *after* wxCreateMGL_WM() because it initializes
251 // wxUniv's themes
252 if ( !wxAppBase::OnInitGui() )
253 return FALSE;
254
255 // ...and this has to be done after wxUniv themes were initialized
256 gs_rootWindow = new wxRootWindow;
257
258 #ifdef MGL_DEBUG
259 // That damn MGL redirects stdin and stdout to physical console
260 FILE *file = fopen("stderr", "wt");
261 wxLog::SetActiveTarget(new wxLogStderr(file));
262 #endif
263
264 return TRUE;
265 }
266
267 bool wxApp::ProcessIdle()
268 {
269 wxIdleEvent event;
270 event.SetEventObject(this);
271 ProcessEvent(event);
272
273 return event.MoreRequested();
274 }
275
276 void wxApp::OnIdle(wxIdleEvent &event)
277 {
278 static bool s_inOnIdle = FALSE;
279
280 /* Avoid recursion (via ProcessEvent default case) */
281 if (s_inOnIdle)
282 return;
283
284 s_inOnIdle = TRUE;
285
286 /* Resend in the main thread events which have been prepared in other
287 threads */
288 ProcessPendingEvents();
289
290 // 'Garbage' collection of windows deleted with Close().
291 DeletePendingObjects();
292
293 // Send OnIdle events to all windows
294 if ( SendIdleEvents() )
295 event.RequestMore(TRUE);
296
297 s_inOnIdle = FALSE;
298 }
299
300 bool wxApp::SendIdleEvents()
301 {
302 bool needMore = FALSE;
303
304 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
305 while (node)
306 {
307 wxWindow* win = node->GetData();
308 if ( SendIdleEvents(win) )
309 needMore = TRUE;
310 node = node->GetNext();
311 }
312
313 return needMore;
314 }
315
316 bool wxApp::SendIdleEvents(wxWindow* win)
317 {
318 bool needMore = FALSE;
319
320 wxIdleEvent event;
321 event.SetEventObject(win);
322
323 win->GetEventHandler()->ProcessEvent(event);
324
325 if ( event.MoreRequested() )
326 needMore = TRUE;
327
328 wxNode* node = win->GetChildren().First();
329 while (node)
330 {
331 wxWindow* win = (wxWindow*) node->Data();
332 if ( SendIdleEvents(win) )
333 needMore = TRUE;
334
335 node = node->Next();
336 }
337 return needMore;
338 }
339
340 int wxApp::MainLoop()
341 {
342 int rt;
343 m_mainLoop = new wxEventLoop;
344
345 rt = m_mainLoop->Run();
346
347 delete m_mainLoop;
348 m_mainLoop = NULL;
349 return rt;
350 }
351
352 void wxApp::ExitMainLoop()
353 {
354 if ( m_mainLoop )
355 m_mainLoop->Exit(0);
356 }
357
358 bool wxApp::Initialized()
359 {
360 return (wxTopLevelWindows.GetCount() != 0);
361 }
362
363 bool wxApp::Pending()
364 {
365 return wxEventLoop::GetActive()->Pending();
366 }
367
368 void wxApp::Dispatch()
369 {
370 wxEventLoop::GetActive()->Dispatch();
371 }
372
373 void wxApp::DeletePendingObjects()
374 {
375 wxNode *node = wxPendingDelete.First();
376 while (node)
377 {
378 wxObject *obj = (wxObject *)node->Data();
379
380 delete obj;
381
382 if ( wxPendingDelete.Find(obj) )
383 delete node;
384
385 node = wxPendingDelete.First();
386 }
387 }
388
389 bool wxApp::Initialize()
390 {
391 if ( MGL_init(".", NULL) == 0 )
392 return FALSE;
393
394 wxBuffer = new wxChar[BUFSIZ + 512];
395
396 wxClassInfo::InitializeClasses();
397
398 wxSystemSettings::Init();
399
400 #if wxUSE_INTL
401 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
402 #endif
403
404 // GL: I'm annoyed ... I don't know where to put this and I don't want to
405 // create a module for that as it's part of the core.
406 #if wxUSE_THREADS
407 wxPendingEvents = new wxList;
408 wxPendingEventsLocker = new wxCriticalSection;
409 #endif
410
411 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
412 wxTheColourDatabase->Initialize();
413
414 // Can't do this in wxModule, because fonts are needed by stock lists
415 wxTheFontsManager = new wxFontsManager;
416
417 wxInitializeStockLists();
418 wxInitializeStockObjects();
419
420 #if wxUSE_WX_RESOURCES
421 wxInitializeResourceSystem();
422 #endif
423
424 wxModule::RegisterModules();
425 if (!wxModule::InitializeModules()) return FALSE;
426
427 return TRUE;
428 }
429
430 wxIcon wxApp::GetStdIcon(int which) const
431 {
432 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
433 }
434
435 void wxApp::CleanUp()
436 {
437 delete gs_rootWindow;
438
439 #if wxUSE_LOG
440 // flush the logged messages if any
441 wxLog *log = wxLog::GetActiveTarget();
442 if (log != NULL && log->HasPendingMessages())
443 log->Flush();
444
445 // continuing to use user defined log target is unsafe from now on because
446 // some resources may be already unavailable, so replace it by something
447 // more safe
448 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
449 if ( oldlog )
450 delete oldlog;
451 #endif // wxUSE_LOG
452
453 wxModule::CleanUpModules();
454
455 #if wxUSE_WX_RESOURCES
456 wxCleanUpResourceSystem();
457 #endif
458
459 if (wxTheColourDatabase)
460 delete wxTheColourDatabase;
461
462 wxTheColourDatabase = (wxColourDatabase*) NULL;
463
464 wxDeleteStockObjects();
465 wxDeleteStockLists();
466
467 // Can't do this in wxModule, because fonts are needed by stock lists
468 delete wxTheFontsManager;
469 wxTheFontsManager = (wxFontsManager*) NULL;
470
471 delete wxTheApp;
472 wxTheApp = (wxApp*) NULL;
473
474 // GL: I'm annoyed ... I don't know where to put this and I don't want to
475 // create a module for that as it's part of the core.
476 #if wxUSE_THREADS
477 delete wxPendingEvents;
478 delete wxPendingEventsLocker;
479 #endif
480
481 wxSystemSettings::Done();
482
483 delete[] wxBuffer;
484
485 wxClassInfo::CleanUpClasses();
486
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
511 int wxEntryStart(int argc, char *argv[])
512 {
513 return wxApp::Initialize() ? 0 : -1;
514 }
515
516
517 int wxEntryInitGui()
518 {
519 return wxTheApp->OnInitGui() ? 0 : -1;
520 }
521
522
523 void wxEntryCleanup()
524 {
525 wxApp::CleanUp();
526 }
527
528
529
530 int 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
595 if ( wxTheApp->Initialized() )
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
616 retValue = wxTheApp->OnExit();
617 }
618 }
619
620 wxEntryCleanup();
621
622 return retValue;
623 }