]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utils.cpp
Fixes for semicolons, commas and wxSTRING_MAXLEN from Wlodek Szafran.
[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
38 #include <string.h>
39
40 #if (defined(__SUNCC__) || defined(__CLCC__))
41 #include <sysent.h>
42 #endif
43
44 #ifdef __VMS__
45 #pragma message disable nosimpint
46 #endif
47
48 #include "wx/unix/execute.h"
49
50 #include <Xm/Xm.h>
51 #include <Xm/Frame.h>
52
53 #include "wx/motif/private.h"
54
55 #if wxUSE_RESOURCES
56 #include "X11/Xresource.h"
57 #endif
58
59 #include "X11/Xutil.h"
60
61 #ifdef __VMS__
62 #pragma message enable nosimpint
63 #endif
64
65 // ----------------------------------------------------------------------------
66 // private functions
67 // ----------------------------------------------------------------------------
68
69 // Yuck this is really BOTH site and platform dependent
70 // so we should use some other strategy!
71 #ifdef sun
72 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
73 #else
74 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
75 #endif
76
77 #if wxUSE_RESOURCES
78 static char *GetIniFile (char *dest, const char *filename);
79 #endif
80
81 // ============================================================================
82 // implementation
83 // ============================================================================
84
85 // ----------------------------------------------------------------------------
86 // async event processing
87 // ----------------------------------------------------------------------------
88
89 // Consume all events until no more left
90 void wxFlushEvents(WXDisplay* wxdisplay)
91 {
92 Display *display = (Display*)wxdisplay;
93 wxEventLoop evtLoop;
94
95 XSync (display, False);
96
97 while (evtLoop.Pending())
98 {
99 XFlush (display);
100 evtLoop.Dispatch();
101 }
102 }
103
104 // ----------------------------------------------------------------------------
105 // wxExecute stuff
106 // ----------------------------------------------------------------------------
107
108 static void xt_notify_end_process(XtPointer data, int *WXUNUSED(fid),
109 XtInputId *id)
110 {
111 wxEndProcessData *proc_data = (wxEndProcessData *)data;
112
113 wxHandleProcessTermination(proc_data);
114
115 // VZ: I think they should be the same...
116 wxASSERT( (int)*id == proc_data->tag );
117
118 XtRemoveInput(*id);
119 }
120
121 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
122 {
123 XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
124 fd,
125 (XtPointer *) XtInputReadMask,
126 (XtInputCallbackProc) xt_notify_end_process,
127 (XtPointer) proc_data);
128
129 return (int)id;
130 }
131
132 // ----------------------------------------------------------------------------
133 // misc
134 // ----------------------------------------------------------------------------
135
136 // Emit a beeeeeep
137 #ifndef __EMX__
138 // on OS/2, we use the wxBell from wxBase library (src/os2/utils.cpp)
139 void wxBell()
140 {
141 // Use current setting for the bell
142 XBell (wxGlobalDisplay(), 0);
143 }
144 #endif
145
146 wxPortId wxGUIAppTraits::GetToolkitVersion(int *verMaj, int *verMin) const
147 {
148 // XmVERSION and XmREVISION are defined in Xm/Xm.h
149 if ( verMaj )
150 *verMaj = XmVERSION;
151 if ( verMin )
152 *verMin = XmREVISION;
153
154 return wxPORT_MOTIF;
155 }
156
157
158 // ----------------------------------------------------------------------------
159 // Reading and writing resources (eg WIN.INI, .Xdefaults)
160 // ----------------------------------------------------------------------------
161
162 #if wxUSE_RESOURCES
163
164 // Read $HOME for what it says is home, if not
165 // read $USER or $LOGNAME for user name else determine
166 // the Real User, then determine the Real home dir.
167 static char * GetIniFile (char *dest, const char *filename)
168 {
169 char *home = NULL;
170 if (filename && wxIsAbsolutePath(filename))
171 {
172 strcpy(dest, filename);
173 }
174 else if ((home = wxGetUserHome()) != NULL)
175 {
176 strcpy(dest, home);
177 if (dest[strlen(dest) - 1] != '/')
178 strcat (dest, "/");
179 if (filename == NULL)
180 {
181 if ((filename = getenv ("XENVIRONMENT")) == NULL)
182 filename = ".Xdefaults";
183 }
184 else if (*filename != '.')
185 strcat (dest, ".");
186 strcat (dest, filename);
187 } else
188 {
189 dest[0] = '\0';
190 }
191 return dest;
192 }
193
194 static char *GetResourcePath(char *buf, const char *name, bool create = false)
195 {
196 if (create && wxFileExists (name) ) {
197 strcpy(buf, name);
198 return buf; // Exists so ...
199 }
200
201 if (*name == '/')
202 strcpy(buf, name);
203 else {
204 // Put in standard place for resource files if not absolute
205 strcpy (buf, DEFAULT_XRESOURCE_DIR);
206 strcat (buf, "/");
207 strcat (buf, wxFileNameFromPath (name).c_str());
208 }
209
210 if (create) {
211 // Touch the file to create it
212 FILE *fd = fopen (buf, "w");
213 if (fd) fclose (fd);
214 }
215 return buf;
216 }
217
218 /*
219 * We have a cache for writing different resource files,
220 * which will only get flushed when we call wxFlushResources().
221 * Build up a list of resource databases waiting to be written.
222 *
223 */
224
225 wxList wxResourceCache (wxKEY_STRING);
226
227 void
228 wxFlushResources (void)
229 {
230 char nameBuffer[512];
231
232 wxNode *node = wxResourceCache.First ();
233 while (node)
234 {
235 const char *file = node->GetKeyString();
236 // If file doesn't exist, create it first.
237 (void)GetResourcePath(nameBuffer, file, true);
238
239 XrmDatabase database = (XrmDatabase) node->Data ();
240 XrmPutFileDatabase (database, nameBuffer);
241 XrmDestroyDatabase (database);
242 wxNode *next = node->Next ();
243 delete node;
244 node = next;
245 }
246 }
247
248 static XrmDatabase wxResourceDatabase = 0;
249
250 void wxXMergeDatabases (wxApp * theApp, Display * display);
251
252 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
253 {
254 char buffer[500];
255
256 (void) GetIniFile (buffer, file);
257
258 XrmDatabase database;
259 wxNode *node = wxResourceCache.Find (buffer);
260 if (node)
261 database = (XrmDatabase) node->Data ();
262 else
263 {
264 database = XrmGetFileDatabase (buffer);
265 wxResourceCache.Append (buffer, (wxObject *) database);
266 }
267
268 char resName[300];
269 strcpy (resName, section.c_str());
270 strcat (resName, ".");
271 strcat (resName, entry.c_str());
272
273 XrmPutStringResource (&database, resName, value);
274 return true;
275 }
276
277 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
278 {
279 char buf[50];
280 sprintf(buf, "%.4f", value);
281 return wxWriteResource(section, entry, buf, file);
282 }
283
284 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
285 {
286 char buf[50];
287 sprintf(buf, "%ld", value);
288 return wxWriteResource(section, entry, buf, file);
289 }
290
291 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
292 {
293 char buf[50];
294 sprintf(buf, "%d", value);
295 return wxWriteResource(section, entry, buf, file);
296 }
297
298 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
299 {
300 if (!wxResourceDatabase)
301 {
302 Display *display = wxGlobalDisplay();
303 wxXMergeDatabases (wxTheApp, display);
304 }
305
306 XrmDatabase database;
307
308 if (!file.empty())
309 {
310 char buffer[500];
311
312 // Is this right? Trying to get it to look in the user's
313 // home directory instead of current directory -- JACS
314 (void) GetIniFile (buffer, file);
315
316 wxNode *node = wxResourceCache.Find (buffer);
317 if (node)
318 database = (XrmDatabase) node->Data ();
319 else
320 {
321 database = XrmGetFileDatabase (buffer);
322 wxResourceCache.Append (buffer, (wxObject *) database);
323 }
324 }
325 else
326 database = wxResourceDatabase;
327
328 XrmValue xvalue;
329 char *str_type[20];
330 char buf[150];
331 strcpy (buf, section);
332 strcat (buf, ".");
333 strcat (buf, entry);
334
335 Bool success = XrmGetResource (database, buf, "*", str_type,
336 &xvalue);
337 // Try different combinations of upper/lower case, just in case...
338 if (!success)
339 {
340 buf[0] = (isupper (buf[0]) ? tolower (buf[0]) : toupper (buf[0]));
341 success = XrmGetResource (database, buf, "*", str_type,
342 &xvalue);
343 }
344 if (success)
345 {
346 if (*value)
347 delete[] *value;
348
349 *value = new char[xvalue.size + 1];
350 strncpy (*value, xvalue.addr, (int) xvalue.size);
351 return true;
352 }
353 return false;
354 }
355
356 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
357 {
358 char *s = NULL;
359 bool succ = wxGetResource(section, entry, (char **)&s, file);
360 if (succ)
361 {
362 *value = (float)strtod(s, NULL);
363 delete[] s;
364 return true;
365 }
366 else return false;
367 }
368
369 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
370 {
371 char *s = NULL;
372 bool succ = wxGetResource(section, entry, (char **)&s, file);
373 if (succ)
374 {
375 *value = strtol(s, NULL, 10);
376 delete[] s;
377 return true;
378 }
379 else return false;
380 }
381
382 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
383 {
384 char *s = NULL;
385 bool succ = wxGetResource(section, entry, (char **)&s, file);
386 if (succ)
387 {
388 // Handle True, False here
389 // True, Yes, Enables, Set or Activated
390 if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
391 *value = true;
392 // False, No, Disabled, Reset, Cleared, Deactivated
393 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
394 *value = false;
395 // Handle as Integer
396 else
397 *value = (int) strtol (s, NULL, 10);
398 delete[] s;
399 return true;
400 }
401 else
402 return false;
403 }
404
405 void wxXMergeDatabases (wxApp * theApp, Display * display)
406 {
407 XrmDatabase homeDB, serverDB, applicationDB;
408 char filenamebuf[1024];
409
410 char *filename = &filenamebuf[0];
411 char *environment;
412 wxString classname = theApp->GetClassName();
413 char name[256];
414 (void) strcpy (name, "/usr/lib/X11/app-defaults/");
415 (void) strcat (name, classname.c_str());
416
417 /* Get application defaults file, if any */
418 applicationDB = XrmGetFileDatabase (name);
419 (void) XrmMergeDatabases (applicationDB, &wxResourceDatabase);
420
421 /* Merge server defaults, created by xrdb, loaded as a property of the root
422 * window when the server initializes and loaded into the display
423 * structure on XOpenDisplay;
424 * if not defined, use .Xdefaults
425 */
426
427 if (XResourceManagerString (display) != NULL)
428 {
429 serverDB = XrmGetStringDatabase (XResourceManagerString (display));
430 }
431 else
432 {
433 (void) GetIniFile (filename, NULL);
434 serverDB = XrmGetFileDatabase (filename);
435 }
436 XrmMergeDatabases (serverDB, &wxResourceDatabase);
437
438 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
439 * and merge into existing database
440 */
441
442 if ((environment = getenv ("XENVIRONMENT")) == NULL)
443 {
444 size_t len;
445 environment = GetIniFile (filename, NULL);
446 len = strlen (environment);
447 wxString hostname = wxGetHostName();
448 if ( !hostname.empty() )
449 strncat(environment, hostname, 1024 - len);
450 }
451 homeDB = XrmGetFileDatabase (environment);
452 XrmMergeDatabases (homeDB, &wxResourceDatabase);
453 }
454
455 #if 0
456
457 /*
458 * Not yet used but may be useful.
459 *
460 */
461 void
462 wxSetDefaultResources (const Widget w, const char **resourceSpec, const char *name)
463 {
464 int i;
465 Display *dpy = XtDisplay (w); // Retrieve the display pointer
466
467 XrmDatabase rdb = NULL; // A resource data base
468
469 // Create an empty resource database
470 rdb = XrmGetStringDatabase ("");
471
472 // Add the Component resources, prepending the name of the component
473
474 i = 0;
475 while (resourceSpec[i] != NULL)
476 {
477 char buf[1000];
478
479 sprintf (buf, "*%s%s", name, resourceSpec[i++]);
480 XrmPutLineResource (&rdb, buf);
481 }
482
483 // Merge them into the Xt database, with lowest precendence
484
485 if (rdb)
486 {
487 #if (XlibSpecificationRelease>=5)
488 XrmDatabase db = XtDatabase (dpy);
489 XrmCombineDatabase (rdb, &db, False);
490 #else
491 XrmMergeDatabases (dpy->db, &rdb);
492 dpy->db = rdb;
493 #endif
494 }
495 }
496 #endif
497 // 0
498
499 #endif // wxUSE_RESOURCES
500
501 // ----------------------------------------------------------------------------
502 // display info
503 // ----------------------------------------------------------------------------
504
505 void wxGetMousePosition( int* x, int* y )
506 {
507 #if wxUSE_NANOX
508 // TODO
509 *x = 0;
510 *y = 0;
511 #else
512 XMotionEvent xev;
513 Window root, child;
514 XQueryPointer(wxGlobalDisplay(),
515 DefaultRootWindow(wxGlobalDisplay()),
516 &root, &child,
517 &(xev.x_root), &(xev.y_root),
518 &(xev.x), &(xev.y),
519 &(xev.state));
520 *x = xev.x_root;
521 *y = xev.y_root;
522 #endif
523 }
524
525 // Return true if we have a colour display
526 bool wxColourDisplay()
527 {
528 return wxDisplayDepth() > 1;
529 }
530
531 // Returns depth of screen
532 int wxDisplayDepth()
533 {
534 Display *dpy = wxGlobalDisplay();
535
536 return DefaultDepth (dpy, DefaultScreen (dpy));
537 }
538
539 // Get size of display
540 void wxDisplaySize(int *width, int *height)
541 {
542 Display *dpy = wxGlobalDisplay();
543
544 if ( width )
545 *width = DisplayWidth (dpy, DefaultScreen (dpy));
546 if ( height )
547 *height = DisplayHeight (dpy, DefaultScreen (dpy));
548 }
549
550 void wxDisplaySizeMM(int *width, int *height)
551 {
552 Display *dpy = wxGlobalDisplay();
553
554 if ( width )
555 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
556 if ( height )
557 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
558 }
559
560 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
561 {
562 // This is supposed to return desktop dimensions minus any window
563 // manager panels, menus, taskbars, etc. If there is a way to do that
564 // for this platform please fix this function, otherwise it defaults
565 // to the entire desktop.
566 if (x) *x = 0;
567 if (y) *y = 0;
568 wxDisplaySize(width, height);
569 }
570
571
572 // Configurable display in wxX11 and wxMotif
573 static WXDisplay *gs_currentDisplay = NULL;
574 static wxString gs_displayName;
575
576 WXDisplay *wxGetDisplay()
577 {
578 if (gs_currentDisplay)
579 return gs_currentDisplay;
580 else if (wxTheApp)
581 return wxTheApp->GetInitialDisplay();
582 return NULL;
583 }
584
585 bool wxSetDisplay(const wxString& display_name)
586 {
587 gs_displayName = display_name;
588
589 if ( display_name.empty() )
590 {
591 gs_currentDisplay = NULL;
592
593 return true;
594 }
595 else
596 {
597 Cardinal argc = 0;
598
599 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
600 display_name.c_str(),
601 wxTheApp->GetAppName().c_str(),
602 wxTheApp->GetClassName().c_str(),
603 NULL,
604 #if XtSpecificationRelease < 5
605 0, &argc,
606 #else
607 0, (int *)&argc,
608 #endif
609 NULL);
610
611 if (display)
612 {
613 gs_currentDisplay = (WXDisplay*) display;
614 return true;
615 }
616 else
617 return false;
618 }
619 }
620
621 wxString wxGetDisplayName()
622 {
623 return gs_displayName;
624 }
625
626 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
627 {
628 return wxGenericFindWindowAtPoint(pt);
629 }
630
631 // ----------------------------------------------------------------------------
632 // Some colour manipulation routines
633 // ----------------------------------------------------------------------------
634
635 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
636 {
637 int h = hsv->h;
638 int s = hsv->s;
639 int v = hsv->v;
640 int r = 0, g = 0, b = 0;
641 int i, f;
642 int p, q, t;
643 s = (s * wxMAX_RGB) / wxMAX_SV;
644 v = (v * wxMAX_RGB) / wxMAX_SV;
645 if (h == 360) h = 0;
646 if (s == 0) { h = 0; r = g = b = v; }
647 i = h / 60;
648 f = h % 60;
649 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
650 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
651 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
652 switch (i)
653 {
654 case 0: r = v, g = t, b = p; break;
655 case 1: r = q, g = v, b = p; break;
656 case 2: r = p, g = v, b = t; break;
657 case 3: r = p, g = q, b = v; break;
658 case 4: r = t, g = p, b = v; break;
659 case 5: r = v, g = p, b = q; break;
660 }
661 rgb->red = (unsigned short)(r << 8);
662 rgb->green = (unsigned short)(g << 8);
663 rgb->blue = (unsigned short)(b << 8);
664 }
665
666 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
667 {
668 int r = rgb->red >> 8;
669 int g = rgb->green >> 8;
670 int b = rgb->blue >> 8;
671 int maxv = wxMax3(r, g, b);
672 int minv = wxMin3(r, g, b);
673 int h = 0, s, v;
674 v = maxv;
675 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
676 else s = 0;
677 if (s == 0) h = 0;
678 else
679 {
680 int rc, gc, bc, hex = 0;
681 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
682 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
683 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
684 if (r == maxv) { h = bc - gc, hex = 0; }
685 else if (g == maxv) { h = rc - bc, hex = 2; }
686 else if (b == maxv) { h = gc - rc, hex = 4; }
687 h = hex * 60 + (h * 60 / wxMAX_RGB);
688 if (h < 0) h += 360;
689 }
690 hsv->h = h;
691 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
692 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
693 }
694
695 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
696 {
697 #if !wxUSE_NANOX
698 int llp;
699
700 int screen = DefaultScreen(d);
701 int num_colors = DisplayCells(d,screen);
702
703 XColor *color_defs = new XColor[num_colors];
704 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
705 XQueryColors(d,cmp,color_defs,num_colors);
706
707 wxHSV hsv_defs, hsv;
708 wxXColorToHSV(&hsv,xc);
709
710 int diff, min_diff = 0, pixel = 0;
711
712 for(llp = 0;llp < num_colors;llp++)
713 {
714 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
715 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
716 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
717 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
718 if (llp == 0) min_diff = diff;
719 if (min_diff > diff) { min_diff = diff; pixel = llp; }
720 if (min_diff == 0) break;
721 }
722
723 xc -> red = color_defs[pixel].red;
724 xc -> green = color_defs[pixel].green;
725 xc -> blue = color_defs[pixel].blue;
726 xc -> flags = DoRed | DoGreen | DoBlue;
727
728 /* FIXME, TODO
729 if (!XAllocColor(d,cmp,xc))
730 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
731 */
732
733 delete[] color_defs;
734 #endif
735 }
736
737 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
738 {
739 if (!XAllocColor(d,cmp,xc))
740 {
741 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
742 wxAllocNearestColor(d,cmp,xc);
743 }
744 }
745
746 #ifdef __WXDEBUG__
747 wxString wxGetXEventName(XEvent& event)
748 {
749 #if wxUSE_NANOX
750 wxString str(wxT("(some event)"));
751 return str;
752 #else
753 int type = event.xany.type;
754 static char* event_name[] = {
755 wxMOTIF_STR(""), wxMOTIF_STR("unknown(-)"), // 0-1
756 wxMOTIF_STR("KeyPress"), wxMOTIF_STR("KeyRelease"), wxMOTIF_STR("ButtonPress"), wxMOTIF_STR("ButtonRelease"), // 2-5
757 wxMOTIF_STR("MotionNotify"), wxMOTIF_STR("EnterNotify"), wxMOTIF_STR("LeaveNotify"), wxMOTIF_STR("FocusIn"), // 6-9
758 wxMOTIF_STR("FocusOut"), wxMOTIF_STR("KeymapNotify"), wxMOTIF_STR("Expose"), wxMOTIF_STR("GraphicsExpose"), // 10-13
759 wxMOTIF_STR("NoExpose"), wxMOTIF_STR("VisibilityNotify"), wxMOTIF_STR("CreateNotify"), // 14-16
760 wxMOTIF_STR("DestroyNotify"), wxMOTIF_STR("UnmapNotify"), wxMOTIF_STR("MapNotify"), wxMOTIF_STR("MapRequest"),// 17-20
761 wxMOTIF_STR("ReparentNotify"), wxMOTIF_STR("ConfigureNotify"), wxMOTIF_STR("ConfigureRequest"), // 21-23
762 wxMOTIF_STR("GravityNotify"), wxMOTIF_STR("ResizeRequest"), wxMOTIF_STR("CirculateNotify"), // 24-26
763 wxMOTIF_STR("CirculateRequest"), wxMOTIF_STR("PropertyNotify"), wxMOTIF_STR("SelectionClear"), // 27-29
764 wxMOTIF_STR("SelectionRequest"), wxMOTIF_STR("SelectionNotify"), wxMOTIF_STR("ColormapNotify"), // 30-32
765 wxMOTIF_STR("ClientMessage"), wxMOTIF_STR("MappingNotify"), // 33-34
766 wxMOTIF_STR("unknown(+)")}; // 35
767 type = wxMin(35, type); type = wxMax(1, type);
768 wxString str(event_name[type]);
769 return str;
770 #endif
771 }
772 #endif
773
774 // ----------------------------------------------------------------------------
775 // accelerators
776 // ----------------------------------------------------------------------------
777
778 // Find the letter corresponding to the mnemonic, for Motif
779 char wxFindMnemonic (const char *s)
780 {
781 char mnem = 0;
782 int len = strlen (s);
783 int i;
784
785 for (i = 0; i < len; i++)
786 {
787 if (s[i] == '&')
788 {
789 // Carefully handle &&
790 if ((i + 1) <= len && s[i + 1] == '&')
791 i++;
792 else
793 {
794 mnem = s[i + 1];
795 break;
796 }
797 }
798 }
799 return mnem;
800 }
801
802 char* wxFindAccelerator( const char *s )
803 {
804 #if 1
805 wxUnusedVar(s);
806 // VZ: this function returns incorrect keysym which completely breaks kbd
807 // handling
808 return NULL;
809 #else
810 // The accelerator text is after the \t char.
811 s = strchr( s, '\t' );
812
813 if( !s ) return NULL;
814
815 /*
816 Now we need to format it as X standard:
817
818 input output
819
820 F7 --> <Key>F7
821 Ctrl+N --> Ctrl<Key>N
822 Alt+k --> Meta<Key>k
823 Ctrl+Shift+A --> Ctrl Shift<Key>A
824
825 and handle Ctrl-N & similia
826 */
827
828 static char buf[256];
829
830 buf[0] = '\0';
831 wxString tmp = s + 1; // skip TAB
832 size_t index = 0;
833
834 while( index < tmp.length() )
835 {
836 size_t plus = tmp.find( '+', index );
837 size_t minus = tmp.find( '-', index );
838
839 // neither '+' nor '-', add <Key>
840 if( plus == wxString::npos && minus == wxString::npos )
841 {
842 strcat( buf, "<Key>" );
843 strcat( buf, tmp.c_str() + index );
844
845 return buf;
846 }
847
848 // OK: npos is big and positive
849 size_t sep = wxMin( plus, minus );
850 wxString mod = tmp.substr( index, sep - index );
851
852 // Ctrl -> Ctrl
853 // Shift -> Shift
854 // Alt -> Meta
855 if( mod == "Alt" )
856 mod = "Meta";
857
858 if( buf[0] )
859 strcat( buf, " " );
860
861 strcat( buf, mod.c_str() );
862
863 index = sep + 1;
864 }
865
866 return NULL;
867 #endif
868 }
869
870 XmString wxFindAcceleratorText (const char *s)
871 {
872 #if 1
873 wxUnusedVar(s);
874 // VZ: this function returns incorrect keysym which completely breaks kbd
875 // handling
876 return NULL;
877 #else
878 // The accelerator text is after the \t char.
879 s = strchr( s, '\t' );
880
881 if( !s ) return NULL;
882
883 return wxStringToXmString( s + 1 ); // skip TAB!
884 #endif
885 }
886
887 // Change a widget's foreground and background colours.
888 void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
889 {
890 // When should we specify the foreground, if it's calculated
891 // by wxComputeColours?
892 // Solution: say we start with the default (computed) foreground colour.
893 // If we call SetForegroundColour explicitly for a control or window,
894 // then the foreground is changed.
895 // Therefore SetBackgroundColour computes the foreground colour, and
896 // SetForegroundColour changes the foreground colour. The ordering is
897 // important.
898
899 XtVaSetValues ((Widget) widget,
900 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
901 NULL);
902 }
903
904 void wxDoChangeBackgroundColour(WXWidget widget, const wxColour& backgroundColour, bool changeArmColour)
905 {
906 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
907 (wxColour*) NULL);
908
909 XtVaSetValues ((Widget) widget,
910 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
911 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
912 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
913 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
914 NULL);
915
916 if (changeArmColour)
917 XtVaSetValues ((Widget) widget,
918 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
919 NULL);
920 }
921
922 extern void wxDoChangeFont(WXWidget widget, const wxFont& font)
923 {
924 // Lesstif 0.87 hangs here, but 0.93 does not; MBN: sometimes it does
925 #if !wxCHECK_LESSTIF() // || wxCHECK_LESSTIF_VERSION( 0, 93 )
926 Widget w = (Widget)widget;
927 XtVaSetValues( w,
928 wxFont::GetFontTag(), font.GetFontTypeC( XtDisplay(w) ),
929 NULL );
930 #else
931 wxUnusedVar(widget);
932 wxUnusedVar(font);
933 #endif
934
935 }
936
937 wxString wxXmStringToString( const XmString& xmString )
938 {
939 char *txt;
940 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
941 {
942 wxString str(txt);
943 XtFree (txt);
944 return str;
945 }
946
947 return wxEmptyString;
948 }
949
950 XmString wxStringToXmString( const wxString& str )
951 {
952 return XmStringCreateLtoR((char *)str.c_str(), XmSTRING_DEFAULT_CHARSET);
953 }
954
955 XmString wxStringToXmString( const char* str )
956 {
957 return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET);
958 }
959
960 // ----------------------------------------------------------------------------
961 // wxBitmap utility functions
962 // ----------------------------------------------------------------------------
963
964 // Creates a bitmap with transparent areas drawn in
965 // the given colour.
966 wxBitmap wxCreateMaskedBitmap(const wxBitmap& bitmap, const wxColour& colour)
967 {
968 wxBitmap newBitmap(bitmap.GetWidth(),
969 bitmap.GetHeight(),
970 bitmap.GetDepth());
971 wxMemoryDC destDC;
972 wxMemoryDC srcDC;
973
974 srcDC.SelectObject(bitmap);
975 destDC.SelectObject(newBitmap);
976
977 wxBrush brush(colour, wxSOLID);
978 destDC.SetBackground(brush);
979 destDC.Clear();
980 destDC.Blit(0, 0, bitmap.GetWidth(), bitmap.GetHeight(),
981 &srcDC, 0, 0, wxCOPY, true);
982
983 return newBitmap;
984 }
985
986 // ----------------------------------------------------------------------------
987 // Miscellaneous functions
988 // ----------------------------------------------------------------------------
989
990 WXWidget wxCreateBorderWidget( WXWidget parent, long style )
991 {
992 Widget borderWidget = (Widget)NULL, parentWidget = (Widget)parent;
993
994 if (style & wxSIMPLE_BORDER)
995 {
996 borderWidget = XtVaCreateManagedWidget
997 (
998 "simpleBorder",
999 xmFrameWidgetClass, parentWidget,
1000 XmNshadowType, XmSHADOW_ETCHED_IN,
1001 XmNshadowThickness, 1,
1002 NULL
1003 );
1004 }
1005 else if (style & wxSUNKEN_BORDER)
1006 {
1007 borderWidget = XtVaCreateManagedWidget
1008 (
1009 "sunkenBorder",
1010 xmFrameWidgetClass, parentWidget,
1011 XmNshadowType, XmSHADOW_IN,
1012 NULL
1013 );
1014 }
1015 else if (style & wxRAISED_BORDER)
1016 {
1017 borderWidget = XtVaCreateManagedWidget
1018 (
1019 "raisedBorder",
1020 xmFrameWidgetClass, parentWidget,
1021 XmNshadowType, XmSHADOW_OUT,
1022 NULL
1023 );
1024 }
1025
1026 return borderWidget;
1027 }