summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libdialog/lineedit.c
blob: 7bfe0e0bba3d9e3c383a4bc11cd4ce01eb36cfdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
 *  Changes Copyright (C) 1995 by Andrey A. Chernov, Moscow
 *
 *  Original Copyright:
 *
 *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <dialog.h>
#include "dialog.priv.h"

static void redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask);

/*
 * Line editor
 */
int line_edit(WINDOW* dialog, int box_y, int box_x, int flen, int box_width, chtype attr, int first, unsigned char *result, int attr_mask)
{
  int i, key;
  chtype old_attr;
  static int input_x, scroll;
  static unsigned char instr[MAX_LEN+1];
  unsigned char erase_char = erasechar();
  unsigned char kill_char = killchar();
#ifdef notyet
  unsignec char werase_char = cur_term->Ottyb.c_cc[VWERASE];
#endif

  old_attr = getattrs(dialog);
  keypad(dialog, TRUE);

  if (first) {
    memset(instr, 0, sizeof(instr));
    strcpy(instr, result);
    i = strlen(instr);
/*    input_x = i % box_width;*/
    input_x = (i > box_width) ? box_width - 1 : i;
/*    scroll = i - input_x;*/
    scroll = (i > box_width) ? i - box_width + 1: 0;
  }
  redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);

  for (;;) {
    wattrset(dialog, attr);
    wrefresh(dialog);
    key = wgetch(dialog);
    switch (key) {
      case ctrl('q'):
	goto ret;
	break;
      case KEY_F(1):
	display_helpfile();
	break;
      case TAB:
      case KEY_BTAB:
      case KEY_UP:
      case KEY_DOWN:
      case ESC:
      case '\r':
      case '\n':
	for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
	  instr[i] = '\0';
	if (key == '\r')
	  key = '\n';
	goto ret;
      case '\025':
      case '\030':
      kill_it:
	input_x = scroll = 0;
	/* fall through */
      case '\013':
      case KEY_EOL:
	memset(instr + scroll + input_x, '\0', sizeof(instr) - scroll - input_x);
	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	continue;
      case '\001':
      case KEY_HOME:
	input_x = scroll = 0;
	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	continue;
      case '\005':
      case KEY_END:
	for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
	  instr[i] = '\0';
	i++;
	input_x = i % box_width;
	scroll = i - input_x;
	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	continue;
      case '\002':
      case KEY_LEFT:
	if (input_x || scroll) {
	  if (!input_x) {
	    int oldscroll = scroll;
	    scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
	    input_x = oldscroll - 1 - scroll;
	    redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	  } else {
	    input_x--;
	    wmove(dialog, box_y, input_x + box_x);
	  }
	} else
	  beep();
	continue;
      case '\006':
      case KEY_RIGHT:
	  if (   scroll+input_x < MAX_LEN
	      && (flen < 0 || scroll+input_x < flen)
	     ) {
	    if (!instr[scroll+input_x])
	      instr[scroll+input_x] = ' ';
	    if (input_x == box_width-1) {
	      scroll++;
	      redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	    }
	    else {
	      wmove(dialog, box_y, input_x + box_x);
	      waddch(dialog, instr[scroll+input_x]);
	      input_x++;
	    }
	  } else
	    beep(); /* Alarm user about overflow */
	continue;
      case '\b':
      case '\177':
      case KEY_BACKSPACE:
      erase_it:
	if (input_x || scroll) {
	  i = strlen(instr);
	  memmove(instr+scroll+input_x-1, instr+scroll+input_x, i-(scroll+input_x)+1);
	  if (!input_x) {
	    int oldscroll = scroll;
	    scroll = scroll < box_width-1 ? 0 : scroll-(box_width-1);
	    input_x = oldscroll - 1 - scroll;
	  } else
	    input_x--;
	  redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	} else
	  beep();
	continue;
      case '\004':
      case KEY_DC:
	for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
	  instr[i] = '\0';
	i++;
	if (i == 0) {
	  beep();
	  continue;
	}
	memmove(instr+scroll+input_x, instr+scroll+input_x+1, i-(scroll+input_x));
	redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	continue;
      default:
	if (CCEQ(key, erase_char))
	  goto erase_it;
	if (CCEQ(key, kill_char))
	  goto kill_it;
	if (key < 0x100 && isprint(key)) {
	  for (i = strlen(instr) - 1; i >= scroll + input_x && instr[i] == ' '; i--)
	    instr[i] = '\0';
	  i++;
	  if (i < MAX_LEN && (flen < 0 || scroll+input_x < flen)) {
	    if (flen < 0 || i < flen)
	      memmove(instr+scroll+input_x+1, instr+scroll+input_x, i-(scroll+input_x));
	    instr[scroll+input_x] = key;
	    if (input_x == box_width-1 && (flen < 0 || i < flen))
	      scroll++;
	    else
	      input_x++;
	    redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, FALSE, attr_mask);
	  } else
	    beep(); /* Alarm user about overflow */
	  continue;
	}
      }
    }
ret:
    redraw_field(dialog, box_y, box_x, flen, box_width, instr, input_x, scroll, attr, old_attr, TRUE, attr_mask);
    wrefresh(dialog);
    strcpy(result, instr);
    return key;
}

static void
redraw_field(WINDOW *dialog, int box_y, int box_x, int flen, int box_width, unsigned char instr[], int input_x, int scroll, chtype attr, chtype old_attr, int fexit, int attr_mask)
{
  int i, fix_len;

  wattrset(dialog, fexit ? old_attr : attr);
  wmove(dialog, box_y, box_x);
  fix_len = flen >= 0 ? MIN(flen-scroll,box_width) : box_width;
  for (i = 0; i < fix_len; i++)
      waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
  wattrset(dialog, old_attr);
  for ( ; i < box_width; i++)
      waddch(dialog, instr[scroll+i] ? ((attr_mask & DITEM_NO_ECHO) ? '*' : instr[scroll+i]) : ' ');
  wmove(dialog, box_y, input_x + box_x);
}
OpenPOWER on IntegriCloud