wordpad: Prompt for saving file changes.

This commit is contained in:
Alexander Nicolaysen Sørnes 2007-07-08 13:51:19 +02:00 committed by Alexandre Julliard
parent 7a6fbf2b09
commit 77bf899160
12 changed files with 184 additions and 44 deletions

View File

@ -80,3 +80,9 @@ BEGIN
STRING_TEXT_FILES_TXT, "Textdateien (*.txt)" STRING_TEXT_FILES_TXT, "Textdateien (*.txt)"
STRING_ALL_FILES, "Alle Dokumente (*.*)" STRING_ALL_FILES, "Alle Dokumente (*.*)"
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -80,3 +80,9 @@ BEGIN
STRING_TEXT_FILES_TXT, "Text files (*.txt)" STRING_TEXT_FILES_TXT, "Text files (*.txt)"
STRING_ALL_FILES, "All documents (*.*)" STRING_ALL_FILES, "All documents (*.*)"
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -73,3 +73,9 @@ BEGIN
END END
END END
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -73,3 +73,9 @@ BEGIN
END END
END END
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -81,3 +81,9 @@ BEGIN
STRING_TEXT_FILES_TXT, "텍스트 파일 (*.txt)" STRING_TEXT_FILES_TXT, "텍스트 파일 (*.txt)"
STRING_ALL_FILES, "모둔 문서 (*.*)" STRING_ALL_FILES, "모둔 문서 (*.*)"
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -73,3 +73,9 @@ BEGIN
END END
END END
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -80,3 +80,9 @@ BEGIN
STRING_TEXT_FILES_TXT, "Tekstfiler (*.txt)" STRING_TEXT_FILES_TXT, "Tekstfiler (*.txt)"
STRING_ALL_FILES, "Alle filer (*.*)" STRING_ALL_FILES, "Alle filer (*.*)"
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Dokument"
STRING_PROMPT_SAVE_CHANGES, "Lagre endringer i «%s»?"
END

View File

@ -81,3 +81,9 @@ BEGIN
STRING_TEXT_FILES_TXT, "Dokument tekstowy (*.txt)" STRING_TEXT_FILES_TXT, "Dokument tekstowy (*.txt)"
STRING_ALL_FILES, "Wszystkie dokumenty (*.*)" STRING_ALL_FILES, "Wszystkie dokumenty (*.*)"
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -71,3 +71,9 @@ BEGIN
END END
END END
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -75,3 +75,9 @@ BEGIN
END END
END END
END END
STRINGTABLE DISCARDABLE
BEGIN
STRING_DEFAULT_FILENAME, "Document"
STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?"
END

View File

@ -74,3 +74,6 @@
#define STRING_ALL_FILES 1400 #define STRING_ALL_FILES 1400
#define STRING_TEXT_FILES_TXT 1401 #define STRING_TEXT_FILES_TXT 1401
#define STRING_RICHTEXT_FILES_RTF 1402 #define STRING_RICHTEXT_FILES_RTF 1402
#define STRING_DEFAULT_FILENAME 1403
#define STRING_PROMPT_SAVE_CHANGES 1404

View File

@ -47,6 +47,8 @@ static HWND hMainWnd;
static HWND hEditorWnd; static HWND hEditorWnd;
static WCHAR wszFilter[MAX_STRING_LEN]; static WCHAR wszFilter[MAX_STRING_LEN];
static WCHAR wszDefaultFileName[MAX_STRING_LEN];
static WCHAR wszSaveChanges[MAX_STRING_LEN];
static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam ); static LRESULT OnSize( HWND hWnd, WPARAM wParam, LPARAM lParam );
@ -72,6 +74,12 @@ static void DoLoadStrings(void)
lstrcpyW(p, files_all); lstrcpyW(p, files_all);
p += lstrlenW(p) + 1; p += lstrlenW(p) + 1;
*p = '\0'; *p = '\0';
p = wszDefaultFileName;
LoadStringW(hInstance, STRING_DEFAULT_FILENAME, p, MAX_STRING_LEN);
p = wszSaveChanges;
LoadStringW(hInstance, STRING_PROMPT_SAVE_CHANGES, p, MAX_STRING_LEN);
} }
static void AddButton(HWND hwndToolBar, int nImage, int nCommand) static void AddButton(HWND hwndToolBar, int nImage, int nCommand)
@ -136,31 +144,27 @@ static WCHAR wszFileName[MAX_PATH];
static void set_caption(LPCWSTR wszNewFileName) static void set_caption(LPCWSTR wszNewFileName)
{ {
static const WCHAR wszSeparator[] = {' ','-',' '}; static const WCHAR wszSeparator[] = {' ','-',' '};
WCHAR *wszCaption;
SIZE_T length = 0;
if(wszNewFileName) if(!wszNewFileName)
{ wszNewFileName = wszDefaultFileName;
WCHAR *wszCaption;
SIZE_T length = 0;
wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, wszCaption = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle)); lstrlenW(wszNewFileName)*sizeof(WCHAR)+sizeof(wszSeparator)+sizeof(wszAppTitle));
if(!wszCaption) if(!wszCaption)
return; return;
memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR)); memcpy(wszCaption, wszNewFileName, lstrlenW(wszNewFileName)*sizeof(WCHAR));
length += lstrlenW(wszNewFileName); length += lstrlenW(wszNewFileName);
memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator)); memcpy(wszCaption + length, wszSeparator, sizeof(wszSeparator));
length += sizeof(wszSeparator) / sizeof(WCHAR); length += sizeof(wszSeparator) / sizeof(WCHAR);
memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle)); memcpy(wszCaption + length, wszAppTitle, sizeof(wszAppTitle));
SetWindowTextW(hMainWnd, wszCaption); SetWindowTextW(hMainWnd, wszCaption);
HeapFree(GetProcessHeap(), 0, wszCaption); HeapFree(GetProcessHeap(), 0, wszCaption);
} else
{
SetWindowTextW(hMainWnd, wszAppTitle);
}
} }
static void DoOpenFile(LPCWSTR szOpenFileName) static void DoOpenFile(LPCWSTR szOpenFileName)
@ -186,27 +190,7 @@ static void DoOpenFile(LPCWSTR szOpenFileName)
set_caption(szOpenFileName); set_caption(szOpenFileName);
lstrcpyW(wszFileName, szOpenFileName); lstrcpyW(wszFileName, szOpenFileName);
} SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
static void DialogOpenFile(void)
{
OPENFILENAMEW ofn;
WCHAR wszFile[MAX_PATH] = {'\0'};
static const WCHAR wszDefExt[] = {'r','t','f','\0'};
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
ofn.hwndOwner = hMainWnd;
ofn.lpstrFilter = wszFilter;
ofn.lpstrFile = wszFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = wszDefExt;
if(GetOpenFileNameW(&ofn))
DoOpenFile(ofn.lpstrFile);
} }
static void DoSaveFile(LPCWSTR wszSaveFileName) static void DoSaveFile(LPCWSTR wszSaveFileName)
@ -236,6 +220,7 @@ static void DoSaveFile(LPCWSTR wszSaveFileName)
lstrcpyW(wszFileName, wszSaveFileName); lstrcpyW(wszFileName, wszSaveFileName);
set_caption(wszFileName); set_caption(wszFileName);
SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
} }
static void DialogSaveFile(void) static void DialogSaveFile(void)
@ -261,6 +246,86 @@ static void DialogSaveFile(void)
DoSaveFile(sfn.lpstrFile); DoSaveFile(sfn.lpstrFile);
} }
static BOOL prompt_save_changes(void)
{
if(!wszFileName[0])
{
GETTEXTLENGTHEX gt;
gt.flags = GTL_NUMCHARS;
gt.codepage = 1200;
if(!SendMessageW(hEditorWnd, EM_GETTEXTLENGTHEX, (WPARAM)&gt, 0))
return TRUE;
}
if(!SendMessageW(hEditorWnd, EM_GETMODIFY, 0, 0))
{
return TRUE;
} else
{
LPWSTR displayFileName;
WCHAR *text;
int ret;
if(!wszFileName[0])
displayFileName = wszDefaultFileName;
else
displayFileName = wszFileName;
text = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
(lstrlenW(displayFileName)+lstrlenW(wszSaveChanges))*sizeof(WCHAR));
if(!text)
return FALSE;
wsprintfW(text, wszSaveChanges, displayFileName);
ret = MessageBoxW(hMainWnd, text, wszAppTitle, MB_YESNOCANCEL | MB_ICONEXCLAMATION);
HeapFree(GetProcessHeap(), 0, text);
switch(ret)
{
case IDNO:
return TRUE;
break;
case IDYES:
if(wszFileName[0])
DoSaveFile(wszFileName);
else
DialogSaveFile();
return TRUE;
default:
return FALSE;
}
}
}
static void DialogOpenFile(void)
{
OPENFILENAMEW ofn;
WCHAR wszFile[MAX_PATH] = {'\0'};
static const WCHAR wszDefExt[] = {'r','t','f','\0'};
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
ofn.hwndOwner = hMainWnd;
ofn.lpstrFilter = wszFilter;
ofn.lpstrFile = wszFile;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrDefExt = wszDefExt;
if(GetOpenFileNameW(&ofn))
{
prompt_save_changes();
DoOpenFile(ofn.lpstrFile);
}
}
static void HandleCommandLine(LPWSTR cmdline) static void HandleCommandLine(LPWSTR cmdline)
{ {
WCHAR delimiter; WCHAR delimiter;
@ -473,6 +538,7 @@ static LRESULT OnCreate( HWND hWnd, WPARAM wParam, LPARAM lParam)
DoDefaultFont(); DoDefaultFont();
DoLoadStrings(); DoLoadStrings();
SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
return 0; return 0;
} }
@ -559,10 +625,14 @@ static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam)
break; break;
case ID_FILE_NEW: case ID_FILE_NEW:
set_caption(NULL); if(prompt_save_changes())
wszFileName[0] = '\0'; {
SetWindowTextW(hwndEditor, wszFileName); set_caption(NULL);
/* FIXME: set default format too */ wszFileName[0] = '\0';
SetWindowTextW(hwndEditor, wszFileName);
SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0);
/* FIXME: set default format too */
}
break; break;
case ID_FILE_OPEN: case ID_FILE_OPEN:
@ -887,6 +957,11 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lPara
PostQuitMessage(0); PostQuitMessage(0);
break; break;
case WM_CLOSE:
if(prompt_save_changes())
PostQuitMessage(0);
break;
case WM_ACTIVATE: case WM_ACTIVATE:
if (LOWORD(wParam)) if (LOWORD(wParam))
SetFocus(GetDlgItem(hWnd, IDC_EDITOR)); SetFocus(GetDlgItem(hWnd, IDC_EDITOR));
@ -934,6 +1009,8 @@ int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hOldInstance, LPSTR szCmdPar
CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL); CW_USEDEFAULT, CW_USEDEFAULT, 680, 260, NULL, NULL, hInstance, NULL);
ShowWindow(hMainWnd, SW_SHOWDEFAULT); ShowWindow(hMainWnd, SW_SHOWDEFAULT);
set_caption(NULL);
HandleCommandLine(GetCommandLineW()); HandleCommandLine(GetCommandLineW());
while(GetMessageW(&msg,0,0,0)) while(GetMessageW(&msg,0,0,0))