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