- add sources.
[platform/framework/web/crosswalk.git] / src / ui / base / ime / input_method_imm32.cc
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.
4
5 #include "ui/base/ime/input_method_imm32.h"
6
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"
11
12
13 namespace ui {
14
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();
26 }
27
28 void InputMethodIMM32::OnFocus() {
29   // Ignore OnFocus event for "always focused" model. See the comment in the
30   // constructor.
31   // TODO(ime): Implement OnFocus once the callers are fixed.
32 }
33
34 void InputMethodIMM32::OnBlur() {
35   // Ignore OnBlur event for "always focused" model. See the comment in the
36   // constructor.
37   // TODO(ime): Implement OnFocus once the callers are fixed.
38 }
39
40 bool InputMethodIMM32::OnUntranslatedIMEMessage(
41     const base::NativeEvent& event, InputMethod::NativeEventResult* result) {
42   LRESULT original_result = 0;
43   BOOL handled = FALSE;
44   switch (event.message) {
45     case WM_IME_SETCONTEXT:
46       original_result = OnImeSetContext(
47           event.hwnd, event.message, event.wParam, event.lParam, &handled);
48       break;
49     case WM_IME_STARTCOMPOSITION:
50       original_result = OnImeStartComposition(
51           event.hwnd, event.message, event.wParam, event.lParam, &handled);
52       break;
53     case WM_IME_COMPOSITION:
54       original_result = OnImeComposition(
55           event.hwnd, event.message, event.wParam, event.lParam, &handled);
56       break;
57     case WM_IME_ENDCOMPOSITION:
58       original_result = OnImeEndComposition(
59           event.hwnd, event.message, event.wParam, event.lParam, &handled);
60       break;
61     case WM_IME_REQUEST:
62       original_result = OnImeRequest(
63           event.message, event.wParam, event.lParam, &handled);
64       break;
65     case WM_CHAR:
66     case WM_SYSCHAR:
67       original_result = OnChar(
68           event.hwnd, event.message, event.wParam, event.lParam, &handled);
69       break;
70     case WM_DEADCHAR:
71     case WM_SYSDEADCHAR:
72       original_result = OnDeadChar(
73           event.message, event.wParam, event.lParam, &handled);
74       break;
75     case WM_IME_NOTIFY:
76       original_result = OnImeNotify(
77           event.message, event.wParam, event.lParam, &handled);
78       break;
79     default:
80       NOTREACHED() << "Unknown IME message:" << event.message;
81       break;
82   }
83   if (result)
84     *result = original_result;
85   return !!handled;
86 }
87
88 void InputMethodIMM32::OnTextInputTypeChanged(const TextInputClient* client) {
89   if (IsTextInputClientFocused(client) && IsWindowFocused(client)) {
90     imm32_manager_.CancelIME(GetAttachedWindowHandle(client));
91     UpdateIMEState();
92   }
93   InputMethodWin::OnTextInputTypeChanged(client);
94 }
95
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());
101
102     HWND attached_window = GetAttachedWindowHandle(client);
103     // TODO(ime): see comment in TextInputClient::GetCaretBounds(), this
104     // conversion shouldn't be necessary.
105     RECT r = {};
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);
112   }
113   InputMethodWin::OnCaretBoundsChanged(client);
114 }
115
116 void InputMethodIMM32::CancelComposition(const TextInputClient* client) {
117   if (enabled_ && IsTextInputClientFocused(client))
118     imm32_manager_.CancelIME(GetAttachedWindowHandle(client));
119 }
120
121 bool InputMethodIMM32::IsCandidatePopupOpen() const {
122   return is_candidate_popup_open_;
123 }
124
125 void InputMethodIMM32::OnWillChangeFocusedClient(
126     TextInputClient* focused_before,
127     TextInputClient* focused) {
128   if (IsWindowFocused(focused_before)) {
129     ConfirmCompositionText();
130   }
131 }
132
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);
140
141     UpdateIMEState();
142
143     // Force to update caret bounds, in case the client thinks that the caret
144     // bounds has not changed.
145     OnCaretBoundsChanged(focused);
146   }
147 }
148
149 LRESULT InputMethodIMM32::OnImeSetContext(HWND window_handle,
150                                           UINT message,
151                                           WPARAM wparam,
152                                           LPARAM lparam,
153                                           BOOL* handled) {
154   if (!!wparam)
155     imm32_manager_.CreateImeWindow(window_handle);
156
157   OnInputMethodChanged();
158   return imm32_manager_.SetImeWindowStyle(
159       window_handle, message, wparam, lparam, handled);
160 }
161
162 LRESULT InputMethodIMM32::OnImeStartComposition(HWND window_handle,
163                                                 UINT message,
164                                                 WPARAM wparam,
165                                                 LPARAM lparam,
166                                                 BOOL* handled) {
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.
170   *handled = TRUE;
171
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);
176   return 0;
177 }
178
179 LRESULT InputMethodIMM32::OnImeComposition(HWND window_handle,
180                                            UINT message,
181                                            WPARAM wparam,
182                                            LPARAM lparam,
183                                            BOOL* handled) {
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.
186   *handled = TRUE;
187
188   // At first, update the position of the IME window.
189   imm32_manager_.UpdateImeWindow(window_handle);
190
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.
202   }
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);
208
209   return 0;
210 }
211
212 LRESULT InputMethodIMM32::OnImeEndComposition(HWND window_handle,
213                                               UINT message,
214                                               WPARAM wparam,
215                                               LPARAM lparam,
216                                               BOOL* handled) {
217   // Let WTL call ::DefWindowProc() and release its resources.
218   *handled = FALSE;
219
220   composing_window_handle_ = NULL;
221
222   if (!IsTextInputTypeNone() && GetTextInputClient()->HasCompositionText())
223     GetTextInputClient()->ClearCompositionText();
224
225   imm32_manager_.ResetComposition(window_handle);
226   imm32_manager_.DestroyImeWindow(window_handle);
227   return 0;
228 }
229
230 LRESULT InputMethodIMM32::OnImeNotify(UINT message,
231                                       WPARAM wparam,
232                                       LPARAM lparam,
233                                       BOOL* handled) {
234   *handled = FALSE;
235
236   // Update |is_candidate_popup_open_|, whether a candidate window is open.
237   switch (wparam) {
238   case IMN_OPENCANDIDATE:
239     is_candidate_popup_open_ = true;
240     break;
241   case IMN_CLOSECANDIDATE:
242     is_candidate_popup_open_ = false;
243     break;
244   }
245
246   return 0;
247 }
248
249 void InputMethodIMM32::ConfirmCompositionText() {
250   if (composing_window_handle_)
251     imm32_manager_.CleanupComposition(composing_window_handle_);
252
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();
259   }
260 }
261
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);
272       enabled_ = false;
273       break;
274     default:
275       imm32_manager_.EnableIME(window_handle);
276       enabled_ = true;
277       break;
278   }
279
280   imm32_manager_.SetTextInputMode(window_handle, text_input_mode);
281   tsf_inputscope::SetInputScopeForTsfUnawareWindow(
282       window_handle, text_input_type, text_input_mode);
283 }
284
285 }  // namespace ui