bb0607d221c4de30ace44eb79b8ef9e877de4f8b
[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         unsigned int backend = value.Get< int >();
99
100         if( impl.mRenderingBackend != backend )
101         {
102           impl.mRenderingBackend = static_cast< unsigned int >( 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
203         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
204         break;
205       }
206       case Toolkit::TextLabel::Property::MULTI_LINE:
207       {
208         if( impl.mController )
209         {
210           value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
211         }
212         break;
213       }
214     }
215   }
216
217   return value;
218 }
219
220 void TextLabel::OnInitialize()
221 {
222   mController = Text::Controller::New( *this );
223 }
224
225 Vector3 TextLabel::GetNaturalSize()
226 {
227   return mController->GetNaturalSize();
228 }
229
230 float TextLabel::GetHeightForWidth( float width )
231 {
232   return mController->GetHeightForWidth( width );
233 }
234
235 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
236 {
237   if( mController->Relayout( size ) ||
238       !mRenderer )
239   {
240     if( !mRenderer )
241     {
242       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
243     }
244
245     RenderableActor renderableActor;
246     if( mRenderer )
247     {
248       renderableActor = mRenderer->Render( mController->GetView() );
249     }
250
251     if( renderableActor != mRenderableActor )
252     {
253       UnparentAndReset( mRenderableActor );
254
255       if( renderableActor )
256       {
257         Self().Add( renderableActor );
258       }
259
260       mRenderableActor = renderableActor;
261     }
262   }
263 }
264
265 void TextLabel::RequestTextRelayout()
266 {
267   RelayoutRequest();
268 }
269
270 TextLabel::TextLabel()
271 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
272   mRenderingBackend( DEFAULT_RENDERING_BACKEND )
273 {
274 }
275
276 TextLabel::~TextLabel()
277 {
278 }
279
280 } // namespace Internal
281
282 } // namespace Toolkit
283
284 } // namespace Dali