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