Merge "DALi Version 1.0.45" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.h
1 #ifndef __DALI_TOOLKIT_TEXT_CONTROLLER_H__
2 #define __DALI_TOOLKIT_TEXT_CONTROLLER_H__
3
4 /*
5  * Copyright (c) 2015 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 <string>
23 #include <dali/devel-api/adaptor-framework/imf-manager.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/events/gesture.h>
27 #include <dali/public-api/events/key-event.h>
28 #include <dali/public-api/math/vector3.h>
29 #include <dali/public-api/math/vector2.h>
30 #include <dali/public-api/object/ref-object.h>
31
32 // INTERNAL INCLUDES
33 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
34 #include <dali-toolkit/internal/text/font-run.h>
35 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
36 #include <dali-toolkit/internal/text/text-control-interface.h>
37 #include <dali-toolkit/internal/text/text-view.h>
38
39 namespace Dali
40 {
41
42 namespace Toolkit
43 {
44
45 namespace Text
46 {
47
48 class Controller;
49 class LayoutEngine;
50
51 typedef IntrusivePtr<Controller> ControllerPtr;
52 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
53
54 /**
55  * @brief Different placeholder-text can be shown when the control is active/inactive.
56  */
57 enum PlaceholderType
58 {
59   PLACEHOLDER_TYPE_ACTIVE,
60   PLACEHOLDER_TYPE_INACTIVE,
61 };
62
63 /**
64  * @brief A Text Controller is used by UI Controls which display text.
65  *
66  * It manipulates the Logical & Visual text models on behalf of the UI Controls.
67  * It provides a view of the text that can be used by rendering back-ends.
68  *
69  * For selectable/editable UI controls, the controller handles input events from the UI control
70  * and decorations (grab handles etc) via an interface.
71  */
72 class Controller : public RefObject, public Decorator::ControllerInterface
73 {
74 public:
75
76   /**
77    * @brief Text related operations to be done in the relayout process.
78    */
79   enum OperationsMask
80   {
81     NO_OPERATION       = 0x0000,
82     CONVERT_TO_UTF32   = 0x0001,
83     GET_SCRIPTS        = 0x0002,
84     VALIDATE_FONTS     = 0x0004,
85     GET_LINE_BREAKS    = 0x0008,
86     GET_WORD_BREAKS    = 0x0010,
87     BIDI_INFO          = 0x0020,
88     SHAPE_TEXT         = 0x0040,
89     GET_GLYPH_METRICS  = 0x0080,
90     LAYOUT             = 0x0100,
91     UPDATE_ACTUAL_SIZE = 0x0200,
92     REORDER            = 0x0400,
93     ALIGN              = 0x0800,
94     ALL_OPERATIONS     = 0xFFFF
95   };
96
97   /**
98    * @brief Used to distinguish between regular key events and IMF events
99    */
100   enum InsertType
101   {
102     COMMIT,
103     PRE_EDIT
104   };
105
106   /**
107    * @brief Create a new instance of a Controller.
108    *
109    * @param[in] controlInterface An interface used to request a text relayout.
110    * @return A pointer to a new Controller.
111    */
112   static ControllerPtr New( ControlInterface& controlInterface );
113
114   /**
115    * @brief Called to enable text input.
116    *
117    * @note Selectable or editable controls should call this once after Controller::New().
118    * @param[in] decorator Used to create cursor, selection handle decorations etc.
119    */
120   void EnableTextInput( DecoratorPtr decorator );
121
122   /**
123    * @brief Replaces any text previously set.
124    *
125    * @note This will be converted into UTF-32 when stored in the text model.
126    * @param[in] text A string of UTF-8 characters.
127    */
128   void SetText( const std::string& text );
129
130   /**
131    * @brief Retrieve any text previously set.
132    *
133    * @return A string of UTF-8 characters.
134    */
135   void GetText( std::string& text ) const;
136
137   /**
138    * @brief Replaces any placeholder text previously set.
139    *
140    * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
141    * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
142    * @return True if the remove was successful.
143    */
144   bool RemoveText( int cursorOffset, int numberOfChars );
145
146   /**
147    * @brief Retrieve the current cursor position.
148    *
149    * @return The cursor position.
150    */
151   unsigned int GetLogicalCursorPosition() const;
152
153   /**
154    * @brief Replaces any placeholder text previously set.
155    *
156    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
157    * @param[in] text A string of UTF-8 characters.
158    */
159   void SetPlaceholderText( PlaceholderType type, const std::string& text );
160
161   /**
162    * @brief Retrieve any placeholder text previously set.
163    *
164    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
165    * @param[out] A string of UTF-8 characters.
166    */
167   void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
168
169   /**
170    * @brief Sets the maximum number of characters that can be inserted into the TextModel
171    *
172    * @param[in] maxCharacters maximum number of characters to be accepted
173    */
174   void SetMaximumNumberOfCharacters( int maxCharacters );
175
176   /**
177    * @brief Sets the maximum number of characters that can be inserted into the TextModel
178    *
179    * @param[in] maxCharacters maximum number of characters to be accepted
180    */
181   int GetMaximumNumberOfCharacters();
182
183   /**
184    * @brief Set the default font family.
185    *
186    * @param[in] defaultFontFamily The default font family.
187    */
188   void SetDefaultFontFamily( const std::string& defaultFontFamily );
189
190   /**
191    * @brief Retrieve the default font family.
192    *
193    * @return The default font family.
194    */
195   const std::string& GetDefaultFontFamily() const;
196
197   /**
198    * @brief Set the default font style.
199    *
200    * @param[in] defaultFontStyle The default font style.
201    */
202   void SetDefaultFontStyle( const std::string& defaultFontStyle );
203
204   /**
205    * @brief Retrieve the default font style.
206    *
207    * @return The default font style.
208    */
209   const std::string& GetDefaultFontStyle() const;
210
211   /**
212    * @brief Set the default point size.
213    *
214    * @param[in] defaultFontStyle The default point size.
215    */
216   void SetDefaultPointSize( float pointSize );
217
218   /**
219    * @brief Retrieve the default point size.
220    *
221    * @return The default point size.
222    */
223   float GetDefaultPointSize() const;
224
225   /**
226    * @brief Set the text color
227    *
228    * @param textColor The text color
229    */
230   void SetTextColor( const Vector4& textColor );
231
232   /**
233    * @brief Retrieve the text color
234    *
235    * @return The text color
236    */
237   const Vector4& GetTextColor() const;
238
239   /**
240    * @brief Set the text color
241    *
242    * @param textColor The text color
243    */
244   void SetPlaceholderTextColor( const Vector4& textColor );
245
246   /**
247    * @brief Retrieve the text color
248    *
249    * @return The text color
250    */
251   const Vector4& GetPlaceholderTextColor() const;
252
253   /**
254    * @brief Set the shadow offset.
255    *
256    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
257    */
258   void SetShadowOffset( const Vector2& shadowOffset );
259
260   /**
261    * @brief Retrieve the shadow offset.
262    *
263    * @return The shadow offset.
264    */
265   const Vector2& GetShadowOffset() const;
266
267   /**
268    * @brief Set the shadow color.
269    *
270    * @param[in] shadowColor The shadow color.
271    */
272   void SetShadowColor( const Vector4& shadowColor );
273
274   /**
275    * @brief Retrieve the shadow color.
276    *
277    * @return The shadow color.
278    */
279   const Vector4& GetShadowColor() const;
280
281   /**
282    * @brief Set the underline color.
283    *
284    * @param[in] color color of underline.
285    */
286   void SetUnderlineColor( const Vector4& color );
287
288   /**
289    * @brief Retrieve the underline color.
290    *
291    * @return The underline color.
292    */
293   const Vector4& GetUnderlineColor() const;
294
295   /**
296    * @brief Set the underline enabled flag.
297    *
298    * @param[in] enabled The underline enabled flag.
299    */
300   void SetUnderlineEnabled( bool enabled );
301
302   /**
303    * @brief Returns whether the text is underlined or not.
304    *
305    * @return The underline state.
306    */
307   bool IsUnderlineEnabled() const;
308
309   /**
310    * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
311    *
312    * @param[in] height The height in pixels of the underline
313    */
314   void SetUnderlineHeight( float height );
315
316   /**
317    * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
318    *
319    * @return The height of the underline, or 0 if height is not overrided.
320    */
321   float GetUnderlineHeight() const;
322
323   /**
324    * @brief Called to enable/disable cursor blink.
325    *
326    * @note Only editable controls should calls this.
327    * @param[in] enabled Whether the cursor should blink or not.
328    */
329   void SetEnableCursorBlink( bool enable );
330
331   /**
332    * @brief Query whether cursor blink is enabled.
333    *
334    * @return Whether the cursor should blink or not.
335    */
336   bool GetEnableCursorBlink() const;
337
338   /**
339    * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
340    *
341    * @return The scroll position.
342    */
343   const Vector2& GetScrollPosition() const;
344
345   /**
346    * @brief Query the alignment offset.
347    *
348    * @return The alignmnet offset.
349    */
350   const Vector2& GetAlignmentOffset() const;
351
352   /**
353    * @copydoc Control::GetNaturalSize()
354    */
355   Vector3 GetNaturalSize();
356
357   /**
358    * @copydoc Control::GetHeightForWidth()
359    */
360   float GetHeightForWidth( float width );
361
362   /**
363    * @brief Triggers a relayout which updates View (if necessary).
364    *
365    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
366    * @param[in] size A the size of a bounding box to layout text within.
367    * @return True if the text model or decorations were updated.
368    */
369   bool Relayout( const Size& size );
370
371   /**
372    * @brief Process queued events which modify the model.
373    */
374   void ProcessModifyEvents();
375
376   /**
377    * @brief Used to remove placeholder text.
378    */
379   void ResetText();
380
381   /**
382    * @brief Used to reset the cursor position after setting a new text.
383    *
384    * @param[in] cursorIndex Where to place the cursor.
385    */
386   void ResetCursorPosition( CharacterIndex cursorIndex );
387
388   /**
389    * @brief Used to reset the scroll position after setting a new text.
390    */
391   void ResetScrollPosition();
392
393   /**
394    * @brief Used to process an event queued from SetText()
395    */
396   void TextReplacedEvent();
397
398   /**
399    * @brief Used to process an event queued from key events etc.
400    */
401   void TextInsertedEvent();
402
403   /**
404    * @brief Used to process an event queued from backspace key etc.
405    */
406   void TextDeletedEvent();
407
408   /**
409    * @brief Lays-out the text.
410    *
411    * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
412    *
413    * @param[in] size A the size of a bounding box to layout text within.
414    * @param[in] operations The layout operations which need to be done.
415    * @param[out] layoutSize The size of the laid-out text.
416    */
417   bool DoRelayout( const Size& size,
418                    OperationsMask operations,
419                    Size& layoutSize );
420
421   /**
422    * @brief Whether to enable the multi-line layout.
423    *
424    * @param[in] enable \e true enables the multi-line (by default)
425    */
426   void SetMultiLineEnabled( bool enable );
427
428   /**
429    * @return Whether the multi-line layout is enabled.
430    */
431   bool IsMultiLineEnabled() const;
432
433   /**
434    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
435    */
436   void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
437
438   /**
439    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
440    */
441   LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
442
443   /**
444    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
445    */
446   void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
447
448   /**
449    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
450    */
451   LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
452
453   /**
454    * @brief Calulates the alignment of the whole text inside the bounding box.
455    *
456    * @param[in] size The size of the bounding box.
457    */
458   void CalculateTextAlignment( const Size& size );
459
460   /**
461    * @brief Return the layout engine.
462    *
463    * @return A reference to the layout engine.
464    */
465   LayoutEngine& GetLayoutEngine();
466
467   /**
468    * @brief Return a view of the text.
469    *
470    * @return A reference to the view.
471    */
472   View& GetView();
473
474   // Text-input Event Queuing
475
476   /**
477    * @brief Caller by editable UI controls when keyboard focus is gained.
478    */
479   void KeyboardFocusGainEvent();
480
481   /**
482    * @brief Caller by editable UI controls when focus is lost.
483    */
484   void KeyboardFocusLostEvent();
485
486   /**
487    * @brief Caller by editable UI controls when key events are received.
488    *
489    * @param[in] event The key event.
490    * @param[in] type Used to distinguish between regular key events and IMF events.
491    */
492   bool KeyEvent( const Dali::KeyEvent& event );
493
494   /**
495    * @brief Caller by editable UI controls when key events are received.
496    *
497    * @param[in] text The text to insert.
498    * @param[in] type Used to distinguish between regular key events and IMF events.
499    */
500   void InsertText( const std::string& text, InsertType type );
501
502   /**
503    * @brief Caller by editable UI controls when a tap gesture occurs.
504    * @param[in] tapCount The number of taps.
505    * @param[in] x The x position relative to the top-left of the parent control.
506    * @param[in] y The y position relative to the top-left of the parent control.
507    */
508   void TapEvent( unsigned int tapCount, float x, float y );
509
510   /**
511    * @brief Caller by editable UI controls when a pan gesture occurs.
512    *
513    * @param[in] state The state of the gesture.
514    * @param[in] displacement This distance panned since the last pan gesture.
515    */
516   void PanEvent( Gesture::State state, const Vector2& displacement );
517
518   /**
519    * @brief Event received from IMF manager
520    *
521    * @param[in] imfManager The IMF manager.
522    * @param[in] imfEvent The event received.
523    * @return A data struture indicating if update is needed, cursor position and current text.
524    */
525   ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
526
527   /**
528    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
529    */
530   virtual void GetTargetSize( Vector2& targetSize );
531
532   /**
533    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
534    */
535   virtual void AddDecoration( Actor& actor, bool needsClipping );
536
537   /**
538    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
539    */
540   virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
541
542 protected:
543
544   /**
545    * @brief A reference counted object may only be deleted by calling Unreference().
546    */
547   virtual ~Controller();
548
549 private:
550
551   /**
552    * @brief Helper to clear font-specific data.
553    */
554   void ShowPlaceholderText();
555
556   /**
557    * @brief Helper to clear all the model data except for LogicalModel::mText.
558    */
559   void ClearModelData();
560
561   /**
562    * @brief Helper to clear font-specific data (only).
563    */
564   void ClearFontData();
565
566   /**
567    * @brief Private constructor.
568    */
569   Controller( ControlInterface& controlInterface );
570
571   // Undefined
572   Controller( const Controller& handle );
573
574   // Undefined
575   Controller& operator=( const Controller& handle );
576
577 private:
578
579   struct Impl;
580   Impl* mImpl;
581 };
582
583 } // namespace Text
584
585 } // namespace Toolkit
586
587 } // namespace Dali
588
589 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__