]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utils.cpp
Applied patch [ 1777700 ] call Reparent() from wxAuiNotebook::InsertPage()
[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 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
209 {
210 // This is supposed to return desktop dimensions minus any window
211 // manager panels, menus, taskbars, etc. If there is a way to do that
212 // for this platform please fix this function, otherwise it defaults
213 // to the entire desktop.
214 if (x) *x = 0;
215 if (y) *y = 0;
216 wxDisplaySize(width, height);
217 }
218
219
220 // Configurable display in wxX11 and wxMotif
221 static WXDisplay *gs_currentDisplay = NULL;
222 static wxString gs_displayName;
223
224 WXDisplay *wxGetDisplay()
225 {
226 if (gs_currentDisplay)
227 return gs_currentDisplay;
228 else if (wxTheApp)
229 return wxTheApp->GetInitialDisplay();
230 return NULL;
231 }
232
233 bool wxSetDisplay(const wxString& display_name)
234 {
235 gs_displayName = display_name;
236
237 if ( display_name.empty() )
238 {
239 gs_currentDisplay = NULL;
240
241 return true;
242 }
243 else
244 {
245 Cardinal argc = 0;
246
247 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
248 display_name.c_str(),
249 wxTheApp->GetAppName().c_str(),
250 wxTheApp->GetClassName().c_str(),
251 NULL,
252 #if XtSpecificationRelease < 5
253 0, &argc,
254 #else
255 0, (int *)&argc,
256 #endif
257 NULL);
258
259 if (display)
260 {
261 gs_currentDisplay = (WXDisplay*) display;
262 return true;
263 }
264 else
265 return false;
266 }
267 }
268
269 wxString wxGetDisplayName()
270 {
271 return gs_displayName;
272 }
273
274 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
275 {
276 return wxGenericFindWindowAtPoint(pt);
277 }
278
279 // ----------------------------------------------------------------------------
280 // Some colour manipulation routines
281 // ----------------------------------------------------------------------------
282
283 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
284 {
285 int h = hsv->h;
286 int s = hsv->s;
287 int v = hsv->v;
288 int r = 0, g = 0, b = 0;
289 int i, f;
290 int p, q, t;
291 s = (s * wxMAX_RGB) / wxMAX_SV;
292 v = (v * wxMAX_RGB) / wxMAX_SV;
293 if (h == 360) h = 0;
294 if (s == 0) { h = 0; r = g = b = v; }
295 i = h / 60;
296 f = h % 60;
297 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
298 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
299 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
300 switch (i)
301 {
302 case 0: r = v, g = t, b = p; break;
303 case 1: r = q, g = v, b = p; break;
304 case 2: r = p, g = v, b = t; break;
305 case 3: r = p, g = q, b = v; break;
306 case 4: r = t, g = p, b = v; break;
307 case 5: r = v, g = p, b = q; break;
308 }
309 rgb->red = (unsigned short)(r << 8);
310 rgb->green = (unsigned short)(g << 8);
311 rgb->blue = (unsigned short)(b << 8);
312 }
313
314 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
315 {
316 int r = rgb->red >> 8;
317 int g = rgb->green >> 8;
318 int b = rgb->blue >> 8;
319 int maxv = wxMax3(r, g, b);
320 int minv = wxMin3(r, g, b);
321 int h = 0, s, v;
322 v = maxv;
323 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
324 else s = 0;
325 if (s == 0) h = 0;
326 else
327 {
328 int rc, gc, bc, hex = 0;
329 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
330 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
331 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
332 if (r == maxv) { h = bc - gc, hex = 0; }
333 else if (g == maxv) { h = rc - bc, hex = 2; }
334 else if (b == maxv) { h = gc - rc, hex = 4; }
335 h = hex * 60 + (h * 60 / wxMAX_RGB);
336 if (h < 0) h += 360;
337 }
338 hsv->h = h;
339 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
340 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
341 }
342
343 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
344 {
345 #if !wxUSE_NANOX
346 int llp;
347
348 int screen = DefaultScreen(d);
349 int num_colors = DisplayCells(d,screen);
350
351 XColor *color_defs = new XColor[num_colors];
352 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
353 XQueryColors(d,cmp,color_defs,num_colors);
354
355 wxHSV hsv_defs, hsv;
356 wxXColorToHSV(&hsv,xc);
357
358 int diff, min_diff = 0, pixel = 0;
359
360 for(llp = 0;llp < num_colors;llp++)
361 {
362 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
363 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
364 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
365 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
366 if (llp == 0) min_diff = diff;
367 if (min_diff > diff) { min_diff = diff; pixel = llp; }
368 if (min_diff == 0) break;
369 }
370
371 xc -> red = color_defs[pixel].red;
372 xc -> green = color_defs[pixel].green;
373 xc -> blue = color_defs[pixel].blue;
374 xc -> flags = DoRed | DoGreen | DoBlue;
375
376 /* FIXME, TODO
377 if (!XAllocColor(d,cmp,xc))
378 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
379 */
380
381 delete[] color_defs;
382 #endif
383 }
384
385 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
386 {
387 if (!XAllocColor(d,cmp,xc))
388 {
389 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
390 wxAllocNearestColor(d,cmp,xc);
391 }
392 }
393
394 #ifdef __WXDEBUG__
395 wxString wxGetXEventName(XEvent& event)
396 {
397 #if wxUSE_NANOX
398 wxString str(wxT("(some event)"));
399 return str;
400 #else
401 int type = event.xany.type;
402 static char* event_name[] = {
403 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
404 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
405 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
406 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
407 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
408 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
409 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
410 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
411 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
412 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
413 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
414 wxMOTIF_STR("unknown(+)")}; // 35
415 type = wxMin(35, type); type = wxMax(1, type);
416 wxString str(event_name[type]);
417 return str;
418 #endif
419 }
420 #endif
421
422 // ----------------------------------------------------------------------------
423 // accelerators
424 // ----------------------------------------------------------------------------
425
426 // Find the letter corresponding to the mnemonic, for Motif
427 char wxFindMnemonic (const char *s)
428 {
429 char mnem = 0;
430 int len = strlen (s);
431 int i;
432
433 for (i = 0; i < len; i++)
434 {
435 if (s[i] == '&')
436 {
437 // Carefully handle &&
438 if ((i + 1) <= len && s[i + 1] == '&')
439 i++;
440 else
441 {
442 mnem = s[i + 1];
443 break;
444 }
445 }
446 }
447 return mnem;
448 }
449
450 char* wxFindAccelerator( const char *s )
451 {
452 #if 1
453 wxUnusedVar(s);
454 // VZ: this function returns incorrect keysym which completely breaks kbd
455 // handling
456 return NULL;
457 #else
458 // The accelerator text is after the \t char.
459 s = strchr( s, '\t' );
460
461 if( !s ) return NULL;
462
463 /*
464 Now we need to format it as X standard:
465
466 input output
467
468 F7 --> <Key>F7
469 Ctrl+N --> Ctrl<Key>N
470 Alt+k --> Meta<Key>k
471 Ctrl+Shift+A --> Ctrl Shift<Key>A
472
473 and handle Ctrl-N & similia
474 */
475
476 static char buf[256];
477
478 buf[0] = '\0';
479 wxString tmp = s + 1; // skip TAB
480 size_t index = 0;
481
482 while( index < tmp.length() )
483 {
484 size_t plus = tmp.find( '+', index );
485 size_t minus = tmp.find( '-', index );
486
487 // neither '+' nor '-', add <Key>
488 if( plus == wxString::npos && minus == wxString::npos )
489 {
490 strcat( buf, "<Key>" );
491 strcat( buf, tmp.c_str() + index );
492
493 return buf;
494 }
495
496 // OK: npos is big and positive
497 size_t sep = wxMin( plus, minus );
498 wxString mod = tmp.substr( index, sep - index );
499
500 // Ctrl -> Ctrl
501 // Shift -> Shift
502 // Alt -> Meta
503 if( mod == "Alt" )
504 mod = "Meta";
505
506 if( buf[0] )
507 strcat( buf, " " );
508
509 strcat( buf, mod.c_str() );
510
511 index = sep + 1;
512 }
513
514 return NULL;
515 #endif
516 }
517
518 XmString wxFindAcceleratorText (const char *s)
519 {
520 #if 1
521 wxUnusedVar(s);
522 // VZ: this function returns incorrect keysym which completely breaks kbd
523 // handling
524 return NULL;
525 #else
526 // The accelerator text is after the \t char.
527 s = strchr( s, '\t' );
528
529 if( !s ) return NULL;
530
531 return wxStringToXmString( s + 1 ); // skip TAB!
532 #endif
533 }
534
535 // Change a widget's foreground and background colours.
536 void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
537 {
538 if (!foregroundColour.Ok())
539 return;
540
541 // When should we specify the foreground, if it's calculated
542 // by wxComputeColours?
543 // Solution: say we start with the default (computed) foreground colour.
544 // If we call SetForegroundColour explicitly for a control or window,
545 // then the foreground is changed.
546 // Therefore SetBackgroundColour computes the foreground colour, and
547 // SetForegroundColour changes the foreground colour. The ordering is
548 // important.
549
550 XtVaSetValues ((Widget) widget,
551 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
552 NULL);
553 }
554
555 void wxDoChangeBackgroundColour(WXWidget widget, const wxColour& backgroundColour, bool changeArmColour)
556 {
557 if (!backgroundColour.Ok())
558 return;
559
560 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
561 (wxColour*) NULL);
562
563 XtVaSetValues ((Widget) widget,
564 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
565 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
566 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
567 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
568 NULL);
569
570 if (changeArmColour)
571 XtVaSetValues ((Widget) widget,
572 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
573 NULL);
574 }
575
576 extern void wxDoChangeFont(WXWidget widget, const wxFont& font)
577 {
578 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
579 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
580 Widget w = (Widget)widget;
581 XtVaSetValues( w,
582 wxFont::GetFontTag(), font.GetFontTypeC( XtDisplay(w) ),
583 NULL );
584 #else
585 wxUnusedVar(widget);
586 wxUnusedVar(font);
587 #endif
588
589 }
590
591 wxString wxXmStringToString( const XmString& xmString )
592 {
593 char *txt;
594 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
595 {
596 wxString str(txt);
597 XtFree (txt);
598 return str;
599 }
600
601 return wxEmptyString;
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 }