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