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"
24 #include "wx/unix/execute.h"
26 #include "wx/gtk/private/timer.h"
27 #include "wx/evtloop.h"
30 #ifdef GDK_WINDOWING_WIN32
31 #include <gdk/gdkwin32.h>
33 #ifdef GDK_WINDOWING_X11
38 #include "wx/gtk/assertdlg_gtk.h"
40 #include "wx/stackwalk.h"
41 #endif // wxUSE_STACKWALKER
42 #endif // wxDEBUG_LEVEL
47 #include <sys/types.h>
53 #include <X11/SM/SMlib.h>
55 #include "wx/unix/utilsx11.h"
58 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
62 extern GtkWidget
*wxGetRootWindow();
64 //----------------------------------------------------------------------------
66 //----------------------------------------------------------------------------
68 // on OS/2, we use the wxBell from wxBase library
76 // ----------------------------------------------------------------------------
77 // display characterstics
78 // ----------------------------------------------------------------------------
85 void wxDisplaySize( int *width
, int *height
)
87 if (width
) *width
= gdk_screen_width();
88 if (height
) *height
= gdk_screen_height();
91 void wxDisplaySizeMM( int *width
, int *height
)
93 if (width
) *width
= gdk_screen_width_mm();
94 if (height
) *height
= gdk_screen_height_mm();
97 void wxGetMousePosition( int* x
, int* y
)
99 gdk_window_get_pointer(gtk_widget_get_root_window(wxGetRootWindow()), x
, y
, NULL
);
102 bool wxColourDisplay()
109 return gtk_widget_get_visual(wxGetRootWindow())->depth
;
112 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
114 return wxGenericFindWindowAtPoint(pt
);
119 WXDLLIMPEXP_CORE wxCharBuffer
120 wxConvertToGTK(const wxString
& s
, wxFontEncoding enc
)
123 if ( enc
== wxFONTENCODING_SYSTEM
|| enc
== wxFONTENCODING_DEFAULT
)
125 wbuf
= wxConvUI
->cMB2WC(s
.c_str());
127 else // another encoding, use generic conversion class
129 wbuf
= wxCSConv(enc
).cMB2WC(s
.c_str());
132 if ( !wbuf
&& !s
.empty() )
134 // conversion failed, but we still want to show something to the user
135 // even if it's going to be wrong it is better than nothing
137 // we choose ISO8859-1 here arbitrarily, it's just the most common
138 // encoding probably and, also importantly here, conversion from it
139 // never fails as it's done internally by wxCSConv
140 wbuf
= wxCSConv(wxFONTENCODING_ISO8859_1
).cMB2WC(s
.c_str());
143 return wxConvUTF8
.cWC2MB(wbuf
);
146 WXDLLIMPEXP_CORE wxCharBuffer
147 wxConvertFromGTK(const wxString
& s
, wxFontEncoding enc
)
149 // this conversion should never fail as GTK+ always uses UTF-8 internally
150 // so there are no complications here
151 const wxWCharBuffer
wbuf(wxConvUTF8
.cMB2WC(s
.c_str()));
152 if ( enc
== wxFONTENCODING_SYSTEM
)
153 return wxConvUI
->cWC2MB(wbuf
);
155 return wxCSConv(enc
).cWC2MB(wbuf
);
158 #endif // !wxUSE_UNICODE
160 // Returns NULL if version is certainly greater or equal than major.minor.micro
161 // Returns string describing the error if version is lower than
162 // major.minor.micro OR it cannot be determined and one should not rely on the
163 // availability of pango version major.minor.micro, nor the non-availability
164 const gchar
*wx_pango_version_check (int major
, int minor
, int micro
)
166 // NOTE: you don't need to use this macro to check for Pango features
167 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
168 // for GTK lib) required pango 1.4...
170 #ifdef PANGO_VERSION_MAJOR
171 if (!gtk_check_version (2,11,0))
173 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
174 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
175 // using has the pango_version_check function:
176 return pango_version_check (major
, minor
, micro
);
179 return "can't check";
180 #else // !PANGO_VERSION_MAJOR
185 return "too old headers";
190 // ----------------------------------------------------------------------------
191 // subprocess routines
192 // ----------------------------------------------------------------------------
195 static gboolean
EndProcessDetector(GIOChannel
* source
, GIOCondition
, void* data
)
197 wxEndProcessData
* const
198 proc_data
= static_cast<wxEndProcessData
*>(data
);
200 // child exited, end waiting
201 close(g_io_channel_unix_get_fd(source
));
203 wxHandleProcessTermination(proc_data
);
205 // don't call us again!
210 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
212 GIOChannel
* channel
= g_io_channel_unix_new(fd
);
213 GIOCondition cond
= GIOCondition(G_IO_IN
| G_IO_HUP
| G_IO_ERR
);
214 unsigned id
= g_io_add_watch(channel
, cond
, EndProcessDetector
, proc_data
);
215 g_io_channel_unref(channel
);
221 // ----------------------------------------------------------------------------
222 // wxPlatformInfo-related
223 // ----------------------------------------------------------------------------
225 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
228 *verMaj
= gtk_major_version
;
230 *verMin
= gtk_minor_version
;
237 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
239 return new wxGTKTimerImpl(timer
);
242 #endif // wxUSE_TIMER
245 static wxString
GetSM()
249 return wxEmptyString
;
253 SmcConn smc_conn
= SmcOpenConnection(NULL
, NULL
,
255 0 /* mask */, NULL
/* callbacks */,
257 WXSIZEOF(smerr
), smerr
);
261 wxLogDebug("Failed to connect to session manager: %s", smerr
);
262 return wxEmptyString
;
265 char *vendor
= SmcVendor(smc_conn
);
266 wxString ret
= wxString::FromAscii( vendor
);
269 SmcCloseConnection(smc_conn
, 0, NULL
);
274 #endif // wxUSE_DETECT_SM
277 //-----------------------------------------------------------------------------
279 //-----------------------------------------------------------------------------
281 wxEventLoopBase
*wxGUIAppTraits::CreateEventLoop()
283 return new wxEventLoop();
288 void wxGUIAppTraits::SetLocale()
291 wxUpdateLocaleIsUtf8();
295 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
297 // private helper class
298 class StackDump
: public wxStackWalker
301 StackDump(GtkAssertDialog
*dlg
) { m_dlg
=dlg
; }
304 virtual void OnStackFrame(const wxStackFrame
& frame
)
306 wxString fncname
= frame
.GetName();
307 wxString fncargs
= fncname
;
309 size_t n
= fncname
.find(wxT('('));
310 if (n
!= wxString::npos
)
312 // remove arguments from function name
315 // remove function name and brackets from arguments
316 fncargs
= fncargs
.substr(n
+1, fncargs
.length()-n
-2);
319 fncargs
= wxEmptyString
;
321 // append this stack frame's info in the dialog
322 if (!frame
.GetFileName().empty() || !fncname
.empty())
323 gtk_assert_dialog_append_stack_frame(m_dlg
,
326 frame
.GetFileName().mb_str(),
331 GtkAssertDialog
*m_dlg
;
334 static void get_stackframe_callback(void* p
)
336 StackDump
* dump
= static_cast<StackDump
*>(p
);
337 // skip over frames up to including wxOnAssert()
338 dump
->ProcessFrames(3);
341 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
343 bool wxGUIAppTraits::ShowAssertDialog(const wxString
& msg
)
346 // we can't show the dialog from another thread
347 if ( wxIsMainThread() )
349 // under GTK2 we prefer to use a dialog widget written using directly
350 // in GTK+ as use a dialog written using wxWidgets would need the
351 // wxWidgets idle processing to work correctly which might not be the
352 // case when assert happens
353 GtkWidget
*dialog
= gtk_assert_dialog_new();
354 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog
), msg
.mb_str());
356 #if wxUSE_STACKWALKER
357 // save the current stack ow...
358 StackDump
dump(GTK_ASSERT_DIALOG(dialog
));
359 dump
.SaveStack(100); // showing more than 100 frames is not very useful
361 // ...but process it only if the user needs it
362 gtk_assert_dialog_set_backtrace_callback
364 GTK_ASSERT_DIALOG(dialog
),
365 get_stackframe_callback
,
368 #endif // wxUSE_STACKWALKER
370 gint result
= gtk_dialog_run(GTK_DIALOG (dialog
));
371 bool returnCode
= false;
374 case GTK_ASSERT_DIALOG_STOP
:
377 case GTK_ASSERT_DIALOG_CONTINUE
:
380 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING
:
386 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
389 gtk_widget_destroy(dialog
);
392 #endif // wxDEBUG_LEVEL
394 return wxAppTraitsBase::ShowAssertDialog(msg
);
397 wxString
wxGUIAppTraits::GetDesktopEnvironment() const
399 wxString de
= wxSystemOptions::GetOption(wxT("gtk.desktop"));
403 static const wxString s_SM
= GetSM();
405 if (s_SM
== wxT("GnomeSM"))
407 else if (s_SM
== wxT("KDE"))
410 #endif // wxUSE_DETECT_SM
417 // see the hack below in wxCmdLineParser::GetUsageString().
418 // TODO: replace this hack with a g_option_group_get_entries()
419 // call as soon as such function exists;
420 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
426 gchar
*help_description
;
428 GDestroyNotify destroy_notify
;
431 GTranslateFunc translate_func
;
432 GDestroyNotify translate_notify
;
433 gpointer translate_data
;
435 GOptionEntry
*entries
;
438 GOptionParseFunc pre_parse_func
;
439 GOptionParseFunc post_parse_func
;
440 GOptionErrorFunc error_func
;
443 wxString
wxGetNameFromGtkOptionEntry(const GOptionEntry
*opt
)
448 ret
<< wxT("-") << opt
->short_name
;
453 ret
<< wxT("--") << opt
->long_name
;
455 if (opt
->arg_description
)
456 ret
<< wxT("=") << opt
->arg_description
;
459 return wxT(" ") + ret
;
462 #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.31
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.30 is the
478 // latest one at the time of this writing)
479 if (glib_check_version(2,6,0) == NULL
&& glib_check_version(2,31,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__