From 3bb42e45363a66ff0a7eb9879b73a6e5fbe2799d Mon Sep 17 00:00:00 2001 From: Sergey Semushin Date: Sat, 16 Mar 2024 18:54:40 +0100 Subject: [PATCH] Support message DSPELLCHECK_SETLANG_MSG via NPPM_MSGTOPLUGIN (#336) --- CMakeLists.txt | 4 +-- include/PluginMsg.h | 9 +++++++ src/plugin/Plugin.cpp | 43 ++++++++++++++++++++++++++---- src/spellers/HunspellInterface.cpp | 2 +- src/spellers/HunspellInterface.h | 2 +- src/spellers/SpellerContainer.h | 2 +- src/spellers/SpellerInterface.cpp | 5 ++-- 7 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 include/PluginMsg.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7c4ae73..502dc21 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,7 @@ if(MSVC) set(DSpellCheck_USE_VLD "OFF" CACHE BOOL "Use Visual Leak Detector") endif() -file (GLOB_RECURSE source_files src/**/*.cpp src/**/*.h) +file (GLOB_RECURSE source_files src/**/*.cpp src/**/*.h include/**/*.h) list(REMOVE_ITEM source_files src/plugin/DllMain.cpp) add_library (DSpellCheckStatic STATIC ${source_files}) @@ -39,7 +39,7 @@ if ( DSpellCheck_USE_VLD ) target_compile_definitions (DSpellCheckStatic PUBLIC VLD_BUILD) endif () -target_include_directories (DSpellCheckStatic PUBLIC src deps/win-iconv) +target_include_directories (DSpellCheckStatic PUBLIC src deps/win-iconv include) target_include_directories (DSpellCheckStatic PRIVATE src/Controls) target_link_libraries (DSpellCheckStatic hunspell minizip ftpclient iconv-static Controls npp_extra lsignal aspell json) diff --git a/include/PluginMsg.h b/include/PluginMsg.h new file mode 100644 index 0000000..1c627c9 --- /dev/null +++ b/include/PluginMsg.h @@ -0,0 +1,9 @@ +#pragma once + +#define DSPELLCHECK_SETLANG_MSG 1 + +struct DSpellCheckSetLangMsgInfo +{ + const wchar_t *lang_name; + bool *was_success; // optional out param, pointed bool set to true if language was switched +}; diff --git a/src/plugin/Plugin.cpp b/src/plugin/Plugin.cpp index 3710e4b..c3dfa61 100644 --- a/src/plugin/Plugin.cpp +++ b/src/plugin/Plugin.cpp @@ -30,6 +30,7 @@ #include "npp/NppInterface.h" #include "spellers/HunspellInterface.h" #include "spellers/SpellerContainer.h" +#include "spellers/LanguageInfo.h" #include "ui/AboutDialog.h" #include "ui/AspellOptionsDialog.h" #include "ui/ConnectionSettingsDialog.h" @@ -41,6 +42,7 @@ #include "ui/SelectMultipleLanguagesDialog.h" #include "ui/SettingsDialog.h" #include "ui/SuggestionMenuButton.h" +#include "PluginMsg.h" #include @@ -206,6 +208,9 @@ void copy_misspellings_to_clipboard() { auto str = spell_checker->get_all_misspellings_as_string(); const size_t len = (str.length() + 1) * 2; HGLOBAL h_mem = GlobalAlloc(GMEM_MOVEABLE, len); + if (!h_mem) { + return; + } memcpy(GlobalLock(h_mem), str.c_str(), len); GlobalUnlock(h_mem); OpenClipboard(nullptr); @@ -710,8 +715,10 @@ void print_to_log(std::wstring_view line, HWND parent_wnd) { MessageBox(parent_wnd, L"Error while writing to a log file", to_wstring(strerror(err)).c_str(), MB_OK); return; } - _fwprintf_p(fp, L"%.*s\n", static_cast(line.length()), line.data()); - fclose(fp); + if (fp) { + _fwprintf_p(fp, L"%.*s\n", static_cast(line.length()), line.data()); + fclose(fp); + } } void delete_log() { @@ -815,8 +822,8 @@ extern "C" __declspec(dllexport) void beNotified(SCNotification *notify_code) { if (settings) print_to_log(L"NPPN_TBMODIFICATION", npp->get_editor_hwnd()); add_icons(); + break; } - default: return; } @@ -857,9 +864,31 @@ void init_needed_dialogs(WPARAM w_param) { } } +bool process_internal_msg(const CommunicationInfo& communication_info) { + switch (communication_info.internalMsg) { + case DSPELLCHECK_SETLANG_MSG: { + if (const auto info = reinterpret_cast(communication_info.info)) { + const auto lang_list = speller_container->active_speller().get_language_list(); + const auto exists = + std::ranges::find(lang_list, info->lang_name, &LanguageInfo::orig_name) != lang_list.end(); + if (exists) { + auto mut = settings->modify(); + mut->get_active_language() = info->lang_name; + } + if (info->was_success) { + *info->was_success = exists; + } + return true; + } + } + break; + } + return false; +} + extern "C" __declspec(dllexport) LRESULT // ReSharper disable once CppInconsistentNaming -messageProc(UINT message, WPARAM w_param, LPARAM /*l_param*/) { +messageProc(UINT message, WPARAM w_param, LPARAM l_param) { // NOLINT switch (message) { case WM_MOVE: @@ -871,10 +900,14 @@ messageProc(UINT message, WPARAM w_param, LPARAM /*l_param*/) { init_needed_dialogs(w_param); context_menu_handler->process_menu_result(w_param); } + return FALSE; + } + case NPPM_MSGTOPLUGIN: { + if (const auto info_ptr = reinterpret_cast(l_param)) + return process_internal_msg(*info_ptr) ? TRUE : FALSE; } break; } - return FALSE; } diff --git a/src/spellers/HunspellInterface.cpp b/src/spellers/HunspellInterface.cpp index 3baa500..3485f59 100644 --- a/src/spellers/HunspellInterface.cpp +++ b/src/spellers/HunspellInterface.cpp @@ -171,7 +171,7 @@ void HunspellInterface::set_use_one_dic(bool value) { m_use_one_dic = value; } bool are_paths_equal(const wchar_t *path1, const wchar_t *path2) { BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2; - HANDLE h2; + HANDLE h2 = nullptr; DWORD access = 0; DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE; diff --git a/src/spellers/HunspellInterface.h b/src/spellers/HunspellInterface.h index 43eb0fe..3f48b5d 100644 --- a/src/spellers/HunspellInterface.h +++ b/src/spellers/HunspellInterface.h @@ -64,7 +64,7 @@ class DicInfo { class AvailableLangInfo { public: std::wstring name; - int type; // Type = 1 - System Dir Dictionary, 0 - Nomal Dictionary + int type = 0; // Type = 1 - System Dir Dictionary, 0 - Nomal Dictionary std::wstring full_path; bool operator<(const AvailableLangInfo &rhs) const { return name < rhs.name; } diff --git a/src/spellers/SpellerContainer.h b/src/spellers/SpellerContainer.h index 15cd85e..937f153 100644 --- a/src/spellers/SpellerContainer.h +++ b/src/spellers/SpellerContainer.h @@ -57,7 +57,7 @@ class SpellerContainer { std::wstring get_aspell_default_personal_dictionary_path() const; void cleanup(); void ignore_word(std::wstring wstr); - void add_to_dictionary(std::wstring wstr);; + void add_to_dictionary(std::wstring wstr); public: mutable lsignal::signal speller_status_changed; diff --git a/src/spellers/SpellerInterface.cpp b/src/spellers/SpellerInterface.cpp index d3a7a47..8d8f292 100644 --- a/src/spellers/SpellerInterface.cpp +++ b/src/spellers/SpellerInterface.cpp @@ -17,7 +17,7 @@ #include "LanguageInfo.h" bool SpellerInterface::check_word(const WordForSpeller &word) const { - return check_words({std::move(word)}).front(); + return check_words({word}).front(); } std::vector @@ -31,8 +31,7 @@ SpellerInterface::check_words(const std::vector &words) const { std::vector DummySpeller::get_language_list() const { return {}; } -void DummySpeller::set_language(const wchar_t * /*lang*/) { -} +void DummySpeller::set_language(const wchar_t * /*lang*/) {} void DummySpeller::set_multiple_languages( const std::vector & /*langs*/) {