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