Layout implementation
[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 );
107
108   /**
109    * @brief Called to enable text input.
110    *
111    * @note Only selectable or editable controls should calls this.
112    * @param[in] decorator Used to create cursor, selection handle decorations etc.
113    */
114   void EnableTextInput( DecoratorPtr decorator );
115
116   /**
117    * @brief Triggers a relayout which updates View (if necessary).
118    *
119    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
120    * @param[in] size A the size of a bounding box to layout text within.
121    * @return True if the text model or decorations were updated.
122    */
123   bool Relayout( const Vector2& size );
124
125   /**
126    * @brief Lays-out the text.
127    *
128    * GetNaturalSize(), GetHeightForWidth() and Relayout() calls this method.
129    *
130    * @param[in] size A the size of a bounding box to layout text within.
131    * @param[in] operations The layout operations which need to be done.
132    */
133   bool DoRelayout( const Vector2& size, OperationsMask operations );
134
135   /**
136    * @copydoc Control::GetNaturalSize()
137    */
138   Vector3 GetNaturalSize();
139
140   /**
141    * @copydoc Control::GetHeightForWidth()
142    */
143   float GetHeightForWidth( float width );
144
145   /**
146    * @brief Return the layout engine.
147    *
148    * @return A reference to the layout engine.
149    */
150   LayoutEngine& GetLayoutEngine();
151
152   /**
153    * @brief Return a view of the text.
154    *
155    * @return A reference to the view.
156    */
157   View& GetView();
158
159   /**
160    * @brief Caller by editable UI controls when keyboard focus is gained.
161    */
162   void KeyboardFocusGainEvent();
163
164   /**
165    * @brief Caller by editable UI controls when focus is lost.
166    */
167   void KeyboardFocusLostEvent();
168
169   /**
170    * @brief Caller by editable UI controls when a tap gesture occurs.
171    * @param[in] tapCount The number of taps.
172    * @param[in] x The x position relative to the top-left of the parent control.
173    * @param[in] y The y position relative to the top-left of the parent control.
174    */
175   void TapEvent( unsigned int tapCount, float x, float y );
176
177   /**
178    * @copydoc Dali::Toolkit::Text::Decorator::Observer::GrabHandleEvent()
179    */
180   virtual void GrabHandleEvent( GrabHandleState state, float x, float y );
181
182 protected:
183
184   /**
185    * @brief A reference counted object may only be deleted by calling Unreference().
186    */
187   virtual ~Controller();
188
189 private:
190
191   /**
192    * @brief Request a relayout using the ControlInterface.
193    */
194   void RequestRelayout();
195
196   /**
197    * @brief Private constructor.
198    */
199   Controller( ControlInterface& controlInterface );
200
201   // Undefined
202   Controller( const Controller& handle );
203
204   // Undefined
205   Controller& operator=( const Controller& handle );
206
207 private:
208
209   struct Impl;
210   Impl* mImpl;
211
212   // Avoid allocating this for non-editable controls
213   struct TextInput;
214 };
215
216 } // namespace Text
217
218 } // namespace Toolkit
219
220 } // namespace Dali
221
222 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__