1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // (c) 2013 Rob Bresalier
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // for compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
16 #if defined(__BORLANDC__)
20 #include "wx/private/eventloopsourcesmanager.h"
22 // ============================================================================
24 // ============================================================================
26 // ----------------------------------------------------------------------------
28 // ----------------------------------------------------------------------------
34 #include "wx/window.h" // for wxTopLevelWindows
35 #include "wx/cursor.h"
36 #include "wx/msgdlg.h"
39 #include "wx/apptrait.h"
40 #include "wx/generic/private/timer.h"
41 #include "wx/evtloop.h"
48 #include <sys/types.h>
56 #if (defined(__SUNCC__) || defined(__CLCC__))
61 #pragma message disable nosimpint
64 #include "wx/x11/private.h"
66 #include "X11/Xutil.h"
69 #pragma message enable nosimpint
72 // ----------------------------------------------------------------------------
73 // async event processing
74 // ----------------------------------------------------------------------------
76 // Consume all events until no more left
79 Display
*display
= (Display
*) wxGetDisplay();
81 XSync (display
, FALSE
);
87 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
89 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
100 // Use current setting for the bell
101 XBell ((Display
*) wxGetDisplay(), 0);
104 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
106 // get X protocol version
107 Display
*display
= wxGlobalDisplay();
111 *verMaj
= ProtocolVersion (display
);
113 *verMin
= ProtocolRevision (display
);
119 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
121 return new wxEventLoop
;
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 void wxGetMousePosition( int* x
, int* y
)
137 XQueryPointer((Display
*) wxGetDisplay(),
138 DefaultRootWindow((Display
*) wxGetDisplay()),
140 &(xev
.x_root
), &(xev
.y_root
),
148 // Return true if we have a colour display
149 bool wxColourDisplay()
151 return wxDisplayDepth() > 1;
154 // Returns depth of screen
157 Display
*dpy
= (Display
*) wxGetDisplay();
159 return DefaultDepth (dpy
, DefaultScreen (dpy
));
162 // Get size of display
163 void wxDisplaySize(int *width
, int *height
)
165 Display
*dpy
= (Display
*) wxGetDisplay();
168 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
170 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
173 void wxDisplaySizeMM(int *width
, int *height
)
175 Display
*dpy
= (Display
*) wxGetDisplay();
178 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
180 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
183 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
185 return wxGenericFindWindowAtPoint(pt
);
189 // Configurable display in wxX11 and wxMotif
190 static Display
*gs_currentDisplay
= NULL
;
191 static wxString gs_displayName
;
193 WXDisplay
*wxGetDisplay()
195 return (WXDisplay
*)gs_currentDisplay
;
198 // close the current display
199 void wxCloseDisplay()
201 if ( gs_currentDisplay
)
203 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
205 wxLogWarning(_("Failed to close the display \"%s\""),
206 gs_displayName
.c_str());
209 gs_currentDisplay
= NULL
;
210 gs_displayName
.clear();
214 bool wxSetDisplay(const wxString
& displayName
)
216 Display
*dpy
= XOpenDisplay
218 displayName
.empty() ? NULL
219 : (const char *)displayName
.mb_str()
224 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
230 gs_currentDisplay
= dpy
;
231 gs_displayName
= displayName
;
236 wxString
wxGetDisplayName()
238 return gs_displayName
;
241 #include "wx/module.h"
243 // the module responsible for closing the X11 display at the program end
244 class wxX11DisplayModule
: public wxModule
247 virtual bool OnInit() { return true; }
248 virtual void OnExit() { wxCloseDisplay(); }
251 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
254 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
256 // ----------------------------------------------------------------------------
257 // Some colour manipulation routines
258 // ----------------------------------------------------------------------------
260 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
265 int r
= 0, g
= 0, b
= 0;
268 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
269 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
271 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
274 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
275 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
276 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
279 case 0: r
= v
, g
= t
, b
= p
; break;
280 case 1: r
= q
, g
= v
, b
= p
; break;
281 case 2: r
= p
, g
= v
, b
= t
; break;
282 case 3: r
= p
, g
= q
, b
= v
; break;
283 case 4: r
= t
, g
= p
, b
= v
; break;
284 case 5: r
= v
, g
= p
, b
= q
; break;
291 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
293 int r
= rgb
->red
>> 8;
294 int g
= rgb
->green
>> 8;
295 int b
= rgb
->blue
>> 8;
296 int maxv
= wxMax3(r
, g
, b
);
297 int minv
= wxMin3(r
, g
, b
);
300 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
305 int rc
, gc
, bc
, hex
= 0;
306 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
307 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
308 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
309 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
310 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
311 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
312 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
316 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
317 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
320 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
325 int screen
= DefaultScreen(d
);
326 int num_colors
= DisplayCells(d
,screen
);
328 XColor
*color_defs
= new XColor
[num_colors
];
329 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
330 XQueryColors(d
,cmp
,color_defs
,num_colors
);
333 wxXColorToHSV(&hsv
,xc
);
335 int diff
, min_diff
= 0, pixel
= 0;
337 for(llp
= 0;llp
< num_colors
;llp
++)
339 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
340 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
341 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
342 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
343 if (llp
== 0) min_diff
= diff
;
344 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
345 if (min_diff
== 0) break;
348 xc
-> red
= color_defs
[pixel
].red
;
349 xc
-> green
= color_defs
[pixel
].green
;
350 xc
-> blue
= color_defs
[pixel
].blue
;
351 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
354 if (!XAllocColor(d,cmp,xc))
355 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
362 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
364 if (!XAllocColor(d
,cmp
,xc
))
366 // cout << "wxAllocColor : Warning : Cannot allocate color, attempt find nearest !\n";
367 wxAllocNearestColor(d
,cmp
,xc
);
371 wxString
wxGetXEventName(XEvent
& event
)
374 wxString
str(wxT("(some event)"));
377 int type
= event
.xany
.type
;
378 static const char* event_name
[] = {
379 "", "unknown(-)", // 0-1
380 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
381 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
382 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
383 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
384 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
385 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
386 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
387 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
388 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
389 "ClientMessage", "MappingNotify", // 33-34
391 type
= wxMin(35, type
); type
= wxMax(1, type
);
392 return wxString::FromAscii(event_name
[type
]);
396 #if wxUSE_EVENTLOOP_SOURCE
398 class wxX11EventLoopSourcesManager
: public wxEventLoopSourcesManagerBase
402 AddSourceForFD(int WXUNUSED(fd
),
403 wxEventLoopSourceHandler
* WXUNUSED(handler
),
406 wxFAIL_MSG("Monitoring FDs in the main loop is not implemented in wxX11");
412 wxEventLoopSourcesManagerBase
* wxGUIAppTraits::GetEventLoopSourcesManager()
414 static wxX11EventLoopSourcesManager s_eventLoopSourcesManager
;
416 return &s_eventLoopSourcesManager
;
419 #endif // wxUSE_EVENTLOOP_SOURCE