]>
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"
43 #include <sys/types.h>
51 #if (defined(__SUNCC__) || defined(__CLCC__))
56 #pragma message disable nosimpint
59 #include "wx/unix/execute.h"
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
*wnd
)
86 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
94 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
100 // ----------------------------------------------------------------------------
102 // ----------------------------------------------------------------------------
106 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
109 // Use current setting for the bell
110 XBell ((Display
*) wxGetDisplay(), 0);
114 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
116 static wxToolkitInfo info
;
117 info
.shortName
= _T("x11univ");
118 info
.name
= _T("wxX11");
119 info
.versionMajor
= 0;
120 info
.versionMinor
= 0;
125 // ----------------------------------------------------------------------------
127 // ----------------------------------------------------------------------------
129 void wxGetMousePosition( int* x
, int* y
)
138 XQueryPointer((Display
*) wxGetDisplay(),
139 DefaultRootWindow((Display
*) wxGetDisplay()),
141 &(xev
.x_root
), &(xev
.y_root
),
149 // Return true if we have a colour display
150 bool wxColourDisplay()
152 return wxDisplayDepth() > 1;
155 // Returns depth of screen
158 Display
*dpy
= (Display
*) wxGetDisplay();
160 return DefaultDepth (dpy
, DefaultScreen (dpy
));
163 // Get size of display
164 void wxDisplaySize(int *width
, int *height
)
166 Display
*dpy
= (Display
*) wxGetDisplay();
169 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
171 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
174 void wxDisplaySizeMM(int *width
, int *height
)
176 Display
*dpy
= (Display
*) wxGetDisplay();
179 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
181 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
184 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
186 // This is supposed to return desktop dimensions minus any window
187 // manager panels, menus, taskbars, etc. If there is a way to do that
188 // for this platform please fix this function, otherwise it defaults
189 // to the entire desktop.
192 wxDisplaySize(width
, height
);
196 // Configurable display in wxX11 and wxMotif
197 static WXDisplay
*gs_currentDisplay
= NULL
;
198 static wxString gs_displayName
;
200 WXDisplay
*wxGetDisplay()
202 if (gs_currentDisplay
)
203 return gs_currentDisplay
;
204 return wxApp::GetDisplay();
207 bool wxSetDisplay(const wxString
& display_name
)
209 gs_displayName
= display_name
;
211 if ( display_name
.empty() )
213 gs_currentDisplay
= NULL
;
219 Display
* display
= XOpenDisplay((char*) display_name
.c_str());
223 gs_currentDisplay
= (WXDisplay
*) display
;
231 wxString
wxGetDisplayName()
233 return gs_displayName
;
236 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
238 return wxGenericFindWindowAtPoint(pt
);
241 // ----------------------------------------------------------------------------
242 // Some colour manipulation routines
243 // ----------------------------------------------------------------------------
245 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
250 int r
= 0, g
= 0, b
= 0;
253 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
254 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
256 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
259 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
260 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
261 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
264 case 0: r
= v
, g
= t
, b
= p
; break;
265 case 1: r
= q
, g
= v
, b
= p
; break;
266 case 2: r
= p
, g
= v
, b
= t
; break;
267 case 3: r
= p
, g
= q
, b
= v
; break;
268 case 4: r
= t
, g
= p
, b
= v
; break;
269 case 5: r
= v
, g
= p
, b
= q
; break;
276 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
278 int r
= rgb
->red
>> 8;
279 int g
= rgb
->green
>> 8;
280 int b
= rgb
->blue
>> 8;
281 int maxv
= wxMax3(r
, g
, b
);
282 int minv
= wxMin3(r
, g
, b
);
285 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
290 int rc
, gc
, bc
, hex
= 0;
291 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
292 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
293 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
294 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
295 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
296 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
297 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
301 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
302 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
305 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
310 int screen
= DefaultScreen(d
);
311 int num_colors
= DisplayCells(d
,screen
);
313 XColor
*color_defs
= new XColor
[num_colors
];
314 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
315 XQueryColors(d
,cmp
,color_defs
,num_colors
);
318 wxXColorToHSV(&hsv
,xc
);
320 int diff
, min_diff
= 0, pixel
= 0;
322 for(llp
= 0;llp
< num_colors
;llp
++)
324 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
325 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
326 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
327 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
328 if (llp
== 0) min_diff
= diff
;
329 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
330 if (min_diff
== 0) break;
333 xc
-> red
= color_defs
[pixel
].red
;
334 xc
-> green
= color_defs
[pixel
].green
;
335 xc
-> blue
= color_defs
[pixel
].blue
;
336 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
339 if (!XAllocColor(d,cmp,xc))
340 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
347 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
349 if (!XAllocColor(d
,cmp
,xc
))
351 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
352 wxAllocNearestColor(d
,cmp
,xc
);
357 wxString
wxGetXEventName(XEvent
& event
)
360 wxString
str(wxT("(some event)"));
363 int type
= event
.xany
.type
;
364 static char* event_name
[] = {
365 "", "unknown(-)", // 0-1
366 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
367 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
368 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
369 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
370 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
371 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
372 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
373 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
374 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
375 "ClientMessage", "MappingNotify", // 33-34
377 type
= wxMin(35, type
); type
= wxMax(1, type
);
378 return wxString::FromAscii(event_name
[type
]);
383 bool wxWindowIsVisible(Window win
)
385 XWindowAttributes wa
;
386 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
388 return (wa
.map_state
== IsViewable
);