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