Clipping support for TextField
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/integration-api/debug.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
28 #include <dali-toolkit/internal/text/rendering/text-backend.h>
29
30 using Dali::Toolkit::Text::LayoutEngine;
31 using Dali::Toolkit::Text::Backend;
32
33 namespace
34 {
35
36 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
37
38 } // namespace
39
40 namespace Dali
41 {
42
43 namespace Toolkit
44 {
45
46 namespace Internal
47 {
48
49 namespace
50 {
51
52 // Type registration
53 BaseHandle Create()
54 {
55   return Toolkit::TextLabel::New();
56 }
57
58 // Setup properties, signals and actions using the type-registry.
59 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
60
61 DALI_PROPERTY_REGISTRATION( TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
62 DALI_PROPERTY_REGISTRATION( TextLabel, "text",              STRING,  TEXT              )
63 DALI_PROPERTY_REGISTRATION( TextLabel, "font-family",       STRING,  FONT_FAMILY       )
64 DALI_PROPERTY_REGISTRATION( TextLabel, "font-style",        STRING,  FONT_STYLE        )
65 DALI_PROPERTY_REGISTRATION( TextLabel, "point-size",        FLOAT,   POINT_SIZE        )
66 DALI_PROPERTY_REGISTRATION( TextLabel, "multi-line",        BOOLEAN, MULTI_LINE        )
67
68 DALI_TYPE_REGISTRATION_END()
69
70 } // namespace
71
72 Toolkit::TextLabel TextLabel::New()
73 {
74   // Create the implementation, temporarily owned by this handle on stack
75   IntrusivePtr< TextLabel > impl = new TextLabel();
76
77   // Pass ownership to CustomActor handle
78   Toolkit::TextLabel handle( *impl );
79
80   // Second-phase init of the implementation
81   // This can only be done after the CustomActor connection has been made...
82   impl->Initialize();
83
84   return handle;
85 }
86
87 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
88 {
89   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
90
91   if( label )
92   {
93     TextLabel& impl( GetImpl( label ) );
94     switch( index )
95     {
96       case Toolkit::TextLabel::Property::RENDERING_BACKEND:
97       {
98         int backend = value.Get< int >();
99
100         if( impl.mRenderingBackend != backend )
101         {
102           impl.mRenderingBackend = backend;
103           impl.mRenderer.Reset();
104           impl.RequestTextRelayout();
105         }
106         break;
107       }
108       case Toolkit::TextLabel::Property::TEXT:
109       {
110         if( impl.mController )
111         {
112           impl.mController->SetText( value.Get< std::string >() );
113           impl.RequestTextRelayout();
114         }
115         break;
116       }
117       case Toolkit::TextLabel::Property::FONT_FAMILY:
118       {
119         if( impl.mController )
120         {
121           std::string fontFamily = value.Get< std::string >();
122
123           if( impl.mController->GetDefaultFontFamily() != fontFamily )
124           {
125             impl.mController->SetDefaultFontFamily( fontFamily );
126             impl.RequestTextRelayout();
127           }
128         }
129         break;
130       }
131       case Toolkit::TextLabel::Property::FONT_STYLE:
132       {
133         if( impl.mController )
134         {
135           std::string fontStyle = value.Get< std::string >();
136
137           if( impl.mController->GetDefaultFontStyle() != fontStyle )
138           {
139             impl.mController->SetDefaultFontStyle( fontStyle );
140             impl.RequestTextRelayout();
141           }
142         }
143         break;
144       }
145       case Toolkit::TextLabel::Property::POINT_SIZE:
146       {
147         if( impl.mController )
148         {
149           float pointSize = value.Get< float >();
150
151           if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
152           {
153             impl.mController->SetDefaultPointSize( pointSize );
154             impl.RequestTextRelayout();
155           }
156         }
157         break;
158       }
159       case Toolkit::TextLabel::Property::MULTI_LINE:
160       {
161         if( impl.mController )
162         {
163           LayoutEngine& engine = impl.mController->GetLayoutEngine();
164           LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
165
166           if( engine.GetLayout() != layout )
167           {
168             impl.mController->GetLayoutEngine().SetLayout( layout );
169             impl.RequestTextRelayout();
170           }
171         }
172         break;
173       }
174     }
175   }
176 }
177
178 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
179 {
180   Property::Value value;
181
182   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
183
184   if( label )
185   {
186     TextLabel& impl( GetImpl( label ) );
187     switch( index )
188     {
189       case Toolkit::TextLabel::Property::RENDERING_BACKEND:
190       {
191         value = impl.mRenderingBackend;
192         break;
193       }
194       case Toolkit::TextLabel::Property::TEXT:
195       {
196         if( impl.mController )
197         {
198           std::string text;
199           impl.mController->GetText( text );
200           value = text;
201         }
202         break;
203       }
204       case Toolkit::TextLabel::Property::MULTI_LINE:
205       {
206         if( impl.mController )
207         {
208           value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
209         }
210         break;
211       }
212     }
213   }
214
215   return value;
216 }
217
218 void TextLabel::OnInitialize()
219 {
220   mController = Text::Controller::New( *this );
221 }
222
223 Vector3 TextLabel::GetNaturalSize()
224 {
225   return mController->GetNaturalSize();
226 }
227
228 float TextLabel::GetHeightForWidth( float width )
229 {
230   return mController->GetHeightForWidth( width );
231 }
232
233 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
234 {
235   if( mController->Relayout( size ) ||
236       !mRenderer )
237   {
238     if( !mRenderer )
239     {
240       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
241     }
242
243     RenderableActor renderableActor;
244     if( mRenderer )
245     {
246       renderableActor = mRenderer->Render( mController->GetView() );
247     }
248
249     if( renderableActor != mRenderableActor )
250     {
251       UnparentAndReset( mRenderableActor );
252
253       if( renderableActor )
254       {
255         Self().Add( renderableActor );
256       }
257
258       mRenderableActor = renderableActor;
259     }
260   }
261 }
262
263 void TextLabel::RequestTextRelayout()
264 {
265   RelayoutRequest();
266 }
267
268 TextLabel::TextLabel()
269 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
270   mRenderingBackend( DEFAULT_RENDERING_BACKEND )
271 {
272 }
273
274 TextLabel::~TextLabel()
275 {
276 }
277
278 } // namespace Internal
279
280 } // namespace Toolkit
281
282 } // namespace Dali