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 )
86 DALI_PROPERTY_REGISTRATION( TextLabel, "shadow-offset", VECTOR2, SHADOW_OFFSET )
87 DALI_PROPERTY_REGISTRATION( TextLabel, "shadow-color", VECTOR4, SHADOW_COLOR )
89 DALI_TYPE_REGISTRATION_END()
93 Toolkit::TextLabel TextLabel::New()
95 // Create the implementation, temporarily owned by this handle on stack
96 IntrusivePtr< TextLabel > impl = new TextLabel();
98 // Pass ownership to CustomActor handle
99 Toolkit::TextLabel handle( *impl );
101 // Second-phase init of the implementation
102 // This can only be done after the CustomActor connection has been made...
108 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
110 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
114 TextLabel& impl( GetImpl( label ) );
117 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
119 int backend = value.Get< int >();
121 if( impl.mRenderingBackend != backend )
123 impl.mRenderingBackend = backend;
124 impl.mRenderer.Reset();
125 impl.RequestTextRelayout();
129 case Toolkit::TextLabel::Property::TEXT:
131 if( impl.mController )
133 impl.mController->SetText( value.Get< std::string >() );
134 impl.RequestTextRelayout();
138 case Toolkit::TextLabel::Property::FONT_FAMILY:
140 if( impl.mController )
142 std::string fontFamily = value.Get< std::string >();
144 if( impl.mController->GetDefaultFontFamily() != fontFamily )
146 impl.mController->SetDefaultFontFamily( fontFamily );
147 impl.RequestTextRelayout();
152 case Toolkit::TextLabel::Property::FONT_STYLE:
154 if( impl.mController )
156 std::string fontStyle = value.Get< std::string >();
158 if( impl.mController->GetDefaultFontStyle() != fontStyle )
160 impl.mController->SetDefaultFontStyle( fontStyle );
161 impl.RequestTextRelayout();
166 case Toolkit::TextLabel::Property::POINT_SIZE:
168 if( impl.mController )
170 float pointSize = value.Get< float >();
172 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
174 impl.mController->SetDefaultPointSize( pointSize );
175 impl.RequestTextRelayout();
180 case Toolkit::TextLabel::Property::MULTI_LINE:
182 if( impl.mController )
184 LayoutEngine& engine = impl.mController->GetLayoutEngine();
185 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
187 if( engine.GetLayout() != layout )
189 engine.SetLayout( layout );
190 impl.RequestTextRelayout();
195 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
197 LayoutEngine& engine = impl.mController->GetLayoutEngine();
198 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
199 HORIZONTAL_ALIGNMENT_STRING_TABLE,
200 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
202 if( engine.GetHorizontalAlignment() != alignment )
204 engine.SetHorizontalAlignment( alignment );
205 impl.RequestTextRelayout();
209 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
211 LayoutEngine& engine = impl.mController->GetLayoutEngine();
212 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
213 VERTICAL_ALIGNMENT_STRING_TABLE,
214 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
216 if( engine.GetVerticalAlignment() != alignment )
218 engine.SetVerticalAlignment( alignment );
219 impl.RequestTextRelayout();
223 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
225 if( impl.mController )
227 Vector2 shadowOffset = value.Get< Vector2 >();
228 if ( impl.mController->GetShadowOffset() != shadowOffset )
230 impl.mController->SetShadowOffset( shadowOffset );
231 impl.RequestTextRelayout();
236 case Toolkit::TextLabel::Property::SHADOW_COLOR:
238 if( impl.mController )
240 Vector4 shadowColor = value.Get< Vector4 >();
241 if ( impl.mController->GetShadowColor() != shadowColor )
243 impl.mController->SetShadowColor( shadowColor );
244 impl.RequestTextRelayout();
253 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
255 Property::Value value;
257 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
261 TextLabel& impl( GetImpl( label ) );
264 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
266 value = impl.mRenderingBackend;
269 case Toolkit::TextLabel::Property::TEXT:
271 if( impl.mController )
274 impl.mController->GetText( text );
279 case Toolkit::TextLabel::Property::MULTI_LINE:
281 if( impl.mController )
283 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
287 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
289 if( impl.mController )
291 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
292 HORIZONTAL_ALIGNMENT_STRING_TABLE,
293 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
297 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
299 if( impl.mController )
301 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
302 VERTICAL_ALIGNMENT_STRING_TABLE,
303 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
307 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
309 if ( impl.mController )
311 value = impl.mController->GetShadowOffset();
315 case Toolkit::TextLabel::Property::SHADOW_COLOR:
317 if ( impl.mController )
319 value = impl.mController->GetShadowColor();
329 void TextLabel::OnInitialize()
333 mController = Text::Controller::New( *this );
335 // Use height-for-width negotiation by default
336 self.SetResizePolicy( FILL_TO_PARENT, WIDTH );
337 self.SetDimensionDependency( HEIGHT, WIDTH );
340 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
342 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
345 Vector3 TextLabel::GetNaturalSize()
347 return mController->GetNaturalSize();
350 float TextLabel::GetHeightForWidth( float width )
352 return mController->GetHeightForWidth( width );
355 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
357 if( mController->Relayout( size ) ||
362 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
365 RenderableActor renderableActor;
368 renderableActor = mRenderer->Render( mController->GetView() );
371 if( renderableActor != mRenderableActor )
373 UnparentAndReset( mRenderableActor );
375 if( renderableActor )
377 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
378 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
380 Self().Add( renderableActor );
383 mRenderableActor = renderableActor;
388 void TextLabel::RequestTextRelayout()
393 TextLabel::TextLabel()
394 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
395 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
399 TextLabel::~TextLabel()
403 } // namespace Internal
405 } // namespace Toolkit