1 /* String input
2 
3  * Copyright (C) 1998 J.A. Bezemer
4  *
5  * Licensed under the terms of the GNU General Public License.
6  * ABSOLUTELY NO WARRANTY.
7  * See the file `COPYING' in this directory.
8  */
9 
10 #include "stringinput.h"
11 #include <string.h>
12 #ifndef OLD_CURSES
13 #include <ncurses.h>
14 #else
15 #include <curses.h>
16 #endif
17 
18 
19 void
stringinput_display(stringinput_t * data)20 stringinput_display (stringinput_t * data)
21 {
22   int i;
23 
24   if (data->cursorpos < 0)
25     data->cursorpos = 0;
26   if (data->cursorpos > strlen (data->string))
27     data->cursorpos = strlen (data->string);
28 
29   if (data->cursorpos < data->firstcharonscreen + 2)
30     data->firstcharonscreen = data->cursorpos - 2;
31   if (data->cursorpos >= data->firstcharonscreen + data->w)
32     data->firstcharonscreen = data->cursorpos - data->w + 1;
33 
34   if (data->firstcharonscreen < 0)
35     data->firstcharonscreen = 0;
36 
37   move (data->y, data->x);
38   i = data->firstcharonscreen;
39   while (i < strlen (data->string) && i < data->firstcharonscreen + data->w)
40     {
41       addch (data->string[i]);
42       i++;
43     }
44 
45   while (i < data->firstcharonscreen + data->w)
46     {
47       addch (' ');
48       i++;
49     }
50 
51   move (data->y, data->x + data->cursorpos - data->firstcharonscreen);
52 }
53 
54 void
stringinput_stdkeys(int key,stringinput_t * data)55 stringinput_stdkeys (int key, stringinput_t * data)
56 {
57   char helpstring[500];
58 
59   switch (key)
60     {
61     case KEY_LEFT:
62       data->cursorpos--;
63       break;
64 
65     case KEY_RIGHT:
66       data->cursorpos++;
67       break;
68 
69     case KEY_HOME:
70       data->cursorpos = 0;
71       break;
72 
73     case KEY_END:
74       data->cursorpos = strlen (data->string);
75       break;
76 
77     case KEY_BACKSPACE:
78 #ifdef TREAT_DEL_AS_BACKSPACE
79     case 127:
80 #endif
81       if (data->cursorpos > 0)
82 	{
83 	  strcpy (helpstring, data->string);
84 	  strcpy (helpstring + data->cursorpos - 1,
85 		  data->string + data->cursorpos);
86 	  strcpy (data->string, helpstring);
87 	  data->cursorpos--;
88 	}
89       break;
90 
91 #ifndef TREAT_DEL_AS_BACKSPACE
92     case 127:			/* DEL */
93       if (data->cursorpos < strlen (data->string))
94 	{
95 	  strcpy (helpstring, data->string);
96 	  strcpy (helpstring + data->cursorpos,
97 		  data->string + data->cursorpos + 1);
98 	  strcpy (data->string, helpstring);
99 	}
100       break;
101 #endif
102     }
103 
104   if (key >= 32 && key <= 126)	/* insert */
105     {
106       strcpy (helpstring, data->string);
107       strcpy (helpstring + data->cursorpos + 1,
108 	      data->string + data->cursorpos);
109       helpstring[data->cursorpos] = (char) key;
110       strcpy (data->string, helpstring);
111       data->cursorpos++;
112     }
113 
114   if (data->cursorpos < 0)
115     data->cursorpos = 0;
116   if (data->cursorpos > strlen (data->string))
117     data->cursorpos = strlen (data->string);
118 }
119