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 "ui/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_->GetTextInputClient() == this) |
| 24 host_->SetFocusedTextInputClient(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_->SetFocusedTextInputClient(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_->GetTextInputClient() == this) |
| 49 host_->SetFocusedTextInputClient(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 host_->OnTextInputTypeChanged(this); |
| 64 InputMethodBase::OnTextInputTypeChanged(view); |
| 65 } |
| 66 |
| 67 void InputMethodBridge::OnCaretBoundsChanged(View* view) { |
| 68 if (IsViewFocused(view) && !IsTextInputTypeNone()) |
| 69 host_->OnCaretBoundsChanged(this); |
| 70 } |
| 71 |
| 72 void InputMethodBridge::CancelComposition(View* view) { |
| 73 if (IsViewFocused(view)) |
| 74 host_->CancelComposition(this); |
| 75 } |
| 76 |
| 77 std::string InputMethodBridge::GetInputLocale() { |
| 78 return host_->GetInputLocale(); |
| 79 } |
| 80 |
| 81 base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() { |
| 82 return host_->GetInputTextDirection(); |
| 83 } |
| 84 |
| 85 bool InputMethodBridge::IsActive() { |
| 86 return host_->IsActive(); |
| 87 } |
| 88 |
| 89 // Overridden from TextInputClient. Forward an event from the system-wide IME |
| 90 // to the text input |client|, which is e.g. views::NativeTextfieldViews. |
| 91 void InputMethodBridge::SetCompositionText( |
| 92 const ui::CompositionText& composition) { |
| 93 TextInputClient* client = GetTextInputClient(); |
| 94 if (client) |
| 95 client->SetCompositionText(composition); |
| 96 } |
| 97 |
| 98 void InputMethodBridge::ConfirmCompositionText() { |
| 99 TextInputClient* client = GetTextInputClient(); |
| 100 if (client) |
| 101 client->ConfirmCompositionText(); |
| 102 } |
| 103 |
| 104 void InputMethodBridge::ClearCompositionText() { |
| 105 TextInputClient* client = GetTextInputClient(); |
| 106 if (client) |
| 107 client->ClearCompositionText(); |
| 108 } |
| 109 |
| 110 void InputMethodBridge::InsertText(const string16& text) { |
| 111 TextInputClient* client = GetTextInputClient(); |
| 112 if (client) |
| 113 client->InsertText(text); |
| 114 } |
| 115 |
| 116 void InputMethodBridge::InsertChar(char16 ch, int flags) { |
| 117 TextInputClient* client = GetTextInputClient(); |
| 118 if (client) |
| 119 client->InsertChar(ch, flags); |
| 120 } |
| 121 |
| 122 ui::TextInputType InputMethodBridge::GetTextInputType() const { |
| 123 TextInputClient* client = GetTextInputClient(); |
| 124 if (client) |
| 125 return client->GetTextInputType(); |
| 126 return ui::TEXT_INPUT_TYPE_NONE; |
| 127 } |
| 128 |
| 129 gfx::Rect InputMethodBridge::GetCaretBounds() { |
| 130 TextInputClient* client = GetTextInputClient(); |
| 131 if (!client || !GetFocusedView()) |
| 132 return gfx::Rect(); |
| 133 |
| 134 const gfx::Rect rect = client->GetCaretBounds(); |
| 135 gfx::Point origin = rect.origin(); |
| 136 gfx::Point end = gfx::Point(rect.right(), rect.bottom()); |
| 137 View::ConvertPointToScreen(GetFocusedView(), &origin); |
| 138 View::ConvertPointToScreen(GetFocusedView(), &end); |
| 139 return gfx::Rect(origin.x(), |
| 140 origin.y(), |
| 141 end.x() - origin.x(), |
| 142 end.y() - origin.y()); |
| 143 } |
| 144 |
| 145 bool InputMethodBridge::HasCompositionText() { |
| 146 TextInputClient* client = GetTextInputClient(); |
| 147 if (client) |
| 148 return client->HasCompositionText(); |
| 149 return false; |
| 150 } |
| 151 |
| 152 bool InputMethodBridge::GetTextRange(ui::Range* range) { |
| 153 TextInputClient* client = GetTextInputClient(); |
| 154 if (client) |
| 155 return client->GetTextRange(range); |
| 156 return false; |
| 157 } |
| 158 |
| 159 bool InputMethodBridge::GetCompositionTextRange(ui::Range* range) { |
| 160 TextInputClient* client = GetTextInputClient(); |
| 161 if (client) |
| 162 return client->GetCompositionTextRange(range); |
| 163 return false; |
| 164 } |
| 165 |
| 166 bool InputMethodBridge::GetSelectionRange(ui::Range* range) { |
| 167 TextInputClient* client = GetTextInputClient(); |
| 168 if (client) |
| 169 return client->GetSelectionRange(range); |
| 170 return false; |
| 171 } |
| 172 |
| 173 bool InputMethodBridge::SetSelectionRange(const ui::Range& range) { |
| 174 TextInputClient* client = GetTextInputClient(); |
| 175 if (client) |
| 176 return client->SetSelectionRange(range); |
| 177 return false; |
| 178 } |
| 179 |
| 180 bool InputMethodBridge::DeleteRange(const ui::Range& range) { |
| 181 TextInputClient* client = GetTextInputClient(); |
| 182 if (client) |
| 183 return client->DeleteRange(range); |
| 184 return false; |
| 185 } |
| 186 |
| 187 bool InputMethodBridge::GetTextFromRange( |
| 188 const ui::Range& range, string16* text) { |
| 189 TextInputClient* client = GetTextInputClient(); |
| 190 if (client) |
| 191 return client->GetTextFromRange(range, text); |
| 192 return false; |
| 193 } |
| 194 |
| 195 void InputMethodBridge::OnInputMethodChanged() { |
| 196 TextInputClient* client = GetTextInputClient(); |
| 197 if (client) |
| 198 client->OnInputMethodChanged(); |
| 199 } |
| 200 |
| 201 bool InputMethodBridge::ChangeTextDirectionAndLayoutAlignment( |
| 202 base::i18n::TextDirection direction) { |
| 203 TextInputClient* client = GetTextInputClient(); |
| 204 if (client) |
| 205 return client->ChangeTextDirectionAndLayoutAlignment(direction); |
| 206 return false; |
| 207 } |
| 208 |
| 209 // Overridden from FocusChangeListener. |
| 210 void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) { |
| 211 ConfirmCompositionText(); |
| 212 } |
| 213 |
| 214 void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) { |
| 215 OnTextInputTypeChanged(GetFocusedView()); |
| 216 OnCaretBoundsChanged(GetFocusedView()); |
| 217 } |
| 218 |
| 219 } // namespace views |
OLD | NEW |