-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFILEDLG.C
380 lines (319 loc) · 11.7 KB
/
FILEDLG.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
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
/*
**************************************************************************
filedlg.c Brett A. Morrison
This file contains the dialog box routines for reading and writing files.
files.c contains the actual read functions. It is called by messages
deciphered in golf.c. It performs thorough error checking for input
of the file names.
**************************************************************************
*/
#include "golf.h"
static char szDefExt [FILE_EXTENSION_SIZE]; /* Default extension */
static char szFileName[FULL_NAME_SIZE]; /* File name to display */
static char szFileSpec[FILE_NAME_SIZE]; /* Type of file */
static POFSTRUCT pof; /* Characteristics of file */
static WORD wFileAttr; /* Stores attributes */
static WORD wStatus; /* Stores read/write status */
static char szWhich; /* Determines if course or player */
/*
--------------------------------------------------------------------------
DoFileOpenDlg
Initializes parameters for the FileOpenDlgProc.
Inputs:
hInst - Instance of the parent window
hwnd - Handle to the parent window
*szFileSpecIn - Pointer to filename passed to procedure
*szDefExtIn - Pointer to default extension
wFileAttrIn - File attribute types to be displayed
*szFileNameOut - Filename returned
pofIn - Structure containing characteristics of file
which - determines if the file is a player or course file
Outputs:
TRUE if OK is selected, FALSE if user ends box with cancel.
--------------------------------------------------------------------------
*/
BOOL DoFileOpenDlg(HANDLE hInst, HWND hwnd, char *szFileSpecIn,
char *szDefExtIn, WORD wFileAttrIn,
char *szFileNameOut, POFSTRUCT pofIn, char which)
{ /* DoFileOpenDlg */
FARPROC lpfnFileOpenDlgProc; /* Procedure handle */
BOOL iReturn; /* Return value */
lstrcpy(szFileSpec, szFileSpecIn); /* Set initial names */
lstrcpy(szDefExt, szDefExtIn);
wFileAttr = wFileAttrIn; /* Set initial attributes */
pof = pofIn; /* Set initial characteristics */
szWhich = which;
/* Set procedure */
lpfnFileOpenDlgProc = MakeProcInstance((FARPROC)FileOpenDlgProc, hInst);
/* Call procedure */
iReturn = DialogBox(hInst, "OPEN", hwnd, lpfnFileOpenDlgProc);
/* Free memory allocated for dialog box */
FreeProcInstance(lpfnFileOpenDlgProc);
lstrcpy(szFileNameOut, szFileName);
return iReturn;
} /* DoFileOpenDlg */
/*
--------------------------------------------------------------------------
DoFileSaveDlg
Initializes parameters for the FileSaveDlgProc.
Inputs:
hInst - Instance of the parent window
hwnd - Handle to the parent window
*szFileSpecIn - Pointer to filename passed to procedure
*szDefExtIn - Pointer to default extension
*pwStatusOut - File attribute types to be displayed
*szFileNameOut - Filename returned
pofIn - Structure containing characteristics of file
which - determines if the file is a player or course file
Outputs:
TRUE if OK is selected, FALSE if user ends box with cancel.
--------------------------------------------------------------------------
*/
BOOL DoFileSaveDlg(HANDLE hInst, HWND hwnd, char *szFileSpecIn,
char *szDefExtIn, WORD wFileAttrIn, WORD *pwStatusOut,
char *szFileNameOut, POFSTRUCT pofIn, char which)
{ /* DoFileSaveDlg */
FARPROC lpfnFileSaveDlgProc;
BOOL iReturn;
wFileAttr = wFileAttrIn; /* Set initial attributes */
lstrcpy(szFileSpec, szFileSpecIn);
lstrcpy(szDefExt, szDefExtIn);
pof = pofIn;
szWhich = which;
/* Set procedure */
lpfnFileSaveDlgProc = MakeProcInstance((FARPROC)FileSaveDlgProc, hInst);
/* Call procedure */
iReturn = DialogBox(hInst, "SAVE", hwnd, lpfnFileSaveDlgProc);
/* Free memory allocated for dialog box */
FreeProcInstance(lpfnFileSaveDlgProc);
lstrcpy(szFileNameOut, szFileName);
*pwStatusOut = wStatus;
return iReturn;
} /* DoFileSaveDlg */
/*
--------------------------------------------------------------------------
FileOpenDlgProc
Processes open file dialog box, and performs error checking.
Inputs:
hDlg - Handle to dialog box
message - Message from MainWndProc
wParam - Additional message information
lParam - Additional message information
Outputs:
TRUE if valid filename is selected, else FALSE.
--------------------------------------------------------------------------
*/
BOOL FAR PASCAL FileOpenDlgProc(HWND hDlg, WORD message, WORD wParam,
LONG lParam)
{ /* FileOpenDlgProc */
char cLastChar;
short nEditLen;
switch (message)
{ /* switch (message) */
case WM_INITDIALOG: /* Setup box */
SendDlgItemMessage(hDlg, IDD_FNAME, EM_LIMITTEXT, COLUMN_WIDTH, 0L);
/* Get current drive and directory */
DlgDirList(hDlg, szFileSpec, IDD_FLIST, IDD_FPATH, wFileAttr);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
SetWindowText(hDlg, szWhich == PLAYER_MARKER ? "Open Player File" :
"Open Course File");
return TRUE;
case WM_COMMAND: /* Command messages from box */
switch (wParam)
{
case IDD_FLIST: /* Relist files */
switch (HIWORD (lParam))
{
case LBN_SELCHANGE: /* Relist files */
if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
lstrcat(szFileName, szFileSpec);
SetDlgItemText(hDlg, IDD_FNAME, szFileName);
return TRUE;
case LBN_DBLCLK: /* Process open message */
if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
{ /* if */
lstrcat(szFileName, szFileSpec);
DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
} /* if */
else /* Send OK message */
{ /* else */
SetDlgItemText(hDlg, IDD_FNAME, szFileName);
SendMessage(hDlg, WM_COMMAND, IDOK, 0L);
} /* else */
return TRUE;
} /* switch (HIWORD (lParam)) */
break;
case IDD_FNAME: /* Print name to small window */
if (HIWORD(lParam) == EN_CHANGE)
EnableWindow(GetDlgItem(hDlg, IDOK),
(BOOL) SendMessage(LOWORD(lParam),
WM_GETTEXTLENGTH, 0, 0L));
return TRUE;
case IDOK: /* Try to open file */
/* This routine was written by Charles Petzold */
GetDlgItemText(hDlg, IDD_FNAME, szFileName, COLUMN_WIDTH);
nEditLen = lstrlen(szFileName);
cLastChar = *AnsiPrev(szFileName, szFileName + nEditLen);
if (cLastChar == '\\' || cLastChar == ':') /* New drive or directory */
lstrcat(szFileName, szFileSpec);
if (lstrchr(szFileName, '*') || lstrchr(szFileName, '?')) /* Wildcards */
{
if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
{
lstrcpy(szFileSpec, szFileName);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
} /* if */
else
MessageBeep(0);
return TRUE;
} /* if */
/* set the selected directory */
lstrcat(lstrcat(szFileName, "\\"), szFileSpec);
/* Reset defaults */
if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
{
lstrcpy(szFileSpec, szFileName);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
return TRUE;
} /* if */
szFileName[nEditLen] = '\0'; /* Terminate the string */
/* check if file can be opened */
if (-1 == OpenFile(szFileName, pof, OF_READ | OF_EXIST))
{
lstrcat(szFileName, szDefExt);
if (-1 == OpenFile(szFileName, pof, OF_READ | OF_EXIST))
{
MessageBeep(0);
return TRUE;
} /* if */
} /* if */
/* Change filename to equal full path name */
lstrcpy(szFileName, AnsiNext(lstrrchr ((LPSTR)pof->szPathName, '\\')));
OemToAnsi(szFileName, szFileName); /* Copy to ansi equivalent */
EndDialog(hDlg, TRUE);
return TRUE;
case IDCANCEL:
EndDialog(hDlg, FALSE);
return TRUE;
} /* switch (wParam) */
} /* switch (mesage) */
return FALSE;
} /* FileOpenDlgProc */
/*
--------------------------------------------------------------------------
FileSaveDlgProc
Processes save file dialog box, and performs error checking.
Inputs:
hDlg - Handle to parent window
message - Message from MainWndProc
wParam - Additional message information
lParam - Additional message information
Outputs:
TRUE if valid filename is selected, else FALSE.
--------------------------------------------------------------------------
*/
BOOL FAR PASCAL FileSaveDlgProc(HWND hDlg, WORD message, WORD wParam,
LONG lParam)
{ /* FileSaveDlgProc */
char cLastChar;
short nEditLen;
switch(message)
{
case WM_INITDIALOG: /* Setup box */
SendDlgItemMessage(hDlg, IDD_FNAME, EM_LIMITTEXT, COLUMN_WIDTH, 0L);
/* Get current drive and directory */
DlgDirList(hDlg, szFileSpec, IDD_FLIST, IDD_FPATH, wFileAttr);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
SetWindowText(hDlg, szWhich == PLAYER_MARKER ? "Save Player File" :
"Save Course File");
return TRUE;
case WM_COMMAND: /* Command messages from box */
switch (wParam)
{ /* switch (wParam) */
case IDD_FLIST: /* Relist files */
switch (HIWORD (lParam))
{ /* switch (HIWORD (lParam)) */
case LBN_SELCHANGE: /* Relist files */
if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
lstrcat(szFileName, szFileSpec);
SetDlgItemText(hDlg, IDD_FNAME, szFileName);
return TRUE;
case LBN_DBLCLK: /* Process open message */
if (DlgDirSelect(hDlg, szFileName, IDD_FLIST))
{ /* if */
lstrcat(szFileName, szFileSpec);
DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
} /* if */
else /* Send OK message */
{ /* else */
SetDlgItemText(hDlg, IDD_FNAME, szFileName);
SendMessage(hDlg, WM_COMMAND, IDOK, 0L);
} /* else */
return TRUE;
} /* switch (HIWORD (lParam)) */
break;
case IDD_FNAME: /* Print name to small window */
if (HIWORD(lParam) == EN_CHANGE)
EnableWindow(GetDlgItem(hDlg, IDOK),
(BOOL) SendMessage(LOWORD (lParam),
WM_GETTEXTLENGTH, 0, 0L));
return TRUE;
case IDOK: /* Try to save file */
/* This routine was written by Charles Petzold */
GetDlgItemText(hDlg, IDD_FNAME, szFileName, COLUMN_WIDTH);
nEditLen = lstrlen(szFileName);
cLastChar = *AnsiPrev(szFileName, szFileName + nEditLen);
if (cLastChar == '\\' || cLastChar == ':') /* New drive or directory */
lstrcat(szFileName, szFileSpec);
if (lstrchr(szFileName, '*') || lstrchr(szFileName, '?')) /* Wildcards */
{
if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
{
lstrcpy(szFileSpec, szFileName);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
} /* if */
else
MessageBeep(0);
return TRUE;
} /* if */
/* set the selected directory */
lstrcat(lstrcat(szFileName, "\\"), szFileSpec);
/* Reset defaults */
if (DlgDirList(hDlg, szFileName, IDD_FLIST, IDD_FPATH, wFileAttr))
{ /* if */
lstrcpy(szFileSpec, szFileName);
SetDlgItemText(hDlg, IDD_FNAME, szFileSpec);
return TRUE;
} /* if */
szFileName[nEditLen] = '\0'; /* Terminate the string */
/* check if file can be opened */
if (-1 == OpenFile(szFileName, pof, OF_PARSE))
{
MessageBeep(0);
return TRUE;
}
if (!lstrchr(AnsiNext(lstrrchr((LPSTR)pof->szPathName, '\\')), '.'))
lstrcat(szFileName, szDefExt);
if (-1 != OpenFile(szFileName, pof, OF_WRITE | OF_EXIST))
wStatus = 1;
else if (-1 != OpenFile(szFileName, pof, OF_CREATE | OF_EXIST))
wStatus = 0;
else
{
MessageBeep(0);
return TRUE;
} /* else */
/* Change filename to equal full path name */
lstrcpy(szFileName, AnsiNext(lstrrchr ((LPSTR)pof->szPathName, '\\')));
OemToAnsi(szFileName, szFileName); /* Copy to ansi equivalent */
EndDialog(hDlg, TRUE);
return TRUE;
case IDCANCEL:
EndDialog(hDlg, FALSE);
return TRUE;
} /* switch (wParam) */
} /* switch (mesage) */
return FALSE;
} /* FileSaveDlgProc */