1 // Copyright 2013 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.
5 #include "ui/base/ime/input_method_auralinux.h"
7 #include "base/environment.h"
8 #include "ui/base/ime/linux/linux_input_method_context_factory.h"
9 #include "ui/base/ime/text_input_client.h"
10 #include "ui/events/event.h"
14 InputMethodAuraLinux::InputMethodAuraLinux(
15 internal::InputMethodDelegate
* delegate
) {
16 SetDelegate(delegate
);
19 InputMethodAuraLinux::~InputMethodAuraLinux() {}
21 // Overriden from InputMethod.
23 void InputMethodAuraLinux::Init(bool focused
) {
24 CHECK(LinuxInputMethodContextFactory::instance())
25 << "This failure was likely caused because "
26 << "ui::InitializeInputMethod(ForTesting) was not called "
27 << "before instantiating this class.";
28 input_method_context_
=
29 LinuxInputMethodContextFactory::instance()->CreateInputMethodContext(
31 CHECK(input_method_context_
.get());
33 InputMethodBase::Init(focused
);
36 input_method_context_
->OnTextInputTypeChanged(
37 GetTextInputClient() ?
38 GetTextInputClient()->GetTextInputType() :
39 TEXT_INPUT_TYPE_TEXT
);
43 bool InputMethodAuraLinux::OnUntranslatedIMEMessage(
44 const base::NativeEvent
& event
,
45 NativeEventResult
* result
) {
49 bool InputMethodAuraLinux::DispatchKeyEvent(const ui::KeyEvent
& event
) {
50 DCHECK(event
.type() == ET_KEY_PRESSED
|| event
.type() == ET_KEY_RELEASED
);
51 DCHECK(system_toplevel_window_focused());
53 // If no text input client, do nothing.
54 if (!GetTextInputClient())
55 return DispatchKeyEventPostIME(event
);
57 // Let an IME handle the key event first.
58 if (input_method_context_
->DispatchKeyEvent(event
)) {
59 if (event
.type() == ET_KEY_PRESSED
&&
60 (event
.flags() & ui::EF_IME_FABRICATED_KEY
) == 0) {
61 const ui::KeyEvent
fabricated_event(ET_KEY_PRESSED
,
65 DispatchKeyEventPostIME(fabricated_event
);
70 // Otherwise, insert the character.
71 const bool handled
= DispatchKeyEventPostIME(event
);
72 if (event
.type() == ET_KEY_PRESSED
&& GetTextInputClient()) {
73 const uint16 ch
= event
.GetCharacter();
75 GetTextInputClient()->InsertChar(ch
, event
.flags());
82 void InputMethodAuraLinux::OnTextInputTypeChanged(
83 const TextInputClient
* client
) {
84 if (!IsTextInputClientFocused(client
))
86 input_method_context_
->Reset();
87 // TODO(yoichio): Support inputmode HTML attribute.
88 input_method_context_
->OnTextInputTypeChanged(client
->GetTextInputType());
91 void InputMethodAuraLinux::OnCaretBoundsChanged(const TextInputClient
* client
) {
92 if (!IsTextInputClientFocused(client
))
94 input_method_context_
->OnCaretBoundsChanged(
95 GetTextInputClient()->GetCaretBounds());
98 void InputMethodAuraLinux::CancelComposition(const TextInputClient
* client
) {
99 if (!IsTextInputClientFocused(client
))
101 input_method_context_
->Reset();
102 input_method_context_
->OnTextInputTypeChanged(client
->GetTextInputType());
105 void InputMethodAuraLinux::OnInputLocaleChanged() {
108 std::string
InputMethodAuraLinux::GetInputLocale() {
112 bool InputMethodAuraLinux::IsActive() {
113 // InputMethodAuraLinux is always ready and up.
117 bool InputMethodAuraLinux::IsCandidatePopupOpen() const {
118 // There seems no way to detect candidate windows or any popups.
122 // Overriden from ui::LinuxInputMethodContextDelegate
124 void InputMethodAuraLinux::OnCommit(const base::string16
& text
) {
125 if (!IsTextInputTypeNone())
126 GetTextInputClient()->InsertText(text
);
129 void InputMethodAuraLinux::OnPreeditChanged(
130 const CompositionText
& composition_text
) {
131 TextInputClient
* text_input_client
= GetTextInputClient();
132 if (text_input_client
)
133 text_input_client
->SetCompositionText(composition_text
);
136 void InputMethodAuraLinux::OnPreeditEnd() {
137 TextInputClient
* text_input_client
= GetTextInputClient();
138 if (text_input_client
&& text_input_client
->HasCompositionText())
139 text_input_client
->ClearCompositionText();
142 void InputMethodAuraLinux::OnPreeditStart() {}
144 // Overridden from InputMethodBase.
146 void InputMethodAuraLinux::OnDidChangeFocusedClient(
147 TextInputClient
* focused_before
,
148 TextInputClient
* focused
) {
149 input_method_context_
->Reset();
150 input_method_context_
->OnTextInputTypeChanged(
151 focused
? focused
->GetTextInputType() : TEXT_INPUT_TYPE_NONE
);
153 InputMethodBase::OnDidChangeFocusedClient(focused_before
, focused
);