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::Alignment > ALIGNMENT_STRING_TABLE[] =
55 { "BEGIN", Toolkit::Text::LayoutEngine::ALIGN_BEGIN },
56 { "CENTER", Toolkit::Text::LayoutEngine::ALIGN_CENTER },
57 { "END", Toolkit::Text::LayoutEngine::ALIGN_END },
59 const unsigned int ALIGNMENT_STRING_TABLE_COUNT = sizeof( ALIGNMENT_STRING_TABLE ) / sizeof( ALIGNMENT_STRING_TABLE[0] );
64 return Toolkit::TextLabel::New();
67 // Setup properties, signals and actions using the type-registry.
68 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
70 DALI_PROPERTY_REGISTRATION( TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
71 DALI_PROPERTY_REGISTRATION( TextLabel, "text", STRING, TEXT )
72 DALI_PROPERTY_REGISTRATION( TextLabel, "font-family", STRING, FONT_FAMILY )
73 DALI_PROPERTY_REGISTRATION( TextLabel, "font-style", STRING, FONT_STYLE )
74 DALI_PROPERTY_REGISTRATION( TextLabel, "point-size", FLOAT, POINT_SIZE )
75 DALI_PROPERTY_REGISTRATION( TextLabel, "multi-line", BOOLEAN, MULTI_LINE )
76 DALI_PROPERTY_REGISTRATION( TextLabel, "alignment", STRING, ALIGNMENT )
78 DALI_TYPE_REGISTRATION_END()
82 Toolkit::TextLabel TextLabel::New()
84 // Create the implementation, temporarily owned by this handle on stack
85 IntrusivePtr< TextLabel > impl = new TextLabel();
87 // Pass ownership to CustomActor handle
88 Toolkit::TextLabel handle( *impl );
90 // Second-phase init of the implementation
91 // This can only be done after the CustomActor connection has been made...
97 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
99 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
103 TextLabel& impl( GetImpl( label ) );
106 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
108 int backend = value.Get< int >();
110 if( impl.mRenderingBackend != backend )
112 impl.mRenderingBackend = backend;
113 impl.mRenderer.Reset();
114 impl.RequestTextRelayout();
118 case Toolkit::TextLabel::Property::TEXT:
120 if( impl.mController )
122 impl.mController->SetText( value.Get< std::string >() );
123 impl.RequestTextRelayout();
127 case Toolkit::TextLabel::Property::FONT_FAMILY:
129 if( impl.mController )
131 std::string fontFamily = value.Get< std::string >();
133 if( impl.mController->GetDefaultFontFamily() != fontFamily )
135 impl.mController->SetDefaultFontFamily( fontFamily );
136 impl.RequestTextRelayout();
141 case Toolkit::TextLabel::Property::FONT_STYLE:
143 if( impl.mController )
145 std::string fontStyle = value.Get< std::string >();
147 if( impl.mController->GetDefaultFontStyle() != fontStyle )
149 impl.mController->SetDefaultFontStyle( fontStyle );
150 impl.RequestTextRelayout();
155 case Toolkit::TextLabel::Property::POINT_SIZE:
157 if( impl.mController )
159 float pointSize = value.Get< float >();
161 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
163 impl.mController->SetDefaultPointSize( pointSize );
164 impl.RequestTextRelayout();
169 case Toolkit::TextLabel::Property::MULTI_LINE:
171 if( impl.mController )
173 LayoutEngine& engine = impl.mController->GetLayoutEngine();
174 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
176 if( engine.GetLayout() != layout )
178 engine.SetLayout( layout );
179 impl.RequestTextRelayout();
184 case Toolkit::TextLabel::Property::ALIGNMENT:
186 LayoutEngine& engine = impl.mController->GetLayoutEngine();
187 const LayoutEngine::Alignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::Alignment >( value.Get< std::string >().c_str(),
188 ALIGNMENT_STRING_TABLE,
189 ALIGNMENT_STRING_TABLE_COUNT );
191 if( engine.GetAlignment() != alignment )
193 engine.SetAlignment( alignment );
194 impl.RequestTextRelayout();
202 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
204 Property::Value value;
206 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
210 TextLabel& impl( GetImpl( label ) );
213 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
215 value = impl.mRenderingBackend;
218 case Toolkit::TextLabel::Property::TEXT:
220 if( impl.mController )
223 impl.mController->GetText( text );
228 case Toolkit::TextLabel::Property::MULTI_LINE:
230 if( impl.mController )
232 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
236 case Toolkit::TextLabel::Property::ALIGNMENT:
238 if( impl.mController )
240 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::Alignment >( impl.mController->GetLayoutEngine().GetAlignment(),
241 ALIGNMENT_STRING_TABLE,
242 ALIGNMENT_STRING_TABLE_COUNT ) );
252 void TextLabel::OnInitialize()
254 mController = Text::Controller::New( *this );
257 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
259 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
262 Vector3 TextLabel::GetNaturalSize()
264 return mController->GetNaturalSize();
267 float TextLabel::GetHeightForWidth( float width )
269 return mController->GetHeightForWidth( width );
272 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
274 if( mController->Relayout( size ) ||
279 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
282 RenderableActor renderableActor;
285 renderableActor = mRenderer->Render( mController->GetView() );
288 if( renderableActor != mRenderableActor )
290 UnparentAndReset( mRenderableActor );
292 if( renderableActor )
294 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
295 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
297 Self().Add( renderableActor );
300 mRenderableActor = renderableActor;
305 void TextLabel::RequestTextRelayout()
310 TextLabel::TextLabel()
311 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
312 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
316 TextLabel::~TextLabel()
320 } // namespace Internal
322 } // namespace Toolkit