diff options
Diffstat (limited to 'src/win/native')
-rw-r--r-- | src/win/native/Cursor.cpp | 2 | ||||
-rw-r--r-- | src/win/native/GodWindow.cpp | 2 | ||||
-rw-r--r-- | src/win/native/InputMethod.cpp | 72 | ||||
-rw-r--r-- | src/win/native/Window.cpp | 23 |
4 files changed, 44 insertions, 55 deletions
diff --git a/src/win/native/Cursor.cpp b/src/win/native/Cursor.cpp index a6ab5bfc..429f6e7c 100644 --- a/src/win/native/Cursor.cpp +++ b/src/win/native/Cursor.cpp @@ -16,7 +16,7 @@ WinCursor::~WinCursor() { if (!::DestroyCursor(handle_)) { // This is not a fetal error but might still need notice because it may // cause leak. - log::TagWarn(log_tag, "Failed to destroy a cursor. Last error code: {}", + log::TagWarn(log_tag, u"Failed to destroy a cursor. Last error code: {}", ::GetLastError()); } } diff --git a/src/win/native/GodWindow.cpp b/src/win/native/GodWindow.cpp index 8ef2788e..b1e7275e 100644 --- a/src/win/native/GodWindow.cpp +++ b/src/win/native/GodWindow.cpp @@ -45,7 +45,7 @@ GodWindow::GodWindow(WinUiApplication* application) { GodWindow::~GodWindow() { if (!::DestroyWindow(hwnd_)) { // Although this could be "safely" ignore. - log::TagWarn(log_tag, "Failed to destroy god window."); + log::TagWarn(log_tag, u"Failed to destroy god window."); } } diff --git a/src/win/native/InputMethod.cpp b/src/win/native/InputMethod.cpp index d3d989f3..5fc6c934 100644 --- a/src/win/native/InputMethod.cpp +++ b/src/win/native/InputMethod.cpp @@ -2,9 +2,9 @@ #include "DpiUtil.hpp" #include "cru/common/Logger.hpp" +#include "cru/common/StringUtil.hpp" #include "cru/platform/Check.hpp" #include "cru/win/Exception.hpp" -#include "cru/win/String.hpp" #include "cru/win/native/Window.hpp" #include <vector> @@ -35,7 +35,7 @@ AutoHIMC& AutoHIMC::operator=(AutoHIMC&& other) { AutoHIMC::~AutoHIMC() { if (handle_) { if (!::ImmReleaseContext(hwnd_, handle_)) - log::TagWarn(log_tag, "Failed to release HIMC."); + log::TagWarn(log_tag, u"Failed to release HIMC."); } } @@ -104,19 +104,19 @@ CompositionClauses GetCompositionClauses(HIMC imm_context, int target_start, return result; } -std::wstring GetString(HIMC imm_context) { +std::u16string GetString(HIMC imm_context) { LONG string_size = ::ImmGetCompositionString(imm_context, GCS_COMPSTR, NULL, 0); - std::wstring result((string_size / sizeof(wchar_t)), 0); + std::u16string result((string_size / sizeof(char16_t)), 0); ::ImmGetCompositionString(imm_context, GCS_COMPSTR, result.data(), string_size); return result; } -std::wstring GetResultString(HIMC imm_context) { +std::u16string GetResultString(HIMC imm_context) { LONG string_size = ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, NULL, 0); - std::wstring result((string_size / sizeof(wchar_t)), 0); + std::u16string result((string_size / sizeof(char16_t)), 0); ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, result.data(), string_size); return result; @@ -126,25 +126,17 @@ CompositionText GetCompositionInfo(HIMC imm_context) { // We only care about GCS_COMPATTR, GCS_COMPCLAUSE and GCS_CURSORPOS, and // convert them into underlines and selection range respectively. - auto w_text = GetString(imm_context); + auto text = GetString(imm_context); - int w_length = static_cast<int>(w_text.length()); + int length = static_cast<int>(text.length()); // Find out the range selected by the user. - int w_target_start = w_length; - int w_target_end = w_length; - GetCompositionTargetRange(imm_context, &w_target_start, &w_target_end); + int target_start = length; + int target_end = length; + GetCompositionTargetRange(imm_context, &target_start, &target_end); - auto clauses = - GetCompositionClauses(imm_context, w_target_start, w_target_end); + auto clauses = GetCompositionClauses(imm_context, target_start, target_end); - int w_cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0); - - auto text = platform::win::ToUtf8String(w_text); - for (auto& clause : clauses) { - clause.start = platform::win::IndexUtf16ToUtf8(w_text, clause.start, text); - clause.end = platform::win::IndexUtf16ToUtf8(w_text, clause.end, text); - } - int cursor = platform::win::IndexUtf16ToUtf8(w_text, w_cursor, text); + int cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0); return CompositionText{std::move(text), std::move(clauses), TextRange{cursor}}; @@ -169,7 +161,7 @@ void WinInputMethodContext::EnableIME() { const auto hwnd = native_window->GetWindowHandle(); if (::ImmAssociateContextEx(hwnd, nullptr, IACE_DEFAULT) == FALSE) { - log::TagWarn(log_tag, "Failed to enable ime."); + log::TagWarn(log_tag, u"Failed to enable ime."); } } @@ -181,11 +173,12 @@ void WinInputMethodContext::DisableIME() { AutoHIMC himc{hwnd}; if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { - log::TagWarn(log_tag, "Failed to complete composition before disable ime."); + log::TagWarn(log_tag, + u"Failed to complete composition before disable ime."); } if (::ImmAssociateContextEx(hwnd, nullptr, 0) == FALSE) { - log::TagWarn(log_tag, "Failed to disable ime."); + log::TagWarn(log_tag, u"Failed to disable ime."); } } @@ -195,7 +188,7 @@ void WinInputMethodContext::CompleteComposition() { auto himc = *std::move(optional_himc); if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { - log::TagWarn(log_tag, "Failed to complete composition."); + log::TagWarn(log_tag, u"Failed to complete composition."); } } @@ -205,7 +198,7 @@ void WinInputMethodContext::CancelComposition() { auto himc = *std::move(optional_himc); if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0)) { - log::TagWarn(log_tag, "Failed to complete composition."); + log::TagWarn(log_tag, u"Failed to complete composition."); } } @@ -229,7 +222,7 @@ void WinInputMethodContext::SetCandidateWindowPosition(const Point& point) { if (!::ImmSetCandidateWindow(himc.Get(), &form)) log::TagDebug(log_tag, - "Failed to set input method candidate window position."); + u"Failed to set input method candidate window position."); } IEvent<std::nullptr_t>* WinInputMethodContext::CompositionStartEvent() { @@ -244,7 +237,7 @@ IEvent<std::nullptr_t>* WinInputMethodContext::CompositionEvent() { return &composition_event_; } -IEvent<std::string_view>* WinInputMethodContext::TextEvent() { +IEvent<std::u16string_view>* WinInputMethodContext::TextEvent() { return &text_event_; } @@ -253,18 +246,17 @@ void WinInputMethodContext::OnWindowNativeMessage( const auto& message = args.GetWindowMessage(); switch (message.msg) { case WM_CHAR: { - const auto c = static_cast<wchar_t>(message.w_param); - if (platform::win::IsSurrogatePair(c)) { + const auto c = static_cast<char16_t>(message.w_param); + if (IsSurrogatePair(c)) { // I don't think this will happen because normal key strike without ime // should only trigger ascci character. If it is a charater from // supplementary planes, it should be handled with ime messages. log::TagWarn(log_tag, - "A WM_CHAR message for character from supplementary " - "planes is ignored."); + u"A WM_CHAR message for character from supplementary " + u"planes is ignored."); } else { - wchar_t s[1] = {c}; - auto str = platform::win::ToUtf8String({s, 1}); - text_event_.Raise(str); + char16_t s[1] = {c}; + text_event_.Raise({s, 1}); } args.HandleWithResult(0); break; @@ -272,8 +264,6 @@ void WinInputMethodContext::OnWindowNativeMessage( case WM_IME_COMPOSITION: { composition_event_.Raise(nullptr); auto composition_text = GetCompositionText(); - log::TagDebug(log_tag, "WM_IME_COMPOSITION composition text:\n{}", - composition_text); if (message.l_param & GCS_RESULTSTR) { auto result_string = GetResultString(); text_event_.Raise(result_string); @@ -291,13 +281,13 @@ void WinInputMethodContext::OnWindowNativeMessage( } } -std::string WinInputMethodContext::GetResultString() { +std::u16string WinInputMethodContext::GetResultString() { auto optional_himc = TryGetHIMC(); - if (!optional_himc.has_value()) return ""; + if (!optional_himc.has_value()) return u""; auto himc = *std::move(optional_himc); - auto w_result = win::GetResultString(himc.Get()); - return platform::win::ToUtf8String(w_result); + auto result = win::GetResultString(himc.Get()); + return result; } std::optional<AutoHIMC> WinInputMethodContext::TryGetHIMC() { diff --git a/src/win/native/Window.cpp b/src/win/native/Window.cpp index 22505bd6..81642451 100644 --- a/src/win/native/Window.cpp +++ b/src/win/native/Window.cpp @@ -5,7 +5,6 @@ #include "WindowManager.hpp" #include "cru/common/Logger.hpp" #include "cru/platform/Check.hpp" -#include "cru/win/String.hpp" #include "cru/win/native/Cursor.hpp" #include "cru/win/native/Exception.hpp" #include "cru/win/native/Keyboard.hpp" @@ -124,7 +123,7 @@ bool WinNativeWindow::ReleaseMouse() { } void WinNativeWindow::RequestRepaint() { - log::TagDebug(log_tag, "A repaint is requested."); + log::TagDebug(log_tag, u"A repaint is requested."); if (!::InvalidateRect(hwnd_, nullptr, FALSE)) throw Win32Error(::GetLastError(), "Failed to invalidate window."); if (!::UpdateWindow(hwnd_)) @@ -145,43 +144,43 @@ void WinNativeWindow::SetCursor(std::shared_ptr<ICursor> cursor) { if (!::SetClassLongPtrW(hwnd_, GCLP_HCURSOR, reinterpret_cast<LONG_PTR>(cursor_->GetHandle()))) { log::TagWarn(log_tag, - "Failed to set cursor because failed to set class long. Last " - "error code: {}.", + u"Failed to set cursor because failed to set class long. Last " + u"error code: {}.", ::GetLastError()); return; } if (!IsVisible()) return; - auto lg = [](const std::string_view& reason) { + auto lg = [](const std::u16string_view& reason) { log::TagWarn( log_tag, - "Failed to set cursor because {} when window is visible. (We need to " - "update cursor if it is inside the window.) Last error code: {}.", + u"Failed to set cursor because {} when window is visible. (We need to " + u"update cursor if it is inside the window.) Last error code: {}.", reason, ::GetLastError()); }; ::POINT point; if (!::GetCursorPos(&point)) { - lg("failed to get cursor pos"); + lg(u"failed to get cursor pos"); return; } ::RECT rect; if (!::GetClientRect(hwnd_, &rect)) { - lg("failed to get window's client rect"); + lg(u"failed to get window's client rect"); return; } ::POINT lefttop{rect.left, rect.top}; ::POINT rightbottom{rect.right, rect.bottom}; if (!::ClientToScreen(hwnd_, &lefttop)) { - lg("failed to call ClientToScreen on lefttop"); + lg(u"failed to call ClientToScreen on lefttop"); return; } if (!::ClientToScreen(hwnd_, &rightbottom)) { - lg("failed to call ClientToScreen on rightbottom"); + lg(u"failed to call ClientToScreen on rightbottom"); return; } @@ -354,7 +353,7 @@ void WinNativeWindow::OnDestroyInternal() { void WinNativeWindow::OnPaintInternal() { paint_event_.Raise(nullptr); ValidateRect(hwnd_, nullptr); - log::TagDebug(log_tag, "A repaint is finished."); + log::TagDebug(log_tag, u"A repaint is finished."); } void WinNativeWindow::OnResizeInternal(const int new_width, |