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 : composition_changed_(false), |
| 17 focus_changed_(false), |
| 18 text_input_type_changed_(false), |
| 19 caret_bounds_changed_(false), |
| 20 cancel_composition_called_(false), |
| 21 locale_("en-US"), |
| 22 direction_(base::i18n::LEFT_TO_RIGHT), |
| 23 active_(true) { |
| 24 } |
| 25 |
| 26 MockInputMethod::~MockInputMethod() { |
| 27 } |
| 28 |
| 29 void MockInputMethod::Init(Widget* widget) { |
| 30 InputMethodBase::Init(widget); |
| 31 } |
| 32 |
| 33 void MockInputMethod::DispatchKeyEvent(const KeyEvent& key) { |
| 34 bool handled = (composition_changed_ || result_text_.length()) && |
| 35 !IsTextInputTypeNone(); |
| 36 |
| 37 ClearStates(); |
| 38 if (handled) { |
| 39 KeyEvent mock_key(ui::ET_KEY_PRESSED, ui::VKEY_PROCESSKEY, key.flags()); |
| 40 DispatchKeyEventPostIME(mock_key); |
| 41 } else { |
| 42 DispatchKeyEventPostIME(key); |
| 43 } |
| 44 |
| 45 if (focus_changed_) |
| 46 return; |
| 47 |
| 48 TextInputClient* client = GetTextInputClient(); |
| 49 if (client) { |
| 50 if (handled) { |
| 51 if (result_text_.length()) |
| 52 client->InsertText(result_text_); |
| 53 if (composition_changed_) { |
| 54 if (composition_.text.length()) |
| 55 client->SetCompositionText(composition_); |
| 56 else |
| 57 client->ClearCompositionText(); |
| 58 } |
| 59 } else if (key.type() == ui::ET_KEY_PRESSED) { |
| 60 char16 ch = key.GetCharacter(); |
| 61 client->InsertChar(ch, key.flags()); |
| 62 } |
| 63 } |
| 64 |
| 65 ClearResult(); |
| 66 } |
| 67 |
| 68 void MockInputMethod::OnTextInputTypeChanged(View* view) { |
| 69 DCHECK(IsViewFocused(view)); |
| 70 text_input_type_changed_ = true; |
| 71 } |
| 72 |
| 73 void MockInputMethod::OnCaretBoundsChanged(View* view) { |
| 74 DCHECK(IsViewFocused(view)); |
| 75 caret_bounds_changed_ = true; |
| 76 } |
| 77 |
| 78 void MockInputMethod::CancelComposition(View* view) { |
| 79 DCHECK(IsViewFocused(view)); |
| 80 cancel_composition_called_ = true; |
| 81 ClearResult(); |
| 82 } |
| 83 |
| 84 std::string MockInputMethod::GetInputLocale() { |
| 85 return locale_; |
| 86 } |
| 87 |
| 88 base::i18n::TextDirection MockInputMethod::GetInputTextDirection() { |
| 89 return direction_; |
| 90 } |
| 91 |
| 92 bool MockInputMethod::IsActive() { |
| 93 return active_; |
| 94 } |
| 95 |
| 96 void MockInputMethod::FocusedViewWillChange() { |
| 97 TextInputClient* client = GetTextInputClient(); |
| 98 if (client && client->HasCompositionText()) |
| 99 client->ConfirmCompositionText(); |
| 100 focus_changed_ = true; |
| 101 ClearResult(); |
| 102 } |
| 103 |
| 104 void MockInputMethod::Clear() { |
| 105 ClearStates(); |
| 106 ClearResult(); |
| 107 } |
| 108 |
| 109 void MockInputMethod::SetCompositionTextForNextKey( |
| 110 const ui::CompositionText& composition) { |
| 111 composition_changed_ = true; |
| 112 composition_ = composition; |
| 113 } |
| 114 |
| 115 void MockInputMethod::SetResultTextForNextKey(const string16& result) { |
| 116 result_text_ = result; |
| 117 } |
| 118 |
| 119 void MockInputMethod::SetInputLocale(const std::string& locale) { |
| 120 if (locale_ != locale) { |
| 121 locale_ = locale; |
| 122 OnInputMethodChanged(); |
| 123 } |
| 124 } |
| 125 |
| 126 void MockInputMethod::SetInputTextDirection( |
| 127 base::i18n::TextDirection direction) { |
| 128 if (direction_ != direction) { |
| 129 direction_ = direction; |
| 130 OnInputMethodChanged(); |
| 131 } |
| 132 } |
| 133 |
| 134 void MockInputMethod::SetActive(bool active) { |
| 135 if (active_ != active) { |
| 136 active_ = active; |
| 137 OnInputMethodChanged(); |
| 138 } |
| 139 } |
| 140 |
| 141 void MockInputMethod::ClearStates() { |
| 142 focus_changed_ = false; |
| 143 text_input_type_changed_ = false; |
| 144 caret_bounds_changed_ = false; |
| 145 cancel_composition_called_ = false; |
| 146 } |
| 147 |
| 148 void MockInputMethod::ClearResult() { |
| 149 composition_.Clear(); |
| 150 composition_changed_ = false; |
| 151 result_text_.clear(); |
| 152 } |
| 153 |
| 154 } // namespace views |
OLD | NEW |