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