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 Replaces any text previously set.
115 * @note This will be converted into UTF-32 when stored in the text model.
116 * @param[in] text A string of UTF-8 characters.
118 void SetText( const std::string& text );
121 * @brief Retrieve any text previously set.
123 * @return A string of UTF-8 characters.
125 void GetText( std::string& text ) const;
128 * @brief Replaces any placeholder text previously set.
130 * @param[in] cursorOffset Start position from the current cursor position to start deleting characters.
131 * @param[in] numberOfChars The number of characters to delete from the cursorOffset.
132 * @return True if the remove was successful.
134 bool RemoveText( int cursorOffset, int numberOfChars );
137 * @brief Retrieve the current cursor position.
139 * @return The cursor position.
141 unsigned int GetLogicalCursorPosition() const;
144 * @brief Replaces any placeholder text previously set.
146 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
147 * @param[in] text A string of UTF-8 characters.
149 void SetPlaceholderText( PlaceholderType type, const std::string& text );
152 * @brief Retrieve any placeholder text previously set.
154 * @param[in] type Different placeholder-text can be shown when the control is active/inactive.
155 * @param[out] A string of UTF-8 characters.
157 void GetPlaceholderText( PlaceholderType type, std::string& text ) const;
160 * @brief Sets the maximum number of characters that can be inserted into the TextModel
162 * @param[in] maxCharacters maximum number of characters to be accepted
164 void SetMaximumNumberOfCharacters( int maxCharacters );
167 * @brief Sets the maximum number of characters that can be inserted into the TextModel
169 * @param[in] maxCharacters maximum number of characters to be accepted
171 int GetMaximumNumberOfCharacters();
174 * @brief Set the default font family.
176 * @param[in] defaultFontFamily The default font family.
178 void SetDefaultFontFamily( const std::string& defaultFontFamily );
181 * @brief Retrieve the default font family.
183 * @return The default font family.
185 const std::string& GetDefaultFontFamily() const;
188 * @brief Set the default font style.
190 * @param[in] defaultFontStyle The default font style.
192 void SetDefaultFontStyle( const std::string& defaultFontStyle );
195 * @brief Retrieve the default font style.
197 * @return The default font style.
199 const std::string& GetDefaultFontStyle() const;
202 * @brief Set the default point size.
204 * @param[in] defaultFontStyle The default point size.
206 void SetDefaultPointSize( float pointSize );
209 * @brief Retrieve the default point size.
211 * @return The default point size.
213 float GetDefaultPointSize() const;
216 * @brief Set the text color
218 * @param textColor The text color
220 void SetTextColor( const Vector4& textColor );
223 * @brief Retrieve the text color
225 * @return The text color
227 const Vector4& GetTextColor() const;
230 * @brief Set the text color
232 * @param textColor The text color
234 void SetPlaceholderTextColor( const Vector4& textColor );
237 * @brief Retrieve the text color
239 * @return The text color
241 const Vector4& GetPlaceholderTextColor() const;
244 * @brief Set the shadow offset.
246 * @param[in] shadowOffset The shadow offset, 0,0 indicates no shadow.
248 void SetShadowOffset( const Vector2& shadowOffset );
251 * @brief Retrieve the shadow offset.
253 * @return The shadow offset.
255 const Vector2& GetShadowOffset() const;
258 * @brief Set the shadow color.
260 * @param[in] shadowColor The shadow color.
262 void SetShadowColor( const Vector4& shadowColor );
265 * @brief Retrieve the shadow color.
267 * @return The shadow color.
269 const Vector4& GetShadowColor() const;
272 * @brief Set the underline color.
274 * @param[in] color color of underline.
276 void SetUnderlineColor( const Vector4& color );
279 * @brief Retrieve the underline color.
281 * @return The underline color.
283 const Vector4& GetUnderlineColor() const;
286 * @brief Set the underline enabled flag.
288 * @param[in] enabled The underline enabled flag.
290 void SetUnderlineEnabled( bool enabled );
293 * @brief Returns whether the text is underlined or not.
295 * @return The underline state.
297 bool IsUnderlineEnabled() const;
300 * @brief Set the override used for underline height, 0 indicates height will be supplied by font metrics
302 * @param[in] height The height in pixels of the underline
304 void SetUnderlineHeight( float height );
307 * @brief Retrieves the override height of an underline, 0 indicates height is supplied by font metrics
309 * @return The height of the underline, or 0 if height is not overrided.
311 float GetUnderlineHeight() const;
314 * @brief Called to enable text input.
316 * @note Only selectable or editable controls should calls this.
317 * @param[in] decorator Used to create cursor, selection handle decorations etc.
319 void EnableTextInput( DecoratorPtr decorator );
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 Private constructor.
489 Controller( ControlInterface& controlInterface );
492 Controller( const Controller& handle );
495 Controller& operator=( const Controller& handle );
505 } // namespace Toolkit
509 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__