]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
fixed location of mac resources
[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$
c41c20a5 6// Copyright: (c) 2001-2002 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"
05c9ccbe 37#include "wx/sysopt.h"
7bdc1879 38#include "wx/mgl/private.h"
32b8ec41
VZ
39
40//-----------------------------------------------------------------------------
505f0a85 41// wxApp::Exit()
32b8ec41
VZ
42//-----------------------------------------------------------------------------
43
e2478fde 44void wxApp::Exit()
32b8ec41 45{
7bdc1879 46 MGL_exit();
32b8ec41
VZ
47 exit(0);
48}
49
50//-----------------------------------------------------------------------------
51// wxYield
52//-----------------------------------------------------------------------------
53
7bdc1879
VS
54static bool gs_inYield = FALSE;
55
e1218bd6 56bool wxApp::Yield(bool onlyIfNeeded)
32b8ec41 57{
e1218bd6
VS
58 if ( gs_inYield )
59 {
60 if ( !onlyIfNeeded )
61 {
62 wxFAIL_MSG( wxT("wxYield called recursively" ) );
63 }
64
65 return FALSE;
66 }
67
7bdc1879
VS
68#if wxUSE_THREADS
69 if ( !wxThread::IsMain() )
70 {
71 // can't process events from other threads, MGL is thread-unsafe
72 return TRUE;
73 }
74#endif // wxUSE_THREADS
75
76 gs_inYield = TRUE;
77
78 wxLog::Suspend();
79
ef344ff8
VS
80 if ( wxEventLoop::GetActive() )
81 {
82 while (wxEventLoop::GetActive()->Pending())
83 wxEventLoop::GetActive()->Dispatch();
84 }
7c9955d1 85
7bdc1879
VS
86 /* it's necessary to call ProcessIdle() to update the frames sizes which
87 might have been changed (it also will update other things set from
88 OnUpdateUI() which is a nice (and desired) side effect) */
89 while (wxTheApp->ProcessIdle()) { }
90
91 wxLog::Resume();
92
93 gs_inYield = FALSE;
94
32b8ec41
VZ
95 return TRUE;
96}
97
7bdc1879 98
32b8ec41
VZ
99//-----------------------------------------------------------------------------
100// wxWakeUpIdle
101//-----------------------------------------------------------------------------
102
e2478fde 103void wxApp::WakeUpIdle()
32b8ec41 104{
7bdc1879
VS
105#if wxUSE_THREADS
106 if (!wxThread::IsMain())
107 wxMutexGuiEnter();
108#endif
109
e2478fde
VZ
110 while (wxTheApp->ProcessIdle())
111 ;
7bdc1879
VS
112
113#if wxUSE_THREADS
114 if (!wxThread::IsMain())
115 wxMutexGuiLeave();
116#endif
32b8ec41
VZ
117}
118
58061670
VS
119//-----------------------------------------------------------------------------
120// Root window
121//-----------------------------------------------------------------------------
122
123class wxRootWindow : public wxWindow
124{
125 public:
126 wxRootWindow() : wxWindow(NULL, -1)
127 {
128 SetMGLwindow_t(MGL_wmGetRootWindow(g_winMng));
129 SetBackgroundColour(wxTHEME_COLOUR(DESKTOP));
130 }
131 ~wxRootWindow()
132 {
133 // we don't want to delete MGL_WM's rootWnd
7c9955d1 134 m_wnd = NULL;
58061670
VS
135 }
136
b7d5acd0 137 virtual bool AcceptsFocus() const { return FALSE; }
7c9955d1 138
b7d5acd0 139 DECLARE_DYNAMIC_CLASS(wxRootWindow)
58061670
VS
140};
141
b7d5acd0
VS
142IMPLEMENT_DYNAMIC_CLASS(wxRootWindow, wxWindow)
143
58061670
VS
144static wxRootWindow *gs_rootWindow = NULL;
145
146//-----------------------------------------------------------------------------
147// MGL initialization
148//-----------------------------------------------------------------------------
149
634f6a1f 150static bool wxCreateMGL_WM(const wxDisplayModeInfo& displayMode)
58061670
VS
151{
152 int mode;
58061670 153 int refresh = MGL_DEFAULT_REFRESH;
7c9955d1 154
58061670 155#if wxUSE_SYSTEM_OPTIONS
05c9ccbe 156 if ( wxSystemOptions::HasOption(wxT("mgl.screen-refresh")) )
58061670
VS
157 refresh = wxSystemOptions::GetOptionInt(wxT("mgl.screen-refresh"));
158#endif
7c9955d1
JS
159
160 mode = MGL_findMode(displayMode.GetWidth(),
161 displayMode.GetHeight(),
634f6a1f 162 displayMode.GetDepth());
58061670
VS
163 if ( mode == -1 )
164 {
7c9955d1 165 wxLogError(_("Mode %ix%i-%i not available."),
d76048f5
VS
166 displayMode.GetWidth(),
167 displayMode.GetHeight(),
634f6a1f 168 displayMode.GetDepth());
1f43b5c9 169 return FALSE;
58061670
VS
170 }
171 g_displayDC = new MGLDisplayDC(mode, 1, refresh);
172 if ( !g_displayDC->isValid() )
173 {
174 delete g_displayDC;
175 g_displayDC = NULL;
176 return FALSE;
177 }
7c9955d1 178
58061670
VS
179 g_winMng = MGL_wmCreate(g_displayDC->getDC());
180 if (!g_winMng)
181 return FALSE;
1f43b5c9 182
58061670
VS
183 return TRUE;
184}
185
186static void wxDestroyMGL_WM()
187{
188 if ( g_winMng )
189 {
190 MGL_wmDestroy(g_winMng);
191 g_winMng = NULL;
192 }
193 if ( g_displayDC )
194 {
195 delete g_displayDC;
196 g_displayDC = NULL;
197 }
198}
199
32b8ec41
VZ
200//-----------------------------------------------------------------------------
201// wxApp
202//-----------------------------------------------------------------------------
203
204IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
205
206BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
207 EVT_IDLE(wxApp::OnIdle)
208END_EVENT_TABLE()
209
210
ef344ff8 211wxApp::wxApp() : m_mainLoop(NULL)
7bdc1879
VS
212{
213}
214
215wxApp::~wxApp()
216{
217}
218
a3e76614
VS
219wxDisplayModeInfo wxGetDefaultDisplayMode()
220{
221 wxString mode;
222 unsigned w, h, bpp;
223
7c9955d1 224 if ( !wxGetEnv(wxT("WXMODE"), &mode) ||
a3e76614
VS
225 (wxSscanf(mode.c_str(), _T("%ux%u-%u"), &w, &h, &bpp) != 3) )
226 {
227 w = 640, h = 480, bpp = 16;
228 }
229
230 return wxDisplayModeInfo(w, h, bpp);
231}
232
634f6a1f
VS
233bool wxApp::SetDisplayMode(const wxDisplayModeInfo& mode)
234{
235 if ( !mode.IsOk() )
236 {
237 return FALSE;
238 }
239 if ( g_displayDC != NULL )
240 {
241 // FIXME_MGL -- we currently don't allow to switch video mode
1f43b5c9 242 // more than once. This can hopefully be changed...
634f6a1f
VS
243 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
244 return FALSE;
245 }
1f43b5c9
VS
246
247 if ( !wxCreateMGL_WM(mode) )
248 return FALSE;
249 gs_rootWindow = new wxRootWindow;
250
634f6a1f 251 m_displayMode = mode;
1f43b5c9 252
634f6a1f
VS
253 return TRUE;
254}
255
7bdc1879
VS
256bool wxApp::OnInitGui()
257{
7bdc1879
VS
258 if ( !wxAppBase::OnInitGui() )
259 return FALSE;
260
d76048f5
VS
261#ifdef __WXDEBUG__
262 // MGL redirects stdout and stderr to physical console, so lets redirect
263 // it to file. Do it only when WXDEBUG environment variable is set
343e418c
VS
264 wxString redirect;
265 if ( wxGetEnv(wxT("WXSTDERR"), &redirect) )
266 freopen(redirect.mb_str(), "wt", stderr);
2ec3892d
VS
267#endif
268
d76048f5
VS
269 wxLog *oldLog = wxLog::SetActiveTarget(new wxLogGui);
270 if ( oldLog ) delete oldLog;
271
7bdc1879
VS
272 return TRUE;
273}
274
275bool wxApp::ProcessIdle()
276{
277 wxIdleEvent event;
278 event.SetEventObject(this);
279 ProcessEvent(event);
280
281 return event.MoreRequested();
282}
283
284void wxApp::OnIdle(wxIdleEvent &event)
285{
286 static bool s_inOnIdle = FALSE;
287
288 /* Avoid recursion (via ProcessEvent default case) */
289 if (s_inOnIdle)
290 return;
291
292 s_inOnIdle = TRUE;
293
294 /* Resend in the main thread events which have been prepared in other
295 threads */
296 ProcessPendingEvents();
297
298 // 'Garbage' collection of windows deleted with Close().
299 DeletePendingObjects();
300
d76048f5
VS
301#if wxUSE_LOG
302 // flush the logged messages if any
303 wxLog::FlushActive();
304#endif // wxUSE_LOG
305
7bdc1879
VS
306 // Send OnIdle events to all windows
307 if ( SendIdleEvents() )
308 event.RequestMore(TRUE);
309
310 s_inOnIdle = FALSE;
311}
312
313bool wxApp::SendIdleEvents()
314{
315 bool needMore = FALSE;
316
317 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
318 while (node)
319 {
320 wxWindow* win = node->GetData();
321 if ( SendIdleEvents(win) )
322 needMore = TRUE;
323 node = node->GetNext();
324 }
325
326 return needMore;
327}
328
329bool wxApp::SendIdleEvents(wxWindow* win)
330{
331 bool needMore = FALSE;
332
333 wxIdleEvent event;
334 event.SetEventObject(win);
335
336 win->GetEventHandler()->ProcessEvent(event);
337
7bdc1879
VS
338 if ( event.MoreRequested() )
339 needMore = TRUE;
7bdc1879
VS
340
341 wxNode* node = win->GetChildren().First();
342 while (node)
343 {
344 wxWindow* win = (wxWindow*) node->Data();
345 if ( SendIdleEvents(win) )
346 needMore = TRUE;
347
348 node = node->Next();
349 }
350 return needMore;
351}
352
353int wxApp::MainLoop()
354{
fd495ab3 355 int rt;
ef344ff8
VS
356 m_mainLoop = new wxEventLoop;
357
358 rt = m_mainLoop->Run();
359
360 delete m_mainLoop;
361 m_mainLoop = NULL;
fd495ab3 362 return rt;
7bdc1879
VS
363}
364
365void wxApp::ExitMainLoop()
366{
ef344ff8
VS
367 if ( m_mainLoop )
368 m_mainLoop->Exit(0);
7bdc1879
VS
369}
370
371bool wxApp::Initialized()
372{
bd73ba41 373 return (wxTopLevelWindows.GetCount() != 0);
7bdc1879
VS
374}
375
376bool wxApp::Pending()
377{
ef344ff8 378 return wxEventLoop::GetActive()->Pending();
7bdc1879
VS
379}
380
381void wxApp::Dispatch()
32b8ec41 382{
ef344ff8 383 wxEventLoop::GetActive()->Dispatch();
32b8ec41
VZ
384}
385
94826170 386bool wxApp::Initialize(int argc, wxChar **argv)
7bdc1879 387{
94826170
VZ
388 // must do it before calling wxAppBase::Initialize(), because fonts are
389 // needed by stock lists which are created there
390 wxTheFontsManager = new wxFontsManager;
32b8ec41 391
94826170
VZ
392 if ( !wxAppBase::Initialize(argc, argv) )
393 return false;
7bdc1879 394
bd73ba41 395 if ( MGL_init(".", NULL) == 0 )
ddfca47f
VS
396 {
397 wxLogError(_("Cannot initialize SciTech MGL!"));
bd73ba41 398
94826170
VZ
399 wxAppBase::CleanUp();
400
401 return false;
402 }
7bdc1879 403
7bdc1879
VS
404#if wxUSE_INTL
405 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
406#endif
407
94826170 408 return true;
32b8ec41
VZ
409}
410
7bdc1879
VS
411void wxApp::CleanUp()
412{
d76048f5
VS
413 delete gs_rootWindow;
414
94826170 415 wxAppBase::CleanUp();
7bdc1879 416
94826170 417 // must do this after calling base class CleanUp()
df16a53e
VS
418 delete wxTheFontsManager;
419 wxTheFontsManager = (wxFontsManager*) NULL;
420
7bdc1879
VS
421 wxDestroyMGL_WM();
422 MGL_exit();
423}
424
425
426int wxEntryStart(int argc, char *argv[])
427{
428 return wxApp::Initialize() ? 0 : -1;
429}
430
431
432int wxEntryInitGui()
433{
434 return wxTheApp->OnInitGui() ? 0 : -1;
435}
436
437
438void wxEntryCleanup()
439{
440 wxApp::CleanUp();
441}
442
443
444
445int wxEntry(int argc, char *argv[])
446{
6ed89758
VS
447#ifdef __DJGPP__
448 // VS: disable long filenames under DJGPP as the very first thing,
449 // since SciTech MGL doesn't like them much...
450 wxSetEnv(wxT("LFN"), wxT("N"));
451#endif
452
7bdc1879
VS
453#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
454 // This seems to be necessary since there are 'rogue'
455 // objects present at this point (perhaps global objects?)
456 // Setting a checkpoint will ignore them as far as the
457 // memory checking facility is concerned.
458 // Of course you may argue that memory allocated in globals should be
459 // checked, but this is a reasonable compromise.
460 wxDebugContext::SetCheckpoint();
461#endif
462 int err = wxEntryStart(argc, argv);
463 if ( err )
464 return err;
465
466 if ( !wxTheApp )
467 {
468 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
469 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
470
471 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
472
473 wxObject *test_app = app_ini();
474
475 wxTheApp = (wxApp*) test_app;
476 }
477
478 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
479
480 wxTheApp->argc = argc;
481#if wxUSE_UNICODE
482 wxTheApp->argv = new wxChar*[argc+1];
483 int mb_argc = 0;
484 while (mb_argc < argc)
485 {
486 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
487 mb_argc++;
488 }
489 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
490#else
491 wxTheApp->argv = argv;
492#endif
493
494 wxString name(wxFileNameFromPath(argv[0]));
495 wxStripExtension(name);
496 wxTheApp->SetAppName(name);
497
498 int retValue;
499 retValue = wxEntryInitGui();
500
501 // Here frames insert themselves automatically into wxTopLevelWindows by
502 // getting created in OnInit().
503 if ( retValue == 0 )
504 {
505 if ( !wxTheApp->OnInit() )
506 retValue = -1;
507 }
508
509 if ( retValue == 0 )
510 {
511 /* delete pending toplevel windows (typically a single
512 dialog) so that, if there isn't any left, we don't
513 call OnRun() */
514 wxTheApp->DeletePendingObjects();
515
fd495ab3 516 if ( wxTheApp->Initialized() )
7bdc1879
VS
517 {
518 wxTheApp->OnRun();
519
520 wxWindow *topWindow = wxTheApp->GetTopWindow();
521 if ( topWindow )
522 {
523 /* Forcibly delete the window. */
524 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
525 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
526 {
527 topWindow->Close(TRUE);
528 wxTheApp->DeletePendingObjects();
529 }
530 else
531 {
532 delete topWindow;
533 wxTheApp->SetTopWindow((wxWindow*) NULL);
534 }
535 }
536
d76048f5
VS
537#if wxUSE_LOG
538 // flush the logged messages if any
539 wxLog *log = wxLog::GetActiveTarget();
540 if (log != NULL && log->HasPendingMessages())
541 log->Flush();
542#endif // wxUSE_LOG
7bdc1879
VS
543 retValue = wxTheApp->OnExit();
544 }
545 }
546
547 wxEntryCleanup();
548
549 return retValue;
550}