]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // for compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if defined(__BORLANDC__)
19 // ============================================================================
21 // ============================================================================
23 // ----------------------------------------------------------------------------
25 // ----------------------------------------------------------------------------
31 #include "wx/window.h" // for wxTopLevelWindows
32 #include "wx/cursor.h"
33 #include "wx/msgdlg.h"
36 #include "wx/apptrait.h"
37 #include "wx/generic/private/timer.h"
38 #include "wx/evtloop.h"
45 #include <sys/types.h>
53 #if (defined(__SUNCC__) || defined(__CLCC__))
58 #pragma message disable nosimpint
61 #include "wx/x11/private.h"
63 #include "X11/Xutil.h"
66 #pragma message enable nosimpint
69 // ----------------------------------------------------------------------------
70 // async event processing
71 // ----------------------------------------------------------------------------
73 // Consume all events until no more left
76 Display
*display
= (Display
*) wxGetDisplay();
78 XSync (display
, FALSE
);
84 bool wxCheckForInterrupt(wxWindow
*WXUNUSED(wnd
))
86 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
97 // Use current setting for the bell
98 XBell ((Display
*) wxGetDisplay(), 0);
101 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
103 // get X protocol version
104 Display
*display
= wxGlobalDisplay();
108 *verMaj
= ProtocolVersion (display
);
110 *verMin
= ProtocolRevision (display
);
116 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
118 return new wxEventLoop
;
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 void wxGetMousePosition( int* x
, int* y
)
134 XQueryPointer((Display
*) wxGetDisplay(),
135 DefaultRootWindow((Display
*) wxGetDisplay()),
137 &(xev
.x_root
), &(xev
.y_root
),
145 // Return true if we have a colour display
146 bool wxColourDisplay()
148 return wxDisplayDepth() > 1;
151 // Returns depth of screen
154 Display
*dpy
= (Display
*) wxGetDisplay();
156 return DefaultDepth (dpy
, DefaultScreen (dpy
));
159 // Get size of display
160 void wxDisplaySize(int *width
, int *height
)
162 Display
*dpy
= (Display
*) wxGetDisplay();
165 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
167 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
170 void wxDisplaySizeMM(int *width
, int *height
)
172 Display
*dpy
= (Display
*) wxGetDisplay();
175 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
177 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
180 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
182 return wxGenericFindWindowAtPoint(pt
);
186 // Configurable display in wxX11 and wxMotif
187 static Display
*gs_currentDisplay
= NULL
;
188 static wxString gs_displayName
;
190 WXDisplay
*wxGetDisplay()
192 return (WXDisplay
*)gs_currentDisplay
;
195 // close the current display
196 void wxCloseDisplay()
198 if ( gs_currentDisplay
)
200 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
202 wxLogWarning(_("Failed to close the display \"%s\""),
203 gs_displayName
.c_str());
206 gs_currentDisplay
= NULL
;
207 gs_displayName
.clear();
211 bool wxSetDisplay(const wxString
& displayName
)
213 Display
*dpy
= XOpenDisplay
215 displayName
.empty() ? NULL
216 : (const char *)displayName
.mb_str()
221 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
227 gs_currentDisplay
= dpy
;
228 gs_displayName
= displayName
;
233 wxString
wxGetDisplayName()
235 return gs_displayName
;
238 #include "wx/module.h"
240 // the module responsible for closing the X11 display at the program end
241 class wxX11DisplayModule
: public wxModule
244 virtual bool OnInit() { return true; }
245 virtual void OnExit() { wxCloseDisplay(); }
248 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
251 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
253 // ----------------------------------------------------------------------------
254 // Some colour manipulation routines
255 // ----------------------------------------------------------------------------
257 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
262 int r
= 0, g
= 0, b
= 0;
265 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
266 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
268 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
271 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
272 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
273 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
276 case 0: r
= v
, g
= t
, b
= p
; break;
277 case 1: r
= q
, g
= v
, b
= p
; break;
278 case 2: r
= p
, g
= v
, b
= t
; break;
279 case 3: r
= p
, g
= q
, b
= v
; break;
280 case 4: r
= t
, g
= p
, b
= v
; break;
281 case 5: r
= v
, g
= p
, b
= q
; break;
288 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
290 int r
= rgb
->red
>> 8;
291 int g
= rgb
->green
>> 8;
292 int b
= rgb
->blue
>> 8;
293 int maxv
= wxMax3(r
, g
, b
);
294 int minv
= wxMin3(r
, g
, b
);
297 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
302 int rc
, gc
, bc
, hex
= 0;
303 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
304 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
305 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
306 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
307 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
308 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
309 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
313 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
314 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
317 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
322 int screen
= DefaultScreen(d
);
323 int num_colors
= DisplayCells(d
,screen
);
325 XColor
*color_defs
= new XColor
[num_colors
];
326 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
327 XQueryColors(d
,cmp
,color_defs
,num_colors
);
330 wxXColorToHSV(&hsv
,xc
);
332 int diff
, min_diff
= 0, pixel
= 0;
334 for(llp
= 0;llp
< num_colors
;llp
++)
336 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
337 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
338 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
339 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
340 if (llp
== 0) min_diff
= diff
;
341 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
342 if (min_diff
== 0) break;
345 xc
-> red
= color_defs
[pixel
].red
;
346 xc
-> green
= color_defs
[pixel
].green
;
347 xc
-> blue
= color_defs
[pixel
].blue
;
348 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
351 if (!XAllocColor(d,cmp,xc))
352 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
359 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
361 if (!XAllocColor(d
,cmp
,xc
))
363 // cout << "wxAllocColor : Warning : Cannot allocate color, attempt find nearest !\n";
364 wxAllocNearestColor(d
,cmp
,xc
);
368 wxString
wxGetXEventName(XEvent
& event
)
371 wxString
str(wxT("(some event)"));
374 int type
= event
.xany
.type
;
375 static const char* event_name
[] = {
376 "", "unknown(-)", // 0-1
377 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
378 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
379 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
380 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
381 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
382 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
383 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
384 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
385 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
386 "ClientMessage", "MappingNotify", // 33-34
388 type
= wxMin(35, type
); type
= wxMax(1, type
);
389 return wxString::FromAscii(event_name
[type
]);