Auto Scrolling Text Label
[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 <dali/devel-api/adaptor-framework/imf-manager.h>
23 #include <dali/public-api/events/gesture.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup-callback-interface.h>
27 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
28 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
29 #include <dali-toolkit/internal/text/text-control-interface.h>
30
31 namespace Dali
32 {
33
34 namespace Toolkit
35 {
36
37 namespace Text
38 {
39
40 class Controller;
41 class View;
42
43 typedef IntrusivePtr<Controller> ControllerPtr;
44 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
45
46 /**
47  * @brief Different placeholder-text can be shown when the control is active/inactive.
48  */
49 enum PlaceholderType
50 {
51   PLACEHOLDER_TYPE_ACTIVE,
52   PLACEHOLDER_TYPE_INACTIVE,
53 };
54
55 /**
56  * @brief A Text Controller is used by UI Controls which display text.
57  *
58  * It manipulates the Logical & Visual text models on behalf of the UI Controls.
59  * It provides a view of the text that can be used by rendering back-ends.
60  *
61  * For selectable/editable UI controls, the controller handles input events from the UI control
62  * and decorations (grab handles etc) via the Decorator::ControllerInterface interface.
63  *
64  * The text selection popup button callbacks are as well handled via the TextSelectionPopupCallbackInterface interface.
65  */
66 class Controller : public RefObject, public Decorator::ControllerInterface, public TextSelectionPopupCallbackInterface
67 {
68 public:
69
70   /**
71    * @brief Text related operations to be done in the relayout process.
72    */
73   enum OperationsMask
74   {
75     NO_OPERATION       = 0x0000,
76     CONVERT_TO_UTF32   = 0x0001,
77     GET_SCRIPTS        = 0x0002,
78     VALIDATE_FONTS     = 0x0004,
79     GET_LINE_BREAKS    = 0x0008,
80     GET_WORD_BREAKS    = 0x0010,
81     BIDI_INFO          = 0x0020,
82     SHAPE_TEXT         = 0x0040,
83     GET_GLYPH_METRICS  = 0x0080,
84     LAYOUT             = 0x0100,
85     UPDATE_ACTUAL_SIZE = 0x0200,
86     REORDER            = 0x0400,
87     ALIGN              = 0x0800,
88     COLOR              = 0x1000,
89     UPDATE_DIRECTION   = 0x2000,
90     ALL_OPERATIONS     = 0xFFFF
91   };
92
93   /**
94    * @brief Used to distinguish between regular key events and IMF events
95    */
96   enum InsertType
97   {
98     COMMIT,
99     PRE_EDIT
100   };
101
102   /**
103    * @brief Used to specify whether to update the input style.
104    */
105   enum UpdateInputStyleType
106   {
107     UPDATE_INPUT_STYLE,
108     DONT_UPDATE_INPUT_STYLE
109   };
110
111   /**
112    * @brief Create a new instance of a Controller.
113    *
114    * @param[in] controlInterface An interface used to request a text relayout.
115    * @return A pointer to a new Controller.
116    */
117   static ControllerPtr New( ControlInterface& controlInterface );
118
119   /**
120    * @brief Called to enable text input.
121    *
122    * @note Selectable or editable controls should call this once after Controller::New().
123    * @param[in] decorator Used to create cursor, selection handle decorations etc.
124    */
125   void EnableTextInput( DecoratorPtr decorator );
126
127   /**
128    * @brief Used to switch between bitmap & vector based glyphs
129    *
130    * @param[in] glyphType The type of glyph; note that metrics for bitmap & vector based glyphs are different.
131    */
132   void SetGlyphType( TextAbstraction::GlyphType glyphType );
133
134   /**
135    * @brief Enables/disables the mark-up processor.
136    *
137    * By default is disabled.
138    *
139    * @param[in] enable Whether to enable the mark-up processor.
140    */
141   void SetMarkupProcessorEnabled( bool enable );
142
143   /**
144    * @brief Retrieves whether the mark-up processor is enabled.
145    *
146    * By default is disabled.
147    *
148    * @return @e true if the mark-up processor is enabled, otherwise returns @e false.
149    */
150   bool IsMarkupProcessorEnabled() const;
151
152   /**
153    * @brief Enables/disables the auto text scrolling
154    *
155    * By default is disabled.
156    *
157    * @param[in] enable Whether to enable the auto scrolling
158    */
159   void SetAutoScrollEnabled( bool enable );
160
161   /**
162    * @brief Retrieves whether auto text scrolling is enabled.
163    *
164    * By default is disabled.
165    *
166    * @return @e true if auto scrolling is enabled, otherwise returns @e false.
167    */
168   bool IsAutoScrollEnabled() const;
169
170   /**
171    * @brief Get direction of the text from the first line of text,
172    * @return bool rtl (right to left) is true
173    */
174   CharacterDirection GetAutoScrollDirection() const;
175
176   /**
177    * @brief Replaces any text previously set.
178    *
179    * @note This will be converted into UTF-32 when stored in the text model.
180    * @param[in] text A string of UTF-8 characters.
181    */
182   void SetText( const std::string& text );
183
184   /**
185    * @brief Retrieve any text previously set.
186    *
187    * @return A string of UTF-8 characters.
188    */
189   void GetText( std::string& text ) const;
190
191   /**
192    * @brief Remove a given number of characters
193    *
194    * When predictve text is used the pre-edit text is removed and inserted again with the new characters.
195    * The UpdateInputStyleType @type parameter if set to DONT_UPDATE_INPUT_STYLE avoids to update the input
196    * style when pre-edit text is removed.
197    *
198    * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
199    * @param[in] numberOfCharacters The number of characters to delete from the cursorOffset.
200    * @param[in] type Whether to update the input style.
201    * @return True if the remove was successful.
202    */
203   bool RemoveText( int cursorOffset,
204                    int numberOfCharacters,
205                    UpdateInputStyleType type  );
206
207   /**
208    * @brief Retrieve the current cursor position.
209    *
210    * @return The cursor position.
211    */
212   unsigned int GetLogicalCursorPosition() const;
213
214   /**
215    * @brief Replaces any placeholder text previously set.
216    *
217    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
218    * @param[in] text A string of UTF-8 characters.
219    */
220   void SetPlaceholderText( PlaceholderType type, const std::string& text );
221
222   /**
223    * @brief Retrieve any placeholder text previously set.
224    *
225    * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
226    * @param[out] A string of UTF-8 characters.
227    */
228   void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
229
230   /**
231    * @brief Sets the maximum number of characters that can be inserted into the TextModel
232    *
233    * @param[in] maxCharacters maximum number of characters to be accepted
234    */
235   void SetMaximumNumberOfCharacters( Length maxCharacters );
236
237   /**
238    * @brief Sets the maximum number of characters that can be inserted into the TextModel
239    *
240    * @param[in] maxCharacters maximum number of characters to be accepted
241    */
242   int GetMaximumNumberOfCharacters();
243
244   /**
245    * @brief Set the default font family.
246    *
247    * @param[in] defaultFontFamily The default font family.
248    */
249   void SetDefaultFontFamily( const std::string& defaultFontFamily );
250
251   /**
252    * @brief Retrieve the default font family.
253    *
254    * @return The default font family.
255    */
256   const std::string& GetDefaultFontFamily() const;
257
258   /**
259    * @brief Sets the font's style string.
260    *
261    * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
262    *
263    * @param[in] style The font's style string.
264    */
265   void SetDefaultFontStyle( const std::string& style );
266
267   /**
268    * @brief Retrieves the font's style.
269    *
270    * @return The font's style.
271    */
272   const std::string& GetDefaultFontStyle() const;
273
274   /**
275    * @brief Sets the default font weight.
276    *
277    * @param[in] weight The font weight.
278    */
279   void SetDefaultFontWeight( FontWeight weight );
280
281   /**
282    * @brief Retrieves the default font weight.
283    *
284    * @return The default font weight.
285    */
286   FontWeight GetDefaultFontWeight() const;
287
288   /**
289    * @brief Sets the default font width.
290    *
291    * @param[in] width The font width.
292    */
293   void SetDefaultFontWidth( FontWidth width );
294
295   /**
296    * @brief Retrieves the default font width.
297    *
298    * @return The default font width.
299    */
300   FontWidth GetDefaultFontWidth() const;
301
302   /**
303    * @brief Sets the default font slant.
304    *
305    * @param[in] slant The font slant.
306    */
307   void SetDefaultFontSlant( FontSlant slant );
308
309   /**
310    * @brief Retrieves the default font slant.
311    *
312    * @return The default font slant.
313    */
314   FontSlant GetDefaultFontSlant() const;
315
316   /**
317    * @brief Set the default point size.
318    *
319    * @param[in] pointSize The default point size.
320    */
321   void SetDefaultPointSize( float pointSize );
322
323   /**
324    * @brief Retrieve the default point size.
325    *
326    * @return The default point size.
327    */
328   float GetDefaultPointSize() const;
329
330   /**
331    * @ brief Update the text after a font change
332    * @param[in] newDefaultFont The new font to change to
333    */
334   void UpdateAfterFontChange( const std::string& newDefaultFont );
335
336   /**
337    * @brief Set the text color
338    *
339    * @param textColor The text color
340    */
341   void SetTextColor( const Vector4& textColor );
342
343   /**
344    * @brief Retrieve the text color
345    *
346    * @return The text color
347    */
348   const Vector4& GetTextColor() const;
349
350   /**
351    * @brief Set the text color
352    *
353    * @param textColor The text color
354    */
355   void SetPlaceholderTextColor( const Vector4& textColor );
356
357   /**
358    * @brief Retrieve the text color
359    *
360    * @return The text color
361    */
362   const Vector4& GetPlaceholderTextColor() const;
363
364   /**
365    * @brief Set the shadow offset.
366    *
367    * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
368    */
369   void SetShadowOffset( const Vector2& shadowOffset );
370
371   /**
372    * @brief Retrieve the shadow offset.
373    *
374    * @return The shadow offset.
375    */
376   const Vector2& GetShadowOffset() const;
377
378   /**
379    * @brief Set the shadow color.
380    *
381    * @param[in] shadowColor The shadow color.
382    */
383   void SetShadowColor( const Vector4& shadowColor );
384
385   /**
386    * @brief Retrieve the shadow color.
387    *
388    * @return The shadow color.
389    */
390   const Vector4& GetShadowColor() const;
391
392   /**
393    * @brief Set the underline color.
394    *
395    * @param[in] color color of underline.
396    */
397   void SetUnderlineColor( const Vector4& color );
398
399   /**
400    * @brief Retrieve the underline color.
401    *
402    * @return The underline color.
403    */
404   const Vector4& GetUnderlineColor() const;
405
406   /**
407    * @brief Set the underline enabled flag.
408    *
409    * @param[in] enabled The underline enabled flag.
410    */
411   void SetUnderlineEnabled( bool enabled );
412
413   /**
414    * @brief Returns whether the text is underlined or not.
415    *
416    * @return The underline state.
417    */
418   bool IsUnderlineEnabled() const;
419
420   /**
421    * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
422    *
423    * @param[in] height The height in pixels of the underline
424    */
425   void SetUnderlineHeight( float height );
426
427   /**
428    * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
429    *
430    * @return The height of the underline, or 0 if height is not overrided.
431    */
432   float GetUnderlineHeight() const;
433
434   /**
435    * @brief Sets the input text's color.
436    *
437    * @param[in] color The input text's color.
438    */
439   void SetInputColor( const Vector4& color );
440
441   /**
442    * @brief Retrieves the input text's color.
443    *
444    * @return The input text's color.
445    */
446   const Vector4& GetInputColor() const;
447
448   /**
449    * @brief Sets the input text's font family name.
450    *
451    * @param[in] fontFamily The text's font family name.
452    */
453   void SetInputFontFamily( const std::string& fontFamily );
454
455   /**
456    * @brief Retrieves the input text's font family name.
457    *
458    * @return The input text's font family name.
459    */
460   const std::string& GetInputFontFamily() const;
461
462   /**
463    * @brief Sets the input text's font style.
464    *
465    * @param[in] fontStyle The input text's font style.
466    */
467   void SetInputFontStyle( const std::string& fontStyle );
468
469   /**
470    * @brief Retrieves the input text's font style.
471    *
472    * @return The input text's font style.
473    */
474   const std::string& GetInputFontStyle() const;
475
476   /**
477    * @brief Sets the input font's weight.
478    *
479    * @param[in] weight The input font's weight.
480    */
481   void SetInputFontWeight( FontWeight weight );
482
483   /**
484    * @brief Retrieves the input font's weight.
485    *
486    * @return The input font's weight.
487    */
488   FontWeight GetInputFontWeight() const;
489
490   /**
491    * @brief Sets the input font's width.
492    *
493    * @param[in] width The input font's width.
494    */
495   void SetInputFontWidth( FontWidth width );
496
497   /**
498    * @brief Retrieves the input font's width.
499    *
500    * @return The input font's width.
501    */
502   FontWidth GetInputFontWidth() const;
503
504   /**
505    * @brief Sets the input font's slant.
506    *
507    * @param[in] slant The input font's slant.
508    */
509   void SetInputFontSlant( FontSlant slant );
510
511   /**
512    * @brief Retrieves the input font's slant.
513    *
514    * @return The input font's slant.
515    */
516   FontSlant GetInputFontSlant() const;
517
518   /**
519    * @brief Sets the input font's point size.
520    *
521    * @param[in] size The input font's point size.
522    */
523   void SetInputFontPointSize( float size );
524
525   /**
526    * @brief Retrieves the input font's point size.
527    *
528    * @return The input font's point size.
529    */
530   float GetInputFontPointSize() const;
531
532   /**
533    * @brief Called to enable/disable cursor blink.
534    *
535    * @note Only editable controls should calls this.
536    * @param[in] enabled Whether the cursor should blink or not.
537    */
538   void SetEnableCursorBlink( bool enable );
539
540   /**
541    * @brief Query whether cursor blink is enabled.
542    *
543    * @return Whether the cursor should blink or not.
544    */
545   bool GetEnableCursorBlink() const;
546
547   /**
548    * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
549    *
550    * @return The scroll position.
551    */
552   const Vector2& GetScrollPosition() const;
553
554   /**
555    * @brief Query the alignment offset.
556    *
557    * @return The alignmnet offset.
558    */
559   const Vector2& GetAlignmentOffset() const;
560
561   /**
562    * @copydoc Control::GetNaturalSize()
563    */
564   Vector3 GetNaturalSize();
565
566   /**
567    * @copydoc Control::GetHeightForWidth()
568    */
569   float GetHeightForWidth( float width );
570
571   /**
572    * @brief Triggers a relayout which updates View (if necessary).
573    *
574    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
575    * @param[in] size A the size of a bounding box to layout text within.
576    * @return True if the text model or decorations were updated.
577    */
578   bool Relayout( const Size& size );
579
580   /**
581    * @brief Process queued events which modify the model.
582    */
583   void ProcessModifyEvents();
584
585   /**
586    * @brief Used to remove placeholder text.
587    */
588   void ResetText();
589
590   /**
591    * @brief Used to reset the cursor position after setting a new text.
592    *
593    * @param[in] cursorIndex Where to place the cursor.
594    */
595   void ResetCursorPosition( CharacterIndex cursorIndex );
596
597   /**
598    * @brief Used to reset the scroll position after setting a new text.
599    */
600   void ResetScrollPosition();
601
602   /**
603    * @brief Used to process an event queued from SetText()
604    */
605   void TextReplacedEvent();
606
607   /**
608    * @brief Used to process an event queued from key events etc.
609    */
610   void TextInsertedEvent();
611
612   /**
613    * @brief Used to process an event queued from backspace key etc.
614    */
615   void TextDeletedEvent();
616
617   /**
618    * @brief Lays-out the text.
619    *
620    * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
621    *
622    * @param[in] size A the size of a bounding box to layout text within.
623    * @param[in] operations The layout operations which need to be done.
624    * @param[out] layoutSize The size of the laid-out text.
625    */
626   bool DoRelayout( const Size& size,
627                    OperationsMask operations,
628                    Size& layoutSize );
629
630   /**
631    * @brief Whether to enable the multi-line layout.
632    *
633    * @param[in] enable \e true enables the multi-line (by default)
634    */
635   void SetMultiLineEnabled( bool enable );
636
637   /**
638    * @return Whether the multi-line layout is enabled.
639    */
640   bool IsMultiLineEnabled() const;
641
642   /**
643    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
644    */
645   void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
646
647   /**
648    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
649    */
650   LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
651
652   /**
653    * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
654    */
655   void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
656
657   /**
658    * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
659    */
660   LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
661
662   /**
663    * @brief Calulates the alignment of the whole text inside the bounding box.
664    *
665    * @param[in] size The size of the bounding box.
666    */
667   void CalculateTextAlignment( const Size& size );
668
669   /**
670    * @brief Return the layout engine.
671    *
672    * @return A reference to the layout engine.
673    */
674   LayoutEngine& GetLayoutEngine();
675
676   /**
677    * @brief Return a view of the text.
678    *
679    * @return A reference to the view.
680    */
681   View& GetView();
682
683   // Text-input Event Queuing
684
685   /**
686    * @brief Called by editable UI controls when keyboard focus is gained.
687    */
688   void KeyboardFocusGainEvent();
689
690   /**
691    * @brief Called by editable UI controls when focus is lost.
692    */
693   void KeyboardFocusLostEvent();
694
695   /**
696    * @brief Called by editable UI controls when key events are received.
697    *
698    * @param[in] event The key event.
699    * @param[in] type Used to distinguish between regular key events and IMF events.
700    */
701   bool KeyEvent( const Dali::KeyEvent& event );
702
703   /**
704    * @brief Called by editable UI controls when key events are received.
705    *
706    * @param[in] text The text to insert.
707    * @param[in] type Used to distinguish between regular key events and IMF events.
708    */
709   void InsertText( const std::string& text, InsertType type );
710
711   /**
712    * @brief Checks if text is selected and if so removes it.
713    * @return true if text was removed
714    */
715   bool RemoveSelectedText();
716
717   /**
718    * @brief Called by editable UI controls when a tap gesture occurs.
719    * @param[in] tapCount The number of taps.
720    * @param[in] x The x position relative to the top-left of the parent control.
721    * @param[in] y The y position relative to the top-left of the parent control.
722    */
723   void TapEvent( unsigned int tapCount, float x, float y );
724
725   /**
726    * @brief Called by editable UI controls when a pan gesture occurs.
727    *
728    * @param[in] state The state of the gesture.
729    * @param[in] displacement This distance panned since the last pan gesture.
730    */
731   void PanEvent( Gesture::State state, const Vector2& displacement );
732
733   /**
734    * @brief Called by editable UI controls when a long press gesture occurs.
735    *
736    * @param[in] state The state of the gesture.
737    * @param[in] x The x position relative to the top-left of the parent control.
738    * @param[in] y The y position relative to the top-left of the parent control.
739    */
740   void LongPressEvent( Gesture::State state, float x, float y );
741
742   /**
743    * @brief Creates a selection event.
744    *
745    * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
746    *
747    * @param[in] x The x position relative to the top-left of the parent control.
748    * @param[in] y The y position relative to the top-left of the parent control.
749    * @param[in] selectAll Whether the whole text is selected.
750    */
751   void SelectEvent( float x, float y, bool selectAll );
752
753   /**
754    * @brief Event received from IMF manager
755    *
756    * @param[in] imfManager The IMF manager.
757    * @param[in] imfEvent The event received.
758    * @return A data struture indicating if update is needed, cursor position and current text.
759    */
760   ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
761
762   /**
763    * @brief Paste given string into Text model
764    * @param[in] stringToPaste this string will be inserted into the text model
765    */
766   void PasteText( const std::string& stringToPaste );
767
768   /**
769    * @brief Event from Clipboard notifying an Item has been selected for pasting
770    */
771   void PasteClipboardItemEvent();
772
773   /**
774    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
775    */
776   virtual void GetTargetSize( Vector2& targetSize );
777
778   /**
779    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
780    */
781   virtual void AddDecoration( Actor& actor, bool needsClipping );
782
783   /**
784    * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
785    */
786   virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
787
788   /**
789    * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
790    */
791   virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
792
793 protected:
794
795   /**
796    * @brief A reference counted object may only be deleted by calling Unreference().
797    */
798   virtual ~Controller();
799
800 private:
801
802   /**
803    * @brief Helper to KeyEvent() to handle the backspace case.
804    *
805    * @return True if a character was deleted.
806    */
807   bool BackspaceKeyEvent();
808
809   /**
810    * @brief Helper to notify IMF manager with surrounding text & cursor changes.
811    */
812   void NotifyImfManager();
813
814   /**
815    * @brief Helper to clear font-specific data.
816    */
817   void ShowPlaceholderText();
818
819   /**
820    * @brief Helper to clear font-specific data (only).
821    */
822   void ClearFontData();
823
824   /**
825    * @brief Helper to clear text's style data.
826    */
827   void ClearStyleData();
828
829   /**
830    * @brief Private constructor.
831    */
832   Controller( ControlInterface& controlInterface );
833
834   // Undefined
835   Controller( const Controller& handle );
836
837   // Undefined
838   Controller& operator=( const Controller& handle );
839
840 private:
841
842   struct Impl;
843   Impl* mImpl;
844 };
845
846 } // namespace Text
847
848 } // namespace Toolkit
849
850 } // namespace Dali
851
852 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__