]>
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
*WXUNUSED(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 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
240 return wxGenericFindWindowAtPoint(pt
);
244 // Configurable display in wxX11 and wxMotif
245 static Display
*gs_currentDisplay
= NULL
;
246 static wxString gs_displayName
;
248 WXDisplay
*wxGetDisplay()
250 return (WXDisplay
*)gs_currentDisplay
;
253 // close the current display
254 void wxCloseDisplay()
256 if ( gs_currentDisplay
)
258 if ( XCloseDisplay(gs_currentDisplay
) != 0 )
260 wxLogWarning(_("Failed to close the display \"%s\""),
261 gs_displayName
.c_str());
264 gs_currentDisplay
= NULL
;
265 gs_displayName
.clear();
269 bool wxSetDisplay(const wxString
& displayName
)
271 Display
*dpy
= XOpenDisplay
273 displayName
.empty() ? NULL
274 : (const char *)displayName
.mb_str()
279 wxLogError(_("Failed to open display \"%s\"."), displayName
.c_str());
285 gs_currentDisplay
= dpy
;
286 gs_displayName
= displayName
;
291 wxString
wxGetDisplayName()
293 return gs_displayName
;
296 #include "wx/module.h"
298 // the module responsible for closing the X11 display at the program end
299 class wxX11DisplayModule
: public wxModule
302 virtual bool OnInit() { return true; }
303 virtual void OnExit() { wxCloseDisplay(); }
306 DECLARE_DYNAMIC_CLASS(wxX11DisplayModule
)
309 IMPLEMENT_DYNAMIC_CLASS(wxX11DisplayModule
, wxModule
)
311 // ----------------------------------------------------------------------------
312 // Some colour manipulation routines
313 // ----------------------------------------------------------------------------
315 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
320 int r
= 0, g
= 0, b
= 0;
323 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
324 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
326 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
329 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
330 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
331 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
334 case 0: r
= v
, g
= t
, b
= p
; break;
335 case 1: r
= q
, g
= v
, b
= p
; break;
336 case 2: r
= p
, g
= v
, b
= t
; break;
337 case 3: r
= p
, g
= q
, b
= v
; break;
338 case 4: r
= t
, g
= p
, b
= v
; break;
339 case 5: r
= v
, g
= p
, b
= q
; break;
346 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
348 int r
= rgb
->red
>> 8;
349 int g
= rgb
->green
>> 8;
350 int b
= rgb
->blue
>> 8;
351 int maxv
= wxMax3(r
, g
, b
);
352 int minv
= wxMin3(r
, g
, b
);
355 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
360 int rc
, gc
, bc
, hex
= 0;
361 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
362 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
363 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
364 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
365 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
366 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
367 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
371 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
372 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
375 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
380 int screen
= DefaultScreen(d
);
381 int num_colors
= DisplayCells(d
,screen
);
383 XColor
*color_defs
= new XColor
[num_colors
];
384 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
385 XQueryColors(d
,cmp
,color_defs
,num_colors
);
388 wxXColorToHSV(&hsv
,xc
);
390 int diff
, min_diff
= 0, pixel
= 0;
392 for(llp
= 0;llp
< num_colors
;llp
++)
394 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
395 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
396 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
397 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
398 if (llp
== 0) min_diff
= diff
;
399 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
400 if (min_diff
== 0) break;
403 xc
-> red
= color_defs
[pixel
].red
;
404 xc
-> green
= color_defs
[pixel
].green
;
405 xc
-> blue
= color_defs
[pixel
].blue
;
406 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
409 if (!XAllocColor(d,cmp,xc))
410 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
417 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
419 if (!XAllocColor(d
,cmp
,xc
))
421 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
422 wxAllocNearestColor(d
,cmp
,xc
);
427 wxString
wxGetXEventName(XEvent
& event
)
430 wxString
str(wxT("(some event)"));
433 int type
= event
.xany
.type
;
434 static char* event_name
[] = {
435 "", "unknown(-)", // 0-1
436 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
437 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
438 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
439 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
440 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
441 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
442 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
443 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
444 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
445 "ClientMessage", "MappingNotify", // 33-34
447 type
= wxMin(35, type
); type
= wxMax(1, type
);
448 return wxString::FromAscii(event_name
[type
]);
453 bool wxWindowIsVisible(Window win
)
455 XWindowAttributes wa
;
456 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
458 return (wa
.map_state
== IsViewable
);