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