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/internal/text/decorator/text-decorator.h>
34 #include <dali-toolkit/internal/text/font-run.h>
35 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
36 #include <dali-toolkit/internal/text/text-control-interface.h>
37 #include <dali-toolkit/internal/text/text-view.h>
51 typedef IntrusivePtr<Controller> ControllerPtr;
52 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
55 * @brief Different placeholder-text can be shown when the control is active/inactive.
59 PLACEHOLDER_TYPE_ACTIVE,
60 PLACEHOLDER_TYPE_INACTIVE,
64 * @brief A Text Controller is used by UI Controls which display text.
66 * It manipulates the Logical & Visual text models on behalf of the UI Controls.
67 * It provides a view of the text that can be used by rendering back-ends.
69 * For selectable/editable UI controls, the controller handles input events from the UI control
70 * and decorations (grab handles etc) via an interface.
72 class Controller : public RefObject, public Decorator::ControllerInterface
77 * @brief Text related operations to be done in the relayout process.
81 NO_OPERATION = 0x0000,
82 CONVERT_TO_UTF32 = 0x0001,
84 VALIDATE_FONTS = 0x0004,
85 GET_LINE_BREAKS = 0x0008,
86 GET_WORD_BREAKS = 0x0010,
89 GET_GLYPH_METRICS = 0x0080,
91 UPDATE_ACTUAL_SIZE = 0x0200,
94 ALL_OPERATIONS = 0xFFFF
98 * @brief Used to distinguish between regular key events and IMF events
107 * @brief Create a new instance of a Controller.
109 * @param[in] controlInterface An interface used to request a text relayout.
110 * @return A pointer to a new Controller.
112 static ControllerPtr New( ControlInterface& controlInterface );
115 * @brief Called to enable text input.
117 * @note Selectable or editable controls should call this once after Controller::New().
118 * @param[in] decorator Used to create cursor, selection handle decorations etc.
120 void EnableTextInput( DecoratorPtr decorator );
123 * @brief Replaces any text previously set.
125 * @note This will be converted into UTF-32 when stored in the text model.
126 * @param[in] text A string of UTF-8 characters.
128 void SetText( const std::string& text );
131 * @brief Retrieve any text previously set.
133 * @return A string of UTF-8 characters.
135 void GetText( std::string& text ) const;
138 * @brief Replaces any placeholder text previously set.
140 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
141 * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
142 * @return True if the remove was successful.
144 bool RemoveText( int cursorOffset, int numberOfChars );
147 * @brief Retrieve the current cursor position.
149 * @return The cursor position.
151 unsigned int GetLogicalCursorPosition() const;
154 * @brief Replaces any placeholder text previously set.
156 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
157 * @param[in] text A string of UTF-8 characters.
159 void SetPlaceholderText( PlaceholderType type, const std::string& text );
162 * @brief Retrieve any placeholder text previously set.
164 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
165 * @param[out] A string of UTF-8 characters.
167 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
170 * @brief Sets the maximum number of characters that can be inserted into the TextModel
172 * @param[in] maxCharacters maximum number of characters to be accepted
174 void SetMaximumNumberOfCharacters( int maxCharacters );
177 * @brief Sets the maximum number of characters that can be inserted into the TextModel
179 * @param[in] maxCharacters maximum number of characters to be accepted
181 int GetMaximumNumberOfCharacters();
184 * @brief Set the default font family.
186 * @param[in] defaultFontFamily The default font family.
188 void SetDefaultFontFamily( const std::string& defaultFontFamily );
191 * @brief Retrieve the default font family.
193 * @return The default font family.
195 const std::string& GetDefaultFontFamily() const;
198 * @brief Set the default font style.
200 * @param[in] defaultFontStyle The default font style.
202 void SetDefaultFontStyle( const std::string& defaultFontStyle );
205 * @brief Retrieve the default font style.
207 * @return The default font style.
209 const std::string& GetDefaultFontStyle() const;
212 * @brief Set the default point size.
214 * @param[in] defaultFontStyle The default point size.
216 void SetDefaultPointSize( float pointSize );
219 * @brief Retrieve the default point size.
221 * @return The default point size.
223 float GetDefaultPointSize() const;
226 * @brief Set the text color
228 * @param textColor The text color
230 void SetTextColor( const Vector4& textColor );
233 * @brief Retrieve the text color
235 * @return The text color
237 const Vector4& GetTextColor() const;
240 * @brief Set the text color
242 * @param textColor The text color
244 void SetPlaceholderTextColor( const Vector4& textColor );
247 * @brief Retrieve the text color
249 * @return The text color
251 const Vector4& GetPlaceholderTextColor() const;
254 * @brief Set the shadow offset.
256 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
258 void SetShadowOffset( const Vector2& shadowOffset );
261 * @brief Retrieve the shadow offset.
263 * @return The shadow offset.
265 const Vector2& GetShadowOffset() const;
268 * @brief Set the shadow color.
270 * @param[in] shadowColor The shadow color.
272 void SetShadowColor( const Vector4& shadowColor );
275 * @brief Retrieve the shadow color.
277 * @return The shadow color.
279 const Vector4& GetShadowColor() const;
282 * @brief Set the underline color.
284 * @param[in] color color of underline.
286 void SetUnderlineColor( const Vector4& color );
289 * @brief Retrieve the underline color.
291 * @return The underline color.
293 const Vector4& GetUnderlineColor() const;
296 * @brief Set the underline enabled flag.
298 * @param[in] enabled The underline enabled flag.
300 void SetUnderlineEnabled( bool enabled );
303 * @brief Returns whether the text is underlined or not.
305 * @return The underline state.
307 bool IsUnderlineEnabled() const;
310 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
312 * @param[in] height The height in pixels of the underline
314 void SetUnderlineHeight( float height );
317 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
319 * @return The height of the underline, or 0 if height is not overrided.
321 float GetUnderlineHeight() const;
324 * @brief Called to enable/disable cursor blink.
326 * @note Only editable controls should calls this.
327 * @param[in] enabled Whether the cursor should blink or not.
329 void SetEnableCursorBlink( bool enable );
332 * @brief Query whether cursor blink is enabled.
334 * @return Whether the cursor should blink or not.
336 bool GetEnableCursorBlink() const;
339 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
341 * @return The scroll position.
343 const Vector2& GetScrollPosition() const;
346 * @brief Query the alignment offset.
348 * @return The alignmnet offset.
350 const Vector2& GetAlignmentOffset() const;
353 * @copydoc Control::GetNaturalSize()
355 Vector3 GetNaturalSize();
358 * @copydoc Control::GetHeightForWidth()
360 float GetHeightForWidth( float width );
363 * @brief Triggers a relayout which updates View (if necessary).
365 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
366 * @param[in] size A the size of a bounding box to layout text within.
367 * @return True if the text model or decorations were updated.
369 bool Relayout( const Size& size );
372 * @brief Process queued events which modify the model.
374 void ProcessModifyEvents();
377 * @brief Used to remove placeholder text.
382 * @brief Used to reset the cursor position after setting a new text.
384 * @param[in] cursorIndex Where to place the cursor.
386 void ResetCursorPosition( CharacterIndex cursorIndex );
389 * @brief Used to reset the scroll position after setting a new text.
391 void ResetScrollPosition();
394 * @brief Used to process an event queued from SetText()
396 void TextReplacedEvent();
399 * @brief Used to process an event queued from key events etc.
401 void TextInsertedEvent();
404 * @brief Used to process an event queued from backspace key etc.
406 void TextDeletedEvent();
409 * @brief Lays-out the text.
411 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
413 * @param[in] size A the size of a bounding box to layout text within.
414 * @param[in] operations The layout operations which need to be done.
415 * @param[out] layoutSize The size of the laid-out text.
417 bool DoRelayout( const Size& size,
418 OperationsMask operations,
422 * @brief Whether to enable the multi-line layout.
424 * @param[in] enable \e true enables the multi-line (by default)
426 void SetMultiLineEnabled( bool enable );
429 * @return Whether the multi-line layout is enabled.
431 bool IsMultiLineEnabled() const;
434 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
436 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
439 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
441 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
444 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
446 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
449 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
451 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
454 * @brief Calulates the alignment of the whole text inside the bounding box.
456 * @param[in] size The size of the bounding box.
458 void CalculateTextAlignment( const Size& size );
461 * @brief Return the layout engine.
463 * @return A reference to the layout engine.
465 LayoutEngine& GetLayoutEngine();
468 * @brief Return a view of the text.
470 * @return A reference to the view.
474 // Text-input Event Queuing
477 * @brief Caller by editable UI controls when keyboard focus is gained.
479 void KeyboardFocusGainEvent();
482 * @brief Caller by editable UI controls when focus is lost.
484 void KeyboardFocusLostEvent();
487 * @brief Caller by editable UI controls when key events are received.
489 * @param[in] event The key event.
490 * @param[in] type Used to distinguish between regular key events and IMF events.
492 bool KeyEvent( const Dali::KeyEvent& event );
495 * @brief Caller by editable UI controls when key events are received.
497 * @param[in] text The text to insert.
498 * @param[in] type Used to distinguish between regular key events and IMF events.
500 void InsertText( const std::string& text, InsertType type );
503 * @brief Caller by editable UI controls when a tap gesture occurs.
504 * @param[in] tapCount The number of taps.
505 * @param[in] x The x position relative to the top-left of the parent control.
506 * @param[in] y The y position relative to the top-left of the parent control.
508 void TapEvent( unsigned int tapCount, float x, float y );
511 * @brief Caller by editable UI controls when a pan gesture occurs.
513 * @param[in] state The state of the gesture.
514 * @param[in] displacement This distance panned since the last pan gesture.
516 void PanEvent( Gesture::State state, const Vector2& displacement );
519 * @brief Event received from IMF manager
521 * @param[in] imfManager The IMF manager.
522 * @param[in] imfEvent The event received.
523 * @return A data struture indicating if update is needed, cursor position and current text.
525 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
528 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
530 virtual void GetTargetSize( Vector2& targetSize );
533 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
535 virtual void AddDecoration( Actor& actor );
538 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
540 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
545 * @brief A reference counted object may only be deleted by calling Unreference().
547 virtual ~Controller();
552 * @brief Helper to clear font-specific data.
554 void ShowPlaceholderText();
557 * @brief Helper to clear all the model data except for LogicalModel::mText.
559 void ClearModelData();
562 * @brief Helper to clear font-specific data (only).
564 void ClearFontData();
567 * @brief Private constructor.
569 Controller( ControlInterface& controlInterface );
572 Controller( const Controller& handle );
575 Controller& operator=( const Controller& handle );
585 } // namespace Toolkit
589 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__