input_method_engine.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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
5#ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_
6#define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_
7
8#include <string>
9#include <vector>
10
11namespace chromeos {
12
13namespace input_method {
14struct KeyEventHandle;
15}  // namespace input_method
16
17extern const char* kExtensionImePrefix;
18
19// InputMethodEngine is used to translate from the Chrome IME API to the native
20// API.
21class InputMethodEngine {
22 public:
23  struct KeyboardEvent {
24    KeyboardEvent();
25    virtual ~KeyboardEvent();
26
27    std::string type;
28    std::string key;
29    bool alt_key;
30    bool ctrl_key;
31    bool shift_key;
32  };
33
34  enum {
35    MENU_ITEM_MODIFIED_LABEL        = 0x0001,
36    MENU_ITEM_MODIFIED_STYLE        = 0x0002,
37    MENU_ITEM_MODIFIED_VISIBLE      = 0x0004,
38    MENU_ITEM_MODIFIED_ENABLED      = 0x0008,
39    MENU_ITEM_MODIFIED_CHECKED      = 0x0010,
40    MENU_ITEM_MODIFIED_ICON         = 0x0020,
41  };
42
43  enum MenuItemStyle {
44    MENU_ITEM_STYLE_NONE,
45    MENU_ITEM_STYLE_CHECK,
46    MENU_ITEM_STYLE_RADIO,
47    MENU_ITEM_STYLE_SEPARATOR,
48  };
49
50  enum MouseButtonEvent {
51    MOUSE_BUTTON_LEFT,
52    MOUSE_BUTTON_RIGHT,
53    MOUSE_BUTTON_MIDDLE,
54  };
55
56  enum SegmentStyle {
57    SEGMENT_STYLE_UNDERLINE,
58    SEGMENT_STYLE_DOUBLE_UNDERLINE,
59  };
60
61  struct MenuItem {
62    MenuItem();
63    virtual ~MenuItem();
64
65    std::string id;
66    std::string label;
67    MenuItemStyle style;
68    bool visible;
69    bool enabled;
70    bool checked;
71
72    unsigned int modified;
73    std::vector<MenuItem> children;
74  };
75
76  struct InputContext {
77    int id;
78    std::string type;
79  };
80
81  struct Candidate {
82    Candidate();
83    virtual ~Candidate();
84
85    std::string value;
86    int id;
87    std::string label;
88    std::string annotation;
89    std::vector<Candidate> candidates;
90  };
91
92  struct SegmentInfo {
93    int start;
94    int end;
95    SegmentStyle style;
96  };
97
98  class Observer {
99   public:
100    virtual ~Observer();
101
102    // Called when the IME becomes the active IME.
103    virtual void OnActivate(const std::string& engine_id) = 0;
104
105    // Called when the IME is no longer active.
106    virtual void OnDeactivated(const std::string& engine_id) = 0;
107
108    // Called when a text field gains focus, and will be sending key events.
109    virtual void OnFocus(const InputContext& context) = 0;
110
111    // Called when a text field loses focus, and will no longer generate events.
112    virtual void OnBlur(int context_id) = 0;
113
114    // Called when an InputContext's properties change while it is focused.
115    virtual void OnInputContextUpdate(const InputContext& context) = 0;
116
117    // Called when the user pressed a key with a text field focused.
118    virtual void OnKeyEvent(const std::string& engine_id,
119                            const KeyboardEvent& event,
120                            input_method::KeyEventHandle* key_data) = 0;
121
122    // Called when the user clicks on an item in the candidate list.
123    virtual void OnCandidateClicked(const std::string& engine_id,
124                                    int candidate_id,
125                                    MouseButtonEvent button) = 0;
126
127    // Called when a menu item for this IME is interacted with.
128    virtual void OnMenuItemActivated(const std::string& engine_id,
129                                     const std::string& menu_id) = 0;
130  };
131
132  virtual ~InputMethodEngine() {}
133
134  // Set the current composition and associated properties.
135  virtual bool SetComposition(int context_id,
136                              const char* text,
137                              int selection_start,
138                              int selection_end,
139                              int cursor,
140                              const std::vector<SegmentInfo>& segments,
141                              std::string* error) = 0;
142
143  // Clear the current composition.
144  virtual bool ClearComposition(int context_id, std::string* error) = 0;
145
146  // Commit the specified text to the specified context.  Fails if the context
147  // is not focused.
148  virtual bool CommitText(int context_id, const char* text,
149                          std::string* error) = 0;
150
151  // Show or hide the candidate window.
152  virtual bool SetCandidateWindowVisible(bool visible, std::string* error) = 0;
153
154  // Show or hide the cursor in the candidate window.
155  virtual void SetCandidateWindowCursorVisible(bool visible) = 0;
156
157  // Set the orientation of the candidate window.
158  virtual void SetCandidateWindowVertical(bool vertical) = 0;
159
160  // Set the number of candidates displayed in the candidate window.
161  virtual void SetCandidateWindowPageSize(int size) = 0;
162
163  // Set the text that appears as a label in the candidate window.
164  virtual void SetCandidateWindowAuxText(const char* text) = 0;
165
166  // Show or hide the extra text in the candidate window.
167  virtual void SetCandidateWindowAuxTextVisible(bool visible) = 0;
168
169  // Set the list of entries displayed in the candidate window.
170  virtual bool SetCandidates(int context_id,
171                             const std::vector<Candidate>& candidates,
172                             std::string* error) = 0;
173
174  // Set the position of the cursor in the candidate window.
175  virtual bool SetCursorPosition(int context_id, int candidate_id,
176                                 std::string* error) = 0;
177
178  // Set the list of items that appears in the language menu when this IME is
179  // active.
180  virtual bool SetMenuItems(const std::vector<MenuItem>& items) = 0;
181
182  // Update the state of the menu items.
183  virtual bool UpdateMenuItems(const std::vector<MenuItem>& items) = 0;
184
185  // Returns true if this IME is active, false if not.
186  virtual bool IsActive() const = 0;
187
188  // Inform the engine that a key event has been processed.
189  virtual void KeyEventDone(input_method::KeyEventHandle* key_data,
190                            bool handled) = 0;
191
192  // Create an IME engine.
193  static InputMethodEngine* CreateEngine(
194      InputMethodEngine::Observer* observer,
195      const char* engine_name,
196      const char* extension_id,
197      const char* engine_id,
198      const char* description,
199      const char* language,
200      const std::vector<std::string>& layouts,
201      std::string* error);
202};
203
204}  // namespace chromeos
205
206#endif  // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_
207