]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
changed ar options to reduce build verbosity
[wxWidgets.git] / src / mgl / app.cpp
CommitLineData
32b8ec41
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
32b8ec41 3// Author: Vaclav Slavik
7bdc1879 4// based on GTK and MSW implementations
32b8ec41 5// Id: $Id$
8f7b34a8 6// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
32b8ec41
VZ
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11 #pragma implementation "app.h"
12#endif
13
7bdc1879
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
17#ifdef __BORLANDC__
18 #pragma hdrstop
19#endif
20
2ec3892d 21
7bdc1879
VS
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"
2ec3892d 28 #include "wx/log.h"
7bdc1879
VS
29 #include "wx/intl.h"
30#endif
32b8ec41 31
7bdc1879 32#include "wx/app.h"
ef344ff8 33#include "wx/fontutil.h"
df028524
VS
34#include "wx/univ/theme.h"
35#include "wx/univ/renderer.h"
58061670 36#include "wx/univ/colschem.h"
7bdc1879 37#include "wx/mgl/private.h"
32b8ec41 38
2ec3892d
VS
39#define MGL_DEBUG
40
41#if defined(MGL_DEBUG) && !defined(__WXDEBUG__)
42#undef MGL_DEBUG
43#endif
44
32b8ec41
VZ
45//-----------------------------------------------------------------------------
46// Global data
47//-----------------------------------------------------------------------------
48
7bdc1879 49wxApp *wxTheApp = NULL;
32b8ec41
VZ
50wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
51
32b8ec41 52
7bdc1879
VS
53//-----------------------------------------------------------------------------
54// wxExit
55//-----------------------------------------------------------------------------
32b8ec41
VZ
56
57void wxExit()
58{
7bdc1879 59 MGL_exit();
32b8ec41
VZ
60 exit(0);
61}
62
63//-----------------------------------------------------------------------------
64// wxYield
65//-----------------------------------------------------------------------------
66
7bdc1879
VS
67static bool gs_inYield = FALSE;
68
e1218bd6 69bool wxApp::Yield(bool onlyIfNeeded)
32b8ec41 70{
e1218bd6
VS
71 if ( gs_inYield )
72 {
73 if ( !onlyIfNeeded )
74 {
75 wxFAIL_MSG( wxT("wxYield called recursively" ) );
76 }
77
78 return FALSE;
79 }
80
7bdc1879
VS
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
ef344ff8
VS
93 if ( wxEventLoop::GetActive() )
94 {
95 while (wxEventLoop::GetActive()->Pending())
96 wxEventLoop::GetActive()->Dispatch();
97 }
7bdc1879
VS
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
32b8ec41
VZ
108 return TRUE;
109}
110
7bdc1879 111
32b8ec41
VZ
112//-----------------------------------------------------------------------------
113// wxWakeUpIdle
114//-----------------------------------------------------------------------------
115
116void wxWakeUpIdle()
117{
7bdc1879
VS
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
32b8ec41
VZ
129}
130
58061670
VS
131//-----------------------------------------------------------------------------
132// Root window
133//-----------------------------------------------------------------------------
134
135class 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
152static wxRootWindow *gs_rootWindow = NULL;
153
154//-----------------------------------------------------------------------------
155// MGL initialization
156//-----------------------------------------------------------------------------
157
158static 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
190static 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
32b8ec41
VZ
204//-----------------------------------------------------------------------------
205// wxApp
206//-----------------------------------------------------------------------------
207
208IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
209
210BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
211 EVT_IDLE(wxApp::OnIdle)
212END_EVENT_TABLE()
213
214
ef344ff8 215wxApp::wxApp() : m_mainLoop(NULL)
7bdc1879
VS
216{
217}
218
219wxApp::~wxApp()
220{
221}
222
223bool wxApp::OnInitGui()
224{
7bdc1879
VS
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;
58061670
VS
232
233 // ...and this has to be done after wxUniv themes were initialized
234 gs_rootWindow = new wxRootWindow;
7bdc1879 235
2ec3892d
VS
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
7bdc1879
VS
242 return TRUE;
243}
244
245bool wxApp::ProcessIdle()
246{
247 wxIdleEvent event;
248 event.SetEventObject(this);
249 ProcessEvent(event);
250
251 return event.MoreRequested();
252}
253
254void 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
278bool 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
294bool wxApp::SendIdleEvents(wxWindow* win)
295{
296 bool needMore = FALSE;
297
298 wxIdleEvent event;
299 event.SetEventObject(win);
300
301 win->GetEventHandler()->ProcessEvent(event);
302
7bdc1879
VS
303 if ( event.MoreRequested() )
304 needMore = TRUE;
7bdc1879
VS
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
318int wxApp::MainLoop()
319{
fd495ab3 320 int rt;
ef344ff8
VS
321 m_mainLoop = new wxEventLoop;
322
323 rt = m_mainLoop->Run();
324
325 delete m_mainLoop;
326 m_mainLoop = NULL;
fd495ab3 327 return rt;
7bdc1879
VS
328}
329
330void wxApp::ExitMainLoop()
331{
ef344ff8
VS
332 if ( m_mainLoop )
333 m_mainLoop->Exit(0);
7bdc1879
VS
334}
335
336bool wxApp::Initialized()
337{
bd73ba41 338 return (wxTopLevelWindows.GetCount() != 0);
7bdc1879
VS
339}
340
341bool wxApp::Pending()
342{
ef344ff8 343 return wxEventLoop::GetActive()->Pending();
7bdc1879
VS
344}
345
346void wxApp::Dispatch()
32b8ec41 347{
ef344ff8 348 wxEventLoop::GetActive()->Dispatch();
32b8ec41
VZ
349}
350
7bdc1879
VS
351void 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;
32b8ec41 362
7bdc1879
VS
363 node = wxPendingDelete.First();
364 }
365}
366
367bool wxApp::Initialize()
32b8ec41 368{
bd73ba41
VS
369 if ( MGL_init(".", NULL) == 0 )
370 return FALSE;
371
32b8ec41
VZ
372 wxBuffer = new wxChar[BUFSIZ + 512];
373
374 wxClassInfo::InitializeClasses();
7bdc1879 375
32b8ec41 376 wxSystemSettings::Init();
7bdc1879
VS
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);
32b8ec41 390 wxTheColourDatabase->Initialize();
ef344ff8
VS
391
392 // Can't do this in wxModule, because fonts are needed by stock lists
393 wxTheFontsManager = new wxFontsManager;
7bdc1879 394
32b8ec41
VZ
395 wxInitializeStockLists();
396 wxInitializeStockObjects();
7bdc1879
VS
397
398#if wxUSE_WX_RESOURCES
399 wxInitializeResourceSystem();
400#endif
401
32b8ec41
VZ
402 wxModule::RegisterModules();
403 if (!wxModule::InitializeModules()) return FALSE;
7bdc1879 404
32b8ec41
VZ
405 return TRUE;
406}
407
7bdc1879
VS
408wxIcon wxApp::GetStdIcon(int which) const
409{
df028524 410 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
7bdc1879
VS
411}
412
413void wxApp::CleanUp()
414{
58061670
VS
415 delete gs_rootWindow;
416
7bdc1879
VS
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();
7bdc1879
VS
443 wxDeleteStockLists();
444
ef344ff8
VS
445 // Can't do this in wxModule, because fonts are needed by stock lists
446 delete wxTheFontsManager;
447 wxTheFontsManager = (wxFontsManager*) NULL;
448
7bdc1879
VS
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
489int wxEntryStart(int argc, char *argv[])
490{
491 return wxApp::Initialize() ? 0 : -1;
492}
493
494
495int wxEntryInitGui()
496{
497 return wxTheApp->OnInitGui() ? 0 : -1;
498}
499
500
501void wxEntryCleanup()
502{
503 wxApp::CleanUp();
504}
505
506
507
508int 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
fd495ab3 573 if ( wxTheApp->Initialized() )
7bdc1879
VS
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}