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