8857203a4f27205483dbec647d81e79e24c48d9f
[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      = 0x0,
67     CONVERT_TO_UTF32  = 0x1,
68     GET_SCRIPTS       = 0x2,
69     VALIDATE_FONTS    = 0x4,
70     GET_LINE_BREAKS   = 0x8,
71     GET_WORD_BREAKS   = 0x10,
72     SHAPE_TEXT        = 0x20,
73     GET_GLYPH_METRICS = 0x40,
74     LAYOUT            = 0x80,
75     REORDER           = 0x100,
76     ALIGNMENT         = 0x200,
77     RENDER            = 0x400,
78     ALL_OPERATIONS    = 0xFFF
79   };
80
81 public:
82
83   /**
84    * @brief Create a new instance of a Controller.
85    *
86    * @param[in] controlInterface An interface used to request a text relayout.
87    * @return A pointer to a new Controller.
88    */
89   static ControllerPtr New( ControlInterface& controlInterface );
90
91   /**
92    * @brief Replaces any text previously set.
93    *
94    * @note This will be converted into UTF-32 when stored in the text model.
95    * @param[in] text A string of UTF-8 characters.
96    */
97   void SetText( const std::string& text );
98
99   /**
100    * @brief Retrieve any text previously set.
101    *
102    * @return A string of UTF-8 characters.
103    */
104   void GetText( std::string& text );
105
106   /**
107    * @brief Called to enable text input.
108    *
109    * @note Only selectable or editable controls should calls this.
110    * @param[in] decorator Used to create cursor, selection handle decorations etc.
111    */
112   void EnableTextInput( DecoratorPtr decorator );
113
114   /**
115    * @brief Triggers a relayout which updates View (if necessary).
116    *
117    * @note UI Controls are expected to minimize calls to this method e.g. call once after size negotiation.
118    * @param[in] size A the size of a bounding box to layout text within.
119    * @return True if the text model or decorations were updated.
120    */
121   bool Relayout( const Vector2& size );
122
123   /**
124    *
125    */
126   bool DoRelayout( const Vector2& size, OperationsMask operations );
127
128   /**
129    * @copydoc Control::GetNaturalSize()
130    */
131   Vector3 GetNaturalSize();
132
133   /**
134    * @copydoc Control::GetHeightForWidth()
135    */
136   float GetHeightForWidth( float width );
137
138   /**
139    * @brief Return the layout engine.
140    *
141    * @return A reference to the layout engine.
142    */
143   LayoutEngine& GetLayoutEngine();
144
145   /**
146    * @brief Return a view of the text.
147    *
148    * @return A reference to the view.
149    */
150   View& GetView();
151
152   /**
153    * @brief Caller by editable UI controls when keyboard focus is gained.
154    */
155   void KeyboardFocusGainEvent();
156
157   /**
158    * @brief Caller by editable UI controls when focus is lost.
159    */
160   void KeyboardFocusLostEvent();
161
162   /**
163    * @brief Caller by editable UI controls when a tap gesture occurs.
164    * @param[in] tapCount The number of taps.
165    * @param[in] x The x position relative to the top-left of the parent control.
166    * @param[in] y The y position relative to the top-left of the parent control.
167    */
168   void TapEvent( unsigned int tapCount, float x, float y );
169
170   /**
171    * @copydoc Dali::Toolkit::Text::Decorator::Observer::GrabHandleEvent()
172    */
173   virtual void GrabHandleEvent( GrabHandleState state, float x, float y );
174
175 protected:
176
177   /**
178    * @brief A reference counted object may only be deleted by calling Unreference().
179    */
180   virtual ~Controller();
181
182 private:
183
184   /**
185    * @brief Request a relayout using the ControlInterface.
186    */
187   void RequestRelayout();
188
189   /**
190    * @brief Private constructor.
191    */
192   Controller( ControlInterface& controlInterface );
193
194   // Undefined
195   Controller( const Controller& handle );
196
197   // Undefined
198   Controller& operator=( const Controller& handle );
199
200 private:
201
202   struct Impl;
203   Impl* mImpl;
204
205   // Avoid allocating this for non-editable controls
206   struct TextInput;
207 };
208
209 } // namespace Text
210
211 } // namespace Toolkit
212
213 } // namespace Dali
214
215 #endif // __DALI_TOOLKIT_TEXT_CONTROLLER_H__