]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/app.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/app.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 // vms_jackets.h should for proper working be included before anything else
12 # include <vms_jackets.h>
13 #undef ConnectionNumber
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/memory.h"
29 #include "wx/thread.h"
35 #include "wx/gtk/win_gtk.h"
36 #include "wx/gtk/private.h"
37 #include "wx/apptrait.h"
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 #if wxUSE_MIMETYPE && wxUSE_LIBGNOMEVFS
47 wxFORCE_LINK_MODULE(gnome_vfs
)
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 bool g_mainThreadLocked
= false;
56 static GtkWidget
*gs_RootWindow
= (GtkWidget
*) NULL
;
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 // not static because used by textctrl.cpp
65 bool wxIsInsideYield
= false;
67 bool wxApp::Yield(bool onlyIfNeeded
)
69 if ( wxIsInsideYield
)
73 wxFAIL_MSG( wxT("wxYield called recursively" ) );
80 if ( !wxThread::IsMain() )
82 // can't call gtk_main_iteration() from other threads like this
85 #endif // wxUSE_THREADS
87 wxIsInsideYield
= true;
90 // disable log flushing from here because a call to wxYield() shouldn't
91 // normally result in message boxes popping up &c
95 while (EventsPending())
98 // It's necessary to call ProcessIdle() to update the frames sizes which
99 // might have been changed (it also will update other things set from
100 // OnUpdateUI() which is a nice (and desired) side effect). But we
101 // call ProcessIdle() only once since this is not meant for longish
102 // background jobs (controlled by wxIdleEvent::RequestMore() and the
103 // return value of Processidle().
107 // let the logs be flashed again
111 wxIsInsideYield
= false;
116 //-----------------------------------------------------------------------------
118 //-----------------------------------------------------------------------------
120 // One-shot signal emission hook, to install idle handler.
123 wx_emission_hook(GSignalInvocationHint
*, guint
, const GValue
*, gpointer data
)
125 wxApp
* app
= wxTheApp
;
128 gulong
* hook_id
= (gulong
*)data
;
129 // record that hook is not installed
136 // Add signal emission hooks, to re-install idle handler when needed.
137 static void wx_add_idle_hooks()
141 static gulong hook_id
= 0;
144 static guint sig_id
= 0;
146 sig_id
= g_signal_lookup("event", GTK_TYPE_WIDGET
);
147 hook_id
= g_signal_add_emission_hook(
148 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
151 // "size_allocate" hook
152 // Needed to match the behavior of the old idle system,
153 // but probably not necessary.
155 static gulong hook_id
= 0;
158 static guint sig_id
= 0;
160 sig_id
= g_signal_lookup("size_allocate", GTK_TYPE_WIDGET
);
161 hook_id
= g_signal_add_emission_hook(
162 sig_id
, 0, wx_emission_hook
, &hook_id
, NULL
);
168 static gboolean
wxapp_idle_callback(gpointer
)
170 return wxTheApp
->DoIdle();
178 // Allow another idle source to be added while this one is busy.
179 // Needed if an idle event handler runs a new event loop,
180 // for example by showing a dialog.
182 wxMutexLocker
lock(*m_idleMutex
);
184 id_save
= m_idleSourceId
;
188 // don't generate the idle events while the assert modal dialog is shown,
189 // this matches the behavior of wxMSW
198 needMore
= ProcessIdle();
199 } while (needMore
&& gtk_events_pending() == 0);
203 wxMutexLocker
lock(*m_idleMutex
);
205 // if a new idle source was added during ProcessIdle
206 if (m_idleSourceId
!= 0)
209 g_source_remove(m_idleSourceId
);
212 // if more idle processing requested
215 // keep this source installed
216 m_idleSourceId
= id_save
;
219 // add hooks and remove this source
226 static GPollFunc wxgs_poll_func
;
229 static gint
wxapp_poll_func( GPollFD
*ufds
, guint nfds
, gint timeout
)
234 g_mainThreadLocked
= true;
236 gint res
= (*wxgs_poll_func
)(ufds
, nfds
, timeout
);
239 g_mainThreadLocked
= false;
247 #endif // wxUSE_THREADS
249 //-----------------------------------------------------------------------------
250 // Access to the root window global
251 //-----------------------------------------------------------------------------
253 GtkWidget
* wxGetRootWindow()
255 if (gs_RootWindow
== NULL
)
257 gs_RootWindow
= gtk_window_new( GTK_WINDOW_TOPLEVEL
);
258 gtk_widget_realize( gs_RootWindow
);
260 return gs_RootWindow
;
263 //-----------------------------------------------------------------------------
265 //-----------------------------------------------------------------------------
267 IMPLEMENT_DYNAMIC_CLASS(wxApp
,wxEvtHandler
)
269 BEGIN_EVENT_TABLE(wxApp
, wxEvtHandler
)
270 EVT_IDLE(wxAppBase::OnIdle
)
276 m_isInAssert
= false;
277 #endif // __WXDEBUG__
288 bool wxApp::OnInitGui()
290 if ( !wxAppBase::OnInitGui() )
293 // if this is a wxGLApp (derived from wxApp), and we've already
294 // chosen a specific visual, then derive the GdkVisual from that
295 if ( GetXVisualInfo() )
297 GdkVisual
* vis
= gtk_widget_get_default_visual();
299 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
300 gtk_widget_set_default_colormap( colormap
);
304 // On some machines, the default visual is just 256 colours, so
305 // we make sure we get the best. This can sometimes be wasteful.
308 if (m_forceTrueColour
)
310 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
313 wxLogError(wxT("Unable to initialize TrueColor visual."));
316 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
317 gtk_widget_set_default_colormap( colormap
);
321 if (gdk_visual_get_best() != gdk_visual_get_system())
323 GdkVisual
* visual
= gdk_visual_get_best();
324 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
325 gtk_widget_set_default_colormap( colormap
);
334 GdkVisual
*wxApp::GetGdkVisual()
336 GdkVisual
*visual
= NULL
;
338 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
340 visual
= gdkx_visual_get( xvi
->visualid
);
342 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
349 bool wxApp::Initialize(int& argc
, wxChar
**argv
)
351 if ( !wxAppBase::Initialize(argc
, argv
) )
355 if (!g_thread_supported())
358 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
359 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
360 #endif // wxUSE_THREADS
362 // We should have the wxUSE_WCHAR_T test on the _outside_
364 // gtk+ 2.0 supports Unicode through UTF-8 strings
365 wxConvCurrent
= &wxConvUTF8
;
366 #else // !wxUSE_WCHAR_T
368 wxConvCurrent
= (wxMBConv
*) NULL
;
369 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
371 // decide which conversion to use for the file names
373 // (1) this variable exists for the sole purpose of specifying the encoding
374 // of the filenames for GTK+ programs, so use it if it is set
375 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
376 encName
= encName
.BeforeFirst(_T(','));
377 if (encName
.CmpNoCase(_T("@locale")) == 0)
383 // (2) if a non default locale is set, assume that the user wants his
384 // filenames in this locale too
385 encName
= wxLocale::GetSystemEncodingName().Upper();
386 // (3) finally use UTF-8 by default
387 if (encName
.empty() || encName
== _T("US-ASCII"))
388 encName
= _T("UTF-8");
389 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
393 encName
= _T("UTF-8");
395 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
396 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
397 // from gtk_init_check() as it does by default
398 gtk_disable_setlocale();
401 static wxConvBrokenFileNames
fileconv(encName
);
402 wxConvFileName
= &fileconv
;
408 // gtk_init() wants UTF-8, not wchar_t, so convert
410 char **argvGTK
= new char *[argc
+ 1];
411 for ( i
= 0; i
< argc
; i
++ )
413 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv
[i
]));
416 argvGTK
[argc
] = NULL
;
421 init_result
= true; // is there a _check() version of this?
422 gpe_application_init( &argcGTK
, &argvGTK
);
424 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
426 wxUpdateLocaleIsUtf8();
428 if ( argcGTK
!= argc
)
430 // we have to drop the parameters which were consumed by GTK+
431 for ( i
= 0; i
< argcGTK
; i
++ )
433 while ( strcmp(wxConvUTF8
.cWX2MB(argv
[i
]), argvGTK
[i
]) != 0 )
435 memmove(argv
+ i
, argv
+ i
+ 1, (argc
- i
)*sizeof(*argv
));
441 //else: gtk_init() didn't modify our parameters
444 for ( i
= 0; i
< argcGTK
; i
++ )
450 #else // !wxUSE_UNICODE
451 // gtk_init() shouldn't actually change argv itself (just its contents) so
452 // it's ok to pass pointer to it
453 init_result
= gtk_init_check( &argc
, &argv
);
454 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
456 // update internal arg[cv] as GTK+ may have removed processed options:
462 // if there are still GTK+ standard options unparsed in the command
463 // line, it means that they were not syntactically correct and GTK+
464 // already printed a warning on the command line and we should now
466 wxArrayString opt
, desc
;
467 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
469 for ( int i
= 0; i
< argc
; i
++ )
471 // leave just the names of the options with values
472 const wxString str
= wxString(argv
[i
]).BeforeFirst('=');
474 for ( size_t j
= 0; j
< opt
.size(); j
++ )
476 // remove the leading spaces from the option string as it does
478 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
480 // a GTK+ option can be left on the command line only if
481 // there was an error in (or before, in another standard
482 // options) it, so abort, just as we do if incorrect
483 // program option is given
484 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
494 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
498 // we can not enter threads before gtk_init is done
501 wxSetDetectableAutoRepeat( true );
504 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
508 m_idleMutex
= new wxMutex
;
510 // make sure GtkWidget type is loaded, idle hooks need it
511 g_type_class_ref(GTK_TYPE_WIDGET
);
517 void wxApp::CleanUp()
519 if (m_idleSourceId
!= 0)
520 g_source_remove(m_idleSourceId
);
525 // release reference acquired by Initialize()
526 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
530 wxAppBase::CleanUp();
533 void wxApp::WakeUpIdle()
536 wxMutexLocker
lock(*m_idleMutex
);
538 if (m_idleSourceId
== 0)
539 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
542 // Checking for pending events requires first removing our idle source,
543 // otherwise it will cause the check to always return true.
544 bool wxApp::EventsPending()
547 wxMutexLocker
lock(*m_idleMutex
);
549 if (m_idleSourceId
!= 0)
551 g_source_remove(m_idleSourceId
);
555 return gtk_events_pending() != 0;
560 void wxApp::OnAssertFailure(const wxChar
*file
,
567 // block wx idle events while assert dialog is showing
570 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
572 m_isInAssert
= false;
575 #endif // __WXDEBUG__