]>
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/msgdlg.h"
24 #include "wx/cursor.h"
25 #include "wx/window.h" // for wxTopLevelWindows
32 #include <sys/types.h>
40 #if (defined(__SUNCC__) || defined(__CLCC__))
45 #pragma message disable nosimpint
48 #include "wx/unix/execute.h"
50 #include "wx/x11/private.h"
52 #include "X11/Xutil.h"
55 #pragma message enable nosimpint
58 // ----------------------------------------------------------------------------
59 // async event processing
60 // ----------------------------------------------------------------------------
62 // Consume all events until no more left
65 Display
*display
= (Display
*) wxGetDisplay();
67 XSync (display
, FALSE
);
73 bool wxCheckForInterrupt(wxWindow
*wnd
)
75 wxASSERT_MSG(FALSE
, "wxCheckForInterrupt not yet implemented.");
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
96 // Use current setting for the bell
97 XBell ((Display
*) wxGetDisplay(), 0);
100 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 void wxGetMousePosition( int* x
, int* y
)
124 XQueryPointer((Display
*) wxGetDisplay(),
125 DefaultRootWindow((Display
*) wxGetDisplay()),
127 &(xev
.x_root
), &(xev
.y_root
),
135 // Return TRUE if we have a colour display
136 bool wxColourDisplay()
138 return wxDisplayDepth() > 1;
141 // Returns depth of screen
144 Display
*dpy
= (Display
*) wxGetDisplay();
146 return DefaultDepth (dpy
, DefaultScreen (dpy
));
149 // Get size of display
150 void wxDisplaySize(int *width
, int *height
)
152 Display
*dpy
= (Display
*) wxGetDisplay();
155 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
157 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
160 void wxDisplaySizeMM(int *width
, int *height
)
162 Display
*dpy
= (Display
*) wxGetDisplay();
165 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
167 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
170 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
172 // This is supposed to return desktop dimensions minus any window
173 // manager panels, menus, taskbars, etc. If there is a way to do that
174 // for this platform please fix this function, otherwise it defaults
175 // to the entire desktop.
178 wxDisplaySize(width
, height
);
182 // Configurable display in wxX11 and wxMotif
183 static WXDisplay
*gs_currentDisplay
= NULL
;
184 static wxString gs_displayName
;
186 WXDisplay
*wxGetDisplay()
188 if (gs_currentDisplay
)
189 return gs_currentDisplay
;
190 return wxApp::GetDisplay();
193 bool wxSetDisplay(const wxString
& display_name
)
195 gs_displayName
= display_name
;
197 if ( display_name
.IsEmpty() )
199 gs_currentDisplay
= NULL
;
205 Display
* display
= XOpenDisplay((char*) display_name
.c_str());
209 gs_currentDisplay
= (WXDisplay
*) display
;
217 wxString
wxGetDisplayName()
219 return gs_displayName
;
222 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
224 return wxGenericFindWindowAtPoint(pt
);
227 // ----------------------------------------------------------------------------
228 // keycode translations
229 // ----------------------------------------------------------------------------
231 #include <X11/keysym.h>
233 // FIXME what about tables??
235 int wxCharCodeXToWX(KeySym keySym
)
242 id
= WXK_SHIFT
; break;
245 id
= WXK_CONTROL
; break;
247 id
= WXK_BACK
; break;
249 id
= WXK_DELETE
; break;
251 id
= WXK_CLEAR
; break;
257 id
= WXK_RETURN
; break;
259 id
= WXK_ESCAPE
; break;
262 id
= WXK_PAUSE
; break;
264 id
= WXK_NUMLOCK
; break;
266 id
= WXK_SCROLL
; break;
269 id
= WXK_HOME
; break;
273 id
= WXK_LEFT
; break;
275 id
= WXK_RIGHT
; break;
279 id
= WXK_DOWN
; break;
281 id
= WXK_NEXT
; break;
283 id
= WXK_PRIOR
; break;
285 id
= WXK_MENU
; break;
287 id
= WXK_SELECT
; break;
289 id
= WXK_CANCEL
; break;
291 id
= WXK_PRINT
; break;
293 id
= WXK_EXECUTE
; break;
295 id
= WXK_INSERT
; break;
297 id
= WXK_HELP
; break;
300 id
= WXK_MULTIPLY
; break;
304 id
= WXK_SUBTRACT
; break;
306 id
= WXK_DIVIDE
; break;
308 id
= WXK_DECIMAL
; break;
316 id
= WXK_RETURN
; break;
318 id
= WXK_NUMPAD0
; break;
320 id
= WXK_NUMPAD1
; break;
322 id
= WXK_NUMPAD2
; break;
324 id
= WXK_NUMPAD3
; break;
326 id
= WXK_NUMPAD4
; break;
328 id
= WXK_NUMPAD5
; break;
330 id
= WXK_NUMPAD6
; break;
332 id
= WXK_NUMPAD7
; break;
334 id
= WXK_NUMPAD8
; break;
336 id
= WXK_NUMPAD9
; break;
386 id
= (keySym
<= 255) ? (int)keySym
: -1;
392 KeySym
wxCharCodeWXToX(int id
)
398 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
399 case WXK_BACK
: keySym
= XK_BackSpace
; break;
400 case WXK_TAB
: keySym
= XK_Tab
; break;
401 case WXK_CLEAR
: keySym
= XK_Clear
; break;
402 case WXK_RETURN
: keySym
= XK_Return
; break;
403 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
404 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
405 case WXK_MENU
: keySym
= XK_Menu
; break;
406 case WXK_PAUSE
: keySym
= XK_Pause
; break;
407 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
408 case WXK_SPACE
: keySym
= ' '; break;
409 case WXK_PRIOR
: keySym
= XK_Prior
; break;
410 case WXK_NEXT
: keySym
= XK_Next
; break;
411 case WXK_END
: keySym
= XK_End
; break;
412 case WXK_HOME
: keySym
= XK_Home
; break;
413 case WXK_LEFT
: keySym
= XK_Left
; break;
414 case WXK_UP
: keySym
= XK_Up
; break;
415 case WXK_RIGHT
: keySym
= XK_Right
; break;
416 case WXK_DOWN
: keySym
= XK_Down
; break;
417 case WXK_SELECT
: keySym
= XK_Select
; break;
418 case WXK_PRINT
: keySym
= XK_Print
; break;
419 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
420 case WXK_INSERT
: keySym
= XK_Insert
; break;
421 case WXK_DELETE
: keySym
= XK_Delete
; break;
422 case WXK_HELP
: keySym
= XK_Help
; break;
423 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
424 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
425 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
426 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
427 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
428 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
429 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
430 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
431 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
432 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
433 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
434 case WXK_ADD
: keySym
= XK_KP_Add
; break;
435 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
436 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
437 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
438 case WXK_F1
: keySym
= XK_F1
; break;
439 case WXK_F2
: keySym
= XK_F2
; break;
440 case WXK_F3
: keySym
= XK_F3
; break;
441 case WXK_F4
: keySym
= XK_F4
; break;
442 case WXK_F5
: keySym
= XK_F5
; break;
443 case WXK_F6
: keySym
= XK_F6
; break;
444 case WXK_F7
: keySym
= XK_F7
; break;
445 case WXK_F8
: keySym
= XK_F8
; break;
446 case WXK_F9
: keySym
= XK_F9
; break;
447 case WXK_F10
: keySym
= XK_F10
; break;
448 case WXK_F11
: keySym
= XK_F11
; break;
449 case WXK_F12
: keySym
= XK_F12
; break;
450 case WXK_F13
: keySym
= XK_F13
; break;
451 case WXK_F14
: keySym
= XK_F14
; break;
452 case WXK_F15
: keySym
= XK_F15
; break;
453 case WXK_F16
: keySym
= XK_F16
; break;
454 case WXK_F17
: keySym
= XK_F17
; break;
455 case WXK_F18
: keySym
= XK_F18
; break;
456 case WXK_F19
: keySym
= XK_F19
; break;
457 case WXK_F20
: keySym
= XK_F20
; break;
458 case WXK_F21
: keySym
= XK_F21
; break;
459 case WXK_F22
: keySym
= XK_F22
; break;
460 case WXK_F23
: keySym
= XK_F23
; break;
461 case WXK_F24
: keySym
= XK_F24
; break;
462 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
463 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
464 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
470 // ----------------------------------------------------------------------------
471 // Some colour manipulation routines
472 // ----------------------------------------------------------------------------
474 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
479 int r
= 0, g
= 0, b
= 0;
482 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
483 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
485 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
488 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
489 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
490 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
493 case 0: r
= v
, g
= t
, b
= p
; break;
494 case 1: r
= q
, g
= v
, b
= p
; break;
495 case 2: r
= p
, g
= v
, b
= t
; break;
496 case 3: r
= p
, g
= q
, b
= v
; break;
497 case 4: r
= t
, g
= p
, b
= v
; break;
498 case 5: r
= v
, g
= p
, b
= q
; break;
505 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
507 int r
= rgb
->red
>> 8;
508 int g
= rgb
->green
>> 8;
509 int b
= rgb
->blue
>> 8;
510 int maxv
= wxMax3(r
, g
, b
);
511 int minv
= wxMin3(r
, g
, b
);
514 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
519 int rc
, gc
, bc
, hex
= 0;
520 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
521 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
522 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
523 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
524 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
525 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
526 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
530 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
531 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
534 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
539 int screen
= DefaultScreen(d
);
540 int num_colors
= DisplayCells(d
,screen
);
542 XColor
*color_defs
= new XColor
[num_colors
];
543 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
544 XQueryColors(d
,cmp
,color_defs
,num_colors
);
547 wxXColorToHSV(&hsv
,xc
);
549 int diff
, min_diff
= 0, pixel
= 0;
551 for(llp
= 0;llp
< num_colors
;llp
++)
553 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
554 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
555 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
556 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
557 if (llp
== 0) min_diff
= diff
;
558 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
559 if (min_diff
== 0) break;
562 xc
-> red
= color_defs
[pixel
].red
;
563 xc
-> green
= color_defs
[pixel
].green
;
564 xc
-> blue
= color_defs
[pixel
].blue
;
565 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
568 if (!XAllocColor(d,cmp,xc))
569 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
576 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
578 if (!XAllocColor(d
,cmp
,xc
))
580 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
581 wxAllocNearestColor(d
,cmp
,xc
);
586 wxString
wxGetXEventName(XEvent
& event
)
589 wxString
str(wxT("(some event)"));
592 int type
= event
.xany
.type
;
593 static char* event_name
[] = {
594 "", "unknown(-)", // 0-1
595 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
596 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
597 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
598 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
599 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
600 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
601 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
602 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
603 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
604 "ClientMessage", "MappingNotify", // 33-34
606 type
= wxMin(35, type
); type
= wxMax(1, type
);
607 wxString
str(event_name
[type
]);
613 bool wxWindowIsVisible(Window win
)
615 XWindowAttributes wa
;
616 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
618 return (wa
.map_state
== IsViewable
);