]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
23 #include "wx/apptrait.h"
24 #include "wx/msgdlg.h"
25 #include "wx/cursor.h"
26 #include "wx/window.h" // for wxTopLevelWindows
33 #include <sys/types.h>
41 #if (defined(__SUNCC__) || defined(__CLCC__))
46 #pragma message disable nosimpint
49 #include "wx/unix/execute.h"
51 #include "wx/x11/private.h"
53 #include "X11/Xutil.h"
56 #pragma message enable nosimpint
59 // ----------------------------------------------------------------------------
60 // async event processing
61 // ----------------------------------------------------------------------------
63 // Consume all events until no more left
66 Display
*display
= (Display
*) wxGetDisplay();
68 XSync (display
, FALSE
);
74 bool wxCheckForInterrupt(wxWindow
*wnd
)
76 wxASSERT_MSG(FALSE
, wxT("wxCheckForInterrupt not yet implemented."));
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
96 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
99 // Use current setting for the bell
100 XBell ((Display
*) wxGetDisplay(), 0);
104 wxToolkitInfo
& wxGUIAppTraits::GetToolkitInfo()
106 static wxToolkitInfo info
;
107 info
.shortName
= _T("x11univ");
108 info
.name
= _T("wxX11");
109 info
.versionMajor
= 0;
110 info
.versionMinor
= 0;
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 void wxGetMousePosition( int* x
, int* y
)
128 XQueryPointer((Display
*) wxGetDisplay(),
129 DefaultRootWindow((Display
*) wxGetDisplay()),
131 &(xev
.x_root
), &(xev
.y_root
),
139 // Return TRUE if we have a colour display
140 bool wxColourDisplay()
142 return wxDisplayDepth() > 1;
145 // Returns depth of screen
148 Display
*dpy
= (Display
*) wxGetDisplay();
150 return DefaultDepth (dpy
, DefaultScreen (dpy
));
153 // Get size of display
154 void wxDisplaySize(int *width
, int *height
)
156 Display
*dpy
= (Display
*) wxGetDisplay();
159 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
161 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
164 void wxDisplaySizeMM(int *width
, int *height
)
166 Display
*dpy
= (Display
*) wxGetDisplay();
169 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
171 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
174 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
176 // This is supposed to return desktop dimensions minus any window
177 // manager panels, menus, taskbars, etc. If there is a way to do that
178 // for this platform please fix this function, otherwise it defaults
179 // to the entire desktop.
182 wxDisplaySize(width
, height
);
186 // Configurable display in wxX11 and wxMotif
187 static WXDisplay
*gs_currentDisplay
= NULL
;
188 static wxString gs_displayName
;
190 WXDisplay
*wxGetDisplay()
192 if (gs_currentDisplay
)
193 return gs_currentDisplay
;
194 return wxApp::GetDisplay();
197 bool wxSetDisplay(const wxString
& display_name
)
199 gs_displayName
= display_name
;
201 if ( display_name
.IsEmpty() )
203 gs_currentDisplay
= NULL
;
209 Display
* display
= XOpenDisplay((char*) display_name
.c_str());
213 gs_currentDisplay
= (WXDisplay
*) display
;
221 wxString
wxGetDisplayName()
223 return gs_displayName
;
226 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
228 return wxGenericFindWindowAtPoint(pt
);
231 // ----------------------------------------------------------------------------
232 // Some colour manipulation routines
233 // ----------------------------------------------------------------------------
235 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
240 int r
= 0, g
= 0, b
= 0;
243 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
244 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
246 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
249 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
250 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
251 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
254 case 0: r
= v
, g
= t
, b
= p
; break;
255 case 1: r
= q
, g
= v
, b
= p
; break;
256 case 2: r
= p
, g
= v
, b
= t
; break;
257 case 3: r
= p
, g
= q
, b
= v
; break;
258 case 4: r
= t
, g
= p
, b
= v
; break;
259 case 5: r
= v
, g
= p
, b
= q
; break;
266 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
268 int r
= rgb
->red
>> 8;
269 int g
= rgb
->green
>> 8;
270 int b
= rgb
->blue
>> 8;
271 int maxv
= wxMax3(r
, g
, b
);
272 int minv
= wxMin3(r
, g
, b
);
275 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
280 int rc
, gc
, bc
, hex
= 0;
281 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
282 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
283 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
284 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
285 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
286 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
287 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
291 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
292 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
295 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
300 int screen
= DefaultScreen(d
);
301 int num_colors
= DisplayCells(d
,screen
);
303 XColor
*color_defs
= new XColor
[num_colors
];
304 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
305 XQueryColors(d
,cmp
,color_defs
,num_colors
);
308 wxXColorToHSV(&hsv
,xc
);
310 int diff
, min_diff
= 0, pixel
= 0;
312 for(llp
= 0;llp
< num_colors
;llp
++)
314 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
315 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
316 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
317 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
318 if (llp
== 0) min_diff
= diff
;
319 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
320 if (min_diff
== 0) break;
323 xc
-> red
= color_defs
[pixel
].red
;
324 xc
-> green
= color_defs
[pixel
].green
;
325 xc
-> blue
= color_defs
[pixel
].blue
;
326 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
329 if (!XAllocColor(d,cmp,xc))
330 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
337 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
339 if (!XAllocColor(d
,cmp
,xc
))
341 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
342 wxAllocNearestColor(d
,cmp
,xc
);
347 wxString
wxGetXEventName(XEvent
& event
)
350 wxString
str(wxT("(some event)"));
353 int type
= event
.xany
.type
;
354 static char* event_name
[] = {
355 "", "unknown(-)", // 0-1
356 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
357 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
358 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
359 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
360 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
361 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
362 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
363 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
364 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
365 "ClientMessage", "MappingNotify", // 33-34
367 type
= wxMin(35, type
); type
= wxMax(1, type
);
368 return wxString::FromAscii(event_name
[type
]);
373 bool wxWindowIsVisible(Window win
)
375 XWindowAttributes wa
;
376 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
378 return (wa
.map_state
== IsViewable
);