]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
Rewrite wxExecute() implementation under Unix.
[wxWidgets.git] / src / x11 / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 // ============================================================================
20 // declarations
21 // ============================================================================
22
23 // ----------------------------------------------------------------------------
24 // headers
25 // ----------------------------------------------------------------------------
26
27 #include "wx/utils.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/window.h" // for wxTopLevelWindows
32 #include "wx/cursor.h"
33 #include "wx/msgdlg.h"
34 #endif
35
36 #include "wx/apptrait.h"
37 #include "wx/generic/private/timer.h"
38 #include "wx/evtloop.h"
39
40 #include <ctype.h>
41 #include <stdarg.h>
42 #include <dirent.h>
43 #include <string.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <unistd.h>
47 #include <sys/wait.h>
48 #include <pwd.h>
49 #include <errno.h>
50 // #include <netdb.h>
51 #include <signal.h>
52
53 #if (defined(__SUNCC__) || defined(__CLCC__))
54 #include <sysent.h>
55 #endif
56
57 #ifdef __VMS__
58 #pragma message disable nosimpint
59 #endif
60
61 #include "wx/x11/private.h"
62
63 #include "X11/Xutil.h"
64
65 #ifdef __VMS__
66 #pragma message enable nosimpint
67 #endif
68
69 // ----------------------------------------------------------------------------
70 // async event processing
71 // ----------------------------------------------------------------------------
72
73 // Consume all events until no more left
74 void wxFlushEvents()
75 {
76 Display *display = (Display*) wxGetDisplay();
77
78 XSync (display, FALSE);
79
80 // TODO for X11
81 // ??
82 }
83
84 bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd))
85 {
86 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
87 return false;
88 }
89
90 // ----------------------------------------------------------------------------
91 // misc
92 // ----------------------------------------------------------------------------
93
94 // Emit a beeeeeep
95 void wxBell()
96 {
97 // Use current setting for the bell
98 XBell ((Display*) wxGetDisplay(), 0);
99 }
100
101 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
102 {
103 // get X protocol version
104 Display *display = wxGlobalDisplay();
105 if (display)
106 {
107 if ( verMaj )
108 *verMaj = ProtocolVersion (display);
109 if ( verMin )
110 *verMin = ProtocolRevision (display);
111 }
112
113 return wxPORT_X11;
114 }
115
116 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
117 {
118 return new wxEventLoop;
119 }
120
121 // ----------------------------------------------------------------------------
122 // display info
123 // ----------------------------------------------------------------------------
124
125 void wxGetMousePosition( int* x, int* y )
126 {
127 #if wxUSE_NANOX
128 // TODO
129 *x = 0;
130 *y = 0;
131 #else
132 XMotionEvent xev;
133 Window root, child;
134 XQueryPointer((Display*) wxGetDisplay(),
135 DefaultRootWindow((Display*) wxGetDisplay()),
136 &root, &child,
137 &(xev.x_root), &(xev.y_root),
138 &(xev.x), &(xev.y),
139 &(xev.state));
140 *x = xev.x_root;
141 *y = xev.y_root;
142 #endif
143 };
144
145 // Return true if we have a colour display
146 bool wxColourDisplay()
147 {
148 return wxDisplayDepth() > 1;
149 }
150
151 // Returns depth of screen
152 int wxDisplayDepth()
153 {
154 Display *dpy = (Display*) wxGetDisplay();
155
156 return DefaultDepth (dpy, DefaultScreen (dpy));
157 }
158
159 // Get size of display
160 void wxDisplaySize(int *width, int *height)
161 {
162 Display *dpy = (Display*) wxGetDisplay();
163
164 if ( width )
165 *width = DisplayWidth (dpy, DefaultScreen (dpy));
166 if ( height )
167 *height = DisplayHeight (dpy, DefaultScreen (dpy));
168 }
169
170 void wxDisplaySizeMM(int *width, int *height)
171 {
172 Display *dpy = (Display*) wxGetDisplay();
173
174 if ( width )
175 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
176 if ( height )
177 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
178 }
179
180 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
181 {
182 return wxGenericFindWindowAtPoint(pt);
183 }
184
185
186 // Configurable display in wxX11 and wxMotif
187 static Display *gs_currentDisplay = NULL;
188 static wxString gs_displayName;
189
190 WXDisplay *wxGetDisplay()
191 {
192 return (WXDisplay *)gs_currentDisplay;
193 }
194
195 // close the current display
196 void wxCloseDisplay()
197 {
198 if ( gs_currentDisplay )
199 {
200 if ( XCloseDisplay(gs_currentDisplay) != 0 )
201 {
202 wxLogWarning(_("Failed to close the display \"%s\""),
203 gs_displayName.c_str());
204 }
205
206 gs_currentDisplay = NULL;
207 gs_displayName.clear();
208 }
209 }
210
211 bool wxSetDisplay(const wxString& displayName)
212 {
213 Display *dpy = XOpenDisplay
214 (
215 displayName.empty() ? NULL
216 : (const char *)displayName.mb_str()
217 );
218
219 if ( !dpy )
220 {
221 wxLogError(_("Failed to open display \"%s\"."), displayName.c_str());
222 return false;
223 }
224
225 wxCloseDisplay();
226
227 gs_currentDisplay = dpy;
228 gs_displayName = displayName;
229
230 return true;
231 }
232
233 wxString wxGetDisplayName()
234 {
235 return gs_displayName;
236 }
237
238 #include "wx/module.h"
239
240 // the module responsible for closing the X11 display at the program end
241 class wxX11DisplayModule : public wxModule
242 {
243 public:
244 virtual bool OnInit() { return true; }
245 virtual void OnExit() { wxCloseDisplay(); }
246
247 private:
248 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule)
249 };
250
251 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule)
252
253 // ----------------------------------------------------------------------------
254 // Some colour manipulation routines
255 // ----------------------------------------------------------------------------
256
257 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
258 {
259 int h = hsv->h;
260 int s = hsv->s;
261 int v = hsv->v;
262 int r = 0, g = 0, b = 0;
263 int i, f;
264 int p, q, t;
265 s = (s * wxMAX_RGB) / wxMAX_SV;
266 v = (v * wxMAX_RGB) / wxMAX_SV;
267 if (h == 360) h = 0;
268 if (s == 0) { h = 0; r = g = b = v; }
269 i = h / 60;
270 f = h % 60;
271 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
272 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
273 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
274 switch (i)
275 {
276 case 0: r = v, g = t, b = p; break;
277 case 1: r = q, g = v, b = p; break;
278 case 2: r = p, g = v, b = t; break;
279 case 3: r = p, g = q, b = v; break;
280 case 4: r = t, g = p, b = v; break;
281 case 5: r = v, g = p, b = q; break;
282 }
283 rgb->red = r << 8;
284 rgb->green = g << 8;
285 rgb->blue = b << 8;
286 }
287
288 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
289 {
290 int r = rgb->red >> 8;
291 int g = rgb->green >> 8;
292 int b = rgb->blue >> 8;
293 int maxv = wxMax3(r, g, b);
294 int minv = wxMin3(r, g, b);
295 int h = 0, s, v;
296 v = maxv;
297 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
298 else s = 0;
299 if (s == 0) h = 0;
300 else
301 {
302 int rc, gc, bc, hex = 0;
303 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
304 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
305 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
306 if (r == maxv) { h = bc - gc, hex = 0; }
307 else if (g == maxv) { h = rc - bc, hex = 2; }
308 else if (b == maxv) { h = gc - rc, hex = 4; }
309 h = hex * 60 + (h * 60 / wxMAX_RGB);
310 if (h < 0) h += 360;
311 }
312 hsv->h = h;
313 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
314 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
315 }
316
317 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
318 {
319 #if !wxUSE_NANOX
320 int llp;
321
322 int screen = DefaultScreen(d);
323 int num_colors = DisplayCells(d,screen);
324
325 XColor *color_defs = new XColor[num_colors];
326 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
327 XQueryColors(d,cmp,color_defs,num_colors);
328
329 wxHSV hsv_defs, hsv;
330 wxXColorToHSV(&hsv,xc);
331
332 int diff, min_diff = 0, pixel = 0;
333
334 for(llp = 0;llp < num_colors;llp++)
335 {
336 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
337 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
338 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
339 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
340 if (llp == 0) min_diff = diff;
341 if (min_diff > diff) { min_diff = diff; pixel = llp; }
342 if (min_diff == 0) break;
343 }
344
345 xc -> red = color_defs[pixel].red;
346 xc -> green = color_defs[pixel].green;
347 xc -> blue = color_defs[pixel].blue;
348 xc -> flags = DoRed | DoGreen | DoBlue;
349
350 /* FIXME, TODO
351 if (!XAllocColor(d,cmp,xc))
352 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
353 */
354
355 delete[] color_defs;
356 #endif
357 }
358
359 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
360 {
361 if (!XAllocColor(d,cmp,xc))
362 {
363 // cout << "wxAllocColor : Warning : Cannot allocate color, attempt find nearest !\n";
364 wxAllocNearestColor(d,cmp,xc);
365 }
366 }
367
368 wxString wxGetXEventName(XEvent& event)
369 {
370 #if wxUSE_NANOX
371 wxString str(wxT("(some event)"));
372 return str;
373 #else
374 int type = event.xany.type;
375 static const char* event_name[] = {
376 "", "unknown(-)", // 0-1
377 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
378 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
379 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
380 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
381 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
382 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
383 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
384 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
385 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
386 "ClientMessage", "MappingNotify", // 33-34
387 "unknown(+)"}; // 35
388 type = wxMin(35, type); type = wxMax(1, type);
389 return wxString::FromAscii(event_name[type]);
390 #endif
391 }
392