-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfileopen.c
203 lines (184 loc) · 5.42 KB
/
fileopen.c
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
/* ----------- fileopen.c ------------- */
#include "dflat.h"
static BOOL DlgFileOpen(char *, char *, char *, DBOX *);
static int DlgFnOpen(WINDOW, MESSAGE, PARAM, PARAM);
static void InitDlgBox(WINDOW);
static void StripPath(char *);
static BOOL IncompleteFilename(char *);
static char FileSpec[15];
static char SrchSpec[15];
static char FileName[15];
extern DBOX FileOpen;
extern DBOX SaveAs;
/*
* Dialog Box to select a file to open
*/
BOOL OpenFileDialogBox(char *Fspec, char *Fname)
{
return DlgFileOpen(Fspec, Fspec, Fname, &FileOpen);
}
/*
* Dialog Box to select a file to save as
*/
BOOL SaveAsDialogBox(char *Fspec, char *Sspec, char *Fname)
{
return DlgFileOpen(Fspec, Sspec ? Sspec : Fspec, Fname, &SaveAs);
}
/* --------- generic file open ---------- */
static BOOL DlgFileOpen(char *Fspec, char *Sspec, char *Fname, DBOX *db)
{
BOOL rtn;
strncpy(FileSpec, Fspec, 15);
strncpy(SrchSpec, Sspec, 15);
if ((rtn = DialogBox(NULL, db, TRUE, DlgFnOpen)) != FALSE)
strcpy(Fname, FileName);
return rtn;
}
/*
* Process dialog box messages
*/
static int DlgFnOpen(WINDOW wnd,MESSAGE msg,PARAM p1,PARAM p2)
{
switch (msg) {
case CREATE_WINDOW: {
int rtn = DefaultWndProc(wnd, msg, p1, p2);
DBOX *db = wnd->extension;
WINDOW cwnd = ControlWindow(db, ID_FILENAME);
SendMessage(cwnd, SETTEXTLENGTH, 64, 0);
return rtn;
}
case INITIATE_DIALOG:
InitDlgBox(wnd);
break;
case COMMAND:
switch ((int) p1) {
case ID_OK:
{
if ((int)p2 == 0) {
char fn[MAXPATH+1];
char nm[MAXFILE];
char ext[MAXEXT];
GetItemText(wnd, ID_FILENAME, fn, MAXPATH);
fnsplit(fn, NULL, NULL, nm, ext);
strcpy(FileName, nm);
strcat(FileName, ext);
CreatePath(NULL, fn, FALSE, TRUE);
if (IncompleteFilename(FileName)) {
/* --- no file name yet --- */
DBOX *db = wnd->extension;
WINDOW cwnd = ControlWindow(db, ID_FILENAME);
strcpy(FileSpec, FileName);
strcpy(SrchSpec, FileName);
InitDlgBox(wnd);
SendMessage(cwnd, SETFOCUS, TRUE, 0);
return TRUE;
}
}
break;
}
case ID_FILES:
switch ((int) p2) {
case ENTERFOCUS:
case LB_SELECTION:
/* selected a different filename */
GetDlgListText(wnd, FileName, ID_FILES);
PutItemText(wnd, ID_FILENAME, FileName);
break;
case LB_CHOOSE:
/* chose a file name */
GetDlgListText(wnd, FileName, ID_FILES);
SendMessage(wnd, COMMAND, ID_OK, 0);
break;
default:
break;
}
return TRUE;
case ID_DIRECTORY:
switch ((int) p2) {
case ENTERFOCUS:
PutItemText(wnd, ID_FILENAME, FileSpec);
break;
case LB_CHOOSE:
{
/* chose dir */
char dd[15];
GetDlgListText(wnd, dd, ID_DIRECTORY);
chdir(dd);
InitDlgBox(wnd);
SendMessage(wnd, COMMAND, ID_OK, 0);
break;
}
default:
break;
}
return TRUE;
case ID_DRIVE:
switch ((int) p2) {
case ENTERFOCUS:
PutItemText(wnd, ID_FILENAME, FileSpec);
break;
case LB_CHOOSE:
{
/* chose dir */
char dr[15];
GetDlgListText(wnd, dr, ID_DRIVE);
setdisk(*dr - 'A');
InitDlgBox(wnd);
SendMessage(wnd, COMMAND, ID_OK, 0);
}
default:
break;
}
return TRUE;
default:
break;
}
default:
break;
}
return DefaultWndProc(wnd, msg, p1, p2);
}
BOOL BuildFileList(WINDOW, char *);
void BuildDirectoryList(WINDOW);
void BuildDriveList(WINDOW);
void BuildPathDisplay(WINDOW);
/*
* Initialize the dialog box
*/
static void InitDlgBox(WINDOW wnd)
{
if (*FileSpec)
PutItemText(wnd, ID_FILENAME, FileSpec);
if (BuildFileList(wnd, SrchSpec))
BuildDirectoryList(wnd);
BuildDriveList(wnd);
BuildPathDisplay(wnd);
}
/*
* Strip the drive and path information from a file spec
*/
static void StripPath(char *filespec)
{
char *cp, *cp1;
cp = strchr(filespec, ':');
if (cp != NULL)
cp++;
else
cp = filespec;
while (TRUE) {
cp1 = strchr(cp, '\\');
if (cp1 == NULL)
break;
cp = cp1+1;
}
strcpy(filespec, cp);
}
static BOOL IncompleteFilename(char *s)
{
int lc = strlen(s)-1;
if (strchr(s, '?') || strchr(s, '*') || !*s)
return TRUE;
if (*(s+lc) == ':' || *(s+lc) == '\\')
return TRUE;
return FALSE;
}