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