]>
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
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 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
87 // Use current setting for the bell
88 XBell (wxGlobalDisplay(), 0);
91 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
93 // XmVERSION and XmREVISION are defined in Xm/Xm.h
102 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
104 return new wxEventLoop
;
107 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
* timer
)
109 return new wxMotifTimerImpl(timer
);
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 void wxGetMousePosition( int* x
, int* y
)
125 XQueryPointer(wxGlobalDisplay(),
126 DefaultRootWindow(wxGlobalDisplay()),
128 &(xev
.x_root
), &(xev
.y_root
),
136 // Return true if we have a colour display
137 bool wxColourDisplay()
139 return wxDisplayDepth() > 1;
142 // Returns depth of screen
145 Display
*dpy
= wxGlobalDisplay();
147 return DefaultDepth (dpy
, DefaultScreen (dpy
));
150 // Get size of display
151 void wxDisplaySize(int *width
, int *height
)
153 Display
*dpy
= wxGlobalDisplay();
156 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
158 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
161 void wxDisplaySizeMM(int *width
, int *height
)
163 Display
*dpy
= wxGlobalDisplay();
166 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
168 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
171 // Configurable display in wxX11 and wxMotif
172 static WXDisplay
*gs_currentDisplay
= NULL
;
173 static wxString gs_displayName
;
175 WXDisplay
*wxGetDisplay()
177 if (gs_currentDisplay
)
178 return gs_currentDisplay
;
180 return wxTheApp
->GetInitialDisplay();
184 bool wxSetDisplay(const wxString
& display_name
)
186 gs_displayName
= display_name
;
188 if ( display_name
.empty() )
190 gs_currentDisplay
= NULL
;
198 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
199 display_name
.c_str(),
200 wxTheApp
->GetAppName().c_str(),
201 wxTheApp
->GetClassName().c_str(),
203 #if XtSpecificationRelease < 5
212 gs_currentDisplay
= (WXDisplay
*) display
;
220 wxString
wxGetDisplayName()
222 return gs_displayName
;
225 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
227 return wxGenericFindWindowAtPoint(pt
);
230 // ----------------------------------------------------------------------------
231 // Some colour manipulation routines
232 // ----------------------------------------------------------------------------
234 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
239 int r
= 0, g
= 0, b
= 0;
242 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
243 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
245 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
248 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
249 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
250 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
253 case 0: r
= v
, g
= t
, b
= p
; break;
254 case 1: r
= q
, g
= v
, b
= p
; break;
255 case 2: r
= p
, g
= v
, b
= t
; break;
256 case 3: r
= p
, g
= q
, b
= v
; break;
257 case 4: r
= t
, g
= p
, b
= v
; break;
258 case 5: r
= v
, g
= p
, b
= q
; break;
260 rgb
->red
= (unsigned short)(r
<< 8);
261 rgb
->green
= (unsigned short)(g
<< 8);
262 rgb
->blue
= (unsigned short)(b
<< 8);
265 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
267 int r
= rgb
->red
>> 8;
268 int g
= rgb
->green
>> 8;
269 int b
= rgb
->blue
>> 8;
270 int maxv
= wxMax3(r
, g
, b
);
271 int minv
= wxMin3(r
, g
, b
);
274 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
279 int rc
, gc
, bc
, hex
= 0;
280 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
281 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
282 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
283 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
284 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
285 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
286 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
290 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
291 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
294 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
299 int screen
= DefaultScreen(d
);
300 int num_colors
= DisplayCells(d
,screen
);
302 XColor
*color_defs
= new XColor
[num_colors
];
303 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
304 XQueryColors(d
,cmp
,color_defs
,num_colors
);
307 wxXColorToHSV(&hsv
,xc
);
309 int diff
, min_diff
= 0, pixel
= 0;
311 for(llp
= 0;llp
< num_colors
;llp
++)
313 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
314 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
315 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
316 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
317 if (llp
== 0) min_diff
= diff
;
318 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
319 if (min_diff
== 0) break;
322 xc
-> red
= color_defs
[pixel
].red
;
323 xc
-> green
= color_defs
[pixel
].green
;
324 xc
-> blue
= color_defs
[pixel
].blue
;
325 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
328 if (!XAllocColor(d,cmp,xc))
329 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
336 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
338 if (!XAllocColor(d
,cmp
,xc
))
340 // cout << "wxAllocColor : Warning : cannot allocate color, attempt find nearest !\n";
341 wxAllocNearestColor(d
,cmp
,xc
);
345 wxString
wxGetXEventName(XEvent
& event
)
348 wxString
str(wxT("(some event)"));
351 int type
= event
.xany
.type
;
352 static char* event_name
[] = {
353 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
354 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
355 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
356 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
357 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
358 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
359 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
360 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
361 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
362 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
363 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
364 wxMOTIF_STR("unknown(+)")}; // 35
365 type
= wxMin(35, type
); type
= wxMax(1, type
);
366 wxString
str(event_name
[type
]);
371 // ----------------------------------------------------------------------------
373 // ----------------------------------------------------------------------------
375 // Find the letter corresponding to the mnemonic, for Motif
376 char wxFindMnemonic (const char *s
)
379 int len
= strlen (s
);
382 for (i
= 0; i
< len
; i
++)
386 // Carefully handle &&
387 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
399 char* wxFindAccelerator( const char *s
)
403 // VZ: this function returns incorrect keysym which completely breaks kbd
407 // The accelerator text is after the \t char.
408 s
= strchr( s
, '\t' );
410 if( !s
) return NULL
;
413 Now we need to format it as X standard:
418 Ctrl+N --> Ctrl<Key>N
420 Ctrl+Shift+A --> Ctrl Shift<Key>A
422 and handle Ctrl-N & similia
425 static char buf
[256];
428 wxString tmp
= s
+ 1; // skip TAB
431 while( index
< tmp
.length() )
433 size_t plus
= tmp
.find( '+', index
);
434 size_t minus
= tmp
.find( '-', index
);
436 // neither '+' nor '-', add <Key>
437 if( plus
== wxString::npos
&& minus
== wxString::npos
)
439 strcat( buf
, "<Key>" );
440 strcat( buf
, tmp
.c_str() + index
);
445 // OK: npos is big and positive
446 size_t sep
= wxMin( plus
, minus
);
447 wxString mod
= tmp
.substr( index
, sep
- index
);
458 strcat( buf
, mod
.c_str() );
467 XmString
wxFindAcceleratorText (const char *s
)
471 // VZ: this function returns incorrect keysym which completely breaks kbd
475 // The accelerator text is after the \t char.
476 s
= strchr( s
, '\t' );
478 if( !s
) return NULL
;
480 return wxStringToXmString( s
+ 1 ); // skip TAB!
484 // Change a widget's foreground and background colours.
485 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
487 if (!foregroundColour
.IsOk())
490 // When should we specify the foreground, if it's calculated
491 // by wxComputeColours?
492 // Solution: say we start with the default (computed) foreground colour.
493 // If we call SetForegroundColour explicitly for a control or window,
494 // then the foreground is changed.
495 // Therefore SetBackgroundColour computes the foreground colour, and
496 // SetForegroundColour changes the foreground colour. The ordering is
499 XtVaSetValues ((Widget
) widget
,
500 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
504 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
506 if (!backgroundColour
.IsOk())
509 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
512 XtVaSetValues ((Widget
) widget
,
513 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
514 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
515 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
516 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
520 XtVaSetValues ((Widget
) widget
,
521 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
525 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
527 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
528 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
529 Widget w
= (Widget
)widget
;
531 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
540 wxString
wxXmStringToString( const XmString
& xmString
)
543 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
550 return wxEmptyString
;
553 XmString
wxStringToXmString( const char* str
)
555 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
558 // ----------------------------------------------------------------------------
559 // wxBitmap utility functions
560 // ----------------------------------------------------------------------------
562 // Creates a bitmap with transparent areas drawn in
564 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
566 wxBitmap
newBitmap(bitmap
.GetWidth(),
572 srcDC
.SelectObjectAsSource(bitmap
);
573 destDC
.SelectObject(newBitmap
);
575 wxBrush
brush(colour
, wxSOLID
);
576 destDC
.SetBackground(brush
);
578 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
579 &srcDC
, 0, 0, wxCOPY
, true);
584 // ----------------------------------------------------------------------------
585 // Miscellaneous functions
586 // ----------------------------------------------------------------------------
588 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
590 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
592 if (style
& wxSIMPLE_BORDER
)
594 borderWidget
= XtVaCreateManagedWidget
597 xmFrameWidgetClass
, parentWidget
,
598 XmNshadowType
, XmSHADOW_ETCHED_IN
,
599 XmNshadowThickness
, 1,
603 else if ((style
& wxSUNKEN_BORDER
) || (style
& wxBORDER_THEME
))
605 borderWidget
= XtVaCreateManagedWidget
608 xmFrameWidgetClass
, parentWidget
,
609 XmNshadowType
, XmSHADOW_IN
,
613 else if (style
& wxRAISED_BORDER
)
615 borderWidget
= XtVaCreateManagedWidget
618 xmFrameWidgetClass
, parentWidget
,
619 XmNshadowType
, XmSHADOW_OUT
,