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