]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
make --static flag act on --cppflags too.
[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
634f6a1f 158static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode)
58061670
VS
159{
160 int mode;
58061670
VS
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
634f6a1f
VS
168 mode = MGL_findMode(displayMode.GetScreenSize().x,
169 displayMode.GetScreenSize().y,
170 displayMode.GetDepth());
58061670
VS
171 if ( mode == -1 )
172 {
1f43b5c9 173 wxLogError(_("Mode %ix%i-%i not available."),
634f6a1f
VS
174 displayMode.GetScreenSize().x,
175 displayMode.GetScreenSize().y,
176 displayMode.GetDepth());
1f43b5c9 177 return FALSE;
58061670
VS
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;
1f43b5c9 190
58061670
VS
191 return TRUE;
192}
193
194static 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
32b8ec41
VZ
208//-----------------------------------------------------------------------------
209// wxApp
210//-----------------------------------------------------------------------------
211
212IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
213
214BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
215 EVT_IDLE(wxApp::OnIdle)
216END_EVENT_TABLE()
217
218
ef344ff8 219wxApp::wxApp() : m_mainLoop(NULL)
7bdc1879
VS
220{
221}
222
223wxApp::~wxApp()
224{
225}
226
634f6a1f
VS
227bool 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
1f43b5c9 236 // more than once. This can hopefully be changed...
634f6a1f
VS
237 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
238 return FALSE;
239 }
1f43b5c9
VS
240
241 if ( !wxCreateMGL_WM(mode) )
242 return FALSE;
243 gs_rootWindow = new wxRootWindow;
244
634f6a1f 245 m_displayMode = mode;
1f43b5c9 246
634f6a1f
VS
247 return TRUE;
248}
249
7bdc1879
VS
250bool wxApp::OnInitGui()
251{
7bdc1879
VS
252 if ( !wxAppBase::OnInitGui() )
253 return FALSE;
254
2ec3892d
VS
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
7bdc1879
VS
261 return TRUE;
262}
263
264bool wxApp::ProcessIdle()
265{
266 wxIdleEvent event;
267 event.SetEventObject(this);
268 ProcessEvent(event);
269
270 return event.MoreRequested();
271}
272
273void 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
297bool 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
313bool wxApp::SendIdleEvents(wxWindow* win)
314{
315 bool needMore = FALSE;
316
317 wxIdleEvent event;
318 event.SetEventObject(win);
319
320 win->GetEventHandler()->ProcessEvent(event);
321
7bdc1879
VS
322 if ( event.MoreRequested() )
323 needMore = TRUE;
7bdc1879
VS
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
337int wxApp::MainLoop()
338{
fd495ab3 339 int rt;
ef344ff8
VS
340 m_mainLoop = new wxEventLoop;
341
342 rt = m_mainLoop->Run();
343
344 delete m_mainLoop;
345 m_mainLoop = NULL;
fd495ab3 346 return rt;
7bdc1879
VS
347}
348
349void wxApp::ExitMainLoop()
350{
ef344ff8
VS
351 if ( m_mainLoop )
352 m_mainLoop->Exit(0);
7bdc1879
VS
353}
354
355bool wxApp::Initialized()
356{
bd73ba41 357 return (wxTopLevelWindows.GetCount() != 0);
7bdc1879
VS
358}
359
360bool wxApp::Pending()
361{
ef344ff8 362 return wxEventLoop::GetActive()->Pending();
7bdc1879
VS
363}
364
365void wxApp::Dispatch()
32b8ec41 366{
ef344ff8 367 wxEventLoop::GetActive()->Dispatch();
32b8ec41
VZ
368}
369
7bdc1879
VS
370void 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;
32b8ec41 381
7bdc1879
VS
382 node = wxPendingDelete.First();
383 }
384}
385
386bool wxApp::Initialize()
32b8ec41 387{
bd73ba41
VS
388 if ( MGL_init(".", NULL) == 0 )
389 return FALSE;
390
32b8ec41
VZ
391 wxBuffer = new wxChar[BUFSIZ + 512];
392
393 wxClassInfo::InitializeClasses();
7bdc1879 394
32b8ec41 395 wxSystemSettings::Init();
7bdc1879
VS
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);
32b8ec41 409 wxTheColourDatabase->Initialize();
ef344ff8
VS
410
411 // Can't do this in wxModule, because fonts are needed by stock lists
412 wxTheFontsManager = new wxFontsManager;
7bdc1879 413
32b8ec41
VZ
414 wxInitializeStockLists();
415 wxInitializeStockObjects();
7bdc1879
VS
416
417#if wxUSE_WX_RESOURCES
418 wxInitializeResourceSystem();
419#endif
420
32b8ec41
VZ
421 wxModule::RegisterModules();
422 if (!wxModule::InitializeModules()) return FALSE;
7bdc1879 423
32b8ec41
VZ
424 return TRUE;
425}
426
7bdc1879
VS
427wxIcon wxApp::GetStdIcon(int which) const
428{
df028524 429 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
7bdc1879
VS
430}
431
432void wxApp::CleanUp()
433{
58061670
VS
434 delete gs_rootWindow;
435
7bdc1879
VS
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();
7bdc1879
VS
462 wxDeleteStockLists();
463
ef344ff8
VS
464 // Can't do this in wxModule, because fonts are needed by stock lists
465 delete wxTheFontsManager;
466 wxTheFontsManager = (wxFontsManager*) NULL;
467
7bdc1879
VS
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
508int wxEntryStart(int argc, char *argv[])
509{
510 return wxApp::Initialize() ? 0 : -1;
511}
512
513
514int wxEntryInitGui()
515{
516 return wxTheApp->OnInitGui() ? 0 : -1;
517}
518
519
520void wxEntryCleanup()
521{
522 wxApp::CleanUp();
523}
524
525
526
527int 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
fd495ab3 592 if ( wxTheApp->Initialized() )
7bdc1879
VS
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}