]>
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 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
* timer
)
141 return new wxMotifTimerImpl(timer
);
144 // ----------------------------------------------------------------------------
146 // ----------------------------------------------------------------------------
148 void wxGetMousePosition( int* x
, int* y
)
157 XQueryPointer(wxGlobalDisplay(),
158 DefaultRootWindow(wxGlobalDisplay()),
160 &(xev
.x_root
), &(xev
.y_root
),
168 // Return true if we have a colour display
169 bool wxColourDisplay()
171 return wxDisplayDepth() > 1;
174 // Returns depth of screen
177 Display
*dpy
= wxGlobalDisplay();
179 return DefaultDepth (dpy
, DefaultScreen (dpy
));
182 // Get size of display
183 void wxDisplaySize(int *width
, int *height
)
185 Display
*dpy
= wxGlobalDisplay();
188 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
190 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
193 void wxDisplaySizeMM(int *width
, int *height
)
195 Display
*dpy
= wxGlobalDisplay();
198 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
200 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
203 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
205 // This is supposed to return desktop dimensions minus any window
206 // manager panels, menus, taskbars, etc. If there is a way to do that
207 // for this platform please fix this function, otherwise it defaults
208 // to the entire desktop.
211 wxDisplaySize(width
, height
);
215 // Configurable display in wxX11 and wxMotif
216 static WXDisplay
*gs_currentDisplay
= NULL
;
217 static wxString gs_displayName
;
219 WXDisplay
*wxGetDisplay()
221 if (gs_currentDisplay
)
222 return gs_currentDisplay
;
224 return wxTheApp
->GetInitialDisplay();
228 bool wxSetDisplay(const wxString
& display_name
)
230 gs_displayName
= display_name
;
232 if ( display_name
.empty() )
234 gs_currentDisplay
= NULL
;
242 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
243 display_name
.c_str(),
244 wxTheApp
->GetAppName().c_str(),
245 wxTheApp
->GetClassName().c_str(),
247 #if XtSpecificationRelease < 5
256 gs_currentDisplay
= (WXDisplay
*) display
;
264 wxString
wxGetDisplayName()
266 return gs_displayName
;
269 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
271 return wxGenericFindWindowAtPoint(pt
);
274 // ----------------------------------------------------------------------------
275 // Some colour manipulation routines
276 // ----------------------------------------------------------------------------
278 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
283 int r
= 0, g
= 0, b
= 0;
286 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
287 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
289 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
292 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
293 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
294 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
297 case 0: r
= v
, g
= t
, b
= p
; break;
298 case 1: r
= q
, g
= v
, b
= p
; break;
299 case 2: r
= p
, g
= v
, b
= t
; break;
300 case 3: r
= p
, g
= q
, b
= v
; break;
301 case 4: r
= t
, g
= p
, b
= v
; break;
302 case 5: r
= v
, g
= p
, b
= q
; break;
304 rgb
->red
= (unsigned short)(r
<< 8);
305 rgb
->green
= (unsigned short)(g
<< 8);
306 rgb
->blue
= (unsigned short)(b
<< 8);
309 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
311 int r
= rgb
->red
>> 8;
312 int g
= rgb
->green
>> 8;
313 int b
= rgb
->blue
>> 8;
314 int maxv
= wxMax3(r
, g
, b
);
315 int minv
= wxMin3(r
, g
, b
);
318 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
323 int rc
, gc
, bc
, hex
= 0;
324 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
325 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
326 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
327 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
328 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
329 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
330 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
334 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
335 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
338 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
343 int screen
= DefaultScreen(d
);
344 int num_colors
= DisplayCells(d
,screen
);
346 XColor
*color_defs
= new XColor
[num_colors
];
347 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
348 XQueryColors(d
,cmp
,color_defs
,num_colors
);
351 wxXColorToHSV(&hsv
,xc
);
353 int diff
, min_diff
= 0, pixel
= 0;
355 for(llp
= 0;llp
< num_colors
;llp
++)
357 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
358 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
359 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
360 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
361 if (llp
== 0) min_diff
= diff
;
362 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
363 if (min_diff
== 0) break;
366 xc
-> red
= color_defs
[pixel
].red
;
367 xc
-> green
= color_defs
[pixel
].green
;
368 xc
-> blue
= color_defs
[pixel
].blue
;
369 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
372 if (!XAllocColor(d,cmp,xc))
373 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
380 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
382 if (!XAllocColor(d
,cmp
,xc
))
384 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
385 wxAllocNearestColor(d
,cmp
,xc
);
390 wxString
wxGetXEventName(XEvent
& event
)
393 wxString
str(wxT("(some event)"));
396 int type
= event
.xany
.type
;
397 static char* event_name
[] = {
398 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
399 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
400 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
401 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
402 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
403 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
404 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
405 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
406 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
407 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
408 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
409 wxMOTIF_STR("unknown(+)")}; // 35
410 type
= wxMin(35, type
); type
= wxMax(1, type
);
411 wxString
str(event_name
[type
]);
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
421 // Find the letter corresponding to the mnemonic, for Motif
422 char wxFindMnemonic (const char *s
)
425 int len
= strlen (s
);
428 for (i
= 0; i
< len
; i
++)
432 // Carefully handle &&
433 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
445 char* wxFindAccelerator( const char *s
)
449 // VZ: this function returns incorrect keysym which completely breaks kbd
453 // The accelerator text is after the \t char.
454 s
= strchr( s
, '\t' );
456 if( !s
) return NULL
;
459 Now we need to format it as X standard:
464 Ctrl+N --> Ctrl<Key>N
466 Ctrl+Shift+A --> Ctrl Shift<Key>A
468 and handle Ctrl-N & similia
471 static char buf
[256];
474 wxString tmp
= s
+ 1; // skip TAB
477 while( index
< tmp
.length() )
479 size_t plus
= tmp
.find( '+', index
);
480 size_t minus
= tmp
.find( '-', index
);
482 // neither '+' nor '-', add <Key>
483 if( plus
== wxString::npos
&& minus
== wxString::npos
)
485 strcat( buf
, "<Key>" );
486 strcat( buf
, tmp
.c_str() + index
);
491 // OK: npos is big and positive
492 size_t sep
= wxMin( plus
, minus
);
493 wxString mod
= tmp
.substr( index
, sep
- index
);
504 strcat( buf
, mod
.c_str() );
513 XmString
wxFindAcceleratorText (const char *s
)
517 // VZ: this function returns incorrect keysym which completely breaks kbd
521 // The accelerator text is after the \t char.
522 s
= strchr( s
, '\t' );
524 if( !s
) return NULL
;
526 return wxStringToXmString( s
+ 1 ); // skip TAB!
530 // Change a widget's foreground and background colours.
531 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
533 if (!foregroundColour
.Ok())
536 // When should we specify the foreground, if it's calculated
537 // by wxComputeColours?
538 // Solution: say we start with the default (computed) foreground colour.
539 // If we call SetForegroundColour explicitly for a control or window,
540 // then the foreground is changed.
541 // Therefore SetBackgroundColour computes the foreground colour, and
542 // SetForegroundColour changes the foreground colour. The ordering is
545 XtVaSetValues ((Widget
) widget
,
546 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
550 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
552 if (!backgroundColour
.Ok())
555 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
558 XtVaSetValues ((Widget
) widget
,
559 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
560 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
561 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
562 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
566 XtVaSetValues ((Widget
) widget
,
567 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
571 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
573 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
574 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
575 Widget w
= (Widget
)widget
;
577 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
586 wxString
wxXmStringToString( const XmString
& xmString
)
589 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
596 return wxEmptyString
;
599 XmString
wxStringToXmString( const wxString
& str
)
601 return wxStringToXmString(str
.mb_str());
604 XmString
wxStringToXmString( const char* str
)
606 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
609 // ----------------------------------------------------------------------------
610 // wxBitmap utility functions
611 // ----------------------------------------------------------------------------
613 // Creates a bitmap with transparent areas drawn in
615 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
617 wxBitmap
newBitmap(bitmap
.GetWidth(),
623 srcDC
.SelectObjectAsSource(bitmap
);
624 destDC
.SelectObject(newBitmap
);
626 wxBrush
brush(colour
, wxSOLID
);
627 destDC
.SetBackground(brush
);
629 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
630 &srcDC
, 0, 0, wxCOPY
, true);
635 // ----------------------------------------------------------------------------
636 // Miscellaneous functions
637 // ----------------------------------------------------------------------------
639 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
641 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
643 if (style
& wxSIMPLE_BORDER
)
645 borderWidget
= XtVaCreateManagedWidget
648 xmFrameWidgetClass
, parentWidget
,
649 XmNshadowType
, XmSHADOW_ETCHED_IN
,
650 XmNshadowThickness
, 1,
654 else if (style
& wxSUNKEN_BORDER
)
656 borderWidget
= XtVaCreateManagedWidget
659 xmFrameWidgetClass
, parentWidget
,
660 XmNshadowType
, XmSHADOW_IN
,
664 else if (style
& wxRAISED_BORDER
)
666 borderWidget
= XtVaCreateManagedWidget
669 xmFrameWidgetClass
, parentWidget
,
670 XmNshadowType
, XmSHADOW_OUT
,