-
Notifications
You must be signed in to change notification settings - Fork 0
/
ufsys.c
97 lines (89 loc) · 2.46 KB
/
ufsys.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
/* User-friendly file system code.
Contains standard provisions for asking the user if they want to
save their file before closing and marking if the file was
modified. Perhaps in the future it will contain an undo and redo
module. */
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <commdlg.h>
/* We need g_hInstance and SaveDialogTemplate() */
#include "dlgedit.h"
char curFilename[MAX_PATH];
BOOL noFileLoaded;
BOOL fileChanged;
/* Shows a dialog to open a file if "type" is zero, or shows a
dialog to save the current file if "type" is one. */
BOOL GetFilenameDialog(char* filename, char* dlgTitle, HWND hwnd,
unsigned type)
{
OPENFILENAME ofn;
char* filterStr = "Dialog Templates (*.dlg)\0*.dlg\0All Files\0*.*\0";
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.hInstance = g_hInstance;
ofn.lpstrFile = filename;
ofn.nMaxFile = MAX_PATH;
if (type == 0) /* Open */
ofn.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
else
ofn.Flags = OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
ofn.lpstrFilter = filterStr;
ofn.lpstrDefExt = "dlg";
ofn.lpstrTitle = dlgTitle;
switch (type)
{
case 0: /* Open */
return GetOpenFileName(&ofn);
case 1: /* Save */
return GetSaveFileName(&ofn);
}
return FALSE;
}
/* Asks the user if they want to save the file if the file was
modified. Returns zero if the application should not continue the
requested operation or one if the file was successfully saved or
not. */
int AskForSave(HWND hwnd)
{
if (fileChanged == TRUE)
{
int result;
result = MessageBox(hwnd,
"Do you want to save your changes to the current file?",
"Confirm", MB_YESNOCANCEL | MB_ICONEXCLAMATION);
switch (result)
{
case IDYES:
if (noFileLoaded == TRUE)
{
if (GetFilenameDialog(curFilename, "Save As", hwnd, 1))
{
if (!SaveDialogTemplate(curFilename))
{
MessageBox(hwnd,
"There was an error saving the file.",
"Error", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
}
else
return 0;
}
else if (!SaveDialogTemplate(curFilename))
{
MessageBox(hwnd,
"There was an error saving the file.",
"Error", MB_OK | MB_ICONEXCLAMATION);
return 0;
}
break;
case IDNO:
/* Do nothing */
break;
case IDCANCEL:
return 0; /* Don't destroy window */
}
}
return 1;
}