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