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