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