From 425c2c642460fd68038a428392e8d2bfa3d71f36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20Nicolaysen=20S=C3=B8rnes?= Date: Wed, 18 Jul 2007 21:59:24 +0200 Subject: [PATCH] wordpad: Handle different formats on open/save. --- programs/wordpad/De.rc | 10 ++-- programs/wordpad/En.rc | 10 ++-- programs/wordpad/Fr.rc | 10 ++-- programs/wordpad/Hu.rc | 10 ++-- programs/wordpad/Ko.rc | 10 ++-- programs/wordpad/Nl.rc | 10 ++-- programs/wordpad/No.rc | 10 ++-- programs/wordpad/Pl.rc | 10 ++-- programs/wordpad/Ru.rc | 10 ++-- programs/wordpad/Tr.rc | 10 ++-- programs/wordpad/resource.h | 12 +++-- programs/wordpad/wordpad.c | 103 +++++++++++++++++++++++++++++++----- 12 files changed, 166 insertions(+), 49 deletions(-) diff --git a/programs/wordpad/De.rc b/programs/wordpad/De.rc index ab8f5488d3b..095beff29be 100644 --- a/programs/wordpad/De.rc +++ b/programs/wordpad/De.rc @@ -95,9 +95,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rich-Text-Format (*.rtf)" - STRING_TEXT_FILES_TXT, "Textdateien (*.txt)" - STRING_ALL_FILES, "Alle Dokumente (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rich-Text-Format (*.rtf)" + STRING_TEXT_FILES_TXT, "Textdateien (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "Alle Dokumente (*.*)" END STRINGTABLE DISCARDABLE @@ -106,4 +107,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/En.rc b/programs/wordpad/En.rc index 8f1b444f83c..11e186d5f54 100644 --- a/programs/wordpad/En.rc +++ b/programs/wordpad/En.rc @@ -95,9 +95,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" - STRING_TEXT_FILES_TXT, "Text files (*.txt)" - STRING_ALL_FILES, "All documents (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" + STRING_TEXT_FILES_TXT, "Text documents (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "All documents (*.*)" END STRINGTABLE DISCARDABLE @@ -106,4 +107,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/Fr.rc b/programs/wordpad/Fr.rc index 7944c08e156..2a30cd5a52a 100644 --- a/programs/wordpad/Fr.rc +++ b/programs/wordpad/Fr.rc @@ -95,9 +95,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" - STRING_TEXT_FILES_TXT, "Text files (*.txt)" - STRING_ALL_FILES, "All documents (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" + STRING_TEXT_FILES_TXT, "Text documents (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "All documents (*.*)" END STRINGTABLE DISCARDABLE @@ -106,4 +107,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/Hu.rc b/programs/wordpad/Hu.rc index 63190f41880..94089a82409 100644 --- a/programs/wordpad/Hu.rc +++ b/programs/wordpad/Hu.rc @@ -95,9 +95,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" - STRING_TEXT_FILES_TXT, "Text files (*.txt)" - STRING_ALL_FILES, "All documents (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" + STRING_TEXT_FILES_TXT, "Text documents (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "All documents (*.*)" END STRINGTABLE DISCARDABLE @@ -106,4 +107,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/Ko.rc b/programs/wordpad/Ko.rc index d7b4d73fd34..caafbcd860f 100644 --- a/programs/wordpad/Ko.rc +++ b/programs/wordpad/Ko.rc @@ -96,9 +96,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "¸®Ä¡ ÅؽºÆ® ÆÄÀÏÇü½Ä (*.rtf)" - STRING_TEXT_FILES_TXT, "ÅؽºÆ® ÆÄÀÏ (*.txt)" - STRING_ALL_FILES, "¸ðµç ¹®¼­ (*.*)" + STRING_RICHTEXT_FILES_RTF, "¸®Ä¡ ÅؽºÆ® ÆÄÀÏÇü½Ä (*.rtf)" + STRING_TEXT_FILES_TXT, "ÅؽºÆ® ÆÄÀÏ (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "¸ðµç ¹®¼­ (*.*)" END STRINGTABLE DISCARDABLE @@ -107,4 +108,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "'%s'ÀÇ ¹Ù²ï °ÍÀ» ÀúÀåÇÏ°Ú½À´Ï±î?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/Nl.rc b/programs/wordpad/Nl.rc index 8e5216c523c..b1858715eca 100644 --- a/programs/wordpad/Nl.rc +++ b/programs/wordpad/Nl.rc @@ -95,9 +95,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" - STRING_TEXT_FILES_TXT, "Text files (*.txt)" - STRING_ALL_FILES, "All documents (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" + STRING_TEXT_FILES_TXT, "Text documents (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "All documents (*.*)" END STRINGTABLE DISCARDABLE @@ -106,4 +107,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/No.rc b/programs/wordpad/No.rc index 30a6cfdfe60..5d511b8c079 100644 --- a/programs/wordpad/No.rc +++ b/programs/wordpad/No.rc @@ -95,9 +95,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rikt tekstformat (*.rtf)" - STRING_TEXT_FILES_TXT, "Tekstfiler (*.txt)" - STRING_ALL_FILES, "Alle filer (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rikt tekstformat (*.rtf)" + STRING_TEXT_FILES_TXT, "Tekstdokument (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode-tekstdokument (*.txt)" + STRING_ALL_FILES, "Alle filer (*.*)" END STRINGTABLE DISCARDABLE @@ -106,4 +107,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Lagre endringer i «%s»?" STRING_SEARCH_FINISHED, "Ferdig med å søke i dokumentet." STRING_LOAD_RICHED_FAILED, "Klarte ikke laste RichEdit-biblioteket." + STRING_SAVE_LOSEFORMATTING, "Du har valgt å lagre i rent tekstformat, noe som " \ + "vil føre til at all formatering går tapt. Er du " \ + "sikker på at du vil fortsette?" END diff --git a/programs/wordpad/Pl.rc b/programs/wordpad/Pl.rc index b7726446ba5..6d396394926 100644 --- a/programs/wordpad/Pl.rc +++ b/programs/wordpad/Pl.rc @@ -96,9 +96,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Tekst sformatowany - RTF (*.rtf)" - STRING_TEXT_FILES_TXT, "Dokument tekstowy (*.txt)" - STRING_ALL_FILES, "Wszystkie dokumenty (*.*)" + STRING_RICHTEXT_FILES_RTF, "Tekst sformatowany - RTF (*.rtf)" + STRING_TEXT_FILES_TXT, "Dokument tekstowy (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "Wszystkie dokumenty (*.*)" END STRINGTABLE DISCARDABLE @@ -107,4 +108,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/Ru.rc b/programs/wordpad/Ru.rc index 08d3188bfb3..e4df0ddbd69 100644 --- a/programs/wordpad/Ru.rc +++ b/programs/wordpad/Ru.rc @@ -93,9 +93,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" - STRING_TEXT_FILES_TXT, "Text files (*.txt)" - STRING_ALL_FILES, "All documents (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" + STRING_TEXT_FILES_TXT, "Text documents (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "All documents (*.*)" END STRINGTABLE DISCARDABLE @@ -104,4 +105,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/Tr.rc b/programs/wordpad/Tr.rc index cb4629897a2..f292a6d9262 100644 --- a/programs/wordpad/Tr.rc +++ b/programs/wordpad/Tr.rc @@ -97,9 +97,10 @@ END STRINGTABLE DISCARDABLE BEGIN - STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" - STRING_TEXT_FILES_TXT, "Text files (*.txt)" - STRING_ALL_FILES, "All documents (*.*)" + STRING_RICHTEXT_FILES_RTF, "Rich text format (*.rtf)" + STRING_TEXT_FILES_TXT, "Text documents (*.txt)" + STRING_TEXT_FILES_UNICODE_TXT, "Unicode text document (*.txt)" + STRING_ALL_FILES, "All documents (*.*)" END STRINGTABLE DISCARDABLE @@ -108,4 +109,7 @@ BEGIN STRING_PROMPT_SAVE_CHANGES, "Save changes to '%s'?" STRING_SEARCH_FINISHED, "Finished searching the document." STRING_LOAD_RICHED_FAILED, "Failed to load the RichEdit library." + STRING_SAVE_LOSEFORMATTING, "You have chosen to save in plain text format, " \ + "which will cause all formatting to be lost. " \ + "Are you sure that you wish to do this?" END diff --git a/programs/wordpad/resource.h b/programs/wordpad/resource.h index 82339b91884..1956d47b16e 100644 --- a/programs/wordpad/resource.h +++ b/programs/wordpad/resource.h @@ -90,9 +90,11 @@ #define STRING_ALL_FILES 1400 #define STRING_TEXT_FILES_TXT 1401 -#define STRING_RICHTEXT_FILES_RTF 1402 +#define STRING_TEXT_FILES_UNICODE_TXT 1402 +#define STRING_RICHTEXT_FILES_RTF 1403 -#define STRING_DEFAULT_FILENAME 1403 -#define STRING_PROMPT_SAVE_CHANGES 1404 -#define STRING_SEARCH_FINISHED 1405 -#define STRING_LOAD_RICHED_FAILED 1406 +#define STRING_DEFAULT_FILENAME 1700 +#define STRING_PROMPT_SAVE_CHANGES 1701 +#define STRING_SEARCH_FINISHED 1702 +#define STRING_LOAD_RICHED_FAILED 1703 +#define STRING_SAVE_LOSEFORMATTING 1704 diff --git a/programs/wordpad/wordpad.c b/programs/wordpad/wordpad.c index 91ea140993c..90daef2de3e 100644 --- a/programs/wordpad/wordpad.c +++ b/programs/wordpad/wordpad.c @@ -55,7 +55,7 @@ static HWND hFindWnd; static UINT ID_FINDMSGSTRING; -static WCHAR wszFilter[MAX_STRING_LEN]; +static WCHAR wszFilter[MAX_STRING_LEN*4+6*3+5]; static WCHAR wszDefaultFileName[MAX_STRING_LEN]; static WCHAR wszSaveChanges[MAX_STRING_LEN]; @@ -78,6 +78,10 @@ static void DoLoadStrings(void) p += lstrlenW(p) + 1; lstrcpyW(p, files_txt); p += lstrlenW(p) + 1; + LoadStringW(hInstance, STRING_TEXT_FILES_UNICODE_TXT, p, MAX_STRING_LEN); + p += lstrlenW(p) + 1; + lstrcpyW(p, files_txt); + p += lstrlenW(p) + 1; LoadStringW(hInstance, STRING_ALL_FILES, p, MAX_STRING_LEN); p += lstrlenW(p) + 1; lstrcpyW(p, files_all); @@ -165,6 +169,7 @@ static LPWSTR file_basename(LPWSTR path) } static WCHAR wszFileName[MAX_PATH]; +static WPARAM fileFormat = SF_RTF; static void set_caption(LPCWSTR wszNewFileName) { @@ -424,21 +429,63 @@ static void registry_set_filelist(LPCWSTR newFile) registry_read_filelist(hMainWnd); } +static int fileformat_number(WPARAM format) +{ + int number = 0; + + if(format == SF_TEXT) + { + number = 1; + } else if (format == (SF_TEXT | SF_UNICODE)) + { + number = 2; + } + return number; +} + +static WPARAM fileformat_flags(int format) +{ + WPARAM flags[] = { SF_RTF , SF_TEXT , SF_TEXT | SF_UNICODE }; + + return flags[format]; +} + +static void set_fileformat(WPARAM format) +{ + fileFormat = format; +} + static void DoOpenFile(LPCWSTR szOpenFileName) { HANDLE hFile; EDITSTREAM es; + char fileStart[5]; + DWORD readOut; + WPARAM format = SF_TEXT; hFile = CreateFileW(szOpenFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return; + ReadFile(hFile, fileStart, 5, &readOut, NULL); + SetFilePointer(hFile, 0, NULL, FILE_BEGIN); + + if(readOut >= 2 && (BYTE)fileStart[0] == 0xff && (BYTE)fileStart[1] == 0xfe) + { + format = SF_TEXT | SF_UNICODE; + SetFilePointer(hFile, 2, NULL, FILE_BEGIN); + } else if(readOut >= 5) + { + static const char header[] = "{\\rtf"; + if(!memcmp(header, fileStart, 5)) + format = SF_RTF; + } + es.dwCookie = (DWORD_PTR)hFile; es.pfnCallback = stream_in; - /* FIXME: Handle different file formats */ - SendMessageW(hEditorWnd, EM_STREAMIN, SF_RTF, (LPARAM)&es); + SendMessageW(hEditorWnd, EM_STREAMIN, format, (LPARAM)&es); CloseHandle(hFile); @@ -449,9 +496,10 @@ static void DoOpenFile(LPCWSTR szOpenFileName) lstrcpyW(wszFileName, szOpenFileName); SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0); registry_set_filelist(szOpenFileName); + set_fileformat(format); } -static void DoSaveFile(LPCWSTR wszSaveFileName) +static void DoSaveFile(LPCWSTR wszSaveFileName, WPARAM format) { HANDLE hFile; EDITSTREAM stream; @@ -463,11 +511,20 @@ static void DoSaveFile(LPCWSTR wszSaveFileName) if(hFile == INVALID_HANDLE_VALUE) return; + if(format == (SF_TEXT | SF_UNICODE)) + { + static const BYTE unicode[] = {0xff,0xfe}; + DWORD writeOut; + WriteFile(hFile, &unicode, sizeof(unicode), &writeOut, 0); + + if(writeOut != sizeof(unicode)) + return; + } + stream.dwCookie = (DWORD_PTR)hFile; stream.pfnCallback = stream_out; - /* FIXME: Handle different formats */ - ret = SendMessageW(hEditorWnd, EM_STREAMOUT, SF_RTF, (LPARAM)&stream); + ret = SendMessageW(hEditorWnd, EM_STREAMOUT, format, (LPARAM)&stream); CloseHandle(hFile); @@ -486,6 +543,7 @@ static void DoSaveFile(LPCWSTR wszSaveFileName) lstrcpyW(wszFileName, wszSaveFileName); set_caption(wszFileName); SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0); + set_fileformat(format); } static void DialogSaveFile(void) @@ -498,17 +556,33 @@ static void DialogSaveFile(void) ZeroMemory(&sfn, sizeof(sfn)); sfn.lStructSize = sizeof(sfn); - sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST; + sfn.Flags = OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT; sfn.hwndOwner = hMainWnd; sfn.lpstrFilter = wszFilter; sfn.lpstrFile = wszFile; sfn.nMaxFile = MAX_PATH; sfn.lpstrDefExt = wszDefExt; + sfn.nFilterIndex = fileformat_number(fileFormat)+1; - if(!GetSaveFileNameW(&sfn)) - return; - - DoSaveFile(sfn.lpstrFile); + while(GetSaveFileNameW(&sfn)) + { + if(fileformat_flags(sfn.nFilterIndex-1) != SF_RTF) + { + if(MessageBoxW(hMainWnd, MAKEINTRESOURCEW(STRING_SAVE_LOSEFORMATTING), + wszAppTitle, MB_YESNO | MB_ICONEXCLAMATION) != IDYES) + { + continue; + } else + { + DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1)); + break; + } + } else + { + DoSaveFile(sfn.lpstrFile, fileformat_flags(sfn.nFilterIndex-1)); + break; + } + } } static BOOL prompt_save_changes(void) @@ -555,7 +629,7 @@ static BOOL prompt_save_changes(void) case IDYES: if(wszFileName[0]) - DoSaveFile(wszFileName); + DoSaveFile(wszFileName, fileFormat); else DialogSaveFile(); return TRUE; @@ -582,6 +656,7 @@ static void DialogOpenFile(void) ofn.lpstrFile = wszFile; ofn.nMaxFile = MAX_PATH; ofn.lpstrDefExt = wszDefExt; + ofn.nFilterIndex = fileformat_number(fileFormat)+1; if(GetOpenFileNameW(&ofn)) { @@ -1068,7 +1143,7 @@ static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam) wszFileName[0] = '\0'; SetWindowTextW(hwndEditor, wszFileName); SendMessageW(hEditorWnd, EM_SETMODIFY, FALSE, 0); - /* FIXME: set default format too */ + fileFormat = SF_RTF; } break; @@ -1079,7 +1154,7 @@ static LRESULT OnCommand( HWND hWnd, WPARAM wParam, LPARAM lParam) case ID_FILE_SAVE: if(wszFileName[0]) { - DoSaveFile(wszFileName); + DoSaveFile(wszFileName, fileFormat); break; } /* Fall through */