]> git.saurik.com Git - wxWidgets.git/blame - src/x11/utils.cpp
add wxUSE_MINIFRAME wrapper
[wxWidgets.git] / src / x11 / utils.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
521bf4ff 2// Name: src/x11/utils.cpp
83df96d6
JS
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
65571936 9// Licence: wxWindows licence
83df96d6
JS
10/////////////////////////////////////////////////////////////////////////////
11
521bf4ff
WS
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#if defined(__BORLANDC__)
670f9935 16 #pragma hdrstop
521bf4ff
WS
17#endif
18
83df96d6
JS
19// ============================================================================
20// declarations
21// ============================================================================
22
23// ----------------------------------------------------------------------------
24// headers
25// ----------------------------------------------------------------------------
26
83df96d6 27#include "wx/utils.h"
670f9935
WS
28
29#ifndef WX_PRECOMP
30 #include "wx/app.h"
cdccdfab 31 #include "wx/window.h" // for wxTopLevelWindows
c8326d64 32 #include "wx/cursor.h"
246c5004 33 #include "wx/msgdlg.h"
670f9935
WS
34#endif
35
cd478a95 36#include "wx/apptrait.h"
c2ca375c 37#include "wx/generic/private/timer.h"
b46b1d59 38#include "wx/evtloop.h"
83df96d6
JS
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>
2b5f62a0 50// #include <netdb.h>
83df96d6
JS
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
bc797f4c 60
83df96d6
JS
61#include "wx/unix/execute.h"
62
bc797f4c 63#include "wx/x11/private.h"
256d631a 64
7266b672 65#include "X11/Xutil.h"
83df96d6 66
256d631a
JS
67#ifdef __VMS__
68#pragma message enable nosimpint
69#endif
70
83df96d6
JS
71// ----------------------------------------------------------------------------
72// async event processing
73// ----------------------------------------------------------------------------
74
75// Consume all events until no more left
76void wxFlushEvents()
77{
78 Display *display = (Display*) wxGetDisplay();
79
80 XSync (display, FALSE);
81
5e29f97a
JS
82 // TODO for X11
83 // ??
83df96d6
JS
84}
85
83df96d6
JS
86bool wxCheckForInterrupt(wxWindow *wnd)
87{
670f9935
WS
88 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
89 return false;
83df96d6
JS
90}
91
92// ----------------------------------------------------------------------------
93// wxExecute stuff
94// ----------------------------------------------------------------------------
83df96d6 95
bd00fe32
VZ
96WX_DECLARE_HASH_MAP( int, wxEndProcessData*, wxIntegerHash, wxIntegerEqual, wxProcMap );
97
98static wxProcMap *gs_procmap;
99
83df96d6
JS
100int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
101{
bd00fe32
VZ
102 if (!gs_procmap) gs_procmap = new wxProcMap();
103 (*gs_procmap)[fd] = proc_data;
104 return 1;
105}
106
107void 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;
43f51d80 115 return;
bd00fe32
VZ
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 }
707440dc 143}
83df96d6
JS
144
145// ----------------------------------------------------------------------------
146// misc
147// ----------------------------------------------------------------------------
148
149// Emit a beeeeeep
189d1ae7
SN
150#ifndef __EMX__
151// on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
83df96d6
JS
152void wxBell()
153{
154 // Use current setting for the bell
155 XBell ((Display*) wxGetDisplay(), 0);
156}
189d1ae7 157#endif
83df96d6 158
8bb6b2c0 159wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
83df96d6 160{
8bb6b2c0
VZ
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;
83df96d6
JS
172}
173
2ddff00c 174wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
175{
176 return new wxEventLoop;
177}
178
83df96d6
JS
179// ----------------------------------------------------------------------------
180// display info
181// ----------------------------------------------------------------------------
182
183void wxGetMousePosition( int* x, int* y )
184{
461e93f9
JS
185#if wxUSE_NANOX
186 // TODO
187 *x = 0;
188 *y = 0;
189#else
83df96d6
JS
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;
461e93f9 200#endif
83df96d6
JS
201};
202
670f9935 203// Return true if we have a colour display
83df96d6
JS
204bool wxColourDisplay()
205{
206 return wxDisplayDepth() > 1;
207}
208
209// Returns depth of screen
210int wxDisplayDepth()
211{
212 Display *dpy = (Display*) wxGetDisplay();
213
214 return DefaultDepth (dpy, DefaultScreen (dpy));
215}
216
217// Get size of display
218void 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
228void 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
238void wxClientDisplayRect(int *x, int *y, int *width, int *height)
239{
240 // This is supposed to return desktop dimensions minus any window
241 // manager panels, menus, taskbars, etc. If there is a way to do that
242 // for this platform please fix this function, otherwise it defaults
243 // to the entire desktop.
244 if (x) *x = 0;
245 if (y) *y = 0;
246 wxDisplaySize(width, height);
247}
248
b886fae6
VZ
249wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
250{
251 return wxGenericFindWindowAtPoint(pt);
252}
253
83df96d6 254
5e29f97a 255// Configurable display in wxX11 and wxMotif
b886fae6 256static Display *gs_currentDisplay = NULL;
83df96d6
JS
257static wxString gs_displayName;
258
259WXDisplay *wxGetDisplay()
260{
b886fae6 261 return (WXDisplay *)gs_currentDisplay;
83df96d6
JS
262}
263
b886fae6
VZ
264// close the current display
265void wxCloseDisplay()
83df96d6 266{
b886fae6 267 if ( gs_currentDisplay )
83df96d6 268 {
b886fae6
VZ
269 if ( XCloseDisplay(gs_currentDisplay) != 0 )
270 {
271 wxLogWarning(_("Failed to close the display \"%s\""),
272 gs_displayName.c_str());
273 }
83df96d6 274
b886fae6
VZ
275 gs_currentDisplay = NULL;
276 gs_displayName.clear();
83df96d6 277 }
b886fae6 278}
83df96d6 279
b886fae6
VZ
280bool wxSetDisplay(const wxString& displayName)
281{
0e4f7936
VZ
282 Display *dpy = XOpenDisplay
283 (
284 displayName.empty() ? NULL
285 : (const char *)displayName.mb_str()
286 );
b886fae6
VZ
287
288 if ( !dpy )
289 {
290 wxLogError(_("Failed to open display \"%s\"."), displayName.c_str());
291 return false;
83df96d6 292 }
b886fae6
VZ
293
294 wxCloseDisplay();
295
296 gs_currentDisplay = dpy;
297 gs_displayName = displayName;
298
299 return true;
83df96d6
JS
300}
301
302wxString wxGetDisplayName()
303{
304 return gs_displayName;
305}
306
b886fae6
VZ
307#include "wx/module.h"
308
309// the module responsible for closing the X11 display at the program end
310class wxX11DisplayModule : public wxModule
83df96d6 311{
b886fae6
VZ
312public:
313 virtual bool OnInit() { return true; }
314 virtual void OnExit() { wxCloseDisplay(); }
315
316private:
317 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule)
318};
319
320IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule)
83df96d6 321
83df96d6
JS
322// ----------------------------------------------------------------------------
323// Some colour manipulation routines
324// ----------------------------------------------------------------------------
325
326void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
327{
328 int h = hsv->h;
329 int s = hsv->s;
330 int v = hsv->v;
331 int r = 0, g = 0, b = 0;
332 int i, f;
333 int p, q, t;
334 s = (s * wxMAX_RGB) / wxMAX_SV;
335 v = (v * wxMAX_RGB) / wxMAX_SV;
336 if (h == 360) h = 0;
337 if (s == 0) { h = 0; r = g = b = v; }
338 i = h / 60;
339 f = h % 60;
340 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
341 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
342 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
343 switch (i)
344 {
345 case 0: r = v, g = t, b = p; break;
346 case 1: r = q, g = v, b = p; break;
347 case 2: r = p, g = v, b = t; break;
348 case 3: r = p, g = q, b = v; break;
349 case 4: r = t, g = p, b = v; break;
350 case 5: r = v, g = p, b = q; break;
351 }
352 rgb->red = r << 8;
353 rgb->green = g << 8;
354 rgb->blue = b << 8;
355}
356
357void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
358{
359 int r = rgb->red >> 8;
360 int g = rgb->green >> 8;
361 int b = rgb->blue >> 8;
362 int maxv = wxMax3(r, g, b);
363 int minv = wxMin3(r, g, b);
364 int h = 0, s, v;
365 v = maxv;
366 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
367 else s = 0;
368 if (s == 0) h = 0;
369 else
370 {
371 int rc, gc, bc, hex = 0;
372 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
373 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
374 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
375 if (r == maxv) { h = bc - gc, hex = 0; }
376 else if (g == maxv) { h = rc - bc, hex = 2; }
377 else if (b == maxv) { h = gc - rc, hex = 4; }
378 h = hex * 60 + (h * 60 / wxMAX_RGB);
379 if (h < 0) h += 360;
380 }
381 hsv->h = h;
382 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
383 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
384}
385
386void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
387{
461e93f9 388#if !wxUSE_NANOX
83df96d6
JS
389 int llp;
390
391 int screen = DefaultScreen(d);
392 int num_colors = DisplayCells(d,screen);
393
394 XColor *color_defs = new XColor[num_colors];
395 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
396 XQueryColors(d,cmp,color_defs,num_colors);
397
398 wxHSV hsv_defs, hsv;
399 wxXColorToHSV(&hsv,xc);
400
401 int diff, min_diff = 0, pixel = 0;
402
403 for(llp = 0;llp < num_colors;llp++)
404 {
405 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
406 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
407 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
408 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
409 if (llp == 0) min_diff = diff;
410 if (min_diff > diff) { min_diff = diff; pixel = llp; }
411 if (min_diff == 0) break;
412 }
413
414 xc -> red = color_defs[pixel].red;
415 xc -> green = color_defs[pixel].green;
416 xc -> blue = color_defs[pixel].blue;
417 xc -> flags = DoRed | DoGreen | DoBlue;
418
419/* FIXME, TODO
420 if (!XAllocColor(d,cmp,xc))
421 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
422*/
423
424 delete[] color_defs;
461e93f9 425#endif
83df96d6
JS
426}
427
428void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
429{
430 if (!XAllocColor(d,cmp,xc))
431 {
432 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
433 wxAllocNearestColor(d,cmp,xc);
434 }
435}
436
45ff6421
JS
437#ifdef __WXDEBUG__
438wxString wxGetXEventName(XEvent& event)
439{
461e93f9
JS
440#if wxUSE_NANOX
441 wxString str(wxT("(some event)"));
442 return str;
443#else
45ff6421 444 int type = event.xany.type;
521bf4ff
WS
445 static char* event_name[] = {
446 "", "unknown(-)", // 0-1
447 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
448 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
449 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
450 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
451 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
452 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
453 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
454 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
455 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
456 "ClientMessage", "MappingNotify", // 33-34
457 "unknown(+)"}; // 35
458 type = wxMin(35, type); type = wxMax(1, type);
54385bdb 459 return wxString::FromAscii(event_name[type]);
461e93f9
JS
460#endif
461}
45ff6421
JS
462#endif
463
3a0b23eb
JS
464bool wxWindowIsVisible(Window win)
465{
466 XWindowAttributes wa;
467 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
468
469 return (wa.map_state == IsViewable);
470}