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.
23 #include <dali/devel-api/adaptor-framework/imf-manager.h>
24 #include <dali/public-api/common/dali-vector.h>
25 #include <dali/public-api/common/intrusive-ptr.h>
26 #include <dali/public-api/events/gesture.h>
27 #include <dali/public-api/events/key-event.h>
28 #include <dali/public-api/math/vector3.h>
29 #include <dali/public-api/math/vector2.h>
30 #include <dali/public-api/object/ref-object.h>
33 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup-callback-interface.h>
34 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
35 #include <dali-toolkit/internal/text/font-run.h>
36 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
37 #include <dali-toolkit/internal/text/text-control-interface.h>
38 #include <dali-toolkit/internal/text/text-view.h>
52 typedef IntrusivePtr<Controller> ControllerPtr;
53 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
56 * @brief Different placeholder-text can be shown when the control is active/inactive.
60 PLACEHOLDER_TYPE_ACTIVE,
61 PLACEHOLDER_TYPE_INACTIVE,
65 * @brief A Text Controller is used by UI Controls which display text.
67 * It manipulates the Logical & Visual text models on behalf of the UI Controls.
68 * It provides a view of the text that can be used by rendering back-ends.
70 * For selectable/editable UI controls, the controller handles input events from the UI control
71 * and decorations (grab handles etc) via the Decorator::ControllerInterface interface.
73 * The text selection popup button callbacks are as well handled via the TextSelectionPopupCallbackInterface interface.
75 class Controller : public RefObject, public Decorator::ControllerInterface, public TextSelectionPopupCallbackInterface
80 * @brief Text related operations to be done in the relayout process.
84 NO_OPERATION = 0x0000,
85 CONVERT_TO_UTF32 = 0x0001,
87 VALIDATE_FONTS = 0x0004,
88 GET_LINE_BREAKS = 0x0008,
89 GET_WORD_BREAKS = 0x0010,
92 GET_GLYPH_METRICS = 0x0080,
94 UPDATE_ACTUAL_SIZE = 0x0200,
97 ALL_OPERATIONS = 0xFFFF
101 * @brief Used to distinguish between regular key events and IMF events
110 * @brief Create a new instance of a Controller.
112 * @param[in] controlInterface An interface used to request a text relayout.
113 * @return A pointer to a new Controller.
115 static ControllerPtr New( ControlInterface& controlInterface );
118 * @brief Called to enable text input.
120 * @note Selectable or editable controls should call this once after Controller::New().
121 * @param[in] decorator Used to create cursor, selection handle decorations etc.
123 void EnableTextInput( DecoratorPtr decorator );
126 * @brief Replaces any text previously set.
128 * @note This will be converted into UTF-32 when stored in the text model.
129 * @param[in] text A string of UTF-8 characters.
131 void SetText( const std::string& text );
134 * @brief Retrieve any text previously set.
136 * @return A string of UTF-8 characters.
138 void GetText( std::string& text ) const;
141 * @brief Replaces any placeholder text previously set.
143 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
144 * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
145 * @return True if the remove was successful.
147 bool RemoveText( int cursorOffset, int numberOfChars );
150 * @brief Retrieve the current cursor position.
152 * @return The cursor position.
154 unsigned int GetLogicalCursorPosition() const;
157 * @brief Replaces any placeholder text previously set.
159 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
160 * @param[in] text A string of UTF-8 characters.
162 void SetPlaceholderText( PlaceholderType type, const std::string& text );
165 * @brief Retrieve any placeholder text previously set.
167 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
168 * @param[out] A string of UTF-8 characters.
170 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
173 * @brief Sets the maximum number of characters that can be inserted into the TextModel
175 * @param[in] maxCharacters maximum number of characters to be accepted
177 void SetMaximumNumberOfCharacters( int maxCharacters );
180 * @brief Sets the maximum number of characters that can be inserted into the TextModel
182 * @param[in] maxCharacters maximum number of characters to be accepted
184 int GetMaximumNumberOfCharacters();
187 * @brief Set the default font family.
189 * @param[in] defaultFontFamily The default font family.
191 void SetDefaultFontFamily( const std::string& defaultFontFamily );
194 * @brief Retrieve the default font family.
196 * @return The default font family.
198 const std::string& GetDefaultFontFamily() const;
201 * @brief Set the default font style.
203 * @param[in] defaultFontStyle The default font style.
205 void SetDefaultFontStyle( const std::string& defaultFontStyle );
208 * @brief Retrieve the default font style.
210 * @return The default font style.
212 const std::string& GetDefaultFontStyle() const;
215 * @brief Set the default point size.
217 * @param[in] defaultFontStyle The default point size.
219 void SetDefaultPointSize( float pointSize );
222 * @brief Retrieve the default point size.
224 * @return The default point size.
226 float GetDefaultPointSize() const;
229 * @brief Set the text color
231 * @param textColor The text color
233 void SetTextColor( const Vector4& textColor );
236 * @brief Retrieve the text color
238 * @return The text color
240 const Vector4& GetTextColor() const;
243 * @brief Set the text color
245 * @param textColor The text color
247 void SetPlaceholderTextColor( const Vector4& textColor );
250 * @brief Retrieve the text color
252 * @return The text color
254 const Vector4& GetPlaceholderTextColor() const;
257 * @brief Set the shadow offset.
259 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
261 void SetShadowOffset( const Vector2& shadowOffset );
264 * @brief Retrieve the shadow offset.
266 * @return The shadow offset.
268 const Vector2& GetShadowOffset() const;
271 * @brief Set the shadow color.
273 * @param[in] shadowColor The shadow color.
275 void SetShadowColor( const Vector4& shadowColor );
278 * @brief Retrieve the shadow color.
280 * @return The shadow color.
282 const Vector4& GetShadowColor() const;
285 * @brief Set the underline color.
287 * @param[in] color color of underline.
289 void SetUnderlineColor( const Vector4& color );
292 * @brief Retrieve the underline color.
294 * @return The underline color.
296 const Vector4& GetUnderlineColor() const;
299 * @brief Set the underline enabled flag.
301 * @param[in] enabled The underline enabled flag.
303 void SetUnderlineEnabled( bool enabled );
306 * @brief Returns whether the text is underlined or not.
308 * @return The underline state.
310 bool IsUnderlineEnabled() const;
313 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
315 * @param[in] height The height in pixels of the underline
317 void SetUnderlineHeight( float height );
320 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
322 * @return The height of the underline, or 0 if height is not overrided.
324 float GetUnderlineHeight() const;
327 * @brief Called to enable/disable cursor blink.
329 * @note Only editable controls should calls this.
330 * @param[in] enabled Whether the cursor should blink or not.
332 void SetEnableCursorBlink( bool enable );
335 * @brief Query whether cursor blink is enabled.
337 * @return Whether the cursor should blink or not.
339 bool GetEnableCursorBlink() const;
342 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
344 * @return The scroll position.
346 const Vector2& GetScrollPosition() const;
349 * @brief Query the alignment offset.
351 * @return The alignmnet offset.
353 const Vector2& GetAlignmentOffset() const;
356 * @copydoc Control::GetNaturalSize()
358 Vector3 GetNaturalSize();
361 * @copydoc Control::GetHeightForWidth()
363 float GetHeightForWidth( float width );
366 * @brief Triggers a relayout which updates View (if necessary).
368 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
369 * @param[in] size A the size of a bounding box to layout text within.
370 * @return True if the text model or decorations were updated.
372 bool Relayout( const Size& size );
375 * @brief Process queued events which modify the model.
377 void ProcessModifyEvents();
380 * @brief Used to remove placeholder text.
385 * @brief Used to reset the cursor position after setting a new text.
387 * @param[in] cursorIndex Where to place the cursor.
389 void ResetCursorPosition( CharacterIndex cursorIndex );
392 * @brief Used to reset the scroll position after setting a new text.
394 void ResetScrollPosition();
397 * @brief Used to process an event queued from SetText()
399 void TextReplacedEvent();
402 * @brief Used to process an event queued from key events etc.
404 void TextInsertedEvent();
407 * @brief Used to process an event queued from backspace key etc.
409 void TextDeletedEvent();
412 * @brief Lays-out the text.
414 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
416 * @param[in] size A the size of a bounding box to layout text within.
417 * @param[in] operations The layout operations which need to be done.
418 * @param[out] layoutSize The size of the laid-out text.
420 bool DoRelayout( const Size& size,
421 OperationsMask operations,
425 * @brief Whether to enable the multi-line layout.
427 * @param[in] enable \e true enables the multi-line (by default)
429 void SetMultiLineEnabled( bool enable );
432 * @return Whether the multi-line layout is enabled.
434 bool IsMultiLineEnabled() const;
437 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
439 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
442 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
444 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
447 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
449 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
452 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
454 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
457 * @brief Calulates the alignment of the whole text inside the bounding box.
459 * @param[in] size The size of the bounding box.
461 void CalculateTextAlignment( const Size& size );
464 * @brief Return the layout engine.
466 * @return A reference to the layout engine.
468 LayoutEngine& GetLayoutEngine();
471 * @brief Return a view of the text.
473 * @return A reference to the view.
477 // Text-input Event Queuing
480 * @brief Caller by editable UI controls when keyboard focus is gained.
482 void KeyboardFocusGainEvent();
485 * @brief Caller by editable UI controls when focus is lost.
487 void KeyboardFocusLostEvent();
490 * @brief Caller by editable UI controls when key events are received.
492 * @param[in] event The key event.
493 * @param[in] type Used to distinguish between regular key events and IMF events.
495 bool KeyEvent( const Dali::KeyEvent& event );
498 * @brief Caller by editable UI controls when key events are received.
500 * @param[in] text The text to insert.
501 * @param[in] type Used to distinguish between regular key events and IMF events.
503 void InsertText( const std::string& text, InsertType type );
506 * @brief Caller by editable UI controls when a tap gesture occurs.
507 * @param[in] tapCount The number of taps.
508 * @param[in] x The x position relative to the top-left of the parent control.
509 * @param[in] y The y position relative to the top-left of the parent control.
511 void TapEvent( unsigned int tapCount, float x, float y );
514 * @brief Caller by editable UI controls when a pan gesture occurs.
516 * @param[in] state The state of the gesture.
517 * @param[in] displacement This distance panned since the last pan gesture.
519 void PanEvent( Gesture::State state, const Vector2& displacement );
522 * @brief Event received from IMF manager
524 * @param[in] imfManager The IMF manager.
525 * @param[in] imfEvent The event received.
526 * @return A data struture indicating if update is needed, cursor position and current text.
528 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
531 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
533 virtual void GetTargetSize( Vector2& targetSize );
536 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
538 virtual void AddDecoration( Actor& actor, bool needsClipping );
541 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
543 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
546 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
548 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
553 * @brief A reference counted object may only be deleted by calling Unreference().
555 virtual ~Controller();
560 * @brief Helper to clear font-specific data.
562 void ShowPlaceholderText();
565 * @brief Helper to clear all the model data except for LogicalModel::mText.
567 void ClearModelData();
570 * @brief Helper to clear font-specific data (only).
572 void ClearFontData();
575 * @brief Private constructor.
577 Controller( ControlInterface& controlInterface );
580 Controller( const Controller& handle );
583 Controller& operator=( const Controller& handle );
593 } // namespace Toolkit
597 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__