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
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 Replaces any text previously set.
129 * @note This will be converted into UTF-32 when stored in the text model.
130 * @param[in] text A string of UTF-8 characters.
132 void SetText( const std::string& text );
135 * @brief Retrieve any text previously set.
137 * @return A string of UTF-8 characters.
139 void GetText( std::string& text ) const;
142 * @brief Remove a given number of characters
144 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
145 * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
146 * @return True if the remove was successful.
148 bool RemoveText( int cursorOffset, int numberOfChars );
151 * @brief Retrieve the current cursor position.
153 * @return The cursor position.
155 unsigned int GetLogicalCursorPosition() const;
158 * @brief Replaces any placeholder text previously set.
160 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
161 * @param[in] text A string of UTF-8 characters.
163 void SetPlaceholderText( PlaceholderType type, const std::string& text );
166 * @brief Retrieve any placeholder text previously set.
168 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
169 * @param[out] A string of UTF-8 characters.
171 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
174 * @brief Sets the maximum number of characters that can be inserted into the TextModel
176 * @param[in] maxCharacters maximum number of characters to be accepted
178 void SetMaximumNumberOfCharacters( int maxCharacters );
181 * @brief Sets the maximum number of characters that can be inserted into the TextModel
183 * @param[in] maxCharacters maximum number of characters to be accepted
185 int GetMaximumNumberOfCharacters();
188 * @brief Set the default font family.
190 * @param[in] defaultFontFamily The default font family.
192 void SetDefaultFontFamily( const std::string& defaultFontFamily );
195 * @brief Retrieve the default font family.
197 * @return The default font family.
199 const std::string& GetDefaultFontFamily() const;
202 * @brief Set the default font style.
204 * @param[in] defaultFontStyle The default font style.
206 void SetDefaultFontStyle( const std::string& defaultFontStyle );
209 * @brief Retrieve the default font style.
211 * @return The default font style.
213 const std::string& GetDefaultFontStyle() const;
216 * @brief Set the default point size.
218 * @param[in] defaultFontStyle The default point size.
220 void SetDefaultPointSize( float pointSize );
223 * @brief Retrieve the default point size.
225 * @return The default point size.
227 float GetDefaultPointSize() const;
230 * @brief Set the text color
232 * @param textColor The text color
234 void SetTextColor( const Vector4& textColor );
237 * @brief Retrieve the text color
239 * @return The text color
241 const Vector4& GetTextColor() const;
244 * @brief Set the text color
246 * @param textColor The text color
248 void SetPlaceholderTextColor( const Vector4& textColor );
251 * @brief Retrieve the text color
253 * @return The text color
255 const Vector4& GetPlaceholderTextColor() const;
258 * @brief Set the shadow offset.
260 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
262 void SetShadowOffset( const Vector2& shadowOffset );
265 * @brief Retrieve the shadow offset.
267 * @return The shadow offset.
269 const Vector2& GetShadowOffset() const;
272 * @brief Set the shadow color.
274 * @param[in] shadowColor The shadow color.
276 void SetShadowColor( const Vector4& shadowColor );
279 * @brief Retrieve the shadow color.
281 * @return The shadow color.
283 const Vector4& GetShadowColor() const;
286 * @brief Set the underline color.
288 * @param[in] color color of underline.
290 void SetUnderlineColor( const Vector4& color );
293 * @brief Retrieve the underline color.
295 * @return The underline color.
297 const Vector4& GetUnderlineColor() const;
300 * @brief Set the underline enabled flag.
302 * @param[in] enabled The underline enabled flag.
304 void SetUnderlineEnabled( bool enabled );
307 * @brief Returns whether the text is underlined or not.
309 * @return The underline state.
311 bool IsUnderlineEnabled() const;
314 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
316 * @param[in] height The height in pixels of the underline
318 void SetUnderlineHeight( float height );
321 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
323 * @return The height of the underline, or 0 if height is not overrided.
325 float GetUnderlineHeight() const;
328 * @brief Called to enable/disable cursor blink.
330 * @note Only editable controls should calls this.
331 * @param[in] enabled Whether the cursor should blink or not.
333 void SetEnableCursorBlink( bool enable );
336 * @brief Query whether cursor blink is enabled.
338 * @return Whether the cursor should blink or not.
340 bool GetEnableCursorBlink() const;
343 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
345 * @return The scroll position.
347 const Vector2& GetScrollPosition() const;
350 * @brief Query the alignment offset.
352 * @return The alignmnet offset.
354 const Vector2& GetAlignmentOffset() const;
357 * @copydoc Control::GetNaturalSize()
359 Vector3 GetNaturalSize();
362 * @copydoc Control::GetHeightForWidth()
364 float GetHeightForWidth( float width );
367 * @brief Triggers a relayout which updates View (if necessary).
369 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
370 * @param[in] size A the size of a bounding box to layout text within.
371 * @return True if the text model or decorations were updated.
373 bool Relayout( const Size& size );
376 * @brief Process queued events which modify the model.
378 void ProcessModifyEvents();
381 * @brief Used to remove placeholder text.
386 * @brief Used to reset the cursor position after setting a new text.
388 * @param[in] cursorIndex Where to place the cursor.
390 void ResetCursorPosition( CharacterIndex cursorIndex );
393 * @brief Used to reset the scroll position after setting a new text.
395 void ResetScrollPosition();
398 * @brief Used to process an event queued from SetText()
400 void TextReplacedEvent();
403 * @brief Used to process an event queued from key events etc.
405 void TextInsertedEvent();
408 * @brief Used to process an event queued from backspace key etc.
410 void TextDeletedEvent();
413 * @brief Lays-out the text.
415 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
417 * @param[in] size A the size of a bounding box to layout text within.
418 * @param[in] operations The layout operations which need to be done.
419 * @param[out] layoutSize The size of the laid-out text.
421 bool DoRelayout( const Size& size,
422 OperationsMask operations,
426 * @brief Whether to enable the multi-line layout.
428 * @param[in] enable \e true enables the multi-line (by default)
430 void SetMultiLineEnabled( bool enable );
433 * @return Whether the multi-line layout is enabled.
435 bool IsMultiLineEnabled() const;
438 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetHorizontalAlignment()
440 void SetHorizontalAlignment( LayoutEngine::HorizontalAlignment alignment );
443 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetHorizontalAlignment()
445 LayoutEngine::HorizontalAlignment GetHorizontalAlignment() const;
448 * @copydoc Dali::Toolkit::Text::LayoutEngine::SetVerticalAlignment()
450 void SetVerticalAlignment( LayoutEngine::VerticalAlignment alignment );
453 * @copydoc Dali::Toolkit::Text::LayoutEngine::GetVerticalAlignment()
455 LayoutEngine::VerticalAlignment GetVerticalAlignment() const;
458 * @brief Calulates the alignment of the whole text inside the bounding box.
460 * @param[in] size The size of the bounding box.
462 void CalculateTextAlignment( const Size& size );
465 * @brief Return the layout engine.
467 * @return A reference to the layout engine.
469 LayoutEngine& GetLayoutEngine();
472 * @brief Return a view of the text.
474 * @return A reference to the view.
478 // Text-input Event Queuing
481 * @brief Called by editable UI controls when keyboard focus is gained.
483 void KeyboardFocusGainEvent();
486 * @brief Called by editable UI controls when focus is lost.
488 void KeyboardFocusLostEvent();
491 * @brief Called by editable UI controls when key events are received.
493 * @param[in] event The key event.
494 * @param[in] type Used to distinguish between regular key events and IMF events.
496 bool KeyEvent( const Dali::KeyEvent& event );
499 * @brief Called by editable UI controls when key events are received.
501 * @param[in] text The text to insert.
502 * @param[in] type Used to distinguish between regular key events and IMF events.
504 void InsertText( const std::string& text, InsertType type );
507 * @brief Called by editable UI controls when a tap gesture occurs.
508 * @param[in] tapCount The number of taps.
509 * @param[in] x The x position relative to the top-left of the parent control.
510 * @param[in] y The y position relative to the top-left of the parent control.
512 void TapEvent( unsigned int tapCount, float x, float y );
515 * @brief Called by editable UI controls when a pan gesture occurs.
517 * @param[in] state The state of the gesture.
518 * @param[in] displacement This distance panned since the last pan gesture.
520 void PanEvent( Gesture::State state, const Vector2& displacement );
523 * @brief Creates a selection event.
525 * It could be called from the TapEvent (double tap) or when the text selection popup's sellect all button is pressed.
527 * @param[in] x The x position relative to the top-left of the parent control.
528 * @param[in] y The y position relative to the top-left of the parent control.
529 * @param[in] selectAll Whether the whole text is selected.
531 void SelectEvent( float x, float y, bool selectAll );
534 * @brief Event received from IMF manager
536 * @param[in] imfManager The IMF manager.
537 * @param[in] imfEvent The event received.
538 * @return A data struture indicating if update is needed, cursor position and current text.
540 ImfManager::ImfCallbackData OnImfEvent( ImfManager& imfManager, const ImfManager::ImfEventData& imfEvent );
543 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::GetTargetSize()
545 virtual void GetTargetSize( Vector2& targetSize );
548 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::AddDecoration()
550 virtual void AddDecoration( Actor& actor, bool needsClipping );
553 * @copydoc Dali::Toolkit::Text::Decorator::ControllerInterface::DecorationEvent()
555 virtual void DecorationEvent( HandleType handle, HandleState state, float x, float y );
558 * @copydoc Dali::Toolkit::TextSelectionPopup::TextPopupButtonCallbackInterface::TextPopupButtonTouched()
560 virtual void TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::Buttons button );
565 * @brief A reference counted object may only be deleted by calling Unreference().
567 virtual ~Controller();
572 * @brief Helper to clear font-specific data.
574 void ShowPlaceholderText();
577 * @brief Helper to clear all the model data except for LogicalModel::mText.
579 void ClearModelData();
582 * @brief Helper to clear font-specific data (only).
584 void ClearFontData();
587 * @brief Private constructor.
589 Controller( ControlInterface& controlInterface );
592 Controller( const Controller& handle );
595 Controller& operator=( const Controller& handle );
605 } // namespace Toolkit
609 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__