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
26 #include "wx/msgdlg.h"
27 #include "wx/cursor.h"
28 #include "wx/window.h" // for wxTopLevelWindows
35 #include <sys/types.h>
43 #if (defined(__SUNCC__) || defined(__CLCC__))
48 #pragma message disable nosimpint
51 #include "wx/unix/execute.h"
55 #include "wx/motif/private.h"
59 #include "wx/x11/private.h"
63 #include "X11/Xresource.h"
66 #include "X11/Xutil.h"
69 #pragma message enable nosimpint
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 // Yuck this is really BOTH site and platform dependent
77 // so we should use some other strategy!
79 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
81 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
84 static char *GetIniFile (char *dest
, const char *filename
);
86 // ============================================================================
88 // ============================================================================
90 // ----------------------------------------------------------------------------
91 // async event processing
92 // ----------------------------------------------------------------------------
94 // Consume all events until no more left
97 Display
*display
= (Display
*) wxGetDisplay();
99 XSync (display
, FALSE
);
102 // XtAppPending returns availability of events AND timers/inputs, which
103 // are processed via callbacks, so XtAppNextEvent will not return if
104 // there are no events. So added '& XtIMXEvent' - Sergey.
105 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
107 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
108 // Jan Lessner: works better when events are non-X events
109 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
118 // Check whether this window wants to process messages, e.g. Stop button
119 // in long calculations.
120 bool wxCheckForInterrupt(wxWindow
*wnd
)
123 wxCHECK_MSG( wnd
, FALSE
, "NULL window in wxCheckForInterrupt" );
125 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
126 Window win
=(Window
) wnd
->GetXWindow();
129 if (wnd
->GetMainWidget())
131 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
134 bool hadEvents
= FALSE
;
135 while( XCheckMaskEvent(dpy
,
136 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
137 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
140 if ( event
.xany
.window
== win
)
144 XtDispatchEvent(&event
);
150 wxASSERT_MSG(FALSE
, "wxCheckForInterrupt not yet implemented.");
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
162 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
164 wxHandleProcessTermination(proc_data
);
166 // VZ: I think they should be the same...
167 wxASSERT( (int)*id
== proc_data
->tag
);
173 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
176 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
178 (XtPointer
*) XtInputReadMask
,
179 (XtInputCallbackProc
) xt_notify_end_process
,
180 (XtPointer
) proc_data
);
190 // ----------------------------------------------------------------------------
192 // ----------------------------------------------------------------------------
197 // Use current setting for the bell
198 XBell ((Display
*) wxGetDisplay(), 0);
201 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
205 // This code is WRONG!! Does NOT return the
206 // Motif version of the libs but the X protocol
208 Display
*display
= XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
210 *majorVsn
= ProtocolVersion (display
);
212 *minorVsn
= ProtocolRevision (display
);
225 // ----------------------------------------------------------------------------
226 // Reading and writing resources (eg WIN.INI, .Xdefaults)
227 // ----------------------------------------------------------------------------
229 // Read $HOME for what it says is home, if not
230 // read $USER or $LOGNAME for user name else determine
231 // the Real User, then determine the Real home dir.
232 static char * GetIniFile (char *dest
, const char *filename
)
235 if (filename
&& wxIsAbsolutePath(filename
))
237 strcpy(dest
, filename
);
239 else if ((home
= wxGetUserHome("")) != NULL
)
242 if (dest
[strlen(dest
) - 1] != '/')
244 if (filename
== NULL
)
246 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
247 filename
= ".Xdefaults";
249 else if (*filename
!= '.')
251 strcat (dest
, filename
);
261 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
263 if (create
&& wxFileExists (name
) ) {
265 return buf
; // Exists so ...
271 // Put in standard place for resource files if not absolute
272 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
274 strcat (buf
, (const char*) wxFileNameFromPath (name
));
278 // Touch the file to create it
279 FILE *fd
= fopen (buf
, "w");
286 * We have a cache for writing different resource files,
287 * which will only get flushed when we call wxFlushResources().
288 * Build up a list of resource databases waiting to be written.
292 wxList
wxResourceCache (wxKEY_STRING
);
295 wxFlushResources (void)
297 char nameBuffer
[512];
299 wxNode
*node
= wxResourceCache
.First ();
302 const char *file
= node
->GetKeyString();
303 // If file doesn't exist, create it first.
304 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
306 XrmDatabase database
= (XrmDatabase
) node
->Data ();
307 XrmPutFileDatabase (database
, nameBuffer
);
308 XrmDestroyDatabase (database
);
309 wxNode
*next
= node
->Next ();
315 static XrmDatabase wxResourceDatabase
= 0;
317 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
319 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
323 (void) GetIniFile (buffer
, file
);
325 XrmDatabase database
;
326 wxNode
*node
= wxResourceCache
.Find (buffer
);
328 database
= (XrmDatabase
) node
->Data ();
331 database
= XrmGetFileDatabase (buffer
);
332 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
336 strcpy (resName
, (const char*) section
);
337 strcat (resName
, ".");
338 strcat (resName
, (const char*) entry
);
340 XrmPutStringResource (&database
, resName
, value
);
344 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
347 sprintf(buf
, "%.4f", value
);
348 return wxWriteResource(section
, entry
, buf
, file
);
351 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
354 sprintf(buf
, "%ld", value
);
355 return wxWriteResource(section
, entry
, buf
, file
);
358 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
361 sprintf(buf
, "%d", value
);
362 return wxWriteResource(section
, entry
, buf
, file
);
365 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
367 if (!wxResourceDatabase
)
369 Display
*display
= (Display
*) wxGetDisplay();
370 wxXMergeDatabases (wxTheApp
, display
);
373 XrmDatabase database
;
379 // Is this right? Trying to get it to look in the user's
380 // home directory instead of current directory -- JACS
381 (void) GetIniFile (buffer
, file
);
383 wxNode
*node
= wxResourceCache
.Find (buffer
);
385 database
= (XrmDatabase
) node
->Data ();
388 database
= XrmGetFileDatabase (buffer
);
389 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
393 database
= wxResourceDatabase
;
398 strcpy (buf
, section
);
402 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
404 // Try different combinations of upper/lower case, just in case...
407 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
408 success
= XrmGetResource (database
, buf
, "*", str_type
,
416 *value
= new char[xvalue
.size
+ 1];
417 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
423 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
426 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
429 *value
= (float)strtod(s
, NULL
);
436 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
439 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
442 *value
= strtol(s
, NULL
, 10);
449 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
452 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
455 // Handle True, False here
456 // True, Yes, Enables, Set or Activated
457 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
459 // False, No, Disabled, Reset, Cleared, Deactivated
460 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
464 *value
= (int) strtol (s
, NULL
, 10);
472 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
474 XrmDatabase homeDB
, serverDB
, applicationDB
;
475 char filenamebuf
[1024];
477 char *filename
= &filenamebuf
[0];
479 wxString classname
= theApp
->GetClassName();
481 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
482 (void) strcat (name
, (const char*) classname
);
484 /* Get application defaults file, if any */
485 applicationDB
= XrmGetFileDatabase (name
);
486 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
488 /* Merge server defaults, created by xrdb, loaded as a property of the root
489 * window when the server initializes and loaded into the display
490 * structure on XOpenDisplay;
491 * if not defined, use .Xdefaults
494 if (XResourceManagerString (display
) != NULL
)
496 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
500 (void) GetIniFile (filename
, NULL
);
501 serverDB
= XrmGetFileDatabase (filename
);
503 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
505 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
506 * and merge into existing database
509 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
512 environment
= GetIniFile (filename
, NULL
);
513 len
= strlen (environment
);
514 wxString hostname
= wxGetHostName();
516 strncat(environment
, hostname
, 1024 - len
);
518 homeDB
= XrmGetFileDatabase (environment
);
519 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
525 * Not yet used but may be useful.
529 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
532 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
534 XrmDatabase rdb
= NULL
; // A resource data base
536 // Create an empty resource database
537 rdb
= XrmGetStringDatabase ("");
539 // Add the Component resources, prepending the name of the component
542 while (resourceSpec
[i
] != NULL
)
546 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
547 XrmPutLineResource (&rdb
, buf
);
550 // Merge them into the Xt database, with lowest precendence
554 #if (XlibSpecificationRelease>=5)
555 XrmDatabase db
= XtDatabase (dpy
);
556 XrmCombineDatabase (rdb
, &db
, FALSE
);
558 XrmMergeDatabases (dpy
->db
, &rdb
);
566 #endif // wxUSE_RESOURCES
568 // ----------------------------------------------------------------------------
570 // ----------------------------------------------------------------------------
572 void wxGetMousePosition( int* x
, int* y
)
581 XQueryPointer((Display
*) wxGetDisplay(),
582 DefaultRootWindow((Display
*) wxGetDisplay()),
584 &(xev
.x_root
), &(xev
.y_root
),
592 // Return TRUE if we have a colour display
593 bool wxColourDisplay()
595 return wxDisplayDepth() > 1;
598 // Returns depth of screen
601 Display
*dpy
= (Display
*) wxGetDisplay();
603 return DefaultDepth (dpy
, DefaultScreen (dpy
));
606 // Get size of display
607 void wxDisplaySize(int *width
, int *height
)
609 Display
*dpy
= (Display
*) wxGetDisplay();
612 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
614 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
617 void wxDisplaySizeMM(int *width
, int *height
)
619 Display
*dpy
= (Display
*) wxGetDisplay();
622 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
624 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
627 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
629 // This is supposed to return desktop dimensions minus any window
630 // manager panels, menus, taskbars, etc. If there is a way to do that
631 // for this platform please fix this function, otherwise it defaults
632 // to the entire desktop.
635 wxDisplaySize(width
, height
);
639 // Configurable display in wxX11 and wxMotif
640 static WXDisplay
*gs_currentDisplay
= NULL
;
641 static wxString gs_displayName
;
643 WXDisplay
*wxGetDisplay()
645 if (gs_currentDisplay
)
646 return gs_currentDisplay
;
648 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
649 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
651 return wxTheApp
->GetInitialDisplay();
655 return wxApp::GetDisplay();
659 bool wxSetDisplay(const wxString
& display_name
)
661 gs_displayName
= display_name
;
663 if ( display_name
.IsEmpty() )
665 gs_currentDisplay
= NULL
;
674 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
675 (const char*) display_name
,
676 (const char*) wxTheApp
->GetAppName(),
677 (const char*) wxTheApp
->GetClassName(),
679 #if XtSpecificationRelease < 5
688 gs_currentDisplay
= (WXDisplay
*) display
;
695 Display
* display
= XOpenDisplay((char*) display_name
.c_str());
699 gs_currentDisplay
= (WXDisplay
*) display
;
708 wxString
wxGetDisplayName()
710 return gs_displayName
;
713 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
715 return wxGenericFindWindowAtPoint(pt
);
718 // ----------------------------------------------------------------------------
719 // keycode translations
720 // ----------------------------------------------------------------------------
722 #include <X11/keysym.h>
724 // FIXME what about tables??
726 int wxCharCodeXToWX(KeySym keySym
)
733 id
= WXK_SHIFT
; break;
736 id
= WXK_CONTROL
; break;
738 id
= WXK_BACK
; break;
740 id
= WXK_DELETE
; break;
742 id
= WXK_CLEAR
; break;
748 id
= WXK_RETURN
; break;
750 id
= WXK_ESCAPE
; break;
753 id
= WXK_PAUSE
; break;
755 id
= WXK_NUMLOCK
; break;
757 id
= WXK_SCROLL
; break;
760 id
= WXK_HOME
; break;
764 id
= WXK_LEFT
; break;
766 id
= WXK_RIGHT
; break;
770 id
= WXK_DOWN
; break;
772 id
= WXK_NEXT
; break;
774 id
= WXK_PRIOR
; break;
776 id
= WXK_MENU
; break;
778 id
= WXK_SELECT
; break;
780 id
= WXK_CANCEL
; break;
782 id
= WXK_PRINT
; break;
784 id
= WXK_EXECUTE
; break;
786 id
= WXK_INSERT
; break;
788 id
= WXK_HELP
; break;
791 id
= WXK_MULTIPLY
; break;
795 id
= WXK_SUBTRACT
; break;
797 id
= WXK_DIVIDE
; break;
799 id
= WXK_DECIMAL
; break;
807 id
= WXK_RETURN
; break;
809 id
= WXK_NUMPAD0
; break;
811 id
= WXK_NUMPAD1
; break;
813 id
= WXK_NUMPAD2
; break;
815 id
= WXK_NUMPAD3
; break;
817 id
= WXK_NUMPAD4
; break;
819 id
= WXK_NUMPAD5
; break;
821 id
= WXK_NUMPAD6
; break;
823 id
= WXK_NUMPAD7
; break;
825 id
= WXK_NUMPAD8
; break;
827 id
= WXK_NUMPAD9
; break;
877 id
= (keySym
<= 255) ? (int)keySym
: -1;
883 KeySym
wxCharCodeWXToX(int id
)
889 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
890 case WXK_BACK
: keySym
= XK_BackSpace
; break;
891 case WXK_TAB
: keySym
= XK_Tab
; break;
892 case WXK_CLEAR
: keySym
= XK_Clear
; break;
893 case WXK_RETURN
: keySym
= XK_Return
; break;
894 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
895 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
896 case WXK_MENU
: keySym
= XK_Menu
; break;
897 case WXK_PAUSE
: keySym
= XK_Pause
; break;
898 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
899 case WXK_SPACE
: keySym
= ' '; break;
900 case WXK_PRIOR
: keySym
= XK_Prior
; break;
901 case WXK_NEXT
: keySym
= XK_Next
; break;
902 case WXK_END
: keySym
= XK_End
; break;
903 case WXK_HOME
: keySym
= XK_Home
; break;
904 case WXK_LEFT
: keySym
= XK_Left
; break;
905 case WXK_UP
: keySym
= XK_Up
; break;
906 case WXK_RIGHT
: keySym
= XK_Right
; break;
907 case WXK_DOWN
: keySym
= XK_Down
; break;
908 case WXK_SELECT
: keySym
= XK_Select
; break;
909 case WXK_PRINT
: keySym
= XK_Print
; break;
910 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
911 case WXK_INSERT
: keySym
= XK_Insert
; break;
912 case WXK_DELETE
: keySym
= XK_Delete
; break;
913 case WXK_HELP
: keySym
= XK_Help
; break;
914 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
915 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
916 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
917 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
918 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
919 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
920 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
921 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
922 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
923 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
924 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
925 case WXK_ADD
: keySym
= XK_KP_Add
; break;
926 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
927 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
928 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
929 case WXK_F1
: keySym
= XK_F1
; break;
930 case WXK_F2
: keySym
= XK_F2
; break;
931 case WXK_F3
: keySym
= XK_F3
; break;
932 case WXK_F4
: keySym
= XK_F4
; break;
933 case WXK_F5
: keySym
= XK_F5
; break;
934 case WXK_F6
: keySym
= XK_F6
; break;
935 case WXK_F7
: keySym
= XK_F7
; break;
936 case WXK_F8
: keySym
= XK_F8
; break;
937 case WXK_F9
: keySym
= XK_F9
; break;
938 case WXK_F10
: keySym
= XK_F10
; break;
939 case WXK_F11
: keySym
= XK_F11
; break;
940 case WXK_F12
: keySym
= XK_F12
; break;
941 case WXK_F13
: keySym
= XK_F13
; break;
942 case WXK_F14
: keySym
= XK_F14
; break;
943 case WXK_F15
: keySym
= XK_F15
; break;
944 case WXK_F16
: keySym
= XK_F16
; break;
945 case WXK_F17
: keySym
= XK_F17
; break;
946 case WXK_F18
: keySym
= XK_F18
; break;
947 case WXK_F19
: keySym
= XK_F19
; break;
948 case WXK_F20
: keySym
= XK_F20
; break;
949 case WXK_F21
: keySym
= XK_F21
; break;
950 case WXK_F22
: keySym
= XK_F22
; break;
951 case WXK_F23
: keySym
= XK_F23
; break;
952 case WXK_F24
: keySym
= XK_F24
; break;
953 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
954 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
955 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
961 // ----------------------------------------------------------------------------
962 // Some colour manipulation routines
963 // ----------------------------------------------------------------------------
965 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
970 int r
= 0, g
= 0, b
= 0;
973 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
974 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
976 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
979 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
980 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
981 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
984 case 0: r
= v
, g
= t
, b
= p
; break;
985 case 1: r
= q
, g
= v
, b
= p
; break;
986 case 2: r
= p
, g
= v
, b
= t
; break;
987 case 3: r
= p
, g
= q
, b
= v
; break;
988 case 4: r
= t
, g
= p
, b
= v
; break;
989 case 5: r
= v
, g
= p
, b
= q
; break;
996 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
998 int r
= rgb
->red
>> 8;
999 int g
= rgb
->green
>> 8;
1000 int b
= rgb
->blue
>> 8;
1001 int maxv
= wxMax3(r
, g
, b
);
1002 int minv
= wxMin3(r
, g
, b
);
1005 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
1010 int rc
, gc
, bc
, hex
= 0;
1011 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
1012 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
1013 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
1014 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
1015 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
1016 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
1017 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
1018 if (h
< 0) h
+= 360;
1021 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
1022 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
1025 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1030 int screen
= DefaultScreen(d
);
1031 int num_colors
= DisplayCells(d
,screen
);
1033 XColor
*color_defs
= new XColor
[num_colors
];
1034 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
1035 XQueryColors(d
,cmp
,color_defs
,num_colors
);
1037 wxHSV hsv_defs
, hsv
;
1038 wxXColorToHSV(&hsv
,xc
);
1040 int diff
, min_diff
= 0, pixel
= 0;
1042 for(llp
= 0;llp
< num_colors
;llp
++)
1044 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
1045 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
1046 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
1047 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
1048 if (llp
== 0) min_diff
= diff
;
1049 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
1050 if (min_diff
== 0) break;
1053 xc
-> red
= color_defs
[pixel
].red
;
1054 xc
-> green
= color_defs
[pixel
].green
;
1055 xc
-> blue
= color_defs
[pixel
].blue
;
1056 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
1059 if (!XAllocColor(d,cmp,xc))
1060 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1063 delete[] color_defs
;
1067 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1069 if (!XAllocColor(d
,cmp
,xc
))
1071 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1072 wxAllocNearestColor(d
,cmp
,xc
);
1077 wxString
wxGetXEventName(XEvent
& event
)
1080 wxString
str(wxT("(some event)"));
1083 int type
= event
.xany
.type
;
1084 static char* event_name
[] = {
1085 "", "unknown(-)", // 0-1
1086 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1087 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1088 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1089 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1090 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1091 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1092 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1093 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1094 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1095 "ClientMessage", "MappingNotify", // 33-34
1096 "unknown(+)"}; // 35
1097 type
= wxMin(35, type
); type
= wxMax(1, type
);
1098 wxString
str(event_name
[type
]);
1105 // ----------------------------------------------------------------------------
1107 // ----------------------------------------------------------------------------
1109 // Find the letter corresponding to the mnemonic, for Motif
1110 char wxFindMnemonic (const char *s
)
1113 int len
= strlen (s
);
1115 for (i
= 0; i
< len
; i
++)
1119 // Carefully handle &&
1120 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1132 char * wxFindAccelerator (const char *s
)
1134 // VZ: this function returns incorrect keysym which completely breaks kbd
1139 // The accelerator text is after the \t char.
1140 while (*s
&& *s
!= '\t')
1146 Now we need to format it as X standard:
1151 Ctrl+N --> Ctrl<Key>N
1152 Alt+k --> Meta<Key>k
1153 Ctrl+Shift+A --> Ctrl Shift<Key>A
1157 static char buf
[256];
1159 char *tmp
= copystring (s
);
1165 while (*p
&& *p
!= '+')
1172 if (strcmp (s
, "Alt"))
1175 strcat (buf
, "Meta");
1180 strcat (buf
, "<Key>");
1190 XmString
wxFindAcceleratorText (const char *s
)
1192 // VZ: this function returns incorrect keysym which completely breaks kbd
1197 // The accelerator text is after the \t char.
1198 while (*s
&& *s
!= '\t')
1203 XmString text
= XmStringCreateSimple ((char *)s
);
1209 // These functions duplicate those in wxWindow, but are needed
1210 // for use outside of wxWindow (e.g. wxMenu, wxMenuBar).
1212 // Change a widget's foreground and background colours.
1214 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1216 // When should we specify the foreground, if it's calculated
1217 // by wxComputeColours?
1218 // Solution: say we start with the default (computed) foreground colour.
1219 // If we call SetForegroundColour explicitly for a control or window,
1220 // then the foreground is changed.
1221 // Therefore SetBackgroundColour computes the foreground colour, and
1222 // SetForegroundColour changes the foreground colour. The ordering is
1225 XtVaSetValues ((Widget
) widget
,
1226 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1230 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1232 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1235 XtVaSetValues ((Widget
) widget
,
1236 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1237 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1238 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1239 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1242 if (changeArmColour
)
1243 XtVaSetValues ((Widget
) widget
,
1244 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1251 bool wxWindowIsVisible(Window win
)
1253 XWindowAttributes wa
;
1254 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
1256 return (wa
.map_state
== IsViewable
);