]> git.saurik.com Git - wxWidgets.git/blame - src/x11/utils.cpp
use wxHashMap, not wxHashTable in wxXPMDecoder
[wxWidgets.git] / src / x11 / utils.cpp
CommitLineData
83df96d6
JS
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
83df96d6
JS
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
bc797f4c 47
83df96d6
JS
48#ifdef __VMS__
49#pragma message enable nosimpint
50#endif
51
52#include "wx/unix/execute.h"
53
bc797f4c 54#include "wx/x11/private.h"
7266b672
JS
55#include "X11/Xresource.h"
56#include "X11/Xutil.h"
83df96d6
JS
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
70static 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
81void wxFlushEvents()
82{
83 Display *display = (Display*) wxGetDisplay();
84
85 XSync (display, FALSE);
7266b672 86 XFlush(display);
83df96d6 87
7266b672
JS
88 // TODO
89#if 0
90
83df96d6
JS
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 }
7266b672 100#endif
83df96d6
JS
101}
102
103// Check whether this window wants to process messages, e.g. Stop button
104// in long calculations.
105bool wxCheckForInterrupt(wxWindow *wnd)
106{
bc797f4c
JS
107 wxASSERT_MSG(FALSE, "wxCheckForInterrupt not yet implemented.");
108 return FALSE;
109#if 0
83df96d6
JS
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;
bc797f4c 136#endif
83df96d6
JS
137}
138
139// ----------------------------------------------------------------------------
140// wxExecute stuff
141// ----------------------------------------------------------------------------
bc797f4c 142#if 0
83df96d6
JS
143static 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}
8354aa92 155#endif
83df96d6
JS
156
157int wxAddProcessCallback(wxEndProcessData *proc_data, int fd)
158{
8354aa92 159#if 0
83df96d6
JS
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;
bc797f4c 167#endif
8354aa92 168}
83df96d6
JS
169
170// ----------------------------------------------------------------------------
171// misc
172// ----------------------------------------------------------------------------
173
174// Emit a beeeeeep
175void wxBell()
176{
177 // Use current setting for the bell
178 XBell ((Display*) wxGetDisplay(), 0);
179}
180
181int wxGetOsVersion(int *majorVsn, int *minorVsn)
182{
bc797f4c
JS
183 if (majorVsn)
184 *majorVsn = 0;
185 if (minorVsn)
186 *minorVsn = 0;
187 return wxX11;
83df96d6
JS
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.
197static 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
226static 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
257wxList wxResourceCache (wxKEY_STRING);
258
259void
260wxFlushResources (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
280static XrmDatabase wxResourceDatabase = 0;
281
282void wxXMergeDatabases (wxApp * theApp, Display * display);
283
284bool 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
309bool 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
316bool 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
323bool 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
330bool 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
388bool 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
401bool 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
414bool 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
437void 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*/
493void
494wxSetDefaultResources (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// busy cursor stuff
535// ----------------------------------------------------------------------------
536
537static int wxBusyCursorCount = 0;
538
539// Helper function
540static void
541wxXSetBusyCursor (wxWindow * win, wxCursor * cursor)
542{
543 Display *display = (Display*) win->GetXDisplay();
544
545 Window xwin = (Window) win->GetXWindow();
546 if (!xwin)
547 return;
548
549 XSetWindowAttributes attrs;
550
551 if (cursor)
552 {
553 attrs.cursor = (Cursor) cursor->GetXCursor(display);
554 }
555 else
556 {
557 // Restore old cursor
558 if (win->GetCursor().Ok())
559 attrs.cursor = (Cursor) win->GetCursor().GetXCursor(display);
560 else
561 attrs.cursor = None;
562 }
563 if (xwin)
564 XChangeWindowAttributes (display, xwin, CWCursor, &attrs);
565
566 XFlush (display);
567
568 for(wxNode *node = win->GetChildren().First (); node; node = node->Next())
569 {
570 wxWindow *child = (wxWindow *) node->Data ();
571 wxXSetBusyCursor (child, cursor);
572 }
573}
574
575// Set the cursor to the busy cursor for all windows
576void wxBeginBusyCursor(wxCursor *cursor)
577{
578 wxBusyCursorCount++;
579 if (wxBusyCursorCount == 1)
580 {
581 for(wxNode *node = wxTopLevelWindows.First (); node; node = node->Next())
582 {
583 wxWindow *win = (wxWindow *) node->Data ();
584 wxXSetBusyCursor (win, cursor);
585 }
586 }
587}
588
589// Restore cursor to normal
590void wxEndBusyCursor()
591{
592 if (wxBusyCursorCount == 0)
593 return;
594
595 wxBusyCursorCount--;
596 if (wxBusyCursorCount == 0)
597 {
598 for(wxNode *node = wxTopLevelWindows.First (); node; node = node->Next())
599 {
600 wxWindow *win = (wxWindow *) node->Data ();
601 wxXSetBusyCursor (win, NULL);
602 }
603 }
604}
605
606// TRUE if we're between the above two calls
607bool wxIsBusy()
608{
609 return (wxBusyCursorCount > 0);
610}
611
612// ----------------------------------------------------------------------------
613// display info
614// ----------------------------------------------------------------------------
615
616void wxGetMousePosition( int* x, int* y )
617{
618 XMotionEvent xev;
619 Window root, child;
620 XQueryPointer((Display*) wxGetDisplay(),
621 DefaultRootWindow((Display*) wxGetDisplay()),
622 &root, &child,
623 &(xev.x_root), &(xev.y_root),
624 &(xev.x), &(xev.y),
625 &(xev.state));
626 *x = xev.x_root;
627 *y = xev.y_root;
628};
629
630// Return TRUE if we have a colour display
631bool wxColourDisplay()
632{
633 return wxDisplayDepth() > 1;
634}
635
636// Returns depth of screen
637int wxDisplayDepth()
638{
639 Display *dpy = (Display*) wxGetDisplay();
640
641 return DefaultDepth (dpy, DefaultScreen (dpy));
642}
643
644// Get size of display
645void wxDisplaySize(int *width, int *height)
646{
647 Display *dpy = (Display*) wxGetDisplay();
648
649 if ( width )
650 *width = DisplayWidth (dpy, DefaultScreen (dpy));
651 if ( height )
652 *height = DisplayHeight (dpy, DefaultScreen (dpy));
653}
654
655void wxDisplaySizeMM(int *width, int *height)
656{
657 Display *dpy = (Display*) wxGetDisplay();
658
659 if ( width )
660 *width = DisplayWidthMM(dpy, DefaultScreen (dpy));
661 if ( height )
662 *height = DisplayHeightMM(dpy, DefaultScreen (dpy));
663}
664
665void wxClientDisplayRect(int *x, int *y, int *width, int *height)
666{
667 // This is supposed to return desktop dimensions minus any window
668 // manager panels, menus, taskbars, etc. If there is a way to do that
669 // for this platform please fix this function, otherwise it defaults
670 // to the entire desktop.
671 if (x) *x = 0;
672 if (y) *y = 0;
673 wxDisplaySize(width, height);
674}
675
676
bc797f4c 677// Configurable display in X11
83df96d6
JS
678static WXDisplay *gs_currentDisplay = NULL;
679static wxString gs_displayName;
680
681WXDisplay *wxGetDisplay()
682{
683 if (gs_currentDisplay)
684 return gs_currentDisplay;
83df96d6
JS
685 else if (wxTheApp)
686 return wxTheApp->GetInitialDisplay();
687 else
688 return (WXDisplay*) NULL;
689}
690
691bool wxSetDisplay(const wxString& display_name)
692{
693 gs_displayName = display_name;
694
bc797f4c 695 if ( display_name.IsEmpty() )
83df96d6
JS
696 {
697 gs_currentDisplay = NULL;
698
699 return TRUE;
700 }
701 else
702 {
bc797f4c 703 Display* display = XOpenDisplay((const char*) display_name);
83df96d6
JS
704
705 if (display)
706 {
707 gs_currentDisplay = (WXDisplay*) display;
708 return TRUE;
709 }
710 else
711 return FALSE;
712 }
713}
714
715wxString wxGetDisplayName()
716{
717 return gs_displayName;
718}
719
720wxWindow* wxFindWindowAtPoint(const wxPoint& pt)
721{
722 return wxGenericFindWindowAtPoint(pt);
723}
724
725// ----------------------------------------------------------------------------
726// accelerators
727// ----------------------------------------------------------------------------
728
729// Find the letter corresponding to the mnemonic, for Motif
730char wxFindMnemonic (const char *s)
731{
732 char mnem = 0;
733 int len = strlen (s);
734 int i;
735 for (i = 0; i < len; i++)
736 {
737 if (s[i] == '&')
738 {
739 // Carefully handle &&
740 if ((i + 1) <= len && s[i + 1] == '&')
741 i++;
742 else
743 {
744 mnem = s[i + 1];
745 break;
746 }
747 }
748 }
749 return mnem;
750}
751
752char * wxFindAccelerator (const char *s)
753{
754 // VZ: this function returns incorrect keysym which completely breaks kbd
755 // handling
756 return NULL;
757
758#if 0
759 // The accelerator text is after the \t char.
760 while (*s && *s != '\t')
761 s++;
762 if (*s == '\0')
763 return (NULL);
764 s++;
765 /*
766 Now we need to format it as X standard:
767
768 input output
769
770 F7 --> <Key>F7
771 Ctrl+N --> Ctrl<Key>N
772 Alt+k --> Meta<Key>k
773 Ctrl+Shift+A --> Ctrl Shift<Key>A
774
775 */
776
777 wxBuffer[0] = '\0';
778 char *tmp = copystring (s);
779 s = tmp;
780 char *p = tmp;
781
782 while (1)
783 {
784 while (*p && *p != '+')
785 p++;
786 if (*p)
787 {
788 *p = '\0';
789 if (wxBuffer[0])
790 strcat (wxBuffer, " ");
791 if (strcmp (s, "Alt"))
792 strcat (wxBuffer, s);
793 else
794 strcat (wxBuffer, "Meta");
795 s = p++;
796 }
797 else
798 {
799 strcat (wxBuffer, "<Key>");
800 strcat (wxBuffer, s);
801 break;
802 }
803 }
804 delete[]tmp;
805 return wxBuffer;
806#endif
807}
808
83df96d6
JS
809// ----------------------------------------------------------------------------
810// keycode translations
811// ----------------------------------------------------------------------------
812
813#include <X11/keysym.h>
814
815// FIXME what about tables??
816
817int wxCharCodeXToWX(KeySym keySym)
818{
819 int id;
820 switch (keySym)
821 {
822 case XK_Shift_L:
823 case XK_Shift_R:
824 id = WXK_SHIFT; break;
825 case XK_Control_L:
826 case XK_Control_R:
827 id = WXK_CONTROL; break;
828 case XK_BackSpace:
829 id = WXK_BACK; break;
830 case XK_Delete:
831 id = WXK_DELETE; break;
832 case XK_Clear:
833 id = WXK_CLEAR; break;
834 case XK_Tab:
835 id = WXK_TAB; break;
836 case XK_numbersign:
837 id = '#'; break;
838 case XK_Return:
839 id = WXK_RETURN; break;
840 case XK_Escape:
841 id = WXK_ESCAPE; break;
842 case XK_Pause:
843 case XK_Break:
844 id = WXK_PAUSE; break;
845 case XK_Num_Lock:
846 id = WXK_NUMLOCK; break;
847 case XK_Scroll_Lock:
848 id = WXK_SCROLL; break;
849
850 case XK_Home:
851 id = WXK_HOME; break;
852 case XK_End:
853 id = WXK_END; break;
854 case XK_Left:
855 id = WXK_LEFT; break;
856 case XK_Right:
857 id = WXK_RIGHT; break;
858 case XK_Up:
859 id = WXK_UP; break;
860 case XK_Down:
861 id = WXK_DOWN; break;
862 case XK_Next:
863 id = WXK_NEXT; break;
864 case XK_Prior:
865 id = WXK_PRIOR; break;
866 case XK_Menu:
867 id = WXK_MENU; break;
868 case XK_Select:
869 id = WXK_SELECT; break;
870 case XK_Cancel:
871 id = WXK_CANCEL; break;
872 case XK_Print:
873 id = WXK_PRINT; break;
874 case XK_Execute:
875 id = WXK_EXECUTE; break;
876 case XK_Insert:
877 id = WXK_INSERT; break;
878 case XK_Help:
879 id = WXK_HELP; break;
880
881 case XK_KP_Multiply:
882 id = WXK_MULTIPLY; break;
883 case XK_KP_Add:
884 id = WXK_ADD; break;
885 case XK_KP_Subtract:
886 id = WXK_SUBTRACT; break;
887 case XK_KP_Divide:
888 id = WXK_DIVIDE; break;
889 case XK_KP_Decimal:
890 id = WXK_DECIMAL; break;
891 case XK_KP_Equal:
892 id = '='; break;
893 case XK_KP_Space:
894 id = ' '; break;
895 case XK_KP_Tab:
896 id = WXK_TAB; break;
897 case XK_KP_Enter:
898 id = WXK_RETURN; break;
899 case XK_KP_0:
900 id = WXK_NUMPAD0; break;
901 case XK_KP_1:
902 id = WXK_NUMPAD1; break;
903 case XK_KP_2:
904 id = WXK_NUMPAD2; break;
905 case XK_KP_3:
906 id = WXK_NUMPAD3; break;
907 case XK_KP_4:
908 id = WXK_NUMPAD4; break;
909 case XK_KP_5:
910 id = WXK_NUMPAD5; break;
911 case XK_KP_6:
912 id = WXK_NUMPAD6; break;
913 case XK_KP_7:
914 id = WXK_NUMPAD7; break;
915 case XK_KP_8:
916 id = WXK_NUMPAD8; break;
917 case XK_KP_9:
918 id = WXK_NUMPAD9; break;
919 case XK_F1:
920 id = WXK_F1; break;
921 case XK_F2:
922 id = WXK_F2; break;
923 case XK_F3:
924 id = WXK_F3; break;
925 case XK_F4:
926 id = WXK_F4; break;
927 case XK_F5:
928 id = WXK_F5; break;
929 case XK_F6:
930 id = WXK_F6; break;
931 case XK_F7:
932 id = WXK_F7; break;
933 case XK_F8:
934 id = WXK_F8; break;
935 case XK_F9:
936 id = WXK_F9; break;
937 case XK_F10:
938 id = WXK_F10; break;
939 case XK_F11:
940 id = WXK_F11; break;
941 case XK_F12:
942 id = WXK_F12; break;
943 case XK_F13:
944 id = WXK_F13; break;
945 case XK_F14:
946 id = WXK_F14; break;
947 case XK_F15:
948 id = WXK_F15; break;
949 case XK_F16:
950 id = WXK_F16; break;
951 case XK_F17:
952 id = WXK_F17; break;
953 case XK_F18:
954 id = WXK_F18; break;
955 case XK_F19:
956 id = WXK_F19; break;
957 case XK_F20:
958 id = WXK_F20; break;
959 case XK_F21:
960 id = WXK_F21; break;
961 case XK_F22:
962 id = WXK_F22; break;
963 case XK_F23:
964 id = WXK_F23; break;
965 case XK_F24:
966 id = WXK_F24; break;
967 default:
968 id = (keySym <= 255) ? (int)keySym : -1;
969 }
970
971 return id;
972}
973
974KeySym wxCharCodeWXToX(int id)
975{
976 KeySym keySym;
977
978 switch (id)
979 {
980 case WXK_CANCEL: keySym = XK_Cancel; break;
981 case WXK_BACK: keySym = XK_BackSpace; break;
982 case WXK_TAB: keySym = XK_Tab; break;
983 case WXK_CLEAR: keySym = XK_Clear; break;
984 case WXK_RETURN: keySym = XK_Return; break;
985 case WXK_SHIFT: keySym = XK_Shift_L; break;
986 case WXK_CONTROL: keySym = XK_Control_L; break;
987 case WXK_MENU : keySym = XK_Menu; break;
988 case WXK_PAUSE: keySym = XK_Pause; break;
989 case WXK_ESCAPE: keySym = XK_Escape; break;
990 case WXK_SPACE: keySym = ' '; break;
991 case WXK_PRIOR: keySym = XK_Prior; break;
992 case WXK_NEXT : keySym = XK_Next; break;
993 case WXK_END: keySym = XK_End; break;
994 case WXK_HOME : keySym = XK_Home; break;
995 case WXK_LEFT : keySym = XK_Left; break;
996 case WXK_UP: keySym = XK_Up; break;
997 case WXK_RIGHT: keySym = XK_Right; break;
998 case WXK_DOWN : keySym = XK_Down; break;
999 case WXK_SELECT: keySym = XK_Select; break;
1000 case WXK_PRINT: keySym = XK_Print; break;
1001 case WXK_EXECUTE: keySym = XK_Execute; break;
1002 case WXK_INSERT: keySym = XK_Insert; break;
1003 case WXK_DELETE: keySym = XK_Delete; break;
1004 case WXK_HELP : keySym = XK_Help; break;
1005 case WXK_NUMPAD0: keySym = XK_KP_0; break;
1006 case WXK_NUMPAD1: keySym = XK_KP_1; break;
1007 case WXK_NUMPAD2: keySym = XK_KP_2; break;
1008 case WXK_NUMPAD3: keySym = XK_KP_3; break;
1009 case WXK_NUMPAD4: keySym = XK_KP_4; break;
1010 case WXK_NUMPAD5: keySym = XK_KP_5; break;
1011 case WXK_NUMPAD6: keySym = XK_KP_6; break;
1012 case WXK_NUMPAD7: keySym = XK_KP_7; break;
1013 case WXK_NUMPAD8: keySym = XK_KP_8; break;
1014 case WXK_NUMPAD9: keySym = XK_KP_9; break;
1015 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
1016 case WXK_ADD: keySym = XK_KP_Add; break;
1017 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
1018 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
1019 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
1020 case WXK_F1: keySym = XK_F1; break;
1021 case WXK_F2: keySym = XK_F2; break;
1022 case WXK_F3: keySym = XK_F3; break;
1023 case WXK_F4: keySym = XK_F4; break;
1024 case WXK_F5: keySym = XK_F5; break;
1025 case WXK_F6: keySym = XK_F6; break;
1026 case WXK_F7: keySym = XK_F7; break;
1027 case WXK_F8: keySym = XK_F8; break;
1028 case WXK_F9: keySym = XK_F9; break;
1029 case WXK_F10: keySym = XK_F10; break;
1030 case WXK_F11: keySym = XK_F11; break;
1031 case WXK_F12: keySym = XK_F12; break;
1032 case WXK_F13: keySym = XK_F13; break;
1033 case WXK_F14: keySym = XK_F14; break;
1034 case WXK_F15: keySym = XK_F15; break;
1035 case WXK_F16: keySym = XK_F16; break;
1036 case WXK_F17: keySym = XK_F17; break;
1037 case WXK_F18: keySym = XK_F18; break;
1038 case WXK_F19: keySym = XK_F19; break;
1039 case WXK_F20: keySym = XK_F20; break;
1040 case WXK_F21: keySym = XK_F21; break;
1041 case WXK_F22: keySym = XK_F22; break;
1042 case WXK_F23: keySym = XK_F23; break;
1043 case WXK_F24: keySym = XK_F24; break;
1044 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
1045 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
1046 default: keySym = id <= 255 ? (KeySym)id : 0;
1047 }
1048
1049 return keySym;
1050}
1051
1052// ----------------------------------------------------------------------------
1053// Some colour manipulation routines
1054// ----------------------------------------------------------------------------
1055
1056void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
1057{
1058 int h = hsv->h;
1059 int s = hsv->s;
1060 int v = hsv->v;
1061 int r = 0, g = 0, b = 0;
1062 int i, f;
1063 int p, q, t;
1064 s = (s * wxMAX_RGB) / wxMAX_SV;
1065 v = (v * wxMAX_RGB) / wxMAX_SV;
1066 if (h == 360) h = 0;
1067 if (s == 0) { h = 0; r = g = b = v; }
1068 i = h / 60;
1069 f = h % 60;
1070 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
1071 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
1072 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
1073 switch (i)
1074 {
1075 case 0: r = v, g = t, b = p; break;
1076 case 1: r = q, g = v, b = p; break;
1077 case 2: r = p, g = v, b = t; break;
1078 case 3: r = p, g = q, b = v; break;
1079 case 4: r = t, g = p, b = v; break;
1080 case 5: r = v, g = p, b = q; break;
1081 }
1082 rgb->red = r << 8;
1083 rgb->green = g << 8;
1084 rgb->blue = b << 8;
1085}
1086
1087void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
1088{
1089 int r = rgb->red >> 8;
1090 int g = rgb->green >> 8;
1091 int b = rgb->blue >> 8;
1092 int maxv = wxMax3(r, g, b);
1093 int minv = wxMin3(r, g, b);
1094 int h = 0, s, v;
1095 v = maxv;
1096 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
1097 else s = 0;
1098 if (s == 0) h = 0;
1099 else
1100 {
1101 int rc, gc, bc, hex = 0;
1102 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
1103 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
1104 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
1105 if (r == maxv) { h = bc - gc, hex = 0; }
1106 else if (g == maxv) { h = rc - bc, hex = 2; }
1107 else if (b == maxv) { h = gc - rc, hex = 4; }
1108 h = hex * 60 + (h * 60 / wxMAX_RGB);
1109 if (h < 0) h += 360;
1110 }
1111 hsv->h = h;
1112 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
1113 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
1114}
1115
1116void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
1117{
1118 int llp;
1119
1120 int screen = DefaultScreen(d);
1121 int num_colors = DisplayCells(d,screen);
1122
1123 XColor *color_defs = new XColor[num_colors];
1124 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
1125 XQueryColors(d,cmp,color_defs,num_colors);
1126
1127 wxHSV hsv_defs, hsv;
1128 wxXColorToHSV(&hsv,xc);
1129
1130 int diff, min_diff = 0, pixel = 0;
1131
1132 for(llp = 0;llp < num_colors;llp++)
1133 {
1134 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
1135 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
1136 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
1137 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
1138 if (llp == 0) min_diff = diff;
1139 if (min_diff > diff) { min_diff = diff; pixel = llp; }
1140 if (min_diff == 0) break;
1141 }
1142
1143 xc -> red = color_defs[pixel].red;
1144 xc -> green = color_defs[pixel].green;
1145 xc -> blue = color_defs[pixel].blue;
1146 xc -> flags = DoRed | DoGreen | DoBlue;
1147
1148/* FIXME, TODO
1149 if (!XAllocColor(d,cmp,xc))
1150 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1151*/
1152
1153 delete[] color_defs;
1154}
1155
1156void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
1157{
1158 if (!XAllocColor(d,cmp,xc))
1159 {
1160 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1161 wxAllocNearestColor(d,cmp,xc);
1162 }
1163}
1164