Skip to content

Commit

Permalink
Support message DSPELLCHECK_SETLANG_MSG via NPPM_MSGTOPLUGIN (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
Predelnik authored Mar 16, 2024
1 parent a8e891a commit 3bb42e4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 13 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand All @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions include/PluginMsg.h
Original file line number Diff line number Diff line change
@@ -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
};
43 changes: 38 additions & 5 deletions src/plugin/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -41,6 +42,7 @@
#include "ui/SelectMultipleLanguagesDialog.h"
#include "ui/SettingsDialog.h"
#include "ui/SuggestionMenuButton.h"
#include "PluginMsg.h"

#include <chrono>

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<int>(line.length()), line.data());
fclose(fp);
if (fp) {
_fwprintf_p(fp, L"%.*s\n", static_cast<int>(line.length()), line.data());
fclose(fp);
}
}

void delete_log() {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<DSpellCheckSetLangMsgInfo *>(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:
Expand All @@ -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<const CommunicationInfo *>(l_param))
return process_internal_msg(*info_ptr) ? TRUE : FALSE;
}
break;
}

return FALSE;
}

Expand Down
2 changes: 1 addition & 1 deletion src/spellers/HunspellInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/spellers/HunspellInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
2 changes: 1 addition & 1 deletion src/spellers/SpellerContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<void()> speller_status_changed;
Expand Down
5 changes: 2 additions & 3 deletions src/spellers/SpellerInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>
Expand All @@ -31,8 +31,7 @@ SpellerInterface::check_words(const std::vector<WordForSpeller> &words) const {

std::vector<LanguageInfo> 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<std::wstring> & /*langs*/) {
Expand Down

0 comments on commit 3bb42e4

Please sign in to comment.