]>
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 // ----------------------------------------------------------------------------
29 #include "wx/apptrait.h"
30 #include "wx/msgdlg.h"
31 #include "wx/cursor.h"
32 #include "wx/window.h" // for wxTopLevelWindows
39 #include <sys/types.h>
47 #if (defined(__SUNCC__) || defined(__CLCC__))
52 #pragma message disable nosimpint
55 #include "wx/unix/execute.h"
57 #include "wx/x11/private.h"
59 #include "X11/Xutil.h"
62 #pragma message enable nosimpint
65 // ----------------------------------------------------------------------------
66 // async event processing
67 // ----------------------------------------------------------------------------
69 // Consume all events until no more left
72 Display
*display
= (Display
*) wxGetDisplay();
74 XSync (display
, FALSE
);
80 bool wxCheckForInterrupt(wxWindow
*wnd
)
82 wxASSERT_MSG(FALSE
, wxT("wxCheckForInterrupt not yet implemented."));
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
102 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
105 // Use current setting for the bell
106 XBell ((Display
*) wxGetDisplay(), 0);
110 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
112 static wxToolkitInfo info
;
113 info
.shortName
= _T("x11univ");
114 info
.name
= _T("wxX11");
115 info
.versionMajor
= 0;
116 info
.versionMinor
= 0;
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 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
182 // This is supposed to return desktop dimensions minus any window
183 // manager panels, menus, taskbars, etc. If there is a way to do that
184 // for this platform please fix this function, otherwise it defaults
185 // to the entire desktop.
188 wxDisplaySize(width
, height
);
192 // Configurable display in wxX11 and wxMotif
193 static WXDisplay
*gs_currentDisplay
= NULL
;
194 static wxString gs_displayName
;
196 WXDisplay
*wxGetDisplay()
198 if (gs_currentDisplay
)
199 return gs_currentDisplay
;
200 return wxApp::GetDisplay();
203 bool wxSetDisplay(const wxString
& display_name
)
205 gs_displayName
= display_name
;
207 if ( display_name
.empty() )
209 gs_currentDisplay
= NULL
;
215 Display
* display
= XOpenDisplay((char*) display_name
.c_str());
219 gs_currentDisplay
= (WXDisplay
*) display
;
227 wxString
wxGetDisplayName()
229 return gs_displayName
;
232 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
234 return wxGenericFindWindowAtPoint(pt
);
237 // ----------------------------------------------------------------------------
238 // Some colour manipulation routines
239 // ----------------------------------------------------------------------------
241 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
246 int r
= 0, g
= 0, b
= 0;
249 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
250 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
252 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
255 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
256 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
257 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
260 case 0: r
= v
, g
= t
, b
= p
; break;
261 case 1: r
= q
, g
= v
, b
= p
; break;
262 case 2: r
= p
, g
= v
, b
= t
; break;
263 case 3: r
= p
, g
= q
, b
= v
; break;
264 case 4: r
= t
, g
= p
, b
= v
; break;
265 case 5: r
= v
, g
= p
, b
= q
; break;
272 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
274 int r
= rgb
->red
>> 8;
275 int g
= rgb
->green
>> 8;
276 int b
= rgb
->blue
>> 8;
277 int maxv
= wxMax3(r
, g
, b
);
278 int minv
= wxMin3(r
, g
, b
);
281 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
286 int rc
, gc
, bc
, hex
= 0;
287 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
288 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
289 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
290 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
291 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
292 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
293 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
297 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
298 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
301 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
306 int screen
= DefaultScreen(d
);
307 int num_colors
= DisplayCells(d
,screen
);
309 XColor
*color_defs
= new XColor
[num_colors
];
310 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
311 XQueryColors(d
,cmp
,color_defs
,num_colors
);
314 wxXColorToHSV(&hsv
,xc
);
316 int diff
, min_diff
= 0, pixel
= 0;
318 for(llp
= 0;llp
< num_colors
;llp
++)
320 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
321 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
322 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
323 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
324 if (llp
== 0) min_diff
= diff
;
325 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
326 if (min_diff
== 0) break;
329 xc
-> red
= color_defs
[pixel
].red
;
330 xc
-> green
= color_defs
[pixel
].green
;
331 xc
-> blue
= color_defs
[pixel
].blue
;
332 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
335 if (!XAllocColor(d,cmp,xc))
336 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
343 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
345 if (!XAllocColor(d
,cmp
,xc
))
347 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
348 wxAllocNearestColor(d
,cmp
,xc
);
353 wxString
wxGetXEventName(XEvent
& event
)
356 wxString
str(wxT("(some event)"));
359 int type
= event
.xany
.type
;
360 static char* event_name
[] = {
361 "", "unknown(-)", // 0-1
362 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
363 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
364 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
365 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
366 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
367 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
368 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
369 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
370 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
371 "ClientMessage", "MappingNotify", // 33-34
373 type
= wxMin(35, type
); type
= wxMax(1, type
);
374 return wxString::FromAscii(event_name
[type
]);
379 bool wxWindowIsVisible(Window win
)
381 XWindowAttributes wa
;
382 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
384 return (wa
.map_state
== IsViewable
);