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 ALL_OPERATIONS = 0xFFFF
93 * @brief Used to distinguish between regular key events and IMF events
102 * @brief Used to specify whether to update the input style.
104 enum UpdateInputStyleType
107 DONT_UPDATE_INPUT_STYLE
111 * @brief Create a new instance of a Controller.
113 * @param[in] controlInterface An interface used to request a text relayout.
114 * @return A pointer to a new Controller.
116 static ControllerPtr New( ControlInterface& controlInterface );
119 * @brief Called to enable text input.
121 * @note Selectable or editable controls should call this once after Controller::New().
122 * @param[in] decorator Used to create cursor, selection handle decorations etc.
124 void EnableTextInput( DecoratorPtr decorator );
127 * @brief Used to switch between bitmap & vector based glyphs
129 * @param[in] glyphType The type of glyph; note that metrics for bitmap & vector based glyphs are different.
131 void SetGlyphType( TextAbstraction::GlyphType glyphType );
134 * @brief Enables/disables the mark-up processor.
136 * By default is disabled.
138 * @param[in] enable Whether to enable the mark-up processor.
140 void SetMarkupProcessorEnabled( bool enable );
143 * @brief Retrieves whether the mark-up processor is enabled.
145 * By default is disabled.
147 * @return @e true if the mark-up processor is enabled, otherwise returns @e false.
149 bool IsMarkupProcessorEnabled() const;
152 * @brief Replaces any text previously set.
154 * @note This will be converted into UTF-32 when stored in the text model.
155 * @param[in] text A string of UTF-8 characters.
157 void SetText( const std::string& text );
160 * @brief Retrieve any text previously set.
162 * @return A string of UTF-8 characters.
164 void GetText( std::string& text ) const;
167 * @brief Remove a given number of characters
169 * When predictve text is used the pre-edit text is removed and inserted again with the new characters.
170 * The UpdateInputStyleType @type parameter if set to DONT_UPDATE_INPUT_STYLE avoids to update the input
171 * style when pre-edit text is removed.
173 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
174 * @param[in] numberOfCharacters The number of characters to delete from the cursorOffset.
175 * @param[in] type Whether to update the input style.
176 * @return True if the remove was successful.
178 bool RemoveText( int cursorOffset,
179 int numberOfCharacters,
180 UpdateInputStyleType type );
183 * @brief Retrieve the current cursor position.
185 * @return The cursor position.
187 unsigned int GetLogicalCursorPosition() const;
190 * @brief Replaces any placeholder text previously set.
192 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
193 * @param[in] text A string of UTF-8 characters.
195 void SetPlaceholderText( PlaceholderType type, const std::string& text );
198 * @brief Retrieve any placeholder text previously set.
200 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
201 * @param[out] A string of UTF-8 characters.
203 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
206 * @brief Sets the maximum number of characters that can be inserted into the TextModel
208 * @param[in] maxCharacters maximum number of characters to be accepted
210 void SetMaximumNumberOfCharacters( Length maxCharacters );
213 * @brief Sets the maximum number of characters that can be inserted into the TextModel
215 * @param[in] maxCharacters maximum number of characters to be accepted
217 int GetMaximumNumberOfCharacters();
220 * @brief Set the default font family.
222 * @param[in] defaultFontFamily The default font family.
224 void SetDefaultFontFamily( const std::string& defaultFontFamily );
227 * @brief Retrieve the default font family.
229 * @return The default font family.
231 const std::string& GetDefaultFontFamily() const;
234 * @brief Sets the font's style string.
236 * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
238 * @param[in] style The font's style string.
240 void SetDefaultFontStyle( const std::string& style );
243 * @brief Retrieves the font's style.
245 * @return The font's style.
247 const std::string& GetDefaultFontStyle() const;
250 * @brief Sets the default font weight.
252 * @param[in] weight The font weight.
254 void SetDefaultFontWeight( FontWeight weight );
257 * @brief Retrieves the default font weight.
259 * @return The default font weight.
261 FontWeight GetDefaultFontWeight() const;
264 * @brief Sets the default font width.
266 * @param[in] width The font width.
268 void SetDefaultFontWidth( FontWidth width );
271 * @brief Retrieves the default font width.
273 * @return The default font width.
275 FontWidth GetDefaultFontWidth() const;
278 * @brief Sets the default font slant.
280 * @param[in] slant The font slant.
282 void SetDefaultFontSlant( FontSlant slant );
285 * @brief Retrieves the default font slant.
287 * @return The default font slant.
289 FontSlant GetDefaultFontSlant() const;
292 * @brief Set the default point size.
294 * @param[in] pointSize The default point size.
296 void SetDefaultPointSize( float pointSize );
299 * @brief Retrieve the default point size.
301 * @return The default point size.
303 float GetDefaultPointSize() const;
306 * @ brief Update the text after a font change
307 * @param[in] newDefaultFont The new font to change to
309 void UpdateAfterFontChange( const std::string& newDefaultFont );
312 * @brief Set the text color
314 * @param textColor The text color
316 void SetTextColor( const Vector4& textColor );
319 * @brief Retrieve the text color
321 * @return The text color
323 const Vector4& GetTextColor() const;
326 * @brief Set the text color
328 * @param textColor The text color
330 void SetPlaceholderTextColor( const Vector4& textColor );
333 * @brief Retrieve the text color
335 * @return The text color
337 const Vector4& GetPlaceholderTextColor() const;
340 * @brief Set the shadow offset.
342 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
344 void SetShadowOffset( const Vector2& shadowOffset );
347 * @brief Retrieve the shadow offset.
349 * @return The shadow offset.
351 const Vector2& GetShadowOffset() const;
354 * @brief Set the shadow color.
356 * @param[in] shadowColor The shadow color.
358 void SetShadowColor( const Vector4& shadowColor );
361 * @brief Retrieve the shadow color.
363 * @return The shadow color.
365 const Vector4& GetShadowColor() const;
368 * @brief Set the underline color.
370 * @param[in] color color of underline.
372 void SetUnderlineColor( const Vector4& color );
375 * @brief Retrieve the underline color.
377 * @return The underline color.
379 const Vector4& GetUnderlineColor() const;
382 * @brief Set the underline enabled flag.
384 * @param[in] enabled The underline enabled flag.
386 void SetUnderlineEnabled( bool enabled );
389 * @brief Returns whether the text is underlined or not.
391 * @return The underline state.
393 bool IsUnderlineEnabled() const;
396 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
398 * @param[in] height The height in pixels of the underline
400 void SetUnderlineHeight( float height );
403 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
405 * @return The height of the underline, or 0 if height is not overrided.
407 float GetUnderlineHeight() const;
410 * @brief Sets the input text's color.
412 * @param[in] color The input text's color.
414 void SetInputColor( const Vector4& color );
417 * @brief Retrieves the input text's color.
419 * @return The input text's color.
421 const Vector4& GetInputColor() const;
424 * @brief Sets the input text's font family name.
426 * @param[in] fontFamily The text's font family name.
428 void SetInputFontFamily( const std::string& fontFamily );
431 * @brief Retrieves the input text's font family name.
433 * @return The input text's font family name.
435 const std::string& GetInputFontFamily() const;
438 * @brief Sets the input text's font style.
440 * @param[in] fontStyle The input text's font style.
442 void SetInputFontStyle( const std::string& fontStyle );
445 * @brief Retrieves the input text's font style.
447 * @return The input text's font style.
449 const std::string& GetInputFontStyle() const;
452 * @brief Sets the input font's weight.
454 * @param[in] weight The input font's weight.
456 void SetInputFontWeight( FontWeight weight );
459 * @brief Retrieves the input font's weight.
461 * @return The input font's weight.
463 FontWeight GetInputFontWeight() const;
466 * @brief Sets the input font's width.
468 * @param[in] width The input font's width.
470 void SetInputFontWidth( FontWidth width );
473 * @brief Retrieves the input font's width.
475 * @return The input font's width.
477 FontWidth GetInputFontWidth() const;
480 * @brief Sets the input font's slant.
482 * @param[in] slant The input font's slant.
484 void SetInputFontSlant( FontSlant slant );
487 * @brief Retrieves the input font's slant.
489 * @return The input font's slant.
491 FontSlant GetInputFontSlant() const;
494 * @brief Sets the input font's point size.
496 * @param[in] size The input font's point size.
498 void SetInputFontPointSize( float size );
501 * @brief Retrieves the input font's point size.
503 * @return The input font's point size.
505 float GetInputFontPointSize() const;
508 * @brief Called to enable/disable cursor blink.
510 * @note Only editable controls should calls this.
511 * @param[in] enabled Whether the cursor should blink or not.
513 void SetEnableCursorBlink( bool enable );
516 * @brief Query whether cursor blink is enabled.
518 * @return Whether the cursor should blink or not.
520 bool GetEnableCursorBlink() const;
523 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
525 * @return The scroll position.
527 const Vector2& GetScrollPosition() const;
530 * @brief Query the alignment offset.
532 * @return The alignmnet offset.
534 const Vector2& GetAlignmentOffset() const;
537 * @copydoc Control::GetNaturalSize()
539 Vector3 GetNaturalSize();
542 * @copydoc Control::GetHeightForWidth()
544 float GetHeightForWidth( float width );
547 * @brief Triggers a relayout which updates View (if necessary).
549 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
550 * @param[in] size A the size of a bounding box to layout text within.
551 * @return True if the text model or decorations were updated.
553 bool Relayout( const Size& size );
556 * @brief Process queued events which modify the model.
558 void ProcessModifyEvents();
561 * @brief Used to remove placeholder text.
566 * @brief Used to reset the cursor position after setting a new text.
568 * @param[in] cursorIndex Where to place the cursor.
570 void ResetCursorPosition( CharacterIndex cursorIndex );
573 * @brief Used to reset the scroll position after setting a new text.
575 void ResetScrollPosition();
578 * @brief Used to process an event queued from SetText()
580 void TextReplacedEvent();
583 * @brief Used to process an event queued from key events etc.
585 void TextInsertedEvent();
588 * @brief Used to process an event queued from backspace key etc.
590 void TextDeletedEvent();
593 * @brief Lays-out the text.
595 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
597 * @param[in] size A the size of a bounding box to layout text within.
598 * @param[in] operations The layout operations which need to be done.
599 * @param[out] layoutSize The size of the laid-out text.
601 bool DoRelayout( const Size& size,
602 OperationsMask operations,
606 * @brief Whether to enable the multi-line layout.
608 * @param[in] enable \e true enables the multi-line (by default)
610 void SetMultiLineEnabled( bool enable );
613 * @return Whether the multi-line layout is enabled.
615 bool IsMultiLineEnabled() const;
618 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
620 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
623 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
625 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
628 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
630 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
633 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
635 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
638 * @brief Calulates the alignment of the whole text inside the bounding box.
640 * @param[in] size The size of the bounding box.
642 void CalculateTextAlignment( const Size& size );
645 * @brief Return the layout engine.
647 * @return A reference to the layout engine.
649 LayoutEngine& GetLayoutEngine();
652 * @brief Return a view of the text.
654 * @return A reference to the view.
658 // Text-input Event Queuing
661 * @brief Called by editable UI controls when keyboard focus is gained.
663 void KeyboardFocusGainEvent();
666 * @brief Called by editable UI controls when focus is lost.
668 void KeyboardFocusLostEvent();
671 * @brief Called by editable UI controls when key events are received.
673 * @param[in] event The key event.
674 * @param[in] type Used to distinguish between regular key events and IMF events.
676 bool KeyEvent( const Dali::KeyEvent& event );
679 * @brief Called by editable UI controls when key events are received.
681 * @param[in] text The text to insert.
682 * @param[in] type Used to distinguish between regular key events and IMF events.
684 void InsertText( const std::string& text, InsertType type );
687 * @brief Checks if text is selected and if so removes it.
688 * @return true if text was removed
690 bool RemoveSelectedText();
693 * @brief Called by editable UI controls when a tap gesture occurs.
694 * @param[in] tapCount The number of taps.
695 * @param[in] x The x position relative to the top-left of the parent control.
696 * @param[in] y The y position relative to the top-left of the parent control.
698 void TapEvent( unsigned int tapCount, float x, float y );
701 * @brief Called by editable UI controls when a pan gesture occurs.
703 * @param[in] state The state of the gesture.
704 * @param[in] displacement This distance panned since the last pan gesture.
706 void PanEvent( Gesture::State state, const Vector2& displacement );
709 * @brief Called by editable UI controls when a long press gesture occurs.
711 * @param[in] state The state of the gesture.
712 * @param[in] x The x position relative to the top-left of the parent control.
713 * @param[in] y The y position relative to the top-left of the parent control.
715 void LongPressEvent( Gesture::State state, float x, float y );
718 * @brief Creates a selection event.
720 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
722 * @param[in] x The x position relative to the top-left of the parent control.
723 * @param[in] y The y position relative to the top-left of the parent control.
724 * @param[in] selectAll Whether the whole text is selected.
726 void SelectEvent( float x, float y, bool selectAll );
729 * @brief Event received from IMF manager
731 * @param[in] imfManager The IMF manager.
732 * @param[in] imfEvent The event received.
733 * @return A data struture indicating if update is needed, cursor position and current text.
735 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
738 * @brief Paste given string into Text model
739 * @param[in] stringToPaste this string will be inserted into the text model
741 void PasteText( const std::string& stringToPaste );
744 * @brief Event from Clipboard notifying an Item has been selected for pasting
746 void PasteClipboardItemEvent();
749 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
751 virtual void GetTargetSize( Vector2& targetSize );
754 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
756 virtual void AddDecoration( Actor& actor, bool needsClipping );
759 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
761 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
764 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
766 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
771 * @brief A reference counted object may only be deleted by calling Unreference().
773 virtual ~Controller();
778 * @brief Helper to KeyEvent() to handle the backspace case.
780 * @return True if a character was deleted.
782 bool BackspaceKeyEvent();
785 * @brief Helper to notify IMF manager with surrounding text & cursor changes.
787 void NotifyImfManager();
790 * @brief Helper to clear font-specific data.
792 void ShowPlaceholderText();
795 * @brief Helper to clear font-specific data (only).
797 void ClearFontData();
800 * @brief Helper to clear text's style data.
802 void ClearStyleData();
805 * @brief Private constructor.
807 Controller( ControlInterface& controlInterface );
810 Controller( const Controller& handle );
813 Controller& operator=( const Controller& handle );
823 } // namespace Toolkit
827 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__