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