1// Copyright (c) 2012 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#ifndef UI_BASE_IME_INPUT_METHOD_IBUS_H_
6#define UI_BASE_IME_INPUT_METHOD_IBUS_H_
7
8#include <set>
9#include <string>
10
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/ibus_bridge.h"
17#include "ui/base/ime/composition_text.h"
18#include "ui/base/ime/input_method_base.h"
19
20namespace dbus {
21class ObjectPath;
22}
23namespace chromeos {
24namespace ibus {
25class IBusText;
26}  // namespace ibus
27}  // namespace chromeos
28
29namespace ui {
30
31// A ui::InputMethod implementation based on IBus.
32class UI_EXPORT InputMethodIBus
33    : public InputMethodBase,
34      public chromeos::IBusInputContextHandlerInterface {
35 public:
36  explicit InputMethodIBus(internal::InputMethodDelegate* delegate);
37  virtual ~InputMethodIBus();
38
39  // Overridden from InputMethod:
40  virtual void OnFocus() OVERRIDE;
41  virtual void OnBlur() OVERRIDE;
42  virtual bool OnUntranslatedIMEMessage(const base::NativeEvent& event,
43                                        NativeEventResult* result) OVERRIDE;
44  virtual bool DispatchKeyEvent(const ui::KeyEvent& event) OVERRIDE;
45  virtual void OnTextInputTypeChanged(const TextInputClient* client) OVERRIDE;
46  virtual void OnCaretBoundsChanged(const TextInputClient* client) OVERRIDE;
47  virtual void CancelComposition(const TextInputClient* client) OVERRIDE;
48  virtual void OnInputLocaleChanged() OVERRIDE;
49  virtual std::string GetInputLocale() OVERRIDE;
50  virtual base::i18n::TextDirection GetInputTextDirection() OVERRIDE;
51  virtual bool IsActive() OVERRIDE;
52  virtual bool IsCandidatePopupOpen() const OVERRIDE;
53
54 protected:
55  // Converts |text| into CompositionText.
56  void ExtractCompositionText(const chromeos::IBusText& text,
57                              uint32 cursor_position,
58                              CompositionText* out_composition) const;
59
60  // Process a key returned from the input method.
61  virtual void ProcessKeyEventPostIME(const ui::KeyEvent& event,
62                                      bool handled);
63
64  // Resets context and abandon all pending results and key events.
65  void ResetContext();
66
67 private:
68  class PendingKeyEvent;
69
70  // Overridden from InputMethodBase:
71  virtual void OnWillChangeFocusedClient(TextInputClient* focused_before,
72                                         TextInputClient* focused) OVERRIDE;
73  virtual void OnDidChangeFocusedClient(TextInputClient* focused_before,
74                                        TextInputClient* focused) OVERRIDE;
75
76  // Asks the client to confirm current composition text.
77  void ConfirmCompositionText();
78
79  // Checks the availability of focused text input client and update focus
80  // state.
81  void UpdateContextFocusState();
82
83  // Processes a key event that was already filtered by the input method.
84  // A VKEY_PROCESSKEY may be dispatched to the focused View.
85  void ProcessFilteredKeyPressEvent(const ui::KeyEvent& event);
86
87  // Processes a key event that was not filtered by the input method.
88  void ProcessUnfilteredKeyPressEvent(const ui::KeyEvent& event);
89
90  // Sends input method result caused by the given key event to the focused text
91  // input client.
92  void ProcessInputMethodResult(const ui::KeyEvent& event, bool filtered);
93
94  // Checks if the pending input method result needs inserting into the focused
95  // text input client as a single character.
96  bool NeedInsertChar() const;
97
98  // Checks if there is pending input method result.
99  bool HasInputMethodResult() const;
100
101  // Abandons all pending key events. It usually happends when we lose keyboard
102  // focus, the text input type is changed or we are destroyed.
103  void AbandonAllPendingKeyEvents();
104
105  // Passes keyevent and executes character composition if necessary. Returns
106  // true if character composer comsumes key event.
107  bool ExecuteCharacterComposer(const ui::KeyEvent& event);
108
109  // chromeos::IBusInputContextHandlerInterface overrides:
110  virtual void CommitText(const std::string& text) OVERRIDE;
111  virtual void UpdatePreeditText(const chromeos::IBusText& text,
112                                 uint32 cursor_pos,
113                                 bool visible) OVERRIDE;
114  virtual void DeleteSurroundingText(int32 offset, uint32 length) OVERRIDE;
115
116  // Hides the composition text.
117  void HidePreeditText();
118
119  // Callback function for IBusEngineHandlerInterface::ProcessKeyEvent.
120  void ProcessKeyEventDone(uint32 id, ui::KeyEvent* event, bool is_handled);
121
122  // All pending key events. Note: we do not own these object, we just save
123  // pointers to these object so that we can abandon them when necessary.
124  // They will be deleted in ProcessKeyEventDone().
125  std::set<uint32> pending_key_events_;
126
127  // Pending composition text generated by the current pending key event.
128  // It'll be sent to the focused text input client as soon as we receive the
129  // processing result of the pending key event.
130  CompositionText composition_;
131
132  // Pending result text generated by the current pending key event.
133  // It'll be sent to the focused text input client as soon as we receive the
134  // processing result of the pending key event.
135  string16 result_text_;
136
137  string16 previous_surrounding_text_;
138  gfx::Range previous_selection_range_;
139
140  // Indicates if input context is focused or not.
141  bool context_focused_;
142
143  // Indicates if there is an ongoing composition text.
144  bool composing_text_;
145
146  // Indicates if the composition text is changed or deleted.
147  bool composition_changed_;
148
149  // If it's true then all input method result received before the next key
150  // event will be discarded.
151  bool suppress_next_result_;
152
153  // The latest id of key event.
154  uint32 current_keyevent_id_;
155
156  // An object to compose a character from a sequence of key presses
157  // including dead key etc.
158  CharacterComposer character_composer_;
159
160  TextInputType previous_textinput_type_;
161
162  // Used for making callbacks.
163  base::WeakPtrFactory<InputMethodIBus> weak_ptr_factory_;
164
165  DISALLOW_COPY_AND_ASSIGN(InputMethodIBus);
166};
167
168}  // namespace ui
169
170#endif  // UI_BASE_IME_INPUT_METHOD_IBUS_H_
171