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 Create a new instance of a Controller.
104 * @param[in] controlInterface An interface used to request a text relayout.
105 * @return A pointer to a new Controller.
107 static ControllerPtr New( ControlInterface& controlInterface );
110 * @brief Called to enable text input.
112 * @note Selectable or editable controls should call this once after Controller::New().
113 * @param[in] decorator Used to create cursor, selection handle decorations etc.
115 void EnableTextInput( DecoratorPtr decorator );
118 * @brief Enables/disables the mark-up processor.
120 * By default is disabled.
122 * @param[in] enable Whether to enable the mark-up processor.
124 void SetMarkupProcessorEnabled( bool enable );
127 * @brief Retrieves whether the mark-up processor is enabled.
129 * By default is disabled.
131 * @return @e true if the mark-up processor is enabled, otherwise returns @e false.
133 bool IsMarkupProcessorEnabled() const;
136 * @brief Replaces any text previously set.
138 * @note This will be converted into UTF-32 when stored in the text model.
139 * @param[in] text A string of UTF-8 characters.
141 void SetText( const std::string& text );
144 * @brief Retrieve any text previously set.
146 * @return A string of UTF-8 characters.
148 void GetText( std::string& text ) const;
151 * @brief Remove a given number of characters
153 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
154 * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
155 * @return True if the remove was successful.
157 bool RemoveText( int cursorOffset, int numberOfChars );
160 * @brief Retrieve the current cursor position.
162 * @return The cursor position.
164 unsigned int GetLogicalCursorPosition() const;
167 * @brief Replaces any placeholder text previously set.
169 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
170 * @param[in] text A string of UTF-8 characters.
172 void SetPlaceholderText( PlaceholderType type, const std::string& text );
175 * @brief Retrieve any placeholder text previously set.
177 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
178 * @param[out] A string of UTF-8 characters.
180 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
183 * @brief Sets the maximum number of characters that can be inserted into the TextModel
185 * @param[in] maxCharacters maximum number of characters to be accepted
187 void SetMaximumNumberOfCharacters( int maxCharacters );
190 * @brief Sets the maximum number of characters that can be inserted into the TextModel
192 * @param[in] maxCharacters maximum number of characters to be accepted
194 int GetMaximumNumberOfCharacters();
197 * @brief Set the default font family.
199 * @param[in] defaultFontFamily The default font family.
201 void SetDefaultFontFamily( const std::string& defaultFontFamily );
204 * @brief Retrieve the default font family.
206 * @return The default font family.
208 const std::string& GetDefaultFontFamily() const;
211 * @brief Sets the font's style string.
213 * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
215 * @param[in] style The font's style string.
217 void SetDefaultFontStyle( const std::string& style );
220 * @brief Retrieves the font's style.
222 * @return The font's style.
224 const std::string& GetDefaultFontStyle() const;
227 * @brief Sets the default font weight.
229 * @param[in] weight The font weight.
231 void SetDefaultFontWeight( FontWeight weight );
234 * @brief Retrieves the default font weight.
236 * @return The default font weight.
238 FontWeight GetDefaultFontWeight() const;
241 * @brief Sets the default font width.
243 * @param[in] width The font width.
245 void SetDefaultFontWidth( FontWidth width );
248 * @brief Retrieves the default font width.
250 * @return The default font width.
252 FontWidth GetDefaultFontWidth() const;
255 * @brief Sets the default font slant.
257 * @param[in] slant The font slant.
259 void SetDefaultFontSlant( FontSlant slant );
262 * @brief Retrieves the default font slant.
264 * @return The default font slant.
266 FontSlant GetDefaultFontSlant() const;
269 * @brief Set the default point size.
271 * @param[in] pointSize The default point size.
273 void SetDefaultPointSize( float pointSize );
276 * @brief Retrieve the default point size.
278 * @return The default point size.
280 float GetDefaultPointSize() const;
283 * @ brief Update the text after a font change
284 * @param[in] newDefaultFont The new font to change to
286 void UpdateAfterFontChange( std::string& newDefaultFont );
289 * @brief Set the text color
291 * @param textColor The text color
293 void SetTextColor( const Vector4& textColor );
296 * @brief Retrieve the text color
298 * @return The text color
300 const Vector4& GetTextColor() const;
303 * @brief Set the text color
305 * @param textColor The text color
307 void SetPlaceholderTextColor( const Vector4& textColor );
310 * @brief Retrieve the text color
312 * @return The text color
314 const Vector4& GetPlaceholderTextColor() const;
317 * @brief Set the shadow offset.
319 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
321 void SetShadowOffset( const Vector2& shadowOffset );
324 * @brief Retrieve the shadow offset.
326 * @return The shadow offset.
328 const Vector2& GetShadowOffset() const;
331 * @brief Set the shadow color.
333 * @param[in] shadowColor The shadow color.
335 void SetShadowColor( const Vector4& shadowColor );
338 * @brief Retrieve the shadow color.
340 * @return The shadow color.
342 const Vector4& GetShadowColor() const;
345 * @brief Set the underline color.
347 * @param[in] color color of underline.
349 void SetUnderlineColor( const Vector4& color );
352 * @brief Retrieve the underline color.
354 * @return The underline color.
356 const Vector4& GetUnderlineColor() const;
359 * @brief Set the underline enabled flag.
361 * @param[in] enabled The underline enabled flag.
363 void SetUnderlineEnabled( bool enabled );
366 * @brief Returns whether the text is underlined or not.
368 * @return The underline state.
370 bool IsUnderlineEnabled() const;
373 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
375 * @param[in] height The height in pixels of the underline
377 void SetUnderlineHeight( float height );
380 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
382 * @return The height of the underline, or 0 if height is not overrided.
384 float GetUnderlineHeight() const;
387 * @brief Sets the input text's color.
389 * @param[in] color The input text's color.
391 void SetInputColor( const Vector4& color );
394 * @brief Retrieves the input text's color.
396 * @return The input text's color.
398 const Vector4& GetInputColor() const;
401 * @brief Sets the input text's font family name.
403 * @param[in] fontFamily The text's font family name.
405 void SetInputFontFamily( const std::string& fontFamily );
408 * @brief Retrieves the input text's font family name.
410 * @return The input text's font family name.
412 const std::string& GetInputFontFamily() const;
415 * @brief Sets the input text's font style.
417 * @param[in] fontStyle The input text's font style.
419 void SetInputFontStyle( const std::string& fontStyle );
422 * @brief Retrieves the input text's font style.
424 * @return The input text's font style.
426 const std::string& GetInputFontStyle() const;
429 * @brief Sets the input font's weight.
431 * @param[in] weight The input font's weight.
433 void SetInputFontWeight( FontWeight weight );
436 * @brief Retrieves the input font's weight.
438 * @return The input font's weight.
440 FontWeight GetInputFontWeight() const;
443 * @brief Sets the input font's width.
445 * @param[in] width The input font's width.
447 void SetInputFontWidth( FontWidth width );
450 * @brief Retrieves the input font's width.
452 * @return The input font's width.
454 FontWidth GetInputFontWidth() const;
457 * @brief Sets the input font's slant.
459 * @param[in] slant The input font's slant.
461 void SetInputFontSlant( FontSlant slant );
464 * @brief Retrieves the input font's slant.
466 * @return The input font's slant.
468 FontSlant GetInputFontSlant() const;
471 * @brief Sets the input font's point size.
473 * @param[in] size The input font's point size.
475 void SetInputFontPointSize( float size );
478 * @brief Retrieves the input font's point size.
480 * @return The input font's point size.
482 float GetInputFontPointSize() const;
485 * @brief Called to enable/disable cursor blink.
487 * @note Only editable controls should calls this.
488 * @param[in] enabled Whether the cursor should blink or not.
490 void SetEnableCursorBlink( bool enable );
493 * @brief Query whether cursor blink is enabled.
495 * @return Whether the cursor should blink or not.
497 bool GetEnableCursorBlink() const;
500 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
502 * @return The scroll position.
504 const Vector2& GetScrollPosition() const;
507 * @brief Query the alignment offset.
509 * @return The alignmnet offset.
511 const Vector2& GetAlignmentOffset() const;
514 * @copydoc Control::GetNaturalSize()
516 Vector3 GetNaturalSize();
519 * @copydoc Control::GetHeightForWidth()
521 float GetHeightForWidth( float width );
524 * @brief Triggers a relayout which updates View (if necessary).
526 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
527 * @param[in] size A the size of a bounding box to layout text within.
528 * @return True if the text model or decorations were updated.
530 bool Relayout( const Size& size );
533 * @brief Process queued events which modify the model.
535 void ProcessModifyEvents();
538 * @brief Used to remove placeholder text.
543 * @brief Used to reset the cursor position after setting a new text.
545 * @param[in] cursorIndex Where to place the cursor.
547 void ResetCursorPosition( CharacterIndex cursorIndex );
550 * @brief Used to reset the scroll position after setting a new text.
552 void ResetScrollPosition();
555 * @brief Used to process an event queued from SetText()
557 void TextReplacedEvent();
560 * @brief Used to process an event queued from key events etc.
562 void TextInsertedEvent();
565 * @brief Used to process an event queued from backspace key etc.
567 void TextDeletedEvent();
570 * @brief Lays-out the text.
572 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
574 * @param[in] size A the size of a bounding box to layout text within.
575 * @param[in] operations The layout operations which need to be done.
576 * @param[out] layoutSize The size of the laid-out text.
578 bool DoRelayout( const Size& size,
579 OperationsMask operations,
583 * @brief Whether to enable the multi-line layout.
585 * @param[in] enable \e true enables the multi-line (by default)
587 void SetMultiLineEnabled( bool enable );
590 * @return Whether the multi-line layout is enabled.
592 bool IsMultiLineEnabled() const;
595 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
597 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
600 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
602 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
605 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
607 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
610 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
612 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
615 * @brief Calulates the alignment of the whole text inside the bounding box.
617 * @param[in] size The size of the bounding box.
619 void CalculateTextAlignment( const Size& size );
622 * @brief Return the layout engine.
624 * @return A reference to the layout engine.
626 LayoutEngine& GetLayoutEngine();
629 * @brief Return a view of the text.
631 * @return A reference to the view.
635 // Text-input Event Queuing
638 * @brief Called by editable UI controls when keyboard focus is gained.
640 void KeyboardFocusGainEvent();
643 * @brief Called by editable UI controls when focus is lost.
645 void KeyboardFocusLostEvent();
648 * @brief Called by editable UI controls when key events are received.
650 * @param[in] event The key event.
651 * @param[in] type Used to distinguish between regular key events and IMF events.
653 bool KeyEvent( const Dali::KeyEvent& event );
656 * @brief Called by editable UI controls when key events are received.
658 * @param[in] text The text to insert.
659 * @param[in] type Used to distinguish between regular key events and IMF events.
661 void InsertText( const std::string& text, InsertType type );
664 * @brief Checks if text is selected and if so removes it.
665 * @return true if text was removed
667 bool RemoveSelectedText();
670 * @brief Called by editable UI controls when a tap gesture occurs.
671 * @param[in] tapCount The number of taps.
672 * @param[in] x The x position relative to the top-left of the parent control.
673 * @param[in] y The y position relative to the top-left of the parent control.
675 void TapEvent( unsigned int tapCount, float x, float y );
678 * @brief Called by editable UI controls when a pan gesture occurs.
680 * @param[in] state The state of the gesture.
681 * @param[in] displacement This distance panned since the last pan gesture.
683 void PanEvent( Gesture::State state, const Vector2& displacement );
686 * @brief Called by editable UI controls when a long press gesture occurs.
688 * @param[in] state The state of the gesture.
689 * @param[in] x The x position relative to the top-left of the parent control.
690 * @param[in] y The y position relative to the top-left of the parent control.
692 void LongPressEvent( Gesture::State state, float x, float y );
695 * @brief Creates a selection event.
697 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
699 * @param[in] x The x position relative to the top-left of the parent control.
700 * @param[in] y The y position relative to the top-left of the parent control.
701 * @param[in] selectAll Whether the whole text is selected.
703 void SelectEvent( float x, float y, bool selectAll );
706 * @brief Event received from IMF manager
708 * @param[in] imfManager The IMF manager.
709 * @param[in] imfEvent The event received.
710 * @return A data struture indicating if update is needed, cursor position and current text.
712 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
715 * @brief Paste given string into Text model
716 * @param[in] stringToPaste this string will be inserted into the text model
718 void PasteText( const std::string& stringToPaste );
721 * @brief Event from Clipboard notifying an Item has been selected for pasting
723 void PasteClipboardItemEvent();
726 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
728 virtual void GetTargetSize( Vector2& targetSize );
731 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
733 virtual void AddDecoration( Actor& actor, bool needsClipping );
736 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
738 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
741 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
743 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
748 * @brief A reference counted object may only be deleted by calling Unreference().
750 virtual ~Controller();
755 * @brief Helper to KeyEvent() to handle the backspace case.
757 * @return True if a character was deleted.
759 bool BackspaceKeyEvent();
762 * @brief Helper to notify IMF manager with surrounding text & cursor changes.
764 void NotifyImfManager();
767 * @brief Helper to clear font-specific data.
769 void ShowPlaceholderText();
772 * @brief Helper to clear all the model data except for LogicalModel::mText.
774 void ClearModelData();
777 * @brief Helper to clear font-specific data (only).
779 void ClearFontData();
782 * @brief Helper to clear text's style data.
784 void ClearStyleData();
787 * @brief Private constructor.
789 Controller( ControlInterface& controlInterface );
792 Controller( const Controller& handle );
795 Controller& operator=( const Controller& handle );
805 } // namespace Toolkit
809 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__