OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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_VIEWS_IME_INPUT_METHOD_IBUS_H_ | |
6 #define UI_VIEWS_IME_INPUT_METHOD_IBUS_H_ | |
7 #pragma once | |
8 | |
9 #include <set> | |
10 #include <string> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/compiler_specific.h" | |
14 #include "base/memory/scoped_ptr.h" | |
15 #include "ui/base/glib/glib_integers.h" | |
16 #include "ui/base/glib/glib_signal.h" | |
17 #include "ui/base/ime/character_composer.h" | |
18 #include "ui/base/ime/composition_text.h" | |
19 #include "ui/base/ime/text_input_client.h" | |
20 #include "ui/views/events/event.h" | |
21 #include "ui/views/ime/input_method_base.h" | |
22 #include "ui/views/view.h" | |
23 | |
24 // Forward declarations, so that we don't need to include ibus.h in this file. | |
25 typedef struct _GAsyncResult GAsyncResult; | |
26 typedef struct _IBusBus IBusBus; | |
27 typedef struct _IBusInputContext IBusInputContext; | |
28 typedef struct _IBusText IBusText; | |
29 | |
30 namespace views { | |
31 | |
32 // An InputMethod implementation based on IBus. | |
33 // TODO(yusukes): Remove this class when TOUCH_UI migrates to Aura. For Aura, | |
34 // ui/base/ime/input_method_* classes are available. | |
35 class InputMethodIBus : public InputMethodBase { | |
36 public: | |
37 explicit InputMethodIBus(internal::InputMethodDelegate* delegate); | |
38 virtual ~InputMethodIBus(); | |
39 | |
40 // Overridden from InputMethod: | |
41 virtual void Init(Widget* widget) OVERRIDE; | |
42 virtual void OnFocus() OVERRIDE; | |
43 virtual void OnBlur() OVERRIDE; | |
44 virtual void DispatchKeyEvent(const KeyEvent& key) OVERRIDE; | |
45 virtual void OnTextInputTypeChanged(View* view) OVERRIDE; | |
46 virtual void OnCaretBoundsChanged(View* view) OVERRIDE; | |
47 virtual void CancelComposition(View* view) OVERRIDE; | |
48 virtual std::string GetInputLocale() OVERRIDE; | |
49 virtual base::i18n::TextDirection GetInputTextDirection() OVERRIDE; | |
50 virtual bool IsActive() OVERRIDE; | |
51 | |
52 // Returns true when | |
53 // 1) built with GYP_DEFINES="touchui=1" or, | |
54 // 2) enabled by SetEnabledInputMethodIBus or, | |
55 // 3) enabled by command line flag "--enable-inputmethod-ibus" | |
56 static bool IsInputMethodIBusEnabled(); | |
57 // Enable/Disable InputMethodIBus | |
58 static void SetEnableInputMethodIBus(bool enabled); | |
59 | |
60 private: | |
61 // A class to hold all data related to a key event being processed by the | |
62 // input method but still has no result back yet. | |
63 class PendingKeyEvent; | |
64 | |
65 // A class to hold information of a pending request for creating an ibus input | |
66 // context. | |
67 class PendingCreateICRequest; | |
68 | |
69 // Overridden from InputMethodBase: | |
70 virtual void OnWillChangeFocus(View* focused_before, View* focused) OVERRIDE; | |
71 virtual void OnDidChangeFocus(View* focused_before, View* focused) OVERRIDE; | |
72 | |
73 // Creates |context_| instance asynchronously. | |
74 void CreateContext(); | |
75 | |
76 // Sets |context_| and hooks necessary signals. | |
77 void SetContext(IBusInputContext* ic); | |
78 | |
79 // Destroys |context_| instance. | |
80 void DestroyContext(); | |
81 | |
82 // Asks the client to confirm current composition text. | |
83 void ConfirmCompositionText(); | |
84 | |
85 // Resets |context_| and abandon all pending results and key events. | |
86 void ResetContext(); | |
87 | |
88 // Checks the availability of focused text input client and update focus state | |
89 // of |context_| and |context_simple_| accordingly. | |
90 void UpdateContextFocusState(); | |
91 | |
92 // Process a key returned from the input method. | |
93 void ProcessKeyEventPostIME(const KeyEvent& key, guint32 ibus_keycode, | |
94 bool handled); | |
95 | |
96 // Processes a key event that was already filtered by the input method. | |
97 // A VKEY_PROCESSKEY may be dispatched to the focused View. | |
98 void ProcessFilteredKeyPressEvent(const KeyEvent& key); | |
99 | |
100 // Processes a key event that was not filtered by the input method. | |
101 void ProcessUnfilteredKeyPressEvent(const KeyEvent& key, | |
102 guint32 ibus_keycode); | |
103 | |
104 // Sends input method result caused by the given key event to the focused text | |
105 // input client. | |
106 void ProcessInputMethodResult(const KeyEvent& key, bool filtered); | |
107 | |
108 // Checks if the pending input method result needs inserting into the focused | |
109 // text input client as a single character. | |
110 bool NeedInsertChar() const; | |
111 | |
112 // Checks if there is pending input method result. | |
113 bool HasInputMethodResult() const; | |
114 | |
115 // Fabricates a key event with VKEY_PROCESSKEY key code and dispatches it to | |
116 // the focused View. | |
117 void SendFakeProcessKeyEvent(bool pressed) const; | |
118 | |
119 // Called when a pending key event has finished. The event will be removed | |
120 // from |pending_key_events_|. | |
121 void FinishPendingKeyEvent(PendingKeyEvent* pending_key); | |
122 | |
123 // Abandons all pending key events. It usually happends when we lose keyboard | |
124 // focus, the text input type is changed or we are destroyed. | |
125 void AbandonAllPendingKeyEvents(); | |
126 | |
127 // Event handlers for IBusInputContext: | |
128 CHROMEG_CALLBACK_1(InputMethodIBus, void, OnCommitText, | |
129 IBusInputContext*, IBusText*); | |
130 CHROMEG_CALLBACK_3(InputMethodIBus, void, OnForwardKeyEvent, | |
131 IBusInputContext*, guint, guint, guint); | |
132 CHROMEG_CALLBACK_0(InputMethodIBus, void, OnShowPreeditText, | |
133 IBusInputContext*); | |
134 CHROMEG_CALLBACK_3(InputMethodIBus, void, OnUpdatePreeditText, | |
135 IBusInputContext*, IBusText*, guint, gboolean); | |
136 CHROMEG_CALLBACK_0(InputMethodIBus, void, OnHidePreeditText, | |
137 IBusInputContext*); | |
138 CHROMEG_CALLBACK_0(InputMethodIBus, void, OnDestroy, IBusInputContext*); | |
139 | |
140 // Event handlers for IBusBus: | |
141 CHROMEG_CALLBACK_0(InputMethodIBus, void, OnIBusConnected, IBusBus*); | |
142 CHROMEG_CALLBACK_0(InputMethodIBus, void, OnIBusDisconnected, IBusBus*); | |
143 | |
144 // Returns the global IBusBus instance. | |
145 static IBusBus* GetIBus(); | |
146 | |
147 // Callback function for ibus_input_context_process_key_event_async(). | |
148 static void ProcessKeyEventDone(IBusInputContext* context, | |
149 GAsyncResult* res, | |
150 PendingKeyEvent* data); | |
151 | |
152 // Callback function for ibus_bus_create_input_context_async(). | |
153 static void CreateInputContextDone(IBusBus* bus, | |
154 GAsyncResult* res, | |
155 PendingCreateICRequest* data); | |
156 | |
157 // The input context for actual text input. | |
158 IBusInputContext* context_; | |
159 | |
160 // All pending key events. Note: we do not own these object, we just save | |
161 // pointers to these object so that we can abandon them when necessary. | |
162 // They will be deleted in ProcessKeyEventDone(). | |
163 std::set<PendingKeyEvent*> pending_key_events_; | |
164 | |
165 // The pending request for creating the |context_| instance. We need to keep | |
166 // this pointer so that we can receive or abandon the result. | |
167 PendingCreateICRequest* pending_create_ic_request_; | |
168 | |
169 // Pending composition text generated by the current pending key event. | |
170 // It'll be sent to the focused text input client as soon as we receive the | |
171 // processing result of the pending key event. | |
172 ui::CompositionText composition_; | |
173 | |
174 // Pending result text generated by the current pending key event. | |
175 // It'll be sent to the focused text input client as soon as we receive the | |
176 // processing result of the pending key event. | |
177 string16 result_text_; | |
178 | |
179 // Indicates if |context_| and |context_simple_| are focused or not. | |
180 bool context_focused_; | |
181 | |
182 // Indicates if there is an ongoing composition text. | |
183 bool composing_text_; | |
184 | |
185 // Indicates if the composition text is changed or deleted. | |
186 bool composition_changed_; | |
187 | |
188 // If it's true then all input method result received before the next key | |
189 // event will be discarded. | |
190 bool suppress_next_result_; | |
191 | |
192 // An object to compose a character from a sequence of key presses | |
193 // including dead key etc. | |
194 ui::CharacterComposer character_composer_; | |
195 | |
196 DISALLOW_COPY_AND_ASSIGN(InputMethodIBus); | |
197 }; | |
198 | |
199 } // namespace views | |
200 | |
201 #endif // UI_VIEWS_IME_INPUT_METHOD_IBUS_H_ | |
OLD | NEW |