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