]>
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
);
70 // XtAppPending returns availability of events AND timers/inputs, which
71 // are processed via callbacks, so XtAppNextEvent will not return if
72 // there are no events. So added '& XtIMXEvent' - Sergey.
73 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
75 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
76 // Jan Lessner: works better when events are non-X events
77 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
86 bool wxCheckForInterrupt(wxWindow
*wnd
)
89 wxCHECK_MSG( wnd
, FALSE
, "NULL window in wxCheckForInterrupt" );
91 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
92 Window win
=(Window
) wnd
->GetXWindow();
95 if (wnd
->GetMainWidget())
97 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
100 bool hadEvents
= FALSE
;
101 while( XCheckMaskEvent(dpy
,
102 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
103 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
106 if ( event
.xany
.window
== win
)
110 XtDispatchEvent(&event
);
116 wxASSERT_MSG(FALSE
, "wxCheckForInterrupt not yet implemented.");
121 // ----------------------------------------------------------------------------
123 // ----------------------------------------------------------------------------
125 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
128 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
130 (XtPointer
*) XtInputReadMask
,
131 (XtInputCallbackProc
) xt_notify_end_process
,
132 (XtPointer
) proc_data
);
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
149 // Use current setting for the bell
150 XBell ((Display
*) wxGetDisplay(), 0);
153 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 void wxGetMousePosition( int* x
, int* y
)
179 XQueryPointer((Display
*) wxGetDisplay(),
180 DefaultRootWindow((Display
*) wxGetDisplay()),
182 &(xev
.x_root
), &(xev
.y_root
),
190 // Return TRUE if we have a colour display
191 bool wxColourDisplay()
193 return wxDisplayDepth() > 1;
196 // Returns depth of screen
199 Display
*dpy
= (Display
*) wxGetDisplay();
201 return DefaultDepth (dpy
, DefaultScreen (dpy
));
204 // Get size of display
205 void wxDisplaySize(int *width
, int *height
)
207 Display
*dpy
= (Display
*) wxGetDisplay();
210 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
212 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
215 void wxDisplaySizeMM(int *width
, int *height
)
217 Display
*dpy
= (Display
*) wxGetDisplay();
220 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
222 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
225 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
227 // This is supposed to return desktop dimensions minus any window
228 // manager panels, menus, taskbars, etc. If there is a way to do that
229 // for this platform please fix this function, otherwise it defaults
230 // to the entire desktop.
233 wxDisplaySize(width
, height
);
237 // Configurable display in wxX11 and wxMotif
238 static WXDisplay
*gs_currentDisplay
= NULL
;
239 static wxString gs_displayName
;
241 WXDisplay
*wxGetDisplay()
243 if (gs_currentDisplay
)
244 return gs_currentDisplay
;
246 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
247 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
249 return wxTheApp
->GetInitialDisplay();
253 return wxApp::GetDisplay();
257 bool wxSetDisplay(const wxString
& display_name
)
259 gs_displayName
= display_name
;
261 if ( display_name
.IsEmpty() )
263 gs_currentDisplay
= NULL
;
269 Display
* display
= XOpenDisplay((char*) display_name
.c_str());
273 gs_currentDisplay
= (WXDisplay
*) display
;
281 wxString
wxGetDisplayName()
283 return gs_displayName
;
286 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
288 return wxGenericFindWindowAtPoint(pt
);
291 // ----------------------------------------------------------------------------
292 // keycode translations
293 // ----------------------------------------------------------------------------
295 #include <X11/keysym.h>
297 // FIXME what about tables??
299 int wxCharCodeXToWX(KeySym keySym
)
306 id
= WXK_SHIFT
; break;
309 id
= WXK_CONTROL
; break;
311 id
= WXK_BACK
; break;
313 id
= WXK_DELETE
; break;
315 id
= WXK_CLEAR
; break;
321 id
= WXK_RETURN
; break;
323 id
= WXK_ESCAPE
; break;
326 id
= WXK_PAUSE
; break;
328 id
= WXK_NUMLOCK
; break;
330 id
= WXK_SCROLL
; break;
333 id
= WXK_HOME
; break;
337 id
= WXK_LEFT
; break;
339 id
= WXK_RIGHT
; break;
343 id
= WXK_DOWN
; break;
345 id
= WXK_NEXT
; break;
347 id
= WXK_PRIOR
; break;
349 id
= WXK_MENU
; break;
351 id
= WXK_SELECT
; break;
353 id
= WXK_CANCEL
; break;
355 id
= WXK_PRINT
; break;
357 id
= WXK_EXECUTE
; break;
359 id
= WXK_INSERT
; break;
361 id
= WXK_HELP
; break;
364 id
= WXK_MULTIPLY
; break;
368 id
= WXK_SUBTRACT
; break;
370 id
= WXK_DIVIDE
; break;
372 id
= WXK_DECIMAL
; break;
380 id
= WXK_RETURN
; break;
382 id
= WXK_NUMPAD0
; break;
384 id
= WXK_NUMPAD1
; break;
386 id
= WXK_NUMPAD2
; break;
388 id
= WXK_NUMPAD3
; break;
390 id
= WXK_NUMPAD4
; break;
392 id
= WXK_NUMPAD5
; break;
394 id
= WXK_NUMPAD6
; break;
396 id
= WXK_NUMPAD7
; break;
398 id
= WXK_NUMPAD8
; break;
400 id
= WXK_NUMPAD9
; break;
450 id
= (keySym
<= 255) ? (int)keySym
: -1;
456 KeySym
wxCharCodeWXToX(int id
)
462 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
463 case WXK_BACK
: keySym
= XK_BackSpace
; break;
464 case WXK_TAB
: keySym
= XK_Tab
; break;
465 case WXK_CLEAR
: keySym
= XK_Clear
; break;
466 case WXK_RETURN
: keySym
= XK_Return
; break;
467 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
468 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
469 case WXK_MENU
: keySym
= XK_Menu
; break;
470 case WXK_PAUSE
: keySym
= XK_Pause
; break;
471 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
472 case WXK_SPACE
: keySym
= ' '; break;
473 case WXK_PRIOR
: keySym
= XK_Prior
; break;
474 case WXK_NEXT
: keySym
= XK_Next
; break;
475 case WXK_END
: keySym
= XK_End
; break;
476 case WXK_HOME
: keySym
= XK_Home
; break;
477 case WXK_LEFT
: keySym
= XK_Left
; break;
478 case WXK_UP
: keySym
= XK_Up
; break;
479 case WXK_RIGHT
: keySym
= XK_Right
; break;
480 case WXK_DOWN
: keySym
= XK_Down
; break;
481 case WXK_SELECT
: keySym
= XK_Select
; break;
482 case WXK_PRINT
: keySym
= XK_Print
; break;
483 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
484 case WXK_INSERT
: keySym
= XK_Insert
; break;
485 case WXK_DELETE
: keySym
= XK_Delete
; break;
486 case WXK_HELP
: keySym
= XK_Help
; break;
487 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
488 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
489 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
490 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
491 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
492 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
493 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
494 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
495 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
496 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
497 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
498 case WXK_ADD
: keySym
= XK_KP_Add
; break;
499 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
500 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
501 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
502 case WXK_F1
: keySym
= XK_F1
; break;
503 case WXK_F2
: keySym
= XK_F2
; break;
504 case WXK_F3
: keySym
= XK_F3
; break;
505 case WXK_F4
: keySym
= XK_F4
; break;
506 case WXK_F5
: keySym
= XK_F5
; break;
507 case WXK_F6
: keySym
= XK_F6
; break;
508 case WXK_F7
: keySym
= XK_F7
; break;
509 case WXK_F8
: keySym
= XK_F8
; break;
510 case WXK_F9
: keySym
= XK_F9
; break;
511 case WXK_F10
: keySym
= XK_F10
; break;
512 case WXK_F11
: keySym
= XK_F11
; break;
513 case WXK_F12
: keySym
= XK_F12
; break;
514 case WXK_F13
: keySym
= XK_F13
; break;
515 case WXK_F14
: keySym
= XK_F14
; break;
516 case WXK_F15
: keySym
= XK_F15
; break;
517 case WXK_F16
: keySym
= XK_F16
; break;
518 case WXK_F17
: keySym
= XK_F17
; break;
519 case WXK_F18
: keySym
= XK_F18
; break;
520 case WXK_F19
: keySym
= XK_F19
; break;
521 case WXK_F20
: keySym
= XK_F20
; break;
522 case WXK_F21
: keySym
= XK_F21
; break;
523 case WXK_F22
: keySym
= XK_F22
; break;
524 case WXK_F23
: keySym
= XK_F23
; break;
525 case WXK_F24
: keySym
= XK_F24
; break;
526 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
527 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
528 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
534 // ----------------------------------------------------------------------------
535 // Some colour manipulation routines
536 // ----------------------------------------------------------------------------
538 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
543 int r
= 0, g
= 0, b
= 0;
546 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
547 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
549 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
552 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
553 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
554 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
557 case 0: r
= v
, g
= t
, b
= p
; break;
558 case 1: r
= q
, g
= v
, b
= p
; break;
559 case 2: r
= p
, g
= v
, b
= t
; break;
560 case 3: r
= p
, g
= q
, b
= v
; break;
561 case 4: r
= t
, g
= p
, b
= v
; break;
562 case 5: r
= v
, g
= p
, b
= q
; break;
569 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
571 int r
= rgb
->red
>> 8;
572 int g
= rgb
->green
>> 8;
573 int b
= rgb
->blue
>> 8;
574 int maxv
= wxMax3(r
, g
, b
);
575 int minv
= wxMin3(r
, g
, b
);
578 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
583 int rc
, gc
, bc
, hex
= 0;
584 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
585 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
586 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
587 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
588 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
589 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
590 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
594 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
595 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
598 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
603 int screen
= DefaultScreen(d
);
604 int num_colors
= DisplayCells(d
,screen
);
606 XColor
*color_defs
= new XColor
[num_colors
];
607 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
608 XQueryColors(d
,cmp
,color_defs
,num_colors
);
611 wxXColorToHSV(&hsv
,xc
);
613 int diff
, min_diff
= 0, pixel
= 0;
615 for(llp
= 0;llp
< num_colors
;llp
++)
617 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
618 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
619 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
620 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
621 if (llp
== 0) min_diff
= diff
;
622 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
623 if (min_diff
== 0) break;
626 xc
-> red
= color_defs
[pixel
].red
;
627 xc
-> green
= color_defs
[pixel
].green
;
628 xc
-> blue
= color_defs
[pixel
].blue
;
629 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
632 if (!XAllocColor(d,cmp,xc))
633 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
640 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
642 if (!XAllocColor(d
,cmp
,xc
))
644 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
645 wxAllocNearestColor(d
,cmp
,xc
);
650 wxString
wxGetXEventName(XEvent
& event
)
653 wxString
str(wxT("(some event)"));
656 int type
= event
.xany
.type
;
657 static char* event_name
[] = {
658 "", "unknown(-)", // 0-1
659 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
660 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
661 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
662 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
663 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
664 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
665 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
666 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
667 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
668 "ClientMessage", "MappingNotify", // 33-34
670 type
= wxMin(35, type
); type
= wxMax(1, type
);
671 wxString
str(event_name
[type
]);
677 bool wxWindowIsVisible(Window win
)
679 XWindowAttributes wa
;
680 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
682 return (wa
.map_state
== IsViewable
);