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 Replaces any text previously set.
179 * @note This will be converted into UTF-32 when stored in the text model.
180 * @param[in] text A string of UTF-8 characters.
182 void SetText( const std::string& text );
185 * @brief Retrieve any text previously set.
187 * @return A string of UTF-8 characters.
189 void GetText( std::string& text ) const;
192 * @brief Remove a given number of characters
194 * When predictve text is used the pre-edit text is removed and inserted again with the new characters.
195 * The UpdateInputStyleType @type parameter if set to DONT_UPDATE_INPUT_STYLE avoids to update the input
196 * style when pre-edit text is removed.
198 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
199 * @param[in] numberOfCharacters The number of characters to delete from the cursorOffset.
200 * @param[in] type Whether to update the input style.
201 * @return True if the remove was successful.
203 bool RemoveText( int cursorOffset,
204 int numberOfCharacters,
205 UpdateInputStyleType type );
208 * @brief Retrieve the current cursor position.
210 * @return The cursor position.
212 unsigned int GetLogicalCursorPosition() const;
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 Set the underline color.
395 * @param[in] color color of underline.
397 void SetUnderlineColor( const Vector4& color );
400 * @brief Retrieve the underline color.
402 * @return The underline color.
404 const Vector4& GetUnderlineColor() const;
407 * @brief Set the underline enabled flag.
409 * @param[in] enabled The underline enabled flag.
411 void SetUnderlineEnabled( bool enabled );
414 * @brief Returns whether the text is underlined or not.
416 * @return The underline state.
418 bool IsUnderlineEnabled() const;
421 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
423 * @param[in] height The height in pixels of the underline
425 void SetUnderlineHeight( float height );
428 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
430 * @return The height of the underline, or 0 if height is not overrided.
432 float GetUnderlineHeight() const;
435 * @brief Sets the input text's color.
437 * @param[in] color The input text's color.
439 void SetInputColor( const Vector4& color );
442 * @brief Retrieves the input text's color.
444 * @return The input text's color.
446 const Vector4& GetInputColor() const;
449 * @brief Sets the input text's font family name.
451 * @param[in] fontFamily The text's font family name.
453 void SetInputFontFamily( const std::string& fontFamily );
456 * @brief Retrieves the input text's font family name.
458 * @return The input text's font family name.
460 const std::string& GetInputFontFamily() const;
463 * @brief Sets the input text's font style.
465 * @param[in] fontStyle The input text's font style.
467 void SetInputFontStyle( const std::string& fontStyle );
470 * @brief Retrieves the input text's font style.
472 * @return The input text's font style.
474 const std::string& GetInputFontStyle() const;
477 * @brief Sets the input font's weight.
479 * @param[in] weight The input font's weight.
481 void SetInputFontWeight( FontWeight weight );
484 * @brief Retrieves the input font's weight.
486 * @return The input font's weight.
488 FontWeight GetInputFontWeight() const;
491 * @brief Sets the input font's width.
493 * @param[in] width The input font's width.
495 void SetInputFontWidth( FontWidth width );
498 * @brief Retrieves the input font's width.
500 * @return The input font's width.
502 FontWidth GetInputFontWidth() const;
505 * @brief Sets the input font's slant.
507 * @param[in] slant The input font's slant.
509 void SetInputFontSlant( FontSlant slant );
512 * @brief Retrieves the input font's slant.
514 * @return The input font's slant.
516 FontSlant GetInputFontSlant() const;
519 * @brief Sets the input font's point size.
521 * @param[in] size The input font's point size.
523 void SetInputFontPointSize( float size );
526 * @brief Retrieves the input font's point size.
528 * @return The input font's point size.
530 float GetInputFontPointSize() const;
533 * @brief Called to enable/disable cursor blink.
535 * @note Only editable controls should calls this.
536 * @param[in] enabled Whether the cursor should blink or not.
538 void SetEnableCursorBlink( bool enable );
541 * @brief Query whether cursor blink is enabled.
543 * @return Whether the cursor should blink or not.
545 bool GetEnableCursorBlink() const;
548 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
550 * @return The scroll position.
552 const Vector2& GetScrollPosition() const;
555 * @brief Query the alignment offset.
557 * @return The alignmnet offset.
559 const Vector2& GetAlignmentOffset() const;
562 * @copydoc Control::GetNaturalSize()
564 Vector3 GetNaturalSize();
567 * @copydoc Control::GetHeightForWidth()
569 float GetHeightForWidth( float width );
572 * @brief Triggers a relayout which updates View (if necessary).
574 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
575 * @param[in] size A the size of a bounding box to layout text within.
576 * @return True if the text model or decorations were updated.
578 bool Relayout( const Size& size );
581 * @brief Process queued events which modify the model.
583 void ProcessModifyEvents();
586 * @brief Used to remove placeholder text.
591 * @brief Used to reset the cursor position after setting a new text.
593 * @param[in] cursorIndex Where to place the cursor.
595 void ResetCursorPosition( CharacterIndex cursorIndex );
598 * @brief Used to reset the scroll position after setting a new text.
600 void ResetScrollPosition();
603 * @brief Used to process an event queued from SetText()
605 void TextReplacedEvent();
608 * @brief Used to process an event queued from key events etc.
610 void TextInsertedEvent();
613 * @brief Used to process an event queued from backspace key etc.
615 void TextDeletedEvent();
618 * @brief Lays-out the text.
620 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
622 * @param[in] size A the size of a bounding box to layout text within.
623 * @param[in] operations The layout operations which need to be done.
624 * @param[out] layoutSize The size of the laid-out text.
626 bool DoRelayout( const Size& size,
627 OperationsMask operations,
631 * @brief Whether to enable the multi-line layout.
633 * @param[in] enable \e true enables the multi-line (by default)
635 void SetMultiLineEnabled( bool enable );
638 * @return Whether the multi-line layout is enabled.
640 bool IsMultiLineEnabled() const;
643 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
645 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
648 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
650 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
653 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
655 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
658 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
660 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
663 * @brief Calulates the alignment of the whole text inside the bounding box.
665 * @param[in] size The size of the bounding box.
667 void CalculateTextAlignment( const Size& size );
670 * @brief Return the layout engine.
672 * @return A reference to the layout engine.
674 LayoutEngine& GetLayoutEngine();
677 * @brief Return a view of the text.
679 * @return A reference to the view.
683 // Text-input Event Queuing
686 * @brief Called by editable UI controls when keyboard focus is gained.
688 void KeyboardFocusGainEvent();
691 * @brief Called by editable UI controls when focus is lost.
693 void KeyboardFocusLostEvent();
696 * @brief Called by editable UI controls when key events are received.
698 * @param[in] event The key event.
699 * @param[in] type Used to distinguish between regular key events and IMF events.
701 bool KeyEvent( const Dali::KeyEvent& event );
704 * @brief Called by editable UI controls when key events are received.
706 * @param[in] text The text to insert.
707 * @param[in] type Used to distinguish between regular key events and IMF events.
709 void InsertText( const std::string& text, InsertType type );
712 * @brief Checks if text is selected and if so removes it.
713 * @return true if text was removed
715 bool RemoveSelectedText();
718 * @brief Called by editable UI controls when a tap gesture occurs.
719 * @param[in] tapCount The number of taps.
720 * @param[in] x The x position relative to the top-left of the parent control.
721 * @param[in] y The y position relative to the top-left of the parent control.
723 void TapEvent( unsigned int tapCount, float x, float y );
726 * @brief Called by editable UI controls when a pan gesture occurs.
728 * @param[in] state The state of the gesture.
729 * @param[in] displacement This distance panned since the last pan gesture.
731 void PanEvent( Gesture::State state, const Vector2& displacement );
734 * @brief Called by editable UI controls when a long press gesture occurs.
736 * @param[in] state The state of the gesture.
737 * @param[in] x The x position relative to the top-left of the parent control.
738 * @param[in] y The y position relative to the top-left of the parent control.
740 void LongPressEvent( Gesture::State state, float x, float y );
743 * @brief Creates a selection event.
745 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
747 * @param[in] x The x position relative to the top-left of the parent control.
748 * @param[in] y The y position relative to the top-left of the parent control.
749 * @param[in] selectAll Whether the whole text is selected.
751 void SelectEvent( float x, float y, bool selectAll );
754 * @brief Event received from IMF manager
756 * @param[in] imfManager The IMF manager.
757 * @param[in] imfEvent The event received.
758 * @return A data struture indicating if update is needed, cursor position and current text.
760 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
763 * @brief Paste given string into Text model
764 * @param[in] stringToPaste this string will be inserted into the text model
766 void PasteText( const std::string& stringToPaste );
769 * @brief Event from Clipboard notifying an Item has been selected for pasting
771 void PasteClipboardItemEvent();
774 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
776 virtual void GetTargetSize( Vector2& targetSize );
779 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
781 virtual void AddDecoration( Actor& actor, bool needsClipping );
784 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
786 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
789 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
791 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
796 * @brief A reference counted object may only be deleted by calling Unreference().
798 virtual ~Controller();
803 * @brief Helper to KeyEvent() to handle the backspace case.
805 * @return True if a character was deleted.
807 bool BackspaceKeyEvent();
810 * @brief Helper to notify IMF manager with surrounding text & cursor changes.
812 void NotifyImfManager();
815 * @brief Helper to clear font-specific data.
817 void ShowPlaceholderText();
820 * @brief Helper to clear font-specific data (only).
822 void ClearFontData();
825 * @brief Helper to clear text's style data.
827 void ClearStyleData();
830 * @brief Private constructor.
832 Controller( ControlInterface& controlInterface );
835 Controller( const Controller& handle );
838 Controller& operator=( const Controller& handle );
848 } // namespace Toolkit
852 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__