1 /****************************************************************************
2 * Copyright (c) 2003-2008,2011 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.17 2011/01/15 18:15:11 tom Exp $
31 * A wrapper for form_driver() which keeps track of the user's editing changes
32 * for each field, and makes the result available as a null-terminated string
33 * in field_buffer(field,1).
35 * Thomas Dickey - 2003/4/26.
38 #include <test.priv.h>
42 #include <edit_field.h>
51 CTRL('A'), REQ_NEXT_CHOICE
, ""
54 CTRL('B'), REQ_PREV_WORD
, "go to previous word"
57 CTRL('C'), REQ_CLR_EOL
, "clear to end of line"
60 CTRL('D'), REQ_DOWN_FIELD
, "move downward to field"
63 CTRL('E'), REQ_END_FIELD
, "go to end of field"
66 CTRL('F'), REQ_NEXT_PAGE
, "go to next page"
69 CTRL('G'), REQ_DEL_WORD
, "delete current word"
72 CTRL('H'), REQ_DEL_PREV
, "delete previous character"
75 CTRL('I'), REQ_INS_CHAR
, "insert character"
78 CTRL('K'), REQ_CLR_EOF
, "clear to end of field"
81 CTRL('L'), REQ_LEFT_FIELD
, "go to field to left"
84 CTRL('M'), REQ_NEW_LINE
, "insert/overlay new line"
87 CTRL('N'), REQ_NEXT_FIELD
, "go to next field"
90 CTRL('O'), REQ_INS_LINE
, "insert blank line at cursor"
93 CTRL('P'), REQ_PREV_FIELD
, "go to previous field"
96 CTRL('Q'), MY_QUIT
, "exit form"
99 CTRL('R'), REQ_RIGHT_FIELD
, "go to field to right"
102 CTRL('S'), REQ_BEG_FIELD
, "go to beginning of field"
105 CTRL('T'), MY_EDT_MODE
, "toggle O_EDIT mode, clear field status",
108 CTRL('U'), REQ_UP_FIELD
, "move upward to field"
111 CTRL('V'), REQ_DEL_CHAR
, "delete character"
114 CTRL('W'), REQ_NEXT_WORD
, "go to next word"
117 CTRL('X'), REQ_CLR_FIELD
, "clear field"
120 CTRL('Y'), REQ_DEL_LINE
, "delete line"
123 CTRL('Z'), REQ_PREV_CHOICE
, ""
126 CTRL('['), MY_QUIT
, "exit form"
129 CTRL(']'), MY_INS_MODE
, "toggle REQ_INS_MODE/REQ_OVL_MODE",
132 KEY_F(1), MY_HELP
, "show this screen",
135 KEY_BACKSPACE
, REQ_DEL_PREV
, "delete previous character"
138 KEY_DOWN
, REQ_DOWN_CHAR
, "move down 1 character"
141 KEY_END
, REQ_LAST_FIELD
, "go to last field"
144 KEY_HOME
, REQ_FIRST_FIELD
, "go to first field"
147 KEY_LEFT
, REQ_LEFT_CHAR
, "move left 1 character"
150 KEY_LL
, REQ_LAST_FIELD
, "go to last field"
153 KEY_NEXT
, REQ_NEXT_FIELD
, "go to next field"
156 KEY_NPAGE
, REQ_NEXT_PAGE
, "go to next page"
159 KEY_PPAGE
, REQ_PREV_PAGE
, "go to previous page"
162 KEY_PREVIOUS
, REQ_PREV_FIELD
, "go to previous field"
165 KEY_RIGHT
, REQ_RIGHT_CHAR
, "move right 1 character"
168 KEY_UP
, REQ_UP_CHAR
, "move up 1 character"
172 static WINDOW
*old_window
;
178 old_window
= dupwin(curscr
);
184 touchwin(old_window
);
185 wnoutrefresh(old_window
);
191 * Display a temporary window listing the keystroke-commands we recognize.
194 help_edit_field(void)
200 int wide
= COLS
- ((x0
+ 1) * 2);
201 int high
= LINES
- ((y0
+ 1) * 2);
202 WINDOW
*help
= newwin(high
, wide
, y0
, x0
);
203 WINDOW
*data
= newpad(2 + SIZEOF(commands
), wide
- 4);
211 waddstr(data
, "Defined form edit/traversal keys:\n");
212 for (n
= 0; n
< SIZEOF(commands
); ++n
) {
214 #ifdef NCURSES_VERSION
215 if ((name
= form_request_name(commands
[n
].result
)) == 0)
217 name
= commands
[n
].help
;
218 wprintw(data
, "%s -- %s\n",
219 keyname(commands
[n
].code
),
220 name
!= 0 ? name
: commands
[n
].help
);
222 waddstr(data
, "Arrow keys move within a field as you would expect.");
276 pnoutrefresh(data
, y1
, 0, y0
+ 1, x0
+ 1, high
, wide
);
278 } while ((ch
= wgetch(data
)) != ERR
&& ch
!= QUIT
&& ch
!= ESCAPE
);
288 offset_in_field(FORM
* form
)
290 FIELD
*field
= current_field(form
);
293 form_getyx(form
, currow
, curcol
);
294 return curcol
+ currow
* field
->dcols
;
298 inactive_field(FIELD
* f
)
300 FieldAttrs
*ptr
= (FieldAttrs
*) field_userptr(f
);
301 set_field_back(f
, ptr
->background
);
305 edit_field(FORM
* form
, int *result
)
307 int ch
= wgetch(form_win(form
));
316 int before_off
= offset_in_field(form
);
318 form_getyx(form
, before_row
, before_col
);
319 before
= current_field(form
);
320 set_field_back(before
, A_NORMAL
);
322 set_field_back(before
, A_REVERSE
);
323 } else if (ch
<= MAX_FORM_COMMAND
) {
324 inactive_field(before
);
328 for (n
= 0; n
< SIZEOF(commands
); ++n
) {
329 if (commands
[n
].code
== ch
) {
330 *result
= commands
[n
].result
;
335 status
= form_driver(form
, *result
);
337 if (status
== E_OK
) {
338 bool modified
= TRUE
;
341 if ((buffer
= field_buffer(before
, 1)) != 0)
342 length
= atoi(buffer
);
343 if (length
< before_off
)
350 if (before_row
+ 1 == before
->rows
)
357 if (length
> before_off
)
362 if (before_col
> 0) {
364 } else if (before_row
> 0) {
365 length
-= before
->cols
+ before_col
;
370 length
+= before
->cols
;
373 /* FIXME: finish these */
374 case REQ_DEL_LINE
: /* delete line */
375 case REQ_DEL_WORD
: /* delete word at cursor */
376 case REQ_INS_CHAR
: /* insert blank char at cursor */
377 case REQ_INS_LINE
: /* insert blank line at cursor */
378 case REQ_INS_MODE
: /* begin insert mode */
379 case REQ_OVL_MODE
: /* begin overlay mode */
381 /* ignore all of the motion commands */
382 case REQ_SCR_BCHAR
: /* FALLTHRU */
383 case REQ_SCR_BHPAGE
: /* FALLTHRU */
384 case REQ_SCR_BLINE
: /* FALLTHRU */
385 case REQ_SCR_BPAGE
: /* FALLTHRU */
386 case REQ_SCR_FCHAR
: /* FALLTHRU */
387 case REQ_SCR_FHPAGE
: /* FALLTHRU */
388 case REQ_SCR_FLINE
: /* FALLTHRU */
389 case REQ_SCR_FPAGE
: /* FALLTHRU */
390 case REQ_SCR_HBHALF
: /* FALLTHRU */
391 case REQ_SCR_HBLINE
: /* FALLTHRU */
392 case REQ_SCR_HFHALF
: /* FALLTHRU */
393 case REQ_SCR_HFLINE
: /* FALLTHRU */
394 case REQ_BEG_FIELD
: /* FALLTHRU */
395 case REQ_BEG_LINE
: /* FALLTHRU */
396 case REQ_DOWN_CHAR
: /* FALLTHRU */
397 case REQ_DOWN_FIELD
: /* FALLTHRU */
398 case REQ_END_FIELD
: /* FALLTHRU */
399 case REQ_END_LINE
: /* FALLTHRU */
400 case REQ_FIRST_FIELD
: /* FALLTHRU */
401 case REQ_FIRST_PAGE
: /* FALLTHRU */
402 case REQ_LAST_FIELD
: /* FALLTHRU */
403 case REQ_LAST_PAGE
: /* FALLTHRU */
404 case REQ_LEFT_CHAR
: /* FALLTHRU */
405 case REQ_LEFT_FIELD
: /* FALLTHRU */
406 case REQ_NEXT_CHAR
: /* FALLTHRU */
407 case REQ_NEXT_CHOICE
: /* FALLTHRU */
408 case REQ_NEXT_FIELD
: /* FALLTHRU */
409 case REQ_NEXT_LINE
: /* FALLTHRU */
410 case REQ_NEXT_PAGE
: /* FALLTHRU */
411 case REQ_NEXT_WORD
: /* FALLTHRU */
412 case REQ_PREV_CHAR
: /* FALLTHRU */
413 case REQ_PREV_CHOICE
: /* FALLTHRU */
414 case REQ_PREV_FIELD
: /* FALLTHRU */
415 case REQ_PREV_LINE
: /* FALLTHRU */
416 case REQ_PREV_PAGE
: /* FALLTHRU */
417 case REQ_PREV_WORD
: /* FALLTHRU */
418 case REQ_RIGHT_CHAR
: /* FALLTHRU */
419 case REQ_RIGHT_FIELD
: /* FALLTHRU */
420 case REQ_SFIRST_FIELD
: /* FALLTHRU */
421 case REQ_SLAST_FIELD
: /* FALLTHRU */
422 case REQ_SNEXT_FIELD
: /* FALLTHRU */
423 case REQ_SPREV_FIELD
: /* FALLTHRU */
424 case REQ_UP_CHAR
: /* FALLTHRU */
425 case REQ_UP_FIELD
: /* FALLTHRU */
426 case REQ_VALIDATION
: /* FALLTHRU */
432 if (ch
>= MIN_FORM_COMMAND
) {
434 } else if (isprint(ch
)) {
441 * If we do not force a re-validation, then field_buffer 0 will
442 * be lagging by one character.
444 if (modified
&& form_driver(form
, REQ_VALIDATION
) == E_OK
&& *result
448 sprintf(lengths
, "%d", length
);
449 set_field_buffer(before
, 1, lengths
);
452 if (current_field(form
) != before
)
453 inactive_field(before
);
458 extern void no_edit_field(void);