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