1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #define XtDisplay XTDISPLAY
27 #include "wx/msgdlg.h"
28 #include "wx/cursor.h"
29 #include "wx/window.h" // for wxTopLevelWindows
36 #include <sys/types.h>
44 #if (defined(__SUNCC__) || defined(__CLCC__))
49 #pragma message disable nosimpint
53 #pragma message enable nosimpint
56 #include "wx/unix/execute.h"
58 #include "wx/motif/private.h"
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // Yuck this is really BOTH site and platform dependent
65 // so we should use some other strategy!
67 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
69 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
72 static char *GetIniFile (char *dest
, const char *filename
);
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // async event processing
80 // ----------------------------------------------------------------------------
82 // Consume all events until no more left
85 Display
*display
= (Display
*) wxGetDisplay();
87 XSync (display
, FALSE
);
89 // XtAppPending returns availability of events AND timers/inputs, which
90 // are processed via callbacks, so XtAppNextEvent will not return if
91 // there are no events. So added '& XtIMXEvent' - Sergey.
92 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
94 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
95 // Jan Lessner: works better when events are non-X events
96 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
100 // Check whether this window wants to process messages, e.g. Stop button
101 // in long calculations.
102 bool wxCheckForInterrupt(wxWindow
*wnd
)
104 wxCHECK_MSG( wnd
, FALSE
, "NULL window in wxCheckForInterrupt" );
106 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
107 Window win
=(Window
) wnd
->GetXWindow();
110 if (wnd
->GetMainWidget())
112 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
115 bool hadEvents
= FALSE
;
116 while( XCheckMaskEvent(dpy
,
117 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
118 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
121 if ( event
.xany
.window
== win
)
125 XtDispatchEvent(&event
);
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
139 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
141 wxHandleProcessTermination(proc_data
);
143 // VZ: I think they should be the same...
144 wxASSERT( (int)*id
== proc_data
->tag
);
149 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
151 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
153 (XtPointer
*) XtInputReadMask
,
154 (XtInputCallbackProc
) xt_notify_end_process
,
155 (XtPointer
) proc_data
);
160 // ----------------------------------------------------------------------------
162 // ----------------------------------------------------------------------------
167 // Use current setting for the bell
168 XBell ((Display
*) wxGetDisplay(), 0);
171 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
174 // This code is WRONG!! Does NOT return the
175 // Motif version of the libs but the X protocol
177 Display
*display
= XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
179 *majorVsn
= ProtocolVersion (display
);
181 *minorVsn
= ProtocolRevision (display
);
186 // ----------------------------------------------------------------------------
187 // Reading and writing resources (eg WIN.INI, .Xdefaults)
188 // ----------------------------------------------------------------------------
190 // Read $HOME for what it says is home, if not
191 // read $USER or $LOGNAME for user name else determine
192 // the Real User, then determine the Real home dir.
193 static char * GetIniFile (char *dest
, const char *filename
)
196 if (filename
&& wxIsAbsolutePath(filename
))
198 strcpy(dest
, filename
);
200 else if ((home
= wxGetUserHome("")) != NULL
)
203 if (dest
[strlen(dest
) - 1] != '/')
205 if (filename
== NULL
)
207 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
208 filename
= ".Xdefaults";
210 else if (*filename
!= '.')
212 strcat (dest
, filename
);
222 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
224 if (create
&& wxFileExists (name
) ) {
226 return buf
; // Exists so ...
232 // Put in standard place for resource files if not absolute
233 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
235 strcat (buf
, (const char*) wxFileNameFromPath (name
));
239 // Touch the file to create it
240 FILE *fd
= fopen (buf
, "w");
247 * We have a cache for writing different resource files,
248 * which will only get flushed when we call wxFlushResources().
249 * Build up a list of resource databases waiting to be written.
253 wxList
wxResourceCache (wxKEY_STRING
);
256 wxFlushResources (void)
258 char nameBuffer
[512];
260 wxNode
*node
= wxResourceCache
.First ();
263 const char *file
= node
->GetKeyString();
264 // If file doesn't exist, create it first.
265 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
267 XrmDatabase database
= (XrmDatabase
) node
->Data ();
268 XrmPutFileDatabase (database
, nameBuffer
);
269 XrmDestroyDatabase (database
);
270 wxNode
*next
= node
->Next ();
276 static XrmDatabase wxResourceDatabase
= 0;
278 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
280 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
284 (void) GetIniFile (buffer
, file
);
286 XrmDatabase database
;
287 wxNode
*node
= wxResourceCache
.Find (buffer
);
289 database
= (XrmDatabase
) node
->Data ();
292 database
= XrmGetFileDatabase (buffer
);
293 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
297 strcpy (resName
, (const char*) section
);
298 strcat (resName
, ".");
299 strcat (resName
, (const char*) entry
);
301 XrmPutStringResource (&database
, resName
, value
);
305 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
308 sprintf(buf
, "%.4f", value
);
309 return wxWriteResource(section
, entry
, buf
, file
);
312 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
315 sprintf(buf
, "%ld", value
);
316 return wxWriteResource(section
, entry
, buf
, file
);
319 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
322 sprintf(buf
, "%d", value
);
323 return wxWriteResource(section
, entry
, buf
, file
);
326 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
328 if (!wxResourceDatabase
)
330 Display
*display
= (Display
*) wxGetDisplay();
331 wxXMergeDatabases (wxTheApp
, display
);
334 XrmDatabase database
;
340 // Is this right? Trying to get it to look in the user's
341 // home directory instead of current directory -- JACS
342 (void) GetIniFile (buffer
, file
);
344 wxNode
*node
= wxResourceCache
.Find (buffer
);
346 database
= (XrmDatabase
) node
->Data ();
349 database
= XrmGetFileDatabase (buffer
);
350 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
354 database
= wxResourceDatabase
;
359 strcpy (buf
, section
);
363 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
365 // Try different combinations of upper/lower case, just in case...
368 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
369 success
= XrmGetResource (database
, buf
, "*", str_type
,
377 *value
= new char[xvalue
.size
+ 1];
378 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
384 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
387 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
390 *value
= (float)strtod(s
, NULL
);
397 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
400 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
403 *value
= strtol(s
, NULL
, 10);
410 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
413 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
416 // Handle True, False here
417 // True, Yes, Enables, Set or Activated
418 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
420 // False, No, Disabled, Reset, Cleared, Deactivated
421 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
425 *value
= (int) strtol (s
, NULL
, 10);
433 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
435 XrmDatabase homeDB
, serverDB
, applicationDB
;
436 char filenamebuf
[1024];
438 char *filename
= &filenamebuf
[0];
440 wxString classname
= theApp
->GetClassName();
442 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
443 (void) strcat (name
, (const char*) classname
);
445 /* Get application defaults file, if any */
446 applicationDB
= XrmGetFileDatabase (name
);
447 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
449 /* Merge server defaults, created by xrdb, loaded as a property of the root
450 * window when the server initializes and loaded into the display
451 * structure on XOpenDisplay;
452 * if not defined, use .Xdefaults
455 if (XResourceManagerString (display
) != NULL
)
457 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
461 (void) GetIniFile (filename
, NULL
);
462 serverDB
= XrmGetFileDatabase (filename
);
464 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
466 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
467 * and merge into existing database
470 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
473 environment
= GetIniFile (filename
, NULL
);
474 len
= strlen (environment
);
475 wxString hostname
= wxGetHostName();
477 strncat(environment
, hostname
, 1024 - len
);
479 homeDB
= XrmGetFileDatabase (environment
);
480 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
486 * Not yet used but may be useful.
490 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
493 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
495 XrmDatabase rdb
= NULL
; // A resource data base
497 // Create an empty resource database
498 rdb
= XrmGetStringDatabase ("");
500 // Add the Component resources, prepending the name of the component
503 while (resourceSpec
[i
] != NULL
)
507 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
508 XrmPutLineResource (&rdb
, buf
);
511 // Merge them into the Xt database, with lowest precendence
515 #if (XlibSpecificationRelease>=5)
516 XrmDatabase db
= XtDatabase (dpy
);
517 XrmCombineDatabase (rdb
, &db
, FALSE
);
519 XrmMergeDatabases (dpy
->db
, &rdb
);
527 #endif // wxUSE_RESOURCES
529 // ----------------------------------------------------------------------------
531 // ----------------------------------------------------------------------------
533 static int wxBusyCursorCount
= 0;
537 wxXSetBusyCursor (wxWindow
* win
, wxCursor
* cursor
)
539 Display
*display
= (Display
*) win
->GetXDisplay();
541 Window xwin
= (Window
) win
->GetXWindow();
545 XSetWindowAttributes attrs
;
549 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
553 // Restore old cursor
554 if (win
->GetCursor().Ok())
555 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
560 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
564 for(wxNode
*node
= win
->GetChildren().First (); node
; node
= node
->Next())
566 wxWindow
*child
= (wxWindow
*) node
->Data ();
567 wxXSetBusyCursor (child
, cursor
);
571 // Set the cursor to the busy cursor for all windows
572 void wxBeginBusyCursor(wxCursor
*cursor
)
575 if (wxBusyCursorCount
== 1)
577 for(wxNode
*node
= wxTopLevelWindows
.First (); node
; node
= node
->Next())
579 wxWindow
*win
= (wxWindow
*) node
->Data ();
580 wxXSetBusyCursor (win
, cursor
);
585 // Restore cursor to normal
586 void wxEndBusyCursor()
588 if (wxBusyCursorCount
== 0)
592 if (wxBusyCursorCount
== 0)
594 for(wxNode
*node
= wxTopLevelWindows
.First (); node
; node
= node
->Next())
596 wxWindow
*win
= (wxWindow
*) node
->Data ();
597 wxXSetBusyCursor (win
, NULL
);
602 // TRUE if we're between the above two calls
605 return (wxBusyCursorCount
> 0);
608 // ----------------------------------------------------------------------------
610 // ----------------------------------------------------------------------------
612 void wxGetMousePosition( int* x
, int* y
)
616 XQueryPointer((Display
*) wxGetDisplay(),
617 DefaultRootWindow((Display
*) wxGetDisplay()),
619 &(xev
.x_root
), &(xev
.y_root
),
626 // Return TRUE if we have a colour display
627 bool wxColourDisplay()
629 return wxDisplayDepth() > 1;
632 // Returns depth of screen
635 Display
*dpy
= (Display
*) wxGetDisplay();
637 return DefaultDepth (dpy
, DefaultScreen (dpy
));
640 // Get size of display
641 void wxDisplaySize(int *width
, int *height
)
643 Display
*dpy
= (Display
*) wxGetDisplay();
646 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
648 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
651 void wxDisplaySizeMM(int *width
, int *height
)
653 Display
*dpy
= (Display
*) wxGetDisplay();
656 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
658 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
661 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
663 // This is supposed to return desktop dimensions minus any window
664 // manager panels, menus, taskbars, etc. If there is a way to do that
665 // for this platform please fix this function, otherwise it defaults
666 // to the entire desktop.
669 wxDisplaySize(width
, height
);
673 // Configurable display in Motif
674 static WXDisplay
*gs_currentDisplay
= NULL
;
675 static wxString gs_displayName
;
677 WXDisplay
*wxGetDisplay()
679 if (gs_currentDisplay
)
680 return gs_currentDisplay
;
682 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
683 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
685 return wxTheApp
->GetInitialDisplay();
687 return (WXDisplay
*) NULL
;
690 bool wxSetDisplay(const wxString
& display_name
)
692 gs_displayName
= display_name
;
696 gs_currentDisplay
= NULL
;
704 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
705 (const char*) display_name
,
706 (const char*) wxTheApp
->GetAppName(),
707 (const char*) wxTheApp
->GetClassName(),
709 #if XtSpecificationRelease < 5
718 gs_currentDisplay
= (WXDisplay
*) display
;
726 wxString
wxGetDisplayName()
728 return gs_displayName
;
731 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
733 return wxGenericFindWindowAtPoint(pt
);
736 // ----------------------------------------------------------------------------
738 // ----------------------------------------------------------------------------
740 // Find the letter corresponding to the mnemonic, for Motif
741 char wxFindMnemonic (const char *s
)
744 int len
= strlen (s
);
746 for (i
= 0; i
< len
; i
++)
750 // Carefully handle &&
751 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
763 char * wxFindAccelerator (const char *s
)
765 // VZ: this function returns incorrect keysym which completely breaks kbd
770 // The accelerator text is after the \t char.
771 while (*s
&& *s
!= '\t')
777 Now we need to format it as X standard:
782 Ctrl+N --> Ctrl<Key>N
784 Ctrl+Shift+A --> Ctrl Shift<Key>A
789 char *tmp
= copystring (s
);
795 while (*p
&& *p
!= '+')
801 strcat (wxBuffer
, " ");
802 if (strcmp (s
, "Alt"))
803 strcat (wxBuffer
, s
);
805 strcat (wxBuffer
, "Meta");
810 strcat (wxBuffer
, "<Key>");
811 strcat (wxBuffer
, s
);
820 XmString
wxFindAcceleratorText (const char *s
)
822 // VZ: this function returns incorrect keysym which completely breaks kbd
827 // The accelerator text is after the \t char.
828 while (*s
&& *s
!= '\t')
833 XmString text
= XmStringCreateSimple ((char *)s
);
838 // ----------------------------------------------------------------------------
839 // keycode translations
840 // ----------------------------------------------------------------------------
842 #include <X11/keysym.h>
844 // FIXME what about tables??
846 int wxCharCodeXToWX(KeySym keySym
)
853 id
= WXK_SHIFT
; break;
856 id
= WXK_CONTROL
; break;
858 id
= WXK_BACK
; break;
860 id
= WXK_DELETE
; break;
862 id
= WXK_CLEAR
; break;
868 id
= WXK_RETURN
; break;
870 id
= WXK_ESCAPE
; break;
873 id
= WXK_PAUSE
; break;
875 id
= WXK_NUMLOCK
; break;
877 id
= WXK_SCROLL
; break;
880 id
= WXK_HOME
; break;
884 id
= WXK_LEFT
; break;
886 id
= WXK_RIGHT
; break;
890 id
= WXK_DOWN
; break;
892 id
= WXK_NEXT
; break;
894 id
= WXK_PRIOR
; break;
896 id
= WXK_MENU
; break;
898 id
= WXK_SELECT
; break;
900 id
= WXK_CANCEL
; break;
902 id
= WXK_PRINT
; break;
904 id
= WXK_EXECUTE
; break;
906 id
= WXK_INSERT
; break;
908 id
= WXK_HELP
; break;
911 id
= WXK_MULTIPLY
; break;
915 id
= WXK_SUBTRACT
; break;
917 id
= WXK_DIVIDE
; break;
919 id
= WXK_DECIMAL
; break;
927 id
= WXK_RETURN
; break;
929 id
= WXK_NUMPAD0
; break;
931 id
= WXK_NUMPAD1
; break;
933 id
= WXK_NUMPAD2
; break;
935 id
= WXK_NUMPAD3
; break;
937 id
= WXK_NUMPAD4
; break;
939 id
= WXK_NUMPAD5
; break;
941 id
= WXK_NUMPAD6
; break;
943 id
= WXK_NUMPAD7
; break;
945 id
= WXK_NUMPAD8
; break;
947 id
= WXK_NUMPAD9
; break;
997 id
= (keySym
<= 255) ? (int)keySym
: -1;
1003 KeySym
wxCharCodeWXToX(int id
)
1009 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
1010 case WXK_BACK
: keySym
= XK_BackSpace
; break;
1011 case WXK_TAB
: keySym
= XK_Tab
; break;
1012 case WXK_CLEAR
: keySym
= XK_Clear
; break;
1013 case WXK_RETURN
: keySym
= XK_Return
; break;
1014 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
1015 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
1016 case WXK_MENU
: keySym
= XK_Menu
; break;
1017 case WXK_PAUSE
: keySym
= XK_Pause
; break;
1018 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
1019 case WXK_SPACE
: keySym
= ' '; break;
1020 case WXK_PRIOR
: keySym
= XK_Prior
; break;
1021 case WXK_NEXT
: keySym
= XK_Next
; break;
1022 case WXK_END
: keySym
= XK_End
; break;
1023 case WXK_HOME
: keySym
= XK_Home
; break;
1024 case WXK_LEFT
: keySym
= XK_Left
; break;
1025 case WXK_UP
: keySym
= XK_Up
; break;
1026 case WXK_RIGHT
: keySym
= XK_Right
; break;
1027 case WXK_DOWN
: keySym
= XK_Down
; break;
1028 case WXK_SELECT
: keySym
= XK_Select
; break;
1029 case WXK_PRINT
: keySym
= XK_Print
; break;
1030 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
1031 case WXK_INSERT
: keySym
= XK_Insert
; break;
1032 case WXK_DELETE
: keySym
= XK_Delete
; break;
1033 case WXK_HELP
: keySym
= XK_Help
; break;
1034 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
1035 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
1036 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
1037 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
1038 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
1039 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
1040 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
1041 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
1042 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
1043 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
1044 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
1045 case WXK_ADD
: keySym
= XK_KP_Add
; break;
1046 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
1047 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
1048 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
1049 case WXK_F1
: keySym
= XK_F1
; break;
1050 case WXK_F2
: keySym
= XK_F2
; break;
1051 case WXK_F3
: keySym
= XK_F3
; break;
1052 case WXK_F4
: keySym
= XK_F4
; break;
1053 case WXK_F5
: keySym
= XK_F5
; break;
1054 case WXK_F6
: keySym
= XK_F6
; break;
1055 case WXK_F7
: keySym
= XK_F7
; break;
1056 case WXK_F8
: keySym
= XK_F8
; break;
1057 case WXK_F9
: keySym
= XK_F9
; break;
1058 case WXK_F10
: keySym
= XK_F10
; break;
1059 case WXK_F11
: keySym
= XK_F11
; break;
1060 case WXK_F12
: keySym
= XK_F12
; break;
1061 case WXK_F13
: keySym
= XK_F13
; break;
1062 case WXK_F14
: keySym
= XK_F14
; break;
1063 case WXK_F15
: keySym
= XK_F15
; break;
1064 case WXK_F16
: keySym
= XK_F16
; break;
1065 case WXK_F17
: keySym
= XK_F17
; break;
1066 case WXK_F18
: keySym
= XK_F18
; break;
1067 case WXK_F19
: keySym
= XK_F19
; break;
1068 case WXK_F20
: keySym
= XK_F20
; break;
1069 case WXK_F21
: keySym
= XK_F21
; break;
1070 case WXK_F22
: keySym
= XK_F22
; break;
1071 case WXK_F23
: keySym
= XK_F23
; break;
1072 case WXK_F24
: keySym
= XK_F24
; break;
1073 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
1074 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
1075 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
1081 // ----------------------------------------------------------------------------
1082 // Some colour manipulation routines
1083 // ----------------------------------------------------------------------------
1085 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
1090 int r
= 0, g
= 0, b
= 0;
1093 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
1094 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
1095 if (h
== 360) h
= 0;
1096 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
1099 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
1100 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
1101 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
1104 case 0: r
= v
, g
= t
, b
= p
; break;
1105 case 1: r
= q
, g
= v
, b
= p
; break;
1106 case 2: r
= p
, g
= v
, b
= t
; break;
1107 case 3: r
= p
, g
= q
, b
= v
; break;
1108 case 4: r
= t
, g
= p
, b
= v
; break;
1109 case 5: r
= v
, g
= p
, b
= q
; break;
1112 rgb
->green
= g
<< 8;
1116 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
1118 int r
= rgb
->red
>> 8;
1119 int g
= rgb
->green
>> 8;
1120 int b
= rgb
->blue
>> 8;
1121 int maxv
= wxMax3(r
, g
, b
);
1122 int minv
= wxMin3(r
, g
, b
);
1125 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
1130 int rc
, gc
, bc
, hex
= 0;
1131 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
1132 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
1133 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
1134 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
1135 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
1136 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
1137 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
1138 if (h
< 0) h
+= 360;
1141 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
1142 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
1145 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1149 int screen
= DefaultScreen(d
);
1150 int num_colors
= DisplayCells(d
,screen
);
1152 XColor
*color_defs
= new XColor
[num_colors
];
1153 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
1154 XQueryColors(d
,cmp
,color_defs
,num_colors
);
1156 wxHSV hsv_defs
, hsv
;
1157 wxXColorToHSV(&hsv
,xc
);
1159 int diff
, min_diff
= 0, pixel
= 0;
1161 for(llp
= 0;llp
< num_colors
;llp
++)
1163 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
1164 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
1165 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
1166 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
1167 if (llp
== 0) min_diff
= diff
;
1168 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
1169 if (min_diff
== 0) break;
1172 xc
-> red
= color_defs
[pixel
].red
;
1173 xc
-> green
= color_defs
[pixel
].green
;
1174 xc
-> blue
= color_defs
[pixel
].blue
;
1175 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
1178 if (!XAllocColor(d,cmp,xc))
1179 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1182 delete[] color_defs
;
1185 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1187 if (!XAllocColor(d
,cmp
,xc
))
1189 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1190 wxAllocNearestColor(d
,cmp
,xc
);
1195 // These functions duplicate those in wxWindow, but are needed
1196 // for use outside of wxWindow (e.g. wxMenu, wxMenuBar).
1198 // Change a widget's foreground and background colours.
1200 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1202 // When should we specify the foreground, if it's calculated
1203 // by wxComputeColours?
1204 // Solution: say we start with the default (computed) foreground colour.
1205 // If we call SetForegroundColour explicitly for a control or window,
1206 // then the foreground is changed.
1207 // Therefore SetBackgroundColour computes the foreground colour, and
1208 // SetForegroundColour changes the foreground colour. The ordering is
1211 XtVaSetValues ((Widget
) widget
,
1212 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1216 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1218 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1221 XtVaSetValues ((Widget
) widget
,
1222 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1223 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1224 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1225 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1228 if (changeArmColour
)
1229 XtVaSetValues ((Widget
) widget
,
1230 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,