TextLabel property fixes
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-label-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/controls/text-controls/text-label-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/integration-api/debug.h>
24
25 // INTERNAL INCLUDES
26 #include <dali-toolkit/internal/text/layouts/layout-engine.h>
27 #include <dali-toolkit/internal/text/rendering/text-backend.h>
28
29 using Dali::Toolkit::Text::LayoutEngine;
30 using Dali::Toolkit::Text::Backend;
31
32 namespace
33 {
34
35 const unsigned int DEFAULT_RENDERING_BACKEND = 0;
36
37 } // namespace
38
39 namespace Dali
40 {
41
42 namespace Toolkit
43 {
44
45 const Property::Index TextLabel::PROPERTY_RENDERING_BACKEND( Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX );
46 const Property::Index TextLabel::PROPERTY_TEXT(              Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX + 1 );
47 const Property::Index TextLabel::PROPERTY_MULTI_LINE(        Internal::TextLabel::TEXTLABEL_PROPERTY_START_INDEX + 2 );
48
49 namespace Internal
50 {
51
52 namespace
53 {
54
55 // Type registration
56 BaseHandle Create()
57 {
58   return Toolkit::TextLabel::New();
59 }
60
61 TypeRegistration mType( typeid(Toolkit::TextLabel), typeid(Toolkit::Control), Create );
62
63 PropertyRegistration property1( mType, "rendering-backend", Toolkit::TextLabel::PROPERTY_RENDERING_BACKEND, Property::INTEGER,          &TextLabel::SetProperty, &TextLabel::GetProperty );
64 PropertyRegistration property2( mType, "text",              Toolkit::TextLabel::PROPERTY_TEXT,              Property::STRING,           &TextLabel::SetProperty, &TextLabel::GetProperty );
65 PropertyRegistration property3( mType, "multi-line",        Toolkit::TextLabel::PROPERTY_MULTI_LINE,        Property::STRING,           &TextLabel::SetProperty, &TextLabel::GetProperty );
66
67 } // namespace
68
69 Toolkit::TextLabel TextLabel::New()
70 {
71   // Create the implementation, temporarily owned by this handle on stack
72   IntrusivePtr< TextLabel > impl = new TextLabel();
73
74   // Pass ownership to CustomActor handle
75   Toolkit::TextLabel handle( *impl );
76
77   // Second-phase init of the implementation
78   // This can only be done after the CustomActor connection has been made...
79   impl->Initialize();
80
81   return handle;
82 }
83
84 void TextLabel::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
85 {
86   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
87
88   if( label )
89   {
90     TextLabel& impl( GetImpl( label ) );
91     switch( index )
92     {
93       case Toolkit::TextLabel::PROPERTY_RENDERING_BACKEND:
94       {
95         unsigned int backend = value.Get< int >();
96
97         if( impl.mRenderingBackend != backend )
98         {
99           impl.mRenderingBackend = static_cast< unsigned int >( backend );
100           impl.mRenderer.Reset();
101           impl.RequestTextRelayout();
102         }
103         break;
104       }
105       case Toolkit::TextLabel::PROPERTY_TEXT:
106       {
107         if( impl.mController )
108         {
109           impl.mController->SetText( value.Get< std::string >() );
110         }
111         break;
112       }
113       case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
114       {
115         if( impl.mController )
116         {
117           LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
118           impl.mController->GetLayoutEngine().SetLayout( layout );
119           impl.RequestTextRelayout();
120         }
121         break;
122       }
123     }
124   }
125 }
126
127 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
128 {
129   Property::Value value;
130
131   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
132
133   if( label )
134   {
135     TextLabel& impl( GetImpl( label ) );
136     switch( index )
137     {
138       case Toolkit::TextLabel::PROPERTY_RENDERING_BACKEND:
139       {
140         value = impl.mRenderingBackend;
141         break;
142       }
143
144       case Toolkit::TextLabel::PROPERTY_TEXT:
145       {
146         if( impl.mController )
147         {
148           std::string text;
149           impl.mController->GetText( text );
150           value = text;
151         }
152
153         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
154         break;
155       }
156
157       case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
158       {
159         if( impl.mController )
160         {
161           value = static_cast<bool>( LayoutEngine::MULTI_LINE_BOX == impl.mController->GetLayoutEngine().GetLayout() );
162         }
163         break;
164       }
165     }
166   }
167
168   return value;
169 }
170
171 void TextLabel::OnInitialize()
172 {
173   mController = Text::Controller::New( *this );
174 }
175
176 Vector3 TextLabel::GetNaturalSize()
177 {
178   return mController->GetNaturalSize();
179 }
180
181 float TextLabel::GetHeightForWidth( float width )
182 {
183   return mController->GetHeightForWidth( width );
184 }
185
186 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
187 {
188   if( mController->Relayout( size ) ||
189       !mRenderer )
190   {
191     if( !mRenderer )
192     {
193       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
194     }
195
196     RenderableActor renderableActor;
197     if( mRenderer )
198     {
199       renderableActor = mRenderer->Render( mController->GetView() );
200     }
201
202     if( renderableActor != mRenderableActor )
203     {
204       UnparentAndReset( mRenderableActor );
205
206       if( renderableActor )
207       {
208         Self().Add( renderableActor );
209       }
210
211       mRenderableActor = renderableActor;
212     }
213   }
214 }
215
216 void TextLabel::RequestTextRelayout()
217 {
218   RelayoutRequest();
219 }
220
221 TextLabel::TextLabel()
222 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
223   mRenderingBackend( DEFAULT_RENDERING_BACKEND )
224 {
225 }
226
227 TextLabel::~TextLabel()
228 {
229 }
230
231 } // namespace Internal
232
233 } // namespace Toolkit
234
235 } // namespace Dali