X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=form%2Ffrm_def.c;h=a9f21f28a94464662904124db1e8bc8ee5fd6d2f;hp=c2044a39dccd153e579825150c1e5e90fa502bcb;hb=643ec2bf782cd02efafe3ccdeaea8920a404645e;hpb=491a3f08b795f494ae17179338c31a11b18fd433 diff --git a/form/frm_def.c b/form/frm_def.c index c2044a39..a9f21f28 100644 --- a/form/frm_def.c +++ b/form/frm_def.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc. * + * Copyright 2020,2021 Thomas E. Dickey * + * Copyright 1998-2010,2012 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -32,7 +33,7 @@ #include "form.priv.h" -MODULE_ID("$Id: frm_def.c,v 1.22 2007/10/13 19:31:17 tom Exp $") +MODULE_ID("$Id: frm_def.c,v 1.30 2021/03/27 23:49:58 tom Exp $") /* this can't be readonly */ static FORM default_form = @@ -61,14 +62,14 @@ static FORM default_form = NULL /* fieldterm */ }; -NCURSES_EXPORT_VAR(FORM *) _nc_Default_Form = &default_form; - +FORM_EXPORT_VAR(FORM *) _nc_Default_Form = &default_form; + /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform | Function : static FIELD *Insert_Field_By_Position( -| FIELD *new_field, +| FIELD *new_field, | FIELD *head ) -| +| | Description : Insert new_field into sorted fieldlist with head "head" | and return new head of sorted fieldlist. Sorting | criteria is (row,column). This is a circular list. @@ -112,9 +113,9 @@ Insert_Field_By_Position(FIELD *newfield, FIELD *head) } /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform | Function : static void Disconnect_Fields(FORM *form) -| +| | Description : Break association between form and array of fields. | | Return Values : - @@ -142,9 +143,9 @@ Disconnect_Fields(FORM *form) } /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform | Function : static int Connect_Fields(FORM *form, FIELD **fields) -| +| | Description : Set association between form and array of fields. | | Return Values : E_OK - no error @@ -157,10 +158,9 @@ Connect_Fields(FORM *form, FIELD **fields) { int field_cnt, j; int page_nr; - int maximum_row_in_field, maximum_col_in_field; _PAGE *pg; - T((T_CALLED("Connect_Fields(%p,%p)"), form, fields)); + T((T_CALLED("Connect_Fields(%p,%p)"), (void *)form, (void *)fields)); assert(form); @@ -188,7 +188,7 @@ Connect_Fields(FORM *form, FIELD **fields) /* allocate page structures */ if ((pg = typeMalloc(_PAGE, page_nr)) != (_PAGE *) 0) { - T((T_CREATE("_PAGE %p"), pg)); + T((T_CREATE("_PAGE %p"), (void *)pg)); form->page = pg; } else @@ -198,15 +198,18 @@ Connect_Fields(FORM *form, FIELD **fields) size of the form */ for (j = 0; j < field_cnt; j++) { + int maximum_row_in_field; + int maximum_col_in_field; + if (j == 0) - pg->pmin = j; + pg->pmin = (short)j; else { if (fields[j]->status & _NEWPAGE) { - pg->pmax = j - 1; + pg->pmax = (short)(j - 1); pg++; - pg->pmin = j; + pg->pmin = (short)j; } } @@ -214,14 +217,14 @@ Connect_Fields(FORM *form, FIELD **fields) maximum_col_in_field = fields[j]->fcol + fields[j]->cols; if (form->rows < maximum_row_in_field) - form->rows = maximum_row_in_field; + form->rows = (short)maximum_row_in_field; if (form->cols < maximum_col_in_field) - form->cols = maximum_col_in_field; + form->cols = (short)maximum_col_in_field; } - pg->pmax = field_cnt - 1; - form->maxfield = field_cnt; - form->maxpage = page_nr; + pg->pmax = (short)(field_cnt - 1); + form->maxfield = (short)field_cnt; + form->maxpage = (short)page_nr; /* Sort fields on form pages */ for (page_nr = 0; page_nr < form->maxpage; page_nr++) @@ -230,21 +233,29 @@ Connect_Fields(FORM *form, FIELD **fields) for (j = form->page[page_nr].pmin; j <= form->page[page_nr].pmax; j++) { - fields[j]->index = j; - fields[j]->page = page_nr; + fields[j]->index = (short)j; + fields[j]->page = (short)page_nr; fld = Insert_Field_By_Position(fields[j], fld); } - form->page[page_nr].smin = fld->index; - form->page[page_nr].smax = fld->sprev->index; + if (fld) + { + form->page[page_nr].smin = fld->index; + form->page[page_nr].smax = fld->sprev->index; + } + else + { + form->page[page_nr].smin = 0; + form->page[page_nr].smax = 0; + } } RETURN(E_OK); } /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform | Function : static int Associate_Fields(FORM *form, FIELD **fields) -| -| Description : Set association between form and array of fields. +| +| Description : Set association between form and array of fields. | If there are fields, position to first active field. | | Return Values : E_OK - success @@ -274,9 +285,9 @@ Associate_Fields(FORM *form, FIELD **fields) } /*--------------------------------------------------------------------------- -| Facility : libnform -| Function : FORM *new_form( FIELD **fields ) -| +| Facility : libnform +| Function : FORM *new_form_sp(SCREEN* sp, FIELD** fields ) +| | Description : Create new form with given array of fields. | | Return Values : Pointer to form. NULL if error occurred. @@ -286,22 +297,32 @@ Associate_Fields(FORM *form, FIELD **fields) | E_CONNECTED - a field is already connected | E_SYSTEM_ERROR - not enough memory +--------------------------------------------------------------------------*/ -NCURSES_EXPORT(FORM *) -new_form(FIELD **fields) +FORM_EXPORT(FORM *) +NCURSES_SP_NAME(new_form) (NCURSES_SP_DCLx FIELD **fields) { int err = E_SYSTEM_ERROR; + FORM *form = (FORM *)0; - FORM *form = typeMalloc(FORM, 1); + T((T_CALLED("new_form(%p,%p)"), (void *)SP_PARM, (void *)fields)); - T((T_CALLED("new_form(%p)"), fields)); - if (form) + if (IsValidScreen(SP_PARM)) { - T((T_CREATE("form %p"), form)); - *form = *_nc_Default_Form; - if ((err = Associate_Fields(form, fields)) != E_OK) + form = typeMalloc(FORM, 1); + + if (form) { - free_form(form); - form = (FORM *)0; + T((T_CREATE("form %p"), (void *)form)); + *form = *_nc_Default_Form; + /* This ensures win and sub are always non-null, + so we can derive always the SCREEN that this form is + running on. */ + form->win = StdScreen(SP_PARM); + form->sub = StdScreen(SP_PARM); + if ((err = Associate_Fields(form, fields)) != E_OK) + { + free_form(form); + form = (FORM *)0; + } } } @@ -312,19 +333,40 @@ new_form(FIELD **fields) } /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform +| Function : FORM* new_form(FIELD** fields ) +| +| Description : Create new form with given array of fields. +| +| Return Values : Pointer to form. NULL if error occurred. +! Set errno: +| E_OK - success +| E_BAD_ARGUMENT - Invalid form pointer or field array +| E_CONNECTED - a field is already connected +| E_SYSTEM_ERROR - not enough memory ++--------------------------------------------------------------------------*/ +#if NCURSES_SP_FUNCS +FORM_EXPORT(FORM *) +new_form(FIELD **fields) +{ + return NCURSES_SP_NAME(new_form) (CURRENT_SCREEN, fields); +} +#endif + +/*--------------------------------------------------------------------------- +| Facility : libnform | Function : int free_form( FORM *form ) -| +| | Description : Release internal memory associated with form. | | Return Values : E_OK - no error | E_BAD_ARGUMENT - invalid form pointer | E_POSTED - form is posted +--------------------------------------------------------------------------*/ -NCURSES_EXPORT(int) +FORM_EXPORT(int) free_form(FORM *form) { - T((T_CALLED("free_form(%p)"), form)); + T((T_CALLED("free_form(%p)"), (void *)form)); if (!form) RETURN(E_BAD_ARGUMENT); @@ -341,9 +383,9 @@ free_form(FORM *form) } /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform | Function : int set_form_fields( FORM *form, FIELD **fields ) -| +| | Description : Set a new association of an array of fields to a form | | Return Values : E_OK - no error @@ -352,13 +394,13 @@ free_form(FORM *form) | E_POSTED - form is posted | E_SYSTEM_ERROR - not enough memory +--------------------------------------------------------------------------*/ -NCURSES_EXPORT(int) +FORM_EXPORT(int) set_form_fields(FORM *form, FIELD **fields) { FIELD **old; int res; - T((T_CALLED("set_form_fields(%p,%p)"), form, fields)); + T((T_CALLED("set_form_fields(%p,%p)"), (void *)form, (void *)fields)); if (!form) RETURN(E_BAD_ARGUMENT); @@ -376,32 +418,32 @@ set_form_fields(FORM *form, FIELD **fields) } /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform | Function : FIELD **form_fields( const FORM *form ) -| +| | Description : Retrieve array of fields | | Return Values : Pointer to field array +--------------------------------------------------------------------------*/ -NCURSES_EXPORT(FIELD **) +FORM_EXPORT(FIELD **) form_fields(const FORM *form) { - T((T_CALLED("form_field(%p)"), form)); + T((T_CALLED("form_field(%p)"), (const void *)form)); returnFieldPtr(Normalize_Form(form)->field); } /*--------------------------------------------------------------------------- -| Facility : libnform +| Facility : libnform | Function : int field_count( const FORM *form ) -| +| | Description : Retrieve number of fields | | Return Values : Number of fields, -1 if none are defined +--------------------------------------------------------------------------*/ -NCURSES_EXPORT(int) +FORM_EXPORT(int) field_count(const FORM *form) { - T((T_CALLED("field_count(%p)"), form)); + T((T_CALLED("field_count(%p)"), (const void *)form)); returnCode(Normalize_Form(form)->maxfield); }