]>
git.saurik.com Git - wxWidgets.git/blob - src/motif/utils.cpp
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/motif/private/timer.h"
37 #if (defined(__SUNCC__) || defined(__CLCC__))
42 #pragma message disable nosimpint
45 #include "wx/unix/execute.h"
50 #include "wx/motif/private.h"
52 #include "X11/Xutil.h"
55 #pragma message enable nosimpint
59 // ============================================================================
61 // ============================================================================
63 // ----------------------------------------------------------------------------
64 // async event processing
65 // ----------------------------------------------------------------------------
67 // Consume all events until no more left
68 void wxFlushEvents(WXDisplay
* wxdisplay
)
70 Display
*display
= (Display
*)wxdisplay
;
73 XSync (display
, False
);
75 while (evtLoop
.Pending())
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
89 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
91 wxHandleProcessTermination(proc_data
);
93 // VZ: I think they should be the same...
94 wxASSERT( (int)*id
== proc_data
->tag
);
99 int wxGUIAppTraits::AddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
101 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
103 (XtPointer
*) XtInputReadMask
,
104 (XtInputCallbackProc
) xt_notify_end_process
,
105 (XtPointer
) proc_data
);
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
116 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
119 // Use current setting for the bell
120 XBell (wxGlobalDisplay(), 0);
124 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
126 // XmVERSION and XmREVISION are defined in Xm/Xm.h
130 *verMin
= XmREVISION
;
135 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
137 return new wxEventLoop
;
140 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
* timer
)
142 return new wxMotifTimerImpl(timer
);
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 void wxGetMousePosition( int* x
, int* y
)
158 XQueryPointer(wxGlobalDisplay(),
159 DefaultRootWindow(wxGlobalDisplay()),
161 &(xev
.x_root
), &(xev
.y_root
),
169 // Return true if we have a colour display
170 bool wxColourDisplay()
172 return wxDisplayDepth() > 1;
175 // Returns depth of screen
178 Display
*dpy
= wxGlobalDisplay();
180 return DefaultDepth (dpy
, DefaultScreen (dpy
));
183 // Get size of display
184 void wxDisplaySize(int *width
, int *height
)
186 Display
*dpy
= wxGlobalDisplay();
189 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
191 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
194 void wxDisplaySizeMM(int *width
, int *height
)
196 Display
*dpy
= wxGlobalDisplay();
199 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
201 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
204 // Configurable display in wxX11 and wxMotif
205 static WXDisplay
*gs_currentDisplay
= NULL
;
206 static wxString gs_displayName
;
208 WXDisplay
*wxGetDisplay()
210 if (gs_currentDisplay
)
211 return gs_currentDisplay
;
213 return wxTheApp
->GetInitialDisplay();
217 bool wxSetDisplay(const wxString
& display_name
)
219 gs_displayName
= display_name
;
221 if ( display_name
.empty() )
223 gs_currentDisplay
= NULL
;
231 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
232 display_name
.c_str(),
233 wxTheApp
->GetAppName().c_str(),
234 wxTheApp
->GetClassName().c_str(),
236 #if XtSpecificationRelease < 5
245 gs_currentDisplay
= (WXDisplay
*) display
;
253 wxString
wxGetDisplayName()
255 return gs_displayName
;
258 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
260 return wxGenericFindWindowAtPoint(pt
);
263 // ----------------------------------------------------------------------------
264 // Some colour manipulation routines
265 // ----------------------------------------------------------------------------
267 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
272 int r
= 0, g
= 0, b
= 0;
275 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
276 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
278 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
281 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
282 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
283 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
286 case 0: r
= v
, g
= t
, b
= p
; break;
287 case 1: r
= q
, g
= v
, b
= p
; break;
288 case 2: r
= p
, g
= v
, b
= t
; break;
289 case 3: r
= p
, g
= q
, b
= v
; break;
290 case 4: r
= t
, g
= p
, b
= v
; break;
291 case 5: r
= v
, g
= p
, b
= q
; break;
293 rgb
->red
= (unsigned short)(r
<< 8);
294 rgb
->green
= (unsigned short)(g
<< 8);
295 rgb
->blue
= (unsigned short)(b
<< 8);
298 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
300 int r
= rgb
->red
>> 8;
301 int g
= rgb
->green
>> 8;
302 int b
= rgb
->blue
>> 8;
303 int maxv
= wxMax3(r
, g
, b
);
304 int minv
= wxMin3(r
, g
, b
);
307 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
312 int rc
, gc
, bc
, hex
= 0;
313 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
314 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
315 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
316 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
317 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
318 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
319 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
323 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
324 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
327 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
332 int screen
= DefaultScreen(d
);
333 int num_colors
= DisplayCells(d
,screen
);
335 XColor
*color_defs
= new XColor
[num_colors
];
336 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
337 XQueryColors(d
,cmp
,color_defs
,num_colors
);
340 wxXColorToHSV(&hsv
,xc
);
342 int diff
, min_diff
= 0, pixel
= 0;
344 for(llp
= 0;llp
< num_colors
;llp
++)
346 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
347 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
348 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
349 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
350 if (llp
== 0) min_diff
= diff
;
351 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
352 if (min_diff
== 0) break;
355 xc
-> red
= color_defs
[pixel
].red
;
356 xc
-> green
= color_defs
[pixel
].green
;
357 xc
-> blue
= color_defs
[pixel
].blue
;
358 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
361 if (!XAllocColor(d,cmp,xc))
362 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
369 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
371 if (!XAllocColor(d
,cmp
,xc
))
373 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
374 wxAllocNearestColor(d
,cmp
,xc
);
378 wxString
wxGetXEventName(XEvent
& event
)
381 wxString
str(wxT("(some event)"));
384 int type
= event
.xany
.type
;
385 static char* event_name
[] = {
386 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
387 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
388 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
389 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
390 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
391 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
392 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
393 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
394 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
395 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
396 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
397 wxMOTIF_STR("unknown(+)")}; // 35
398 type
= wxMin(35, type
); type
= wxMax(1, type
);
399 wxString
str(event_name
[type
]);
404 // ----------------------------------------------------------------------------
406 // ----------------------------------------------------------------------------
408 // Find the letter corresponding to the mnemonic, for Motif
409 char wxFindMnemonic (const char *s
)
412 int len
= strlen (s
);
415 for (i
= 0; i
< len
; i
++)
419 // Carefully handle &&
420 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
432 char* wxFindAccelerator( const char *s
)
436 // VZ: this function returns incorrect keysym which completely breaks kbd
440 // The accelerator text is after the \t char.
441 s
= strchr( s
, '\t' );
443 if( !s
) return NULL
;
446 Now we need to format it as X standard:
451 Ctrl+N --> Ctrl<Key>N
453 Ctrl+Shift+A --> Ctrl Shift<Key>A
455 and handle Ctrl-N & similia
458 static char buf
[256];
461 wxString tmp
= s
+ 1; // skip TAB
464 while( index
< tmp
.length() )
466 size_t plus
= tmp
.find( '+', index
);
467 size_t minus
= tmp
.find( '-', index
);
469 // neither '+' nor '-', add <Key>
470 if( plus
== wxString::npos
&& minus
== wxString::npos
)
472 strcat( buf
, "<Key>" );
473 strcat( buf
, tmp
.c_str() + index
);
478 // OK: npos is big and positive
479 size_t sep
= wxMin( plus
, minus
);
480 wxString mod
= tmp
.substr( index
, sep
- index
);
491 strcat( buf
, mod
.c_str() );
500 XmString
wxFindAcceleratorText (const char *s
)
504 // VZ: this function returns incorrect keysym which completely breaks kbd
508 // The accelerator text is after the \t char.
509 s
= strchr( s
, '\t' );
511 if( !s
) return NULL
;
513 return wxStringToXmString( s
+ 1 ); // skip TAB!
517 // Change a widget's foreground and background colours.
518 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
520 if (!foregroundColour
.Ok())
523 // When should we specify the foreground, if it's calculated
524 // by wxComputeColours?
525 // Solution: say we start with the default (computed) foreground colour.
526 // If we call SetForegroundColour explicitly for a control or window,
527 // then the foreground is changed.
528 // Therefore SetBackgroundColour computes the foreground colour, and
529 // SetForegroundColour changes the foreground colour. The ordering is
532 XtVaSetValues ((Widget
) widget
,
533 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
537 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
539 if (!backgroundColour
.Ok())
542 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
545 XtVaSetValues ((Widget
) widget
,
546 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
547 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
548 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
549 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
553 XtVaSetValues ((Widget
) widget
,
554 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
558 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
560 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
561 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
562 Widget w
= (Widget
)widget
;
564 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
573 wxString
wxXmStringToString( const XmString
& xmString
)
576 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
583 return wxEmptyString
;
586 XmString
wxStringToXmString( const char* str
)
588 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
591 // ----------------------------------------------------------------------------
592 // wxBitmap utility functions
593 // ----------------------------------------------------------------------------
595 // Creates a bitmap with transparent areas drawn in
597 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
599 wxBitmap
newBitmap(bitmap
.GetWidth(),
605 srcDC
.SelectObjectAsSource(bitmap
);
606 destDC
.SelectObject(newBitmap
);
608 wxBrush
brush(colour
, wxSOLID
);
609 destDC
.SetBackground(brush
);
611 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
612 &srcDC
, 0, 0, wxCOPY
, true);
617 // ----------------------------------------------------------------------------
618 // Miscellaneous functions
619 // ----------------------------------------------------------------------------
621 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
623 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
625 if (style
& wxSIMPLE_BORDER
)
627 borderWidget
= XtVaCreateManagedWidget
630 xmFrameWidgetClass
, parentWidget
,
631 XmNshadowType
, XmSHADOW_ETCHED_IN
,
632 XmNshadowThickness
, 1,
636 else if ((style
& wxSUNKEN_BORDER
) || (style
& wxBORDER_THEME
))
638 borderWidget
= XtVaCreateManagedWidget
641 xmFrameWidgetClass
, parentWidget
,
642 XmNshadowType
, XmSHADOW_IN
,
646 else if (style
& wxRAISED_BORDER
)
648 borderWidget
= XtVaCreateManagedWidget
651 xmFrameWidgetClass
, parentWidget
,
652 XmNshadowType
, XmSHADOW_OUT
,