]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/utilsgtk.cpp
Add virtual ~wxAnyScrollHelperBase() to fix compiler warning.
[wxWidgets.git] / src / gtk / utilsgtk.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/utilsgtk.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
8
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
11
12 #include "wx/utils.h"
13
14 #ifndef WX_PRECOMP
15 #include "wx/string.h"
16 #include "wx/intl.h"
17 #include "wx/log.h"
18 #endif
19
20 #include "wx/apptrait.h"
21 #include "wx/process.h"
22 #include "wx/sysopt.h"
23
24 #include "wx/gtk/private/timer.h"
25 #include "wx/evtloop.h"
26
27 #include <gtk/gtk.h>
28 #ifdef GDK_WINDOWING_WIN32
29 #include <gdk/gdkwin32.h>
30 #endif
31 #ifdef GDK_WINDOWING_X11
32 #include <gdk/gdkx.h>
33 #endif
34
35 #if wxDEBUG_LEVEL
36 #include "wx/gtk/assertdlg_gtk.h"
37 #if wxUSE_STACKWALKER
38 #include "wx/stackwalk.h"
39 #endif // wxUSE_STACKWALKER
40 #endif // wxDEBUG_LEVEL
41
42 #include <stdarg.h>
43 #include <string.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #ifdef __UNIX__
47 #include <unistd.h>
48 #endif
49
50 #if wxUSE_DETECT_SM
51 #include <X11/SM/SMlib.h>
52
53 #include "wx/unix/utilsx11.h"
54 #endif
55
56 #include "wx/gtk/private/gtk2-compat.h"
57
58 //-----------------------------------------------------------------------------
59 // data
60 //-----------------------------------------------------------------------------
61
62 extern GtkWidget *wxGetRootWindow();
63
64 //----------------------------------------------------------------------------
65 // misc.
66 //----------------------------------------------------------------------------
67
68 void wxBell()
69 {
70 gdk_beep();
71 }
72
73 // ----------------------------------------------------------------------------
74 // display characteristics
75 // ----------------------------------------------------------------------------
76
77 #ifdef GDK_WINDOWING_X11
78 void *wxGetDisplay()
79 {
80 return GDK_DISPLAY_XDISPLAY(gtk_widget_get_display(wxGetRootWindow()));
81 }
82 #endif
83
84 void wxDisplaySize( int *width, int *height )
85 {
86 if (width) *width = gdk_screen_width();
87 if (height) *height = gdk_screen_height();
88 }
89
90 void wxDisplaySizeMM( int *width, int *height )
91 {
92 if (width) *width = gdk_screen_width_mm();
93 if (height) *height = gdk_screen_height_mm();
94 }
95
96 bool wxColourDisplay()
97 {
98 return true;
99 }
100
101 int wxDisplayDepth()
102 {
103 return gdk_visual_get_depth(gtk_widget_get_visual(wxGetRootWindow()));
104 }
105
106 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
107 {
108 return wxGenericFindWindowAtPoint(pt);
109 }
110
111 #if !wxUSE_UNICODE
112
113 WXDLLIMPEXP_CORE wxCharBuffer
114 wxConvertToGTK(const wxString& s, wxFontEncoding enc)
115 {
116 wxWCharBuffer wbuf;
117 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
118 {
119 wbuf = wxConvUI->cMB2WC(s.c_str());
120 }
121 else // another encoding, use generic conversion class
122 {
123 wbuf = wxCSConv(enc).cMB2WC(s.c_str());
124 }
125
126 if ( !wbuf && !s.empty() )
127 {
128 // conversion failed, but we still want to show something to the user
129 // even if it's going to be wrong it is better than nothing
130 //
131 // we choose ISO8859-1 here arbitrarily, it's just the most common
132 // encoding probably and, also importantly here, conversion from it
133 // never fails as it's done internally by wxCSConv
134 wbuf = wxCSConv(wxFONTENCODING_ISO8859_1).cMB2WC(s.c_str());
135 }
136
137 return wxConvUTF8.cWC2MB(wbuf);
138 }
139
140 WXDLLIMPEXP_CORE wxCharBuffer
141 wxConvertFromGTK(const wxString& s, wxFontEncoding enc)
142 {
143 // this conversion should never fail as GTK+ always uses UTF-8 internally
144 // so there are no complications here
145 const wxWCharBuffer wbuf(wxConvUTF8.cMB2WC(s.c_str()));
146 if ( enc == wxFONTENCODING_SYSTEM )
147 return wxConvUI->cWC2MB(wbuf);
148
149 return wxCSConv(enc).cWC2MB(wbuf);
150 }
151
152 #endif // !wxUSE_UNICODE
153
154 // Returns NULL if version is certainly greater or equal than major.minor.micro
155 // Returns string describing the error if version is lower than
156 // major.minor.micro OR it cannot be determined and one should not rely on the
157 // availability of pango version major.minor.micro, nor the non-availability
158 const gchar *wx_pango_version_check (int major, int minor, int micro)
159 {
160 // NOTE: you don't need to use this macro to check for Pango features
161 // added in pango-1.4 or earlier since GTK 2.4 (our minimum requirement
162 // for GTK lib) required pango 1.4...
163
164 #ifdef __WXGTK3__
165 return pango_version_check(major, minor, micro);
166 #elif defined(PANGO_VERSION_MAJOR)
167 if (!gtk_check_version (2,11,0))
168 {
169 // GTK+ 2.11 requires Pango >= 1.15.3 and pango_version_check
170 // was added in Pango 1.15.2 thus we know for sure the pango lib we're
171 // using has the pango_version_check function:
172 return pango_version_check (major, minor, micro);
173 }
174
175 return "can't check";
176 #else // !PANGO_VERSION_MAJOR
177 wxUnusedVar(major);
178 wxUnusedVar(minor);
179 wxUnusedVar(micro);
180
181 return "too old headers";
182 #endif
183 }
184
185 // ----------------------------------------------------------------------------
186 // wxPlatformInfo-related
187 // ----------------------------------------------------------------------------
188
189 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
190 {
191 if ( verMaj )
192 *verMaj = gtk_major_version;
193 if ( verMin )
194 *verMin = gtk_minor_version;
195
196 return wxPORT_GTK;
197 }
198
199 #if wxUSE_TIMER
200
201 wxTimerImpl *wxGUIAppTraits::CreateTimerImpl(wxTimer *timer)
202 {
203 return new wxGTKTimerImpl(timer);
204 }
205
206 #endif // wxUSE_TIMER
207
208 #if wxUSE_DETECT_SM
209 static wxString GetSM()
210 {
211 wxX11Display dpy;
212 if ( !dpy )
213 return wxEmptyString;
214
215 char smerr[256];
216 char *client_id;
217 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
218 999, 999,
219 0 /* mask */, NULL /* callbacks */,
220 NULL, &client_id,
221 WXSIZEOF(smerr), smerr);
222
223 if ( !smc_conn )
224 {
225 wxLogDebug("Failed to connect to session manager: %s", smerr);
226 return wxEmptyString;
227 }
228
229 char *vendor = SmcVendor(smc_conn);
230 wxString ret = wxString::FromAscii( vendor );
231 free(vendor);
232
233 SmcCloseConnection(smc_conn, 0, NULL);
234 free(client_id);
235
236 return ret;
237 }
238 #endif // wxUSE_DETECT_SM
239
240
241 //-----------------------------------------------------------------------------
242 // wxGUIAppTraits
243 //-----------------------------------------------------------------------------
244
245 wxEventLoopBase *wxGUIAppTraits::CreateEventLoop()
246 {
247 return new wxEventLoop();
248 }
249
250
251 #ifdef __UNIX__
252
253 #if wxDEBUG_LEVEL && wxUSE_STACKWALKER
254
255 // private helper class
256 class StackDump : public wxStackWalker
257 {
258 public:
259 StackDump(GtkAssertDialog *dlg) { m_dlg=dlg; }
260
261 protected:
262 virtual void OnStackFrame(const wxStackFrame& frame)
263 {
264 wxString fncname = frame.GetName();
265
266 // append this stack frame's info in the dialog
267 if (!frame.GetFileName().empty() || !fncname.empty())
268 {
269 gtk_assert_dialog_append_stack_frame(m_dlg,
270 fncname.utf8_str(),
271 frame.GetFileName().utf8_str(),
272 frame.GetLine());
273 }
274 }
275
276 private:
277 GtkAssertDialog *m_dlg;
278 };
279
280 static void get_stackframe_callback(void* p)
281 {
282 StackDump* dump = static_cast<StackDump*>(p);
283 // skip over frames up to including wxOnAssert()
284 dump->ProcessFrames(6);
285 }
286
287 #endif // wxDEBUG_LEVEL && wxUSE_STACKWALKER
288
289 bool wxGUIAppTraits::ShowAssertDialog(const wxString& msg)
290 {
291 #if wxDEBUG_LEVEL
292 // we can't show the dialog from another thread
293 if ( wxIsMainThread() )
294 {
295 // under GTK2 we prefer to use a dialog widget written using directly
296 // in GTK+ as use a dialog written using wxWidgets would need the
297 // wxWidgets idle processing to work correctly which might not be the
298 // case when assert happens
299 GtkWidget *dialog = gtk_assert_dialog_new();
300 gtk_assert_dialog_set_message(GTK_ASSERT_DIALOG(dialog), msg.mb_str());
301
302 #if wxUSE_STACKWALKER
303 // save the current stack ow...
304 StackDump dump(GTK_ASSERT_DIALOG(dialog));
305 dump.SaveStack(100); // showing more than 100 frames is not very useful
306
307 // ...but process it only if the user needs it
308 gtk_assert_dialog_set_backtrace_callback
309 (
310 GTK_ASSERT_DIALOG(dialog),
311 get_stackframe_callback,
312 &dump
313 );
314 #endif // wxUSE_STACKWALKER
315
316 gint result = gtk_dialog_run(GTK_DIALOG (dialog));
317 bool returnCode = false;
318 switch (result)
319 {
320 case GTK_ASSERT_DIALOG_STOP:
321 wxTrap();
322 break;
323 case GTK_ASSERT_DIALOG_CONTINUE:
324 // nothing to do
325 break;
326 case GTK_ASSERT_DIALOG_CONTINUE_SUPPRESSING:
327 // no more asserts
328 returnCode = true;
329 break;
330
331 default:
332 wxFAIL_MSG( wxT("unexpected return code from GtkAssertDialog") );
333 }
334
335 gtk_widget_destroy(dialog);
336 return returnCode;
337 }
338 #endif // wxDEBUG_LEVEL
339
340 return wxAppTraitsBase::ShowAssertDialog(msg);
341 }
342
343 #endif // __UNIX__
344
345 #if defined(__UNIX__) || defined(__OS2__)
346
347 wxString wxGUIAppTraits::GetDesktopEnvironment() const
348 {
349 wxString de = wxSystemOptions::GetOption(wxT("gtk.desktop"));
350 #if wxUSE_DETECT_SM
351 if ( de.empty() )
352 {
353 static const wxString s_SM = GetSM();
354
355 if (s_SM == wxT("GnomeSM"))
356 de = wxT("GNOME");
357 else if (s_SM == wxT("KDE"))
358 de = wxT("KDE");
359 }
360 #endif // wxUSE_DETECT_SM
361
362 return de;
363 }
364
365 #endif // __UNIX__ || __OS2__
366
367 #ifdef __UNIX__
368
369 // see the hack below in wxCmdLineParser::GetUsageString().
370 // TODO: replace this hack with a g_option_group_get_entries()
371 // call as soon as such function exists;
372 // see http://bugzilla.gnome.org/show_bug.cgi?id=431021 for the relative
373 // feature request
374 struct _GOptionGroup
375 {
376 gchar *name;
377 gchar *description;
378 gchar *help_description;
379
380 GDestroyNotify destroy_notify;
381 gpointer user_data;
382
383 GTranslateFunc translate_func;
384 GDestroyNotify translate_notify;
385 gpointer translate_data;
386
387 GOptionEntry *entries;
388 gint n_entries;
389
390 GOptionParseFunc pre_parse_func;
391 GOptionParseFunc post_parse_func;
392 GOptionErrorFunc error_func;
393 };
394
395 static
396 wxString wxGetNameFromGtkOptionEntry(const GOptionEntry *opt)
397 {
398 wxString ret;
399
400 if (opt->short_name)
401 ret << wxT("-") << opt->short_name;
402 if (opt->long_name)
403 {
404 if (!ret.empty())
405 ret << wxT(", ");
406 ret << wxT("--") << opt->long_name;
407
408 if (opt->arg_description)
409 ret << wxT("=") << opt->arg_description;
410 }
411
412 return wxT(" ") + ret;
413 }
414
415 wxString
416 wxGUIAppTraits::GetStandardCmdLineOptions(wxArrayString& names,
417 wxArrayString& desc) const
418 {
419 wxString usage;
420
421 // check whether GLib version is greater than 2.6 but also lower than 2.33
422 // because, as we use the undocumented _GOptionGroup struct, we don't want
423 // to run this code with future versions which might change it (2.32 is the
424 // latest one at the time of this writing)
425 if (glib_check_version(2,33,0))
426 {
427 usage << _("The following standard GTK+ options are also supported:\n");
428
429 // passing true here means that the function can open the default
430 // display while parsing (not really used here anyhow)
431 GOptionGroup *gtkOpts = gtk_get_option_group(true);
432
433 // WARNING: here we access the internals of GOptionGroup:
434 GOptionEntry *entries = ((_GOptionGroup*)gtkOpts)->entries;
435 unsigned int n_entries = ((_GOptionGroup*)gtkOpts)->n_entries;
436 wxArrayString namesOptions, descOptions;
437
438 for ( size_t n = 0; n < n_entries; n++ )
439 {
440 if ( entries[n].flags & G_OPTION_FLAG_HIDDEN )
441 continue; // skip
442
443 names.push_back(wxGetNameFromGtkOptionEntry(&entries[n]));
444
445 const gchar * const entryDesc = entries[n].description;
446 desc.push_back(wxString(entryDesc));
447 }
448
449 g_option_group_free (gtkOpts);
450 }
451
452 return usage;
453 }
454
455 #endif // __UNIX__