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