-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialog.c
485 lines (432 loc) · 12.9 KB
/
dialog.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/* Lev Panov, 2057/2, October 2010 */
#include <windows.h>
#include <shlwapi.h> // for wnsprintf()
#include <stdio.h>
#include "main.h"
#include "dialog.h"
/***********************************************************************
* ShowLastError
*
* Show last windows error
*
* ARGUMENTS: none
* RETURNS: none
*/
void ShowLastError(void)
{
DWORD error = GetLastError();
if (error != NO_ERROR)
{
char *lpMsgBuf;
char Title[MAX_STRING_LEN];
LoadString(Globals.hInstance, STRING_ERROR, Title, ARRAY_SIZE(Title));
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, error, 0, (char *)&lpMsgBuf, 0, NULL);
MessageBox(NULL, lpMsgBuf, Title, MB_OK | MB_ICONERROR);
LocalFree(lpMsgBuf);
}
}
/**
* Sets the caption of the main window according to Globals.FileTitle:
* Untitled - Notepad if no file is open
* filename - Notepad if a file is given
*/
void UpdateWindowCaption(void)
{
char Caption[MAX_STRING_LEN];
char Notepad[MAX_STRING_LEN];
static const char hyphenW[] = { ' ','-',' ',0 };
if (Globals.FileTitle[0] != '\0')
lstrcpy(Caption, Globals.FileTitle);
else
LoadString(Globals.hInstance, STRING_UNTITLED, Caption, ARRAY_SIZE(Caption));
LoadString(Globals.hInstance, STRING_NOTEPAD, Notepad, ARRAY_SIZE(Notepad));
lstrcat(Caption, hyphenW);
lstrcat(Caption, Notepad);
SetWindowText(Globals.hMainWnd, Caption);
}
/***********************************************************************
* DIALOG_StringMsgBox
*
* Format and show a message in a message box
*
* ARGUMENTS:
* - parent window:
* HWND hParent
* - format id:
* int formatId
* - message string:
* const char *String
* - flags for message box
* DWORD dwFlags
* RETURNS: none
*/
int DIALOG_StringMsgBox(HWND hParent, int formatId, const char *String, DWORD dwFlags)
{
char Message[MAX_STRING_LEN];
char Resource[MAX_STRING_LEN];
/* Load and format Message */
LoadString(Globals.hInstance, formatId, Resource, ARRAY_SIZE(Resource));
wnsprintf(Message, ARRAY_SIZE(Message), Resource, String);
/* Load Caption */
if ((dwFlags & MB_ICONMASK) == MB_ICONEXCLAMATION)
LoadString(Globals.hInstance, STRING_ERROR, Resource, ARRAY_SIZE(Resource));
else
LoadString(Globals.hInstance, STRING_NOTEPAD, Resource, ARRAY_SIZE(Resource));
/* Display Modal Dialog */
if (hParent == NULL)
hParent = Globals.hMainWnd;
return MessageBox(hParent, Message, Resource, dwFlags);
}
/***********************************************************************
* AlertFileNotFound
*
* Alert that file not found
*
* ARGUMENTS:
* - file name:
* const char *FileName
* RETURNS: none
*/
static void AlertFileNotFound(const char *FileName)
{
DIALOG_StringMsgBox(NULL, STRING_NOTFOUND, FileName, MB_ICONEXCLAMATION|MB_OK);
}
/***********************************************************************
* AlertFileNotSaved
*
* Alert that file not saved
*
* ARGUMENTS:
* - file name:
* const char *FileName
* RETURNS:
* (int) user choice made in message box
*/
static int AlertFileNotSaved(const char *FileName)
{
char Untitled[MAX_STRING_LEN];
LoadString(Globals.hInstance, STRING_UNTITLED, Untitled, ARRAY_SIZE(Untitled));
return DIALOG_StringMsgBox(NULL, STRING_NOTSAVED, FileName[0] ? FileName : Untitled,
MB_ICONQUESTION | MB_YESNOCANCEL);
}
/**
* Returns:
* true - if file exists
* false - if file does not exist
*/
bool FileExists(const char *Filename)
{
FILE *File;
File = fopen(Filename, "rb");
if (File == NULL)
return false;
fclose(File);
return true;
}
/***********************************************************************
* ResetScrollAndCaret
*
* Reset scroll and caret position
*
* ARGUMENTS: none
* RETURNS: none
*/
static void ResetScrollAndCaret(void)
{
// Reset scroll position
SetScrollPos(Globals.hMainWnd, SB_VERT, 0, true);
SetScrollPos(Globals.hMainWnd, SB_HORZ, 0, true);
// Reset caret position
Globals.CaretAbsLine = 0;
Globals.CaretAbsPos = 0;
Globals.CaretCurLine = 0;
Globals.CaretCurPos = 0;
}
typedef enum {
SAVED_OK, // File saved successfully
SAVE_FAILED // Something gone wrong
} SAVE_STATUS; // File save status
/* FileName is the filename to save under.
*
* If the function succeeds, it returns SAVED_OK.
* If the function fails, it returns SAVE_FAILED.
*/
static SAVE_STATUS DoSaveFile(const char *FileName)
{
FILE *outFile;
if ((outFile = fopen(FileName, "wb")) == NULL)
return SAVE_FAILED;
if (Globals.TextList.first == NULL) {
fclose(outFile);
return SAVED_OK;
}
for (TextItem *a = Globals.TextList.first; ; a = a->next) {
fwrite(a->str.data, sizeof(char), a->str.len, outFile);
if (a == Globals.TextList.last)
break;
// Write proper line break
if (Globals.EOL_type == EOL_CRLF)
fputc(CR, outFile);
fputc(LF, outFile);
}
fclose(outFile);
Globals.isModified = false;
return SAVED_OK;
}
/**
* Returns:
* true - User agreed to close (both save/don't save)
* false - User cancelled close by selecting "Cancel"
*/
bool DoCloseFile(void)
{
int Result;
static const char empty_str[] = { 0 };
if (Globals.isModified) {
/* prompt user to save changes */
Result = AlertFileNotSaved(Globals.FileName);
switch (Result) {
case IDYES: return DIALOG_FileSave();
case IDNO: break;
case IDCANCEL: return false;
default: return false;
}
}
EDIT_ClearTextList();
Globals.isModified = false;
SetFileName(empty_str);
UpdateWindowCaption();
return true;
}
/***********************************************************************
* DoOpenFile
*
* Open and load file
*
* ARGUMENTS:
* - file name:
* const char *FileName
* RETURNS: none
*/
void DoOpenFile(const char *FileName)
{
FILE *inFile;
//int size;
int curstrlen = 0;
/* Close any files and prompt to save changes */
if (!DoCloseFile())
return;
inFile = fopen(FileName, "rb");
if (inFile == NULL) {
AlertFileNotFound(FileName);
return;
}
// TODO -- let user know that file is
// too big/needs some time to be loaded
//fseek(inFile, 0, SEEK_END);
//size = ftell(inFile);
//if (size > MAX_FILE_SIZE)
//AlertFileTooBig();
//rewind(inFile);
Globals.TextList.LongestStringLength = 0;
while (1) {
int c = fgetc(inFile);
if (c == LF) {
Globals.EOL_type = EOL_LF;
fseek(inFile, -curstrlen - 1, SEEK_CUR);
EDIT_AddTextItem(inFile, curstrlen);
fgetc(inFile); // Skip LF
if (curstrlen > Globals.TextList.LongestStringLength)
Globals.TextList.LongestStringLength = curstrlen;
curstrlen = 0;
}
// Windows-like CRLF line ending
// TODO -- add some extra check; binary files handling??
else if (c == CR) {
Globals.EOL_type = EOL_CRLF;
fseek(inFile, -curstrlen - 1, SEEK_CUR);
EDIT_AddTextItem(inFile, curstrlen);
fgetc(inFile); // Skip CR
fgetc(inFile); // Skip LF
if (curstrlen > Globals.TextList.LongestStringLength)
Globals.TextList.LongestStringLength = curstrlen;
curstrlen = 0;
}
else if (c == EOF) {
fseek(inFile, -curstrlen, SEEK_CUR);
EDIT_AddTextItem(inFile, curstrlen);
if (curstrlen > Globals.TextList.LongestStringLength)
Globals.TextList.LongestStringLength = curstrlen;
break;
}
else
curstrlen++;
}
for (TextItem *a = Globals.TextList.first; ; a = a->next) {
a->drawnums = a->offsets = NULL;
if (a == Globals.TextList.last)
break;
}
EDIT_CountOffsets();
#ifdef DEBUG
for (TextItem *a = Globals.TextList.first; ; a = a->next) {
fputs(a->str.data, stdout);
fputc(LF, stdout);
if (a == Globals.TextList.last)
break;
}
#endif
Globals.isModified = false;
// Update window caption
SetFileName(FileName);
UpdateWindowCaption();
// Reset scroll and caret position
ResetScrollAndCaret();
SendMessage(Globals.hMainWnd, WM_KEYDOWN, 0, 0);
// Redraw window
SendMessage(Globals.hMainWnd, WM_SIZE, 0, MAKELONG(Globals.W, Globals.H));
}
/***********************************************************************
* DIALOG_FileNew
*
* New file dialog
*
* ARGUMENTS: none
* RETURNS: none
*/
void DIALOG_FileNew(void)
{
static const char empty_str[] = { 0 };
/* Close any files and prompt to save changes */
if (DoCloseFile()) {
// Remove current text list from memory (if exists)
EDIT_ClearTextList();
Globals.isModified = false;
// Reset window caption
SetWindowText(Globals.hMainWnd, empty_str);
UpdateWindowCaption();
// Reset scroll and caret position
ResetScrollAndCaret();
SendMessage(Globals.hMainWnd, WM_KEYDOWN, 0, 0);
// Redraw window
SendMessage(Globals.hMainWnd, WM_SIZE, 0, MAKELONG(Globals.W, Globals.H));
}
}
/***********************************************************************
* DIALOG_FileOpen
*
* Open file dialog
*
* ARGUMENTS: none
* RETURNS: none
*/
void DIALOG_FileOpen(void)
{
OPENFILENAME openfilename;
char Path[MAX_PATH];
static const char DefaultExt[] = { 't','x','t',0 };
static const char txt_files[] = { '*','.','t','x','t',0 };
ZeroMemory(&openfilename, sizeof(openfilename));
lstrcpy(Path, txt_files);
openfilename.lStructSize = sizeof(openfilename);
openfilename.hwndOwner = Globals.hMainWnd;
openfilename.hInstance = Globals.hInstance;
openfilename.lpstrFilter = Globals.Filter;
openfilename.lpstrFile = Path;
openfilename.nMaxFile = ARRAY_SIZE(Path);
openfilename.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST |
OFN_HIDEREADONLY | OFN_ENABLESIZING;
openfilename.lpstrDefExt = DefaultExt;
if (GetOpenFileName(&openfilename))
DoOpenFile(openfilename.lpstrFile);
}
/***********************************************************************
* DIALOG_FileSave
*
* Save file dialog
*
* ARGUMENTS: none
* RETURNS:
* (bool) false to cancel close of opened file, true otherwise
*/
bool DIALOG_FileSave(void)
{
if (Globals.FileName[0] == '\0')
return DIALOG_FileSaveAs();
else
{
switch (DoSaveFile(Globals.FileName))
{
case SAVED_OK: return true;
default: return false;
}
}
}
/***********************************************************************
* DIALOG_FileSaveAs
*
* Save file as dialog
*
* ARGUMENTS: none
* RETURNS:
* (bool) false to cancel close of opened file, true otherwise
*/
bool DIALOG_FileSaveAs(void)
{
OPENFILENAME saveas;
char Path[MAX_PATH];
static const char DefaultExt[] = { 't','x','t',0 };
static const char txt_files[] = { '*','.','t','x','t',0 };
ZeroMemory(&saveas, sizeof(saveas));
lstrcpy(Path, txt_files);
saveas.lStructSize = sizeof(OPENFILENAMEW);
saveas.hwndOwner = Globals.hMainWnd;
saveas.hInstance = Globals.hInstance;
saveas.lpstrFilter = Globals.Filter;
saveas.lpstrFile = Path;
saveas.nMaxFile = ARRAY_SIZE(Path);
saveas.Flags = OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT |
OFN_HIDEREADONLY | OFN_ENABLESIZING;
saveas.lpstrDefExt = DefaultExt;
if (!GetSaveFileName(&saveas))
return false;
switch (DoSaveFile(Path))
{
case SAVED_OK:
SetFileName(Path);
UpdateWindowCaption();
return true;
default:
return false;
}
}
/***********************************************************************
* DIALOG_FileExit
*
* Exit file dialog
*
* ARGUMENTS: none
* RETURNS: none
*/
void DIALOG_FileExit(void)
{
PostMessage(Globals.hMainWnd, WM_CLOSE, 0, 0l);
}
/***********************************************************************
* DIALOG_FileExit
*
* Toggle wrap mode dialog
*
* ARGUMENTS: none
* RETURNS: none
*/
void DIALOG_EditWrap(void)
{
Globals.isWrapLongLines = !Globals.isWrapLongLines;
CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
MF_BYCOMMAND | (Globals.isWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
// Redraw window
SendMessage(Globals.hMainWnd, WM_SIZE, 0, MAKELONG(Globals.W, Globals.H));
}