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