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/base/ime/mock_input_method.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/string16.h" |
| 9 #include "ui/base/events.h" |
| 10 #include "ui/base/glib/glib_integers.h" |
| 11 #include "ui/base/ime/input_method_delegate.h" |
| 12 #include "ui/base/ime/text_input_client.h" |
| 13 #include "ui/base/keycodes/keyboard_code_conversion.h" |
| 14 |
| 15 #if defined(USE_X11) |
| 16 #include <X11/X.h> |
| 17 #include <X11/Xlib.h> |
| 18 #include <X11/Xutil.h> |
| 19 #include "ui/base/keycodes/keyboard_code_conversion_x.h" |
| 20 #endif |
| 21 |
| 22 namespace { |
| 23 |
| 24 #if defined(USE_X11) |
| 25 guint32 EventFlagsFromXFlags(unsigned int flags) { |
| 26 return (flags & LockMask ? ui::EF_CAPS_LOCK_DOWN : 0U) | |
| 27 (flags & ControlMask ? ui::EF_CONTROL_DOWN : 0U) | |
| 28 (flags & ShiftMask ? ui::EF_SHIFT_DOWN : 0U) | |
| 29 (flags & Mod1Mask ? ui::EF_ALT_DOWN : 0U); |
| 30 } |
| 31 #endif |
| 32 |
| 33 } // namespace |
| 34 |
| 35 namespace ui { |
| 36 |
| 37 MockInputMethod::MockInputMethod(internal::InputMethodDelegate* delegate) |
| 38 : delegate_(NULL), |
| 39 text_input_client_(NULL), |
| 40 consume_next_key_(false) { |
| 41 SetDelegate(delegate); |
| 42 } |
| 43 |
| 44 MockInputMethod::~MockInputMethod() { |
| 45 } |
| 46 |
| 47 void MockInputMethod::SetDelegate(internal::InputMethodDelegate* delegate) { |
| 48 delegate_ = delegate; |
| 49 } |
| 50 |
| 51 void MockInputMethod::SetFocusedTextInputClient(TextInputClient* client) { |
| 52 text_input_client_ = client; |
| 53 } |
| 54 |
| 55 TextInputClient* MockInputMethod::GetTextInputClient() const { |
| 56 return text_input_client_; |
| 57 } |
| 58 |
| 59 void MockInputMethod::DispatchKeyEvent(const base::NativeEvent& native_event) { |
| 60 #if defined(USE_X11) |
| 61 if (native_event->type == KeyRelease) { |
| 62 // On key release, just dispatch it. |
| 63 delegate_->DispatchKeyEventPostIME(native_event); |
| 64 } else { |
| 65 const uint32 state = |
| 66 EventFlagsFromXFlags(reinterpret_cast<XKeyEvent*>(native_event)->state); |
| 67 if (consume_next_key_) { |
| 68 // Send the VKEY_PROCESSKEY RawKeyDown event. |
| 69 SendFakeProcessKeyEvent(true, state); |
| 70 } else { |
| 71 // Send a RawKeyDown event first, |
| 72 delegate_->DispatchKeyEventPostIME(native_event); |
| 73 if (text_input_client_) { |
| 74 // then send a Char event via ui::TextInputClient. |
| 75 const KeyboardCode key_code = ui::KeyboardCodeFromNative(native_event); |
| 76 uint16 ch = ui::DefaultSymbolFromXEvent(native_event); |
| 77 if (!ch) |
| 78 ch = ui::GetCharacterFromKeyCode(key_code, state); |
| 79 if (ch) |
| 80 text_input_client_->InsertChar(ch, state); |
| 81 } |
| 82 } |
| 83 } |
| 84 consume_next_key_ = false; |
| 85 #else |
| 86 // TODO(yusukes): Support Windows. |
| 87 delegate_->DispatchKeyEventPostIME(native_event); |
| 88 #endif |
| 89 } |
| 90 |
| 91 void MockInputMethod::Init(bool focused) {} |
| 92 void MockInputMethod::OnFocus() {} |
| 93 void MockInputMethod::OnBlur() {} |
| 94 void MockInputMethod::OnTextInputTypeChanged(const TextInputClient* client) {} |
| 95 void MockInputMethod::OnCaretBoundsChanged(const TextInputClient* client) {} |
| 96 void MockInputMethod::CancelComposition(const TextInputClient* client) {} |
| 97 |
| 98 std::string MockInputMethod::GetInputLocale() { |
| 99 return ""; |
| 100 } |
| 101 |
| 102 base::i18n::TextDirection MockInputMethod::GetInputTextDirection() { |
| 103 return base::i18n::UNKNOWN_DIRECTION; |
| 104 } |
| 105 |
| 106 bool MockInputMethod::IsActive() { |
| 107 return true; |
| 108 } |
| 109 |
| 110 ui::TextInputType MockInputMethod::GetTextInputType() const { |
| 111 return ui::TEXT_INPUT_TYPE_NONE; |
| 112 } |
| 113 |
| 114 void MockInputMethod::ConsumeNextKey() { |
| 115 consume_next_key_ = true; |
| 116 } |
| 117 |
| 118 void MockInputMethod::SendFakeProcessKeyEvent(bool pressed, int flags) const { |
| 119 delegate_->DispatchFabricatedKeyEventPostIME( |
| 120 pressed ? ET_KEY_PRESSED : ET_KEY_RELEASED, VKEY_PROCESSKEY, flags); |
| 121 } |
| 122 |
| 123 } // namespace ui |
OLD | NEW |