3 * Rob Zimmermann. All rights reserved.
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 static const char sccsid
[] = "$Id: m_util.c,v 8.8 1996/12/11 20:58:25 bostic Exp $ (Berkeley) $Date: 1996/12/11 20:58:25 $";
16 #include <X11/Intrinsic.h>
17 #include <X11/StringDefs.h>
18 #include <X11/Shell.h>
19 #include <X11/Xatom.h>
26 static Colormap cmap
= 0;
27 static Display
*cmap_display
= 0;
30 /* ***********************************************************************
32 * ***********************************************************************
35 #define Screen DefaultScreen( display )
36 #define Black BlackPixel( display, Screen )
37 #define White WhitePixel( display, Screen )
39 #define get_current_colormap() \
40 (cmap) ? cmap : DefaultColormap( display, Screen )
43 /* Widget hierarchy routines
45 * void XutShowWidgetTree( FILE *fp, Widget root, int indent )
46 * prints the widgets and sub-widgets beneath the named root widget
51 void XutShowWidgetTree( FILE *fp
, Widget root
, int indent
)
53 void XutShowWidgetTree( fp
, root
, indent
)
59 #if ! defined(DECWINDOWS)
63 /* print where we are right now */
65 "%*.*swidget => 0x%x name => \"%s\"\n\r",
72 /* get the correct widget values */
73 XtVaGetValues( root
, XtNchildren
, &l
, 0 );
74 XtVaGetValues( root
, XtNchildren
, &l2
, 0 );
75 XtVaGetValues( root
, XtNnumChildren
, &count
, 0 );
77 /* print the sub-widgets */
78 for ( i
=0; i
<count
; i
++ ) {
79 XutShowWidgetTree( fp
, l
[i
], indent
+4 );
82 /* tidy up if this thing contained children */
84 /* we may or may not have to free the children */
87 XtFree( (char *) l2
);
95 /* Color support utilities
97 * Pixel XutGetContrastingPixel( Display *display, Pixel pix )
98 * select a Black or White to contrast with the specified color
100 * Bool XutStringToColor( Widget, String, Pixel * )
101 * given a String (either a name or RBG in hex) returns a
102 * pixel which matches the String.
105 #define MaxIntensity 65535
107 #if defined(__STDC__)
108 Bool
XutStringToColor( Widget w
, String name
, Pixel
*pixel_value
)
110 Bool
XutStringToColor( w
, name
, pixel_value
)
116 XrmValue fromVal
, toVal
;
119 fromVal
.size
= strlen(name
);
120 XtConvert( w
, XtRString
, &fromVal
, XtRPixel
, &toVal
);
122 if ( ! toVal
.addr
) return False
;
124 *pixel_value
= *((Pixel
*)toVal
.addr
);
129 #if defined(__STDC__)
130 void XutGetColorComponents( Display
*display
, Pixel pix
, XColor
*col
)
132 void XutGetColorComponents( display
, pix
, col
)
139 XQueryColor( display
, get_current_colormap(), col
);
143 #if defined(__STDC__)
144 Pixel
XutGetContrastingPixel( Display
*display
, Pixel pix
)
146 Pixel
XutGetContrastingPixel( display
, pix
)
152 int intensity
= 0, possible
= 0;
153 Visual
*vis
= DefaultVisual( display
, Screen
);
155 /* for b&w displays, invert the pixel value */
156 if ( DisplayPlanes( display
, Screen
) == 1 ) return ! pix
;
159 if ( vis
== NULL
) return Black
;
161 /* what are the RGB components of the color? */
162 XutGetColorComponents( display
, pix
, &col
);
164 if ( col
.flags
& DoGreen
) {
165 intensity
+= col
.green
;
166 possible
+= MaxIntensity
;
168 /* if a gray-scale monitor, ignore the unhooked wires */
169 if ( vis
->class != GrayScale
&& vis
->class != StaticGray
) {
170 if ( col
.flags
& DoRed
) {
171 intensity
+= col
.red
;
172 possible
+= MaxIntensity
;
174 if ( col
.flags
& DoBlue
) {
175 intensity
+= col
.blue
;
176 possible
+= MaxIntensity
;
180 /* use black for the contrast unless the background is more than 66% dark. */
181 return ( intensity
< 0.33 * possible
) ? White
: Black
;
185 /* Utilities for converting X resources...
187 * XutConvertResources( Widget, String root, XutResource *, int count )
188 * The resource block is loaded with converted values
189 * If the X resource does not exist, no change is made to the value
190 * 'root' should be the application name.
193 #if defined(__STDC__)
194 void XutConvertResources( Widget wid
,
196 XutResource
*resources
,
200 void XutConvertResources( wid
, root
, resources
, count
)
203 XutResource
*resources
;
210 Boolean success
= True
;
212 /* for each resource */
213 for (i
=0; i
<count
; i
++) {
215 /* is there a value in the database? */
216 from
.addr
= XGetDefault( XtDisplay(wid
), root
, resources
[i
].name
);
217 if ( from
.addr
== NULL
|| *from
.addr
== '\0' )
220 /* load common parameters */
221 from
.size
= strlen( from
.addr
);
222 to
.addr
= resources
[i
].value
;
224 /* load type-specific parameters */
225 switch ( resources
[i
].kind
) {
227 to
.size
= sizeof(int);
232 /* String to Boolean */
233 to
.size
= sizeof(Boolean
);
238 /* String to Font structure */
239 to
.size
= sizeof(XFontStruct
*);
240 kind
= XtRFontStruct
;
243 case XutRKpixelBackup
:
244 /* String to Pixel backup algorithm */
245 if ( success
) continue;
249 /* String to Pixel */
250 to
.size
= sizeof(Pixel
);
255 /* String to Cursor */
256 to
.size
= sizeof(int);
264 /* call the converter */
265 success
= XtConvertAndStore( wid
, XtRString
, &from
, kind
, &to
);
271 * PUBLIC: void XutSetIcon __P((Widget, int, int, Pixmap));
274 XutSetIcon(wid
, height
, width
, p
)
279 Display
*display
= XtDisplay(wid
);
282 /* best bet is to set the icon window */
283 XtVaGetValues( wid
, XtNiconWindow
, &win
, 0 );
286 win
= XCreateSimpleWindow( display
,
287 RootWindow( display
, Screen
),
297 XtVaSetValues( wid
, XtNiconWindow
, win
, 0 );
298 XSetWindowBackgroundPixmap( display
, win
, p
);
302 /* do it the old fashioned way */
303 XtVaSetValues( wid
, XtNiconPixmap
, p
, 0 );
308 /* Support for multiple colormaps
310 * XutInstallColormap( String name, Widget wid )
311 * The first time called, this routine checks to see if the
312 * resource "name*installColormap" is "True". If so, the
313 * widget is assigned a newly allocated colormap.
315 * Subsequent calls ignore the "name" parameter and use the
318 * Future versions of this routine may handle multiple colormaps
321 * PUBLIC: void XutInstallColormap __P((String, Widget));
324 static enum { cmap_look
, cmap_use
, cmap_ignore
} cmap_state
= cmap_look
;
326 static Boolean use_colormap
= False
;
328 XutResource colormap_resources
[] = {
329 { "installColormap", XutRKboolean
, &use_colormap
}
333 XutInstallColormap(name
, wid
)
337 Display
*display
= XtDisplay(wid
);
339 /* what is the current finite state? */
340 if ( cmap_state
== cmap_look
) {
342 /* what does the resource say? */
343 XutConvertResources( wid
,
346 XtNumber(colormap_resources
)
349 /* was the result "True"? */
350 if ( ! use_colormap
) {
351 cmap_state
= cmap_ignore
;
356 cmap_state
= cmap_use
;
357 cmap_display
= display
;
358 cmap
= XCopyColormapAndFree( display
,
359 DefaultColormap( display
,
360 DefaultScreen( display
)
365 /* use the private colormap? */
366 if ( cmap_state
== cmap_use
) {
367 XtVaSetValues( wid
, XtNcolormap
, cmap
, 0 );
372 #if defined(__STDC__)
373 void XutFreeColormap( String name
)
375 void XutFreeColormap( name
)
379 if ( cmap_state
== cmap_use
) {
380 XFreeColormap( cmap_display
, cmap
);
382 cmap_state
= cmap_look
;
387 #if defined(__STDC__)
388 Colormap
XutGetColormap( Widget wid
, String name
)
390 Colormap
XutGetColormap( wid
, name
)
395 Display
*display
= XtDisplay( wid
);
397 return get_current_colormap();