]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
Don't scale 16x15 to 16x16 - fixes corruption of bookmark icons in help window
[wxWidgets.git] / src / gtk / utilsgtk.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
dbd25330 2// Name: src/gtk/utilsgtk.cpp
c801d85f
KB
3// Purpose:
4// Author: Robert Roebling
dfcb1ae0 5// Id: $Id$
6c9a19aa 6// Copyright: (c) 1998 Robert Roebling
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
c801d85f 13#include "wx/utils.h"
df91131c
WS
14
15#ifndef WX_PRECOMP
16 #include "wx/string.h"
88a7a4e1 17 #include "wx/intl.h"
e4db172a 18 #include "wx/log.h"
df91131c 19#endif
c801d85f 20
46446cc2 21#include "wx/apptrait.h"
5336ece4 22#include "wx/process.h"
9d0e0377 23#include "wx/sysopt.h"
518b5d2f
VZ
24#include "wx/unix/execute.h"
25
c2ca375c 26#include "wx/gtk/private/timer.h"
b46b1d59 27#include "wx/evtloop.h"
c2ca375c 28
a481bbc3 29#include <gtk/gtk.h>
3b81515c
VZ
30#ifdef GDK_WINDOWING_WIN32
31#include <gdk/gdkwin32.h>
32#endif
33#ifdef GDK_WINDOWING_X11
a481bbc3 34#include <gdk/gdkx.h>
3b81515c 35#endif
a481bbc3 36
4b6a582b 37#if wxDEBUG_LEVEL
92696e94 38 #include "wx/gtk/assertdlg_gtk.h"
db9febdf 39 #if wxUSE_STACKWALKER
db9febdf
RR
40 #include "wx/stackwalk.h"
41 #endif // wxUSE_STACKWALKER
4b6a582b 42#endif // wxDEBUG_LEVEL
db9febdf 43
c801d85f 44#include <stdarg.h>
c801d85f
KB
45#include <string.h>
46#include <sys/stat.h>
47#include <sys/types.h>
f04f570f 48#ifdef __UNIX__
c801d85f 49#include <unistd.h>
f04f570f 50#endif
91b8de8d 51
88bbc332 52#if wxUSE_DETECT_SM
dc771347 53 #include <X11/SM/SMlib.h>
acdc8633
VZ
54
55 #include "wx/unix/utilsx11.h"
88bbc332
RR
56#endif
57
d76fe38b
RR
58//-----------------------------------------------------------------------------
59// data
60//-----------------------------------------------------------------------------
61
c2fa61e8 62extern GtkWidget *wxGetRootWindow();
d76fe38b
RR
63
64//----------------------------------------------------------------------------
c801d85f 65// misc.
d76fe38b 66//----------------------------------------------------------------------------
189d1ae7
SN
67#ifndef __EMX__
68// on OS/2, we use the wxBell from wxBase library
c801d85f 69
518b5d2f 70void wxBell()
c801d85f 71{
e52f60e6
RR
72 gdk_beep();
73}
189d1ae7 74#endif
c801d85f 75
518b5d2f
VZ
76// ----------------------------------------------------------------------------
77// display characterstics
78// ----------------------------------------------------------------------------
82052aff 79
d111a89a
VZ
80void *wxGetDisplay()
81{
27df579a 82 return GDK_DISPLAY();
d111a89a
VZ
83}
84
c0392997
RR
85void wxDisplaySize( int *width, int *height )
86{
ef26a5c4
JS
87 if (width) *width = gdk_screen_width();
88 if (height) *height = gdk_screen_height();
c0392997
RR
89}
90
904a68b6
RL
91void wxDisplaySizeMM( int *width, int *height )
92{
93 if (width) *width = gdk_screen_width_mm();
94 if (height) *height = gdk_screen_height_mm();
95}
96
6de97a3b
RR
97void wxGetMousePosition( int* x, int* y )
98{
803a1b94 99 gdk_window_get_pointer(gtk_widget_get_root_window(wxGetRootWindow()), x, y, NULL);
e52f60e6 100}
6de97a3b 101
518b5d2f 102bool wxColourDisplay()
6de97a3b 103{
df91131c 104 return true;
6de97a3b
RR
105}
106
518b5d2f 107int wxDisplayDepth()
6de97a3b 108{
385e8575 109 return gtk_widget_get_visual(wxGetRootWindow())->depth;
6de97a3b
RR
110}
111
57591e0e
JS
112wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
113{
114 return wxGenericFindWindowAtPoint(pt);
115}
116
5f11fef5
VZ
117#if !wxUSE_UNICODE
118
afb6a92f
VS
119WXDLLIMPEXP_CORE wxCharBuffer
120wxConvertToGTK(const wxString& s, wxFontEncoding enc)
5f11fef5 121{
404ac4c6
VZ
122 wxWCharBuffer wbuf;
123 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
5f11fef5 124 {
de004174 125 wbuf = wxConvUI->cMB2WC(s.c_str());
12bc5f9a 126 }
404ac4c6 127 else // another encoding, use generic conversion class
12bc5f9a 128 {
de004174 129 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
404ac4c6 130 }
12bc5f9a 131
404ac4c6
VZ
132 if ( !wbuf && !s.empty() )
133 {
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
136 //
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
de004174 140 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
27dee9ae 141 }
5f11fef5 142
404ac4c6 143 return wxConvUTF8.cWC2MB(wbuf);
5f11fef5
VZ
144}
145
afb6a92f
VS
146WXDLLIMPEXP_CORE wxCharBuffer
147wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
30083ad8
VZ
148{
149 // this conversion should never fail as GTK+ always uses UTF-8 internally
150 // so there are no complications here
de004174 151 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
30083ad8
VZ
152 if ( enc == wxFONTENCODING_SYSTEM )
153 return wxConvUI->cWC2MB(wbuf);
154
155 return wxCSConv(enc).cWC2MB(wbuf);
156}
157
5f11fef5 158#endif // !wxUSE_UNICODE
57591e0e 159
573a2a4c
VS
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
5f4d1820
VZ
164const gchar *wx_pango_version_check (int major, int minor, int micro)
165{
b5791cc7
FM
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...
03647350 169
5f4d1820
VZ
170#ifdef PANGO_VERSION_MAJOR
171 if (!gtk_check_version (2,11,0))
172 {
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);
177 }
178
179 return "can't check";
180#else // !PANGO_VERSION_MAJOR
e4161a2a
VZ
181 wxUnusedVar(major);
182 wxUnusedVar(minor);
183 wxUnusedVar(micro);
184
5f4d1820
VZ
185 return "too old headers";
186#endif
187}
188
189
518b5d2f 190// ----------------------------------------------------------------------------
c801d85f 191// subprocess routines
518b5d2f 192// ----------------------------------------------------------------------------
cf447356 193
865bb325 194extern "C" {
385e8575 195static gboolean EndProcessDetector(GIOChannel* source, GIOCondition, void* data)
cf447356 196{
b827d647 197 wxEndProcessData * const
5c33522f 198 proc_data = static_cast<wxEndProcessData *>(data);
dbd25330 199
b827d647 200 // child exited, end waiting
385e8575 201 close(g_io_channel_unix_get_fd(source));
ab857a4e 202
b827d647 203 wxHandleProcessTermination(proc_data);
385e8575
PC
204
205 // don't call us again!
206 return false;
3069ac4e 207}
865bb325 208}
cf447356 209
1d043598 210int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd)
c801d85f 211{
753cba1a
PC
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);
216 return int(id);
3069ac4e 217}
8bb6b2c0
VZ
218
219
220
221// ----------------------------------------------------------------------------
222// wxPlatformInfo-related
223// ----------------------------------------------------------------------------
224
225wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
226{
227 if ( verMaj )
228 *verMaj = gtk_major_version;
229 if ( verMin )
230 *verMin = gtk_minor_version;
231
232 return wxPORT_GTK;
233}
88bbc332 234
3c029873
VZ
235#if wxUSE_TIMER
236
c2ca375c
VZ
237wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
238{
239 return new wxGTKTimerImpl(timer);
240}
241
3c029873
VZ
242#endif // wxUSE_TIMER
243
88bbc332
RR
244#if wxUSE_DETECT_SM
245static wxString GetSM()
246{
acdc8633 247 wxX11Display dpy;
391bf008
VZ
248 if ( !dpy )
249 return wxEmptyString;
250
dee28fdd 251 char smerr[256];
391bf008
VZ
252 char *client_id;
253 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
254 999, 999,
255 0 /* mask */, NULL /* callbacks */,
256 NULL, &client_id,
dee28fdd 257 WXSIZEOF(smerr), smerr);
391bf008
VZ
258
259 if ( !smc_conn )
dee28fdd 260 {
366721d7 261 wxLogDebug("Failed to connect to session manager: %s", smerr);
391bf008 262 return wxEmptyString;
dee28fdd 263 }
391bf008
VZ
264
265 char *vendor = SmcVendor(smc_conn);
266 wxString ret = wxString::FromAscii( vendor );
267 free(vendor);
268
269 SmcCloseConnection(smc_conn, 0, NULL);
270 free(client_id);
271
272 return ret;
88bbc332 273}
391bf008 274#endif // wxUSE_DETECT_SM
88bbc332 275
db9febdf
RR
276
277//-----------------------------------------------------------------------------
278// wxGUIAppTraits
279//-----------------------------------------------------------------------------
280
2ddff00c 281wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
282{
283 return new wxEventLoop();
284}
285
286
6c4f5ea5 287#if wxUSE_INTL
d774f916
VZ
288void wxGUIAppTraits::SetLocale()
289{
290 gtk_set_locale();
cb352236 291 wxUpdateLocaleIsUtf8();
d774f916 292}
6c4f5ea5 293#endif
d774f916 294
4b6a582b 295#if wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
296
297// private helper class
298class StackDump : public wxStackWalker
299{
300public:
301 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
302
303protected:
304 virtual void OnStackFrame(const wxStackFrame& frame)
305 {
306 wxString fncname = frame.GetName();
307 wxString fncargs = fncname;
308
309 size_t n = fncname.find(wxT('('));
310 if (n != wxString::npos)
311 {
312 // remove arguments from function name
313 fncname.erase(n);
314
315 // remove function name and brackets from arguments
316 fncargs = fncargs.substr(n+1, fncargs.length()-n-2);
317 }
318 else
319 fncargs = wxEmptyString;
320
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,
324 fncname.mb_str(),
325 fncargs.mb_str(),
326 frame.GetFileName().mb_str(),
327 frame.GetLine());
328 }
329
330private:
331 GtkAssertDialog *m_dlg;
332};
333
a481bbc3 334static void get_stackframe_callback(void* p)
db9febdf 335{
a481bbc3
PC
336 StackDump* dump = static_cast<StackDump*>(p);
337 // skip over frames up to including wxOnAssert()
338 dump->ProcessFrames(3);
db9febdf
RR
339}
340
4b6a582b 341#endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
342
343bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
344{
4b6a582b 345#if wxDEBUG_LEVEL
2d8e0096
VZ
346 // we can't show the dialog from another thread
347 if ( wxIsMainThread() )
348 {
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());
db9febdf
RR
355
356#if wxUSE_STACKWALKER
61c18e1a 357 // save the current stack ow...
2d8e0096 358 StackDump dump(GTK_ASSERT_DIALOG(dialog));
61c18e1a 359 dump.SaveStack(100); // showing more than 100 frames is not very useful
2d8e0096
VZ
360
361 // ...but process it only if the user needs it
362 gtk_assert_dialog_set_backtrace_callback
363 (
364 GTK_ASSERT_DIALOG(dialog),
a481bbc3 365 get_stackframe_callback,
2d8e0096
VZ
366 &dump
367 );
368#endif // wxUSE_STACKWALKER
369
370 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
371 bool returnCode = false;
372 switch (result)
373 {
374 case GTK_ASSERT_DIALOG_STOP:
375 wxTrap();
376 break;
377 case GTK_ASSERT_DIALOG_CONTINUE:
378 // nothing to do
379 break;
380 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
381 // no more asserts
382 returnCode = true;
383 break;
384
385 default:
386 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
387 }
388
389 gtk_widget_destroy(dialog);
390 return returnCode;
db9febdf 391 }
2d8e0096 392#endif // wxDEBUG_LEVEL
db9febdf 393
2d8e0096 394 return wxAppTraitsBase::ShowAssertDialog(msg);
db9febdf
RR
395}
396
88bbc332
RR
397wxString wxGUIAppTraits::GetDesktopEnvironment() const
398{
9a83f860 399 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
3c029873 400#if wxUSE_DETECT_SM
9d0e0377
VZ
401 if ( de.empty() )
402 {
9d0e0377 403 static const wxString s_SM = GetSM();
391bf008 404
9d0e0377
VZ
405 if (s_SM == wxT("GnomeSM"))
406 de = wxT("GNOME");
407 else if (s_SM == wxT("KDE"))
408 de = wxT("KDE");
409 }
391bf008 410#endif // wxUSE_DETECT_SM
88bbc332 411
9d0e0377 412 return de;
88bbc332
RR
413}
414
d3a0a0ee 415#ifdef __WXGTK26__
88bbc332 416
d3a0a0ee
VZ
417// see the hack below in wxCmdLineParser::GetUsageString().
418// TODO: replace this hack with a g_option_group_get_entries()
baa4a967
FM
419// call as soon as such function exists;
420// see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
421// feature request
d3a0a0ee
VZ
422struct _GOptionGroup
423{
424 gchar *name;
425 gchar *description;
426 gchar *help_description;
427
428 GDestroyNotify destroy_notify;
429 gpointer user_data;
430
431 GTranslateFunc translate_func;
432 GDestroyNotify translate_notify;
433 gpointer translate_data;
434
435 GOptionEntry *entries;
436 gint n_entries;
437
438 GOptionParseFunc pre_parse_func;
439 GOptionParseFunc post_parse_func;
440 GOptionErrorFunc error_func;
441};
442
443wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
444{
445 wxString ret;
446
447 if (opt->short_name)
9a83f860 448 ret << wxT("-") << opt->short_name;
d3a0a0ee
VZ
449 if (opt->long_name)
450 {
451 if (!ret.empty())
9a83f860
VZ
452 ret << wxT(", ");
453 ret << wxT("--") << opt->long_name;
d3a0a0ee
VZ
454
455 if (opt->arg_description)
9a83f860 456 ret << wxT("=") << opt->arg_description;
d3a0a0ee
VZ
457 }
458
9a83f860 459 return wxT(" ") + ret;
d3a0a0ee
VZ
460}
461
462#endif // __WXGTK26__
463
464wxString
465wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
466 wxArrayString& desc) const
467{
468 wxString usage;
469
470#ifdef __WXGTK26__
baa4a967 471 if (!gtk_check_version(2,6,0))
d3a0a0ee 472 {
baa4a967 473 // since GTK>=2.6, we can use the glib_check_version() symbol...
d3a0a0ee 474
8f81f01d 475 // check whether GLib version is greater than 2.6 but also lower than 2.31
baa4a967 476 // because, as we use the undocumented _GOptionGroup struct, we don't want
8f81f01d 477 // to run this code with future versions which might change it (2.30 is the
baa4a967 478 // latest one at the time of this writing)
8f81f01d 479 if (glib_check_version(2,6,0) == NULL && glib_check_version(2,31,0))
baa4a967
FM
480 {
481 usage << _("The following standard GTK+ options are also supported:\n");
d3a0a0ee 482
baa4a967
FM
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);
d3a0a0ee 486
baa4a967
FM
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;
d3a0a0ee 491
baa4a967
FM
492 for ( size_t n = 0; n < n_entries; n++ )
493 {
494 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
495 continue; // skip
d3a0a0ee 496
baa4a967 497 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
d3a0a0ee 498
baa4a967
FM
499 const gchar * const entryDesc = entries[n].description;
500 desc.push_back(wxString(entryDesc));
501 }
502
503 g_option_group_free (gtkOpts);
504 }
d3a0a0ee
VZ
505 }
506#else
507 wxUnusedVar(names);
508 wxUnusedVar(desc);
509#endif // __WXGTK26__
510
511 return usage;
512}
88bbc332 513