Merge "Lines added to the model." into new_text
[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 // INTERNAL INCLUDES
22 #include <dali-toolkit/internal/text/decorator/text-decorator.h>
23 #include <dali-toolkit/internal/text/text-control-interface.h>
24 #include <dali-toolkit/internal/text/text-view.h>
25
26 // EXTERNAL INCLUDES
27 #include <dali/public-api/common/intrusive-ptr.h>
28 #include <dali/public-api/events/key-event.h>
29 #include <dali/public-api/math/vector3.h>
30 #include <dali/public-api/math/vector2.h>
31 #include <dali/public-api/object/ref-object.h>
32 #include <string>
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Text
41 {
42
43 class Controller;
44 class LayoutEngine;
45
46 typedef IntrusivePtr<Controller> ControllerPtr;
47 typedef Dali::Toolkit::Text::ControlInterface ControlInterface;
48
49 /**
50  * @brief A Text Controller is used by UI Controls which display text.
51  *
52  * It manipulates the Logical & Visual text models on behalf of the UI Controls.
53  * It provides a view of the text that can be used by rendering back-ends.
54  *
55  * For selectable/editable UI controls, the controller handles input events from the UI control
56  * and decorations (grab handles etc) via an observer interface.
57  */
58 class Controller : public RefObject, public Decorator::Observer
59 {
60 private:
61
62   /**
63    * @brief Text related operations to be done in the relayout process.
64    */
65   enum OperationsMask
66   {
67     NO_OPERATION       = 0x0000,
68     CONVERT_TO_UTF32   = 0x0001,
69     GET_SCRIPTS        = 0x0002,
70     VALIDATE_FONTS     = 0x0004,
71     GET_LINE_BREAKS    = 0x0008,
72     GET_WORD_BREAKS    = 0x0010,
73     SHAPE_TEXT         = 0x0020,
74     GET_GLYPH_METRICS  = 0x0040,
75     LAYOUT             = 0x0080,
76     UPDATE_ACTUAL_SIZE = 0x0100,
77     UPDATE_POSITIONS   = 0x0200,
78     UPDATE_LINES       = 0x0400,
79     REORDER            = 0x0800,
80     ALIGNMENT          = 0x1000,
81     RENDER             = 0x2000,
82     ALL_OPERATIONS     = 0xFFFF
83   };
84
85 public:
86
87   /**
88    * @brief Create a new instance of a Controller.
89    *
90    * @param[in] controlInterface An interface used to request a text relayout.
91    * @return A pointer to a new Controller.
92    */
93   static ControllerPtr New( ControlInterface& controlInterface );
94
95   /**
96    * @brief Replaces any text previously set.
97    *
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.
100    */
101   void SetText( const std::string& text );
102
103   /**
104    * @brief Retrieve any text previously set.
105    *
106    * @return A string of UTF-8 characters.
107    */
108   void GetText( std::string& text ) const;
109
110   /**
111    * @brief Replaces any placeholder text previously set.
112    *
113    * @param[in] text A string of UTF-8 characters.
114    */
115   void SetPlaceholderText( const std::string& text );
116
117   /**
118    * @brief Retrieve any placeholder text previously set.
119    *
120    * @return A string of UTF-8 characters.
121    */
122   void GetPlaceholderText( std::string& text ) const;
123
124   /**
125    * @brief Set the default font family.
126    *
127    * @param[in] defaultFontFamily The default font family.
128    */
129   void SetDefaultFontFamily( const std::string& defaultFontFamily );
130
131   /**
132    * @brief Retrieve the default font family.
133    *
134    * @return The default font family.
135    */
136    const std::string& GetDefaultFontFamily() const;
137
138   /**
139    * @brief Set the default font style.
140    *
141    * @param[in] defaultFontStyle The default font style.
142    */
143   void SetDefaultFontStyle( const std::string& defaultFontStyle );
144
145   /**
146    * @brief Retrieve the default font style.
147    *
148    * @return The default font style.
149    */
150    const std::string& GetDefaultFontStyle() const;
151
152   /**
153    * @brief Set the default point size.
154    *
155    * @param[in] defaultFontStyle The default point size.
156    */
157   void SetDefaultPointSize( float pointSize );
158
159   /**
160    * @brief Retrieve the default point size.
161    *
162    * @return The default point size.
163    */
164    float GetDefaultPointSize() const;
165
166   /**
167    * @brief Called to enable text input.
168    *
169    * @note Only selectable or editable controls should calls this.
170    * @param[in] decorator Used to create cursor, selection handle decorations etc.
171    */
172   void EnableTextInput( DecoratorPtr decorator );
173
174   /**
175    * @brief Triggers a relayout which updates View (if necessary).
176    *
177    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
178    * @param[in] size A the size of a bounding box to layout text within.
179    * @return True if the text model or decorations were updated.
180    */
181   bool Relayout( const Vector2& size );
182
183   /**
184    * @brief Lays-out the text.
185    *
186    * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
187    *
188    * @param[in] size A the size of a bounding box to layout text within.
189    * @param[in] operations The layout operations which need to be done.
190    * @param[out] layoutSize The size of the laid-out text.
191    */
192   bool DoRelayout( const Vector2& size,
193                    OperationsMask operations,
194                    Size& layoutSize );
195
196   /**
197    * @copydoc Control::GetNaturalSize()
198    */
199   Vector3 GetNaturalSize();
200
201   /**
202    * @copydoc Control::GetHeightForWidth()
203    */
204   float GetHeightForWidth( float width );
205
206   /**
207    * @brief Return the layout engine.
208    *
209    * @return A reference to the layout engine.
210    */
211   LayoutEngine& GetLayoutEngine();
212
213   /**
214    * @brief Return a view of the text.
215    *
216    * @return A reference to the view.
217    */
218   View& GetView();
219
220   /**
221    * @brief Caller by editable UI controls when keyboard focus is gained.
222    */
223   void KeyboardFocusGainEvent();
224
225   /**
226    * @brief Caller by editable UI controls when focus is lost.
227    */
228   void KeyboardFocusLostEvent();
229
230   /**
231    * @brief Caller by editable UI controls when key events are received.
232    *
233    * @param[in] event The key event.
234    */
235   bool KeyEvent( const Dali::KeyEvent& event );
236
237   /**
238    * @brief Caller by editable UI controls when a tap gesture occurs.
239    * @param[in] tapCount The number of taps.
240    * @param[in] x The x position relative to the top-left of the parent control.
241    * @param[in] y The y position relative to the top-left of the parent control.
242    */
243   void TapEvent( unsigned int tapCount, float x, float y );
244
245   /**
246    * @copydoc Dali::Toolkit::Text::Decorator::Observer::GrabHandleEvent()
247    */
248   virtual void GrabHandleEvent( GrabHandleState state, float x, float y );
249
250 protected:
251
252   /**
253    * @brief A reference counted object may only be deleted by calling Unreference().
254    */
255   virtual ~Controller();
256
257 private:
258
259   /**
260    * @brief Request a relayout using the ControlInterface.
261    */
262   void RequestRelayout();
263
264   /**
265    * @brief Private constructor.
266    */
267   Controller( ControlInterface& controlInterface );
268
269   // Undefined
270   Controller( const Controller& handle );
271
272   // Undefined
273   Controller& operator=( const Controller& handle );
274
275 private:
276
277   struct Impl;
278   Impl* mImpl;
279
280   // Avoid allocating this when the user does not specify a font
281   struct FontDefaults;
282
283   // Avoid allocating this for non-editable controls
284   struct TextInput;
285 };
286
287 } // namespace Text
288
289 } // namespace Toolkit
290
291 } // namespace Dali
292
293 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__