Blame lib/Mrm/Mrmwvalues.c

Packit b099d7
/* 
Packit b099d7
 * Motif
Packit b099d7
 *
Packit b099d7
 * Copyright (c) 1987-2012, The Open Group. All rights reserved.
Packit b099d7
 *
Packit b099d7
 * These libraries and programs are free software; you can
Packit b099d7
 * redistribute them and/or modify them under the terms of the GNU
Packit b099d7
 * Lesser General Public License as published by the Free Software
Packit b099d7
 * Foundation; either version 2 of the License, or (at your option)
Packit b099d7
 * any later version.
Packit b099d7
 *
Packit b099d7
 * These libraries and programs are distributed in the hope that
Packit b099d7
 * they will be useful, but WITHOUT ANY WARRANTY; without even the
Packit b099d7
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Packit b099d7
 * PURPOSE. See the GNU Lesser General Public License for more
Packit b099d7
 * details.
Packit b099d7
 *
Packit b099d7
 * You should have received a copy of the GNU Lesser General Public
Packit b099d7
 * License along with these librararies and programs; if not, write
Packit b099d7
 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
Packit b099d7
 * Floor, Boston, MA 02110-1301 USA
Packit b099d7
*/ 
Packit b099d7
/* 
Packit b099d7
 * HISTORY
Packit b099d7
*/ 
Packit b099d7
#ifdef HAVE_CONFIG_H
Packit b099d7
#include <config.h>
Packit b099d7
#endif
Packit b099d7
Packit b099d7
Packit b099d7
#ifdef REV_INFO
Packit b099d7
#ifndef lint
Packit b099d7
static char rcsid[] = "$TOG: Mrmwvalues.c /main/12 1999/04/16 09:42:44 mgreess $"
Packit b099d7
#endif
Packit b099d7
#endif
Packit b099d7
Packit b099d7
/*
Packit b099d7
*  (c) Copyright 1989, 1990, DIGITAL EQUIPMENT CORPORATION, MAYNARD, MASS. */
Packit b099d7
Packit b099d7
/*
Packit b099d7
 *++
Packit b099d7
 *  FACILITY:
Packit b099d7
 *
Packit b099d7
 *      UIL Resource Manager (URM):
Packit b099d7
 *
Packit b099d7
 *  ABSTRACT:
Packit b099d7
 *
Packit b099d7
 *	This module contains the routines which implement modifying
Packit b099d7
 *	a widget (XtSetValues) from UID literals.
Packit b099d7
 *
Packit b099d7
 *--
Packit b099d7
 */
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 *
Packit b099d7
 *  INCLUDE FILES
Packit b099d7
 *
Packit b099d7
 */
Packit b099d7
Packit b099d7
#include <Mrm/MrmAppl.h>
Packit b099d7
#include <Mrm/Mrm.h>
Packit b099d7
Packit b099d7
Packit b099d7
/*
Packit b099d7
 *
Packit b099d7
 *  TABLE OF CONTENTS
Packit b099d7
 *
Packit b099d7
 *	UrmFetchSetValues	Do XtSetValues from UID literals.
Packit b099d7
 *
Packit b099d7
 */
Packit b099d7
Packit b099d7
Packit b099d7

Packit b099d7
/*
Packit b099d7
 *++
Packit b099d7
 *
Packit b099d7
 *  PROCEDURE DESCRIPTION:
Packit b099d7
 *
Packit b099d7
 *	UrmFetchSetValues is the internal routine which will
Packit b099d7
 *	modify a widget via XtSetValues on an argument list whose values
Packit b099d7
 *	are evaluated literals from a URM hierarchy. 
Packit b099d7
 *
Packit b099d7
 *	The args list has ordinary Strings as each tag name. Each value
Packit b099d7
 *	is interpreted as a String whose value is the index of a literal
Packit b099d7
 *	to be fetched and evaluated as the actual value. This routine
Packit b099d7
 *	constructs its own arglist, copying each tag, and setting the
Packit b099d7
 *	value to the fetched, fixed up, and converted literal value. The
Packit b099d7
 *	argument is not set in this list (is dropped) if there are any
Packit b099d7
 *	errors.
Packit b099d7
 *
Packit b099d7
 *	This code must also handle pixmaps in the same way that URM does;
Packit b099d7
 *	it defers converting icons and inserting them in the list
Packit b099d7
 *	until all the rest of the arglist is done; this allows FG/BG
Packit b099d7
 *	values to be uncovered in the list for use in the pixmap.
Packit b099d7
 *
Packit b099d7
 *  FORMAL PARAMETERS:
Packit b099d7
 *
Packit b099d7
 *	hierarchy_id	URM hierarchy from which to read public resources
Packit b099d7
 *	w		widget to be modified
Packit b099d7
 *	args		arglist, with literal indexes as values
Packit b099d7
 *	num_args	# args in args
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT INPUTS:
Packit b099d7
 *
Packit b099d7
 *  IMPLICIT OUTPUTS:
Packit b099d7
 *
Packit b099d7
 *  FUNCTION VALUE:
Packit b099d7
 *
Packit b099d7
 *	MrmSUCCESS		operation succeeded
Packit b099d7
 *	MrmPARTIAL_SUCCESS	at least one fetch suboperation succeeded
Packit b099d7
 *	other			any error fatal to the operation
Packit b099d7
 *
Packit b099d7
 *  SIDE EFFECTS:
Packit b099d7
 *
Packit b099d7
 *--
Packit b099d7
 */
Packit b099d7
Packit b099d7
Cardinal 
Packit b099d7
UrmFetchSetValues (MrmHierarchy		hierarchy_id ,
Packit b099d7
		   Widget		w ,
Packit b099d7
		   ArgList		args ,
Packit b099d7
		   Cardinal		num_args )
Packit b099d7
{
Packit b099d7
  /*
Packit b099d7
   * Local structures
Packit b099d7
   */
Packit b099d7
  typedef struct {
Packit b099d7
    RGMIconImagePtr	icon ;		/* icon to be converted */
Packit b099d7
    int		pixndx ;	/* argument in arglist (args) */
Packit b099d7
  } _SavePixmapArg, *_SavePixmapArgPtr ;
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   *  Local variables
Packit b099d7
   */
Packit b099d7
  Cardinal		result ;	/* function results */
Packit b099d7
  int			num_succ = 0;	/* # of successful fetches */
Packit b099d7
  int			fet_fail = 0;	/* # of failed fetches */
Packit b099d7
  int			cvt_fail = 0;	/* # failed conversions */
Packit b099d7
  Cardinal		badfet_res = MrmNOT_FOUND; /* to save NOT_FOUND */
Packit b099d7
  ArgList		locargs ;	/* local arglist */
Packit b099d7
  Cardinal		num_used = 0 ;	/* # args used in arglist */
Packit b099d7
  RGMResourceDescPtr	resptr ;	/* descriptor for literals */
Packit b099d7
  int			indexlen = 0 ;	/* longest literal length */
Packit b099d7
  URMPointerListPtr	ptrlist ;	/* to hold scratch contexts */
Packit b099d7
  int			ndx ;		/* loop index */
Packit b099d7
  IDBFile		file_id ;	/* file from which literal read */
Packit b099d7
  MrmType		reptype ;	/* arg value representation type */
Packit b099d7
  long			val ;		/* value as immediate or pointer */
Packit b099d7
  int			vec_count ;	/* number of items in val if vector */
Packit b099d7
  int			vec_size ;
Packit b099d7
  _SavePixmapArg	pixargs[10] ;	/* to save pixmap args */
Packit b099d7
  Cardinal		pixargs_cnt = 0 ;  /* # pixargs saved */
Packit b099d7
  _SavePixmapArgPtr	savepix ;	/* current saved pixmap entry */
Packit b099d7
  Screen		*screen ;	/* screen for pixmaps */
Packit b099d7
  Display		*display ;	/* display for pixmaps */
Packit b099d7
  Pixel			fgint = (Pixel) -1 ; /* fg for pixmaps. */
Packit b099d7
  Pixel			bgint = (Pixel) -1 ; /* background for pixmaps */
Packit b099d7
  Pixmap		pixmap ;	/* pixmap created from icon */
Packit b099d7
  Boolean		swap_needed = FALSE; 
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * Create local arglist and pointer list for contexts.
Packit b099d7
   */
Packit b099d7
  locargs = (ArgList) XtMalloc (num_args*sizeof(Arg)) ;
Packit b099d7
  UrmPlistInit (num_args, &ptrlist) ;
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * Find the longest literal index, and allocate a resource descriptor
Packit b099d7
   * which can hold it. Set the fixed fields of the descriptor.
Packit b099d7
   */
Packit b099d7
  for ( ndx=0 ; ndx
Packit b099d7
    indexlen = MAX (indexlen, strlen((char*)args[ndx].value)) ;
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * Create a resource descriptor which can be reused to fetch each literal
Packit b099d7
   */
Packit b099d7
  resptr = (RGMResourceDescPtr) XtMalloc (sizeof(RGMResourceDesc)+indexlen) ;
Packit b099d7
  resptr->access = URMaPublic ;
Packit b099d7
  resptr->type = URMrIndex ;
Packit b099d7
  resptr->res_group = URMgLiteral ;
Packit b099d7
  resptr->cvt_type = URMtNul ;
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * Loop through all the entries in the given arglist. Copy each tag into
Packit b099d7
   * the local arglist. Create a new context to hold the literal, then
Packit b099d7
   * try to read it from the hierarchy.
Packit b099d7
   */
Packit b099d7
  for ( ndx=0 ; ndx
Packit b099d7
    {
Packit b099d7
      locargs[num_used].name = args[ndx].name ;
Packit b099d7
      strcpy (resptr->key.index, (char*)args[ndx].value) ;
Packit b099d7
      result = Urm__CW_ReadLiteral (resptr, hierarchy_id, NULL,
Packit b099d7
				    ptrlist, &reptype, &val, &vec_count, 
Packit b099d7
				    &file_id, &vec_size) ;
Packit b099d7
      if ( result == MrmSUCCESS )
Packit b099d7
	num_succ += 1;
Packit b099d7
      else
Packit b099d7
	{
Packit b099d7
	  fet_fail += 1;
Packit b099d7
	  if ( result != MrmNOT_FOUND )
Packit b099d7
	    badfet_res = result;
Packit b099d7
	  continue;
Packit b099d7
	}
Packit b099d7
Packit b099d7
      /*
Packit b099d7
       * Fix up and perform conversion on the value. If this succeeds, put it
Packit b099d7
       * in the local arglist. Special casing is required for some types,
Packit b099d7
       * particularly icons.
Packit b099d7
       *
Packit b099d7
       * Type conversion for some ASCIZ strings is also provided as a V1
Packit b099d7
       * compatibility feature.
Packit b099d7
       */
Packit b099d7
      switch ( reptype )
Packit b099d7
        {
Packit b099d7
	case MrmRtypeIconImage:
Packit b099d7
	  savepix = &pixargs[pixargs_cnt] ;
Packit b099d7
	  savepix->icon = (RGMIconImagePtr) val ;
Packit b099d7
	  savepix->pixndx = ndx ;
Packit b099d7
	  pixargs_cnt += 1 ;
Packit b099d7
	  continue ;
Packit b099d7
	case MrmRtypeFontList:
Packit b099d7
	  /* Check for 1.1 version and malloc new fontlist if necessary. */
Packit b099d7
	  if (strcmp(file_id->db_version, URM1_1version) <= 0)
Packit b099d7
	    {
Packit b099d7
	      int count = ((OldRGMFontListPtr)val)->count;
Packit b099d7
	      RGMFontListPtr fontlist = (RGMFontListPtr)
Packit b099d7
		XtMalloc(sizeof(RGMFontList) +
Packit b099d7
			 (sizeof(RGMFontItem) * (count - 1)));
Packit b099d7
	      result = Urm__CW_FixupValue((long)fontlist, reptype, 
Packit b099d7
					  (XtPointer)val, file_id,
Packit b099d7
					  &swap_needed);
Packit b099d7
	      XtFree((char *)val);
Packit b099d7
	      val = (long)fontlist;
Packit b099d7
	    }
Packit b099d7
	  else
Packit b099d7
	    result = Urm__CW_FixupValue (val, reptype, (XtPointer)val, 
Packit b099d7
					 file_id, &swap_needed) ;
Packit b099d7
Packit b099d7
	default:
Packit b099d7
	  result = 
Packit b099d7
	    Urm__CW_FixupValue (val, reptype, (XtPointer)val, file_id,
Packit b099d7
				&swap_needed) ;
Packit b099d7
	  if ( result != MrmSUCCESS )
Packit b099d7
	    {
Packit b099d7
	      num_succ -= 1;
Packit b099d7
	      cvt_fail += 1;
Packit b099d7
	      continue;
Packit b099d7
	    }
Packit b099d7
	  if (XtIsWidget(w))
Packit b099d7
	    display = XtDisplay(w);
Packit b099d7
	  else
Packit b099d7
	    display = XtDisplay(XtParent(w));
Packit b099d7
	    
Packit b099d7
	  result = Urm__CW_ConvertValue (XtParent(w), &val, reptype, (MrmType)0,
Packit b099d7
					 display, hierarchy_id,  NULL) ;
Packit b099d7
	  if ( result != MrmSUCCESS )
Packit b099d7
	    {
Packit b099d7
	      num_succ -= 1;
Packit b099d7
	      cvt_fail += 1;
Packit b099d7
	      continue;
Packit b099d7
	    }
Packit b099d7
	  locargs[num_used].value = (XtArgVal)val ;
Packit b099d7
	  num_used += 1 ;
Packit b099d7
	  break ;
Packit b099d7
        }
Packit b099d7
Packit b099d7
      /*
Packit b099d7
       * Do any processing based on specific arguments (after conversion):
Packit b099d7
       *	- retain FG/BG info
Packit b099d7
       */
Packit b099d7
      switch ( reptype )
Packit b099d7
	{
Packit b099d7
	case MrmRtypeColor:
Packit b099d7
	  if ( strcmp(args[ndx].name,XmNforeground) == 0 )
Packit b099d7
	    fgint = val;
Packit b099d7
Packit b099d7
	  else if ( strcmp (args[ndx].name,XmNbackground) == 0 )
Packit b099d7
	    bgint = val;
Packit b099d7
	  break ;
Packit b099d7
        }
Packit b099d7
Packit b099d7
    }	/* end loop */
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * Now set any pixmap arguments. This requires finding the display, screen,
Packit b099d7
   * foreground, and background values for this widget. These values are
Packit b099d7
   * available from the parent widget and the arglist.
Packit b099d7
   */
Packit b099d7
  if ( pixargs_cnt > 0 )
Packit b099d7
    {
Packit b099d7
      Urm__CW_GetPixmapParms (w, &screen, &display, &fgint, &bgint) ;
Packit b099d7
      for ( ndx=0,savepix=pixargs ; ndx
Packit b099d7
        {
Packit b099d7
	  result = UrmCreatePixmap (savepix->icon, screen, display,
Packit b099d7
				    (Pixel)fgint, (Pixel)bgint, &pixmap, w) ;
Packit b099d7
	  if ( result != MrmSUCCESS )
Packit b099d7
	    {
Packit b099d7
	      num_succ -= 1;
Packit b099d7
	      cvt_fail += 1;
Packit b099d7
	      continue;
Packit b099d7
	    }
Packit b099d7
	  locargs[num_used].name = args[savepix->pixndx].name ;
Packit b099d7
	  locargs[num_used].value = (XtArgVal) pixmap ;
Packit b099d7
	  num_used += 1 ;
Packit b099d7
        }
Packit b099d7
    }
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * arglist creation complete. Modify the widget if we have succeeded in
Packit b099d7
   * fetching any values. Deallocate resources.
Packit b099d7
   */
Packit b099d7
  if ( num_used > 0 )
Packit b099d7
    XtSetValues (w, locargs, num_used) ;
Packit b099d7
Packit b099d7
  XtFree ((char*)locargs) ;
Packit b099d7
  XtFree ((char*)resptr) ;
Packit b099d7
  for ( ndx=0 ; ndx
Packit b099d7
    UrmFreeResourceContext ((URMResourceContextPtr)UrmPlistPtrN(ptrlist,ndx)) ;
Packit b099d7
  UrmPlistFree (ptrlist) ;
Packit b099d7
Packit b099d7
  /*
Packit b099d7
   * Result depends on number of successes vs. number of failurs
Packit b099d7
   */
Packit b099d7
  if ( num_succ > 0 )
Packit b099d7
    {
Packit b099d7
      if ( fet_fail==0 && cvt_fail == 0 )
Packit b099d7
	return MrmSUCCESS;
Packit b099d7
      else
Packit b099d7
	return MrmPARTIAL_SUCCESS;
Packit b099d7
    }
Packit b099d7
  if ( fet_fail > 0 )
Packit b099d7
    return badfet_res;
Packit b099d7
  else
Packit b099d7
    return MrmFAILURE;
Packit b099d7
Packit b099d7
}
Packit b099d7