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 "ui/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 // TODO(yusukes): We don't need to call OnTextInputTypeChanged() once we move |
| 40 // text input type tracker code to ui::InputMethodBase. |
| 41 if (GetFocusedView()) |
| 42 OnTextInputTypeChanged(GetFocusedView()); |
| 43 } |
| 44 |
| 45 void InputMethodBridge::OnBlur() { |
| 46 DCHECK(widget_focused()); |
| 47 |
| 48 ConfirmCompositionText(); |
| 49 InputMethodBase::OnBlur(); |
| 50 if (host_->GetTextInputClient() == this) |
| 51 host_->SetFocusedTextInputClient(NULL); |
| 52 } |
| 53 |
| 54 void InputMethodBridge::DispatchKeyEvent(const KeyEvent& key) { |
| 55 DCHECK(key.type() == ui::ET_KEY_PRESSED || key.type() == ui::ET_KEY_RELEASED); |
| 56 DCHECK(widget_focused()); |
| 57 |
| 58 // We can just dispatch the event here since the |key| is already processed by |
| 59 // the system-wide IME. |
| 60 DispatchKeyEventPostIME(key); |
| 61 } |
| 62 |
| 63 void InputMethodBridge::OnTextInputTypeChanged(View* view) { |
| 64 if (IsViewFocused(view)) |
| 65 host_->OnTextInputTypeChanged(this); |
| 66 InputMethodBase::OnTextInputTypeChanged(view); |
| 67 } |
| 68 |
| 69 void InputMethodBridge::OnCaretBoundsChanged(View* view) { |
| 70 if (IsViewFocused(view) && !IsTextInputTypeNone()) |
| 71 host_->OnCaretBoundsChanged(this); |
| 72 } |
| 73 |
| 74 void InputMethodBridge::CancelComposition(View* view) { |
| 75 if (IsViewFocused(view)) |
| 76 host_->CancelComposition(this); |
| 77 } |
| 78 |
| 79 std::string InputMethodBridge::GetInputLocale() { |
| 80 return host_->GetInputLocale(); |
| 81 } |
| 82 |
| 83 base::i18n::TextDirection InputMethodBridge::GetInputTextDirection() { |
| 84 return host_->GetInputTextDirection(); |
| 85 } |
| 86 |
| 87 bool InputMethodBridge::IsActive() { |
| 88 return host_->IsActive(); |
| 89 } |
| 90 |
| 91 // Overridden from TextInputClient. Forward an event from the system-wide IME |
| 92 // to the text input |client|, which is e.g. views::NativeTextfieldViews. |
| 93 void InputMethodBridge::SetCompositionText( |
| 94 const ui::CompositionText& composition) { |
| 95 TextInputClient* client = GetTextInputClient(); |
| 96 if (client) |
| 97 client->SetCompositionText(composition); |
| 98 } |
| 99 |
| 100 void InputMethodBridge::ConfirmCompositionText() { |
| 101 TextInputClient* client = GetTextInputClient(); |
| 102 if (client) |
| 103 client->ConfirmCompositionText(); |
| 104 } |
| 105 |
| 106 void InputMethodBridge::ClearCompositionText() { |
| 107 TextInputClient* client = GetTextInputClient(); |
| 108 if (client) |
| 109 client->ClearCompositionText(); |
| 110 } |
| 111 |
| 112 void InputMethodBridge::InsertText(const string16& text) { |
| 113 TextInputClient* client = GetTextInputClient(); |
| 114 if (client) |
| 115 client->InsertText(text); |
| 116 } |
| 117 |
| 118 void InputMethodBridge::InsertChar(char16 ch, int flags) { |
| 119 TextInputClient* client = GetTextInputClient(); |
| 120 if (client) |
| 121 client->InsertChar(ch, flags); |
| 122 } |
| 123 |
| 124 ui::TextInputType InputMethodBridge::GetTextInputType() const { |
| 125 TextInputClient* client = GetTextInputClient(); |
| 126 return client ? client->GetTextInputType() : 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 return client ? client->HasCompositionText() : false; |
| 148 } |
| 149 |
| 150 bool InputMethodBridge::GetTextRange(ui::Range* range) { |
| 151 TextInputClient* client = GetTextInputClient(); |
| 152 return client ? client->GetTextRange(range) : false; |
| 153 } |
| 154 |
| 155 bool InputMethodBridge::GetCompositionTextRange(ui::Range* range) { |
| 156 TextInputClient* client = GetTextInputClient(); |
| 157 return client ? client->GetCompositionTextRange(range) : false; |
| 158 } |
| 159 |
| 160 bool InputMethodBridge::GetSelectionRange(ui::Range* range) { |
| 161 TextInputClient* client = GetTextInputClient(); |
| 162 return client ? client->GetSelectionRange(range) : false; |
| 163 } |
| 164 |
| 165 bool InputMethodBridge::SetSelectionRange(const ui::Range& range) { |
| 166 TextInputClient* client = GetTextInputClient(); |
| 167 return client ? client->SetSelectionRange(range) : false; |
| 168 } |
| 169 |
| 170 bool InputMethodBridge::DeleteRange(const ui::Range& range) { |
| 171 TextInputClient* client = GetTextInputClient(); |
| 172 return client ? client->DeleteRange(range) : false; |
| 173 } |
| 174 |
| 175 bool InputMethodBridge::GetTextFromRange( |
| 176 const ui::Range& range, string16* text) { |
| 177 TextInputClient* client = GetTextInputClient(); |
| 178 return client ? client->GetTextFromRange(range, text) : false; |
| 179 } |
| 180 |
| 181 void InputMethodBridge::OnInputMethodChanged() { |
| 182 TextInputClient* client = GetTextInputClient(); |
| 183 if (client) |
| 184 client->OnInputMethodChanged(); |
| 185 } |
| 186 |
| 187 bool InputMethodBridge::ChangeTextDirectionAndLayoutAlignment( |
| 188 base::i18n::TextDirection direction) { |
| 189 TextInputClient* client = GetTextInputClient(); |
| 190 return client ? |
| 191 client->ChangeTextDirectionAndLayoutAlignment(direction) : false; |
| 192 } |
| 193 |
| 194 // Overridden from FocusChangeListener. |
| 195 void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) { |
| 196 ConfirmCompositionText(); |
| 197 } |
| 198 |
| 199 void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) { |
| 200 OnTextInputTypeChanged(GetFocusedView()); |
| 201 OnCaretBoundsChanged(GetFocusedView()); |
| 202 } |
| 203 |
| 204 } // namespace views |
OLD | NEW |