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