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