]> git.saurik.com Git - wxWidgets.git/blame - src/x11/utils.cpp
Don't use "Cancel" button in the about dialog of the listctrl sample.
[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
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
76void 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 86bool 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
97void wxBell()
98{
99 // Use current setting for the bell
100 XBell ((Display*) wxGetDisplay(), 0);
101}
102
8bb6b2c0 103wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
83df96d6 104{
8bb6b2c0
VZ
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;
83df96d6
JS
116}
117
2ddff00c 118wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
b46b1d59
VZ
119{
120 return new wxEventLoop;
121}
122
83df96d6
JS
123// ----------------------------------------------------------------------------
124// display info
125// ----------------------------------------------------------------------------
126
127void wxGetMousePosition( int* x, int* y )
128{
461e93f9
JS
129#if wxUSE_NANOX
130 // TODO
131 *x = 0;
132 *y = 0;
133#else
83df96d6
JS
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;
461e93f9 144#endif
83df96d6
JS
145};
146
670f9935 147// Return true if we have a colour display
83df96d6
JS
148bool wxColourDisplay()
149{
150 return wxDisplayDepth() > 1;
151}
152
153// Returns depth of screen
154int wxDisplayDepth()
155{
156 Display *dpy = (Display*) wxGetDisplay();
157
158 return DefaultDepth (dpy, DefaultScreen (dpy));
159}
160
161// Get size of display
162void 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
172void 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
b886fae6
VZ
182wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
183{
184 return wxGenericFindWindowAtPoint(pt);
185}
186
83df96d6 187
5e29f97a 188// Configurable display in wxX11 and wxMotif
b886fae6 189static Display *gs_currentDisplay = NULL;
83df96d6
JS
190static wxString gs_displayName;
191
192WXDisplay *wxGetDisplay()
193{
b886fae6 194 return (WXDisplay *)gs_currentDisplay;
83df96d6
JS
195}
196
b886fae6
VZ
197// close the current display
198void wxCloseDisplay()
83df96d6 199{
b886fae6 200 if ( gs_currentDisplay )
83df96d6 201 {
b886fae6
VZ
202 if ( XCloseDisplay(gs_currentDisplay) != 0 )
203 {
204 wxLogWarning(_("Failed to close the display \"%s\""),
205 gs_displayName.c_str());
206 }
83df96d6 207
b886fae6
VZ
208 gs_currentDisplay = NULL;
209 gs_displayName.clear();
83df96d6 210 }
b886fae6 211}
83df96d6 212
b886fae6
VZ
213bool wxSetDisplay(const wxString& displayName)
214{
0e4f7936
VZ
215 Display *dpy = XOpenDisplay
216 (
217 displayName.empty() ? NULL
218 : (const char *)displayName.mb_str()
219 );
b886fae6
VZ
220
221 if ( !dpy )
222 {
223 wxLogError(_("Failed to open display \"%s\"."), displayName.c_str());
224 return false;
83df96d6 225 }
b886fae6
VZ
226
227 wxCloseDisplay();
228
229 gs_currentDisplay = dpy;
230 gs_displayName = displayName;
231
232 return true;
83df96d6
JS
233}
234
235wxString wxGetDisplayName()
236{
237 return gs_displayName;
238}
239
b886fae6
VZ
240#include "wx/module.h"
241
242// the module responsible for closing the X11 display at the program end
243class wxX11DisplayModule : public wxModule
83df96d6 244{
b886fae6
VZ
245public:
246 virtual bool OnInit() { return true; }
247 virtual void OnExit() { wxCloseDisplay(); }
248
249private:
250 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule)
251};
252
253IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule, wxModule)
83df96d6 254
83df96d6
JS
255// ----------------------------------------------------------------------------
256// Some colour manipulation routines
257// ----------------------------------------------------------------------------
258
259void 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
290void 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
319void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
320{
461e93f9 321#if !wxUSE_NANOX
83df96d6
JS
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;
461e93f9 358#endif
83df96d6
JS
359}
360
361void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
362{
363 if (!XAllocColor(d,cmp,xc))
364 {
4c51a665 365 // cout << "wxAllocColor : Warning : Cannot allocate color, attempt find nearest !\n";
83df96d6
JS
366 wxAllocNearestColor(d,cmp,xc);
367 }
368}
369
45ff6421
JS
370wxString wxGetXEventName(XEvent& event)
371{
461e93f9
JS
372#if wxUSE_NANOX
373 wxString str(wxT("(some event)"));
374 return str;
375#else
45ff6421 376 int type = event.xany.type;
ffbd0d14 377 static const char* event_name[] = {
521bf4ff
WS
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);
54385bdb 391 return wxString::FromAscii(event_name[type]);
461e93f9
JS
392#endif
393}
45ff6421 394