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