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 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; |
| 125 } |
| 126 |
| 127 gfx::Rect InputMethodBridge::GetCaretBounds() { |
| 128 TextInputClient* client = GetTextInputClient(); |
| 129 if (!client || !GetFocusedView()) |
| 130 return gfx::Rect(); |
| 131 |
| 132 const gfx::Rect rect = client->GetCaretBounds(); |
| 133 gfx::Point origin = rect.origin(); |
| 134 gfx::Point end = gfx::Point(rect.right(), rect.bottom()); |
| 135 View::ConvertPointToScreen(GetFocusedView(), &origin); |
| 136 View::ConvertPointToScreen(GetFocusedView(), &end); |
| 137 return gfx::Rect(origin.x(), |
| 138 origin.y(), |
| 139 end.x() - origin.x(), |
| 140 end.y() - origin.y()); |
| 141 } |
| 142 |
| 143 bool InputMethodBridge::HasCompositionText() { |
| 144 TextInputClient* client = GetTextInputClient(); |
| 145 return client ? client->HasCompositionText() : false; |
| 146 } |
| 147 |
| 148 bool InputMethodBridge::GetTextRange(ui::Range* range) { |
| 149 TextInputClient* client = GetTextInputClient(); |
| 150 return client ? client->GetTextRange(range) : false; |
| 151 } |
| 152 |
| 153 bool InputMethodBridge::GetCompositionTextRange(ui::Range* range) { |
| 154 TextInputClient* client = GetTextInputClient(); |
| 155 return client ? client->GetCompositionTextRange(range) : false; |
| 156 } |
| 157 |
| 158 bool InputMethodBridge::GetSelectionRange(ui::Range* range) { |
| 159 TextInputClient* client = GetTextInputClient(); |
| 160 return client ? client->GetSelectionRange(range) : false; |
| 161 } |
| 162 |
| 163 bool InputMethodBridge::SetSelectionRange(const ui::Range& range) { |
| 164 TextInputClient* client = GetTextInputClient(); |
| 165 return client ? client->SetSelectionRange(range) : false; |
| 166 } |
| 167 |
| 168 bool InputMethodBridge::DeleteRange(const ui::Range& range) { |
| 169 TextInputClient* client = GetTextInputClient(); |
| 170 return client ? client->DeleteRange(range) : false; |
| 171 } |
| 172 |
| 173 bool InputMethodBridge::GetTextFromRange( |
| 174 const ui::Range& range, string16* text) { |
| 175 TextInputClient* client = GetTextInputClient(); |
| 176 return client ? client->GetTextFromRange(range, text) : false; |
| 177 } |
| 178 |
| 179 void InputMethodBridge::OnInputMethodChanged() { |
| 180 TextInputClient* client = GetTextInputClient(); |
| 181 if (client) |
| 182 client->OnInputMethodChanged(); |
| 183 } |
| 184 |
| 185 bool InputMethodBridge::ChangeTextDirectionAndLayoutAlignment( |
| 186 base::i18n::TextDirection direction) { |
| 187 TextInputClient* client = GetTextInputClient(); |
| 188 return client ? |
| 189 client->ChangeTextDirectionAndLayoutAlignment(direction) : false; |
| 190 } |
| 191 |
| 192 // Overridden from FocusChangeListener. |
| 193 void InputMethodBridge::OnWillChangeFocus(View* focused_before, View* focused) { |
| 194 ConfirmCompositionText(); |
| 195 } |
| 196 |
| 197 void InputMethodBridge::OnDidChangeFocus(View* focused_before, View* focused) { |
| 198 OnTextInputTypeChanged(GetFocusedView()); |
| 199 OnCaretBoundsChanged(GetFocusedView()); |
| 200 } |
| 201 |
| 202 } // namespace views |
OLD | NEW |