Skip to content

Commit 3bb42e4

Browse files
authored
Support message DSPELLCHECK_SETLANG_MSG via NPPM_MSGTOPLUGIN (#336)
1 parent a8e891a commit 3bb42e4

File tree

7 files changed

+54
-13
lines changed

7 files changed

+54
-13
lines changed

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ if(MSVC)
2929
set(DSpellCheck_USE_VLD "OFF" CACHE BOOL "Use Visual Leak Detector")
3030
endif()
3131

32-
file (GLOB_RECURSE source_files src/**/*.cpp src/**/*.h)
32+
file (GLOB_RECURSE source_files src/**/*.cpp src/**/*.h include/**/*.h)
3333
list(REMOVE_ITEM source_files src/plugin/DllMain.cpp)
3434

3535
add_library (DSpellCheckStatic STATIC ${source_files})
@@ -39,7 +39,7 @@ if ( DSpellCheck_USE_VLD )
3939
target_compile_definitions (DSpellCheckStatic PUBLIC VLD_BUILD)
4040
endif ()
4141

42-
target_include_directories (DSpellCheckStatic PUBLIC src deps/win-iconv)
42+
target_include_directories (DSpellCheckStatic PUBLIC src deps/win-iconv include)
4343
target_include_directories (DSpellCheckStatic PRIVATE src/Controls)
4444

4545
target_link_libraries (DSpellCheckStatic hunspell minizip ftpclient iconv-static Controls npp_extra lsignal aspell json)

include/PluginMsg.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
#define DSPELLCHECK_SETLANG_MSG 1
4+
5+
struct DSpellCheckSetLangMsgInfo
6+
{
7+
const wchar_t *lang_name;
8+
bool *was_success; // optional out param, pointed bool set to true if language was switched
9+
};

src/plugin/Plugin.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "npp/NppInterface.h"
3131
#include "spellers/HunspellInterface.h"
3232
#include "spellers/SpellerContainer.h"
33+
#include "spellers/LanguageInfo.h"
3334
#include "ui/AboutDialog.h"
3435
#include "ui/AspellOptionsDialog.h"
3536
#include "ui/ConnectionSettingsDialog.h"
@@ -41,6 +42,7 @@
4142
#include "ui/SelectMultipleLanguagesDialog.h"
4243
#include "ui/SettingsDialog.h"
4344
#include "ui/SuggestionMenuButton.h"
45+
#include "PluginMsg.h"
4446

4547
#include <chrono>
4648

@@ -206,6 +208,9 @@ void copy_misspellings_to_clipboard() {
206208
auto str = spell_checker->get_all_misspellings_as_string();
207209
const size_t len = (str.length() + 1) * 2;
208210
HGLOBAL h_mem = GlobalAlloc(GMEM_MOVEABLE, len);
211+
if (!h_mem) {
212+
return;
213+
}
209214
memcpy(GlobalLock(h_mem), str.c_str(), len);
210215
GlobalUnlock(h_mem);
211216
OpenClipboard(nullptr);
@@ -710,8 +715,10 @@ void print_to_log(std::wstring_view line, HWND parent_wnd) {
710715
MessageBox(parent_wnd, L"Error while writing to a log file", to_wstring(strerror(err)).c_str(), MB_OK);
711716
return;
712717
}
713-
_fwprintf_p(fp, L"%.*s\n", static_cast<int>(line.length()), line.data());
714-
fclose(fp);
718+
if (fp) {
719+
_fwprintf_p(fp, L"%.*s\n", static_cast<int>(line.length()), line.data());
720+
fclose(fp);
721+
}
715722
}
716723

717724
void delete_log() {
@@ -815,8 +822,8 @@ extern "C" __declspec(dllexport) void beNotified(SCNotification *notify_code) {
815822
if (settings)
816823
print_to_log(L"NPPN_TBMODIFICATION", npp->get_editor_hwnd());
817824
add_icons();
825+
break;
818826
}
819-
820827
default:
821828
return;
822829
}
@@ -857,9 +864,31 @@ void init_needed_dialogs(WPARAM w_param) {
857864
}
858865
}
859866

867+
bool process_internal_msg(const CommunicationInfo& communication_info) {
868+
switch (communication_info.internalMsg) {
869+
case DSPELLCHECK_SETLANG_MSG: {
870+
if (const auto info = reinterpret_cast<DSpellCheckSetLangMsgInfo *>(communication_info.info)) {
871+
const auto lang_list = speller_container->active_speller().get_language_list();
872+
const auto exists =
873+
std::ranges::find(lang_list, info->lang_name, &LanguageInfo::orig_name) != lang_list.end();
874+
if (exists) {
875+
auto mut = settings->modify();
876+
mut->get_active_language() = info->lang_name;
877+
}
878+
if (info->was_success) {
879+
*info->was_success = exists;
880+
}
881+
return true;
882+
}
883+
}
884+
break;
885+
}
886+
return false;
887+
}
888+
860889
extern "C" __declspec(dllexport) LRESULT
861890
// ReSharper disable once CppInconsistentNaming
862-
messageProc(UINT message, WPARAM w_param, LPARAM /*l_param*/) {
891+
messageProc(UINT message, WPARAM w_param, LPARAM l_param) {
863892
// NOLINT
864893
switch (message) {
865894
case WM_MOVE:
@@ -871,10 +900,14 @@ messageProc(UINT message, WPARAM w_param, LPARAM /*l_param*/) {
871900
init_needed_dialogs(w_param);
872901
context_menu_handler->process_menu_result(w_param);
873902
}
903+
return FALSE;
904+
}
905+
case NPPM_MSGTOPLUGIN: {
906+
if (const auto info_ptr = reinterpret_cast<const CommunicationInfo *>(l_param))
907+
return process_internal_msg(*info_ptr) ? TRUE : FALSE;
874908
}
875909
break;
876910
}
877-
878911
return FALSE;
879912
}
880913

src/spellers/HunspellInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ void HunspellInterface::set_use_one_dic(bool value) { m_use_one_dic = value; }
171171

172172
bool are_paths_equal(const wchar_t *path1, const wchar_t *path2) {
173173
BY_HANDLE_FILE_INFORMATION bhfi1, bhfi2;
174-
HANDLE h2;
174+
HANDLE h2 = nullptr;
175175
DWORD access = 0;
176176
DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
177177

src/spellers/HunspellInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class DicInfo {
6464
class AvailableLangInfo {
6565
public:
6666
std::wstring name;
67-
int type; // Type = 1 - System Dir Dictionary, 0 - Nomal Dictionary
67+
int type = 0; // Type = 1 - System Dir Dictionary, 0 - Nomal Dictionary
6868
std::wstring full_path;
6969

7070
bool operator<(const AvailableLangInfo &rhs) const { return name < rhs.name; }

src/spellers/SpellerContainer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class SpellerContainer {
5757
std::wstring get_aspell_default_personal_dictionary_path() const;
5858
void cleanup();
5959
void ignore_word(std::wstring wstr);
60-
void add_to_dictionary(std::wstring wstr);;
60+
void add_to_dictionary(std::wstring wstr);
6161

6262
public:
6363
mutable lsignal::signal<void()> speller_status_changed;

src/spellers/SpellerInterface.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "LanguageInfo.h"
1818

1919
bool SpellerInterface::check_word(const WordForSpeller &word) const {
20-
return check_words({std::move(word)}).front();
20+
return check_words({word}).front();
2121
}
2222

2323
std::vector<bool>
@@ -31,8 +31,7 @@ SpellerInterface::check_words(const std::vector<WordForSpeller> &words) const {
3131

3232
std::vector<LanguageInfo> DummySpeller::get_language_list() const { return {}; }
3333

34-
void DummySpeller::set_language(const wchar_t * /*lang*/) {
35-
}
34+
void DummySpeller::set_language(const wchar_t * /*lang*/) {}
3635

3736
void DummySpeller::set_multiple_languages(
3837
const std::vector<std::wstring> & /*langs*/) {

0 commit comments

Comments
 (0)