Index: ui/base/ime/input_method_win.cc |
diff --git a/ui/base/ime/input_method_win.cc b/ui/base/ime/input_method_win.cc |
index ddecd1db6a6f2a3ca752b851dea573fd778be5ac..acabe8fd545bbdc5da55739de62860236fa2efda 100644 |
--- a/ui/base/ime/input_method_win.cc |
+++ b/ui/base/ime/input_method_win.cc |
@@ -6,6 +6,7 @@ |
#include "base/basictypes.h" |
#include "ui/base/ime/text_input_client.h" |
+#include "ui/base/ime/win/tsf_input_scope.h" |
#include "ui/events/event.h" |
#include "ui/events/event_constants.h" |
#include "ui/events/event_utils.h" |
@@ -23,11 +24,20 @@ static const size_t kExtraNumberOfChars = 20; |
InputMethodWin::InputMethodWin(internal::InputMethodDelegate* delegate, |
HWND toplevel_window_handle) |
- : active_(false), |
- toplevel_window_handle_(toplevel_window_handle), |
+ : toplevel_window_handle_(toplevel_window_handle), |
pending_requested_direction_(base::i18n::UNKNOWN_DIRECTION), |
- accept_carriage_return_(false) { |
+ accept_carriage_return_(false), |
+ active_(false), |
+ enabled_(false), |
+ is_candidate_popup_open_(false), |
+ composing_window_handle_(NULL) { |
SetDelegate(delegate); |
+ // In non-Aura environment, appropriate callbacks to OnFocus() and OnBlur() |
+ // are not implemented yet. To work around this limitation, here we use |
+ // "always focused" model. |
+ // TODO(ime): Fix the caller of OnFocus() and OnBlur() so that appropriate |
+ // focus event will be passed. |
+ InputMethodBase::OnFocus(); |
} |
void InputMethodWin::Init(bool focused) { |
@@ -37,6 +47,62 @@ void InputMethodWin::Init(bool focused) { |
InputMethodBase::Init(focused); |
} |
+void InputMethodWin::OnFocus() { |
+ // Ignore OnFocus event for "always focused" model. See the comment in the |
+ // constructor. |
+ // TODO(ime): Implement OnFocus once the callers are fixed. |
+} |
+ |
+void InputMethodWin::OnBlur() { |
+ // Ignore OnBlur event for "always focused" model. See the comment in the |
+ // constructor. |
+ // TODO(ime): Implement OnFocus once the callers are fixed. |
+} |
+ |
+bool InputMethodWin::OnUntranslatedIMEMessage( |
+ const base::NativeEvent& event, |
+ InputMethod::NativeEventResult* result) { |
+ LRESULT original_result = 0; |
+ BOOL handled = FALSE; |
+ switch (event.message) { |
+ case WM_IME_SETCONTEXT: |
+ original_result = OnImeSetContext( |
+ event.hwnd, event.message, event.wParam, event.lParam, &handled); |
+ break; |
+ case WM_IME_STARTCOMPOSITION: |
+ original_result = OnImeStartComposition( |
+ event.hwnd, event.message, event.wParam, event.lParam, &handled); |
+ break; |
+ case WM_IME_COMPOSITION: |
+ original_result = OnImeComposition( |
+ event.hwnd, event.message, event.wParam, event.lParam, &handled); |
+ break; |
+ case WM_IME_ENDCOMPOSITION: |
+ original_result = OnImeEndComposition( |
+ event.hwnd, event.message, event.wParam, event.lParam, &handled); |
+ break; |
+ case WM_IME_REQUEST: |
+ original_result = OnImeRequest( |
+ event.message, event.wParam, event.lParam, &handled); |
+ break; |
+ case WM_CHAR: |
+ case WM_SYSCHAR: |
+ original_result = OnChar( |
+ event.hwnd, event.message, event.wParam, event.lParam, &handled); |
+ break; |
+ case WM_IME_NOTIFY: |
+ original_result = OnImeNotify( |
+ event.message, event.wParam, event.lParam, &handled); |
+ break; |
+ default: |
+ NOTREACHED() << "Unknown IME message:" << event.message; |
+ break; |
+ } |
+ if (result) |
+ *result = original_result; |
+ return !!handled; |
+} |
+ |
bool InputMethodWin::DispatchKeyEvent(const ui::KeyEvent& event) { |
if (!event.HasNativeEvent()) |
return DispatchFabricatedKeyEvent(event); |
@@ -75,6 +141,39 @@ bool InputMethodWin::DispatchKeyEvent(const ui::KeyEvent& event) { |
return DispatchKeyEventPostIME(event); |
} |
+void InputMethodWin::OnTextInputTypeChanged(const TextInputClient* client) { |
+ if (!IsTextInputClientFocused(client) || !IsWindowFocused(client)) |
+ return; |
+ imm32_manager_.CancelIME(GetAttachedWindowHandle(client)); |
+ UpdateIMEState(); |
+} |
+ |
+void InputMethodWin::OnCaretBoundsChanged(const TextInputClient* client) { |
+ if (!enabled_ || !IsTextInputClientFocused(client) || |
+ !IsWindowFocused(client)) { |
+ return; |
+ } |
+ // The current text input type should not be NONE if |client| is focused. |
+ DCHECK(!IsTextInputTypeNone()); |
+ gfx::Rect screen_bounds(GetTextInputClient()->GetCaretBounds()); |
+ |
+ HWND attached_window = GetAttachedWindowHandle(client); |
+ // TODO(ime): see comment in TextInputClient::GetCaretBounds(), this |
+ // conversion shouldn't be necessary. |
+ RECT r = {}; |
+ GetClientRect(attached_window, &r); |
+ POINT window_point = { screen_bounds.x(), screen_bounds.y() }; |
+ ScreenToClient(attached_window, &window_point); |
+ gfx::Rect caret_rect(gfx::Point(window_point.x, window_point.y), |
+ screen_bounds.size()); |
+ imm32_manager_.UpdateCaretRect(attached_window, caret_rect); |
+} |
+ |
+void InputMethodWin::CancelComposition(const TextInputClient* client) { |
+ if (enabled_ && IsTextInputClientFocused(client)) |
+ imm32_manager_.CancelIME(GetAttachedWindowHandle(client)); |
+} |
+ |
void InputMethodWin::OnInputLocaleChanged() { |
active_ = imm32_manager_.SetInputLanguage(); |
locale_ = imm32_manager_.GetInputLanguageName(); |
@@ -89,39 +188,33 @@ bool InputMethodWin::IsActive() { |
return active_; |
} |
+bool InputMethodWin::IsCandidatePopupOpen() const { |
+ return is_candidate_popup_open_; |
+} |
+ |
+void InputMethodWin::OnWillChangeFocusedClient(TextInputClient* focused_before, |
+ TextInputClient* focused) { |
+ if (IsWindowFocused(focused_before)) |
+ ConfirmCompositionText(); |
+} |
+ |
void InputMethodWin::OnDidChangeFocusedClient( |
TextInputClient* focused_before, |
TextInputClient* focused) { |
- if (focused_before != focused) |
- accept_carriage_return_ = false; |
-} |
+ if (IsWindowFocused(focused)) { |
+ // Force to update the input type since client's TextInputStateChanged() |
+ // function might not be called if text input types before the client loses |
+ // focus and after it acquires focus again are the same. |
+ OnTextInputTypeChanged(focused); |
-LRESULT InputMethodWin::OnImeRequest(UINT message, |
- WPARAM wparam, |
- LPARAM lparam, |
- BOOL* handled) { |
- *handled = FALSE; |
+ UpdateIMEState(); |
- // Should not receive WM_IME_REQUEST message, if IME is disabled. |
- const ui::TextInputType type = GetTextInputType(); |
- if (type == ui::TEXT_INPUT_TYPE_NONE || |
- type == ui::TEXT_INPUT_TYPE_PASSWORD) { |
- return 0; |
- } |
- |
- switch (wparam) { |
- case IMR_RECONVERTSTRING: |
- *handled = TRUE; |
- return OnReconvertString(reinterpret_cast<RECONVERTSTRING*>(lparam)); |
- case IMR_DOCUMENTFEED: |
- *handled = TRUE; |
- return OnDocumentFeed(reinterpret_cast<RECONVERTSTRING*>(lparam)); |
- case IMR_QUERYCHARPOSITION: |
- *handled = TRUE; |
- return OnQueryCharPosition(reinterpret_cast<IMECHARPOSITION*>(lparam)); |
- default: |
- return 0; |
+ // Force to update caret bounds, in case the client thinks that the caret |
+ // bounds has not changed. |
+ OnCaretBoundsChanged(focused); |
} |
+ if (focused_before != focused) |
+ accept_carriage_return_ = false; |
} |
LRESULT InputMethodWin::OnChar(HWND window_handle, |
@@ -157,6 +250,146 @@ LRESULT InputMethodWin::OnChar(HWND window_handle, |
return 0; |
} |
+LRESULT InputMethodWin::OnImeSetContext(HWND window_handle, |
+ UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam, |
+ BOOL* handled) { |
+ if (!!wparam) |
+ imm32_manager_.CreateImeWindow(window_handle); |
+ |
+ OnInputMethodChanged(); |
+ return imm32_manager_.SetImeWindowStyle( |
+ window_handle, message, wparam, lparam, handled); |
+} |
+ |
+LRESULT InputMethodWin::OnImeStartComposition(HWND window_handle, |
+ UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam, |
+ BOOL* handled) { |
+ // We have to prevent WTL from calling ::DefWindowProc() because the function |
+ // calls ::ImmSetCompositionWindow() and ::ImmSetCandidateWindow() to |
+ // over-write the position of IME windows. |
+ *handled = TRUE; |
+ |
+ // Reset the composition status and create IME windows. |
+ composing_window_handle_ = window_handle; |
+ imm32_manager_.CreateImeWindow(window_handle); |
+ imm32_manager_.ResetComposition(window_handle); |
+ return 0; |
+} |
+ |
+LRESULT InputMethodWin::OnImeComposition(HWND window_handle, |
+ UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam, |
+ BOOL* handled) { |
+ // We have to prevent WTL from calling ::DefWindowProc() because we do not |
+ // want for the IMM (Input Method Manager) to send WM_IME_CHAR messages. |
+ *handled = TRUE; |
+ |
+ // At first, update the position of the IME window. |
+ imm32_manager_.UpdateImeWindow(window_handle); |
+ |
+ // Retrieve the result string and its attributes of the ongoing composition |
+ // and send it to a renderer process. |
+ ui::CompositionText composition; |
+ if (imm32_manager_.GetResult(window_handle, lparam, &composition.text)) { |
+ if (!IsTextInputTypeNone()) |
+ GetTextInputClient()->InsertText(composition.text); |
+ imm32_manager_.ResetComposition(window_handle); |
+ // Fall though and try reading the composition string. |
+ // Japanese IMEs send a message containing both GCS_RESULTSTR and |
+ // GCS_COMPSTR, which means an ongoing composition has been finished |
+ // by the start of another composition. |
+ } |
+ // Retrieve the composition string and its attributes of the ongoing |
+ // composition and send it to a renderer process. |
+ if (imm32_manager_.GetComposition(window_handle, lparam, &composition) && |
+ !IsTextInputTypeNone()) |
+ GetTextInputClient()->SetCompositionText(composition); |
+ |
+ return 0; |
+} |
+ |
+LRESULT InputMethodWin::OnImeEndComposition(HWND window_handle, |
+ UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam, |
+ BOOL* handled) { |
+ // Let WTL call ::DefWindowProc() and release its resources. |
+ *handled = FALSE; |
+ |
+ composing_window_handle_ = NULL; |
+ |
+ if (!IsTextInputTypeNone() && GetTextInputClient()->HasCompositionText()) |
+ GetTextInputClient()->ClearCompositionText(); |
+ |
+ imm32_manager_.ResetComposition(window_handle); |
+ imm32_manager_.DestroyImeWindow(window_handle); |
+ return 0; |
+} |
+ |
+LRESULT InputMethodWin::OnImeNotify(UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam, |
+ BOOL* handled) { |
+ *handled = FALSE; |
+ |
+ bool previous_state = is_candidate_popup_open_; |
+ |
+ // Update |is_candidate_popup_open_|, whether a candidate window is open. |
+ switch (wparam) { |
+ case IMN_OPENCANDIDATE: |
+ is_candidate_popup_open_ = true; |
+ if (!previous_state) |
+ OnCandidateWindowShown(); |
+ break; |
+ case IMN_CLOSECANDIDATE: |
+ is_candidate_popup_open_ = false; |
+ if (previous_state) |
+ OnCandidateWindowHidden(); |
+ break; |
+ case IMN_CHANGECANDIDATE: |
+ // TODO(kochi): The IME API expects this event to notify window size change, |
+ // while this may fire more often without window resize. There is no generic |
+ // way to get bounds of candidate window. |
+ OnCandidateWindowUpdated(); |
+ break; |
+ } |
+ |
+ return 0; |
+} |
+ |
+LRESULT InputMethodWin::OnImeRequest(UINT message, |
+ WPARAM wparam, |
+ LPARAM lparam, |
+ BOOL* handled) { |
+ *handled = FALSE; |
+ |
+ // Should not receive WM_IME_REQUEST message, if IME is disabled. |
+ const ui::TextInputType type = GetTextInputType(); |
+ if (type == ui::TEXT_INPUT_TYPE_NONE || |
+ type == ui::TEXT_INPUT_TYPE_PASSWORD) { |
+ return 0; |
+ } |
+ |
+ switch (wparam) { |
+ case IMR_RECONVERTSTRING: |
+ *handled = TRUE; |
+ return OnReconvertString(reinterpret_cast<RECONVERTSTRING*>(lparam)); |
+ case IMR_DOCUMENTFEED: |
+ *handled = TRUE; |
+ return OnDocumentFeed(reinterpret_cast<RECONVERTSTRING*>(lparam)); |
+ case IMR_QUERYCHARPOSITION: |
+ *handled = TRUE; |
+ return OnQueryCharPosition(reinterpret_cast<IMECHARPOSITION*>(lparam)); |
+ default: |
+ return 0; |
+ } |
+} |
+ |
LRESULT InputMethodWin::OnDocumentFeed(RECONVERTSTRING* reconv) { |
ui::TextInputClient* client = GetTextInputClient(); |
if (!client) |
@@ -337,4 +570,40 @@ bool InputMethodWin::DispatchFabricatedKeyEvent(const ui::KeyEvent& event) { |
return DispatchKeyEventPostIME(event); |
} |
+void InputMethodWin::ConfirmCompositionText() { |
+ if (composing_window_handle_) |
+ imm32_manager_.CleanupComposition(composing_window_handle_); |
+ |
+ if (!IsTextInputTypeNone()) { |
+ // Though above line should confirm the client's composition text by sending |
+ // a result text to us, in case the input method and the client are in |
+ // inconsistent states, we check the client's composition state again. |
+ if (GetTextInputClient()->HasCompositionText()) |
+ GetTextInputClient()->ConfirmCompositionText(); |
+ } |
+} |
+ |
+void InputMethodWin::UpdateIMEState() { |
+ // Use switch here in case we are going to add more text input types. |
+ // We disable input method in password field. |
+ const HWND window_handle = GetAttachedWindowHandle(GetTextInputClient()); |
+ const TextInputType text_input_type = GetTextInputType(); |
+ const TextInputMode text_input_mode = GetTextInputMode(); |
+ switch (text_input_type) { |
+ case ui::TEXT_INPUT_TYPE_NONE: |
+ case ui::TEXT_INPUT_TYPE_PASSWORD: |
+ imm32_manager_.DisableIME(window_handle); |
+ enabled_ = false; |
+ break; |
+ default: |
+ imm32_manager_.EnableIME(window_handle); |
+ enabled_ = true; |
+ break; |
+ } |
+ |
+ imm32_manager_.SetTextInputMode(window_handle, text_input_mode); |
+ tsf_inputscope::SetInputScopeForTsfUnawareWindow( |
+ window_handle, text_input_type, text_input_mode); |
+} |
+ |
} // namespace ui |