1 #ifndef DALI_INPUT_METHOD_CONTEXT_H
2 #define DALI_INPUT_METHOD_CONTEXT_H
5 * Copyright (c) 2019 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/events/key-event.h>
23 #include <dali/public-api/common/dali-vector.h>
26 #include <dali/public-api/object/base-handle.h>
27 #include <dali/public-api/signals/dali-signal.h>
28 #include <dali/devel-api/adaptor-framework/input-method-options.h>
33 namespace Internal DALI_INTERNAL
37 class InputMethodContext;
44 * @brief The InputMethodContext class
46 * Specifically manages the ecore input method framework which enables the virtual or hardware keyboards.
48 class DALI_ADAPTOR_API InputMethodContext : public BaseHandle
53 * @brief The direction of text.
62 * @brief Events that are generated by the InputMethodContext.
67 PRE_EDIT, ///< Pre-Edit changed
68 COMMIT, ///< Commit recieved
69 DELETE_SURROUNDING, ///< Event to delete a range of characters from the string
70 GET_SURROUNDING, ///< Event to query string and cursor position
71 PRIVATE_COMMAND ///< Private command sent from the input panel
75 * @brief Enumeration for state of the input panel.
79 DEFAULT = 0, ///< Unknown state
80 SHOW, ///< Input panel is shown
81 HIDE, ///< Input panel is hidden
82 WILL_SHOW ///< Input panel in process of being shown
86 * @brief Enumeration for the type of Keyboard.
90 SOFTWARE_KEYBOARD, ///< Software keyboard (Virtual keyboard) is default
91 HARDWARE_KEYBOARD ///< Hardware keyboard
95 * @brief Enumeration for the language mode of the input panel.
97 enum class InputPanelLanguage
99 AUTOMATIC, ///< IME Language automatically set depending on the system display
100 ALPHABET ///< Latin alphabet at all times
104 * @brief Enumeration for the preedit style types.
106 enum class PreeditStyle
108 NONE, ///< None style
109 UNDERLINE, ///< Underline substring style
110 REVERSE, ///< Reverse substring style
111 HIGHLIGHT, ///< Highlight substring style
112 CUSTOM_PLATFORM_STYLE_1, ///< Custom style for platform
113 CUSTOM_PLATFORM_STYLE_2, ///< Custom style for platform
114 CUSTOM_PLATFORM_STYLE_3, ///< Custom style for platform
115 CUSTOM_PLATFORM_STYLE_4 ///< Custom style for platform
119 * @brief This structure is for the preedit style types and indices.
121 struct PreeditAttributeData
123 PreeditAttributeData()
124 : preeditType( PreeditStyle::NONE ),
130 PreeditStyle preeditType; /// The preedit style type
131 unsigned int startIndex; /// The start index of preedit
132 unsigned int endIndex; /// The end index of preedit
136 * @brief This structure is used to pass on data from the InputMethodContext regarding predictive text.
141 * @brief Default Constructor.
144 : predictiveString(),
154 * @param[in] aEventName The name of the event from the InputMethodContext.
155 * @param[in] aPredictiveString The pre-edit or commit string.
156 * @param[in] aCursorOffset Start position from the current cursor position to start deleting characters.
157 * @param[in] aNumberOfChars The number of characters to delete from the cursorOffset.
159 EventData( EventType aEventName, const std::string& aPredictiveString, int aCursorOffset, int aNumberOfChars )
160 : predictiveString( aPredictiveString ),
161 eventName( aEventName ),
162 cursorOffset( aCursorOffset ),
163 numberOfChars( aNumberOfChars )
168 std::string predictiveString; ///< The pre-edit or commit string.
169 EventType eventName; ///< The name of the event from the InputMethodContext.
170 int cursorOffset; ///< Start position from the current cursor position to start deleting characters.
171 int numberOfChars; ///< number of characters to delete from the cursorOffset.
175 * @brief Data required by InputMethodContext from the callback
186 preeditResetRequired( false )
192 * @param[in] aUpdate True if cursor position needs to be updated
193 * @param[in] aCursorPosition new position of cursor
194 * @param[in] aCurrentText current text string
195 * @param[in] aPreeditResetRequired flag if preedit reset is required.
197 CallbackData( bool aUpdate, int aCursorPosition, const std::string& aCurrentText, bool aPreeditResetRequired )
198 : currentText( aCurrentText ),
199 cursorPosition( aCursorPosition ),
201 preeditResetRequired( aPreeditResetRequired )
205 std::string currentText; ///< current text string
206 int cursorPosition; ///< new position of cursor
207 bool update :1; ///< if cursor position needs to be updated
208 bool preeditResetRequired :1; ///< flag if preedit reset is required.
211 typedef Signal< void (InputMethodContext&) > ActivatedSignalType; ///< Keyboard actived signal
212 typedef Signal< CallbackData ( InputMethodContext&, const EventData& ) > KeyboardEventSignalType; ///< keyboard events
213 typedef Signal< void () > VoidSignalType;
214 typedef Signal< void ( bool ) > StatusSignalType;
215 typedef Signal< void ( KeyboardType ) > KeyboardTypeSignalType; ///< keyboard type
216 typedef Signal< void ( int ) > KeyboardResizedSignalType; ///< Keyboard resized signal
217 typedef Signal< void ( int ) > LanguageChangedSignalType; ///< Language changed signal
218 typedef Signal< void ( const std::string&, const std::string&, const std::string& ) > ContentReceivedSignalType; ///< Content received signal
220 using PreEditAttributeDataContainer = Vector< Dali::InputMethodContext::PreeditAttributeData >;
225 * @brief Retrieve a handle to the instance of InputMethodContext.
226 * @return A handle to the InputMethodContext.
227 * @brief Constructor.
229 InputMethodContext();
234 * This is non-virtual since derived Handle types must not contain data or virtual methods.
236 ~InputMethodContext();
239 * @brief Create a new instance of an InputMethodContext.
241 static InputMethodContext New();
244 * @brief Create a new instance of an InputMethodContext.
246 * @param[in] actor The actor that uses the new InputMethodContext instance.
248 static InputMethodContext New( Actor actor );
251 * @brief Copy constructor.
253 * @param[in] inputMethodContext InputMethodContext to copy. The copied inputMethodContext will point at the same implementation.
255 InputMethodContext( const InputMethodContext& inputMethodContext );
258 * @brief Assignment operator.
260 * @param[in] inputMethodContext The InputMethodContext to assign from.
261 * @return The updated InputMethodContext.
263 InputMethodContext& operator=( const InputMethodContext& inputMethodContext );
266 * @brief Downcast a handle to InputMethodContext handle.
268 * If handle points to an InputMethodContext the downcast produces valid
269 * handle. If not the returned handle is left uninitialized.
271 * @param[in] handle Handle to an object.
272 * @return Handle to an InputMethodContext or an uninitialized handle.
274 static InputMethodContext DownCast( BaseHandle handle );
279 * @brief Finalize the InputMethodContext.
281 * It means that the context will be deleted.
286 * @brief Activate the InputMethodContext.
288 * It means that the text editing is started at somewhere.
289 * If the H/W keyboard isn't connected then it will show the virtual keyboard.
294 * @brief Deactivate the InputMethodContext.
296 * It means that the text editing is finished at somewhere.
301 * @brief Get the restoration status, which controls if the keyboard is restored after the focus lost then regained.
303 * If true then keyboard will be restored (activated) after focus is regained.
304 * @return restoration status.
306 bool RestoreAfterFocusLost() const;
309 * @brief Set status whether the InputMethodContext has to restore the keyboard after losing focus.
311 * @param[in] toggle True means that keyboard should be restored after focus lost and regained.
313 void SetRestoreAfterFocusLost( bool toggle );
316 * @brief Send message reset the pred-edit state / InputMethodContext module.
318 * Used to interupt pre-edit state maybe due to a touch input.
323 * @brief Notifies InputMethodContext that the cursor position has changed, required for features like auto-capitalisation.
325 void NotifyCursorPosition();
328 * @brief Sets cursor position stored in VirtualKeyboard, this is required by the InputMethodContext.
330 * @param[in] cursorPosition position of cursor
332 void SetCursorPosition( unsigned int cursorPosition );
335 * @brief Gets cursor position stored in VirtualKeyboard, this is required by the InputMethodContext.
337 * @return current position of cursor
339 unsigned int GetCursorPosition() const;
342 * @brief Method to store the string required by the InputMethodContext, this is used to provide predictive word suggestions.
344 * @param[in] text The text string surrounding the current cursor point.
346 void SetSurroundingText( const std::string& text );
349 * @brief Gets current text string set within the InputMethodContext manager, this is used to offer predictive suggestions.
351 * @return current position of cursor
353 const std::string& GetSurroundingText() const;
356 * @brief Notifies InputMethodContext that text input is set to multi line or not
358 * @param[in] multiLine True if multiline text input is used
360 void NotifyTextInputMultiLine( bool multiLine );
363 * @brief Returns text direction of the keyboard's current input language.
364 * @return The direction of the text.
366 TextDirection GetTextDirection();
369 * @brief Provides size and position of keyboard.
371 * Position is relative to whether keyboard is visible or not.
372 * If keyboard is not visible then position will be off the screen.
373 * If keyboard is not being shown when this method is called the keyboard is partially setup (IMFContext) to get
374 * the values then taken down. So ideally GetInputMethodArea() should be called after Show().
375 * @return rect which is keyboard panel x, y, width, height
377 Dali::Rect<int> GetInputMethodArea();
380 * @brief Set one or more of the Input Method options
381 * @param[in] options The options to be applied
383 void ApplyOptions( const InputMethodOptions& options );
386 * @brief Sets up the input-panel specific data.
387 * @param[in] data The specific data to be set to the input panel
389 void SetInputPanelData( const std::string& data );
392 * @brief Gets the specific data of the current active input panel.
394 * Input Panel Data is not always the data which is set by SetInputPanelData().
395 * Data can be changed internally in the input panel.
396 * It is just used to get a specific data from the input panel to an application.
397 * @param[in] data The specific data to be got from the input panel
399 void GetInputPanelData( std::string& data );
402 * @brief Gets the state of the current active input panel.
403 * @return The state of the input panel.
405 State GetInputPanelState();
408 * @brief Sets the return key on the input panel to be visible or invisible.
410 * The default is true.
411 * @param[in] visible True if the return key is visible(enabled), false otherwise.
413 void SetReturnKeyState( bool visible );
416 * @brief Enable to show the input panel automatically when focused.
417 * @param[in] enabled If true, the input panel will be shown when focused
419 void AutoEnableInputPanel( bool enabled );
422 * @brief Shows the input panel.
424 void ShowInputPanel();
427 * @brief Hides the input panel.
429 void HideInputPanel();
432 * @brief Gets the keyboard type.
434 * The default keyboard type is SOFTWARE_KEYBOARD.
435 * @return The keyboard type
437 KeyboardType GetKeyboardType();
440 * @brief Gets the current language locale of the input panel.
442 * ex) en_US, en_GB, en_PH, fr_FR, ...
443 * @return The current language locale of the input panel
445 std::string GetInputPanelLocale();
448 * @brief Sets the allowed MIME Types to deliver to the input panel.
450 * ex) std::string mimeTypes = "text/plain,image/png,image/gif,application/pdf";
452 * You can receive a media content URI and its MIME type from ContentReceivedSignal(). @see ContentReceivedSignal
453 * @param[in] mimeTypes The allowed MIME types
455 void SetContentMIMETypes( const std::string& mimeTypes );
458 * @brief Process event key down or up, whether filter a key to isf.
460 * @param[in] keyEvent The event key to be handled.
461 * @return Whether the event key is handled.
463 bool FilterEventKey( const Dali::KeyEvent& keyEvent );
466 * @brief Sets whether the IM context should allow to use the text prediction.
468 * @param[in] prediction Whether to allow text prediction or not.
470 void AllowTextPrediction( bool prediction );
473 * @brief Gets whether the IM context allow to use the text prediction.
475 * @return Whether the IM allow text prediction or not.
477 bool IsTextPredictionAllowed() const;
480 * @brief Sets the language of the input panel.
482 * This method can be used when you want to show the English keyboard.
483 * @param[in] language The language to be set to the input panel
485 void SetInputPanelLanguage( InputPanelLanguage language );
488 * @brief Gets the language of the input panel.
490 * @return The language of the input panel
492 InputPanelLanguage GetInputPanelLanguage() const;
495 * @brief Sets the x,y coordinates of the input panel.
497 * @param[in] x The top-left x coordinate of the input panel
498 * @param[in] y The top-left y coordinate of the input panel
500 void SetInputPanelPosition( unsigned int x, unsigned int y );
503 * @brief Gets the preedit attributes data.
505 * @param[out] attrs The preedit attributes data.
507 void GetPreeditStyle( PreEditAttributeDataContainer& attrs ) const;
514 * @brief This is emitted when the virtual keyboard is connected to or the hardware keyboard is activated.
516 * @return The InputMethodContext Activated signal.
518 ActivatedSignalType& ActivatedSignal();
521 * @brief This is emitted when the InputMethodContext manager receives an event from the InputMethodContext.
523 * @return The Event signal containing the event data.
525 KeyboardEventSignalType& EventReceivedSignal();
528 * @brief Connect to this signal to be notified when the virtual keyboard is shown or hidden.
530 * A callback of the following type may be connected:
532 * void YourCallbackName(bool keyboardShown);
534 * If the parameter keyboardShown is true, then the keyboard has just shown, if it is false, then it
535 * has just been hidden.
536 * @return The signal to connect to.
538 StatusSignalType& StatusChangedSignal();
541 * @brief Connect to this signal to be notified when the virtual keyboard is resized.
543 * A callback of the following type may be connected:
545 * void YourCallbackName( int resolvedResize );
547 * The parameter sends the resolved resize defined by the InputMethodContext.
549 * User can get changed size by using GetInputMethodArea() in the callback
550 * @return The signal to connect to.
552 KeyboardResizedSignalType& ResizedSignal();
555 * @brief Connect to this signal to be notified when the virtual keyboard's language is changed.
557 * A callback of the following type may be connected:
559 * void YourCallbackName( int resolvedLanguage );
561 * The parameter sends the resolved language defined by the InputMethodContext.
563 * User can get the text direction of the language by calling GetTextDirection() in the callback.
564 * @return The signal to connect to.
566 LanguageChangedSignalType& LanguageChangedSignal();
569 * @brief Connect to this signal to be notified when the keyboard type is changed.
571 * A callback of the following type may be connected:
573 * void YourCallbackName( KeyboardType keyboard );
576 * @return The signal to connect to.
578 KeyboardTypeSignalType& KeyboardTypeChangedSignal();
581 * @brief Connect to this signal to be notified when the content, such as images, of input method is received.
583 * A callback of the following type may be connected:
585 * void YourCallbackName( const std::string& contentUri, const std::string& description, const std::string& contentMIMEType );
588 * @return The signal to connect to.
590 ContentReceivedSignalType& ContentReceivedSignal();
595 * @brief This constructor is used by InputMethodContext::New().
597 * @param[in] inputMethodContext A pointer to the InputMethodContext.
599 explicit DALI_INTERNAL InputMethodContext( Internal::Adaptor::InputMethodContext* inputMethodContext );
607 #endif // DALI_INPUT_METHOD_CONTEXT_H