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