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 11 class GURL; 12 13 namespace chromeos { 14 15 namespace input_method { 16 struct KeyEventHandle; 17 } // namespace input_method 18 19 // InputMethodEngine is used to translate from the Chrome IME API to the native 20 // API. 21 class InputMethodEngine { 22 public: 23 struct KeyboardEvent { 24 KeyboardEvent(); 25 virtual ~KeyboardEvent(); 26 27 std::string type; 28 std::string key; 29 std::string code; 30 bool alt_key; 31 bool ctrl_key; 32 bool shift_key; 33 bool caps_lock; 34 }; 35 36 enum { 37 MENU_ITEM_MODIFIED_LABEL = 0x0001, 38 MENU_ITEM_MODIFIED_STYLE = 0x0002, 39 MENU_ITEM_MODIFIED_VISIBLE = 0x0004, 40 MENU_ITEM_MODIFIED_ENABLED = 0x0008, 41 MENU_ITEM_MODIFIED_CHECKED = 0x0010, 42 MENU_ITEM_MODIFIED_ICON = 0x0020, 43 }; 44 45 enum MenuItemStyle { 46 MENU_ITEM_STYLE_NONE, 47 MENU_ITEM_STYLE_CHECK, 48 MENU_ITEM_STYLE_RADIO, 49 MENU_ITEM_STYLE_SEPARATOR, 50 }; 51 52 enum MouseButtonEvent { 53 MOUSE_BUTTON_LEFT, 54 MOUSE_BUTTON_RIGHT, 55 MOUSE_BUTTON_MIDDLE, 56 }; 57 58 enum SegmentStyle { 59 SEGMENT_STYLE_UNDERLINE, 60 SEGMENT_STYLE_DOUBLE_UNDERLINE, 61 }; 62 63 enum CandidateWindowPosition { 64 WINDOW_POS_CURSOR, 65 WINDOW_POS_COMPOSITTION, 66 }; 67 68 struct MenuItem { 69 MenuItem(); 70 virtual ~MenuItem(); 71 72 std::string id; 73 std::string label; 74 MenuItemStyle style; 75 bool visible; 76 bool enabled; 77 bool checked; 78 79 unsigned int modified; 80 std::vector<MenuItem> children; 81 }; 82 83 struct InputContext { 84 int id; 85 std::string type; 86 }; 87 88 struct UsageEntry { 89 std::string title; 90 std::string body; 91 }; 92 93 struct Candidate { 94 Candidate(); 95 virtual ~Candidate(); 96 97 std::string value; 98 int id; 99 std::string label; 100 std::string annotation; 101 UsageEntry usage; 102 std::vector<Candidate> candidates; 103 }; 104 105 struct SegmentInfo { 106 int start; 107 int end; 108 SegmentStyle style; 109 }; 110 111 class Observer { 112 public: 113 virtual ~Observer(); 114 115 // Called when the IME becomes the active IME. 116 virtual void OnActivate(const std::string& engine_id) = 0; 117 118 // Called when the IME is no longer active. 119 virtual void OnDeactivated(const std::string& engine_id) = 0; 120 121 // Called when a text field gains focus, and will be sending key events. 122 virtual void OnFocus(const InputContext& context) = 0; 123 124 // Called when a text field loses focus, and will no longer generate events. 125 virtual void OnBlur(int context_id) = 0; 126 127 // Called when an InputContext's properties change while it is focused. 128 virtual void OnInputContextUpdate(const InputContext& context) = 0; 129 130 // Called when the user pressed a key with a text field focused. 131 virtual void OnKeyEvent(const std::string& engine_id, 132 const KeyboardEvent& event, 133 input_method::KeyEventHandle* key_data) = 0; 134 135 // Called when the user clicks on an item in the candidate list. 136 virtual void OnCandidateClicked(const std::string& engine_id, 137 int candidate_id, 138 MouseButtonEvent button) = 0; 139 140 // Called when a menu item for this IME is interacted with. 141 virtual void OnMenuItemActivated(const std::string& engine_id, 142 const std::string& menu_id) = 0; 143 144 // Called when a surrounding text is changed. 145 virtual void OnSurroundingTextChanged(const std::string& engine_id, 146 const std::string& text, 147 int cursor_pos, 148 int anchor_pos) = 0; 149 150 // Called when Chrome terminates on-going text input session. 151 virtual void OnReset(const std::string& engine_id) = 0; 152 }; 153 154 virtual ~InputMethodEngine() {} 155 156 // Called when the input metho initialization is done. 157 // This function is called from private API. 158 // TODO(nona): Remove this function. 159 virtual void StartIme() = 0; 160 161 // Set the current composition and associated properties. 162 virtual bool SetComposition(int context_id, 163 const char* text, 164 int selection_start, 165 int selection_end, 166 int cursor, 167 const std::vector<SegmentInfo>& segments, 168 std::string* error) = 0; 169 170 // Clear the current composition. 171 virtual bool ClearComposition(int context_id, std::string* error) = 0; 172 173 // Commit the specified text to the specified context. Fails if the context 174 // is not focused. 175 virtual bool CommitText(int context_id, const char* text, 176 std::string* error) = 0; 177 178 // Show or hide the candidate window. 179 virtual bool SetCandidateWindowVisible(bool visible, std::string* error) = 0; 180 181 // Show or hide the cursor in the candidate window. 182 virtual void SetCandidateWindowCursorVisible(bool visible) = 0; 183 184 // Set the orientation of the candidate window. 185 virtual void SetCandidateWindowVertical(bool vertical) = 0; 186 187 // Set the number of candidates displayed in the candidate window. 188 virtual void SetCandidateWindowPageSize(int size) = 0; 189 190 // Set the text that appears as a label in the candidate window. 191 virtual void SetCandidateWindowAuxText(const char* text) = 0; 192 193 // Show or hide the extra text in the candidate window. 194 virtual void SetCandidateWindowAuxTextVisible(bool visible) = 0; 195 196 // Sets the candidate window position. 197 virtual void SetCandidateWindowPosition(CandidateWindowPosition position) = 0; 198 199 // Set the list of entries displayed in the candidate window. 200 virtual bool SetCandidates(int context_id, 201 const std::vector<Candidate>& candidates, 202 std::string* error) = 0; 203 204 // Set the position of the cursor in the candidate window. 205 virtual bool SetCursorPosition(int context_id, int candidate_id, 206 std::string* error) = 0; 207 208 // Set the list of items that appears in the language menu when this IME is 209 // active. 210 virtual bool SetMenuItems(const std::vector<MenuItem>& items) = 0; 211 212 // Update the state of the menu items. 213 virtual bool UpdateMenuItems(const std::vector<MenuItem>& items) = 0; 214 215 // Returns true if this IME is active, false if not. 216 virtual bool IsActive() const = 0; 217 218 // Inform the engine that a key event has been processed. 219 virtual void KeyEventDone(input_method::KeyEventHandle* key_data, 220 bool handled) = 0; 221 222 // Deletes |number_of_chars| unicode characters as the basis of |offset| from 223 // the surrounding text. The |offset| is relative position based on current 224 // caret. 225 // NOTE: Currently we are falling back to backspace forwarding workaround, 226 // because delete_surrounding_text is not supported in Chrome. So this 227 // function is restricted for only preceding text. 228 // TODO(nona): Support full spec delete surrounding text. 229 virtual bool DeleteSurroundingText(int context_id, 230 int offset, 231 size_t number_of_chars, 232 std::string* error) = 0; 233 234 // Create an IME engine. 235 static InputMethodEngine* CreateEngine( 236 InputMethodEngine::Observer* observer, 237 const char* engine_name, 238 const char* extension_id, 239 const char* engine_id, 240 const char* description, 241 const std::vector<std::string>& language, 242 const std::vector<std::string>& layouts, 243 const GURL& options_page, 244 std::string* error); 245 }; 246 247 } // namespace chromeos 248 249 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_INPUT_METHOD_ENGINE_H_ 250