License conversion from Flora to Apache 2.0
[platform/core/uifw/dali-toolkit.git] / capi / dali-toolkit / public-api / controls / text-view / text-view.h
1 #ifndef __DALI_TOOLKIT_TEXT_VIEW_H__
2 #define __DALI_TOOLKIT_TEXT_VIEW_H__
3
4 /*
5  * Copyright (c) 2014 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 /**
22  * @addtogroup CAPI_DALI_TOOLKIT_TEXT_VIEW_MODULE
23  * @{
24  */
25
26 // EXTERNAL INCLUDES
27 #include <string>
28
29 // INTERNAL INCLUDES
30 #include <dali-toolkit/public-api/controls/alignment/alignment.h>
31 #include <dali-toolkit/public-api/markup-processor/markup-processor.h>
32
33 namespace Dali DALI_IMPORT_API
34 {
35
36 namespace Toolkit
37 {
38
39 namespace Internal DALI_INTERNAL
40 {
41 class TextView;
42 }
43
44 /**
45  * @brief TextView is a layout container for text with alignment, multi-line wrapping and formatting support.
46  *
47  * Different multi-line and exceed policies could be chosen to represent the given text.
48  * \see Toolkit::TextView::SetMultilinePolicy \see Toolkit::TextView::SetExceedPolicy
49  *
50  * Multi-line policies:
51  * <ul>
52  *   <li>\e Split by new line character.
53  *          Text will split when a '\\n' character is found.
54  *
55  *   <li>\e Split by word.
56  *          Text will split when a '\\n' character is found or if the text doesn't fit in the text view width.
57  *          In that case, some words will be moved to a new line.
58  *
59  *   <li>\e Split by character.
60  *          Text will split when a '\\n' character is found or if the text doesn't fit in the text view width.
61  *          In that case, words which doesn't fit will be split in two and the remaining text moved to a new line.
62  * </ul>
63  *
64  * Exceed policies work in combination with multi-line policies:
65  * <ul>
66  *   <li>\e Original size.
67  *          Text will be displayed with its original size.
68  *
69  *   <li>\e Truncate.
70  *          Text will be truncated.
71  *
72  *   <li>\e Fade.
73  *          Text will be faded out.
74  *
75  *   <li>\e Split.
76  *          Text will be split and moved to a new line.
77  *
78  *   <li>\e Shrink to fit.
79  *          Text will be shrink to fit in the text view's boundary.
80  *
81  *   <li>\e EllipsizeEnd.
82  *          Text will be ellipsized at the end.
83  *
84  * </ul>
85  *
86  * Text alignment could be set to align the whole text block inside the text view's boundary. \see Toolkit::TextView::SetTextAlignment.
87  *
88  * Line justification could be set to align lines inside a text block. \see Toolkit::TextView::SetLineJustification.
89  *
90  * A default font could be set through the \see Toolkit::TextView::SetFont method. If any font is set, text view automatically detects a suitable font for every character.
91  *
92  * Font priority:
93  * 1) Use the font specified in text decoration.
94  * 2) Use automatic font detection.
95  *
96  * Integration with other classes (GetSizeAndPositionTable )
97  *
98  * Text decoration ( Color, Font, Size, italic and all features supported in TextActor )
99  */
100 class TextView : public Control
101 {
102 public:
103
104   // Signal Names
105   static const char* const SIGNAL_TEXT_SCROLLED; ///< Signal emitted when the scroll position changes. @see SignalScrolled()
106
107   // Properties
108   static const Property::Index PROPERTY_MARKUP_ENABLED;         ///< name "markup-enabled", @see SetMarkupProcessingEnabled(), type BOOLEAN
109   static const Property::Index PROPERTY_TEXT;                   ///< name "text",                 type STRING
110   static const Property::Index PROPERTY_MULTILINE_POLICY;       ///< name "multiline-policy",     type STRING
111   static const Property::Index PROPERTY_WIDTH_EXCEED_POLICY;    ///< name "width-exceed-policy",  type STRING
112   static const Property::Index PROPERTY_HEIGHT_EXCEED_POLICY;   ///< name "height-exceed-policy", type STRING
113   static const Property::Index PROPERTY_LINE_JUSTIFICATION;     ///< name "line-justification",   type STRING
114   static const Property::Index PROPERTY_FADE_BOUNDARY;          ///< name "fade-boundary",        type VECTOR4
115   static const Property::Index PROPERTY_LINE_HEIGHT_OFFSET;     ///< name "line-height-offset",   type FLOAT
116   static const Property::Index PROPERTY_HORIZONTAL_ALIGNMENT;   ///< name "horizontal-alignment", type STRING
117   static const Property::Index PROPERTY_VERTICAL_ALIGNMENT;     ///< name "vertical-alignment",   type STRING
118
119   /**
120    * @brief Structure used to retrieve Layout info per character.
121    */
122   struct CharacterLayoutInfo
123   {
124     /**
125      * @brief Default constructor.
126      *
127      * Initializes all members to their default values.
128      */
129     CharacterLayoutInfo();
130
131     /**
132      * @brief Empty destructor.
133      *
134      * @note Added to increase coverage.
135      */
136     ~CharacterLayoutInfo();
137
138     /**
139      * @brief Copy constructor.
140      */
141     CharacterLayoutInfo( const CharacterLayoutInfo& characterLayoutInfo );
142
143     /**
144      * @brief Assignment operator.
145      */
146     CharacterLayoutInfo& operator=( const CharacterLayoutInfo& character );
147
148     /**
149      * @brief Constructor.
150      *
151      * @param[in] size of the character.
152      * @param[in] position of the character.
153      * @param[in] isNewLineChar Whether the character is a new line character.
154      * @param[in] isRightToLeftCharacter Whether is a right to left character.
155      * @param[in] visible Whether is visible.
156      * @param[in] descender of the character (distance from the base line to the bottom of the character.)
157      */
158     CharacterLayoutInfo( const Size& size,
159                          const Vector3& position,
160                          bool isNewLineChar,
161                          bool isRightToLeftCharacter,
162                          bool visible,
163                          float descender );
164
165     Size    mSize;                     ///< Size of the group of characters.
166     Vector3 mPosition;                 ///< Position of the group of characters within the text view.
167     bool    mIsNewLineChar:1;          ///< Whether this group of characters represent a new line.
168     bool    mIsRightToLeftCharacter:1; ///< Whether it's a right-to-left character.
169     bool    mIsVisible:1;              ///< Whether this group of characters is visible or not.
170     float   mDescender;                ///< The character's descender which is the distance from the baseline to the bottom of the character
171   };
172
173   typedef std::vector<CharacterLayoutInfo> CharacterLayoutInfoContainer; ///< Container of Character layouts
174
175   /**
176    * @brief Stores some info about a laid-out line.
177    */
178   struct LineLayoutInfo
179   {
180     std::size_t mCharacterGlobalIndex; ///< Global index within the whole text of the first character of current laid-out line.
181     Size        mSize;                 ///< Size of the current laid-out line.
182     float       mAscender;             ///< The max ascender of the current laid-out line.
183   };
184
185   typedef std::vector<LineLayoutInfo> LineLayoutInfoContainer; ///< Container of line layouts
186
187   /**
188    * @brief How text is laid out.
189    */
190   struct TextLayoutInfo
191   {
192     /**
193      * @brief Default constructor.
194      */
195     TextLayoutInfo();
196
197     /**
198      * @brief Empty destructor.
199      *
200      * @note Added to increase coverage.
201      */
202     ~TextLayoutInfo();
203
204     /**
205      * @brief Copy constructor.
206      */
207     TextLayoutInfo( const TextLayoutInfo& textLayoutInfo );
208
209     /**
210      * @brief Assignment operator.
211      */
212     TextLayoutInfo& operator=( const TextLayoutInfo& textLayoutInfo );
213
214     CharacterLayoutInfoContainer mCharacterLayoutInfoTable;    ///< The table of character positions and sizes sorted by the characters' visual index.
215     LineLayoutInfoContainer      mLines;                       ///< For each laid-out line, it stores an index to the first character of the line.
216     std::vector<int>             mCharacterLogicalToVisualMap; ///< The map to store the character's logical (input) index according to its visual (reordered) index.
217     std::vector<int>             mCharacterVisualToLogicalMap; ///< The map to store the character's visual (reordered) index according to its logical (input) index.
218     Size                         mTextSize;                    ///< Text size after relayout.
219     Vector2                      mScrollOffset;                ///< Scroll's position.
220   };
221
222   /**
223    * @brief This structure represents a fade boundary.
224    *
225    * If Exceed policy is set to Fade all text which does not fit within the text-view fade boundary is faded out. Text which exceeds the text-view boundary becomes invisible.
226    * The \e left, \e right, \e top and \e bottom values are positive, in pixels and set the distances between the text-view and fade boundaries.
227    */
228   struct FadeBoundary
229   {
230     /**
231      * @brief Default constructor.
232      *
233      * Initializes all values to 0. It means no fade boundary.
234      */
235     FadeBoundary();
236
237     /**
238      * @brief Constructor.
239      *
240      * Initializes the fade boundary with the given values.
241      *
242      * @param[in] left value in pixels.
243      * @param[in] right value in pixels.
244      * @param[in] top value in pixels.
245      * @param[in] bottom value in pixels.
246      */
247     FadeBoundary( PixelSize left, PixelSize right, PixelSize top, PixelSize bottom );
248
249     PixelSize mLeft;   ///< The left fade boundary
250     PixelSize mRight;  ///< The right fade boundary
251     PixelSize mTop;    ///< The top fade boundary
252     PixelSize mBottom; ///< The bottom fade bounday
253   };
254
255   /**
256    * @brief Define how to split the text in lines.
257    *
258    * SplitByNewLineChar will split the text in lines when a '\\n' character is found.
259    * SplitByWord has effect only when TextView size is assigned.
260    * It will split the text in lines when a '\\n' character is found or if a line exceeds the TextView's boundary. This option won't split a word in two.
261    * SplitByChar has effect only when TextView size is assigned.
262    * It will split the text in lines when a '\\n' character is found or if a line exceeds the TextView's boundary. This option might split a word in two.
263    * The default value is SplitByNewLineChar.
264    */
265   enum MultilinePolicy
266   {
267     SplitByNewLineChar, ///< Text lines will split when '\\n' character is found.
268     SplitByWord,        ///< Text lines will split by word or if '\\n' character is found. It has effect only when TextView size is assigned.
269     SplitByChar         ///< Text lines will split by char or if '\\n' character is found. It has effect only when TextView size is assigned.
270   };
271
272   /**
273    * @brief Define how to display the text when it doesn't fit inside the TextView.
274    *
275    * The default value is ShrinkToFit.
276    */
277   enum ExceedPolicy
278   {
279     Original,         ///< Will display the text in its original size. If a line, a word or a character is bigger than the TextView size it may exceed its boundary.
280     Truncate,         ///< Will display the text in its original size. It won't display the text which exceeds the TextView boundary.
281     Fade,             ///< Will display the text in its original size. It won't display the text which exceeds the TextView boundary. It fades the text out.
282     Split,            ///< Will split the text in a new line.
283     ShrinkToFit,      ///< Will shrink the text to fit the TextView boundary.
284     EllipsizeEnd      ///< Will ellipsize the text by the end.
285   };
286
287   /**
288    * @brief Define how to justify lines inside the text area.
289    *
290    * The default value is Left.
291    */
292   enum LineJustification
293   {
294     Left,     ///< Justify to the left.
295     Center,   ///< Centered.
296     Right,    ///< Justify to the right.
297     Justified ///< Line justified.
298   };
299
300 public:
301   /**
302    * @brief Create an TextView handle; this can be initialised with TextView::New().
303    *
304    * Calling member functions with an uninitialised Dali::Object handle is not allowed.
305    */
306   TextView();
307
308   /**
309    * @brief Copy constructor.
310    *
311    * Creates another handle that points to the same real object
312    * @param[in] handle The handle to copy from
313    */
314   TextView( const TextView& handle );
315
316   /**
317    * @brief Assignment operator.
318    *
319    * Changes this handle to point to another real object
320    * @param[in] handle The handle to copy from
321    * @return a reference to this
322    */
323   TextView& operator=( const TextView& handle );
324
325   /**
326    * @brief Create a TextView control with no text.
327    *
328    * @return A handle the TextView control.
329    */
330   static TextView New();
331
332   /**
333    * @brief Create a TextView control.
334    *
335    * @param[in] text to display.
336    * @return A handle the TextView control.
337    */
338   static TextView New( const std::string& text );
339
340   /**
341    * @copydoc TextView New( const std::string& text )
342    */
343   static TextView New( const MarkupProcessor::StyledTextArray& text );
344
345   /**
346    * @brief Downcast an Object handle to TextView.
347    *
348    * If handle points to a TextView the
349    * downcast produces valid handle. If not the returned handle is left uninitialized.
350    * @param[in] handle Handle to an object
351    * @return handle to a TextView or an uninitialized handle
352    */
353   static TextView DownCast( BaseHandle handle );
354
355   /**
356    * @brief Virtual destructor.
357    *
358    * Dali::Object derived classes typically do not contain member data.
359    */
360   virtual ~TextView();
361
362   /**
363    * @brief Replace the current text with a new text string.
364    *
365    * @param[in] text to display. The string may contain style tags.
366    */
367   void SetText( const std::string& text );
368
369   /**
370    * @brief Replace the current text with a new text string with style.
371    *
372    * @param[in] text with style to display.
373    */
374   void SetText( const MarkupProcessor::StyledTextArray& text );
375
376   /**
377    * @brief Inserts the given text in the specified position.
378    *
379    * @param[in] position Where the given text is going to be added.
380    * @param[in] text The text to be added.
381    */
382   void InsertTextAt( std::size_t position, const std::string& text );
383
384   /**
385    * @copydoc InsertTextAt( std::size_t position, const std::string& text )
386    */
387   void InsertTextAt( std::size_t position, const MarkupProcessor::StyledTextArray& text );
388
389   /**
390    * @brief Replaces part of the text.
391    *
392    * It removes the specified number of characters from the given position and inserts the given text in the same specified position.
393    *
394    * @param[in] position of the first character to be removed and Where the given text is going to be added.
395    * @param[in] numberOfCharacters number of characters to be removed.
396    * @param[in] text The text to be added.
397    */
398   void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text );
399
400   /**
401    * @copydoc ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const std::string& text )
402    */
403   void ReplaceTextFromTo( std::size_t position, std::size_t numberOfCharacters, const MarkupProcessor::StyledTextArray& text );
404
405   /**
406    * @brief Removes the specified number of characters from the given position.
407    *
408    * @param[in] position of the first character to be removed.
409    * @param[in] numberOfCharacters number of characters to be removed.
410    */
411   void RemoveTextFrom( std::size_t position, std::size_t numberOfCharacters );
412
413   /**
414    * @brief Get the currently displayed text.
415    *
416    * @return The currently displayed text.
417    */
418   std::string GetText() const;
419
420   /**
421    * @brief Sets a line height offset.
422    *
423    * The line height offset will be added to the font line height.
424    * @param [in] offset The height offset in PointSize units.
425    */
426   void SetLineHeightOffset( PointSize offset );
427
428   /**
429    * @brief Retrieves the line height offset.
430    *
431    * @return The line height offset in PointSize units.
432    */
433   PointSize GetLineHeightOffset() const;
434
435   /**
436    * @brief Sets the given style to the current text.
437    *
438    * By default all style settings are applied but a bit mask could be used to modify only certain style settings.
439    * @note TextView doesn't store a copy of the given style, it applies the given style to the current text only.
440    * Subsequent calls to SetText() will override any style set by this method.
441    * @param[in] style The given style
442    * @param[in] mask The bit mask.
443    */
444   void SetStyleToCurrentText( const TextStyle& style, TextStyle::Mask mask = TextStyle::ALL );
445
446   /**
447    * @brief Set the current text alignment.
448    *
449    * Default alignment is (HorizontalCenter | VerticalCenter)
450    * @param[in] align The new alignment option.
451    */
452   void SetTextAlignment( Alignment::Type align );
453
454   /**
455    * @brief Get the current text alignment combined into a single value.
456    *
457    * The values can be tested by using the & operator
458    * and the desired flag. e.g. if (GetTextAlignment() & HorizontalCentre) ...
459    * @return the combined alignment
460    */
461   Alignment::Type GetTextAlignment() const;
462
463   /**
464    * @brief Sets how to split the text in lines policy.
465    *
466    * @param policy The multi-line policy. SplitByNewLineChar is set by default.
467    */
468   void SetMultilinePolicy( MultilinePolicy policy );
469
470   /**
471    * @brief Gets the split in lines policy.
472    *
473    * @return The multi-line policy.
474    */
475   MultilinePolicy GetMultilinePolicy() const;
476
477   /**
478    * @brief Sets how to display the text inside the TextView when it exceeds the text-view's width.
479    *
480    * @param policy The exceed policy. Original is set by default.
481    */
482   void SetWidthExceedPolicy( ExceedPolicy policy );
483
484   /**
485    * @brief Gets the width exceed policy.
486    *
487    * @return The exceed policy.
488    */
489   ExceedPolicy GetWidthExceedPolicy() const;
490
491   /**
492    * @brief Sets how to display the text inside the TextView when it exceeds the text-view's height.
493    *
494    * @param policy The exceed policy. Original is set by default.
495    */
496   void SetHeightExceedPolicy( ExceedPolicy policy );
497
498   /**
499    * @brief Gets the height exceed policy.
500    *
501    * @return The exceed policy.
502    */
503   ExceedPolicy GetHeightExceedPolicy() const;
504
505   /**
506    * @brief Sets how to justify lines inside the text area.
507    *
508    * @param justification The line justification. Left is set by default.
509    */
510   void SetLineJustification( LineJustification justification );
511
512   /**
513    * @brief Gets the line justification.
514    *
515    * @return The line justification.
516    */
517   LineJustification GetLineJustification() const;
518
519   /**
520    * @brief Sets a fade boundary.
521    *
522    * @see FadeBoundary.
523    *
524    * @param[in] fadeBoundary The given fade boundary.
525    */
526   void SetFadeBoundary( const FadeBoundary& fadeBoundary );
527
528   /**
529    * @brief Retrieves the fade boundary.
530    *
531    * @see FadeBoundary.
532    *
533    * @return The fade boundary.
534    */
535   const FadeBoundary& GetFadeBoundary() const;
536
537   /**
538    * @brief Sets the ellipsize text.
539    *
540    * @param[in] ellipsizeText The new text. The string may contain style tags. By default the ellipsize text is '...'
541    */
542   void SetEllipsizeText( const std::string& ellipsizeText );
543
544   /**
545    * @brief Sets the ellipsize text.
546    *
547    * @param[in] ellipsizeText The new text with its style.
548    */
549   void SetEllipsizeText( const MarkupProcessor::StyledTextArray& ellipsizeText );
550
551   /**
552    * @brief Retrieves the ellipsize text.
553    *
554    * @return The ellipsize text.
555    */
556   std::string GetEllipsizeText() const;
557
558   /**
559    * @brief A mechanism to retrieve layout information from the TextView.
560    *
561    * It produces a vector of CharcterLayoutInfo structures which describe the size and position of each character,
562    * two vectors which maps the logical and visual positions of the characters in a bidirectional text, the size
563    * of the whole laid-out text and the scroll offset value.
564    *
565    * @see TextLayoutInfo.
566    *
567    * @param[out] textLayoutInfo A structure with text layout information.
568    */
569   void GetTextLayoutInfo( TextLayoutInfo& textLayoutInfo );
570
571   /**
572    * @brief Allows modification of text-actor's position in the depth sort algorithm.
573    *
574    * @see Dali::RenderableActor::SetSortModifier()
575    * @param [in] depthOffset the offset to be given to the internal text-actors. Positive values pushing it further back.
576    */
577   void SetSortModifier( float depthOffset );
578
579   /**
580    * @brief Sets whether text-view renders text using a previously generated snapshot.
581    *
582    * Rendering long text using a snapshot may increase performance. The default value is \e true (render using a snapshot).
583    *
584    * @param[in] enable Whether text-view is using a snapshot to render text.
585    */
586   void SetSnapshotModeEnabled( bool enable );
587
588   /**
589    * @brief Retrieves whether text-view is using a snapshot to render text.
590    *
591    * @return \e true if text-view is using a snapshot to render text, otherwhise it returns \e false.
592    */
593   bool IsSnapshotModeEnabled() const;
594
595   /**
596    * @brief Sets whether markup processing should be carried out.
597    *
598    * @param[in] enable whether markup processing is carried out or not.
599    */
600   void SetMarkupProcessingEnabled( bool enable );
601
602   /**
603    * @brief Retrieves whether text-view is processing markup text
604    *
605    * @return \e true if text-view markup processing is enabled, otherwhise it returns \e false.
606    */
607   bool IsMarkupProcessingEnabled() const;
608
609   /**
610    * @brief Enables or disables the text scroll.
611    *
612    * When scroll is enabled, snapshot mode will be enabled automatically. Equally, if scroll is disabled
613    * the snapshot mode is restored to the previous value.
614    *
615    * @param[in] enable Whether to enable the text scroll.
616    */
617   void SetScrollEnabled( bool enable );
618
619   /**
620    * @brief Retrieves whether the text scroll is enabled.
621    *
622    * @return \e true if the scroll is enabled.
623    */
624   bool IsScrollEnabled() const;
625
626   /**
627    * @brief Sets a new scroll position.
628    *
629    * The new scroll position set could be trimmed if the text doesn't cover the whole text-view.
630    * i.e. If a text-view is 100x100 and a text is 200x100 a scroll position beyond 50x0 will be trimmed to 50x0.
631    *
632    * Call IsScrollPositionTrimmed() to know if the last scroll position set has been trimmed.
633    *
634    * A signal is emitted. @see ScrolledSignal().
635    *
636    * @param[in] position The new scroll position.
637    */
638   void SetScrollPosition( const Vector2& position );
639
640   /**
641    * @brief Recrieves current scroll position.
642    *
643    * @return The scroll position.
644    */
645   const Vector2& GetScrollPosition() const;
646
647   /**
648    * @brief Whether the last scroll position set was trimmed.
649    *
650    * @return \e true if the last scroll position set was trimmed, otherwise \e false.
651    */
652   bool IsScrollPositionTrimmed() const;
653
654 public:
655   /// @brief Signal types
656   typedef SignalV2< void ( TextView textView, Vector2 scrollDelta ) > ScrolledSignalV2;
657
658   /**
659    * @brief Signal emitted when the scroll position changes.
660    *
661    * A callback with the following prototype can be connected to this signal.
662    *
663    * Callback(TextView textView, Vector2 scrollDelta)
664    *
665    * \e textView is the handle of the text-view emitting the signal.
666    * \e scrollDelta is the differente of the current scroll position with the previous one.
667    * @return The signal to connect to
668    */
669   ScrolledSignalV2& ScrolledSignal();
670
671 public: // Not intended for application developers
672
673   /**
674    * @brief Creates a handle using the Toolkit::Internal implementation.
675    *
676    * @param[in]  implementation  The Control implementation.
677    */
678   TextView( Internal::TextView& implementation );
679
680   /**
681    * @brief Allows the creation of this Control from an Internal::CustomActor pointer.
682    *
683    * @param[in]  internal  A pointer to the internal CustomActor.
684    */
685   TextView( Dali::Internal::CustomActor* internal );
686 };
687
688 } // namespace Toolkit
689
690 } // namespace Dali
691
692 /**
693  * @}
694  */
695 #endif // __DALI_TOOLKIT_ITEM_VIEW_H__