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