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>
42 #if (defined(__SUNCC__) || defined(__CLCC__))
47 #pragma message disable nosimpint
50 #include "wx/unix/execute.h"
53 #include "wx/motif/private.h"
56 #include "X11/Xresource.h"
59 #include "X11/Xutil.h"
62 #pragma message enable nosimpint
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 // Yuck this is really BOTH site and platform dependent
70 // so we should use some other strategy!
72 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
74 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
78 static char *GetIniFile (char *dest
, const char *filename
);
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
86 // async event processing
87 // ----------------------------------------------------------------------------
89 // Consume all events until no more left
92 Display
*display
= (Display
*) wxGetDisplay();
94 XSync (display
, FALSE
);
96 // XtAppPending returns availability of events AND timers/inputs, which
97 // are processed via callbacks, so XtAppNextEvent will not return if
98 // there are no events. So added '& XtIMXEvent' - Sergey.
99 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
101 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
102 // Jan Lessner: works better when events are non-X events
103 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
108 // Check whether this window wants to process messages, e.g. Stop button
109 // in long calculations.
110 bool wxCheckForInterrupt(wxWindow
*wnd
)
112 wxCHECK_MSG( wnd
, FALSE
, "NULL window in wxCheckForInterrupt" );
114 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
115 Window win
=(Window
) wnd
->GetXWindow();
118 if (wnd
->GetMainWidget())
120 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
123 bool hadEvents
= FALSE
;
124 while( XCheckMaskEvent(dpy
,
125 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
126 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
129 if ( event
.xany
.window
== win
)
133 XtDispatchEvent(&event
);
141 // ----------------------------------------------------------------------------
143 // ----------------------------------------------------------------------------
145 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
148 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
150 wxHandleProcessTermination(proc_data
);
152 // VZ: I think they should be the same...
153 wxASSERT( (int)*id
== proc_data
->tag
);
158 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
160 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
162 (XtPointer
*) XtInputReadMask
,
163 (XtInputCallbackProc
) xt_notify_end_process
,
164 (XtPointer
) proc_data
);
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
176 // Use current setting for the bell
177 XBell ((Display
*) wxGetDisplay(), 0);
180 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
183 // This code is WRONG!! Does NOT return the
184 // Motif version of the libs but the X protocol
186 Display
*display
= XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
188 *majorVsn
= ProtocolVersion (display
);
190 *minorVsn
= ProtocolRevision (display
);
195 // ----------------------------------------------------------------------------
196 // Reading and writing resources (eg WIN.INI, .Xdefaults)
197 // ----------------------------------------------------------------------------
201 // Read $HOME for what it says is home, if not
202 // read $USER or $LOGNAME for user name else determine
203 // the Real User, then determine the Real home dir.
204 static char * GetIniFile (char *dest
, const char *filename
)
207 if (filename
&& wxIsAbsolutePath(filename
))
209 strcpy(dest
, filename
);
211 else if ((home
= wxGetUserHome("")) != NULL
)
214 if (dest
[strlen(dest
) - 1] != '/')
216 if (filename
== NULL
)
218 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
219 filename
= ".Xdefaults";
221 else if (*filename
!= '.')
223 strcat (dest
, filename
);
231 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
233 if (create
&& wxFileExists (name
) ) {
235 return buf
; // Exists so ...
241 // Put in standard place for resource files if not absolute
242 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
244 strcat (buf
, wxFileNameFromPath (name
).c_str());
248 // Touch the file to create it
249 FILE *fd
= fopen (buf
, "w");
256 * We have a cache for writing different resource files,
257 * which will only get flushed when we call wxFlushResources().
258 * Build up a list of resource databases waiting to be written.
262 wxList
wxResourceCache (wxKEY_STRING
);
265 wxFlushResources (void)
267 char nameBuffer
[512];
269 wxNode
*node
= wxResourceCache
.First ();
272 const char *file
= node
->GetKeyString();
273 // If file doesn't exist, create it first.
274 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
276 XrmDatabase database
= (XrmDatabase
) node
->Data ();
277 XrmPutFileDatabase (database
, nameBuffer
);
278 XrmDestroyDatabase (database
);
279 wxNode
*next
= node
->Next ();
285 static XrmDatabase wxResourceDatabase
= 0;
287 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
289 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
293 (void) GetIniFile (buffer
, file
);
295 XrmDatabase database
;
296 wxNode
*node
= wxResourceCache
.Find (buffer
);
298 database
= (XrmDatabase
) node
->Data ();
301 database
= XrmGetFileDatabase (buffer
);
302 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
306 strcpy (resName
, section
.c_str());
307 strcat (resName
, ".");
308 strcat (resName
, entry
.c_str());
310 XrmPutStringResource (&database
, resName
, value
);
314 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
317 sprintf(buf
, "%.4f", value
);
318 return wxWriteResource(section
, entry
, buf
, file
);
321 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
324 sprintf(buf
, "%ld", value
);
325 return wxWriteResource(section
, entry
, buf
, file
);
328 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
331 sprintf(buf
, "%d", value
);
332 return wxWriteResource(section
, entry
, buf
, file
);
335 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
337 if (!wxResourceDatabase
)
339 Display
*display
= (Display
*) wxGetDisplay();
340 wxXMergeDatabases (wxTheApp
, display
);
343 XrmDatabase database
;
349 // Is this right? Trying to get it to look in the user's
350 // home directory instead of current directory -- JACS
351 (void) GetIniFile (buffer
, file
);
353 wxNode
*node
= wxResourceCache
.Find (buffer
);
355 database
= (XrmDatabase
) node
->Data ();
358 database
= XrmGetFileDatabase (buffer
);
359 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
363 database
= wxResourceDatabase
;
368 strcpy (buf
, section
);
372 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
374 // Try different combinations of upper/lower case, just in case...
377 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
378 success
= XrmGetResource (database
, buf
, "*", str_type
,
386 *value
= new char[xvalue
.size
+ 1];
387 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
393 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
396 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
399 *value
= (float)strtod(s
, NULL
);
406 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
409 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
412 *value
= strtol(s
, NULL
, 10);
419 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
422 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
425 // Handle True, False here
426 // True, Yes, Enables, Set or Activated
427 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
429 // False, No, Disabled, Reset, Cleared, Deactivated
430 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
434 *value
= (int) strtol (s
, NULL
, 10);
442 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
444 XrmDatabase homeDB
, serverDB
, applicationDB
;
445 char filenamebuf
[1024];
447 char *filename
= &filenamebuf
[0];
449 wxString classname
= theApp
->GetClassName();
451 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
452 (void) strcat (name
, classname
.c_str());
454 /* Get application defaults file, if any */
455 applicationDB
= XrmGetFileDatabase (name
);
456 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
458 /* Merge server defaults, created by xrdb, loaded as a property of the root
459 * window when the server initializes and loaded into the display
460 * structure on XOpenDisplay;
461 * if not defined, use .Xdefaults
464 if (XResourceManagerString (display
) != NULL
)
466 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
470 (void) GetIniFile (filename
, NULL
);
471 serverDB
= XrmGetFileDatabase (filename
);
473 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
475 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
476 * and merge into existing database
479 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
482 environment
= GetIniFile (filename
, NULL
);
483 len
= strlen (environment
);
484 wxString hostname
= wxGetHostName();
486 strncat(environment
, hostname
, 1024 - len
);
488 homeDB
= XrmGetFileDatabase (environment
);
489 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
495 * Not yet used but may be useful.
499 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
502 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
504 XrmDatabase rdb
= NULL
; // A resource data base
506 // Create an empty resource database
507 rdb
= XrmGetStringDatabase ("");
509 // Add the Component resources, prepending the name of the component
512 while (resourceSpec
[i
] != NULL
)
516 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
517 XrmPutLineResource (&rdb
, buf
);
520 // Merge them into the Xt database, with lowest precendence
524 #if (XlibSpecificationRelease>=5)
525 XrmDatabase db
= XtDatabase (dpy
);
526 XrmCombineDatabase (rdb
, &db
, FALSE
);
528 XrmMergeDatabases (dpy
->db
, &rdb
);
536 #endif // wxUSE_RESOURCES
538 // ----------------------------------------------------------------------------
540 // ----------------------------------------------------------------------------
542 void wxGetMousePosition( int* x
, int* y
)
551 XQueryPointer((Display
*) wxGetDisplay(),
552 DefaultRootWindow((Display
*) wxGetDisplay()),
554 &(xev
.x_root
), &(xev
.y_root
),
562 // Return TRUE if we have a colour display
563 bool wxColourDisplay()
565 return wxDisplayDepth() > 1;
568 // Returns depth of screen
571 Display
*dpy
= (Display
*) wxGetDisplay();
573 return DefaultDepth (dpy
, DefaultScreen (dpy
));
576 // Get size of display
577 void wxDisplaySize(int *width
, int *height
)
579 Display
*dpy
= (Display
*) wxGetDisplay();
582 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
584 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
587 void wxDisplaySizeMM(int *width
, int *height
)
589 Display
*dpy
= (Display
*) wxGetDisplay();
592 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
594 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
597 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
599 // This is supposed to return desktop dimensions minus any window
600 // manager panels, menus, taskbars, etc. If there is a way to do that
601 // for this platform please fix this function, otherwise it defaults
602 // to the entire desktop.
605 wxDisplaySize(width
, height
);
609 // Configurable display in wxX11 and wxMotif
610 static WXDisplay
*gs_currentDisplay
= NULL
;
611 static wxString gs_displayName
;
613 WXDisplay
*wxGetDisplay()
615 if (gs_currentDisplay
)
616 return gs_currentDisplay
;
617 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
618 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
620 return wxTheApp
->GetInitialDisplay();
624 bool wxSetDisplay(const wxString
& display_name
)
626 gs_displayName
= display_name
;
628 if ( display_name
.IsEmpty() )
630 gs_currentDisplay
= NULL
;
638 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
639 display_name
.c_str(),
640 wxTheApp
->GetAppName().c_str(),
641 wxTheApp
->GetClassName().c_str(),
643 #if XtSpecificationRelease < 5
652 gs_currentDisplay
= (WXDisplay
*) display
;
660 wxString
wxGetDisplayName()
662 return gs_displayName
;
665 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
667 return wxGenericFindWindowAtPoint(pt
);
670 // ----------------------------------------------------------------------------
671 // keycode translations
672 // ----------------------------------------------------------------------------
674 #include <X11/keysym.h>
676 // FIXME what about tables??
678 int wxCharCodeXToWX(KeySym keySym
)
685 id
= WXK_SHIFT
; break;
688 id
= WXK_CONTROL
; break;
690 id
= WXK_BACK
; break;
692 id
= WXK_DELETE
; break;
694 id
= WXK_CLEAR
; break;
700 id
= WXK_RETURN
; break;
702 id
= WXK_ESCAPE
; break;
705 id
= WXK_PAUSE
; break;
707 id
= WXK_NUMLOCK
; break;
709 id
= WXK_SCROLL
; break;
712 id
= WXK_HOME
; break;
716 id
= WXK_LEFT
; break;
718 id
= WXK_RIGHT
; break;
722 id
= WXK_DOWN
; break;
724 id
= WXK_NEXT
; break;
726 id
= WXK_PRIOR
; break;
728 id
= WXK_MENU
; break;
730 id
= WXK_SELECT
; break;
732 id
= WXK_CANCEL
; break;
734 id
= WXK_PRINT
; break;
736 id
= WXK_EXECUTE
; break;
738 id
= WXK_INSERT
; break;
740 id
= WXK_HELP
; break;
743 id
= WXK_MULTIPLY
; break;
747 id
= WXK_SUBTRACT
; break;
749 id
= WXK_DIVIDE
; break;
751 id
= WXK_DECIMAL
; break;
759 id
= WXK_RETURN
; break;
761 id
= WXK_NUMPAD0
; break;
763 id
= WXK_NUMPAD1
; break;
765 id
= WXK_NUMPAD2
; break;
767 id
= WXK_NUMPAD3
; break;
769 id
= WXK_NUMPAD4
; break;
771 id
= WXK_NUMPAD5
; break;
773 id
= WXK_NUMPAD6
; break;
775 id
= WXK_NUMPAD7
; break;
777 id
= WXK_NUMPAD8
; break;
779 id
= WXK_NUMPAD9
; break;
829 id
= (keySym
<= 255) ? (int)keySym
: -1;
835 KeySym
wxCharCodeWXToX(int id
)
841 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
842 case WXK_BACK
: keySym
= XK_BackSpace
; break;
843 case WXK_TAB
: keySym
= XK_Tab
; break;
844 case WXK_CLEAR
: keySym
= XK_Clear
; break;
845 case WXK_RETURN
: keySym
= XK_Return
; break;
846 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
847 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
848 case WXK_MENU
: keySym
= XK_Menu
; break;
849 case WXK_PAUSE
: keySym
= XK_Pause
; break;
850 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
851 case WXK_SPACE
: keySym
= ' '; break;
852 case WXK_PRIOR
: keySym
= XK_Prior
; break;
853 case WXK_NEXT
: keySym
= XK_Next
; break;
854 case WXK_END
: keySym
= XK_End
; break;
855 case WXK_HOME
: keySym
= XK_Home
; break;
856 case WXK_LEFT
: keySym
= XK_Left
; break;
857 case WXK_UP
: keySym
= XK_Up
; break;
858 case WXK_RIGHT
: keySym
= XK_Right
; break;
859 case WXK_DOWN
: keySym
= XK_Down
; break;
860 case WXK_SELECT
: keySym
= XK_Select
; break;
861 case WXK_PRINT
: keySym
= XK_Print
; break;
862 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
863 case WXK_INSERT
: keySym
= XK_Insert
; break;
864 case WXK_DELETE
: keySym
= XK_Delete
; break;
865 case WXK_HELP
: keySym
= XK_Help
; break;
866 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
867 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
868 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
869 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
870 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
871 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
872 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
873 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
874 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
875 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
876 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
877 case WXK_ADD
: keySym
= XK_KP_Add
; break;
878 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
879 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
880 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
881 case WXK_F1
: keySym
= XK_F1
; break;
882 case WXK_F2
: keySym
= XK_F2
; break;
883 case WXK_F3
: keySym
= XK_F3
; break;
884 case WXK_F4
: keySym
= XK_F4
; break;
885 case WXK_F5
: keySym
= XK_F5
; break;
886 case WXK_F6
: keySym
= XK_F6
; break;
887 case WXK_F7
: keySym
= XK_F7
; break;
888 case WXK_F8
: keySym
= XK_F8
; break;
889 case WXK_F9
: keySym
= XK_F9
; break;
890 case WXK_F10
: keySym
= XK_F10
; break;
891 case WXK_F11
: keySym
= XK_F11
; break;
892 case WXK_F12
: keySym
= XK_F12
; break;
893 case WXK_F13
: keySym
= XK_F13
; break;
894 case WXK_F14
: keySym
= XK_F14
; break;
895 case WXK_F15
: keySym
= XK_F15
; break;
896 case WXK_F16
: keySym
= XK_F16
; break;
897 case WXK_F17
: keySym
= XK_F17
; break;
898 case WXK_F18
: keySym
= XK_F18
; break;
899 case WXK_F19
: keySym
= XK_F19
; break;
900 case WXK_F20
: keySym
= XK_F20
; break;
901 case WXK_F21
: keySym
= XK_F21
; break;
902 case WXK_F22
: keySym
= XK_F22
; break;
903 case WXK_F23
: keySym
= XK_F23
; break;
904 case WXK_F24
: keySym
= XK_F24
; break;
905 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
906 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
907 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
913 // ----------------------------------------------------------------------------
914 // Some colour manipulation routines
915 // ----------------------------------------------------------------------------
917 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
922 int r
= 0, g
= 0, b
= 0;
925 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
926 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
928 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
931 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
932 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
933 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
936 case 0: r
= v
, g
= t
, b
= p
; break;
937 case 1: r
= q
, g
= v
, b
= p
; break;
938 case 2: r
= p
, g
= v
, b
= t
; break;
939 case 3: r
= p
, g
= q
, b
= v
; break;
940 case 4: r
= t
, g
= p
, b
= v
; break;
941 case 5: r
= v
, g
= p
, b
= q
; break;
948 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
950 int r
= rgb
->red
>> 8;
951 int g
= rgb
->green
>> 8;
952 int b
= rgb
->blue
>> 8;
953 int maxv
= wxMax3(r
, g
, b
);
954 int minv
= wxMin3(r
, g
, b
);
957 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
962 int rc
, gc
, bc
, hex
= 0;
963 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
964 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
965 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
966 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
967 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
968 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
969 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
973 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
974 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
977 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
982 int screen
= DefaultScreen(d
);
983 int num_colors
= DisplayCells(d
,screen
);
985 XColor
*color_defs
= new XColor
[num_colors
];
986 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
987 XQueryColors(d
,cmp
,color_defs
,num_colors
);
990 wxXColorToHSV(&hsv
,xc
);
992 int diff
, min_diff
= 0, pixel
= 0;
994 for(llp
= 0;llp
< num_colors
;llp
++)
996 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
997 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
998 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
999 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
1000 if (llp
== 0) min_diff
= diff
;
1001 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
1002 if (min_diff
== 0) break;
1005 xc
-> red
= color_defs
[pixel
].red
;
1006 xc
-> green
= color_defs
[pixel
].green
;
1007 xc
-> blue
= color_defs
[pixel
].blue
;
1008 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
1011 if (!XAllocColor(d,cmp,xc))
1012 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1015 delete[] color_defs
;
1019 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1021 if (!XAllocColor(d
,cmp
,xc
))
1023 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1024 wxAllocNearestColor(d
,cmp
,xc
);
1029 wxString
wxGetXEventName(XEvent
& event
)
1032 wxString
str(wxT("(some event)"));
1035 int type
= event
.xany
.type
;
1036 static char* event_name
[] = {
1037 "", "unknown(-)", // 0-1
1038 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1039 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1040 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1041 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1042 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1043 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1044 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1045 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1046 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1047 "ClientMessage", "MappingNotify", // 33-34
1048 "unknown(+)"}; // 35
1049 type
= wxMin(35, type
); type
= wxMax(1, type
);
1050 wxString
str(event_name
[type
]);
1056 // ----------------------------------------------------------------------------
1058 // ----------------------------------------------------------------------------
1060 // Find the letter corresponding to the mnemonic, for Motif
1061 char wxFindMnemonic (const char *s
)
1064 int len
= strlen (s
);
1066 for (i
= 0; i
< len
; i
++)
1070 // Carefully handle &&
1071 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1083 char * wxFindAccelerator (const char *s
)
1085 // VZ: this function returns incorrect keysym which completely breaks kbd
1090 // The accelerator text is after the \t char.
1091 while (*s
&& *s
!= '\t')
1097 Now we need to format it as X standard:
1102 Ctrl+N --> Ctrl<Key>N
1103 Alt+k --> Meta<Key>k
1104 Ctrl+Shift+A --> Ctrl Shift<Key>A
1108 static char buf
[256];
1110 char *tmp
= copystring (s
);
1116 while (*p
&& *p
!= '+')
1123 if (strcmp (s
, "Alt"))
1126 strcat (buf
, "Meta");
1131 strcat (buf
, "<Key>");
1141 XmString
wxFindAcceleratorText (const char *s
)
1143 // VZ: this function returns incorrect keysym which completely breaks kbd
1148 // The accelerator text is after the \t char.
1149 while (*s
&& *s
!= '\t')
1154 XmString text
= XmStringCreateSimple ((char *)s
);
1160 // Change a widget's foreground and background colours.
1162 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1164 // When should we specify the foreground, if it's calculated
1165 // by wxComputeColours?
1166 // Solution: say we start with the default (computed) foreground colour.
1167 // If we call SetForegroundColour explicitly for a control or window,
1168 // then the foreground is changed.
1169 // Therefore SetBackgroundColour computes the foreground colour, and
1170 // SetForegroundColour changes the foreground colour. The ordering is
1173 XtVaSetValues ((Widget
) widget
,
1174 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1178 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1180 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1183 XtVaSetValues ((Widget
) widget
,
1184 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1185 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1186 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1187 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1190 if (changeArmColour
)
1191 XtVaSetValues ((Widget
) widget
,
1192 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1196 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1198 // Lesstif 0.87 hangs here, but 0.93 does not
1199 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1200 Widget w
= (Widget
)widget
;
1202 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1208 bool wxWindowIsVisible(Window win
)
1210 XWindowAttributes wa
;
1211 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
1213 return (wa
.map_state
== IsViewable
);
1216 wxString
wxXmStringToString( const XmString
& xmString
)
1219 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1226 return wxEmptyString
;
1229 XmString
wxStringToXmString( const wxString
& str
)
1231 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1234 XmString
wxStringToXmString( const char* str
)
1236 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);