]> git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
use SubclassWin() insteaf of just wxAssociateWinWithHandle() to make sure we generate...
[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 #endif
33
34 #include "wx/apptrait.h"
35 #include "wx/msgdlg.h"
36 #include "wx/cursor.h"
37
38 #include <ctype.h>
39 #include <stdarg.h>
40 #include <dirent.h>
41 #include <string.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <unistd.h>
45 #include <sys/wait.h>
46 #include <pwd.h>
47 #include <errno.h>
48 // #include <netdb.h>
49 #include <signal.h>
50
51 #if (defined(__SUNCC__) || defined(__CLCC__))
52 #include <sysent.h>
53 #endif
54
55 #ifdef __VMS__
56 #pragma message disable nosimpint
57 #endif
58
59 #include "wx/unix/execute.h"
60
61 #include "wx/x11/private.h"
62
63 #include "X11/Xutil.h"
64
65 #ifdef __VMS__
66 #pragma message enable nosimpint
67 #endif
68
69 // ----------------------------------------------------------------------------
70 // async event processing
71 // ----------------------------------------------------------------------------
72
73 // Consume all events until no more left
74 void wxFlushEvents()
75 {
76 Display *display = (Display*) wxGetDisplay();
77
78 XSync (display, FALSE);
79
80 // TODO for X11
81 // ??
82 }
83
84 bool wxCheckForInterrupt(wxWindow *wnd)
85 {
86 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
87 return false;
88 }
89
90 // ----------------------------------------------------------------------------
91 // wxExecute stuff
92 // ----------------------------------------------------------------------------
93
94 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
95 {
96 // TODO
97 return 0;
98 }
99
100 // ----------------------------------------------------------------------------
101 // misc
102 // ----------------------------------------------------------------------------
103
104 // Emit a beeeeeep
105 #ifndef __EMX__
106 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
107 void wxBell()
108 {
109 // Use current setting for the bell
110 XBell ((Display*) wxGetDisplay(), 0);
111 }
112 #endif
113
114 wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
115 {
116 static wxToolkitInfo info;
117 info.shortName = _T("x11univ");
118 info.name = _T("wxX11");
119 info.versionMajor = 0;
120 info.versionMinor = 0;
121 info.os = wxX11;
122 return info;
123 }
124
125 // ----------------------------------------------------------------------------
126 // display info
127 // ----------------------------------------------------------------------------
128
129 void wxGetMousePosition( int* x, int* y )
130 {
131 #if wxUSE_NANOX
132 // TODO
133 *x = 0;
134 *y = 0;
135 #else
136 XMotionEvent xev;
137 Window root, child;
138 XQueryPointer((Display*) wxGetDisplay(),
139 DefaultRootWindow((Display*) wxGetDisplay()),
140 &root, &child,
141 &(xev.x_root), &(xev.y_root),
142 &(xev.x), &(xev.y),
143 &(xev.state));
144 *x = xev.x_root;
145 *y = xev.y_root;
146 #endif
147 };
148
149 // Return true if we have a colour display
150 bool wxColourDisplay()
151 {
152 return wxDisplayDepth() > 1;
153 }
154
155 // Returns depth of screen
156 int wxDisplayDepth()
157 {
158 Display *dpy = (Display*) wxGetDisplay();
159
160 return DefaultDepth (dpy, DefaultScreen (dpy));
161 }
162
163 // Get size of display
164 void wxDisplaySize(int *width, int *height)
165 {
166 Display *dpy = (Display*) wxGetDisplay();
167
168 if ( width )
169 *width = DisplayWidth (dpy, DefaultScreen (dpy));
170 if ( height )
171 *height = DisplayHeight (dpy, DefaultScreen (dpy));
172 }
173
174 void wxDisplaySizeMM(int *width, int *height)
175 {
176 Display *dpy = (Display*) wxGetDisplay();
177
178 if ( width )
179 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
180 if ( height )
181 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
182 }
183
184 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
185 {
186 // This is supposed to return desktop dimensions minus any window
187 // manager panels, menus, taskbars, etc. If there is a way to do that
188 // for this platform please fix this function, otherwise it defaults
189 // to the entire desktop.
190 if (x) *x = 0;
191 if (y) *y = 0;
192 wxDisplaySize(width, height);
193 }
194
195
196 // Configurable display in wxX11 and wxMotif
197 static WXDisplay *gs_currentDisplay = NULL;
198 static wxString gs_displayName;
199
200 WXDisplay *wxGetDisplay()
201 {
202 if (gs_currentDisplay)
203 return gs_currentDisplay;
204 return wxApp::GetDisplay();
205 }
206
207 bool wxSetDisplay(const wxString& display_name)
208 {
209 gs_displayName = display_name;
210
211 if ( display_name.empty() )
212 {
213 gs_currentDisplay = NULL;
214
215 return true;
216 }
217 else
218 {
219 Display* display = XOpenDisplay((char*) display_name.c_str());
220
221 if (display)
222 {
223 gs_currentDisplay = (WXDisplay*) display;
224 return true;
225 }
226 else
227 return false;
228 }
229 }
230
231 wxString wxGetDisplayName()
232 {
233 return gs_displayName;
234 }
235
236 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
237 {
238 return wxGenericFindWindowAtPoint(pt);
239 }
240
241 // ----------------------------------------------------------------------------
242 // Some colour manipulation routines
243 // ----------------------------------------------------------------------------
244
245 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
246 {
247 int h = hsv->h;
248 int s = hsv->s;
249 int v = hsv->v;
250 int r = 0, g = 0, b = 0;
251 int i, f;
252 int p, q, t;
253 s = (s * wxMAX_RGB) / wxMAX_SV;
254 v = (v * wxMAX_RGB) / wxMAX_SV;
255 if (h == 360) h = 0;
256 if (s == 0) { h = 0; r = g = b = v; }
257 i = h / 60;
258 f = h % 60;
259 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
260 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
261 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
262 switch (i)
263 {
264 case 0: r = v, g = t, b = p; break;
265 case 1: r = q, g = v, b = p; break;
266 case 2: r = p, g = v, b = t; break;
267 case 3: r = p, g = q, b = v; break;
268 case 4: r = t, g = p, b = v; break;
269 case 5: r = v, g = p, b = q; break;
270 }
271 rgb->red = r << 8;
272 rgb->green = g << 8;
273 rgb->blue = b << 8;
274 }
275
276 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
277 {
278 int r = rgb->red >> 8;
279 int g = rgb->green >> 8;
280 int b = rgb->blue >> 8;
281 int maxv = wxMax3(r, g, b);
282 int minv = wxMin3(r, g, b);
283 int h = 0, s, v;
284 v = maxv;
285 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
286 else s = 0;
287 if (s == 0) h = 0;
288 else
289 {
290 int rc, gc, bc, hex = 0;
291 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
292 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
293 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
294 if (r == maxv) { h = bc - gc, hex = 0; }
295 else if (g == maxv) { h = rc - bc, hex = 2; }
296 else if (b == maxv) { h = gc - rc, hex = 4; }
297 h = hex * 60 + (h * 60 / wxMAX_RGB);
298 if (h < 0) h += 360;
299 }
300 hsv->h = h;
301 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
302 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
303 }
304
305 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
306 {
307 #if !wxUSE_NANOX
308 int llp;
309
310 int screen = DefaultScreen(d);
311 int num_colors = DisplayCells(d,screen);
312
313 XColor *color_defs = new XColor[num_colors];
314 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
315 XQueryColors(d,cmp,color_defs,num_colors);
316
317 wxHSV hsv_defs, hsv;
318 wxXColorToHSV(&hsv,xc);
319
320 int diff, min_diff = 0, pixel = 0;
321
322 for(llp = 0;llp < num_colors;llp++)
323 {
324 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
325 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
326 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
327 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
328 if (llp == 0) min_diff = diff;
329 if (min_diff > diff) { min_diff = diff; pixel = llp; }
330 if (min_diff == 0) break;
331 }
332
333 xc -> red = color_defs[pixel].red;
334 xc -> green = color_defs[pixel].green;
335 xc -> blue = color_defs[pixel].blue;
336 xc -> flags = DoRed | DoGreen | DoBlue;
337
338 /* FIXME, TODO
339 if (!XAllocColor(d,cmp,xc))
340 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
341 */
342
343 delete[] color_defs;
344 #endif
345 }
346
347 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
348 {
349 if (!XAllocColor(d,cmp,xc))
350 {
351 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
352 wxAllocNearestColor(d,cmp,xc);
353 }
354 }
355
356 #ifdef __WXDEBUG__
357 wxString wxGetXEventName(XEvent& event)
358 {
359 #if wxUSE_NANOX
360 wxString str(wxT("(some event)"));
361 return str;
362 #else
363 int type = event.xany.type;
364 static char* event_name[] = {
365 "", "unknown(-)", // 0-1
366 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
367 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
368 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
369 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
370 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
371 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
372 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
373 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
374 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
375 "ClientMessage", "MappingNotify", // 33-34
376 "unknown(+)"}; // 35
377 type = wxMin(35, type); type = wxMax(1, type);
378 return wxString::FromAscii(event_name[type]);
379 #endif
380 }
381 #endif
382
383 bool wxWindowIsVisible(Window win)
384 {
385 XWindowAttributes wa;
386 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
387
388 return (wa.map_state == IsViewable);
389 }