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