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 * @param[out] text 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 Replaces any placeholder text previously set.
217 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
218 * @param[in] text A string of UTF-8 characters.
220 void SetPlaceholderText( PlaceholderType type, const std::string& text );
223 * @brief Retrieve any placeholder text previously set.
225 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
226 * @param[out] A string of UTF-8 characters.
228 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
231 * @brief Sets the maximum number of characters that can be inserted into the TextModel
233 * @param[in] maxCharacters maximum number of characters to be accepted
235 void SetMaximumNumberOfCharacters( Length maxCharacters );
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 int GetMaximumNumberOfCharacters();
245 * @brief Set the default font family.
247 * @param[in] defaultFontFamily The default font family.
249 void SetDefaultFontFamily( const std::string& defaultFontFamily );
252 * @brief Retrieve the default font family.
254 * @return The default font family.
256 const std::string& GetDefaultFontFamily() const;
259 * @brief Sets the font's style string.
261 * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
263 * @param[in] style The font's style string.
265 void SetDefaultFontStyle( const std::string& style );
268 * @brief Retrieves the font's style.
270 * @return The font's style.
272 const std::string& GetDefaultFontStyle() const;
275 * @brief Sets the default font weight.
277 * @param[in] weight The font weight.
279 void SetDefaultFontWeight( FontWeight weight );
282 * @brief Retrieves the default font weight.
284 * @return The default font weight.
286 FontWeight GetDefaultFontWeight() const;
289 * @brief Sets the default font width.
291 * @param[in] width The font width.
293 void SetDefaultFontWidth( FontWidth width );
296 * @brief Retrieves the default font width.
298 * @return The default font width.
300 FontWidth GetDefaultFontWidth() const;
303 * @brief Sets the default font slant.
305 * @param[in] slant The font slant.
307 void SetDefaultFontSlant( FontSlant slant );
310 * @brief Retrieves the default font slant.
312 * @return The default font slant.
314 FontSlant GetDefaultFontSlant() const;
317 * @brief Set the default point size.
319 * @param[in] pointSize The default point size.
321 void SetDefaultPointSize( float pointSize );
324 * @brief Retrieve the default point size.
326 * @return The default point size.
328 float GetDefaultPointSize() const;
331 * @ brief Update the text after a font change
332 * @param[in] newDefaultFont The new font to change to
334 void UpdateAfterFontChange( const std::string& newDefaultFont );
337 * @brief Set the text color
339 * @param textColor The text color
341 void SetTextColor( const Vector4& textColor );
344 * @brief Retrieve the text color
346 * @return The text color
348 const Vector4& GetTextColor() const;
351 * @brief Set the text color
353 * @param textColor The text color
355 void SetPlaceholderTextColor( const Vector4& textColor );
358 * @brief Retrieve the text color
360 * @return The text color
362 const Vector4& GetPlaceholderTextColor() const;
365 * @brief Set the shadow offset.
367 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
369 void SetShadowOffset( const Vector2& shadowOffset );
372 * @brief Retrieve the shadow offset.
374 * @return The shadow offset.
376 const Vector2& GetShadowOffset() const;
379 * @brief Set the shadow color.
381 * @param[in] shadowColor The shadow color.
383 void SetShadowColor( const Vector4& shadowColor );
386 * @brief Retrieve the shadow color.
388 * @return The shadow color.
390 const Vector4& GetShadowColor() const;
393 * @brief Sets the shadow's properties string.
395 * @note The string is stored to be recovered.
397 * @param[in] shadowProperties The shadow's properties string.
399 void SetDefaultShadowProperties( const std::string& shadowProperties );
402 * @brief Retrieves the shadow's properties string.
404 * @return The shadow's properties string.
406 const std::string& GetDefaultShadowProperties() const;
409 * @brief Set the underline color.
411 * @param[in] color color of underline.
413 void SetUnderlineColor( const Vector4& color );
416 * @brief Retrieve the underline color.
418 * @return The underline color.
420 const Vector4& GetUnderlineColor() const;
423 * @brief Set the underline enabled flag.
425 * @param[in] enabled The underline enabled flag.
427 void SetUnderlineEnabled( bool enabled );
430 * @brief Returns whether the text is underlined or not.
432 * @return The underline state.
434 bool IsUnderlineEnabled() const;
437 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
439 * @param[in] height The height in pixels of the underline
441 void SetUnderlineHeight( float height );
444 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
446 * @return The height of the underline, or 0 if height is not overrided.
448 float GetUnderlineHeight() const;
451 * @brief Sets the underline's properties string.
453 * @note The string is stored to be recovered.
455 * @param[in] underlineProperties The underline's properties string.
457 void SetDefaultUnderlineProperties( const std::string& underlineProperties );
460 * @brief Retrieves the underline's properties string.
462 * @return The underline's properties string.
464 const std::string& GetDefaultUnderlineProperties() const;
467 * @brief Sets the emboss's properties string.
469 * @note The string is stored to be recovered.
471 * @param[in] embossProperties The emboss's properties string.
473 void SetDefaultEmbossProperties( const std::string& embossProperties );
476 * @brief Retrieves the emboss's properties string.
478 * @return The emboss's properties string.
480 const std::string& GetDefaultEmbossProperties() const;
483 * @brief Sets the outline's properties string.
485 * @note The string is stored to be recovered.
487 * @param[in] outlineProperties The outline's properties string.
489 void SetDefaultOutlineProperties( const std::string& outlineProperties );
492 * @brief Retrieves the outline's properties string.
494 * @return The outline's properties string.
496 const std::string& GetDefaultOutlineProperties() const;
499 * @brief Sets the default line spacing.
501 * @param[in] lineSpacing The line spacing.
503 void SetDefaultLineSpacing( float lineSpacing );
506 * @brief Retrieves the default line spacing.
508 * @return The line spacing.
510 float GetDefaultLineSpacing() const;
513 * @brief Sets the input text's color.
515 * @param[in] color The input text's color.
517 void SetInputColor( const Vector4& color );
520 * @brief Retrieves the input text's color.
522 * @return The input text's color.
524 const Vector4& GetInputColor() const;
527 * @brief Sets the input text's font family name.
529 * @param[in] fontFamily The text's font family name.
531 void SetInputFontFamily( const std::string& fontFamily );
534 * @brief Retrieves the input text's font family name.
536 * @return The input text's font family name.
538 const std::string& GetInputFontFamily() const;
541 * @brief Sets the input text's font style.
543 * @param[in] fontStyle The input text's font style.
545 void SetInputFontStyle( const std::string& fontStyle );
548 * @brief Retrieves the input text's font style.
550 * @return The input text's font style.
552 const std::string& GetInputFontStyle() const;
555 * @brief Sets the input font's weight.
557 * @param[in] weight The input font's weight.
559 void SetInputFontWeight( FontWeight weight );
562 * @brief Retrieves the input font's weight.
564 * @return The input font's weight.
566 FontWeight GetInputFontWeight() const;
569 * @brief Sets the input font's width.
571 * @param[in] width The input font's width.
573 void SetInputFontWidth( FontWidth width );
576 * @brief Retrieves the input font's width.
578 * @return The input font's width.
580 FontWidth GetInputFontWidth() const;
583 * @brief Sets the input font's slant.
585 * @param[in] slant The input font's slant.
587 void SetInputFontSlant( FontSlant slant );
590 * @brief Retrieves the input font's slant.
592 * @return The input font's slant.
594 FontSlant GetInputFontSlant() const;
597 * @brief Sets the input font's point size.
599 * @param[in] size The input font's point size.
601 void SetInputFontPointSize( float size );
604 * @brief Retrieves the input font's point size.
606 * @return The input font's point size.
608 float GetInputFontPointSize() const;
611 * @brief Sets the input line spacing.
613 * @param[in] lineSpacing The line spacing.
615 void SetInputLineSpacing( float lineSpacing );
618 * @brief Retrieves the input line spacing.
620 * @return The line spacing.
622 float GetInputLineSpacing() const;
625 * @brief Sets the input shadow's properties string.
627 * @note The string is stored to be recovered.
629 * @param[in] shadowProperties The shadow's properties string.
631 void SetInputShadowProperties( const std::string& shadowProperties );
634 * @brief Retrieves the input shadow's properties string.
636 * @return The shadow's properties string.
638 const std::string& GetInputShadowProperties() const;
641 * @brief Sets the input underline's properties string.
643 * @note The string is stored to be recovered.
645 * @param[in] underlineProperties The underline's properties string.
647 void SetInputUnderlineProperties( const std::string& underlineProperties );
650 * @brief Retrieves the input underline's properties string.
652 * @return The underline's properties string.
654 const std::string& GetInputUnderlineProperties() const;
657 * @brief Sets the input emboss's properties string.
659 * @note The string is stored to be recovered.
661 * @param[in] embossProperties The emboss's properties string.
663 void SetInputEmbossProperties( const std::string& embossProperties );
666 * @brief Retrieves the input emboss's properties string.
668 * @return The emboss's properties string.
670 const std::string& GetInputEmbossProperties() const;
673 * @brief Sets input the outline's properties string.
675 * @note The string is stored to be recovered.
677 * @param[in] outlineProperties The outline's properties string.
679 void SetInputOutlineProperties( const std::string& outlineProperties );
682 * @brief Retrieves the input outline's properties string.
684 * @return The outline's properties string.
686 const std::string& GetInputOutlineProperties() const;
689 * @brief Called to enable/disable cursor blink.
691 * @note Only editable controls should calls this.
692 * @param[in] enabled Whether the cursor should blink or not.
694 void SetEnableCursorBlink( bool enable );
697 * @brief Query whether cursor blink is enabled.
699 * @return Whether the cursor should blink or not.
701 bool GetEnableCursorBlink() const;
704 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
706 * @return The scroll position.
708 const Vector2& GetScrollPosition() const;
711 * @copydoc Control::GetNaturalSize()
713 Vector3 GetNaturalSize();
716 * @copydoc Control::GetHeightForWidth()
718 float GetHeightForWidth( float width );
721 * @brief Triggers a relayout which updates View (if necessary).
723 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
724 * @param[in] size A the size of a bounding box to layout text within.
725 * @return True if the text model or decorations were updated.
727 bool Relayout( const Size& size );
730 * @brief Process queued events which modify the model.
732 void ProcessModifyEvents();
735 * @brief Used to remove placeholder text.
740 * @brief Used to reset the cursor position after setting a new text.
742 * @param[in] cursorIndex Where to place the cursor.
744 void ResetCursorPosition( CharacterIndex cursorIndex );
747 * @brief Used to reset the scroll position after setting a new text.
749 void ResetScrollPosition();
752 * @brief Used to process an event queued from SetText()
754 void TextReplacedEvent();
757 * @brief Used to process an event queued from key events etc.
759 void TextInsertedEvent();
762 * @brief Used to process an event queued from backspace key etc.
764 void TextDeletedEvent();
767 * @brief Lays-out the text.
769 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
771 * @param[in] size A the size of a bounding box to layout text within.
772 * @param[in] operations The layout operations which need to be done.
773 * @param[out] layoutSize The size of the laid-out text.
775 bool DoRelayout( const Size& size,
776 OperationsMask operations,
780 * @brief Whether to enable the multi-line layout.
782 * @param[in] enable \e true enables the multi-line (by default)
784 void SetMultiLineEnabled( bool enable );
787 * @return Whether the multi-line layout is enabled.
789 bool IsMultiLineEnabled() const;
792 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
794 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
797 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
799 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
802 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
804 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
807 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
809 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
812 * @brief Calulates the vertical offset to align the text inside the bounding box.
814 * @param[in] size The size of the bounding box.
816 void CalculateVerticalOffset( const Size& size );
819 * @brief Return the layout engine.
821 * @return A reference to the layout engine.
823 LayoutEngine& GetLayoutEngine();
826 * @brief Return a view of the text.
828 * @return A reference to the view.
832 // Text-input Event Queuing
835 * @brief Called by editable UI controls when keyboard focus is gained.
837 void KeyboardFocusGainEvent();
840 * @brief Called by editable UI controls when focus is lost.
842 void KeyboardFocusLostEvent();
845 * @brief Called by editable UI controls when key events are received.
847 * @param[in] event The key event.
848 * @param[in] type Used to distinguish between regular key events and IMF events.
850 bool KeyEvent( const Dali::KeyEvent& event );
853 * @brief Called by editable UI controls when key events are received.
855 * @param[in] text The text to insert.
856 * @param[in] type Used to distinguish between regular key events and IMF events.
858 void InsertText( const std::string& text, InsertType type );
861 * @brief Checks if text is selected and if so removes it.
862 * @return true if text was removed
864 bool RemoveSelectedText();
867 * @brief Called by editable UI controls when a tap gesture occurs.
868 * @param[in] tapCount The number of taps.
869 * @param[in] x The x position relative to the top-left of the parent control.
870 * @param[in] y The y position relative to the top-left of the parent control.
872 void TapEvent( unsigned int tapCount, float x, float y );
875 * @brief Called by editable UI controls when a pan gesture occurs.
877 * @param[in] state The state of the gesture.
878 * @param[in] displacement This distance panned since the last pan gesture.
880 void PanEvent( Gesture::State state, const Vector2& displacement );
883 * @brief Called by editable UI controls when a long press gesture occurs.
885 * @param[in] state The state of the gesture.
886 * @param[in] x The x position relative to the top-left of the parent control.
887 * @param[in] y The y position relative to the top-left of the parent control.
889 void LongPressEvent( Gesture::State state, float x, float y );
892 * @brief Creates a selection event.
894 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
896 * @param[in] x The x position relative to the top-left of the parent control.
897 * @param[in] y The y position relative to the top-left of the parent control.
898 * @param[in] selectAll Whether the whole text is selected.
900 void SelectEvent( float x, float y, bool selectAll );
903 * @brief Event received from IMF manager
905 * @param[in] imfManager The IMF manager.
906 * @param[in] imfEvent The event received.
907 * @return A data struture indicating if update is needed, cursor position and current text.
909 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
912 * @brief Paste given string into Text model
913 * @param[in] stringToPaste this string will be inserted into the text model
915 void PasteText( const std::string& stringToPaste );
918 * @brief Event from Clipboard notifying an Item has been selected for pasting
920 void PasteClipboardItemEvent();
923 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
925 virtual void GetTargetSize( Vector2& targetSize );
928 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
930 virtual void AddDecoration( Actor& actor, bool needsClipping );
933 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
935 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
938 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
940 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
945 * @brief A reference counted object may only be deleted by calling Unreference().
947 virtual ~Controller();
952 * @brief Helper to KeyEvent() to handle the backspace case.
954 * @return True if a character was deleted.
956 bool BackspaceKeyEvent();
959 * @brief Helper to clear font-specific data.
961 void ShowPlaceholderText();
964 * @brief Helper to clear font-specific data (only).
966 void ClearFontData();
969 * @brief Helper to clear text's style data.
971 void ClearStyleData();
974 * @brief Private constructor.
976 Controller( ControlInterface& controlInterface );
979 Controller( const Controller& handle );
982 Controller& operator=( const Controller& handle );
992 } // namespace Toolkit
996 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__