X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fedit_field.c;h=506d08ae0396ae4fa2cc9cf568e7cf067b200b84;hp=5ac096dcfb7b59aba8c1a9b3ab690d79a978128e;hb=a0bb9c404174926bf131a5989a0524a6eecd09eb;hpb=06ae48ca77d71610e4d86d138a8fd19db84634ce diff --git a/test/edit_field.c b/test/edit_field.c index 5ac096dc..506d08ae 100644 --- a/test/edit_field.c +++ b/test/edit_field.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 2003-2006,2008 Free Software Foundation, Inc. * + * Copyright (c) 2003-2011,2013 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 * @@ -26,11 +26,11 @@ * authorization. * ****************************************************************************/ /* - * $Id: edit_field.c,v 1.15 2008/12/20 19:23:01 tom Exp $ + * $Id: edit_field.c,v 1.22 2013/09/28 22:02:17 tom Exp $ * * A wrapper for form_driver() which keeps track of the user's editing changes - * for each field, and makes the result available as a null-terminated string - * in field_buffer(field,1). + * for each field, and makes the resulting length available as a + * null-terminated string in field_buffer(field,1). * * Thomas Dickey - 2003/4/26. */ @@ -288,14 +288,58 @@ static int offset_in_field(FORM * form) { FIELD *field = current_field(form); - return form->curcol + form->currow * field->dcols; + int currow, curcol; + + form_getyx(form, currow, curcol); + return curcol + currow * field->dcols; } static void inactive_field(FIELD * f) { - FieldAttrs *ptr = (FieldAttrs *) field_userptr(f); - set_field_back(f, ptr->background); + set_field_back(f, field_attrs(f)->background); +} + +FieldAttrs * +field_attrs(FIELD * f) +{ + return (FieldAttrs *) field_userptr(f); +} + +static int +buffer_length(FIELD * f) +{ + return field_attrs(f)->row_lengths[0]; +} + +static void +set_buffer_length(FIELD * f, int length) +{ + field_attrs(f)->row_lengths[0] = length; +} + +/* + * The userptr is used in edit_field.c's inactive_field(), as well as for + * keeping track of the actual lengths of lines in a multiline field. + */ +void +init_edit_field(FIELD * f, char *value) +{ + FieldAttrs *ptr = field_attrs(f); + if (ptr == 0) { + int rows, cols, frow, fcol, nrow, nbuf; + + ptr = typeCalloc(FieldAttrs, (size_t) 1); + ptr->background = field_back(f); + if (field_info(f, &rows, &cols, &frow, &fcol, &nrow, &nbuf) == E_OK) { + ptr->row_count = nrow; + ptr->row_lengths = typeCalloc(int, (size_t) nrow + 1); + } + } + set_field_userptr(f, (void *) ptr); + set_field_buffer(f, 0, value); /* will be formatted */ + set_field_buffer(f, 1, value); /* will be unformatted */ + set_buffer_length(f, (int) strlen(value)); } int @@ -305,13 +349,12 @@ edit_field(FORM * form, int *result) int status; FIELD *before; unsigned n; - char lengths[80]; int length; - char *buffer; - int before_row = form->currow; - int before_col = form->curcol; + int before_row; + int before_col; int before_off = offset_in_field(form); + form_getyx(form, before_row, before_col); before = current_field(form); set_field_back(before, A_NORMAL); if (ch <= KEY_MAX) { @@ -333,9 +376,7 @@ edit_field(FORM * form, int *result) if (status == E_OK) { bool modified = TRUE; - length = 0; - if ((buffer = field_buffer(before, 1)) != 0) - length = atoi(buffer); + length = buffer_length(before); if (length < before_off) length = before_off; switch (*result) { @@ -441,8 +482,7 @@ edit_field(FORM * form, int *result) < MIN_FORM_COMMAND) ++length; - sprintf(lengths, "%d", length); - set_field_buffer(before, 1, lengths); + set_buffer_length(before, length); } if (current_field(form) != before)