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 Different placeholder-text can be shown when the control is active/inactive.
57 PLACEHOLDER_TYPE_ACTIVE,
58 PLACEHOLDER_TYPE_INACTIVE,
62 * @brief A Text Controller is used by UI Controls which display text.
64 * It manipulates the Logical & Visual text models on behalf of the UI Controls.
65 * It provides a view of the text that can be used by rendering back-ends.
67 * For selectable/editable UI controls, the controller handles input events from the UI control
68 * and decorations (grab handles etc) via an observer interface.
70 class Controller : public RefObject, public Decorator::Observer
75 * @brief Text related operations to be done in the relayout process.
79 NO_OPERATION = 0x0000,
80 CONVERT_TO_UTF32 = 0x0001,
82 VALIDATE_FONTS = 0x0004,
83 GET_LINE_BREAKS = 0x0008,
84 GET_WORD_BREAKS = 0x0010,
87 GET_GLYPH_METRICS = 0x0080,
89 UPDATE_ACTUAL_SIZE = 0x0200,
92 ALL_OPERATIONS = 0xFFFF
96 * @brief Used to distinguish between regular key events and IMF events
105 * @brief Create a new instance of a Controller.
107 * @param[in] controlInterface An interface used to request a text relayout.
108 * @return A pointer to a new Controller.
110 static ControllerPtr New( ControlInterface& controlInterface );
113 * @brief Called to enable text input.
115 * @note Selectable or editable controls should call this once after Controller::New().
116 * @param[in] decorator Used to create cursor, selection handle decorations etc.
118 void EnableTextInput( DecoratorPtr decorator );
121 * @brief Replaces any text previously set.
123 * @note This will be converted into UTF-32 when stored in the text model.
124 * @param[in] text A string of UTF-8 characters.
126 void SetText( const std::string& text );
129 * @brief Retrieve any text previously set.
131 * @return A string of UTF-8 characters.
133 void GetText( std::string& text ) const;
136 * @brief Replaces any placeholder text previously set.
138 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
139 * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
140 * @return True if the remove was successful.
142 bool RemoveText( int cursorOffset, int numberOfChars );
145 * @brief Retrieve the current cursor position.
147 * @return The cursor position.
149 unsigned int GetLogicalCursorPosition() const;
152 * @brief Replaces any placeholder text previously set.
154 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
155 * @param[in] text A string of UTF-8 characters.
157 void SetPlaceholderText( PlaceholderType type, const std::string& text );
160 * @brief Retrieve any placeholder text previously set.
162 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
163 * @param[out] A string of UTF-8 characters.
165 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
168 * @brief Sets the maximum number of characters that can be inserted into the TextModel
170 * @param[in] maxCharacters maximum number of characters to be accepted
172 void SetMaximumNumberOfCharacters( int maxCharacters );
175 * @brief Sets the maximum number of characters that can be inserted into the TextModel
177 * @param[in] maxCharacters maximum number of characters to be accepted
179 int GetMaximumNumberOfCharacters();
182 * @brief Set the default font family.
184 * @param[in] defaultFontFamily The default font family.
186 void SetDefaultFontFamily( const std::string& defaultFontFamily );
189 * @brief Retrieve the default font family.
191 * @return The default font family.
193 const std::string& GetDefaultFontFamily() const;
196 * @brief Set the default font style.
198 * @param[in] defaultFontStyle The default font style.
200 void SetDefaultFontStyle( const std::string& defaultFontStyle );
203 * @brief Retrieve the default font style.
205 * @return The default font style.
207 const std::string& GetDefaultFontStyle() const;
210 * @brief Set the default point size.
212 * @param[in] defaultFontStyle The default point size.
214 void SetDefaultPointSize( float pointSize );
217 * @brief Retrieve the default point size.
219 * @return The default point size.
221 float GetDefaultPointSize() const;
224 * @brief Set the text color
226 * @param textColor The text color
228 void SetTextColor( const Vector4& textColor );
231 * @brief Retrieve the text color
233 * @return The text color
235 const Vector4& GetTextColor() const;
238 * @brief Set the text color
240 * @param textColor The text color
242 void SetPlaceholderTextColor( const Vector4& textColor );
245 * @brief Retrieve the text color
247 * @return The text color
249 const Vector4& GetPlaceholderTextColor() const;
252 * @brief Set the shadow offset.
254 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
256 void SetShadowOffset( const Vector2& shadowOffset );
259 * @brief Retrieve the shadow offset.
261 * @return The shadow offset.
263 const Vector2& GetShadowOffset() const;
266 * @brief Set the shadow color.
268 * @param[in] shadowColor The shadow color.
270 void SetShadowColor( const Vector4& shadowColor );
273 * @brief Retrieve the shadow color.
275 * @return The shadow color.
277 const Vector4& GetShadowColor() const;
280 * @brief Set the underline color.
282 * @param[in] color color of underline.
284 void SetUnderlineColor( const Vector4& color );
287 * @brief Retrieve the underline color.
289 * @return The underline color.
291 const Vector4& GetUnderlineColor() const;
294 * @brief Set the underline enabled flag.
296 * @param[in] enabled The underline enabled flag.
298 void SetUnderlineEnabled( bool enabled );
301 * @brief Returns whether the text is underlined or not.
303 * @return The underline state.
305 bool IsUnderlineEnabled() const;
308 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
310 * @param[in] height The height in pixels of the underline
312 void SetUnderlineHeight( float height );
315 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
317 * @return The height of the underline, or 0 if height is not overrided.
319 float GetUnderlineHeight() const;
322 * @brief Called to enable/disable cursor blink.
324 * @note Only editable controls should calls this.
325 * @param[in] enabled Whether the cursor should blink or not.
327 void SetEnableCursorBlink( bool enable );
330 * @brief Query whether cursor blink is enabled.
332 * @return Whether the cursor should blink or not.
334 bool GetEnableCursorBlink() const;
337 * @brief Query the current scroll position; the UI control is responsible for moving actors to this position.
339 * @return The scroll position.
341 const Vector2& GetScrollPosition() const;
344 * @brief Query the alignment offset.
346 * @return The alignmnet offset.
348 const Vector2& GetAlignmentOffset() const;
351 * @copydoc Control::GetNaturalSize()
353 Vector3 GetNaturalSize();
356 * @copydoc Control::GetHeightForWidth()
358 float GetHeightForWidth( float width );
361 * @brief Triggers a relayout which updates View (if necessary).
363 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
364 * @param[in] size A the size of a bounding box to layout text within.
365 * @return True if the text model or decorations were updated.
367 bool Relayout( const Size& size );
370 * @brief Process queued events which modify the model.
372 void ProcessModifyEvents();
375 * @brief Used to remove placeholder text.
380 * @brief Used to process an event queued from SetText()
382 void TextReplacedEvent();
385 * @brief Used to process an event queued from key events etc.
387 void TextInsertedEvent();
390 * @brief Used to process an event queued from backspace key etc.
392 void TextDeletedEvent();
395 * @brief Lays-out the text.
397 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
399 * @param[in] size A the size of a bounding box to layout text within.
400 * @param[in] operations The layout operations which need to be done.
401 * @param[out] layoutSize The size of the laid-out text.
403 bool DoRelayout( const Size& size,
404 OperationsMask operations,
408 * @brief Calulates the alignment of the whole text inside the bounding box.
410 * @param[in] size The size of the bounding box.
412 void CalculateTextAlignment( const Size& size );
415 * @brief Return the layout engine.
417 * @return A reference to the layout engine.
419 LayoutEngine& GetLayoutEngine();
422 * @brief Return a view of the text.
424 * @return A reference to the view.
428 // Text-input Event Queuing
431 * @brief Caller by editable UI controls when keyboard focus is gained.
433 void KeyboardFocusGainEvent();
436 * @brief Caller by editable UI controls when focus is lost.
438 void KeyboardFocusLostEvent();
441 * @brief Caller by editable UI controls when key events are received.
443 * @param[in] event The key event.
444 * @param[in] type Used to distinguish between regular key events and IMF events.
446 bool KeyEvent( const Dali::KeyEvent& event );
449 * @brief Caller by editable UI controls when key events are received.
451 * @param[in] text The text to insert.
452 * @param[in] type Used to distinguish between regular key events and IMF events.
454 void InsertText( const std::string& text, InsertType type );
457 * @brief Caller by editable UI controls when a tap gesture occurs.
458 * @param[in] tapCount The number of taps.
459 * @param[in] x The x position relative to the top-left of the parent control.
460 * @param[in] y The y position relative to the top-left of the parent control.
462 void TapEvent( unsigned int tapCount, float x, float y );
465 * @brief Caller by editable UI controls when a pan gesture occurs.
467 * @param[in] state The state of the gesture.
468 * @param[in] displacement This distance panned since the last pan gesture.
470 void PanEvent( Gesture::State state, const Vector2& displacement );
473 * @copydoc Dali::Toolkit::Text::Decorator::Observer::HandleEvent()
475 virtual void HandleEvent( HandleType handle, HandleState state, float x, float y );
480 * @brief A reference counted object may only be deleted by calling Unreference().
482 virtual ~Controller();
487 * @brief Helper to clear font-specific data.
489 void ShowPlaceholderText();
492 * @brief Helper to clear all the model data except for LogicalModel::mText.
494 void ClearModelData();
497 * @brief Helper to clear font-specific data (only).
499 void ClearFontData();
502 * @brief Private constructor.
504 Controller( ControlInterface& controlInterface );
507 Controller( const Controller& handle );
510 Controller& operator=( const Controller& handle );
520 } // namespace Toolkit
524 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__