Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / base / ime / input_method_chromeos.h
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.
4
5 #ifndef UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_
6 #define UI_BASE_IME_INPUT_METHOD_CHROMEOS_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/ime_bridge.h"
17 #include "ui/base/ime/composition_text.h"
18 #include "ui/base/ime/input_method_base.h"
19
20 namespace chromeos {
21 namespace ibus {
22 class CompositionText;
23 }  // namespace ibus
24 }  // namespace chromeos
25
26 namespace ui {
27
28 // A ui::InputMethod implementation based on IBus.
29 class UI_BASE_EXPORT InputMethodChromeOS
30     : public InputMethodBase,
31       public chromeos::IBusInputContextHandlerInterface {
32  public:
33   explicit InputMethodChromeOS(internal::InputMethodDelegate* delegate);
34   virtual ~InputMethodChromeOS();
35
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;
49
50  protected:
51   // Converts |text| into CompositionText.
52   void ExtractCompositionText(const chromeos::CompositionText& text,
53                               uint32 cursor_position,
54                               CompositionText* out_composition) const;
55
56   // Process a key returned from the input method.
57   virtual void ProcessKeyEventPostIME(const ui::KeyEvent& event,
58                                       bool handled);
59
60   // Resets context and abandon all pending results and key events.
61   void ResetContext();
62
63  private:
64   class PendingKeyEvent;
65
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;
71
72   // Asks the client to confirm current composition text.
73   void ConfirmCompositionText();
74
75   // Checks the availability of focused text input client and update focus
76   // state.
77   void UpdateContextFocusState();
78
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);
82
83   // Processes a key event that was not filtered by the input method.
84   void ProcessUnfilteredKeyPressEvent(const ui::KeyEvent& event);
85
86   // Sends input method result caused by the given key event to the focused text
87   // input client.
88   void ProcessInputMethodResult(const ui::KeyEvent& event, bool filtered);
89
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;
93
94   // Checks if there is pending input method result.
95   bool HasInputMethodResult() const;
96
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();
100
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);
104
105   // chromeos::IBusInputContextHandlerInterface overrides:
106   virtual void CommitText(const std::string& text) OVERRIDE;
107   virtual void UpdateCompositionText(const chromeos::CompositionText& text,
108                                      uint32 cursor_pos,
109                                      bool visible) OVERRIDE;
110   virtual void DeleteSurroundingText(int32 offset, uint32 length) OVERRIDE;
111
112   // Hides the composition text.
113   void HidePreeditText();
114
115   // Callback function for IMEEngineHandlerInterface::ProcessKeyEvent.
116   void ProcessKeyEventDone(uint32 id, ui::KeyEvent* event, bool is_handled);
117
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_;
122
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_;
127
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_;
132
133   base::string16 previous_surrounding_text_;
134   gfx::Range previous_selection_range_;
135
136   // Indicates if input context is focused or not.
137   bool context_focused_;
138
139   // Indicates if there is an ongoing composition text.
140   bool composing_text_;
141
142   // Indicates if the composition text is changed or deleted.
143   bool composition_changed_;
144
145   // The latest id of key event.
146   uint32 current_keyevent_id_;
147
148   // An object to compose a character from a sequence of key presses
149   // including dead key etc.
150   CharacterComposer character_composer_;
151
152   TextInputType previous_textinput_type_;
153
154   // Used for making callbacks.
155   base::WeakPtrFactory<InputMethodChromeOS> weak_ptr_factory_;
156
157   DISALLOW_COPY_AND_ASSIGN(InputMethodChromeOS);
158 };
159
160 }  // namespace ui
161
162 #endif  // UI_BASE_IME_INPUT_METHOD_CHROMEOS_H_