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