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