]>
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 WX_DECLARE_HASH_MAP( int, wxEndProcessData
*, wxIntegerHash
, wxIntegerEqual
, wxProcMap
);
96 static wxProcMap
*gs_procmap
;
98 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
100 if (!gs_procmap
) gs_procmap
= new wxProcMap();
101 (*gs_procmap
)[fd
] = proc_data
;
105 void wxCheckForFinishedChildren()
107 wxProcMap::iterator it
;
108 if (!gs_procmap
) return;
109 if (gs_procmap
->size() == 0) {
110 // Map empty, delete it.
114 for (it
= gs_procmap
->begin();it
!= gs_procmap
->end(); ++it
)
116 wxEndProcessData
*proc_data
= it
->second
;
117 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
119 // has the process really terminated?
120 int rc
= waitpid(pid
, &status
, WNOHANG
);
122 continue; // no, it didn't exit yet, continue waiting
124 // set exit code to -1 if something bad happened
125 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ?
126 WEXITSTATUS(status
) : -1;
128 // child exited, end waiting
131 // don't call us again!
132 gs_procmap
->erase(it
->first
);
134 wxHandleProcessTermination(proc_data
);
136 // Iterator is invalid. Handle any further children in subsequent
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
148 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
151 // Use current setting for the bell
152 XBell ((Display
*) wxGetDisplay(), 0);
156 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
158 // get X protocol version
159 Display
*display
= wxGlobalDisplay();
163 *verMaj
= ProtocolVersion (display
);
165 *verMin
= ProtocolRevision (display
);
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 void wxGetMousePosition( int* x
, int* y
)
185 XQueryPointer((Display
*) wxGetDisplay(),
186 DefaultRootWindow((Display
*) wxGetDisplay()),
188 &(xev
.x_root
), &(xev
.y_root
),
196 // Return true if we have a colour display
197 bool wxColourDisplay()
199 return wxDisplayDepth() > 1;
202 // Returns depth of screen
205 Display
*dpy
= (Display
*) wxGetDisplay();
207 return DefaultDepth (dpy
, DefaultScreen (dpy
));
210 // Get size of display
211 void wxDisplaySize(int *width
, int *height
)
213 Display
*dpy
= (Display
*) wxGetDisplay();
216 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
218 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
221 void wxDisplaySizeMM(int *width
, int *height
)
223 Display
*dpy
= (Display
*) wxGetDisplay();
226 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
228 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
231 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
233 // This is supposed to return desktop dimensions minus any window
234 // manager panels, menus, taskbars, etc. If there is a way to do that
235 // for this platform please fix this function, otherwise it defaults
236 // to the entire desktop.
239 wxDisplaySize(width
, height
);
242 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
244 return wxGenericFindWindowAtPoint(pt
);
248 // Configurable display in wxX11 and wxMotif
249 static Display
*gs_currentDisplay
= NULL
;
250 static wxString gs_displayName
;
252 WXDisplay
*wxGetDisplay()
254 return (WXDisplay
*)gs_currentDisplay
;
257 // close the current display
258 void wxCloseDisplay()
260 if ( gs_currentDisplay
)
262 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
264 wxLogWarning(_("Failed to close the display \"%s\""),
265 gs_displayName
.c_str());
268 gs_currentDisplay
= NULL
;
269 gs_displayName
.clear();
273 bool wxSetDisplay(const wxString
& displayName
)
276 dpy
= XOpenDisplay(displayName
.empty() ? NULL
: displayName
.mb_str());
280 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
286 gs_currentDisplay
= dpy
;
287 gs_displayName
= displayName
;
292 wxString
wxGetDisplayName()
294 return gs_displayName
;
297 #include "wx/module.h"
299 // the module responsible for closing the X11 display at the program end
300 class wxX11DisplayModule
: public wxModule
303 virtual bool OnInit() { return true; }
304 virtual void OnExit() { wxCloseDisplay(); }
307 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
310 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
312 // ----------------------------------------------------------------------------
313 // Some colour manipulation routines
314 // ----------------------------------------------------------------------------
316 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
321 int r
= 0, g
= 0, b
= 0;
324 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
325 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
327 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
330 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
331 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
332 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
335 case 0: r
= v
, g
= t
, b
= p
; break;
336 case 1: r
= q
, g
= v
, b
= p
; break;
337 case 2: r
= p
, g
= v
, b
= t
; break;
338 case 3: r
= p
, g
= q
, b
= v
; break;
339 case 4: r
= t
, g
= p
, b
= v
; break;
340 case 5: r
= v
, g
= p
, b
= q
; break;
347 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
349 int r
= rgb
->red
>> 8;
350 int g
= rgb
->green
>> 8;
351 int b
= rgb
->blue
>> 8;
352 int maxv
= wxMax3(r
, g
, b
);
353 int minv
= wxMin3(r
, g
, b
);
356 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
361 int rc
, gc
, bc
, hex
= 0;
362 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
363 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
364 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
365 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
366 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
367 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
368 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
372 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
373 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
376 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
381 int screen
= DefaultScreen(d
);
382 int num_colors
= DisplayCells(d
,screen
);
384 XColor
*color_defs
= new XColor
[num_colors
];
385 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
386 XQueryColors(d
,cmp
,color_defs
,num_colors
);
389 wxXColorToHSV(&hsv
,xc
);
391 int diff
, min_diff
= 0, pixel
= 0;
393 for(llp
= 0;llp
< num_colors
;llp
++)
395 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
396 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
397 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
398 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
399 if (llp
== 0) min_diff
= diff
;
400 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
401 if (min_diff
== 0) break;
404 xc
-> red
= color_defs
[pixel
].red
;
405 xc
-> green
= color_defs
[pixel
].green
;
406 xc
-> blue
= color_defs
[pixel
].blue
;
407 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
410 if (!XAllocColor(d,cmp,xc))
411 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
418 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
420 if (!XAllocColor(d
,cmp
,xc
))
422 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
423 wxAllocNearestColor(d
,cmp
,xc
);
428 wxString
wxGetXEventName(XEvent
& event
)
431 wxString
str(wxT("(some event)"));
434 int type
= event
.xany
.type
;
435 static char* event_name
[] = {
436 "", "unknown(-)", // 0-1
437 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
438 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
439 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
440 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
441 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
442 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
443 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
444 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
445 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
446 "ClientMessage", "MappingNotify", // 33-34
448 type
= wxMin(35, type
); type
= wxMax(1, type
);
449 return wxString::FromAscii(event_name
[type
]);
454 bool wxWindowIsVisible(Window win
)
456 XWindowAttributes wa
;
457 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
459 return (wa
.map_state
== IsViewable
);