1 // Copyright 2014 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 #ifndef UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_
6 #define UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "ui/base/ime/chromeos/character_composer.h"
16 #include "ui/base/ime/chromeos/ime_bridge.h"
17 #include "ui/base/ime/composition_text.h"
18 #include "ui/base/ime/input_method_base.h"
22 class CompositionText;
24 } // namespace chromeos
28 // A ui::InputMethod implementation based on IBus.
29 class UI_BASE_EXPORT InputMethodChromeOS
30 : public InputMethodBase,
31 public chromeos::IBusInputContextHandlerInterface {
33 explicit InputMethodChromeOS(internal::InputMethodDelegate* delegate);
34 virtual ~InputMethodChromeOS();
36 // Overridden from InputMethod:
37 virtual void OnFocus() OVERRIDE;
38 virtual void OnBlur() OVERRIDE;
39 virtual bool OnUntranslatedIMEMessage(const base::NativeEvent& event,
40 NativeEventResult* result) OVERRIDE;
41 virtual bool DispatchKeyEvent(const ui::KeyEvent& event) OVERRIDE;
42 virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE;
43 virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE;
44 virtual void CancelComposition(const TextInputClient* client) OVERRIDE;
45 virtual void OnInputLocaleChanged() OVERRIDE;
46 virtual std::string GetInputLocale() OVERRIDE;
47 virtual bool IsActive() OVERRIDE;
48 virtual bool IsCandidatePopupOpen() const OVERRIDE;
51 // Converts |text| into CompositionText.
52 void ExtractCompositionText(const chromeos::CompositionText& text,
53 uint32 cursor_position,
54 CompositionText* out_composition) const;
56 // Process a key returned from the input method.
57 virtual void ProcessKeyEventPostIME(const ui::KeyEvent& event,
60 // Resets context and abandon all pending results and key events.
64 class PendingKeyEvent;
66 // Overridden from InputMethodBase:
67 virtual void OnWillChangeFocusedClient(TextInputClient* focused_before,
68 TextInputClient* focused) OVERRIDE;
69 virtual void OnDidChangeFocusedClient(TextInputClient* focused_before,
70 TextInputClient* focused) OVERRIDE;
72 // Asks the client to confirm current composition text.
73 void ConfirmCompositionText();
75 // Checks the availability of focused text input client and update focus
77 void UpdateContextFocusState();
79 // Processes a key event that was already filtered by the input method.
80 // A VKEY_PROCESSKEY may be dispatched to the focused View.
81 void ProcessFilteredKeyPressEvent(const ui::KeyEvent& event);
83 // Processes a key event that was not filtered by the input method.
84 void ProcessUnfilteredKeyPressEvent(const ui::KeyEvent& event);
86 // Sends input method result caused by the given key event to the focused text
88 void ProcessInputMethodResult(const ui::KeyEvent& event, bool filtered);
90 // Checks if the pending input method result needs inserting into the focused
91 // text input client as a single character.
92 bool NeedInsertChar() const;
94 // Checks if there is pending input method result.
95 bool HasInputMethodResult() const;
97 // Abandons all pending key events. It usually happends when we lose keyboard
98 // focus, the text input type is changed or we are destroyed.
99 void AbandonAllPendingKeyEvents();
101 // Passes keyevent and executes character composition if necessary. Returns
102 // true if character composer comsumes key event.
103 bool ExecuteCharacterComposer(const ui::KeyEvent& event);
105 // chromeos::IBusInputContextHandlerInterface overrides:
106 virtual void CommitText(const std::string& text) OVERRIDE;
107 virtual void UpdateCompositionText(const chromeos::CompositionText& text,
109 bool visible) OVERRIDE;
110 virtual void DeleteSurroundingText(int32 offset, uint32 length) OVERRIDE;
112 // Hides the composition text.
113 void HidePreeditText();
115 // Callback function for IMEEngineHandlerInterface::ProcessKeyEvent.
116 void ProcessKeyEventDone(uint32 id, ui::KeyEvent* event, bool is_handled);
118 // All pending key events. Note: we do not own these object, we just save
119 // pointers to these object so that we can abandon them when necessary.
120 // They will be deleted in ProcessKeyEventDone().
121 std::set<uint32> pending_key_events_;
123 // Pending composition text generated by the current pending key event.
124 // It'll be sent to the focused text input client as soon as we receive the
125 // processing result of the pending key event.
126 CompositionText composition_;
128 // Pending result text generated by the current pending key event.
129 // It'll be sent to the focused text input client as soon as we receive the
130 // processing result of the pending key event.
131 base::string16 result_text_;
133 base::string16 previous_surrounding_text_;
134 gfx::Range previous_selection_range_;
136 // Indicates if input context is focused or not.
137 bool context_focused_;
139 // Indicates if there is an ongoing composition text.
140 bool composing_text_;
142 // Indicates if the composition text is changed or deleted.
143 bool composition_changed_;
145 // The latest id of key event.
146 uint32 current_keyevent_id_;
148 // An object to compose a character from a sequence of key presses
149 // including dead key etc.
150 CharacterComposer character_composer_;
152 TextInputType previous_textinput_type_;
154 // Used for making callbacks.
155 base::WeakPtrFactory<InputMethodChromeOS> weak_ptr_factory_;
157 DISALLOW_COPY_AND_ASSIGN(InputMethodChromeOS);
162 #endif // UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_