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