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/internal/text/layouts/layout-engine.h>
28 #include <dali-toolkit/internal/text/rendering/text-backend.h>
29 #include <dali-toolkit/public-api/text/rendering-backend.h>
31 using Dali::Toolkit::Text::LayoutEngine;
32 using Dali::Toolkit::Text::Backend;
45 const unsigned int DEFAULT_RENDERING_BACKEND = Dali::Toolkit::Text::DEFAULT_RENDERING_BACKEND;
54 return Toolkit::TextLabel::New();
57 // Setup properties, signals and actions using the type-registry.
58 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextLabel, Toolkit::Control, Create );
60 DALI_PROPERTY_REGISTRATION( TextLabel, "rendering-backend", INTEGER, RENDERING_BACKEND )
61 DALI_PROPERTY_REGISTRATION( TextLabel, "text", STRING, TEXT )
62 DALI_PROPERTY_REGISTRATION( TextLabel, "font-family", STRING, FONT_FAMILY )
63 DALI_PROPERTY_REGISTRATION( TextLabel, "font-style", STRING, FONT_STYLE )
64 DALI_PROPERTY_REGISTRATION( TextLabel, "point-size", FLOAT, POINT_SIZE )
65 DALI_PROPERTY_REGISTRATION( TextLabel, "multi-line", BOOLEAN, MULTI_LINE )
67 DALI_TYPE_REGISTRATION_END()
71 Toolkit::TextLabel TextLabel::New()
73 // Create the implementation, temporarily owned by this handle on stack
74 IntrusivePtr< TextLabel > impl = new TextLabel();
76 // Pass ownership to CustomActor handle
77 Toolkit::TextLabel handle( *impl );
79 // Second-phase init of the implementation
80 // This can only be done after the CustomActor connection has been made...
86 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
88 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
92 TextLabel& impl( GetImpl( label ) );
95 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
97 int backend = value.Get< int >();
99 if( impl.mRenderingBackend != backend )
101 impl.mRenderingBackend = backend;
102 impl.mRenderer.Reset();
103 impl.RequestTextRelayout();
107 case Toolkit::TextLabel::Property::TEXT:
109 if( impl.mController )
111 impl.mController->SetText( value.Get< std::string >() );
112 impl.RequestTextRelayout();
116 case Toolkit::TextLabel::Property::FONT_FAMILY:
118 if( impl.mController )
120 std::string fontFamily = value.Get< std::string >();
122 if( impl.mController->GetDefaultFontFamily() != fontFamily )
124 impl.mController->SetDefaultFontFamily( fontFamily );
125 impl.RequestTextRelayout();
130 case Toolkit::TextLabel::Property::FONT_STYLE:
132 if( impl.mController )
134 std::string fontStyle = value.Get< std::string >();
136 if( impl.mController->GetDefaultFontStyle() != fontStyle )
138 impl.mController->SetDefaultFontStyle( fontStyle );
139 impl.RequestTextRelayout();
144 case Toolkit::TextLabel::Property::POINT_SIZE:
146 if( impl.mController )
148 float pointSize = value.Get< float >();
150 if( impl.mController->GetDefaultPointSize() != pointSize /*TODO - epsilon*/ )
152 impl.mController->SetDefaultPointSize( pointSize );
153 impl.RequestTextRelayout();
158 case Toolkit::TextLabel::Property::MULTI_LINE:
160 if( impl.mController )
162 LayoutEngine& engine = impl.mController->GetLayoutEngine();
163 LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
165 if( engine.GetLayout() != layout )
167 impl.mController->GetLayoutEngine().SetLayout( layout );
168 impl.RequestTextRelayout();
177 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
179 Property::Value value;
181 Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
185 TextLabel& impl( GetImpl( label ) );
188 case Toolkit::TextLabel::Property::RENDERING_BACKEND:
190 value = impl.mRenderingBackend;
193 case Toolkit::TextLabel::Property::TEXT:
195 if( impl.mController )
198 impl.mController->GetText( text );
203 case Toolkit::TextLabel::Property::MULTI_LINE:
205 if( impl.mController )
207 value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
217 void TextLabel::OnInitialize()
219 mController = Text::Controller::New( *this );
222 Vector3 TextLabel::GetNaturalSize()
224 return mController->GetNaturalSize();
227 float TextLabel::GetHeightForWidth( float width )
229 return mController->GetHeightForWidth( width );
232 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
234 if( mController->Relayout( size ) ||
239 mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
242 RenderableActor renderableActor;
245 renderableActor = mRenderer->Render( mController->GetView() );
248 if( renderableActor != mRenderableActor )
250 UnparentAndReset( mRenderableActor );
252 if( renderableActor )
254 Self().Add( renderableActor );
257 mRenderableActor = renderableActor;
262 void TextLabel::RequestTextRelayout()
267 TextLabel::TextLabel()
268 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
269 mRenderingBackend( DEFAULT_RENDERING_BACKEND )
273 TextLabel::~TextLabel()
277 } // namespace Internal
279 } // namespace Toolkit