• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 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_win.h"
6 
7 #include "base/basictypes.h"
8 #include "ui/base/ime/text_input_client.h"
9 #include "ui/base/ime/win/tsf_input_scope.h"
10 #include "ui/events/event.h"
11 #include "ui/events/event_constants.h"
12 #include "ui/events/event_utils.h"
13 #include "ui/events/keycodes/keyboard_codes.h"
14 #include "ui/gfx/win/dpi.h"
15 #include "ui/gfx/win/hwnd_util.h"
16 
17 namespace ui {
18 namespace {
19 
20 // Extra number of chars before and after selection (or composition) range which
21 // is returned to IME for improving conversion accuracy.
22 static const size_t kExtraNumberOfChars = 20;
23 
24 }  // namespace
25 
InputMethodWin(internal::InputMethodDelegate * delegate,HWND toplevel_window_handle)26 InputMethodWin::InputMethodWin(internal::InputMethodDelegate* delegate,
27                                HWND toplevel_window_handle)
28     : toplevel_window_handle_(toplevel_window_handle),
29       pending_requested_direction_(base::i18n::UNKNOWN_DIRECTION),
30       accept_carriage_return_(false),
31       active_(false),
32       enabled_(false),
33       is_candidate_popup_open_(false),
34       composing_window_handle_(NULL) {
35   SetDelegate(delegate);
36   // In non-Aura environment, appropriate callbacks to OnFocus() and OnBlur()
37   // are not implemented yet. To work around this limitation, here we use
38   // "always focused" model.
39   // TODO(ime): Fix the caller of OnFocus() and OnBlur() so that appropriate
40   // focus event will be passed.
41   InputMethodBase::OnFocus();
42 }
43 
Init(bool focused)44 void InputMethodWin::Init(bool focused) {
45   // Gets the initial input locale.
46   OnInputLocaleChanged();
47 
48   InputMethodBase::Init(focused);
49 }
50 
OnFocus()51 void InputMethodWin::OnFocus() {
52   // Ignore OnFocus event for "always focused" model. See the comment in the
53   // constructor.
54   // TODO(ime): Implement OnFocus once the callers are fixed.
55 }
56 
OnBlur()57 void InputMethodWin::OnBlur() {
58   // Ignore OnBlur event for "always focused" model. See the comment in the
59   // constructor.
60   // TODO(ime): Implement OnFocus once the callers are fixed.
61 }
62 
OnUntranslatedIMEMessage(const base::NativeEvent & event,InputMethod::NativeEventResult * result)63 bool InputMethodWin::OnUntranslatedIMEMessage(
64     const base::NativeEvent& event,
65     InputMethod::NativeEventResult* result) {
66   LRESULT original_result = 0;
67   BOOL handled = FALSE;
68   switch (event.message) {
69     case WM_IME_SETCONTEXT:
70       original_result = OnImeSetContext(
71           event.hwnd, event.message, event.wParam, event.lParam, &handled);
72       break;
73     case WM_IME_STARTCOMPOSITION:
74       original_result = OnImeStartComposition(
75           event.hwnd, event.message, event.wParam, event.lParam, &handled);
76       break;
77     case WM_IME_COMPOSITION:
78       original_result = OnImeComposition(
79           event.hwnd, event.message, event.wParam, event.lParam, &handled);
80       break;
81     case WM_IME_ENDCOMPOSITION:
82       original_result = OnImeEndComposition(
83           event.hwnd, event.message, event.wParam, event.lParam, &handled);
84       break;
85     case WM_IME_REQUEST:
86       original_result = OnImeRequest(
87           event.message, event.wParam, event.lParam, &handled);
88       break;
89     case WM_CHAR:
90     case WM_SYSCHAR:
91       original_result = OnChar(
92           event.hwnd, event.message, event.wParam, event.lParam, &handled);
93       break;
94     case WM_IME_NOTIFY:
95       original_result = OnImeNotify(
96           event.message, event.wParam, event.lParam, &handled);
97       break;
98     default:
99       NOTREACHED() << "Unknown IME message:" << event.message;
100       break;
101   }
102   if (result)
103     *result = original_result;
104   return !!handled;
105 }
106 
DispatchKeyEvent(const ui::KeyEvent & event)107 bool InputMethodWin::DispatchKeyEvent(const ui::KeyEvent& event) {
108   if (!event.HasNativeEvent())
109     return DispatchFabricatedKeyEvent(event);
110 
111   const base::NativeEvent& native_key_event = event.native_event();
112   if (native_key_event.message == WM_CHAR) {
113     BOOL handled;
114     OnChar(native_key_event.hwnd, native_key_event.message,
115            native_key_event.wParam, native_key_event.lParam, &handled);
116     return !!handled;  // Don't send WM_CHAR for post event processing.
117   }
118   // Handles ctrl-shift key to change text direction and layout alignment.
119   if (ui::IMM32Manager::IsRTLKeyboardLayoutInstalled() &&
120       !IsTextInputTypeNone()) {
121     // TODO: shouldn't need to generate a KeyEvent here.
122     const ui::KeyEvent key(native_key_event,
123                            native_key_event.message == WM_CHAR);
124     ui::KeyboardCode code = key.key_code();
125     if (key.type() == ui::ET_KEY_PRESSED) {
126       if (code == ui::VKEY_SHIFT) {
127         base::i18n::TextDirection dir;
128         if (ui::IMM32Manager::IsCtrlShiftPressed(&dir))
129           pending_requested_direction_ = dir;
130       } else if (code != ui::VKEY_CONTROL) {
131         pending_requested_direction_ = base::i18n::UNKNOWN_DIRECTION;
132       }
133     } else if (key.type() == ui::ET_KEY_RELEASED &&
134                (code == ui::VKEY_SHIFT || code == ui::VKEY_CONTROL) &&
135                pending_requested_direction_ != base::i18n::UNKNOWN_DIRECTION) {
136       GetTextInputClient()->ChangeTextDirectionAndLayoutAlignment(
137           pending_requested_direction_);
138       pending_requested_direction_ = base::i18n::UNKNOWN_DIRECTION;
139     }
140   }
141 
142   return DispatchKeyEventPostIME(event);
143 }
144 
OnTextInputTypeChanged(const TextInputClient * client)145 void InputMethodWin::OnTextInputTypeChanged(const TextInputClient* client) {
146   if (!IsTextInputClientFocused(client) || !IsWindowFocused(client))
147     return;
148   imm32_manager_.CancelIME(GetAttachedWindowHandle(client));
149   UpdateIMEState();
150 }
151 
OnCaretBoundsChanged(const TextInputClient * client)152 void InputMethodWin::OnCaretBoundsChanged(const TextInputClient* client) {
153   if (!enabled_ || !IsTextInputClientFocused(client) ||
154       !IsWindowFocused(client)) {
155     return;
156   }
157   // The current text input type should not be NONE if |client| is focused.
158   DCHECK(!IsTextInputTypeNone());
159   // Tentatively assume that the returned value is DIP (Density Independent
160   // Pixel). See the comment in text_input_client.h and http://crbug.com/360334.
161   const gfx::Rect dip_screen_bounds(GetTextInputClient()->GetCaretBounds());
162   const gfx::Rect screen_bounds = gfx::win::DIPToScreenRect(dip_screen_bounds);
163 
164   HWND attached_window = GetAttachedWindowHandle(client);
165   // TODO(ime): see comment in TextInputClient::GetCaretBounds(), this
166   // conversion shouldn't be necessary.
167   RECT r = {};
168   GetClientRect(attached_window, &r);
169   POINT window_point = { screen_bounds.x(), screen_bounds.y() };
170   ScreenToClient(attached_window, &window_point);
171   gfx::Rect caret_rect(gfx::Point(window_point.x, window_point.y),
172                        screen_bounds.size());
173   imm32_manager_.UpdateCaretRect(attached_window, caret_rect);
174 }
175 
CancelComposition(const TextInputClient * client)176 void InputMethodWin::CancelComposition(const TextInputClient* client) {
177   if (enabled_ && IsTextInputClientFocused(client))
178     imm32_manager_.CancelIME(GetAttachedWindowHandle(client));
179 }
180 
OnInputLocaleChanged()181 void InputMethodWin::OnInputLocaleChanged() {
182   active_ = imm32_manager_.SetInputLanguage();
183   locale_ = imm32_manager_.GetInputLanguageName();
184   OnInputMethodChanged();
185 }
186 
GetInputLocale()187 std::string InputMethodWin::GetInputLocale() {
188   return locale_;
189 }
190 
IsActive()191 bool InputMethodWin::IsActive() {
192   return active_;
193 }
194 
IsCandidatePopupOpen() const195 bool InputMethodWin::IsCandidatePopupOpen() const {
196   return is_candidate_popup_open_;
197 }
198 
OnWillChangeFocusedClient(TextInputClient * focused_before,TextInputClient * focused)199 void InputMethodWin::OnWillChangeFocusedClient(TextInputClient* focused_before,
200                                                TextInputClient* focused) {
201   if (IsWindowFocused(focused_before))
202     ConfirmCompositionText();
203 }
204 
OnDidChangeFocusedClient(TextInputClient * focused_before,TextInputClient * focused)205 void InputMethodWin::OnDidChangeFocusedClient(
206     TextInputClient* focused_before,
207     TextInputClient* focused) {
208   if (IsWindowFocused(focused)) {
209     // Force to update the input type since client's TextInputStateChanged()
210     // function might not be called if text input types before the client loses
211     // focus and after it acquires focus again are the same.
212     OnTextInputTypeChanged(focused);
213 
214     UpdateIMEState();
215 
216     // Force to update caret bounds, in case the client thinks that the caret
217     // bounds has not changed.
218     OnCaretBoundsChanged(focused);
219   }
220   if (focused_before != focused)
221     accept_carriage_return_ = false;
222 }
223 
OnChar(HWND window_handle,UINT message,WPARAM wparam,LPARAM lparam,BOOL * handled)224 LRESULT InputMethodWin::OnChar(HWND window_handle,
225                                UINT message,
226                                WPARAM wparam,
227                                LPARAM lparam,
228                                BOOL* handled) {
229   *handled = TRUE;
230 
231   // We need to send character events to the focused text input client event if
232   // its text input type is ui::TEXT_INPUT_TYPE_NONE.
233   if (GetTextInputClient()) {
234     const base::char16 kCarriageReturn = L'\r';
235     const base::char16 ch = static_cast<base::char16>(wparam);
236     // A mask to determine the previous key state from |lparam|. The value is 1
237     // if the key is down before the message is sent, or it is 0 if the key is
238     // up.
239     const uint32 kPrevKeyDownBit = 0x40000000;
240     if (ch == kCarriageReturn && !(lparam & kPrevKeyDownBit))
241       accept_carriage_return_ = true;
242     // Conditionally ignore '\r' events to work around crbug.com/319100.
243     // TODO(yukawa, IME): Figure out long-term solution.
244     if (ch != kCarriageReturn || accept_carriage_return_)
245       GetTextInputClient()->InsertChar(ch, ui::GetModifiersFromKeyState());
246   }
247 
248   // Explicitly show the system menu at a good location on [Alt]+[Space].
249   // Note: Setting |handled| to FALSE for DefWindowProc triggering of the system
250   //       menu causes undesirable titlebar artifacts in the classic theme.
251   if (message == WM_SYSCHAR && wparam == VK_SPACE)
252     gfx::ShowSystemMenu(window_handle);
253 
254   return 0;
255 }
256 
OnImeSetContext(HWND window_handle,UINT message,WPARAM wparam,LPARAM lparam,BOOL * handled)257 LRESULT InputMethodWin::OnImeSetContext(HWND window_handle,
258                                         UINT message,
259                                         WPARAM wparam,
260                                         LPARAM lparam,
261                                         BOOL* handled) {
262   if (!!wparam)
263     imm32_manager_.CreateImeWindow(window_handle);
264 
265   OnInputMethodChanged();
266   return imm32_manager_.SetImeWindowStyle(
267       window_handle, message, wparam, lparam, handled);
268 }
269 
OnImeStartComposition(HWND window_handle,UINT message,WPARAM wparam,LPARAM lparam,BOOL * handled)270 LRESULT InputMethodWin::OnImeStartComposition(HWND window_handle,
271                                               UINT message,
272                                               WPARAM wparam,
273                                               LPARAM lparam,
274                                               BOOL* handled) {
275   // We have to prevent WTL from calling ::DefWindowProc() because the function
276   // calls ::ImmSetCompositionWindow() and ::ImmSetCandidateWindow() to
277   // over-write the position of IME windows.
278   *handled = TRUE;
279 
280   // Reset the composition status and create IME windows.
281   composing_window_handle_ = window_handle;
282   imm32_manager_.CreateImeWindow(window_handle);
283   imm32_manager_.ResetComposition(window_handle);
284   return 0;
285 }
286 
OnImeComposition(HWND window_handle,UINT message,WPARAM wparam,LPARAM lparam,BOOL * handled)287 LRESULT InputMethodWin::OnImeComposition(HWND window_handle,
288                                          UINT message,
289                                          WPARAM wparam,
290                                          LPARAM lparam,
291                                          BOOL* handled) {
292   // We have to prevent WTL from calling ::DefWindowProc() because we do not
293   // want for the IMM (Input Method Manager) to send WM_IME_CHAR messages.
294   *handled = TRUE;
295 
296   // At first, update the position of the IME window.
297   imm32_manager_.UpdateImeWindow(window_handle);
298 
299   // Retrieve the result string and its attributes of the ongoing composition
300   // and send it to a renderer process.
301   ui::CompositionText composition;
302   if (imm32_manager_.GetResult(window_handle, lparam, &composition.text)) {
303     if (!IsTextInputTypeNone())
304       GetTextInputClient()->InsertText(composition.text);
305     imm32_manager_.ResetComposition(window_handle);
306     // Fall though and try reading the composition string.
307     // Japanese IMEs send a message containing both GCS_RESULTSTR and
308     // GCS_COMPSTR, which means an ongoing composition has been finished
309     // by the start of another composition.
310   }
311   // Retrieve the composition string and its attributes of the ongoing
312   // composition and send it to a renderer process.
313   if (imm32_manager_.GetComposition(window_handle, lparam, &composition) &&
314       !IsTextInputTypeNone())
315     GetTextInputClient()->SetCompositionText(composition);
316 
317   return 0;
318 }
319 
OnImeEndComposition(HWND window_handle,UINT message,WPARAM wparam,LPARAM lparam,BOOL * handled)320 LRESULT InputMethodWin::OnImeEndComposition(HWND window_handle,
321                                             UINT message,
322                                             WPARAM wparam,
323                                             LPARAM lparam,
324                                             BOOL* handled) {
325   // Let WTL call ::DefWindowProc() and release its resources.
326   *handled = FALSE;
327 
328   composing_window_handle_ = NULL;
329 
330   if (!IsTextInputTypeNone() && GetTextInputClient()->HasCompositionText())
331     GetTextInputClient()->ClearCompositionText();
332 
333   imm32_manager_.ResetComposition(window_handle);
334   imm32_manager_.DestroyImeWindow(window_handle);
335   return 0;
336 }
337 
OnImeNotify(UINT message,WPARAM wparam,LPARAM lparam,BOOL * handled)338 LRESULT InputMethodWin::OnImeNotify(UINT message,
339                                     WPARAM wparam,
340                                     LPARAM lparam,
341                                     BOOL* handled) {
342   *handled = FALSE;
343 
344   bool previous_state = is_candidate_popup_open_;
345 
346   // Update |is_candidate_popup_open_|, whether a candidate window is open.
347   switch (wparam) {
348   case IMN_OPENCANDIDATE:
349     is_candidate_popup_open_ = true;
350     if (!previous_state)
351       OnCandidateWindowShown();
352     break;
353   case IMN_CLOSECANDIDATE:
354     is_candidate_popup_open_ = false;
355     if (previous_state)
356       OnCandidateWindowHidden();
357     break;
358   case IMN_CHANGECANDIDATE:
359     // TODO(kochi): The IME API expects this event to notify window size change,
360     // while this may fire more often without window resize. There is no generic
361     // way to get bounds of candidate window.
362     OnCandidateWindowUpdated();
363     break;
364   }
365 
366   return 0;
367 }
368 
OnImeRequest(UINT message,WPARAM wparam,LPARAM lparam,BOOL * handled)369 LRESULT InputMethodWin::OnImeRequest(UINT message,
370                                      WPARAM wparam,
371                                      LPARAM lparam,
372                                      BOOL* handled) {
373   *handled = FALSE;
374 
375   // Should not receive WM_IME_REQUEST message, if IME is disabled.
376   const ui::TextInputType type = GetTextInputType();
377   if (type == ui::TEXT_INPUT_TYPE_NONE ||
378       type == ui::TEXT_INPUT_TYPE_PASSWORD) {
379     return 0;
380   }
381 
382   switch (wparam) {
383     case IMR_RECONVERTSTRING:
384       *handled = TRUE;
385       return OnReconvertString(reinterpret_cast<RECONVERTSTRING*>(lparam));
386     case IMR_DOCUMENTFEED:
387       *handled = TRUE;
388       return OnDocumentFeed(reinterpret_cast<RECONVERTSTRING*>(lparam));
389     case IMR_QUERYCHARPOSITION:
390       *handled = TRUE;
391       return OnQueryCharPosition(reinterpret_cast<IMECHARPOSITION*>(lparam));
392     default:
393       return 0;
394   }
395 }
396 
OnDocumentFeed(RECONVERTSTRING * reconv)397 LRESULT InputMethodWin::OnDocumentFeed(RECONVERTSTRING* reconv) {
398   ui::TextInputClient* client = GetTextInputClient();
399   if (!client)
400     return 0;
401 
402   gfx::Range text_range;
403   if (!client->GetTextRange(&text_range) || text_range.is_empty())
404     return 0;
405 
406   bool result = false;
407   gfx::Range target_range;
408   if (client->HasCompositionText())
409     result = client->GetCompositionTextRange(&target_range);
410 
411   if (!result || target_range.is_empty()) {
412     if (!client->GetSelectionRange(&target_range) ||
413         !target_range.IsValid()) {
414       return 0;
415     }
416   }
417 
418   if (!text_range.Contains(target_range))
419     return 0;
420 
421   if (target_range.GetMin() - text_range.start() > kExtraNumberOfChars)
422     text_range.set_start(target_range.GetMin() - kExtraNumberOfChars);
423 
424   if (text_range.end() - target_range.GetMax() > kExtraNumberOfChars)
425     text_range.set_end(target_range.GetMax() + kExtraNumberOfChars);
426 
427   size_t len = text_range.length();
428   size_t need_size = sizeof(RECONVERTSTRING) + len * sizeof(WCHAR);
429 
430   if (!reconv)
431     return need_size;
432 
433   if (reconv->dwSize < need_size)
434     return 0;
435 
436   base::string16 text;
437   if (!GetTextInputClient()->GetTextFromRange(text_range, &text))
438     return 0;
439   DCHECK_EQ(text_range.length(), text.length());
440 
441   reconv->dwVersion = 0;
442   reconv->dwStrLen = len;
443   reconv->dwStrOffset = sizeof(RECONVERTSTRING);
444   reconv->dwCompStrLen =
445       client->HasCompositionText() ? target_range.length() : 0;
446   reconv->dwCompStrOffset =
447       (target_range.GetMin() - text_range.start()) * sizeof(WCHAR);
448   reconv->dwTargetStrLen = target_range.length();
449   reconv->dwTargetStrOffset = reconv->dwCompStrOffset;
450 
451   memcpy((char*)reconv + sizeof(RECONVERTSTRING),
452          text.c_str(), len * sizeof(WCHAR));
453 
454   // According to Microsoft API document, IMR_RECONVERTSTRING and
455   // IMR_DOCUMENTFEED should return reconv, but some applications return
456   // need_size.
457   return reinterpret_cast<LRESULT>(reconv);
458 }
459 
OnReconvertString(RECONVERTSTRING * reconv)460 LRESULT InputMethodWin::OnReconvertString(RECONVERTSTRING* reconv) {
461   ui::TextInputClient* client = GetTextInputClient();
462   if (!client)
463     return 0;
464 
465   // If there is a composition string already, we don't allow reconversion.
466   if (client->HasCompositionText())
467     return 0;
468 
469   gfx::Range text_range;
470   if (!client->GetTextRange(&text_range) || text_range.is_empty())
471     return 0;
472 
473   gfx::Range selection_range;
474   if (!client->GetSelectionRange(&selection_range) ||
475       selection_range.is_empty()) {
476     return 0;
477   }
478 
479   DCHECK(text_range.Contains(selection_range));
480 
481   size_t len = selection_range.length();
482   size_t need_size = sizeof(RECONVERTSTRING) + len * sizeof(WCHAR);
483 
484   if (!reconv)
485     return need_size;
486 
487   if (reconv->dwSize < need_size)
488     return 0;
489 
490   // TODO(penghuang): Return some extra context to help improve IME's
491   // reconversion accuracy.
492   base::string16 text;
493   if (!GetTextInputClient()->GetTextFromRange(selection_range, &text))
494     return 0;
495   DCHECK_EQ(selection_range.length(), text.length());
496 
497   reconv->dwVersion = 0;
498   reconv->dwStrLen = len;
499   reconv->dwStrOffset = sizeof(RECONVERTSTRING);
500   reconv->dwCompStrLen = len;
501   reconv->dwCompStrOffset = 0;
502   reconv->dwTargetStrLen = len;
503   reconv->dwTargetStrOffset = 0;
504 
505   memcpy(reinterpret_cast<char*>(reconv) + sizeof(RECONVERTSTRING),
506          text.c_str(), len * sizeof(WCHAR));
507 
508   // According to Microsoft API document, IMR_RECONVERTSTRING and
509   // IMR_DOCUMENTFEED should return reconv, but some applications return
510   // need_size.
511   return reinterpret_cast<LRESULT>(reconv);
512 }
513 
OnQueryCharPosition(IMECHARPOSITION * char_positon)514 LRESULT InputMethodWin::OnQueryCharPosition(IMECHARPOSITION* char_positon) {
515   if (!char_positon)
516     return 0;
517 
518   if (char_positon->dwSize < sizeof(IMECHARPOSITION))
519     return 0;
520 
521   ui::TextInputClient* client = GetTextInputClient();
522   if (!client)
523     return 0;
524 
525   // Tentatively assume that the returned value from |client| is DIP (Density
526   // Independent Pixel). See the comment in text_input_client.h and
527   // http://crbug.com/360334.
528   gfx::Rect dip_rect;
529   if (client->HasCompositionText()) {
530     if (!client->GetCompositionCharacterBounds(char_positon->dwCharPos,
531                                                &dip_rect)) {
532       return 0;
533     }
534   } else {
535     // If there is no composition and the first character is queried, returns
536     // the caret bounds. This behavior is the same to that of RichEdit control.
537     if (char_positon->dwCharPos != 0)
538       return 0;
539     dip_rect = client->GetCaretBounds();
540   }
541   const gfx::Rect rect = gfx::win::DIPToScreenRect(dip_rect);
542 
543   char_positon->pt.x = rect.x();
544   char_positon->pt.y = rect.y();
545   char_positon->cLineHeight = rect.height();
546   return 1;  // returns non-zero value when succeeded.
547 }
548 
GetAttachedWindowHandle(const TextInputClient * text_input_client) const549 HWND InputMethodWin::GetAttachedWindowHandle(
550   const TextInputClient* text_input_client) const {
551   // On Aura environment, we can assume that |toplevel_window_handle_| always
552   // represents the valid top-level window handle because each top-level window
553   // is responsible for lifecycle management of corresponding InputMethod
554   // instance.
555   return toplevel_window_handle_;
556 }
557 
IsWindowFocused(const TextInputClient * client) const558 bool InputMethodWin::IsWindowFocused(const TextInputClient* client) const {
559   if (!client)
560     return false;
561   HWND attached_window_handle = GetAttachedWindowHandle(client);
562   // When Aura is enabled, |attached_window_handle| should always be a top-level
563   // window. So we can safely assume that |attached_window_handle| is ready for
564   // receiving keyboard input as long as it is an active window. This works well
565   // even when the |attached_window_handle| becomes active but has not received
566   // WM_FOCUS yet.
567   return attached_window_handle && GetActiveWindow() == attached_window_handle;
568 }
569 
DispatchFabricatedKeyEvent(const ui::KeyEvent & event)570 bool InputMethodWin::DispatchFabricatedKeyEvent(const ui::KeyEvent& event) {
571   if (event.is_char()) {
572     if (GetTextInputClient()) {
573       GetTextInputClient()->InsertChar(event.key_code(),
574                                        ui::GetModifiersFromKeyState());
575       return true;
576     }
577   }
578   return DispatchKeyEventPostIME(event);
579 }
580 
ConfirmCompositionText()581 void InputMethodWin::ConfirmCompositionText() {
582   if (composing_window_handle_)
583     imm32_manager_.CleanupComposition(composing_window_handle_);
584 
585   if (!IsTextInputTypeNone()) {
586     // Though above line should confirm the client's composition text by sending
587     // a result text to us, in case the input method and the client are in
588     // inconsistent states, we check the client's composition state again.
589     if (GetTextInputClient()->HasCompositionText())
590       GetTextInputClient()->ConfirmCompositionText();
591   }
592 }
593 
UpdateIMEState()594 void InputMethodWin::UpdateIMEState() {
595   // Use switch here in case we are going to add more text input types.
596   // We disable input method in password field.
597   const HWND window_handle = GetAttachedWindowHandle(GetTextInputClient());
598   const TextInputType text_input_type = GetTextInputType();
599   const TextInputMode text_input_mode = GetTextInputMode();
600   switch (text_input_type) {
601     case ui::TEXT_INPUT_TYPE_NONE:
602     case ui::TEXT_INPUT_TYPE_PASSWORD:
603       imm32_manager_.DisableIME(window_handle);
604       enabled_ = false;
605       break;
606     default:
607       imm32_manager_.EnableIME(window_handle);
608       enabled_ = true;
609       break;
610   }
611 
612   imm32_manager_.SetTextInputMode(window_handle, text_input_mode);
613   tsf_inputscope::SetInputScopeForTsfUnawareWindow(
614       window_handle, text_input_type, text_input_mode);
615 }
616 
617 }  // namespace ui
618