]>
Commit | Line | Data |
---|---|---|
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 | |
76 | void wxFlushEvents() | |
77 | { | |
78 | Display *display = (Display*) wxGetDisplay(); | |
79 | ||
80 | XSync (display, FALSE); | |
81 | ||
5e29f97a JS |
82 | // TODO for X11 |
83 | // ?? | |
83df96d6 JS |
84 | } |
85 | ||
89954433 | 86 | bool wxCheckForInterrupt(wxWindow *WXUNUSED(wnd)) |
83df96d6 | 87 | { |
670f9935 WS |
88 | wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented.")); |
89 | return false; | |
83df96d6 JS |
90 | } |
91 | ||
83df96d6 JS |
92 | // ---------------------------------------------------------------------------- |
93 | // misc | |
94 | // ---------------------------------------------------------------------------- | |
95 | ||
96 | // Emit a beeeeeep | |
189d1ae7 SN |
97 | #ifndef __EMX__ |
98 | // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp) | |
83df96d6 JS |
99 | void wxBell() |
100 | { | |
101 | // Use current setting for the bell | |
102 | XBell ((Display*) wxGetDisplay(), 0); | |
103 | } | |
189d1ae7 | 104 | #endif |
83df96d6 | 105 | |
8bb6b2c0 | 106 | wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const |
83df96d6 | 107 | { |
8bb6b2c0 VZ |
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; | |
83df96d6 JS |
119 | } |
120 | ||
2ddff00c | 121 | wxEventLoopBase* wxGUIAppTraits::CreateEventLoop() |
b46b1d59 VZ |
122 | { |
123 | return new wxEventLoop; | |
124 | } | |
125 | ||
83df96d6 JS |
126 | // ---------------------------------------------------------------------------- |
127 | // display info | |
128 | // ---------------------------------------------------------------------------- | |
129 | ||
130 | void wxGetMousePosition( int* x, int* y ) | |
131 | { | |
461e93f9 JS |
132 | #if wxUSE_NANOX |
133 | // TODO | |
134 | *x = 0; | |
135 | *y = 0; | |
136 | #else | |
83df96d6 JS |
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; | |
461e93f9 | 147 | #endif |
83df96d6 JS |
148 | }; |
149 | ||
670f9935 | 150 | // Return true if we have a colour display |
83df96d6 JS |
151 | bool wxColourDisplay() |
152 | { | |
153 | return wxDisplayDepth() > 1; | |
154 | } | |
155 | ||
156 | // Returns depth of screen | |
157 | int wxDisplayDepth() | |
158 | { | |
159 | Display *dpy = (Display*) wxGetDisplay(); | |
160 | ||
161 | return DefaultDepth (dpy, DefaultScreen (dpy)); | |
162 | } | |
163 | ||
164 | // Get size of display | |
165 | void 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 | ||
175 | void 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 | ||
b886fae6 VZ |
185 | wxWindow* wxFindWindowAtPoint(const wxPoint& pt) |
186 | { | |
187 | return wxGenericFindWindowAtPoint(pt); | |
188 | } | |
189 | ||
83df96d6 | 190 | |
5e29f97a | 191 | // Configurable display in wxX11 and wxMotif |
b886fae6 | 192 | static Display *gs_currentDisplay = NULL; |
83df96d6 JS |
193 | static wxString gs_displayName; |
194 | ||
195 | WXDisplay *wxGetDisplay() | |
196 | { | |
b886fae6 | 197 | return (WXDisplay *)gs_currentDisplay; |
83df96d6 JS |
198 | } |
199 | ||
b886fae6 VZ |
200 | // close the current display |
201 | void wxCloseDisplay() | |
83df96d6 | 202 | { |
b886fae6 | 203 | if ( gs_currentDisplay ) |
83df96d6 | 204 | { |
b886fae6 VZ |
205 | if ( XCloseDisplay(gs_currentDisplay) != 0 ) |
206 | { | |
207 | wxLogWarning(_("Failed to close the display \"%s\""), | |
208 | gs_displayName.c_str()); | |
209 | } | |
83df96d6 | 210 | |
b886fae6 VZ |
211 | gs_currentDisplay = NULL; |
212 | gs_displayName.clear(); | |
83df96d6 | 213 | } |
b886fae6 | 214 | } |
83df96d6 | 215 | |
b886fae6 VZ |
216 | bool wxSetDisplay(const wxString& displayName) |
217 | { | |
0e4f7936 VZ |
218 | Display *dpy = XOpenDisplay |
219 | ( | |
220 | displayName.empty() ? NULL | |
221 | : (const char *)displayName.mb_str() | |
222 | ); | |
b886fae6 VZ |
223 | |
224 | if ( !dpy ) | |
225 | { | |
226 | wxLogError(_("Failed to open display \"%s\"."), displayName.c_str()); | |
227 | return false; | |
83df96d6 | 228 | } |
b886fae6 VZ |
229 | |
230 | wxCloseDisplay(); | |
231 | ||
232 | gs_currentDisplay = dpy; | |
233 | gs_displayName = displayName; | |
234 | ||
235 | return true; | |
83df96d6 JS |
236 | } |
237 | ||
238 | wxString wxGetDisplayName() | |
239 | { | |
240 | return gs_displayName; | |
241 | } | |
242 | ||
b886fae6 VZ |
243 | #include "wx/module.h" |
244 | ||
245 | // the module responsible for closing the X11 display at the program end | |
246 | class wxX11DisplayModule : public wxModule | |
83df96d6 | 247 | { |
b886fae6 VZ |
248 | public: |
249 | virtual bool OnInit() { return true; } | |
250 | virtual void OnExit() { wxCloseDisplay(); } | |
251 | ||
252 | private: | |
253 | DECLARE_DYNAMIC_CLASS(wxX11DisplayModule) | |
254 | }; | |
255 | ||
256 | IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule) | |
83df96d6 | 257 | |
83df96d6 JS |
258 | // ---------------------------------------------------------------------------- |
259 | // Some colour manipulation routines | |
260 | // ---------------------------------------------------------------------------- | |
261 | ||
262 | void 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 | ||
293 | void 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 | ||
322 | void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc) | |
323 | { | |
461e93f9 | 324 | #if !wxUSE_NANOX |
83df96d6 JS |
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; | |
461e93f9 | 361 | #endif |
83df96d6 JS |
362 | } |
363 | ||
364 | void 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 | ||
45ff6421 JS |
373 | wxString wxGetXEventName(XEvent& event) |
374 | { | |
461e93f9 JS |
375 | #if wxUSE_NANOX |
376 | wxString str(wxT("(some event)")); | |
377 | return str; | |
378 | #else | |
45ff6421 | 379 | int type = event.xany.type; |
ffbd0d14 | 380 | static const char* event_name[] = { |
521bf4ff WS |
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); | |
54385bdb | 394 | return wxString::FromAscii(event_name[type]); |
461e93f9 JS |
395 | #endif |
396 | } | |
45ff6421 | 397 | |
3a0b23eb JS |
398 | bool wxWindowIsVisible(Window win) |
399 | { | |
400 | XWindowAttributes wa; | |
401 | XGetWindowAttributes(wxGlobalDisplay(), win, &wa); | |
402 | ||
403 | return (wa.map_state == IsViewable); | |
404 | } |