1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/dcmemory.h"
28 #include "wx/bitmap.h"
31 #include "wx/apptrait.h"
32 #include "wx/evtloop.h"
33 #include "wx/private/eventloopsourcesmanager.h"
34 #include "wx/motif/private/timer.h"
38 #if (defined(__SUNCC__) || defined(__CLCC__))
43 #pragma message disable nosimpint
49 #include "wx/motif/private.h"
51 #include "X11/Xutil.h"
54 #pragma message enable nosimpint
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
63 // async event processing
64 // ----------------------------------------------------------------------------
66 // Consume all events until no more left
67 void wxFlushEvents(WXDisplay
* wxdisplay
)
69 Display
*display
= (Display
*)wxdisplay
;
72 XSync (display
, False
);
74 while (evtLoop
.Pending())
81 #if wxUSE_EVENTLOOP_SOURCE
88 wxMotifInputHandler(XtPointer data
,
90 XtInputId
* WXUNUSED(inputId
))
92 wxEventLoopSourceHandler
* const
93 handler
= static_cast<wxEventLoopSourceHandler
*>(data
);
95 handler
->OnReadWaiting();
100 // This class exists just to call XtRemoveInput() in its dtor, the real work of
101 // dispatching events on the file descriptor to the handler is done by
102 // wxMotifInputHandler callback above.
103 class wxMotifEventLoopSource
: public wxEventLoopSource
106 wxMotifEventLoopSource(XtInputId inputId
,
107 wxEventLoopSourceHandler
*handler
,
109 : wxEventLoopSource(handler
, flags
),
114 virtual ~wxMotifEventLoopSource()
116 XtRemoveInput(m_inputId
);
120 const XtInputId m_inputId
;
122 wxDECLARE_NO_COPY_CLASS(wxMotifEventLoopSource
);
125 class wxMotifEventLoopSourcesManager
: public wxEventLoopSourcesManagerBase
129 AddSourceForFD(int fd
, wxEventLoopSourceHandler
* handler
, int flags
)
131 wxCHECK_MSG( wxTheApp
, NULL
, "Must create wxTheApp first" );
133 // The XtInputXXXMask values cannot be combined (hence "Mask" is a
134 // complete misnomer), and supporting those would make the code more
135 // complicated and we don't need them for now.
136 wxCHECK_MSG( !(flags
& (wxEVENT_SOURCE_OUTPUT
|
137 wxEVENT_SOURCE_EXCEPTION
)),
139 "Monitoring FDs for output/errors not supported" );
141 wxCHECK_MSG( flags
& wxEVENT_SOURCE_INPUT
,
143 "Should be monitoring for input" );
145 XtInputId inputId
= XtAppAddInput
147 (XtAppContext
) wxTheApp
->GetAppContext(),
149 (XtPointer
) XtInputReadMask
,
156 return new wxMotifEventLoopSource(inputId
, handler
, flags
);
160 wxEventLoopSourcesManagerBase
* wxGUIAppTraits::GetEventLoopSourcesManager()
162 static wxMotifEventLoopSourcesManager s_eventLoopSourcesManager
;
164 return &s_eventLoopSourcesManager
;
167 #endif // wxUSE_EVENTLOOP_SOURCE
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
176 // Use current setting for the bell
177 XBell (wxGlobalDisplay(), 0);
180 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
182 // XmVERSION and XmREVISION are defined in Xm/Xm.h
186 *verMin
= XmREVISION
;
191 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
193 return new wxEventLoop
;
196 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
* timer
)
198 return new wxMotifTimerImpl(timer
);
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
205 void wxGetMousePosition( int* x
, int* y
)
214 XQueryPointer(wxGlobalDisplay(),
215 DefaultRootWindow(wxGlobalDisplay()),
217 &(xev
.x_root
), &(xev
.y_root
),
225 // Return true if we have a colour display
226 bool wxColourDisplay()
228 return wxDisplayDepth() > 1;
231 // Returns depth of screen
234 Display
*dpy
= wxGlobalDisplay();
236 return DefaultDepth (dpy
, DefaultScreen (dpy
));
239 // Get size of display
240 void wxDisplaySize(int *width
, int *height
)
242 Display
*dpy
= wxGlobalDisplay();
245 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
247 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
250 void wxDisplaySizeMM(int *width
, int *height
)
252 Display
*dpy
= wxGlobalDisplay();
255 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
257 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
260 // Configurable display in wxX11 and wxMotif
261 static WXDisplay
*gs_currentDisplay
= NULL
;
262 static wxString gs_displayName
;
264 WXDisplay
*wxGetDisplay()
266 if (gs_currentDisplay
)
267 return gs_currentDisplay
;
269 return wxTheApp
->GetInitialDisplay();
273 bool wxSetDisplay(const wxString
& display_name
)
275 gs_displayName
= display_name
;
277 if ( display_name
.empty() )
279 gs_currentDisplay
= NULL
;
287 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
288 display_name
.c_str(),
289 wxTheApp
->GetAppName().c_str(),
290 wxTheApp
->GetClassName().c_str(),
292 #if XtSpecificationRelease < 5
301 gs_currentDisplay
= (WXDisplay
*) display
;
309 wxString
wxGetDisplayName()
311 return gs_displayName
;
314 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
316 return wxGenericFindWindowAtPoint(pt
);
319 // ----------------------------------------------------------------------------
320 // Some colour manipulation routines
321 // ----------------------------------------------------------------------------
323 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
328 int r
= 0, g
= 0, b
= 0;
331 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
332 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
334 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
337 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
338 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
339 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
342 case 0: r
= v
, g
= t
, b
= p
; break;
343 case 1: r
= q
, g
= v
, b
= p
; break;
344 case 2: r
= p
, g
= v
, b
= t
; break;
345 case 3: r
= p
, g
= q
, b
= v
; break;
346 case 4: r
= t
, g
= p
, b
= v
; break;
347 case 5: r
= v
, g
= p
, b
= q
; break;
349 rgb
->red
= (unsigned short)(r
<< 8);
350 rgb
->green
= (unsigned short)(g
<< 8);
351 rgb
->blue
= (unsigned short)(b
<< 8);
354 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
356 int r
= rgb
->red
>> 8;
357 int g
= rgb
->green
>> 8;
358 int b
= rgb
->blue
>> 8;
359 int maxv
= wxMax3(r
, g
, b
);
360 int minv
= wxMin3(r
, g
, b
);
363 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
368 int rc
, gc
, bc
, hex
= 0;
369 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
370 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
371 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
372 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
373 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
374 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
375 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
379 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
380 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
383 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
388 int screen
= DefaultScreen(d
);
389 int num_colors
= DisplayCells(d
,screen
);
391 XColor
*color_defs
= new XColor
[num_colors
];
392 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
393 XQueryColors(d
,cmp
,color_defs
,num_colors
);
396 wxXColorToHSV(&hsv
,xc
);
398 int diff
, min_diff
= 0, pixel
= 0;
400 for(llp
= 0;llp
< num_colors
;llp
++)
402 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
403 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
404 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
405 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
406 if (llp
== 0) min_diff
= diff
;
407 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
408 if (min_diff
== 0) break;
411 xc
-> red
= color_defs
[pixel
].red
;
412 xc
-> green
= color_defs
[pixel
].green
;
413 xc
-> blue
= color_defs
[pixel
].blue
;
414 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
417 if (!XAllocColor(d,cmp,xc))
418 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
425 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
427 if (!XAllocColor(d
,cmp
,xc
))
429 // cout << "wxAllocColor : Warning : cannot allocate color, attempt find nearest !\n";
430 wxAllocNearestColor(d
,cmp
,xc
);
434 wxString
wxGetXEventName(XEvent
& event
)
437 wxString
str(wxT("(some event)"));
440 int type
= event
.xany
.type
;
441 static char* event_name
[] = {
442 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
443 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
444 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
445 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
446 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
447 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
448 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
449 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
450 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
451 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
452 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
453 wxMOTIF_STR("unknown(+)")}; // 35
454 type
= wxMin(35, type
); type
= wxMax(1, type
);
455 wxString
str(event_name
[type
]);
460 // ----------------------------------------------------------------------------
462 // ----------------------------------------------------------------------------
464 // Find the letter corresponding to the mnemonic, for Motif
465 char wxFindMnemonic (const char *s
)
468 int len
= strlen (s
);
471 for (i
= 0; i
< len
; i
++)
475 // Carefully handle &&
476 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
488 char* wxFindAccelerator( const char *s
)
492 // VZ: this function returns incorrect keysym which completely breaks kbd
496 // The accelerator text is after the \t char.
497 s
= strchr( s
, '\t' );
499 if( !s
) return NULL
;
502 Now we need to format it as X standard:
507 Ctrl+N --> Ctrl<Key>N
509 Ctrl+Shift+A --> Ctrl Shift<Key>A
511 and handle Ctrl-N & similia
514 static char buf
[256];
517 wxString tmp
= s
+ 1; // skip TAB
520 while( index
< tmp
.length() )
522 size_t plus
= tmp
.find( '+', index
);
523 size_t minus
= tmp
.find( '-', index
);
525 // neither '+' nor '-', add <Key>
526 if( plus
== wxString::npos
&& minus
== wxString::npos
)
528 strcat( buf
, "<Key>" );
529 strcat( buf
, tmp
.c_str() + index
);
534 // OK: npos is big and positive
535 size_t sep
= wxMin( plus
, minus
);
536 wxString mod
= tmp
.substr( index
, sep
- index
);
547 strcat( buf
, mod
.c_str() );
556 XmString
wxFindAcceleratorText (const char *s
)
560 // VZ: this function returns incorrect keysym which completely breaks kbd
564 // The accelerator text is after the \t char.
565 s
= strchr( s
, '\t' );
567 if( !s
) return NULL
;
569 return wxStringToXmString( s
+ 1 ); // skip TAB!
573 // Change a widget's foreground and background colours.
574 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
576 if (!foregroundColour
.IsOk())
579 // When should we specify the foreground, if it's calculated
580 // by wxComputeColours?
581 // Solution: say we start with the default (computed) foreground colour.
582 // If we call SetForegroundColour explicitly for a control or window,
583 // then the foreground is changed.
584 // Therefore SetBackgroundColour computes the foreground colour, and
585 // SetForegroundColour changes the foreground colour. The ordering is
588 XtVaSetValues ((Widget
) widget
,
589 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
593 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
595 if (!backgroundColour
.IsOk())
598 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
601 XtVaSetValues ((Widget
) widget
,
602 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
603 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
604 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
605 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
609 XtVaSetValues ((Widget
) widget
,
610 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
614 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
616 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
617 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
618 Widget w
= (Widget
)widget
;
620 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
629 wxString
wxXmStringToString( const XmString
& xmString
)
632 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
639 return wxEmptyString
;
642 XmString
wxStringToXmString( const char* str
)
644 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
647 // ----------------------------------------------------------------------------
648 // wxBitmap utility functions
649 // ----------------------------------------------------------------------------
651 // Creates a bitmap with transparent areas drawn in
653 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
655 wxBitmap
newBitmap(bitmap
.GetWidth(),
661 srcDC
.SelectObjectAsSource(bitmap
);
662 destDC
.SelectObject(newBitmap
);
664 wxBrush
brush(colour
, wxSOLID
);
665 destDC
.SetBackground(brush
);
667 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
668 &srcDC
, 0, 0, wxCOPY
, true);
673 // ----------------------------------------------------------------------------
674 // Miscellaneous functions
675 // ----------------------------------------------------------------------------
677 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
679 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
681 if (style
& wxSIMPLE_BORDER
)
683 borderWidget
= XtVaCreateManagedWidget
686 xmFrameWidgetClass
, parentWidget
,
687 XmNshadowType
, XmSHADOW_ETCHED_IN
,
688 XmNshadowThickness
, 1,
692 else if ((style
& wxSUNKEN_BORDER
) || (style
& wxBORDER_THEME
))
694 borderWidget
= XtVaCreateManagedWidget
697 xmFrameWidgetClass
, parentWidget
,
698 XmNshadowType
, XmSHADOW_IN
,
702 else if (style
& wxRAISED_BORDER
)
704 borderWidget
= XtVaCreateManagedWidget
707 xmFrameWidgetClass
, parentWidget
,
708 XmNshadowType
, XmSHADOW_OUT
,