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 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 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 LayoutEngine::HorizontalAlignment alignment( LayoutEngine::HORIZONTAL_ALIGN_BEGIN );
193 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
194 HORIZONTAL_ALIGNMENT_STRING_TABLE,
195 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT,
198 impl.mController->SetHorizontalAlignment( alignment );
203 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
205 if( impl.mController )
207 LayoutEngine::VerticalAlignment alignment( LayoutEngine::VERTICAL_ALIGN_BOTTOM );
208 if( Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
209 VERTICAL_ALIGNMENT_STRING_TABLE,
210 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT,
213 impl.mController->SetVerticalAlignment( alignment );
219 case Toolkit::TextLabel::Property::TEXT_COLOR:
221 if( impl.mController )
223 const Vector4 textColor = value.Get< Vector4 >();
224 if( impl.mController->GetTextColor() != textColor )
226 impl.mController->SetTextColor( textColor );
227 impl.mRenderer.Reset();
233 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
235 if( impl.mController )
237 const Vector2 shadowOffset = value.Get< Vector2 >();
238 if ( impl.mController->GetShadowOffset() != shadowOffset )
240 impl.mController->SetShadowOffset( shadowOffset );
241 impl.mRenderer.Reset();
246 case Toolkit::TextLabel::Property::SHADOW_COLOR:
248 if( impl.mController )
250 const Vector4 shadowColor = value.Get< Vector4 >();
251 if ( impl.mController->GetShadowColor() != shadowColor )
253 impl.mController->SetShadowColor( shadowColor );
254 impl.mRenderer.Reset();
259 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
261 if( impl.mController )
263 const Vector4 color = value.Get< Vector4 >();
264 if ( impl.mController->GetUnderlineColor() != color )
266 impl.mController->SetUnderlineColor( color );
267 impl.mRenderer.Reset();
272 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
274 if( impl.mController )
276 const bool enabled = value.Get< bool >();
277 if ( impl.mController->IsUnderlineEnabled() != enabled )
279 impl.mController->SetUnderlineEnabled( enabled );
280 impl.mRenderer.Reset();
286 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
288 if( impl.mController )
290 float height = value.Get< float >();
291 if ( impl.mController->GetUnderlineHeight() != height )
293 impl.mController->SetUnderlineHeight( height );
294 impl.mRenderer.Reset();
303 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
305 Property::Value value;
307 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
311 TextLabel& impl( GetImpl( label ) );
314 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
316 value = impl.mRenderingBackend;
319 case Toolkit::TextLabel::Property::TEXT:
321 if( impl.mController )
324 impl.mController->GetText( text );
329 case Toolkit::TextLabel::Property::FONT_FAMILY:
331 if( impl.mController )
333 value = impl.mController->GetDefaultFontFamily();
337 case Toolkit::TextLabel::Property::FONT_STYLE:
339 if( impl.mController )
341 value = impl.mController->GetDefaultFontStyle();
345 case Toolkit::TextLabel::Property::POINT_SIZE:
347 if( impl.mController )
349 value = impl.mController->GetDefaultPointSize();
353 case Toolkit::TextLabel::Property::MULTI_LINE:
355 if( impl.mController )
357 value = impl.mController->IsMultiLineEnabled();
361 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
363 if( impl.mController )
365 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetHorizontalAlignment(),
366 HORIZONTAL_ALIGNMENT_STRING_TABLE,
367 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
370 value = std::string( name );
375 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
377 if( impl.mController )
379 const char* name = Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetVerticalAlignment(),
380 VERTICAL_ALIGNMENT_STRING_TABLE,
381 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
384 value = std::string( name );
389 case Toolkit::TextLabel::Property::TEXT_COLOR:
391 if ( impl.mController )
393 value = impl.mController->GetTextColor();
397 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
399 if ( impl.mController )
401 value = impl.mController->GetShadowOffset();
405 case Toolkit::TextLabel::Property::SHADOW_COLOR:
407 if ( impl.mController )
409 value = impl.mController->GetShadowColor();
413 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
415 if ( impl.mController )
417 value = impl.mController->GetUnderlineColor();
421 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
423 if ( impl.mController )
425 value = impl.mController->IsUnderlineEnabled();
429 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
431 if ( impl.mController )
433 value = impl.mController->GetUnderlineHeight();
443 void TextLabel::OnInitialize()
447 mController = Text::Controller::New( *this );
449 // Use height-for-width negotiation by default
450 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
451 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
453 // Enable the text ellipsis.
454 LayoutEngine& engine = mController->GetLayoutEngine();
455 engine.SetTextEllipsisEnabled( true );
458 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
460 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
463 Vector3 TextLabel::GetNaturalSize()
465 return mController->GetNaturalSize();
468 float TextLabel::GetHeightForWidth( float width )
470 return mController->GetHeightForWidth( width );
473 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
475 if( mController->Relayout( size ) ||
480 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
483 RenderableActor renderableActor;
486 renderableActor = mRenderer->Render( mController->GetView() );
489 if( renderableActor != mRenderableActor )
491 UnparentAndReset( mRenderableActor );
493 if( renderableActor )
495 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
496 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
498 Self().Add( renderableActor );
501 mRenderableActor = renderableActor;
506 void TextLabel::AddDecoration( Actor& actor, bool needsClipping )
508 // TextLabel does not show decorations
511 void TextLabel::RequestTextRelayout()
516 void TextLabel::TextChanged()
518 // TextLabel does not provide a signal for this
521 void TextLabel::MaxLengthReached()
523 // Pure Virtual from TextController Interface, only needed when inputting text
526 TextLabel::TextLabel()
527 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
528 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
532 TextLabel::~TextLabel()
536 } // namespace Internal
538 } // namespace Toolkit