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