]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
Use std::isfinite() for wxFinite() for C++11 compilers.
[wxWidgets.git] / src / gtk / utilsgtk.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
dbd25330 2// Name: src/gtk/utilsgtk.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
6c9a19aa 5// Copyright: (c) 1998 Robert Roebling
65571936 6// Licence: wxWindows licence
c801d85f
KB
7/////////////////////////////////////////////////////////////////////////////
8
14f355c2
VS
9// For compilers that support precompilation, includes "wx.h".
10#include "wx/wxprec.h"
11
c801d85f 12#include "wx/utils.h"
df91131c
WS
13
14#ifndef WX_PRECOMP
15 #include "wx/string.h"
88a7a4e1 16 #include "wx/intl.h"
e4db172a 17 #include "wx/log.h"
df91131c 18#endif
c801d85f 19
46446cc2 20#include "wx/apptrait.h"
5336ece4 21#include "wx/process.h"
9d0e0377 22#include "wx/sysopt.h"
518b5d2f 23
c2ca375c 24#include "wx/gtk/private/timer.h"
b46b1d59 25#include "wx/evtloop.h"
c2ca375c 26
a481bbc3 27#include <gtk/gtk.h>
3b81515c
VZ
28#ifdef GDK_WINDOWING_WIN32
29#include <gdk/gdkwin32.h>
30#endif
31#ifdef GDK_WINDOWING_X11
a481bbc3 32#include <gdk/gdkx.h>
3b81515c 33#endif
a481bbc3 34
4b6a582b 35#if wxDEBUG_LEVEL
92696e94 36 #include "wx/gtk/assertdlg_gtk.h"
db9febdf 37 #if wxUSE_STACKWALKER
db9febdf
RR
38 #include "wx/stackwalk.h"
39 #endif // wxUSE_STACKWALKER
4b6a582b 40#endif // wxDEBUG_LEVEL
db9febdf 41
c801d85f 42#include <stdarg.h>
c801d85f
KB
43#include <string.h>
44#include <sys/stat.h>
45#include <sys/types.h>
f04f570f 46#ifdef __UNIX__
c801d85f 47#include <unistd.h>
f04f570f 48#endif
91b8de8d 49
88bbc332 50#if wxUSE_DETECT_SM
dc771347 51 #include <X11/SM/SMlib.h>
acdc8633
VZ
52
53 #include "wx/unix/utilsx11.h"
88bbc332
RR
54#endif
55
9dc44eff
PC
56#include "wx/gtk/private/gtk2-compat.h"
57
d76fe38b
RR
58//-----------------------------------------------------------------------------
59// data
60//-----------------------------------------------------------------------------
61
c2fa61e8 62extern GtkWidget *wxGetRootWindow();
d76fe38b
RR
63
64//----------------------------------------------------------------------------
c801d85f 65// misc.
d76fe38b 66//----------------------------------------------------------------------------
c801d85f 67
518b5d2f 68void wxBell()
c801d85f 69{
e52f60e6
RR
70 gdk_beep();
71}
c801d85f 72
518b5d2f 73// ----------------------------------------------------------------------------
9dc44eff 74// display characteristics
518b5d2f 75// ----------------------------------------------------------------------------
82052aff 76
9dc44eff 77#ifdef GDK_WINDOWING_X11
d111a89a
VZ
78void *wxGetDisplay()
79{
9dc44eff 80 return GDK_DISPLAY_XDISPLAY(gtk_widget_get_display(wxGetRootWindow()));
d111a89a 81}
9dc44eff 82#endif
d111a89a 83
c0392997
RR
84void wxDisplaySize( int *width, int *height )
85{
ef26a5c4
JS
86 if (width) *width = gdk_screen_width();
87 if (height) *height = gdk_screen_height();
c0392997
RR
88}
89
904a68b6
RL
90void wxDisplaySizeMM( int *width, int *height )
91{
92 if (width) *width = gdk_screen_width_mm();
93 if (height) *height = gdk_screen_height_mm();
94}
95
518b5d2f 96bool wxColourDisplay()
6de97a3b 97{
df91131c 98 return true;
6de97a3b
RR
99}
100
518b5d2f 101int wxDisplayDepth()
6de97a3b 102{
9dc44eff 103 return gdk_visual_get_depth(gtk_widget_get_visual(wxGetRootWindow()));
6de97a3b
RR
104}
105
57591e0e
JS
106wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
107{
108 return wxGenericFindWindowAtPoint(pt);
109}
110
5f11fef5
VZ
111#if !wxUSE_UNICODE
112
afb6a92f
VS
113WXDLLIMPEXP_CORE wxCharBuffer
114wxConvertToGTK(const wxString& s, wxFontEncoding enc)
5f11fef5 115{
404ac4c6
VZ
116 wxWCharBuffer wbuf;
117 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
5f11fef5 118 {
f069ac48 119 wbuf = wxConvUI->cMB2WC(s);
12bc5f9a 120 }
404ac4c6 121 else // another encoding, use generic conversion class
12bc5f9a 122 {
de004174 123 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
404ac4c6 124 }
12bc5f9a 125
404ac4c6
VZ
126 if ( !wbuf && !s.empty() )
127 {
128 // conversion failed, but we still want to show something to the user
129 // even if it's going to be wrong it is better than nothing
130 //
131 // we choose ISO8859-1 here arbitrarily, it's just the most common
132 // encoding probably and, also importantly here, conversion from it
133 // never fails as it's done internally by wxCSConv
de004174 134 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
27dee9ae 135 }
5f11fef5 136
404ac4c6 137 return wxConvUTF8.cWC2MB(wbuf);
5f11fef5
VZ
138}
139
afb6a92f
VS
140WXDLLIMPEXP_CORE wxCharBuffer
141wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
30083ad8
VZ
142{
143 // this conversion should never fail as GTK+ always uses UTF-8 internally
144 // so there are no complications here
de004174 145 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
30083ad8
VZ
146 if ( enc == wxFONTENCODING_SYSTEM )
147 return wxConvUI->cWC2MB(wbuf);
148
149 return wxCSConv(enc).cWC2MB(wbuf);
150}
151
5f11fef5 152#endif // !wxUSE_UNICODE
57591e0e 153
573a2a4c
VS
154// Returns NULL if version is certainly greater or equal than major.minor.micro
155// Returns string describing the error if version is lower than
156// major.minor.micro OR it cannot be determined and one should not rely on the
157// availability of pango version major.minor.micro, nor the non-availability
5f4d1820
VZ
158const gchar *wx_pango_version_check (int major, int minor, int micro)
159{
b5791cc7
FM
160 // NOTE: you don't need to use this macro to check for Pango features
161 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
162 // for GTK lib) required pango 1.4...
03647350 163
9dc44eff
PC
164#ifdef __WXGTK3__
165 return pango_version_check(major, minor, micro);
166#elif defined(PANGO_VERSION_MAJOR)
5f4d1820
VZ
167 if (!gtk_check_version (2,11,0))
168 {
169 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
170 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
171 // using has the pango_version_check function:
172 return pango_version_check (major, minor, micro);
173 }
174
175 return "can't check";
176#else // !PANGO_VERSION_MAJOR
e4161a2a
VZ
177 wxUnusedVar(major);
178 wxUnusedVar(minor);
179 wxUnusedVar(micro);
180
5f4d1820
VZ
181 return "too old headers";
182#endif
183}
184
8bb6b2c0
VZ
185// ----------------------------------------------------------------------------
186// wxPlatformInfo-related
187// ----------------------------------------------------------------------------
188
189wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
190{
191 if ( verMaj )
192 *verMaj = gtk_major_version;
193 if ( verMin )
194 *verMin = gtk_minor_version;
195
196 return wxPORT_GTK;
197}
88bbc332 198
3c029873
VZ
199#if wxUSE_TIMER
200
c2ca375c
VZ
201wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
202{
203 return new wxGTKTimerImpl(timer);
204}
205
3c029873
VZ
206#endif // wxUSE_TIMER
207
88bbc332
RR
208#if wxUSE_DETECT_SM
209static wxString GetSM()
210{
acdc8633 211 wxX11Display dpy;
391bf008
VZ
212 if ( !dpy )
213 return wxEmptyString;
214
dee28fdd 215 char smerr[256];
391bf008
VZ
216 char *client_id;
217 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
218 999, 999,
219 0 /* mask */, NULL /* callbacks */,
220 NULL, &client_id,
dee28fdd 221 WXSIZEOF(smerr), smerr);
391bf008
VZ
222
223 if ( !smc_conn )
dee28fdd 224 {
366721d7 225 wxLogDebug("Failed to connect to session manager: %s", smerr);
391bf008 226 return wxEmptyString;
dee28fdd 227 }
391bf008
VZ
228
229 char *vendor = SmcVendor(smc_conn);
230 wxString ret = wxString::FromAscii( vendor );
231 free(vendor);
232
233 SmcCloseConnection(smc_conn, 0, NULL);
234 free(client_id);
235
236 return ret;
88bbc332 237}
391bf008 238#endif // wxUSE_DETECT_SM
88bbc332 239
db9febdf
RR
240
241//-----------------------------------------------------------------------------
242// wxGUIAppTraits
243//-----------------------------------------------------------------------------
244
2ddff00c 245wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
246{
247 return new wxEventLoop();
248}
249
250
85b1dc6e
VZ
251#ifdef __UNIX__
252
4b6a582b 253#if wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
254
255// private helper class
256class StackDump : public wxStackWalker
257{
258public:
259 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
260
261protected:
262 virtual void OnStackFrame(const wxStackFrame& frame)
263 {
264 wxString fncname = frame.GetName();
db9febdf
RR
265
266 // append this stack frame's info in the dialog
267 if (!frame.GetFileName().empty() || !fncname.empty())
3fce0e64 268 {
db9febdf 269 gtk_assert_dialog_append_stack_frame(m_dlg,
3fce0e64
VZ
270 fncname.utf8_str(),
271 frame.GetFileName().utf8_str(),
db9febdf 272 frame.GetLine());
3fce0e64 273 }
db9febdf
RR
274 }
275
276private:
277 GtkAssertDialog *m_dlg;
278};
279
a481bbc3 280static void get_stackframe_callback(void* p)
db9febdf 281{
a481bbc3
PC
282 StackDump* dump = static_cast<StackDump*>(p);
283 // skip over frames up to including wxOnAssert()
2c64b066 284 dump->ProcessFrames(6);
db9febdf
RR
285}
286
4b6a582b 287#endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
288
289bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
290{
4b6a582b 291#if wxDEBUG_LEVEL
2d8e0096
VZ
292 // we can't show the dialog from another thread
293 if ( wxIsMainThread() )
294 {
295 // under GTK2 we prefer to use a dialog widget written using directly
296 // in GTK+ as use a dialog written using wxWidgets would need the
297 // wxWidgets idle processing to work correctly which might not be the
298 // case when assert happens
299 GtkWidget *dialog = gtk_assert_dialog_new();
300 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
db9febdf 301
2a9332ae 302#if wxUSE_STACKWALKER
61c18e1a 303 // save the current stack ow...
2d8e0096 304 StackDump dump(GTK_ASSERT_DIALOG(dialog));
61c18e1a 305 dump.SaveStack(100); // showing more than 100 frames is not very useful
2d8e0096
VZ
306
307 // ...but process it only if the user needs it
308 gtk_assert_dialog_set_backtrace_callback
309 (
310 GTK_ASSERT_DIALOG(dialog),
a481bbc3 311 get_stackframe_callback,
2d8e0096
VZ
312 &dump
313 );
314#endif // wxUSE_STACKWALKER
315
316 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
317 bool returnCode = false;
318 switch (result)
319 {
320 case GTK_ASSERT_DIALOG_STOP:
321 wxTrap();
322 break;
323 case GTK_ASSERT_DIALOG_CONTINUE:
324 // nothing to do
325 break;
326 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
327 // no more asserts
328 returnCode = true;
329 break;
330
331 default:
332 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
333 }
334
335 gtk_widget_destroy(dialog);
336 return returnCode;
db9febdf 337 }
2d8e0096 338#endif // wxDEBUG_LEVEL
db9febdf 339
2d8e0096 340 return wxAppTraitsBase::ShowAssertDialog(msg);
db9febdf
RR
341}
342
85b1dc6e
VZ
343#endif // __UNIX__
344
345#if defined(__UNIX__) || defined(__OS2__)
346
88bbc332
RR
347wxString wxGUIAppTraits::GetDesktopEnvironment() const
348{
9a83f860 349 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
3c029873 350#if wxUSE_DETECT_SM
9d0e0377
VZ
351 if ( de.empty() )
352 {
9d0e0377 353 static const wxString s_SM = GetSM();
391bf008 354
9d0e0377
VZ
355 if (s_SM == wxT("GnomeSM"))
356 de = wxT("GNOME");
357 else if (s_SM == wxT("KDE"))
358 de = wxT("KDE");
359 }
391bf008 360#endif // wxUSE_DETECT_SM
88bbc332 361
9d0e0377 362 return de;
88bbc332
RR
363}
364
85b1dc6e
VZ
365#endif // __UNIX__ || __OS2__
366
2b76114b
VZ
367#ifdef __UNIX__
368
d3a0a0ee
VZ
369// see the hack below in wxCmdLineParser::GetUsageString().
370// TODO: replace this hack with a g_option_group_get_entries()
baa4a967
FM
371// call as soon as such function exists;
372// see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
373// feature request
d3a0a0ee
VZ
374struct _GOptionGroup
375{
376 gchar *name;
377 gchar *description;
378 gchar *help_description;
379
380 GDestroyNotify destroy_notify;
381 gpointer user_data;
382
383 GTranslateFunc translate_func;
384 GDestroyNotify translate_notify;
385 gpointer translate_data;
386
387 GOptionEntry *entries;
388 gint n_entries;
389
390 GOptionParseFunc pre_parse_func;
391 GOptionParseFunc post_parse_func;
392 GOptionErrorFunc error_func;
393};
394
2b76114b 395static
d3a0a0ee
VZ
396wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
397{
398 wxString ret;
399
400 if (opt->short_name)
9a83f860 401 ret << wxT("-") << opt->short_name;
d3a0a0ee
VZ
402 if (opt->long_name)
403 {
404 if (!ret.empty())
9a83f860
VZ
405 ret << wxT(", ");
406 ret << wxT("--") << opt->long_name;
d3a0a0ee
VZ
407
408 if (opt->arg_description)
9a83f860 409 ret << wxT("=") << opt->arg_description;
d3a0a0ee
VZ
410 }
411
9a83f860 412 return wxT(" ") + ret;
d3a0a0ee
VZ
413}
414
d3a0a0ee
VZ
415wxString
416wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
417 wxArrayString& desc) const
418{
419 wxString usage;
420
c49ba211
PC
421 // check whether GLib version is greater than 2.6 but also lower than 2.33
422 // because, as we use the undocumented _GOptionGroup struct, we don't want
423 // to run this code with future versions which might change it (2.32 is the
424 // latest one at the time of this writing)
425 if (glib_check_version(2,33,0))
d3a0a0ee 426 {
c49ba211 427 usage << _("The following standard GTK+ options are also supported:\n");
d3a0a0ee 428
c49ba211
PC
429 // passing true here means that the function can open the default
430 // display while parsing (not really used here anyhow)
431 GOptionGroup *gtkOpts = gtk_get_option_group(true);
d3a0a0ee 432
c49ba211
PC
433 // WARNING: here we access the internals of GOptionGroup:
434 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
435 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
436 wxArrayString namesOptions, descOptions;
d3a0a0ee 437
c49ba211
PC
438 for ( size_t n = 0; n < n_entries; n++ )
439 {
440 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
441 continue; // skip
d3a0a0ee 442
c49ba211 443 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
baa4a967 444
c49ba211
PC
445 const gchar * const entryDesc = entries[n].description;
446 desc.push_back(wxString(entryDesc));
baa4a967 447 }
c49ba211
PC
448
449 g_option_group_free (gtkOpts);
d3a0a0ee 450 }
d3a0a0ee
VZ
451
452 return usage;
453}
88bbc332 454
85b1dc6e 455#endif // __UNIX__