]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
Correctly initialize wxGtkPrintNativeData members.
[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
4b6a582b 29#if wxDEBUG_LEVEL
92696e94 30 #include "wx/gtk/assertdlg_gtk.h"
db9febdf 31 #if wxUSE_STACKWALKER
db9febdf
RR
32 #include "wx/stackwalk.h"
33 #endif // wxUSE_STACKWALKER
4b6a582b 34#endif // wxDEBUG_LEVEL
db9febdf 35
c801d85f 36#include <stdarg.h>
c801d85f
KB
37#include <string.h>
38#include <sys/stat.h>
39#include <sys/types.h>
dbd25330 40#include <sys/wait.h> // for WNOHANG
c801d85f 41#include <unistd.h>
91b8de8d
RR
42
43#include "glib.h"
44#include "gdk/gdk.h"
45#include "gtk/gtk.h"
91b8de8d 46#include "gdk/gdkx.h"
88ac883a 47
88bbc332
RR
48#if wxUSE_DETECT_SM
49 #include "X11/Xlib.h"
50 #include "X11/SM/SMlib.h"
acdc8633
VZ
51
52 #include "wx/unix/utilsx11.h"
88bbc332
RR
53#endif
54
d76fe38b
RR
55//-----------------------------------------------------------------------------
56// data
57//-----------------------------------------------------------------------------
58
c2fa61e8 59extern GtkWidget *wxGetRootWindow();
d76fe38b
RR
60
61//----------------------------------------------------------------------------
c801d85f 62// misc.
d76fe38b 63//----------------------------------------------------------------------------
189d1ae7
SN
64#ifndef __EMX__
65// on OS/2, we use the wxBell from wxBase library
c801d85f 66
518b5d2f 67void wxBell()
c801d85f 68{
e52f60e6
RR
69 gdk_beep();
70}
189d1ae7 71#endif
c801d85f 72
518b5d2f
VZ
73// ----------------------------------------------------------------------------
74// display characterstics
75// ----------------------------------------------------------------------------
82052aff 76
d111a89a
VZ
77void *wxGetDisplay()
78{
27df579a 79 return GDK_DISPLAY();
d111a89a
VZ
80}
81
c0392997
RR
82void wxDisplaySize( int *width, int *height )
83{
ef26a5c4
JS
84 if (width) *width = gdk_screen_width();
85 if (height) *height = gdk_screen_height();
c0392997
RR
86}
87
904a68b6
RL
88void wxDisplaySizeMM( int *width, int *height )
89{
90 if (width) *width = gdk_screen_width_mm();
91 if (height) *height = gdk_screen_height_mm();
92}
93
6de97a3b
RR
94void wxGetMousePosition( int* x, int* y )
95{
d3b9f782 96 gdk_window_get_pointer( NULL, x, y, NULL );
e52f60e6 97}
6de97a3b 98
518b5d2f 99bool wxColourDisplay()
6de97a3b 100{
df91131c 101 return true;
6de97a3b
RR
102}
103
518b5d2f 104int wxDisplayDepth()
6de97a3b 105{
22a3bce4 106 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
6de97a3b
RR
107}
108
57591e0e
JS
109wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
110{
111 return wxGenericFindWindowAtPoint(pt);
112}
113
5f11fef5
VZ
114#if !wxUSE_UNICODE
115
afb6a92f
VS
116WXDLLIMPEXP_CORE wxCharBuffer
117wxConvertToGTK(const wxString& s, wxFontEncoding enc)
5f11fef5 118{
dd5ab30d
VZ
119 // Passing an empty string to cMB2WC() returns an invalid buffer, i.e. a
120 // buffer whose data is NULL and this can result in passing NULL to a GTK+
121 // function and a crash, so handle this case specially to avoid this.
122 if ( s.empty() )
123 return wxCharBuffer("");
124
404ac4c6
VZ
125 wxWCharBuffer wbuf;
126 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
5f11fef5 127 {
de004174 128 wbuf = wxConvUI->cMB2WC(s.c_str());
12bc5f9a 129 }
404ac4c6 130 else // another encoding, use generic conversion class
12bc5f9a 131 {
de004174 132 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
404ac4c6 133 }
12bc5f9a 134
404ac4c6
VZ
135 if ( !wbuf && !s.empty() )
136 {
137 // conversion failed, but we still want to show something to the user
138 // even if it's going to be wrong it is better than nothing
139 //
140 // we choose ISO8859-1 here arbitrarily, it's just the most common
141 // encoding probably and, also importantly here, conversion from it
142 // never fails as it's done internally by wxCSConv
de004174 143 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
27dee9ae 144 }
5f11fef5 145
404ac4c6 146 return wxConvUTF8.cWC2MB(wbuf);
5f11fef5
VZ
147}
148
afb6a92f
VS
149WXDLLIMPEXP_CORE wxCharBuffer
150wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
30083ad8
VZ
151{
152 // this conversion should never fail as GTK+ always uses UTF-8 internally
153 // so there are no complications here
de004174 154 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
30083ad8
VZ
155 if ( enc == wxFONTENCODING_SYSTEM )
156 return wxConvUI->cWC2MB(wbuf);
157
158 return wxCSConv(enc).cWC2MB(wbuf);
159}
160
5f11fef5 161#endif // !wxUSE_UNICODE
57591e0e 162
573a2a4c
VS
163// Returns NULL if version is certainly greater or equal than major.minor.micro
164// Returns string describing the error if version is lower than
165// major.minor.micro OR it cannot be determined and one should not rely on the
166// availability of pango version major.minor.micro, nor the non-availability
5f4d1820
VZ
167const gchar *wx_pango_version_check (int major, int minor, int micro)
168{
b5791cc7
FM
169 // NOTE: you don't need to use this macro to check for Pango features
170 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
171 // for GTK lib) required pango 1.4...
03647350 172
5f4d1820
VZ
173#ifdef PANGO_VERSION_MAJOR
174 if (!gtk_check_version (2,11,0))
175 {
176 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
177 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
178 // using has the pango_version_check function:
179 return pango_version_check (major, minor, micro);
180 }
181
182 return "can't check";
183#else // !PANGO_VERSION_MAJOR
e4161a2a
VZ
184 wxUnusedVar(major);
185 wxUnusedVar(minor);
186 wxUnusedVar(micro);
187
5f4d1820
VZ
188 return "too old headers";
189#endif
190}
191
192
518b5d2f 193// ----------------------------------------------------------------------------
c801d85f 194// subprocess routines
518b5d2f 195// ----------------------------------------------------------------------------
cf447356 196
865bb325
VZ
197extern "C" {
198static
90350682 199void GTK_EndProcessDetector(gpointer data, gint source,
b827d647 200 GdkInputCondition WXUNUSED(condition))
cf447356 201{
b827d647 202 wxEndProcessData * const
5c33522f 203 proc_data = static_cast<wxEndProcessData *>(data);
dbd25330 204
b827d647
VZ
205 // child exited, end waiting
206 close(source);
447f908a 207
b827d647
VZ
208 // don't call us again!
209 gdk_input_remove(proc_data->tag);
ab857a4e 210
b827d647 211 wxHandleProcessTermination(proc_data);
3069ac4e 212}
865bb325 213}
cf447356 214
1d043598 215int wxGUIAppTraits::AddProcessCallback(wxEndProcessData *proc_data, int fd)
c801d85f 216{
518b5d2f
VZ
217 int tag = gdk_input_add(fd,
218 GDK_INPUT_READ,
219 GTK_EndProcessDetector,
220 (gpointer)proc_data);
c801d85f 221
518b5d2f 222 return tag;
3069ac4e 223}
8bb6b2c0
VZ
224
225
226
227// ----------------------------------------------------------------------------
228// wxPlatformInfo-related
229// ----------------------------------------------------------------------------
230
231wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
232{
233 if ( verMaj )
234 *verMaj = gtk_major_version;
235 if ( verMin )
236 *verMin = gtk_minor_version;
237
238 return wxPORT_GTK;
239}
88bbc332 240
3c029873
VZ
241#if wxUSE_TIMER
242
c2ca375c
VZ
243wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
244{
245 return new wxGTKTimerImpl(timer);
246}
247
3c029873
VZ
248#endif // wxUSE_TIMER
249
88bbc332
RR
250#if wxUSE_DETECT_SM
251static wxString GetSM()
252{
acdc8633 253 wxX11Display dpy;
391bf008
VZ
254 if ( !dpy )
255 return wxEmptyString;
256
dee28fdd 257 char smerr[256];
391bf008
VZ
258 char *client_id;
259 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
260 999, 999,
261 0 /* mask */, NULL /* callbacks */,
262 NULL, &client_id,
dee28fdd 263 WXSIZEOF(smerr), smerr);
391bf008
VZ
264
265 if ( !smc_conn )
dee28fdd 266 {
366721d7 267 wxLogDebug("Failed to connect to session manager: %s", smerr);
391bf008 268 return wxEmptyString;
dee28fdd 269 }
391bf008
VZ
270
271 char *vendor = SmcVendor(smc_conn);
272 wxString ret = wxString::FromAscii( vendor );
273 free(vendor);
274
275 SmcCloseConnection(smc_conn, 0, NULL);
276 free(client_id);
277
278 return ret;
88bbc332 279}
391bf008 280#endif // wxUSE_DETECT_SM
88bbc332 281
db9febdf
RR
282
283//-----------------------------------------------------------------------------
284// wxGUIAppTraits
285//-----------------------------------------------------------------------------
286
2ddff00c 287wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
288{
289 return new wxEventLoop();
290}
291
292
6c4f5ea5 293#if wxUSE_INTL
d774f916
VZ
294void wxGUIAppTraits::SetLocale()
295{
296 gtk_set_locale();
cb352236 297 wxUpdateLocaleIsUtf8();
d774f916 298}
6c4f5ea5 299#endif
d774f916 300
4b6a582b 301#if wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
302
303// private helper class
304class StackDump : public wxStackWalker
305{
306public:
307 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
308
309protected:
310 virtual void OnStackFrame(const wxStackFrame& frame)
311 {
312 wxString fncname = frame.GetName();
313 wxString fncargs = fncname;
314
315 size_t n = fncname.find(wxT('('));
316 if (n != wxString::npos)
317 {
318 // remove arguments from function name
319 fncname.erase(n);
320
321 // remove function name and brackets from arguments
322 fncargs = fncargs.substr(n+1, fncargs.length()-n-2);
323 }
324 else
325 fncargs = wxEmptyString;
326
327 // append this stack frame's info in the dialog
328 if (!frame.GetFileName().empty() || !fncname.empty())
329 gtk_assert_dialog_append_stack_frame(m_dlg,
330 fncname.mb_str(),
331 fncargs.mb_str(),
332 frame.GetFileName().mb_str(),
333 frame.GetLine());
334 }
335
336private:
337 GtkAssertDialog *m_dlg;
338};
339
340// the callback functions must be extern "C" to comply with GTK+ declarations
341extern "C"
342{
343 void get_stackframe_callback(StackDump *dump)
344 {
62710178
VZ
345 // skip over frames up to including wxOnAssert()
346 dump->ProcessFrames(3);
db9febdf
RR
347 }
348}
349
4b6a582b 350#endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
db9febdf
RR
351
352bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
353{
4b6a582b 354#if wxDEBUG_LEVEL
2d8e0096
VZ
355 // we can't show the dialog from another thread
356 if ( wxIsMainThread() )
357 {
358 // under GTK2 we prefer to use a dialog widget written using directly
359 // in GTK+ as use a dialog written using wxWidgets would need the
360 // wxWidgets idle processing to work correctly which might not be the
361 // case when assert happens
362 GtkWidget *dialog = gtk_assert_dialog_new();
363 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
db9febdf
RR
364
365#if wxUSE_STACKWALKER
61c18e1a 366 // save the current stack ow...
2d8e0096 367 StackDump dump(GTK_ASSERT_DIALOG(dialog));
61c18e1a 368 dump.SaveStack(100); // showing more than 100 frames is not very useful
2d8e0096
VZ
369
370 // ...but process it only if the user needs it
371 gtk_assert_dialog_set_backtrace_callback
372 (
373 GTK_ASSERT_DIALOG(dialog),
374 (GtkAssertDialogStackFrameCallback)get_stackframe_callback,
375 &dump
376 );
377#endif // wxUSE_STACKWALKER
378
379 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
380 bool returnCode = false;
381 switch (result)
382 {
383 case GTK_ASSERT_DIALOG_STOP:
384 wxTrap();
385 break;
386 case GTK_ASSERT_DIALOG_CONTINUE:
387 // nothing to do
388 break;
389 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
390 // no more asserts
391 returnCode = true;
392 break;
393
394 default:
395 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
396 }
397
398 gtk_widget_destroy(dialog);
399 return returnCode;
db9febdf 400 }
2d8e0096 401#endif // wxDEBUG_LEVEL
db9febdf 402
2d8e0096 403 return wxAppTraitsBase::ShowAssertDialog(msg);
db9febdf
RR
404}
405
88bbc332
RR
406wxString wxGUIAppTraits::GetDesktopEnvironment() const
407{
9a83f860 408 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
3c029873 409#if wxUSE_DETECT_SM
9d0e0377
VZ
410 if ( de.empty() )
411 {
9d0e0377 412 static const wxString s_SM = GetSM();
391bf008 413
9d0e0377
VZ
414 if (s_SM == wxT("GnomeSM"))
415 de = wxT("GNOME");
416 else if (s_SM == wxT("KDE"))
417 de = wxT("KDE");
418 }
391bf008 419#endif // wxUSE_DETECT_SM
88bbc332 420
9d0e0377 421 return de;
88bbc332
RR
422}
423
d3a0a0ee 424#ifdef __WXGTK26__
88bbc332 425
d3a0a0ee
VZ
426// see the hack below in wxCmdLineParser::GetUsageString().
427// TODO: replace this hack with a g_option_group_get_entries()
baa4a967
FM
428// call as soon as such function exists;
429// see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
430// feature request
d3a0a0ee
VZ
431struct _GOptionGroup
432{
433 gchar *name;
434 gchar *description;
435 gchar *help_description;
436
437 GDestroyNotify destroy_notify;
438 gpointer user_data;
439
440 GTranslateFunc translate_func;
441 GDestroyNotify translate_notify;
442 gpointer translate_data;
443
444 GOptionEntry *entries;
445 gint n_entries;
446
447 GOptionParseFunc pre_parse_func;
448 GOptionParseFunc post_parse_func;
449 GOptionErrorFunc error_func;
450};
451
452wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
453{
454 wxString ret;
455
456 if (opt->short_name)
9a83f860 457 ret << wxT("-") << opt->short_name;
d3a0a0ee
VZ
458 if (opt->long_name)
459 {
460 if (!ret.empty())
9a83f860
VZ
461 ret << wxT(", ");
462 ret << wxT("--") << opt->long_name;
d3a0a0ee
VZ
463
464 if (opt->arg_description)
9a83f860 465 ret << wxT("=") << opt->arg_description;
d3a0a0ee
VZ
466 }
467
9a83f860 468 return wxT(" ") + ret;
d3a0a0ee
VZ
469}
470
471#endif // __WXGTK26__
472
473wxString
474wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
475 wxArrayString& desc) const
476{
477 wxString usage;
478
479#ifdef __WXGTK26__
baa4a967 480 if (!gtk_check_version(2,6,0))
d3a0a0ee 481 {
baa4a967 482 // since GTK>=2.6, we can use the glib_check_version() symbol...
d3a0a0ee 483
baa4a967
FM
484 // check whether GLib version is greater than 2.6 but also lower than 2.19
485 // because, as we use the undocumented _GOptionGroup struct, we don't want
486 // to run this code with future versions which might change it (2.19 is the
487 // latest one at the time of this writing)
3fb2bf54 488 if (!glib_check_version(2,6,0) && glib_check_version(2,20,0))
baa4a967
FM
489 {
490 usage << _("The following standard GTK+ options are also supported:\n");
d3a0a0ee 491
baa4a967
FM
492 // passing true here means that the function can open the default
493 // display while parsing (not really used here anyhow)
494 GOptionGroup *gtkOpts = gtk_get_option_group(true);
d3a0a0ee 495
baa4a967
FM
496 // WARNING: here we access the internals of GOptionGroup:
497 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
498 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
499 wxArrayString namesOptions, descOptions;
d3a0a0ee 500
baa4a967
FM
501 for ( size_t n = 0; n < n_entries; n++ )
502 {
503 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
504 continue; // skip
d3a0a0ee 505
baa4a967 506 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
d3a0a0ee 507
baa4a967
FM
508 const gchar * const entryDesc = entries[n].description;
509 desc.push_back(wxString(entryDesc));
510 }
511
512 g_option_group_free (gtkOpts);
513 }
d3a0a0ee
VZ
514 }
515#else
516 wxUnusedVar(names);
517 wxUnusedVar(desc);
518#endif // __WXGTK26__
519
520 return usage;
521}
88bbc332 522