]> git.saurik.com Git - wxWidgets.git/blob - src/motif/utils.cpp
More Motif additions: mdi and sashtest samples now just about work!
[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 char *wxGetUserHome (const wxString& user)
619 {
620 #ifdef VMS
621 return(NULL);
622 #else
623 struct passwd *who = NULL;
624
625 if (user == "") {
626 register char *ptr;
627
628 if ((ptr = getenv("HOME")) != NULL)
629 return ptr;
630 if ((ptr = getenv("USER")) != NULL ||
631 (ptr = getenv("LOGNAME")) != NULL)
632 {
633 who = getpwnam( ptr );
634 }
635 // We now make sure the the user exists!
636 if (who == NULL)
637 who = getpwuid( getuid() );
638 } else
639 who = getpwnam ((const char*) user);
640
641 return who ? who->pw_dir : (char*) NULL;
642 #endif
643 // ifdef VMS
644 }
645
646 // Check whether this window wants to process messages, e.g. Stop button
647 // in long calculations.
648 bool wxCheckForInterrupt(wxWindow *wnd)
649 {
650 if(wnd){
651 Display *dpy=(Display*) wnd->GetXDisplay();
652 Window win=(Window) wnd->GetXWindow();
653 XEvent event;
654 XFlush(dpy);
655 if(wnd->GetMainWidget()){
656 XmUpdateDisplay((Widget)(wnd->GetMainWidget()));
657 }
658 while(XCheckMaskEvent(dpy,
659 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask|
660 PointerMotionMask|KeyPressMask|KeyReleaseMask,
661 &event)){
662 if(event.xany.window==win)
663 XtDispatchEvent(&event);
664 // else
665 // XBell(dpy,50);
666 }
667 return TRUE;//*** temporary?
668 }
669 else{
670 wxMessageBox("wnd==NULL !!!");
671 return FALSE;//*** temporary?
672 }
673 }
674
675 void wxGetMousePosition( int* x, int* y )
676 {
677 XMotionEvent xev;
678 Window root, child;
679 XQueryPointer((Display*) wxGetDisplay(),
680 DefaultRootWindow((Display*) wxGetDisplay()), &root, &child,
681 &(xev.x_root), &(xev.y_root),
682 &(xev.x), &(xev.y),
683 &(xev.state));
684 *x = xev.x_root;
685 *y = xev.y_root;
686 };
687
688 // Return TRUE if we have a colour display
689 bool wxColourDisplay()
690 {
691 Display *dpy = (Display*) wxGetDisplay();
692
693 if (DefaultDepth (dpy, DefaultScreen (dpy)) < 2)
694 return FALSE;
695 else
696 return TRUE;
697 }
698
699 // Returns depth of screen
700 int wxDisplayDepth()
701 {
702 Display *dpy = (Display*) wxGetDisplay();
703 return DefaultDepth (dpy, DefaultScreen (dpy));
704 }
705
706 // Get size of display
707 void wxDisplaySize(int *width, int *height)
708 {
709 Display *dpy = (Display*) wxGetDisplay();
710
711 *width = DisplayWidth (dpy, DefaultScreen (dpy));
712 *height = DisplayHeight (dpy, DefaultScreen (dpy));
713 }
714
715 /* Configurable display in Motif */
716 static WXDisplay *gs_currentDisplay = NULL;
717 static wxString gs_displayName;
718
719 WXDisplay *wxGetDisplay()
720 {
721 if (gs_currentDisplay)
722 return gs_currentDisplay;
723
724 if (wxTheApp && wxTheApp->GetTopLevelWidget())
725 return XtDisplay ((Widget) wxTheApp->GetTopLevelWidget());
726 else if (wxTheApp)
727 return wxTheApp->GetInitialDisplay();
728 else
729 return (WXDisplay*) NULL;
730 }
731
732 bool wxSetDisplay(const wxString& display_name)
733 {
734 gs_displayName = display_name;
735
736 if (display_name.IsNull() || display_name.IsEmpty())
737 {
738 gs_currentDisplay = NULL;
739 return TRUE;
740 }
741 else
742 {
743 Cardinal argc = 0;
744
745 Display *display = XtOpenDisplay((XtAppContext) wxTheApp->GetAppContext(),
746 (const char*) display_name,
747 (const char*) wxTheApp->GetAppName(),
748 (const char*) wxTheApp->GetClassName(),
749 NULL,
750 # if XtSpecificationRelease < 5
751 0, &argc, NULL);
752 # else
753 0, (int *)&argc, NULL);
754 # endif
755
756 if (display)
757 {
758 gs_currentDisplay = (WXDisplay*) display;
759 return TRUE;
760 } else
761 return FALSE;
762 }
763 return FALSE;
764 }
765
766 wxString wxGetDisplayName()
767 {
768 return gs_displayName;
769 }
770
771 // Find the letter corresponding to the mnemonic, for Motif
772 char wxFindMnemonic (const char *s)
773 {
774 char mnem = 0;
775 int len = strlen (s);
776 int i;
777 for (i = 0; i < len; i++)
778 {
779 if (s[i] == '&')
780 {
781 // Carefully handle &&
782 if ((i + 1) <= len && s[i + 1] == '&')
783 i++;
784 else
785 {
786 mnem = s[i + 1];
787 break;
788 }
789 }
790 }
791 return mnem;
792 }
793
794 char * wxFindAccelerator (char *s)
795 {
796 // The accelerator text is after the \t char.
797 while (*s && *s != '\t')
798 s++;
799 if (*s == '\0')
800 return (NULL);
801 s++;
802 /*
803 Now we need to format it as X standard:
804
805 input output
806
807 F7 --> <Key>F7
808 Ctrl+N --> Ctrl<Key>N
809 Alt+k --> Meta<Key>k
810 Ctrl+Shift+A --> Ctrl Shift<Key>A
811
812 */
813
814 wxBuffer[0] = '\0';
815 char *tmp = copystring (s);
816 s = tmp;
817 char *p = s;
818
819 while (1)
820 {
821 while (*p && *p != '+')
822 p++;
823 if (*p)
824 {
825 *p = '\0';
826 if (wxBuffer[0])
827 strcat (wxBuffer, " ");
828 if (strcmp (s, "Alt"))
829 strcat (wxBuffer, s);
830 else
831 strcat (wxBuffer, "Meta");
832 s = p + 1;
833 p = s;
834 }
835 else
836 {
837 strcat (wxBuffer, "<Key>");
838 strcat (wxBuffer, s);
839 break;
840 }
841 }
842 delete[]tmp;
843 return wxBuffer;
844 }
845
846 XmString wxFindAcceleratorText (char *s)
847 {
848 // The accelerator text is after the \t char.
849 while (*s && *s != '\t')
850 s++;
851 if (*s == '\0')
852 return (NULL);
853 s++;
854 XmString text = XmStringCreateSimple (s);
855 return text;
856 }
857
858 #include <X11/keysym.h>
859
860 int wxCharCodeXToWX(KeySym keySym)
861 {
862 int id;
863 switch (keySym) {
864 case XK_Shift_L:
865 case XK_Shift_R:
866 id = WXK_SHIFT; break;
867 case XK_Control_L:
868 case XK_Control_R:
869 id = WXK_CONTROL; break;
870 case XK_BackSpace:
871 id = WXK_BACK; break;
872 case XK_Delete:
873 id = WXK_DELETE; break;
874 case XK_Clear:
875 id = WXK_CLEAR; break;
876 case XK_Tab:
877 id = WXK_TAB; break;
878 case XK_numbersign:
879 id = '#'; break;
880 case XK_Return:
881 id = WXK_RETURN; break;
882 case XK_Escape:
883 id = WXK_ESCAPE; break;
884 case XK_Pause:
885 case XK_Break:
886 id = WXK_PAUSE; break;
887 case XK_Num_Lock:
888 id = WXK_NUMLOCK; break;
889 case XK_Scroll_Lock:
890 id = WXK_SCROLL; break;
891
892 case XK_Home:
893 id = WXK_HOME; break;
894 case XK_End:
895 id = WXK_END; break;
896 case XK_Left:
897 id = WXK_LEFT; break;
898 case XK_Right:
899 id = WXK_RIGHT; break;
900 case XK_Up:
901 id = WXK_UP; break;
902 case XK_Down:
903 id = WXK_DOWN; break;
904 case XK_Next:
905 id = WXK_NEXT; break;
906 case XK_Prior:
907 id = WXK_PRIOR; break;
908 case XK_Menu:
909 id = WXK_MENU; break;
910 case XK_Select:
911 id = WXK_SELECT; break;
912 case XK_Cancel:
913 id = WXK_CANCEL; break;
914 case XK_Print:
915 id = WXK_PRINT; break;
916 case XK_Execute:
917 id = WXK_EXECUTE; break;
918 case XK_Insert:
919 id = WXK_INSERT; break;
920 case XK_Help:
921 id = WXK_HELP; break;
922
923 case XK_KP_Multiply:
924 id = WXK_MULTIPLY; break;
925 case XK_KP_Add:
926 id = WXK_ADD; break;
927 case XK_KP_Subtract:
928 id = WXK_SUBTRACT; break;
929 case XK_KP_Divide:
930 id = WXK_DIVIDE; break;
931 case XK_KP_Decimal:
932 id = WXK_DECIMAL; break;
933 case XK_KP_Equal:
934 id = '='; break;
935 case XK_KP_Space:
936 id = ' '; break;
937 case XK_KP_Tab:
938 id = WXK_TAB; break;
939 case XK_KP_Enter:
940 id = WXK_RETURN; break;
941 case XK_KP_0:
942 id = WXK_NUMPAD0; break;
943 case XK_KP_1:
944 id = WXK_NUMPAD1; break;
945 case XK_KP_2:
946 id = WXK_NUMPAD2; break;
947 case XK_KP_3:
948 id = WXK_NUMPAD3; break;
949 case XK_KP_4:
950 id = WXK_NUMPAD4; break;
951 case XK_KP_5:
952 id = WXK_NUMPAD5; break;
953 case XK_KP_6:
954 id = WXK_NUMPAD6; break;
955 case XK_KP_7:
956 id = WXK_NUMPAD7; break;
957 case XK_KP_8:
958 id = WXK_NUMPAD8; break;
959 case XK_KP_9:
960 id = WXK_NUMPAD9; break;
961 case XK_F1:
962 id = WXK_F1; break;
963 case XK_F2:
964 id = WXK_F2; break;
965 case XK_F3:
966 id = WXK_F3; break;
967 case XK_F4:
968 id = WXK_F4; break;
969 case XK_F5:
970 id = WXK_F5; break;
971 case XK_F6:
972 id = WXK_F6; break;
973 case XK_F7:
974 id = WXK_F7; break;
975 case XK_F8:
976 id = WXK_F8; break;
977 case XK_F9:
978 id = WXK_F9; break;
979 case XK_F10:
980 id = WXK_F10; break;
981 case XK_F11:
982 id = WXK_F11; break;
983 case XK_F12:
984 id = WXK_F12; break;
985 case XK_F13:
986 id = WXK_F13; break;
987 case XK_F14:
988 id = WXK_F14; break;
989 case XK_F15:
990 id = WXK_F15; break;
991 case XK_F16:
992 id = WXK_F16; break;
993 case XK_F17:
994 id = WXK_F17; break;
995 case XK_F18:
996 id = WXK_F18; break;
997 case XK_F19:
998 id = WXK_F19; break;
999 case XK_F20:
1000 id = WXK_F20; break;
1001 case XK_F21:
1002 id = WXK_F21; break;
1003 case XK_F22:
1004 id = WXK_F22; break;
1005 case XK_F23:
1006 id = WXK_F23; break;
1007 case XK_F24:
1008 id = WXK_F24; break;
1009 default:
1010 id = (keySym <= 255) ? (int)keySym : -1;
1011 } // switch
1012 return id;
1013 }
1014
1015 KeySym wxCharCodeWXToX(int id)
1016 {
1017 KeySym keySym;
1018
1019 switch (id) {
1020 case WXK_CANCEL: keySym = XK_Cancel; break;
1021 case WXK_BACK: keySym = XK_BackSpace; break;
1022 case WXK_TAB: keySym = XK_Tab; break;
1023 case WXK_CLEAR: keySym = XK_Clear; break;
1024 case WXK_RETURN: keySym = XK_Return; break;
1025 case WXK_SHIFT: keySym = XK_Shift_L; break;
1026 case WXK_CONTROL: keySym = XK_Control_L; break;
1027 case WXK_MENU : keySym = XK_Menu; break;
1028 case WXK_PAUSE: keySym = XK_Pause; break;
1029 case WXK_ESCAPE: keySym = XK_Escape; break;
1030 case WXK_SPACE: keySym = ' '; break;
1031 case WXK_PRIOR: keySym = XK_Prior; break;
1032 case WXK_NEXT : keySym = XK_Next; break;
1033 case WXK_END: keySym = XK_End; break;
1034 case WXK_HOME : keySym = XK_Home; break;
1035 case WXK_LEFT : keySym = XK_Left; break;
1036 case WXK_UP: keySym = XK_Up; break;
1037 case WXK_RIGHT: keySym = XK_Right; break;
1038 case WXK_DOWN : keySym = XK_Down; break;
1039 case WXK_SELECT: keySym = XK_Select; break;
1040 case WXK_PRINT: keySym = XK_Print; break;
1041 case WXK_EXECUTE: keySym = XK_Execute; break;
1042 case WXK_INSERT: keySym = XK_Insert; break;
1043 case WXK_DELETE: keySym = XK_Delete; break;
1044 case WXK_HELP : keySym = XK_Help; break;
1045 case WXK_NUMPAD0: keySym = XK_KP_0; break;
1046 case WXK_NUMPAD1: keySym = XK_KP_1; break;
1047 case WXK_NUMPAD2: keySym = XK_KP_2; break;
1048 case WXK_NUMPAD3: keySym = XK_KP_3; break;
1049 case WXK_NUMPAD4: keySym = XK_KP_4; break;
1050 case WXK_NUMPAD5: keySym = XK_KP_5; break;
1051 case WXK_NUMPAD6: keySym = XK_KP_6; break;
1052 case WXK_NUMPAD7: keySym = XK_KP_7; break;
1053 case WXK_NUMPAD8: keySym = XK_KP_8; break;
1054 case WXK_NUMPAD9: keySym = XK_KP_9; break;
1055 case WXK_MULTIPLY: keySym = XK_KP_Multiply; break;
1056 case WXK_ADD: keySym = XK_KP_Add; break;
1057 case WXK_SUBTRACT: keySym = XK_KP_Subtract; break;
1058 case WXK_DECIMAL: keySym = XK_KP_Decimal; break;
1059 case WXK_DIVIDE: keySym = XK_KP_Divide; break;
1060 case WXK_F1: keySym = XK_F1; break;
1061 case WXK_F2: keySym = XK_F2; break;
1062 case WXK_F3: keySym = XK_F3; break;
1063 case WXK_F4: keySym = XK_F4; break;
1064 case WXK_F5: keySym = XK_F5; break;
1065 case WXK_F6: keySym = XK_F6; break;
1066 case WXK_F7: keySym = XK_F7; break;
1067 case WXK_F8: keySym = XK_F8; break;
1068 case WXK_F9: keySym = XK_F9; break;
1069 case WXK_F10: keySym = XK_F10; break;
1070 case WXK_F11: keySym = XK_F11; break;
1071 case WXK_F12: keySym = XK_F12; break;
1072 case WXK_F13: keySym = XK_F13; break;
1073 case WXK_F14: keySym = XK_F14; break;
1074 case WXK_F15: keySym = XK_F15; break;
1075 case WXK_F16: keySym = XK_F16; break;
1076 case WXK_F17: keySym = XK_F17; break;
1077 case WXK_F18: keySym = XK_F18; break;
1078 case WXK_F19: keySym = XK_F19; break;
1079 case WXK_F20: keySym = XK_F20; break;
1080 case WXK_F21: keySym = XK_F21; break;
1081 case WXK_F22: keySym = XK_F22; break;
1082 case WXK_F23: keySym = XK_F23; break;
1083 case WXK_F24: keySym = XK_F24; break;
1084 case WXK_NUMLOCK: keySym = XK_Num_Lock; break;
1085 case WXK_SCROLL: keySym = XK_Scroll_Lock; break;
1086 default: keySym = id <= 255 ? (KeySym)id : 0;
1087 } // switch
1088 return keySym;
1089 }
1090
1091 // Read $HOME for what it says is home, if not
1092 // read $USER or $LOGNAME for user name else determine
1093 // the Real User, then determine the Real home dir.
1094 static char * GetIniFile (char *dest, const char *filename)
1095 {
1096 char *home = NULL;
1097 if (filename && wxIsAbsolutePath(filename))
1098 {
1099 strcpy(dest, filename);
1100 }
1101 else if ((home = wxGetUserHome("")) != NULL)
1102 {
1103 strcpy(dest, home);
1104 if (dest[strlen(dest) - 1] != '/')
1105 strcat (dest, "/");
1106 if (filename == NULL)
1107 {
1108 if ((filename = getenv ("XENVIRONMENT")) == NULL)
1109 filename = ".Xdefaults";
1110 }
1111 else if (*filename != '.')
1112 strcat (dest, ".");
1113 strcat (dest, filename);
1114 } else
1115 {
1116 dest[0] = '\0';
1117 }
1118 return dest;
1119 }
1120
1121 /*
1122 * Some colour manipulation routines
1123 */
1124
1125 void wxHSVToXColor(wxHSV *hsv,XColor *rgb)
1126 {
1127 int h = hsv->h;
1128 int s = hsv->s;
1129 int v = hsv->v;
1130 int r, g, b;
1131 int i, f;
1132 int p, q, t;
1133 s = (s * wxMAX_RGB) / wxMAX_SV;
1134 v = (v * wxMAX_RGB) / wxMAX_SV;
1135 if (h == 360) h = 0;
1136 if (s == 0) { h = 0; r = g = b = v; }
1137 i = h / 60;
1138 f = h % 60;
1139 p = v * (wxMAX_RGB - s) / wxMAX_RGB;
1140 q = v * (wxMAX_RGB - s * f / 60) / wxMAX_RGB;
1141 t = v * (wxMAX_RGB - s * (60 - f) / 60) / wxMAX_RGB;
1142 switch (i)
1143 {
1144 case 0: r = v, g = t, b = p; break;
1145 case 1: r = q, g = v, b = p; break;
1146 case 2: r = p, g = v, b = t; break;
1147 case 3: r = p, g = q, b = v; break;
1148 case 4: r = t, g = p, b = v; break;
1149 case 5: r = v, g = p, b = q; break;
1150 }
1151 rgb->red = r << 8;
1152 rgb->green = g << 8;
1153 rgb->blue = b << 8;
1154 }
1155
1156 void wxXColorToHSV(wxHSV *hsv,XColor *rgb)
1157 {
1158 int r = rgb->red >> 8;
1159 int g = rgb->green >> 8;
1160 int b = rgb->blue >> 8;
1161 int maxv = wxMax3(r, g, b);
1162 int minv = wxMin3(r, g, b);
1163 int h, s, v;
1164 v = maxv;
1165 if (maxv) s = (maxv - minv) * wxMAX_RGB / maxv;
1166 else s = 0;
1167 if (s == 0) h = 0;
1168 else
1169 {
1170 int rc, gc, bc, hex;
1171 rc = (maxv - r) * wxMAX_RGB / (maxv - minv);
1172 gc = (maxv - g) * wxMAX_RGB / (maxv - minv);
1173 bc = (maxv - b) * wxMAX_RGB / (maxv - minv);
1174 if (r == maxv) { h = bc - gc, hex = 0; }
1175 else if (g == maxv) { h = rc - bc, hex = 2; }
1176 else if (b == maxv) { h = gc - rc, hex = 4; }
1177 h = hex * 60 + (h * 60 / wxMAX_RGB);
1178 if (h < 0) h += 360;
1179 }
1180 hsv->h = h;
1181 hsv->s = (s * wxMAX_SV) / wxMAX_RGB;
1182 hsv->v = (v * wxMAX_SV) / wxMAX_RGB;
1183 }
1184
1185 void wxAllocNearestColor(Display *d,Colormap cmp,XColor *xc)
1186 {
1187 int llp;
1188
1189 int screen = DefaultScreen(d);
1190 int num_colors = DisplayCells(d,screen);
1191
1192 XColor *color_defs = new XColor[num_colors];
1193 for(llp = 0;llp < num_colors;llp++) color_defs[llp].pixel = llp;
1194 XQueryColors(d,cmp,color_defs,num_colors);
1195
1196 wxHSV hsv_defs, hsv;
1197 wxXColorToHSV(&hsv,xc);
1198
1199 int diff, min_diff, pixel = 0;
1200
1201 for(llp = 0;llp < num_colors;llp++)
1202 {
1203 wxXColorToHSV(&hsv_defs,&color_defs[llp]);
1204 diff = wxSIGN(wxH_WEIGHT * (hsv.h - hsv_defs.h)) +
1205 wxSIGN(wxS_WEIGHT * (hsv.s - hsv_defs.s)) +
1206 wxSIGN(wxV_WEIGHT * (hsv.v - hsv_defs.v));
1207 if (llp == 0) min_diff = diff;
1208 if (min_diff > diff) { min_diff = diff; pixel = llp; }
1209 if (min_diff == 0) break;
1210 }
1211
1212 xc -> red = color_defs[pixel].red;
1213 xc -> green = color_defs[pixel].green;
1214 xc -> blue = color_defs[pixel].blue;
1215 xc -> flags = DoRed | DoGreen | DoBlue;
1216 if (!XAllocColor(d,cmp,xc))
1217 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1218
1219 delete[] color_defs;
1220 }
1221
1222 void wxAllocColor(Display *d,Colormap cmp,XColor *xc)
1223 {
1224 if (!XAllocColor(d,cmp,xc))
1225 {
1226 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1227 wxAllocNearestColor(d,cmp,xc);
1228 }
1229 }
1230
1231