]>
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
)
272 m_isInAssert
= false;
273 #endif // __WXDEBUG__
284 bool wxApp::OnInitGui()
286 if ( !wxAppBase::OnInitGui() )
289 // if this is a wxGLApp (derived from wxApp), and we've already
290 // chosen a specific visual, then derive the GdkVisual from that
291 if ( GetXVisualInfo() )
293 GdkVisual
* vis
= gtk_widget_get_default_visual();
295 GdkColormap
*colormap
= gdk_colormap_new( vis
, FALSE
);
296 gtk_widget_set_default_colormap( colormap
);
300 // On some machines, the default visual is just 256 colours, so
301 // we make sure we get the best. This can sometimes be wasteful.
304 if (m_forceTrueColour
)
306 GdkVisual
* visual
= gdk_visual_get_best_with_both( 24, GDK_VISUAL_TRUE_COLOR
);
309 wxLogError(wxT("Unable to initialize TrueColor visual."));
312 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
313 gtk_widget_set_default_colormap( colormap
);
317 if (gdk_visual_get_best() != gdk_visual_get_system())
319 GdkVisual
* visual
= gdk_visual_get_best();
320 GdkColormap
*colormap
= gdk_colormap_new( visual
, FALSE
);
321 gtk_widget_set_default_colormap( colormap
);
330 GdkVisual
*wxApp::GetGdkVisual()
332 GdkVisual
*visual
= NULL
;
334 XVisualInfo
*xvi
= (XVisualInfo
*)GetXVisualInfo();
336 visual
= gdkx_visual_get( xvi
->visualid
);
338 visual
= gdk_drawable_get_visual( wxGetRootWindow()->window
);
345 // use unusual names for the parameters to avoid conflict with wxApp::arg[cv]
346 bool wxApp::Initialize(int& argc_
, wxChar
**argv_
)
348 if ( !wxAppBase::Initialize(argc_
, argv_
) )
352 if (!g_thread_supported())
355 wxgs_poll_func
= g_main_context_get_poll_func(NULL
);
356 g_main_context_set_poll_func(NULL
, wxapp_poll_func
);
357 #endif // wxUSE_THREADS
359 // We should have the wxUSE_WCHAR_T test on the _outside_
361 // gtk+ 2.0 supports Unicode through UTF-8 strings
362 wxConvCurrent
= &wxConvUTF8
;
363 #else // !wxUSE_WCHAR_T
365 wxConvCurrent
= (wxMBConv
*) NULL
;
366 #endif // wxUSE_WCHAR_T/!wxUSE_WCHAR_T
368 // decide which conversion to use for the file names
370 // (1) this variable exists for the sole purpose of specifying the encoding
371 // of the filenames for GTK+ programs, so use it if it is set
372 wxString
encName(wxGetenv(_T("G_FILENAME_ENCODING")));
373 encName
= encName
.BeforeFirst(_T(','));
374 if (encName
.CmpNoCase(_T("@locale")) == 0)
380 // (2) if a non default locale is set, assume that the user wants his
381 // filenames in this locale too
382 encName
= wxLocale::GetSystemEncodingName().Upper();
383 // (3) finally use UTF-8 by default
384 if (encName
.empty() || encName
== _T("US-ASCII"))
385 encName
= _T("UTF-8");
386 wxSetEnv(_T("G_FILENAME_ENCODING"), encName
);
390 encName
= _T("UTF-8");
392 // if wxUSE_INTL==0 it probably indicates that only "C" locale is supported
393 // by the program anyhow so prevent GTK+ from calling setlocale(LC_ALL, "")
394 // from gtk_init_check() as it does by default
395 gtk_disable_setlocale();
398 static wxConvBrokenFileNames
fileconv(encName
);
399 wxConvFileName
= &fileconv
;
405 // gtk_init() wants UTF-8, not wchar_t, so convert
407 char **argvGTK
= new char *[argc_
+ 1];
408 for ( i
= 0; i
< argc_
; i
++ )
410 argvGTK
[i
] = wxStrdupA(wxConvUTF8
.cWX2MB(argv_
[i
]));
413 argvGTK
[argc_
] = NULL
;
418 init_result
= true; // is there a _check() version of this?
419 gpe_application_init( &argcGTK
, &argvGTK
);
421 init_result
= gtk_init_check( &argcGTK
, &argvGTK
);
423 wxUpdateLocaleIsUtf8();
425 if ( argcGTK
!= argc_
)
427 // we have to drop the parameters which were consumed by GTK+
428 for ( i
= 0; i
< argcGTK
; i
++ )
430 while ( strcmp(wxConvUTF8
.cWX2MB(argv_
[i
]), argvGTK
[i
]) != 0 )
432 memmove(argv_
+ i
, argv_
+ i
+ 1, (argc_
- i
)*sizeof(*argv_
));
438 //else: gtk_init() didn't modify our parameters
441 for ( i
= 0; i
< argcGTK
; i
++ )
447 #else // !wxUSE_UNICODE
448 // gtk_init() shouldn't actually change argv_ itself (just its contents) so
449 // it's ok to pass pointer to it
450 init_result
= gtk_init_check( &argc_
, &argv_
);
451 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
453 // update internal arg[cv] as GTK+ may have removed processed options:
459 // if there are still GTK+ standard options unparsed in the command
460 // line, it means that they were not syntactically correct and GTK+
461 // already printed a warning on the command line and we should now
463 wxArrayString opt
, desc
;
464 m_traits
->GetStandardCmdLineOptions(opt
, desc
);
466 for ( int i
= 0; i
< argc_
; i
++ )
468 // leave just the names of the options with values
469 const wxString str
= wxString(argv_
[i
]).BeforeFirst('=');
471 for ( size_t j
= 0; j
< opt
.size(); j
++ )
473 // remove the leading spaces from the option string as it does
475 if ( opt
[j
].Trim(false).BeforeFirst('=') == str
)
477 // a GTK+ option can be left on the command line only if
478 // there was an error in (or before, in another standard
479 // options) it, so abort, just as we do if incorrect
480 // program option is given
481 wxLogError(_("Invalid GTK+ command line option, use \"%s --help\""),
491 wxLogError(_("Unable to initialize GTK+, is DISPLAY set properly?"));
495 // we can not enter threads before gtk_init is done
498 wxSetDetectableAutoRepeat( true );
501 wxFont::SetDefaultEncoding(wxLocale::GetSystemEncoding());
505 m_idleMutex
= new wxMutex
;
507 // make sure GtkWidget type is loaded, idle hooks need it
508 g_type_class_ref(GTK_TYPE_WIDGET
);
514 void wxApp::CleanUp()
516 if (m_idleSourceId
!= 0)
517 g_source_remove(m_idleSourceId
);
522 // release reference acquired by Initialize()
523 g_type_class_unref(g_type_class_peek(GTK_TYPE_WIDGET
));
527 wxAppBase::CleanUp();
530 void wxApp::WakeUpIdle()
533 wxMutexLocker
lock(*m_idleMutex
);
535 if (m_idleSourceId
== 0)
536 m_idleSourceId
= g_idle_add_full(G_PRIORITY_LOW
, wxapp_idle_callback
, NULL
, NULL
);
539 // Checking for pending events requires first removing our idle source,
540 // otherwise it will cause the check to always return true.
541 bool wxApp::EventsPending()
544 wxMutexLocker
lock(*m_idleMutex
);
546 if (m_idleSourceId
!= 0)
548 g_source_remove(m_idleSourceId
);
552 return gtk_events_pending() != 0;
557 void wxApp::OnAssertFailure(const wxChar
*file
,
564 // block wx idle events while assert dialog is showing
567 wxAppBase::OnAssertFailure(file
, line
, func
, cond
, msg
);
569 m_isInAssert
= false;
572 #endif // __WXDEBUG__