1 /* Track Splitting - Get file-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 "tracksplit_filenm.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
tracksplit_select_file(char * startdir,char * selectedfile)34 tracksplit_select_file (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 for track splitting.                 TAB: Next field",
53     " Enter name of sound file for track splitting.                  TAB: Next field",
54     " Back to main menu.                                             TAB: Next field",
55     " To Track Splitting - Parameters.                               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 (TRACKSPLIT_FILE_HEADERTEXT);
90 
91   do
92     {
93       header (TRACKSPLIT_FILE_HEADERTEXT);
94 
95       if (focus == 2)
96 	cancel_button.selected = TRUE;
97       else
98 	cancel_button.selected = FALSE;
99 
100       if (focus == 3)
101 	next_button.selected = TRUE;
102       else
103 	next_button.selected = FALSE;
104 
105       dirfilelist.hasfocus = (focus == 0);
106 
107       scrollmenu_display (&dirfilelist);
108       mybox (dirfilelist.y - 1, dirfilelist.x - 1,
109 	     dirfilelist.h + 2, dirfilelist.w + 2);
110       mvprintw (dirfilelist.y - 1, dirfilelist.x + 1,
111 		"Files and directories:");
112 
113       stringinput_display (&string);
114       mybox (string.y - 1, string.x - 1, 3, string.w + 2);
115       mvprintw (string.y - 1, string.x + 1, "File name:");
116 
117       button_display (&cancel_button);
118       mybox (cancel_button.y - 1, cancel_button.x - 1,
119 	     3, strlen (cancel_button.text) + 2);
120       button_display (&next_button);
121       mybox (next_button.y - 1, next_button.x - 1,
122 	     3, strlen (next_button.text) + 2);
123 
124       helpline (helplines[focus]);
125 
126       if (focus == 1)
127 	stringinput_display (&string);
128       else
129 	move (0, 79);
130 
131       refresh ();
132 
133       in_ch = getch ();
134 
135       switch (focus)
136 	{
137 	case 0:		/* dirfilelist */
138 	  if (scrollmenu_stdkeys (in_ch, &dirfilelist) >= 0)
139 	    {
140 	      oldselected = dirfilelist.selected;
141 	      i = dirfilemenu_process_select (&dirfilelist,
142 					      helpstring);
143 	      if (i == 0)	/* filename in helpstring */
144 		{
145 		  strcpy (string.string, helpstring);
146 		  focus = 1;
147 		  string.cursorpos = strlen (string.string);
148 		  string.firstcharonscreen = 0;
149 		}
150 	      else
151 		/* dir in helpstring */
152 		{
153 		  scrollmenu_delete_menu (&dirfilelist);
154 		  dirfilemenu (helpstring, &dirfilelist);
155 		  if (dirfilelist.number == 0)
156 		    {
157 		      error_window (
158 				   "No permission to read this directory.");
159 		      scrollmenu_delete_menu (&dirfilelist);
160 		      dirfilemenu (startdir, &dirfilelist);
161 		      dirfilelist.selected = oldselected;
162 		    }
163 		  else
164 		    {
165 		      strcpy (startdir, helpstring);
166 		      dirfilelist.firstonscreen = 0;
167 
168 		      charpointer = strrchr (string.string, '/');
169 		      if (charpointer != NULL)
170 			strcat (helpstring, charpointer + 1);
171 		      else
172 			strcat (helpstring, string.string);
173 		      strcpy (string.string, helpstring);
174 		    }
175 		}
176 	    }
177 	  else
178 	    switch (in_ch)
179 	      {
180 	      case KEY_LEFT:
181 		focus--;
182 		break;
183 	      case KEY_RIGHT:
184 		focus++;
185 		break;
186 	      }
187 	  break;
188 
189 	case 1:		/* string */
190 	  stringinput_stdkeys (in_ch, &string);
191 	  if (in_ch == KEY_ENTER || in_ch == 13)
192 	    {
193 	      strcpy (helpstring, string.string);
194 
195 	      /* cut away last '/'-s */
196 	      while (strlen (helpstring) > 0 &&
197 		     helpstring[strlen (helpstring) - 1] == '/')
198 		helpstring[strlen (helpstring) - 1] = '\0';
199 
200 	      strcat (helpstring, "/");
201 
202 	      if (!stat (helpstring, &filestats) &&
203 		  S_ISDIR (filestats.st_mode))
204 		{
205 		  strcpy (startdir, helpstring);
206 		  scrollmenu_delete_menu (&dirfilelist);
207 		  dirfilemenu (startdir, &dirfilelist);
208 		  dirfilelist.firstonscreen = 0;
209 		  dirfilelist.selected =
210 		    dirfilelist.last_of_1st_part + 1;
211 		  strcpy (string.string, startdir);
212 		  string.cursorpos = strlen (string.string);
213 		  focus = 0;
214 		}
215 	      else		/* it's a file */
216 		focus = 3;
217 	    }
218 	  else
219 	    switch (in_ch)
220 	      {
221 	      case KEY_UP:
222 		focus--;
223 		break;
224 	      case KEY_DOWN:
225 		focus++;
226 		break;
227 	      }
228 	  break;
229 
230 	case 2:		/* Cancel */
231 	  if (in_ch == KEY_ENTER || in_ch == 13)
232 	    {
233 	      returnval = 0;
234 	      dont_stop = FALSE;
235 	    }
236 	  else
237 	    switch (in_ch)
238 	      {
239 	      case KEY_LEFT:
240 	      case KEY_UP:
241 		focus--;
242 		break;
243 	      case KEY_RIGHT:
244 	      case KEY_DOWN:
245 		focus++;
246 		break;
247 	      }
248 	  break;
249 
250 	case 3:		/* Next > */
251 	  if (in_ch == KEY_ENTER || in_ch == 13)
252 	    switch (checkfile (string.string))
253 	      {
254 	      case FILE_EXISTS:
255 		strcpy (selectedfile, string.string);
256 		returnval = 2;
257 		dont_stop = FALSE;
258 		break;
259 
260 	      case DIR_EXISTS:
261 		error_window ("The specified name is of a directory. A \
262 file name must be specified.");
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 		string.cursorpos = strlen (string.string);
271 		focus = 1;
272 		break;
273 
274 	      default:
275 		error_window ("Fell out of switch, tracksplit_filenm #1");
276 		break;
277 	      }
278 	  else
279 	    switch (in_ch)
280 	      {
281 	      case KEY_LEFT:
282 	      case KEY_UP:
283 		focus--;
284 		break;
285 	      case KEY_RIGHT:
286 	      case KEY_DOWN:
287 		focus++;
288 		break;
289 	      }
290 	  break;
291 	}
292 
293       if (in_ch == 9)		/* TAB */
294 	focus++;
295 
296       if (in_ch == 27)
297 	dont_stop = FALSE;
298 
299       if (focus > 3)
300 	focus = 0;
301       if (focus < 0)
302 	focus = 3;
303     }
304   while (dont_stop);
305 
306   scrollmenu_delete_menu (&dirfilelist);
307   free (string.string);
308 
309   return returnval;
310 }
311