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