]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk1/app.cpp
corrected warnings when compiling with -Wall -W
[wxWidgets.git] / src / gtk1 / app.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: app.cpp
3// Purpose:
4// Author: Robert Roebling
5// Id: $Id$
6// Copyright: (c) 1998 Robert Roebling, Julian Smart
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10#ifdef __GNUG__
11 #pragma implementation "app.h"
12#endif
13
14#ifdef __VMS
15#include <vms_jackets.h>
16#endif
17
18#include "wx/app.h"
19#include "wx/gdicmn.h"
20#include "wx/utils.h"
21#include "wx/intl.h"
22#include "wx/log.h"
23#include "wx/memory.h"
24#include "wx/font.h"
25#include "wx/settings.h"
26#include "wx/dialog.h"
27
28#if wxUSE_WX_RESOURCES
29 #include "wx/resource.h"
30#endif
31
32#include "wx/module.h"
33#include "wx/image.h"
34
35#ifdef __WXUNIVERSAL__
36 #include "wx/univ/theme.h"
37 #include "wx/univ/renderer.h"
38#endif
39
40#if wxUSE_THREADS
41 #include "wx/thread.h"
42#endif
43
44#include <unistd.h>
45#if defined(__DARWIN__)
46// FIXME: select must be used instead of poll (GD)
47#elif defined(__VMS)
48# include <poll.h>
49#else
50# include <sys/poll.h>
51#endif
52#include "wx/gtk/win_gtk.h"
53
54#include <gtk/gtk.h>
55
56
57//-----------------------------------------------------------------------------
58// global data
59//-----------------------------------------------------------------------------
60
61wxApp *wxTheApp = (wxApp *) NULL;
62wxAppInitializerFunction wxAppBase::m_appInitFn = (wxAppInitializerFunction) NULL;
63
64bool g_mainThreadLocked = FALSE;
65gint g_pendingTag = 0;
66
67static GtkWidget *gs_RootWindow = (GtkWidget*) NULL;
68
69//-----------------------------------------------------------------------------
70// idle system
71//-----------------------------------------------------------------------------
72
73extern bool g_isIdle;
74
75void wxapp_install_idle_handler();
76
77//-----------------------------------------------------------------------------
78// wxExit
79//-----------------------------------------------------------------------------
80
81void wxExit()
82{
83 gtk_main_quit();
84}
85
86//-----------------------------------------------------------------------------
87// wxYield
88//-----------------------------------------------------------------------------
89
90bool wxApp::Yield(bool onlyIfNeeded)
91{
92 // MT-FIXME
93 static bool s_inYield = FALSE;
94
95 if ( s_inYield )
96 {
97 if ( !onlyIfNeeded )
98 {
99 wxFAIL_MSG( wxT("wxYield called recursively" ) );
100 }
101
102 return FALSE;
103 }
104
105#if wxUSE_THREADS
106 if ( !wxThread::IsMain() )
107 {
108 // can't call gtk_main_iteration() from other threads like this
109 return TRUE;
110 }
111#endif // wxUSE_THREADS
112
113 s_inYield = TRUE;
114
115 if (!g_isIdle)
116 {
117 // We need to remove idle callbacks or the loop will
118 // never finish.
119 gtk_idle_remove( m_idleTag );
120 m_idleTag = 0;
121 g_isIdle = TRUE;
122 }
123
124 // disable log flushing from here because a call to wxYield() shouldn't
125 // normally result in message boxes popping up &c
126 wxLog::Suspend();
127
128 while (gtk_events_pending())
129 gtk_main_iteration();
130
131 // It's necessary to call ProcessIdle() to update the frames sizes which
132 // might have been changed (it also will update other things set from
133 // OnUpdateUI() which is a nice (and desired) side effect). But we
134 // call ProcessIdle() only once since this is not meant for longish
135 // background jobs (controlled by wxIdleEvent::RequestMore() and the
136 // return value of Processidle().
137 ProcessIdle();
138
139 // let the logs be flashed again
140 wxLog::Resume();
141
142 s_inYield = FALSE;
143
144 return TRUE;
145}
146
147//-----------------------------------------------------------------------------
148// wxWakeUpIdle
149//-----------------------------------------------------------------------------
150
151void wxWakeUpIdle()
152{
153#if wxUSE_THREADS
154 if (!wxThread::IsMain())
155 wxMutexGuiEnter();
156#endif
157
158 if (g_isIdle)
159 wxapp_install_idle_handler();
160
161#if wxUSE_THREADS
162 if (!wxThread::IsMain())
163 wxMutexGuiLeave();
164#endif
165}
166
167//-----------------------------------------------------------------------------
168// local functions
169//-----------------------------------------------------------------------------
170
171// the callback functions must be extern "C" to comply with GTK+ declarations
172extern "C"
173{
174
175static gint wxapp_pending_callback( gpointer WXUNUSED(data) )
176{
177 if (!wxTheApp) return TRUE;
178
179 // When getting called from GDK's time-out handler
180 // we are no longer within GDK's grab on the GUI
181 // thread so we must lock it here ourselves.
182 gdk_threads_enter();
183
184 // Sent idle event to all who request them.
185 wxTheApp->ProcessPendingEvents();
186
187 g_pendingTag = 0;
188
189 // Flush the logged messages if any.
190#if wxUSE_LOG
191 wxLog::FlushActive();
192#endif // wxUSE_LOG
193
194 // Release lock again
195 gdk_threads_leave();
196
197 // Return FALSE to indicate that no more idle events are
198 // to be sent (single shot instead of continuous stream)
199 return FALSE;
200}
201
202static gint wxapp_idle_callback( gpointer WXUNUSED(data) )
203{
204 if (!wxTheApp)
205 return TRUE;
206
207#ifdef __WXDEBUG__
208 // don't generate the idle events while the assert modal dialog is shown,
209 // this completely confuses the apps which don't expect to be reentered
210 // from some safely-looking functions
211 if ( wxTheApp->IsInAssert() )
212 {
213 return TRUE;
214 }
215#endif // __WXDEBUG__
216
217 // When getting called from GDK's time-out handler
218 // we are no longer within GDK's grab on the GUI
219 // thread so we must lock it here ourselves.
220 gdk_threads_enter();
221
222 // Indicate that we are now in idle mode and event handlers
223 // will have to reinstall the idle handler again.
224 g_isIdle = TRUE;
225 wxTheApp->m_idleTag = 0;
226
227 // Send idle event to all who request them as long as
228 // no events have popped up in the event queue.
229 while (wxTheApp->ProcessIdle() && (gtk_events_pending() == 0))
230 ;
231
232 // Release lock again
233 gdk_threads_leave();
234
235 // Return FALSE to indicate that no more idle events are
236 // to be sent (single shot instead of continuous stream).
237 return FALSE;
238}
239
240#if wxUSE_THREADS
241
242static gint wxapp_poll_func( GPollFD *ufds, guint nfds, gint timeout )
243{
244 gint res;
245 gdk_threads_enter();
246
247 wxMutexGuiLeave();
248 g_mainThreadLocked = TRUE;
249
250#ifdef __DARWIN__
251 // FIXME: poll is not available under Darwin/Mac OS X and this needs
252 // to be implemented using select instead (GD)
253 // what about other BSD derived systems?
254 res = -1;
255#else
256 res = poll( (struct pollfd*) ufds, nfds, timeout );
257#endif
258
259 wxMutexGuiEnter();
260 g_mainThreadLocked = FALSE;
261
262 gdk_threads_leave();
263
264 return res;
265}
266
267#endif // wxUSE_THREADS
268
269} // extern "C"
270
271void wxapp_install_idle_handler()
272{
273 wxASSERT_MSG( wxTheApp->m_idleTag == 0, wxT("attempt to install idle handler twice") );
274
275 g_isIdle = FALSE;
276
277 if (g_pendingTag == 0)
278 g_pendingTag = gtk_idle_add_priority( 900, wxapp_pending_callback, (gpointer) NULL );
279
280 // This routine gets called by all event handlers
281 // indicating that the idle is over. It may also
282 // get called from other thread for sending events
283 // to the main thread (and processing these in
284 // idle time). Very low priority.
285 wxTheApp->m_idleTag = gtk_idle_add_priority( 1000, wxapp_idle_callback, (gpointer) NULL );
286}
287
288//-----------------------------------------------------------------------------
289// Access to the root window global
290//-----------------------------------------------------------------------------
291
292GtkWidget* wxGetRootWindow()
293{
294 if (gs_RootWindow == NULL)
295 {
296 gs_RootWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );
297 gtk_widget_realize( gs_RootWindow );
298 }
299 return gs_RootWindow;
300}
301
302//-----------------------------------------------------------------------------
303// wxApp
304//-----------------------------------------------------------------------------
305
306IMPLEMENT_DYNAMIC_CLASS(wxApp,wxEvtHandler)
307
308BEGIN_EVENT_TABLE(wxApp, wxEvtHandler)
309 EVT_IDLE(wxApp::OnIdle)
310END_EVENT_TABLE()
311
312wxApp::wxApp()
313{
314 m_initialized = FALSE;
315#ifdef __WXDEBUG__
316 m_isInAssert = FALSE;
317#endif // __WXDEBUG__
318
319 m_idleTag = 0;
320 wxapp_install_idle_handler();
321
322#if wxUSE_THREADS
323 g_main_set_poll_func( wxapp_poll_func );
324#endif
325
326 m_colorCube = (unsigned char*) NULL;
327
328 // this is NULL for a "regular" wxApp, but is set (and freed) by a wxGLApp
329 m_glVisualInfo = (void *) NULL;
330}
331
332wxApp::~wxApp()
333{
334 if (m_idleTag) gtk_idle_remove( m_idleTag );
335
336 if (m_colorCube) free(m_colorCube);
337}
338
339bool wxApp::OnInitGui()
340{
341 if ( !wxAppBase::OnInitGui() )
342 return FALSE;
343
344 GdkVisual *visual = gdk_visual_get_system();
345
346 // if this is a wxGLApp (derived from wxApp), and we've already
347 // chosen a specific visual, then derive the GdkVisual from that
348 if (m_glVisualInfo != NULL)
349 {
350#ifdef __WXGTK20__
351 // seems gtk_widget_set_default_visual no longer exists?
352 GdkVisual* vis = gtk_widget_get_default_visual();
353#else
354 GdkVisual* vis = gdkx_visual_get(
355 ((XVisualInfo *) m_glVisualInfo) ->visualid );
356 gtk_widget_set_default_visual( vis );
357#endif
358
359 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
360 gtk_widget_set_default_colormap( colormap );
361
362 visual = vis;
363 }
364
365 // On some machines, the default visual is just 256 colours, so
366 // we make sure we get the best. This can sometimes be wasteful.
367
368 else
369 if ((gdk_visual_get_best() != gdk_visual_get_system()) && (m_useBestVisual))
370 {
371#ifdef __WXGTK20__
372 /* seems gtk_widget_set_default_visual no longer exists? */
373 GdkVisual* vis = gtk_widget_get_default_visual();
374#else
375 GdkVisual* vis = gdk_visual_get_best();
376 gtk_widget_set_default_visual( vis );
377#endif
378
379 GdkColormap *colormap = gdk_colormap_new( vis, FALSE );
380 gtk_widget_set_default_colormap( colormap );
381
382 visual = vis;
383 }
384
385 // Nothing to do for 15, 16, 24, 32 bit displays
386 if (visual->depth > 8) return TRUE;
387
388 // initialize color cube for 8-bit color reduction dithering
389
390 GdkColormap *cmap = gtk_widget_get_default_colormap();
391
392 m_colorCube = (unsigned char*)malloc(32 * 32 * 32);
393
394 for (int r = 0; r < 32; r++)
395 {
396 for (int g = 0; g < 32; g++)
397 {
398 for (int b = 0; b < 32; b++)
399 {
400 int rr = (r << 3) | (r >> 2);
401 int gg = (g << 3) | (g >> 2);
402 int bb = (b << 3) | (b >> 2);
403
404 int index = -1;
405
406 GdkColor *colors = cmap->colors;
407 if (colors)
408 {
409 int max = 3 * 65536;
410
411 for (int i = 0; i < cmap->size; i++)
412 {
413 int rdiff = ((rr << 8) - colors[i].red);
414 int gdiff = ((gg << 8) - colors[i].green);
415 int bdiff = ((bb << 8) - colors[i].blue);
416 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
417 if (sum < max)
418 {
419 index = i; max = sum;
420 }
421 }
422 }
423 else
424 {
425 // assume 8-bit true or static colors. this really exists
426 GdkVisual* vis = gdk_colormap_get_visual( cmap );
427 index = (r >> (5 - vis->red_prec)) << vis->red_shift;
428 index |= (g >> (5 - vis->green_prec)) << vis->green_shift;
429 index |= (b >> (5 - vis->blue_prec)) << vis->blue_shift;
430 }
431 m_colorCube[ (r*1024) + (g*32) + b ] = index;
432 }
433 }
434 }
435
436 return TRUE;
437}
438
439GdkVisual *wxApp::GetGdkVisual()
440{
441 GdkVisual *visual = NULL;
442
443 if (m_glVisualInfo)
444 visual = gdkx_visual_get( ((XVisualInfo *) m_glVisualInfo)->visualid );
445 else
446 visual = gdk_window_get_visual( wxGetRootWindow()->window );
447
448 wxASSERT( visual );
449
450 return visual;
451}
452
453bool wxApp::ProcessIdle()
454{
455 wxIdleEvent event;
456 event.SetEventObject( this );
457 ProcessEvent( event );
458
459 return event.MoreRequested();
460}
461
462void wxApp::OnIdle( wxIdleEvent &event )
463{
464 static bool s_inOnIdle = FALSE;
465
466 // Avoid recursion (via ProcessEvent default case)
467 if (s_inOnIdle)
468 return;
469
470 s_inOnIdle = TRUE;
471
472 // Resend in the main thread events which have been prepared in other
473 // threads
474 ProcessPendingEvents();
475
476 // 'Garbage' collection of windows deleted with Close()
477 DeletePendingObjects();
478
479 // Send OnIdle events to all windows
480 bool needMore = SendIdleEvents();
481
482 if (needMore)
483 event.RequestMore(TRUE);
484
485 s_inOnIdle = FALSE;
486}
487
488bool wxApp::SendIdleEvents()
489{
490 bool needMore = FALSE;
491
492 wxWindowList::Node* node = wxTopLevelWindows.GetFirst();
493 while (node)
494 {
495 wxWindow* win = node->GetData();
496 if (SendIdleEvents(win))
497 needMore = TRUE;
498
499 node = node->GetNext();
500 }
501
502 node = wxTopLevelWindows.GetFirst();
503 while (node)
504 {
505 wxWindow* win = node->GetData();
506 CallInternalIdle( win );
507
508 node = node->GetNext();
509 }
510 return needMore;
511}
512
513bool wxApp::CallInternalIdle( wxWindow* win )
514{
515 win->OnInternalIdle();
516
517 wxNode* node = win->GetChildren().First();
518 while (node)
519 {
520 wxWindow* win = (wxWindow*) node->Data();
521 CallInternalIdle( win );
522
523 node = node->Next();
524 }
525
526 return TRUE;
527}
528
529bool wxApp::SendIdleEvents( wxWindow* win )
530{
531 bool needMore = FALSE;
532
533 wxIdleEvent event;
534 event.SetEventObject(win);
535
536 win->GetEventHandler()->ProcessEvent(event);
537
538 if (event.MoreRequested())
539 needMore = TRUE;
540
541 wxNode* node = win->GetChildren().First();
542 while (node)
543 {
544 wxWindow* win = (wxWindow*) node->Data();
545 if (SendIdleEvents(win))
546 needMore = TRUE;
547
548 node = node->Next();
549 }
550
551 return needMore;
552}
553
554int wxApp::MainLoop()
555{
556 gtk_main();
557 return 0;
558}
559
560void wxApp::ExitMainLoop()
561{
562 if (gtk_main_level() > 0)
563 gtk_main_quit();
564}
565
566bool wxApp::Initialized()
567{
568 return m_initialized;
569}
570
571bool wxApp::Pending()
572{
573 return (gtk_events_pending() > 0);
574}
575
576void wxApp::Dispatch()
577{
578 gtk_main_iteration();
579}
580
581void wxApp::DeletePendingObjects()
582{
583 wxNode *node = wxPendingDelete.First();
584 while (node)
585 {
586 wxObject *obj = (wxObject *)node->Data();
587
588 delete obj;
589
590 if (wxPendingDelete.Find(obj))
591 delete node;
592
593 node = wxPendingDelete.First();
594 }
595}
596
597bool wxApp::Initialize()
598{
599 wxClassInfo::InitializeClasses();
600
601#if wxUSE_INTL
602 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
603#endif
604
605 // GL: I'm annoyed ... I don't know where to put this and I don't want to
606 // create a module for that as it's part of the core.
607#if wxUSE_THREADS
608 wxPendingEvents = new wxList();
609 wxPendingEventsLocker = new wxCriticalSection();
610#endif
611
612 wxTheColourDatabase = new wxColourDatabase( wxKEY_STRING );
613 wxTheColourDatabase->Initialize();
614
615 wxInitializeStockLists();
616 wxInitializeStockObjects();
617
618#if wxUSE_WX_RESOURCES
619 wxInitializeResourceSystem();
620#endif
621
622 wxModule::RegisterModules();
623 if (!wxModule::InitializeModules()) return FALSE;
624
625 return TRUE;
626}
627
628void wxApp::CleanUp()
629{
630 wxModule::CleanUpModules();
631
632#if wxUSE_WX_RESOURCES
633 wxCleanUpResourceSystem();
634#endif
635
636 delete wxTheColourDatabase;
637 wxTheColourDatabase = (wxColourDatabase*) NULL;
638
639 wxDeleteStockObjects();
640
641 wxDeleteStockLists();
642
643 delete wxTheApp;
644 wxTheApp = (wxApp*) NULL;
645
646 wxClassInfo::CleanUpClasses();
647
648#if wxUSE_THREADS
649 delete wxPendingEvents;
650 delete wxPendingEventsLocker;
651#endif
652
653 // check for memory leaks
654#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
655 if (wxDebugContext::CountObjectsLeft(TRUE) > 0)
656 {
657 wxLogDebug(wxT("There were memory leaks.\n"));
658 wxDebugContext::Dump();
659 wxDebugContext::PrintStatistics();
660 }
661#endif // Debug
662
663#if wxUSE_LOG
664 // do this as the very last thing because everything else can log messages
665 wxLog::DontCreateOnDemand();
666
667 wxLog *oldLog = wxLog::SetActiveTarget( (wxLog*) NULL );
668 if (oldLog)
669 delete oldLog;
670#endif // wxUSE_LOG
671}
672
673//-----------------------------------------------------------------------------
674// wxEntry
675//-----------------------------------------------------------------------------
676
677// NB: argc and argv may be changed here, pass by reference!
678int wxEntryStart( int& argc, char *argv[] )
679{
680#if wxUSE_THREADS
681 // GTK 1.2 up to version 1.2.3 has broken threads
682 if ((gtk_major_version == 1) &&
683 (gtk_minor_version == 2) &&
684 (gtk_micro_version < 4))
685 {
686 printf( "wxWindows warning: GUI threading disabled due to outdated GTK version\n" );
687 }
688 else
689 {
690 g_thread_init(NULL);
691 }
692#endif
693
694 gtk_set_locale();
695
696 // We should have the wxUSE_WCHAR_T test on the _outside_
697#if wxUSE_WCHAR_T
698#if defined(__WXGTK20__)
699 // gtk+ 2.0 supports Unicode through UTF-8 strings
700 wxConvCurrent = &wxConvUTF8;
701#else
702 if (!wxOKlibc()) wxConvCurrent = &wxConvLocal;
703#endif
704#else
705 if (!wxOKlibc()) wxConvCurrent = (wxMBConv*) NULL;
706#endif
707
708 gdk_threads_enter();
709
710 gtk_init( &argc, &argv );
711
712 wxSetDetectableAutoRepeat( TRUE );
713
714 if (!wxApp::Initialize())
715 {
716 gdk_threads_leave();
717 return -1;
718 }
719
720 return 0;
721}
722
723
724int wxEntryInitGui()
725{
726 int retValue = 0;
727
728 if ( !wxTheApp->OnInitGui() )
729 retValue = -1;
730
731 wxGetRootWindow();
732
733 return retValue;
734}
735
736
737void wxEntryCleanup()
738{
739#if wxUSE_LOG
740 // flush the logged messages if any
741 wxLog *log = wxLog::GetActiveTarget();
742 if (log != NULL && log->HasPendingMessages())
743 log->Flush();
744
745 // continuing to use user defined log target is unsafe from now on because
746 // some resources may be already unavailable, so replace it by something
747 // more safe
748 wxLog *oldlog = wxLog::SetActiveTarget(new wxLogStderr);
749 if ( oldlog )
750 delete oldlog;
751#endif // wxUSE_LOG
752
753 wxApp::CleanUp();
754
755 gdk_threads_leave();
756}
757
758
759int wxEntry( int argc, char *argv[] )
760{
761#if (defined(__WXDEBUG__) && wxUSE_MEMORY_TRACING) || wxUSE_DEBUG_CONTEXT
762 // This seems to be necessary since there are 'rogue'
763 // objects present at this point (perhaps global objects?)
764 // Setting a checkpoint will ignore them as far as the
765 // memory checking facility is concerned.
766 // Of course you may argue that memory allocated in globals should be
767 // checked, but this is a reasonable compromise.
768 wxDebugContext::SetCheckpoint();
769#endif
770 int err = wxEntryStart(argc, argv);
771 if (err)
772 return err;
773
774 if (!wxTheApp)
775 {
776 wxCHECK_MSG( wxApp::GetInitializerFunction(), -1,
777 wxT("wxWindows error: No initializer - use IMPLEMENT_APP macro.\n") );
778
779 wxAppInitializerFunction app_ini = wxApp::GetInitializerFunction();
780
781 wxObject *test_app = app_ini();
782
783 wxTheApp = (wxApp*) test_app;
784 }
785
786 wxCHECK_MSG( wxTheApp, -1, wxT("wxWindows error: no application object") );
787
788 wxTheApp->argc = argc;
789#if wxUSE_UNICODE
790 wxTheApp->argv = new wxChar*[argc+1];
791 int mb_argc = 0;
792 while (mb_argc < argc)
793 {
794 wxTheApp->argv[mb_argc] = wxStrdup(wxConvLibc.cMB2WX(argv[mb_argc]));
795 mb_argc++;
796 }
797 wxTheApp->argv[mb_argc] = (wxChar *)NULL;
798#else
799 wxTheApp->argv = argv;
800#endif
801
802 wxString name(wxFileNameFromPath(argv[0]));
803 wxStripExtension( name );
804 wxTheApp->SetAppName( name );
805
806 int retValue;
807 retValue = wxEntryInitGui();
808
809 // Here frames insert themselves automatically into wxTopLevelWindows by
810 // getting created in OnInit().
811 if ( retValue == 0 )
812 {
813 if ( !wxTheApp->OnInit() )
814 retValue = -1;
815 }
816
817 if ( retValue == 0 )
818 {
819 // Delete pending toplevel windows
820 wxTheApp->DeletePendingObjects();
821
822 // When is the app not initialized ?
823 wxTheApp->m_initialized = TRUE;
824
825 if (wxTheApp->Initialized())
826 {
827 wxTheApp->OnRun();
828
829 wxWindow *topWindow = wxTheApp->GetTopWindow();
830
831 // Delete all pending windows if any
832 wxTheApp->DeletePendingObjects();
833
834 // Reset top window
835 if (topWindow)
836 wxTheApp->SetTopWindow( (wxWindow*) NULL );
837
838 retValue = wxTheApp->OnExit();
839 }
840 }
841
842 wxEntryCleanup();
843
844 return retValue;
845}
846
847#ifdef __WXDEBUG__
848
849void wxApp::OnAssert(const wxChar *file, int line, const wxChar *msg)
850{
851 m_isInAssert = TRUE;
852
853 wxAppBase::OnAssert(file, line, msg);
854
855 m_isInAssert = FALSE;
856}
857
858#endif // __WXDEBUG__
859