2 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/public-api/scripting/scripting.h>
25 #include <dali/integration-api/debug.h>
28 #include <dali-toolkit/public-api/text/rendering-backend.h>
29 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
30 #include <dali-toolkit/internal/text/rendering/text-backend.h>
31 #include <dali-toolkit/internal/styling/style-manager-impl.h>
33 using Dali::Toolkit::Text::LayoutEngine;
34 using Dali::Toolkit::Text::Backend;
47 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
53 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::HorizontalAlignment > HORIZONTAL_ALIGNMENT_STRING_TABLE[] =
55 { "BEGIN", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_BEGIN },
56 { "CENTER", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_CENTER },
57 { "END", Toolkit::Text::LayoutEngine::HORIZONTAL_ALIGN_END },
59 const unsigned int HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE ) / sizeof( HORIZONTAL_ALIGNMENT_STRING_TABLE[0] );
61 const Scripting::StringEnum< Toolkit::Text::LayoutEngine::VerticalAlignment > VERTICAL_ALIGNMENT_STRING_TABLE[] =
63 { "TOP", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_TOP },
64 { "CENTER", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_CENTER },
65 { "BOTTOM", Toolkit::Text::LayoutEngine::VERTICAL_ALIGN_BOTTOM },
67 const unsigned int VERTICAL_ALIGNMENT_STRING_TABLE_COUNT = sizeof( VERTICAL_ALIGNMENT_STRING_TABLE ) / sizeof( VERTICAL_ALIGNMENT_STRING_TABLE[0] );
72 return Toolkit::TextLabel::New();
75 // Setup properties, signals and actions using the type-registry.
76 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
78 DALI_PROPERTY_REGISTRATION( TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
79 DALI_PROPERTY_REGISTRATION( TextLabel, "text", STRING, TEXT )
80 DALI_PROPERTY_REGISTRATION( TextLabel, "font-family", STRING, FONT_FAMILY )
81 DALI_PROPERTY_REGISTRATION( TextLabel, "font-style", STRING, FONT_STYLE )
82 DALI_PROPERTY_REGISTRATION( TextLabel, "point-size", FLOAT, POINT_SIZE )
83 DALI_PROPERTY_REGISTRATION( TextLabel, "multi-line", BOOLEAN, MULTI_LINE )
84 DALI_PROPERTY_REGISTRATION( TextLabel, "horizontal-alignment", STRING, HORIZONTAL_ALIGNMENT )
85 DALI_PROPERTY_REGISTRATION( TextLabel, "vertical-alignment", STRING, VERTICAL_ALIGNMENT )
87 DALI_TYPE_REGISTRATION_END()
91 Toolkit::TextLabel TextLabel::New()
93 // Create the implementation, temporarily owned by this handle on stack
94 IntrusivePtr< TextLabel > impl = new TextLabel();
96 // Pass ownership to CustomActor handle
97 Toolkit::TextLabel handle( *impl );
99 // Second-phase init of the implementation
100 // This can only be done after the CustomActor connection has been made...
106 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
108 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
112 TextLabel& impl( GetImpl( label ) );
115 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
117 int backend = value.Get< int >();
119 if( impl.mRenderingBackend != backend )
121 impl.mRenderingBackend = backend;
122 impl.mRenderer.Reset();
123 impl.RequestTextRelayout();
127 case Toolkit::TextLabel::Property::TEXT:
129 if( impl.mController )
131 impl.mController->SetText( value.Get< std::string >() );
132 impl.RequestTextRelayout();
136 case Toolkit::TextLabel::Property::FONT_FAMILY:
138 if( impl.mController )
140 std::string fontFamily = value.Get< std::string >();
142 if( impl.mController->GetDefaultFontFamily() != fontFamily )
144 impl.mController->SetDefaultFontFamily( fontFamily );
145 impl.RequestTextRelayout();
150 case Toolkit::TextLabel::Property::FONT_STYLE:
152 if( impl.mController )
154 std::string fontStyle = value.Get< std::string >();
156 if( impl.mController->GetDefaultFontStyle() != fontStyle )
158 impl.mController->SetDefaultFontStyle( fontStyle );
159 impl.RequestTextRelayout();
164 case Toolkit::TextLabel::Property::POINT_SIZE:
166 if( impl.mController )
168 float pointSize = value.Get< float >();
170 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
172 impl.mController->SetDefaultPointSize( pointSize );
173 impl.RequestTextRelayout();
178 case Toolkit::TextLabel::Property::MULTI_LINE:
180 if( impl.mController )
182 LayoutEngine& engine = impl.mController->GetLayoutEngine();
183 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
185 if( engine.GetLayout() != layout )
187 engine.SetLayout( layout );
188 impl.RequestTextRelayout();
193 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
195 LayoutEngine& engine = impl.mController->GetLayoutEngine();
196 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
197 HORIZONTAL_ALIGNMENT_STRING_TABLE,
198 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
200 if( engine.GetHorizontalAlignment() != alignment )
202 engine.SetHorizontalAlignment( alignment );
203 impl.RequestTextRelayout();
207 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
209 LayoutEngine& engine = impl.mController->GetLayoutEngine();
210 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
211 VERTICAL_ALIGNMENT_STRING_TABLE,
212 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
214 if( engine.GetVerticalAlignment() != alignment )
216 engine.SetVerticalAlignment( alignment );
217 impl.RequestTextRelayout();
225 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
227 Property::Value value;
229 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
233 TextLabel& impl( GetImpl( label ) );
236 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
238 value = impl.mRenderingBackend;
241 case Toolkit::TextLabel::Property::TEXT:
243 if( impl.mController )
246 impl.mController->GetText( text );
251 case Toolkit::TextLabel::Property::MULTI_LINE:
253 if( impl.mController )
255 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
259 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
261 if( impl.mController )
263 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
264 HORIZONTAL_ALIGNMENT_STRING_TABLE,
265 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
269 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
271 if( impl.mController )
273 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
274 VERTICAL_ALIGNMENT_STRING_TABLE,
275 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
285 void TextLabel::OnInitialize()
287 mController = Text::Controller::New( *this );
290 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
292 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
295 Vector3 TextLabel::GetNaturalSize()
297 return mController->GetNaturalSize();
300 float TextLabel::GetHeightForWidth( float width )
302 return mController->GetHeightForWidth( width );
305 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
307 if( mController->Relayout( size ) ||
312 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
315 RenderableActor renderableActor;
318 renderableActor = mRenderer->Render( mController->GetView() );
321 if( renderableActor != mRenderableActor )
323 UnparentAndReset( mRenderableActor );
325 if( renderableActor )
327 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
328 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
330 Self().Add( renderableActor );
333 mRenderableActor = renderableActor;
338 void TextLabel::RequestTextRelayout()
343 TextLabel::TextLabel()
344 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
345 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
349 TextLabel::~TextLabel()
353 } // namespace Internal
355 } // namespace Toolkit