]>
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"
40 #if (defined(__SUNCC__) || defined(__CLCC__))
45 #pragma message disable nosimpint
48 #include "wx/unix/execute.h"
53 #include "wx/motif/private.h"
55 #include "X11/Xutil.h"
58 #pragma message enable nosimpint
62 // ============================================================================
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // async event processing
68 // ----------------------------------------------------------------------------
70 // Consume all events until no more left
71 void wxFlushEvents(WXDisplay
* wxdisplay
)
73 Display
*display
= (Display
*)wxdisplay
;
76 XSync (display
, False
);
78 while (evtLoop
.Pending())
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
92 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
94 wxHandleProcessTermination(proc_data
);
96 // VZ: I think they should be the same...
97 wxASSERT( (int)*id
== proc_data
->tag
);
102 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
104 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
106 (XtPointer
*) XtInputReadMask
,
107 (XtInputCallbackProc
) xt_notify_end_process
,
108 (XtPointer
) proc_data
);
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
119 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
122 // Use current setting for the bell
123 XBell (wxGlobalDisplay(), 0);
127 wxPortId
wxGUIAppTraits::GetToolkitVersion(int *verMaj
, int *verMin
) const
129 // XmVERSION and XmREVISION are defined in Xm/Xm.h
133 *verMin
= XmREVISION
;
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 void wxGetMousePosition( int* x
, int* y
)
151 XQueryPointer(wxGlobalDisplay(),
152 DefaultRootWindow(wxGlobalDisplay()),
154 &(xev
.x_root
), &(xev
.y_root
),
162 // Return true if we have a colour display
163 bool wxColourDisplay()
165 return wxDisplayDepth() > 1;
168 // Returns depth of screen
171 Display
*dpy
= wxGlobalDisplay();
173 return DefaultDepth (dpy
, DefaultScreen (dpy
));
176 // Get size of display
177 void wxDisplaySize(int *width
, int *height
)
179 Display
*dpy
= wxGlobalDisplay();
182 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
184 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
187 void wxDisplaySizeMM(int *width
, int *height
)
189 Display
*dpy
= wxGlobalDisplay();
192 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
194 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
197 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
199 // This is supposed to return desktop dimensions minus any window
200 // manager panels, menus, taskbars, etc. If there is a way to do that
201 // for this platform please fix this function, otherwise it defaults
202 // to the entire desktop.
205 wxDisplaySize(width
, height
);
209 // Configurable display in wxX11 and wxMotif
210 static WXDisplay
*gs_currentDisplay
= NULL
;
211 static wxString gs_displayName
;
213 WXDisplay
*wxGetDisplay()
215 if (gs_currentDisplay
)
216 return gs_currentDisplay
;
218 return wxTheApp
->GetInitialDisplay();
222 bool wxSetDisplay(const wxString
& display_name
)
224 gs_displayName
= display_name
;
226 if ( display_name
.empty() )
228 gs_currentDisplay
= NULL
;
236 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
237 display_name
.c_str(),
238 wxTheApp
->GetAppName().c_str(),
239 wxTheApp
->GetClassName().c_str(),
241 #if XtSpecificationRelease < 5
250 gs_currentDisplay
= (WXDisplay
*) display
;
258 wxString
wxGetDisplayName()
260 return gs_displayName
;
263 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
265 return wxGenericFindWindowAtPoint(pt
);
268 // ----------------------------------------------------------------------------
269 // Some colour manipulation routines
270 // ----------------------------------------------------------------------------
272 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
277 int r
= 0, g
= 0, b
= 0;
280 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
281 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
283 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
286 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
287 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
288 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
291 case 0: r
= v
, g
= t
, b
= p
; break;
292 case 1: r
= q
, g
= v
, b
= p
; break;
293 case 2: r
= p
, g
= v
, b
= t
; break;
294 case 3: r
= p
, g
= q
, b
= v
; break;
295 case 4: r
= t
, g
= p
, b
= v
; break;
296 case 5: r
= v
, g
= p
, b
= q
; break;
298 rgb
->red
= (unsigned short)(r
<< 8);
299 rgb
->green
= (unsigned short)(g
<< 8);
300 rgb
->blue
= (unsigned short)(b
<< 8);
303 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
305 int r
= rgb
->red
>> 8;
306 int g
= rgb
->green
>> 8;
307 int b
= rgb
->blue
>> 8;
308 int maxv
= wxMax3(r
, g
, b
);
309 int minv
= wxMin3(r
, g
, b
);
312 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
317 int rc
, gc
, bc
, hex
= 0;
318 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
319 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
320 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
321 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
322 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
323 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
324 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
328 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
329 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
332 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
337 int screen
= DefaultScreen(d
);
338 int num_colors
= DisplayCells(d
,screen
);
340 XColor
*color_defs
= new XColor
[num_colors
];
341 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
342 XQueryColors(d
,cmp
,color_defs
,num_colors
);
345 wxXColorToHSV(&hsv
,xc
);
347 int diff
, min_diff
= 0, pixel
= 0;
349 for(llp
= 0;llp
< num_colors
;llp
++)
351 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
352 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
353 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
354 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
355 if (llp
== 0) min_diff
= diff
;
356 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
357 if (min_diff
== 0) break;
360 xc
-> red
= color_defs
[pixel
].red
;
361 xc
-> green
= color_defs
[pixel
].green
;
362 xc
-> blue
= color_defs
[pixel
].blue
;
363 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
366 if (!XAllocColor(d,cmp,xc))
367 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
374 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
376 if (!XAllocColor(d
,cmp
,xc
))
378 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
379 wxAllocNearestColor(d
,cmp
,xc
);
384 wxString
wxGetXEventName(XEvent
& event
)
387 wxString
str(wxT("(some event)"));
390 int type
= event
.xany
.type
;
391 static char* event_name
[] = {
392 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
393 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
394 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
395 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
396 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
397 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
398 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
399 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
400 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
401 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
402 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
403 wxMOTIF_STR("unknown(+)")}; // 35
404 type
= wxMin(35, type
); type
= wxMax(1, type
);
405 wxString
str(event_name
[type
]);
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 // Find the letter corresponding to the mnemonic, for Motif
416 char wxFindMnemonic (const char *s
)
419 int len
= strlen (s
);
422 for (i
= 0; i
< len
; i
++)
426 // Carefully handle &&
427 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
439 char* wxFindAccelerator( const char *s
)
443 // VZ: this function returns incorrect keysym which completely breaks kbd
447 // The accelerator text is after the \t char.
448 s
= strchr( s
, '\t' );
450 if( !s
) return NULL
;
453 Now we need to format it as X standard:
458 Ctrl+N --> Ctrl<Key>N
460 Ctrl+Shift+A --> Ctrl Shift<Key>A
462 and handle Ctrl-N & similia
465 static char buf
[256];
468 wxString tmp
= s
+ 1; // skip TAB
471 while( index
< tmp
.length() )
473 size_t plus
= tmp
.find( '+', index
);
474 size_t minus
= tmp
.find( '-', index
);
476 // neither '+' nor '-', add <Key>
477 if( plus
== wxString::npos
&& minus
== wxString::npos
)
479 strcat( buf
, "<Key>" );
480 strcat( buf
, tmp
.c_str() + index
);
485 // OK: npos is big and positive
486 size_t sep
= wxMin( plus
, minus
);
487 wxString mod
= tmp
.substr( index
, sep
- index
);
498 strcat( buf
, mod
.c_str() );
507 XmString
wxFindAcceleratorText (const char *s
)
511 // VZ: this function returns incorrect keysym which completely breaks kbd
515 // The accelerator text is after the \t char.
516 s
= strchr( s
, '\t' );
518 if( !s
) return NULL
;
520 return wxStringToXmString( s
+ 1 ); // skip TAB!
524 // Change a widget's foreground and background colours.
525 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
527 if (!foregroundColour
.Ok())
530 // When should we specify the foreground, if it's calculated
531 // by wxComputeColours?
532 // Solution: say we start with the default (computed) foreground colour.
533 // If we call SetForegroundColour explicitly for a control or window,
534 // then the foreground is changed.
535 // Therefore SetBackgroundColour computes the foreground colour, and
536 // SetForegroundColour changes the foreground colour. The ordering is
539 XtVaSetValues ((Widget
) widget
,
540 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
544 void wxDoChangeBackgroundColour(WXWidget widget
, const wxColour
& backgroundColour
, bool changeArmColour
)
546 if (!backgroundColour
.Ok())
549 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
552 XtVaSetValues ((Widget
) widget
,
553 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
554 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
555 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
556 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
560 XtVaSetValues ((Widget
) widget
,
561 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
565 extern void wxDoChangeFont(WXWidget widget
, const wxFont
& font
)
567 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
568 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
569 Widget w
= (Widget
)widget
;
571 wxFont::GetFontTag(), font
.GetFontTypeC( XtDisplay(w
) ),
580 wxString
wxXmStringToString( const XmString
& xmString
)
583 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
590 return wxEmptyString
;
593 XmString
wxStringToXmString( const wxString
& str
)
595 return wxStringToXmString(str
.mb_str());
598 XmString
wxStringToXmString( const char* str
)
600 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
603 // ----------------------------------------------------------------------------
604 // wxBitmap utility functions
605 // ----------------------------------------------------------------------------
607 // Creates a bitmap with transparent areas drawn in
609 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, const wxColour
& colour
)
611 wxBitmap
newBitmap(bitmap
.GetWidth(),
617 srcDC
.SelectObjectAsSource(bitmap
);
618 destDC
.SelectObject(newBitmap
);
620 wxBrush
brush(colour
, wxSOLID
);
621 destDC
.SetBackground(brush
);
623 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
624 &srcDC
, 0, 0, wxCOPY
, true);
629 // ----------------------------------------------------------------------------
630 // Miscellaneous functions
631 // ----------------------------------------------------------------------------
633 WXWidget
wxCreateBorderWidget( WXWidget parent
, long style
)
635 Widget borderWidget
= (Widget
)NULL
, parentWidget
= (Widget
)parent
;
637 if (style
& wxSIMPLE_BORDER
)
639 borderWidget
= XtVaCreateManagedWidget
642 xmFrameWidgetClass
, parentWidget
,
643 XmNshadowType
, XmSHADOW_ETCHED_IN
,
644 XmNshadowThickness
, 1,
648 else if (style
& wxSUNKEN_BORDER
)
650 borderWidget
= XtVaCreateManagedWidget
653 xmFrameWidgetClass
, parentWidget
,
654 XmNshadowType
, XmSHADOW_IN
,
658 else if (style
& wxRAISED_BORDER
)
660 borderWidget
= XtVaCreateManagedWidget
663 xmFrameWidgetClass
, parentWidget
,
664 XmNshadowType
, XmSHADOW_OUT
,