1 /*
2  * Motif
3  *
4  * Copyright (c) 1987-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 
28 #ifdef REV_INFO
29 #ifndef lint
30 static char rcsid[] = "$XConsortium: Mrmicon.c /main/14 1996/11/13 14:01:43 drk $"
31 #endif
32 #endif
33 
34 /*
35  *++
36  *  FACILITY:
37  *
38  *      UIL Resource Manager (URM)
39  *
40  *  ABSTRACT:
41  *
42  *	This module contains routines which operate on URM icon images -
43  *	RGMIconImage structs.
44  *
45  *--
46  */
47 
48 
49 /*
50  *
51  *  INCLUDE FILES
52  *
53  */
54 
55 #include <stdio.h>
56 #include <Mrm/MrmAppl.h>
57 #include <Mrm/Mrm.h>
58 #include "MrmMsgI.h"
59 
60 
61 
62 
63 /*
64  *++
65  *
66  *  PROCEDURE DESCRIPTION:
67  *
68  *	This routine creates and returns an X pixmap from a URM icon
69  *	for some widget. It uses the foreground and background obtainable
70  *	from the widget as required.
71  *
72  *  FORMAL PARAMETERS:
73  *
74  *	icon		URM icon image to be converted to X pixmap
75  *	screen		screen to use for pixmap
76  *	display		display to use for pixmap
77  *	fgpix		foreground color for pixmap
78  *	bgpix		background color for pixmap
79  *	pixmap		to return resulting X pixmap
80  *
81  *  IMPLICIT INPUTS:
82  *
83  *  IMPLICIT OUTPUTS:
84  *
85  *  FUNCTION VALUE:
86  *
87  *	URM status
88  *
89  *  SIDE EFFECTS:
90  *
91  *--
92  */
93 
94 Cardinal
95 UrmCreatePixmap (RGMIconImagePtr	icon,
96 		 Screen			*screen,
97 		 Display		*display,
98 		 Pixel			fgpix,
99 		 Pixel			bgpix,
100 		 Pixel			*pixmap,
101 		 Widget			parent)
102 {
103 
104   /*
105    *  Local variables
106    */
107   Cardinal		result;		/* function results */
108   RGMColorTablePtr	ctable;		/* color table in icon */
109   unsigned		maxbits;	/* # bits required for X image */
110   int			srcpix;		/* # bits per pixel in icon (actual) */
111   int			dds ;
112 
113   /*
114    * Convert the color table colors to pixels.
115    */
116   ctable = icon->color_table.ctptr;
117   result =
118     Urm__RealizeColorTable (screen, display, fgpix, bgpix, ctable, parent);
119   if ( result != MrmSUCCESS ) return result;
120 
121   /*
122    * Use the depth of screen to infer the number of bits required to
123    * hold the pixels in X format. The ZPixmap format only supports 1, 8, 16,
124    * and 32 bit pixels, so we always map to one of these sizes.
125    */
126   if (parent) dds = parent->core.depth;
127   else dds = DefaultDepthOfScreen(screen) ;
128 
129   if (dds == 1)
130     maxbits = 1;
131   else if (dds <= 8)
132     maxbits = 8;
133   else if (dds <= 16)
134     maxbits = 16;
135   else
136     maxbits = 32;
137 
138 
139   /*
140    * See if the icon can be mapped as a bitmap. This will be true if the
141    * color table has only FG/BG entries, or uses only those entries.
142    */
143   if ( ctable->count <= 2 ) maxbits = 1;
144 
145 
146   /*
147    * Compute the number of bits available in the icon pixmap
148    */
149   switch ( icon->pixel_size )
150     {
151     case URMPixelSize1Bit:
152       srcpix = 1;
153       break;
154     case URMPixelSize2Bit:
155       srcpix = 2;
156       break;
157     case URMPixelSize4Bit:
158       srcpix = 4;
159       break;
160     case URMPixelSize8Bit:
161       srcpix = 8;
162       break;
163     default:
164       return MrmNOT_VALID;
165     }
166 
167   /*
168    * Map the icon image pixmap from color table indices to Pixel values.
169    * There are three cases:
170    *	maxbits == 1; a bitmap is possible, so map to a bitmap.
171    *	maxbits <= # bits/pixel in pixmap. Map in place. In fact,
172    *		only works if maxbits == srcpix == 8.
173    *	maxbits > # bits/pixel. Map to allocated pixmap.
174    *
175    * Each of the three routines which performs these mappings completes
176    * the entire process of doing the mapping and creating the X pixmap.
177    */
178   if ( maxbits == 1 )
179     return Urm__MapIconBitmap
180       (icon, srcpix, ctable, screen, display, pixmap);
181   if ((maxbits == 8) && (srcpix == 8))
182     return Urm__MapIconReplace
183       (icon, srcpix, ctable, screen, display, pixmap, parent);
184   if ( maxbits > srcpix )
185     return Urm__MapIconAllocate
186       (icon, srcpix, maxbits, ctable, screen, display, pixmap, parent);
187 
188   return MrmNOT_VALID;
189 }
190 
191 
192 /*
193  *++
194  *
195  *  PROCEDURE DESCRIPTION:
196  *
197  *	This routine creates and returns an X pixmap of depth 1 from a URM icon
198  *	for some widget.
199  *
200  *  FORMAL PARAMETERS:
201  *
202  *	icon		URM icon image to be converted to X pixmap
203  *	screen		screen to use for pixmap
204  *	display		display to use for pixmap
205  *	pixmap		to return resulting X pixmap
206  *
207  *  IMPLICIT INPUTS:
208  *
209  *  IMPLICIT OUTPUTS:
210  *
211  *  FUNCTION VALUE:
212  *
213  *	URM status
214  *
215  *  SIDE EFFECTS:
216  *
217  *--
218  */
219 
220 Cardinal
UrmCreateBitmap(RGMIconImagePtr icon,Screen * screen,Display * display,Pixel * pixmap)221 UrmCreateBitmap (RGMIconImagePtr	icon,
222 		 Screen			*screen,
223 		 Display		*display,
224 		 Pixel			*pixmap)
225 {
226 
227   /*
228    *  Local variables
229    */
230   int			srcpix;		/* # bits per pixel in icon (actual) */
231 
232 
233   /*
234    * Compute the number of bits available in the icon pixmap
235    */
236   switch ( icon->pixel_size )
237     {
238     case URMPixelSize1Bit:
239       srcpix = URMPixelSize1Bit;
240       break;
241     default:
242       return MrmNOT_VALID;
243     }
244 
245   return Urm__MapIconBitmapDepth1 (icon, srcpix, screen, display, pixmap);
246 
247 }
248 
249 
250 /*
251  *++
252  *
253  *  PROCEDURE DESCRIPTION:
254  *
255  *	This routine creates and returns an X pixmap from the X bitmap file
256  *	name specified.  It uses the foreground and background as needed.
257  *
258  *
259  *  FORMAL PARAMETERS:
260  *
261  *	filename	file containing the bitmap to be converted to X pixmap
262  *	screen		screen to use for pixmap
263  *	display		display to use for pixmap - not used
264  *	fgint		foreground color for pixmap
265  *	bgint		background color for pixmap
266  *	pixmap		to return resulting X pixmap
267  *
268  *  IMPLICIT INPUTS:
269  *
270  *  IMPLICIT OUTPUTS:
271  *
272  *  FUNCTION VALUE:
273  *
274  *	URM status
275  *
276  *  SIDE EFFECTS:
277  *
278  *--
279  */
280 
281 Cardinal
Urm__CW_ReadBitmapFile(String filename,Screen * screen,Pixel fgint,Pixel bgint,Pixmap * pixmap,Widget parent)282 Urm__CW_ReadBitmapFile (String			filename,
283 			Screen			*screen,
284 			Pixel			fgint,
285 			Pixel			bgint,
286 			Pixmap			*pixmap,
287 			Widget			parent)
288 {
289   int		depth;
290   char		err_msg[300];
291 
292   /*
293   **  Create a pixmap from a X bitmap file specification
294   */
295   depth = parent ? parent->core.depth : DefaultDepthOfScreen(screen);
296 
297   *pixmap = XmGetPixmapByDepth(screen, filename, fgint, bgint, depth);
298 
299   if (*pixmap == XmUNSPECIFIED_PIXMAP)
300     {
301       pixmap = 0;
302       sprintf (err_msg, _MrmMMsg_0033, filename);
303       return Urm__UT_Error ("UrmReadBitmapFile", err_msg,
304 			    NULL, NULL, MrmFAILURE);
305     }
306 
307   return MrmSUCCESS;
308 }
309 
310 
311 
312 
313 /*
314  *++
315  *
316  *  PROCEDURE DESCRIPTION:
317  *
318  *	This routine maps the pixmap in the icon into a bitmap. The
319  *	bitmap is written over pixmap data. The X pixmap is then
320  *	constructed as an XYBitmap.
321  *
322  *  FORMAL PARAMETERS:
323  *
324  *	icon		the IconImage being converted
325  *	srcpix		number of bits/pixel in icon
326  *	ctable		the color table for (in) icon
327  *	screen		screen for the X pixmap
328  *	display		display for the X pixmap
329  *	pixmap		to return the result
330  *
331  *  IMPLICIT INPUTS:
332  *
333  *  IMPLICIT OUTPUTS:
334  *
335  *  FUNCTION VALUE:
336  *
337  *  SIDE EFFECTS:
338  *
339  *--
340  */
341 
342 Cardinal
Urm__MapIconBitmap(RGMIconImagePtr icon,int srcpix,RGMColorTablePtr ctable,Screen * screen,Display * display,Pixmap * pixmap)343 Urm__MapIconBitmap(RGMIconImagePtr		icon,
344 		   int				srcpix,
345 		   RGMColorTablePtr		ctable,
346 		   Screen			*screen,
347 		   Display			*display,
348 		   Pixmap			*pixmap)
349 {
350 
351   /*
352    *  Local variables
353    */
354   Pixel			fgpix;		/* foreground pixel value */
355   int			iconwid;	/* icon width */
356   int			srclinebyt;	/* # bytes per icon line */
357   int			dstlinebyt;	/* # bytes per bitmap line */
358   char			*srcbytptr;	/* image byte pointer */
359   char			*dstbytptr;	/* bitmap pointer */
360   int			lin;		/* icon line number */
361   int			byt;		/* line byte number */
362   int			pix;		/* line pixel number */
363   unsigned char		srcbyt;		/* icon image byte */
364   unsigned char		dstbyt;		/* mapped image byte */
365   int			tndx;		/* color table index */
366   XImage		*imagep;	/* X image */
367   GC			gc;
368   XGCValues		gcValues;
369   int			endian;		/* to determine which endian. */
370 
371   /*
372    * Overwrite the icon data with a bitmap. Use 0 for background, 1 for
373    * foreground.
374    */
375   fgpix = ctable->item[URMColorTableFG].color_pixel;
376   iconwid = icon->width;
377   srclinebyt = (iconwid*srcpix+7) / 8;
378   dstlinebyt = (iconwid+7) / 8;
379   srcbytptr = icon->pixel_data.pdptr;
380   for ( lin=0 ; lin<icon->height ; lin++ )
381     {
382       pix = 0;
383       dstbytptr = icon->pixel_data.pdptr + lin*dstlinebyt;
384       dstbyt = 0;
385       for ( byt=0 ; byt<srclinebyt ; byt++ )
386         {
387 	  srcbyt = *srcbytptr;
388 	  switch ( icon->pixel_size )
389             {
390             case URMPixelSize1Bit:
391 	      *dstbytptr = srcbyt;
392 	      srcbytptr += 1;
393 	      dstbytptr += 1;
394 	      pix += 8;
395 	      /*
396 	       *  NOTE: The original algorithm used here cleared any
397 	       *        unused bits of the last byte.  I don't think
398 	       *        the protocol requires that...
399 	       if ( pix > iconwid )
400 	       */
401 	      continue;
402             case URMPixelSize2Bit:
403 	      tndx = srcbyt & 0x3;
404 	      if ( pix < iconwid )
405 		if ( ctable->item[tndx].color_pixel == fgpix)
406 		  dstbyt |= 1<<(pix%8);
407 	      pix += 1;
408 	      srcbyt >>= 2;
409 	      tndx = srcbyt & 0x3;
410 	      if ( pix < iconwid )
411 		if ( ctable->item[tndx].color_pixel == fgpix)
412 		  dstbyt |= 1<<(pix%8);
413 	      pix += 1;
414 	      srcbyt >>= 2;
415 	      tndx = srcbyt & 0x3;
416 	      if ( pix < iconwid )
417 		if ( ctable->item[tndx].color_pixel == fgpix)
418 		  dstbyt |= 1<<(pix%8);
419 	      pix += 1;
420 	      srcbyt >>= 2;
421 	      tndx = srcbyt & 0x3;
422 	      if ( pix < iconwid )
423 		if ( ctable->item[tndx].color_pixel == fgpix)
424 		  dstbyt |= 1<<(pix%8);
425 	      pix += 1;
426 	      break;
427             case URMPixelSize4Bit:
428 	      tndx = srcbyt & 0xF;
429 	      if ( pix < iconwid )
430 		if ( ctable->item[tndx].color_pixel == fgpix)
431 		  dstbyt |= 1<<(pix%8);
432 	      pix += 1;
433 	      srcbyt >>= 4;
434 	      tndx = srcbyt & 0xF;
435 	      if ( pix < iconwid )
436 		if ( ctable->item[tndx].color_pixel == fgpix)
437 		  dstbyt |= 1<<(pix%8);
438 	      pix += 1;
439 	      break;
440             case URMPixelSize8Bit:
441 	      if ( pix < iconwid )
442 		if ( ctable->item[srcbyt].color_pixel == fgpix)
443 		  dstbyt |= 1<<(pix%8);
444 	      pix += 1;
445 	      break;
446             }
447 	  /*
448 	   *  NOTE:  The "1Bit" case CONTINUEs directly and will never
449 	   *         reach this point in the loop...
450 	   */
451 	  srcbytptr += 1;
452 	  if ( pix%8 == 0 )
453             {
454 	      *dstbytptr = dstbyt;
455 	      dstbytptr += 1;
456 	      dstbyt = 0;
457             }
458         }
459       if ( pix%8 != 0 )
460         *dstbytptr = dstbyt;
461     }
462 
463 
464   imagep = XCreateImage(display,
465 			DefaultVisualOfScreen(screen),
466 			1,
467 			XYBitmap,
468 			0,
469 			icon->pixel_data.pdptr,
470 			(unsigned int)icon->width,
471 			(unsigned int)icon->height,
472 			8,
473 			dstlinebyt);
474 
475   if ( imagep == NULL )
476     return Urm__UT_Error ("Urm__MapIconBitmap", _MrmMMsg_0034,
477 			  NULL, NULL, MrmFAILURE);
478 
479   /*
480    * Well, Uil creates a uid icon in its byteorder, on his side
481    * XCreateImage creates an image in the byte_order of the server, so
482    * its not correct, we have to adjust the byte_order fields of this
483    * image to match those of the icon.
484    */
485 
486   endian = 1;
487   imagep->bitmap_unit = 8 ;
488   if (*(char *) &endian) {
489     imagep->byte_order = LSBFirst ;
490     imagep->bitmap_bit_order = LSBFirst ;
491   }
492   else {
493     imagep->byte_order = MSBFirst;
494     imagep->bitmap_bit_order = LSBFirst ;
495   }
496 
497   *pixmap = XCreatePixmap (display,
498 			   RootWindowOfScreen(screen),
499 			   icon->width,
500 			   icon->height,
501 			   (unsigned)DefaultDepthOfScreen(screen));
502   if ( *pixmap == (Pixmap)0)
503     {
504       XFree((char*)imagep);
505       return Urm__UT_Error ("Urm__MapIconBitmap", _MrmMMsg_0035,
506 			    NULL, NULL, MrmFAILURE);
507     }
508 
509   /*
510    * Build a gc to use when drawing into the pixmap
511    */
512   gcValues.foreground = ctable->item[URMColorTableFG].color_pixel;
513   gcValues.background = ctable->item[URMColorTableBG].color_pixel;
514   gcValues.fill_style = FillTiled;
515   gcValues.tile       = *pixmap;
516 
517   gc = XCreateGC (display,
518 		  RootWindowOfScreen (screen),
519 		  GCForeground | GCBackground | GCFillStyle | GCTile,
520 		  &gcValues);
521   if ( gc == NULL )
522     return Urm__UT_Error ("Urm__MapIconBitmap", _MrmMMsg_0036,
523 			  NULL, NULL, MrmFAILURE);
524 
525   /*
526    * Put bits into the pixmap
527    */
528   XPutImage (display,
529 	     *pixmap,
530 	     gc,
531 	     imagep,
532 	     0, 0,				/* source x, y */
533 	     0, 0, icon->width, icon->height);	/* dest, loc & size */
534 
535   XFreeGC (display, gc);
536   XFree ((char*)imagep);
537 
538   /*
539    * Successfully created
540    */
541   return MrmSUCCESS;
542 
543 }
544 
545 
546 /*
547  *++
548  *
549  *  PROCEDURE DESCRIPTION:
550  *
551  *	This routine maps the pixmap in the icon into a bitmap. The
552  *	bitmap is written over pixmap data. The X pixmap is then
553  *	constructed as an XYBitmap.
554  *
555  *  FORMAL PARAMETERS:
556  *
557  *	icon		the IconImage being converted
558  *	srcpix		number of bits/pixel in icon
559  *	screen		screen for the X pixmap
560  *	display		display for the X pixmap
561  *	pixmap		to return the result
562  *
563  *  IMPLICIT INPUTS:
564  *
565  *  IMPLICIT OUTPUTS:
566  *
567  *  FUNCTION VALUE:
568  *
569  *  SIDE EFFECTS:
570  *
571  *--
572  */
573 
574 Cardinal
Urm__MapIconBitmapDepth1(RGMIconImagePtr icon,int srcpix,Screen * screen,Display * display,Pixmap * pixmap)575 Urm__MapIconBitmapDepth1 (RGMIconImagePtr	icon,
576 			  int			srcpix,
577 			  Screen		*screen,
578 			  Display		*display,
579 			  Pixmap		*pixmap)
580 {
581 
582   /*
583    *  Local variables
584    */
585   int			iconwid;	/* icon width */
586   int			srclinebyt;	/* # bytes per icon line */
587   int			dstlinebyt;	/* # bytes per bitmap line */
588   char			*srcbytptr;	/* image byte pointer */
589   char			*dstbytptr;	/* bitmap pointer */
590   int			lin;		/* icon line number */
591   int			byt;		/* line byte number */
592   int			pix;		/* line pixel number */
593   unsigned char		srcbyt;		/* icon image byte */
594   unsigned char		dstbyt;		/* mapped image byte */
595   XImage		*imagep;	/* X image */
596   GC			gc;
597   XGCValues		gcValues;
598   int			endian;		/* to determine which endian. */
599 
600 
601   /*
602    * Overwrite the icon data with a bitmap. Use 0 for background, 1 for
603    * foreground.
604    */
605   iconwid = icon->width;
606   srclinebyt = (iconwid*srcpix+7) / 8;
607   dstlinebyt = (iconwid+7) / 8;
608   srcbytptr = icon->pixel_data.pdptr;
609   for ( lin=0 ; lin<icon->height ; lin++ )
610     {
611       pix = 0;
612       dstbytptr = icon->pixel_data.pdptr + lin*dstlinebyt;
613       dstbyt = 0;
614       for ( byt=0 ; byt<srclinebyt ; byt++ )
615         {
616 	  srcbyt = *srcbytptr;
617 	  switch ( icon->pixel_size )
618             {
619             case URMPixelSize1Bit:
620 	      *dstbytptr = srcbyt;
621 	      srcbytptr += 1;
622 	      dstbytptr += 1;
623 	      pix += 8;
624 	      /*
625 	       *  NOTE: The original algorithm used here cleared any
626 	       *        unused bits of the last byte.  I don't think
627 	       *        the protocol requires that...
628 	       if ( pix > iconwid )
629 	       */
630 	      continue;
631 	    default:
632 	      return MrmNOT_VALID;
633             }
634         }
635       if ( pix%8 != 0 )
636         *dstbytptr = dstbyt;
637     }
638 
639 
640   imagep = XCreateImage(display,
641 			DefaultVisualOfScreen(screen),
642 			1,
643 			XYBitmap,
644 			0,
645 			icon->pixel_data.pdptr,
646 			(unsigned int)icon->width,
647 			(unsigned int)icon->height,
648 			8,
649 			dstlinebyt);
650 
651   if ( imagep == NULL )
652     return Urm__UT_Error ("Urm__MapIconBitmapDepth1", _MrmMMsg_0034,
653 			  NULL, NULL, MrmFAILURE);
654 
655   /* Well, Uil creates a uid icon in its byteorder, on his side
656      XCreateImage creates an image in the byte_order of the server, so
657      its not correct, we have to adjust the byte_order fields of this
658      image to match those of the icon. */
659 
660   endian = 1;
661   imagep->bitmap_unit = 8 ;
662   if (*(char *) &endian) {
663     imagep->byte_order = LSBFirst ;
664     imagep->bitmap_bit_order = LSBFirst ;
665   }
666   else {
667     imagep->byte_order = MSBFirst;
668     imagep->bitmap_bit_order = LSBFirst ;
669   }
670 
671   *pixmap = XCreatePixmap (display,
672 			   RootWindowOfScreen(screen),
673 			   icon->width,
674 			   icon->height,
675 			   1);
676   if ( *pixmap == (Pixmap)0)
677     {
678       XFree((char*)imagep);
679       return Urm__UT_Error ("Urm__MapIconBitmapDepth1", _MrmMMsg_0035,
680 			    NULL, NULL, MrmFAILURE);
681     }
682 
683 
684   /*
685    * Build a gc to use when drawing into the pixmap
686    */
687   gcValues.foreground = 1;
688   gcValues.background = 0;
689   gcValues.fill_style = FillTiled;
690   gcValues.tile       = *pixmap;
691 
692   gc = XCreateGC (display,
693 		  *pixmap,
694 		  GCForeground | GCBackground | GCFillStyle | GCTile,
695 		  &gcValues);
696 
697   if ( gc == NULL )
698     return Urm__UT_Error ("Urm__MapIconBitmapDepth1", _MrmMMsg_0036,
699 			  NULL, NULL, MrmFAILURE);
700 
701   /*
702    * Put bits into the pixmap
703    */
704   XPutImage (display,
705 	     *pixmap,
706 	     gc,
707 	     imagep,
708 	     0, 0,				/* source x, y */
709 	     0, 0, icon->width, icon->height);	/* dest, loc & size */
710 
711 
712   XFreeGC (display, gc);
713   XFree ((char *)imagep);
714 
715   /*
716    * Successfully created
717    */
718   return MrmSUCCESS;
719 }
720 
721 
722 /*
723  *++
724  *
725  *  PROCEDURE DESCRIPTION:
726  *
727  *	This routine maps the pixmap in the icon into a ZPixmap. The
728  *	ZPixmap is written over the icon image pixmap data. The X pixmap
729  *	is then constructed as a ZPixmap from the overwritten data.
730  *
731  *	This routine is called only when srcpix == 8.
732  *
733  *  FORMAL PARAMETERS:
734  *
735  *	icon		the IconImage being converted
736  *	srcpix		number of bits/pixel in icon
737  *	ctable		the color table for (in) icon
738  *	screen		screen for the X pixmap
739  *	display		display for the X pixmap
740  *	pixmap		to return the result
741  *
742  *  IMPLICIT INPUTS:
743  *
744  *  IMPLICIT OUTPUTS:
745  *
746  *  FUNCTION VALUE:
747  *
748  *  SIDE EFFECTS:
749  *
750  *--
751  */
752 
753 Cardinal
Urm__MapIconReplace(RGMIconImagePtr icon,int srcpix,RGMColorTablePtr ctable,Screen * screen,Display * display,Pixmap * pixmap,Widget parent)754 Urm__MapIconReplace (RGMIconImagePtr		icon,
755 		     int			srcpix,
756 		     RGMColorTablePtr		ctable,
757 		     Screen			*screen,
758 		     Display			*display,
759 		     Pixmap			*pixmap,
760 		     Widget			parent)
761 {
762 
763   /*
764    *  Local variables
765    */
766   int			iconwid;	/* icon width */
767   int			linebyt;	/* # bytes per icon line */
768   char			*bytptr;	/* image byte pointer */
769   int			lin;		/* icon line number */
770   int			byt;		/* line byte number */
771   int			pix;		/* line pixel number */
772   unsigned char		srcbyt;		/* icon image byte */
773   XImage		*imagep;	/* X image */
774   GC			gc;
775   XGCValues		gcValues;
776   int			depth;		/* depth of screen  */
777 
778   /*
779    * Overwrite the pixmap with actual Pixel values. The source and destination
780    * bit widths are the same (==8), and the same pointer can be used for
781    * both source and destination.
782    */
783   iconwid = icon->width;
784   linebyt = (iconwid*srcpix+7) / 8;
785   bytptr = icon->pixel_data.pdptr;
786   for ( lin=0 ; lin<icon->height ; lin++ )
787     {
788       pix = 0;
789       for ( byt=0 ; byt<linebyt ; byt++ )
790         {
791 	  srcbyt = *bytptr;
792 	  if ( pix < iconwid )
793 	    *bytptr = ctable->item[srcbyt].color_pixel;
794 	  pix += 1;
795 	  bytptr += 1;
796         }
797     }
798 
799   depth = parent ? parent->core.depth : DefaultDepthOfScreen(screen);
800 
801   imagep = XCreateImage(display,
802 			DefaultVisualOfScreen(screen),
803 			depth,
804 			ZPixmap,
805 			0,
806 			icon->pixel_data.pdptr,
807 			(unsigned int)icon->width,
808 			(unsigned int)icon->height,
809 			srcpix,
810 			linebyt);
811 
812   if ( imagep == NULL )
813     return Urm__UT_Error ("Urm__MapIconReplace", _MrmMMsg_0034,
814 			  NULL, NULL, MrmFAILURE);
815 
816   *pixmap = XCreatePixmap (display,
817 			   RootWindowOfScreen(screen),
818 			   icon->width,
819 			   icon->height,
820 			   (unsigned)depth);
821   if ( *pixmap == (Pixmap)0)
822     {
823       XFree((char*)imagep);
824       return Urm__UT_Error ("Urm__MapIconReplace", _MrmMMsg_0035,
825 			    NULL, NULL, MrmFAILURE);
826     }
827 
828   /*
829    * Build a gc to use when drawing into the pixmap
830    */
831   gcValues.foreground = ctable->item[URMColorTableFG].color_pixel;
832   gcValues.background = ctable->item[URMColorTableBG].color_pixel;
833   gcValues.fill_style = FillTiled;
834   gcValues.tile       = *pixmap;
835 
836   gc = XCreateGC (display,
837 		  RootWindowOfScreen (screen),
838 		  GCForeground | GCBackground | GCFillStyle | GCTile,
839 		  &gcValues);
840   if ( gc == NULL )
841     return Urm__UT_Error ("Urm__MapIconReplace", _MrmMMsg_0036,
842 			  NULL, NULL, MrmFAILURE);
843 
844   /*
845    * Put bits into the pixmap
846    */
847   XPutImage (display,
848 	     *pixmap,
849 	     gc,
850 	     imagep,
851 	     0, 0,				/* source x, y */
852 	     0, 0, icon->width, icon->height);	/* dest, loc & size */
853 
854   XFreeGC (display, gc);
855   XFree((char*)imagep);
856 
857   /*
858    * Successfully created
859    */
860   return MrmSUCCESS;
861 
862 }
863 
864 
865 
866 /*
867  *++
868  *
869  *  PROCEDURE DESCRIPTION:
870  *
871  *	This routine maps the pixmap in the icon into a ZPixmap. The
872  *	ZPixmap is allocated 1 byte per pixel, as the icon image pixmap
873  *	has too few bits per pixel to be replace in situ. The X pixmap
874  *	is created from the allocated ZPixmap.
875  *
876  *  FORMAL PARAMETERS:
877  *
878  *	icon		the IconImage being converted
879  *	srcpix		number of bits/pixel in icon
880  *	dstpix		number of bits/pixel in resulting image
881  *	ctable		the color table for (in) icon
882  *	screen		screen for the X pixmap
883  *	display		display for the X pixmap
884  *	pixmap		to return the result
885  *
886  *  IMPLICIT INPUTS:
887  *
888  *  IMPLICIT OUTPUTS:
889  *
890  *  FUNCTION VALUE:
891  *
892  *  SIDE EFFECTS:
893  *
894  *--
895  */
896 
897 Cardinal
Urm__MapIconAllocate(RGMIconImagePtr icon,int srcpix,int dstpix,RGMColorTablePtr ctable,Screen * screen,Display * display,Pixmap * pixmap,Widget parent)898 Urm__MapIconAllocate (RGMIconImagePtr		icon,
899 		      int			srcpix,
900 		      int			dstpix,
901 		      RGMColorTablePtr		ctable,
902 		      Screen			*screen,
903 		      Display			*display,
904 		      Pixmap			*pixmap,
905 		      Widget			parent)
906 {
907 
908   /*
909    *  Local variables
910    */
911   int			iconwid;	/* icon width */
912   int			iconhgt;	/* icon height */
913   char			*alloc_pixmap;	/* allocated pixmap */
914   int			dstbytsize;	/* # 8-bit bytes per bitmap byte */
915   int			srclinebyt;	/* # bytes per icon line */
916   char			*srcbytptr;	/* image byte pointer */
917   int			lin;		/* icon line number */
918   int			byt;		/* line byte number */
919   int			bit;		/* bit loop index */
920   int			pix;		/* line pixel number */
921   unsigned char		srcbyt;		/* icon image byte */
922   int			bitmask = 0;    /* mask all but significant bits */
923   int			num_bits = 0;   /* real (not coded) pixel size */
924   int			tndx;		/* color table index */
925   XImage		*imagep;	/* X image */
926   GC			gc;
927   XGCValues		gcValues;
928   int			depth;		/* depth of screen */
929 
930   /*
931    * Allocate a new pixmap image.
932    */
933   iconwid = icon->width;
934   iconhgt = icon->height;
935   if (dstpix <= 8) dstpix = 8;
936   else if (dstpix <= 16) dstpix = 16;
937   else dstpix = 32;
938   dstbytsize = dstpix / 8;
939   alloc_pixmap = (char *) XtMalloc (iconwid * iconhgt * dstbytsize);
940   if (alloc_pixmap == NULL)
941     return Urm__UT_Error ("Urm__MapIconAllocate", _MrmMMsg_0037,
942 			  NULL, NULL, MrmFAILURE);
943   srclinebyt = (iconwid * srcpix + 7) / 8;
944   srcbytptr = icon->pixel_data.pdptr;
945 
946   depth = parent ? parent->core.depth : DefaultDepthOfScreen(screen);
947 
948   imagep = XCreateImage(display,
949 			DefaultVisualOfScreen(screen),
950 			depth,
951 			ZPixmap,
952 			0,
953 			alloc_pixmap,
954 			iconwid,
955 			iconhgt,
956 			dstpix,
957 			0);	/* Let Xlib calculate it. */
958 
959   if ( imagep == NULL )
960     {
961       XtFree (alloc_pixmap);
962       return Urm__UT_Error ("Urm__MapIconAllocate", _MrmMMsg_0034,
963 			    NULL, NULL, MrmFAILURE);
964     }
965 
966   if(icon->pixel_size == URMPixelSize1Bit)
967     { num_bits = 1; bitmask=0x1; }
968   else if(icon->pixel_size == URMPixelSize2Bit)
969     { num_bits = 2; bitmask=0x3; }
970   else if(icon->pixel_size == URMPixelSize4Bit)
971     { num_bits = 4; bitmask=0xF; }
972   else if(icon->pixel_size == URMPixelSize8Bit)
973     { num_bits = 8; bitmask=0xFF; }
974   for ( pix=0,lin=0 ; lin<icon->height ; pix=0,lin++ )
975     {
976       for ( byt=0 ; byt<srclinebyt ; byt++,srcbytptr++ )
977 	{
978 	  srcbyt = *srcbytptr;
979 	  for ( bit=0 ; bit<8 ; bit+=num_bits )
980 	    {
981 	      tndx = srcbyt & bitmask;
982 	      if ( pix < iconwid )
983 		XPutPixel(imagep, pix, lin, ctable->item[tndx].color_pixel);
984 	      pix++;
985 	      srcbyt >>= num_bits;
986 	    }
987 	}
988     }
989 
990   *pixmap = XCreatePixmap (display,
991 			   RootWindowOfScreen(screen),
992 			   iconwid,
993 			   iconhgt,
994 			   (unsigned)depth);
995   if ( *pixmap == (Pixmap)0)
996     {
997       XtFree (alloc_pixmap);
998       XFree((char*)imagep);
999       return Urm__UT_Error ("Urm__MapIconAllocate", _MrmMMsg_0035,
1000 			    NULL, NULL, MrmFAILURE);
1001     }
1002 
1003   /*
1004    * Build a gc to use when drawing into the pixmap
1005    */
1006   gcValues.foreground = ctable->item[URMColorTableFG].color_pixel;
1007   gcValues.background = ctable->item[URMColorTableBG].color_pixel;
1008   gcValues.fill_style = FillTiled;
1009   gcValues.tile       = *pixmap;
1010 
1011   gc = XCreateGC (display,
1012 		  RootWindowOfScreen (screen),
1013 		  GCForeground | GCBackground | GCFillStyle | GCTile,
1014 		  &gcValues);
1015   if ( gc == NULL )
1016     {
1017       XtFree (alloc_pixmap);
1018       return Urm__UT_Error ("Urm__MapIconAllocate", _MrmMMsg_0036,
1019 			    NULL, NULL, MrmFAILURE);
1020     }
1021 
1022   /*
1023    * Put bits into the pixmap
1024    */
1025   XPutImage (display,
1026 	     *pixmap,
1027 	     gc,
1028 	     imagep,
1029 	     0, 0,				/* source x, y */
1030 	     0, 0, iconwid, iconhgt);		/* dest, loc & size */
1031 
1032   /*
1033    * don't deallocate with XDestroyImage, which would destroy alloc_pixmap
1034    */
1035   XFree((char*)imagep);
1036   XFreeGC (display, gc);
1037   XtFree (alloc_pixmap);
1038 
1039   /*
1040    * Successfully created
1041    */
1042   return MrmSUCCESS;
1043 
1044 }
1045 
1046 
1047 
1048 /*
1049  *++
1050  *
1051  *  PROCEDURE DESCRIPTION:
1052  *
1053  *	This routine sets the Pixel values corresponding to each of the
1054  *	entries in the color table. Foreground and background are set by
1055  *	querying the widget for those values, or falling back on
1056  *	Black/WhitePixelOfScreen. All other colors are set by honoring
1057  *	FG/BG setting for monochrome devices, or by getting the xolor
1058  *	Pixel values from X.
1059  *
1060  *  FORMAL PARAMETERS:
1061  *
1062  *	screen		screen to use for color table
1063  *	display		display to use for color table
1064  *	fgpix		foreground color for color table
1065  *	bgpix		background color for color table
1066  *	ctable		the color table
1067  *
1068  *  IMPLICIT INPUTS:
1069  *
1070  *  IMPLICIT OUTPUTS:
1071  *
1072  *  FUNCTION VALUE:
1073  *
1074  *  SIDE EFFECTS:
1075  *
1076  *--
1077  */
1078 
1079 Cardinal
Urm__RealizeColorTable(Screen * screen,Display * display,Pixel fgpix,Pixel bgpix,RGMColorTablePtr ctable,Widget parent)1080 Urm__RealizeColorTable (Screen			*screen,
1081 			Display			*display,
1082 			Pixel			fgpix,
1083 			Pixel			bgpix,
1084 			RGMColorTablePtr	ctable,
1085 			Widget			parent)
1086 {
1087 
1088   /*
1089    *  Local variables
1090    */
1091   Cardinal		result = 0;		/* function results */
1092   Cardinal		ndx;		/* loop index */
1093   RGMColorTableEntryPtr	citem;		/* color table entry */
1094   Colormap		cmap;		/* default color map */
1095   int			depth;		/* # planes in screen */
1096   char			err_msg[300];
1097 
1098 
1099   /*
1100    * Load the foreground and background pixel values.
1101    */
1102   ctable->item[URMColorTableFG].color_pixel = fgpix;
1103   ctable->item[URMColorTableBG].color_pixel = bgpix;
1104 
1105   /*
1106    * Get the Pixel for each defined color. Honor the FG/BG specification
1107    * if present on monochrome displays. Otherwise, get the Pixel value for
1108    * the color. Use the FG/BG specification as a fallback for unfound
1109    * colors in non-monochrome. If no reasonable color Pixel can be found,
1110    * return an error.
1111    */
1112   cmap = parent ? parent->core.colormap : DefaultColormapOfScreen(screen);
1113   depth = parent ? parent->core.depth : DefaultDepthOfScreen(screen);
1114 
1115   for ( ndx=URMColorTableUserMin ; ndx<ctable->count ; ndx++ )
1116     {
1117       citem = &ctable->item[ndx];
1118       if ( depth == 1 )
1119         switch ( citem->color_item.cptr->mono_state )
1120 	  {
1121 	  case URMColorMonochromeUnspecified:
1122 	    switch (citem->color_item.cptr->desc_type)
1123 	      {
1124 	      case URMColorDescTypeName:
1125 		result = Urm__UT_GetNamedColorPixel
1126 		  (display, cmap, citem->color_item.cptr, &citem->color_pixel,
1127 		   ctable->item[URMColorTableBG].color_pixel);
1128 		if ( result != MrmSUCCESS) {
1129 		  /* we use PARTIAL_SUCCESS only to indicate
1130 		     the color allocation failed and we've
1131 		     substituted the fallback color. We still
1132 		     want a warning, though */
1133 		  if (result == MrmPARTIAL_SUCCESS) {
1134 		    result = MrmSUCCESS;
1135 		    sprintf (err_msg, _MrmMMsg_0038,
1136 			     citem->color_item.cptr->desc.name);
1137 		    return Urm__UT_Error ("Urm__RealizeColorTable",
1138 					  err_msg, NULL, NULL, result);
1139 		  } else {
1140 		    sprintf (err_msg, _MrmMMsg_0038,
1141 			     citem->color_item.cptr->desc.name);
1142 		    return Urm__UT_Error ("Urm__RealizeColorTable",
1143 					  err_msg, NULL, NULL, result);
1144 		  }
1145 		}
1146 		break;
1147 	      case URMColorDescTypeRGB:
1148 		result = Urm__UT_GetColorPixel
1149 		  (display, cmap, citem->color_item.cptr, &citem->color_pixel,
1150 		   ctable->item[URMColorTableBG].color_pixel);
1151 		if ( result != MrmSUCCESS ) {
1152 		  /* we use PARTIAL_SUCCESS only to indicate
1153 		     the color allocation failed and we've
1154 		     substituted the fallback color. We still
1155 		     want a warning, though */
1156 		  if (result == MrmPARTIAL_SUCCESS) {
1157 		    result = MrmSUCCESS;
1158 		    sprintf (err_msg, _MrmMMsg_0038,
1159 			     citem->color_item.cptr->desc.name);
1160 		    return Urm__UT_Error ("Urm__RealizeColorTable",
1161 					  err_msg, NULL, NULL, result);
1162 		  } else {
1163 		    sprintf (err_msg, _MrmMMsg_0039,
1164 			     citem->color_item.cptr->desc.rgb.red,
1165 			     citem->color_item.cptr->desc.rgb.green,
1166 			     citem->color_item.cptr->desc.rgb.blue) ;
1167 		    return Urm__UT_Error ("Urm__RealizeColorTable",
1168 					  err_msg, NULL, NULL, result);
1169 		  }
1170 		}
1171 		break;
1172 	      default:
1173 		sprintf(err_msg, _MrmMMsg_0040);
1174 		return Urm__UT_Error ("Urm__RelizeColorTable",
1175 				      err_msg, NULL, NULL, MrmFAILURE) ;
1176 	      }
1177 	    break;
1178 	  case URMColorMonochromeForeground:
1179 	    citem->color_pixel =
1180 	      ctable->item[URMColorTableFG].color_pixel;
1181 	    break;
1182 	  case URMColorMonochromeBackground:
1183 	    citem->color_pixel =
1184 	      ctable->item[URMColorTableBG].color_pixel;
1185 	    break;
1186 	  default:
1187 	    sprintf (err_msg, _MrmMMsg_0041,
1188 		     citem->color_item.cptr->mono_state);
1189 	    return Urm__UT_Error ("Urm__RealizeColorTable",
1190 				  err_msg, NULL, NULL, result);
1191 	  }
1192       else
1193         {
1194 	  switch (citem->color_item.cptr->desc_type)
1195 	    {
1196 	    case URMColorDescTypeName:
1197 	      result = Urm__UT_GetNamedColorPixel
1198 		(display, cmap, citem->color_item.cptr, &citem->color_pixel,
1199 		 ((citem->color_item.cptr->mono_state ==
1200 		   URMColorMonochromeForeground) ?
1201 		  ctable->item[URMColorTableFG].color_pixel :
1202 		  ctable->item[URMColorTableBG].color_pixel));
1203 	      if (result != MrmSUCCESS) {
1204 		/* we use PARTIAL_SUCCESS only to indicate
1205 		   the color allocation failed and we've
1206 		   substituted the fallback color. We still
1207 		   want a warning, though */
1208 		if (result == MrmPARTIAL_SUCCESS) {
1209 		  result = MrmSUCCESS;
1210 		  sprintf (err_msg, _MrmMMsg_0038,
1211 			   citem->color_item.cptr->desc.name);
1212 		  return Urm__UT_Error ("Urm__RealizeColorTable",
1213 					err_msg, NULL, NULL, result);
1214 		} else {
1215 		  sprintf (err_msg, _MrmMMsg_0038,
1216 			   citem->color_item.cptr->desc.name);
1217 		  Urm__UT_Error ("Urm__RealizeColorTable",
1218 				 err_msg, NULL, NULL, result);
1219 		}
1220 	      }
1221 	      break;
1222 	    case URMColorDescTypeRGB:
1223 	      result = Urm__UT_GetColorPixel
1224 		(display, cmap, citem->color_item.cptr, &citem->color_pixel,
1225 		 ctable->item[URMColorTableBG].color_pixel);
1226 	      if (result != MrmSUCCESS) {
1227 		/* we use PARTIAL_SUCCESS only to indicate
1228 		   the color allocation failed and we've
1229 		   substituted the fallback color. We still
1230 		   want a warning, though */
1231 		if (result == MrmPARTIAL_SUCCESS) {
1232 		  result = MrmSUCCESS;
1233 		  sprintf (err_msg, _MrmMMsg_0038,
1234 			   citem->color_item.cptr->desc.name);
1235 		  return Urm__UT_Error ("Urm__RealizeColorTable",
1236 					err_msg, NULL, NULL, result);
1237 		} else {
1238 		  sprintf (err_msg, _MrmMMsg_0039,
1239 			   citem->color_item.cptr->desc.rgb.red,
1240 			   citem->color_item.cptr->desc.rgb.green,
1241 			   citem->color_item.cptr->desc.rgb.blue) ;
1242 		  Urm__UT_Error ("Urm__RealizeColorTable",
1243 				 err_msg, NULL, NULL, result);
1244 		}
1245 	      }
1246 	      break;
1247 	    default:
1248 	      result = MrmFAILURE;
1249 	      sprintf (err_msg, _MrmMMsg_0040);
1250 	      Urm__UT_Error ("Urm__RelizeColorTable",
1251 			     err_msg, NULL, NULL, MrmFAILURE) ;
1252 	    }
1253 	  if ( result != MrmSUCCESS )
1254             {
1255 	      switch ( citem->color_item.cptr->mono_state )
1256                 {
1257                 case URMColorMonochromeForeground:
1258 		  citem->color_pixel =
1259 		    ctable->item[URMColorTableFG].color_pixel;
1260 		  break;
1261                 case URMColorMonochromeBackground:
1262 		  citem->color_pixel =
1263 		    ctable->item[URMColorTableBG].color_pixel;
1264 		  break;
1265                 default:
1266 		  return result;
1267                 }
1268             }
1269         }
1270     }
1271 
1272   return MrmSUCCESS;
1273 }
1274 
1275 /*
1276  *++
1277  *
1278  *  PROCEDURE DESCRIPTION:
1279  *
1280  *	This routine makes a copy of a URM icon into a memory block
1281  *	which has been pre-allocated. The block must be big enough
1282  *	to hold both the header and the bit vector.
1283  *
1284  *  FORMAL PARAMETERS:
1285  *
1286  *	dst_icon	the memory block to receive the copy
1287  *	src_icon	the URM icon descriptor to be copied.
1288  *
1289  *  IMPLICIT INPUTS:
1290  *
1291  *  IMPLICIT OUTPUTS:
1292  *
1293  *  FUNCTION VALUE:
1294  *
1295  *  SIDE EFFECTS:
1296  *
1297  *--
1298  */
1299 
1300 
1301 RGMIconImagePtr
UrmCopyAllocatedIconImage(RGMIconImagePtr dst_icon,RGMIconImagePtr src_icon)1302 UrmCopyAllocatedIconImage (RGMIconImagePtr	dst_icon,
1303 			   RGMIconImagePtr	src_icon)
1304 {
1305 
1306   /*
1307    * Copy the header and bit vector into the new memory block.
1308    */
1309   dst_icon->validation = URMIconImageValid;
1310   dst_icon->pixel_size = src_icon->pixel_size;
1311   dst_icon->width = src_icon->width;
1312   dst_icon->height = src_icon->height;
1313   dst_icon->hot_x = src_icon->hot_x;
1314   dst_icon->hot_y = src_icon->hot_y;
1315 
1316   /*
1317    * Copy the color table as an immediate. It is allocated immediately
1318    * after the image header.
1319    */
1320 
1321   /*
1322    * Copy the pixel data
1323    */
1324 
1325   return dst_icon;
1326 
1327 }
1328 
1329 
1330 
1331 /*
1332  *++
1333  *
1334  *  PROCEDURE DESCRIPTION:
1335  *
1336  *	This routine attempts to look up the name of a color, and return
1337  *	the pixel value required as a widget arglist value. It will use
1338  *	the default color map if necessary, and returns the closest color
1339  *	supported by the hardware (as defined by X), not the exact database
1340  *	definition. If the screen is depth 1 (monochrome), the routine will
1341  *	honor the monochrome rendition specified in the color descriptor.
1342  *
1343  *  FORMAL PARAMETERS:
1344  *
1345  *	display		specifies the X server connection
1346  *	cmap		color map ID. If NULL, the default color map is used
1347  *	colorptr	color descriptor
1348  *	pixel_return	to return the pixel value for the color
1349  *      fallback        fallback color to use in case of alloc failure
1350  *
1351  *  IMPLICIT INPUTS:
1352  *
1353  *  IMPLICIT OUTPUTS:
1354  *
1355  *  FUNCTION VALUE:
1356  *
1357  *	MrmSUCCESS	   color found and translated
1358  *	MrmNOT_FOUND	   conversion failure
1359  *      MrmPARTIAL_SUCCESS internal only, for allocation failure
1360  *
1361  *  SIDE EFFECTS:
1362  *
1363  *--
1364  */
1365 
1366 Cardinal
Urm__UT_GetNamedColorPixel(Display * display,Colormap cmap,RGMColorDescPtr colorptr,Pixel * pixel_return,Pixel fallback)1367 Urm__UT_GetNamedColorPixel (Display		*display,
1368 			    Colormap		cmap,
1369 			    RGMColorDescPtr	colorptr,
1370 			    Pixel		*pixel_return,
1371 			    Pixel		fallback)
1372 {
1373 
1374   /*
1375    *  Local variables
1376    */
1377   XColor		screen_def;	/* realizable values */
1378   XColor		exact_def;	/* exact values */
1379   int			status;		/* function return */
1380 
1381 
1382   if ( cmap == (Colormap)0)
1383     cmap = DefaultColormap (display, DefaultScreen(display));
1384 
1385   /* CR 9891: Support new pixel constants. */
1386   if (XmeNamesAreEqual(colorptr->desc.name, "default_select_color"))
1387     {
1388       *pixel_return = XmDEFAULT_SELECT_COLOR;
1389       return MrmSUCCESS;
1390     }
1391   else if (XmeNamesAreEqual(colorptr->desc.name, "reversed_ground_colors"))
1392     {
1393       *pixel_return = XmREVERSED_GROUND_COLORS;
1394       return MrmSUCCESS;
1395     }
1396   else if (XmeNamesAreEqual(colorptr->desc.name, "highlight_color"))
1397     {
1398       *pixel_return = XmHIGHLIGHT_COLOR;
1399       return MrmSUCCESS;
1400     }
1401 
1402   status = XAllocNamedColor
1403     (display, cmap, colorptr->desc.name, &screen_def, &exact_def);
1404 
1405   if ( status == 0) {
1406     if (fallback) {
1407       *pixel_return = fallback;
1408       return MrmPARTIAL_SUCCESS;
1409     }
1410     else
1411       return MrmFAILURE;
1412   }
1413   *pixel_return = screen_def.pixel;
1414   return MrmSUCCESS;
1415 
1416 }
1417 
1418 
1419 
1420 /*
1421  *++
1422  *
1423  *  PROCEDURE DESCRIPTION:
1424  *
1425  *	This routine attempts to look up the RGB values of a color, and return
1426  *	the pixel value required as a widget arglist value. It will use
1427  *	the default color map if necessary, and returns the closest color
1428  *	supported by the hardware (as defined by X), not the exact database
1429  *	definition. If the screen is depth 1 (monochrome), the routine will
1430  *	honor the monochrome rendition specified in the color descriptor.
1431  *
1432  *  FORMAL PARAMETERS:
1433  *
1434  *	display		specifies the X server connection
1435  *	cmap		color map ID. If NULL, the default color map is used
1436  *	colorptr	color descriptor
1437  *	pixel_return	to return the pixel value for the color
1438  *      fallback        fallback color to use in case of alloc failure
1439  *
1440  *  IMPLICIT INPUTS:
1441  *
1442  *  IMPLICIT OUTPUTS:
1443  *
1444  *  FUNCTION VALUE:
1445  *
1446  *	MrmSUCCESS	color found and translated
1447  *	MrmNOT_FOUND	conversion failure
1448  *      MrmPARTIAL_SUCCESS internal only, for allocation failure
1449  *
1450  *  SIDE EFFECTS:
1451  *
1452  *--
1453  */
1454 
1455 Cardinal
Urm__UT_GetColorPixel(Display * display,Colormap cmap,RGMColorDescPtr colorptr,Pixel * pixel_return,Pixel fallback)1456 Urm__UT_GetColorPixel (Display			*display,
1457 		       Colormap			cmap,
1458 		       RGMColorDescPtr		colorptr,
1459 		       Pixel			*pixel_return,
1460 		       Pixel			fallback)
1461 {
1462 
1463   /*
1464    *  Local variables
1465    */
1466   XColor		screen_in_out;	/* realizable values */
1467   int			status;		/* function return */
1468 
1469 
1470   if ( cmap == (Colormap)0)
1471     cmap = DefaultColormap (display, DefaultScreen(display));
1472   screen_in_out.red = colorptr->desc.rgb.red;
1473   screen_in_out.green = colorptr->desc.rgb.green;
1474   screen_in_out.blue = colorptr->desc.rgb.blue;
1475   status = XAllocColor (display, cmap, &screen_in_out);
1476 
1477   if ( status == 0) {
1478     if (fallback) {
1479       *pixel_return = fallback;
1480       return MrmPARTIAL_SUCCESS;
1481     } else
1482       return MrmFAILURE;
1483   }
1484   *pixel_return = screen_in_out.pixel;
1485   return MrmSUCCESS;
1486 
1487 }
1488 
1489 
1490 
1491 /*
1492  *++
1493  *
1494  *  PROCEDURE DESCRIPTION:
1495  *
1496  *	This routine computes the number of bytes used by a URM icon
1497  *
1498  *  FORMAL PARAMETERS:
1499  *
1500  *	icon		URM icon image
1501  *
1502  *  IMPLICIT INPUTS:
1503  *
1504  *  IMPLICIT OUTPUTS:
1505  *
1506  *  FUNCTION VALUE:
1507  *
1508  *	# bytes in the image
1509  *
1510  *  SIDE EFFECTS:
1511  *
1512  *--
1513  */
1514 
1515 Cardinal
UrmIconImageSize(RGMIconImagePtr icon)1516 UrmIconImageSize (RGMIconImagePtr	icon)
1517 {
1518 
1519   /*
1520    *  Local variables
1521    */
1522   int			bytes_per_line;	/* # bytes for padded width */
1523   int			raster_len;	/* bytes in image */
1524   Cardinal		size;		/* # bytes in descriptor */
1525 
1526 
1527   bytes_per_line = (icon->width+7) / 8;
1528   raster_len = bytes_per_line * icon->height;
1529   size = sizeof(RGMIconImage) + (raster_len-1)*sizeof(char);
1530   return size;
1531 
1532 }
1533 
1534 
1535 /*
1536  *++
1537  *
1538  *  PROCEDURE DESCRIPTION:
1539  *
1540  *	This routine computes the number of bytes necessary to store
1541  *	the given color table in a single memory block.
1542  *
1543  *  FORMAL PARAMETERS:
1544  *
1545  *	ctable		An allocated color table
1546  *
1547  *  IMPLICIT INPUTS:
1548  *
1549  *  IMPLICIT OUTPUTS:
1550  *
1551  *  FUNCTION VALUE:
1552  *
1553  *	table size in bytes
1554  *
1555  *  SIDE EFFECTS:
1556  *
1557  *--
1558  */
1559 
1560 /*ARGSUSED*/
1561 Cardinal
UrmColorTableSize(RGMColorTablePtr ctable)1562 UrmColorTableSize (RGMColorTablePtr	ctable)	/* unused */
1563 {
1564 
1565   return sizeof(RGMColorTable);
1566 
1567 }
1568