#if !NO_SCCS_STRINGS
static char sccs_width_c[] = "@(#)width.c	128.1 1/20/93 19:39:09";
#endif /* NO_SCCS_STRINGS */
/***********************************************************************\

   (c) Copyright 1991 by Lotus Development Corporation
   All Rights Reserved

   Module:  width.c

   Description:  A custom @function that returns the current column's
   width.  An @function's result does not normally depend on the location
   of the cell in which the formula resides.  Since we do not wish the
   user to have to specifiy this location as input, we use the 
   LpiClConstructContext function, which initializes a Cell object to the
   currently recalculating cell.  @WIDTH, therefore, requires no input
   arguments.  During recalculation, 1-2-3 only updates the value of
   functions whose inputs have changed.  As @WIDTH's result depends on
   the current column width, we want to mark the function as always
   requiring recalculation. 

\************************************************************************/

#define LPI_MAIN_MODULE      /* identify this module as the primary one */ 

#define USE_REGISTER     /* incorporate routines to register @functions */
#define USE_CELL

#include "lpixport.h"           /* incorporate Lotus header information */

#define WIDTH_NAME "width"                  /* name of add-in @function */

/************************************************************************/

lsshort col_width(RegContext, arg_count, LpiContext)
   LpiRegContextType RegContext;       /* required registration context */
   lushort arg_count;                      /* number of input arguments */
   lptr(CONTEXT) LpiContext;                    /* required Lpi context */
{
   
   lushort theWidth;                                    /* result value */
   lsshort retval;                                       /* return code */
   LpiCellType theCell;

   /* construct cell for current recalc coords */

   retval = LpiClConstructContext (&theCell, LPI_T_REGISTRATION_ATFUNCTION);
   if (retval != LPI_SUCCESS) return retval; 

   /* get column width of current recalc cell */

   retval = LpiClGetWidth (theCell, &theWidth);
   if (retval == LPI_S_HIDDEN_COLUMN) {
      theWidth = 0;
   }
   else if (retval != LPI_SUCCESS) {
      LpiClDestroy(&theCell);
      return retval;
   }

   /* identify the return type and set the return value */

   LpiRegAtSetRetType(LPI_T_REG_AT_TYPE_NUM_INT);       
   * ( (lptr(lulong))LpiRegAtAccessRetData ()) = theWidth; 

   LpiClDestroy(&theCell);
   return LPI_SUCCESS;                            /* signal no problems */
}

/************************************************************************\

   The following function, LpiBind, is called when 1-2-3 loads our add-in.
   In this function we load the Register module and register our @function.
   Use this function to perform any setup, such as allocating memory,
   opening files, etc.

\************************************************************************/

LpiBind(width_b)
{
   lsshort retval;                              /* function return code */

   LPI_SETUP_LOAD;      /* required before loading zero or more modules */


   if (LPI_LOAD_CELL != LPI_SUCCESS)               /* load Cell lmodule */
      LpiSignal (LPI_FAILURE);
   if (LPI_LOAD_REGISTER != LPI_SUCCESS)        /* load Register module */
      LpiSignal (LPI_FAILURE);

   retval = LpiRegAtfunc(
      WIDTH_NAME,                  /* @function name in a #define above */
      col_width,          /* name of function that implements @function */
      0,                                   /* number of input arguments */
      LNULL,                           /* array of input argument types */ 
      LNULL,                                    /* registration context */
      LPI_T_REG_AT_ALWAYS_DIRTY,                  /* always recalculate */
      LPI_T_REG_AT_TYPE_NUM_INT);                 /* returns an integer */

   /* if registration fails, indicate an error */
   if (retval != LPI_SUCCESS) LpiSignal (LPI_FAILURE); 

   LpiSignal (LPI_SUCCESS);             /* otherwise indicate no errors */
}

/************************************************************************\

   The following function, LpiUnbind, is called when 1-2-3 unloads our
   add-in.  This function simply executes the macro LPI_UNBIND and
   signals no errors.  Use this function to perform any clean up such as
   deallocating memory you allocated in LpiBind, closing opened files, etc.

   Note: You do not have to explicitly unregister an @function.  When you
   unload the add-in that defines the function, 1-2-3 automatically
   unregisters any @functions the add-in defined.  If you attempt to enter
   an @function that has not yet been registered, 1-2-3 beeps. All existing
   formulas that contain add-in @functions that are currently unregistered
   resolve to ERR.  The @functions will resolve properly the next time the
   add-in registers the @function.

/************************************************************************/

LpiUnbind(width_u)
{
   LPI_UNLOAD;                                    /* macro for clean-up */

   LpiSignal(LPI_SUCCESS);                        /* indicate no errors */
}
