1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
/*
* program: fselect.c
* author: Marc van Kempen (wmbfmk@urc.tue.nl)
* Desc: File selection routine
*
* Copyright (c) 1995, Marc van Kempen
*
* All rights reserved.
*
* This software may be used, modified, copied, distributed, and
* sold, in both source and binary form provided that the above
* copyright and these terms are retained, verbatim, as the first
* lines of this file. Under no circumstances is the author
* responsible for the proper functioning of this software, nor does
* the author assume any responsibility for damages incurred with
* its use.
*
*/
#include <stdlib.h>
#include <unistd.h>
#include <sys/param.h>
#include <dialog.h>
#include "ui_objects.h"
#include "dir.h"
#include "dialog.priv.h"
/*
* Local prototypes
*/
char *dialog_dfselect(char *dir, char *fmask, int is_fselect);
/*
* Functions
*/
void
get_directories(DirList *d, int n, char ***names, int *nd)
/*
* Desc: return the directorienames in <dir> as an array in
* <names>, the # of entries in <nd>, memory allocated
* to *names should be freed when done with it.
*/
{
int i;
/* count the directories, which are in front */
*nd = 0;
while ((*nd < n) && (S_ISDIR(d[*nd].filestatus.st_mode))) (*nd)++;
*names = (char **) malloc( *nd * sizeof(char *) );
for (i=0; i<*nd; i++) {
(*names)[i] = (char *) malloc( strlen(d[i].filename) + 1);
strcpy((*names)[i], d[i].filename);
}
return;
} /* get_directories() */
void
get_filenames(DirList *d, int n, char ***names, int *nf)
/*
* Desc: return the filenames in <dir> as an arry in
* <names>, the # of entries in <nf>, memory allocated
* to *names should be freed when done.
*/
{
int nd, i;
/* the # of regular files is the total # of files - # of directories */
/* count the # of directories */
nd = 0;
while ((nd < n) && (S_ISDIR(d[nd].filestatus.st_mode))) nd++;
*names = (char **) malloc( (n-nd) * sizeof(char *) );
*nf = n - nd;
for (i=0; i<*nf; i++) {
(*names)[i] = (char *) malloc( strlen(d[i+nd].filename) + 1);
strcpy((*names)[i], d[i+nd].filename);
}
return;
} /* get_filenames() */
void
FreeNames(char **names, int n)
/*
* Desc: free the space occupied by names
*/
{
int i;
/* free the space occupied by names */
for (i=0; i<n; i++) {
free(names[i]);
}
free(names);
return;
} /* FreeNames() */
int
dialog_dselect_old(void)
/*
* Desc: starting from the current directory,
* choose a new current directory
*/
{
DirList *d = NULL;
char **names, old_dir[MAXPATHLEN];
WINDOW *ds_win;
ButtonObj *okbut, *cancelbut;
ListObj *dirs_obj;
StringObj *dir_obj;
char o_dir[MAXPATHLEN];
struct ComposeObj *obj = NULL;
int n, nd, okbutton, cancelbutton,
quit, cancel, ret;
ds_win = newwin(LINES-8, COLS-30, 4, 15);
if (ds_win == NULL) {
fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n",
LINES-8, COLS-30, 4, 15);
exit(1);
}
draw_box(ds_win, 0, 0, LINES-8, COLS-30, dialog_attr, border_attr);
wattrset(ds_win, dialog_attr);
mvwaddstr(ds_win, 0, (COLS-30)/2 - 9, " Directory Select ");
draw_shadow(stdscr, 4, 15, LINES-8, COLS-30);
display_helpline(ds_win, LINES-9, COLS-30);
/* the Directory string input field */
getcwd(o_dir, MAXPATHLEN);
dir_obj = NewStringObj(ds_win, "Directory:", o_dir, 1, 2, COLS-34, MAXPATHLEN-1);
AddObj(&obj, STRINGOBJ, (void *) dir_obj);
/* the list of directories */
get_dir(".", "*", &d, &n);
get_directories(d, n, &names, &nd);
dirs_obj = NewListObj(ds_win, "Directories:", names, o_dir, 5, 2,
LINES-15, COLS-48, nd);
AddObj(&obj, LISTOBJ, (void *) dirs_obj);
/* the Ok-button */
okbutton = FALSE;
okbut = NewButtonObj(ds_win, "Continue", &okbutton, 7, COLS-45);
AddObj(&obj, BUTTONOBJ, (void *) okbut);
/* the Cancel-button */
cancelbutton = FALSE;
cancelbut = NewButtonObj(ds_win, "Return", &cancelbutton, 11, COLS-44);
AddObj(&obj, BUTTONOBJ, (void *) cancelbut);
quit = FALSE;
cancel = FALSE;
strcpy(old_dir, o_dir);
while (!quit) {
ret = PollObj(&obj);
switch(ret) {
case SEL_BUTTON:
if (okbutton) {
quit = TRUE;
}
if (cancelbutton) {
quit = TRUE;
cancel = TRUE;
}
break;
case SEL_CR:
if (strcmp(old_dir, o_dir)) {
/* the directory was changed, cd into it */
if (chdir(o_dir)) {
dialog_notify("Could not change into directory");
strcpy(o_dir, old_dir);
} else {
getcwd(o_dir, MAXPATHLEN);
strcpy(old_dir, o_dir);
}
RefreshStringObj(dir_obj);
}
get_dir(".", "*", &d, &n);
FreeNames(names, nd);
get_directories(d, n, &names, &nd);
UpdateListObj(dirs_obj, names, nd);
if (((obj->prev)->obj == (void *) dirs_obj)) {
obj=obj->prev;
}
break;
case SEL_ESC:
quit = TRUE;
cancel = TRUE;
break;
case KEY_F(1):
display_helpfile();
break;
}
}
FreeNames(names, nd);
DelObj(obj);
delwin(ds_win);
return(cancel);
} /* dialog_dselect() */
int
dialog_dselect(char *dir, char *fmask)
/*
* Desc: Choose a directory
*/
{
if (dialog_dfselect(dir, fmask, FALSE)) {
return(FALSE); /* esc or cancel was pressed */
} else {
return(TRUE); /* directory was selected */
}
} /* dialog_dselect() */
char *
dialog_fselect(char *dir, char *fmask)
/*
* Desc: Choose a file from a directory
*/
{
return(dialog_dfselect(dir, fmask, TRUE));
} /* dialog_fselect() */
char *
dialog_dfselect(char *dir, char *fmask, int is_fselect)
/*
* Desc: choose a file from the directory <dir>, which
* initially display files with the mask <filemask>
* pre: <dir> is the initial directory
* only files corresponding to the mask <fmask> are displayed
* post: returns NULL if no file was selected
* else returns pointer to filename, space is allocated, should
* be freed after use.
*/
{
DirList *d = NULL;
char msg[512];
char **fnames, **dnames, *ret_name;
WINDOW *fs_win;
int n, nd, nf, ret;
StringObj *fm_obj, *dir_obj, *sel_obj;
char o_fm[255], o_dir[MAXPATHLEN], o_sel[MAXPATHLEN];
char old_fmask[255], old_dir[MAXPATHLEN];
ListObj *dirs_obj, *files_obj;
struct ComposeObj *obj = NULL, *o;
int quit, cancel;
ButtonObj *okbut_obj, *canbut_obj;
int ok_button, cancel_button;
if (chdir(dir)) {
sprintf(msg, "Could not move into specified directory: %s", dir);
dialog_notify(msg);
return(NULL);
}
getcwd(o_dir, MAXPATHLEN);
/* setup the fileselect-window and initialize its components */
fs_win = newwin(LINES-2, COLS-20, 1, 10);
if (fs_win == NULL) {
endwin();
fprintf(stderr, "\nnewwin(%d,%d,%d,%d) failed, maybe wrong dims\n",
LINES-2, COLS-20, 2, 10);
exit(1);
}
draw_box(fs_win, 0, 0, LINES-2, COLS-20, dialog_attr, border_attr);
wattrset(fs_win, dialog_attr);
if (is_fselect) {
mvwaddstr(fs_win, 0, (COLS-20)/2 - 7, " File Select ");
} else {
mvwaddstr(fs_win, 0, (COLS-20)/2 - 9, " Directory Select ");
}
draw_shadow(stdscr, 1, 10, LINES-2, COLS-20);
display_helpline(fs_win, LINES-3, COLS-20);
/* Filemask entry */
strcpy(o_fm, fmask);
fm_obj = NewStringObj(fs_win, "Filemask:", o_fm, 1, 2, 19, 255);
AddObj(&obj, STRINGOBJ, (void *) fm_obj);
/* Directory entry */
dir_obj = NewStringObj(fs_win, "Directory:", o_dir, 1, 22, COLS-44, 255);
AddObj(&obj, STRINGOBJ, (void *) dir_obj);
/* Directory list */
get_dir(".", fmask, &d, &n); /* read the entire directory */
get_directories(d, n, &dnames, &nd); /* extract the dir-entries */
if (is_fselect) {
dirs_obj = NewListObj(fs_win, "Directories:", dnames, o_dir, 5, 2,
LINES-16, (COLS-20)/2-2, nd);
} else {
dirs_obj = NewListObj(fs_win, "Directories:", dnames, o_dir, 5, 2,
LINES-12, (COLS-20)/2-2, nd);
}
AddObj(&obj, LISTOBJ, (void *) dirs_obj);
/* Filenames list */
get_filenames(d, n, &fnames, &nf); /* extract the filenames */
if (is_fselect) {
files_obj = NewListObj(fs_win, "Files:", fnames, o_sel, 5, (COLS-20)/2+1,
LINES-16, (COLS-20)/2-3, nf);
} else {
files_obj = NewListObj(fs_win, "Files:", fnames, o_sel, 5, (COLS-20)/2+1,
LINES-12, (COLS-20)/2-3, nf);
}
AddObj(&obj, LISTOBJ, (void *) files_obj);
if (is_fselect) {
/* Selection entry */
o_sel[0] = '\0';
sel_obj = NewStringObj(fs_win, "Selection:", o_sel, LINES-10, 2, COLS-24, 255);
AddObj(&obj, STRINGOBJ, (void *) sel_obj);
}
/* Ok button */
ok_button = FALSE;
okbut_obj = NewButtonObj(fs_win, "Ok", &ok_button, LINES-6, 20);
AddObj(&obj, BUTTONOBJ, (void *) okbut_obj);
/* Cancel button */
cancel_button = FALSE;
canbut_obj = NewButtonObj(fs_win, "Cancel", &cancel_button, LINES-6, 30);
AddObj(&obj, BUTTONOBJ, (void *) canbut_obj);
/* Make sure all objects on the window are drawn */
wrefresh(fs_win);
keypad(fs_win, TRUE);
/* Start the reading */
o = obj;
strcpy(old_fmask, o_fm);
strcpy(old_dir, o_dir);
quit = FALSE;
cancel = FALSE;
while (!quit) {
ret = PollObj(&o);
switch(ret) {
case SEL_CR:
if (strcmp(old_fmask, o_fm) || strcmp(old_dir, o_dir)) {
/* reread directory and update the listobjects */
if (strcmp(old_dir, o_dir)) { /* dir entry was changed */
if (chdir(o_dir)) {
dialog_notify("Could not change into directory");
strcpy(o_dir, old_dir);
} else {
getcwd(o_dir, MAXPATHLEN);
strcpy(old_dir, o_dir);
}
RefreshStringObj(dir_obj);
} else { /* fmask entry was changed */
strcpy(old_fmask, o_fm);
}
get_dir(".", o_fm, &d, &n);
FreeNames(dnames, nd);
get_directories(d, n, &dnames, &nd);
UpdateListObj(dirs_obj, dnames, nd);
FreeNames(fnames, nf);
get_filenames(d, n, &fnames, &nf);
UpdateListObj(files_obj, fnames, nf);
if (((o->prev)->obj == (void *) dirs_obj)) {
o=o->prev;
}
}
break;
case SEL_BUTTON:
/* check which button was pressed */
if (ok_button) {
quit = TRUE;
}
if (cancel_button) {
quit = TRUE;
cancel = TRUE;
}
break;
case SEL_ESC:
quit = TRUE;
cancel = TRUE;
break;
case KEY_F(1):
case '?':
display_helpfile();
break;
}
}
DelObj(obj);
FreeNames(dnames, nd);
FreeNames(fnames, nf);
delwin(fs_win);
if (cancel || (strlen(o_sel) == 0)) {
return(NULL);
} else {
ret_name = (char *) malloc( strlen(o_sel) + 1 );
strcpy(ret_name, o_sel);
return(ret_name);
}
} /* dialog_fselect() */
|