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_PROPERTY_REGISTRATION( TextLabel, "underline-height", FLOAT, UNDERLINE_HEIGHT )
92 DALI_TYPE_REGISTRATION_END()
96 Toolkit::TextLabel TextLabel::New()
98 // Create the implementation, temporarily owned by this handle on stack
99 IntrusivePtr< TextLabel > impl = new TextLabel();
101 // Pass ownership to CustomActor handle
102 Toolkit::TextLabel handle( *impl );
104 // Second-phase init of the implementation
105 // This can only be done after the CustomActor connection has been made...
111 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
113 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
117 TextLabel& impl( GetImpl( label ) );
120 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
122 int backend = value.Get< int >();
124 if( impl.mRenderingBackend != backend )
126 impl.mRenderingBackend = backend;
127 impl.mRenderer.Reset();
128 impl.RequestTextRelayout();
132 case Toolkit::TextLabel::Property::TEXT:
134 if( impl.mController )
136 impl.mController->SetText( value.Get< std::string >() );
137 impl.RequestTextRelayout();
141 case Toolkit::TextLabel::Property::FONT_FAMILY:
143 if( impl.mController )
145 std::string fontFamily = value.Get< std::string >();
147 if( impl.mController->GetDefaultFontFamily() != fontFamily )
149 impl.mController->SetDefaultFontFamily( fontFamily );
150 impl.RequestTextRelayout();
155 case Toolkit::TextLabel::Property::FONT_STYLE:
157 if( impl.mController )
159 std::string fontStyle = value.Get< std::string >();
161 if( impl.mController->GetDefaultFontStyle() != fontStyle )
163 impl.mController->SetDefaultFontStyle( fontStyle );
164 impl.RequestTextRelayout();
169 case Toolkit::TextLabel::Property::POINT_SIZE:
171 if( impl.mController )
173 float pointSize = value.Get< float >();
175 if( fabsf(impl.mController->GetDefaultPointSize() - pointSize) > Math::MACHINE_EPSILON_1 )
177 impl.mController->SetDefaultPointSize( pointSize );
178 impl.RequestTextRelayout();
183 case Toolkit::TextLabel::Property::MULTI_LINE:
185 if( impl.mController )
187 LayoutEngine& engine = impl.mController->GetLayoutEngine();
188 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
190 if( engine.GetLayout() != layout )
192 engine.SetLayout( layout );
193 impl.RequestTextRelayout();
198 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
200 LayoutEngine& engine = impl.mController->GetLayoutEngine();
201 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
202 HORIZONTAL_ALIGNMENT_STRING_TABLE,
203 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
205 if( engine.GetHorizontalAlignment() != alignment )
207 engine.SetHorizontalAlignment( alignment );
208 impl.RequestTextRelayout();
212 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
214 LayoutEngine& engine = impl.mController->GetLayoutEngine();
215 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
216 VERTICAL_ALIGNMENT_STRING_TABLE,
217 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
219 if( engine.GetVerticalAlignment() != alignment )
221 engine.SetVerticalAlignment( alignment );
222 impl.RequestTextRelayout();
227 case Toolkit::TextLabel::Property::TEXT_COLOR:
229 if ( impl.mController )
231 Vector4 textColor = value.Get< Vector4 >();
232 if ( impl.mController->GetTextColor() != textColor )
234 impl.mController->SetTextColor( textColor );
235 impl.RequestTextRelayout();
241 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
243 if( impl.mController )
245 Vector2 shadowOffset = value.Get< Vector2 >();
246 if ( impl.mController->GetShadowOffset() != shadowOffset )
248 impl.mController->SetShadowOffset( shadowOffset );
249 impl.RequestTextRelayout();
254 case Toolkit::TextLabel::Property::SHADOW_COLOR:
256 if( impl.mController )
258 Vector4 shadowColor = value.Get< Vector4 >();
259 if ( impl.mController->GetShadowColor() != shadowColor )
261 impl.mController->SetShadowColor( shadowColor );
262 impl.RequestTextRelayout();
267 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
269 if( impl.mController )
271 Vector4 color = value.Get< Vector4 >();
272 if ( impl.mController->GetUnderlineColor() != color )
274 impl.mController->SetUnderlineColor( color );
275 impl.RequestTextRelayout();
280 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
282 if( impl.mController )
284 bool enabled = value.Get< bool >();
285 if ( impl.mController->IsUnderlineEnabled() != enabled )
287 impl.mController->SetUnderlineEnabled( enabled );
288 impl.RequestTextRelayout();
294 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
296 if( impl.mController )
298 float height = value.Get< float >();
299 if ( impl.mController->GetUnderlineHeight() != height )
301 impl.mController->SetUnderlineHeight( height );
302 impl.RequestTextRelayout();
311 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
313 Property::Value value;
315 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
319 TextLabel& impl( GetImpl( label ) );
322 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
324 value = impl.mRenderingBackend;
327 case Toolkit::TextLabel::Property::TEXT:
329 if( impl.mController )
332 impl.mController->GetText( text );
337 case Toolkit::TextLabel::Property::MULTI_LINE:
339 if( impl.mController )
341 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
345 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
347 if( impl.mController )
349 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
350 HORIZONTAL_ALIGNMENT_STRING_TABLE,
351 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
355 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
357 if( impl.mController )
359 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
360 VERTICAL_ALIGNMENT_STRING_TABLE,
361 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
365 case Toolkit::TextLabel::Property::TEXT_COLOR:
367 if ( impl.mController )
369 value = impl.mController->GetTextColor();
373 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
375 if ( impl.mController )
377 value = impl.mController->GetShadowOffset();
381 case Toolkit::TextLabel::Property::SHADOW_COLOR:
383 if ( impl.mController )
385 value = impl.mController->GetShadowColor();
389 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
391 if ( impl.mController )
393 value = impl.mController->GetUnderlineColor();
397 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
399 if ( impl.mController )
401 value = impl.mController->IsUnderlineEnabled();
405 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
407 if ( impl.mController )
409 value = impl.mController->GetUnderlineHeight();
419 void TextLabel::OnInitialize()
423 mController = Text::Controller::New( *this );
425 // Use height-for-width negotiation by default
426 self.SetResizePolicy( FILL_TO_PARENT, WIDTH );
427 self.SetResizePolicy( DIMENSION_DEPENDENCY, HEIGHT );
430 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
432 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
435 Vector3 TextLabel::GetNaturalSize()
437 return mController->GetNaturalSize();
440 float TextLabel::GetHeightForWidth( float width )
442 return mController->GetHeightForWidth( width );
445 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
447 if( mController->Relayout( size ) ||
452 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
455 RenderableActor renderableActor;
458 renderableActor = mRenderer->Render( mController->GetView() );
461 if( renderableActor != mRenderableActor )
463 UnparentAndReset( mRenderableActor );
465 if( renderableActor )
467 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
468 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
470 Self().Add( renderableActor );
473 mRenderableActor = renderableActor;
478 void TextLabel::RequestTextRelayout()
483 TextLabel::TextLabel()
484 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
485 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
489 TextLabel::~TextLabel()
493 } // namespace Internal
495 } // namespace Toolkit