Property system fixes
[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/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
27 #include <dali-toolkit/internal/text/rendering/text-backend.h>
28
29 using Dali::Toolkit::Text::LayoutEngine;
30 using Dali::Toolkit::Text::Backend;
31
32 namespace
33 {
34
35 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
36
37 } // namespace
38
39 namespace Dali
40 {
41
42 namespace Toolkit
43 {
44
45 namespace Internal
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", INT,     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         unsigned int backend = value.Get< int >();
98
99         if( impl.mRenderingBackend != backend )
100         {
101           impl.mRenderingBackend = static_cast< unsigned int >( 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
202         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
203         break;
204       }
205       case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
206       {
207         if( impl.mController )
208         {
209           value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
210         }
211         break;
212       }
213     }
214   }
215
216   return value;
217 }
218
219 void TextLabel::OnInitialize()
220 {
221   mController = Text::Controller::New( *this );
222 }
223
224 Vector3 TextLabel::GetNaturalSize()
225 {
226   return mController->GetNaturalSize();
227 }
228
229 float TextLabel::GetHeightForWidth( float width )
230 {
231   return mController->GetHeightForWidth( width );
232 }
233
234 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
235 {
236   if( mController->Relayout( size ) ||
237       !mRenderer )
238   {
239     if( !mRenderer )
240     {
241       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
242     }
243
244     RenderableActor renderableActor;
245     if( mRenderer )
246     {
247       renderableActor = mRenderer->Render( mController->GetView() );
248     }
249
250     if( renderableActor != mRenderableActor )
251     {
252       UnparentAndReset( mRenderableActor );
253
254       if( renderableActor )
255       {
256         Self().Add( renderableActor );
257       }
258
259       mRenderableActor = renderableActor;
260     }
261   }
262 }
263
264 void TextLabel::RequestTextRelayout()
265 {
266   RelayoutRequest();
267 }
268
269 TextLabel::TextLabel()
270 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
271   mRenderingBackend( DEFAULT_RENDERING_BACKEND )
272 {
273 }
274
275 TextLabel::~TextLabel()
276 {
277 }
278
279 } // namespace Internal
280
281 } // namespace Toolkit
282
283 } // namespace Dali