]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
more DC changes for wxX11
[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/unix/execute.h"
62
63 #include "wx/x11/private.h"
64
65 #include "X11/Xutil.h"
66
67 #ifdef __VMS__
68 #pragma message enable nosimpint
69 #endif
70
71 // ----------------------------------------------------------------------------
72 // async event processing
73 // ----------------------------------------------------------------------------
74
75 // Consume all events until no more left
76 void wxFlushEvents()
77 {
78 Display *display = (Display*) wxGetDisplay();
79
80 XSync (display, FALSE);
81
82 // TODO for X11
83 // ??
84 }
85
86 bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd))
87 {
88 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
89 return false;
90 }
91
92 // ----------------------------------------------------------------------------
93 // wxExecute stuff
94 // ----------------------------------------------------------------------------
95
96 WX_DECLARE_HASH_MAP( int, wxEndProcessData*, wxIntegerHash, wxIntegerEqual, wxProcMap );
97
98 static wxProcMap *gs_procmap;
99
100 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
101 {
102 if (!gs_procmap) gs_procmap = new wxProcMap();
103 (*gs_procmap)[fd] = proc_data;
104 return 1;
105 }
106
107 void wxCheckForFinishedChildren()
108 {
109 wxProcMap::iterator it;
110 if (!gs_procmap) return;
111 if (gs_procmap->size() == 0) {
112 // Map empty, delete it.
113 delete gs_procmap;
114 gs_procmap = NULL;
115 return;
116 }
117 for (it = gs_procmap->begin();it != gs_procmap->end(); ++it)
118 {
119 wxEndProcessData *proc_data = it->second;
120 int pid = (proc_data->pid > 0) ? proc_data->pid : -(proc_data->pid);
121 int status = 0;
122 // has the process really terminated?
123 int rc = waitpid(pid, &status, WNOHANG);
124 if (rc == 0)
125 continue; // no, it didn't exit yet, continue waiting
126
127 // set exit code to -1 if something bad happened
128 proc_data->exitcode = rc != -1 && WIFEXITED(status) ?
129 WEXITSTATUS(status) : -1;
130
131 // child exited, end waiting
132 close(it->first);
133
134 // don't call us again!
135 gs_procmap->erase(it->first);
136
137 wxHandleProcessTermination(proc_data);
138
139 // Iterator is invalid. Handle any further children in subsequent
140 // calls.
141 break;
142 }
143 }
144
145 // ----------------------------------------------------------------------------
146 // misc
147 // ----------------------------------------------------------------------------
148
149 // Emit a beeeeeep
150 #ifndef __EMX__
151 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
152 void wxBell()
153 {
154 // Use current setting for the bell
155 XBell ((Display*) wxGetDisplay(), 0);
156 }
157 #endif
158
159 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
160 {
161 // get X protocol version
162 Display *display = wxGlobalDisplay();
163 if (display)
164 {
165 if ( verMaj )
166 *verMaj = ProtocolVersion (display);
167 if ( verMin )
168 *verMin = ProtocolRevision (display);
169 }
170
171 return wxPORT_X11;
172 }
173
174 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
175 {
176 return new wxEventLoop;
177 }
178
179 // ----------------------------------------------------------------------------
180 // display info
181 // ----------------------------------------------------------------------------
182
183 void wxGetMousePosition( int* x, int* y )
184 {
185 #if wxUSE_NANOX
186 // TODO
187 *x = 0;
188 *y = 0;
189 #else
190 XMotionEvent xev;
191 Window root, child;
192 XQueryPointer((Display*) wxGetDisplay(),
193 DefaultRootWindow((Display*) wxGetDisplay()),
194 &root, &child,
195 &(xev.x_root), &(xev.y_root),
196 &(xev.x), &(xev.y),
197 &(xev.state));
198 *x = xev.x_root;
199 *y = xev.y_root;
200 #endif
201 };
202
203 // Return true if we have a colour display
204 bool wxColourDisplay()
205 {
206 return wxDisplayDepth() > 1;
207 }
208
209 // Returns depth of screen
210 int wxDisplayDepth()
211 {
212 Display *dpy = (Display*) wxGetDisplay();
213
214 return DefaultDepth (dpy, DefaultScreen (dpy));
215 }
216
217 // Get size of display
218 void wxDisplaySize(int *width, int *height)
219 {
220 Display *dpy = (Display*) wxGetDisplay();
221
222 if ( width )
223 *width = DisplayWidth (dpy, DefaultScreen (dpy));
224 if ( height )
225 *height = DisplayHeight (dpy, DefaultScreen (dpy));
226 }
227
228 void wxDisplaySizeMM(int *width, int *height)
229 {
230 Display *dpy = (Display*) wxGetDisplay();
231
232 if ( width )
233 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
234 if ( height )
235 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
236 }
237
238 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
239 {
240 return wxGenericFindWindowAtPoint(pt);
241 }
242
243
244 // Configurable display in wxX11 and wxMotif
245 static Display *gs_currentDisplay = NULL;
246 static wxString gs_displayName;
247
248 WXDisplay *wxGetDisplay()
249 {
250 return (WXDisplay *)gs_currentDisplay;
251 }
252
253 // close the current display
254 void wxCloseDisplay()
255 {
256 if ( gs_currentDisplay )
257 {
258 if ( XCloseDisplay(gs_currentDisplay) != 0 )
259 {
260 wxLogWarning(_("Failed to close the display \"%s\""),
261 gs_displayName.c_str());
262 }
263
264 gs_currentDisplay = NULL;
265 gs_displayName.clear();
266 }
267 }
268
269 bool wxSetDisplay(const wxString& displayName)
270 {
271 Display *dpy = XOpenDisplay
272 (
273 displayName.empty() ? NULL
274 : (const char *)displayName.mb_str()
275 );
276
277 if ( !dpy )
278 {
279 wxLogError(_("Failed to open display \"%s\"."), displayName.c_str());
280 return false;
281 }
282
283 wxCloseDisplay();
284
285 gs_currentDisplay = dpy;
286 gs_displayName = displayName;
287
288 return true;
289 }
290
291 wxString wxGetDisplayName()
292 {
293 return gs_displayName;
294 }
295
296 #include "wx/module.h"
297
298 // the module responsible for closing the X11 display at the program end
299 class wxX11DisplayModule : public wxModule
300 {
301 public:
302 virtual bool OnInit() { return true; }
303 virtual void OnExit() { wxCloseDisplay(); }
304
305 private:
306 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule)
307 };
308
309 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule)
310
311 // ----------------------------------------------------------------------------
312 // Some colour manipulation routines
313 // ----------------------------------------------------------------------------
314
315 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
316 {
317 int h = hsv->h;
318 int s = hsv->s;
319 int v = hsv->v;
320 int r = 0, g = 0, b = 0;
321 int i, f;
322 int p, q, t;
323 s = (s * wxMAX_RGB) / wxMAX_SV;
324 v = (v * wxMAX_RGB) / wxMAX_SV;
325 if (h == 360) h = 0;
326 if (s == 0) { h = 0; r = g = b = v; }
327 i = h / 60;
328 f = h % 60;
329 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
330 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
331 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
332 switch (i)
333 {
334 case 0: r = v, g = t, b = p; break;
335 case 1: r = q, g = v, b = p; break;
336 case 2: r = p, g = v, b = t; break;
337 case 3: r = p, g = q, b = v; break;
338 case 4: r = t, g = p, b = v; break;
339 case 5: r = v, g = p, b = q; break;
340 }
341 rgb->red = r << 8;
342 rgb->green = g << 8;
343 rgb->blue = b << 8;
344 }
345
346 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
347 {
348 int r = rgb->red >> 8;
349 int g = rgb->green >> 8;
350 int b = rgb->blue >> 8;
351 int maxv = wxMax3(r, g, b);
352 int minv = wxMin3(r, g, b);
353 int h = 0, s, v;
354 v = maxv;
355 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
356 else s = 0;
357 if (s == 0) h = 0;
358 else
359 {
360 int rc, gc, bc, hex = 0;
361 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
362 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
363 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
364 if (r == maxv) { h = bc - gc, hex = 0; }
365 else if (g == maxv) { h = rc - bc, hex = 2; }
366 else if (b == maxv) { h = gc - rc, hex = 4; }
367 h = hex * 60 + (h * 60 / wxMAX_RGB);
368 if (h < 0) h += 360;
369 }
370 hsv->h = h;
371 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
372 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
373 }
374
375 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
376 {
377 #if !wxUSE_NANOX
378 int llp;
379
380 int screen = DefaultScreen(d);
381 int num_colors = DisplayCells(d,screen);
382
383 XColor *color_defs = new XColor[num_colors];
384 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
385 XQueryColors(d,cmp,color_defs,num_colors);
386
387 wxHSV hsv_defs, hsv;
388 wxXColorToHSV(&hsv,xc);
389
390 int diff, min_diff = 0, pixel = 0;
391
392 for(llp = 0;llp < num_colors;llp++)
393 {
394 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
395 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
396 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
397 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
398 if (llp == 0) min_diff = diff;
399 if (min_diff > diff) { min_diff = diff; pixel = llp; }
400 if (min_diff == 0) break;
401 }
402
403 xc -> red = color_defs[pixel].red;
404 xc -> green = color_defs[pixel].green;
405 xc -> blue = color_defs[pixel].blue;
406 xc -> flags = DoRed | DoGreen | DoBlue;
407
408 /* FIXME, TODO
409 if (!XAllocColor(d,cmp,xc))
410 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
411 */
412
413 delete[] color_defs;
414 #endif
415 }
416
417 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
418 {
419 if (!XAllocColor(d,cmp,xc))
420 {
421 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
422 wxAllocNearestColor(d,cmp,xc);
423 }
424 }
425
426 #ifdef __WXDEBUG__
427 wxString wxGetXEventName(XEvent& event)
428 {
429 #if wxUSE_NANOX
430 wxString str(wxT("(some event)"));
431 return str;
432 #else
433 int type = event.xany.type;
434 static char* event_name[] = {
435 "", "unknown(-)", // 0-1
436 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
437 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
438 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
439 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
440 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
441 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
442 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
443 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
444 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
445 "ClientMessage", "MappingNotify", // 33-34
446 "unknown(+)"}; // 35
447 type = wxMin(35, type); type = wxMax(1, type);
448 return wxString::FromAscii(event_name[type]);
449 #endif
450 }
451 #endif
452
453 bool wxWindowIsVisible(Window win)
454 {
455 XWindowAttributes wa;
456 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
457
458 return (wa.map_state == IsViewable);
459 }