1 /****************************************************************************
2 * Copyright (c) 2003-2014,2017 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: edit_field.c,v 1.28 2017/06/24 23:10:08 tom Exp $
31 * A wrapper for form_driver() which keeps track of the user's editing changes
32 * for each field, and makes the resulting length available as a
33 * null-terminated string in field_buffer(field,1).
35 * Thomas Dickey - 2003/4/26.
38 #include <test.priv.h>
42 #include <edit_field.h>
43 #include <popup_msg.h>
52 CTRL('A'), REQ_NEXT_CHOICE, ""
55 CTRL('B'), REQ_PREV_WORD, "go to previous word"
58 CTRL('C'), REQ_CLR_EOL, "clear to end of line"
61 CTRL('D'), REQ_DOWN_FIELD, "move downward to field"
64 CTRL('E'), REQ_END_FIELD, "go to end of field"
67 CTRL('F'), REQ_NEXT_PAGE, "go to next page"
70 CTRL('G'), REQ_DEL_WORD, "delete current word"
73 CTRL('H'), REQ_DEL_PREV, "delete previous character"
76 CTRL('I'), REQ_INS_CHAR, "insert character"
79 CTRL('K'), REQ_CLR_EOF, "clear to end of field"
82 CTRL('L'), REQ_LEFT_FIELD, "go to field to left"
85 CTRL('M'), REQ_NEW_LINE, "insert/overlay new line"
88 CTRL('N'), REQ_NEXT_FIELD, "go to next field"
91 CTRL('O'), REQ_INS_LINE, "insert blank line at cursor"
94 CTRL('P'), REQ_PREV_FIELD, "go to previous field"
97 CTRL('Q'), MY_QUIT, "exit form"
100 CTRL('R'), REQ_RIGHT_FIELD, "go to field to right"
103 CTRL('S'), REQ_BEG_FIELD, "go to beginning of field"
106 CTRL('T'), MY_EDT_MODE, "toggle O_EDIT mode, clear field status",
109 CTRL('U'), REQ_UP_FIELD, "move upward to field"
112 CTRL('V'), REQ_DEL_CHAR, "delete character"
115 CTRL('W'), REQ_NEXT_WORD, "go to next word"
118 CTRL('X'), REQ_CLR_FIELD, "clear field"
121 CTRL('Y'), REQ_DEL_LINE, "delete line"
124 CTRL('Z'), REQ_PREV_CHOICE, ""
127 CTRL('['), MY_QUIT, "exit form"
130 CTRL(']'), MY_INS_MODE, "toggle REQ_INS_MODE/REQ_OVL_MODE",
133 KEY_F(1), MY_HELP, "show this screen",
136 KEY_BACKSPACE, REQ_DEL_PREV, "delete previous character"
139 KEY_DOWN, REQ_DOWN_CHAR, "move down 1 character"
142 KEY_END, REQ_LAST_FIELD, "go to last field"
145 KEY_HOME, REQ_FIRST_FIELD, "go to first field"
148 KEY_LEFT, REQ_LEFT_CHAR, "move left 1 character"
151 KEY_LL, REQ_LAST_FIELD, "go to last field"
154 KEY_NEXT, REQ_NEXT_FIELD, "go to next field"
157 KEY_NPAGE, REQ_NEXT_PAGE, "go to next page"
160 KEY_PPAGE, REQ_PREV_PAGE, "go to previous page"
163 KEY_PREVIOUS, REQ_PREV_FIELD, "go to previous field"
166 KEY_RIGHT, REQ_RIGHT_CHAR, "move right 1 character"
169 KEY_UP, REQ_UP_CHAR, "move up 1 character"
174 * Display a temporary window listing the keystroke-commands we recognize.
177 help_edit_field(void)
181 char **msgs = typeCalloc(char *, 3 + SIZEOF(commands));
183 msgs[used++] = strdup("Defined form edit/traversal keys:");
184 for (n = 0; n < SIZEOF(commands); ++n) {
187 const char *code = keyname(commands[n].code);
189 #ifdef NCURSES_VERSION
190 if ((name = form_request_name(commands[n].result)) == 0)
192 name = commands[n].help;
193 need = 5 + strlen(code) + strlen(name);
194 msg = typeMalloc(char, need);
195 sprintf(msg, "%s -- %s", code, name);
199 strdup("Arrow keys move within a field as you would expect.");
201 popup_msg2(stdscr, msgs);
202 for (n = 0; msgs[n] != 0; ++n) {
209 offset_in_field(FORM *form)
211 FIELD *field = current_field(form);
214 form_getyx(form, currow, curcol);
215 return curcol + currow * field->dcols;
219 inactive_field(FIELD *f)
221 set_field_back(f, field_attrs(f)->background);
225 field_attrs(FIELD *f)
227 return (FieldAttrs *) field_userptr(f);
231 buffer_length(FIELD *f)
233 return field_attrs(f)->row_lengths[0];
237 set_buffer_length(FIELD *f, int length)
239 field_attrs(f)->row_lengths[0] = length;
243 * The userptr is used in edit_field.c's inactive_field(), as well as for
244 * keeping track of the actual lengths of lines in a multiline field.
247 init_edit_field(FIELD *f, char *value)
250 FieldAttrs *ptr = field_attrs(f);
252 int rows, cols, frow, fcol, nrow, nbuf;
254 ptr = typeCalloc(FieldAttrs, (size_t) 1);
255 ptr->background = field_back(f);
256 if (field_info(f, &rows, &cols, &frow, &fcol, &nrow, &nbuf) == E_OK) {
257 ptr->row_count = nrow;
258 ptr->row_lengths = typeCalloc(int, (size_t) nrow + 1);
265 set_field_userptr(f, (void *) ptr);
266 set_field_buffer(f, 0, value); /* will be formatted */
267 set_field_buffer(f, 1, value); /* will be unformatted */
268 set_buffer_length(f, (int) strlen(value));
272 edit_field(FORM *form, int *result)
274 int ch = wgetch(form_win(form));
281 int before_off = offset_in_field(form);
283 form_getyx(form, before_row, before_col);
284 before = current_field(form);
285 set_field_back(before, A_NORMAL);
287 set_field_back(before, A_REVERSE);
288 } else if (ch <= MAX_FORM_COMMAND) {
289 inactive_field(before);
293 for (n = 0; n < SIZEOF(commands); ++n) {
294 if (commands[n].code == ch) {
295 *result = commands[n].result;
300 status = form_driver(form, *result);
302 if (status == E_OK) {
303 bool modified = TRUE;
305 length = buffer_length(before);
306 if (length < before_off)
313 if ((int) (before_row + 1) == (int) (before->rows))
320 if (length > before_off)
325 if (before_col > 0) {
327 } else if (before_row > 0) {
328 length -= before->cols + before_col;
333 length += before->cols;
336 /* FIXME: finish these */
337 case REQ_DEL_LINE: /* delete line */
338 case REQ_DEL_WORD: /* delete word at cursor */
339 case REQ_INS_CHAR: /* insert blank char at cursor */
340 case REQ_INS_LINE: /* insert blank line at cursor */
341 case REQ_INS_MODE: /* begin insert mode */
342 case REQ_OVL_MODE: /* begin overlay mode */
344 /* ignore all of the motion commands */
345 case REQ_SCR_BCHAR: /* FALLTHRU */
346 case REQ_SCR_BHPAGE: /* FALLTHRU */
347 case REQ_SCR_BLINE: /* FALLTHRU */
348 case REQ_SCR_BPAGE: /* FALLTHRU */
349 case REQ_SCR_FCHAR: /* FALLTHRU */
350 case REQ_SCR_FHPAGE: /* FALLTHRU */
351 case REQ_SCR_FLINE: /* FALLTHRU */
352 case REQ_SCR_FPAGE: /* FALLTHRU */
353 case REQ_SCR_HBHALF: /* FALLTHRU */
354 case REQ_SCR_HBLINE: /* FALLTHRU */
355 case REQ_SCR_HFHALF: /* FALLTHRU */
356 case REQ_SCR_HFLINE: /* FALLTHRU */
357 case REQ_BEG_FIELD: /* FALLTHRU */
358 case REQ_BEG_LINE: /* FALLTHRU */
359 case REQ_DOWN_CHAR: /* FALLTHRU */
360 case REQ_DOWN_FIELD: /* FALLTHRU */
361 case REQ_END_FIELD: /* FALLTHRU */
362 case REQ_END_LINE: /* FALLTHRU */
363 case REQ_FIRST_FIELD: /* FALLTHRU */
364 case REQ_FIRST_PAGE: /* FALLTHRU */
365 case REQ_LAST_FIELD: /* FALLTHRU */
366 case REQ_LAST_PAGE: /* FALLTHRU */
367 case REQ_LEFT_CHAR: /* FALLTHRU */
368 case REQ_LEFT_FIELD: /* FALLTHRU */
369 case REQ_NEXT_CHAR: /* FALLTHRU */
370 case REQ_NEXT_CHOICE: /* FALLTHRU */
371 case REQ_NEXT_FIELD: /* FALLTHRU */
372 case REQ_NEXT_LINE: /* FALLTHRU */
373 case REQ_NEXT_PAGE: /* FALLTHRU */
374 case REQ_NEXT_WORD: /* FALLTHRU */
375 case REQ_PREV_CHAR: /* FALLTHRU */
376 case REQ_PREV_CHOICE: /* FALLTHRU */
377 case REQ_PREV_FIELD: /* FALLTHRU */
378 case REQ_PREV_LINE: /* FALLTHRU */
379 case REQ_PREV_PAGE: /* FALLTHRU */
380 case REQ_PREV_WORD: /* FALLTHRU */
381 case REQ_RIGHT_CHAR: /* FALLTHRU */
382 case REQ_RIGHT_FIELD: /* FALLTHRU */
383 case REQ_SFIRST_FIELD: /* FALLTHRU */
384 case REQ_SLAST_FIELD: /* FALLTHRU */
385 case REQ_SNEXT_FIELD: /* FALLTHRU */
386 case REQ_SPREV_FIELD: /* FALLTHRU */
387 case REQ_UP_CHAR: /* FALLTHRU */
388 case REQ_UP_FIELD: /* FALLTHRU */
389 case REQ_VALIDATION: /* FALLTHRU */
395 if (ch >= MIN_FORM_COMMAND) {
397 } else if (isprint(ch)) {
404 * If we do not force a re-validation, then field_buffer 0 will
405 * be lagging by one character.
407 if (modified && form_driver(form, REQ_VALIDATION) == E_OK && *result
411 set_buffer_length(before, length);
414 if (current_field(form) != before)
415 inactive_field(before);
420 free_edit_field(FIELD *f)
422 FieldAttrs *ptr = field_attrs(f);
424 free(ptr->row_lengths);
430 extern void no_edit_field(void);