From 507e0bc1e682dddfaf929fe37b80be35f8396ede Mon Sep 17 00:00:00 2001 From: Akash Mozumdar Date: Fri, 21 Dec 2018 15:11:12 -0500 Subject: [PATCH] add extra window to view text --- extensions/CMakeLists.txt | 2 ++ extensions/extrawindow.cpp | 66 ++++++++++++++++++++++++++++++++++++++ extensions/regexfilter.cpp | 4 +-- include/text.h | 1 + 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 extensions/extrawindow.cpp diff --git a/extensions/CMakeLists.txt b/extensions/CMakeLists.txt index 440cd72..aacfaa8 100644 --- a/extensions/CMakeLists.txt +++ b/extensions/CMakeLists.txt @@ -7,10 +7,12 @@ cmake_policy(SET CMP0037 OLD) add_library(Bing\ Translate SHARED bingtranslate.cpp extensionimpl.cpp) add_library(Copy\ to\ Clipboard SHARED copyclipboard.cpp extensionimpl.cpp) add_library(Extra\ Newlines SHARED extranewlines.cpp extensionimpl.cpp) +add_library(Extra\ Window SHARED extrawindow.cpp extensionimpl.cpp) add_library(Google\ Translate SHARED googletranslate.cpp extensionimpl.cpp) add_library(Regex\ Filter SHARED regexfilter.cpp extensionimpl.cpp) add_library(Remove\ Repetition SHARED removerepeat.cpp extensionimpl.cpp) target_link_libraries(Bing\ Translate winhttp Qt5::Widgets) +target_link_libraries(Extra\ Window Qt5::Widgets) target_link_libraries(Google\ Translate winhttp Qt5::Widgets) target_link_libraries(Regex\ Filter Qt5::Widgets) diff --git a/extensions/extrawindow.cpp b/extensions/extrawindow.cpp new file mode 100644 index 0000000..3090884 --- /dev/null +++ b/extensions/extrawindow.cpp @@ -0,0 +1,66 @@ +#include "extension.h" +#include "text.h" +#include +#include +#include +#include +#include + +std::mutex m; + +struct : QMainWindow +{ + QLabel* display; + void launch() + { + auto centralWidget = new QWidget(this); + auto layout = new QVBoxLayout(centralWidget); + auto options = new QHBoxLayout(centralWidget); + layout->addItem(options); + layout->addWidget(display = new QLabel(centralWidget)); + auto onTop = new QCheckBox(ALWAYS_ON_TOP, this); + options->addWidget(onTop); + connect(onTop, &QCheckBox::stateChanged, [this](int state) + { + SetWindowPos((HWND)winId(), state == Qt::Checked ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE); + }); + resize(800, 600); + setCentralWidget(centralWidget); + show(); + } +}*window = nullptr; + +BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) +{ + switch (ul_reason_for_call) + { + case DLL_PROCESS_ATTACH: + { + QTimer::singleShot(0, [] + { + std::lock_guard l(m); + (window = new std::remove_pointer_t)->launch(); + }); + } + break; + case DLL_PROCESS_DETACH: + { + if (lpReserved == NULL) // https://blogs.msdn.microsoft.com/oldnewthing/20120105-00/?p=8683 + { + std::lock_guard l(m); + delete window; + window = nullptr; + } + } + break; + } + return TRUE; +} + +bool ProcessSentence(std::wstring& sentence, SentenceInfo sentenceInfo) +{ + std::lock_guard l(m); + if (window == nullptr || !sentenceInfo["current select"]) return false; + window->display->setText(QString::fromStdWString(sentence)); + return false; +} diff --git a/extensions/regexfilter.cpp b/extensions/regexfilter.cpp index 370bf6e..f4d2734 100644 --- a/extensions/regexfilter.cpp +++ b/extensions/regexfilter.cpp @@ -11,7 +11,7 @@ std::mutex m; struct : QMainWindow { - void Initialize() + void launch() { auto centralWidget = new QWidget(this); auto layout = new QVBoxLayout(centralWidget); @@ -43,7 +43,7 @@ BOOL WINAPI DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved QTimer::singleShot(0, [] { std::lock_guard l(m); - (window = new std::remove_pointer_t)->Initialize(); + (window = new std::remove_pointer_t)->launch(); }); } break; diff --git a/include/text.h b/include/text.h index b064e31..af7b2be 100644 --- a/include/text.h +++ b/include/text.h @@ -58,6 +58,7 @@ constexpr auto BING_PROMPT = u8"What language should Bing translate to?"; constexpr auto GOOGLE_PROMPT = u8"What language should Google translate to?"; constexpr auto TOO_MANY_TRANS_REQUESTS = L"\r\nToo many translation requests: refuse to make more"; constexpr auto TRANSLATION_ERROR = L"Error while translating"; +constexpr auto ALWAYS_ON_TOP = u8"Keep this window on top"; constexpr auto REGEX_FILTER = u8"Regex Filter"; constexpr auto INVALID_REGEX = u8"Invalid regex"; constexpr auto CURRENT_FILTER = u8"Currently filtering: ";