ee77ca3f7b779f089654d1a80a0f49919275bdd1
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller.h
1 #ifndef __DALI_TOOLKIT_TEXT_CONTROLLER_H__
2 #define __DALI_TOOLKIT_TEXT_CONTROLLER_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
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>
29
30 // INTERNAL INCLUDES
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>
35
36 namespace Dali
37 {
38
39 namespace Toolkit
40 {
41
42 namespace Text
43 {
44
45 class Controller;
46 class LayoutEngine;
47
48 typedef IntrusivePtr<Controller> ControllerPtr;
49 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
50
51 /**
52  * @brief A Text Controller is used by UI Controls which display text.
53  *
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.
56  *
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.
59  */
60 class Controller : public RefObject, public Decorator::Observer
61 {
62 private:
63
64   /**
65    * @brief Text related operations to be done in the relayout process.
66    */
67   enum OperationsMask
68   {
69     NO_OPERATION       = 0x0000,
70     CONVERT_TO_UTF32   = 0x0001,
71     GET_SCRIPTS        = 0x0002,
72     VALIDATE_FONTS     = 0x0004,
73     GET_LINE_BREAKS    = 0x0008,
74     GET_WORD_BREAKS    = 0x0010,
75     BIDI_INFO          = 0x0020,
76     SHAPE_TEXT         = 0x0040,
77     GET_GLYPH_METRICS  = 0x0080,
78     LAYOUT             = 0x0100,
79     UPDATE_ACTUAL_SIZE = 0x0200,
80     UPDATE_POSITIONS   = 0x0400,
81     UPDATE_LINES       = 0x0800,
82     REORDER            = 0x1000,
83     ALIGNMENT          = 0x2000,
84     RENDER             = 0x4000,
85     ALL_OPERATIONS     = 0xFFFF
86   };
87
88 public:
89
90   /**
91    * @brief Create a new instance of a Controller.
92    *
93    * @param[in] controlInterface An interface used to request a text relayout.
94    * @return A pointer to a new Controller.
95    */
96   static ControllerPtr New( ControlInterface& controlInterface );
97
98   /**
99    * @brief Replaces any text previously set.
100    *
101    * @note This will be converted into UTF-32 when stored in the text model.
102    * @param[in] text A string of UTF-8 characters.
103    */
104   void SetText( const std::string& text );
105
106   /**
107    * @brief Retrieve any text previously set.
108    *
109    * @return A string of UTF-8 characters.
110    */
111   void GetText( std::string& text ) const;
112
113   /**
114    * @brief Replaces any placeholder text previously set.
115    *
116    * @param[in] text A string of UTF-8 characters.
117    */
118   void SetPlaceholderText( const std::string& text );
119
120   /**
121    * @brief Retrieve any placeholder text previously set.
122    *
123    * @return A string of UTF-8 characters.
124    */
125   void GetPlaceholderText( std::string& text ) const;
126
127   /**
128    * @brief Set the default font family.
129    *
130    * @param[in] defaultFontFamily The default font family.
131    */
132   void SetDefaultFontFamily( const std::string& defaultFontFamily );
133
134   /**
135    * @brief Retrieve the default font family.
136    *
137    * @return The default font family.
138    */
139   const std::string& GetDefaultFontFamily() const;
140
141   /**
142    * @brief Set the default font style.
143    *
144    * @param[in] defaultFontStyle The default font style.
145    */
146   void SetDefaultFontStyle( const std::string& defaultFontStyle );
147
148   /**
149    * @brief Retrieve the default font style.
150    *
151    * @return The default font style.
152    */
153   const std::string& GetDefaultFontStyle() const;
154
155   /**
156    * @brief Set the default point size.
157    *
158    * @param[in] defaultFontStyle The default point size.
159    */
160   void SetDefaultPointSize( float pointSize );
161
162   /**
163    * @brief Retrieve the default point size.
164    *
165    * @return The default point size.
166    */
167   float GetDefaultPointSize() const;
168
169   /**
170    * @brief Retrieve the default fonts.
171    *
172    * @param[out] fonts The default font family, style and point sizes.
173    * @param[in] numberOfCharacters The number of characters in the logical model.
174    */
175   void GetDefaultFonts( Dali::Vector<FontRun>& fonts, Length numberOfCharacters );
176
177   /**
178    * @brief Called to enable text input.
179    *
180    * @note Only selectable or editable controls should calls this.
181    * @param[in] decorator Used to create cursor, selection handle decorations etc.
182    */
183   void EnableTextInput( DecoratorPtr decorator );
184
185   /**
186    * @brief Called to enable/disable cursor blink.
187    *
188    * @note Only editable controls should calls this.
189    * @param[in] enabled Whether the cursor should blink or not.
190    */
191   void SetEnableCursorBlink( bool enable );
192
193   /**
194    * @brief Query whether cursor blink is enabled.
195    *
196    * @return Whether the cursor should blink or not.
197    */
198   bool GetEnableCursorBlink() const;
199
200   /**
201    * @brief Triggers a relayout which updates View (if necessary).
202    *
203    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
204    * @param[in] size A the size of a bounding box to layout text within.
205    * @return True if the text model or decorations were updated.
206    */
207   bool Relayout( const Vector2& size );
208
209   /**
210    * @brief Update the model with new text.
211    *
212    * @param[in] operations The layout operations which need to be done.
213    */
214   void ReplaceText( OperationsMask operations );
215
216   /**
217    * @brief Lays-out the text.
218    *
219    * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
220    *
221    * @param[in] size A the size of a bounding box to layout text within.
222    * @param[in] operations The layout operations which need to be done.
223    * @param[out] layoutSize The size of the laid-out text.
224    */
225   bool DoRelayout( const Vector2& size,
226                    OperationsMask operations,
227                    Size& layoutSize );
228
229   /**
230    * @copydoc Control::GetNaturalSize()
231    */
232   Vector3 GetNaturalSize();
233
234   /**
235    * @copydoc Control::GetHeightForWidth()
236    */
237   float GetHeightForWidth( float width );
238
239   /**
240    * @brief Return the layout engine.
241    *
242    * @return A reference to the layout engine.
243    */
244   LayoutEngine& GetLayoutEngine();
245
246   /**
247    * @brief Return a view of the text.
248    *
249    * @return A reference to the view.
250    */
251   View& GetView();
252
253   /**
254    * @brief Caller by editable UI controls when keyboard focus is gained.
255    */
256   void KeyboardFocusGainEvent();
257
258   /**
259    * @brief Caller by editable UI controls when focus is lost.
260    */
261   void KeyboardFocusLostEvent();
262
263   /**
264    * @brief Caller by editable UI controls when key events are received.
265    *
266    * @param[in] event The key event.
267    */
268   bool KeyEvent( const Dali::KeyEvent& event );
269
270   /**
271    * @brief Caller by editable UI controls when a tap gesture occurs.
272    * @param[in] tapCount The number of taps.
273    * @param[in] x The x position relative to the top-left of the parent control.
274    * @param[in] y The y position relative to the top-left of the parent control.
275    */
276   void TapEvent( unsigned int tapCount, float x, float y );
277
278   /**
279    * @copydoc Dali::Toolkit::Text::Decorator::Observer::GrabHandleEvent()
280    */
281   virtual void GrabHandleEvent( GrabHandleState state, float x, float y );
282
283 protected:
284
285   /**
286    * @brief A reference counted object may only be deleted by calling Unreference().
287    */
288   virtual ~Controller();
289
290 private:
291
292   /**
293    * @brief Request a relayout using the ControlInterface.
294    */
295   void RequestRelayout();
296
297   /**
298    * @brief Private constructor.
299    */
300   Controller( ControlInterface& controlInterface );
301
302   // Undefined
303   Controller( const Controller& handle );
304
305   // Undefined
306   Controller& operator=( const Controller& handle );
307
308 private:
309
310   struct Impl;
311   Impl* mImpl;
312
313   // Avoid allocating this when the user does not specify a font
314   struct FontDefaults;
315
316   // Avoid allocating this for non-editable controls
317   struct TextInput;
318 };
319
320 } // namespace Text
321
322 } // namespace Toolkit
323
324 } // namespace Dali
325
326 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__