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