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/public-api/common/dali-vector.h>
24 #include <dali/public-api/common/intrusive-ptr.h>
25 #include <dali/public-api/events/gesture.h>
26 #include <dali/public-api/events/key-event.h>
27 #include <dali/public-api/math/vector3.h>
28 #include <dali/public-api/math/vector2.h>
29 #include <dali/public-api/object/ref-object.h>
32 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
33 #include <dali-toolkit/internal/text/font-run.h>
34 #include <dali-toolkit/internal/text/text-control-interface.h>
35 #include <dali-toolkit/internal/text/text-view.h>
49 typedef IntrusivePtr<Controller> ControllerPtr;
50 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
53 * @brief A Text Controller is used by UI Controls which display text.
55 * It manipulates the Logical & Visual text models on behalf of the UI Controls.
56 * It provides a view of the text that can be used by rendering back-ends.
58 * For selectable/editable UI controls, the controller handles input events from the UI control
59 * and decorations (grab handles etc) via an observer interface.
61 class Controller : public RefObject, public Decorator::Observer
66 * @brief Text related operations to be done in the relayout process.
70 NO_OPERATION = 0x0000,
71 CONVERT_TO_UTF32 = 0x0001,
73 VALIDATE_FONTS = 0x0004,
74 GET_LINE_BREAKS = 0x0008,
75 GET_WORD_BREAKS = 0x0010,
78 GET_GLYPH_METRICS = 0x0080,
80 UPDATE_ACTUAL_SIZE = 0x0200,
83 ALL_OPERATIONS = 0xFFFF
89 * @brief Create a new instance of a Controller.
91 * @param[in] controlInterface An interface used to request a text relayout.
92 * @return A pointer to a new Controller.
94 static ControllerPtr New( ControlInterface& controlInterface );
97 * @brief Replaces any text previously set.
99 * @note This will be converted into UTF-32 when stored in the text model.
100 * @param[in] text A string of UTF-8 characters.
102 void SetText( const std::string& text );
105 * @brief Retrieve any text previously set.
107 * @return A string of UTF-8 characters.
109 void GetText( std::string& text ) const;
112 * @brief Replaces any placeholder text previously set.
114 * @param[in] text A string of UTF-8 characters.
116 void SetPlaceholderText( const std::string& text );
119 * @brief Retrieve any placeholder text previously set.
121 * @return A string of UTF-8 characters.
123 void GetPlaceholderText( std::string& text ) const;
126 * @brief Set the default font family.
128 * @param[in] defaultFontFamily The default font family.
130 void SetDefaultFontFamily( const std::string& defaultFontFamily );
133 * @brief Retrieve the default font family.
135 * @return The default font family.
137 const std::string& GetDefaultFontFamily() const;
140 * @brief Set the default font style.
142 * @param[in] defaultFontStyle The default font style.
144 void SetDefaultFontStyle( const std::string& defaultFontStyle );
147 * @brief Retrieve the default font style.
149 * @return The default font style.
151 const std::string& GetDefaultFontStyle() const;
154 * @brief Set the default point size.
156 * @param[in] defaultFontStyle The default point size.
158 void SetDefaultPointSize( float pointSize );
161 * @brief Retrieve the default point size.
163 * @return The default point size.
165 float GetDefaultPointSize() const;
168 * @brief Retrieve the default fonts.
170 * @param[out] fonts The default font family, style and point sizes.
171 * @param[in] numberOfCharacters The number of characters in the logical model.
173 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters ) const;
176 * @brief Set the text color
178 * @param textColor The text color
180 void SetTextColor( const Vector4& textColor );
183 * @brief Retrieve the text color
185 * @return The text color
187 const Vector4& GetTextColor() const;
190 * @brief Set the shadow offset.
192 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
194 void SetShadowOffset( const Vector2& shadowOffset );
197 * @brief Retrieve the shadow offset.
199 * @return The shadow offset.
201 const Vector2& GetShadowOffset() const;
204 * @brief Set the shadow color.
206 * @param[in] shadowColor The shadow color.
208 void SetShadowColor( const Vector4& shadowColor );
211 * @brief Retrieve the shadow color.
213 * @return The shadow color.
215 const Vector4& GetShadowColor() const;
218 * @brief Set the underline color.
220 * @param[in] color color of underline.
222 void SetUnderlineColor( const Vector4& color );
225 * @brief Retrieve the underline color.
227 * @return The underline color.
229 const Vector4& GetUnderlineColor() const;
232 * @brief Set the underline enabled flag.
234 * @param[in] enabled The underline enabled flag.
236 void SetUnderlineEnabled( bool enabled );
239 * @brief Returns whether the text is underlined or not.
241 * @return The underline state.
243 bool IsUnderlineEnabled() const;
246 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
248 * @param[in] height The height in pixels of the underline
250 void SetUnderlineHeight( float height );
253 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
255 * @return The height of the underline, or 0 if height is not overrided.
257 float GetUnderlineHeight() const;
260 * @brief Called to enable text input.
262 * @note Only selectable or editable controls should calls this.
263 * @param[in] decorator Used to create cursor, selection handle decorations etc.
265 void EnableTextInput( DecoratorPtr decorator );
268 * @brief Called to enable/disable cursor blink.
270 * @note Only editable controls should calls this.
271 * @param[in] enabled Whether the cursor should blink or not.
273 void SetEnableCursorBlink( bool enable );
276 * @brief Query whether cursor blink is enabled.
278 * @return Whether the cursor should blink or not.
280 bool GetEnableCursorBlink() const;
283 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
285 * @return The scroll position.
287 const Vector2& GetScrollPosition() const;
290 * @brief Query the alignment offset.
292 * @return The alignmnet offset.
294 const Vector2& GetAlignmentOffset() const;
297 * @copydoc Control::GetNaturalSize()
299 Vector3 GetNaturalSize();
302 * @copydoc Control::GetHeightForWidth()
304 float GetHeightForWidth( float width );
307 * @brief Triggers a relayout which updates View (if necessary).
309 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
310 * @param[in] size A the size of a bounding box to layout text within.
311 * @return True if the text model or decorations were updated.
313 bool Relayout( const Size& size );
316 * @brief Process queued events which modify the model.
318 void ProcessModifyEvents();
321 * @brief Used to process an event queued from SetText()
323 * @param[in] newText The new text to store in the logical model.
325 void ReplaceTextEvent( const std::string& newText );
328 * @brief Used to process an event queued from key events etc.
330 * @param[in] text The text to insert into the logical model.
332 void InsertTextEvent( const std::string& text );
335 * @brief Used to process an event queued from backspace key etc.
337 void DeleteTextEvent();
340 * @brief Update the model following text replace/insert etc.
342 * @param[in] operationsRequired The layout operations which need to be done.
344 void UpdateModel( OperationsMask operationsRequired );
347 * @brief Lays-out the text.
349 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
351 * @param[in] size A the size of a bounding box to layout text within.
352 * @param[in] operations The layout operations which need to be done.
353 * @param[out] layoutSize The size of the laid-out text.
355 bool DoRelayout( const Size& size,
356 OperationsMask operations,
360 * @brief Calulates the alignment of the whole text inside the bounding box.
362 * @param[in] size The size of the bounding box.
364 void CalculateTextAlignment( const Size& size );
367 * @brief Return the layout engine.
369 * @return A reference to the layout engine.
371 LayoutEngine& GetLayoutEngine();
374 * @brief Return a view of the text.
376 * @return A reference to the view.
380 // Text-input Event Queuing
383 * @brief Caller by editable UI controls when keyboard focus is gained.
385 void KeyboardFocusGainEvent();
388 * @brief Caller by editable UI controls when focus is lost.
390 void KeyboardFocusLostEvent();
393 * @brief Caller by editable UI controls when key events are received.
395 * @param[in] event The key event.
397 bool KeyEvent( const Dali::KeyEvent& event );
400 * @brief Caller by editable UI controls when a tap gesture occurs.
401 * @param[in] tapCount The number of taps.
402 * @param[in] x The x position relative to the top-left of the parent control.
403 * @param[in] y The y position relative to the top-left of the parent control.
405 void TapEvent( unsigned int tapCount, float x, float y );
408 * @brief Caller by editable UI controls when a pan gesture occurs.
410 * @param[in] state The state of the gesture.
411 * @param[in] displacement This distance panned since the last pan gesture.
413 void PanEvent( Gesture::State state, const Vector2& displacement );
416 * @copydoc Dali::Toolkit::Text::Decorator::Observer::GrabHandleEvent()
418 virtual void GrabHandleEvent( GrabHandleState state, float x, float y );
423 * @brief A reference counted object may only be deleted by calling Unreference().
425 virtual ~Controller();
430 * @brief Request a relayout using the ControlInterface.
432 void RequestRelayout();
435 * @brief Private constructor.
437 Controller( ControlInterface& controlInterface );
440 Controller( const Controller& handle );
443 Controller& operator=( const Controller& handle );
450 // Avoid allocating this when the user does not specify a font
453 // Avoid allocating this for non-editable controls
459 } // namespace Toolkit
463 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__