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/dcmemory.h"
29 #include "wx/bitmap.h"
36 #include <sys/types.h>
43 #if (defined(__SUNCC__) || defined(__CLCC__))
48 #pragma message disable nosimpint
51 #include "wx/unix/execute.h"
54 #include "wx/motif/private.h"
57 #include "X11/Xresource.h"
60 #include "X11/Xutil.h"
63 #pragma message enable nosimpint
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Yuck this is really BOTH site and platform dependent
71 // so we should use some other strategy!
73 #define DEFAULT_XRESOURCE_DIR "/usr/openwin/lib/app-defaults"
75 #define DEFAULT_XRESOURCE_DIR "/usr/lib/X11/app-defaults"
79 static char *GetIniFile (char *dest
, const char *filename
);
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
87 // async event processing
88 // ----------------------------------------------------------------------------
90 // Consume all events until no more left
93 Display
*display
= (Display
*) wxGetDisplay();
95 XSync (display
, FALSE
);
97 // XtAppPending returns availability of events AND timers/inputs, which
98 // are processed via callbacks, so XtAppNextEvent will not return if
99 // there are no events. So added '& XtIMXEvent' - Sergey.
100 while (XtAppPending ((XtAppContext
) wxTheApp
->GetAppContext()) & XtIMXEvent
)
102 XFlush (XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget()));
103 // Jan Lessner: works better when events are non-X events
104 XtAppProcessEvent((XtAppContext
) wxTheApp
->GetAppContext(), XtIMXEvent
);
109 // Check whether this window wants to process messages, e.g. Stop button
110 // in long calculations.
111 bool wxCheckForInterrupt(wxWindow
*wnd
)
113 wxCHECK_MSG( wnd
, FALSE
, "NULL window in wxCheckForInterrupt" );
115 Display
*dpy
=(Display
*) wnd
->GetXDisplay();
116 Window win
=(Window
) wnd
->GetXWindow();
119 if (wnd
->GetMainWidget())
121 XmUpdateDisplay((Widget
)(wnd
->GetMainWidget()));
124 bool hadEvents
= FALSE
;
125 while( XCheckMaskEvent(dpy
,
126 ButtonPressMask
|ButtonReleaseMask
|ButtonMotionMask
|
127 PointerMotionMask
|KeyPressMask
|KeyReleaseMask
,
130 if ( event
.xany
.window
== win
)
134 XtDispatchEvent(&event
);
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 static void xt_notify_end_process(XtPointer data
, int *WXUNUSED(fid
),
149 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
151 wxHandleProcessTermination(proc_data
);
153 // VZ: I think they should be the same...
154 wxASSERT( (int)*id
== proc_data
->tag
);
159 int wxAddProcessCallback(wxEndProcessData
*proc_data
, int fd
)
161 XtInputId id
= XtAppAddInput((XtAppContext
) wxTheApp
->GetAppContext(),
163 (XtPointer
*) XtInputReadMask
,
164 (XtInputCallbackProc
) xt_notify_end_process
,
165 (XtPointer
) proc_data
);
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
177 // Use current setting for the bell
178 XBell ((Display
*) wxGetDisplay(), 0);
181 int wxGetOsVersion(int *majorVsn
, int *minorVsn
)
184 // This code is WRONG!! Does NOT return the
185 // Motif version of the libs but the X protocol
187 Display
*display
= XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
189 *majorVsn
= ProtocolVersion (display
);
191 *minorVsn
= ProtocolRevision (display
);
196 // ----------------------------------------------------------------------------
197 // Reading and writing resources (eg WIN.INI, .Xdefaults)
198 // ----------------------------------------------------------------------------
202 // Read $HOME for what it says is home, if not
203 // read $USER or $LOGNAME for user name else determine
204 // the Real User, then determine the Real home dir.
205 static char * GetIniFile (char *dest
, const char *filename
)
208 if (filename
&& wxIsAbsolutePath(filename
))
210 strcpy(dest
, filename
);
212 else if ((home
= wxGetUserHome("")) != NULL
)
215 if (dest
[strlen(dest
) - 1] != '/')
217 if (filename
== NULL
)
219 if ((filename
= getenv ("XENVIRONMENT")) == NULL
)
220 filename
= ".Xdefaults";
222 else if (*filename
!= '.')
224 strcat (dest
, filename
);
232 static char *GetResourcePath(char *buf
, const char *name
, bool create
= FALSE
)
234 if (create
&& wxFileExists (name
) ) {
236 return buf
; // Exists so ...
242 // Put in standard place for resource files if not absolute
243 strcpy (buf
, DEFAULT_XRESOURCE_DIR
);
245 strcat (buf
, wxFileNameFromPath (name
).c_str());
249 // Touch the file to create it
250 FILE *fd
= fopen (buf
, "w");
257 * We have a cache for writing different resource files,
258 * which will only get flushed when we call wxFlushResources().
259 * Build up a list of resource databases waiting to be written.
263 wxList
wxResourceCache (wxKEY_STRING
);
266 wxFlushResources (void)
268 char nameBuffer
[512];
270 wxNode
*node
= wxResourceCache
.First ();
273 const char *file
= node
->GetKeyString();
274 // If file doesn't exist, create it first.
275 (void)GetResourcePath(nameBuffer
, file
, TRUE
);
277 XrmDatabase database
= (XrmDatabase
) node
->Data ();
278 XrmPutFileDatabase (database
, nameBuffer
);
279 XrmDestroyDatabase (database
);
280 wxNode
*next
= node
->Next ();
286 static XrmDatabase wxResourceDatabase
= 0;
288 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
);
290 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, const wxString
& value
, const wxString
& file
)
294 (void) GetIniFile (buffer
, file
);
296 XrmDatabase database
;
297 wxNode
*node
= wxResourceCache
.Find (buffer
);
299 database
= (XrmDatabase
) node
->Data ();
302 database
= XrmGetFileDatabase (buffer
);
303 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
307 strcpy (resName
, section
.c_str());
308 strcat (resName
, ".");
309 strcat (resName
, entry
.c_str());
311 XrmPutStringResource (&database
, resName
, value
);
315 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, float value
, const wxString
& file
)
318 sprintf(buf
, "%.4f", value
);
319 return wxWriteResource(section
, entry
, buf
, file
);
322 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, long value
, const wxString
& file
)
325 sprintf(buf
, "%ld", value
);
326 return wxWriteResource(section
, entry
, buf
, file
);
329 bool wxWriteResource(const wxString
& section
, const wxString
& entry
, int value
, const wxString
& file
)
332 sprintf(buf
, "%d", value
);
333 return wxWriteResource(section
, entry
, buf
, file
);
336 bool wxGetResource(const wxString
& section
, const wxString
& entry
, char **value
, const wxString
& file
)
338 if (!wxResourceDatabase
)
340 Display
*display
= (Display
*) wxGetDisplay();
341 wxXMergeDatabases (wxTheApp
, display
);
344 XrmDatabase database
;
350 // Is this right? Trying to get it to look in the user's
351 // home directory instead of current directory -- JACS
352 (void) GetIniFile (buffer
, file
);
354 wxNode
*node
= wxResourceCache
.Find (buffer
);
356 database
= (XrmDatabase
) node
->Data ();
359 database
= XrmGetFileDatabase (buffer
);
360 wxResourceCache
.Append (buffer
, (wxObject
*) database
);
364 database
= wxResourceDatabase
;
369 strcpy (buf
, section
);
373 Bool success
= XrmGetResource (database
, buf
, "*", str_type
,
375 // Try different combinations of upper/lower case, just in case...
378 buf
[0] = (isupper (buf
[0]) ? tolower (buf
[0]) : toupper (buf
[0]));
379 success
= XrmGetResource (database
, buf
, "*", str_type
,
387 *value
= new char[xvalue
.size
+ 1];
388 strncpy (*value
, xvalue
.addr
, (int) xvalue
.size
);
394 bool wxGetResource(const wxString
& section
, const wxString
& entry
, float *value
, const wxString
& file
)
397 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
400 *value
= (float)strtod(s
, NULL
);
407 bool wxGetResource(const wxString
& section
, const wxString
& entry
, long *value
, const wxString
& file
)
410 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
413 *value
= strtol(s
, NULL
, 10);
420 bool wxGetResource(const wxString
& section
, const wxString
& entry
, int *value
, const wxString
& file
)
423 bool succ
= wxGetResource(section
, entry
, (char **)&s
, file
);
426 // Handle True, False here
427 // True, Yes, Enables, Set or Activated
428 if (*s
== 'T' || *s
== 'Y' || *s
== 'E' || *s
== 'S' || *s
== 'A')
430 // False, No, Disabled, Reset, Cleared, Deactivated
431 else if (*s
== 'F' || *s
== 'N' || *s
== 'D' || *s
== 'R' || *s
== 'C')
435 *value
= (int) strtol (s
, NULL
, 10);
443 void wxXMergeDatabases (wxApp
* theApp
, Display
* display
)
445 XrmDatabase homeDB
, serverDB
, applicationDB
;
446 char filenamebuf
[1024];
448 char *filename
= &filenamebuf
[0];
450 wxString classname
= theApp
->GetClassName();
452 (void) strcpy (name
, "/usr/lib/X11/app-defaults/");
453 (void) strcat (name
, classname
.c_str());
455 /* Get application defaults file, if any */
456 applicationDB
= XrmGetFileDatabase (name
);
457 (void) XrmMergeDatabases (applicationDB
, &wxResourceDatabase
);
459 /* Merge server defaults, created by xrdb, loaded as a property of the root
460 * window when the server initializes and loaded into the display
461 * structure on XOpenDisplay;
462 * if not defined, use .Xdefaults
465 if (XResourceManagerString (display
) != NULL
)
467 serverDB
= XrmGetStringDatabase (XResourceManagerString (display
));
471 (void) GetIniFile (filename
, NULL
);
472 serverDB
= XrmGetFileDatabase (filename
);
474 XrmMergeDatabases (serverDB
, &wxResourceDatabase
);
476 /* Open XENVIRONMENT file, or if not defined, the .Xdefaults,
477 * and merge into existing database
480 if ((environment
= getenv ("XENVIRONMENT")) == NULL
)
483 environment
= GetIniFile (filename
, NULL
);
484 len
= strlen (environment
);
485 wxString hostname
= wxGetHostName();
487 strncat(environment
, hostname
, 1024 - len
);
489 homeDB
= XrmGetFileDatabase (environment
);
490 XrmMergeDatabases (homeDB
, &wxResourceDatabase
);
496 * Not yet used but may be useful.
500 wxSetDefaultResources (const Widget w
, const char **resourceSpec
, const char *name
)
503 Display
*dpy
= XtDisplay (w
); // Retrieve the display pointer
505 XrmDatabase rdb
= NULL
; // A resource data base
507 // Create an empty resource database
508 rdb
= XrmGetStringDatabase ("");
510 // Add the Component resources, prepending the name of the component
513 while (resourceSpec
[i
] != NULL
)
517 sprintf (buf
, "*%s%s", name
, resourceSpec
[i
++]);
518 XrmPutLineResource (&rdb
, buf
);
521 // Merge them into the Xt database, with lowest precendence
525 #if (XlibSpecificationRelease>=5)
526 XrmDatabase db
= XtDatabase (dpy
);
527 XrmCombineDatabase (rdb
, &db
, FALSE
);
529 XrmMergeDatabases (dpy
->db
, &rdb
);
537 #endif // wxUSE_RESOURCES
539 // ----------------------------------------------------------------------------
541 // ----------------------------------------------------------------------------
543 void wxGetMousePosition( int* x
, int* y
)
552 XQueryPointer((Display
*) wxGetDisplay(),
553 DefaultRootWindow((Display
*) wxGetDisplay()),
555 &(xev
.x_root
), &(xev
.y_root
),
563 // Return TRUE if we have a colour display
564 bool wxColourDisplay()
566 return wxDisplayDepth() > 1;
569 // Returns depth of screen
572 Display
*dpy
= (Display
*) wxGetDisplay();
574 return DefaultDepth (dpy
, DefaultScreen (dpy
));
577 // Get size of display
578 void wxDisplaySize(int *width
, int *height
)
580 Display
*dpy
= (Display
*) wxGetDisplay();
583 *width
= DisplayWidth (dpy
, DefaultScreen (dpy
));
585 *height
= DisplayHeight (dpy
, DefaultScreen (dpy
));
588 void wxDisplaySizeMM(int *width
, int *height
)
590 Display
*dpy
= (Display
*) wxGetDisplay();
593 *width
= DisplayWidthMM(dpy
, DefaultScreen (dpy
));
595 *height
= DisplayHeightMM(dpy
, DefaultScreen (dpy
));
598 void wxClientDisplayRect(int *x
, int *y
, int *width
, int *height
)
600 // This is supposed to return desktop dimensions minus any window
601 // manager panels, menus, taskbars, etc. If there is a way to do that
602 // for this platform please fix this function, otherwise it defaults
603 // to the entire desktop.
606 wxDisplaySize(width
, height
);
610 // Configurable display in wxX11 and wxMotif
611 static WXDisplay
*gs_currentDisplay
= NULL
;
612 static wxString gs_displayName
;
614 WXDisplay
*wxGetDisplay()
616 if (gs_currentDisplay
)
617 return gs_currentDisplay
;
618 if (wxTheApp
&& wxTheApp
->GetTopLevelWidget())
619 return XtDisplay ((Widget
) wxTheApp
->GetTopLevelWidget());
621 return wxTheApp
->GetInitialDisplay();
625 bool wxSetDisplay(const wxString
& display_name
)
627 gs_displayName
= display_name
;
629 if ( display_name
.IsEmpty() )
631 gs_currentDisplay
= NULL
;
639 Display
*display
= XtOpenDisplay((XtAppContext
) wxTheApp
->GetAppContext(),
640 display_name
.c_str(),
641 wxTheApp
->GetAppName().c_str(),
642 wxTheApp
->GetClassName().c_str(),
644 #if XtSpecificationRelease < 5
653 gs_currentDisplay
= (WXDisplay
*) display
;
661 wxString
wxGetDisplayName()
663 return gs_displayName
;
666 wxWindow
* wxFindWindowAtPoint(const wxPoint
& pt
)
668 return wxGenericFindWindowAtPoint(pt
);
671 // ----------------------------------------------------------------------------
672 // keycode translations
673 // ----------------------------------------------------------------------------
675 #include <X11/keysym.h>
677 // FIXME what about tables??
679 int wxCharCodeXToWX(KeySym keySym
)
686 id
= WXK_SHIFT
; break;
689 id
= WXK_CONTROL
; break;
691 id
= WXK_BACK
; break;
693 id
= WXK_DELETE
; break;
695 id
= WXK_CLEAR
; break;
701 id
= WXK_RETURN
; break;
703 id
= WXK_ESCAPE
; break;
706 id
= WXK_PAUSE
; break;
708 id
= WXK_NUMLOCK
; break;
710 id
= WXK_SCROLL
; break;
713 id
= WXK_HOME
; break;
717 id
= WXK_LEFT
; break;
719 id
= WXK_RIGHT
; break;
723 id
= WXK_DOWN
; break;
725 id
= WXK_NEXT
; break;
727 id
= WXK_PRIOR
; break;
729 id
= WXK_MENU
; break;
731 id
= WXK_SELECT
; break;
733 id
= WXK_CANCEL
; break;
735 id
= WXK_PRINT
; break;
737 id
= WXK_EXECUTE
; break;
739 id
= WXK_INSERT
; break;
741 id
= WXK_HELP
; break;
744 id
= WXK_MULTIPLY
; break;
748 id
= WXK_SUBTRACT
; break;
750 id
= WXK_DIVIDE
; break;
752 id
= WXK_DECIMAL
; break;
760 id
= WXK_RETURN
; break;
762 id
= WXK_NUMPAD0
; break;
764 id
= WXK_NUMPAD1
; break;
766 id
= WXK_NUMPAD2
; break;
768 id
= WXK_NUMPAD3
; break;
770 id
= WXK_NUMPAD4
; break;
772 id
= WXK_NUMPAD5
; break;
774 id
= WXK_NUMPAD6
; break;
776 id
= WXK_NUMPAD7
; break;
778 id
= WXK_NUMPAD8
; break;
780 id
= WXK_NUMPAD9
; break;
830 id
= (keySym
<= 255) ? (int)keySym
: -1;
836 KeySym
wxCharCodeWXToX(int id
)
842 case WXK_CANCEL
: keySym
= XK_Cancel
; break;
843 case WXK_BACK
: keySym
= XK_BackSpace
; break;
844 case WXK_TAB
: keySym
= XK_Tab
; break;
845 case WXK_CLEAR
: keySym
= XK_Clear
; break;
846 case WXK_RETURN
: keySym
= XK_Return
; break;
847 case WXK_SHIFT
: keySym
= XK_Shift_L
; break;
848 case WXK_CONTROL
: keySym
= XK_Control_L
; break;
849 case WXK_MENU
: keySym
= XK_Menu
; break;
850 case WXK_PAUSE
: keySym
= XK_Pause
; break;
851 case WXK_ESCAPE
: keySym
= XK_Escape
; break;
852 case WXK_SPACE
: keySym
= ' '; break;
853 case WXK_PRIOR
: keySym
= XK_Prior
; break;
854 case WXK_NEXT
: keySym
= XK_Next
; break;
855 case WXK_END
: keySym
= XK_End
; break;
856 case WXK_HOME
: keySym
= XK_Home
; break;
857 case WXK_LEFT
: keySym
= XK_Left
; break;
858 case WXK_UP
: keySym
= XK_Up
; break;
859 case WXK_RIGHT
: keySym
= XK_Right
; break;
860 case WXK_DOWN
: keySym
= XK_Down
; break;
861 case WXK_SELECT
: keySym
= XK_Select
; break;
862 case WXK_PRINT
: keySym
= XK_Print
; break;
863 case WXK_EXECUTE
: keySym
= XK_Execute
; break;
864 case WXK_INSERT
: keySym
= XK_Insert
; break;
865 case WXK_DELETE
: keySym
= XK_Delete
; break;
866 case WXK_HELP
: keySym
= XK_Help
; break;
867 case WXK_NUMPAD0
: keySym
= XK_KP_0
; break;
868 case WXK_NUMPAD1
: keySym
= XK_KP_1
; break;
869 case WXK_NUMPAD2
: keySym
= XK_KP_2
; break;
870 case WXK_NUMPAD3
: keySym
= XK_KP_3
; break;
871 case WXK_NUMPAD4
: keySym
= XK_KP_4
; break;
872 case WXK_NUMPAD5
: keySym
= XK_KP_5
; break;
873 case WXK_NUMPAD6
: keySym
= XK_KP_6
; break;
874 case WXK_NUMPAD7
: keySym
= XK_KP_7
; break;
875 case WXK_NUMPAD8
: keySym
= XK_KP_8
; break;
876 case WXK_NUMPAD9
: keySym
= XK_KP_9
; break;
877 case WXK_MULTIPLY
: keySym
= XK_KP_Multiply
; break;
878 case WXK_ADD
: keySym
= XK_KP_Add
; break;
879 case WXK_SUBTRACT
: keySym
= XK_KP_Subtract
; break;
880 case WXK_DECIMAL
: keySym
= XK_KP_Decimal
; break;
881 case WXK_DIVIDE
: keySym
= XK_KP_Divide
; break;
882 case WXK_F1
: keySym
= XK_F1
; break;
883 case WXK_F2
: keySym
= XK_F2
; break;
884 case WXK_F3
: keySym
= XK_F3
; break;
885 case WXK_F4
: keySym
= XK_F4
; break;
886 case WXK_F5
: keySym
= XK_F5
; break;
887 case WXK_F6
: keySym
= XK_F6
; break;
888 case WXK_F7
: keySym
= XK_F7
; break;
889 case WXK_F8
: keySym
= XK_F8
; break;
890 case WXK_F9
: keySym
= XK_F9
; break;
891 case WXK_F10
: keySym
= XK_F10
; break;
892 case WXK_F11
: keySym
= XK_F11
; break;
893 case WXK_F12
: keySym
= XK_F12
; break;
894 case WXK_F13
: keySym
= XK_F13
; break;
895 case WXK_F14
: keySym
= XK_F14
; break;
896 case WXK_F15
: keySym
= XK_F15
; break;
897 case WXK_F16
: keySym
= XK_F16
; break;
898 case WXK_F17
: keySym
= XK_F17
; break;
899 case WXK_F18
: keySym
= XK_F18
; break;
900 case WXK_F19
: keySym
= XK_F19
; break;
901 case WXK_F20
: keySym
= XK_F20
; break;
902 case WXK_F21
: keySym
= XK_F21
; break;
903 case WXK_F22
: keySym
= XK_F22
; break;
904 case WXK_F23
: keySym
= XK_F23
; break;
905 case WXK_F24
: keySym
= XK_F24
; break;
906 case WXK_NUMLOCK
: keySym
= XK_Num_Lock
; break;
907 case WXK_SCROLL
: keySym
= XK_Scroll_Lock
; break;
908 default: keySym
= id
<= 255 ? (KeySym
)id
: 0;
914 // ----------------------------------------------------------------------------
915 // Some colour manipulation routines
916 // ----------------------------------------------------------------------------
918 void wxHSVToXColor(wxHSV
*hsv
,XColor
*rgb
)
923 int r
= 0, g
= 0, b
= 0;
926 s
= (s
* wxMAX_RGB
) / wxMAX_SV
;
927 v
= (v
* wxMAX_RGB
) / wxMAX_SV
;
929 if (s
== 0) { h
= 0; r
= g
= b
= v
; }
932 p
= v
* (wxMAX_RGB
- s
) / wxMAX_RGB
;
933 q
= v
* (wxMAX_RGB
- s
* f
/ 60) / wxMAX_RGB
;
934 t
= v
* (wxMAX_RGB
- s
* (60 - f
) / 60) / wxMAX_RGB
;
937 case 0: r
= v
, g
= t
, b
= p
; break;
938 case 1: r
= q
, g
= v
, b
= p
; break;
939 case 2: r
= p
, g
= v
, b
= t
; break;
940 case 3: r
= p
, g
= q
, b
= v
; break;
941 case 4: r
= t
, g
= p
, b
= v
; break;
942 case 5: r
= v
, g
= p
, b
= q
; break;
949 void wxXColorToHSV(wxHSV
*hsv
,XColor
*rgb
)
951 int r
= rgb
->red
>> 8;
952 int g
= rgb
->green
>> 8;
953 int b
= rgb
->blue
>> 8;
954 int maxv
= wxMax3(r
, g
, b
);
955 int minv
= wxMin3(r
, g
, b
);
958 if (maxv
) s
= (maxv
- minv
) * wxMAX_RGB
/ maxv
;
963 int rc
, gc
, bc
, hex
= 0;
964 rc
= (maxv
- r
) * wxMAX_RGB
/ (maxv
- minv
);
965 gc
= (maxv
- g
) * wxMAX_RGB
/ (maxv
- minv
);
966 bc
= (maxv
- b
) * wxMAX_RGB
/ (maxv
- minv
);
967 if (r
== maxv
) { h
= bc
- gc
, hex
= 0; }
968 else if (g
== maxv
) { h
= rc
- bc
, hex
= 2; }
969 else if (b
== maxv
) { h
= gc
- rc
, hex
= 4; }
970 h
= hex
* 60 + (h
* 60 / wxMAX_RGB
);
974 hsv
->s
= (s
* wxMAX_SV
) / wxMAX_RGB
;
975 hsv
->v
= (v
* wxMAX_SV
) / wxMAX_RGB
;
978 void wxAllocNearestColor(Display
*d
,Colormap cmp
,XColor
*xc
)
983 int screen
= DefaultScreen(d
);
984 int num_colors
= DisplayCells(d
,screen
);
986 XColor
*color_defs
= new XColor
[num_colors
];
987 for(llp
= 0;llp
< num_colors
;llp
++) color_defs
[llp
].pixel
= llp
;
988 XQueryColors(d
,cmp
,color_defs
,num_colors
);
991 wxXColorToHSV(&hsv
,xc
);
993 int diff
, min_diff
= 0, pixel
= 0;
995 for(llp
= 0;llp
< num_colors
;llp
++)
997 wxXColorToHSV(&hsv_defs
,&color_defs
[llp
]);
998 diff
= wxSIGN(wxH_WEIGHT
* (hsv
.h
- hsv_defs
.h
)) +
999 wxSIGN(wxS_WEIGHT
* (hsv
.s
- hsv_defs
.s
)) +
1000 wxSIGN(wxV_WEIGHT
* (hsv
.v
- hsv_defs
.v
));
1001 if (llp
== 0) min_diff
= diff
;
1002 if (min_diff
> diff
) { min_diff
= diff
; pixel
= llp
; }
1003 if (min_diff
== 0) break;
1006 xc
-> red
= color_defs
[pixel
].red
;
1007 xc
-> green
= color_defs
[pixel
].green
;
1008 xc
-> blue
= color_defs
[pixel
].blue
;
1009 xc
-> flags
= DoRed
| DoGreen
| DoBlue
;
1012 if (!XAllocColor(d,cmp,xc))
1013 cout << "wxAllocNearestColor : Warning : Cannot find nearest color !\n";
1016 delete[] color_defs
;
1020 void wxAllocColor(Display
*d
,Colormap cmp
,XColor
*xc
)
1022 if (!XAllocColor(d
,cmp
,xc
))
1024 // cout << "wxAllocColor : Warning : Can not allocate color, attempt find nearest !\n";
1025 wxAllocNearestColor(d
,cmp
,xc
);
1030 wxString
wxGetXEventName(XEvent
& event
)
1033 wxString
str(wxT("(some event)"));
1036 int type
= event
.xany
.type
;
1037 static char* event_name
[] = {
1038 "", "unknown(-)", // 0-1
1039 "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease", // 2-5
1040 "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", // 6-9
1041 "FocusOut", "KeymapNotify", "Expose", "GraphicsExpose", // 10-13
1042 "NoExpose", "VisibilityNotify", "CreateNotify", // 14-16
1043 "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",// 17-20
1044 "ReparentNotify", "ConfigureNotify", "ConfigureRequest", // 21-23
1045 "GravityNotify", "ResizeRequest", "CirculateNotify", // 24-26
1046 "CirculateRequest", "PropertyNotify", "SelectionClear", // 27-29
1047 "SelectionRequest", "SelectionNotify", "ColormapNotify", // 30-32
1048 "ClientMessage", "MappingNotify", // 33-34
1049 "unknown(+)"}; // 35
1050 type
= wxMin(35, type
); type
= wxMax(1, type
);
1051 wxString
str(event_name
[type
]);
1057 // ----------------------------------------------------------------------------
1059 // ----------------------------------------------------------------------------
1061 // Find the letter corresponding to the mnemonic, for Motif
1062 char wxFindMnemonic (const char *s
)
1065 int len
= strlen (s
);
1067 for (i
= 0; i
< len
; i
++)
1071 // Carefully handle &&
1072 if ((i
+ 1) <= len
&& s
[i
+ 1] == '&')
1084 char * wxFindAccelerator (const char *s
)
1086 // VZ: this function returns incorrect keysym which completely breaks kbd
1091 // The accelerator text is after the \t char.
1092 while (*s
&& *s
!= '\t')
1098 Now we need to format it as X standard:
1103 Ctrl+N --> Ctrl<Key>N
1104 Alt+k --> Meta<Key>k
1105 Ctrl+Shift+A --> Ctrl Shift<Key>A
1109 static char buf
[256];
1111 char *tmp
= copystring (s
);
1117 while (*p
&& *p
!= '+')
1124 if (strcmp (s
, "Alt"))
1127 strcat (buf
, "Meta");
1132 strcat (buf
, "<Key>");
1142 XmString
wxFindAcceleratorText (const char *s
)
1144 // VZ: this function returns incorrect keysym which completely breaks kbd
1149 // The accelerator text is after the \t char.
1150 while (*s
&& *s
!= '\t')
1155 XmString text
= XmStringCreateSimple ((char *)s
);
1161 // Change a widget's foreground and background colours.
1163 void wxDoChangeForegroundColour(WXWidget widget
, wxColour
& foregroundColour
)
1165 // When should we specify the foreground, if it's calculated
1166 // by wxComputeColours?
1167 // Solution: say we start with the default (computed) foreground colour.
1168 // If we call SetForegroundColour explicitly for a control or window,
1169 // then the foreground is changed.
1170 // Therefore SetBackgroundColour computes the foreground colour, and
1171 // SetForegroundColour changes the foreground colour. The ordering is
1174 XtVaSetValues ((Widget
) widget
,
1175 XmNforeground
, foregroundColour
.AllocColour(XtDisplay((Widget
) widget
)),
1179 void wxDoChangeBackgroundColour(WXWidget widget
, wxColour
& backgroundColour
, bool changeArmColour
)
1181 wxComputeColours (XtDisplay((Widget
) widget
), & backgroundColour
,
1184 XtVaSetValues ((Widget
) widget
,
1185 XmNbackground
, g_itemColors
[wxBACK_INDEX
].pixel
,
1186 XmNtopShadowColor
, g_itemColors
[wxTOPS_INDEX
].pixel
,
1187 XmNbottomShadowColor
, g_itemColors
[wxBOTS_INDEX
].pixel
,
1188 XmNforeground
, g_itemColors
[wxFORE_INDEX
].pixel
,
1191 if (changeArmColour
)
1192 XtVaSetValues ((Widget
) widget
,
1193 XmNarmColor
, g_itemColors
[wxSELE_INDEX
].pixel
,
1197 extern void wxDoChangeFont(WXWidget widget
, wxFont
& font
)
1199 // Lesstif 0.87 hangs here, but 0.93 does not
1200 #if !wxCHECK_LESSTIF() || wxCHECK_LESSTIF_VERSION( 0, 93 )
1201 Widget w
= (Widget
)widget
;
1203 wxFont::GetFontTag(), font
.GetFontType( XtDisplay(w
) ),
1209 bool wxWindowIsVisible(Window win
)
1211 XWindowAttributes wa
;
1212 XGetWindowAttributes(wxGlobalDisplay(), win
, &wa
);
1214 return (wa
.map_state
== IsViewable
);
1217 wxString
wxXmStringToString( const XmString
& xmString
)
1220 if( XmStringGetLtoR( xmString
, XmSTRING_DEFAULT_CHARSET
, &txt
) )
1227 return wxEmptyString
;
1230 XmString
wxStringToXmString( const wxString
& str
)
1232 return XmStringCreateLtoR((char *)str
.c_str(), XmSTRING_DEFAULT_CHARSET
);
1235 XmString
wxStringToXmString( const char* str
)
1237 return XmStringCreateLtoR((char *)str
, XmSTRING_DEFAULT_CHARSET
);
1240 // ----------------------------------------------------------------------------
1241 // wxBitmap utility functions
1242 // ----------------------------------------------------------------------------
1244 // Creates a bitmap with transparent areas drawn in
1245 // the given colour.
1246 wxBitmap
wxCreateMaskedBitmap(const wxBitmap
& bitmap
, wxColour
& colour
)
1248 wxBitmap
newBitmap(bitmap
.GetWidth(),
1254 srcDC
.SelectObject(bitmap
);
1255 destDC
.SelectObject(newBitmap
);
1257 wxBrush
brush(colour
, wxSOLID
);
1258 // destDC.SetOptimization(FALSE);
1259 destDC
.SetBackground(brush
);
1261 destDC
.Blit(0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(),
1262 &srcDC
, 0, 0, wxCOPY
, TRUE
);