d53e02a3a029193c3beed26563f94f1b61fe9d9e
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / toolkit-input-method-context.h
1 #ifndef DALI_TOOLKIT_TOOLKIT_INPUT_METHOD_CONTEXT_H
2 #define DALI_TOOLKIT_TOOLKIT_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 #define DALI_INPUT_METHOD_CONTEXT_H
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/object/base-handle.h>
25 #include <dali/public-api/signals/dali-signal.h>
26 #include <dali/devel-api/adaptor-framework/input-method-options.h>
27 #include <dali/public-api/events/key-event.h>
28
29 namespace Dali DALI_IMPORT_API
30 {
31
32 namespace Internal DALI_INTERNAL
33 {
34 namespace Adaptor
35 {
36 class InputMethodContext;
37 }
38 }
39
40 /**
41  * @brief The InputMethodContext class
42  *
43  * Specifically manages the ecore input method framework which enables the virtual or hardware keyboards.
44  */
45 class InputMethodContext : public BaseHandle
46 {
47 public:
48
49   /**
50   * @brief The direction of text.
51   */
52   enum TextDirection
53   {
54     LeftToRight,
55     RightToLeft,
56   };
57
58   /**
59    * @brief Events that are generated by the InputMethodContext.
60    */
61   enum EventType
62   {
63     VOID,                ///< No event
64     PRE_EDIT,             ///< Pre-Edit changed
65     COMMIT,              ///< Commit recieved
66     DELETE_SURROUNDING,   ///< Event to delete a range of characters from the string
67     GET_SURROUNDING,      ///< Event to query string and cursor position
68     PRIVATE_COMMAND       ///< Private command sent from the input panel
69   };
70
71   /**
72    * @brief Enumeration for state of the input panel.
73    */
74   enum State
75   {
76     DEFAULT = 0,   ///< Unknown state
77     SHOW,          ///< Input panel is shown
78     HIDE,          ///< Input panel is hidden
79     WILL_SHOW      ///< Input panel in process of being shown
80   };
81
82   /**
83    * @brief Enumeration for the type of Keyboard.
84    */
85   enum KeyboardType
86   {
87     SOFTWARE_KEYBOARD,  ///< Software keyboard (Virtual keyboard) is default
88     HARDWARE_KEYBOARD   ///< Hardware keyboard
89   };
90
91   /**
92    * @brief Enumeration for the language mode of the input panel.
93    */
94   enum class InputPanelLanguage
95   {
96     AUTOMATIC,    ///< IME Language automatically set depending on the system display
97     ALPHABET      ///< Latin alphabet at all times
98   };
99
100   /**
101    * @brief Enumeration for the preedit style types.
102    */
103   enum class PreeditStyle
104   {
105     NONE,                    ///< None style
106     UNDERLINE,               ///< Underline substring style
107     REVERSE,                 ///< Reverse substring style
108     HIGHLIGHT,               ///< Highlight substring style
109     CUSTOM_PLATFORM_STYLE_1, ///< Custom style for platform
110     CUSTOM_PLATFORM_STYLE_2, ///< Custom style for platform
111     CUSTOM_PLATFORM_STYLE_3, ///< Custom style for platform
112     CUSTOM_PLATFORM_STYLE_4  ///< Custom style for platform
113   };
114
115   /**
116    * @brief This structure is for the preedit style types and indices.
117    */
118   struct PreeditAttributeData
119   {
120     PreeditAttributeData()
121     : preeditType( PreeditStyle::NONE ),
122       startIndex( 0 ),
123       endIndex( 0 )
124     {
125     }
126
127     PreeditStyle preeditType;  /// The preedit style type
128     unsigned int startIndex;   /// The start index of preedit
129     unsigned int endIndex;     /// The end index of preedit
130   };
131
132   /**
133    * @brief This structure is used to pass on data from the InputMethodContext regarding predictive text.
134    */
135   struct EventData
136   {
137     /**
138      * @brief Default Constructor.
139      */
140     EventData()
141     : predictiveString(),
142       eventName( VOID ),
143       cursorOffset( 0 ),
144       numberOfChars ( 0 )
145     {
146     };
147
148     /**
149      * @brief Constructor
150      *
151      * @param[in] aEventName The name of the event from the input method context.
152      * @param[in] aPredictiveString The pre-edit or commit string.
153      * @param[in] aCursorOffset Start position from the current cursor position to start deleting characters.
154      * @param[in] aNumberOfChars The number of characters to delete from the cursorOffset.
155      */
156     EventData( EventType aEventName, const std::string& aPredictiveString, int aCursorOffset, int aNumberOfChars )
157     : predictiveString( aPredictiveString ),
158       eventName( aEventName ),
159       cursorOffset( aCursorOffset ),
160       numberOfChars( aNumberOfChars )
161     {
162     }
163
164     // Data
165     std::string predictiveString; ///< The pre-edit or commit string.
166     EventType eventName;           ///< The name of the event from the input method context.
167     int cursorOffset;             ///< Start position from the current cursor position to start deleting characters.
168     int numberOfChars;            ///< number of characters to delete from the cursorOffset.
169   };
170
171   /**
172    * @brief Data required by input method context from the callback
173    */
174   struct CallbackData
175   {
176     /**
177      * @brief Constructor
178      */
179     CallbackData()
180     : currentText(),
181       cursorPosition( 0 ),
182       update( false ),
183       preeditResetRequired( false )
184     {
185     }
186
187     /**
188      * @brief Constructor
189      * @param[in] aUpdate True if cursor position needs to be updated
190      * @param[in] aCursorPosition new position of cursor
191      * @param[in] aCurrentText current text string
192      * @param[in] aPreeditResetRequired flag if preedit reset is required.
193      */
194     CallbackData( bool aUpdate, int aCursorPosition, const std::string& aCurrentText, bool aPreeditResetRequired )
195     : currentText( aCurrentText ),
196       cursorPosition( aCursorPosition ),
197       update( aUpdate ),
198       preeditResetRequired( aPreeditResetRequired )
199     {
200     }
201
202     std::string currentText;      ///< current text string
203     int cursorPosition;           ///< new position of cursor
204     bool update               :1; ///< if cursor position needs to be updated
205     bool preeditResetRequired :1; ///< flag if preedit reset is required.
206   };
207
208   typedef Signal< void (InputMethodContext&) > ActivatedSignalType; ///< Keyboard actived signal
209   typedef Signal< CallbackData ( InputMethodContext&, const EventData& ) > KeyboardEventSignalType; ///< keyboard events
210   typedef Signal< void () > VoidSignalType;
211   typedef Signal< void (bool) > StatusSignalType;
212
213   using PreEditAttributeDataContainer = Vector< Dali::InputMethodContext::PreeditAttributeData >;
214
215 public:
216
217   /**
218    * @brief Create a handle to the instance of InputMethodContext.
219    * @return A handle to the InputMethodContext.
220    */
221   static InputMethodContext New();
222
223   /**
224    * @brief Create a handle to the instance of InputMethodContext.
225    *
226    * @param[in] actor The actor that uses the new InputMethodContext instance.
227    * @return A handle to the InputMethodContext.
228    */
229   static InputMethodContext New( Actor actor );
230
231   /**
232    * @brief Finalize the InputMethodContext.
233    *
234    * It means that the context will be deleted.
235    */
236   void Finalize();
237
238   /**
239    * @brief Activate the input method context.
240    *
241    * It means that the text editing is started at somewhere.
242    * If the H/W keyboard isn't connected then it will show the virtual keyboard.
243    */
244   void Activate();
245
246   /**
247    * @brief Deactivate the input method context.
248    *
249    * It means that the text editing is finished at somewhere.
250    */
251   void Deactivate();
252
253   /**
254    * @brief Get the restoration status, which controls if the keyboard is restored after the focus lost then regained.
255    *
256    * If true then keyboard will be restored (activated) after focus is regained.
257    * @return restoration status.
258    */
259   bool RestoreAfterFocusLost() const;
260
261   /**
262    * @brief Set status whether the input method context has to restore the keyboard after losing focus.
263    *
264    * @param[in] toggle True means that keyboard should be restored after focus lost and regained.
265    */
266   void SetRestoreAfterFocusLost( bool toggle );
267
268   /**
269    * @brief Send message reset the pred-edit state / input method context module.
270    *
271    * Used to interupt pre-edit state maybe due to a touch input.
272    */
273   void Reset();
274
275   /**
276    * @brief Notifies ImfContext that the cursor position has changed, required for features like auto-capitalisation.
277    */
278   void NotifyCursorPosition();
279
280   /**
281    * @brief Sets cursor position stored in VirtualKeyboard, this is required by the ImfContext.
282    *
283    * @param[in] cursorPosition position of cursor
284    */
285   void SetCursorPosition( unsigned int cursorPosition );
286
287   /**
288    * @brief Gets cursor position stored in VirtualKeyboard, this is required by the ImfContext.
289    *
290    * @return current position of cursor
291    */
292   unsigned int GetCursorPosition() const;
293
294   /**
295    * @brief Method to store the string required by the input method context, this is used to provide predictive word suggestions.
296    *
297    * @param[in] text The text string surrounding the current cursor point.
298    */
299   void SetSurroundingText( const std::string& text );
300
301   /**
302    * @brief Gets current text string set within the input method context, this is used to offer predictive suggestions.
303    *
304    * @return current position of cursor
305    */
306   const std::string& GetSurroundingText() const;
307
308   /**
309   * @brief Notifies ImfContext that text input is set to multi line or not
310   */
311   void NotifyTextInputMultiLine( bool multiLine );
312
313   /**
314    * @brief Set one or more of the Input Method options
315    * @param[in] options The options to be applied
316    */
317   void ApplyOptions( const InputMethodOptions& options );
318
319   /**
320    * @brief Process event key down or up, whether filter a key to isf.
321    *
322    * @param[in] keyEvent The event key to be handled.
323    * @return Whether the event key is handled.
324    */
325   bool FilterEventKey( const Dali::KeyEvent& keyEvent );
326
327   /**
328    * @brief Sets the preedit type.
329    *
330    * @param[in] type The preedit style type
331    */
332   void SetPreeditStyle( PreeditStyle type );
333
334   /**
335    * @brief Gets the preedit attributes data.
336    *
337    * @param[out] attrs The preedit attributes data.
338    */
339   void GetPreeditStyle( Dali::InputMethodContext::PreEditAttributeDataContainer& attrs ) const;
340
341 public:
342
343   // Signals
344
345   /**
346    * @brief This is emitted when the virtual keyboard is connected to or the hardware keyboard is activated.
347    *
348    * @return The input method context Activated signal.
349    */
350   ActivatedSignalType& ActivatedSignal();
351
352   /**
353    * @brief This is emitted when the input method context receives an event.
354    *
355    * @return The Event signal containing the event data.
356    */
357   KeyboardEventSignalType& EventReceivedSignal();
358
359   /**
360    * @brief Connect to this signal to be notified when the virtual keyboard is shown or hidden.
361    *
362    * @return The signal connect to status changed event.
363    */
364   StatusSignalType& StatusChangedSignal();
365
366   /**
367    * @brief Connect to this signal to be notified when the virtual keyboard is resized.
368    *
369    * @return The signal to connect to resized event.
370    */
371   VoidSignalType& ResizedSignal();
372
373   /**
374    * @brief Connect to this signal to be notified when the virtual keyboard's language is changed.
375    *
376    * @return The signal to connect to language changed event.
377    */
378   VoidSignalType& LanguageChangedSignal();
379
380   // Construction & Destruction
381
382   /**
383    * @brief Constructor.
384    */
385   InputMethodContext();
386
387   /**
388    * @brief Destructor
389    *
390    * This is non-virtual since derived Handle types must not contain data or virtual methods.
391    */
392   ~InputMethodContext();
393
394   /**
395    * @brief This constructor is used by InputMethodContext::Get().
396    *
397    * @param[in] inputMethodContext A pointer to the input method context.
398    */
399   explicit InputMethodContext( Internal::Adaptor::InputMethodContext* inputMethodContext );
400 };
401
402 } // namespace Dali
403
404 #endif // DALI_TOOLKIT_TOOLKIT_INPUT_METHOD_CONTEXT_H