Home | History | Annotate | Download | only in libform

Lines Matching defs:field

1 /*	$NetBSD: field.c,v 1.32 2021/04/13 13:13:03 christos Exp $	*/
32 __RCSID("$NetBSD: field.c,v 1.32 2021/04/13 13:13:03 christos Exp $");
43 FIELD _formi_default_field = {
44 0, /* rows in the field */
45 0, /* columns in the field */
52 0, /* index of this field in form fields array. */
53 0, /* number of buffers associated with this field */
55 NO_JUSTIFICATION, /* justification style of the field */
56 FALSE, /* set to true if field is in overlay mode */
59 NULL, /* starting line in field (vert scroll) */
60 0, /* number of rows actually used in field */
62 0, /* x pos of cursor in field */
63 0, /* y pos of cursor in field */
65 0, /* number of the page this field is on */
69 DEFAULT_FORM_OPTS, /* options for the field */
70 NULL, /* the form this field is bound to, if any */
71 NULL, /* field above this one */
72 NULL, /* field below this one */
73 NULL, /* field to the left of this one */
74 NULL, /* field to the right of this one */
77 NULL, /* type struct for the field */
79 NULL, /* args for field type. */
82 NULL, /* array of buffers for the field */
87 field_buffer_init(FIELD *field, int buffer, unsigned int len);
88 static FIELD *
89 _formi_create_field(FIELD *, int, int, int, int, int, int);
93 * Set the userptr for the field
96 set_field_userptr(FIELD *field, void *ptr)
98 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
106 * Return the userptr for the field.
110 field_userptr(FIELD *field)
112 if (field == NULL)
115 return field->userptr;
119 * Set the options for the designated field.
122 set_field_opts(FIELD *field, Form_Options options)
126 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
128 /* not allowed to set opts if the field is the current one */
129 if ((field != NULL) && (field->parent != NULL) &&
130 (field->parent->cur_field == field->index))
142 /* if appropriate, redraw the field */
143 if ((field != NULL) && (field->parent != NULL)
144 && (field->parent->posted == 1)) {
145 _formi_redraw_field(field->parent, field->index);
146 pos_form_cursor(field->parent);
147 wrefresh(field->parent->scrwin);
154 * Turn on the passed field options.
157 field_opts_on(FIELD *field, Form_Options options)
161 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
163 /* not allowed to set opts if the field is the current one */
164 if ((field != NULL) && (field->parent != NULL) &&
165 (field->parent->cur_field == field->index))
177 /* if appropriate, redraw the field */
178 if ((field != NULL) && (field->parent != NULL)
179 && (field->parent->posted == 1)) {
180 _formi_redraw_field(field->parent, field->index);
181 pos_form_cursor(field->parent);
182 wrefresh(field->parent->scrwin);
189 * Turn off the passed field options.
192 field_opts_off(FIELD *field, Form_Options options)
194 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
196 /* not allowed to set opts if the field is the current one */
197 if ((field != NULL) && (field->parent != NULL) &&
198 (field->parent->cur_field == field->index))
203 /* if appropriate, redraw the field */
204 if ((field != NULL) && (field->parent != NULL)
205 && (field->parent->posted == 1)) {
206 _formi_redraw_field(field->parent, field->index);
207 pos_form_cursor(field->parent);
208 wrefresh(field->parent->scrwin);
215 * Return the field options associated with the passed field.
218 field_opts(FIELD *field)
220 if (field == NULL)
223 return field->opts;
227 * Set the justification for the passed field.
230 set_field_just(FIELD *field, int justification)
232 FIELD *fp = (field == NULL) ? &_formi_default_field : field;
235 * not allowed to set justification if the field is
238 if ((field != NULL) && (field->parent != NULL) &&
239 (field->parent->cur_field == field->index))
259 * Return the justification style of the field passed.
262 field_just(FIELD *field)
264 if (field == NULL)
267 return field->justification;
271 * Return information about the field passed.
274 field_info(FIELD *field, int *rows, int *cols, int *frow, int *fcol,
277 if (field == NULL)
280 *rows = field->rows;
281 *cols = field->cols;
282 *frow = field->form_row;
283 *fcol = field->form_col;
284 *nrow = field->nrows;
285 *nbuf = field->nbuf;
291 * Report the dynamic field information.
294 dynamic_field_info(FIELD *field, int *drows, int *dcols, int *max)
296 if (field == NULL)
299 if ((field->opts & O_STATIC) == O_STATIC) {
300 *drows = field->rows;
301 *dcols = field->cols;
303 *drows = field->drows;
304 *dcols = field->dcols;
307 *max = field->max;
313 * Init all the field variables, perform wrapping and other tasks
314 * after the field buffer is set.
317 field_buffer_init(FIELD *field, int buffer, unsigned int len)
323 field->start_char = 0;
324 field->start_line = 0;
325 field->row_xpos = 0;
326 field->cursor_xpos = 0;
327 field->cursor_ypos = 0;
328 field->row_count = 1; /* must be at least one row XXX need to shift old rows (if any) to free list??? */
329 field->alines->length = len;
330 if ((newp = realloc(field->alines->string,
333 field->alines->string = newp;
334 field->alines->allocated = len + 1;
335 strlcpy(field->alines->string, field->buffers[buffer].string,
337 field->alines->expanded =
338 _formi_tab_expanded_length(field->alines->string,
339 0, field->alines->length);
341 field->start_line = field->alines;
342 field->cur_line = field->alines;
346 status = _formi_wrap_field(field, field->cur_line);
351 * calculate the tabs for a single row field, the
354 if (field->row_count == 1)
355 _formi_calculate_tabs(field->alines);
357 /* redraw the field to reflect the new contents. If the field
360 if ((field->parent != NULL) && (field->parent->posted == 1)) {
361 _formi_redraw_field(field->parent, field->index);
362 /* make sure cursor goes back to current field */
363 pos_form_cursor(field->parent);
372 * Set the field buffer to the string that results from processing
376 set_field_printf(FIELD *field, int buffer, char *fmt, ...)
381 if (field == NULL)
384 if (buffer >= field->nbuf)
389 if (field->buffers[buffer].allocated != 0)
390 free(field->buffers[buffer].string);
392 len = vasprintf(&field->buffers[buffer].string, fmt, args);
397 field->buffers[buffer].length = len;
398 field->buffers[buffer].allocated = len + 1;
399 if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
400 && ((field->rows + field->nrows) == 1))
401 len = field->cols;
403 field->buffers[buffer].string[len] = '\0';
404 return field_buffer_init(field, buffer, (unsigned int) len);
408 * Set the value of the field buffer to the value given.
412 set_field_buffer(FIELD *field, int buffer, const char *value)
417 if (field == NULL)
420 if (buffer >= field->nbuf) /* make sure buffer is valid */
424 if (((field->opts & O_STATIC) == O_STATIC) && (len > field->cols)
425 && ((field->rows + field->nrows) == 1))
426 len = field->cols;
430 if (field->buffers[buffer].string != NULL)
432 field->buffers[buffer].string,
433 field->buffers[buffer].length);
437 field->alines[0].length);
439 if ((field->buffers[buffer].string = realloc(
440 field->buffers[buffer].string, (size_t) len + 1)) == NULL)
443 strlcpy(field->buffers[buffer].string, value, (size_t) len + 1);
444 field->buffers[buffer].length = len;
445 field->buffers[buffer].allocated = len + 1;
446 status = field_buffer_init(field, buffer, len);
450 field->buffers[buffer].string, field->buffers[buffer].length);
452 field->alines[0].length);
458 * Return the requested field buffer to the caller.
461 field_buffer(FIELD *field, int buffer)
468 if (field == NULL)
471 if (buffer >= field->nbuf)
483 if (_formi_sync_buffer(field) != E_OK)
486 if ((field->opts & O_REFORMAT) != O_REFORMAT)
487 return field->buffers[buffer].string;
489 if (field->row_count <= 1)
490 return strdup(field->buffers[buffer].string);
499 for (linep = field->alines; linep; linep = linep->next) {
518 * Set the buffer 0 field status.
521 set_field_status(FIELD *field, int status)
524 if (field == NULL)
528 field->buf0_status = TRUE;
530 field->buf0_status = FALSE;
536 * Return the buffer 0 status flag for the given field.
539 field_status(FIELD *field)
542 if (field == NULL) /* the default buffer 0 never changes :-) */
545 return field->buf0_status;
549 * Set the maximum growth for a dynamic field.
552 set_max_field(FIELD *fptr, int max)
554 FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
556 if ((field->opts & O_STATIC) == O_STATIC) /* check if field dynamic */
562 field->max = max;
567 * Set the field foreground character attributes.
570 set_field_fore(FIELD *fptr, chtype attribute)
572 FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
574 field->fore = attribute;
579 * Return the foreground character attribute for the given field.
582 field_fore(FIELD *field)
584 if (field == NULL)
587 return field->fore;
591 * Set the background character attribute for the given field.
594 set_field_back(FIELD *field, chtype attribute)
596 if (field == NULL)
599 field->back = attribute;
605 * Get the background character attribute for the given field.
608 field_back(FIELD *field)
610 if (field == NULL)
613 return field->back;
617 * Set the pad character for the given field.
620 set_field_pad(FIELD *field, int pad)
622 if (field == NULL)
625 field->pad = pad;
631 * Return the padding character for the given field.
634 field_pad(FIELD *field)
636 if (field == NULL)
639 return field->pad;
643 * Set the field initialisation function hook.
657 * Return the function hook for the field initialisation.
669 * Set the field termination function hook.
683 * Return the function hook defined for the field termination.
695 * Set the page flag on the given field to indicate it is the start of a
699 set_new_page(FIELD *fptr, int page)
701 FIELD *field = (fptr == NULL)? &_formi_default_field : fptr;
703 if (field->parent != NULL) /* check if field is connected to a form */
706 field->page_break = (page != FALSE);
711 * Return the page status for the given field. TRUE is returned if the
712 * field is the start of a new page.
715 new_page(FIELD *field)
717 if (field == NULL)
720 return field->page_break;
724 * Return the index of the field in the form fields array.
727 field_index(FIELD *field)
729 if (field == NULL)
732 if (field->parent == NULL)
735 return field->index;
739 * Internal function that does most of the work to create a new field.
740 * The new field is initialised from the information in the prototype
741 * field passed.
744 static FIELD *
745 _formi_create_field(FIELD *prototype, int rows, int cols, int frow,
748 FIELD *new;
758 /* copy in the default field info */
772 * Create a new field structure.
774 FIELD *
777 FIELD *new;
827 * Duplicate the given field, including its buffers.
829 FIELD *
830 dup_field(FIELD *field, int frow, int fcol)
832 FIELD *new;
835 if (field == NULL)
839 if ((new = _formi_create_field(field, (int) field->rows,
840 (int) field->cols,
841 frow, fcol, (int) field->nrows,
842 field->nbuf - 1)) == NULL)
845 row_len = (field->rows + field->nrows + 1) * field->cols;
846 buf_len = (field->nbuf + 1) * row_len * sizeof(*new->buffers);
854 /* copy the buffers from the source field into the new copy */
855 memcpy(new->buffers, field->buffers, buf_len);
861 * Create a new field at the specified location by duplicating the given
862 * field. The buffers are shared with the parent field.
864 FIELD *
865 link_field(FIELD *field, int frow, int fcol)
867 FIELD *new;
869 if (field == NULL)
872 if ((new = _formi_create_field(field, (int) field->rows,
873 (int) field->cols,
874 frow, fcol, (int) field->nrows,
875 field->nbuf - 1)) == NULL)
878 new->link = field->link;
879 field->link = new;
881 /* we are done. The buffer pointer was copied during the field
887 * Release all storage allocated to the field
890 free_field(FIELD *field)
892 FIELD *flink;
896 if (field == NULL)
899 if (field->parent != NULL)
902 if (field->link == field) { /* check if field linked */
904 free(field->buffers);
906 for (i = 0; i + 1 < field->row_count; i++) {
907 if (field->alines[i].tabs != NULL) {
908 ts = field->alines[i].tabs;
917 /* is linked, traverse the links to find the field referring
920 for (flink = field->link; flink != field; flink = flink->link);
921 flink->link = field->link;
924 free(field);