]>
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"
24 #define XtDisplay XTDISPLAY
31 #include "wx/dcmemory.h"
32 #include "wx/bitmap.h"
35 #include "wx/apptrait.h"
36 #include "wx/evtloop.h"
37 #include "wx/motif/private/timer.h"
41 #if (defined(__SUNCC__) || defined(__CLCC__))
46 #pragma message disable nosimpint
49 #include "wx/unix/execute.h"
54 #include "wx/motif/private.h"
56 #include "X11/Xutil.h"
59 #pragma message enable nosimpint
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
68 // async event processing
69 // ----------------------------------------------------------------------------
71 // Consume all events until no more left
72 void wxFlushEvents(WXDisplay
* wxdisplay
)
74 Display
*display
= (Display
*)wxdisplay
;
77 XSync (display
, False
);
79 while (evtLoop
.Pending())
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
93 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
95 wxHandleProcessTermination(proc_data
);
97 // VZ: I think they should be the same...
98 wxASSERT( (int)*id
== proc_data
->tag
);
103 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
105 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
107 (XtPointer
*) XtInputReadMask
,
108 (XtInputCallbackProc
) xt_notify_end_process
,
109 (XtPointer
) proc_data
);
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
120 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
123 // Use current setting for the bell
124 XBell (wxGlobalDisplay(), 0);
128 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
130 // XmVERSION and XmREVISION are defined in Xm/Xm.h
134 *verMin
= XmREVISION
;
139 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
141 return new wxEventLoop
;
144 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
* timer
)
146 return new wxMotifTimerImpl(timer
);
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 void wxGetMousePosition( int* x
, int* y
)
162 XQueryPointer(wxGlobalDisplay(),
163 DefaultRootWindow(wxGlobalDisplay()),
165 &(xev
.x_root
), &(xev
.y_root
),
173 // Return true if we have a colour display
174 bool wxColourDisplay()
176 return wxDisplayDepth() > 1;
179 // Returns depth of screen
182 Display
*dpy
= wxGlobalDisplay();
184 return DefaultDepth (dpy
, DefaultScreen (dpy
));
187 // Get size of display
188 void wxDisplaySize(int *width
, int *height
)
190 Display
*dpy
= wxGlobalDisplay();
193 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
195 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
198 void wxDisplaySizeMM(int *width
, int *height
)
200 Display
*dpy
= wxGlobalDisplay();
203 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
205 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
208 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
210 // This is supposed to return desktop dimensions minus any window
211 // manager panels, menus, taskbars, etc. If there is a way to do that
212 // for this platform please fix this function, otherwise it defaults
213 // to the entire desktop.
216 wxDisplaySize(width
, height
);
220 // Configurable display in wxX11 and wxMotif
221 static WXDisplay
*gs_currentDisplay
= NULL
;
222 static wxString gs_displayName
;
224 WXDisplay
*wxGetDisplay()
226 if (gs_currentDisplay
)
227 return gs_currentDisplay
;
229 return wxTheApp
->GetInitialDisplay();
233 bool wxSetDisplay(const wxString
& display_name
)
235 gs_displayName
= display_name
;
237 if ( display_name
.empty() )
239 gs_currentDisplay
= NULL
;
247 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
248 display_name
.c_str(),
249 wxTheApp
->GetAppName().c_str(),
250 wxTheApp
->GetClassName().c_str(),
252 #if XtSpecificationRelease < 5
261 gs_currentDisplay
= (WXDisplay
*) display
;
269 wxString
wxGetDisplayName()
271 return gs_displayName
;
274 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
276 return wxGenericFindWindowAtPoint(pt
);
279 // ----------------------------------------------------------------------------
280 // Some colour manipulation routines
281 // ----------------------------------------------------------------------------
283 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
288 int r
= 0, g
= 0, b
= 0;
291 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
292 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
294 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
297 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
298 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
299 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
302 case 0: r
= v
, g
= t
, b
= p
; break;
303 case 1: r
= q
, g
= v
, b
= p
; break;
304 case 2: r
= p
, g
= v
, b
= t
; break;
305 case 3: r
= p
, g
= q
, b
= v
; break;
306 case 4: r
= t
, g
= p
, b
= v
; break;
307 case 5: r
= v
, g
= p
, b
= q
; break;
309 rgb
->red
= (unsigned short)(r
<< 8);
310 rgb
->green
= (unsigned short)(g
<< 8);
311 rgb
->blue
= (unsigned short)(b
<< 8);
314 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
316 int r
= rgb
->red
>> 8;
317 int g
= rgb
->green
>> 8;
318 int b
= rgb
->blue
>> 8;
319 int maxv
= wxMax3(r
, g
, b
);
320 int minv
= wxMin3(r
, g
, b
);
323 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
328 int rc
, gc
, bc
, hex
= 0;
329 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
330 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
331 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
332 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
333 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
334 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
335 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
339 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
340 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
343 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
348 int screen
= DefaultScreen(d
);
349 int num_colors
= DisplayCells(d
,screen
);
351 XColor
*color_defs
= new XColor
[num_colors
];
352 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
353 XQueryColors(d
,cmp
,color_defs
,num_colors
);
356 wxXColorToHSV(&hsv
,xc
);
358 int diff
, min_diff
= 0, pixel
= 0;
360 for(llp
= 0;llp
< num_colors
;llp
++)
362 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
363 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
364 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
365 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
366 if (llp
== 0) min_diff
= diff
;
367 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
368 if (min_diff
== 0) break;
371 xc
-> red
= color_defs
[pixel
].red
;
372 xc
-> green
= color_defs
[pixel
].green
;
373 xc
-> blue
= color_defs
[pixel
].blue
;
374 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
377 if (!XAllocColor(d,cmp,xc))
378 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
385 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
387 if (!XAllocColor(d
,cmp
,xc
))
389 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
390 wxAllocNearestColor(d
,cmp
,xc
);
395 wxString
wxGetXEventName(XEvent
& event
)
398 wxString
str(wxT("(some event)"));
401 int type
= event
.xany
.type
;
402 static char* event_name
[] = {
403 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
404 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
405 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
406 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
407 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
408 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
409 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
410 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
411 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
412 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
413 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
414 wxMOTIF_STR("unknown(+)")}; // 35
415 type
= wxMin(35, type
); type
= wxMax(1, type
);
416 wxString
str(event_name
[type
]);
422 // ----------------------------------------------------------------------------
424 // ----------------------------------------------------------------------------
426 // Find the letter corresponding to the mnemonic, for Motif
427 char wxFindMnemonic (const char *s
)
430 int len
= strlen (s
);
433 for (i
= 0; i
< len
; i
++)
437 // Carefully handle &&
438 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
450 char* wxFindAccelerator( const char *s
)
454 // VZ: this function returns incorrect keysym which completely breaks kbd
458 // The accelerator text is after the \t char.
459 s
= strchr( s
, '\t' );
461 if( !s
) return NULL
;
464 Now we need to format it as X standard:
469 Ctrl+N --> Ctrl<Key>N
471 Ctrl+Shift+A --> Ctrl Shift<Key>A
473 and handle Ctrl-N & similia
476 static char buf
[256];
479 wxString tmp
= s
+ 1; // skip TAB
482 while( index
< tmp
.length() )
484 size_t plus
= tmp
.find( '+', index
);
485 size_t minus
= tmp
.find( '-', index
);
487 // neither '+' nor '-', add <Key>
488 if( plus
== wxString::npos
&& minus
== wxString::npos
)
490 strcat( buf
, "<Key>" );
491 strcat( buf
, tmp
.c_str() + index
);
496 // OK: npos is big and positive
497 size_t sep
= wxMin( plus
, minus
);
498 wxString mod
= tmp
.substr( index
, sep
- index
);
509 strcat( buf
, mod
.c_str() );
518 XmString
wxFindAcceleratorText (const char *s
)
522 // VZ: this function returns incorrect keysym which completely breaks kbd
526 // The accelerator text is after the \t char.
527 s
= strchr( s
, '\t' );
529 if( !s
) return NULL
;
531 return wxStringToXmString( s
+ 1 ); // skip TAB!
535 // Change a widget's foreground and background colours.
536 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
538 if (!foregroundColour
.Ok())
541 // When should we specify the foreground, if it's calculated
542 // by wxComputeColours?
543 // Solution: say we start with the default (computed) foreground colour.
544 // If we call SetForegroundColour explicitly for a control or window,
545 // then the foreground is changed.
546 // Therefore SetBackgroundColour computes the foreground colour, and
547 // SetForegroundColour changes the foreground colour. The ordering is
550 XtVaSetValues ((Widget
) widget
,
551 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
555 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
557 if (!backgroundColour
.Ok())
560 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
563 XtVaSetValues ((Widget
) widget
,
564 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
565 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
566 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
567 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
571 XtVaSetValues ((Widget
) widget
,
572 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
576 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
578 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
579 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
580 Widget w
= (Widget
)widget
;
582 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
591 wxString
wxXmStringToString( const XmString
& xmString
)
594 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
601 return wxEmptyString
;
604 XmString
wxStringToXmString( const wxString
& str
)
606 return wxStringToXmString(str
.mb_str());
609 XmString
wxStringToXmString( const char* str
)
611 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
614 // ----------------------------------------------------------------------------
615 // wxBitmap utility functions
616 // ----------------------------------------------------------------------------
618 // Creates a bitmap with transparent areas drawn in
620 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
622 wxBitmap
newBitmap(bitmap
.GetWidth(),
628 srcDC
.SelectObjectAsSource(bitmap
);
629 destDC
.SelectObject(newBitmap
);
631 wxBrush
brush(colour
, wxSOLID
);
632 destDC
.SetBackground(brush
);
634 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
635 &srcDC
, 0, 0, wxCOPY
, true);
640 // ----------------------------------------------------------------------------
641 // Miscellaneous functions
642 // ----------------------------------------------------------------------------
644 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
646 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
648 if (style
& wxSIMPLE_BORDER
)
650 borderWidget
= XtVaCreateManagedWidget
653 xmFrameWidgetClass
, parentWidget
,
654 XmNshadowType
, XmSHADOW_ETCHED_IN
,
655 XmNshadowThickness
, 1,
659 else if (style
& wxSUNKEN_BORDER
)
661 borderWidget
= XtVaCreateManagedWidget
664 xmFrameWidgetClass
, parentWidget
,
665 XmNshadowType
, XmSHADOW_IN
,
669 else if (style
& wxRAISED_BORDER
)
671 borderWidget
= XtVaCreateManagedWidget
674 xmFrameWidgetClass
, parentWidget
,
675 XmNshadowType
, XmSHADOW_OUT
,