1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/utilsgtk.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
16 #include "wx/string.h"
21 #include "wx/apptrait.h"
22 #include "wx/process.h"
23 #include "wx/sysopt.h"
25 #include "wx/unix/execute.h"
28 #include "wx/gtk/private/timer.h"
29 #include "wx/evtloop.h"
32 #ifdef GDK_WINDOWING_WIN32
33 #include <gdk/gdkwin32.h>
35 #ifdef GDK_WINDOWING_X11
40 #include "wx/gtk/assertdlg_gtk.h"
42 #include "wx/stackwalk.h"
43 #endif // wxUSE_STACKWALKER
44 #endif // wxDEBUG_LEVEL
49 #include <sys/types.h>
55 #include <X11/SM/SMlib.h>
57 #include "wx/unix/utilsx11.h"
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 extern GtkWidget
*wxGetRootWindow();
66 //----------------------------------------------------------------------------
68 //----------------------------------------------------------------------------
70 // on OS/2, we use the wxBell from wxBase library
78 // ----------------------------------------------------------------------------
79 // display characterstics
80 // ----------------------------------------------------------------------------
87 void wxDisplaySize( int *width
, int *height
)
89 if (width
) *width
= gdk_screen_width();
90 if (height
) *height
= gdk_screen_height();
93 void wxDisplaySizeMM( int *width
, int *height
)
95 if (width
) *width
= gdk_screen_width_mm();
96 if (height
) *height
= gdk_screen_height_mm();
99 void wxGetMousePosition( int* x
, int* y
)
101 gdk_window_get_pointer(gtk_widget_get_root_window(wxGetRootWindow()), x
, y
, NULL
);
104 bool wxColourDisplay()
111 return gtk_widget_get_visual(wxGetRootWindow())->depth
;
114 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
116 return wxGenericFindWindowAtPoint(pt
);
121 WXDLLIMPEXP_CORE wxCharBuffer
122 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
125 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
127 wbuf
= wxConvUI
->cMB2WC(s
);
129 else // another encoding, use generic conversion class
131 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
134 if ( !wbuf
&& !s
.empty() )
136 // conversion failed, but we still want to show something to the user
137 // even if it's going to be wrong it is better than nothing
139 // we choose ISO8859-1 here arbitrarily, it's just the most common
140 // encoding probably and, also importantly here, conversion from it
141 // never fails as it's done internally by wxCSConv
142 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
145 return wxConvUTF8
.cWC2MB(wbuf
);
148 WXDLLIMPEXP_CORE wxCharBuffer
149 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
151 // this conversion should never fail as GTK+ always uses UTF-8 internally
152 // so there are no complications here
153 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
154 if ( enc
== wxFONTENCODING_SYSTEM
)
155 return wxConvUI
->cWC2MB(wbuf
);
157 return wxCSConv(enc
).cWC2MB(wbuf
);
160 #endif // !wxUSE_UNICODE
162 // Returns NULL if version is certainly greater or equal than major.minor.micro
163 // Returns string describing the error if version is lower than
164 // major.minor.micro OR it cannot be determined and one should not rely on the
165 // availability of pango version major.minor.micro, nor the non-availability
166 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
168 // NOTE: you don't need to use this macro to check for Pango features
169 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
170 // for GTK lib) required pango 1.4...
172 #ifdef PANGO_VERSION_MAJOR
173 if (!gtk_check_version (2,11,0))
175 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
176 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
177 // using has the pango_version_check function:
178 return pango_version_check (major
, minor
, micro
);
181 return "can't check";
182 #else // !PANGO_VERSION_MAJOR
187 return "too old headers";
192 // ----------------------------------------------------------------------------
193 // subprocess routines
194 // ----------------------------------------------------------------------------
199 static gboolean
EndProcessDetector(GIOChannel
* source
, GIOCondition
, void* data
)
201 wxEndProcessData
* const
202 proc_data
= static_cast<wxEndProcessData
*>(data
);
204 // child exited, end waiting
205 close(g_io_channel_unix_get_fd(source
));
207 wxHandleProcessTermination(proc_data
);
209 // don't call us again!
214 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
216 GIOChannel
* channel
= g_io_channel_unix_new(fd
);
217 GIOCondition cond
= GIOCondition(G_IO_IN
| G_IO_HUP
| G_IO_ERR
);
218 unsigned id
= g_io_add_watch(channel
, cond
, EndProcessDetector
, proc_data
);
219 g_io_channel_unref(channel
);
225 // ----------------------------------------------------------------------------
226 // wxPlatformInfo-related
227 // ----------------------------------------------------------------------------
229 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
232 *verMaj
= gtk_major_version
;
234 *verMin
= gtk_minor_version
;
241 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
243 return new wxGTKTimerImpl(timer
);
246 #endif // wxUSE_TIMER
249 static wxString
GetSM()
253 return wxEmptyString
;
257 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
259 0 /* mask */, NULL
/* callbacks */,
261 WXSIZEOF(smerr
), smerr
);
265 wxLogDebug("Failed to connect to session manager: %s", smerr
);
266 return wxEmptyString
;
269 char *vendor
= SmcVendor(smc_conn
);
270 wxString ret
= wxString::FromAscii( vendor
);
273 SmcCloseConnection(smc_conn
, 0, NULL
);
278 #endif // wxUSE_DETECT_SM
281 //-----------------------------------------------------------------------------
283 //-----------------------------------------------------------------------------
285 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
287 return new wxEventLoop();
291 #if wxUSE_INTL && defined(__UNIX__)
292 void wxGUIAppTraits::SetLocale()
295 wxUpdateLocaleIsUtf8();
301 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
303 // private helper class
304 class StackDump
: public wxStackWalker
307 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
310 virtual void OnStackFrame(const wxStackFrame
& frame
)
312 wxString fncname
= frame
.GetName();
314 // append this stack frame's info in the dialog
315 if (!frame
.GetFileName().empty() || !fncname
.empty())
316 gtk_assert_dialog_append_stack_frame(m_dlg
,
318 frame
.GetFileName().mb_str(),
323 GtkAssertDialog
*m_dlg
;
326 static void get_stackframe_callback(void* p
)
328 StackDump
* dump
= static_cast<StackDump
*>(p
);
329 // skip over frames up to including wxOnAssert()
330 dump
->ProcessFrames(3);
333 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
335 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
338 // we can't show the dialog from another thread
339 if ( wxIsMainThread() )
341 // under GTK2 we prefer to use a dialog widget written using directly
342 // in GTK+ as use a dialog written using wxWidgets would need the
343 // wxWidgets idle processing to work correctly which might not be the
344 // case when assert happens
345 GtkWidget
*dialog
= gtk_assert_dialog_new();
346 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
348 #if wxUSE_STACKWALKER
349 // save the current stack ow...
350 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
351 dump
.SaveStack(100); // showing more than 100 frames is not very useful
353 // ...but process it only if the user needs it
354 gtk_assert_dialog_set_backtrace_callback
356 GTK_ASSERT_DIALOG(dialog
),
357 get_stackframe_callback
,
360 #endif // wxUSE_STACKWALKER
362 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
363 bool returnCode
= false;
366 case GTK_ASSERT_DIALOG_STOP
:
369 case GTK_ASSERT_DIALOG_CONTINUE
:
372 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
378 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
381 gtk_widget_destroy(dialog
);
384 #endif // wxDEBUG_LEVEL
386 return wxAppTraitsBase::ShowAssertDialog(msg
);
391 #if defined(__UNIX__) || defined(__OS2__)
393 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
395 wxString de
= wxSystemOptions::GetOption(wxT("gtk.desktop"));
399 static const wxString s_SM
= GetSM();
401 if (s_SM
== wxT("GnomeSM"))
403 else if (s_SM
== wxT("KDE"))
406 #endif // wxUSE_DETECT_SM
411 #endif // __UNIX__ || __OS2__
415 // see the hack below in wxCmdLineParser::GetUsageString().
416 // TODO: replace this hack with a g_option_group_get_entries()
417 // call as soon as such function exists;
418 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
424 gchar
*help_description
;
426 GDestroyNotify destroy_notify
;
429 GTranslateFunc translate_func
;
430 GDestroyNotify translate_notify
;
431 gpointer translate_data
;
433 GOptionEntry
*entries
;
436 GOptionParseFunc pre_parse_func
;
437 GOptionParseFunc post_parse_func
;
438 GOptionErrorFunc error_func
;
441 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
446 ret
<< wxT("-") << opt
->short_name
;
451 ret
<< wxT("--") << opt
->long_name
;
453 if (opt
->arg_description
)
454 ret
<< wxT("=") << opt
->arg_description
;
457 return wxT(" ") + ret
;
460 #endif // __WXGTK26__
465 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString
& names
,
466 wxArrayString
& desc
) const
471 if (!gtk_check_version(2,6,0))
473 // since GTK>=2.6, we can use the glib_check_version() symbol...
475 // check whether GLib version is greater than 2.6 but also lower than 2.33
476 // because, as we use the undocumented _GOptionGroup struct, we don't want
477 // to run this code with future versions which might change it (2.32 is the
478 // latest one at the time of this writing)
479 if (glib_check_version(2,6,0) == NULL
&& glib_check_version(2,33,0))
481 usage
<< _("The following standard GTK+ options are also supported:\n");
483 // passing true here means that the function can open the default
484 // display while parsing (not really used here anyhow)
485 GOptionGroup
*gtkOpts
= gtk_get_option_group(true);
487 // WARNING: here we access the internals of GOptionGroup:
488 GOptionEntry
*entries
= ((_GOptionGroup
*)gtkOpts
)->entries
;
489 unsigned int n_entries
= ((_GOptionGroup
*)gtkOpts
)->n_entries
;
490 wxArrayString namesOptions
, descOptions
;
492 for ( size_t n
= 0; n
< n_entries
; n
++ )
494 if ( entries
[n
].flags
& G_OPTION_FLAG_HIDDEN
)
497 names
.push_back(wxGetNameFromGtkOptionEntry(&entries
[n
]));
499 const gchar
* const entryDesc
= entries
[n
].description
;
500 desc
.push_back(wxString(entryDesc
));
503 g_option_group_free (gtkOpts
);
509 #endif // __WXGTK26__