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 "views/ime/mock_input_method.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
| 9 #include "ui/base/keycodes/keyboard_codes.h" |
| 10 #include "views/events/event.h" |
| 11 #include "views/widget/widget.h" |
| 12 |
| 13 namespace views { |
| 14 |
| 15 MockInputMethod::MockInputMethod() |
| 16 : delegate_(NULL), |
| 17 widget_(NULL), |
| 18 focused_view_(NULL), |
| 19 composition_changed_(false), |
| 20 focus_changed_(false), |
| 21 text_input_type_changed_(false), |
| 22 caret_bounds_changed_(false), |
| 23 cancel_composition_called_(false), |
| 24 locale_("en-US"), |
| 25 direction_(base::i18n::LEFT_TO_RIGHT), |
| 26 active_(true) { |
| 27 } |
| 28 |
| 29 MockInputMethod::~MockInputMethod() { |
| 30 if (widget_) { |
| 31 widget_->GetFocusManager()->RemoveFocusChangeListener(this); |
| 32 widget_ = NULL; |
| 33 } |
| 34 } |
| 35 |
| 36 void MockInputMethod::set_delegate(internal::InputMethodDelegate* delegate) { |
| 37 delegate_ = delegate; |
| 38 } |
| 39 |
| 40 void MockInputMethod::Init(Widget* widget) { |
| 41 DCHECK(widget); |
| 42 DCHECK(widget->GetFocusManager()); |
| 43 |
| 44 if (widget_) { |
| 45 NOTREACHED() << "The input method is already initialized."; |
| 46 return; |
| 47 } |
| 48 |
| 49 widget_ = widget; |
| 50 widget->GetFocusManager()->AddFocusChangeListener(this); |
| 51 } |
| 52 |
| 53 void MockInputMethod::DispatchKeyEvent(const KeyEvent& key) { |
| 54 bool handled = (composition_changed_ || result_text_.length()) && |
| 55 GetTextInputType() != ui::TEXT_INPUT_TYPE_NONE; |
| 56 |
| 57 ClearStates(); |
| 58 if (delegate_) { |
| 59 if (handled) { |
| 60 KeyEvent mock_key(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, key.flags()); |
| 61 delegate_->DispatchKeyEventPostIME(mock_key); |
| 62 } else { |
| 63 delegate_->DispatchKeyEventPostIME(key); |
| 64 } |
| 65 } |
| 66 |
| 67 if (focus_changed_) |
| 68 return; |
| 69 |
| 70 TextInputClient* client = GetTextInputClient(); |
| 71 if (client) { |
| 72 if (handled) { |
| 73 if (result_text_.length()) |
| 74 client->InsertText(result_text_); |
| 75 if (composition_changed_) { |
| 76 if (composition_.text.length()) |
| 77 client->SetComposition(composition_); |
| 78 else |
| 79 client->ClearComposition(); |
| 80 } |
| 81 } else { |
| 82 char16 ch = key.GetCharacter(); |
| 83 client->InsertChar(ch, key.flags()); |
| 84 } |
| 85 } |
| 86 |
| 87 ClearResult(); |
| 88 } |
| 89 |
| 90 void MockInputMethod::OnTextInputTypeChanged(View* view) { |
| 91 DCHECK_EQ(focused_view_, view); |
| 92 text_input_type_changed_ = true; |
| 93 } |
| 94 |
| 95 void MockInputMethod::OnCaretBoundsChanged(View* view) { |
| 96 DCHECK_EQ(focused_view_, view); |
| 97 caret_bounds_changed_ = true; |
| 98 } |
| 99 |
| 100 void MockInputMethod::CancelComposition(View* view) { |
| 101 DCHECK_EQ(focused_view_, view); |
| 102 cancel_composition_called_ = true; |
| 103 ClearResult(); |
| 104 } |
| 105 |
| 106 std::string MockInputMethod::GetInputLocale() { |
| 107 return locale_; |
| 108 } |
| 109 |
| 110 base::i18n::TextDirection MockInputMethod::GetInputTextDirection() { |
| 111 return direction_; |
| 112 } |
| 113 |
| 114 bool MockInputMethod::IsActive() { |
| 115 return active_; |
| 116 } |
| 117 |
| 118 void MockInputMethod::FocusWillChange(View* focused_before, View* focused) { |
| 119 DCHECK_EQ(focused_view_, focused_before); |
| 120 TextInputClient* client = GetTextInputClient(); |
| 121 if (client && client->HasComposition()) |
| 122 client->ConfirmComposition(); |
| 123 focused_view_ = focused; |
| 124 focus_changed_ = true; |
| 125 ClearResult(); |
| 126 } |
| 127 |
| 128 ui::TextInputType MockInputMethod::GetTextInputType() const { |
| 129 TextInputClient* client = GetTextInputClient(); |
| 130 return client ? client->GetTextInputType() : ui::TEXT_INPUT_TYPE_NONE; |
| 131 } |
| 132 |
| 133 void MockInputMethod::Clear() { |
| 134 ClearStates(); |
| 135 ClearResult(); |
| 136 } |
| 137 |
| 138 void MockInputMethod::SetCompositionForNextKey( |
| 139 const ui::Composition& composition) { |
| 140 composition_changed_ = true; |
| 141 composition_ = composition; |
| 142 } |
| 143 |
| 144 void MockInputMethod::SetResultForNextKey(const string16& result) { |
| 145 result_text_ = result; |
| 146 } |
| 147 |
| 148 void MockInputMethod::SetInputLocale(const std::string& locale) { |
| 149 if (locale_ != locale) { |
| 150 locale_ = locale; |
| 151 OnInputMethodChanged(); |
| 152 } |
| 153 } |
| 154 |
| 155 void MockInputMethod::SetInputTextDirection( |
| 156 base::i18n::TextDirection direction) { |
| 157 if (direction_ != direction) { |
| 158 direction_ = direction; |
| 159 OnInputMethodChanged(); |
| 160 } |
| 161 } |
| 162 |
| 163 void MockInputMethod::SetActive(bool active) { |
| 164 if (active_ != active) { |
| 165 active_ = active; |
| 166 OnInputMethodChanged(); |
| 167 } |
| 168 } |
| 169 |
| 170 void MockInputMethod::ClearStates() { |
| 171 focus_changed_ = false; |
| 172 text_input_type_changed_ = false; |
| 173 caret_bounds_changed_ = false; |
| 174 cancel_composition_called_ = false; |
| 175 } |
| 176 |
| 177 void MockInputMethod::ClearResult() { |
| 178 composition_.Clear(); |
| 179 composition_changed_ = false; |
| 180 result_text_.clear(); |
| 181 } |
| 182 |
| 183 TextInputClient* MockInputMethod::GetTextInputClient() const { |
| 184 return focused_view_ ? focused_view_->GetTextInputClient() : NULL; |
| 185 } |
| 186 |
| 187 void MockInputMethod::OnInputMethodChanged() const { |
| 188 TextInputClient* client = GetTextInputClient(); |
| 189 if (client) |
| 190 client->OnInputMethodChanged(); |
| 191 } |
| 192 |
| 193 } // namespace views |
OLD | NEW |