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