1209d61ef8c4815bb852e371196157eb3c2bdc2b
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / input-method-context.h
1 #ifndef DALI_INPUT_METHOD_CONTEXT_H
2 #define DALI_INPUT_METHOD_CONTEXT_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/events/key-event.h>
23 #include <dali/public-api/common/dali-vector.h>
24
25 // EXTERNAL INCLUDES
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>
29
30 namespace Dali
31 {
32
33 namespace Internal DALI_INTERNAL
34 {
35 namespace Adaptor
36 {
37 class InputMethodContext;
38 }
39 }
40
41 class Actor;
42
43 /**
44  * @brief The InputMethodContext class
45  *
46  * Specifically manages the ecore input method framework which enables the virtual or hardware keyboards.
47  */
48 class DALI_ADAPTOR_API InputMethodContext : public BaseHandle
49 {
50 public:
51
52   /**
53   * @brief The direction of text.
54   */
55   enum TextDirection
56   {
57     LeftToRight,
58     RightToLeft,
59   };
60
61   /**
62    * @brief Events that are generated by the InputMethodContext.
63    */
64   enum EventType
65   {
66     VOID,                ///< No event
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
72   };
73
74   /**
75    * @brief Enumeration for state of the input panel.
76    */
77   enum State
78   {
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
83   };
84
85   /**
86    * @brief Enumeration for the type of Keyboard.
87    */
88   enum KeyboardType
89   {
90     SOFTWARE_KEYBOARD,  ///< Software keyboard (Virtual keyboard) is default
91     HARDWARE_KEYBOARD   ///< Hardware keyboard
92   };
93
94   /**
95    * @brief Enumeration for the language mode of the input panel.
96    */
97   enum class InputPanelLanguage
98   {
99     AUTOMATIC,    ///< IME Language automatically set depending on the system display
100     ALPHABET      ///< Latin alphabet at all times
101   };
102
103   /**
104    * @brief Enumeration for the preedit style types.
105    */
106   enum class PreeditStyle
107   {
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
116   };
117
118   /**
119    * @brief This structure is for the preedit style types and indices.
120    */
121   struct PreeditAttributeData
122   {
123     PreeditStyle preeditType; /// The preedit style type
124     unsigned int startIndex;  /// The start index of preedit
125     unsigned int endIndex;    /// The end index of preedit
126   };
127
128   /**
129    * @brief This structure is used to pass on data from the InputMethodContext regarding predictive text.
130    */
131   struct EventData
132   {
133     /**
134      * @brief Default Constructor.
135      */
136     EventData()
137     : predictiveString(),
138       eventName( VOID ),
139       cursorOffset( 0 ),
140       numberOfChars ( 0 )
141     {
142     };
143
144     /**
145      * @brief Constructor
146      *
147      * @param[in] aEventName The name of the event from the InputMethodContext.
148      * @param[in] aPredictiveString The pre-edit or commit string.
149      * @param[in] aCursorOffset Start position from the current cursor position to start deleting characters.
150      * @param[in] aNumberOfChars The number of characters to delete from the cursorOffset.
151      */
152     EventData( EventType aEventName, const std::string& aPredictiveString, int aCursorOffset, int aNumberOfChars )
153     : predictiveString( aPredictiveString ),
154       eventName( aEventName ),
155       cursorOffset( aCursorOffset ),
156       numberOfChars( aNumberOfChars )
157     {
158     }
159
160     // Data
161     std::string predictiveString; ///< The pre-edit or commit string.
162     EventType eventName;           ///< The name of the event from the InputMethodContext.
163     int cursorOffset;             ///< Start position from the current cursor position to start deleting characters.
164     int numberOfChars;            ///< number of characters to delete from the cursorOffset.
165   };
166
167   /**
168    * @brief Data required by InputMethodContext from the callback
169    */
170   struct CallbackData
171   {
172     /**
173      * @brief Constructor
174      */
175     CallbackData()
176     : currentText(),
177       cursorPosition( 0 ),
178       update( false ),
179       preeditResetRequired( false )
180     {
181     }
182
183     /**
184      * @brief Constructor
185      * @param[in] aUpdate True if cursor position needs to be updated
186      * @param[in] aCursorPosition new position of cursor
187      * @param[in] aCurrentText current text string
188      * @param[in] aPreeditResetRequired flag if preedit reset is required.
189      */
190     CallbackData( bool aUpdate, int aCursorPosition, const std::string& aCurrentText, bool aPreeditResetRequired )
191     : currentText( aCurrentText ),
192       cursorPosition( aCursorPosition ),
193       update( aUpdate ),
194       preeditResetRequired( aPreeditResetRequired )
195     {
196     }
197
198     std::string currentText;      ///< current text string
199     int cursorPosition;           ///< new position of cursor
200     bool update               :1; ///< if cursor position needs to be updated
201     bool preeditResetRequired :1; ///< flag if preedit reset is required.
202   };
203
204   typedef Signal< void (InputMethodContext&) > ActivatedSignalType; ///< Keyboard actived signal
205   typedef Signal< CallbackData ( InputMethodContext&, const EventData& ) > KeyboardEventSignalType; ///< keyboard events
206   typedef Signal< void () > VoidSignalType;
207   typedef Signal< void ( bool ) > StatusSignalType;
208   typedef Signal< void ( KeyboardType ) > KeyboardTypeSignalType; ///< keyboard type
209   typedef Signal< void ( int ) > KeyboardResizedSignalType;  ///< Keyboard resized signal
210   typedef Signal< void ( int ) > LanguageChangedSignalType;  ///< Language changed signal
211   typedef Signal< void ( const std::string&, const std::string&, const std::string& ) > ContentReceivedSignalType; ///< Content received signal
212
213 public:
214
215   /**
216    * @brief Retrieve a handle to the instance of InputMethodContext.
217    * @return A handle to the InputMethodContext.
218    * @brief Constructor.
219    */
220   InputMethodContext();
221
222   /**
223    * @brief Destructor
224    *
225    * This is non-virtual since derived Handle types must not contain data or virtual methods.
226    */
227   ~InputMethodContext();
228
229   /**
230    * @brief Create a new instance of an InputMethodContext.
231    */
232   static InputMethodContext New();
233
234   /**
235    * @brief Create a new instance of an InputMethodContext.
236    *
237    * @param[in] actor The actor that uses the new InputMethodContext instance.
238    */
239   static InputMethodContext New( Actor actor );
240
241   /**
242    * @brief Copy constructor.
243    *
244    * @param[in] inputMethodContext InputMethodContext to copy. The copied inputMethodContext will point at the same implementation.
245    */
246  InputMethodContext( const InputMethodContext& inputMethodContext );
247
248   /**
249    * @brief Assignment operator.
250    *
251    * @param[in] inputMethodContext The InputMethodContext to assign from.
252    * @return The updated InputMethodContext.
253    */
254  InputMethodContext& operator=( const InputMethodContext& inputMethodContext );
255
256   /**
257    * @brief Downcast a handle to InputMethodContext handle.
258    *
259    * If handle points to an InputMethodContext the downcast produces valid
260    * handle. If not the returned handle is left uninitialized.
261    *
262    * @param[in] handle Handle to an object.
263    * @return Handle to an InputMethodContext or an uninitialized handle.
264    */
265   static InputMethodContext DownCast( BaseHandle handle );
266
267 public:
268
269   /**
270    * @brief Finalize the InputMethodContext.
271    *
272    * It means that the context will be deleted.
273    */
274   void Finalize();
275
276   /**
277    * @brief Activate the InputMethodContext.
278    *
279    * It means that the text editing is started at somewhere.
280    * If the H/W keyboard isn't connected then it will show the virtual keyboard.
281    */
282   void Activate();
283
284   /**
285    * @brief Deactivate the InputMethodContext.
286    *
287    * It means that the text editing is finished at somewhere.
288    */
289   void Deactivate();
290
291   /**
292    * @brief Get the restoration status, which controls if the keyboard is restored after the focus lost then regained.
293    *
294    * If true then keyboard will be restored (activated) after focus is regained.
295    * @return restoration status.
296    */
297   bool RestoreAfterFocusLost() const;
298
299   /**
300    * @brief Set status whether the InputMethodContext has to restore the keyboard after losing focus.
301    *
302    * @param[in] toggle True means that keyboard should be restored after focus lost and regained.
303    */
304   void SetRestoreAfterFocusLost( bool toggle );
305
306   /**
307    * @brief Send message reset the pred-edit state / InputMethodContext module.
308    *
309    * Used to interupt pre-edit state maybe due to a touch input.
310    */
311   void Reset();
312
313   /**
314    * @brief Notifies InputMethodContext that the cursor position has changed, required for features like auto-capitalisation.
315    */
316   void NotifyCursorPosition();
317
318   /**
319    * @brief Sets cursor position stored in VirtualKeyboard, this is required by the InputMethodContext.
320    *
321    * @param[in] cursorPosition position of cursor
322    */
323   void SetCursorPosition( unsigned int cursorPosition );
324
325   /**
326    * @brief Gets cursor position stored in VirtualKeyboard, this is required by the InputMethodContext.
327    *
328    * @return current position of cursor
329    */
330   unsigned int GetCursorPosition() const;
331
332   /**
333    * @brief Method to store the string required by the InputMethodContext, this is used to provide predictive word suggestions.
334    *
335    * @param[in] text The text string surrounding the current cursor point.
336    */
337   void SetSurroundingText( const std::string& text );
338
339   /**
340    * @brief Gets current text string set within the InputMethodContext manager, this is used to offer predictive suggestions.
341    *
342    * @return current position of cursor
343    */
344   const std::string& GetSurroundingText() const;
345
346   /**
347  * @brief Notifies InputMethodContext that text input is set to multi line or not
348  *
349  * @param[in] multiLine True if multiline text input is used
350  */
351   void NotifyTextInputMultiLine( bool multiLine );
352
353   /**
354    * @brief Returns text direction of the keyboard's current input language.
355    * @return The direction of the text.
356    */
357   TextDirection GetTextDirection();
358
359   /**
360    * @brief Provides size and position of keyboard.
361    *
362    * Position is relative to whether keyboard is visible or not.
363    * If keyboard is not visible then position will be off the screen.
364    * If keyboard is not being shown when this method is called the keyboard is partially setup (IMFContext) to get
365    * the values then taken down.  So ideally GetInputMethodArea() should be called after Show().
366    * @return rect which is keyboard panel x, y, width, height
367    */
368   Dali::Rect<int> GetInputMethodArea();
369
370   /**
371    * @brief Set one or more of the Input Method options
372    * @param[in] options The options to be applied
373    */
374   void ApplyOptions( const InputMethodOptions& options );
375
376   /**
377    * @brief Sets up the input-panel specific data.
378    * @param[in] data The specific data to be set to the input panel
379    */
380   void SetInputPanelData( const std::string& data );
381
382   /**
383    * @brief Gets the specific data of the current active input panel.
384    *
385    * Input Panel Data is not always the data which is set by SetInputPanelData().
386    * Data can be changed internally in the input panel.
387    * It is just used to get a specific data from the input panel to an application.
388    * @param[in] data The specific data to be got from the input panel
389    */
390   void GetInputPanelData( std::string& data );
391
392   /**
393    * @brief Gets the state of the current active input panel.
394    * @return The state of the input panel.
395    */
396   State GetInputPanelState();
397
398   /**
399    * @brief Sets the return key on the input panel to be visible or invisible.
400    *
401    * The default is true.
402    * @param[in] visible True if the return key is visible(enabled), false otherwise.
403    */
404   void SetReturnKeyState( bool visible );
405
406   /**
407    * @brief Enable to show the input panel automatically when focused.
408    * @param[in] enabled If true, the input panel will be shown when focused
409    */
410   void AutoEnableInputPanel( bool enabled );
411
412   /**
413    * @brief Shows the input panel.
414    */
415   void ShowInputPanel();
416
417   /**
418    * @brief Hides the input panel.
419    */
420   void HideInputPanel();
421
422   /**
423    * @brief Gets the keyboard type.
424    *
425    * The default keyboard type is SOFTWARE_KEYBOARD.
426    * @return The keyboard type
427    */
428   KeyboardType GetKeyboardType();
429
430   /**
431    * @brief Gets the current language locale of the input panel.
432    *
433    * ex) en_US, en_GB, en_PH, fr_FR, ...
434    * @return The current language locale of the input panel
435    */
436   std::string GetInputPanelLocale();
437
438   /**
439    * @brief Sets the allowed MIME Types to deliver to the input panel.
440    *
441    * ex) std::string mimeTypes = "text/plain,image/png,image/gif,application/pdf";
442    *
443    * You can receive a media content URI and its MIME type from ContentReceivedSignal(). @see ContentReceivedSignal
444    * @param[in] mimeTypes The allowed MIME types
445    */
446   void SetContentMIMETypes( const std::string& mimeTypes );
447
448   /**
449    * @brief Process event key down or up, whether filter a key to isf.
450    *
451    * @param[in] keyEvent The event key to be handled.
452    * @return Whether the event key is handled.
453    */
454   bool FilterEventKey( const Dali::KeyEvent& keyEvent );
455
456   /**
457    * @brief Sets whether the IM context should allow to use the text prediction.
458    *
459    * @param[in] prediction Whether to allow text prediction or not.
460    */
461   void AllowTextPrediction( bool prediction );
462
463   /**
464    * @brief Gets whether the IM context allow to use the text prediction.
465    *
466    * @return Whether the IM allow text prediction or not.
467    */
468   bool IsTextPredictionAllowed() const;
469
470   /**
471    * @brief Sets the language of the input panel.
472    *
473    * This method can be used when you want to show the English keyboard.
474    * @param[in] language The language to be set to the input panel
475    */
476   void SetInputPanelLanguage( InputPanelLanguage language );
477
478   /**
479    * @brief Gets the language of the input panel.
480    *
481    * @return The language of the input panel
482    */
483   InputPanelLanguage GetInputPanelLanguage() const;
484
485   /**
486    * @brief Sets the x,y coordinates of the input panel.
487    *
488    * @param[in] x The top-left x coordinate of the input panel
489    * @param[in] y The top-left y coordinate of the input panel
490    */
491   void SetInputPanelPosition( unsigned int x, unsigned int y );
492
493   /**
494    * @brief Gets the preedit attrs data.
495    *
496    * @param[out] attrs The preedit attrs data.
497    */
498   void GetPreeditStyle( Vector<PreeditAttributeData>& attrs ) const;
499
500 public:
501
502   // Signals
503
504   /**
505    * @brief This is emitted when the virtual keyboard is connected to or the hardware keyboard is activated.
506    *
507    * @return The InputMethodContext Activated signal.
508    */
509   ActivatedSignalType& ActivatedSignal();
510
511   /**
512    * @brief This is emitted when the InputMethodContext manager receives an event from the InputMethodContext.
513    *
514    * @return The Event signal containing the event data.
515    */
516   KeyboardEventSignalType& EventReceivedSignal();
517
518   /**
519    * @brief Connect to this signal to be notified when the virtual keyboard is shown or hidden.
520    *
521    * A callback of the following type may be connected:
522    * @code
523    *   void YourCallbackName(bool keyboardShown);
524    * @endcode
525    * If the parameter keyboardShown is true, then the keyboard has just shown, if it is false, then it
526    * has just been hidden.
527    * @return The signal to connect to.
528    */
529   StatusSignalType& StatusChangedSignal();
530
531   /**
532    * @brief Connect to this signal to be notified when the virtual keyboard is resized.
533    *
534    * A callback of the following type may be connected:
535    * @code
536    *   void YourCallbackName( int resolvedResize );
537    * @endcode
538    * The parameter sends the resolved resize defined by the InputMethodContext.
539    *
540    * User can get changed size by using GetInputMethodArea() in the callback
541    * @return The signal to connect to.
542    */
543   KeyboardResizedSignalType& ResizedSignal();
544
545   /**
546    * @brief Connect to this signal to be notified when the virtual keyboard's language is changed.
547    *
548    * A callback of the following type may be connected:
549    * @code
550    *   void YourCallbackName( int resolvedLanguage );
551    * @endcode
552    * The parameter sends the resolved language defined by the InputMethodContext.
553    *
554    * User can get the text direction of the language by calling GetTextDirection() in the callback.
555    * @return The signal to connect to.
556    */
557   LanguageChangedSignalType& LanguageChangedSignal();
558
559   /**
560    * @brief Connect to this signal to be notified when the keyboard type is changed.
561    *
562    * A callback of the following type may be connected:
563    * @code
564    *   void YourCallbackName( KeyboardType keyboard );
565    * @endcode
566    *
567    * @return The signal to connect to.
568    */
569   KeyboardTypeSignalType& KeyboardTypeChangedSignal();
570
571   /**
572    * @brief Connect to this signal to be notified when the content, such as images, of input method is received.
573    *
574    * A callback of the following type may be connected:
575    * @code
576    *   void YourCallbackName( const std::string& contentUri, const std::string& description, const std::string& contentMIMEType );
577    * @endcode
578    *
579    * @return The signal to connect to.
580    */
581   ContentReceivedSignalType& ContentReceivedSignal();
582
583 public:
584
585   /**
586    * @brief This constructor is used by InputMethodContext::New().
587    *
588    * @param[in] inputMethodContext A pointer to the InputMethodContext.
589    */
590   explicit DALI_INTERNAL InputMethodContext( Internal::Adaptor::InputMethodContext* inputMethodContext );
591
592 };
593
594
595
596 } // namespace Dali
597
598 #endif // DALI_INPUT_METHOD_CONTEXT_H