- add sources.
[platform/framework/web/crosswalk.git] / src / chromeos / ime / ibus_bridge.h
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 // TODO(nona): Rename this file to ime_bridge.h
5
6 #ifndef CHROMEOS_IME_IBUS_BRIDGE_H_
7 #define CHROMEOS_IME_IBUS_BRIDGE_H_
8
9 #include <string>
10 #include "base/basictypes.h"
11 #include "chromeos/chromeos_export.h"
12 #include "chromeos/dbus/ibus/ibus_engine_factory_service.h"
13 #include "chromeos/ime/ime_constants.h"
14 #include "chromeos/ime/input_method_property.h"
15
16 namespace chromeos {
17 namespace input_method {
18 class CandidateWindow;
19 }  // namespace input_method
20
21 class IBusText;
22
23 class CHROMEOS_EXPORT IBusInputContextHandlerInterface {
24  public:
25   // Called when the engine commit a text.
26   virtual void CommitText(const std::string& text) = 0;
27
28   // Called when the engine forward a key event.
29   virtual void ForwardKeyEvent(uint32 keyval, uint32 keycode, uint32 state) = 0;
30
31   // Called when the engine update preedit stroing.
32   virtual void UpdatePreeditText(const IBusText& text,
33                                  uint32 cursor_pos,
34                                  bool visible) = 0;
35
36   // Called when the engine request showing preedit string.
37   virtual void ShowPreeditText() = 0;
38
39   // Called when the engine request hiding preedit string.
40   virtual void HidePreeditText() = 0;
41
42   // Called when the engine request deleting surrounding string.
43   virtual void DeleteSurroundingText(int32 offset, uint32 length) = 0;
44 };
45
46
47 // A interface to handle the engine handler method call.
48 class CHROMEOS_EXPORT IBusEngineHandlerInterface {
49  public:
50   typedef base::Callback<void (bool consumed)> KeyEventDoneCallback;
51
52   // Following capability mask is introduced from
53   // http://ibus.googlecode.com/svn/docs/ibus-1.4/ibus-ibustypes.html#IBusCapabilite
54   // TODO(nona): Move to ibus_contants and merge one in ui/base/ime/*
55   enum IBusCapability {
56     IBUS_CAPABILITY_PREEDIT_TEXT = 1U,
57     IBUS_CAPABILITY_FOCUS = 8U,
58   };
59
60   virtual ~IBusEngineHandlerInterface() {}
61
62   // Called when the Chrome input field get the focus.
63   virtual void FocusIn(ibus::TextInputType text_input_type) = 0;
64
65   // Called when the Chrome input field lose the focus.
66   virtual void FocusOut() = 0;
67
68   // Called when the IME is enabled.
69   virtual void Enable() = 0;
70
71   // Called when the IME is disabled.
72   virtual void Disable() = 0;
73
74   // Called when a property is activated or changed.
75   virtual void PropertyActivate(const std::string& property_name) = 0;
76
77   // Called when a property is shown.
78   virtual void PropertyShow(const std::string& property_name) = 0;
79
80   // Called when a property is hidden.
81   virtual void PropertyHide(const std::string& property_name) = 0;
82
83   // Called when the Chrome input field set their capabilities.
84   virtual void SetCapability(IBusCapability capability) = 0;
85
86   // Called when the IME is reset.
87   virtual void Reset() = 0;
88
89   // Called when the key event is received. The |keycode| is raw layout
90   // independent keycode. The |keysym| is result of XLookupString function
91   // which translate |keycode| to keyboard layout dependent symbol value.
92   // Actual implementation must call |callback| after key event handling.
93   // For example: key press event for 'd' key on us layout and dvorak layout.
94   //                  keyval keycode state
95   //      us layout :  0x64   0x20    0x00
96   //  dvorak layout :  0x65   0x20    0x00
97   virtual void ProcessKeyEvent(uint32 keysym, uint32 keycode, uint32 state,
98                                const KeyEventDoneCallback& callback) = 0;
99
100   // Called when the candidate in lookup table is clicked. The |index| is 0
101   // based candidate index in lookup table. The |state| is same value as
102   // GdkModifierType in
103   // http://developer.gnome.org/gdk/stable/gdk-Windows.html#GdkModifierType
104   virtual void CandidateClicked(uint32 index, ibus::IBusMouseButton button,
105                                 uint32 state) = 0;
106
107   // Called when a new surrounding text is set. The |text| is surrounding text
108   // and |cursor_pos| is 0 based index of cursor position in |text|. If there is
109   // selection range, |anchor_pos| represents opposite index from |cursor_pos|.
110   // Otherwise |anchor_pos| is equal to |cursor_pos|.
111   virtual void SetSurroundingText(const std::string& text, uint32 cursor_pos,
112                                   uint32 anchor_pos) = 0;
113
114  protected:
115   IBusEngineHandlerInterface() {}
116 };
117
118 // A interface to handle the candidate window related method call.
119 class CHROMEOS_EXPORT IBusPanelCandidateWindowHandlerInterface {
120  public:
121   virtual ~IBusPanelCandidateWindowHandlerInterface() {}
122
123   // Called when the IME updates the lookup table.
124   virtual void UpdateLookupTable(
125       const input_method::CandidateWindow& candidate_window,
126       bool visible) = 0;
127
128   // Called when the IME hides the lookup table.
129   virtual void HideLookupTable() = 0;
130
131   // Called when the IME updates the auxiliary text. The |text| is given in
132   // UTF-8 encoding.
133   virtual void UpdateAuxiliaryText(const std::string& text, bool visible) = 0;
134
135   // Called when the IME hides the auxiliary text.
136   virtual void HideAuxiliaryText() = 0;
137
138   // Called when the IME updates the preedit text. The |text| is given in
139   // UTF-8 encoding.
140   virtual void UpdatePreeditText(const std::string& text, uint32 cursor_pos,
141                                  bool visible) = 0;
142
143   // Called when the IME hides the preedit text.
144   virtual void HidePreeditText() = 0;
145
146   // Called when the application changes its caret location.
147   virtual void SetCursorLocation(const ibus::Rect& cursor_location,
148                                  const ibus::Rect& composition_head) = 0;
149
150  protected:
151   IBusPanelCandidateWindowHandlerInterface() {}
152 };
153
154 // A interface to handle the property related method call.
155 class CHROMEOS_EXPORT IBusPanelPropertyHandlerInterface {
156  public:
157   virtual ~IBusPanelPropertyHandlerInterface() {}
158
159   // Called when a new property is registered.
160   virtual void RegisterProperties(
161     const chromeos::input_method::InputMethodPropertyList& properties) = 0;
162
163  protected:
164   IBusPanelPropertyHandlerInterface() {}
165 };
166
167
168 // IBusBridge provides access of each IME related handler. This class is used
169 // for IME implementation without ibus-daemon. The legacy ibus IME communicates
170 // their engine with dbus protocol, but new implementation doesn't. Instead of
171 // dbus communcation, new implementation calls target service(e.g. PanelService
172 // or EngineService) directly by using this class.
173 class IBusBridge {
174  public:
175   virtual ~IBusBridge();
176
177   // Allocates the global instance. Must be called before any calls to Get().
178   static CHROMEOS_EXPORT void Initialize();
179
180   // Releases the global instance.
181   static CHROMEOS_EXPORT void Shutdown();
182
183   // Returns IBusBridge global instance. Initialize() must be called first.
184   static CHROMEOS_EXPORT IBusBridge* Get();
185
186   // Returns current InputContextHandler. This function returns NULL if input
187   // context is not ready to use.
188   virtual IBusInputContextHandlerInterface* GetInputContextHandler() const = 0;
189
190   // Updates current InputContextHandler. If there is no active input context,
191   // pass NULL for |handler|. Caller must release |handler|.
192   virtual void SetInputContextHandler(
193       IBusInputContextHandlerInterface* handler) = 0;
194
195   // Returns current EngineHandler. This function returns NULL if current engine
196   // is not ready to use.
197   virtual IBusEngineHandlerInterface* GetEngineHandler() const = 0;
198
199   // Updates current EngineHandler. If there is no active engine service, pass
200   // NULL for |handler|. Caller must release |handler|.
201   virtual void SetEngineHandler(IBusEngineHandlerInterface* handler) = 0;
202
203   // Returns current CandidateWindowHandler. This function returns NULL if
204   // current candidate window is not ready to use.
205   virtual IBusPanelCandidateWindowHandlerInterface*
206       GetCandidateWindowHandler() const = 0;
207
208   // Updates current CandidatWindowHandler. If there is no active candidate
209   // window service, pass NULL for |handler|. Caller must release |handler|.
210   virtual void SetCandidateWindowHandler(
211       IBusPanelCandidateWindowHandlerInterface* handler) = 0;
212
213   // Returns current PropertyHandler. This function returns NULL if panel window
214   // is not ready to use.
215   virtual IBusPanelPropertyHandlerInterface* GetPropertyHandler() const = 0;
216
217   // Updates current PropertyHandler. If there is no active property service,
218   // pass NULL for |handler|. Caller must release |handler|.
219   virtual void SetPropertyHandler(
220       IBusPanelPropertyHandlerInterface* handler) = 0;
221
222   // Sets create engine handler for |engine_id|. |engine_id| must not be empty
223   // and |handler| must not be null.
224   virtual void SetCreateEngineHandler(
225       const std::string& engine_id,
226       const IBusEngineFactoryService::CreateEngineHandler& handler) = 0;
227
228   // Unsets create engine handler for |engine_id|. |engine_id| must not be
229   // empty.
230   virtual void UnsetCreateEngineHandler(const std::string& engine_id) = 0;
231
232   // Creates engine. Do not call this function before SetCreateEngineHandler
233   // call with |engine_id|.
234   virtual void CreateEngine(const std::string& engine_id) = 0;
235
236  protected:
237   IBusBridge();
238
239  private:
240   DISALLOW_COPY_AND_ASSIGN(IBusBridge);
241 };
242
243 }  // namespace chromeos
244
245 #endif  // CHROMEOS_IME_IBUS_BRIDGE_H_