1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/base/ime/input_method_imm32.h"
7 #include "base/basictypes.h"
8 #include "ui/base/ime/composition_text.h"
9 #include "ui/base/ime/text_input_client.h"
10 #include "ui/base/ime/win/tsf_input_scope.h"
15 InputMethodIMM32::InputMethodIMM32(internal::InputMethodDelegate* delegate,
16 HWND toplevel_window_handle)
17 : InputMethodWin(delegate, toplevel_window_handle),
18 enabled_(false), is_candidate_popup_open_(false),
19 composing_window_handle_(NULL) {
20 // In non-Aura environment, appropriate callbacks to OnFocus() and OnBlur()
21 // are not implemented yet. To work around this limitation, here we use
22 // "always focused" model.
23 // TODO(ime): Fix the caller of OnFocus() and OnBlur() so that appropriate
24 // focus event will be passed.
25 InputMethodWin::OnFocus();
28 void InputMethodIMM32::OnFocus() {
29 // Ignore OnFocus event for "always focused" model. See the comment in the
31 // TODO(ime): Implement OnFocus once the callers are fixed.
34 void InputMethodIMM32::OnBlur() {
35 // Ignore OnBlur event for "always focused" model. See the comment in the
37 // TODO(ime): Implement OnFocus once the callers are fixed.
40 bool InputMethodIMM32::OnUntranslatedIMEMessage(
41 const base::NativeEvent& event, InputMethod::NativeEventResult* result) {
42 LRESULT original_result = 0;
44 switch (event.message) {
45 case WM_IME_SETCONTEXT:
46 original_result = OnImeSetContext(
47 event.hwnd, event.message, event.wParam, event.lParam, &handled);
49 case WM_IME_STARTCOMPOSITION:
50 original_result = OnImeStartComposition(
51 event.hwnd, event.message, event.wParam, event.lParam, &handled);
53 case WM_IME_COMPOSITION:
54 original_result = OnImeComposition(
55 event.hwnd, event.message, event.wParam, event.lParam, &handled);
57 case WM_IME_ENDCOMPOSITION:
58 original_result = OnImeEndComposition(
59 event.hwnd, event.message, event.wParam, event.lParam, &handled);
62 original_result = OnImeRequest(
63 event.message, event.wParam, event.lParam, &handled);
67 original_result = OnChar(
68 event.hwnd, event.message, event.wParam, event.lParam, &handled);
72 original_result = OnDeadChar(
73 event.message, event.wParam, event.lParam, &handled);
76 original_result = OnImeNotify(
77 event.message, event.wParam, event.lParam, &handled);
80 NOTREACHED() << "Unknown IME message:" << event.message;
84 *result = original_result;
88 void InputMethodIMM32::OnTextInputTypeChanged(const TextInputClient* client) {
89 if (IsTextInputClientFocused(client) && IsWindowFocused(client)) {
90 imm32_manager_.CancelIME(GetAttachedWindowHandle(client));
93 InputMethodWin::OnTextInputTypeChanged(client);
96 void InputMethodIMM32::OnCaretBoundsChanged(const TextInputClient* client) {
97 if (enabled_ && IsTextInputClientFocused(client) && IsWindowFocused(client)) {
98 // The current text input type should not be NONE if |client| is focused.
99 DCHECK(!IsTextInputTypeNone());
100 gfx::Rect screen_bounds(GetTextInputClient()->GetCaretBounds());
102 HWND attached_window = GetAttachedWindowHandle(client);
103 // TODO(ime): see comment in TextInputClient::GetCaretBounds(), this
104 // conversion shouldn't be necessary.
106 GetClientRect(attached_window, &r);
107 POINT window_point = { screen_bounds.x(), screen_bounds.y() };
108 ScreenToClient(attached_window, &window_point);
109 gfx::Rect caret_rect(gfx::Point(window_point.x, window_point.y),
110 screen_bounds.size());
111 imm32_manager_.UpdateCaretRect(attached_window, caret_rect);
113 InputMethodWin::OnCaretBoundsChanged(client);
116 void InputMethodIMM32::CancelComposition(const TextInputClient* client) {
117 if (enabled_ && IsTextInputClientFocused(client))
118 imm32_manager_.CancelIME(GetAttachedWindowHandle(client));
121 bool InputMethodIMM32::IsCandidatePopupOpen() const {
122 return is_candidate_popup_open_;
125 void InputMethodIMM32::OnWillChangeFocusedClient(
126 TextInputClient* focused_before,
127 TextInputClient* focused) {
128 if (IsWindowFocused(focused_before)) {
129 ConfirmCompositionText();
133 void InputMethodIMM32::OnDidChangeFocusedClient(TextInputClient* focused_before,
134 TextInputClient* focused) {
135 if (IsWindowFocused(focused)) {
136 // Force to update the input type since client's TextInputStateChanged()
137 // function might not be called if text input types before the client loses
138 // focus and after it acquires focus again are the same.
139 OnTextInputTypeChanged(focused);
143 // Force to update caret bounds, in case the client thinks that the caret
144 // bounds has not changed.
145 OnCaretBoundsChanged(focused);
149 LRESULT InputMethodIMM32::OnImeSetContext(HWND window_handle,
155 imm32_manager_.CreateImeWindow(window_handle);
157 OnInputMethodChanged();
158 return imm32_manager_.SetImeWindowStyle(
159 window_handle, message, wparam, lparam, handled);
162 LRESULT InputMethodIMM32::OnImeStartComposition(HWND window_handle,
167 // We have to prevent WTL from calling ::DefWindowProc() because the function
168 // calls ::ImmSetCompositionWindow() and ::ImmSetCandidateWindow() to
169 // over-write the position of IME windows.
172 // Reset the composition status and create IME windows.
173 composing_window_handle_ = window_handle;
174 imm32_manager_.CreateImeWindow(window_handle);
175 imm32_manager_.ResetComposition(window_handle);
179 LRESULT InputMethodIMM32::OnImeComposition(HWND window_handle,
184 // We have to prevent WTL from calling ::DefWindowProc() because we do not
185 // want for the IMM (Input Method Manager) to send WM_IME_CHAR messages.
188 // At first, update the position of the IME window.
189 imm32_manager_.UpdateImeWindow(window_handle);
191 // Retrieve the result string and its attributes of the ongoing composition
192 // and send it to a renderer process.
193 ui::CompositionText composition;
194 if (imm32_manager_.GetResult(window_handle, lparam, &composition.text)) {
195 if (!IsTextInputTypeNone())
196 GetTextInputClient()->InsertText(composition.text);
197 imm32_manager_.ResetComposition(window_handle);
198 // Fall though and try reading the composition string.
199 // Japanese IMEs send a message containing both GCS_RESULTSTR and
200 // GCS_COMPSTR, which means an ongoing composition has been finished
201 // by the start of another composition.
203 // Retrieve the composition string and its attributes of the ongoing
204 // composition and send it to a renderer process.
205 if (imm32_manager_.GetComposition(window_handle, lparam, &composition) &&
206 !IsTextInputTypeNone())
207 GetTextInputClient()->SetCompositionText(composition);
212 LRESULT InputMethodIMM32::OnImeEndComposition(HWND window_handle,
217 // Let WTL call ::DefWindowProc() and release its resources.
220 composing_window_handle_ = NULL;
222 if (!IsTextInputTypeNone() && GetTextInputClient()->HasCompositionText())
223 GetTextInputClient()->ClearCompositionText();
225 imm32_manager_.ResetComposition(window_handle);
226 imm32_manager_.DestroyImeWindow(window_handle);
230 LRESULT InputMethodIMM32::OnImeNotify(UINT message,
236 // Update |is_candidate_popup_open_|, whether a candidate window is open.
238 case IMN_OPENCANDIDATE:
239 is_candidate_popup_open_ = true;
241 case IMN_CLOSECANDIDATE:
242 is_candidate_popup_open_ = false;
249 void InputMethodIMM32::ConfirmCompositionText() {
250 if (composing_window_handle_)
251 imm32_manager_.CleanupComposition(composing_window_handle_);
253 if (!IsTextInputTypeNone()) {
254 // Though above line should confirm the client's composition text by sending
255 // a result text to us, in case the input method and the client are in
256 // inconsistent states, we check the client's composition state again.
257 if (GetTextInputClient()->HasCompositionText())
258 GetTextInputClient()->ConfirmCompositionText();
262 void InputMethodIMM32::UpdateIMEState() {
263 // Use switch here in case we are going to add more text input types.
264 // We disable input method in password field.
265 const HWND window_handle = GetAttachedWindowHandle(GetTextInputClient());
266 const TextInputType text_input_type = GetTextInputType();
267 const TextInputMode text_input_mode = GetTextInputMode();
268 switch (text_input_type) {
269 case ui::TEXT_INPUT_TYPE_NONE:
270 case ui::TEXT_INPUT_TYPE_PASSWORD:
271 imm32_manager_.DisableIME(window_handle);
275 imm32_manager_.EnableIME(window_handle);
280 imm32_manager_.SetTextInputMode(window_handle, text_input_mode);
281 tsf_inputscope::SetInputScopeForTsfUnawareWindow(
282 window_handle, text_input_type, text_input_mode);