]>
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 // ----------------------------------------------------------------------------
117 // Use current setting for the bell
118 XBell (wxGlobalDisplay(), 0);
121 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
123 // XmVERSION and XmREVISION are defined in Xm/Xm.h
127 *verMin
= XmREVISION
;
132 wxEventLoopBase
* wxGUIAppTraits::CreateEventLoop()
134 return new wxEventLoop
;
137 wxTimerImpl
* wxGUIAppTraits::CreateTimerImpl(wxTimer
* timer
)
139 return new wxMotifTimerImpl(timer
);
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 void wxGetMousePosition( int* x
, int* y
)
155 XQueryPointer(wxGlobalDisplay(),
156 DefaultRootWindow(wxGlobalDisplay()),
158 &(xev
.x_root
), &(xev
.y_root
),
166 // Return true if we have a colour display
167 bool wxColourDisplay()
169 return wxDisplayDepth() > 1;
172 // Returns depth of screen
175 Display
*dpy
= wxGlobalDisplay();
177 return DefaultDepth (dpy
, DefaultScreen (dpy
));
180 // Get size of display
181 void wxDisplaySize(int *width
, int *height
)
183 Display
*dpy
= wxGlobalDisplay();
186 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
188 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
191 void wxDisplaySizeMM(int *width
, int *height
)
193 Display
*dpy
= wxGlobalDisplay();
196 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
198 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
201 // Configurable display in wxX11 and wxMotif
202 static WXDisplay
*gs_currentDisplay
= NULL
;
203 static wxString gs_displayName
;
205 WXDisplay
*wxGetDisplay()
207 if (gs_currentDisplay
)
208 return gs_currentDisplay
;
210 return wxTheApp
->GetInitialDisplay();
214 bool wxSetDisplay(const wxString
& display_name
)
216 gs_displayName
= display_name
;
218 if ( display_name
.empty() )
220 gs_currentDisplay
= NULL
;
228 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
229 display_name
.c_str(),
230 wxTheApp
->GetAppName().c_str(),
231 wxTheApp
->GetClassName().c_str(),
233 #if XtSpecificationRelease < 5
242 gs_currentDisplay
= (WXDisplay
*) display
;
250 wxString
wxGetDisplayName()
252 return gs_displayName
;
255 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
257 return wxGenericFindWindowAtPoint(pt
);
260 // ----------------------------------------------------------------------------
261 // Some colour manipulation routines
262 // ----------------------------------------------------------------------------
264 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
269 int r
= 0, g
= 0, b
= 0;
272 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
273 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
275 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
278 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
279 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
280 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
283 case 0: r
= v
, g
= t
, b
= p
; break;
284 case 1: r
= q
, g
= v
, b
= p
; break;
285 case 2: r
= p
, g
= v
, b
= t
; break;
286 case 3: r
= p
, g
= q
, b
= v
; break;
287 case 4: r
= t
, g
= p
, b
= v
; break;
288 case 5: r
= v
, g
= p
, b
= q
; break;
290 rgb
->red
= (unsigned short)(r
<< 8);
291 rgb
->green
= (unsigned short)(g
<< 8);
292 rgb
->blue
= (unsigned short)(b
<< 8);
295 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
297 int r
= rgb
->red
>> 8;
298 int g
= rgb
->green
>> 8;
299 int b
= rgb
->blue
>> 8;
300 int maxv
= wxMax3(r
, g
, b
);
301 int minv
= wxMin3(r
, g
, b
);
304 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
309 int rc
, gc
, bc
, hex
= 0;
310 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
311 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
312 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
313 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
314 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
315 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
316 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
320 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
321 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
324 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
329 int screen
= DefaultScreen(d
);
330 int num_colors
= DisplayCells(d
,screen
);
332 XColor
*color_defs
= new XColor
[num_colors
];
333 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
334 XQueryColors(d
,cmp
,color_defs
,num_colors
);
337 wxXColorToHSV(&hsv
,xc
);
339 int diff
, min_diff
= 0, pixel
= 0;
341 for(llp
= 0;llp
< num_colors
;llp
++)
343 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
344 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
345 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
346 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
347 if (llp
== 0) min_diff
= diff
;
348 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
349 if (min_diff
== 0) break;
352 xc
-> red
= color_defs
[pixel
].red
;
353 xc
-> green
= color_defs
[pixel
].green
;
354 xc
-> blue
= color_defs
[pixel
].blue
;
355 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
358 if (!XAllocColor(d,cmp,xc))
359 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
366 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
368 if (!XAllocColor(d
,cmp
,xc
))
370 // cout << "wxAllocColor : Warning : cannot allocate color, attempt find nearest !\n";
371 wxAllocNearestColor(d
,cmp
,xc
);
375 wxString
wxGetXEventName(XEvent
& event
)
378 wxString
str(wxT("(some event)"));
381 int type
= event
.xany
.type
;
382 static char* event_name
[] = {
383 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
384 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
385 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
386 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
387 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
388 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
389 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
390 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
391 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
392 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
393 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
394 wxMOTIF_STR("unknown(+)")}; // 35
395 type
= wxMin(35, type
); type
= wxMax(1, type
);
396 wxString
str(event_name
[type
]);
401 // ----------------------------------------------------------------------------
403 // ----------------------------------------------------------------------------
405 // Find the letter corresponding to the mnemonic, for Motif
406 char wxFindMnemonic (const char *s
)
409 int len
= strlen (s
);
412 for (i
= 0; i
< len
; i
++)
416 // Carefully handle &&
417 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
429 char* wxFindAccelerator( const char *s
)
433 // VZ: this function returns incorrect keysym which completely breaks kbd
437 // The accelerator text is after the \t char.
438 s
= strchr( s
, '\t' );
440 if( !s
) return NULL
;
443 Now we need to format it as X standard:
448 Ctrl+N --> Ctrl<Key>N
450 Ctrl+Shift+A --> Ctrl Shift<Key>A
452 and handle Ctrl-N & similia
455 static char buf
[256];
458 wxString tmp
= s
+ 1; // skip TAB
461 while( index
< tmp
.length() )
463 size_t plus
= tmp
.find( '+', index
);
464 size_t minus
= tmp
.find( '-', index
);
466 // neither '+' nor '-', add <Key>
467 if( plus
== wxString::npos
&& minus
== wxString::npos
)
469 strcat( buf
, "<Key>" );
470 strcat( buf
, tmp
.c_str() + index
);
475 // OK: npos is big and positive
476 size_t sep
= wxMin( plus
, minus
);
477 wxString mod
= tmp
.substr( index
, sep
- index
);
488 strcat( buf
, mod
.c_str() );
497 XmString
wxFindAcceleratorText (const char *s
)
501 // VZ: this function returns incorrect keysym which completely breaks kbd
505 // The accelerator text is after the \t char.
506 s
= strchr( s
, '\t' );
508 if( !s
) return NULL
;
510 return wxStringToXmString( s
+ 1 ); // skip TAB!
514 // Change a widget's foreground and background colours.
515 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
517 if (!foregroundColour
.IsOk())
520 // When should we specify the foreground, if it's calculated
521 // by wxComputeColours?
522 // Solution: say we start with the default (computed) foreground colour.
523 // If we call SetForegroundColour explicitly for a control or window,
524 // then the foreground is changed.
525 // Therefore SetBackgroundColour computes the foreground colour, and
526 // SetForegroundColour changes the foreground colour. The ordering is
529 XtVaSetValues ((Widget
) widget
,
530 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
534 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
536 if (!backgroundColour
.IsOk())
539 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
542 XtVaSetValues ((Widget
) widget
,
543 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
544 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
545 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
546 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
550 XtVaSetValues ((Widget
) widget
,
551 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
555 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
557 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
558 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
559 Widget w
= (Widget
)widget
;
561 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
570 wxString
wxXmStringToString( const XmString
& xmString
)
573 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
580 return wxEmptyString
;
583 XmString
wxStringToXmString( const char* str
)
585 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
588 // ----------------------------------------------------------------------------
589 // wxBitmap utility functions
590 // ----------------------------------------------------------------------------
592 // Creates a bitmap with transparent areas drawn in
594 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
596 wxBitmap
newBitmap(bitmap
.GetWidth(),
602 srcDC
.SelectObjectAsSource(bitmap
);
603 destDC
.SelectObject(newBitmap
);
605 wxBrush
brush(colour
, wxSOLID
);
606 destDC
.SetBackground(brush
);
608 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
609 &srcDC
, 0, 0, wxCOPY
, true);
614 // ----------------------------------------------------------------------------
615 // Miscellaneous functions
616 // ----------------------------------------------------------------------------
618 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
620 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
622 if (style
& wxSIMPLE_BORDER
)
624 borderWidget
= XtVaCreateManagedWidget
627 xmFrameWidgetClass
, parentWidget
,
628 XmNshadowType
, XmSHADOW_ETCHED_IN
,
629 XmNshadowThickness
, 1,
633 else if ((style
& wxSUNKEN_BORDER
) || (style
& wxBORDER_THEME
))
635 borderWidget
= XtVaCreateManagedWidget
638 xmFrameWidgetClass
, parentWidget
,
639 XmNshadowType
, XmSHADOW_IN
,
643 else if (style
& wxRAISED_BORDER
)
645 borderWidget
= XtVaCreateManagedWidget
648 xmFrameWidgetClass
, parentWidget
,
649 XmNshadowType
, XmSHADOW_OUT
,