| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/base/ime/input_method_win.h" | 5 #include "ui/base/ime/input_method_win.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/profiler/scoped_tracker.h" | 9 #include "base/profiler/scoped_tracker.h" |
| 10 #include "ui/base/ime/text_input_client.h" | 10 #include "ui/base/ime/text_input_client.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 InputMethodWin::InputMethodWin(internal::InputMethodDelegate* delegate, | 28 InputMethodWin::InputMethodWin(internal::InputMethodDelegate* delegate, |
| 29 HWND toplevel_window_handle) | 29 HWND toplevel_window_handle) |
| 30 : toplevel_window_handle_(toplevel_window_handle), | 30 : toplevel_window_handle_(toplevel_window_handle), |
| 31 pending_requested_direction_(base::i18n::UNKNOWN_DIRECTION), | 31 pending_requested_direction_(base::i18n::UNKNOWN_DIRECTION), |
| 32 accept_carriage_return_(false), | 32 accept_carriage_return_(false), |
| 33 enabled_(false), | 33 enabled_(false), |
| 34 is_candidate_popup_open_(false), | 34 is_candidate_popup_open_(false), |
| 35 composing_window_handle_(NULL), | 35 composing_window_handle_(NULL) { |
| 36 suppress_next_char_(false) { | |
| 37 SetDelegate(delegate); | 36 SetDelegate(delegate); |
| 38 } | 37 } |
| 39 | 38 |
| 40 void InputMethodWin::OnFocus() { | 39 void InputMethodWin::OnFocus() { |
| 41 InputMethodBase::OnFocus(); | 40 InputMethodBase::OnFocus(); |
| 42 if (GetTextInputClient()) | 41 if (GetTextInputClient()) |
| 43 UpdateIMEState(); | 42 UpdateIMEState(); |
| 44 } | 43 } |
| 45 | 44 |
| 46 void InputMethodWin::OnBlur() { | 45 void InputMethodWin::OnBlur() { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 *result = original_result; | 96 *result = original_result; |
| 98 return !!handled; | 97 return !!handled; |
| 99 } | 98 } |
| 100 | 99 |
| 101 void InputMethodWin::DispatchKeyEvent(ui::KeyEvent* event) { | 100 void InputMethodWin::DispatchKeyEvent(ui::KeyEvent* event) { |
| 102 if (!event->HasNativeEvent()) { | 101 if (!event->HasNativeEvent()) { |
| 103 DispatchFabricatedKeyEvent(event); | 102 DispatchFabricatedKeyEvent(event); |
| 104 return; | 103 return; |
| 105 } | 104 } |
| 106 | 105 |
| 106 std::vector<MSG> char_msgs; |
| 107 const base::NativeEvent& native_key_event = event->native_event(); | 107 const base::NativeEvent& native_key_event = event->native_event(); |
| 108 ::TranslateMessage(&native_key_event); |
| 109 // Peek & remove the following messages in the message queue. |
| 110 // - WM_CHAR (0x0102) |
| 111 // - WM_DEADCHAR (0x0103) |
| 112 // - WM_SYSCHAR (0x0106) |
| 113 // - WM_SYSDEADCHAR (0x0107) |
| 114 // And only process WM_CHAR & WM_SYSCHAR message in browser. |
| 115 MSG msg; |
| 116 while (::PeekMessage(&msg, native_key_event.hwnd, WM_CHAR, WM_DEADCHAR, |
| 117 PM_REMOVE)) { |
| 118 if (char_msg.message == WM_CHAR) |
| 119 char_msgs.push_back(msg); |
| 120 } |
| 121 while (::PeekMessage(&msg, native_key_event.hwnd, WM_SYSCHAR, WM_SYSDEADCHAR, |
| 122 PM_REMOVE)) { |
| 123 if (char_msg.message == WM_SYSCHAR) |
| 124 char_msgs.push_back(msg); |
| 125 } |
| 126 |
| 127 BOOL handled = FALSE; |
| 108 if (native_key_event.message == WM_CHAR) { | 128 if (native_key_event.message == WM_CHAR) { |
| 109 BOOL handled; | |
| 110 OnChar(native_key_event.hwnd, native_key_event.message, | 129 OnChar(native_key_event.hwnd, native_key_event.message, |
| 111 native_key_event.wParam, native_key_event.lParam, &handled); | 130 native_key_event.wParam, native_key_event.lParam, &handled); |
| 112 if (handled) | 131 if (handled) |
| 113 event->StopPropagation(); | 132 event->StopPropagation(); |
| 114 return; | 133 return; |
| 115 } | 134 } |
| 116 // Handles ctrl-shift key to change text direction and layout alignment. | 135 // Handles ctrl-shift key to change text direction and layout alignment. |
| 117 if (ui::IMM32Manager::IsRTLKeyboardLayoutInstalled() && | 136 if (ui::IMM32Manager::IsRTLKeyboardLayoutInstalled() && |
| 118 !IsTextInputTypeNone()) { | 137 !IsTextInputTypeNone()) { |
| 119 // TODO: shouldn't need to generate a KeyEvent here. | 138 // TODO: shouldn't need to generate a KeyEvent here. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 130 } else if (key.type() == ui::ET_KEY_RELEASED && | 149 } else if (key.type() == ui::ET_KEY_RELEASED && |
| 131 (code == ui::VKEY_SHIFT || code == ui::VKEY_CONTROL) && | 150 (code == ui::VKEY_SHIFT || code == ui::VKEY_CONTROL) && |
| 132 pending_requested_direction_ != base::i18n::UNKNOWN_DIRECTION) { | 151 pending_requested_direction_ != base::i18n::UNKNOWN_DIRECTION) { |
| 133 GetTextInputClient()->ChangeTextDirectionAndLayoutAlignment( | 152 GetTextInputClient()->ChangeTextDirectionAndLayoutAlignment( |
| 134 pending_requested_direction_); | 153 pending_requested_direction_); |
| 135 pending_requested_direction_ = base::i18n::UNKNOWN_DIRECTION; | 154 pending_requested_direction_ = base::i18n::UNKNOWN_DIRECTION; |
| 136 } | 155 } |
| 137 } | 156 } |
| 138 | 157 |
| 139 ui::EventDispatchDetails details = DispatchKeyEventPostIME(event); | 158 ui::EventDispatchDetails details = DispatchKeyEventPostIME(event); |
| 140 if (!details.dispatcher_destroyed) | 159 if (details.dispatcher_destroyed || details.target_destroyed || |
| 141 suppress_next_char_ = event->stopped_propagation(); | 160 event->stopped_propagation()) { |
| 161 return; |
| 162 } |
| 163 |
| 164 for (size_t i = 0; i < char_msgs.size(); ++i) { |
| 165 msg = char_msgs[i]; |
| 166 OnChar(msg.hwnd, msg.message, msg.wParam, msg.lParam, &handled); |
| 167 } |
| 142 } | 168 } |
| 143 | 169 |
| 144 void InputMethodWin::OnTextInputTypeChanged(const TextInputClient* client) { | 170 void InputMethodWin::OnTextInputTypeChanged(const TextInputClient* client) { |
| 145 if (!IsTextInputClientFocused(client) || !IsWindowFocused(client)) | 171 if (!IsTextInputClientFocused(client) || !IsWindowFocused(client)) |
| 146 return; | 172 return; |
| 147 imm32_manager_.CancelIME(toplevel_window_handle_); | 173 imm32_manager_.CancelIME(toplevel_window_handle_); |
| 148 UpdateIMEState(); | 174 UpdateIMEState(); |
| 149 } | 175 } |
| 150 | 176 |
| 151 void InputMethodWin::OnCaretBoundsChanged(const TextInputClient* client) { | 177 void InputMethodWin::OnCaretBoundsChanged(const TextInputClient* client) { |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 UINT message, | 243 UINT message, |
| 218 WPARAM wparam, | 244 WPARAM wparam, |
| 219 LPARAM lparam, | 245 LPARAM lparam, |
| 220 BOOL* handled) { | 246 BOOL* handled) { |
| 221 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed. | 247 // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed. |
| 222 tracked_objects::ScopedTracker tracking_profile( | 248 tracked_objects::ScopedTracker tracking_profile( |
| 223 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 InputMethodWin::OnChar")); | 249 FROM_HERE_WITH_EXPLICIT_FUNCTION("440919 InputMethodWin::OnChar")); |
| 224 | 250 |
| 225 *handled = TRUE; | 251 *handled = TRUE; |
| 226 | 252 |
| 227 if (suppress_next_char_) { | |
| 228 suppress_next_char_ = false; | |
| 229 return 0; | |
| 230 } | |
| 231 | |
| 232 // We need to send character events to the focused text input client event if | 253 // We need to send character events to the focused text input client event if |
| 233 // its text input type is ui::TEXT_INPUT_TYPE_NONE. | 254 // its text input type is ui::TEXT_INPUT_TYPE_NONE. |
| 234 if (GetTextInputClient()) { | 255 if (GetTextInputClient()) { |
| 235 const base::char16 kCarriageReturn = L'\r'; | 256 const base::char16 kCarriageReturn = L'\r'; |
| 236 const base::char16 ch = static_cast<base::char16>(wparam); | 257 const base::char16 ch = static_cast<base::char16>(wparam); |
| 237 // A mask to determine the previous key state from |lparam|. The value is 1 | 258 // A mask to determine the previous key state from |lparam|. The value is 1 |
| 238 // if the key is down before the message is sent, or it is 0 if the key is | 259 // if the key is down before the message is sent, or it is 0 if the key is |
| 239 // up. | 260 // up. |
| 240 const uint32 kPrevKeyDownBit = 0x40000000; | 261 const uint32 kPrevKeyDownBit = 0x40000000; |
| 241 if (ch == kCarriageReturn && !(lparam & kPrevKeyDownBit)) | 262 if (ch == kCarriageReturn && !(lparam & kPrevKeyDownBit)) |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 // window. So we can safely assume that |attached_window_handle| is ready for | 601 // window. So we can safely assume that |attached_window_handle| is ready for |
| 581 // receiving keyboard input as long as it is an active window. This works well | 602 // receiving keyboard input as long as it is an active window. This works well |
| 582 // even when the |attached_window_handle| becomes active but has not received | 603 // even when the |attached_window_handle| becomes active but has not received |
| 583 // WM_FOCUS yet. | 604 // WM_FOCUS yet. |
| 584 return toplevel_window_handle_ && | 605 return toplevel_window_handle_ && |
| 585 GetActiveWindow() == toplevel_window_handle_; | 606 GetActiveWindow() == toplevel_window_handle_; |
| 586 } | 607 } |
| 587 | 608 |
| 588 void InputMethodWin::DispatchFabricatedKeyEvent(ui::KeyEvent* event) { | 609 void InputMethodWin::DispatchFabricatedKeyEvent(ui::KeyEvent* event) { |
| 589 if (event->is_char()) { | 610 if (event->is_char()) { |
| 590 if (suppress_next_char_) { | |
| 591 suppress_next_char_ = false; | |
| 592 return; | |
| 593 } | |
| 594 if (GetTextInputClient()) { | 611 if (GetTextInputClient()) { |
| 595 GetTextInputClient()->InsertChar( | 612 GetTextInputClient()->InsertChar( |
| 596 static_cast<base::char16>(event->key_code()), | 613 static_cast<base::char16>(event->key_code()), |
| 597 ui::GetModifiersFromKeyState()); | 614 ui::GetModifiersFromKeyState()); |
| 598 return; | 615 return; |
| 599 } | 616 } |
| 600 } | 617 } |
| 601 ignore_result(DispatchKeyEventPostIME(event)); | 618 ignore_result(DispatchKeyEventPostIME(event)); |
| 602 } | 619 } |
| 603 | 620 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 631 enabled_ = true; | 648 enabled_ = true; |
| 632 break; | 649 break; |
| 633 } | 650 } |
| 634 | 651 |
| 635 imm32_manager_.SetTextInputMode(window_handle, text_input_mode); | 652 imm32_manager_.SetTextInputMode(window_handle, text_input_mode); |
| 636 tsf_inputscope::SetInputScopeForTsfUnawareWindow( | 653 tsf_inputscope::SetInputScopeForTsfUnawareWindow( |
| 637 window_handle, text_input_type, text_input_mode); | 654 window_handle, text_input_type, text_input_mode); |
| 638 } | 655 } |
| 639 | 656 |
| 640 } // namespace ui | 657 } // namespace ui |
| OLD | NEW |