1 #ifndef __DALI_TOOLKIT_TEXT_CONTROLLER_H__
2 #define __DALI_TOOLKIT_TEXT_CONTROLLER_H__
5 * Copyright (c) 2016 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_LAYOUT_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
115 DECORATOR_UPDATED = 0x2
119 * @brief Create a new instance of a Controller.
121 * @param[in] controlInterface An interface used to request a text relayout.
122 * @return A pointer to a new Controller.
124 static ControllerPtr New( ControlInterface& controlInterface );
127 * @brief Called to enable text input.
129 * @note Selectable or editable controls should call this once after Controller::New().
130 * @param[in] decorator Used to create cursor, selection handle decorations etc.
132 void EnableTextInput( DecoratorPtr decorator );
135 * @brief Used to switch between bitmap & vector based glyphs
137 * @param[in] glyphType The type of glyph; note that metrics for bitmap & vector based glyphs are different.
139 void SetGlyphType( TextAbstraction::GlyphType glyphType );
142 * @brief Enables/disables the mark-up processor.
144 * By default is disabled.
146 * @param[in] enable Whether to enable the mark-up processor.
148 void SetMarkupProcessorEnabled( bool enable );
151 * @brief Retrieves whether the mark-up processor is enabled.
153 * By default is disabled.
155 * @return @e true if the mark-up processor is enabled, otherwise returns @e false.
157 bool IsMarkupProcessorEnabled() const;
160 * @brief Enables/disables the auto text scrolling
162 * By default is disabled.
164 * @param[in] enable Whether to enable the auto scrolling
166 void SetAutoScrollEnabled( bool enable );
169 * @brief Retrieves whether auto text scrolling is enabled.
171 * By default is disabled.
173 * @return @e true if auto scrolling is enabled, otherwise returns @e false.
175 bool IsAutoScrollEnabled() const;
178 * @brief Get direction of the text from the first line of text,
179 * @return bool rtl (right to left) is true
181 CharacterDirection GetAutoScrollDirection() const;
184 * @brief Get the alignment offset of the first line of text.
186 * @return The alignment offset.
188 float GetAutoScrollLineAlignment() const;
191 * @brief Enables the horizontal scrolling.
193 * @param[in] enable Whether to enable the horizontal scrolling.
195 void SetHorizontalScrollEnabled( bool enable );
198 * @brief Retrieves whether the horizontal scrolling is enabled.
200 * @return @e true if the horizontal scrolling is enabled, otherwise it returns @e false.
202 bool IsHorizontalScrollEnabled() const;
205 * @brief Enables the vertical scrolling.
207 * @param[in] enable Whether to enable the vertical scrolling.
209 void SetVerticalScrollEnabled( bool enable );
212 * @brief Retrieves whether the verticall scrolling is enabled.
214 * @return @e true if the vertical scrolling is enabled, otherwise it returns @e false.
216 bool IsVerticalScrollEnabled() const;
219 * @brief Enables the smooth handle panning.
221 * @param[in] enable Whether to enable the smooth handle panning.
223 void SetSmoothHandlePanEnabled( bool enable );
226 * @brief Retrieves whether the smooth handle panning is enabled.
228 * @return @e true if the smooth handle panning is enabled.
230 bool IsSmoothHandlePanEnabled() const;
233 * @brief Replaces any text previously set.
235 * @note This will be converted into UTF-32 when stored in the text model.
236 * @param[in] text A string of UTF-8 characters.
238 void SetText( const std::string& text );
241 * @brief Retrieve any text previously set.
243 * @param[out] text A string of UTF-8 characters.
245 void GetText( std::string& text ) const;
248 * @brief Remove a given number of characters
250 * When predictve text is used the pre-edit text is removed and inserted again with the new characters.
251 * The UpdateInputStyleType @type parameter if set to DONT_UPDATE_INPUT_STYLE avoids to update the input
252 * style when pre-edit text is removed.
254 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
255 * @param[in] numberOfCharacters The number of characters to delete from the cursorOffset.
256 * @param[in] type Whether to update the input style.
257 * @return True if the remove was successful.
259 bool RemoveText( int cursorOffset,
260 int numberOfCharacters,
261 UpdateInputStyleType type );
264 * @brief Replaces any placeholder text previously set.
266 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
267 * @param[in] text A string of UTF-8 characters.
269 void SetPlaceholderText( PlaceholderType type, const std::string& text );
272 * @brief Retrieve any placeholder text previously set.
274 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
275 * @param[out] A string of UTF-8 characters.
277 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
280 * @brief Sets the maximum number of characters that can be inserted into the TextModel
282 * @param[in] maxCharacters maximum number of characters to be accepted
284 void SetMaximumNumberOfCharacters( Length maxCharacters );
287 * @brief Sets the maximum number of characters that can be inserted into the TextModel
289 * @param[in] maxCharacters maximum number of characters to be accepted
291 int GetMaximumNumberOfCharacters();
294 * @brief Set the default font family.
296 * @param[in] defaultFontFamily The default font family.
298 void SetDefaultFontFamily( const std::string& defaultFontFamily );
301 * @brief Retrieve the default font family.
303 * @return The default font family.
305 const std::string& GetDefaultFontFamily() const;
308 * @brief Sets the default font weight.
310 * @param[in] weight The font weight.
312 void SetDefaultFontWeight( FontWeight weight );
315 * @brief Whether the font's weight has been defined.
317 bool IsDefaultFontWeightDefined() const;
320 * @brief Retrieves the default font weight.
322 * @return The default font weight.
324 FontWeight GetDefaultFontWeight() const;
327 * @brief Sets the default font width.
329 * @param[in] width The font width.
331 void SetDefaultFontWidth( FontWidth width );
334 * @brief Whether the font's width has been defined.
336 bool IsDefaultFontWidthDefined() const;
339 * @brief Retrieves the default font width.
341 * @return The default font width.
343 FontWidth GetDefaultFontWidth() const;
346 * @brief Sets the default font slant.
348 * @param[in] slant The font slant.
350 void SetDefaultFontSlant( FontSlant slant );
353 * @brief Whether the font's slant has been defined.
355 bool IsDefaultFontSlantDefined() const;
358 * @brief Retrieves the default font slant.
360 * @return The default font slant.
362 FontSlant GetDefaultFontSlant() const;
365 * @brief Set the default point size.
367 * @param[in] pointSize The default point size.
369 void SetDefaultPointSize( float pointSize );
372 * @brief Retrieve the default point size.
374 * @return The default point size.
376 float GetDefaultPointSize() const;
379 * @ brief Update the text after a font change
380 * @param[in] newDefaultFont The new font to change to
382 void UpdateAfterFontChange( const std::string& newDefaultFont );
385 * @brief Set the text color
387 * @param textColor The text color
389 void SetTextColor( const Vector4& textColor );
392 * @brief Retrieve the text color
394 * @return The text color
396 const Vector4& GetTextColor() const;
399 * @brief Set the text color
401 * @param textColor The text color
403 void SetPlaceholderTextColor( const Vector4& textColor );
406 * @brief Retrieve the text color
408 * @return The text color
410 const Vector4& GetPlaceholderTextColor() const;
413 * @brief Set the shadow offset.
415 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
417 void SetShadowOffset( const Vector2& shadowOffset );
420 * @brief Retrieve the shadow offset.
422 * @return The shadow offset.
424 const Vector2& GetShadowOffset() const;
427 * @brief Set the shadow color.
429 * @param[in] shadowColor The shadow color.
431 void SetShadowColor( const Vector4& shadowColor );
434 * @brief Retrieve the shadow color.
436 * @return The shadow color.
438 const Vector4& GetShadowColor() const;
441 * @brief Sets the shadow's properties string.
443 * @note The string is stored to be recovered.
445 * @param[in] shadowProperties The shadow's properties string.
447 void SetDefaultShadowProperties( const std::string& shadowProperties );
450 * @brief Retrieves the shadow's properties string.
452 * @return The shadow's properties string.
454 const std::string& GetDefaultShadowProperties() const;
457 * @brief Set the underline color.
459 * @param[in] color color of underline.
461 void SetUnderlineColor( const Vector4& color );
464 * @brief Retrieve the underline color.
466 * @return The underline color.
468 const Vector4& GetUnderlineColor() const;
471 * @brief Set the underline enabled flag.
473 * @param[in] enabled The underline enabled flag.
475 void SetUnderlineEnabled( bool enabled );
478 * @brief Returns whether the text is underlined or not.
480 * @return The underline state.
482 bool IsUnderlineEnabled() const;
485 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
487 * @param[in] height The height in pixels of the underline
489 void SetUnderlineHeight( float height );
492 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
494 * @return The height of the underline, or 0 if height is not overrided.
496 float GetUnderlineHeight() const;
499 * @brief Sets the underline's properties string.
501 * @note The string is stored to be recovered.
503 * @param[in] underlineProperties The underline's properties string.
505 void SetDefaultUnderlineProperties( const std::string& underlineProperties );
508 * @brief Retrieves the underline's properties string.
510 * @return The underline's properties string.
512 const std::string& GetDefaultUnderlineProperties() const;
515 * @brief Sets the emboss's properties string.
517 * @note The string is stored to be recovered.
519 * @param[in] embossProperties The emboss's properties string.
521 void SetDefaultEmbossProperties( const std::string& embossProperties );
524 * @brief Retrieves the emboss's properties string.
526 * @return The emboss's properties string.
528 const std::string& GetDefaultEmbossProperties() const;
531 * @brief Sets the outline's properties string.
533 * @note The string is stored to be recovered.
535 * @param[in] outlineProperties The outline's properties string.
537 void SetDefaultOutlineProperties( const std::string& outlineProperties );
540 * @brief Retrieves the outline's properties string.
542 * @return The outline's properties string.
544 const std::string& GetDefaultOutlineProperties() const;
547 * @brief Sets the default line spacing.
549 * @param[in] lineSpacing The line spacing.
551 void SetDefaultLineSpacing( float lineSpacing );
554 * @brief Retrieves the default line spacing.
556 * @return The line spacing.
558 float GetDefaultLineSpacing() const;
561 * @brief Sets the input text's color.
563 * @param[in] color The input text's color.
565 void SetInputColor( const Vector4& color );
568 * @brief Retrieves the input text's color.
570 * @return The input text's color.
572 const Vector4& GetInputColor() const;
575 * @brief Sets the input text's font family name.
577 * @param[in] fontFamily The text's font family name.
579 void SetInputFontFamily( const std::string& fontFamily );
582 * @brief Retrieves the input text's font family name.
584 * @return The input text's font family name.
586 const std::string& GetInputFontFamily() const;
589 * @brief Sets the input font's weight.
591 * @param[in] weight The input font's weight.
593 void SetInputFontWeight( FontWeight weight );
596 * @return Whether the font's weight has been defined.
598 bool IsInputFontWeightDefined() const;
601 * @brief Retrieves the input font's weight.
603 * @return The input font's weight.
605 FontWeight GetInputFontWeight() const;
608 * @brief Sets the input font's width.
610 * @param[in] width The input font's width.
612 void SetInputFontWidth( FontWidth width );
615 * @return Whether the font's width has been defined.
617 bool IsInputFontWidthDefined() const;
620 * @brief Retrieves the input font's width.
622 * @return The input font's width.
624 FontWidth GetInputFontWidth() const;
627 * @brief Sets the input font's slant.
629 * @param[in] slant The input font's slant.
631 void SetInputFontSlant( FontSlant slant );
634 * @return Whether the font's slant has been defined.
636 bool IsInputFontSlantDefined() const;
639 * @brief Retrieves the input font's slant.
641 * @return The input font's slant.
643 FontSlant GetInputFontSlant() const;
646 * @brief Sets the input font's point size.
648 * @param[in] size The input font's point size.
650 void SetInputFontPointSize( float size );
653 * @brief Retrieves the input font's point size.
655 * @return The input font's point size.
657 float GetInputFontPointSize() const;
660 * @brief Sets the input line spacing.
662 * @param[in] lineSpacing The line spacing.
664 void SetInputLineSpacing( float lineSpacing );
667 * @brief Retrieves the input line spacing.
669 * @return The line spacing.
671 float GetInputLineSpacing() const;
674 * @brief Sets the input shadow's properties string.
676 * @note The string is stored to be recovered.
678 * @param[in] shadowProperties The shadow's properties string.
680 void SetInputShadowProperties( const std::string& shadowProperties );
683 * @brief Retrieves the input shadow's properties string.
685 * @return The shadow's properties string.
687 const std::string& GetInputShadowProperties() const;
690 * @brief Sets the input underline's properties string.
692 * @note The string is stored to be recovered.
694 * @param[in] underlineProperties The underline's properties string.
696 void SetInputUnderlineProperties( const std::string& underlineProperties );
699 * @brief Retrieves the input underline's properties string.
701 * @return The underline's properties string.
703 const std::string& GetInputUnderlineProperties() const;
706 * @brief Sets the input emboss's properties string.
708 * @note The string is stored to be recovered.
710 * @param[in] embossProperties The emboss's properties string.
712 void SetInputEmbossProperties( const std::string& embossProperties );
715 * @brief Retrieves the input emboss's properties string.
717 * @return The emboss's properties string.
719 const std::string& GetInputEmbossProperties() const;
722 * @brief Sets input the outline's properties string.
724 * @note The string is stored to be recovered.
726 * @param[in] outlineProperties The outline's properties string.
728 void SetInputOutlineProperties( const std::string& outlineProperties );
731 * @brief Retrieves the input outline's properties string.
733 * @return The outline's properties string.
735 const std::string& GetInputOutlineProperties() const;
738 * @brief Called to enable/disable cursor blink.
740 * @note Only editable controls should calls this.
741 * @param[in] enabled Whether the cursor should blink or not.
743 void SetEnableCursorBlink( bool enable );
746 * @brief Query whether cursor blink is enabled.
748 * @return Whether the cursor should blink or not.
750 bool GetEnableCursorBlink() const;
753 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
755 * @return The scroll position.
757 const Vector2& GetScrollPosition() const;
760 * @copydoc Control::GetNaturalSize()
762 Vector3 GetNaturalSize();
765 * @copydoc Control::GetHeightForWidth()
767 float GetHeightForWidth( float width );
770 * @brief Triggers a relayout which updates View (if necessary).
772 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
773 * @param[in] size A the size of a bounding box to layout text within.
775 * @return Whether the text model or decorations were updated.
777 UpdateTextType Relayout( const Size& size );
780 * @brief Process queued events which modify the model.
782 void ProcessModifyEvents();
785 * @brief Used to remove placeholder text.
790 * @brief Used to reset the cursor position after setting a new text.
792 * @param[in] cursorIndex Where to place the cursor.
794 void ResetCursorPosition( CharacterIndex cursorIndex );
797 * @brief Used to reset the scroll position after setting a new text.
799 void ResetScrollPosition();
802 * @brief Used to process an event queued from SetText()
804 void TextReplacedEvent();
807 * @brief Used to process an event queued from key events etc.
809 void TextInsertedEvent();
812 * @brief Used to process an event queued from backspace key etc.
814 void TextDeletedEvent();
817 * @brief Lays-out the text.
819 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
821 * @param[in] size A the size of a bounding box to layout text within.
822 * @param[in] operations The layout operations which need to be done.
823 * @param[out] layoutSize The size of the laid-out text.
825 bool DoRelayout( const Size& size,
826 OperationsMask operations,
830 * @brief Whether to enable the multi-line layout.
832 * @param[in] enable \e true enables the multi-line (by default)
834 void SetMultiLineEnabled( bool enable );
837 * @return Whether the multi-line layout is enabled.
839 bool IsMultiLineEnabled() const;
842 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
844 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
847 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
849 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
852 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
854 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
857 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
859 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
862 * @brief Calulates the vertical offset to align the text inside the bounding box.
864 * @param[in] size The size of the bounding box.
866 void CalculateVerticalOffset( const Size& size );
869 * @brief Return the layout engine.
871 * @return A reference to the layout engine.
873 LayoutEngine& GetLayoutEngine();
876 * @brief Return a view of the text.
878 * @return A reference to the view.
882 // Text-input Event Queuing
885 * @brief Called by editable UI controls when keyboard focus is gained.
887 void KeyboardFocusGainEvent();
890 * @brief Called by editable UI controls when focus is lost.
892 void KeyboardFocusLostEvent();
895 * @brief Called by editable UI controls when key events are received.
897 * @param[in] event The key event.
898 * @param[in] type Used to distinguish between regular key events and IMF events.
900 bool KeyEvent( const Dali::KeyEvent& event );
903 * @brief Called by editable UI controls when key events are received.
905 * @param[in] text The text to insert.
906 * @param[in] type Used to distinguish between regular key events and IMF events.
908 void InsertText( const std::string& text, InsertType type );
911 * @brief Checks if text is selected and if so removes it.
912 * @return true if text was removed
914 bool RemoveSelectedText();
917 * @brief Called by editable UI controls when a tap gesture occurs.
918 * @param[in] tapCount The number of taps.
919 * @param[in] x The x position relative to the top-left of the parent control.
920 * @param[in] y The y position relative to the top-left of the parent control.
922 void TapEvent( unsigned int tapCount, float x, float y );
925 * @brief Called by editable UI controls when a pan gesture occurs.
927 * @param[in] state The state of the gesture.
928 * @param[in] displacement This distance panned since the last pan gesture.
930 void PanEvent( Gesture::State state, const Vector2& displacement );
933 * @brief Called by editable UI controls when a long press gesture occurs.
935 * @param[in] state The state of the gesture.
936 * @param[in] x The x position relative to the top-left of the parent control.
937 * @param[in] y The y position relative to the top-left of the parent control.
939 void LongPressEvent( Gesture::State state, float x, float y );
942 * @brief Creates a selection event.
944 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
946 * @param[in] x The x position relative to the top-left of the parent control.
947 * @param[in] y The y position relative to the top-left of the parent control.
948 * @param[in] selectAll Whether the whole text is selected.
950 void SelectEvent( float x, float y, bool selectAll );
953 * @brief Event received from IMF manager
955 * @param[in] imfManager The IMF manager.
956 * @param[in] imfEvent The event received.
957 * @return A data struture indicating if update is needed, cursor position and current text.
959 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
962 * @brief Paste given string into Text model
963 * @param[in] stringToPaste this string will be inserted into the text model
965 void PasteText( const std::string& stringToPaste );
968 * @brief Event from Clipboard notifying an Item has been selected for pasting
970 void PasteClipboardItemEvent();
973 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
975 virtual void GetTargetSize( Vector2& targetSize );
978 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
980 virtual void AddDecoration( Actor& actor, bool needsClipping );
983 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
985 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
988 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
990 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
995 * @brief A reference counted object may only be deleted by calling Unreference().
997 virtual ~Controller();
1002 * @brief Helper to KeyEvent() to handle the backspace case.
1004 * @return True if a character was deleted.
1006 bool BackspaceKeyEvent();
1009 * @brief Helper to clear font-specific data.
1011 void ShowPlaceholderText();
1014 * @brief Helper to clear font-specific data (only).
1016 void ClearFontData();
1019 * @brief Helper to clear text's style data.
1021 void ClearStyleData();
1024 * @brief Private constructor.
1026 Controller( ControlInterface& controlInterface );
1029 Controller( const Controller& handle );
1032 Controller& operator=( const Controller& handle );
1042 } // namespace Toolkit
1046 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__