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