Remove (now) pointless checks for __WXX11__/__WXMOTIF__ in
[wxWidgets.git] / src / motif / utils.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __VMS
21 #define XtDisplay XTDISPLAY
22 #endif
23 #include "wx/setup.h"
24 #include "wx/utils.h"
25 #include "wx/app.h"
26 #include "wx/msgdlg.h"
27 #include "wx/cursor.h"
28 #include "wx/window.h" // for wxTopLevelWindows
29
30 #include <ctype.h>
31 #include <stdarg.h>
32 #include <dirent.h>
33 #include <string.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <sys/wait.h>
38 #include <pwd.h>
39 #include <errno.h>
40 #include <signal.h>
41
42 #if (defined(__SUNCC__) || defined(__CLCC__))
43 #include <sysent.h>
44 #endif
45
46 #ifdef __VMS__
47 #pragma message disable nosimpint
48 #endif
49
50 #include "wx/unix/execute.h"
51
52 #include <Xm/Xm.h>
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()
91 {
92 Display *display = (Display*) wxGetDisplay();
93
94 XSync (display, FALSE);
95
96 // XtAppPending returns availability of events AND timers/inputs, which
97 // are processed via callbacks, so XtAppNextEvent will not return if
98 // there are no events. So added '& XtIMXEvent' - Sergey.
99 while (XtAppPending ((XtAppContext) wxTheApp->GetAppContext()) & XtIMXEvent)
100 {
101 XFlush (XtDisplay ((Widget) wxTheApp->GetTopLevelWidget()));
102 // Jan Lessner: works better when events are non-X events
103 XtAppProcessEvent((XtAppContext) wxTheApp->GetAppContext(), XtIMXEvent);
104 }
105 }
106
107 #if 0
108 // Check whether this window wants to process messages, e.g. Stop button
109 // in long calculations.
110 bool wxCheckForInterrupt(wxWindow *wnd)
111 {
112 wxCHECK_MSG( wnd, FALSE, "NULL window in wxCheckForInterrupt" );
113
114 Display *dpy=(Display*) wnd->GetXDisplay();
115 Window win=(Window) wnd->GetXWindow();
116 XEvent event;
117 XFlush(dpy);
118 if (wnd->GetMainWidget())
119 {
120 XmUpdateDisplay((Widget)(wnd->GetMainWidget()));
121 }
122
123 bool hadEvents = FALSE;
124 while( XCheckMaskEvent(dpy,
125 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|
126 PointerMotionMask|KeyPressMask|KeyReleaseMask,
127 &event) )
128 {
129 if ( event.xany.window == win )
130 {
131 hadEvents = TRUE;
132
133 XtDispatchEvent(&event);
134 }
135 }
136
137 return hadEvents;
138 }
139 #endif
140
141 // ----------------------------------------------------------------------------
142 // wxExecute stuff
143 // ----------------------------------------------------------------------------
144
145 static void xt_notify_end_process(XtPointer data, int *WXUNUSED(fid),
146 XtInputId *id)
147 {
148 wxEndProcessData *proc_data = (wxEndProcessData *)data;
149
150 wxHandleProcessTermination(proc_data);
151
152 // VZ: I think they should be the same...
153 wxASSERT( (int)*id == proc_data->tag );
154
155 XtRemoveInput(*id);
156 }
157
158 int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
159 {
160 XtInputId id = XtAppAddInput((XtAppContext) wxTheApp->GetAppContext(),
161 fd,
162 (XtPointer *) XtInputReadMask,
163 (XtInputCallbackProc) xt_notify_end_process,
164 (XtPointer) proc_data);
165
166 return (int)id;
167 }
168
169 // ----------------------------------------------------------------------------
170 // misc
171 // ----------------------------------------------------------------------------
172
173 // Emit a beeeeeep
174 void wxBell()
175 {
176 // Use current setting for the bell
177 XBell ((Display*) wxGetDisplay(), 0);
178 }
179
180 int wxGetOsVersion(int *majorVsn, int *minorVsn)
181 {
182 // FIXME TODO
183 // This code is WRONG!! Does NOT return the
184 // Motif version of the libs but the X protocol
185 // version!
186 Display *display = XtDisplay ((Widget) wxTheApp->GetTopLevelWidget());
187 if (majorVsn)
188 *majorVsn = ProtocolVersion (display);
189 if (minorVsn)
190 *minorVsn = ProtocolRevision (display);
191
192 return wxMOTIF_X;
193 }
194
195 // ----------------------------------------------------------------------------
196 // Reading and writing resources (eg WIN.INI, .Xdefaults)
197 // ----------------------------------------------------------------------------
198
199 #if wxUSE_RESOURCES
200
201 // Read $HOME for what it says is home, if not
202 // read $USER or $LOGNAME for user name else determine
203 // the Real User, then determine the Real home dir.
204 static char * GetIniFile (char *dest, const char *filename)
205 {
206 char *home = NULL;
207 if (filename && wxIsAbsolutePath(filename))
208 {
209 strcpy(dest, filename);
210 }
211 else if ((home = wxGetUserHome("")) != NULL)
212 {
213 strcpy(dest, home);
214 if (dest[strlen(dest) - 1] != '/')
215 strcat (dest, "/");
216 if (filename == NULL)
217 {
218 if ((filename = getenv ("XENVIRONMENT")) == NULL)
219 filename = ".Xdefaults";
220 }
221 else if (*filename != '.')
222 strcat (dest, ".");
223 strcat (dest, filename);
224 } else
225 {
226 dest[0] = '\0';
227 }
228 return dest;
229 }
230
231 static char *GetResourcePath(char *buf, const char *name, bool create = FALSE)
232 {
233 if (create && wxFileExists (name) ) {
234 strcpy(buf, name);
235 return buf; // Exists so ...
236 }
237
238 if (*name == '/')
239 strcpy(buf, name);
240 else {
241 // Put in standard place for resource files if not absolute
242 strcpy (buf, DEFAULT_XRESOURCE_DIR);
243 strcat (buf, "/");
244 strcat (buf, wxFileNameFromPath (name).c_str());
245 }
246
247 if (create) {
248 // Touch the file to create it
249 FILE *fd = fopen (buf, "w");
250 if (fd) fclose (fd);
251 }
252 return buf;
253 }
254
255 /*
256 * We have a cache for writing different resource files,
257 * which will only get flushed when we call wxFlushResources().
258 * Build up a list of resource databases waiting to be written.
259 *
260 */
261
262 wxList wxResourceCache (wxKEY_STRING);
263
264 void
265 wxFlushResources (void)
266 {
267 char nameBuffer[512];
268
269 wxNode *node = wxResourceCache.First ();
270 while (node)
271 {
272 const char *file = node->GetKeyString();
273 // If file doesn't exist, create it first.
274 (void)GetResourcePath(nameBuffer, file, TRUE);
275
276 XrmDatabase database = (XrmDatabase) node->Data ();
277 XrmPutFileDatabase (database, nameBuffer);
278 XrmDestroyDatabase (database);
279 wxNode *next = node->Next ();
280 delete node;
281 node = next;
282 }
283 }
284
285 static XrmDatabase wxResourceDatabase = 0;
286
287 void wxXMergeDatabases (wxApp * theApp, Display * display);
288
289 bool wxWriteResource(const wxString& section, const wxString& entry, const wxString& value, const wxString& file)
290 {
291 char buffer[500];
292
293 (void) GetIniFile (buffer, file);
294
295 XrmDatabase database;
296 wxNode *node = wxResourceCache.Find (buffer);
297 if (node)
298 database = (XrmDatabase) node->Data ();
299 else
300 {
301 database = XrmGetFileDatabase (buffer);
302 wxResourceCache.Append (buffer, (wxObject *) database);
303 }
304
305 char resName[300];
306 strcpy (resName, section.c_str());
307 strcat (resName, ".");
308 strcat (resName, entry.c_str());
309
310 XrmPutStringResource (&database, resName, value);
311 return TRUE;
312 }
313
314 bool wxWriteResource(const wxString& section, const wxString& entry, float value, const wxString& file)
315 {
316 char buf[50];
317 sprintf(buf, "%.4f", value);
318 return wxWriteResource(section, entry, buf, file);
319 }
320
321 bool wxWriteResource(const wxString& section, const wxString& entry, long value, const wxString& file)
322 {
323 char buf[50];
324 sprintf(buf, "%ld", value);
325 return wxWriteResource(section, entry, buf, file);
326 }
327
328 bool wxWriteResource(const wxString& section, const wxString& entry, int value, const wxString& file)
329 {
330 char buf[50];
331 sprintf(buf, "%d", value);
332 return wxWriteResource(section, entry, buf, file);
333 }
334
335 bool wxGetResource(const wxString& section, const wxString& entry, char **value, const wxString& file)
336 {
337 if (!wxResourceDatabase)
338 {
339 Display *display = (Display*) wxGetDisplay();
340 wxXMergeDatabases (wxTheApp, display);
341 }
342
343 XrmDatabase database;
344
345 if (file != "")
346 {
347 char buffer[500];
348
349 // Is this right? Trying to get it to look in the user's
350 // home directory instead of current directory -- JACS
351 (void) GetIniFile (buffer, file);
352
353 wxNode *node = wxResourceCache.Find (buffer);
354 if (node)
355 database = (XrmDatabase) node->Data ();
356 else
357 {
358 database = XrmGetFileDatabase (buffer);
359 wxResourceCache.Append (buffer, (wxObject *) database);
360 }
361 }
362 else
363 database = wxResourceDatabase;
364
365 XrmValue xvalue;
366 char *str_type[20];
367 char buf[150];
368 strcpy (buf, section);
369 strcat (buf, ".");
370 strcat (buf, entry);
371
372 Bool success = XrmGetResource (database, buf, "*", str_type,
373 &xvalue);
374 // Try different combinations of upper/lower case, just in case...
375 if (!success)
376 {
377 buf[0] = (isupper (buf[0]) ? tolower (buf[0]) : toupper (buf[0]));
378 success = XrmGetResource (database, buf, "*", str_type,
379 &xvalue);
380 }
381 if (success)
382 {
383 if (*value)
384 delete[] *value;
385
386 *value = new char[xvalue.size + 1];
387 strncpy (*value, xvalue.addr, (int) xvalue.size);
388 return TRUE;
389 }
390 return FALSE;
391 }
392
393 bool wxGetResource(const wxString& section, const wxString& entry, float *value, const wxString& file)
394 {
395 char *s = NULL;
396 bool succ = wxGetResource(section, entry, (char **)&s, file);
397 if (succ)
398 {
399 *value = (float)strtod(s, NULL);
400 delete[] s;
401 return TRUE;
402 }
403 else return FALSE;
404 }
405
406 bool wxGetResource(const wxString& section, const wxString& entry, long *value, const wxString& file)
407 {
408 char *s = NULL;
409 bool succ = wxGetResource(section, entry, (char **)&s, file);
410 if (succ)
411 {
412 *value = strtol(s, NULL, 10);
413 delete[] s;
414 return TRUE;
415 }
416 else return FALSE;
417 }
418
419 bool wxGetResource(const wxString& section, const wxString& entry, int *value, const wxString& file)
420 {
421 char *s = NULL;
422 bool succ = wxGetResource(section, entry, (char **)&s, file);
423 if (succ)
424 {
425 // Handle True, False here
426 // True, Yes, Enables, Set or Activated
427 if (*s == 'T' || *s == 'Y' || *s == 'E' || *s == 'S' || *s == 'A')
428 *value = TRUE;
429 // False, No, Disabled, Reset, Cleared, Deactivated
430 else if (*s == 'F' || *s == 'N' || *s == 'D' || *s == 'R' || *s == 'C')
431 *value = FALSE;
432 // Handle as Integer
433 else
434 *value = (int) strtol (s, NULL, 10);
435 delete[] s;
436 return TRUE;
437 }
438 else
439 return FALSE;
440 }
441
442 void wxXMergeDatabases (wxApp * theApp, Display * display)
443 {
444 XrmDatabase homeDB, serverDB, applicationDB;
445 char filenamebuf[1024];
446
447 char *filename = &filenamebuf[0];
448 char *environment;
449 wxString classname = theApp->GetClassName();
450 char name[256];
451 (void) strcpy (name, "/usr/lib/X11/app-defaults/");
452 (void) strcat (name, classname.c_str());
453
454 /* Get application defaults file, if any */
455 applicationDB = XrmGetFileDatabase (name);
456 (void) XrmMergeDatabases (applicationDB, &wxResourceDatabase);
457
458 /* Merge server defaults, created by xrdb, loaded as a property of the root
459 * window when the server initializes and loaded into the display
460 * structure on XOpenDisplay;
461 * if not defined, use .Xdefaults
462 */
463
464 if (XResourceManagerString (display) != NULL)
465 {
466 serverDB = XrmGetStringDatabase (XResourceManagerString (display));
467 }
468 else
469 {
470 (void) GetIniFile (filename, NULL);
471 serverDB = XrmGetFileDatabase (filename);
472 }
473 XrmMergeDatabases (serverDB, &wxResourceDatabase);
474
475 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
476 * and merge into existing database
477 */
478
479 if ((environment = getenv ("XENVIRONMENT")) == NULL)
480 {
481 size_t len;
482 environment = GetIniFile (filename, NULL);
483 len = strlen (environment);
484 wxString hostname = wxGetHostName();
485 if ( !!hostname )
486 strncat(environment, hostname, 1024 - len);
487 }
488 homeDB = XrmGetFileDatabase (environment);
489 XrmMergeDatabases (homeDB, &wxResourceDatabase);
490 }
491
492 #if 0
493
494 /*
495 * Not yet used but may be useful.
496 *
497 */
498 void
499 wxSetDefaultResources (const Widget w, const char **resourceSpec, const char *name)
500 {
501 int i;
502 Display *dpy = XtDisplay (w); // Retrieve the display pointer
503
504 XrmDatabase rdb = NULL; // A resource data base
505
506 // Create an empty resource database
507 rdb = XrmGetStringDatabase ("");
508
509 // Add the Component resources, prepending the name of the component
510
511 i = 0;
512 while (resourceSpec[i] != NULL)
513 {
514 char buf[1000];
515
516 sprintf (buf, "*%s%s", name, resourceSpec[i++]);
517 XrmPutLineResource (&rdb, buf);
518 }
519
520 // Merge them into the Xt database, with lowest precendence
521
522 if (rdb)
523 {
524 #if (XlibSpecificationRelease>=5)
525 XrmDatabase db = XtDatabase (dpy);
526 XrmCombineDatabase (rdb, &db, FALSE);
527 #else
528 XrmMergeDatabases (dpy->db, &rdb);
529 dpy->db = rdb;
530 #endif
531 }
532 }
533 #endif
534 // 0
535
536 #endif // wxUSE_RESOURCES
537
538 // ----------------------------------------------------------------------------
539 // display info
540 // ----------------------------------------------------------------------------
541
542 void wxGetMousePosition( int* x, int* y )
543 {
544 #if wxUSE_NANOX
545 // TODO
546 *x = 0;
547 *y = 0;
548 #else
549 XMotionEvent xev;
550 Window root, child;
551 XQueryPointer((Display*) wxGetDisplay(),
552 DefaultRootWindow((Display*) wxGetDisplay()),
553 &root, &child,
554 &(xev.x_root), &(xev.y_root),
555 &(xev.x), &(xev.y),
556 &(xev.state));
557 *x = xev.x_root;
558 *y = xev.y_root;
559 #endif
560 };
561
562 // Return TRUE if we have a colour display
563 bool wxColourDisplay()
564 {
565 return wxDisplayDepth() > 1;
566 }
567
568 // Returns depth of screen
569 int wxDisplayDepth()
570 {
571 Display *dpy = (Display*) wxGetDisplay();
572
573 return DefaultDepth (dpy, DefaultScreen (dpy));
574 }
575
576 // Get size of display
577 void wxDisplaySize(int *width, int *height)
578 {
579 Display *dpy = (Display*) wxGetDisplay();
580
581 if ( width )
582 *width = DisplayWidth (dpy, DefaultScreen (dpy));
583 if ( height )
584 *height = DisplayHeight (dpy, DefaultScreen (dpy));
585 }
586
587 void wxDisplaySizeMM(int *width, int *height)
588 {
589 Display *dpy = (Display*) wxGetDisplay();
590
591 if ( width )
592 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
593 if ( height )
594 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
595 }
596
597 void wxClientDisplayRect(int *x, int *y, int *width, int *height)
598 {
599 // This is supposed to return desktop dimensions minus any window
600 // manager panels, menus, taskbars, etc. If there is a way to do that
601 // for this platform please fix this function, otherwise it defaults
602 // to the entire desktop.
603 if (x) *x = 0;
604 if (y) *y = 0;
605 wxDisplaySize(width, height);
606 }
607
608
609 // Configurable display in wxX11 and wxMotif
610 static WXDisplay *gs_currentDisplay = NULL;
611 static wxString gs_displayName;
612
613 WXDisplay *wxGetDisplay()
614 {
615 if (gs_currentDisplay)
616 return gs_currentDisplay;
617 if (wxTheApp && wxTheApp->GetTopLevelWidget())
618 return XtDisplay ((Widget) wxTheApp->GetTopLevelWidget());
619 else if (wxTheApp)
620 return wxTheApp->GetInitialDisplay();
621 return NULL;
622 }
623
624 bool wxSetDisplay(const wxString& display_name)
625 {
626 gs_displayName = display_name;
627
628 if ( display_name.IsEmpty() )
629 {
630 gs_currentDisplay = NULL;
631
632 return TRUE;
633 }
634 else
635 {
636 Cardinal argc = 0;
637
638 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
639 display_name.c_str(),
640 wxTheApp->GetAppName().c_str(),
641 wxTheApp->GetClassName().c_str(),
642 NULL,
643 #if XtSpecificationRelease < 5
644 0, &argc,
645 #else
646 0, (int *)&argc,
647 #endif
648 NULL);
649
650 if (display)
651 {
652 gs_currentDisplay = (WXDisplay*) display;
653 return TRUE;
654 }
655 else
656 return FALSE;
657 }
658 }
659
660 wxString wxGetDisplayName()
661 {
662 return gs_displayName;
663 }
664
665 wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
666 {
667 return wxGenericFindWindowAtPoint(pt);
668 }
669
670 // ----------------------------------------------------------------------------
671 // keycode translations
672 // ----------------------------------------------------------------------------
673
674 #include <X11/keysym.h>
675
676 // FIXME what about tables??
677
678 int wxCharCodeXToWX(KeySym keySym)
679 {
680 int id;
681 switch (keySym)
682 {
683 case XK_Shift_L:
684 case XK_Shift_R:
685 id = WXK_SHIFT; break;
686 case XK_Control_L:
687 case XK_Control_R:
688 id = WXK_CONTROL; break;
689 case XK_BackSpace:
690 id = WXK_BACK; break;
691 case XK_Delete:
692 id = WXK_DELETE; break;
693 case XK_Clear:
694 id = WXK_CLEAR; break;
695 case XK_Tab:
696 id = WXK_TAB; break;
697 case XK_numbersign:
698 id = '#'; break;
699 case XK_Return:
700 id = WXK_RETURN; break;
701 case XK_Escape:
702 id = WXK_ESCAPE; break;
703 case XK_Pause:
704 case XK_Break:
705 id = WXK_PAUSE; break;
706 case XK_Num_Lock:
707 id = WXK_NUMLOCK; break;
708 case XK_Scroll_Lock:
709 id = WXK_SCROLL; break;
710
711 case XK_Home:
712 id = WXK_HOME; break;
713 case XK_End:
714 id = WXK_END; break;
715 case XK_Left:
716 id = WXK_LEFT; break;
717 case XK_Right:
718 id = WXK_RIGHT; break;
719 case XK_Up:
720 id = WXK_UP; break;
721 case XK_Down:
722 id = WXK_DOWN; break;
723 case XK_Next:
724 id = WXK_NEXT; break;
725 case XK_Prior:
726 id = WXK_PRIOR; break;
727 case XK_Menu:
728 id = WXK_MENU; break;
729 case XK_Select:
730 id = WXK_SELECT; break;
731 case XK_Cancel:
732 id = WXK_CANCEL; break;
733 case XK_Print:
734 id = WXK_PRINT; break;
735 case XK_Execute:
736 id = WXK_EXECUTE; break;
737 case XK_Insert:
738 id = WXK_INSERT; break;
739 case XK_Help:
740 id = WXK_HELP; break;
741
742 case XK_KP_Multiply:
743 id = WXK_MULTIPLY; break;
744 case XK_KP_Add:
745 id = WXK_ADD; break;
746 case XK_KP_Subtract:
747 id = WXK_SUBTRACT; break;
748 case XK_KP_Divide:
749 id = WXK_DIVIDE; break;
750 case XK_KP_Decimal:
751 id = WXK_DECIMAL; break;
752 case XK_KP_Equal:
753 id = '='; break;
754 case XK_KP_Space:
755 id = ' '; break;
756 case XK_KP_Tab:
757 id = WXK_TAB; break;
758 case XK_KP_Enter:
759 id = WXK_RETURN; break;
760 case XK_KP_0:
761 id = WXK_NUMPAD0; break;
762 case XK_KP_1:
763 id = WXK_NUMPAD1; break;
764 case XK_KP_2:
765 id = WXK_NUMPAD2; break;
766 case XK_KP_3:
767 id = WXK_NUMPAD3; break;
768 case XK_KP_4:
769 id = WXK_NUMPAD4; break;
770 case XK_KP_5:
771 id = WXK_NUMPAD5; break;
772 case XK_KP_6:
773 id = WXK_NUMPAD6; break;
774 case XK_KP_7:
775 id = WXK_NUMPAD7; break;
776 case XK_KP_8:
777 id = WXK_NUMPAD8; break;
778 case XK_KP_9:
779 id = WXK_NUMPAD9; break;
780 case XK_F1:
781 id = WXK_F1; break;
782 case XK_F2:
783 id = WXK_F2; break;
784 case XK_F3:
785 id = WXK_F3; break;
786 case XK_F4:
787 id = WXK_F4; break;
788 case XK_F5:
789 id = WXK_F5; break;
790 case XK_F6:
791 id = WXK_F6; break;
792 case XK_F7:
793 id = WXK_F7; break;
794 case XK_F8:
795 id = WXK_F8; break;
796 case XK_F9:
797 id = WXK_F9; break;
798 case XK_F10:
799 id = WXK_F10; break;
800 case XK_F11:
801 id = WXK_F11; break;
802 case XK_F12:
803 id = WXK_F12; break;
804 case XK_F13:
805 id = WXK_F13; break;
806 case XK_F14:
807 id = WXK_F14; break;
808 case XK_F15:
809 id = WXK_F15; break;
810 case XK_F16:
811 id = WXK_F16; break;
812 case XK_F17:
813 id = WXK_F17; break;
814 case XK_F18:
815 id = WXK_F18; break;
816 case XK_F19:
817 id = WXK_F19; break;
818 case XK_F20:
819 id = WXK_F20; break;
820 case XK_F21:
821 id = WXK_F21; break;
822 case XK_F22:
823 id = WXK_F22; break;
824 case XK_F23:
825 id = WXK_F23; break;
826 case XK_F24:
827 id = WXK_F24; break;
828 default:
829 id = (keySym <= 255) ? (int)keySym : -1;
830 }
831
832 return id;
833 }
834
835 KeySym wxCharCodeWXToX(int id)
836 {
837 KeySym keySym;
838
839 switch (id)
840 {
841 case WXK_CANCEL: keySym = XK_Cancel; break;
842 case WXK_BACK: keySym = XK_BackSpace; break;
843 case WXK_TAB: keySym = XK_Tab; break;
844 case WXK_CLEAR: keySym = XK_Clear; break;
845 case WXK_RETURN: keySym = XK_Return; break;
846 case WXK_SHIFT: keySym = XK_Shift_L; break;
847 case WXK_CONTROL: keySym = XK_Control_L; break;
848 case WXK_MENU : keySym = XK_Menu; break;
849 case WXK_PAUSE: keySym = XK_Pause; break;
850 case WXK_ESCAPE: keySym = XK_Escape; break;
851 case WXK_SPACE: keySym = ' '; break;
852 case WXK_PRIOR: keySym = XK_Prior; break;
853 case WXK_NEXT : keySym = XK_Next; break;
854 case WXK_END: keySym = XK_End; break;
855 case WXK_HOME : keySym = XK_Home; break;
856 case WXK_LEFT : keySym = XK_Left; break;
857 case WXK_UP: keySym = XK_Up; break;
858 case WXK_RIGHT: keySym = XK_Right; break;
859 case WXK_DOWN : keySym = XK_Down; break;
860 case WXK_SELECT: keySym = XK_Select; break;
861 case WXK_PRINT: keySym = XK_Print; break;
862 case WXK_EXECUTE: keySym = XK_Execute; break;
863 case WXK_INSERT: keySym = XK_Insert; break;
864 case WXK_DELETE: keySym = XK_Delete; break;
865 case WXK_HELP : keySym = XK_Help; break;
866 case WXK_NUMPAD0: keySym = XK_KP_0; break;
867 case WXK_NUMPAD1: keySym = XK_KP_1; break;
868 case WXK_NUMPAD2: keySym = XK_KP_2; break;
869 case WXK_NUMPAD3: keySym = XK_KP_3; break;
870 case WXK_NUMPAD4: keySym = XK_KP_4; break;
871 case WXK_NUMPAD5: keySym = XK_KP_5; break;
872 case WXK_NUMPAD6: keySym = XK_KP_6; break;
873 case WXK_NUMPAD7: keySym = XK_KP_7; break;
874 case WXK_NUMPAD8: keySym = XK_KP_8; break;
875 case WXK_NUMPAD9: keySym = XK_KP_9; break;
876 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
877 case WXK_ADD: keySym = XK_KP_Add; break;
878 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
879 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
880 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
881 case WXK_F1: keySym = XK_F1; break;
882 case WXK_F2: keySym = XK_F2; break;
883 case WXK_F3: keySym = XK_F3; break;
884 case WXK_F4: keySym = XK_F4; break;
885 case WXK_F5: keySym = XK_F5; break;
886 case WXK_F6: keySym = XK_F6; break;
887 case WXK_F7: keySym = XK_F7; break;
888 case WXK_F8: keySym = XK_F8; break;
889 case WXK_F9: keySym = XK_F9; break;
890 case WXK_F10: keySym = XK_F10; break;
891 case WXK_F11: keySym = XK_F11; break;
892 case WXK_F12: keySym = XK_F12; break;
893 case WXK_F13: keySym = XK_F13; break;
894 case WXK_F14: keySym = XK_F14; break;
895 case WXK_F15: keySym = XK_F15; break;
896 case WXK_F16: keySym = XK_F16; break;
897 case WXK_F17: keySym = XK_F17; break;
898 case WXK_F18: keySym = XK_F18; break;
899 case WXK_F19: keySym = XK_F19; break;
900 case WXK_F20: keySym = XK_F20; break;
901 case WXK_F21: keySym = XK_F21; break;
902 case WXK_F22: keySym = XK_F22; break;
903 case WXK_F23: keySym = XK_F23; break;
904 case WXK_F24: keySym = XK_F24; break;
905 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
906 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
907 default: keySym = id <= 255 ? (KeySym)id : 0;
908 }
909
910 return keySym;
911 }
912
913 // ----------------------------------------------------------------------------
914 // Some colour manipulation routines
915 // ----------------------------------------------------------------------------
916
917 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
918 {
919 int h = hsv->h;
920 int s = hsv->s;
921 int v = hsv->v;
922 int r = 0, g = 0, b = 0;
923 int i, f;
924 int p, q, t;
925 s = (s * wxMAX_RGB) / wxMAX_SV;
926 v = (v * wxMAX_RGB) / wxMAX_SV;
927 if (h == 360) h = 0;
928 if (s == 0) { h = 0; r = g = b = v; }
929 i = h / 60;
930 f = h % 60;
931 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
932 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
933 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
934 switch (i)
935 {
936 case 0: r = v, g = t, b = p; break;
937 case 1: r = q, g = v, b = p; break;
938 case 2: r = p, g = v, b = t; break;
939 case 3: r = p, g = q, b = v; break;
940 case 4: r = t, g = p, b = v; break;
941 case 5: r = v, g = p, b = q; break;
942 }
943 rgb->red = r << 8;
944 rgb->green = g << 8;
945 rgb->blue = b << 8;
946 }
947
948 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
949 {
950 int r = rgb->red >> 8;
951 int g = rgb->green >> 8;
952 int b = rgb->blue >> 8;
953 int maxv = wxMax3(r, g, b);
954 int minv = wxMin3(r, g, b);
955 int h = 0, s, v;
956 v = maxv;
957 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
958 else s = 0;
959 if (s == 0) h = 0;
960 else
961 {
962 int rc, gc, bc, hex = 0;
963 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
964 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
965 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
966 if (r == maxv) { h = bc - gc, hex = 0; }
967 else if (g == maxv) { h = rc - bc, hex = 2; }
968 else if (b == maxv) { h = gc - rc, hex = 4; }
969 h = hex * 60 + (h * 60 / wxMAX_RGB);
970 if (h < 0) h += 360;
971 }
972 hsv->h = h;
973 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
974 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
975 }
976
977 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
978 {
979 #if !wxUSE_NANOX
980 int llp;
981
982 int screen = DefaultScreen(d);
983 int num_colors = DisplayCells(d,screen);
984
985 XColor *color_defs = new XColor[num_colors];
986 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
987 XQueryColors(d,cmp,color_defs,num_colors);
988
989 wxHSV hsv_defs, hsv;
990 wxXColorToHSV(&hsv,xc);
991
992 int diff, min_diff = 0, pixel = 0;
993
994 for(llp = 0;llp < num_colors;llp++)
995 {
996 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
997 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
998 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
999 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
1000 if (llp == 0) min_diff = diff;
1001 if (min_diff > diff) { min_diff = diff; pixel = llp; }
1002 if (min_diff == 0) break;
1003 }
1004
1005 xc -> red = color_defs[pixel].red;
1006 xc -> green = color_defs[pixel].green;
1007 xc -> blue = color_defs[pixel].blue;
1008 xc -> flags = DoRed | DoGreen | DoBlue;
1009
1010 /* FIXME, TODO
1011 if (!XAllocColor(d,cmp,xc))
1012 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1013 */
1014
1015 delete[] color_defs;
1016 #endif
1017 }
1018
1019 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
1020 {
1021 if (!XAllocColor(d,cmp,xc))
1022 {
1023 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1024 wxAllocNearestColor(d,cmp,xc);
1025 }
1026 }
1027
1028 #ifdef __WXDEBUG__
1029 wxString wxGetXEventName(XEvent& event)
1030 {
1031 #if wxUSE_NANOX
1032 wxString str(wxT("(some event)"));
1033 return str;
1034 #else
1035 int type = event.xany.type;
1036 static char* event_name[] = {
1037 "", "unknown(-)", // 0-1
1038 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1039 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1040 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1041 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1042 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1043 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1044 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1045 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1046 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1047 "ClientMessage", "MappingNotify", // 33-34
1048 "unknown(+)"}; // 35
1049 type = wxMin(35, type); type = wxMax(1, type);
1050 wxString str(event_name[type]);
1051 return str;
1052 #endif
1053 }
1054 #endif
1055
1056 // ----------------------------------------------------------------------------
1057 // accelerators
1058 // ----------------------------------------------------------------------------
1059
1060 // Find the letter corresponding to the mnemonic, for Motif
1061 char wxFindMnemonic (const char *s)
1062 {
1063 char mnem = 0;
1064 int len = strlen (s);
1065 int i;
1066 for (i = 0; i < len; i++)
1067 {
1068 if (s[i] == '&')
1069 {
1070 // Carefully handle &&
1071 if ((i + 1) <= len && s[i + 1] == '&')
1072 i++;
1073 else
1074 {
1075 mnem = s[i + 1];
1076 break;
1077 }
1078 }
1079 }
1080 return mnem;
1081 }
1082
1083 char * wxFindAccelerator (const char *s)
1084 {
1085 // VZ: this function returns incorrect keysym which completely breaks kbd
1086 // handling
1087 return NULL;
1088
1089 #if 0
1090 // The accelerator text is after the \t char.
1091 while (*s && *s != '\t')
1092 s++;
1093 if (*s == '\0')
1094 return (NULL);
1095 s++;
1096 /*
1097 Now we need to format it as X standard:
1098
1099 input output
1100
1101 F7 --> <Key>F7
1102 Ctrl+N --> Ctrl<Key>N
1103 Alt+k --> Meta<Key>k
1104 Ctrl+Shift+A --> Ctrl Shift<Key>A
1105
1106 */
1107
1108 static char buf[256];
1109 buf[0] = '\0';
1110 char *tmp = copystring (s);
1111 s = tmp;
1112 char *p = tmp;
1113
1114 while (1)
1115 {
1116 while (*p && *p != '+')
1117 p++;
1118 if (*p)
1119 {
1120 *p = '\0';
1121 if (buf[0])
1122 strcat (buf, " ");
1123 if (strcmp (s, "Alt"))
1124 strcat (buf, s);
1125 else
1126 strcat (buf, "Meta");
1127 s = p++;
1128 }
1129 else
1130 {
1131 strcat (buf, "<Key>");
1132 strcat (buf, s);
1133 break;
1134 }
1135 }
1136 delete[]tmp;
1137 return buf;
1138 #endif
1139 }
1140
1141 XmString wxFindAcceleratorText (const char *s)
1142 {
1143 // VZ: this function returns incorrect keysym which completely breaks kbd
1144 // handling
1145 return NULL;
1146
1147 #if 0
1148 // The accelerator text is after the \t char.
1149 while (*s && *s != '\t')
1150 s++;
1151 if (*s == '\0')
1152 return (NULL);
1153 s++;
1154 XmString text = XmStringCreateSimple ((char *)s);
1155 return text;
1156 #endif
1157 }
1158
1159
1160 // Change a widget's foreground and background colours.
1161
1162 void wxDoChangeForegroundColour(WXWidget widget, wxColour& foregroundColour)
1163 {
1164 // When should we specify the foreground, if it's calculated
1165 // by wxComputeColours?
1166 // Solution: say we start with the default (computed) foreground colour.
1167 // If we call SetForegroundColour explicitly for a control or window,
1168 // then the foreground is changed.
1169 // Therefore SetBackgroundColour computes the foreground colour, and
1170 // SetForegroundColour changes the foreground colour. The ordering is
1171 // important.
1172
1173 XtVaSetValues ((Widget) widget,
1174 XmNforeground, foregroundColour.AllocColour(XtDisplay((Widget) widget)),
1175 NULL);
1176 }
1177
1178 void wxDoChangeBackgroundColour(WXWidget widget, wxColour& backgroundColour, bool changeArmColour)
1179 {
1180 wxComputeColours (XtDisplay((Widget) widget), & backgroundColour,
1181 (wxColour*) NULL);
1182
1183 XtVaSetValues ((Widget) widget,
1184 XmNbackground, g_itemColors[wxBACK_INDEX].pixel,
1185 XmNtopShadowColor, g_itemColors[wxTOPS_INDEX].pixel,
1186 XmNbottomShadowColor, g_itemColors[wxBOTS_INDEX].pixel,
1187 XmNforeground, g_itemColors[wxFORE_INDEX].pixel,
1188 NULL);
1189
1190 if (changeArmColour)
1191 XtVaSetValues ((Widget) widget,
1192 XmNarmColor, g_itemColors[wxSELE_INDEX].pixel,
1193 NULL);
1194 }
1195
1196 extern void wxDoChangeFont(WXWidget widget, wxFont& font)
1197 {
1198 // Lesstif 0.87 hangs here, but 0.93 does not
1199 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1200 Widget w = (Widget)widget;
1201 XtVaSetValues( w,
1202 wxFont::GetFontTag(), font.GetFontType( XtDisplay(w) ),
1203 NULL );
1204 #endif
1205
1206 }
1207
1208 bool wxWindowIsVisible(Window win)
1209 {
1210 XWindowAttributes wa;
1211 XGetWindowAttributes(wxGlobalDisplay(), win, &wa);
1212
1213 return (wa.map_state == IsViewable);
1214 }
1215
1216 wxString wxXmStringToString( const XmString& xmString )
1217 {
1218 char *txt;
1219 if( XmStringGetLtoR( xmString, XmSTRING_DEFAULT_CHARSET, &txt ) )
1220 {
1221 wxString str(txt);
1222 XtFree (txt);
1223 return str;
1224 }
1225
1226 return wxEmptyString;
1227 }
1228
1229 XmString wxStringToXmString( const wxString& str )
1230 {
1231 return XmStringCreateLtoR((char *)str.c_str(), XmSTRING_DEFAULT_CHARSET);
1232 }
1233
1234 XmString wxStringToXmString( const char* str )
1235 {
1236 return XmStringCreateLtoR((char *)str, XmSTRING_DEFAULT_CHARSET);
1237 }