1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Various utilities
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
23 #include "wx/msgdlg.h"
24 #include "wx/cursor.h"
25 #include "wx/window.h" // for wxTopLevelWindows
32 #include <sys/types.h>
40 #if (defined(__SUNCC__) || defined(__CLCC__))
45 #pragma message disable nosimpint
49 #pragma message enable nosimpint
52 #include "wx/unix/execute.h"
54 #include "wx/motif/private.h"
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // Yuck this is really BOTH site and platform dependent
61 // so we should use some other strategy!
63 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
65 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
68 static char *GetIniFile (char *dest
, const char *filename
);
70 // ============================================================================
72 // ============================================================================
74 // ----------------------------------------------------------------------------
75 // async event processing
76 // ----------------------------------------------------------------------------
78 // Consume all events until no more left
81 Display
*display
= (Display
*) wxGetDisplay();
83 XSync (display
, FALSE
);
85 // XtAppPending returns availability of events AND timers/inputs, which
86 // are processed via callbacks, so XtAppNextEvent will not return if
87 // there are no events. So added '& XtIMXEvent' - Sergey.
88 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
90 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
91 // Jan Lessner: works better when events are non-X events
92 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
96 // Check whether this window wants to process messages, e.g. Stop button
97 // in long calculations.
98 bool wxCheckForInterrupt(wxWindow
*wnd
)
100 wxCHECK_MSG( wnd
, FALSE
, "NULL window in wxCheckForInterrupt" );
102 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
103 Window win
=(Window
) wnd
->GetXWindow();
106 if (wnd
->GetMainWidget())
108 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
111 bool hadEvents
= FALSE
;
112 while( XCheckMaskEvent(dpy
,
113 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
114 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
117 if ( event
.xany
.window
== win
)
121 XtDispatchEvent(&event
);
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
132 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
135 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
137 wxHandleProcessTermination(proc_data
);
139 // VZ: I think they should be the same...
140 wxASSERT( (int)*id
== proc_data
->tag
);
145 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
147 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
149 (XtPointer
*) XtInputReadMask
,
150 (XtInputCallbackProc
) xt_notify_end_process
,
151 (XtPointer
) proc_data
);
156 // ----------------------------------------------------------------------------
158 // ----------------------------------------------------------------------------
163 // Use current setting for the bell
164 XBell ((Display
*) wxGetDisplay(), 0);
167 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
170 // This code is WRONG!! Does NOT return the
171 // Motif version of the libs but the X protocol
173 Display
*display
= XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
175 *majorVsn
= ProtocolVersion (display
);
177 *minorVsn
= ProtocolRevision (display
);
182 // ----------------------------------------------------------------------------
183 // Reading and writing resources (eg WIN.INI, .Xdefaults)
184 // ----------------------------------------------------------------------------
186 // Read $HOME for what it says is home, if not
187 // read $USER or $LOGNAME for user name else determine
188 // the Real User, then determine the Real home dir.
189 static char * GetIniFile (char *dest
, const char *filename
)
192 if (filename
&& wxIsAbsolutePath(filename
))
194 strcpy(dest
, filename
);
196 else if ((home
= wxGetUserHome("")) != NULL
)
199 if (dest
[strlen(dest
) - 1] != '/')
201 if (filename
== NULL
)
203 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
204 filename
= ".Xdefaults";
206 else if (*filename
!= '.')
208 strcat (dest
, filename
);
218 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
220 if (create
&& wxFileExists (name
) ) {
222 return buf
; // Exists so ...
228 // Put in standard place for resource files if not absolute
229 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
231 strcat (buf
, (const char*) wxFileNameFromPath (name
));
235 // Touch the file to create it
236 FILE *fd
= fopen (buf
, "w");
243 * We have a cache for writing different resource files,
244 * which will only get flushed when we call wxFlushResources().
245 * Build up a list of resource databases waiting to be written.
249 wxList
wxResourceCache (wxKEY_STRING
);
252 wxFlushResources (void)
254 char nameBuffer
[512];
256 wxNode
*node
= wxResourceCache
.First ();
259 const char *file
= node
->GetKeyString();
260 // If file doesn't exist, create it first.
261 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
263 XrmDatabase database
= (XrmDatabase
) node
->Data ();
264 XrmPutFileDatabase (database
, nameBuffer
);
265 XrmDestroyDatabase (database
);
266 wxNode
*next
= node
->Next ();
272 static XrmDatabase wxResourceDatabase
= 0;
274 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
276 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
280 (void) GetIniFile (buffer
, file
);
282 XrmDatabase database
;
283 wxNode
*node
= wxResourceCache
.Find (buffer
);
285 database
= (XrmDatabase
) node
->Data ();
288 database
= XrmGetFileDatabase (buffer
);
289 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
293 strcpy (resName
, (const char*) section
);
294 strcat (resName
, ".");
295 strcat (resName
, (const char*) entry
);
297 XrmPutStringResource (&database
, resName
, value
);
301 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
304 sprintf(buf
, "%.4f", value
);
305 return wxWriteResource(section
, entry
, buf
, file
);
308 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
311 sprintf(buf
, "%ld", value
);
312 return wxWriteResource(section
, entry
, buf
, file
);
315 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
318 sprintf(buf
, "%d", value
);
319 return wxWriteResource(section
, entry
, buf
, file
);
322 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
324 if (!wxResourceDatabase
)
326 Display
*display
= (Display
*) wxGetDisplay();
327 wxXMergeDatabases (wxTheApp
, display
);
330 XrmDatabase database
;
336 // Is this right? Trying to get it to look in the user's
337 // home directory instead of current directory -- JACS
338 (void) GetIniFile (buffer
, file
);
340 wxNode
*node
= wxResourceCache
.Find (buffer
);
342 database
= (XrmDatabase
) node
->Data ();
345 database
= XrmGetFileDatabase (buffer
);
346 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
350 database
= wxResourceDatabase
;
355 strcpy (buf
, section
);
359 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
361 // Try different combinations of upper/lower case, just in case...
364 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
365 success
= XrmGetResource (database
, buf
, "*", str_type
,
373 *value
= new char[xvalue
.size
+ 1];
374 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
380 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
383 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
386 *value
= (float)strtod(s
, NULL
);
393 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
396 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
399 *value
= strtol(s
, NULL
, 10);
406 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
409 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
412 // Handle True, False here
413 // True, Yes, Enables, Set or Activated
414 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
416 // False, No, Disabled, Reset, Cleared, Deactivated
417 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
421 *value
= (int) strtol (s
, NULL
, 10);
429 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
431 XrmDatabase homeDB
, serverDB
, applicationDB
;
432 char filenamebuf
[1024];
434 char *filename
= &filenamebuf
[0];
436 wxString classname
= theApp
->GetClassName();
438 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
439 (void) strcat (name
, (const char*) classname
);
441 /* Get application defaults file, if any */
442 applicationDB
= XrmGetFileDatabase (name
);
443 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
445 /* Merge server defaults, created by xrdb, loaded as a property of the root
446 * window when the server initializes and loaded into the display
447 * structure on XOpenDisplay;
448 * if not defined, use .Xdefaults
451 if (XResourceManagerString (display
) != NULL
)
453 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
457 (void) GetIniFile (filename
, NULL
);
458 serverDB
= XrmGetFileDatabase (filename
);
460 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
462 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
463 * and merge into existing database
466 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
469 environment
= GetIniFile (filename
, NULL
);
470 len
= strlen (environment
);
471 wxString hostname
= wxGetHostName();
473 strncat(environment
, hostname
, 1024 - len
);
475 homeDB
= XrmGetFileDatabase (environment
);
476 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
482 * Not yet used but may be useful.
486 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
489 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
491 XrmDatabase rdb
= NULL
; // A resource data base
493 // Create an empty resource database
494 rdb
= XrmGetStringDatabase ("");
496 // Add the Component resources, prepending the name of the component
499 while (resourceSpec
[i
] != NULL
)
503 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
504 XrmPutLineResource (&rdb
, buf
);
507 // Merge them into the Xt database, with lowest precendence
511 #if (XlibSpecificationRelease>=5)
512 XrmDatabase db
= XtDatabase (dpy
);
513 XrmCombineDatabase (rdb
, &db
, FALSE
);
515 XrmMergeDatabases (dpy
->db
, &rdb
);
523 #endif // wxUSE_RESOURCES
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
529 static int wxBusyCursorCount
= 0;
533 wxXSetBusyCursor (wxWindow
* win
, wxCursor
* cursor
)
535 Display
*display
= (Display
*) win
->GetXDisplay();
537 Window xwin
= (Window
) win
->GetXWindow();
541 XSetWindowAttributes attrs
;
545 attrs
.cursor
= (Cursor
) cursor
->GetXCursor(display
);
549 // Restore old cursor
550 if (win
->GetCursor().Ok())
551 attrs
.cursor
= (Cursor
) win
->GetCursor().GetXCursor(display
);
556 XChangeWindowAttributes (display
, xwin
, CWCursor
, &attrs
);
560 for(wxNode
*node
= win
->GetChildren().First (); node
; node
= node
->Next())
562 wxWindow
*child
= (wxWindow
*) node
->Data ();
563 wxXSetBusyCursor (child
, cursor
);
567 // Set the cursor to the busy cursor for all windows
568 void wxBeginBusyCursor(wxCursor
*cursor
)
571 if (wxBusyCursorCount
== 1)
573 for(wxNode
*node
= wxTopLevelWindows
.First (); node
; node
= node
->Next())
575 wxWindow
*win
= (wxWindow
*) node
->Data ();
576 wxXSetBusyCursor (win
, cursor
);
581 // Restore cursor to normal
582 void wxEndBusyCursor()
584 if (wxBusyCursorCount
== 0)
588 if (wxBusyCursorCount
== 0)
590 for(wxNode
*node
= wxTopLevelWindows
.First (); node
; node
= node
->Next())
592 wxWindow
*win
= (wxWindow
*) node
->Data ();
593 wxXSetBusyCursor (win
, NULL
);
598 // TRUE if we're between the above two calls
601 return (wxBusyCursorCount
> 0);
604 // ----------------------------------------------------------------------------
606 // ----------------------------------------------------------------------------
608 void wxGetMousePosition( int* x
, int* y
)
612 XQueryPointer((Display
*) wxGetDisplay(),
613 DefaultRootWindow((Display
*) wxGetDisplay()),
615 &(xev
.x_root
), &(xev
.y_root
),
622 // Return TRUE if we have a colour display
623 bool wxColourDisplay()
625 return wxDisplayDepth() > 1;
628 // Returns depth of screen
631 Display
*dpy
= (Display
*) wxGetDisplay();
633 return DefaultDepth (dpy
, DefaultScreen (dpy
));
636 // Get size of display
637 void wxDisplaySize(int *width
, int *height
)
639 Display
*dpy
= (Display
*) wxGetDisplay();
642 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
644 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
647 // Configurable display in Motif
648 static WXDisplay
*gs_currentDisplay
= NULL
;
649 static wxString gs_displayName
;
651 WXDisplay
*wxGetDisplay()
653 if (gs_currentDisplay
)
654 return gs_currentDisplay
;
656 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
657 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
659 return wxTheApp
->GetInitialDisplay();
661 return (WXDisplay
*) NULL
;
664 bool wxSetDisplay(const wxString
& display_name
)
666 gs_displayName
= display_name
;
670 gs_currentDisplay
= NULL
;
678 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
679 (const char*) display_name
,
680 (const char*) wxTheApp
->GetAppName(),
681 (const char*) wxTheApp
->GetClassName(),
683 #if XtSpecificationRelease < 5
692 gs_currentDisplay
= (WXDisplay
*) display
;
700 wxString
wxGetDisplayName()
702 return gs_displayName
;
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 // Find the letter corresponding to the mnemonic, for Motif
710 char wxFindMnemonic (const char *s
)
713 int len
= strlen (s
);
715 for (i
= 0; i
< len
; i
++)
719 // Carefully handle &&
720 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
732 char * wxFindAccelerator (const char *s
)
734 // VZ: this function returns incorrect keysym which completely breaks kbd
739 // The accelerator text is after the \t char.
740 while (*s
&& *s
!= '\t')
746 Now we need to format it as X standard:
751 Ctrl+N --> Ctrl<Key>N
753 Ctrl+Shift+A --> Ctrl Shift<Key>A
758 char *tmp
= copystring (s
);
764 while (*p
&& *p
!= '+')
770 strcat (wxBuffer
, " ");
771 if (strcmp (s
, "Alt"))
772 strcat (wxBuffer
, s
);
774 strcat (wxBuffer
, "Meta");
779 strcat (wxBuffer
, "<Key>");
780 strcat (wxBuffer
, s
);
789 XmString
wxFindAcceleratorText (const char *s
)
791 // VZ: this function returns incorrect keysym which completely breaks kbd
796 // The accelerator text is after the \t char.
797 while (*s
&& *s
!= '\t')
802 XmString text
= XmStringCreateSimple ((char *)s
);
807 // ----------------------------------------------------------------------------
808 // keycode translations
809 // ----------------------------------------------------------------------------
811 #include <X11/keysym.h>
813 // FIXME what about tables??
815 int wxCharCodeXToWX(KeySym keySym
)
822 id
= WXK_SHIFT
; break;
825 id
= WXK_CONTROL
; break;
827 id
= WXK_BACK
; break;
829 id
= WXK_DELETE
; break;
831 id
= WXK_CLEAR
; break;
837 id
= WXK_RETURN
; break;
839 id
= WXK_ESCAPE
; break;
842 id
= WXK_PAUSE
; break;
844 id
= WXK_NUMLOCK
; break;
846 id
= WXK_SCROLL
; break;
849 id
= WXK_HOME
; break;
853 id
= WXK_LEFT
; break;
855 id
= WXK_RIGHT
; break;
859 id
= WXK_DOWN
; break;
861 id
= WXK_NEXT
; break;
863 id
= WXK_PRIOR
; break;
865 id
= WXK_MENU
; break;
867 id
= WXK_SELECT
; break;
869 id
= WXK_CANCEL
; break;
871 id
= WXK_PRINT
; break;
873 id
= WXK_EXECUTE
; break;
875 id
= WXK_INSERT
; break;
877 id
= WXK_HELP
; break;
880 id
= WXK_MULTIPLY
; break;
884 id
= WXK_SUBTRACT
; break;
886 id
= WXK_DIVIDE
; break;
888 id
= WXK_DECIMAL
; break;
896 id
= WXK_RETURN
; break;
898 id
= WXK_NUMPAD0
; break;
900 id
= WXK_NUMPAD1
; break;
902 id
= WXK_NUMPAD2
; break;
904 id
= WXK_NUMPAD3
; break;
906 id
= WXK_NUMPAD4
; break;
908 id
= WXK_NUMPAD5
; break;
910 id
= WXK_NUMPAD6
; break;
912 id
= WXK_NUMPAD7
; break;
914 id
= WXK_NUMPAD8
; break;
916 id
= WXK_NUMPAD9
; break;
966 id
= (keySym
<= 255) ? (int)keySym
: -1;
972 KeySym
wxCharCodeWXToX(int id
)
978 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
979 case WXK_BACK
: keySym
= XK_BackSpace
; break;
980 case WXK_TAB
: keySym
= XK_Tab
; break;
981 case WXK_CLEAR
: keySym
= XK_Clear
; break;
982 case WXK_RETURN
: keySym
= XK_Return
; break;
983 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
984 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
985 case WXK_MENU
: keySym
= XK_Menu
; break;
986 case WXK_PAUSE
: keySym
= XK_Pause
; break;
987 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
988 case WXK_SPACE
: keySym
= ' '; break;
989 case WXK_PRIOR
: keySym
= XK_Prior
; break;
990 case WXK_NEXT
: keySym
= XK_Next
; break;
991 case WXK_END
: keySym
= XK_End
; break;
992 case WXK_HOME
: keySym
= XK_Home
; break;
993 case WXK_LEFT
: keySym
= XK_Left
; break;
994 case WXK_UP
: keySym
= XK_Up
; break;
995 case WXK_RIGHT
: keySym
= XK_Right
; break;
996 case WXK_DOWN
: keySym
= XK_Down
; break;
997 case WXK_SELECT
: keySym
= XK_Select
; break;
998 case WXK_PRINT
: keySym
= XK_Print
; break;
999 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
1000 case WXK_INSERT
: keySym
= XK_Insert
; break;
1001 case WXK_DELETE
: keySym
= XK_Delete
; break;
1002 case WXK_HELP
: keySym
= XK_Help
; break;
1003 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
1004 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
1005 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
1006 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
1007 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
1008 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
1009 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
1010 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
1011 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
1012 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
1013 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
1014 case WXK_ADD
: keySym
= XK_KP_Add
; break;
1015 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
1016 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
1017 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
1018 case WXK_F1
: keySym
= XK_F1
; break;
1019 case WXK_F2
: keySym
= XK_F2
; break;
1020 case WXK_F3
: keySym
= XK_F3
; break;
1021 case WXK_F4
: keySym
= XK_F4
; break;
1022 case WXK_F5
: keySym
= XK_F5
; break;
1023 case WXK_F6
: keySym
= XK_F6
; break;
1024 case WXK_F7
: keySym
= XK_F7
; break;
1025 case WXK_F8
: keySym
= XK_F8
; break;
1026 case WXK_F9
: keySym
= XK_F9
; break;
1027 case WXK_F10
: keySym
= XK_F10
; break;
1028 case WXK_F11
: keySym
= XK_F11
; break;
1029 case WXK_F12
: keySym
= XK_F12
; break;
1030 case WXK_F13
: keySym
= XK_F13
; break;
1031 case WXK_F14
: keySym
= XK_F14
; break;
1032 case WXK_F15
: keySym
= XK_F15
; break;
1033 case WXK_F16
: keySym
= XK_F16
; break;
1034 case WXK_F17
: keySym
= XK_F17
; break;
1035 case WXK_F18
: keySym
= XK_F18
; break;
1036 case WXK_F19
: keySym
= XK_F19
; break;
1037 case WXK_F20
: keySym
= XK_F20
; break;
1038 case WXK_F21
: keySym
= XK_F21
; break;
1039 case WXK_F22
: keySym
= XK_F22
; break;
1040 case WXK_F23
: keySym
= XK_F23
; break;
1041 case WXK_F24
: keySym
= XK_F24
; break;
1042 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
1043 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
1044 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
1050 // ----------------------------------------------------------------------------
1051 // Some colour manipulation routines
1052 // ----------------------------------------------------------------------------
1054 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
1059 int r
= 0, g
= 0, b
= 0;
1062 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
1063 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
1064 if (h
== 360) h
= 0;
1065 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
1068 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
1069 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
1070 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
1073 case 0: r
= v
, g
= t
, b
= p
; break;
1074 case 1: r
= q
, g
= v
, b
= p
; break;
1075 case 2: r
= p
, g
= v
, b
= t
; break;
1076 case 3: r
= p
, g
= q
, b
= v
; break;
1077 case 4: r
= t
, g
= p
, b
= v
; break;
1078 case 5: r
= v
, g
= p
, b
= q
; break;
1081 rgb
->green
= g
<< 8;
1085 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
1087 int r
= rgb
->red
>> 8;
1088 int g
= rgb
->green
>> 8;
1089 int b
= rgb
->blue
>> 8;
1090 int maxv
= wxMax3(r
, g
, b
);
1091 int minv
= wxMin3(r
, g
, b
);
1094 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
1099 int rc
, gc
, bc
, hex
= 0;
1100 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
1101 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
1102 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
1103 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
1104 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
1105 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
1106 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
1107 if (h
< 0) h
+= 360;
1110 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
1111 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
1114 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1118 int screen
= DefaultScreen(d
);
1119 int num_colors
= DisplayCells(d
,screen
);
1121 XColor
*color_defs
= new XColor
[num_colors
];
1122 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
1123 XQueryColors(d
,cmp
,color_defs
,num_colors
);
1125 wxHSV hsv_defs
, hsv
;
1126 wxXColorToHSV(&hsv
,xc
);
1128 int diff
, min_diff
= 0, pixel
= 0;
1130 for(llp
= 0;llp
< num_colors
;llp
++)
1132 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
1133 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
1134 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
1135 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
1136 if (llp
== 0) min_diff
= diff
;
1137 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
1138 if (min_diff
== 0) break;
1141 xc
-> red
= color_defs
[pixel
].red
;
1142 xc
-> green
= color_defs
[pixel
].green
;
1143 xc
-> blue
= color_defs
[pixel
].blue
;
1144 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
1147 if (!XAllocColor(d,cmp,xc))
1148 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1151 delete[] color_defs
;
1154 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1156 if (!XAllocColor(d
,cmp
,xc
))
1158 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1159 wxAllocNearestColor(d
,cmp
,xc
);
1164 // These functions duplicate those in wxWindow, but are needed
1165 // for use outside of wxWindow (e.g. wxMenu, wxMenuBar).
1167 // Change a widget's foreground and background colours.
1169 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1171 // When should we specify the foreground, if it's calculated
1172 // by wxComputeColours?
1173 // Solution: say we start with the default (computed) foreground colour.
1174 // If we call SetForegroundColour explicitly for a control or window,
1175 // then the foreground is changed.
1176 // Therefore SetBackgroundColour computes the foreground colour, and
1177 // SetForegroundColour changes the foreground colour. The ordering is
1180 XtVaSetValues ((Widget
) widget
,
1181 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1185 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1187 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1190 XtVaSetValues ((Widget
) widget
,
1191 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1192 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1193 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1194 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1197 if (changeArmColour
)
1198 XtVaSetValues ((Widget
) widget
,
1199 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,