]>
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 // keycode translations
233 // ----------------------------------------------------------------------------
235 #include <X11/keysym.h>
237 // FIXME what about tables??
239 int wxCharCodeXToWX(KeySym keySym
)
246 id
= WXK_SHIFT
; break;
249 id
= WXK_CONTROL
; break;
251 id
= WXK_BACK
; break;
253 id
= WXK_DELETE
; break;
255 id
= WXK_CLEAR
; break;
261 id
= WXK_RETURN
; break;
263 id
= WXK_ESCAPE
; break;
266 id
= WXK_PAUSE
; break;
268 id
= WXK_NUMLOCK
; break;
270 id
= WXK_SCROLL
; break;
273 id
= WXK_HOME
; break;
277 id
= WXK_LEFT
; break;
279 id
= WXK_RIGHT
; break;
283 id
= WXK_DOWN
; break;
285 id
= WXK_NEXT
; break;
287 id
= WXK_PRIOR
; break;
289 id
= WXK_MENU
; break;
291 id
= WXK_SELECT
; break;
293 id
= WXK_CANCEL
; break;
295 id
= WXK_PRINT
; break;
297 id
= WXK_EXECUTE
; break;
299 id
= WXK_INSERT
; break;
301 id
= WXK_HELP
; break;
304 id
= WXK_MULTIPLY
; break;
308 id
= WXK_SUBTRACT
; break;
310 id
= WXK_DIVIDE
; break;
312 id
= WXK_DECIMAL
; break;
320 id
= WXK_RETURN
; break;
322 id
= WXK_NUMPAD0
; break;
324 id
= WXK_NUMPAD1
; break;
326 id
= WXK_NUMPAD2
; break;
328 id
= WXK_NUMPAD3
; break;
330 id
= WXK_NUMPAD4
; break;
332 id
= WXK_NUMPAD5
; break;
334 id
= WXK_NUMPAD6
; break;
336 id
= WXK_NUMPAD7
; break;
338 id
= WXK_NUMPAD8
; break;
340 id
= WXK_NUMPAD9
; break;
390 id
= (keySym
<= 255) ? (int)keySym
: -1;
396 KeySym
wxCharCodeWXToX(int id
)
402 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
403 case WXK_BACK
: keySym
= XK_BackSpace
; break;
404 case WXK_TAB
: keySym
= XK_Tab
; break;
405 case WXK_CLEAR
: keySym
= XK_Clear
; break;
406 case WXK_RETURN
: keySym
= XK_Return
; break;
407 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
408 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
409 case WXK_MENU
: keySym
= XK_Menu
; break;
410 case WXK_PAUSE
: keySym
= XK_Pause
; break;
411 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
412 case WXK_SPACE
: keySym
= ' '; break;
413 case WXK_PRIOR
: keySym
= XK_Prior
; break;
414 case WXK_NEXT
: keySym
= XK_Next
; break;
415 case WXK_END
: keySym
= XK_End
; break;
416 case WXK_HOME
: keySym
= XK_Home
; break;
417 case WXK_LEFT
: keySym
= XK_Left
; break;
418 case WXK_UP
: keySym
= XK_Up
; break;
419 case WXK_RIGHT
: keySym
= XK_Right
; break;
420 case WXK_DOWN
: keySym
= XK_Down
; break;
421 case WXK_SELECT
: keySym
= XK_Select
; break;
422 case WXK_PRINT
: keySym
= XK_Print
; break;
423 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
424 case WXK_INSERT
: keySym
= XK_Insert
; break;
425 case WXK_DELETE
: keySym
= XK_Delete
; break;
426 case WXK_HELP
: keySym
= XK_Help
; break;
427 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
428 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
429 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
430 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
431 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
432 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
433 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
434 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
435 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
436 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
437 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
438 case WXK_ADD
: keySym
= XK_KP_Add
; break;
439 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
440 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
441 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
442 case WXK_F1
: keySym
= XK_F1
; break;
443 case WXK_F2
: keySym
= XK_F2
; break;
444 case WXK_F3
: keySym
= XK_F3
; break;
445 case WXK_F4
: keySym
= XK_F4
; break;
446 case WXK_F5
: keySym
= XK_F5
; break;
447 case WXK_F6
: keySym
= XK_F6
; break;
448 case WXK_F7
: keySym
= XK_F7
; break;
449 case WXK_F8
: keySym
= XK_F8
; break;
450 case WXK_F9
: keySym
= XK_F9
; break;
451 case WXK_F10
: keySym
= XK_F10
; break;
452 case WXK_F11
: keySym
= XK_F11
; break;
453 case WXK_F12
: keySym
= XK_F12
; break;
454 case WXK_F13
: keySym
= XK_F13
; break;
455 case WXK_F14
: keySym
= XK_F14
; break;
456 case WXK_F15
: keySym
= XK_F15
; break;
457 case WXK_F16
: keySym
= XK_F16
; break;
458 case WXK_F17
: keySym
= XK_F17
; break;
459 case WXK_F18
: keySym
= XK_F18
; break;
460 case WXK_F19
: keySym
= XK_F19
; break;
461 case WXK_F20
: keySym
= XK_F20
; break;
462 case WXK_F21
: keySym
= XK_F21
; break;
463 case WXK_F22
: keySym
= XK_F22
; break;
464 case WXK_F23
: keySym
= XK_F23
; break;
465 case WXK_F24
: keySym
= XK_F24
; break;
466 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
467 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
468 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
474 // ----------------------------------------------------------------------------
475 // Some colour manipulation routines
476 // ----------------------------------------------------------------------------
478 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
483 int r
= 0, g
= 0, b
= 0;
486 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
487 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
489 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
492 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
493 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
494 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
497 case 0: r
= v
, g
= t
, b
= p
; break;
498 case 1: r
= q
, g
= v
, b
= p
; break;
499 case 2: r
= p
, g
= v
, b
= t
; break;
500 case 3: r
= p
, g
= q
, b
= v
; break;
501 case 4: r
= t
, g
= p
, b
= v
; break;
502 case 5: r
= v
, g
= p
, b
= q
; break;
509 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
511 int r
= rgb
->red
>> 8;
512 int g
= rgb
->green
>> 8;
513 int b
= rgb
->blue
>> 8;
514 int maxv
= wxMax3(r
, g
, b
);
515 int minv
= wxMin3(r
, g
, b
);
518 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
523 int rc
, gc
, bc
, hex
= 0;
524 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
525 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
526 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
527 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
528 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
529 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
530 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
534 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
535 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
538 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
543 int screen
= DefaultScreen(d
);
544 int num_colors
= DisplayCells(d
,screen
);
546 XColor
*color_defs
= new XColor
[num_colors
];
547 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
548 XQueryColors(d
,cmp
,color_defs
,num_colors
);
551 wxXColorToHSV(&hsv
,xc
);
553 int diff
, min_diff
= 0, pixel
= 0;
555 for(llp
= 0;llp
< num_colors
;llp
++)
557 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
558 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
559 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
560 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
561 if (llp
== 0) min_diff
= diff
;
562 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
563 if (min_diff
== 0) break;
566 xc
-> red
= color_defs
[pixel
].red
;
567 xc
-> green
= color_defs
[pixel
].green
;
568 xc
-> blue
= color_defs
[pixel
].blue
;
569 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
572 if (!XAllocColor(d,cmp,xc))
573 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
580 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
582 if (!XAllocColor(d
,cmp
,xc
))
584 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
585 wxAllocNearestColor(d
,cmp
,xc
);
590 wxString
wxGetXEventName(XEvent
& event
)
593 wxString
str(wxT("(some event)"));
596 int type
= event
.xany
.type
;
597 static char* event_name
[] = {
598 "", "unknown(-)", // 0-1
599 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
600 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
601 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
602 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
603 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
604 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
605 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
606 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
607 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
608 "ClientMessage", "MappingNotify", // 33-34
610 type
= wxMin(35, type
); type
= wxMax(1, type
);
611 return wxString::FromAscii(event_name
[type
]);
616 bool wxWindowIsVisible(Window win
)
618 XWindowAttributes wa
;
619 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
621 return (wa
.map_state
== IsViewable
);