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,
88 ALL_OPERATIONS = 0xFFFF
92 * @brief Used to distinguish between regular key events and IMF events
101 * @brief Create a new instance of a Controller.
103 * @param[in] controlInterface An interface used to request a text relayout.
104 * @return A pointer to a new Controller.
106 static ControllerPtr New( ControlInterface& controlInterface );
109 * @brief Called to enable text input.
111 * @note Selectable or editable controls should call this once after Controller::New().
112 * @param[in] decorator Used to create cursor, selection handle decorations etc.
114 void EnableTextInput( DecoratorPtr decorator );
117 * @brief Replaces any text previously set.
119 * @note This will be converted into UTF-32 when stored in the text model.
120 * @param[in] text A string of UTF-8 characters.
122 void SetText( const std::string& text );
125 * @brief Retrieve any text previously set.
127 * @return A string of UTF-8 characters.
129 void GetText( std::string& text ) const;
132 * @brief Remove a given number of characters
134 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
135 * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
136 * @return True if the remove was successful.
138 bool RemoveText( int cursorOffset, int numberOfChars );
141 * @brief Retrieve the current cursor position.
143 * @return The cursor position.
145 unsigned int GetLogicalCursorPosition() const;
148 * @brief Replaces any placeholder text previously set.
150 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
151 * @param[in] text A string of UTF-8 characters.
153 void SetPlaceholderText( PlaceholderType type, const std::string& text );
156 * @brief Retrieve any placeholder text previously set.
158 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
159 * @param[out] A string of UTF-8 characters.
161 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
164 * @brief Sets the maximum number of characters that can be inserted into the TextModel
166 * @param[in] maxCharacters maximum number of characters to be accepted
168 void SetMaximumNumberOfCharacters( int maxCharacters );
171 * @brief Sets the maximum number of characters that can be inserted into the TextModel
173 * @param[in] maxCharacters maximum number of characters to be accepted
175 int GetMaximumNumberOfCharacters();
178 * @brief Set the default font family.
180 * @param[in] defaultFontFamily The default font family.
182 void SetDefaultFontFamily( const std::string& defaultFontFamily );
185 * @brief Retrieve the default font family.
187 * @return The default font family.
189 const std::string& GetDefaultFontFamily() const;
192 * @brief Sets the font's style string.
194 * @note The style set may be changed by the underlying font system. The string is stored to be recovered.
196 * @param[in] style The font's style string.
198 void SetDefaultFontStyle( const std::string& style );
201 * @brief Retrieves the font's style.
203 * @return The font's style.
205 const std::string& GetDefaultFontStyle() const;
208 * @brief Sets the default font width.
210 * @param[in] width The font width.
212 void SetDefaultFontWidth( FontWidth width );
215 * @brief Retrieves the default font width.
217 * @return The default font width.
219 FontWidth GetDefaultFontWidth() const;
222 * @brief Sets the default font weight.
224 * @param[in] weight The font weight.
226 void SetDefaultFontWeight( FontWeight weight );
229 * @brief Retrieves the default font weight.
231 * @return The default font weight.
233 FontWeight GetDefaultFontWeight() const;
236 * @brief Sets the default font slant.
238 * @param[in] slant The font slant.
240 void SetDefaultFontSlant( FontSlant slant );
243 * @brief Retrieves the default font slant.
245 * @return The default font slant.
247 FontSlant GetDefaultFontSlant() const;
250 * @brief Set the default point size.
252 * @param[in] pointSize The default point size.
254 void SetDefaultPointSize( float pointSize );
257 * @brief Retrieve the default point size.
259 * @return The default point size.
261 float GetDefaultPointSize() const;
264 * @ brief Update the text after a font change
265 * @param[in] newDefaultFont The new font to change to
267 void UpdateAfterFontChange( std::string& newDefaultFont );
270 * @brief Set the text color
272 * @param textColor The text color
274 void SetTextColor( const Vector4& textColor );
277 * @brief Retrieve the text color
279 * @return The text color
281 const Vector4& GetTextColor() const;
284 * @brief Set the text color
286 * @param textColor The text color
288 void SetPlaceholderTextColor( const Vector4& textColor );
291 * @brief Retrieve the text color
293 * @return The text color
295 const Vector4& GetPlaceholderTextColor() const;
298 * @brief Set the shadow offset.
300 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
302 void SetShadowOffset( const Vector2& shadowOffset );
305 * @brief Retrieve the shadow offset.
307 * @return The shadow offset.
309 const Vector2& GetShadowOffset() const;
312 * @brief Set the shadow color.
314 * @param[in] shadowColor The shadow color.
316 void SetShadowColor( const Vector4& shadowColor );
319 * @brief Retrieve the shadow color.
321 * @return The shadow color.
323 const Vector4& GetShadowColor() const;
326 * @brief Set the underline color.
328 * @param[in] color color of underline.
330 void SetUnderlineColor( const Vector4& color );
333 * @brief Retrieve the underline color.
335 * @return The underline color.
337 const Vector4& GetUnderlineColor() const;
340 * @brief Set the underline enabled flag.
342 * @param[in] enabled The underline enabled flag.
344 void SetUnderlineEnabled( bool enabled );
347 * @brief Returns whether the text is underlined or not.
349 * @return The underline state.
351 bool IsUnderlineEnabled() const;
354 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
356 * @param[in] height The height in pixels of the underline
358 void SetUnderlineHeight( float height );
361 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
363 * @return The height of the underline, or 0 if height is not overrided.
365 float GetUnderlineHeight() const;
368 * @brief Called to enable/disable cursor blink.
370 * @note Only editable controls should calls this.
371 * @param[in] enabled Whether the cursor should blink or not.
373 void SetEnableCursorBlink( bool enable );
376 * @brief Query whether cursor blink is enabled.
378 * @return Whether the cursor should blink or not.
380 bool GetEnableCursorBlink() const;
383 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
385 * @return The scroll position.
387 const Vector2& GetScrollPosition() const;
390 * @brief Query the alignment offset.
392 * @return The alignmnet offset.
394 const Vector2& GetAlignmentOffset() const;
397 * @copydoc Control::GetNaturalSize()
399 Vector3 GetNaturalSize();
402 * @copydoc Control::GetHeightForWidth()
404 float GetHeightForWidth( float width );
407 * @brief Triggers a relayout which updates View (if necessary).
409 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
410 * @param[in] size A the size of a bounding box to layout text within.
411 * @return True if the text model or decorations were updated.
413 bool Relayout( const Size& size );
416 * @brief Process queued events which modify the model.
418 void ProcessModifyEvents();
421 * @brief Used to remove placeholder text.
426 * @brief Used to reset the cursor position after setting a new text.
428 * @param[in] cursorIndex Where to place the cursor.
430 void ResetCursorPosition( CharacterIndex cursorIndex );
433 * @brief Used to reset the scroll position after setting a new text.
435 void ResetScrollPosition();
438 * @brief Used to process an event queued from SetText()
440 void TextReplacedEvent();
443 * @brief Used to process an event queued from key events etc.
445 void TextInsertedEvent();
448 * @brief Used to process an event queued from backspace key etc.
450 void TextDeletedEvent();
453 * @brief Lays-out the text.
455 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
457 * @param[in] size A the size of a bounding box to layout text within.
458 * @param[in] operations The layout operations which need to be done.
459 * @param[out] layoutSize The size of the laid-out text.
461 bool DoRelayout( const Size& size,
462 OperationsMask operations,
466 * @brief Whether to enable the multi-line layout.
468 * @param[in] enable \e true enables the multi-line (by default)
470 void SetMultiLineEnabled( bool enable );
473 * @return Whether the multi-line layout is enabled.
475 bool IsMultiLineEnabled() const;
478 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
480 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
483 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
485 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
488 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
490 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
493 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
495 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
498 * @brief Calulates the alignment of the whole text inside the bounding box.
500 * @param[in] size The size of the bounding box.
502 void CalculateTextAlignment( const Size& size );
505 * @brief Return the layout engine.
507 * @return A reference to the layout engine.
509 LayoutEngine& GetLayoutEngine();
512 * @brief Return a view of the text.
514 * @return A reference to the view.
518 // Text-input Event Queuing
521 * @brief Called by editable UI controls when keyboard focus is gained.
523 void KeyboardFocusGainEvent();
526 * @brief Called by editable UI controls when focus is lost.
528 void KeyboardFocusLostEvent();
531 * @brief Called by editable UI controls when key events are received.
533 * @param[in] event The key event.
534 * @param[in] type Used to distinguish between regular key events and IMF events.
536 bool KeyEvent( const Dali::KeyEvent& event );
539 * @brief Called by editable UI controls when key events are received.
541 * @param[in] text The text to insert.
542 * @param[in] type Used to distinguish between regular key events and IMF events.
544 void InsertText( const std::string& text, InsertType type );
547 * @brief Checks if text is selected and if so removes it.
548 * @return true if text was removed
550 bool RemoveSelectedText();
553 * @brief Called by editable UI controls when a tap gesture occurs.
554 * @param[in] tapCount The number of taps.
555 * @param[in] x The x position relative to the top-left of the parent control.
556 * @param[in] y The y position relative to the top-left of the parent control.
558 void TapEvent( unsigned int tapCount, float x, float y );
561 * @brief Called by editable UI controls when a pan gesture occurs.
563 * @param[in] state The state of the gesture.
564 * @param[in] displacement This distance panned since the last pan gesture.
566 void PanEvent( Gesture::State state, const Vector2& displacement );
569 * @brief Called by editable UI controls when a long press gesture occurs.
571 * @param[in] state The state of the gesture.
572 * @param[in] x The x position relative to the top-left of the parent control.
573 * @param[in] y The y position relative to the top-left of the parent control.
575 void LongPressEvent( Gesture::State state, float x, float y );
578 * @brief Creates a selection event.
580 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
582 * @param[in] x The x position relative to the top-left of the parent control.
583 * @param[in] y The y position relative to the top-left of the parent control.
584 * @param[in] selectAll Whether the whole text is selected.
586 void SelectEvent( float x, float y, bool selectAll );
589 * @brief Event received from IMF manager
591 * @param[in] imfManager The IMF manager.
592 * @param[in] imfEvent The event received.
593 * @return A data struture indicating if update is needed, cursor position and current text.
595 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
598 * @brief Paste given string into Text model
599 * @param[in] stringToPaste this string will be inserted into the text model
601 void PasteText( const std::string& stringToPaste );
604 * @brief Event from Clipboard notifying an Item has been selected for pasting
606 void PasteClipboardItemEvent();
609 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
611 virtual void GetTargetSize( Vector2& targetSize );
614 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
616 virtual void AddDecoration( Actor& actor, bool needsClipping );
619 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
621 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
624 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
626 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
631 * @brief A reference counted object may only be deleted by calling Unreference().
633 virtual ~Controller();
638 * @brief Helper to KeyEvent() to handle the backspace case.
640 * @return True if a character was deleted.
642 bool BackspaceKeyEvent();
645 * @brief Helper to notify IMF manager with surrounding text & cursor changes.
647 void NotifyImfManager();
650 * @brief Helper to clear font-specific data.
652 void ShowPlaceholderText();
655 * @brief Helper to clear all the model data except for LogicalModel::mText.
657 void ClearModelData();
660 * @brief Helper to clear font-specific data (only).
662 void ClearFontData();
665 * @brief Private constructor.
667 Controller( ControlInterface& controlInterface );
670 Controller( const Controller& handle );
673 Controller& operator=( const Controller& handle );
683 } // namespace Toolkit
687 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__