@@ -45,7 +45,8 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
4545 const char * init )
4646{
4747 int i , x , y , box_y , box_x , box_width ;
48- int input_x = 0 , scroll = 0 , key = 0 , button = -1 ;
48+ int input_x = 0 , key = 0 , button = -1 ;
49+ int show_x , len , pos ;
4950 char * instr = dialog_input_result ;
5051 WINDOW * dialog ;
5152
@@ -97,14 +98,17 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
9798 wmove (dialog , box_y , box_x );
9899 wattrset (dialog , dlg .inputbox .atr );
99100
100- input_x = strlen (instr );
101+ len = strlen (instr );
102+ pos = len ;
101103
102- if (input_x >= box_width ) {
103- scroll = input_x - box_width + 1 ;
104+ if (len >= box_width ) {
105+ show_x = len - box_width + 1 ;
104106 input_x = box_width - 1 ;
105107 for (i = 0 ; i < box_width - 1 ; i ++ )
106- waddch (dialog , instr [scroll + i ]);
108+ waddch (dialog , instr [show_x + i ]);
107109 } else {
110+ show_x = 0 ;
111+ input_x = len ;
108112 waddstr (dialog , instr );
109113 }
110114
@@ -121,45 +125,104 @@ int dialog_inputbox(const char *title, const char *prompt, int height, int width
121125 case KEY_UP :
122126 case KEY_DOWN :
123127 break ;
124- case KEY_LEFT :
125- continue ;
126- case KEY_RIGHT :
127- continue ;
128128 case KEY_BACKSPACE :
129129 case 127 :
130- if (input_x || scroll ) {
130+ if (pos ) {
131131 wattrset (dialog , dlg .inputbox .atr );
132- if (!input_x ) {
133- scroll = scroll < box_width - 1 ? 0 : scroll - (box_width - 1 );
134- wmove (dialog , box_y , box_x );
135- for (i = 0 ; i < box_width ; i ++ )
136- waddch (dialog ,
137- instr [scroll + input_x + i ] ?
138- instr [scroll + input_x + i ] : ' ' );
139- input_x = strlen (instr ) - scroll ;
132+ if (input_x == 0 ) {
133+ show_x -- ;
140134 } else
141135 input_x -- ;
142- instr [scroll + input_x ] = '\0' ;
143- mvwaddch (dialog , box_y , input_x + box_x , ' ' );
136+
137+ if (pos < len ) {
138+ for (i = pos - 1 ; i < len ; i ++ ) {
139+ instr [i ] = instr [i + 1 ];
140+ }
141+ }
142+
143+ pos -- ;
144+ len -- ;
145+ instr [len ] = '\0' ;
146+ wmove (dialog , box_y , box_x );
147+ for (i = 0 ; i < box_width ; i ++ ) {
148+ if (!instr [show_x + i ]) {
149+ waddch (dialog , ' ' );
150+ break ;
151+ }
152+ waddch (dialog , instr [show_x + i ]);
153+ }
144154 wmove (dialog , box_y , input_x + box_x );
145155 wrefresh (dialog );
146156 }
147157 continue ;
158+ case KEY_LEFT :
159+ if (pos > 0 ) {
160+ if (input_x > 0 ) {
161+ wmove (dialog , box_y , -- input_x + box_x );
162+ } else if (input_x == 0 ) {
163+ show_x -- ;
164+ wmove (dialog , box_y , box_x );
165+ for (i = 0 ; i < box_width ; i ++ ) {
166+ if (!instr [show_x + i ]) {
167+ waddch (dialog , ' ' );
168+ break ;
169+ }
170+ waddch (dialog , instr [show_x + i ]);
171+ }
172+ wmove (dialog , box_y , box_x );
173+ }
174+ pos -- ;
175+ }
176+ continue ;
177+ case KEY_RIGHT :
178+ if (pos < len ) {
179+ if (input_x < box_width - 1 ) {
180+ wmove (dialog , box_y , ++ input_x + box_x );
181+ } else if (input_x == box_width - 1 ) {
182+ show_x ++ ;
183+ wmove (dialog , box_y , box_x );
184+ for (i = 0 ; i < box_width ; i ++ ) {
185+ if (!instr [show_x + i ]) {
186+ waddch (dialog , ' ' );
187+ break ;
188+ }
189+ waddch (dialog , instr [show_x + i ]);
190+ }
191+ wmove (dialog , box_y , input_x + box_x );
192+ }
193+ pos ++ ;
194+ }
195+ continue ;
148196 default :
149197 if (key < 0x100 && isprint (key )) {
150- if (scroll + input_x < MAX_LEN ) {
198+ if (len < MAX_LEN ) {
151199 wattrset (dialog , dlg .inputbox .atr );
152- instr [scroll + input_x ] = key ;
153- instr [scroll + input_x + 1 ] = '\0' ;
200+ if (pos < len ) {
201+ for (i = len ; i > pos ; i -- )
202+ instr [i ] = instr [i - 1 ];
203+ instr [pos ] = key ;
204+ } else {
205+ instr [len ] = key ;
206+ }
207+ pos ++ ;
208+ len ++ ;
209+ instr [len ] = '\0' ;
210+
154211 if (input_x == box_width - 1 ) {
155- scroll ++ ;
156- wmove (dialog , box_y , box_x );
157- for (i = 0 ; i < box_width - 1 ; i ++ )
158- waddch (dialog , instr [scroll + i ]);
212+ show_x ++ ;
159213 } else {
160- wmove (dialog , box_y , input_x ++ + box_x );
161- waddch (dialog , key );
214+ input_x ++ ;
215+ }
216+
217+ wmove (dialog , box_y , box_x );
218+ for (i = 0 ; i < box_width ; i ++ ) {
219+ if (!instr [show_x + i ]) {
220+ waddch (dialog , ' ' );
221+ break ;
222+ }
223+ waddch (dialog , instr [show_x + i ]);
162224 }
225+ wmove (dialog , box_y , input_x + box_x );
163226 wrefresh (dialog );
164227 } else
165228 flash (); /* Alarm user about overflow */
0 commit comments