aboutsummaryrefslogtreecommitdiff
path: root/src/win/gui/InputMethod.cpp
blob: 492f1c2f7159d132ff00dcbab2d3c61f6832dbd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "cru/win/gui/InputMethod.hpp"

#include "cru/common/Logger.hpp"
#include "cru/common/StringUtil.hpp"
#include "cru/platform/Check.hpp"
#include "cru/platform/gui/DebugFlags.hpp"
#include "cru/win/Exception.hpp"
#include "cru/win/gui/Window.hpp"

#include <vector>

namespace cru::platform::gui::win {
AutoHIMC::AutoHIMC(HWND hwnd) : hwnd_(hwnd) {
  Expects(hwnd);
  handle_ = ::ImmGetContext(hwnd);
}

AutoHIMC::AutoHIMC(AutoHIMC&& other)
    : hwnd_(other.hwnd_), handle_(other.handle_) {
  other.hwnd_ = nullptr;
  other.handle_ = nullptr;
}

AutoHIMC& AutoHIMC::operator=(AutoHIMC&& other) {
  if (this != &other) {
    Object::operator=(std::move(other));
    this->hwnd_ = other.hwnd_;
    this->handle_ = other.handle_;
    other.hwnd_ = nullptr;
    other.handle_ = nullptr;
  }
  return *this;
}

AutoHIMC::~AutoHIMC() {
  if (handle_) {
    if (!::ImmReleaseContext(hwnd_, handle_))
      log::TagWarn(log_tag, u"Failed to release HIMC.");
  }
}

// copied from chromium
namespace {
// Determines whether or not the given attribute represents a target
// (a.k.a. a selection).
bool IsTargetAttribute(char attribute) {
  return (attribute == ATTR_TARGET_CONVERTED ||
          attribute == ATTR_TARGET_NOTCONVERTED);
}
// Helper function for ImeInput::GetCompositionInfo() method, to get the target
// range that's selected by the user in the current composition string.
void GetCompositionTargetRange(HIMC imm_context, int* target_start,
                               int* target_end) {
  int attribute_size =
      ::ImmGetCompositionString(imm_context, GCS_COMPATTR, NULL, 0);
  if (attribute_size > 0) {
    int start = 0;
    int end = 0;
    std::vector<char> attribute_data(attribute_size);
    ::ImmGetCompositionString(imm_context, GCS_COMPATTR, attribute_data.data(),
                              attribute_size);
    for (start = 0; start < attribute_size; ++start) {
      if (IsTargetAttribute(attribute_data[start])) break;
    }
    for (end = start; end < attribute_size; ++end) {
      if (!IsTargetAttribute(attribute_data[end])) break;
    }
    if (start == attribute_size) {
      // This composition clause does not contain any target clauses,
      // i.e. this clauses is an input clause.
      // We treat the whole composition as a target clause.
      start = 0;
      end = attribute_size;
    }
    *target_start = start;
    *target_end = end;
  }
}
// Helper function for ImeInput::GetCompositionInfo() method, to get underlines
// information of the current composition string.
CompositionClauses GetCompositionClauses(HIMC imm_context, int target_start,
                                         int target_end) {
  CompositionClauses result;
  int clause_size =
      ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE, NULL, 0);
  int clause_length = clause_size / sizeof(std::uint32_t);
  if (clause_length) {
    result.reserve(clause_length - 1);
    std::vector<std::uint32_t> clause_data(clause_length);
    ::ImmGetCompositionString(imm_context, GCS_COMPCLAUSE, clause_data.data(),
                              clause_size);
    for (int i = 0; i < clause_length - 1; ++i) {
      CompositionClause clause;
      clause.start = clause_data[i];
      clause.end = clause_data[i + 1];
      clause.target = false;
      // Use thick underline for the target clause.
      if (clause.start >= target_start && clause.end <= target_end) {
        clause.target = true;
      }
      result.push_back(clause);
    }
  }
  return result;
}

std::u16string GetString(HIMC imm_context) {
  LONG string_size =
      ::ImmGetCompositionString(imm_context, GCS_COMPSTR, NULL, 0);
  std::u16string result((string_size / sizeof(char16_t)), 0);
  ::ImmGetCompositionString(imm_context, GCS_COMPSTR, result.data(),
                            string_size);
  return result;
}

std::u16string GetResultString(HIMC imm_context) {
  LONG string_size =
      ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, NULL, 0);
  std::u16string result((string_size / sizeof(char16_t)), 0);
  ::ImmGetCompositionString(imm_context, GCS_RESULTSTR, result.data(),
                            string_size);
  return result;
}

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 text = GetString(imm_context);

  int length = static_cast<int>(text.length());
  // Find out the range selected by the user.
  int target_start = length;
  int target_end = length;
  GetCompositionTargetRange(imm_context, &target_start, &target_end);

  auto clauses = GetCompositionClauses(imm_context, target_start, target_end);

  int cursor = ::ImmGetCompositionString(imm_context, GCS_CURSORPOS, NULL, 0);

  return CompositionText{std::move(text), std::move(clauses),
                         TextRange{cursor}};
}

}  // namespace

WinInputMethodContext::WinInputMethodContext(
    gsl::not_null<WinNativeWindow*> window)
    : native_window_(window) {
  event_guard_ += window->NativeMessageEvent()->AddHandler(
      std::bind(&WinInputMethodContext::OnWindowNativeMessage, this,
                std::placeholders::_1));
}

WinInputMethodContext::~WinInputMethodContext() {}

void WinInputMethodContext::EnableIME() {
  const auto hwnd = native_window_->GetWindowHandle();
  if (::ImmAssociateContextEx(hwnd, nullptr, IACE_DEFAULT) == FALSE) {
    log::TagWarn(log_tag, u"Failed to enable ime.");
  }
}

void WinInputMethodContext::DisableIME() {
  const auto hwnd = native_window_->GetWindowHandle();
  AutoHIMC himc{hwnd};

  if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) {
    log::TagWarn(log_tag,
                 u"Failed to complete composition before disable ime.");
  }

  if (::ImmAssociateContextEx(hwnd, nullptr, 0) == FALSE) {
    log::TagWarn(log_tag, u"Failed to disable ime.");
  }
}

void WinInputMethodContext::CompleteComposition() {
  auto himc = GetHIMC();
  if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) {
    log::TagWarn(log_tag, u"Failed to complete composition.");
  }
}

void WinInputMethodContext::CancelComposition() {
  auto himc = GetHIMC();
  if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0)) {
    log::TagWarn(log_tag, u"Failed to complete composition.");
  }
}

CompositionText WinInputMethodContext::GetCompositionText() {
  auto himc = GetHIMC();
  return GetCompositionInfo(himc.Get());
}

void WinInputMethodContext::SetCandidateWindowPosition(const Point& point) {
  auto himc = GetHIMC();

  ::CANDIDATEFORM form;
  form.dwIndex = 1;
  form.dwStyle = CFS_CANDIDATEPOS;

  form.ptCurrentPos = native_window_->DipToPixel(point);

  if (!::ImmSetCandidateWindow(himc.Get(), &form))
    log::TagDebug(log_tag,
                  u"Failed to set input method candidate window position.");
}

IEvent<std::nullptr_t>* WinInputMethodContext::CompositionStartEvent() {
  return &composition_start_event_;
}

IEvent<std::nullptr_t>* WinInputMethodContext::CompositionEndEvent() {
  return &composition_end_event_;
};

IEvent<std::nullptr_t>* WinInputMethodContext::CompositionEvent() {
  return &composition_event_;
}

IEvent<std::u16string_view>* WinInputMethodContext::TextEvent() {
  return &text_event_;
}

void WinInputMethodContext::OnWindowNativeMessage(
    WindowNativeMessageEventArgs& args) {
  const auto& message = args.GetWindowMessage();
  switch (message.msg) {
    case WM_CHAR: {
      const auto c = static_cast<char16_t>(message.w_param);
      if (IsUtf16SurrogatePairCodeUnit(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,
                     u"A WM_CHAR message for character from supplementary "
                     u"planes is ignored.");
      } else {
        char16_t s[1] = {c};
        text_event_.Raise({s, 1});
      }
      args.HandleWithResult(0);
      break;
    }
    case WM_IME_COMPOSITION: {
      composition_event_.Raise(nullptr);
      auto composition_text = GetCompositionText();
      if constexpr (DebugFlags::input_method) {
        log::TagDebug(log_tag, u"WM_IME_COMPOSITION composition text:\n{}",
                      composition_text);
      }
      if (message.l_param & GCS_RESULTSTR) {
        auto result_string = GetResultString();
        text_event_.Raise(result_string);
      }
      break;
    }
    case WM_IME_STARTCOMPOSITION: {
      if constexpr (DebugFlags::input_method) {
        log::TagDebug(log_tag, u"WM_IME_STARTCOMPOSITION received.");
      }
      composition_start_event_.Raise(nullptr);
      break;
    }
    case WM_IME_ENDCOMPOSITION: {
      if constexpr (DebugFlags::input_method) {
        log::TagDebug(log_tag, u"WM_IME_ENDCOMPOSITION received.");
      }
      composition_end_event_.Raise(nullptr);
      break;
    }
  }
}

std::u16string WinInputMethodContext::GetResultString() {
  auto himc = GetHIMC();
  auto result = win::GetResultString(himc.Get());
  return result;
}

AutoHIMC WinInputMethodContext::GetHIMC() {
  const auto hwnd = native_window_->GetWindowHandle();
  return AutoHIMC{hwnd};
}
}  // namespace cru::platform::gui::win