1 /* curses.c - helper functions related to curses
2 * Copyright © 2013 Géraud Meyer <graud@gmx.com>
4 * This file is part of ng-jackspa.
6 * ng-jackspa is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License version 2 as published by the
8 * Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along
16 * with ng-jackspa. If not, see <http://www.gnu.org/licenses/>.
19 #define CTRL(key) ((key)-64)
21 /* Read user text input from a curses window with line editing keys.
22 * The result is obtained from the window itself, not from the keyboard; a
23 * space marks the end of the input; if the text contains a ^O character, the
24 * input is cancelled (as soon as the ^O is processed).
25 * The terminal must not echo, and it should be in cbreak mode (rather than in
26 * line editing mode, aka cooked mode); a pseudo line editing terminal mode is
27 * emulated, with additional emacs-like edit/move keys.
29 int wtextentry(WINDOW
*win
, char *result
, size_t size
)
31 short s
; /* state varibale */
32 int k
; /* keyboard input */
33 char c
; /* input char */
39 switch(k
= wgetch(win
)) {
41 case CTRL('O'): /* cancel */
51 case KEY_BACKSPACE
: /* erase */
53 getyx(win
, row
, col
), wmove(win
, row
, col
-1), wdelch(win
);
55 case KEY_DC
: /* delete */
59 case CTRL('U'): /* kill */
60 wmove(win
, 0, 0), wclrtoeol(win
);
65 case KEY_UP
: /* default/previous input field */
67 wmove(win
, 0, 0), wclrtoeol(win
);
68 wmove(win
, 0, 0), waddnstr(win
, result
, size
);
72 getyx(win
, row
, col
), wmove(win
, row
, col
-1);
77 if (col
+1 < size
) wmove(win
, row
, col
+1);
86 for (; col
< size
-1 && (c
= mvwinch(win
, row
, col
)&A_CHARTEXT
) != ' '; col
++);
97 return (result
[0] = '\0', 1);
99 for (col
= 0; col
< size
-1 && (c
= mvwinch(win
, 0, col
)&A_CHARTEXT
) != ' '; col
++)