]>
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 // Configurable display in wxX11 and wxMotif
209 static WXDisplay
*gs_currentDisplay
= NULL
;
210 static wxString gs_displayName
;
212 WXDisplay
*wxGetDisplay()
214 if (gs_currentDisplay
)
215 return gs_currentDisplay
;
217 return wxTheApp
->GetInitialDisplay();
221 bool wxSetDisplay(const wxString
& display_name
)
223 gs_displayName
= display_name
;
225 if ( display_name
.empty() )
227 gs_currentDisplay
= NULL
;
235 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
236 display_name
.c_str(),
237 wxTheApp
->GetAppName().c_str(),
238 wxTheApp
->GetClassName().c_str(),
240 #if XtSpecificationRelease < 5
249 gs_currentDisplay
= (WXDisplay
*) display
;
257 wxString
wxGetDisplayName()
259 return gs_displayName
;
262 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
264 return wxGenericFindWindowAtPoint(pt
);
267 // ----------------------------------------------------------------------------
268 // Some colour manipulation routines
269 // ----------------------------------------------------------------------------
271 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
276 int r
= 0, g
= 0, b
= 0;
279 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
280 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
282 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
285 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
286 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
287 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
290 case 0: r
= v
, g
= t
, b
= p
; break;
291 case 1: r
= q
, g
= v
, b
= p
; break;
292 case 2: r
= p
, g
= v
, b
= t
; break;
293 case 3: r
= p
, g
= q
, b
= v
; break;
294 case 4: r
= t
, g
= p
, b
= v
; break;
295 case 5: r
= v
, g
= p
, b
= q
; break;
297 rgb
->red
= (unsigned short)(r
<< 8);
298 rgb
->green
= (unsigned short)(g
<< 8);
299 rgb
->blue
= (unsigned short)(b
<< 8);
302 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
304 int r
= rgb
->red
>> 8;
305 int g
= rgb
->green
>> 8;
306 int b
= rgb
->blue
>> 8;
307 int maxv
= wxMax3(r
, g
, b
);
308 int minv
= wxMin3(r
, g
, b
);
311 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
316 int rc
, gc
, bc
, hex
= 0;
317 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
318 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
319 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
320 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
321 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
322 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
323 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
327 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
328 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
331 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
336 int screen
= DefaultScreen(d
);
337 int num_colors
= DisplayCells(d
,screen
);
339 XColor
*color_defs
= new XColor
[num_colors
];
340 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
341 XQueryColors(d
,cmp
,color_defs
,num_colors
);
344 wxXColorToHSV(&hsv
,xc
);
346 int diff
, min_diff
= 0, pixel
= 0;
348 for(llp
= 0;llp
< num_colors
;llp
++)
350 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
351 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
352 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
353 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
354 if (llp
== 0) min_diff
= diff
;
355 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
356 if (min_diff
== 0) break;
359 xc
-> red
= color_defs
[pixel
].red
;
360 xc
-> green
= color_defs
[pixel
].green
;
361 xc
-> blue
= color_defs
[pixel
].blue
;
362 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
365 if (!XAllocColor(d,cmp,xc))
366 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
373 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
375 if (!XAllocColor(d
,cmp
,xc
))
377 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
378 wxAllocNearestColor(d
,cmp
,xc
);
383 wxString
wxGetXEventName(XEvent
& event
)
386 wxString
str(wxT("(some event)"));
389 int type
= event
.xany
.type
;
390 static char* event_name
[] = {
391 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
392 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
393 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
394 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
395 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
396 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
397 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
398 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
399 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
400 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
401 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
402 wxMOTIF_STR("unknown(+)")}; // 35
403 type
= wxMin(35, type
); type
= wxMax(1, type
);
404 wxString
str(event_name
[type
]);
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 // Find the letter corresponding to the mnemonic, for Motif
415 char wxFindMnemonic (const char *s
)
418 int len
= strlen (s
);
421 for (i
= 0; i
< len
; i
++)
425 // Carefully handle &&
426 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
438 char* wxFindAccelerator( const char *s
)
442 // VZ: this function returns incorrect keysym which completely breaks kbd
446 // The accelerator text is after the \t char.
447 s
= strchr( s
, '\t' );
449 if( !s
) return NULL
;
452 Now we need to format it as X standard:
457 Ctrl+N --> Ctrl<Key>N
459 Ctrl+Shift+A --> Ctrl Shift<Key>A
461 and handle Ctrl-N & similia
464 static char buf
[256];
467 wxString tmp
= s
+ 1; // skip TAB
470 while( index
< tmp
.length() )
472 size_t plus
= tmp
.find( '+', index
);
473 size_t minus
= tmp
.find( '-', index
);
475 // neither '+' nor '-', add <Key>
476 if( plus
== wxString::npos
&& minus
== wxString::npos
)
478 strcat( buf
, "<Key>" );
479 strcat( buf
, tmp
.c_str() + index
);
484 // OK: npos is big and positive
485 size_t sep
= wxMin( plus
, minus
);
486 wxString mod
= tmp
.substr( index
, sep
- index
);
497 strcat( buf
, mod
.c_str() );
506 XmString
wxFindAcceleratorText (const char *s
)
510 // VZ: this function returns incorrect keysym which completely breaks kbd
514 // The accelerator text is after the \t char.
515 s
= strchr( s
, '\t' );
517 if( !s
) return NULL
;
519 return wxStringToXmString( s
+ 1 ); // skip TAB!
523 // Change a widget's foreground and background colours.
524 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
526 if (!foregroundColour
.Ok())
529 // When should we specify the foreground, if it's calculated
530 // by wxComputeColours?
531 // Solution: say we start with the default (computed) foreground colour.
532 // If we call SetForegroundColour explicitly for a control or window,
533 // then the foreground is changed.
534 // Therefore SetBackgroundColour computes the foreground colour, and
535 // SetForegroundColour changes the foreground colour. The ordering is
538 XtVaSetValues ((Widget
) widget
,
539 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
543 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
545 if (!backgroundColour
.Ok())
548 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
551 XtVaSetValues ((Widget
) widget
,
552 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
553 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
554 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
555 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
559 XtVaSetValues ((Widget
) widget
,
560 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
564 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
566 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
567 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
568 Widget w
= (Widget
)widget
;
570 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
579 wxString
wxXmStringToString( const XmString
& xmString
)
582 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
589 return wxEmptyString
;
592 XmString
wxStringToXmString( const char* str
)
594 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
597 // ----------------------------------------------------------------------------
598 // wxBitmap utility functions
599 // ----------------------------------------------------------------------------
601 // Creates a bitmap with transparent areas drawn in
603 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
605 wxBitmap
newBitmap(bitmap
.GetWidth(),
611 srcDC
.SelectObjectAsSource(bitmap
);
612 destDC
.SelectObject(newBitmap
);
614 wxBrush
brush(colour
, wxSOLID
);
615 destDC
.SetBackground(brush
);
617 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
618 &srcDC
, 0, 0, wxCOPY
, true);
623 // ----------------------------------------------------------------------------
624 // Miscellaneous functions
625 // ----------------------------------------------------------------------------
627 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
629 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
631 if (style
& wxSIMPLE_BORDER
)
633 borderWidget
= XtVaCreateManagedWidget
636 xmFrameWidgetClass
, parentWidget
,
637 XmNshadowType
, XmSHADOW_ETCHED_IN
,
638 XmNshadowThickness
, 1,
642 else if ((style
& wxSUNKEN_BORDER
) || (style
& wxBORDER_THEME
))
644 borderWidget
= XtVaCreateManagedWidget
647 xmFrameWidgetClass
, parentWidget
,
648 XmNshadowType
, XmSHADOW_IN
,
652 else if (style
& wxRAISED_BORDER
)
654 borderWidget
= XtVaCreateManagedWidget
657 xmFrameWidgetClass
, parentWidget
,
658 XmNshadowType
, XmSHADOW_OUT
,