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/key-event.h>
26 #include <dali/public-api/math/vector3.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/object/ref-object.h>
31 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
32 #include <dali-toolkit/internal/text/font-run.h>
33 #include <dali-toolkit/internal/text/text-control-interface.h>
34 #include <dali-toolkit/internal/text/text-view.h>
48 typedef IntrusivePtr<Controller> ControllerPtr;
49 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
52 * @brief A Text Controller is used by UI Controls which display text.
54 * It manipulates the Logical & Visual text models on behalf of the UI Controls.
55 * It provides a view of the text that can be used by rendering back-ends.
57 * For selectable/editable UI controls, the controller handles input events from the UI control
58 * and decorations (grab handles etc) via an observer interface.
60 class Controller : public RefObject, public Decorator::Observer
65 * @brief Text related operations to be done in the relayout process.
69 NO_OPERATION = 0x0000,
70 CONVERT_TO_UTF32 = 0x0001,
72 VALIDATE_FONTS = 0x0004,
73 GET_LINE_BREAKS = 0x0008,
74 GET_WORD_BREAKS = 0x0010,
77 GET_GLYPH_METRICS = 0x0080,
79 UPDATE_ACTUAL_SIZE = 0x0200,
82 ALL_OPERATIONS = 0xFFFF
88 * @brief Create a new instance of a Controller.
90 * @param[in] controlInterface An interface used to request a text relayout.
91 * @return A pointer to a new Controller.
93 static ControllerPtr New( ControlInterface& controlInterface );
96 * @brief Replaces any text previously set.
98 * @note This will be converted into UTF-32 when stored in the text model.
99 * @param[in] text A string of UTF-8 characters.
101 void SetText( const std::string& text );
104 * @brief Retrieve any text previously set.
106 * @return A string of UTF-8 characters.
108 void GetText( std::string& text ) const;
111 * @brief Replaces any placeholder text previously set.
113 * @param[in] text A string of UTF-8 characters.
115 void SetPlaceholderText( const std::string& text );
118 * @brief Retrieve any placeholder text previously set.
120 * @return A string of UTF-8 characters.
122 void GetPlaceholderText( std::string& text ) const;
125 * @brief Set the default font family.
127 * @param[in] defaultFontFamily The default font family.
129 void SetDefaultFontFamily( const std::string& defaultFontFamily );
132 * @brief Retrieve the default font family.
134 * @return The default font family.
136 const std::string& GetDefaultFontFamily() const;
139 * @brief Set the default font style.
141 * @param[in] defaultFontStyle The default font style.
143 void SetDefaultFontStyle( const std::string& defaultFontStyle );
146 * @brief Retrieve the default font style.
148 * @return The default font style.
150 const std::string& GetDefaultFontStyle() const;
153 * @brief Set the default point size.
155 * @param[in] defaultFontStyle The default point size.
157 void SetDefaultPointSize( float pointSize );
160 * @brief Retrieve the default point size.
162 * @return The default point size.
164 float GetDefaultPointSize() const;
167 * @brief Retrieve the default fonts.
169 * @param[out] fonts The default font family, style and point sizes.
170 * @param[in] numberOfCharacters The number of characters in the logical model.
172 void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
175 * @brief Called to enable text input.
177 * @note Only selectable or editable controls should calls this.
178 * @param[in] decorator Used to create cursor, selection handle decorations etc.
180 void EnableTextInput( DecoratorPtr decorator );
183 * @brief Called to enable/disable cursor blink.
185 * @note Only editable controls should calls this.
186 * @param[in] enabled Whether the cursor should blink or not.
188 void SetEnableCursorBlink( bool enable );
191 * @brief Query whether cursor blink is enabled.
193 * @return Whether the cursor should blink or not.
195 bool GetEnableCursorBlink() const;
198 * @copydoc Control::GetNaturalSize()
200 Vector3 GetNaturalSize();
203 * @copydoc Control::GetHeightForWidth()
205 float GetHeightForWidth( float width );
208 * @brief Triggers a relayout which updates View (if necessary).
210 * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
211 * @param[in] size A the size of a bounding box to layout text within.
212 * @return True if the text model or decorations were updated.
214 bool Relayout( const Vector2& size );
217 * @brief Process queued events which modify the model.
219 void ProcessModifyEvents();
222 * @brief Used to process an event queued from SetText()
224 * @param[in] newText The new text to store in the logical model.
226 void ReplaceTextEvent( const std::string& newText );
229 * @brief Used to process an event queued from key events etc.
231 * @param[in] text The text to insert into the logical model.
233 void InsertTextEvent( const std::string& text );
236 * @brief Used to process an event queued from backspace key etc.
238 void DeleteTextEvent();
241 * @brief Update the model following text replace/insert etc.
243 * @param[in] operationsRequired The layout operations which need to be done.
245 void UpdateModel( OperationsMask operationsRequired );
248 * @brief Lays-out the text.
250 * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
252 * @param[in] size A the size of a bounding box to layout text within.
253 * @param[in] operations The layout operations which need to be done.
254 * @param[out] layoutSize The size of the laid-out text.
256 bool DoRelayout( const Vector2& size,
257 OperationsMask operations,
261 * @brief Return the layout engine.
263 * @return A reference to the layout engine.
265 LayoutEngine& GetLayoutEngine();
268 * @brief Return a view of the text.
270 * @return A reference to the view.
274 // Text-input Event Queuing
277 * @brief Caller by editable UI controls when keyboard focus is gained.
279 void KeyboardFocusGainEvent();
282 * @brief Caller by editable UI controls when focus is lost.
284 void KeyboardFocusLostEvent();
287 * @brief Caller by editable UI controls when key events are received.
289 * @param[in] event The key event.
291 bool KeyEvent( const Dali::KeyEvent& event );
294 * @brief Caller by editable UI controls when a tap gesture occurs.
295 * @param[in] tapCount The number of taps.
296 * @param[in] x The x position relative to the top-left of the parent control.
297 * @param[in] y The y position relative to the top-left of the parent control.
299 void TapEvent( unsigned int tapCount, float x, float y );
302 * @copydoc Dali::Toolkit::Text::Decorator::Observer::GrabHandleEvent()
304 virtual void GrabHandleEvent( GrabHandleState state, float x, float y );
309 * @brief A reference counted object may only be deleted by calling Unreference().
311 virtual ~Controller();
316 * @brief Request a relayout using the ControlInterface.
318 void RequestRelayout();
321 * @brief Private constructor.
323 Controller( ControlInterface& controlInterface );
326 Controller( const Controller& handle );
329 Controller& operator=( const Controller& handle );
336 // Avoid allocating this when the user does not specify a font
339 // Avoid allocating this for non-editable controls
345 } // namespace Toolkit
349 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__