]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/app.cpp
implemented late(r) initialization of wxUniv themes
[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 {
634f6a1f
VS
173 wxLogWarning(_("Mode %ix%i-%i not available, falling back to default mode."),
174 displayMode.GetScreenSize().x,
175 displayMode.GetScreenSize().y,
176 displayMode.GetDepth());
58061670
VS
177 mode = 0; // always available
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;
190
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 220{
634f6a1f 221 m_displayMode = wxDisplayModeInfo(wxSize(640, 480), 16);
7bdc1879
VS
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
237 // at runtime. This can hopefully be changed...
238 wxFAIL_MSG(wxT("Can't change display mode after intialization!"));
239 return FALSE;
240 }
241 m_displayMode = mode;
242 return TRUE;
243}
244
7bdc1879
VS
245bool wxApp::OnInitGui()
246{
634f6a1f 247 if ( !wxCreateMGL_WM(m_displayMode) )
7bdc1879
VS
248 return FALSE;
249
250 // This has to be done *after* wxCreateMGL_WM() because it initializes
251 // wxUniv's themes
252 if ( !wxAppBase::OnInitGui() )
253 return FALSE;
58061670
VS
254
255 // ...and this has to be done after wxUniv themes were initialized
256 gs_rootWindow = new wxRootWindow;
7bdc1879 257
2ec3892d
VS
258#ifdef MGL_DEBUG
259 // That damn MGL redirects stdin and stdout to physical console
260 FILE *file = fopen("stderr", "wt");
261 wxLog::SetActiveTarget(new wxLogStderr(file));
262#endif
263
7bdc1879
VS
264 return TRUE;
265}
266
267bool wxApp::ProcessIdle()
268{
269 wxIdleEvent event;
270 event.SetEventObject(this);
271 ProcessEvent(event);
272
273 return event.MoreRequested();
274}
275
276void wxApp::OnIdle(wxIdleEvent &event)
277{
278 static bool s_inOnIdle = FALSE;
279
280 /* Avoid recursion (via ProcessEvent default case) */
281 if (s_inOnIdle)
282 return;
283
284 s_inOnIdle = TRUE;
285
286 /* Resend in the main thread events which have been prepared in other
287 threads */
288 ProcessPendingEvents();
289
290 // 'Garbage' collection of windows deleted with Close().
291 DeletePendingObjects();
292
293 // Send OnIdle events to all windows
294 if ( SendIdleEvents() )
295 event.RequestMore(TRUE);
296
297 s_inOnIdle = FALSE;
298}
299
300bool wxApp::SendIdleEvents()
301{
302 bool needMore = FALSE;
303
304 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
305 while (node)
306 {
307 wxWindow* win = node->GetData();
308 if ( SendIdleEvents(win) )
309 needMore = TRUE;
310 node = node->GetNext();
311 }
312
313 return needMore;
314}
315
316bool wxApp::SendIdleEvents(wxWindow* win)
317{
318 bool needMore = FALSE;
319
320 wxIdleEvent event;
321 event.SetEventObject(win);
322
323 win->GetEventHandler()->ProcessEvent(event);
324
7bdc1879
VS
325 if ( event.MoreRequested() )
326 needMore = TRUE;
7bdc1879
VS
327
328 wxNode* node = win->GetChildren().First();
329 while (node)
330 {
331 wxWindow* win = (wxWindow*) node->Data();
332 if ( SendIdleEvents(win) )
333 needMore = TRUE;
334
335 node = node->Next();
336 }
337 return needMore;
338}
339
340int wxApp::MainLoop()
341{
fd495ab3 342 int rt;
ef344ff8
VS
343 m_mainLoop = new wxEventLoop;
344
345 rt = m_mainLoop->Run();
346
347 delete m_mainLoop;
348 m_mainLoop = NULL;
fd495ab3 349 return rt;
7bdc1879
VS
350}
351
352void wxApp::ExitMainLoop()
353{
ef344ff8
VS
354 if ( m_mainLoop )
355 m_mainLoop->Exit(0);
7bdc1879
VS
356}
357
358bool wxApp::Initialized()
359{
bd73ba41 360 return (wxTopLevelWindows.GetCount() != 0);
7bdc1879
VS
361}
362
363bool wxApp::Pending()
364{
ef344ff8 365 return wxEventLoop::GetActive()->Pending();
7bdc1879
VS
366}
367
368void wxApp::Dispatch()
32b8ec41 369{
ef344ff8 370 wxEventLoop::GetActive()->Dispatch();
32b8ec41
VZ
371}
372
7bdc1879
VS
373void wxApp::DeletePendingObjects()
374{
375 wxNode *node = wxPendingDelete.First();
376 while (node)
377 {
378 wxObject *obj = (wxObject *)node->Data();
379
380 delete obj;
381
382 if ( wxPendingDelete.Find(obj) )
383 delete node;
32b8ec41 384
7bdc1879
VS
385 node = wxPendingDelete.First();
386 }
387}
388
389bool wxApp::Initialize()
32b8ec41 390{
bd73ba41
VS
391 if ( MGL_init(".", NULL) == 0 )
392 return FALSE;
393
32b8ec41
VZ
394 wxBuffer = new wxChar[BUFSIZ + 512];
395
396 wxClassInfo::InitializeClasses();
7bdc1879 397
32b8ec41 398 wxSystemSettings::Init();
7bdc1879
VS
399
400#if wxUSE_INTL
401 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
402#endif
403
404 // GL: I'm annoyed ... I don't know where to put this and I don't want to
405 // create a module for that as it's part of the core.
406#if wxUSE_THREADS
407 wxPendingEvents = new wxList;
408 wxPendingEventsLocker = new wxCriticalSection;
409#endif
410
411 wxTheColourDatabase = new wxColourDatabase(wxKEY_STRING);
32b8ec41 412 wxTheColourDatabase->Initialize();
ef344ff8
VS
413
414 // Can't do this in wxModule, because fonts are needed by stock lists
415 wxTheFontsManager = new wxFontsManager;
7bdc1879 416
32b8ec41
VZ
417 wxInitializeStockLists();
418 wxInitializeStockObjects();
7bdc1879
VS
419
420#if wxUSE_WX_RESOURCES
421 wxInitializeResourceSystem();
422#endif
423
32b8ec41
VZ
424 wxModule::RegisterModules();
425 if (!wxModule::InitializeModules()) return FALSE;
7bdc1879 426
32b8ec41
VZ
427 return TRUE;
428}
429
7bdc1879
VS
430wxIcon wxApp::GetStdIcon(int which) const
431{
df028524 432 return wxTheme::Get()->GetRenderer()->GetStdIcon(which);
7bdc1879
VS
433}
434
435void wxApp::CleanUp()
436{
58061670
VS
437 delete gs_rootWindow;
438
7bdc1879
VS
439#if wxUSE_LOG
440 // flush the logged messages if any
441 wxLog *log = wxLog::GetActiveTarget();
442 if (log != NULL && log->HasPendingMessages())
443 log->Flush();
444
445 // continuing to use user defined log target is unsafe from now on because
446 // some resources may be already unavailable, so replace it by something
447 // more safe
448 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
449 if ( oldlog )
450 delete oldlog;
451#endif // wxUSE_LOG
452
453 wxModule::CleanUpModules();
454
455#if wxUSE_WX_RESOURCES
456 wxCleanUpResourceSystem();
457#endif
458
459 if (wxTheColourDatabase)
460 delete wxTheColourDatabase;
461
462 wxTheColourDatabase = (wxColourDatabase*) NULL;
463
464 wxDeleteStockObjects();
7bdc1879
VS
465 wxDeleteStockLists();
466
ef344ff8
VS
467 // Can't do this in wxModule, because fonts are needed by stock lists
468 delete wxTheFontsManager;
469 wxTheFontsManager = (wxFontsManager*) NULL;
470
7bdc1879
VS
471 delete wxTheApp;
472 wxTheApp = (wxApp*) NULL;
473
474 // GL: I'm annoyed ... I don't know where to put this and I don't want to
475 // create a module for that as it's part of the core.
476#if wxUSE_THREADS
477 delete wxPendingEvents;
478 delete wxPendingEventsLocker;
479#endif
480
481 wxSystemSettings::Done();
482
483 delete[] wxBuffer;
484
485 wxClassInfo::CleanUpClasses();
486
487 // check for memory leaks
488#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
489 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
490 {
491 wxLogDebug(wxT("There were memory leaks.\n"));
492 wxDebugContext::Dump();
493 wxDebugContext::PrintStatistics();
494 }
495#endif // Debug
496
497#if wxUSE_LOG
498 // do this as the very last thing because everything else can log messages
499 wxLog::DontCreateOnDemand();
500
501 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
502 if (oldLog)
503 delete oldLog;
504#endif // wxUSE_LOG
505
506 wxDestroyMGL_WM();
507 MGL_exit();
508}
509
510
511int wxEntryStart(int argc, char *argv[])
512{
513 return wxApp::Initialize() ? 0 : -1;
514}
515
516
517int wxEntryInitGui()
518{
519 return wxTheApp->OnInitGui() ? 0 : -1;
520}
521
522
523void wxEntryCleanup()
524{
525 wxApp::CleanUp();
526}
527
528
529
530int wxEntry(int argc, char *argv[])
531{
532#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
533 // This seems to be necessary since there are 'rogue'
534 // objects present at this point (perhaps global objects?)
535 // Setting a checkpoint will ignore them as far as the
536 // memory checking facility is concerned.
537 // Of course you may argue that memory allocated in globals should be
538 // checked, but this is a reasonable compromise.
539 wxDebugContext::SetCheckpoint();
540#endif
541 int err = wxEntryStart(argc, argv);
542 if ( err )
543 return err;
544
545 if ( !wxTheApp )
546 {
547 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
548 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
549
550 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
551
552 wxObject *test_app = app_ini();
553
554 wxTheApp = (wxApp*) test_app;
555 }
556
557 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
558
559 wxTheApp->argc = argc;
560#if wxUSE_UNICODE
561 wxTheApp->argv = new wxChar*[argc+1];
562 int mb_argc = 0;
563 while (mb_argc < argc)
564 {
565 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
566 mb_argc++;
567 }
568 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
569#else
570 wxTheApp->argv = argv;
571#endif
572
573 wxString name(wxFileNameFromPath(argv[0]));
574 wxStripExtension(name);
575 wxTheApp->SetAppName(name);
576
577 int retValue;
578 retValue = wxEntryInitGui();
579
580 // Here frames insert themselves automatically into wxTopLevelWindows by
581 // getting created in OnInit().
582 if ( retValue == 0 )
583 {
584 if ( !wxTheApp->OnInit() )
585 retValue = -1;
586 }
587
588 if ( retValue == 0 )
589 {
590 /* delete pending toplevel windows (typically a single
591 dialog) so that, if there isn't any left, we don't
592 call OnRun() */
593 wxTheApp->DeletePendingObjects();
594
fd495ab3 595 if ( wxTheApp->Initialized() )
7bdc1879
VS
596 {
597 wxTheApp->OnRun();
598
599 wxWindow *topWindow = wxTheApp->GetTopWindow();
600 if ( topWindow )
601 {
602 /* Forcibly delete the window. */
603 if (topWindow->IsKindOf(CLASSINFO(wxFrame)) ||
604 topWindow->IsKindOf(CLASSINFO(wxDialog)) )
605 {
606 topWindow->Close(TRUE);
607 wxTheApp->DeletePendingObjects();
608 }
609 else
610 {
611 delete topWindow;
612 wxTheApp->SetTopWindow((wxWindow*) NULL);
613 }
614 }
615
616 retValue = wxTheApp->OnExit();
617 }
618 }
619
620 wxEntryCleanup();
621
622 return retValue;
623}