From 9ad574ddf7053794e11300b4735e5fc213add735 Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Fri, 19 Apr 2024 10:21:44 +0900 Subject: [PATCH] improve --- Dictionary.cpp | 41 +++++++++++++++++++++++++++++++++-------- XG_HiddenDialog.cpp | 9 --------- lang/en_US.rc | 2 ++ lang/ja_JP.rc | 2 ++ resource.h | 2 ++ 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Dictionary.cpp b/Dictionary.cpp index e4202fb..cd2c6bc 100644 --- a/Dictionary.cpp +++ b/Dictionary.cpp @@ -588,13 +588,14 @@ void XgSelectDict(HWND hwnd, size_t iDict) // カギを使って辞書を更新する。 BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name) { - // カギがなければ失敗。 - if (!xg_bSolved || xg_vecHorzHints.empty() || xg_vecVertHints.empty()) - return FALSE; + WCHAR szText[MAX_PATH]; - // 辞書名がなければ失敗。 - if (dict_name.empty()) + // カギがなければ失敗。 + if (!xg_bSolved || xg_vecHorzHints.empty() || xg_vecVertHints.empty()) { + XgCenterMessageBoxW(hwnd, XgLoadStringDx1(IDS_NOCHANGE), + XgLoadStringDx2(IDS_APPNAME), MB_ICONINFORMATION); return FALSE; + } // 単語からヒントへの写像を作成する。 std::map word_to_hint_map; @@ -607,8 +608,10 @@ BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name) // 辞書ファイルをすべて読み込む。 XGStringW str; - if (!XgReadTextFileAll(dict_name.c_str(), str)) + if (dict_name.empty() || !XgReadTextFileAll(dict_name.c_str(), str)) { + XgCenterMessageBoxW(hwnd, XgLoadStringDx1(IDS_NODICTSELECTED), NULL, MB_ICONERROR); return FALSE; + } // 改行コードを正規化。 xg_str_replace_all(str, L"\r\n", L"\n"); @@ -618,6 +621,7 @@ BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name) mstr_split(lines, str, L"\n"); // 一行ずつ処理する。 + BOOL bNoChange = TRUE; for (auto& line : lines) { if (line[0] == L'#') continue; @@ -636,7 +640,11 @@ BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name) if (it != word_to_hint_map.end()) { fields[1] = it->second; word_to_hint_map.erase(it); - line = mstr_join(fields, L"\t"); + auto strNew = mstr_join(fields, L"\t"); + if (line != strNew) { + line = strNew; + bNoChange = FALSE; + } } } @@ -646,12 +654,24 @@ BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name) line += pair.second; line += L'\n'; lines.push_back(line); + bNoChange = FALSE; + } + + if (bNoChange) { + XgCenterMessageBoxW(hwnd, XgLoadStringDx1(IDS_NOCHANGE), + XgLoadStringDx2(IDS_APPNAME), MB_ICONINFORMATION); + return FALSE; } // 辞書ファイルに書き込む。 FILE *fp = _wfopen(dict_name.c_str(), L"w"); - if (!fp) + if (!fp) { + // 失敗メッセージ。 + StringCchPrintfW(szText, _countof(szText), XgLoadStringDx1(IDS_UPDATEDICTFAIL), + PathFindFileNameW(xg_dict_name.c_str())); + XgCenterMessageBoxW(hwnd, szText, nullptr, MB_ICONERROR); return FALSE; + } fprintf(fp, "\xEF\xBB\xBF"); // UTF-8 BOM @@ -664,5 +684,10 @@ BOOL XgUpdateDictionaryUsingClues(HWND hwnd, const XGStringW& dict_name) fclose(fp); + // 成功メッセージ。 + StringCchPrintfW(szText, _countof(szText), XgLoadStringDx1(IDS_UPDATEDICTOK), + PathFindFileNameW(xg_dict_name.c_str())); + XgCenterMessageBoxW(hwnd, szText, XgLoadStringDx2(IDS_APPNAME), MB_ICONINFORMATION); + return TRUE; } diff --git a/XG_HiddenDialog.cpp b/XG_HiddenDialog.cpp index 9d015f2..fa1b0a7 100644 --- a/XG_HiddenDialog.cpp +++ b/XG_HiddenDialog.cpp @@ -58,19 +58,10 @@ class XG_HiddenDialog break; case psh4: // カギを使って辞書を更新する。 if (codeNotify == BN_CLICKED) { - WCHAR szText[MAX_PATH * 2]; HCURSOR hOldCursor = ::SetCursor(::LoadCursor(NULL, IDC_WAIT)); XG_HintsWnd::UpdateHintData(); // ヒントに変更があれば、更新する。 if (XgUpdateDictionaryUsingClues(hwnd, xg_dict_name)) { - // 成功メッセージ。 - StringCchPrintfW(szText, _countof(szText), XgLoadStringDx1(IDS_UPDATEDICTOK), - PathFindFileNameW(xg_dict_name.c_str())); - XgCenterMessageBoxW(hwnd, szText, XgLoadStringDx2(IDS_APPNAME), MB_ICONINFORMATION); } else { - // 失敗メッセージ。 - StringCchPrintfW(szText, _countof(szText), XgLoadStringDx1(IDS_UPDATEDICTFAIL), - PathFindFileNameW(xg_dict_name.c_str())); - XgCenterMessageBoxW(hwnd, szText, nullptr, MB_ICONERROR); } ::SetCursor(hOldCursor); } diff --git a/lang/en_US.rc b/lang/en_US.rc index d60f7a5..8cff38f 100644 --- a/lang/en_US.rc +++ b/lang/en_US.rc @@ -1831,6 +1831,8 @@ STRINGTABLE IDS_UPDATEDTIME, "Updated Time" IDS_UPDATEDICTOK, "Successfully wrote to dictionary file ""%s""." IDS_UPDATEDICTFAIL, "Failed to write to dictionary file ""%s"". The folder may not be writable." + IDS_NOCHANGE, "There is no change." + IDS_NODICTSELECTED, "No dictionary selected, or unable to load the dictionary." IDS_TT_NEW, "New crossword" IDS_TT_GENERATE, "Generate crossword" IDS_TT_OPEN, "Open crossword" diff --git a/lang/ja_JP.rc b/lang/ja_JP.rc index 3e54ce4..778d76a 100644 --- a/lang/ja_JP.rc +++ b/lang/ja_JP.rc @@ -1835,6 +1835,8 @@ STRINGTABLE IDS_UPDATEDTIME, "更新日時" IDS_UPDATEDICTOK, "辞書ファイル「%s」への書き込みに成功しました。" IDS_UPDATEDICTFAIL, "辞書ファイル「%s」への書き込みに失敗しました。書き込めないフォルダの可能性があります。" + IDS_NOCHANGE, "変更点はありません。" + IDS_NODICTSELECTED, "辞書が選択されていないか、読み込めません。" IDS_TT_NEW, "新規作成" IDS_TT_GENERATE, "問題を自動生成する" IDS_TT_OPEN, "問題を開く" diff --git a/resource.h b/resource.h index 2c35e98..32672ab 100644 --- a/resource.h +++ b/resource.h @@ -257,6 +257,8 @@ #define IDS_UPDATEDTIME 334 #define IDS_UPDATEDICTOK 335 #define IDS_UPDATEDICTFAIL 336 +#define IDS_NOCHANGE 337 +#define IDS_NODICTSELECTED 338 #define IDS_TT_NEW 10100 #define IDS_TT_GENERATE 10101 #define IDS_TT_OPEN 10102