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( 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 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 >() );
138 impl.RequestTextRelayout();
142 case Toolkit::TextLabel::Property::FONT_FAMILY:
144 if( impl.mController )
146 std::string fontFamily = value.Get< std::string >();
148 if( impl.mController->GetDefaultFontFamily() != fontFamily )
150 impl.mController->SetDefaultFontFamily( fontFamily );
151 impl.RequestTextRelayout();
156 case Toolkit::TextLabel::Property::FONT_STYLE:
158 if( impl.mController )
160 std::string fontStyle = value.Get< std::string >();
162 if( impl.mController->GetDefaultFontStyle() != fontStyle )
164 impl.mController->SetDefaultFontStyle( fontStyle );
165 impl.RequestTextRelayout();
170 case Toolkit::TextLabel::Property::POINT_SIZE:
172 if( impl.mController )
174 float pointSize = value.Get< float >();
176 if( !Equals( impl.mController->GetDefaultPointSize(), pointSize ) )
178 impl.mController->SetDefaultPointSize( pointSize );
179 impl.RequestTextRelayout();
184 case Toolkit::TextLabel::Property::MULTI_LINE:
186 if( impl.mController )
188 LayoutEngine& engine = impl.mController->GetLayoutEngine();
189 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
191 if( engine.GetLayout() != layout )
193 engine.SetLayout( layout );
194 impl.RequestTextRelayout();
199 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
201 LayoutEngine& engine = impl.mController->GetLayoutEngine();
202 const LayoutEngine::HorizontalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::HorizontalAlignment >( value.Get< std::string >().c_str(),
203 HORIZONTAL_ALIGNMENT_STRING_TABLE,
204 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT );
206 if( engine.GetHorizontalAlignment() != alignment )
208 engine.SetHorizontalAlignment( alignment );
209 impl.RequestTextRelayout();
213 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
215 LayoutEngine& engine = impl.mController->GetLayoutEngine();
216 const LayoutEngine::VerticalAlignment alignment = Scripting::GetEnumeration< Toolkit::Text::LayoutEngine::VerticalAlignment >( value.Get< std::string >().c_str(),
217 VERTICAL_ALIGNMENT_STRING_TABLE,
218 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT );
220 if( engine.GetVerticalAlignment() != alignment )
222 engine.SetVerticalAlignment( alignment );
223 impl.RequestTextRelayout();
228 case Toolkit::TextLabel::Property::TEXT_COLOR:
230 if ( impl.mController )
232 Vector4 textColor = value.Get< Vector4 >();
233 if ( impl.mController->GetTextColor() != textColor )
235 impl.mController->SetTextColor( textColor );
236 impl.RequestTextRelayout();
242 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
244 if( impl.mController )
246 Vector2 shadowOffset = value.Get< Vector2 >();
247 if ( impl.mController->GetShadowOffset() != shadowOffset )
249 impl.mController->SetShadowOffset( shadowOffset );
250 impl.RequestTextRelayout();
255 case Toolkit::TextLabel::Property::SHADOW_COLOR:
257 if( impl.mController )
259 Vector4 shadowColor = value.Get< Vector4 >();
260 if ( impl.mController->GetShadowColor() != shadowColor )
262 impl.mController->SetShadowColor( shadowColor );
263 impl.RequestTextRelayout();
268 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
270 if( impl.mController )
272 Vector4 color = value.Get< Vector4 >();
273 if ( impl.mController->GetUnderlineColor() != color )
275 impl.mController->SetUnderlineColor( color );
276 impl.RequestTextRelayout();
281 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
283 if( impl.mController )
285 bool enabled = value.Get< bool >();
286 if ( impl.mController->IsUnderlineEnabled() != enabled )
288 impl.mController->SetUnderlineEnabled( enabled );
289 impl.RequestTextRelayout();
295 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
297 if( impl.mController )
299 float height = value.Get< float >();
300 if ( impl.mController->GetUnderlineHeight() != height )
302 impl.mController->SetUnderlineHeight( height );
303 impl.RequestTextRelayout();
312 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
314 Property::Value value;
316 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
320 TextLabel& impl( GetImpl( label ) );
323 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
325 value = impl.mRenderingBackend;
328 case Toolkit::TextLabel::Property::TEXT:
330 if( impl.mController )
333 impl.mController->GetText( text );
338 case Toolkit::TextLabel::Property::MULTI_LINE:
340 if( impl.mController )
342 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
346 case Toolkit::TextLabel::Property::HORIZONTAL_ALIGNMENT:
348 if( impl.mController )
350 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::HorizontalAlignment >( impl.mController->GetLayoutEngine().GetHorizontalAlignment(),
351 HORIZONTAL_ALIGNMENT_STRING_TABLE,
352 HORIZONTAL_ALIGNMENT_STRING_TABLE_COUNT ) );
356 case Toolkit::TextLabel::Property::VERTICAL_ALIGNMENT:
358 if( impl.mController )
360 value = std::string( Scripting::GetEnumerationName< Toolkit::Text::LayoutEngine::VerticalAlignment >( impl.mController->GetLayoutEngine().GetVerticalAlignment(),
361 VERTICAL_ALIGNMENT_STRING_TABLE,
362 VERTICAL_ALIGNMENT_STRING_TABLE_COUNT ) );
366 case Toolkit::TextLabel::Property::TEXT_COLOR:
368 if ( impl.mController )
370 value = impl.mController->GetTextColor();
374 case Toolkit::TextLabel::Property::SHADOW_OFFSET:
376 if ( impl.mController )
378 value = impl.mController->GetShadowOffset();
382 case Toolkit::TextLabel::Property::SHADOW_COLOR:
384 if ( impl.mController )
386 value = impl.mController->GetShadowColor();
390 case Toolkit::TextLabel::Property::UNDERLINE_COLOR:
392 if ( impl.mController )
394 value = impl.mController->GetUnderlineColor();
398 case Toolkit::TextLabel::Property::UNDERLINE_ENABLED:
400 if ( impl.mController )
402 value = impl.mController->IsUnderlineEnabled();
406 case Toolkit::TextLabel::Property::UNDERLINE_HEIGHT:
408 if ( impl.mController )
410 value = impl.mController->GetUnderlineHeight();
420 void TextLabel::OnInitialize()
424 mController = Text::Controller::New( *this );
426 // Use height-for-width negotiation by default
427 self.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::WIDTH );
428 self.SetResizePolicy( ResizePolicy::DIMENSION_DEPENDENCY, Dimension::HEIGHT );
431 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
433 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
436 Vector3 TextLabel::GetNaturalSize()
438 return mController->GetNaturalSize();
441 float TextLabel::GetHeightForWidth( float width )
443 return mController->GetHeightForWidth( width );
446 void TextLabel::OnRelayout( const Vector2& size, RelayoutContainer& container )
448 if( mController->Relayout( size ) ||
453 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
456 RenderableActor renderableActor;
459 renderableActor = mRenderer->Render( mController->GetView() );
462 if( renderableActor != mRenderableActor )
464 UnparentAndReset( mRenderableActor );
466 if( renderableActor )
468 const Vector2& alignmentOffset = mController->GetAlignmentOffset();
469 renderableActor.SetPosition( alignmentOffset.x, alignmentOffset.y );
471 Self().Add( renderableActor );
474 mRenderableActor = renderableActor;
479 void TextLabel::RequestTextRelayout()
484 void TextLabel::MaxLengthReached()
486 // Pure Virtual from TextController Interface, only needed when inputting text
489 TextLabel::TextLabel()
490 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
491 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
495 TextLabel::~TextLabel()
499 } // namespace Internal
501 } // namespace Toolkit