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