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