]>
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/unix/execute.h"
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 // ----------------------------------------------------------------------------
98 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
101 // Use current setting for the bell
102 XBell ((Display
*) wxGetDisplay(), 0);
106 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
108 // get X protocol version
109 Display
*display
= wxGlobalDisplay();
113 *verMaj
= ProtocolVersion (display
);
115 *verMin
= ProtocolRevision (display
);
121 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
123 return new wxEventLoop
;
126 // ----------------------------------------------------------------------------
128 // ----------------------------------------------------------------------------
130 void wxGetMousePosition( int* x
, int* y
)
139 XQueryPointer((Display
*) wxGetDisplay(),
140 DefaultRootWindow((Display
*) wxGetDisplay()),
142 &(xev
.x_root
), &(xev
.y_root
),
150 // Return true if we have a colour display
151 bool wxColourDisplay()
153 return wxDisplayDepth() > 1;
156 // Returns depth of screen
159 Display
*dpy
= (Display
*) wxGetDisplay();
161 return DefaultDepth (dpy
, DefaultScreen (dpy
));
164 // Get size of display
165 void wxDisplaySize(int *width
, int *height
)
167 Display
*dpy
= (Display
*) wxGetDisplay();
170 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
172 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
175 void wxDisplaySizeMM(int *width
, int *height
)
177 Display
*dpy
= (Display
*) wxGetDisplay();
180 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
182 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
185 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
187 return wxGenericFindWindowAtPoint(pt
);
191 // Configurable display in wxX11 and wxMotif
192 static Display
*gs_currentDisplay
= NULL
;
193 static wxString gs_displayName
;
195 WXDisplay
*wxGetDisplay()
197 return (WXDisplay
*)gs_currentDisplay
;
200 // close the current display
201 void wxCloseDisplay()
203 if ( gs_currentDisplay
)
205 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
207 wxLogWarning(_("Failed to close the display \"%s\""),
208 gs_displayName
.c_str());
211 gs_currentDisplay
= NULL
;
212 gs_displayName
.clear();
216 bool wxSetDisplay(const wxString
& displayName
)
218 Display
*dpy
= XOpenDisplay
220 displayName
.empty() ? NULL
221 : (const char *)displayName
.mb_str()
226 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
232 gs_currentDisplay
= dpy
;
233 gs_displayName
= displayName
;
238 wxString
wxGetDisplayName()
240 return gs_displayName
;
243 #include "wx/module.h"
245 // the module responsible for closing the X11 display at the program end
246 class wxX11DisplayModule
: public wxModule
249 virtual bool OnInit() { return true; }
250 virtual void OnExit() { wxCloseDisplay(); }
253 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
256 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
258 // ----------------------------------------------------------------------------
259 // Some colour manipulation routines
260 // ----------------------------------------------------------------------------
262 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
267 int r
= 0, g
= 0, b
= 0;
270 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
271 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
273 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
276 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
277 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
278 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
281 case 0: r
= v
, g
= t
, b
= p
; break;
282 case 1: r
= q
, g
= v
, b
= p
; break;
283 case 2: r
= p
, g
= v
, b
= t
; break;
284 case 3: r
= p
, g
= q
, b
= v
; break;
285 case 4: r
= t
, g
= p
, b
= v
; break;
286 case 5: r
= v
, g
= p
, b
= q
; break;
293 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
295 int r
= rgb
->red
>> 8;
296 int g
= rgb
->green
>> 8;
297 int b
= rgb
->blue
>> 8;
298 int maxv
= wxMax3(r
, g
, b
);
299 int minv
= wxMin3(r
, g
, b
);
302 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
307 int rc
, gc
, bc
, hex
= 0;
308 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
309 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
310 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
311 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
312 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
313 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
314 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
318 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
319 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
322 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
327 int screen
= DefaultScreen(d
);
328 int num_colors
= DisplayCells(d
,screen
);
330 XColor
*color_defs
= new XColor
[num_colors
];
331 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
332 XQueryColors(d
,cmp
,color_defs
,num_colors
);
335 wxXColorToHSV(&hsv
,xc
);
337 int diff
, min_diff
= 0, pixel
= 0;
339 for(llp
= 0;llp
< num_colors
;llp
++)
341 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
342 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
343 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
344 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
345 if (llp
== 0) min_diff
= diff
;
346 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
347 if (min_diff
== 0) break;
350 xc
-> red
= color_defs
[pixel
].red
;
351 xc
-> green
= color_defs
[pixel
].green
;
352 xc
-> blue
= color_defs
[pixel
].blue
;
353 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
356 if (!XAllocColor(d,cmp,xc))
357 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
364 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
366 if (!XAllocColor(d
,cmp
,xc
))
368 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
369 wxAllocNearestColor(d
,cmp
,xc
);
373 wxString
wxGetXEventName(XEvent
& event
)
376 wxString
str(wxT("(some event)"));
379 int type
= event
.xany
.type
;
380 static const char* event_name
[] = {
381 "", "unknown(-)", // 0-1
382 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
383 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
384 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
385 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
386 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
387 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
388 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
389 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
390 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
391 "ClientMessage", "MappingNotify", // 33-34
393 type
= wxMin(35, type
); type
= wxMax(1, type
);
394 return wxString::FromAscii(event_name
[type
]);
398 bool wxWindowIsVisible(Window win
)
400 XWindowAttributes wa
;
401 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
403 return (wa
.map_state
== IsViewable
);