checkpoint
[nvi.git] / motif_l / m_util.c
blob6c0a488d28bd0c504d89ac2e376db09795b790f8
1 /*-
2 * Copyright (c) 1996
3 * Rob Zimmermann. All rights reserved.
4 * Copyright (c) 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
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 $";
14 #endif /* not lint */
16 #include <X11/Intrinsic.h>
17 #include <X11/StringDefs.h>
18 #include <X11/Shell.h>
19 #include <X11/Xatom.h>
21 #include <stdio.h>
22 #include <string.h>
24 #include "m_util.h"
26 static Colormap cmap = 0;
27 static Display *cmap_display = 0;
30 /* ***********************************************************************
31 * Useful macros
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
50 #if defined(__STDC__)
51 void XutShowWidgetTree( FILE *fp, Widget root, int indent )
52 #else
53 void XutShowWidgetTree( fp, root, indent )
54 FILE *fp;
55 Widget root;
56 int indent;
57 #endif
59 #if ! defined(DECWINDOWS)
60 WidgetList l, l2;
61 int i, count = 0;
63 /* print where we are right now */
64 fprintf( fp,
65 "%*.*swidget => 0x%x name => \"%s\"\n\r",
66 indent,
67 indent,
68 "",
69 root,
70 XtName(root) );
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 */
83 if ( count > 0 ) {
84 /* we may or may not have to free the children */
85 if ( l != l2 ) {
86 XtFree( (char *) l );
87 XtFree( (char *) l2 );
90 #endif
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 )
109 #else
110 Bool XutStringToColor( w, name, pixel_value )
111 Widget w;
112 String name;
113 Pixel *pixel_value;
114 #endif
116 XrmValue fromVal, toVal;
118 fromVal.addr = name;
119 fromVal.size = strlen(name);
120 XtConvert( w, XtRString, &fromVal, XtRPixel, &toVal );
122 if ( ! toVal.addr ) return False;
124 *pixel_value = *((Pixel *)toVal.addr);
125 return True;
129 #if defined(__STDC__)
130 void XutGetColorComponents( Display *display, Pixel pix, XColor *col )
131 #else
132 void XutGetColorComponents( display, pix, col )
133 Display *display;
134 Pixel pix;
135 XColor *col;
136 #endif
138 col->pixel = pix;
139 XQueryColor( display, get_current_colormap(), col );
143 #if defined(__STDC__)
144 Pixel XutGetContrastingPixel( Display *display, Pixel pix )
145 #else
146 Pixel XutGetContrastingPixel( display, pix )
147 Display *display;
148 Pixel pix;
149 #endif
151 XColor col;
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;
158 /* who knows? */
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,
195 String root,
196 XutResource *resources,
197 int count
199 #else
200 void XutConvertResources( wid, root, resources, count )
201 Widget wid;
202 String root;
203 XutResource *resources;
204 int count;
205 #endif
207 int i;
208 XrmValue from, to;
209 String kind;
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' )
218 continue;
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 ) {
226 case XutRKinteger:
227 to.size = sizeof(int);
228 kind = XtRInt;
229 break;
231 case XutRKboolean:
232 /* String to Boolean */
233 to.size = sizeof(Boolean);
234 kind = XtRBoolean;
235 break;
237 case XutRKfont:
238 /* String to Font structure */
239 to.size = sizeof(XFontStruct *);
240 kind = XtRFontStruct;
241 break;
243 case XutRKpixelBackup:
244 /* String to Pixel backup algorithm */
245 if ( success ) continue;
246 /* FALL through */
248 case XutRKpixel:
249 /* String to Pixel */
250 to.size = sizeof(Pixel);
251 kind = XtRPixel;
252 break;
254 case XutRKcursor:
255 /* String to Cursor */
256 to.size = sizeof(int);
257 kind = XtRCursor;
258 break;
260 default:
261 return;
264 /* call the converter */
265 success = XtConvertAndStore( wid, XtRString, &from, kind, &to );
271 * PUBLIC: void XutSetIcon __P((Widget, int, int, Pixmap));
273 void
274 XutSetIcon(wid, height, width, p)
275 Widget wid;
276 int height, width;
277 Pixmap p;
279 Display *display = XtDisplay(wid);
280 Window win;
282 /* best bet is to set the icon window */
283 XtVaGetValues( wid, XtNiconWindow, &win, 0 );
285 if ( win == None ) {
286 win = XCreateSimpleWindow( display,
287 RootWindow( display, Screen ),
288 0, 0,
289 width, height,
291 CopyFromParent,
292 CopyFromParent
296 if ( win != None ) {
297 XtVaSetValues( wid, XtNiconWindow, win, 0 );
298 XSetWindowBackgroundPixmap( display, win, p );
301 else {
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
316 * same colormap.
318 * Future versions of this routine may handle multiple colormaps
319 * by name.
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 }
332 void
333 XutInstallColormap(name, wid)
334 String name;
335 Widget 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,
344 name,
345 colormap_resources,
346 XtNumber(colormap_resources)
349 /* was the result "True"? */
350 if ( ! use_colormap ) {
351 cmap_state = cmap_ignore;
352 return;
355 /* yes it was */
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 )
374 #else
375 void XutFreeColormap( name )
376 String name;
377 #endif
379 if ( cmap_state == cmap_use ) {
380 XFreeColormap( cmap_display, cmap );
381 cmap = 0;
382 cmap_state = cmap_look;
387 #if defined(__STDC__)
388 Colormap XutGetColormap( Widget wid, String name )
389 #else
390 Colormap XutGetColormap( wid, name )
391 Widget wid;
392 String name;
393 #endif
395 Display *display = XtDisplay( wid );
397 return get_current_colormap();