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
48 #include "wx/unix/execute.h"
52 #include "wx/motif/private.h"
56 #include "wx/x11/private.h"
60 #include "X11/Xresource.h"
63 #include "X11/Xutil.h"
66 #pragma message enable nosimpint
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // Yuck this is really BOTH site and platform dependent
74 // so we should use some other strategy!
76 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
78 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
81 static char *GetIniFile (char *dest
, const char *filename
);
83 // ============================================================================
85 // ============================================================================
87 // ----------------------------------------------------------------------------
88 // async event processing
89 // ----------------------------------------------------------------------------
91 // Consume all events until no more left
94 Display
*display
= (Display
*) wxGetDisplay();
96 XSync (display
, FALSE
);
99 // XtAppPending returns availability of events AND timers/inputs, which
100 // are processed via callbacks, so XtAppNextEvent will not return if
101 // there are no events. So added '& XtIMXEvent' - Sergey.
102 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
104 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
105 // Jan Lessner: works better when events are non-X events
106 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
115 // Check whether this window wants to process messages, e.g. Stop button
116 // in long calculations.
117 bool wxCheckForInterrupt(wxWindow
*wnd
)
120 wxCHECK_MSG( wnd
, FALSE
, "NULL window in wxCheckForInterrupt" );
122 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
123 Window win
=(Window
) wnd
->GetXWindow();
126 if (wnd
->GetMainWidget())
128 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
131 bool hadEvents
= FALSE
;
132 while( XCheckMaskEvent(dpy
,
133 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
134 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
137 if ( event
.xany
.window
== win
)
141 XtDispatchEvent(&event
);
147 wxASSERT_MSG(FALSE
, "wxCheckForInterrupt not yet implemented.");
152 // ----------------------------------------------------------------------------
154 // ----------------------------------------------------------------------------
156 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
159 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
161 wxHandleProcessTermination(proc_data
);
163 // VZ: I think they should be the same...
164 wxASSERT( (int)*id
== proc_data
->tag
);
170 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
173 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
175 (XtPointer
*) XtInputReadMask
,
176 (XtInputCallbackProc
) xt_notify_end_process
,
177 (XtPointer
) proc_data
);
187 // ----------------------------------------------------------------------------
189 // ----------------------------------------------------------------------------
194 // Use current setting for the bell
195 XBell ((Display
*) wxGetDisplay(), 0);
198 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
202 // This code is WRONG!! Does NOT return the
203 // Motif version of the libs but the X protocol
205 Display
*display
= XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
207 *majorVsn
= ProtocolVersion (display
);
209 *minorVsn
= ProtocolRevision (display
);
222 // ----------------------------------------------------------------------------
223 // Reading and writing resources (eg WIN.INI, .Xdefaults)
224 // ----------------------------------------------------------------------------
226 // Read $HOME for what it says is home, if not
227 // read $USER or $LOGNAME for user name else determine
228 // the Real User, then determine the Real home dir.
229 static char * GetIniFile (char *dest
, const char *filename
)
232 if (filename
&& wxIsAbsolutePath(filename
))
234 strcpy(dest
, filename
);
236 else if ((home
= wxGetUserHome("")) != NULL
)
239 if (dest
[strlen(dest
) - 1] != '/')
241 if (filename
== NULL
)
243 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
244 filename
= ".Xdefaults";
246 else if (*filename
!= '.')
248 strcat (dest
, filename
);
258 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
260 if (create
&& wxFileExists (name
) ) {
262 return buf
; // Exists so ...
268 // Put in standard place for resource files if not absolute
269 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
271 strcat (buf
, (const char*) wxFileNameFromPath (name
));
275 // Touch the file to create it
276 FILE *fd
= fopen (buf
, "w");
283 * We have a cache for writing different resource files,
284 * which will only get flushed when we call wxFlushResources().
285 * Build up a list of resource databases waiting to be written.
289 wxList
wxResourceCache (wxKEY_STRING
);
292 wxFlushResources (void)
294 char nameBuffer
[512];
296 wxNode
*node
= wxResourceCache
.First ();
299 const char *file
= node
->GetKeyString();
300 // If file doesn't exist, create it first.
301 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
303 XrmDatabase database
= (XrmDatabase
) node
->Data ();
304 XrmPutFileDatabase (database
, nameBuffer
);
305 XrmDestroyDatabase (database
);
306 wxNode
*next
= node
->Next ();
312 static XrmDatabase wxResourceDatabase
= 0;
314 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
316 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
320 (void) GetIniFile (buffer
, file
);
322 XrmDatabase database
;
323 wxNode
*node
= wxResourceCache
.Find (buffer
);
325 database
= (XrmDatabase
) node
->Data ();
328 database
= XrmGetFileDatabase (buffer
);
329 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
333 strcpy (resName
, (const char*) section
);
334 strcat (resName
, ".");
335 strcat (resName
, (const char*) entry
);
337 XrmPutStringResource (&database
, resName
, value
);
341 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
344 sprintf(buf
, "%.4f", value
);
345 return wxWriteResource(section
, entry
, buf
, file
);
348 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
351 sprintf(buf
, "%ld", value
);
352 return wxWriteResource(section
, entry
, buf
, file
);
355 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
358 sprintf(buf
, "%d", value
);
359 return wxWriteResource(section
, entry
, buf
, file
);
362 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
364 if (!wxResourceDatabase
)
366 Display
*display
= (Display
*) wxGetDisplay();
367 wxXMergeDatabases (wxTheApp
, display
);
370 XrmDatabase database
;
376 // Is this right? Trying to get it to look in the user's
377 // home directory instead of current directory -- JACS
378 (void) GetIniFile (buffer
, file
);
380 wxNode
*node
= wxResourceCache
.Find (buffer
);
382 database
= (XrmDatabase
) node
->Data ();
385 database
= XrmGetFileDatabase (buffer
);
386 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
390 database
= wxResourceDatabase
;
395 strcpy (buf
, section
);
399 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
401 // Try different combinations of upper/lower case, just in case...
404 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
405 success
= XrmGetResource (database
, buf
, "*", str_type
,
413 *value
= new char[xvalue
.size
+ 1];
414 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
420 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
423 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
426 *value
= (float)strtod(s
, NULL
);
433 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
436 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
439 *value
= strtol(s
, NULL
, 10);
446 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
449 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
452 // Handle True, False here
453 // True, Yes, Enables, Set or Activated
454 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
456 // False, No, Disabled, Reset, Cleared, Deactivated
457 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
461 *value
= (int) strtol (s
, NULL
, 10);
469 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
471 XrmDatabase homeDB
, serverDB
, applicationDB
;
472 char filenamebuf
[1024];
474 char *filename
= &filenamebuf
[0];
476 wxString classname
= theApp
->GetClassName();
478 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
479 (void) strcat (name
, (const char*) classname
);
481 /* Get application defaults file, if any */
482 applicationDB
= XrmGetFileDatabase (name
);
483 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
485 /* Merge server defaults, created by xrdb, loaded as a property of the root
486 * window when the server initializes and loaded into the display
487 * structure on XOpenDisplay;
488 * if not defined, use .Xdefaults
491 if (XResourceManagerString (display
) != NULL
)
493 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
497 (void) GetIniFile (filename
, NULL
);
498 serverDB
= XrmGetFileDatabase (filename
);
500 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
502 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
503 * and merge into existing database
506 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
509 environment
= GetIniFile (filename
, NULL
);
510 len
= strlen (environment
);
511 wxString hostname
= wxGetHostName();
513 strncat(environment
, hostname
, 1024 - len
);
515 homeDB
= XrmGetFileDatabase (environment
);
516 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
522 * Not yet used but may be useful.
526 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
529 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
531 XrmDatabase rdb
= NULL
; // A resource data base
533 // Create an empty resource database
534 rdb
= XrmGetStringDatabase ("");
536 // Add the Component resources, prepending the name of the component
539 while (resourceSpec
[i
] != NULL
)
543 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
544 XrmPutLineResource (&rdb
, buf
);
547 // Merge them into the Xt database, with lowest precendence
551 #if (XlibSpecificationRelease>=5)
552 XrmDatabase db
= XtDatabase (dpy
);
553 XrmCombineDatabase (rdb
, &db
, FALSE
);
555 XrmMergeDatabases (dpy
->db
, &rdb
);
563 #endif // wxUSE_RESOURCES
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 void wxGetMousePosition( int* x
, int* y
)
573 XQueryPointer((Display
*) wxGetDisplay(),
574 DefaultRootWindow((Display
*) wxGetDisplay()),
576 &(xev
.x_root
), &(xev
.y_root
),
583 // Return TRUE if we have a colour display
584 bool wxColourDisplay()
586 return wxDisplayDepth() > 1;
589 // Returns depth of screen
592 Display
*dpy
= (Display
*) wxGetDisplay();
594 return DefaultDepth (dpy
, DefaultScreen (dpy
));
597 // Get size of display
598 void wxDisplaySize(int *width
, int *height
)
600 Display
*dpy
= (Display
*) wxGetDisplay();
603 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
605 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
608 void wxDisplaySizeMM(int *width
, int *height
)
610 Display
*dpy
= (Display
*) wxGetDisplay();
613 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
615 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
618 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
620 // This is supposed to return desktop dimensions minus any window
621 // manager panels, menus, taskbars, etc. If there is a way to do that
622 // for this platform please fix this function, otherwise it defaults
623 // to the entire desktop.
626 wxDisplaySize(width
, height
);
630 // Configurable display in wxX11 and wxMotif
631 static WXDisplay
*gs_currentDisplay
= NULL
;
632 static wxString gs_displayName
;
634 WXDisplay
*wxGetDisplay()
636 if (gs_currentDisplay
)
637 return gs_currentDisplay
;
639 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
640 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
642 return wxTheApp
->GetInitialDisplay();
646 return wxApp::GetDisplay();
650 bool wxSetDisplay(const wxString
& display_name
)
652 gs_displayName
= display_name
;
654 if ( display_name
.IsEmpty() )
656 gs_currentDisplay
= NULL
;
665 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
666 (const char*) display_name
,
667 (const char*) wxTheApp
->GetAppName(),
668 (const char*) wxTheApp
->GetClassName(),
670 #if XtSpecificationRelease < 5
679 gs_currentDisplay
= (WXDisplay
*) display
;
686 Display
* display
= XOpenDisplay((const char*) display_name
);
690 gs_currentDisplay
= (WXDisplay
*) display
;
699 wxString
wxGetDisplayName()
701 return gs_displayName
;
704 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
706 return wxGenericFindWindowAtPoint(pt
);
709 // ----------------------------------------------------------------------------
710 // keycode translations
711 // ----------------------------------------------------------------------------
713 #include <X11/keysym.h>
715 // FIXME what about tables??
717 int wxCharCodeXToWX(KeySym keySym
)
724 id
= WXK_SHIFT
; break;
727 id
= WXK_CONTROL
; break;
729 id
= WXK_BACK
; break;
731 id
= WXK_DELETE
; break;
733 id
= WXK_CLEAR
; break;
739 id
= WXK_RETURN
; break;
741 id
= WXK_ESCAPE
; break;
744 id
= WXK_PAUSE
; break;
746 id
= WXK_NUMLOCK
; break;
748 id
= WXK_SCROLL
; break;
751 id
= WXK_HOME
; break;
755 id
= WXK_LEFT
; break;
757 id
= WXK_RIGHT
; break;
761 id
= WXK_DOWN
; break;
763 id
= WXK_NEXT
; break;
765 id
= WXK_PRIOR
; break;
767 id
= WXK_MENU
; break;
769 id
= WXK_SELECT
; break;
771 id
= WXK_CANCEL
; break;
773 id
= WXK_PRINT
; break;
775 id
= WXK_EXECUTE
; break;
777 id
= WXK_INSERT
; break;
779 id
= WXK_HELP
; break;
782 id
= WXK_MULTIPLY
; break;
786 id
= WXK_SUBTRACT
; break;
788 id
= WXK_DIVIDE
; break;
790 id
= WXK_DECIMAL
; break;
798 id
= WXK_RETURN
; break;
800 id
= WXK_NUMPAD0
; break;
802 id
= WXK_NUMPAD1
; break;
804 id
= WXK_NUMPAD2
; break;
806 id
= WXK_NUMPAD3
; break;
808 id
= WXK_NUMPAD4
; break;
810 id
= WXK_NUMPAD5
; break;
812 id
= WXK_NUMPAD6
; break;
814 id
= WXK_NUMPAD7
; break;
816 id
= WXK_NUMPAD8
; break;
818 id
= WXK_NUMPAD9
; break;
868 id
= (keySym
<= 255) ? (int)keySym
: -1;
874 KeySym
wxCharCodeWXToX(int id
)
880 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
881 case WXK_BACK
: keySym
= XK_BackSpace
; break;
882 case WXK_TAB
: keySym
= XK_Tab
; break;
883 case WXK_CLEAR
: keySym
= XK_Clear
; break;
884 case WXK_RETURN
: keySym
= XK_Return
; break;
885 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
886 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
887 case WXK_MENU
: keySym
= XK_Menu
; break;
888 case WXK_PAUSE
: keySym
= XK_Pause
; break;
889 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
890 case WXK_SPACE
: keySym
= ' '; break;
891 case WXK_PRIOR
: keySym
= XK_Prior
; break;
892 case WXK_NEXT
: keySym
= XK_Next
; break;
893 case WXK_END
: keySym
= XK_End
; break;
894 case WXK_HOME
: keySym
= XK_Home
; break;
895 case WXK_LEFT
: keySym
= XK_Left
; break;
896 case WXK_UP
: keySym
= XK_Up
; break;
897 case WXK_RIGHT
: keySym
= XK_Right
; break;
898 case WXK_DOWN
: keySym
= XK_Down
; break;
899 case WXK_SELECT
: keySym
= XK_Select
; break;
900 case WXK_PRINT
: keySym
= XK_Print
; break;
901 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
902 case WXK_INSERT
: keySym
= XK_Insert
; break;
903 case WXK_DELETE
: keySym
= XK_Delete
; break;
904 case WXK_HELP
: keySym
= XK_Help
; break;
905 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
906 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
907 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
908 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
909 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
910 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
911 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
912 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
913 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
914 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
915 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
916 case WXK_ADD
: keySym
= XK_KP_Add
; break;
917 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
918 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
919 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
920 case WXK_F1
: keySym
= XK_F1
; break;
921 case WXK_F2
: keySym
= XK_F2
; break;
922 case WXK_F3
: keySym
= XK_F3
; break;
923 case WXK_F4
: keySym
= XK_F4
; break;
924 case WXK_F5
: keySym
= XK_F5
; break;
925 case WXK_F6
: keySym
= XK_F6
; break;
926 case WXK_F7
: keySym
= XK_F7
; break;
927 case WXK_F8
: keySym
= XK_F8
; break;
928 case WXK_F9
: keySym
= XK_F9
; break;
929 case WXK_F10
: keySym
= XK_F10
; break;
930 case WXK_F11
: keySym
= XK_F11
; break;
931 case WXK_F12
: keySym
= XK_F12
; break;
932 case WXK_F13
: keySym
= XK_F13
; break;
933 case WXK_F14
: keySym
= XK_F14
; break;
934 case WXK_F15
: keySym
= XK_F15
; break;
935 case WXK_F16
: keySym
= XK_F16
; break;
936 case WXK_F17
: keySym
= XK_F17
; break;
937 case WXK_F18
: keySym
= XK_F18
; break;
938 case WXK_F19
: keySym
= XK_F19
; break;
939 case WXK_F20
: keySym
= XK_F20
; break;
940 case WXK_F21
: keySym
= XK_F21
; break;
941 case WXK_F22
: keySym
= XK_F22
; break;
942 case WXK_F23
: keySym
= XK_F23
; break;
943 case WXK_F24
: keySym
= XK_F24
; break;
944 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
945 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
946 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
952 // ----------------------------------------------------------------------------
953 // Some colour manipulation routines
954 // ----------------------------------------------------------------------------
956 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
961 int r
= 0, g
= 0, b
= 0;
964 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
965 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
967 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
970 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
971 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
972 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
975 case 0: r
= v
, g
= t
, b
= p
; break;
976 case 1: r
= q
, g
= v
, b
= p
; break;
977 case 2: r
= p
, g
= v
, b
= t
; break;
978 case 3: r
= p
, g
= q
, b
= v
; break;
979 case 4: r
= t
, g
= p
, b
= v
; break;
980 case 5: r
= v
, g
= p
, b
= q
; break;
987 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
989 int r
= rgb
->red
>> 8;
990 int g
= rgb
->green
>> 8;
991 int b
= rgb
->blue
>> 8;
992 int maxv
= wxMax3(r
, g
, b
);
993 int minv
= wxMin3(r
, g
, b
);
996 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
1001 int rc
, gc
, bc
, hex
= 0;
1002 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
1003 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
1004 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
1005 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
1006 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
1007 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
1008 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
1009 if (h
< 0) h
+= 360;
1012 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
1013 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
1016 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1020 int screen
= DefaultScreen(d
);
1021 int num_colors
= DisplayCells(d
,screen
);
1023 XColor
*color_defs
= new XColor
[num_colors
];
1024 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
1025 XQueryColors(d
,cmp
,color_defs
,num_colors
);
1027 wxHSV hsv_defs
, hsv
;
1028 wxXColorToHSV(&hsv
,xc
);
1030 int diff
, min_diff
= 0, pixel
= 0;
1032 for(llp
= 0;llp
< num_colors
;llp
++)
1034 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
1035 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
1036 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
1037 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
1038 if (llp
== 0) min_diff
= diff
;
1039 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
1040 if (min_diff
== 0) break;
1043 xc
-> red
= color_defs
[pixel
].red
;
1044 xc
-> green
= color_defs
[pixel
].green
;
1045 xc
-> blue
= color_defs
[pixel
].blue
;
1046 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
1049 if (!XAllocColor(d,cmp,xc))
1050 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1053 delete[] color_defs
;
1056 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1058 if (!XAllocColor(d
,cmp
,xc
))
1060 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1061 wxAllocNearestColor(d
,cmp
,xc
);
1066 wxString
wxGetXEventName(XEvent
& event
)
1068 int type
= event
.xany
.type
;
1069 static char* event_name
[] = {
1070 "", "unknown(-)", // 0-1
1071 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1072 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1073 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1074 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1075 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1076 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1077 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1078 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1079 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1080 "ClientMessage", "MappingNotify", // 33-34
1081 "unknown(+)"}; // 35
1082 type
= wxMin(35, type
); type
= wxMax(1, type
);
1083 wxString
str(event_name
[type
]);
1089 // ----------------------------------------------------------------------------
1091 // ----------------------------------------------------------------------------
1093 // Find the letter corresponding to the mnemonic, for Motif
1094 char wxFindMnemonic (const char *s
)
1097 int len
= strlen (s
);
1099 for (i
= 0; i
< len
; i
++)
1103 // Carefully handle &&
1104 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1116 char * wxFindAccelerator (const char *s
)
1118 // VZ: this function returns incorrect keysym which completely breaks kbd
1123 // The accelerator text is after the \t char.
1124 while (*s
&& *s
!= '\t')
1130 Now we need to format it as X standard:
1135 Ctrl+N --> Ctrl<Key>N
1136 Alt+k --> Meta<Key>k
1137 Ctrl+Shift+A --> Ctrl Shift<Key>A
1141 static char buf
[256];
1143 char *tmp
= copystring (s
);
1149 while (*p
&& *p
!= '+')
1156 if (strcmp (s
, "Alt"))
1159 strcat (buf
, "Meta");
1164 strcat (buf
, "<Key>");
1174 XmString
wxFindAcceleratorText (const char *s
)
1176 // VZ: this function returns incorrect keysym which completely breaks kbd
1181 // The accelerator text is after the \t char.
1182 while (*s
&& *s
!= '\t')
1187 XmString text
= XmStringCreateSimple ((char *)s
);
1193 // These functions duplicate those in wxWindow, but are needed
1194 // for use outside of wxWindow (e.g. wxMenu, wxMenuBar).
1196 // Change a widget's foreground and background colours.
1198 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1200 // When should we specify the foreground, if it's calculated
1201 // by wxComputeColours?
1202 // Solution: say we start with the default (computed) foreground colour.
1203 // If we call SetForegroundColour explicitly for a control or window,
1204 // then the foreground is changed.
1205 // Therefore SetBackgroundColour computes the foreground colour, and
1206 // SetForegroundColour changes the foreground colour. The ordering is
1209 XtVaSetValues ((Widget
) widget
,
1210 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1214 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1216 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1219 XtVaSetValues ((Widget
) widget
,
1220 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1221 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1222 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1223 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1226 if (changeArmColour
)
1227 XtVaSetValues ((Widget
) widget
,
1228 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1235 bool wxWindowIsVisible(Window win
)
1237 XWindowAttributes wa
;
1238 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
1240 return (wa
.map_state
== IsViewable
);