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