]>
git.saurik.com Git - wxWidgets.git/blob - src/x11/utils.cpp
665d4a2ec9d4453fdc2de0928a3199ad2f5d249b
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"
44 #include <sys/types.h>
52 #if (defined(__SUNCC__) || defined(__CLCC__))
57 #pragma message disable nosimpint
60 #include "wx/unix/execute.h"
62 #include "wx/x11/private.h"
64 #include "X11/Xutil.h"
67 #pragma message enable nosimpint
70 // ----------------------------------------------------------------------------
71 // async event processing
72 // ----------------------------------------------------------------------------
74 // Consume all events until no more left
77 Display
*display
= (Display
*) wxGetDisplay();
79 XSync (display
, FALSE
);
85 bool wxCheckForInterrupt(wxWindow
*wnd
)
87 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 WX_DECLARE_HASH_MAP( int, wxEndProcessData
*, wxIntegerHash
, wxIntegerEqual
, wxProcMap
);
97 static wxProcMap
*gs_procmap
;
99 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
101 if (!gs_procmap
) gs_procmap
= new wxProcMap();
102 (*gs_procmap
)[fd
] = proc_data
;
106 void wxCheckForFinishedChildren()
108 wxProcMap::iterator it
;
109 if (!gs_procmap
) return;
110 if (gs_procmap
->size() == 0) {
111 // Map empty, delete it.
116 for (it
= gs_procmap
->begin();it
!= gs_procmap
->end(); ++it
)
118 wxEndProcessData
*proc_data
= it
->second
;
119 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
121 // has the process really terminated?
122 int rc
= waitpid(pid
, &status
, WNOHANG
);
124 continue; // no, it didn't exit yet, continue waiting
126 // set exit code to -1 if something bad happened
127 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ?
128 WEXITSTATUS(status
) : -1;
130 // child exited, end waiting
133 // don't call us again!
134 gs_procmap
->erase(it
->first
);
136 wxHandleProcessTermination(proc_data
);
138 // Iterator is invalid. Handle any further children in subsequent
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
150 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
153 // Use current setting for the bell
154 XBell ((Display
*) wxGetDisplay(), 0);
158 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
160 // get X protocol version
161 Display
*display
= wxGlobalDisplay();
165 *verMaj
= ProtocolVersion (display
);
167 *verMin
= ProtocolRevision (display
);
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 void wxGetMousePosition( int* x
, int* y
)
186 XQueryPointer((Display
*) wxGetDisplay(),
187 DefaultRootWindow((Display
*) wxGetDisplay()),
189 &(xev
.x_root
), &(xev
.y_root
),
197 // Return true if we have a colour display
198 bool wxColourDisplay()
200 return wxDisplayDepth() > 1;
203 // Returns depth of screen
206 Display
*dpy
= (Display
*) wxGetDisplay();
208 return DefaultDepth (dpy
, DefaultScreen (dpy
));
211 // Get size of display
212 void wxDisplaySize(int *width
, int *height
)
214 Display
*dpy
= (Display
*) wxGetDisplay();
217 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
219 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
222 void wxDisplaySizeMM(int *width
, int *height
)
224 Display
*dpy
= (Display
*) wxGetDisplay();
227 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
229 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
232 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
234 // This is supposed to return desktop dimensions minus any window
235 // manager panels, menus, taskbars, etc. If there is a way to do that
236 // for this platform please fix this function, otherwise it defaults
237 // to the entire desktop.
240 wxDisplaySize(width
, height
);
243 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
245 return wxGenericFindWindowAtPoint(pt
);
249 // Configurable display in wxX11 and wxMotif
250 static Display
*gs_currentDisplay
= NULL
;
251 static wxString gs_displayName
;
253 WXDisplay
*wxGetDisplay()
255 return (WXDisplay
*)gs_currentDisplay
;
258 // close the current display
259 void wxCloseDisplay()
261 if ( gs_currentDisplay
)
263 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
265 wxLogWarning(_("Failed to close the display \"%s\""),
266 gs_displayName
.c_str());
269 gs_currentDisplay
= NULL
;
270 gs_displayName
.clear();
274 bool wxSetDisplay(const wxString
& displayName
)
276 Display
*dpy
= XOpenDisplay
278 displayName
.empty() ? NULL
279 : (const char *)displayName
.mb_str()
284 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
290 gs_currentDisplay
= dpy
;
291 gs_displayName
= displayName
;
296 wxString
wxGetDisplayName()
298 return gs_displayName
;
301 #include "wx/module.h"
303 // the module responsible for closing the X11 display at the program end
304 class wxX11DisplayModule
: public wxModule
307 virtual bool OnInit() { return true; }
308 virtual void OnExit() { wxCloseDisplay(); }
311 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
314 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
316 // ----------------------------------------------------------------------------
317 // Some colour manipulation routines
318 // ----------------------------------------------------------------------------
320 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
325 int r
= 0, g
= 0, b
= 0;
328 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
329 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
331 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
334 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
335 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
336 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
339 case 0: r
= v
, g
= t
, b
= p
; break;
340 case 1: r
= q
, g
= v
, b
= p
; break;
341 case 2: r
= p
, g
= v
, b
= t
; break;
342 case 3: r
= p
, g
= q
, b
= v
; break;
343 case 4: r
= t
, g
= p
, b
= v
; break;
344 case 5: r
= v
, g
= p
, b
= q
; break;
351 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
353 int r
= rgb
->red
>> 8;
354 int g
= rgb
->green
>> 8;
355 int b
= rgb
->blue
>> 8;
356 int maxv
= wxMax3(r
, g
, b
);
357 int minv
= wxMin3(r
, g
, b
);
360 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
365 int rc
, gc
, bc
, hex
= 0;
366 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
367 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
368 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
369 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
370 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
371 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
372 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
376 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
377 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
380 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
385 int screen
= DefaultScreen(d
);
386 int num_colors
= DisplayCells(d
,screen
);
388 XColor
*color_defs
= new XColor
[num_colors
];
389 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
390 XQueryColors(d
,cmp
,color_defs
,num_colors
);
393 wxXColorToHSV(&hsv
,xc
);
395 int diff
, min_diff
= 0, pixel
= 0;
397 for(llp
= 0;llp
< num_colors
;llp
++)
399 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
400 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
401 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
402 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
403 if (llp
== 0) min_diff
= diff
;
404 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
405 if (min_diff
== 0) break;
408 xc
-> red
= color_defs
[pixel
].red
;
409 xc
-> green
= color_defs
[pixel
].green
;
410 xc
-> blue
= color_defs
[pixel
].blue
;
411 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
414 if (!XAllocColor(d,cmp,xc))
415 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
422 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
424 if (!XAllocColor(d
,cmp
,xc
))
426 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
427 wxAllocNearestColor(d
,cmp
,xc
);
432 wxString
wxGetXEventName(XEvent
& event
)
435 wxString
str(wxT("(some event)"));
438 int type
= event
.xany
.type
;
439 static char* event_name
[] = {
440 "", "unknown(-)", // 0-1
441 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
442 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
443 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
444 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
445 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
446 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
447 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
448 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
449 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
450 "ClientMessage", "MappingNotify", // 33-34
452 type
= wxMin(35, type
); type
= wxMax(1, type
);
453 return wxString::FromAscii(event_name
[type
]);
458 bool wxWindowIsVisible(Window win
)
460 XWindowAttributes wa
;
461 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
463 return (wa
.map_state
== IsViewable
);