forked from alexfru/dflat20
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfileopen.c
171 lines (153 loc) · 4.93 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
/* ----------- 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[MAXPATH];
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, sizeof(FileSpec)-1);
strncpy(SrchSpec, Sspec, sizeof(FileSpec)-1);
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) {
GetItemText(wnd, ID_FILENAME, FileName, MAXPATH);
if (CheckAndChangeDir(FileName))
strcpy(FileName, "*");
if (IncompleteFilename(FileName)) {
/* --- no file name yet --- */
DBOX *db = wnd->extension;
WINDOW cwnd = ControlWindow(db, ID_FILENAME);
strncpy(FileSpec, FileName, sizeof(FileSpec)-1);
strncpy(SrchSpec, FileName, sizeof(FileSpec)-1);
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[MAXPATH];
GetDlgListText(wnd, dd, ID_DIRECTORY);
chdir(dd);
InitDlgBox(wnd);
SendMessage(wnd, COMMAND, ID_OK, 0);
break;
}
default:
break;
}
return TRUE;
default:
break;
}
default:
break;
}
return DefaultWndProc(wnd, msg, p1, p2);
}
BOOL BuildFileList(WINDOW, char *);
void BuildDirectoryList(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);
BuildPathDisplay(wnd);
}
/*
* Strip the path information from a file spec
*/
static void StripPath(char *filespec)
{
char *cp, *cp1;
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;
return FALSE;
}