]> git.saurik.com Git - wxWidgets.git/blob - src/mgl/app.cpp
make --static flag act on --cppflags too.
[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 wxLogError(_("Mode %ix%i-%i not available."),
174 displayMode.GetScreenSize().x,
175 displayMode.GetScreenSize().y,
176 displayMode.GetDepth());
177 return FALSE;
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 }
222
223 wxApp::~wxApp()
224 {
225 }
226
227 bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode)
228 {
229 if ( !mode.IsOk() )
230 {
231 return FALSE;
232 }
233 if ( g_displayDC != NULL )
234 {
235 // FIXME_MGL -- we currently don't allow to switch video mode
236 // more than once. This can hopefully be changed...
237 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
238 return FALSE;
239 }
240
241 if ( !wxCreateMGL_WM(mode) )
242 return FALSE;
243 gs_rootWindow = new wxRootWindow;
244
245 m_displayMode = mode;
246
247 return TRUE;
248 }
249
250 bool wxApp::OnInitGui()
251 {
252 if ( !wxAppBase::OnInitGui() )
253 return FALSE;
254
255 #ifdef MGL_DEBUG
256 // That damn MGL redirects stdin and stdout to physical console
257 FILE *file = fopen("stderr", "wt");
258 wxLog::SetActiveTarget(new wxLogStderr(file));
259 #endif
260
261 return TRUE;
262 }
263
264 bool wxApp::ProcessIdle()
265 {
266 wxIdleEvent event;
267 event.SetEventObject(this);
268 ProcessEvent(event);
269
270 return event.MoreRequested();
271 }
272
273 void 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
290 // Send OnIdle events to all windows
291 if ( SendIdleEvents() )
292 event.RequestMore(TRUE);
293
294 s_inOnIdle = FALSE;
295 }
296
297 bool wxApp::SendIdleEvents()
298 {
299 bool needMore = FALSE;
300
301 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
302 while (node)
303 {
304 wxWindow* win = node->GetData();
305 if ( SendIdleEvents(win) )
306 needMore = TRUE;
307 node = node->GetNext();
308 }
309
310 return needMore;
311 }
312
313 bool wxApp::SendIdleEvents(wxWindow* win)
314 {
315 bool needMore = FALSE;
316
317 wxIdleEvent event;
318 event.SetEventObject(win);
319
320 win->GetEventHandler()->ProcessEvent(event);
321
322 if ( event.MoreRequested() )
323 needMore = TRUE;
324
325 wxNode* node = win->GetChildren().First();
326 while (node)
327 {
328 wxWindow* win = (wxWindow*) node->Data();
329 if ( SendIdleEvents(win) )
330 needMore = TRUE;
331
332 node = node->Next();
333 }
334 return needMore;
335 }
336
337 int wxApp::MainLoop()
338 {
339 int rt;
340 m_mainLoop = new wxEventLoop;
341
342 rt = m_mainLoop->Run();
343
344 delete m_mainLoop;
345 m_mainLoop = NULL;
346 return rt;
347 }
348
349 void wxApp::ExitMainLoop()
350 {
351 if ( m_mainLoop )
352 m_mainLoop->Exit(0);
353 }
354
355 bool wxApp::Initialized()
356 {
357 return (wxTopLevelWindows.GetCount() != 0);
358 }
359
360 bool wxApp::Pending()
361 {
362 return wxEventLoop::GetActive()->Pending();
363 }
364
365 void wxApp::Dispatch()
366 {
367 wxEventLoop::GetActive()->Dispatch();
368 }
369
370 void wxApp::DeletePendingObjects()
371 {
372 wxNode *node = wxPendingDelete.First();
373 while (node)
374 {
375 wxObject *obj = (wxObject *)node->Data();
376
377 delete obj;
378
379 if ( wxPendingDelete.Find(obj) )
380 delete node;
381
382 node = wxPendingDelete.First();
383 }
384 }
385
386 bool wxApp::Initialize()
387 {
388 if ( MGL_init(".", NULL) == 0 )
389 return FALSE;
390
391 wxBuffer = new wxChar[BUFSIZ + 512];
392
393 wxClassInfo::InitializeClasses();
394
395 wxSystemSettings::Init();
396
397 #if wxUSE_INTL
398 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
399 #endif
400
401 // GL: I'm annoyed ... I don't know where to put this and I don't want to
402 // create a module for that as it's part of the core.
403 #if wxUSE_THREADS
404 wxPendingEvents = new wxList;
405 wxPendingEventsLocker = new wxCriticalSection;
406 #endif
407
408 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
409 wxTheColourDatabase->Initialize();
410
411 // Can't do this in wxModule, because fonts are needed by stock lists
412 wxTheFontsManager = new wxFontsManager;
413
414 wxInitializeStockLists();
415 wxInitializeStockObjects();
416
417 #if wxUSE_WX_RESOURCES
418 wxInitializeResourceSystem();
419 #endif
420
421 wxModule::RegisterModules();
422 if (!wxModule::InitializeModules()) return FALSE;
423
424 return TRUE;
425 }
426
427 wxIcon wxApp::GetStdIcon(int which) const
428 {
429 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
430 }
431
432 void wxApp::CleanUp()
433 {
434 delete gs_rootWindow;
435
436 #if wxUSE_LOG
437 // flush the logged messages if any
438 wxLog *log = wxLog::GetActiveTarget();
439 if (log != NULL && log->HasPendingMessages())
440 log->Flush();
441
442 // continuing to use user defined log target is unsafe from now on because
443 // some resources may be already unavailable, so replace it by something
444 // more safe
445 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
446 if ( oldlog )
447 delete oldlog;
448 #endif // wxUSE_LOG
449
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();
462 wxDeleteStockLists();
463
464 // Can't do this in wxModule, because fonts are needed by stock lists
465 delete wxTheFontsManager;
466 wxTheFontsManager = (wxFontsManager*) NULL;
467
468 delete wxTheApp;
469 wxTheApp = (wxApp*) NULL;
470
471 // GL: I'm annoyed ... I don't know where to put this and I don't want to
472 // create a module for that as it's part of the core.
473 #if wxUSE_THREADS
474 delete wxPendingEvents;
475 delete wxPendingEventsLocker;
476 #endif
477
478 wxSystemSettings::Done();
479
480 delete[] wxBuffer;
481
482 wxClassInfo::CleanUpClasses();
483
484 // check for memory leaks
485 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
486 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
487 {
488 wxLogDebug(wxT("There were memory leaks.\n"));
489 wxDebugContext::Dump();
490 wxDebugContext::PrintStatistics();
491 }
492 #endif // Debug
493
494 #if wxUSE_LOG
495 // do this as the very last thing because everything else can log messages
496 wxLog::DontCreateOnDemand();
497
498 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
499 if (oldLog)
500 delete oldLog;
501 #endif // wxUSE_LOG
502
503 wxDestroyMGL_WM();
504 MGL_exit();
505 }
506
507
508 int wxEntryStart(int argc, char *argv[])
509 {
510 return wxApp::Initialize() ? 0 : -1;
511 }
512
513
514 int wxEntryInitGui()
515 {
516 return wxTheApp->OnInitGui() ? 0 : -1;
517 }
518
519
520 void wxEntryCleanup()
521 {
522 wxApp::CleanUp();
523 }
524
525
526
527 int wxEntry(int argc, char *argv[])
528 {
529 #if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
530 // This seems to be necessary since there are 'rogue'
531 // objects present at this point (perhaps global objects?)
532 // Setting a checkpoint will ignore them as far as the
533 // memory checking facility is concerned.
534 // Of course you may argue that memory allocated in globals should be
535 // checked, but this is a reasonable compromise.
536 wxDebugContext::SetCheckpoint();
537 #endif
538 int err = wxEntryStart(argc, argv);
539 if ( err )
540 return err;
541
542 if ( !wxTheApp )
543 {
544 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
545 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
546
547 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
548
549 wxObject *test_app = app_ini();
550
551 wxTheApp = (wxApp*) test_app;
552 }
553
554 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
555
556 wxTheApp->argc = argc;
557 #if wxUSE_UNICODE
558 wxTheApp->argv = new wxChar*[argc+1];
559 int mb_argc = 0;
560 while (mb_argc < argc)
561 {
562 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
563 mb_argc++;
564 }
565 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
566 #else
567 wxTheApp->argv = argv;
568 #endif
569
570 wxString name(wxFileNameFromPath(argv[0]));
571 wxStripExtension(name);
572 wxTheApp->SetAppName(name);
573
574 int retValue;
575 retValue = wxEntryInitGui();
576
577 // Here frames insert themselves automatically into wxTopLevelWindows by
578 // getting created in OnInit().
579 if ( retValue == 0 )
580 {
581 if ( !wxTheApp->OnInit() )
582 retValue = -1;
583 }
584
585 if ( retValue == 0 )
586 {
587 /* delete pending toplevel windows (typically a single
588 dialog) so that, if there isn't any left, we don't
589 call OnRun() */
590 wxTheApp->DeletePendingObjects();
591
592 if ( wxTheApp->Initialized() )
593 {
594 wxTheApp->OnRun();
595
596 wxWindow *topWindow = wxTheApp->GetTopWindow();
597 if ( topWindow )
598 {
599 /* Forcibly delete the window. */
600 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
601 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
602 {
603 topWindow->Close(TRUE);
604 wxTheApp->DeletePendingObjects();
605 }
606 else
607 {
608 delete topWindow;
609 wxTheApp->SetTopWindow((wxWindow*) NULL);
610 }
611 }
612
613 retValue = wxTheApp->OnExit();
614 }
615 }
616
617 wxEntryCleanup();
618
619 return retValue;
620 }