xref: /openbsd-src/lib/libform/fld_max.c (revision c7ef0cfc17afcba97172c25e1e3a943e893bc632)
1*c7ef0cfcSnicm /*	$OpenBSD: fld_max.c,v 1.7 2023/10/17 09:52:10 nicm Exp $	*/
202f2426aSmillert /****************************************************************************
3*c7ef0cfcSnicm  * Copyright 2019-2020,2021 Thomas E. Dickey                                *
4*c7ef0cfcSnicm  * Copyright 1998-2012,2013 Free Software Foundation, Inc.                  *
502f2426aSmillert  *                                                                          *
602f2426aSmillert  * Permission is hereby granted, free of charge, to any person obtaining a  *
702f2426aSmillert  * copy of this software and associated documentation files (the            *
802f2426aSmillert  * "Software"), to deal in the Software without restriction, including      *
902f2426aSmillert  * without limitation the rights to use, copy, modify, merge, publish,      *
1002f2426aSmillert  * distribute, distribute with modifications, sublicense, and/or sell       *
1102f2426aSmillert  * copies of the Software, and to permit persons to whom the Software is    *
1202f2426aSmillert  * furnished to do so, subject to the following conditions:                 *
1302f2426aSmillert  *                                                                          *
1402f2426aSmillert  * The above copyright notice and this permission notice shall be included  *
1502f2426aSmillert  * in all copies or substantial portions of the Software.                   *
1602f2426aSmillert  *                                                                          *
1702f2426aSmillert  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1802f2426aSmillert  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1902f2426aSmillert  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
2002f2426aSmillert  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
2102f2426aSmillert  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2202f2426aSmillert  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2302f2426aSmillert  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2402f2426aSmillert  *                                                                          *
2502f2426aSmillert  * Except as contained in this notice, the name(s) of the above copyright   *
2602f2426aSmillert  * holders shall not be used in advertising or otherwise to promote the     *
2702f2426aSmillert  * sale, use or other dealings in this Software without prior written       *
2802f2426aSmillert  * authorization.                                                           *
2902f2426aSmillert  ****************************************************************************/
3002f2426aSmillert 
3102f2426aSmillert /****************************************************************************
3281d8c4e1Snicm  *   Author:  Juergen Pfeifer, 1995,1997                                    *
3302f2426aSmillert  ****************************************************************************/
348d0fca71Smillert 
358d0fca71Smillert #include "form.priv.h"
368d0fca71Smillert 
37*c7ef0cfcSnicm MODULE_ID("$Id: fld_max.c,v 1.7 2023/10/17 09:52:10 nicm Exp $")
388d0fca71Smillert 
398d0fca71Smillert /*---------------------------------------------------------------------------
408d0fca71Smillert |   Facility      :  libnform
418d0fca71Smillert |   Function      :  int set_max_field(FIELD *field, int maxgrow)
428d0fca71Smillert |
438d0fca71Smillert |   Description   :  Set the maximum growth for a dynamic field. If maxgrow=0
448d0fca71Smillert |                    the field may grow to any possible size.
458d0fca71Smillert |
468d0fca71Smillert |   Return Values :  E_OK           - success
478d0fca71Smillert |                    E_BAD_ARGUMENT - invalid argument
488d0fca71Smillert +--------------------------------------------------------------------------*/
FORM_EXPORT(int)49*c7ef0cfcSnicm FORM_EXPORT(int)
5084af20ceSmillert set_max_field(FIELD *field, int maxgrow)
518d0fca71Smillert {
52*c7ef0cfcSnicm   T((T_CALLED("set_max_field(%p,%d)"), (void *)field, maxgrow));
5381d8c4e1Snicm 
548d0fca71Smillert   if (!field || (maxgrow < 0))
558d0fca71Smillert     RETURN(E_BAD_ARGUMENT);
568d0fca71Smillert   else
578d0fca71Smillert     {
588d0fca71Smillert       bool single_line_field = Single_Line_Field(field);
598d0fca71Smillert 
608d0fca71Smillert       if (maxgrow > 0)
618d0fca71Smillert 	{
62*c7ef0cfcSnicm 	  if (((single_line_field && (maxgrow < field->dcols)) ||
63*c7ef0cfcSnicm 	       (!single_line_field && (maxgrow < field->drows))) &&
64*c7ef0cfcSnicm 	      !Field_Has_Option(field, O_INPUT_LIMIT))
658d0fca71Smillert 	    RETURN(E_BAD_ARGUMENT);
668d0fca71Smillert 	}
678d0fca71Smillert       field->maxgrow = maxgrow;
68*c7ef0cfcSnicm       /* shrink */
69*c7ef0cfcSnicm       if (maxgrow > 0 && Field_Has_Option(field, O_INPUT_LIMIT) &&
70*c7ef0cfcSnicm 	  field->dcols > maxgrow)
71*c7ef0cfcSnicm 	field->dcols = maxgrow;
72*c7ef0cfcSnicm       ClrStatus(field, _MAY_GROW);
73*c7ef0cfcSnicm       if (!((unsigned)field->opts & O_STATIC))
748d0fca71Smillert 	{
758d0fca71Smillert 	  if ((maxgrow == 0) ||
768d0fca71Smillert 	      (single_line_field && (field->dcols < maxgrow)) ||
778d0fca71Smillert 	      (!single_line_field && (field->drows < maxgrow)))
78*c7ef0cfcSnicm 	    SetStatus(field, _MAY_GROW);
798d0fca71Smillert 	}
808d0fca71Smillert     }
818d0fca71Smillert   RETURN(E_OK);
828d0fca71Smillert }
838d0fca71Smillert 
848d0fca71Smillert /* fld_max.c ends here */
85