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