1 /* Record from LP - Get filename
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 "reclp_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 #include "reclp_main.h"
23
24 #include <stdlib.h>
25 #include <sys/stat.h>
26 #include <string.h>
27 #ifndef OLD_CURSES
28 #include <ncurses.h>
29 #else
30 #include <curses.h>
31 #endif
32
33
34 int
record_from_lp_get_filename(char * startdir,char * selectedfile)35 record_from_lp_get_filename (char *startdir, char *selectedfile)
36 /* Returns 1 if file selected, 0 if canceled */
37 {
38 scrollmenu_t dirfilelist;
39 stringinput_t string;
40 button_t ok_button, cancel_button, mixer_button;
41 int dont_stop = TRUE;
42 int returnval = 0;
43 int focus = 1;
44 int in_ch;
45 int i;
46 char helpstring[500];
47 char *charpointer;
48 struct stat filestats;
49 int oldselected;
50
51 char *helplines[5] =
52 {
53 " Browse files and directories. TAB: Next field",
54 " Enter name of destination sound file. TAB: Next field",
55 " Back to main menu TAB: Next field",
56 " Start `xmixer' to adjust recording volumes. TAB: Next field",
57 " Start recording in the specified sound file. TAB: Next field"};
58
59 dirfilelist.y = 3;
60 dirfilelist.x = 5;
61 dirfilelist.h = 12;
62 dirfilelist.w = 32;
63 dirfilelist.firstonscreen = 0;
64 dirfilemenu (startdir, &dirfilelist);
65 dirfilelist.selected = dirfilelist.last_of_1st_part + 1;
66
67 string.maxlen = 500;
68 string.string = (char *) malloc (string.maxlen * sizeof (char));
69 strcpy (string.string, startdir);
70 string.y = 17;
71 string.x = 5;
72 string.w = 70;
73 strcat (string.string, "new.wav");
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 ok_button.text = " Start recording ";
80 ok_button.y = 20;
81 ok_button.x = 58;
82 ok_button.selected = FALSE;
83
84 cancel_button.text = " Cancel ";
85 cancel_button.y = 20;
86 cancel_button.x = 5;
87 cancel_button.selected = FALSE;
88
89 mixer_button.text = " Mixer ";
90 mixer_button.y = 20;
91 mixer_button.x = 35;
92 mixer_button.selected = FALSE;
93
94 clearscreen (RECLP_HEADERTEXT);
95
96 do
97 {
98 header (RECLP_HEADERTEXT);
99
100 if (focus == 2)
101 cancel_button.selected = TRUE;
102 else
103 cancel_button.selected = FALSE;
104
105 if (focus == 3)
106 mixer_button.selected = TRUE;
107 else
108 mixer_button.selected = FALSE;
109
110 if (focus == 4)
111 ok_button.selected = TRUE;
112 else
113 ok_button.selected = FALSE;
114
115 dirfilelist.hasfocus = (focus == 0);
116
117 scrollmenu_display (&dirfilelist);
118 mybox (dirfilelist.y - 1, dirfilelist.x - 1,
119 dirfilelist.h + 2, dirfilelist.w + 2);
120 mvprintw (dirfilelist.y - 1, dirfilelist.x + 1,
121 "Files and directories:");
122
123 stringinput_display (&string);
124 mybox (string.y - 1, string.x - 1, 3, string.w + 2);
125 mvprintw (string.y - 1, string.x + 1, "File name:");
126
127 button_display (&cancel_button);
128 mybox (cancel_button.y - 1, cancel_button.x - 1,
129 3, strlen (cancel_button.text) + 2);
130 button_display (&mixer_button);
131 mybox (mixer_button.y - 1, mixer_button.x - 1,
132 3, strlen (mixer_button.text) + 2);
133 button_display (&ok_button);
134 mybox (ok_button.y - 1, ok_button.x - 1,
135 3, strlen (ok_button.text) + 2);
136
137 helpline (helplines[focus]);
138
139 if (focus == 1)
140 stringinput_display (&string);
141 else
142 move (0, 79);
143
144 refresh ();
145
146 in_ch = getch ();
147
148 switch (focus)
149 {
150 case 0: /* dirfilelist */
151 if (scrollmenu_stdkeys (in_ch, &dirfilelist) >= 0)
152 {
153 oldselected = dirfilelist.selected;
154 i = dirfilemenu_process_select (&dirfilelist,
155 helpstring);
156 if (i == 0) /* filename in helpstring */
157 {
158 strcpy (string.string, helpstring);
159 focus = 1;
160 string.cursorpos = strlen (string.string);
161 string.firstcharonscreen = 0;
162 }
163 else
164 /* dir in helpstring */
165 {
166 scrollmenu_delete_menu (&dirfilelist);
167 dirfilemenu (helpstring, &dirfilelist);
168 if (dirfilelist.number == 0)
169 {
170 error_window (
171 "No permission to read this directory.");
172
173 scrollmenu_delete_menu (&dirfilelist);
174 dirfilemenu (startdir, &dirfilelist);
175 dirfilelist.selected = oldselected;
176 }
177 else
178 {
179 strcpy (startdir, helpstring);
180 dirfilelist.firstonscreen = 0;
181
182 charpointer = strrchr (string.string, '/');
183 if (charpointer != NULL)
184 strcat (helpstring, charpointer + 1);
185 else
186 strcat (helpstring, string.string);
187 strcpy (string.string, helpstring);
188 }
189 }
190 }
191 else
192 switch (in_ch)
193 {
194 case KEY_LEFT:
195 focus--;
196 break;
197 case KEY_RIGHT:
198 focus++;
199 break;
200 }
201 break;
202
203 case 1: /* string */
204 stringinput_stdkeys (in_ch, &string);
205 if (in_ch == KEY_ENTER || in_ch == 13)
206 {
207 strcpy (helpstring, string.string);
208
209 /* cut away last '/'-s */
210 while (strlen (helpstring) > 0 &&
211 helpstring[strlen (helpstring) - 1] == '/')
212 helpstring[strlen (helpstring) - 1] = '\0';
213
214 strcat (helpstring, "/");
215
216 if (!stat (helpstring, &filestats) &&
217 S_ISDIR (filestats.st_mode))
218 {
219 strcpy (startdir, helpstring);
220 scrollmenu_delete_menu (&dirfilelist);
221 dirfilemenu (startdir, &dirfilelist);
222 dirfilelist.firstonscreen = 0;
223 dirfilelist.selected =
224 dirfilelist.last_of_1st_part + 1;
225 strcpy (string.string, startdir);
226 string.cursorpos = strlen (string.string);
227 focus = 0;
228 }
229 else /* it's a file */
230 focus = 4;
231 }
232 else
233 switch (in_ch)
234 {
235 case KEY_UP:
236 focus--;
237 break;
238 case KEY_DOWN:
239 focus++;
240 break;
241 }
242 break;
243
244 case 2: /* Cancel */
245 if (in_ch == KEY_ENTER || in_ch == 13)
246 {
247 returnval = 0;
248 dont_stop = FALSE;
249 }
250 else
251 switch (in_ch)
252 {
253 case KEY_LEFT:
254 case KEY_UP:
255 focus--;
256 break;
257 case KEY_RIGHT:
258 case KEY_DOWN:
259 focus++;
260 break;
261 }
262 break;
263
264 case 3: /* Mixer */
265 if (in_ch == KEY_ENTER || in_ch == 13)
266 {
267 system ("xmixer &");
268
269 error_window ("Make sure that the appropriate channel is \
270 selected for recording, and all the others for playback.");
271 }
272 else
273 switch (in_ch)
274 {
275 case KEY_LEFT:
276 case KEY_UP:
277 focus--;
278 break;
279 case KEY_RIGHT:
280 case KEY_DOWN:
281 focus++;
282 break;
283 }
284 break;
285
286 case 4: /* OK */
287 if (in_ch == KEY_ENTER || in_ch == 13)
288 switch (checkfile (string.string))
289 {
290 case FILE_EXISTS:
291 if (yesno_window ("The specified file already exists. \
292 Overwrite it?", " Yes ", " No ", 0))
293 {
294 strcpy (selectedfile, string.string);
295 returnval = 1;
296 dont_stop = FALSE;
297 }
298 else
299 {
300 string.cursorpos = strlen (string.string);
301 focus = 1;
302 }
303 break;
304
305 case DIR_EXISTS:
306 error_window ("The specified name is of a directory. A \
307 file name must be specified.");
308 string.cursorpos = strlen (string.string);
309 focus = 1;
310 break;
311
312 case DIR_OK_NEW_FILE:
313 strcpy (selectedfile, string.string);
314 returnval = 1;
315 dont_stop = FALSE;
316 break;
317
318 case DIR_WRONG:
319 error_window ("The directory of the specified file does \
320 not exist.");
321 string.cursorpos = strlen (string.string);
322 focus = 1;
323 break;
324
325 default:
326 error_window ("Fell out of switch, reclp_filenm #1");
327 break;
328 }
329 else
330 switch (in_ch)
331 {
332 case KEY_LEFT:
333 case KEY_UP:
334 focus--;
335 break;
336 case KEY_RIGHT:
337 case KEY_DOWN:
338 focus++;
339 break;
340 }
341 break;
342 }
343
344 if (in_ch == 9) /* TAB */
345 focus++;
346
347 if (in_ch == 27)
348 dont_stop = FALSE;
349
350 if (focus > 4)
351 focus = 0;
352 if (focus < 0)
353 focus = 4;
354 }
355 while (dont_stop);
356
357 scrollmenu_delete_menu (&dirfilelist);
358 free (string.string);
359
360 return returnval;
361 }
362