fdce0edd128a975980a20c090843e48cc7711aa2
[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 #include <dali-toolkit/public-api/text/rendering-backend.h>
30
31 using Dali::Toolkit::Text::LayoutEngine;
32 using Dali::Toolkit::Text::Backend;
33
34 namespace Dali
35 {
36
37 namespace Toolkit
38 {
39
40 namespace Internal
41 {
42
43 namespace
44 {
45   const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
46 }
47
48 namespace
49 {
50
51 // Type registration
52 BaseHandle Create()
53 {
54   return Toolkit::TextLabel::New();
55 }
56
57 // Setup properties, signals and actions using the type-registry.
58 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
59
60 DALI_PROPERTY_REGISTRATION( TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
61 DALI_PROPERTY_REGISTRATION( TextLabel, "text",              STRING,  TEXT              )
62 DALI_PROPERTY_REGISTRATION( TextLabel, "font-family",       STRING,  FONT_FAMILY       )
63 DALI_PROPERTY_REGISTRATION( TextLabel, "font-style",        STRING,  FONT_STYLE        )
64 DALI_PROPERTY_REGISTRATION( TextLabel, "point-size",        FLOAT,   POINT_SIZE        )
65 DALI_PROPERTY_REGISTRATION( TextLabel, "multi-line",        BOOLEAN, MULTI_LINE        )
66
67 DALI_TYPE_REGISTRATION_END()
68
69 } // namespace
70
71 Toolkit::TextLabel TextLabel::New()
72 {
73   // Create the implementation, temporarily owned by this handle on stack
74   IntrusivePtr< TextLabel > impl = new TextLabel();
75
76   // Pass ownership to CustomActor handle
77   Toolkit::TextLabel handle( *impl );
78
79   // Second-phase init of the implementation
80   // This can only be done after the CustomActor connection has been made...
81   impl->Initialize();
82
83   return handle;
84 }
85
86 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
87 {
88   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
89
90   if( label )
91   {
92     TextLabel& impl( GetImpl( label ) );
93     switch( index )
94     {
95       case Toolkit::TextLabel::Property::RENDERING_BACKEND:
96       {
97         int backend = value.Get< int >();
98
99         if( impl.mRenderingBackend != backend )
100         {
101           impl.mRenderingBackend = backend;
102           impl.mRenderer.Reset();
103           impl.RequestTextRelayout();
104         }
105         break;
106       }
107       case Toolkit::TextLabel::Property::TEXT:
108       {
109         if( impl.mController )
110         {
111           impl.mController->SetText( value.Get< std::string >() );
112           impl.RequestTextRelayout();
113         }
114         break;
115       }
116       case Toolkit::TextLabel::Property::FONT_FAMILY:
117       {
118         if( impl.mController )
119         {
120           std::string fontFamily = value.Get< std::string >();
121
122           if( impl.mController->GetDefaultFontFamily() != fontFamily )
123           {
124             impl.mController->SetDefaultFontFamily( fontFamily );
125             impl.RequestTextRelayout();
126           }
127         }
128         break;
129       }
130       case Toolkit::TextLabel::Property::FONT_STYLE:
131       {
132         if( impl.mController )
133         {
134           std::string fontStyle = value.Get< std::string >();
135
136           if( impl.mController->GetDefaultFontStyle() != fontStyle )
137           {
138             impl.mController->SetDefaultFontStyle( fontStyle );
139             impl.RequestTextRelayout();
140           }
141         }
142         break;
143       }
144       case Toolkit::TextLabel::Property::POINT_SIZE:
145       {
146         if( impl.mController )
147         {
148           float pointSize = value.Get< float >();
149
150           if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
151           {
152             impl.mController->SetDefaultPointSize( pointSize );
153             impl.RequestTextRelayout();
154           }
155         }
156         break;
157       }
158       case Toolkit::TextLabel::Property::MULTI_LINE:
159       {
160         if( impl.mController )
161         {
162           LayoutEngine& engine = impl.mController->GetLayoutEngine();
163           LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
164
165           if( engine.GetLayout() != layout )
166           {
167             impl.mController->GetLayoutEngine().SetLayout( layout );
168             impl.RequestTextRelayout();
169           }
170         }
171         break;
172       }
173     }
174   }
175 }
176
177 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
178 {
179   Property::Value value;
180
181   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
182
183   if( label )
184   {
185     TextLabel& impl( GetImpl( label ) );
186     switch( index )
187     {
188       case Toolkit::TextLabel::Property::RENDERING_BACKEND:
189       {
190         value = impl.mRenderingBackend;
191         break;
192       }
193       case Toolkit::TextLabel::Property::TEXT:
194       {
195         if( impl.mController )
196         {
197           std::string text;
198           impl.mController->GetText( text );
199           value = text;
200         }
201         break;
202       }
203       case Toolkit::TextLabel::Property::MULTI_LINE:
204       {
205         if( impl.mController )
206         {
207           value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
208         }
209         break;
210       }
211     }
212   }
213
214   return value;
215 }
216
217 void TextLabel::OnInitialize()
218 {
219   mController = Text::Controller::New( *this );
220 }
221
222 Vector3 TextLabel::GetNaturalSize()
223 {
224   return mController->GetNaturalSize();
225 }
226
227 float TextLabel::GetHeightForWidth( float width )
228 {
229   return mController->GetHeightForWidth( width );
230 }
231
232 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
233 {
234   if( mController->Relayout( size ) ||
235       !mRenderer )
236   {
237     if( !mRenderer )
238     {
239       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
240     }
241
242     RenderableActor renderableActor;
243     if( mRenderer )
244     {
245       renderableActor = mRenderer->Render( mController->GetView() );
246     }
247
248     if( renderableActor != mRenderableActor )
249     {
250       UnparentAndReset( mRenderableActor );
251
252       if( renderableActor )
253       {
254         Self().Add( renderableActor );
255       }
256
257       mRenderableActor = renderableActor;
258     }
259   }
260 }
261
262 void TextLabel::RequestTextRelayout()
263 {
264   RelayoutRequest();
265 }
266
267 TextLabel::TextLabel()
268 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
269   mRenderingBackend( DEFAULT_RENDERING_BACKEND )
270 {
271 }
272
273 TextLabel::~TextLabel()
274 {
275 }
276
277 } // namespace Internal
278
279 } // namespace Toolkit
280
281 } // namespace Dali