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