1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
26 #include "wx/dcmemory.h"
27 #include "wx/bitmap.h"
30 #include "wx/apptrait.h"
31 #include "wx/evtloop.h"
32 #include "wx/private/eventloopsourcesmanager.h"
33 #include "wx/motif/private/timer.h"
37 #if (defined(__SUNCC__) || defined(__CLCC__))
42 #pragma message disable nosimpint
48 #include "wx/motif/private.h"
50 #include "X11/Xutil.h"
53 #pragma message enable nosimpint
57 // ============================================================================
59 // ============================================================================
61 // ----------------------------------------------------------------------------
62 // async event processing
63 // ----------------------------------------------------------------------------
65 // Consume all events until no more left
66 void wxFlushEvents(WXDisplay
* wxdisplay
)
68 Display
*display
= (Display
*)wxdisplay
;
71 XSync (display
, False
);
73 while (evtLoop
.Pending())
80 #if wxUSE_EVENTLOOP_SOURCE
87 wxMotifInputHandler(XtPointer data
,
89 XtInputId
* WXUNUSED(inputId
))
91 wxEventLoopSourceHandler
* const
92 handler
= static_cast<wxEventLoopSourceHandler
*>(data
);
94 handler
->OnReadWaiting();
99 // This class exists just to call XtRemoveInput() in its dtor, the real work of
100 // dispatching events on the file descriptor to the handler is done by
101 // wxMotifInputHandler callback above.
102 class wxMotifEventLoopSource
: public wxEventLoopSource
105 wxMotifEventLoopSource(XtInputId inputId
,
106 wxEventLoopSourceHandler
*handler
,
108 : wxEventLoopSource(handler
, flags
),
113 virtual ~wxMotifEventLoopSource()
115 XtRemoveInput(m_inputId
);
119 const XtInputId m_inputId
;
121 wxDECLARE_NO_COPY_CLASS(wxMotifEventLoopSource
);
124 class wxMotifEventLoopSourcesManager
: public wxEventLoopSourcesManagerBase
128 AddSourceForFD(int fd
, wxEventLoopSourceHandler
* handler
, int flags
)
130 wxCHECK_MSG( wxTheApp
, NULL
, "Must create wxTheApp first" );
132 // The XtInputXXXMask values cannot be combined (hence "Mask" is a
133 // complete misnomer), and supporting those would make the code more
134 // complicated and we don't need them for now.
135 wxCHECK_MSG( !(flags
& (wxEVENT_SOURCE_OUTPUT
|
136 wxEVENT_SOURCE_EXCEPTION
)),
138 "Monitoring FDs for output/errors not supported" );
140 wxCHECK_MSG( flags
& wxEVENT_SOURCE_INPUT
,
142 "Should be monitoring for input" );
144 XtInputId inputId
= XtAppAddInput
146 (XtAppContext
) wxTheApp
->GetAppContext(),
148 (XtPointer
) XtInputReadMask
,
155 return new wxMotifEventLoopSource(inputId
, handler
, flags
);
159 wxEventLoopSourcesManagerBase
* wxGUIAppTraits::GetEventLoopSourcesManager()
161 static wxMotifEventLoopSourcesManager s_eventLoopSourcesManager
;
163 return &s_eventLoopSourcesManager
;
166 #endif // wxUSE_EVENTLOOP_SOURCE
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
175 // Use current setting for the bell
176 XBell (wxGlobalDisplay(), 0);
179 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
181 // XmVERSION and XmREVISION are defined in Xm/Xm.h
185 *verMin
= XmREVISION
;
190 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
192 return new wxEventLoop
;
195 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
* timer
)
197 return new wxMotifTimerImpl(timer
);
200 // ----------------------------------------------------------------------------
202 // ----------------------------------------------------------------------------
204 void wxGetMousePosition( int* x
, int* y
)
213 XQueryPointer(wxGlobalDisplay(),
214 DefaultRootWindow(wxGlobalDisplay()),
216 &(xev
.x_root
), &(xev
.y_root
),
224 // Return true if we have a colour display
225 bool wxColourDisplay()
227 return wxDisplayDepth() > 1;
230 // Returns depth of screen
233 Display
*dpy
= wxGlobalDisplay();
235 return DefaultDepth (dpy
, DefaultScreen (dpy
));
238 // Get size of display
239 void wxDisplaySize(int *width
, int *height
)
241 Display
*dpy
= wxGlobalDisplay();
244 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
246 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
249 void wxDisplaySizeMM(int *width
, int *height
)
251 Display
*dpy
= wxGlobalDisplay();
254 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
256 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
259 // Configurable display in wxX11 and wxMotif
260 static WXDisplay
*gs_currentDisplay
= NULL
;
261 static wxString gs_displayName
;
263 WXDisplay
*wxGetDisplay()
265 if (gs_currentDisplay
)
266 return gs_currentDisplay
;
268 return wxTheApp
->GetInitialDisplay();
272 bool wxSetDisplay(const wxString
& display_name
)
274 gs_displayName
= display_name
;
276 if ( display_name
.empty() )
278 gs_currentDisplay
= NULL
;
286 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
287 display_name
.c_str(),
288 wxTheApp
->GetAppName().c_str(),
289 wxTheApp
->GetClassName().c_str(),
291 #if XtSpecificationRelease < 5
300 gs_currentDisplay
= (WXDisplay
*) display
;
308 wxString
wxGetDisplayName()
310 return gs_displayName
;
313 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
315 return wxGenericFindWindowAtPoint(pt
);
318 // ----------------------------------------------------------------------------
319 // Some colour manipulation routines
320 // ----------------------------------------------------------------------------
322 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
327 int r
= 0, g
= 0, b
= 0;
330 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
331 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
333 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
336 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
337 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
338 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
341 case 0: r
= v
, g
= t
, b
= p
; break;
342 case 1: r
= q
, g
= v
, b
= p
; break;
343 case 2: r
= p
, g
= v
, b
= t
; break;
344 case 3: r
= p
, g
= q
, b
= v
; break;
345 case 4: r
= t
, g
= p
, b
= v
; break;
346 case 5: r
= v
, g
= p
, b
= q
; break;
348 rgb
->red
= (unsigned short)(r
<< 8);
349 rgb
->green
= (unsigned short)(g
<< 8);
350 rgb
->blue
= (unsigned short)(b
<< 8);
353 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
355 int r
= rgb
->red
>> 8;
356 int g
= rgb
->green
>> 8;
357 int b
= rgb
->blue
>> 8;
358 int maxv
= wxMax3(r
, g
, b
);
359 int minv
= wxMin3(r
, g
, b
);
362 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
367 int rc
, gc
, bc
, hex
= 0;
368 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
369 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
370 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
371 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
372 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
373 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
374 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
378 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
379 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
382 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
387 int screen
= DefaultScreen(d
);
388 int num_colors
= DisplayCells(d
,screen
);
390 XColor
*color_defs
= new XColor
[num_colors
];
391 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
392 XQueryColors(d
,cmp
,color_defs
,num_colors
);
395 wxXColorToHSV(&hsv
,xc
);
397 int diff
, min_diff
= 0, pixel
= 0;
399 for(llp
= 0;llp
< num_colors
;llp
++)
401 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
402 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
403 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
404 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
405 if (llp
== 0) min_diff
= diff
;
406 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
407 if (min_diff
== 0) break;
410 xc
-> red
= color_defs
[pixel
].red
;
411 xc
-> green
= color_defs
[pixel
].green
;
412 xc
-> blue
= color_defs
[pixel
].blue
;
413 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
416 if (!XAllocColor(d,cmp,xc))
417 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
424 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
426 if (!XAllocColor(d
,cmp
,xc
))
428 // cout << "wxAllocColor : Warning : cannot allocate color, attempt find nearest !\n";
429 wxAllocNearestColor(d
,cmp
,xc
);
433 wxString
wxGetXEventName(XEvent
& event
)
436 wxString
str(wxT("(some event)"));
439 int type
= event
.xany
.type
;
440 static char* event_name
[] = {
441 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
442 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
443 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
444 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
445 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
446 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
447 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
448 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
449 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
450 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
451 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
452 wxMOTIF_STR("unknown(+)")}; // 35
453 type
= wxMin(35, type
); type
= wxMax(1, type
);
454 wxString
str(event_name
[type
]);
459 // ----------------------------------------------------------------------------
461 // ----------------------------------------------------------------------------
463 // Find the letter corresponding to the mnemonic, for Motif
464 char wxFindMnemonic (const char *s
)
467 int len
= strlen (s
);
470 for (i
= 0; i
< len
; i
++)
474 // Carefully handle &&
475 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
487 char* wxFindAccelerator( const char *s
)
491 // VZ: this function returns incorrect keysym which completely breaks kbd
495 // The accelerator text is after the \t char.
496 s
= strchr( s
, '\t' );
498 if( !s
) return NULL
;
501 Now we need to format it as X standard:
506 Ctrl+N --> Ctrl<Key>N
508 Ctrl+Shift+A --> Ctrl Shift<Key>A
510 and handle Ctrl-N & similia
513 static char buf
[256];
516 wxString tmp
= s
+ 1; // skip TAB
519 while( index
< tmp
.length() )
521 size_t plus
= tmp
.find( '+', index
);
522 size_t minus
= tmp
.find( '-', index
);
524 // neither '+' nor '-', add <Key>
525 if( plus
== wxString::npos
&& minus
== wxString::npos
)
527 strcat( buf
, "<Key>" );
528 strcat( buf
, tmp
.c_str() + index
);
533 // OK: npos is big and positive
534 size_t sep
= wxMin( plus
, minus
);
535 wxString mod
= tmp
.substr( index
, sep
- index
);
546 strcat( buf
, mod
.c_str() );
555 XmString
wxFindAcceleratorText (const char *s
)
559 // VZ: this function returns incorrect keysym which completely breaks kbd
563 // The accelerator text is after the \t char.
564 s
= strchr( s
, '\t' );
566 if( !s
) return NULL
;
568 return wxStringToXmString( s
+ 1 ); // skip TAB!
572 // Change a widget's foreground and background colours.
573 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
575 if (!foregroundColour
.IsOk())
578 // When should we specify the foreground, if it's calculated
579 // by wxComputeColours?
580 // Solution: say we start with the default (computed) foreground colour.
581 // If we call SetForegroundColour explicitly for a control or window,
582 // then the foreground is changed.
583 // Therefore SetBackgroundColour computes the foreground colour, and
584 // SetForegroundColour changes the foreground colour. The ordering is
587 XtVaSetValues ((Widget
) widget
,
588 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
592 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
594 if (!backgroundColour
.IsOk())
597 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
600 XtVaSetValues ((Widget
) widget
,
601 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
602 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
603 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
604 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
608 XtVaSetValues ((Widget
) widget
,
609 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
613 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
615 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
616 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
617 Widget w
= (Widget
)widget
;
619 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
628 wxString
wxXmStringToString( const XmString
& xmString
)
631 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
638 return wxEmptyString
;
641 XmString
wxStringToXmString( const char* str
)
643 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
646 // ----------------------------------------------------------------------------
647 // wxBitmap utility functions
648 // ----------------------------------------------------------------------------
650 // Creates a bitmap with transparent areas drawn in
652 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
654 wxBitmap
newBitmap(bitmap
.GetWidth(),
660 srcDC
.SelectObjectAsSource(bitmap
);
661 destDC
.SelectObject(newBitmap
);
663 wxBrush
brush(colour
, wxSOLID
);
664 destDC
.SetBackground(brush
);
666 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
667 &srcDC
, 0, 0, wxCOPY
, true);
672 // ----------------------------------------------------------------------------
673 // Miscellaneous functions
674 // ----------------------------------------------------------------------------
676 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
678 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
680 if (style
& wxSIMPLE_BORDER
)
682 borderWidget
= XtVaCreateManagedWidget
685 xmFrameWidgetClass
, parentWidget
,
686 XmNshadowType
, XmSHADOW_ETCHED_IN
,
687 XmNshadowThickness
, 1,
691 else if ((style
& wxSUNKEN_BORDER
) || (style
& wxBORDER_THEME
))
693 borderWidget
= XtVaCreateManagedWidget
696 xmFrameWidgetClass
, parentWidget
,
697 XmNshadowType
, XmSHADOW_IN
,
701 else if (style
& wxRAISED_BORDER
)
703 borderWidget
= XtVaCreateManagedWidget
706 xmFrameWidgetClass
, parentWidget
,
707 XmNshadowType
, XmSHADOW_OUT
,