1 /* Signal Processing - Get infile-name
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 "signpr_infilenm.h"
11 #include "scrollmenu.h"
12 #include "stringinput.h"
13 #include "buttons.h"
14 #include "boxes.h"
15 #include "dirfilemenu.h"
16 #include "errorwindow.h"
17 #include "textwindow.h"
18 #include "checkfile.h"
19 #include "yesnowindow.h"
20 #include "helpline.h"
21 #include "clrscr.h"
22 
23 #include <stdlib.h>
24 #include <sys/stat.h>
25 #include <string.h>
26 #ifndef OLD_CURSES
27 #include <ncurses.h>
28 #else
29 #include <curses.h>
30 #endif
31 
32 
33 int
signproc_select_infile(char * startdir,char * selectedfile)34 signproc_select_infile (char *startdir, char *selectedfile)
35 /* Returns 0: canceled, 2: NextScreen */
36 {
37   scrollmenu_t dirfilelist;
38   stringinput_t string;
39   button_t next_button, cancel_button;
40   int dont_stop = TRUE;
41   int returnval = 0;
42   int focus = 1;
43   int in_ch;
44   int i;
45   char helpstring[500];
46   char *charpointer;
47   struct stat filestats;
48   int oldselected;
49 
50   char *helplines[4] =
51   {
52     " Select name of sound file to be processed.                     TAB: Next field",
53     " Enter name of sound file to be processed.                      TAB: Next field",
54     " Back to main menu.                                             TAB: Next field",
55     " To Signal Processing - Filter Selection.                       TAB: Next field"};
56 
57   dirfilelist.y = 3;
58   dirfilelist.x = 5;
59   dirfilelist.h = 12;
60   dirfilelist.w = 32;
61   dirfilelist.firstonscreen = 0;
62   dirfilemenu (startdir, &dirfilelist);
63   dirfilelist.selected = dirfilelist.last_of_1st_part + 1;
64 
65   string.maxlen = 500;
66   string.string = (char *) malloc (string.maxlen * sizeof (char));
67   if (selectedfile[0] == '\0')
68     strcpy (string.string, startdir);
69   else
70     strcpy (string.string, selectedfile);
71   string.y = 17;
72   string.x = 5;
73   string.w = 70;
74   string.cursorpos = strlen (string.string);
75   string.firstcharonscreen = strlen (string.string) - string.w + 2;
76   if (string.firstcharonscreen < 0)
77     string.firstcharonscreen = 0;
78 
79   next_button.text = " Next screen > ";
80   next_button.y = 20;
81   next_button.x = 60;
82   next_button.selected = FALSE;
83 
84   cancel_button.text = " Cancel ";
85   cancel_button.y = 20;
86   cancel_button.x = 36;
87   cancel_button.selected = FALSE;
88 
89   clearscreen (SIGNPR_INFILE_HEADERTEXT);
90 
91   do
92     {
93       if (focus == 2)
94 	cancel_button.selected = TRUE;
95       else
96 	cancel_button.selected = FALSE;
97 
98       if (focus == 3)
99 	next_button.selected = TRUE;
100       else
101 	next_button.selected = FALSE;
102 
103       dirfilelist.hasfocus = (focus == 0);
104 
105       scrollmenu_display (&dirfilelist);
106       mybox (dirfilelist.y - 1, dirfilelist.x - 1,
107 	     dirfilelist.h + 2, dirfilelist.w + 2);
108       mvprintw (dirfilelist.y - 1, dirfilelist.x + 1,
109 		"Files and directories:");
110 
111       stringinput_display (&string);
112       mybox (string.y - 1, string.x - 1, 3, string.w + 2);
113       mvprintw (string.y - 1, string.x + 1, "Source file name:");
114 
115       button_display (&cancel_button);
116       mybox (cancel_button.y - 1, cancel_button.x - 1,
117 	     3, strlen (cancel_button.text) + 2);
118       button_display (&next_button);
119       mybox (next_button.y - 1, next_button.x - 1,
120 	     3, strlen (next_button.text) + 2);
121 
122       helpline (helplines[focus]);
123 
124       if (focus == 1)
125 	stringinput_display (&string);
126       else
127 	move (0, 79);
128 
129       refresh ();
130 
131       in_ch = getch ();
132 
133       switch (focus)
134 	{
135 	case 0:		/* dirfilelist */
136 	  if (scrollmenu_stdkeys (in_ch, &dirfilelist) >= 0)
137 	    {
138 	      oldselected = dirfilelist.selected;
139 	      i = dirfilemenu_process_select (&dirfilelist,
140 					      helpstring);
141 	      if (i == 0)	/* filename in helpstring */
142 		{
143 		  strcpy (string.string, helpstring);
144 		  focus = 1;
145 		  string.cursorpos = strlen (string.string);
146 		  string.firstcharonscreen = 0;
147 		}
148 	      else
149 		/* dir in helpstring */
150 		{
151 		  scrollmenu_delete_menu (&dirfilelist);
152 		  dirfilemenu (helpstring, &dirfilelist);
153 		  if (dirfilelist.number == 0)
154 		    {
155 		      error_window (
156 				   "No permission to read this directory.");
157 		      header (SIGNPR_INFILE_HEADERTEXT);
158 		      scrollmenu_delete_menu (&dirfilelist);
159 		      dirfilemenu (startdir, &dirfilelist);
160 		      dirfilelist.selected = oldselected;
161 		    }
162 		  else
163 		    {
164 		      strcpy (startdir, helpstring);
165 		      dirfilelist.firstonscreen = 0;
166 
167 		      charpointer = strrchr (string.string, '/');
168 		      if (charpointer != NULL)
169 			strcat (helpstring, charpointer + 1);
170 		      else
171 			strcat (helpstring, string.string);
172 		      strcpy (string.string, helpstring);
173 		    }
174 		}
175 	    }
176 	  else
177 	    switch (in_ch)
178 	      {
179 	      case KEY_LEFT:
180 		focus--;
181 		break;
182 	      case KEY_RIGHT:
183 		focus++;
184 		break;
185 	      }
186 	  break;
187 
188 	case 1:		/* string */
189 	  stringinput_stdkeys (in_ch, &string);
190 	  if (in_ch == KEY_ENTER || in_ch == 13)
191 	    {
192 	      strcpy (helpstring, string.string);
193 
194 	      /* cut away last '/'-s */
195 	      while (strlen (helpstring) > 0 &&
196 		     helpstring[strlen (helpstring) - 1] == '/')
197 		helpstring[strlen (helpstring) - 1] = '\0';
198 
199 	      strcat (helpstring, "/");
200 
201 	      if (!stat (helpstring, &filestats) &&
202 		  S_ISDIR (filestats.st_mode))
203 		{
204 		  strcpy (startdir, helpstring);
205 		  scrollmenu_delete_menu (&dirfilelist);
206 		  dirfilemenu (startdir, &dirfilelist);
207 		  dirfilelist.firstonscreen = 0;
208 		  dirfilelist.selected =
209 		    dirfilelist.last_of_1st_part + 1;
210 		  strcpy (string.string, startdir);
211 		  string.cursorpos = strlen (string.string);
212 		  focus = 0;
213 		}
214 	      else		/* it's a file */
215 		focus = 3;
216 	    }
217 	  else
218 	    switch (in_ch)
219 	      {
220 	      case KEY_UP:
221 		focus--;
222 		break;
223 	      case KEY_DOWN:
224 		focus++;
225 		break;
226 	      }
227 	  break;
228 
229 	case 2:		/* Cancel */
230 	  if (in_ch == KEY_ENTER || in_ch == 13)
231 	    {
232 	      returnval = 0;
233 	      dont_stop = FALSE;
234 	    }
235 	  else
236 	    switch (in_ch)
237 	      {
238 	      case KEY_LEFT:
239 	      case KEY_UP:
240 		focus--;
241 		break;
242 	      case KEY_RIGHT:
243 	      case KEY_DOWN:
244 		focus++;
245 		break;
246 	      }
247 	  break;
248 
249 	case 3:		/* Next > */
250 	  if (in_ch == KEY_ENTER || in_ch == 13)
251 	    switch (checkfile (string.string))
252 	      {
253 	      case FILE_EXISTS:
254 		strcpy (selectedfile, string.string);
255 		returnval = 2;
256 		dont_stop = FALSE;
257 		break;
258 
259 	      case DIR_EXISTS:
260 		error_window ("The specified name is of a directory. A \
261 file name must be specified.");
262 		header (SIGNPR_INFILE_HEADERTEXT);
263 		string.cursorpos = strlen (string.string);
264 		focus = 1;
265 		break;
266 
267 	      case DIR_OK_NEW_FILE:
268 	      case DIR_WRONG:
269 		error_window ("The specified file does not exist.");
270 		header (SIGNPR_INFILE_HEADERTEXT);
271 		string.cursorpos = strlen (string.string);
272 		focus = 1;
273 		break;
274 
275 	      default:
276 		error_window ("Fell out of switch, signpr_infilenm #1");
277 		header (SIGNPR_INFILE_HEADERTEXT);
278 		break;
279 	      }
280 	  else
281 	    switch (in_ch)
282 	      {
283 	      case KEY_LEFT:
284 	      case KEY_UP:
285 		focus--;
286 		break;
287 	      case KEY_RIGHT:
288 	      case KEY_DOWN:
289 		focus++;
290 		break;
291 	      }
292 	  break;
293 	}
294 
295       if (in_ch == 9)		/* TAB */
296 	focus++;
297 
298       if (in_ch == 27)
299 	dont_stop = FALSE;
300 
301       if (focus > 3)
302 	focus = 0;
303       if (focus < 0)
304 	focus = 3;
305     }
306   while (dont_stop);
307 
308   scrollmenu_delete_menu (&dirfilelist);
309   free (string.string);
310 
311   return returnval;
312 }
313