1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 // Note: this is done in utilscmn.cpp now.
14 // #pragma implementation
15 // #pragma implementation "utils.h"
21 #include "wx/msgdlg.h"
22 #include "wx/cursor.h"
29 #include <sys/types.h>
37 #if defined(__SOLARIS__) || defined(__SVR4__) && !defined(__HPUX__)
38 #include <sys/systeminfo.h>
41 #if (defined(__SUNCC__) || defined(__CLCC__))
47 #include "wx/motif/private.h"
49 // Yuck this is really BOTH site and platform dependent
50 // so we should use some other strategy!
52 # define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
54 # define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
57 static char *GetIniFile (char *dest
, const char *filename
);
59 extern wxList wxTopLevelWindows
;
61 // Get full hostname (eg. DoDo.BSn-Germany.crg.de)
62 bool wxGetHostName(char *buf
, int maxSize
)
64 #if defined(__SOLARIS__) || defined(__SVR4__) && !defined(__HPUX__)
65 return (sysinfo (SI_HOSTNAME
, buf
, maxSize
) != -1);
66 #else /* BSD Sockets */
71 if (gethostname (name
, sizeof (name
) / sizeof (char) - 1) == -1)
73 // Get official full name of host
75 ,(h
= gethostbyname (name
)) != NULL
? h
->h_name
: name
81 // Get user ID e.g. jacs
82 bool wxGetUserId(char *buf
, int maxSize
)
85 *buf
= '\0'; // return empty string
90 if ((who
= getpwuid (getuid ())) != NULL
)
92 strncpy (buf
, who
->pw_name
, maxSize
- 1);
99 // Get user name e.g. Julian Smart
100 bool wxGetUserName(char *buf
, int maxSize
)
103 *buf
= '\0'; // return empty string
108 if ((who
= getpwuid (getuid ())) != NULL
)
110 strncpy (buf
, who
->pw_gecos
, maxSize
- 1);
117 int wxKill(long pid
, int sig
)
124 unixSignal
= SIGTERM
;
126 return kill( (int)pid
, unixSignal
);
130 // Execute a program in an Interactive Shell
132 bool wxShell(const wxString
& command
)
137 #if defined(sun) || defined(__ultrix) || defined(__bsdi__)
138 pid_t pid
= vfork ();
146 // Generic X windows terminal window
148 execlp("xterm", "-e", (char *) (const char*) command
, NULL
);
150 execlp("xterm", NULL
);
158 // Get free memory in bytes, or -1 if cannot determine amount (e.g. on UNIX)
159 long wxGetFreeMemory()
164 void wxSleep(int nSecs
)
169 // Consume all events until no more left
172 Display
*display
= (Display
*) wxGetDisplay();
174 XSync (display
, FALSE
);
176 // XtAppPending returns availability of events AND timers/inputs, which
177 // are processed via callbacks, so XtAppNextEvent will not return if
178 // there are no events. So added '& XtIMXEvent' - Sergey.
179 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
181 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
182 // Jan Lessner: works better when events are non-X events
183 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
187 // Output a debug message, in a system dependent fashion.
188 void wxDebugMsg(const char *fmt
...)
193 if (!wxTheApp
->GetWantDebugOutput())
198 vsprintf (buffer
, fmt
, ap
);
204 // Non-fatal error: pop up message box and (possibly) continue
205 void wxError(const wxString
& msg
, const wxString
& title
)
207 cerr
<< (const char*) title
<< ": " << (const char*) msg
<< "\n";
210 // Fatal error: pop up message box and abort
211 void wxFatalError(const wxString
& msg
, const wxString
& title
)
213 cerr
<< (const char*) title
<< ": " << (const char*) msg
<< "\n";
220 // Use current setting for the bell
221 XBell ((Display
*) wxGetDisplay(), 0);
224 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
227 // This code is WRONG!! Does NOT return the
228 // Motif version of the libs but the X protocol
229 // version! @@@@@ Fix ME!!!!!!!!!
230 Display
*display
= XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
232 *majorVsn
= ProtocolVersion (display
);
234 *minorVsn
= ProtocolRevision (display
);
238 // Reading and writing resources (eg WIN.INI, .Xdefaults)
241 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
243 if (create
&& wxFileExists (name
) ) {
245 return buf
; // Exists so ...
251 // Put in standard place for resource files if not absolute
252 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
254 strcat (buf
, (const char*) wxFileNameFromPath (name
));
258 // Touch the file to create it
259 FILE *fd
= fopen (buf
, "w");
266 * We have a cache for writing different resource files,
267 * which will only get flushed when we call wxFlushResources().
268 * Build up a list of resource databases waiting to be written.
272 wxList
wxResourceCache (wxKEY_STRING
);
275 wxFlushResources (void)
277 char nameBuffer
[512];
279 wxNode
*node
= wxResourceCache
.First ();
282 const char *file
= node
->GetKeyString();
283 // If file doesn't exist, create it first.
284 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
286 XrmDatabase database
= (XrmDatabase
) node
->Data ();
287 XrmPutFileDatabase (database
, nameBuffer
);
288 XrmDestroyDatabase (database
);
289 wxNode
*next
= node
->Next ();
295 static XrmDatabase wxResourceDatabase
= 0;
297 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
299 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
303 (void) GetIniFile (buffer
, file
);
305 XrmDatabase database
;
306 wxNode
*node
= wxResourceCache
.Find (buffer
);
308 database
= (XrmDatabase
) node
->Data ();
311 database
= XrmGetFileDatabase (buffer
);
312 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
316 strcpy (resName
, (const char*) section
);
317 strcat (resName
, ".");
318 strcat (resName
, (const char*) entry
);
320 XrmPutStringResource (&database
, resName
, value
);
324 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
327 sprintf(buf
, "%.4f", value
);
328 return wxWriteResource(section
, entry
, buf
, file
);
331 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
334 sprintf(buf
, "%ld", value
);
335 return wxWriteResource(section
, entry
, buf
, file
);
338 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
341 sprintf(buf
, "%d", value
);
342 return wxWriteResource(section
, entry
, buf
, file
);
345 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
347 if (!wxResourceDatabase
)
349 Display
*display
= (Display
*) wxGetDisplay();
350 wxXMergeDatabases (wxTheApp
, display
);
353 XrmDatabase database
;
359 // Is this right? Trying to get it to look in the user's
360 // home directory instead of current directory -- JACS
361 (void) GetIniFile (buffer
, file
);
363 wxNode
*node
= wxResourceCache
.Find (buffer
);
365 database
= (XrmDatabase
) node
->Data ();
368 database
= XrmGetFileDatabase (buffer
);
369 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
373 database
= wxResourceDatabase
;
378 strcpy (buf
, section
);
382 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
384 // Try different combinations of upper/lower case, just in case...
387 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
388 success
= XrmGetResource (database
, buf
, "*", str_type
,
396 *value
= new char[xvalue
.size
+ 1];
397 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
403 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
406 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
409 *value
= (float)strtod(s
, NULL
);
416 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
419 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
422 *value
= strtol(s
, NULL
, 10);
429 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
432 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
435 // Handle True, False here
436 // True, Yes, Enables, Set or Activated
437 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
439 // False, No, Disabled, Reset, Cleared, Deactivated
440 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
444 *value
= (int) strtol (s
, NULL
, 10);
452 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
454 XrmDatabase homeDB
, serverDB
, applicationDB
;
455 char filenamebuf
[1024];
457 char *filename
= &filenamebuf
[0];
459 wxString classname
= theApp
->GetClassName();
461 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
462 (void) strcat (name
, (const char*) classname
);
464 /* Get application defaults file, if any */
465 applicationDB
= XrmGetFileDatabase (name
);
466 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
468 /* Merge server defaults, created by xrdb, loaded as a property of the root
469 * window when the server initializes and loaded into the display
470 * structure on XOpenDisplay;
471 * if not defined, use .Xdefaults
474 if (XResourceManagerString (display
) != NULL
)
476 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
480 (void) GetIniFile (filename
, NULL
);
481 serverDB
= XrmGetFileDatabase (filename
);
483 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
485 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
486 * and merge into existing database
489 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
492 environment
= GetIniFile (filename
, NULL
);
493 len
= strlen (environment
);
494 #if defined(__SOLARIS__) || defined(__SVR4__) && !defined(__HPUX__)
495 (void) sysinfo (SI_HOSTNAME
, environment
+ len
, 1024 - len
);
497 (void) gethostname (environment
+ len
, 1024 - len
);
500 homeDB
= XrmGetFileDatabase (environment
);
501 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
507 * Not yet used but may be useful.
511 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
514 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
516 XrmDatabase rdb
= NULL
; // A resource data base
518 // Create an empty resource database
519 rdb
= XrmGetStringDatabase ("");
521 // Add the Component resources, prepending the name of the component
524 while (resourceSpec
[i
] != NULL
)
528 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
529 XrmPutLineResource (&rdb
, buf
);
532 // Merge them into the Xt database, with lowest precendence
536 #if (XlibSpecificationRelease>=5)
537 XrmDatabase db
= XtDatabase (dpy
);
538 XrmCombineDatabase (rdb
, &db
, FALSE
);
540 XrmMergeDatabases (dpy
->db
, &rdb
);
548 #endif // wxUSE_RESOURCES
550 static int wxBusyCursorCount
= 0;
554 wxXSetBusyCursor (wxWindow
* win
, wxCursor
* cursor
)
556 Display
*display
= (Display
*) win
->GetXDisplay();
558 Window xwin
= (Window
) win
->GetXWindow();
562 XSetWindowAttributes attrs
;
566 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
570 // Restore old cursor
571 if (win
->GetCursor()->Ok())
572 attrs
.cursor
= (Cursor
) win
->GetCursor()->GetXCursor(display
);
577 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
581 for(wxNode
*node
= win
->GetChildren().First (); node
; node
= node
->Next())
583 wxWindow
*child
= (wxWindow
*) node
->Data ();
584 wxXSetBusyCursor (child
, cursor
);
588 // Set the cursor to the busy cursor for all windows
589 void wxBeginBusyCursor(wxCursor
*cursor
)
592 if (wxBusyCursorCount
== 1)
594 for(wxNode
*node
= wxTopLevelWindows
.First (); node
; node
= node
->Next())
596 wxWindow
*win
= (wxWindow
*) node
->Data ();
597 wxXSetBusyCursor (win
, cursor
);
602 // Restore cursor to normal
603 void wxEndBusyCursor()
605 if (wxBusyCursorCount
== 0)
609 if (wxBusyCursorCount
== 0)
611 for(wxNode
*node
= wxTopLevelWindows
.First (); node
; node
= node
->Next())
613 wxWindow
*win
= (wxWindow
*) node
->Data ();
614 wxXSetBusyCursor (win
, NULL
);
619 // TRUE if we're between the above two calls
622 return (wxBusyCursorCount
> 0);
625 const char* wxGetHomeDir( wxString
*home
)
627 *home
= wxGetUserHome( wxString() );
628 if (home
->IsNull()) *home
= "/";
632 char *wxGetUserHome (const wxString
& user
)
637 struct passwd
*who
= NULL
;
642 if ((ptr
= getenv("HOME")) != NULL
)
644 if ((ptr
= getenv("USER")) != NULL
||
645 (ptr
= getenv("LOGNAME")) != NULL
)
647 who
= getpwnam( ptr
);
649 // We now make sure the the user exists!
651 who
= getpwuid( getuid() );
653 who
= getpwnam ((const char*) user
);
655 return who
? who
->pw_dir
: (char*) NULL
;
660 // Check whether this window wants to process messages, e.g. Stop button
661 // in long calculations.
662 bool wxCheckForInterrupt(wxWindow
*wnd
)
665 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
666 Window win
=(Window
) wnd
->GetXWindow();
669 if(wnd
->GetMainWidget()){
670 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
672 while(XCheckMaskEvent(dpy
,
673 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
674 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
676 if(event
.xany
.window
==win
)
677 XtDispatchEvent(&event
);
681 return TRUE
;//*** temporary?
684 wxMessageBox("wnd==NULL !!!");
685 return FALSE
;//*** temporary?
689 void wxGetMousePosition( int* x
, int* y
)
693 XQueryPointer((Display
*) wxGetDisplay(),
694 DefaultRootWindow((Display
*) wxGetDisplay()), &root
, &child
,
695 &(xev
.x_root
), &(xev
.y_root
),
702 // Return TRUE if we have a colour display
703 bool wxColourDisplay()
705 Display
*dpy
= (Display
*) wxGetDisplay();
707 if (DefaultDepth (dpy
, DefaultScreen (dpy
)) < 2)
713 // Returns depth of screen
716 Display
*dpy
= (Display
*) wxGetDisplay();
717 return DefaultDepth (dpy
, DefaultScreen (dpy
));
720 // Get size of display
721 void wxDisplaySize(int *width
, int *height
)
723 Display
*dpy
= (Display
*) wxGetDisplay();
725 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
726 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
729 /* Configurable display in Motif */
730 static WXDisplay
*gs_currentDisplay
= NULL
;
731 static wxString gs_displayName
;
733 WXDisplay
*wxGetDisplay()
735 if (gs_currentDisplay
)
736 return gs_currentDisplay
;
738 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
739 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
741 return wxTheApp
->GetInitialDisplay();
743 return (WXDisplay
*) NULL
;
746 bool wxSetDisplay(const wxString
& display_name
)
748 gs_displayName
= display_name
;
750 if (display_name
.IsNull() || display_name
.IsEmpty())
752 gs_currentDisplay
= NULL
;
759 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
760 (const char*) display_name
,
761 (const char*) wxTheApp
->GetAppName(),
762 (const char*) wxTheApp
->GetClassName(),
764 # if XtSpecificationRelease < 5
767 0, (int *)&argc
, NULL
);
772 gs_currentDisplay
= (WXDisplay
*) display
;
780 wxString
wxGetDisplayName()
782 return gs_displayName
;
785 // Find the letter corresponding to the mnemonic, for Motif
786 char wxFindMnemonic (const char *s
)
789 int len
= strlen (s
);
791 for (i
= 0; i
< len
; i
++)
795 // Carefully handle &&
796 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
808 char * wxFindAccelerator (char *s
)
810 // The accelerator text is after the \t char.
811 while (*s
&& *s
!= '\t')
817 Now we need to format it as X standard:
822 Ctrl+N --> Ctrl<Key>N
824 Ctrl+Shift+A --> Ctrl Shift<Key>A
829 char *tmp
= copystring (s
);
835 while (*p
&& *p
!= '+')
841 strcat (wxBuffer
, " ");
842 if (strcmp (s
, "Alt"))
843 strcat (wxBuffer
, s
);
845 strcat (wxBuffer
, "Meta");
851 strcat (wxBuffer
, "<Key>");
852 strcat (wxBuffer
, s
);
860 XmString
wxFindAcceleratorText (char *s
)
862 // The accelerator text is after the \t char.
863 while (*s
&& *s
!= '\t')
868 XmString text
= XmStringCreateSimple (s
);
872 #include <X11/keysym.h>
874 int wxCharCodeXToWX(KeySym keySym
)
880 id
= WXK_SHIFT
; break;
883 id
= WXK_CONTROL
; break;
885 id
= WXK_BACK
; break;
887 id
= WXK_DELETE
; break;
889 id
= WXK_CLEAR
; break;
895 id
= WXK_RETURN
; break;
897 id
= WXK_ESCAPE
; break;
900 id
= WXK_PAUSE
; break;
902 id
= WXK_NUMLOCK
; break;
904 id
= WXK_SCROLL
; break;
907 id
= WXK_HOME
; break;
911 id
= WXK_LEFT
; break;
913 id
= WXK_RIGHT
; break;
917 id
= WXK_DOWN
; break;
919 id
= WXK_NEXT
; break;
921 id
= WXK_PRIOR
; break;
923 id
= WXK_MENU
; break;
925 id
= WXK_SELECT
; break;
927 id
= WXK_CANCEL
; break;
929 id
= WXK_PRINT
; break;
931 id
= WXK_EXECUTE
; break;
933 id
= WXK_INSERT
; break;
935 id
= WXK_HELP
; break;
938 id
= WXK_MULTIPLY
; break;
942 id
= WXK_SUBTRACT
; break;
944 id
= WXK_DIVIDE
; break;
946 id
= WXK_DECIMAL
; break;
954 id
= WXK_RETURN
; break;
956 id
= WXK_NUMPAD0
; break;
958 id
= WXK_NUMPAD1
; break;
960 id
= WXK_NUMPAD2
; break;
962 id
= WXK_NUMPAD3
; break;
964 id
= WXK_NUMPAD4
; break;
966 id
= WXK_NUMPAD5
; break;
968 id
= WXK_NUMPAD6
; break;
970 id
= WXK_NUMPAD7
; break;
972 id
= WXK_NUMPAD8
; break;
974 id
= WXK_NUMPAD9
; break;
1000 id
= WXK_F13
; break;
1002 id
= WXK_F14
; break;
1004 id
= WXK_F15
; break;
1006 id
= WXK_F16
; break;
1008 id
= WXK_F17
; break;
1010 id
= WXK_F18
; break;
1012 id
= WXK_F19
; break;
1014 id
= WXK_F20
; break;
1016 id
= WXK_F21
; break;
1018 id
= WXK_F22
; break;
1020 id
= WXK_F23
; break;
1022 id
= WXK_F24
; break;
1024 id
= (keySym
<= 255) ? (int)keySym
: -1;
1029 KeySym
wxCharCodeWXToX(int id
)
1034 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
1035 case WXK_BACK
: keySym
= XK_BackSpace
; break;
1036 case WXK_TAB
: keySym
= XK_Tab
; break;
1037 case WXK_CLEAR
: keySym
= XK_Clear
; break;
1038 case WXK_RETURN
: keySym
= XK_Return
; break;
1039 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
1040 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
1041 case WXK_MENU
: keySym
= XK_Menu
; break;
1042 case WXK_PAUSE
: keySym
= XK_Pause
; break;
1043 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
1044 case WXK_SPACE
: keySym
= ' '; break;
1045 case WXK_PRIOR
: keySym
= XK_Prior
; break;
1046 case WXK_NEXT
: keySym
= XK_Next
; break;
1047 case WXK_END
: keySym
= XK_End
; break;
1048 case WXK_HOME
: keySym
= XK_Home
; break;
1049 case WXK_LEFT
: keySym
= XK_Left
; break;
1050 case WXK_UP
: keySym
= XK_Up
; break;
1051 case WXK_RIGHT
: keySym
= XK_Right
; break;
1052 case WXK_DOWN
: keySym
= XK_Down
; break;
1053 case WXK_SELECT
: keySym
= XK_Select
; break;
1054 case WXK_PRINT
: keySym
= XK_Print
; break;
1055 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
1056 case WXK_INSERT
: keySym
= XK_Insert
; break;
1057 case WXK_DELETE
: keySym
= XK_Delete
; break;
1058 case WXK_HELP
: keySym
= XK_Help
; break;
1059 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
1060 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
1061 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
1062 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
1063 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
1064 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
1065 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
1066 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
1067 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
1068 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
1069 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
1070 case WXK_ADD
: keySym
= XK_KP_Add
; break;
1071 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
1072 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
1073 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
1074 case WXK_F1
: keySym
= XK_F1
; break;
1075 case WXK_F2
: keySym
= XK_F2
; break;
1076 case WXK_F3
: keySym
= XK_F3
; break;
1077 case WXK_F4
: keySym
= XK_F4
; break;
1078 case WXK_F5
: keySym
= XK_F5
; break;
1079 case WXK_F6
: keySym
= XK_F6
; break;
1080 case WXK_F7
: keySym
= XK_F7
; break;
1081 case WXK_F8
: keySym
= XK_F8
; break;
1082 case WXK_F9
: keySym
= XK_F9
; break;
1083 case WXK_F10
: keySym
= XK_F10
; break;
1084 case WXK_F11
: keySym
= XK_F11
; break;
1085 case WXK_F12
: keySym
= XK_F12
; break;
1086 case WXK_F13
: keySym
= XK_F13
; break;
1087 case WXK_F14
: keySym
= XK_F14
; break;
1088 case WXK_F15
: keySym
= XK_F15
; break;
1089 case WXK_F16
: keySym
= XK_F16
; break;
1090 case WXK_F17
: keySym
= XK_F17
; break;
1091 case WXK_F18
: keySym
= XK_F18
; break;
1092 case WXK_F19
: keySym
= XK_F19
; break;
1093 case WXK_F20
: keySym
= XK_F20
; break;
1094 case WXK_F21
: keySym
= XK_F21
; break;
1095 case WXK_F22
: keySym
= XK_F22
; break;
1096 case WXK_F23
: keySym
= XK_F23
; break;
1097 case WXK_F24
: keySym
= XK_F24
; break;
1098 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
1099 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
1100 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
1105 // Read $HOME for what it says is home, if not
1106 // read $USER or $LOGNAME for user name else determine
1107 // the Real User, then determine the Real home dir.
1108 static char * GetIniFile (char *dest
, const char *filename
)
1111 if (filename
&& wxIsAbsolutePath(filename
))
1113 strcpy(dest
, filename
);
1115 else if ((home
= wxGetUserHome("")) != NULL
)
1118 if (dest
[strlen(dest
) - 1] != '/')
1120 if (filename
== NULL
)
1122 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
1123 filename
= ".Xdefaults";
1125 else if (*filename
!= '.')
1127 strcat (dest
, filename
);
1136 * Some colour manipulation routines
1139 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
1147 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
1148 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
1149 if (h
== 360) h
= 0;
1150 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
1153 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
1154 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
1155 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
1158 case 0: r
= v
, g
= t
, b
= p
; break;
1159 case 1: r
= q
, g
= v
, b
= p
; break;
1160 case 2: r
= p
, g
= v
, b
= t
; break;
1161 case 3: r
= p
, g
= q
, b
= v
; break;
1162 case 4: r
= t
, g
= p
, b
= v
; break;
1163 case 5: r
= v
, g
= p
, b
= q
; break;
1166 rgb
->green
= g
<< 8;
1170 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
1172 int r
= rgb
->red
>> 8;
1173 int g
= rgb
->green
>> 8;
1174 int b
= rgb
->blue
>> 8;
1175 int maxv
= wxMax3(r
, g
, b
);
1176 int minv
= wxMin3(r
, g
, b
);
1179 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
1184 int rc
, gc
, bc
, hex
;
1185 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
1186 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
1187 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
1188 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
1189 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
1190 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
1191 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
1192 if (h
< 0) h
+= 360;
1195 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
1196 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
1199 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1203 int screen
= DefaultScreen(d
);
1204 int num_colors
= DisplayCells(d
,screen
);
1206 XColor
*color_defs
= new XColor
[num_colors
];
1207 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
1208 XQueryColors(d
,cmp
,color_defs
,num_colors
);
1210 wxHSV hsv_defs
, hsv
;
1211 wxXColorToHSV(&hsv
,xc
);
1213 int diff
, min_diff
, pixel
= 0;
1215 for(llp
= 0;llp
< num_colors
;llp
++)
1217 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
1218 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
1219 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
1220 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
1221 if (llp
== 0) min_diff
= diff
;
1222 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
1223 if (min_diff
== 0) break;
1226 xc
-> red
= color_defs
[pixel
].red
;
1227 xc
-> green
= color_defs
[pixel
].green
;
1228 xc
-> blue
= color_defs
[pixel
].blue
;
1229 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
1230 if (!XAllocColor(d
,cmp
,xc
))
1231 cout
<< "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1233 delete[] color_defs
;
1236 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1238 if (!XAllocColor(d
,cmp
,xc
))
1240 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1241 wxAllocNearestColor(d
,cmp
,xc
);
1246 // These functions duplicate those in wxWindow, but are needed
1247 // for use outside of wxWindow (e.g. wxMenu, wxMenuBar).
1249 // Change a widget's foreground and background colours.
1251 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1253 // When should we specify the foreground, if it's calculated
1254 // by wxComputeColours?
1255 // Solution: say we start with the default (computed) foreground colour.
1256 // If we call SetForegroundColour explicitly for a control or window,
1257 // then the foreground is changed.
1258 // Therefore SetBackgroundColour computes the foreground colour, and
1259 // SetForegroundColour changes the foreground colour. The ordering is
1262 XtVaSetValues ((Widget
) widget
,
1263 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1267 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1269 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1272 XtVaSetValues ((Widget
) widget
,
1273 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1274 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1275 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1276 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1279 if (changeArmColour
)
1280 XtVaSetValues ((Widget
) widget
,
1281 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,