]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/utilsgtk.cpp
GetBestFittingSize --> GetEffectiveMinSize
[wxWidgets.git] / src / gtk / utilsgtk.cpp
CommitLineData
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"
02847e59 22
5336ece4
VZ
23#include "wx/process.h"
24
518b5d2f
VZ
25#include "wx/unix/execute.h"
26
c801d85f 27#include <stdarg.h>
c801d85f
KB
28#include <string.h>
29#include <sys/stat.h>
30#include <sys/types.h>
dbd25330 31#include <sys/wait.h> // for WNOHANG
c801d85f 32#include <unistd.h>
91b8de8d
RR
33
34#include "glib.h"
35#include "gdk/gdk.h"
36#include "gtk/gtk.h"
91b8de8d 37#include "gdk/gdkx.h"
88ac883a 38
27933891 39#ifdef HAVE_X11_XKBLIB_H
154c4aa1
VZ
40 /* under HP-UX and Solaris 2.6, at least, XKBlib.h defines structures with
41 * field named "explicit" - which is, of course, an error for a C++
42 * compiler. To be on the safe side, just redefine it everywhere. */
88ac883a 43 #define explicit __wx_explicit
ec5d7799 44
154c4aa1 45 #include "X11/XKBlib.h"
ec5d7799 46
88ac883a 47 #undef explicit
27933891 48#endif // HAVE_X11_XKBLIB_H
dfcb1ae0 49
88bbc332
RR
50
51#if wxUSE_DETECT_SM
52 #include "X11/Xlib.h"
53 #include "X11/SM/SMlib.h"
54#endif
55
d76fe38b
RR
56//-----------------------------------------------------------------------------
57// data
58//-----------------------------------------------------------------------------
59
c2fa61e8 60extern GtkWidget *wxGetRootWindow();
d76fe38b
RR
61
62//----------------------------------------------------------------------------
c801d85f 63// misc.
d76fe38b 64//----------------------------------------------------------------------------
189d1ae7
SN
65#ifndef __EMX__
66// on OS/2, we use the wxBell from wxBase library
c801d85f 67
518b5d2f 68void wxBell()
c801d85f 69{
e52f60e6
RR
70 gdk_beep();
71}
189d1ae7 72#endif
c801d85f 73
27933891
RR
74/* Don't synthesize KeyUp events holding down a key and producing
75 KeyDown events with autorepeat. */
76#ifdef HAVE_X11_XKBLIB_H
f0492f7d
RR
77bool wxSetDetectableAutoRepeat( bool flag )
78{
79 Bool result;
80 XkbSetDetectableAutoRepeat( GDK_DISPLAY(), flag, &result );
df91131c 81 return result; /* true if keyboard hardware supports this mode */
27933891
RR
82}
83#else
84bool wxSetDetectableAutoRepeat( bool WXUNUSED(flag) )
85{
df91131c 86 return false;
f0492f7d 87}
27933891 88#endif
f0492f7d 89
66bd83b4
VS
90// Escapes string so that it is valid Pango markup XML string:
91wxString wxEscapeStringForPangoMarkup(const wxString& str)
92{
93 size_t len = str.length();
94 wxString out;
95 out.Alloc(len);
96 for (size_t i = 0; i < len; i++)
97 {
98 wxChar c = str[i];
99 switch (c)
100 {
101 case _T('&'):
102 out << _T("&amp;");
103 break;
104 case _T('<'):
105 out << _T("&lt;");
106 break;
107 case _T('>'):
108 out << _T("&gt;");
109 break;
110 case _T('\''):
111 out << _T("&apos;");
112 break;
113 case _T('"'):
114 out << _T("&quot;");
115 break;
116 default:
117 out << c;
118 break;
119 }
120 }
121 return out;
122}
66bd83b4
VS
123
124
518b5d2f
VZ
125// ----------------------------------------------------------------------------
126// display characterstics
127// ----------------------------------------------------------------------------
82052aff 128
d111a89a
VZ
129void *wxGetDisplay()
130{
27df579a 131 return GDK_DISPLAY();
d111a89a
VZ
132}
133
c0392997
RR
134void wxDisplaySize( int *width, int *height )
135{
e52f60e6
RR
136 if (width) *width = gdk_screen_width();
137 if (height) *height = gdk_screen_height();
c0392997
RR
138}
139
904a68b6
RL
140void wxDisplaySizeMM( int *width, int *height )
141{
142 if (width) *width = gdk_screen_width_mm();
143 if (height) *height = gdk_screen_height_mm();
144}
145
ec5d7799
RD
146void wxClientDisplayRect(int *x, int *y, int *width, int *height)
147{
148 // This is supposed to return desktop dimensions minus any window
149 // manager panels, menus, taskbars, etc. If there is a way to do that
150 // for this platform please fix this function, otherwise it defaults
151 // to the entire desktop.
152 if (x) *x = 0;
153 if (y) *y = 0;
154 wxDisplaySize(width, height);
155}
156
6de97a3b
RR
157void wxGetMousePosition( int* x, int* y )
158{
bbe0af5b 159 gdk_window_get_pointer( (GdkWindow*) NULL, x, y, (GdkModifierType*) NULL );
e52f60e6 160}
6de97a3b 161
518b5d2f 162bool wxColourDisplay()
6de97a3b 163{
df91131c 164 return true;
6de97a3b
RR
165}
166
518b5d2f 167int wxDisplayDepth()
6de97a3b 168{
22a3bce4 169 return gdk_drawable_get_visual( wxGetRootWindow()->window )->depth;
6de97a3b
RR
170}
171
57591e0e
JS
172wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
173{
174 return wxGenericFindWindowAtPoint(pt);
175}
176
5f11fef5
VZ
177#if !wxUSE_UNICODE
178
179wxCharBuffer wxConvertToGTK(const wxString& s, wxFontEncoding enc)
180{
27dee9ae 181 wxCharBuffer buf;
5f11fef5
VZ
182 if ( enc == wxFONTENCODING_UTF8 )
183 {
27dee9ae
VZ
184 // no need for conversion at all, but do check that we have a valid
185 // UTF-8 string because passing invalid UTF-8 to GTK+ is going to
186 // result in a GTK+ error message and, especially, loss of data which
187 // was supposed to be shown in the GUI
188 if ( wxConvUTF8.ToWChar(NULL, 0, s, s.length()) == wxCONV_FAILED )
189 {
190 // warn the programmer that something is probably wrong in his code
191 //
192 // NB: don't include the string in output because chances are that
193 // this invalid UTF-8 string could result in more errors itself
194 // if the application shows logs in the GUI and so we get into
195 // an infinite loop
196 wxLogDebug(_T("Invalid UTF-8 string in wxConvertToGTK()"));
197
198 // but still try to show at least something on the screen
199 wxMBConvUTF8 utf8permissive(wxMBConvUTF8::MAP_INVALID_UTF8_TO_OCTAL);
200 wxWCharBuffer wbuf(utf8permissive.cMB2WC(s));
201 buf = wxConvUTF8.cWC2MB(wbuf);
202 }
203 else // valid UTF-8 string, no need to convert
204 {
205 buf = wxCharBuffer(s);
206 }
12bc5f9a 207 }
27dee9ae 208 else // !UTF-8
12bc5f9a 209 {
27dee9ae
VZ
210 wxWCharBuffer wbuf;
211 if ( enc == wxFONTENCODING_SYSTEM || enc == wxFONTENCODING_DEFAULT )
212 {
213 wbuf = wxConvUI->cMB2WC(s);
214 }
215 else // another encoding, use generic conversion class
216 {
217 wbuf = wxCSConv(enc).cMB2WC(s);
218 }
12bc5f9a 219
27dee9ae
VZ
220 if ( wbuf )
221 buf = wxConvUTF8.cWC2MB(wbuf);
222 }
5f11fef5
VZ
223
224 return buf;
225}
226
227#endif // !wxUSE_UNICODE
57591e0e 228
518b5d2f 229// ----------------------------------------------------------------------------
c801d85f 230// subprocess routines
518b5d2f 231// ----------------------------------------------------------------------------
cf447356 232
865bb325
VZ
233extern "C" {
234static
90350682
VZ
235void GTK_EndProcessDetector(gpointer data, gint source,
236 GdkInputCondition WXUNUSED(condition) )
cf447356 237{
ab857a4e 238 wxEndProcessData *proc_data = (wxEndProcessData *)data;
dbd25330
VZ
239
240 // has the process really terminated? unfortunately GDK (or GLib) seem to
241 // generate G_IO_HUP notification even when it simply tries to read from a
242 // closed fd and hasn't terminated at all
243 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
447f908a
VZ
244 int status = 0;
245 int rc = waitpid(pid, &status, WNOHANG);
246
247 if ( rc == 0 )
dbd25330
VZ
248 {
249 // no, it didn't exit yet, continue waiting
250 return;
251 }
252
447f908a
VZ
253 // set exit code to -1 if something bad happened
254 proc_data->exitcode = rc != -1 && WIFEXITED(status) ? WEXITSTATUS(status)
255 : -1;
256
dbd25330 257 // child exited, end waiting
ab857a4e 258 close(source);
dbd25330
VZ
259
260 // don't call us again!
ab857a4e
KB
261 gdk_input_remove(proc_data->tag);
262
ab857a4e 263 wxHandleProcessTermination(proc_data);
3069ac4e 264}
865bb325 265}
cf447356 266
518b5d2f 267int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
c801d85f 268{
518b5d2f
VZ
269 int tag = gdk_input_add(fd,
270 GDK_INPUT_READ,
271 GTK_EndProcessDetector,
272 (gpointer)proc_data);
c801d85f 273
518b5d2f 274 return tag;
3069ac4e 275}
8bb6b2c0
VZ
276
277
278
279// ----------------------------------------------------------------------------
280// wxPlatformInfo-related
281// ----------------------------------------------------------------------------
282
283wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
284{
285 if ( verMaj )
286 *verMaj = gtk_major_version;
287 if ( verMin )
288 *verMin = gtk_minor_version;
289
290 return wxPORT_GTK;
291}
88bbc332
RR
292
293#if wxUSE_DETECT_SM
294static wxString GetSM()
295{
391bf008 296 class Dpy
cb1bf052 297 {
391bf008
VZ
298 public:
299 Dpy() { m_dpy = XOpenDisplay(NULL); }
300 ~Dpy() { if ( m_dpy ) XCloseDisplay(m_dpy); }
88bbc332 301
391bf008
VZ
302 operator Display *() const { return m_dpy; }
303 private:
304 Display *m_dpy;
305 } dpy;
88bbc332 306
391bf008
VZ
307 if ( !dpy )
308 return wxEmptyString;
309
310 char *client_id;
311 SmcConn smc_conn = SmcOpenConnection(NULL, NULL,
312 999, 999,
313 0 /* mask */, NULL /* callbacks */,
314 NULL, &client_id,
315 0, NULL);
316
317 if ( !smc_conn )
318 return wxEmptyString;
319
320 char *vendor = SmcVendor(smc_conn);
321 wxString ret = wxString::FromAscii( vendor );
322 free(vendor);
323
324 SmcCloseConnection(smc_conn, 0, NULL);
325 free(client_id);
326
327 return ret;
88bbc332 328}
391bf008 329#endif // wxUSE_DETECT_SM
88bbc332
RR
330
331wxString wxGUIAppTraits::GetDesktopEnvironment() const
332{
333#if wxUSE_DETECT_SM
391bf008
VZ
334 const wxString SM = GetSM();
335
88bbc332
RR
336 if (SM == wxT("GnomeSM"))
337 return wxT("GNOME");
391bf008 338
88bbc332
RR
339 if (SM == wxT("KDE"))
340 return wxT("KDE");
391bf008 341#endif // wxUSE_DETECT_SM
88bbc332
RR
342
343 return wxEmptyString;
344}
345
346
347