]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utils.cpp
OS/2 compilation fixes
[wxWidgets.git] / src / motif / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/utils.cpp
3 // Purpose: Various utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __VMS
24 #define XtDisplay XTDISPLAY
25 #endif
26
27 #include "wx/utils.h"
28
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/dcmemory.h"
32 #include "wx/bitmap.h"
33 #endif
34
35 #include "wx/apptrait.h"
36 #include "wx/evtloop.h"
37 #include "wx/motif/private/timer.h"
38
39 #include <string.h>
40
41 #if (defined(__SUNCC__) || defined(__CLCC__))
42 #include <sysent.h>
43 #endif
44
45 #ifdef __VMS__
46 #pragma message disable nosimpint
47 #endif
48
49 #include "wx/unix/execute.h"
50
51 #include <Xm/Xm.h>
52 #include <Xm/Frame.h>
53
54 #include "wx/motif/private.h"
55
56 #include "X11/Xutil.h"
57
58 #ifdef __VMS__
59 #pragma message enable nosimpint
60 #endif
61
62
63 // ============================================================================
64 // implementation
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // async event processing
69 // ----------------------------------------------------------------------------
70
71 // Consume all events until no more left
72 void wxFlushEvents(WXDisplay* wxdisplay)
73 {
74 Display *display = (Display*)wxdisplay;
75 wxEventLoop evtLoop;
76
77 XSync (display, False);
78
79 while (evtLoop.Pending())
80 {
81 XFlush (display);
82 evtLoop.Dispatch();
83 }
84 }
85
86 // ----------------------------------------------------------------------------
87 // wxExecute stuff
88 // ----------------------------------------------------------------------------
89
90 static void xt_notify_end_process(XtPointer data, int *WXUNUSED(fid),
91 XtInputId *id)
92 {
93 wxEndProcessData *proc_data = (wxEndProcessData *)data;
94
95 wxHandleProcessTermination(proc_data);
96
97 // VZ: I think they should be the same...
98 wxASSERT( (int)*id == proc_data->tag );
99
100 XtRemoveInput(*id);
101 }
102
103 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
104 {
105 XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
106 fd,
107 (XtPointer *) XtInputReadMask,
108 (XtInputCallbackProc) xt_notify_end_process,
109 (XtPointer) proc_data);
110
111 return (int)id;
112 }
113
114 // ----------------------------------------------------------------------------
115 // misc
116 // ----------------------------------------------------------------------------
117
118 // Emit a beeeeeep
119 #ifndef __EMX__
120 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
121 void wxBell()
122 {
123 // Use current setting for the bell
124 XBell (wxGlobalDisplay(), 0);
125 }
126 #endif
127
128 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
129 {
130 // XmVERSION and XmREVISION are defined in Xm/Xm.h
131 if ( verMaj )
132 *verMaj = XmVERSION;
133 if ( verMin )
134 *verMin = XmREVISION;
135
136 return wxPORT_MOTIF;
137 }
138
139 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer* timer)
140 {
141 return new wxMotifTimerImpl(timer);
142 }
143
144 // ----------------------------------------------------------------------------
145 // display info
146 // ----------------------------------------------------------------------------
147
148 void wxGetMousePosition( int* x, int* y )
149 {
150 #if wxUSE_NANOX
151 // TODO
152 *x = 0;
153 *y = 0;
154 #else
155 XMotionEvent xev;
156 Window root, child;
157 XQueryPointer(wxGlobalDisplay(),
158 DefaultRootWindow(wxGlobalDisplay()),
159 &root, &child,
160 &(xev.x_root), &(xev.y_root),
161 &(xev.x), &(xev.y),
162 &(xev.state));
163 *x = xev.x_root;
164 *y = xev.y_root;
165 #endif
166 }
167
168 // Return true if we have a colour display
169 bool wxColourDisplay()
170 {
171 return wxDisplayDepth() > 1;
172 }
173
174 // Returns depth of screen
175 int wxDisplayDepth()
176 {
177 Display *dpy = wxGlobalDisplay();
178
179 return DefaultDepth (dpy, DefaultScreen (dpy));
180 }
181
182 // Get size of display
183 void wxDisplaySize(int *width, int *height)
184 {
185 Display *dpy = wxGlobalDisplay();
186
187 if ( width )
188 *width = DisplayWidth (dpy, DefaultScreen (dpy));
189 if ( height )
190 *height = DisplayHeight (dpy, DefaultScreen (dpy));
191 }
192
193 void wxDisplaySizeMM(int *width, int *height)
194 {
195 Display *dpy = wxGlobalDisplay();
196
197 if ( width )
198 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
199 if ( height )
200 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
201 }
202
203 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
204 {
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.
209 if (x) *x = 0;
210 if (y) *y = 0;
211 wxDisplaySize(width, height);
212 }
213
214
215 // Configurable display in wxX11 and wxMotif
216 static WXDisplay *gs_currentDisplay = NULL;
217 static wxString gs_displayName;
218
219 WXDisplay *wxGetDisplay()
220 {
221 if (gs_currentDisplay)
222 return gs_currentDisplay;
223 else if (wxTheApp)
224 return wxTheApp->GetInitialDisplay();
225 return NULL;
226 }
227
228 bool wxSetDisplay(const wxString& display_name)
229 {
230 gs_displayName = display_name;
231
232 if ( display_name.empty() )
233 {
234 gs_currentDisplay = NULL;
235
236 return true;
237 }
238 else
239 {
240 Cardinal argc = 0;
241
242 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
243 display_name.c_str(),
244 wxTheApp->GetAppName().c_str(),
245 wxTheApp->GetClassName().c_str(),
246 NULL,
247 #if XtSpecificationRelease < 5
248 0, &argc,
249 #else
250 0, (int *)&argc,
251 #endif
252 NULL);
253
254 if (display)
255 {
256 gs_currentDisplay = (WXDisplay*) display;
257 return true;
258 }
259 else
260 return false;
261 }
262 }
263
264 wxString wxGetDisplayName()
265 {
266 return gs_displayName;
267 }
268
269 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
270 {
271 return wxGenericFindWindowAtPoint(pt);
272 }
273
274 // ----------------------------------------------------------------------------
275 // Some colour manipulation routines
276 // ----------------------------------------------------------------------------
277
278 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
279 {
280 int h = hsv->h;
281 int s = hsv->s;
282 int v = hsv->v;
283 int r = 0, g = 0, b = 0;
284 int i, f;
285 int p, q, t;
286 s = (s * wxMAX_RGB) / wxMAX_SV;
287 v = (v * wxMAX_RGB) / wxMAX_SV;
288 if (h == 360) h = 0;
289 if (s == 0) { h = 0; r = g = b = v; }
290 i = h / 60;
291 f = h % 60;
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;
295 switch (i)
296 {
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;
303 }
304 rgb->red = (unsigned short)(r << 8);
305 rgb->green = (unsigned short)(g << 8);
306 rgb->blue = (unsigned short)(b << 8);
307 }
308
309 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
310 {
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);
316 int h = 0, s, v;
317 v = maxv;
318 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
319 else s = 0;
320 if (s == 0) h = 0;
321 else
322 {
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);
331 if (h < 0) h += 360;
332 }
333 hsv->h = h;
334 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
335 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
336 }
337
338 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
339 {
340 #if !wxUSE_NANOX
341 int llp;
342
343 int screen = DefaultScreen(d);
344 int num_colors = DisplayCells(d,screen);
345
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);
349
350 wxHSV hsv_defs, hsv;
351 wxXColorToHSV(&hsv,xc);
352
353 int diff, min_diff = 0, pixel = 0;
354
355 for(llp = 0;llp < num_colors;llp++)
356 {
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;
364 }
365
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;
370
371 /* FIXME, TODO
372 if (!XAllocColor(d,cmp,xc))
373 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
374 */
375
376 delete[] color_defs;
377 #endif
378 }
379
380 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
381 {
382 if (!XAllocColor(d,cmp,xc))
383 {
384 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
385 wxAllocNearestColor(d,cmp,xc);
386 }
387 }
388
389 #ifdef __WXDEBUG__
390 wxString wxGetXEventName(XEvent& event)
391 {
392 #if wxUSE_NANOX
393 wxString str(wxT("(some event)"));
394 return str;
395 #else
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]);
412 return str;
413 #endif
414 }
415 #endif
416
417 // ----------------------------------------------------------------------------
418 // accelerators
419 // ----------------------------------------------------------------------------
420
421 // Find the letter corresponding to the mnemonic, for Motif
422 char wxFindMnemonic (const char *s)
423 {
424 char mnem = 0;
425 int len = strlen (s);
426 int i;
427
428 for (i = 0; i < len; i++)
429 {
430 if (s[i] == '&')
431 {
432 // Carefully handle &&
433 if ((i + 1) <= len && s[i + 1] == '&')
434 i++;
435 else
436 {
437 mnem = s[i + 1];
438 break;
439 }
440 }
441 }
442 return mnem;
443 }
444
445 char* wxFindAccelerator( const char *s )
446 {
447 #if 1
448 wxUnusedVar(s);
449 // VZ: this function returns incorrect keysym which completely breaks kbd
450 // handling
451 return NULL;
452 #else
453 // The accelerator text is after the \t char.
454 s = strchr( s, '\t' );
455
456 if( !s ) return NULL;
457
458 /*
459 Now we need to format it as X standard:
460
461 input output
462
463 F7 --> <Key>F7
464 Ctrl+N --> Ctrl<Key>N
465 Alt+k --> Meta<Key>k
466 Ctrl+Shift+A --> Ctrl Shift<Key>A
467
468 and handle Ctrl-N & similia
469 */
470
471 static char buf[256];
472
473 buf[0] = '\0';
474 wxString tmp = s + 1; // skip TAB
475 size_t index = 0;
476
477 while( index < tmp.length() )
478 {
479 size_t plus = tmp.find( '+', index );
480 size_t minus = tmp.find( '-', index );
481
482 // neither '+' nor '-', add <Key>
483 if( plus == wxString::npos && minus == wxString::npos )
484 {
485 strcat( buf, "<Key>" );
486 strcat( buf, tmp.c_str() + index );
487
488 return buf;
489 }
490
491 // OK: npos is big and positive
492 size_t sep = wxMin( plus, minus );
493 wxString mod = tmp.substr( index, sep - index );
494
495 // Ctrl -> Ctrl
496 // Shift -> Shift
497 // Alt -> Meta
498 if( mod == "Alt" )
499 mod = "Meta";
500
501 if( buf[0] )
502 strcat( buf, " " );
503
504 strcat( buf, mod.c_str() );
505
506 index = sep + 1;
507 }
508
509 return NULL;
510 #endif
511 }
512
513 XmString wxFindAcceleratorText (const char *s)
514 {
515 #if 1
516 wxUnusedVar(s);
517 // VZ: this function returns incorrect keysym which completely breaks kbd
518 // handling
519 return NULL;
520 #else
521 // The accelerator text is after the \t char.
522 s = strchr( s, '\t' );
523
524 if( !s ) return NULL;
525
526 return wxStringToXmString( s + 1 ); // skip TAB!
527 #endif
528 }
529
530 // Change a widget's foreground and background colours.
531 void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
532 {
533 if (!foregroundColour.Ok())
534 return;
535
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
543 // important.
544
545 XtVaSetValues ((Widget) widget,
546 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
547 NULL);
548 }
549
550 void wxDoChangeBackgroundColour(WXWidget widget, const wxColour& backgroundColour, bool changeArmColour)
551 {
552 if (!backgroundColour.Ok())
553 return;
554
555 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
556 (wxColour*) NULL);
557
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,
563 NULL);
564
565 if (changeArmColour)
566 XtVaSetValues ((Widget) widget,
567 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
568 NULL);
569 }
570
571 extern void wxDoChangeFont(WXWidget widget, const wxFont& font)
572 {
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;
576 XtVaSetValues( w,
577 wxFont::GetFontTag(), font.GetFontTypeC( XtDisplay(w) ),
578 NULL );
579 #else
580 wxUnusedVar(widget);
581 wxUnusedVar(font);
582 #endif
583
584 }
585
586 wxString wxXmStringToString( const XmString& xmString )
587 {
588 char *txt;
589 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
590 {
591 wxString str(txt);
592 XtFree (txt);
593 return str;
594 }
595
596 return wxEmptyString;
597 }
598
599 XmString wxStringToXmString( const wxString& str )
600 {
601 return wxStringToXmString(str.mb_str());
602 }
603
604 XmString wxStringToXmString( const char* str )
605 {
606 return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET);
607 }
608
609 // ----------------------------------------------------------------------------
610 // wxBitmap utility functions
611 // ----------------------------------------------------------------------------
612
613 // Creates a bitmap with transparent areas drawn in
614 // the given colour.
615 wxBitmap wxCreateMaskedBitmap(const wxBitmap& bitmap, const wxColour& colour)
616 {
617 wxBitmap newBitmap(bitmap.GetWidth(),
618 bitmap.GetHeight(),
619 bitmap.GetDepth());
620 wxMemoryDC destDC;
621 wxMemoryDC srcDC;
622
623 srcDC.SelectObjectAsSource(bitmap);
624 destDC.SelectObject(newBitmap);
625
626 wxBrush brush(colour, wxSOLID);
627 destDC.SetBackground(brush);
628 destDC.Clear();
629 destDC.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(),
630 &srcDC, 0, 0, wxCOPY, true);
631
632 return newBitmap;
633 }
634
635 // ----------------------------------------------------------------------------
636 // Miscellaneous functions
637 // ----------------------------------------------------------------------------
638
639 WXWidget wxCreateBorderWidget( WXWidget parent, long style )
640 {
641 Widget borderWidget = (Widget)NULL, parentWidget = (Widget)parent;
642
643 if (style & wxSIMPLE_BORDER)
644 {
645 borderWidget = XtVaCreateManagedWidget
646 (
647 "simpleBorder",
648 xmFrameWidgetClass, parentWidget,
649 XmNshadowType, XmSHADOW_ETCHED_IN,
650 XmNshadowThickness, 1,
651 NULL
652 );
653 }
654 else if (style & wxSUNKEN_BORDER)
655 {
656 borderWidget = XtVaCreateManagedWidget
657 (
658 "sunkenBorder",
659 xmFrameWidgetClass, parentWidget,
660 XmNshadowType, XmSHADOW_IN,
661 NULL
662 );
663 }
664 else if (style & wxRAISED_BORDER)
665 {
666 borderWidget = XtVaCreateManagedWidget
667 (
668 "raisedBorder",
669 xmFrameWidgetClass, parentWidget,
670 XmNshadowType, XmSHADOW_OUT,
671 NULL
672 );
673 }
674
675 return borderWidget;
676 }