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