]>
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
, "wxCheckForInterrupt not yet implemented.");
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
97 // Use current setting for the bell
98 XBell ((Display
*) wxGetDisplay(), 0);
101 int wxGUIAppTraits::GetOSVersion(int *majorVsn
, int *minorVsn
)
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 void wxGetMousePosition( int* x
, int* y
)
125 XQueryPointer((Display
*) wxGetDisplay(),
126 DefaultRootWindow((Display
*) wxGetDisplay()),
128 &(xev
.x_root
), &(xev
.y_root
),
136 // Return TRUE if we have a colour display
137 bool wxColourDisplay()
139 return wxDisplayDepth() > 1;
142 // Returns depth of screen
145 Display
*dpy
= (Display
*) wxGetDisplay();
147 return DefaultDepth (dpy
, DefaultScreen (dpy
));
150 // Get size of display
151 void wxDisplaySize(int *width
, int *height
)
153 Display
*dpy
= (Display
*) wxGetDisplay();
156 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
158 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
161 void wxDisplaySizeMM(int *width
, int *height
)
163 Display
*dpy
= (Display
*) wxGetDisplay();
166 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
168 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
171 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
173 // This is supposed to return desktop dimensions minus any window
174 // manager panels, menus, taskbars, etc. If there is a way to do that
175 // for this platform please fix this function, otherwise it defaults
176 // to the entire desktop.
179 wxDisplaySize(width
, height
);
183 // Configurable display in wxX11 and wxMotif
184 static WXDisplay
*gs_currentDisplay
= NULL
;
185 static wxString gs_displayName
;
187 WXDisplay
*wxGetDisplay()
189 if (gs_currentDisplay
)
190 return gs_currentDisplay
;
191 return wxApp::GetDisplay();
194 bool wxSetDisplay(const wxString
& display_name
)
196 gs_displayName
= display_name
;
198 if ( display_name
.IsEmpty() )
200 gs_currentDisplay
= NULL
;
206 Display
* display
= XOpenDisplay((char*) display_name
.c_str());
210 gs_currentDisplay
= (WXDisplay
*) display
;
218 wxString
wxGetDisplayName()
220 return gs_displayName
;
223 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
225 return wxGenericFindWindowAtPoint(pt
);
228 // ----------------------------------------------------------------------------
229 // keycode translations
230 // ----------------------------------------------------------------------------
232 #include <X11/keysym.h>
234 // FIXME what about tables??
236 int wxCharCodeXToWX(KeySym keySym
)
243 id
= WXK_SHIFT
; break;
246 id
= WXK_CONTROL
; break;
248 id
= WXK_BACK
; break;
250 id
= WXK_DELETE
; break;
252 id
= WXK_CLEAR
; break;
258 id
= WXK_RETURN
; break;
260 id
= WXK_ESCAPE
; break;
263 id
= WXK_PAUSE
; break;
265 id
= WXK_NUMLOCK
; break;
267 id
= WXK_SCROLL
; break;
270 id
= WXK_HOME
; break;
274 id
= WXK_LEFT
; break;
276 id
= WXK_RIGHT
; break;
280 id
= WXK_DOWN
; break;
282 id
= WXK_NEXT
; break;
284 id
= WXK_PRIOR
; break;
286 id
= WXK_MENU
; break;
288 id
= WXK_SELECT
; break;
290 id
= WXK_CANCEL
; break;
292 id
= WXK_PRINT
; break;
294 id
= WXK_EXECUTE
; break;
296 id
= WXK_INSERT
; break;
298 id
= WXK_HELP
; break;
301 id
= WXK_MULTIPLY
; break;
305 id
= WXK_SUBTRACT
; break;
307 id
= WXK_DIVIDE
; break;
309 id
= WXK_DECIMAL
; break;
317 id
= WXK_RETURN
; break;
319 id
= WXK_NUMPAD0
; break;
321 id
= WXK_NUMPAD1
; break;
323 id
= WXK_NUMPAD2
; break;
325 id
= WXK_NUMPAD3
; break;
327 id
= WXK_NUMPAD4
; break;
329 id
= WXK_NUMPAD5
; break;
331 id
= WXK_NUMPAD6
; break;
333 id
= WXK_NUMPAD7
; break;
335 id
= WXK_NUMPAD8
; break;
337 id
= WXK_NUMPAD9
; break;
387 id
= (keySym
<= 255) ? (int)keySym
: -1;
393 KeySym
wxCharCodeWXToX(int id
)
399 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
400 case WXK_BACK
: keySym
= XK_BackSpace
; break;
401 case WXK_TAB
: keySym
= XK_Tab
; break;
402 case WXK_CLEAR
: keySym
= XK_Clear
; break;
403 case WXK_RETURN
: keySym
= XK_Return
; break;
404 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
405 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
406 case WXK_MENU
: keySym
= XK_Menu
; break;
407 case WXK_PAUSE
: keySym
= XK_Pause
; break;
408 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
409 case WXK_SPACE
: keySym
= ' '; break;
410 case WXK_PRIOR
: keySym
= XK_Prior
; break;
411 case WXK_NEXT
: keySym
= XK_Next
; break;
412 case WXK_END
: keySym
= XK_End
; break;
413 case WXK_HOME
: keySym
= XK_Home
; break;
414 case WXK_LEFT
: keySym
= XK_Left
; break;
415 case WXK_UP
: keySym
= XK_Up
; break;
416 case WXK_RIGHT
: keySym
= XK_Right
; break;
417 case WXK_DOWN
: keySym
= XK_Down
; break;
418 case WXK_SELECT
: keySym
= XK_Select
; break;
419 case WXK_PRINT
: keySym
= XK_Print
; break;
420 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
421 case WXK_INSERT
: keySym
= XK_Insert
; break;
422 case WXK_DELETE
: keySym
= XK_Delete
; break;
423 case WXK_HELP
: keySym
= XK_Help
; break;
424 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
425 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
426 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
427 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
428 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
429 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
430 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
431 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
432 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
433 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
434 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
435 case WXK_ADD
: keySym
= XK_KP_Add
; break;
436 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
437 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
438 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
439 case WXK_F1
: keySym
= XK_F1
; break;
440 case WXK_F2
: keySym
= XK_F2
; break;
441 case WXK_F3
: keySym
= XK_F3
; break;
442 case WXK_F4
: keySym
= XK_F4
; break;
443 case WXK_F5
: keySym
= XK_F5
; break;
444 case WXK_F6
: keySym
= XK_F6
; break;
445 case WXK_F7
: keySym
= XK_F7
; break;
446 case WXK_F8
: keySym
= XK_F8
; break;
447 case WXK_F9
: keySym
= XK_F9
; break;
448 case WXK_F10
: keySym
= XK_F10
; break;
449 case WXK_F11
: keySym
= XK_F11
; break;
450 case WXK_F12
: keySym
= XK_F12
; break;
451 case WXK_F13
: keySym
= XK_F13
; break;
452 case WXK_F14
: keySym
= XK_F14
; break;
453 case WXK_F15
: keySym
= XK_F15
; break;
454 case WXK_F16
: keySym
= XK_F16
; break;
455 case WXK_F17
: keySym
= XK_F17
; break;
456 case WXK_F18
: keySym
= XK_F18
; break;
457 case WXK_F19
: keySym
= XK_F19
; break;
458 case WXK_F20
: keySym
= XK_F20
; break;
459 case WXK_F21
: keySym
= XK_F21
; break;
460 case WXK_F22
: keySym
= XK_F22
; break;
461 case WXK_F23
: keySym
= XK_F23
; break;
462 case WXK_F24
: keySym
= XK_F24
; break;
463 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
464 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
465 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
471 // ----------------------------------------------------------------------------
472 // Some colour manipulation routines
473 // ----------------------------------------------------------------------------
475 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
480 int r
= 0, g
= 0, b
= 0;
483 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
484 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
486 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
489 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
490 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
491 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
494 case 0: r
= v
, g
= t
, b
= p
; break;
495 case 1: r
= q
, g
= v
, b
= p
; break;
496 case 2: r
= p
, g
= v
, b
= t
; break;
497 case 3: r
= p
, g
= q
, b
= v
; break;
498 case 4: r
= t
, g
= p
, b
= v
; break;
499 case 5: r
= v
, g
= p
, b
= q
; break;
506 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
508 int r
= rgb
->red
>> 8;
509 int g
= rgb
->green
>> 8;
510 int b
= rgb
->blue
>> 8;
511 int maxv
= wxMax3(r
, g
, b
);
512 int minv
= wxMin3(r
, g
, b
);
515 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
520 int rc
, gc
, bc
, hex
= 0;
521 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
522 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
523 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
524 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
525 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
526 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
527 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
531 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
532 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
535 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
540 int screen
= DefaultScreen(d
);
541 int num_colors
= DisplayCells(d
,screen
);
543 XColor
*color_defs
= new XColor
[num_colors
];
544 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
545 XQueryColors(d
,cmp
,color_defs
,num_colors
);
548 wxXColorToHSV(&hsv
,xc
);
550 int diff
, min_diff
= 0, pixel
= 0;
552 for(llp
= 0;llp
< num_colors
;llp
++)
554 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
555 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
556 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
557 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
558 if (llp
== 0) min_diff
= diff
;
559 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
560 if (min_diff
== 0) break;
563 xc
-> red
= color_defs
[pixel
].red
;
564 xc
-> green
= color_defs
[pixel
].green
;
565 xc
-> blue
= color_defs
[pixel
].blue
;
566 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
569 if (!XAllocColor(d,cmp,xc))
570 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
577 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
579 if (!XAllocColor(d
,cmp
,xc
))
581 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
582 wxAllocNearestColor(d
,cmp
,xc
);
587 wxString
wxGetXEventName(XEvent
& event
)
590 wxString
str(wxT("(some event)"));
593 int type
= event
.xany
.type
;
594 static char* event_name
[] = {
595 "", "unknown(-)", // 0-1
596 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
597 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
598 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
599 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
600 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
601 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
602 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
603 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
604 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
605 "ClientMessage", "MappingNotify", // 33-34
607 type
= wxMin(35, type
); type
= wxMax(1, type
);
608 wxString
str(event_name
[type
]);
614 bool wxWindowIsVisible(Window win
)
616 XWindowAttributes wa
;
617 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
619 return (wa
.map_state
== IsViewable
);