[Tizen] Add codes for Dali Windows Backend
[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) 2018 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
24 // EXTERNAL INCLUDES
25 #include <dali/public-api/object/base-handle.h>
26 #include <dali/public-api/signals/dali-signal.h>
27 #include <dali/devel-api/adaptor-framework/input-method-options.h>
28
29 namespace Dali
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 DALI_ADAPTOR_API 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 This structure is used to pass on data from the InputMethodContext regarding predictive text.
93    */
94   struct EventData
95   {
96     /**
97      * @brief Default Constructor.
98      */
99     EventData()
100     : predictiveString(),
101       eventName( VOID ),
102       cursorOffset( 0 ),
103       numberOfChars ( 0 )
104     {
105     };
106
107     /**
108      * @brief Constructor
109      *
110      * @param[in] aEventName The name of the event from the InputMethodContext.
111      * @param[in] aPredictiveString The pre-edit or commit string.
112      * @param[in] aCursorOffset Start position from the current cursor position to start deleting characters.
113      * @param[in] aNumberOfChars The number of characters to delete from the cursorOffset.
114      */
115     EventData( EventType aEventName, const std::string& aPredictiveString, int aCursorOffset, int aNumberOfChars )
116     : predictiveString( aPredictiveString ),
117       eventName( aEventName ),
118       cursorOffset( aCursorOffset ),
119       numberOfChars( aNumberOfChars )
120     {
121     }
122
123     // Data
124     std::string predictiveString; ///< The pre-edit or commit string.
125     EventType eventName;           ///< The name of the event from the InputMethodContext.
126     int cursorOffset;             ///< Start position from the current cursor position to start deleting characters.
127     int numberOfChars;            ///< number of characters to delete from the cursorOffset.
128   };
129
130   /**
131    * @brief Data required by InputMethodContext from the callback
132    */
133   struct CallbackData
134   {
135     /**
136      * @brief Constructor
137      */
138     CallbackData()
139     : currentText(),
140       cursorPosition( 0 ),
141       update( false ),
142       preeditResetRequired( false )
143     {
144     }
145
146     /**
147      * @brief Constructor
148      * @param[in] aUpdate True if cursor position needs to be updated
149      * @param[in] aCursorPosition new position of cursor
150      * @param[in] aCurrentText current text string
151      * @param[in] aPreeditResetRequired flag if preedit reset is required.
152      */
153     CallbackData( bool aUpdate, int aCursorPosition, const std::string& aCurrentText, bool aPreeditResetRequired )
154     : currentText( aCurrentText ),
155       cursorPosition( aCursorPosition ),
156       update( aUpdate ),
157       preeditResetRequired( aPreeditResetRequired )
158     {
159     }
160
161     std::string currentText;      ///< current text string
162     int cursorPosition;           ///< new position of cursor
163     bool update               :1; ///< if cursor position needs to be updated
164     bool preeditResetRequired :1; ///< flag if preedit reset is required.
165   };
166
167   typedef Signal< void (InputMethodContext&) > ActivatedSignalType; ///< Keyboard actived signal
168   typedef Signal< CallbackData ( InputMethodContext&, const EventData& ) > KeyboardEventSignalType; ///< keyboard events
169   typedef Signal< void () > VoidSignalType;
170   typedef Signal< void ( bool ) > StatusSignalType;
171   typedef Signal< void ( KeyboardType ) > KeyboardTypeSignalType; ///< keyboard type
172   typedef Signal< void ( int ) > KeyboardResizedSignalType;  ///< Keyboard resized signal
173   typedef Signal< void ( int ) > LanguageChangedSignalType;  ///< Language changed signal
174
175 public:
176
177   /**
178    * @brief Retrieve a handle to the instance of InputMethodContext.
179    * @return A handle to the InputMethodContext.
180    * @brief Constructor.
181    */
182   InputMethodContext();
183
184   /**
185    * @brief Destructor
186    *
187    * This is non-virtual since derived Handle types must not contain data or virtual methods.
188    */
189   ~InputMethodContext();
190
191   /**
192    * @brief Create a new instance of an InputMethodContext.
193    */
194   static InputMethodContext New();
195
196   /**
197    * @brief Copy constructor.
198    *
199    * @param[in] inputMethodContext InputMethodContext to copy. The copied inputMethodContext will point at the same implementation.
200    */
201  InputMethodContext( const InputMethodContext& inputMethodContext );
202
203   /**
204    * @brief Assignment operator.
205    *
206    * @param[in] inputMethodContext The InputMethodContext to assign from.
207    * @return The updated InputMethodContext.
208    */
209  InputMethodContext& operator=( const InputMethodContext& inputMethodContext );
210
211   /**
212    * @brief Downcast a handle to InputMethodContext handle.
213    *
214    * If handle points to an InputMethodContext the downcast produces valid
215    * handle. If not the returned handle is left uninitialized.
216    *
217    * @param[in] handle Handle to an object.
218    * @return Handle to an InputMethodContext or an uninitialized handle.
219    */
220   static InputMethodContext DownCast( BaseHandle handle );
221
222 public:
223
224   /**
225    * @brief Finalize the InputMethodContext.
226    *
227    * It means that the context will be deleted.
228    */
229   void Finalize();
230
231   /**
232    * @brief Activate the InputMethodContext.
233    *
234    * It means that the text editing is started at somewhere.
235    * If the H/W keyboard isn't connected then it will show the virtual keyboard.
236    */
237   void Activate();
238
239   /**
240    * @brief Deactivate the InputMethodContext.
241    *
242    * It means that the text editing is finished at somewhere.
243    */
244   void Deactivate();
245
246   /**
247    * @brief Get the restoration status, which controls if the keyboard is restored after the focus lost then regained.
248    *
249    * If true then keyboard will be restored (activated) after focus is regained.
250    * @return restoration status.
251    */
252   bool RestoreAfterFocusLost() const;
253
254   /**
255    * @brief Set status whether the InputMethodContext has to restore the keyboard after losing focus.
256    *
257    * @param[in] toggle True means that keyboard should be restored after focus lost and regained.
258    */
259   void SetRestoreAfterFocusLost( bool toggle );
260
261   /**
262    * @brief Send message reset the pred-edit state / InputMethodContext module.
263    *
264    * Used to interupt pre-edit state maybe due to a touch input.
265    */
266   void Reset();
267
268   /**
269    * @brief Notifies InputMethodContext that the cursor position has changed, required for features like auto-capitalisation.
270    */
271   void NotifyCursorPosition();
272
273   /**
274    * @brief Sets cursor position stored in VirtualKeyboard, this is required by the InputMethodContext.
275    *
276    * @param[in] cursorPosition position of cursor
277    */
278   void SetCursorPosition( unsigned int cursorPosition );
279
280   /**
281    * @brief Gets cursor position stored in VirtualKeyboard, this is required by the InputMethodContext.
282    *
283    * @return current position of cursor
284    */
285   unsigned int GetCursorPosition() const;
286
287   /**
288    * @brief Method to store the string required by the InputMethodContext, this is used to provide predictive word suggestions.
289    *
290    * @param[in] text The text string surrounding the current cursor point.
291    */
292   void SetSurroundingText( const std::string& text );
293
294   /**
295    * @brief Gets current text string set within the InputMethodContext manager, this is used to offer predictive suggestions.
296    *
297    * @return current position of cursor
298    */
299   const std::string& GetSurroundingText() const;
300
301   /**
302  * @brief Notifies InputMethodContext that text input is set to multi line or not
303  *
304  * @param[in] multiLine True if multiline text input is used
305  */
306   void NotifyTextInputMultiLine( bool multiLine );
307
308   /**
309    * @brief Returns text direction of the keyboard's current input language.
310    * @return The direction of the text.
311    */
312   TextDirection GetTextDirection();
313
314   /**
315    * @brief Provides size and position of keyboard.
316    *
317    * Position is relative to whether keyboard is visible or not.
318    * If keyboard is not visible then position will be off the screen.
319    * If keyboard is not being shown when this method is called the keyboard is partially setup (IMFContext) to get
320    * the values then taken down.  So ideally GetInputMethodArea() should be called after Show().
321    * @return rect which is keyboard panel x, y, width, height
322    */
323   Dali::Rect<int> GetInputMethodArea();
324
325   /**
326    * @brief Set one or more of the Input Method options
327    * @param[in] options The options to be applied
328    */
329   void ApplyOptions( const InputMethodOptions& options );
330
331   /**
332    * @brief Sets up the input-panel specific data.
333    * @param[in] data The specific data to be set to the input panel
334    */
335   void SetInputPanelData( const std::string& data );
336
337   /**
338    * @brief Gets the specific data of the current active input panel.
339    *
340    * Input Panel Data is not always the data which is set by SetInputPanelData().
341    * Data can be changed internally in the input panel.
342    * It is just used to get a specific data from the input panel to an application.
343    * @param[in] data The specific data to be got from the input panel
344    */
345   void GetInputPanelData( std::string& data );
346
347   /**
348    * @brief Gets the state of the current active input panel.
349    * @return The state of the input panel.
350    */
351   State GetInputPanelState();
352
353   /**
354    * @brief Sets the return key on the input panel to be visible or invisible.
355    *
356    * The default is true.
357    * @param[in] visible True if the return key is visible(enabled), false otherwise.
358    */
359   void SetReturnKeyState( bool visible );
360
361   /**
362    * @brief Enable to show the input panel automatically when focused.
363    * @param[in] enabled If true, the input panel will be shown when focused
364    */
365   void AutoEnableInputPanel( bool enabled );
366
367   /**
368    * @brief Shows the input panel.
369    */
370   void ShowInputPanel();
371
372   /**
373    * @brief Hides the input panel.
374    */
375   void HideInputPanel();
376
377   /**
378    * @brief Gets the keyboard type.
379    *
380    * The default keyboard type is SOFTWARE_KEYBOARD.
381    * @return The keyboard type
382    */
383   KeyboardType GetKeyboardType();
384
385   /**
386    * @brief Gets the current language locale of the input panel.
387    *
388    * ex) en_US, en_GB, en_PH, fr_FR, ...
389    * @return The current language locale of the input panel
390    */
391   std::string GetInputPanelLocale();
392
393   /**
394    * @brief Process event key down or up, whether filter a key to isf.
395    *
396    * @param[in] keyEvent The event key to be handled.
397    * @return Whether the event key is handled.
398    */
399   bool FilterEventKey( const Dali::KeyEvent& keyEvent );
400
401   /**
402    * @brief Sets whether the IM context should allow to use the text prediction.
403    *
404    * @param[in] prediction Whether to allow text prediction or not.
405    */
406   void AllowTextPrediction( bool prediction );
407
408   /**
409    * @brief Gets whether the IM context allow to use the text prediction.
410    *
411    * @return Whether the IM allow text prediction or not.
412    */
413   bool IsTextPredictionAllowed() const;
414 public:
415
416   // Signals
417
418   /**
419    * @brief This is emitted when the virtual keyboard is connected to or the hardware keyboard is activated.
420    *
421    * @return The InputMethodContext Activated signal.
422    */
423   ActivatedSignalType& ActivatedSignal();
424
425   /**
426    * @brief This is emitted when the InputMethodContext manager receives an event from the InputMethodContext.
427    *
428    * @return The Event signal containing the event data.
429    */
430   KeyboardEventSignalType& EventReceivedSignal();
431
432   /**
433    * @brief Connect to this signal to be notified when the virtual keyboard is shown or hidden.
434    *
435    * A callback of the following type may be connected:
436    * @code
437    *   void YourCallbackName(bool keyboardShown);
438    * @endcode
439    * If the parameter keyboardShown is true, then the keyboard has just shown, if it is false, then it
440    * has just been hidden.
441    * @return The signal to connect to.
442    */
443   StatusSignalType& StatusChangedSignal();
444
445   /**
446    * @brief Connect to this signal to be notified when the virtual keyboard is resized.
447    *
448    * A callback of the following type may be connected:
449    * @code
450    *   void YourCallbackName( int resolvedResize );
451    * @endcode
452    * The parameter sends the resolved resize defined by the InputMethodContext.
453    *
454    * User can get changed size by using GetInputMethodArea() in the callback
455    * @return The signal to connect to.
456    */
457   KeyboardResizedSignalType& ResizedSignal();
458
459   /**
460    * @brief Connect to this signal to be notified when the virtual keyboard's language is changed.
461    *
462    * A callback of the following type may be connected:
463    * @code
464    *   void YourCallbackName( int resolvedLanguage );
465    * @endcode
466    * The parameter sends the resolved language defined by the InputMethodContext.
467    *
468    * User can get the text direction of the language by calling GetTextDirection() in the callback.
469    * @return The signal to connect to.
470    */
471   LanguageChangedSignalType& LanguageChangedSignal();
472
473   /**
474    * @brief Connect to this signal to be notified when the keyboard type is changed.
475    *
476    * A callback of the following type may be connected:
477    * @code
478    *   void YourCallbackName( KeyboardType keyboard );
479    * @endcode
480    *
481    * @return The signal to connect to.
482    */
483   KeyboardTypeSignalType& KeyboardTypeChangedSignal();
484
485 public:
486
487   /**
488    * @brief This constructor is used by InputMethodContext::New().
489    *
490    * @param[in] inputMethodContext A pointer to the InputMethodContext.
491    */
492   explicit DALI_INTERNAL InputMethodContext( Internal::Adaptor::InputMethodContext* inputMethodContext );
493
494 };
495
496
497
498 } // namespace Dali
499
500 #endif // __DALI_INPUT_METHOD_CONTEXT_H__