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, "text-color", VECTOR4, TEXT_COLOR )
87 DALI_PROPERTY_REGISTRATION( TextLabel, "shadow-offset", VECTOR2, SHADOW_OFFSET )
88 DALI_PROPERTY_REGISTRATION( TextLabel, "shadow-color", VECTOR4, SHADOW_COLOR )
89 DALI_PROPERTY_REGISTRATION( TextLabel, "underline-enabled", BOOLEAN, UNDERLINE_ENABLED )
90 DALI_PROPERTY_REGISTRATION( TextLabel, "underline-color", VECTOR4, UNDERLINE_COLOR )
91 DALI_TYPE_REGISTRATION_END()
95 Toolkit::TextLabel TextLabel::New()
97 // Create the implementation, temporarily owned by this handle on stack
98 IntrusivePtr< TextLabel > impl = new TextLabel();
100 // Pass ownership to CustomActor handle
101 Toolkit::TextLabel handle( *impl );
103 // Second-phase init of the implementation
104 // This can only be done after the CustomActor connection has been made...
110 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
112 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
116 TextLabel& impl( GetImpl( label ) );
119 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
121 int backend = value.Get< int >();
123 if( impl.mRenderingBackend != backend )
125 impl.mRenderingBackend = backend;
126 impl.mRenderer.Reset();
127 impl.RequestTextRelayout();
131 case Toolkit::TextLabel::Property::TEXT:
133 if( impl.mController )
135 impl.mController->SetText( value.Get< std::string >() );
136 impl.RequestTextRelayout();
140 case Toolkit::TextLabel::Property::FONT_FAMILY:
142 if( impl.mController )
144 std::string fontFamily = value.Get< std::string >();
146 if( impl.mController->GetDefaultFontFamily() != fontFamily )
148 impl.mController->SetDefaultFontFamily( fontFamily );
149 impl.RequestTextRelayout();
154 case Toolkit::TextLabel::Property::FONT_STYLE:
156 if( impl.mController )
158 std::string fontStyle = value.Get< std::string >();
160 if( impl.mController->GetDefaultFontStyle() != fontStyle )
162 impl.mController->SetDefaultFontStyle( fontStyle );
163 impl.RequestTextRelayout();
168 case Toolkit::TextLabel::Property::POINT_SIZE:
170 if( impl.mController )
172 float pointSize = value.Get< float >();
174 if( fabsf(impl.mController->GetDefaultPointSize() - pointSize) > Math::MACHINE_EPSILON_1 )
176 impl.mController->SetDefaultPointSize( pointSize );
177 impl.RequestTextRelayout();
182 case Toolkit::TextLabel::Property::MULTI_LINE:
184 if( impl.mController )
186 LayoutEngine& engine = impl.mController->GetLayoutEngine();
187 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
189 if( engine.GetLayout() != layout )
191 engine.SetLayout( layout );
192 impl.RequestTextRelayout();
197 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
199 LayoutEngine& engine = impl.mController->GetLayoutEngine();
200 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
201 HORIZONTAL_ALIGNMENT_STRING_TABLE,
202 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
204 if( engine.GetHorizontalAlignment() != alignment )
206 engine.SetHorizontalAlignment( alignment );
207 impl.RequestTextRelayout();
211 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
213 LayoutEngine& engine = impl.mController->GetLayoutEngine();
214 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
215 VERTICAL_ALIGNMENT_STRING_TABLE,
216 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
218 if( engine.GetVerticalAlignment() != alignment )
220 engine.SetVerticalAlignment( alignment );
221 impl.RequestTextRelayout();
226 case Toolkit::TextLabel::Property::TEXT_COLOR:
228 if ( impl.mController )
230 Vector4 textColor = value.Get< Vector4 >();
231 if ( impl.mController->GetTextColor() != textColor )
233 impl.mController->SetTextColor( textColor );
234 impl.RequestTextRelayout();
240 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
242 if( impl.mController )
244 Vector2 shadowOffset = value.Get< Vector2 >();
245 if ( impl.mController->GetShadowOffset() != shadowOffset )
247 impl.mController->SetShadowOffset( shadowOffset );
248 impl.RequestTextRelayout();
253 case Toolkit::TextLabel::Property::SHADOW_COLOR:
255 if( impl.mController )
257 Vector4 shadowColor = value.Get< Vector4 >();
258 if ( impl.mController->GetShadowColor() != shadowColor )
260 impl.mController->SetShadowColor( shadowColor );
261 impl.RequestTextRelayout();
266 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
268 if( impl.mController )
270 Vector4 color = value.Get< Vector4 >();
271 if ( impl.mController->GetUnderlineColor() != color )
273 impl.mController->SetUnderlineColor( color );
274 impl.RequestTextRelayout();
279 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
281 if( impl.mController )
283 bool enabled = value.Get< bool >();
284 if ( impl.mController->IsUnderlineEnabled() != enabled )
286 impl.mController->SetUnderlineEnabled( enabled );
287 impl.RequestTextRelayout();
296 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
298 Property::Value value;
300 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
304 TextLabel& impl( GetImpl( label ) );
307 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
309 value = impl.mRenderingBackend;
312 case Toolkit::TextLabel::Property::TEXT:
314 if( impl.mController )
317 impl.mController->GetText( text );
322 case Toolkit::TextLabel::Property::MULTI_LINE:
324 if( impl.mController )
326 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
330 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
332 if( impl.mController )
334 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
335 HORIZONTAL_ALIGNMENT_STRING_TABLE,
336 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
340 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
342 if( impl.mController )
344 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
345 VERTICAL_ALIGNMENT_STRING_TABLE,
346 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
350 case Toolkit::TextLabel::Property::TEXT_COLOR:
352 if ( impl.mController )
354 value = impl.mController->GetTextColor();
358 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
360 if ( impl.mController )
362 value = impl.mController->GetShadowOffset();
366 case Toolkit::TextLabel::Property::SHADOW_COLOR:
368 if ( impl.mController )
370 value = impl.mController->GetShadowColor();
374 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
376 if ( impl.mController )
378 value = impl.mController->GetUnderlineColor();
382 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
384 if ( impl.mController )
386 value = impl.mController->IsUnderlineEnabled();
396 void TextLabel::OnInitialize()
400 mController = Text::Controller::New( *this );
402 // Use height-for-width negotiation by default
403 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
404 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
407 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
409 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
412 Vector3 TextLabel::GetNaturalSize()
414 return mController->GetNaturalSize();
417 float TextLabel::GetHeightForWidth( float width )
419 return mController->GetHeightForWidth( width );
422 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
424 if( mController->Relayout( size ) ||
429 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
432 RenderableActor renderableActor;
435 renderableActor = mRenderer->Render( mController->GetView() );
438 if( renderableActor != mRenderableActor )
440 UnparentAndReset( mRenderableActor );
442 if( renderableActor )
444 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
445 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
447 Self().Add( renderableActor );
450 mRenderableActor = renderableActor;
455 void TextLabel::RequestTextRelayout()
460 TextLabel::TextLabel()
461 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
462 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
466 TextLabel::~TextLabel()
470 } // namespace Internal
472 } // namespace Toolkit