1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // (c) 2013 Rob Bresalier
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if defined(__BORLANDC__)
19 #include "wx/private/eventloopsourcesmanager.h"
21 // ============================================================================
23 // ============================================================================
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
33 #include "wx/window.h" // for wxTopLevelWindows
34 #include "wx/cursor.h"
35 #include "wx/msgdlg.h"
38 #include "wx/apptrait.h"
39 #include "wx/generic/private/timer.h"
40 #include "wx/evtloop.h"
47 #include <sys/types.h>
55 #if (defined(__SUNCC__) || defined(__CLCC__))
60 #pragma message disable nosimpint
63 #include "wx/x11/private.h"
65 #include "X11/Xutil.h"
68 #pragma message enable nosimpint
71 // ----------------------------------------------------------------------------
72 // async event processing
73 // ----------------------------------------------------------------------------
75 // Consume all events until no more left
78 Display
*display
= (Display
*) wxGetDisplay();
80 XSync (display
, FALSE
);
86 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
88 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
99 // Use current setting for the bell
100 XBell ((Display
*) wxGetDisplay(), 0);
103 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
105 // get X protocol version
106 Display
*display
= wxGlobalDisplay();
110 *verMaj
= ProtocolVersion (display
);
112 *verMin
= ProtocolRevision (display
);
118 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
120 return new wxEventLoop
;
123 // ----------------------------------------------------------------------------
125 // ----------------------------------------------------------------------------
127 void wxGetMousePosition( int* x
, int* y
)
136 XQueryPointer((Display
*) wxGetDisplay(),
137 DefaultRootWindow((Display
*) wxGetDisplay()),
139 &(xev
.x_root
), &(xev
.y_root
),
147 // Return true if we have a colour display
148 bool wxColourDisplay()
150 return wxDisplayDepth() > 1;
153 // Returns depth of screen
156 Display
*dpy
= (Display
*) wxGetDisplay();
158 return DefaultDepth (dpy
, DefaultScreen (dpy
));
161 // Get size of display
162 void wxDisplaySize(int *width
, int *height
)
164 Display
*dpy
= (Display
*) wxGetDisplay();
167 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
169 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
172 void wxDisplaySizeMM(int *width
, int *height
)
174 Display
*dpy
= (Display
*) wxGetDisplay();
177 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
179 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
182 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
184 return wxGenericFindWindowAtPoint(pt
);
188 // Configurable display in wxX11 and wxMotif
189 static Display
*gs_currentDisplay
= NULL
;
190 static wxString gs_displayName
;
192 WXDisplay
*wxGetDisplay()
194 return (WXDisplay
*)gs_currentDisplay
;
197 // close the current display
198 void wxCloseDisplay()
200 if ( gs_currentDisplay
)
202 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
204 wxLogWarning(_("Failed to close the display \"%s\""),
205 gs_displayName
.c_str());
208 gs_currentDisplay
= NULL
;
209 gs_displayName
.clear();
213 bool wxSetDisplay(const wxString
& displayName
)
215 Display
*dpy
= XOpenDisplay
217 displayName
.empty() ? NULL
218 : (const char *)displayName
.mb_str()
223 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
229 gs_currentDisplay
= dpy
;
230 gs_displayName
= displayName
;
235 wxString
wxGetDisplayName()
237 return gs_displayName
;
240 #include "wx/module.h"
242 // the module responsible for closing the X11 display at the program end
243 class wxX11DisplayModule
: public wxModule
246 virtual bool OnInit() { return true; }
247 virtual void OnExit() { wxCloseDisplay(); }
250 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
253 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
255 // ----------------------------------------------------------------------------
256 // Some colour manipulation routines
257 // ----------------------------------------------------------------------------
259 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
264 int r
= 0, g
= 0, b
= 0;
267 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
268 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
270 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
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
;
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;
290 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
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
);
299 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
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
);
315 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
316 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
319 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
324 int screen
= DefaultScreen(d
);
325 int num_colors
= DisplayCells(d
,screen
);
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
);
332 wxXColorToHSV(&hsv
,xc
);
334 int diff
, min_diff
= 0, pixel
= 0;
336 for(llp
= 0;llp
< num_colors
;llp
++)
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;
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
;
353 if (!XAllocColor(d,cmp,xc))
354 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
361 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
363 if (!XAllocColor(d
,cmp
,xc
))
365 // cout << "wxAllocColor : Warning : Cannot allocate color, attempt find nearest !\n";
366 wxAllocNearestColor(d
,cmp
,xc
);
370 wxString
wxGetXEventName(XEvent
& event
)
373 wxString
str(wxT("(some event)"));
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
390 type
= wxMin(35, type
); type
= wxMax(1, type
);
391 return wxString::FromAscii(event_name
[type
]);
395 #if wxUSE_EVENTLOOP_SOURCE
397 class wxX11EventLoopSourcesManager
: public wxEventLoopSourcesManagerBase
401 AddSourceForFD(int WXUNUSED(fd
),
402 wxEventLoopSourceHandler
* WXUNUSED(handler
),
405 wxFAIL_MSG("Monitoring FDs in the main loop is not implemented in wxX11");
411 wxEventLoopSourcesManagerBase
* wxGUIAppTraits::GetEventLoopSourcesManager()
413 static wxX11EventLoopSourcesManager s_eventLoopSourcesManager
;
415 return &s_eventLoopSourcesManager
;
418 #endif // wxUSE_EVENTLOOP_SOURCE