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