]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utils.cpp
rebaked after timer/socket changes
[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 wxEventLoopBase* wxGUIAppTraits::CreateEventLoop()
140 {
141 return new wxEventLoop;
142 }
143
144 wxTimerImpl* wxGUIAppTraits::CreateTimerImpl(wxTimer* timer)
145 {
146 return new wxMotifTimerImpl(timer);
147 }
148
149 // ----------------------------------------------------------------------------
150 // display info
151 // ----------------------------------------------------------------------------
152
153 void wxGetMousePosition( int* x, int* y )
154 {
155 #if wxUSE_NANOX
156 // TODO
157 *x = 0;
158 *y = 0;
159 #else
160 XMotionEvent xev;
161 Window root, child;
162 XQueryPointer(wxGlobalDisplay(),
163 DefaultRootWindow(wxGlobalDisplay()),
164 &root, &child,
165 &(xev.x_root), &(xev.y_root),
166 &(xev.x), &(xev.y),
167 &(xev.state));
168 *x = xev.x_root;
169 *y = xev.y_root;
170 #endif
171 }
172
173 // Return true if we have a colour display
174 bool wxColourDisplay()
175 {
176 return wxDisplayDepth() > 1;
177 }
178
179 // Returns depth of screen
180 int wxDisplayDepth()
181 {
182 Display *dpy = wxGlobalDisplay();
183
184 return DefaultDepth (dpy, DefaultScreen (dpy));
185 }
186
187 // Get size of display
188 void wxDisplaySize(int *width, int *height)
189 {
190 Display *dpy = wxGlobalDisplay();
191
192 if ( width )
193 *width = DisplayWidth (dpy, DefaultScreen (dpy));
194 if ( height )
195 *height = DisplayHeight (dpy, DefaultScreen (dpy));
196 }
197
198 void wxDisplaySizeMM(int *width, int *height)
199 {
200 Display *dpy = wxGlobalDisplay();
201
202 if ( width )
203 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
204 if ( height )
205 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
206 }
207
208 // Configurable display in wxX11 and wxMotif
209 static WXDisplay *gs_currentDisplay = NULL;
210 static wxString gs_displayName;
211
212 WXDisplay *wxGetDisplay()
213 {
214 if (gs_currentDisplay)
215 return gs_currentDisplay;
216 else if (wxTheApp)
217 return wxTheApp->GetInitialDisplay();
218 return NULL;
219 }
220
221 bool wxSetDisplay(const wxString& display_name)
222 {
223 gs_displayName = display_name;
224
225 if ( display_name.empty() )
226 {
227 gs_currentDisplay = NULL;
228
229 return true;
230 }
231 else
232 {
233 Cardinal argc = 0;
234
235 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
236 display_name.c_str(),
237 wxTheApp->GetAppName().c_str(),
238 wxTheApp->GetClassName().c_str(),
239 NULL,
240 #if XtSpecificationRelease < 5
241 0, &argc,
242 #else
243 0, (int *)&argc,
244 #endif
245 NULL);
246
247 if (display)
248 {
249 gs_currentDisplay = (WXDisplay*) display;
250 return true;
251 }
252 else
253 return false;
254 }
255 }
256
257 wxString wxGetDisplayName()
258 {
259 return gs_displayName;
260 }
261
262 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
263 {
264 return wxGenericFindWindowAtPoint(pt);
265 }
266
267 // ----------------------------------------------------------------------------
268 // Some colour manipulation routines
269 // ----------------------------------------------------------------------------
270
271 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
272 {
273 int h = hsv->h;
274 int s = hsv->s;
275 int v = hsv->v;
276 int r = 0, g = 0, b = 0;
277 int i, f;
278 int p, q, t;
279 s = (s * wxMAX_RGB) / wxMAX_SV;
280 v = (v * wxMAX_RGB) / wxMAX_SV;
281 if (h == 360) h = 0;
282 if (s == 0) { h = 0; r = g = b = v; }
283 i = h / 60;
284 f = h % 60;
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;
288 switch (i)
289 {
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;
296 }
297 rgb->red = (unsigned short)(r << 8);
298 rgb->green = (unsigned short)(g << 8);
299 rgb->blue = (unsigned short)(b << 8);
300 }
301
302 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
303 {
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);
309 int h = 0, s, v;
310 v = maxv;
311 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
312 else s = 0;
313 if (s == 0) h = 0;
314 else
315 {
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);
324 if (h < 0) h += 360;
325 }
326 hsv->h = h;
327 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
328 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
329 }
330
331 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
332 {
333 #if !wxUSE_NANOX
334 int llp;
335
336 int screen = DefaultScreen(d);
337 int num_colors = DisplayCells(d,screen);
338
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);
342
343 wxHSV hsv_defs, hsv;
344 wxXColorToHSV(&hsv,xc);
345
346 int diff, min_diff = 0, pixel = 0;
347
348 for(llp = 0;llp < num_colors;llp++)
349 {
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;
357 }
358
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;
363
364 /* FIXME, TODO
365 if (!XAllocColor(d,cmp,xc))
366 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
367 */
368
369 delete[] color_defs;
370 #endif
371 }
372
373 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
374 {
375 if (!XAllocColor(d,cmp,xc))
376 {
377 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
378 wxAllocNearestColor(d,cmp,xc);
379 }
380 }
381
382 #ifdef __WXDEBUG__
383 wxString wxGetXEventName(XEvent& event)
384 {
385 #if wxUSE_NANOX
386 wxString str(wxT("(some event)"));
387 return str;
388 #else
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]);
405 return str;
406 #endif
407 }
408 #endif
409
410 // ----------------------------------------------------------------------------
411 // accelerators
412 // ----------------------------------------------------------------------------
413
414 // Find the letter corresponding to the mnemonic, for Motif
415 char wxFindMnemonic (const char *s)
416 {
417 char mnem = 0;
418 int len = strlen (s);
419 int i;
420
421 for (i = 0; i < len; i++)
422 {
423 if (s[i] == '&')
424 {
425 // Carefully handle &&
426 if ((i + 1) <= len && s[i + 1] == '&')
427 i++;
428 else
429 {
430 mnem = s[i + 1];
431 break;
432 }
433 }
434 }
435 return mnem;
436 }
437
438 char* wxFindAccelerator( const char *s )
439 {
440 #if 1
441 wxUnusedVar(s);
442 // VZ: this function returns incorrect keysym which completely breaks kbd
443 // handling
444 return NULL;
445 #else
446 // The accelerator text is after the \t char.
447 s = strchr( s, '\t' );
448
449 if( !s ) return NULL;
450
451 /*
452 Now we need to format it as X standard:
453
454 input output
455
456 F7 --> <Key>F7
457 Ctrl+N --> Ctrl<Key>N
458 Alt+k --> Meta<Key>k
459 Ctrl+Shift+A --> Ctrl Shift<Key>A
460
461 and handle Ctrl-N & similia
462 */
463
464 static char buf[256];
465
466 buf[0] = '\0';
467 wxString tmp = s + 1; // skip TAB
468 size_t index = 0;
469
470 while( index < tmp.length() )
471 {
472 size_t plus = tmp.find( '+', index );
473 size_t minus = tmp.find( '-', index );
474
475 // neither '+' nor '-', add <Key>
476 if( plus == wxString::npos && minus == wxString::npos )
477 {
478 strcat( buf, "<Key>" );
479 strcat( buf, tmp.c_str() + index );
480
481 return buf;
482 }
483
484 // OK: npos is big and positive
485 size_t sep = wxMin( plus, minus );
486 wxString mod = tmp.substr( index, sep - index );
487
488 // Ctrl -> Ctrl
489 // Shift -> Shift
490 // Alt -> Meta
491 if( mod == "Alt" )
492 mod = "Meta";
493
494 if( buf[0] )
495 strcat( buf, " " );
496
497 strcat( buf, mod.c_str() );
498
499 index = sep + 1;
500 }
501
502 return NULL;
503 #endif
504 }
505
506 XmString wxFindAcceleratorText (const char *s)
507 {
508 #if 1
509 wxUnusedVar(s);
510 // VZ: this function returns incorrect keysym which completely breaks kbd
511 // handling
512 return NULL;
513 #else
514 // The accelerator text is after the \t char.
515 s = strchr( s, '\t' );
516
517 if( !s ) return NULL;
518
519 return wxStringToXmString( s + 1 ); // skip TAB!
520 #endif
521 }
522
523 // Change a widget's foreground and background colours.
524 void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
525 {
526 if (!foregroundColour.Ok())
527 return;
528
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
536 // important.
537
538 XtVaSetValues ((Widget) widget,
539 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
540 NULL);
541 }
542
543 void wxDoChangeBackgroundColour(WXWidget widget, const wxColour& backgroundColour, bool changeArmColour)
544 {
545 if (!backgroundColour.Ok())
546 return;
547
548 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
549 (wxColour*) NULL);
550
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,
556 NULL);
557
558 if (changeArmColour)
559 XtVaSetValues ((Widget) widget,
560 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
561 NULL);
562 }
563
564 extern void wxDoChangeFont(WXWidget widget, const wxFont& font)
565 {
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;
569 XtVaSetValues( w,
570 wxFont::GetFontTag(), font.GetFontTypeC( XtDisplay(w) ),
571 NULL );
572 #else
573 wxUnusedVar(widget);
574 wxUnusedVar(font);
575 #endif
576
577 }
578
579 wxString wxXmStringToString( const XmString& xmString )
580 {
581 char *txt;
582 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
583 {
584 wxString str(txt);
585 XtFree (txt);
586 return str;
587 }
588
589 return wxEmptyString;
590 }
591
592 XmString wxStringToXmString( const char* str )
593 {
594 return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET);
595 }
596
597 // ----------------------------------------------------------------------------
598 // wxBitmap utility functions
599 // ----------------------------------------------------------------------------
600
601 // Creates a bitmap with transparent areas drawn in
602 // the given colour.
603 wxBitmap wxCreateMaskedBitmap(const wxBitmap& bitmap, const wxColour& colour)
604 {
605 wxBitmap newBitmap(bitmap.GetWidth(),
606 bitmap.GetHeight(),
607 bitmap.GetDepth());
608 wxMemoryDC destDC;
609 wxMemoryDC srcDC;
610
611 srcDC.SelectObjectAsSource(bitmap);
612 destDC.SelectObject(newBitmap);
613
614 wxBrush brush(colour, wxSOLID);
615 destDC.SetBackground(brush);
616 destDC.Clear();
617 destDC.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(),
618 &srcDC, 0, 0, wxCOPY, true);
619
620 return newBitmap;
621 }
622
623 // ----------------------------------------------------------------------------
624 // Miscellaneous functions
625 // ----------------------------------------------------------------------------
626
627 WXWidget wxCreateBorderWidget( WXWidget parent, long style )
628 {
629 Widget borderWidget = (Widget)NULL, parentWidget = (Widget)parent;
630
631 if (style & wxSIMPLE_BORDER)
632 {
633 borderWidget = XtVaCreateManagedWidget
634 (
635 "simpleBorder",
636 xmFrameWidgetClass, parentWidget,
637 XmNshadowType, XmSHADOW_ETCHED_IN,
638 XmNshadowThickness, 1,
639 NULL
640 );
641 }
642 else if ((style & wxSUNKEN_BORDER) || (style & wxBORDER_THEME))
643 {
644 borderWidget = XtVaCreateManagedWidget
645 (
646 "sunkenBorder",
647 xmFrameWidgetClass, parentWidget,
648 XmNshadowType, XmSHADOW_IN,
649 NULL
650 );
651 }
652 else if (style & wxRAISED_BORDER)
653 {
654 borderWidget = XtVaCreateManagedWidget
655 (
656 "raisedBorder",
657 xmFrameWidgetClass, parentWidget,
658 XmNshadowType, XmSHADOW_OUT,
659 NULL
660 );
661 }
662
663 return borderWidget;
664 }