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/devel-api/object/type-registry-helper.h>
24 #include <dali/devel-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( Toolkit, TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
79 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text", STRING, TEXT )
80 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "font-family", STRING, FONT_FAMILY )
81 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "font-style", STRING, FONT_STYLE )
82 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "point-size", FLOAT, POINT_SIZE )
83 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "multi-line", BOOLEAN, MULTI_LINE )
84 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "horizontal-alignment", STRING, HORIZONTAL_ALIGNMENT )
85 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "vertical-alignment", STRING, VERTICAL_ALIGNMENT )
86 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "text-color", VECTOR4, TEXT_COLOR )
87 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow-offset", VECTOR2, SHADOW_OFFSET )
88 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "shadow-color", VECTOR4, SHADOW_COLOR )
89 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-enabled", BOOLEAN, UNDERLINE_ENABLED )
90 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-color", VECTOR4, UNDERLINE_COLOR )
91 DALI_PROPERTY_REGISTRATION( Toolkit, TextLabel, "underline-height", FLOAT, UNDERLINE_HEIGHT )
93 DALI_TYPE_REGISTRATION_END()
97 Toolkit::TextLabel TextLabel::New()
99 // Create the implementation, temporarily owned by this handle on stack
100 IntrusivePtr< TextLabel > impl = new TextLabel();
102 // Pass ownership to CustomActor handle
103 Toolkit::TextLabel handle( *impl );
105 // Second-phase init of the implementation
106 // This can only be done after the CustomActor connection has been made...
112 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
114 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
118 TextLabel& impl( GetImpl( label ) );
121 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
123 const int backend = value.Get< int >();
125 if( impl.mRenderingBackend != backend )
127 impl.mRenderingBackend = backend;
128 impl.mRenderer.Reset();
129 impl.RequestTextRelayout();
133 case Toolkit::TextLabel::Property::TEXT:
135 if( impl.mController )
137 impl.mController->SetText( value.Get< std::string >() );
141 case Toolkit::TextLabel::Property::FONT_FAMILY:
143 if( impl.mController )
145 const std::string fontFamily = value.Get< std::string >();
147 if( impl.mController->GetDefaultFontFamily() != fontFamily )
149 impl.mController->SetDefaultFontFamily( fontFamily );
154 case Toolkit::TextLabel::Property::FONT_STYLE:
156 if( impl.mController )
158 const std::string fontStyle = value.Get< std::string >();
160 if( impl.mController->GetDefaultFontStyle() != fontStyle )
162 impl.mController->SetDefaultFontStyle( fontStyle );
167 case Toolkit::TextLabel::Property::POINT_SIZE:
169 if( impl.mController )
171 const float pointSize = value.Get< float >();
173 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
175 impl.mController->SetDefaultPointSize( pointSize );
180 case Toolkit::TextLabel::Property::MULTI_LINE:
182 if( impl.mController )
184 impl.mController->SetMultiLineEnabled( value.Get< bool >() );
188 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
190 if( impl.mController )
192 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
193 HORIZONTAL_ALIGNMENT_STRING_TABLE,
194 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
196 impl.mController->SetHorizontalAlignment( alignment );
200 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
202 if( impl.mController )
204 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
205 VERTICAL_ALIGNMENT_STRING_TABLE,
206 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
208 impl.mController->SetVerticalAlignment( alignment );
213 case Toolkit::TextLabel::Property::TEXT_COLOR:
215 if( impl.mController )
217 const Vector4 textColor = value.Get< Vector4 >();
218 if( impl.mController->GetTextColor() != textColor )
220 impl.mController->SetTextColor( textColor );
221 impl.mRenderer.Reset();
227 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
229 if( impl.mController )
231 const Vector2 shadowOffset = value.Get< Vector2 >();
232 if ( impl.mController->GetShadowOffset() != shadowOffset )
234 impl.mController->SetShadowOffset( shadowOffset );
235 impl.mRenderer.Reset();
240 case Toolkit::TextLabel::Property::SHADOW_COLOR:
242 if( impl.mController )
244 const Vector4 shadowColor = value.Get< Vector4 >();
245 if ( impl.mController->GetShadowColor() != shadowColor )
247 impl.mController->SetShadowColor( shadowColor );
248 impl.mRenderer.Reset();
253 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
255 if( impl.mController )
257 const Vector4 color = value.Get< Vector4 >();
258 if ( impl.mController->GetUnderlineColor() != color )
260 impl.mController->SetUnderlineColor( color );
261 impl.mRenderer.Reset();
266 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
268 if( impl.mController )
270 const bool enabled = value.Get< bool >();
271 if ( impl.mController->IsUnderlineEnabled() != enabled )
273 impl.mController->SetUnderlineEnabled( enabled );
274 impl.mRenderer.Reset();
280 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
282 if( impl.mController )
284 float height = value.Get< float >();
285 if ( impl.mController->GetUnderlineHeight() != height )
287 impl.mController->SetUnderlineHeight( height );
288 impl.mRenderer.Reset();
297 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
299 Property::Value value;
301 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
305 TextLabel& impl( GetImpl( label ) );
308 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
310 value = impl.mRenderingBackend;
313 case Toolkit::TextLabel::Property::TEXT:
315 if( impl.mController )
318 impl.mController->GetText( text );
323 case Toolkit::TextLabel::Property::FONT_FAMILY:
325 if( impl.mController )
327 value = impl.mController->GetDefaultFontFamily();
331 case Toolkit::TextLabel::Property::FONT_STYLE:
333 if( impl.mController )
335 value = impl.mController->GetDefaultFontStyle();
339 case Toolkit::TextLabel::Property::POINT_SIZE:
341 if( impl.mController )
343 value = impl.mController->GetDefaultPointSize();
347 case Toolkit::TextLabel::Property::MULTI_LINE:
349 if( impl.mController )
351 value = impl.mController->IsMultiLineEnabled();
355 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
357 if( impl.mController )
359 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
360 HORIZONTAL_ALIGNMENT_STRING_TABLE,
361 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
365 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
367 if( impl.mController )
369 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
370 VERTICAL_ALIGNMENT_STRING_TABLE,
371 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
375 case Toolkit::TextLabel::Property::TEXT_COLOR:
377 if ( impl.mController )
379 value = impl.mController->GetTextColor();
383 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
385 if ( impl.mController )
387 value = impl.mController->GetShadowOffset();
391 case Toolkit::TextLabel::Property::SHADOW_COLOR:
393 if ( impl.mController )
395 value = impl.mController->GetShadowColor();
399 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
401 if ( impl.mController )
403 value = impl.mController->GetUnderlineColor();
407 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
409 if ( impl.mController )
411 value = impl.mController->IsUnderlineEnabled();
415 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
417 if ( impl.mController )
419 value = impl.mController->GetUnderlineHeight();
429 void TextLabel::OnInitialize()
433 mController = Text::Controller::New( *this );
435 // Use height-for-width negotiation by default
436 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
437 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
439 // Enable the text ellipsis.
440 LayoutEngine& engine = mController->GetLayoutEngine();
441 engine.SetTextEllipsisEnabled( true );
444 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
446 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
449 Vector3 TextLabel::GetNaturalSize()
451 return mController->GetNaturalSize();
454 float TextLabel::GetHeightForWidth( float width )
456 return mController->GetHeightForWidth( width );
459 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
461 if( mController->Relayout( size ) ||
466 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
469 RenderableActor renderableActor;
472 renderableActor = mRenderer->Render( mController->GetView() );
475 if( renderableActor != mRenderableActor )
477 UnparentAndReset( mRenderableActor );
479 if( renderableActor )
481 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
482 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
484 Self().Add( renderableActor );
487 mRenderableActor = renderableActor;
492 void TextLabel::RequestTextRelayout()
497 void TextLabel::TextChanged()
499 // TextLabel does not provide a signal for this
502 void TextLabel::MaxLengthReached()
504 // Pure Virtual from TextController Interface, only needed when inputting text
507 TextLabel::TextLabel()
508 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
509 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
513 TextLabel::~TextLabel()
517 } // namespace Internal
519 } // namespace Toolkit