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/integration-api/debug.h>
27 #include <dali-toolkit/public-api/text/rendering-backend.h>
28 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
29 #include <dali-toolkit/internal/text/rendering/text-backend.h>
30 #include <dali-toolkit/internal/styling/style-manager-impl.h>
32 using Dali::Toolkit::Text::LayoutEngine;
33 using Dali::Toolkit::Text::Backend;
46 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
55 return Toolkit::TextLabel::New();
58 // Setup properties, signals and actions using the type-registry.
59 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
61 DALI_PROPERTY_REGISTRATION( TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
62 DALI_PROPERTY_REGISTRATION( TextLabel, "text", STRING, TEXT )
63 DALI_PROPERTY_REGISTRATION( TextLabel, "font-family", STRING, FONT_FAMILY )
64 DALI_PROPERTY_REGISTRATION( TextLabel, "font-style", STRING, FONT_STYLE )
65 DALI_PROPERTY_REGISTRATION( TextLabel, "point-size", FLOAT, POINT_SIZE )
66 DALI_PROPERTY_REGISTRATION( TextLabel, "multi-line", BOOLEAN, MULTI_LINE )
68 DALI_TYPE_REGISTRATION_END()
72 Toolkit::TextLabel TextLabel::New()
74 // Create the implementation, temporarily owned by this handle on stack
75 IntrusivePtr< TextLabel > impl = new TextLabel();
77 // Pass ownership to CustomActor handle
78 Toolkit::TextLabel handle( *impl );
80 // Second-phase init of the implementation
81 // This can only be done after the CustomActor connection has been made...
87 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
89 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
93 TextLabel& impl( GetImpl( label ) );
96 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
98 int backend = value.Get< int >();
100 if( impl.mRenderingBackend != backend )
102 impl.mRenderingBackend = backend;
103 impl.mRenderer.Reset();
104 impl.RequestTextRelayout();
108 case Toolkit::TextLabel::Property::TEXT:
110 if( impl.mController )
112 impl.mController->SetText( value.Get< std::string >() );
113 impl.RequestTextRelayout();
117 case Toolkit::TextLabel::Property::FONT_FAMILY:
119 if( impl.mController )
121 std::string fontFamily = value.Get< std::string >();
123 if( impl.mController->GetDefaultFontFamily() != fontFamily )
125 impl.mController->SetDefaultFontFamily( fontFamily );
126 impl.RequestTextRelayout();
131 case Toolkit::TextLabel::Property::FONT_STYLE:
133 if( impl.mController )
135 std::string fontStyle = value.Get< std::string >();
137 if( impl.mController->GetDefaultFontStyle() != fontStyle )
139 impl.mController->SetDefaultFontStyle( fontStyle );
140 impl.RequestTextRelayout();
145 case Toolkit::TextLabel::Property::POINT_SIZE:
147 if( impl.mController )
149 float pointSize = value.Get< float >();
151 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
153 impl.mController->SetDefaultPointSize( pointSize );
154 impl.RequestTextRelayout();
159 case Toolkit::TextLabel::Property::MULTI_LINE:
161 if( impl.mController )
163 LayoutEngine& engine = impl.mController->GetLayoutEngine();
164 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
166 if( engine.GetLayout() != layout )
168 impl.mController->GetLayoutEngine().SetLayout( layout );
169 impl.RequestTextRelayout();
178 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
180 Property::Value value;
182 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
186 TextLabel& impl( GetImpl( label ) );
189 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
191 value = impl.mRenderingBackend;
194 case Toolkit::TextLabel::Property::TEXT:
196 if( impl.mController )
199 impl.mController->GetText( text );
204 case Toolkit::TextLabel::Property::MULTI_LINE:
206 if( impl.mController )
208 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
218 void TextLabel::OnInitialize()
220 mController = Text::Controller::New( *this );
223 void TextLabel::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange change )
225 GetImpl( styleManager ).ApplyThemeStyle( Toolkit::Control( GetOwner() ) );
228 Vector3 TextLabel::GetNaturalSize()
230 return mController->GetNaturalSize();
233 float TextLabel::GetHeightForWidth( float width )
235 return mController->GetHeightForWidth( width );
238 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
240 if( mController->Relayout( size ) ||
245 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
248 RenderableActor renderableActor;
251 renderableActor = mRenderer->Render( mController->GetView() );
254 if( renderableActor != mRenderableActor )
256 UnparentAndReset( mRenderableActor );
258 if( renderableActor )
260 Self().Add( renderableActor );
263 mRenderableActor = renderableActor;
268 void TextLabel::RequestTextRelayout()
273 TextLabel::TextLabel()
274 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
275 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
279 TextLabel::~TextLabel()
283 } // namespace Internal
285 } // namespace Toolkit