]> git.saurik.com Git - wxWidgets.git/blame - src/x11/utils.cpp
use SubclassWin() insteaf of just wxAssociateWinWithHandle() to make sure we generate...
[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
670f9935
WS
32#endif
33
cd478a95 34#include "wx/apptrait.h"
83df96d6
JS
35#include "wx/msgdlg.h"
36#include "wx/cursor.h"
83df96d6
JS
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>
2b5f62a0 48// #include <netdb.h>
83df96d6
JS
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
bc797f4c 58
83df96d6
JS
59#include "wx/unix/execute.h"
60
bc797f4c 61#include "wx/x11/private.h"
256d631a 62
7266b672 63#include "X11/Xutil.h"
83df96d6 64
256d631a
JS
65#ifdef __VMS__
66#pragma message enable nosimpint
67#endif
68
83df96d6
JS
69// ----------------------------------------------------------------------------
70// async event processing
71// ----------------------------------------------------------------------------
72
73// Consume all events until no more left
74void wxFlushEvents()
75{
76 Display *display = (Display*) wxGetDisplay();
77
78 XSync (display, FALSE);
79
5e29f97a
JS
80 // TODO for X11
81 // ??
83df96d6
JS
82}
83
83df96d6
JS
84bool wxCheckForInterrupt(wxWindow *wnd)
85{
670f9935
WS
86 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
87 return false;
83df96d6
JS
88}
89
90// ----------------------------------------------------------------------------
91// wxExecute stuff
92// ----------------------------------------------------------------------------
83df96d6
JS
93
94int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
95{
707440dc
JS
96 // TODO
97 return 0;
707440dc 98}
83df96d6
JS
99
100// ----------------------------------------------------------------------------
101// misc
102// ----------------------------------------------------------------------------
103
104// Emit a beeeeeep
189d1ae7
SN
105#ifndef __EMX__
106// on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
83df96d6
JS
107void wxBell()
108{
109 // Use current setting for the bell
110 XBell ((Display*) wxGetDisplay(), 0);
111}
189d1ae7 112#endif
83df96d6 113
324899f6 114wxToolkitInfo& wxGUIAppTraits::GetToolkitInfo()
83df96d6 115{
a8eaaeb2
VS
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;
324899f6 122 return info;
83df96d6
JS
123}
124
83df96d6
JS
125// ----------------------------------------------------------------------------
126// display info
127// ----------------------------------------------------------------------------
128
129void wxGetMousePosition( int* x, int* y )
130{
461e93f9
JS
131#if wxUSE_NANOX
132 // TODO
133 *x = 0;
134 *y = 0;
135#else
83df96d6
JS
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;
461e93f9 146#endif
83df96d6
JS
147};
148
670f9935 149// Return true if we have a colour display
83df96d6
JS
150bool wxColourDisplay()
151{
152 return wxDisplayDepth() > 1;
153}
154
155// Returns depth of screen
156int wxDisplayDepth()
157{
158 Display *dpy = (Display*) wxGetDisplay();
159
160 return DefaultDepth (dpy, DefaultScreen (dpy));
161}
162
163// Get size of display
164void 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
174void 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
184void 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
5e29f97a 196// Configurable display in wxX11 and wxMotif
83df96d6
JS
197static WXDisplay *gs_currentDisplay = NULL;
198static wxString gs_displayName;
199
200WXDisplay *wxGetDisplay()
201{
202 if (gs_currentDisplay)
203 return gs_currentDisplay;
5e29f97a 204 return wxApp::GetDisplay();
83df96d6
JS
205}
206
207bool wxSetDisplay(const wxString& display_name)
208{
209 gs_displayName = display_name;
210
521bf4ff 211 if ( display_name.empty() )
83df96d6
JS
212 {
213 gs_currentDisplay = NULL;
214
521bf4ff 215 return true;
83df96d6
JS
216 }
217 else
218 {
461e93f9 219 Display* display = XOpenDisplay((char*) display_name.c_str());
83df96d6
JS
220
221 if (display)
222 {
223 gs_currentDisplay = (WXDisplay*) display;
521bf4ff 224 return true;
83df96d6
JS
225 }
226 else
670f9935 227 return false;
83df96d6
JS
228 }
229}
230
231wxString wxGetDisplayName()
232{
233 return gs_displayName;
234}
235
236wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
237{
238 return wxGenericFindWindowAtPoint(pt);
239}
240
83df96d6
JS
241// ----------------------------------------------------------------------------
242// Some colour manipulation routines
243// ----------------------------------------------------------------------------
244
245void 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
276void 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
305void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
306{
461e93f9 307#if !wxUSE_NANOX
83df96d6
JS
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;
461e93f9 344#endif
83df96d6
JS
345}
346
347void 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
45ff6421
JS
356#ifdef __WXDEBUG__
357wxString wxGetXEventName(XEvent& event)
358{
461e93f9
JS
359#if wxUSE_NANOX
360 wxString str(wxT("(some event)"));
361 return str;
362#else
45ff6421 363 int type = event.xany.type;
521bf4ff
WS
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);
54385bdb 378 return wxString::FromAscii(event_name[type]);
461e93f9
JS
379#endif
380}
45ff6421
JS
381#endif
382
3a0b23eb
JS
383bool wxWindowIsVisible(Window win)
384{
385 XWindowAttributes wa;
386 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
387
388 return (wa.map_state == IsViewable);
389}