OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/views/ime/input_method_bridge.h" |
| 6 |
| 7 #include "ui/base/ime/input_method.h" |
| 8 #include "ui/gfx/rect.h" |
| 9 #include "views/view.h" |
| 10 #include "views/widget/widget.h" |
| 11 |
| 12 namespace views { |
| 13 |
| 14 InputMethodBridge::InputMethodBridge(internal::InputMethodDelegate* delegate, |
| 15 ui::InputMethod* host) |
| 16 : host_(host), |
| 17 context_focused_(false) { |
| 18 DCHECK(host_); |
| 19 set_delegate(delegate); |
| 20 } |
| 21 |
| 22 InputMethodBridge::~InputMethodBridge() { |
| 23 if (host_->text_input_client() == this) |
| 24 host_->set_text_input_client(NULL); |
| 25 } |
| 26 |
| 27 void InputMethodBridge::Init(Widget* widget) { |
| 28 InputMethodBase::Init(widget); |
| 29 } |
| 30 |
| 31 void InputMethodBridge::OnFocus() { |
| 32 DCHECK(!widget_focused()); |
| 33 InputMethodBase::OnFocus(); |
| 34 |
| 35 // Ask the system-wide IME to send all TextInputClient messages to |this| |
| 36 // object. |
| 37 host_->set_text_input_client(this); |
| 38 |
| 39 if (GetFocusedView()) |
| 40 OnTextInputTypeChanged(GetFocusedView()); |
| 41 } |
| 42 |
| 43 void InputMethodBridge::OnBlur() { |
| 44 DCHECK(widget_focused()); |
| 45 |
| 46 ConfirmCompositionText(); |
| 47 InputMethodBase::OnBlur(); |
| 48 if (host_->text_input_client() == this) |
| 49 host_->set_text_input_client(NULL); |
| 50 } |
| 51 |
| 52 void InputMethodBridge::DispatchKeyEvent(const KeyEvent& key) { |
| 53 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); |
| 54 DCHECK(widget_focused()); |
| 55 |
| 56 // We can just dispatch the event here since the |key| is already processed by |
| 57 // the system-wide IME. |
| 58 DispatchKeyEventPostIME(key); |
| 59 } |
| 60 |
| 61 void InputMethodBridge::OnTextInputTypeChanged(View* view) { |
| 62 if (IsViewFocused(view)) { |
| 63 #if defined(USE_AURA) |
| 64 host_->OnTextInputTypeChanged(view->GetWidget()->GetNativeWindow()); |
| 65 #else |
| 66 NOTIMPLEMENTED(); |
| 67 #endif |
| 68 } |
| 69 InputMethodBase::OnTextInputTypeChanged(view); |
| 70 } |
| 71 |
| 72 void InputMethodBridge::OnCaretBoundsChanged(View* view) { |
| 73 if (IsViewFocused(view) && !IsTextInputTypeNone()) { |
| 74 #if defined(USE_AURA) |
| 75 host_->OnCaretBoundsChanged(view->GetWidget()->GetNativeWindow()); |
| 76 #else |
| 77 NOTIMPLEMENTED(); |
| 78 #endif |
| 79 } |
| 80 } |
| 81 |
| 82 void InputMethodBridge::CancelComposition(View* view) { |
| 83 if (IsViewFocused(view)) { |
| 84 #if defined(USE_AURA) |
| 85 host_->CancelComposition(view->GetWidget()->GetNativeWindow()); |
| 86 #else |
| 87 NOTIMPLEMENTED(); |
| 88 #endif |
| 89 } |
| 90 } |
| 91 |
| 92 std::string InputMethodBridge::GetInputLocale() { |
| 93 return host_->GetInputLocale(); |
| 94 } |
| 95 |
| 96 base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() { |
| 97 return host_->GetInputTextDirection(); |
| 98 } |
| 99 |
| 100 bool InputMethodBridge::IsActive() { |
| 101 return host_->IsActive(); |
| 102 } |
| 103 |
| 104 // Overridden from TextInputClient. Forward an event from the system-wide IME |
| 105 // to the text input |client|, which is e.g. views::NativeTextfieldViews. |
| 106 void InputMethodBridge::SetCompositionText( |
| 107 const ui::CompositionText& composition) { |
| 108 TextInputClient* client = GetTextInputClient(); |
| 109 if (client) |
| 110 client->SetCompositionText(composition); |
| 111 } |
| 112 |
| 113 void InputMethodBridge::ConfirmCompositionText() { |
| 114 TextInputClient* client = GetTextInputClient(); |
| 115 if (client) |
| 116 client->ConfirmCompositionText(); |
| 117 } |
| 118 |
| 119 void InputMethodBridge::ClearCompositionText() { |
| 120 TextInputClient* client = GetTextInputClient(); |
| 121 if (client) |
| 122 client->ClearCompositionText(); |
| 123 } |
| 124 |
| 125 void InputMethodBridge::InsertText(const string16& text) { |
| 126 TextInputClient* client = GetTextInputClient(); |
| 127 if (client) |
| 128 client->InsertText(text); |
| 129 } |
| 130 |
| 131 void InputMethodBridge::InsertChar(char16 ch, int flags) { |
| 132 TextInputClient* client = GetTextInputClient(); |
| 133 if (client) |
| 134 client->InsertChar(ch, flags); |
| 135 } |
| 136 |
| 137 ui::TextInputType InputMethodBridge::GetTextInputType() const { |
| 138 TextInputClient* client = GetTextInputClient(); |
| 139 if (client) |
| 140 return client->GetTextInputType(); |
| 141 return ui::TEXT_INPUT_TYPE_NONE; |
| 142 } |
| 143 |
| 144 gfx::Rect InputMethodBridge::GetCaretBounds() { |
| 145 TextInputClient* client = GetTextInputClient(); |
| 146 if (!client || !GetFocusedView()) |
| 147 return gfx::Rect(); |
| 148 |
| 149 const gfx::Rect rect = client->GetCaretBounds(); |
| 150 gfx::Point origin = rect.origin(); |
| 151 gfx::Point end = gfx::Point(rect.right(), rect.bottom()); |
| 152 View::ConvertPointToWidget(GetFocusedView(), &origin); |
| 153 View::ConvertPointToWidget(GetFocusedView(), &end); |
| 154 return gfx::Rect(origin.x(), |
| 155 origin.y(), |
| 156 end.x() - origin.x(), |
| 157 end.y() - origin.y()); |
| 158 } |
| 159 |
| 160 bool InputMethodBridge::HasCompositionText() { |
| 161 TextInputClient* client = GetTextInputClient(); |
| 162 if (client) |
| 163 return client->HasCompositionText(); |
| 164 return false; |
| 165 } |
| 166 |
| 167 bool InputMethodBridge::GetTextRange(ui::Range* range) { |
| 168 TextInputClient* client = GetTextInputClient(); |
| 169 if (client) |
| 170 return client->GetTextRange(range); |
| 171 return false; |
| 172 } |
| 173 |
| 174 bool InputMethodBridge::GetCompositionTextRange(ui::Range* range) { |
| 175 TextInputClient* client = GetTextInputClient(); |
| 176 if (client) |
| 177 return client->GetCompositionTextRange(range); |
| 178 return false; |
| 179 } |
| 180 |
| 181 bool InputMethodBridge::GetSelectionRange(ui::Range* range) { |
| 182 TextInputClient* client = GetTextInputClient(); |
| 183 if (client) |
| 184 return client->GetSelectionRange(range); |
| 185 return false; |
| 186 } |
| 187 |
| 188 bool InputMethodBridge::SetSelectionRange(const ui::Range& range) { |
| 189 TextInputClient* client = GetTextInputClient(); |
| 190 if (client) |
| 191 return client->SetSelectionRange(range); |
| 192 return false; |
| 193 } |
| 194 |
| 195 bool InputMethodBridge::DeleteRange(const ui::Range& range) { |
| 196 TextInputClient* client = GetTextInputClient(); |
| 197 if (client) |
| 198 return client->DeleteRange(range); |
| 199 return false; |
| 200 } |
| 201 |
| 202 bool InputMethodBridge::GetTextFromRange( |
| 203 const ui::Range& range, string16* text) { |
| 204 TextInputClient* client = GetTextInputClient(); |
| 205 if (client) |
| 206 return client->GetTextFromRange(range, text); |
| 207 return false; |
| 208 } |
| 209 |
| 210 void InputMethodBridge::OnInputMethodChanged() { |
| 211 TextInputClient* client = GetTextInputClient(); |
| 212 if (client) |
| 213 client->OnInputMethodChanged(); |
| 214 } |
| 215 |
| 216 bool InputMethodBridge::ChangeTextDirectionAndLayoutAlignment( |
| 217 base::i18n::TextDirection direction) { |
| 218 TextInputClient* client = GetTextInputClient(); |
| 219 if (client) |
| 220 return client->ChangeTextDirectionAndLayoutAlignment(direction); |
| 221 return false; |
| 222 } |
| 223 |
| 224 // Overridden from FocusChangeListener. |
| 225 void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) { |
| 226 ConfirmCompositionText(); |
| 227 } |
| 228 |
| 229 void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) { |
| 230 OnTextInputTypeChanged(GetFocusedView()); |
| 231 OnCaretBoundsChanged(GetFocusedView()); |
| 232 } |
| 233 |
| 234 } // namespace views |
OLD | NEW |