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