]>
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"
37 #include "wx/generic/private/timer.h"
38 #include "wx/evtloop.h"
45 #include <sys/types.h>
53 #if (defined(__SUNCC__) || defined(__CLCC__))
58 #pragma message disable nosimpint
61 #include "wx/unix/execute.h"
63 #include "wx/x11/private.h"
65 #include "X11/Xutil.h"
68 #pragma message enable nosimpint
71 // ----------------------------------------------------------------------------
72 // async event processing
73 // ----------------------------------------------------------------------------
75 // Consume all events until no more left
78 Display
*display
= (Display
*) wxGetDisplay();
80 XSync (display
, FALSE
);
86 bool wxCheckForInterrupt(wxWindow
*wnd
)
88 wxFAIL_MSG(wxT("wxCheckForInterrupt not yet implemented."));
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 WX_DECLARE_HASH_MAP( int, wxEndProcessData
*, wxIntegerHash
, wxIntegerEqual
, wxProcMap
);
98 static wxProcMap
*gs_procmap
;
100 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
102 if (!gs_procmap
) gs_procmap
= new wxProcMap();
103 (*gs_procmap
)[fd
] = proc_data
;
107 void wxCheckForFinishedChildren()
109 wxProcMap::iterator it
;
110 if (!gs_procmap
) return;
111 if (gs_procmap
->size() == 0) {
112 // Map empty, delete it.
117 for (it
= gs_procmap
->begin();it
!= gs_procmap
->end(); ++it
)
119 wxEndProcessData
*proc_data
= it
->second
;
120 int pid
= (proc_data
->pid
> 0) ? proc_data
->pid
: -(proc_data
->pid
);
122 // has the process really terminated?
123 int rc
= waitpid(pid
, &status
, WNOHANG
);
125 continue; // no, it didn't exit yet, continue waiting
127 // set exit code to -1 if something bad happened
128 proc_data
->exitcode
= rc
!= -1 && WIFEXITED(status
) ?
129 WEXITSTATUS(status
) : -1;
131 // child exited, end waiting
134 // don't call us again!
135 gs_procmap
->erase(it
->first
);
137 wxHandleProcessTermination(proc_data
);
139 // Iterator is invalid. Handle any further children in subsequent
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
151 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
154 // Use current setting for the bell
155 XBell ((Display
*) wxGetDisplay(), 0);
159 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
161 // get X protocol version
162 Display
*display
= wxGlobalDisplay();
166 *verMaj
= ProtocolVersion (display
);
168 *verMin
= ProtocolRevision (display
);
174 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
176 return new wxEventLoop
;
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 void wxGetMousePosition( int* x
, int* y
)
192 XQueryPointer((Display
*) wxGetDisplay(),
193 DefaultRootWindow((Display
*) wxGetDisplay()),
195 &(xev
.x_root
), &(xev
.y_root
),
203 // Return true if we have a colour display
204 bool wxColourDisplay()
206 return wxDisplayDepth() > 1;
209 // Returns depth of screen
212 Display
*dpy
= (Display
*) wxGetDisplay();
214 return DefaultDepth (dpy
, DefaultScreen (dpy
));
217 // Get size of display
218 void wxDisplaySize(int *width
, int *height
)
220 Display
*dpy
= (Display
*) wxGetDisplay();
223 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
225 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
228 void wxDisplaySizeMM(int *width
, int *height
)
230 Display
*dpy
= (Display
*) wxGetDisplay();
233 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
235 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
238 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
240 // This is supposed to return desktop dimensions minus any window
241 // manager panels, menus, taskbars, etc. If there is a way to do that
242 // for this platform please fix this function, otherwise it defaults
243 // to the entire desktop.
246 wxDisplaySize(width
, height
);
249 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
251 return wxGenericFindWindowAtPoint(pt
);
255 // Configurable display in wxX11 and wxMotif
256 static Display
*gs_currentDisplay
= NULL
;
257 static wxString gs_displayName
;
259 WXDisplay
*wxGetDisplay()
261 return (WXDisplay
*)gs_currentDisplay
;
264 // close the current display
265 void wxCloseDisplay()
267 if ( gs_currentDisplay
)
269 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
271 wxLogWarning(_("Failed to close the display \"%s\""),
272 gs_displayName
.c_str());
275 gs_currentDisplay
= NULL
;
276 gs_displayName
.clear();
280 bool wxSetDisplay(const wxString
& displayName
)
282 Display
*dpy
= XOpenDisplay
284 displayName
.empty() ? NULL
285 : (const char *)displayName
.mb_str()
290 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
296 gs_currentDisplay
= dpy
;
297 gs_displayName
= displayName
;
302 wxString
wxGetDisplayName()
304 return gs_displayName
;
307 #include "wx/module.h"
309 // the module responsible for closing the X11 display at the program end
310 class wxX11DisplayModule
: public wxModule
313 virtual bool OnInit() { return true; }
314 virtual void OnExit() { wxCloseDisplay(); }
317 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
320 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
322 // ----------------------------------------------------------------------------
323 // Some colour manipulation routines
324 // ----------------------------------------------------------------------------
326 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
331 int r
= 0, g
= 0, b
= 0;
334 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
335 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
337 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
340 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
341 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
342 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
345 case 0: r
= v
, g
= t
, b
= p
; break;
346 case 1: r
= q
, g
= v
, b
= p
; break;
347 case 2: r
= p
, g
= v
, b
= t
; break;
348 case 3: r
= p
, g
= q
, b
= v
; break;
349 case 4: r
= t
, g
= p
, b
= v
; break;
350 case 5: r
= v
, g
= p
, b
= q
; break;
357 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
359 int r
= rgb
->red
>> 8;
360 int g
= rgb
->green
>> 8;
361 int b
= rgb
->blue
>> 8;
362 int maxv
= wxMax3(r
, g
, b
);
363 int minv
= wxMin3(r
, g
, b
);
366 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
371 int rc
, gc
, bc
, hex
= 0;
372 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
373 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
374 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
375 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
376 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
377 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
378 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
382 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
383 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
386 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
391 int screen
= DefaultScreen(d
);
392 int num_colors
= DisplayCells(d
,screen
);
394 XColor
*color_defs
= new XColor
[num_colors
];
395 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
396 XQueryColors(d
,cmp
,color_defs
,num_colors
);
399 wxXColorToHSV(&hsv
,xc
);
401 int diff
, min_diff
= 0, pixel
= 0;
403 for(llp
= 0;llp
< num_colors
;llp
++)
405 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
406 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
407 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
408 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
409 if (llp
== 0) min_diff
= diff
;
410 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
411 if (min_diff
== 0) break;
414 xc
-> red
= color_defs
[pixel
].red
;
415 xc
-> green
= color_defs
[pixel
].green
;
416 xc
-> blue
= color_defs
[pixel
].blue
;
417 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
420 if (!XAllocColor(d,cmp,xc))
421 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
428 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
430 if (!XAllocColor(d
,cmp
,xc
))
432 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
433 wxAllocNearestColor(d
,cmp
,xc
);
438 wxString
wxGetXEventName(XEvent
& event
)
441 wxString
str(wxT("(some event)"));
444 int type
= event
.xany
.type
;
445 static char* event_name
[] = {
446 "", "unknown(-)", // 0-1
447 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
448 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
449 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
450 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
451 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
452 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
453 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
454 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
455 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
456 "ClientMessage", "MappingNotify", // 33-34
458 type
= wxMin(35, type
); type
= wxMax(1, type
);
459 return wxString::FromAscii(event_name
[type
]);
464 bool wxWindowIsVisible(Window win
)
466 XWindowAttributes wa
;
467 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
469 return (wa
.map_state
== IsViewable
);