1 #ifndef __DALI_TOOLKIT_TEXT_CONTROLLER_H__
2 #define __DALI_TOOLKIT_TEXT_CONTROLLER_H__
5 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/devel-api/adaptor-framework/imf-manager.h>
23 #include <dali/public-api/events/gesture.h>
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>
43 typedef IntrusivePtr<Controller> ControllerPtr;
44 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
47 * @brief Different placeholder-text can be shown when the control is active/inactive.
51 PLACEHOLDER_TYPE_ACTIVE,
52 PLACEHOLDER_TYPE_INACTIVE,
56 * @brief A Text Controller is used by UI Controls which display text.
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.
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.
64 * The text selection popup button callbacks are as well handled via the TextSelectionPopupCallbackInterface interface.
66 class Controller : public RefObject, public Decorator::ControllerInterface, public TextSelectionPopupCallbackInterface
71 * @brief Text related operations to be done in the relayout process.
75 NO_OPERATION = 0x0000,
76 CONVERT_TO_UTF32 = 0x0001,
78 VALIDATE_FONTS = 0x0004,
79 GET_LINE_BREAKS = 0x0008,
80 GET_WORD_BREAKS = 0x0010,
83 GET_GLYPH_METRICS = 0x0080,
85 UPDATE_ACTUAL_SIZE = 0x0200,
89 UPDATE_DIRECTION = 0x2000,
90 ALL_OPERATIONS = 0xFFFF
94 * @brief Used to distinguish between regular key events and IMF events
103 * @brief Used to specify whether to update the input style.
105 enum UpdateInputStyleType
108 DONT_UPDATE_INPUT_STYLE
112 * @brief Create a new instance of a Controller.
114 * @param[in] controlInterface An interface used to request a text relayout.
115 * @return A pointer to a new Controller.
117 static ControllerPtr New( ControlInterface& controlInterface );
120 * @brief Called to enable text input.
122 * @note Selectable or editable controls should call this once after Controller::New().
123 * @param[in] decorator Used to create cursor, selection handle decorations etc.
125 void EnableTextInput( DecoratorPtr decorator );
128 * @brief Used to switch between bitmap & vector based glyphs
130 * @param[in] glyphType The type of glyph; note that metrics for bitmap & vector based glyphs are different.
132 void SetGlyphType( TextAbstraction::GlyphType glyphType );
135 * @brief Enables/disables the mark-up processor.
137 * By default is disabled.
139 * @param[in] enable Whether to enable the mark-up processor.
141 void SetMarkupProcessorEnabled( bool enable );
144 * @brief Retrieves whether the mark-up processor is enabled.
146 * By default is disabled.
148 * @return @e true if the mark-up processor is enabled, otherwise returns @e false.
150 bool IsMarkupProcessorEnabled() const;
153 * @brief Enables/disables the auto text scrolling
155 * By default is disabled.
157 * @param[in] enable Whether to enable the auto scrolling
159 void SetAutoScrollEnabled( bool enable );
162 * @brief Retrieves whether auto text scrolling is enabled.
164 * By default is disabled.
166 * @return @e true if auto scrolling is enabled, otherwise returns @e false.
168 bool IsAutoScrollEnabled() const;
171 * @brief Get direction of the text from the first line of text,
172 * @return bool rtl (right to left) is true
174 CharacterDirection GetAutoScrollDirection() const;
177 * @brief Get the alignment offset of the first line of text.
179 * @return The alignment offset.
181 float GetAutoScrollLineAlignment() const;
184 * @brief Replaces any text previously set.
186 * @note This will be converted into UTF-32 when stored in the text model.
187 * @param[in] text A string of UTF-8 characters.
189 void SetText( const std::string& text );
192 * @brief Retrieve any text previously set.
194 * @return A string of UTF-8 characters.
196 void GetText( std::string& text ) const;
199 * @brief Remove a given number of characters
201 * When predictve text is used the pre-edit text is removed and inserted again with the new characters.
202 * The UpdateInputStyleType @type parameter if set to DONT_UPDATE_INPUT_STYLE avoids to update the input
203 * style when pre-edit text is removed.
205 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
206 * @param[in] numberOfCharacters The number of characters to delete from the cursorOffset.
207 * @param[in] type Whether to update the input style.
208 * @return True if the remove was successful.
210 bool RemoveText( int cursorOffset,
211 int numberOfCharacters,
212 UpdateInputStyleType type );
215 * @brief Retrieve the current cursor position.
217 * @return The cursor position.
219 unsigned int GetLogicalCursorPosition() const;
222 * @brief Replaces any placeholder text previously set.
224 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
225 * @param[in] text A string of UTF-8 characters.
227 void SetPlaceholderText( PlaceholderType type, const std::string& text );
230 * @brief Retrieve any placeholder text previously set.
232 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
233 * @param[out] A string of UTF-8 characters.
235 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
238 * @brief Sets the maximum number of characters that can be inserted into the TextModel
240 * @param[in] maxCharacters maximum number of characters to be accepted
242 void SetMaximumNumberOfCharacters( Length maxCharacters );
245 * @brief Sets the maximum number of characters that can be inserted into the TextModel
247 * @param[in] maxCharacters maximum number of characters to be accepted
249 int GetMaximumNumberOfCharacters();
252 * @brief Set the default font family.
254 * @param[in] defaultFontFamily The default font family.
256 void SetDefaultFontFamily( const std::string& defaultFontFamily );
259 * @brief Retrieve the default font family.
261 * @return The default font family.
263 const std::string& GetDefaultFontFamily() const;
266 * @brief Sets the font's style string.
268 * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
270 * @param[in] style The font's style string.
272 void SetDefaultFontStyle( const std::string& style );
275 * @brief Retrieves the font's style.
277 * @return The font's style.
279 const std::string& GetDefaultFontStyle() const;
282 * @brief Sets the default font weight.
284 * @param[in] weight The font weight.
286 void SetDefaultFontWeight( FontWeight weight );
289 * @brief Retrieves the default font weight.
291 * @return The default font weight.
293 FontWeight GetDefaultFontWeight() const;
296 * @brief Sets the default font width.
298 * @param[in] width The font width.
300 void SetDefaultFontWidth( FontWidth width );
303 * @brief Retrieves the default font width.
305 * @return The default font width.
307 FontWidth GetDefaultFontWidth() const;
310 * @brief Sets the default font slant.
312 * @param[in] slant The font slant.
314 void SetDefaultFontSlant( FontSlant slant );
317 * @brief Retrieves the default font slant.
319 * @return The default font slant.
321 FontSlant GetDefaultFontSlant() const;
324 * @brief Set the default point size.
326 * @param[in] pointSize The default point size.
328 void SetDefaultPointSize( float pointSize );
331 * @brief Retrieve the default point size.
333 * @return The default point size.
335 float GetDefaultPointSize() const;
338 * @ brief Update the text after a font change
339 * @param[in] newDefaultFont The new font to change to
341 void UpdateAfterFontChange( const std::string& newDefaultFont );
344 * @brief Set the text color
346 * @param textColor The text color
348 void SetTextColor( const Vector4& textColor );
351 * @brief Retrieve the text color
353 * @return The text color
355 const Vector4& GetTextColor() const;
358 * @brief Set the text color
360 * @param textColor The text color
362 void SetPlaceholderTextColor( const Vector4& textColor );
365 * @brief Retrieve the text color
367 * @return The text color
369 const Vector4& GetPlaceholderTextColor() const;
372 * @brief Set the shadow offset.
374 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
376 void SetShadowOffset( const Vector2& shadowOffset );
379 * @brief Retrieve the shadow offset.
381 * @return The shadow offset.
383 const Vector2& GetShadowOffset() const;
386 * @brief Set the shadow color.
388 * @param[in] shadowColor The shadow color.
390 void SetShadowColor( const Vector4& shadowColor );
393 * @brief Retrieve the shadow color.
395 * @return The shadow color.
397 const Vector4& GetShadowColor() const;
400 * @brief Sets the shadow's properties string.
402 * @note The string is stored to be recovered.
404 * @param[in] shadowProperties The shadow's properties string.
406 void SetDefaultShadowProperties( const std::string& shadowProperties );
409 * @brief Retrieves the shadow's properties string.
411 * @return The shadow's properties string.
413 const std::string& GetDefaultShadowProperties() const;
416 * @brief Set the underline color.
418 * @param[in] color color of underline.
420 void SetUnderlineColor( const Vector4& color );
423 * @brief Retrieve the underline color.
425 * @return The underline color.
427 const Vector4& GetUnderlineColor() const;
430 * @brief Set the underline enabled flag.
432 * @param[in] enabled The underline enabled flag.
434 void SetUnderlineEnabled( bool enabled );
437 * @brief Returns whether the text is underlined or not.
439 * @return The underline state.
441 bool IsUnderlineEnabled() const;
444 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
446 * @param[in] height The height in pixels of the underline
448 void SetUnderlineHeight( float height );
451 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
453 * @return The height of the underline, or 0 if height is not overrided.
455 float GetUnderlineHeight() const;
458 * @brief Sets the underline's properties string.
460 * @note The string is stored to be recovered.
462 * @param[in] underlineProperties The underline's properties string.
464 void SetDefaultUnderlineProperties( const std::string& underlineProperties );
467 * @brief Retrieves the underline's properties string.
469 * @return The underline's properties string.
471 const std::string& GetDefaultUnderlineProperties() const;
474 * @brief Sets the emboss's properties string.
476 * @note The string is stored to be recovered.
478 * @param[in] embossProperties The emboss's properties string.
480 void SetDefaultEmbossProperties( const std::string& embossProperties );
483 * @brief Retrieves the emboss's properties string.
485 * @return The emboss's properties string.
487 const std::string& GetDefaultEmbossProperties() const;
490 * @brief Sets the outline's properties string.
492 * @note The string is stored to be recovered.
494 * @param[in] outlineProperties The outline's properties string.
496 void SetDefaultOutlineProperties( const std::string& outlineProperties );
499 * @brief Retrieves the outline's properties string.
501 * @return The outline's properties string.
503 const std::string& GetDefaultOutlineProperties() const;
506 * @brief Sets the default line spacing.
508 * @param[in] lineSpacing The line spacing.
510 void SetDefaultLineSpacing( float lineSpacing );
513 * @brief Retrieves the default line spacing.
515 * @return The line spacing.
517 float GetDefaultLineSpacing() const;
520 * @brief Sets the input text's color.
522 * @param[in] color The input text's color.
524 void SetInputColor( const Vector4& color );
527 * @brief Retrieves the input text's color.
529 * @return The input text's color.
531 const Vector4& GetInputColor() const;
534 * @brief Sets the input text's font family name.
536 * @param[in] fontFamily The text's font family name.
538 void SetInputFontFamily( const std::string& fontFamily );
541 * @brief Retrieves the input text's font family name.
543 * @return The input text's font family name.
545 const std::string& GetInputFontFamily() const;
548 * @brief Sets the input text's font style.
550 * @param[in] fontStyle The input text's font style.
552 void SetInputFontStyle( const std::string& fontStyle );
555 * @brief Retrieves the input text's font style.
557 * @return The input text's font style.
559 const std::string& GetInputFontStyle() const;
562 * @brief Sets the input font's weight.
564 * @param[in] weight The input font's weight.
566 void SetInputFontWeight( FontWeight weight );
569 * @brief Retrieves the input font's weight.
571 * @return The input font's weight.
573 FontWeight GetInputFontWeight() const;
576 * @brief Sets the input font's width.
578 * @param[in] width The input font's width.
580 void SetInputFontWidth( FontWidth width );
583 * @brief Retrieves the input font's width.
585 * @return The input font's width.
587 FontWidth GetInputFontWidth() const;
590 * @brief Sets the input font's slant.
592 * @param[in] slant The input font's slant.
594 void SetInputFontSlant( FontSlant slant );
597 * @brief Retrieves the input font's slant.
599 * @return The input font's slant.
601 FontSlant GetInputFontSlant() const;
604 * @brief Sets the input font's point size.
606 * @param[in] size The input font's point size.
608 void SetInputFontPointSize( float size );
611 * @brief Retrieves the input font's point size.
613 * @return The input font's point size.
615 float GetInputFontPointSize() const;
618 * @brief Sets the input line spacing.
620 * @param[in] lineSpacing The line spacing.
622 void SetInputLineSpacing( float lineSpacing );
625 * @brief Retrieves the input line spacing.
627 * @return The line spacing.
629 float GetInputLineSpacing() const;
632 * @brief Sets the input shadow's properties string.
634 * @note The string is stored to be recovered.
636 * @param[in] shadowProperties The shadow's properties string.
638 void SetInputShadowProperties( const std::string& shadowProperties );
641 * @brief Retrieves the input shadow's properties string.
643 * @return The shadow's properties string.
645 const std::string& GetInputShadowProperties() const;
648 * @brief Sets the input underline's properties string.
650 * @note The string is stored to be recovered.
652 * @param[in] underlineProperties The underline's properties string.
654 void SetInputUnderlineProperties( const std::string& underlineProperties );
657 * @brief Retrieves the input underline's properties string.
659 * @return The underline's properties string.
661 const std::string& GetInputUnderlineProperties() const;
664 * @brief Sets the input emboss's properties string.
666 * @note The string is stored to be recovered.
668 * @param[in] embossProperties The emboss's properties string.
670 void SetInputEmbossProperties( const std::string& embossProperties );
673 * @brief Retrieves the input emboss's properties string.
675 * @return The emboss's properties string.
677 const std::string& GetInputEmbossProperties() const;
680 * @brief Sets input the outline's properties string.
682 * @note The string is stored to be recovered.
684 * @param[in] outlineProperties The outline's properties string.
686 void SetInputOutlineProperties( const std::string& outlineProperties );
689 * @brief Retrieves the input outline's properties string.
691 * @return The outline's properties string.
693 const std::string& GetInputOutlineProperties() const;
696 * @brief Called to enable/disable cursor blink.
698 * @note Only editable controls should calls this.
699 * @param[in] enabled Whether the cursor should blink or not.
701 void SetEnableCursorBlink( bool enable );
704 * @brief Query whether cursor blink is enabled.
706 * @return Whether the cursor should blink or not.
708 bool GetEnableCursorBlink() const;
711 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
713 * @return The scroll position.
715 const Vector2& GetScrollPosition() const;
718 * @copydoc Control::GetNaturalSize()
720 Vector3 GetNaturalSize();
723 * @copydoc Control::GetHeightForWidth()
725 float GetHeightForWidth( float width );
728 * @brief Triggers a relayout which updates View (if necessary).
730 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
731 * @param[in] size A the size of a bounding box to layout text within.
732 * @return True if the text model or decorations were updated.
734 bool Relayout( const Size& size );
737 * @brief Process queued events which modify the model.
739 void ProcessModifyEvents();
742 * @brief Used to remove placeholder text.
747 * @brief Used to reset the cursor position after setting a new text.
749 * @param[in] cursorIndex Where to place the cursor.
751 void ResetCursorPosition( CharacterIndex cursorIndex );
754 * @brief Used to reset the scroll position after setting a new text.
756 void ResetScrollPosition();
759 * @brief Used to process an event queued from SetText()
761 void TextReplacedEvent();
764 * @brief Used to process an event queued from key events etc.
766 void TextInsertedEvent();
769 * @brief Used to process an event queued from backspace key etc.
771 void TextDeletedEvent();
774 * @brief Lays-out the text.
776 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
778 * @param[in] size A the size of a bounding box to layout text within.
779 * @param[in] operations The layout operations which need to be done.
780 * @param[out] layoutSize The size of the laid-out text.
782 bool DoRelayout( const Size& size,
783 OperationsMask operations,
787 * @brief Whether to enable the multi-line layout.
789 * @param[in] enable \e true enables the multi-line (by default)
791 void SetMultiLineEnabled( bool enable );
794 * @return Whether the multi-line layout is enabled.
796 bool IsMultiLineEnabled() const;
799 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
801 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
804 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
806 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
809 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
811 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
814 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
816 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
819 * @brief Calulates the vertical offset to align the text inside the bounding box.
821 * @param[in] size The size of the bounding box.
823 void CalculateVerticalOffset( const Size& size );
826 * @brief Return the layout engine.
828 * @return A reference to the layout engine.
830 LayoutEngine& GetLayoutEngine();
833 * @brief Return a view of the text.
835 * @return A reference to the view.
839 // Text-input Event Queuing
842 * @brief Called by editable UI controls when keyboard focus is gained.
844 void KeyboardFocusGainEvent();
847 * @brief Called by editable UI controls when focus is lost.
849 void KeyboardFocusLostEvent();
852 * @brief Called by editable UI controls when key events are received.
854 * @param[in] event The key event.
855 * @param[in] type Used to distinguish between regular key events and IMF events.
857 bool KeyEvent( const Dali::KeyEvent& event );
860 * @brief Called by editable UI controls when key events are received.
862 * @param[in] text The text to insert.
863 * @param[in] type Used to distinguish between regular key events and IMF events.
865 void InsertText( const std::string& text, InsertType type );
868 * @brief Checks if text is selected and if so removes it.
869 * @return true if text was removed
871 bool RemoveSelectedText();
874 * @brief Called by editable UI controls when a tap gesture occurs.
875 * @param[in] tapCount The number of taps.
876 * @param[in] x The x position relative to the top-left of the parent control.
877 * @param[in] y The y position relative to the top-left of the parent control.
879 void TapEvent( unsigned int tapCount, float x, float y );
882 * @brief Called by editable UI controls when a pan gesture occurs.
884 * @param[in] state The state of the gesture.
885 * @param[in] displacement This distance panned since the last pan gesture.
887 void PanEvent( Gesture::State state, const Vector2& displacement );
890 * @brief Called by editable UI controls when a long press gesture occurs.
892 * @param[in] state The state of the gesture.
893 * @param[in] x The x position relative to the top-left of the parent control.
894 * @param[in] y The y position relative to the top-left of the parent control.
896 void LongPressEvent( Gesture::State state, float x, float y );
899 * @brief Creates a selection event.
901 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
903 * @param[in] x The x position relative to the top-left of the parent control.
904 * @param[in] y The y position relative to the top-left of the parent control.
905 * @param[in] selectAll Whether the whole text is selected.
907 void SelectEvent( float x, float y, bool selectAll );
910 * @brief Event received from IMF manager
912 * @param[in] imfManager The IMF manager.
913 * @param[in] imfEvent The event received.
914 * @return A data struture indicating if update is needed, cursor position and current text.
916 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
919 * @brief Paste given string into Text model
920 * @param[in] stringToPaste this string will be inserted into the text model
922 void PasteText( const std::string& stringToPaste );
925 * @brief Event from Clipboard notifying an Item has been selected for pasting
927 void PasteClipboardItemEvent();
930 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
932 virtual void GetTargetSize( Vector2& targetSize );
935 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
937 virtual void AddDecoration( Actor& actor, bool needsClipping );
940 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
942 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
945 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
947 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
952 * @brief A reference counted object may only be deleted by calling Unreference().
954 virtual ~Controller();
959 * @brief Helper to KeyEvent() to handle the backspace case.
961 * @return True if a character was deleted.
963 bool BackspaceKeyEvent();
966 * @brief Helper to notify IMF manager with surrounding text & cursor changes.
968 void NotifyImfManager();
971 * @brief Helper to clear font-specific data.
973 void ShowPlaceholderText();
976 * @brief Helper to clear font-specific data (only).
978 void ClearFontData();
981 * @brief Helper to clear text's style data.
983 void ClearStyleData();
986 * @brief Private constructor.
988 Controller( ControlInterface& controlInterface );
991 Controller( const Controller& handle );
994 Controller& operator=( const Controller& handle );
1004 } // namespace Toolkit
1008 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__