]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
update GTK size hints when window decorations change
[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
db9febdf 29#ifdef __WXDEBUG__
92696e94 30 #include "wx/gtk/assertdlg_gtk.h"
db9febdf 31 #if wxUSE_STACKWALKER
db9febdf
RR
32 #include "wx/stackwalk.h"
33 #endif // wxUSE_STACKWALKER
34#endif // __WXDEBUG__
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
27933891 48#ifdef HAVE_X11_XKBLIB_H
154c4aa1
VZ
49 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
50 * field named "explicit" - which is, of course, an error for a C++
51 * compiler. To be on the safe side, just redefine it everywhere. */
88ac883a 52 #define explicit __wx_explicit
ec5d7799 53
154c4aa1 54 #include "X11/XKBlib.h"
ec5d7799 55
88ac883a 56 #undef explicit
27933891 57#endif // HAVE_X11_XKBLIB_H
dfcb1ae0 58
88bbc332
RR
59
60#if wxUSE_DETECT_SM
61 #include "X11/Xlib.h"
62 #include "X11/SM/SMlib.h"
63#endif
64
d76fe38b
RR
65//-----------------------------------------------------------------------------
66// data
67//-----------------------------------------------------------------------------
68
c2fa61e8 69extern GtkWidget *wxGetRootWindow();
d76fe38b
RR
70
71//----------------------------------------------------------------------------
c801d85f 72// misc.
d76fe38b 73//----------------------------------------------------------------------------
189d1ae7
SN
74#ifndef __EMX__
75// on OS/2, we use the wxBell from wxBase library
c801d85f 76
518b5d2f 77void wxBell()
c801d85f 78{
e52f60e6
RR
79 gdk_beep();
80}
189d1ae7 81#endif
c801d85f 82
27933891
RR
83/* Don't synthesize KeyUp events holding down a key and producing
84 KeyDown events with autorepeat. */
85#ifdef HAVE_X11_XKBLIB_H
f0492f7d
RR
86bool wxSetDetectableAutoRepeat( bool flag )
87{
88 Bool result;
89 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag, &result );
df91131c 90 return result; /* true if keyboard hardware supports this mode */
27933891
RR
91}
92#else
93bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
94{
df91131c 95 return false;
f0492f7d 96}
27933891 97#endif
f0492f7d 98
518b5d2f
VZ
99// ----------------------------------------------------------------------------
100// display characterstics
101// ----------------------------------------------------------------------------
82052aff 102
d111a89a
VZ
103void *wxGetDisplay()
104{
27df579a 105 return GDK_DISPLAY();
d111a89a
VZ
106}
107
c0392997
RR
108void wxDisplaySize( int *width, int *height )
109{
ef26a5c4
JS
110 if (width) *width = gdk_screen_width();
111 if (height) *height = gdk_screen_height();
c0392997
RR
112}
113
904a68b6
RL
114void wxDisplaySizeMM( int *width, int *height )
115{
116 if (width) *width = gdk_screen_width_mm();
117 if (height) *height = gdk_screen_height_mm();
118}
119
6de97a3b
RR
120void wxGetMousePosition( int* x, int* y )
121{
bbe0af5b 122 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
e52f60e6 123}
6de97a3b 124
518b5d2f 125bool wxColourDisplay()
6de97a3b 126{
df91131c 127 return true;
6de97a3b
RR
128}
129
518b5d2f 130int wxDisplayDepth()
6de97a3b 131{
22a3bce4 132 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
6de97a3b
RR
133}
134
57591e0e
JS
135wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
136{
137 return wxGenericFindWindowAtPoint(pt);
138}
139
5f11fef5
VZ
140#if !wxUSE_UNICODE
141
afb6a92f
VS
142WXDLLIMPEXP_CORE wxCharBuffer
143wxConvertToGTK(const wxString& s, wxFontEncoding enc)
5f11fef5 144{
404ac4c6
VZ
145 wxWCharBuffer wbuf;
146 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
5f11fef5 147 {
de004174 148 wbuf = wxConvUI->cMB2WC(s.c_str());
12bc5f9a 149 }
404ac4c6 150 else // another encoding, use generic conversion class
12bc5f9a 151 {
de004174 152 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
404ac4c6 153 }
12bc5f9a 154
404ac4c6
VZ
155 if ( !wbuf && !s.empty() )
156 {
157 // conversion failed, but we still want to show something to the user
158 // even if it's going to be wrong it is better than nothing
159 //
160 // we choose ISO8859-1 here arbitrarily, it's just the most common
161 // encoding probably and, also importantly here, conversion from it
162 // never fails as it's done internally by wxCSConv
de004174 163 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
27dee9ae 164 }
5f11fef5 165
404ac4c6 166 return wxConvUTF8.cWC2MB(wbuf);
5f11fef5
VZ
167}
168
afb6a92f
VS
169WXDLLIMPEXP_CORE wxCharBuffer
170wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
30083ad8
VZ
171{
172 // this conversion should never fail as GTK+ always uses UTF-8 internally
173 // so there are no complications here
de004174 174 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
30083ad8
VZ
175 if ( enc == wxFONTENCODING_SYSTEM )
176 return wxConvUI->cWC2MB(wbuf);
177
178 return wxCSConv(enc).cWC2MB(wbuf);
179}
180
5f11fef5 181#endif // !wxUSE_UNICODE
57591e0e 182
573a2a4c
VS
183// Returns NULL if version is certainly greater or equal than major.minor.micro
184// Returns string describing the error if version is lower than
185// major.minor.micro OR it cannot be determined and one should not rely on the
186// availability of pango version major.minor.micro, nor the non-availability
5f4d1820
VZ
187const gchar *wx_pango_version_check (int major, int minor, int micro)
188{
189#ifdef PANGO_VERSION_MAJOR
190 if (!gtk_check_version (2,11,0))
191 {
192 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
193 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
194 // using has the pango_version_check function:
195 return pango_version_check (major, minor, micro);
196 }
197
198 return "can't check";
199#else // !PANGO_VERSION_MAJOR
e4161a2a
VZ
200 wxUnusedVar(major);
201 wxUnusedVar(minor);
202 wxUnusedVar(micro);
203
5f4d1820
VZ
204 return "too old headers";
205#endif
206}
207
208
518b5d2f 209// ----------------------------------------------------------------------------
c801d85f 210// subprocess routines
518b5d2f 211// ----------------------------------------------------------------------------
cf447356 212
865bb325
VZ
213extern "C" {
214static
90350682
VZ
215void GTK_EndProcessDetector(gpointer data, gint source,
216 GdkInputCondition WXUNUSED(condition) )
cf447356 217{
ab857a4e 218 wxEndProcessData *proc_data = (wxEndProcessData *)data;
dbd25330
VZ
219
220 // has the process really terminated? unfortunately GDK (or GLib) seem to
221 // generate G_IO_HUP notification even when it simply tries to read from a
222 // closed fd and hasn't terminated at all
223 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
447f908a
VZ
224 int status = 0;
225 int rc = waitpid(pid, &status, WNOHANG);
226
56465170
VS
227 if ( rc == 0 )
228 {
229 // This can only happen if the child application closes our dummy
230 // pipe that is used to monitor its lifetime; in that case, our
231 // best bet is to pretend the process did terminate, because
232 // otherwise wxExecute() would hang indefinitely
233 // (OnExceptionWaiting() won't be called again, the descriptor
234 // is closed now).
235 wxLogDebug("Child process (PID %i) still alive, even though notification was received that it terminated.", pid);
236 }
237 else if ( rc == -1 )
238 {
239 // As above, if waitpid() fails, the best we can do is to log the
240 // error and pretend the child terminated:
241 wxLogSysError(_("Failed to check child process' status"));
242 }
dbd25330 243
447f908a 244 // set exit code to -1 if something bad happened
56465170
VS
245 proc_data->exitcode = (rc > 0 && WIFEXITED(status))
246 ? WEXITSTATUS(status)
247 : -1;
447f908a 248
dbd25330 249 // child exited, end waiting
ab857a4e 250 close(source);
dbd25330
VZ
251
252 // don't call us again!
ab857a4e
KB
253 gdk_input_remove(proc_data->tag);
254
ab857a4e 255 wxHandleProcessTermination(proc_data);
3069ac4e 256}
865bb325 257}
cf447356 258
518b5d2f 259int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
c801d85f 260{
518b5d2f
VZ
261 int tag = gdk_input_add(fd,
262 GDK_INPUT_READ,
263 GTK_EndProcessDetector,
264 (gpointer)proc_data);
c801d85f 265
518b5d2f 266 return tag;
3069ac4e 267}
8bb6b2c0
VZ
268
269
270
271// ----------------------------------------------------------------------------
272// wxPlatformInfo-related
273// ----------------------------------------------------------------------------
274
275wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
276{
277 if ( verMaj )
278 *verMaj = gtk_major_version;
279 if ( verMin )
280 *verMin = gtk_minor_version;
281
282 return wxPORT_GTK;
283}
88bbc332 284
3c029873
VZ
285#if wxUSE_TIMER
286
c2ca375c
VZ
287wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
288{
289 return new wxGTKTimerImpl(timer);
290}
291
3c029873
VZ
292#endif // wxUSE_TIMER
293
88bbc332
RR
294#if wxUSE_DETECT_SM
295static wxString GetSM()
296{
391bf008 297 class Dpy
cb1bf052 298 {
391bf008
VZ
299 public:
300 Dpy() { m_dpy = XOpenDisplay(NULL); }
301 ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); }
88bbc332 302
391bf008
VZ
303 operator Display *() const { return m_dpy; }
304 private:
305 Display *m_dpy;
306 } dpy;
88bbc332 307
391bf008
VZ
308 if ( !dpy )
309 return wxEmptyString;
310
dee28fdd 311 char smerr[256];
391bf008
VZ
312 char *client_id;
313 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
314 999, 999,
315 0 /* mask */, NULL /* callbacks */,
316 NULL, &client_id,
dee28fdd 317 WXSIZEOF(smerr), smerr);
391bf008
VZ
318
319 if ( !smc_conn )
dee28fdd
VZ
320 {
321 wxLogWarning(_("Failed to connect to session manager: %s"), smerr);
391bf008 322 return wxEmptyString;
dee28fdd 323 }
391bf008
VZ
324
325 char *vendor = SmcVendor(smc_conn);
326 wxString ret = wxString::FromAscii( vendor );
327 free(vendor);
328
329 SmcCloseConnection(smc_conn, 0, NULL);
330 free(client_id);
331
332 return ret;
88bbc332 333}
391bf008 334#endif // wxUSE_DETECT_SM
88bbc332 335
db9febdf
RR
336
337//-----------------------------------------------------------------------------
338// wxGUIAppTraits
339//-----------------------------------------------------------------------------
340
2ddff00c 341wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
342{
343 return new wxEventLoop();
344}
345
346
6c4f5ea5 347#if wxUSE_INTL
d774f916
VZ
348void wxGUIAppTraits::SetLocale()
349{
350 gtk_set_locale();
cb352236 351 wxUpdateLocaleIsUtf8();
d774f916 352}
6c4f5ea5 353#endif
d774f916 354
db9febdf
RR
355#ifdef __WXDEBUG__
356
357#if wxUSE_STACKWALKER
358
359// private helper class
360class StackDump : public wxStackWalker
361{
362public:
363 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
364
365protected:
366 virtual void OnStackFrame(const wxStackFrame& frame)
367 {
368 wxString fncname = frame.GetName();
369 wxString fncargs = fncname;
370
371 size_t n = fncname.find(wxT('('));
372 if (n != wxString::npos)
373 {
374 // remove arguments from function name
375 fncname.erase(n);
376
377 // remove function name and brackets from arguments
378 fncargs = fncargs.substr(n+1, fncargs.length()-n-2);
379 }
380 else
381 fncargs = wxEmptyString;
382
383 // append this stack frame's info in the dialog
384 if (!frame.GetFileName().empty() || !fncname.empty())
385 gtk_assert_dialog_append_stack_frame(m_dlg,
386 fncname.mb_str(),
387 fncargs.mb_str(),
388 frame.GetFileName().mb_str(),
389 frame.GetLine());
390 }
391
392private:
393 GtkAssertDialog *m_dlg;
394};
395
396// the callback functions must be extern "C" to comply with GTK+ declarations
397extern "C"
398{
399 void get_stackframe_callback(StackDump *dump)
400 {
62710178
VZ
401 // skip over frames up to including wxOnAssert()
402 dump->ProcessFrames(3);
db9febdf
RR
403 }
404}
405
406#endif // wxUSE_STACKWALKER
407
408bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
409{
410 // under GTK2 we prefer to use a dialog widget written using directly GTK+;
411 // in fact we cannot use a dialog written using wxWidgets: it would need
412 // the wxWidgets idle processing to work correctly!
413 GtkWidget *dialog = gtk_assert_dialog_new();
414 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
415
416#if wxUSE_STACKWALKER
417 // don't show more than maxLines or we could get a dialog too tall to be
418 // shown on screen: 20 should be ok everywhere as even with 15 pixel high
419 // characters it is still only 300 pixels...
420 static const int maxLines = 20;
421
422 // save current stack frame...
423 StackDump dump(GTK_ASSERT_DIALOG(dialog));
424 dump.SaveStack(maxLines);
425
426 // ...but process it only if the user needs it
427 gtk_assert_dialog_set_backtrace_callback(GTK_ASSERT_DIALOG(dialog),
428 (GtkAssertDialogStackFrameCallback)get_stackframe_callback,
429 &dump);
430#endif // wxUSE_STACKWALKER
431
432 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
433 bool returnCode = false;
434 switch (result)
435 {
436 case GTK_ASSERT_DIALOG_STOP:
437 wxTrap();
438 break;
439 case GTK_ASSERT_DIALOG_CONTINUE:
440 // nothing to do
441 break;
442 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
443 // no more asserts
444 returnCode = true;
445 break;
446
447 default:
448 wxFAIL_MSG( _T("unexpected return code from GtkAssertDialog") );
449 }
450
451 gtk_widget_destroy(dialog);
452 return returnCode;
453}
454
455#endif // __WXDEBUG__
456
88bbc332
RR
457wxString wxGUIAppTraits::GetDesktopEnvironment() const
458{
9d0e0377 459 wxString de = wxSystemOptions::GetOption(_T("gtk.desktop"));
3c029873 460#if wxUSE_DETECT_SM
9d0e0377
VZ
461 if ( de.empty() )
462 {
9d0e0377 463 static const wxString s_SM = GetSM();
391bf008 464
9d0e0377
VZ
465 if (s_SM == wxT("GnomeSM"))
466 de = wxT("GNOME");
467 else if (s_SM == wxT("KDE"))
468 de = wxT("KDE");
469 }
391bf008 470#endif // wxUSE_DETECT_SM
88bbc332 471
9d0e0377 472 return de;
88bbc332
RR
473}
474
d3a0a0ee 475#ifdef __WXGTK26__
88bbc332 476
d3a0a0ee
VZ
477// see the hack below in wxCmdLineParser::GetUsageString().
478// TODO: replace this hack with a g_option_group_get_entries()
479// call as soon as such function exists
480struct _GOptionGroup
481{
482 gchar *name;
483 gchar *description;
484 gchar *help_description;
485
486 GDestroyNotify destroy_notify;
487 gpointer user_data;
488
489 GTranslateFunc translate_func;
490 GDestroyNotify translate_notify;
491 gpointer translate_data;
492
493 GOptionEntry *entries;
494 gint n_entries;
495
496 GOptionParseFunc pre_parse_func;
497 GOptionParseFunc post_parse_func;
498 GOptionErrorFunc error_func;
499};
500
501wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
502{
503 wxString ret;
504
505 if (opt->short_name)
506 ret << _T("-") << opt->short_name;
507 if (opt->long_name)
508 {
509 if (!ret.empty())
510 ret << _T(", ");
511 ret << _T("--") << opt->long_name;
512
513 if (opt->arg_description)
514 ret << _T("=") << opt->arg_description;
515 }
516
517 return _T(" ") + ret;
518}
519
520#endif // __WXGTK26__
521
522wxString
523wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
524 wxArrayString& desc) const
525{
526 wxString usage;
527
528#ifdef __WXGTK26__
529 // check whether GTK version is greater than 2.6 but also lower than 2.12
530 // because, as we use the undocumented _GOptionGroup struct, we don't want
531 // to run this code with future versions which might change it (2.11 is the
532 // latest one at the time of this writing)
533 if (!gtk_check_version(2,6,0) &&
534 gtk_check_version(2,12,0))
535 {
536 usage << _("The following standard GTK+ options are also supported:\n");
537
538 // passing true here means that the function can open the default
539 // display while parsing (not really used here anyhow)
540 GOptionGroup *gtkOpts = gtk_get_option_group(true);
541
542 // WARNING: here we access the internals of GOptionGroup:
543 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
544 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
545 wxArrayString namesOptions, descOptions;
546
547 for ( size_t n = 0; n < n_entries; n++ )
548 {
549 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
550 continue; // skip
551
552 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
553
554 const gchar * const entryDesc = entries[n].description;
194323c7 555 desc.push_back(wxString(entryDesc));
d3a0a0ee
VZ
556 }
557
558 g_option_group_free (gtkOpts);
559 }
560#else
561 wxUnusedVar(names);
562 wxUnusedVar(desc);
563#endif // __WXGTK26__
564
565 return usage;
566}
88bbc332 567