Changed Type of PROPERTY_RENDERING_BACKEND to signed, to correct Property::Value...
[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/public-api/text/layouts/layout-engine.h>
27 #include <dali-toolkit/public-api/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         }
102         break;
103       }
104       case Toolkit::TextLabel::PROPERTY_TEXT:
105       {
106         if( impl.mController )
107         {
108           impl.mController->SetText( value.Get< std::string >() );
109         }
110         break;
111       }
112       case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
113       {
114         if( impl.mController )
115         {
116           LayoutEngine::Layout layout = value.Get< bool >() ? LayoutEngine::MULTI_LINE_BOX : LayoutEngine::SINGLE_LINE_BOX;
117           impl.mController->GetLayoutEngine().SetLayout( layout );
118         }
119         break;
120       }
121     }
122   }
123 }
124
125 Property::Value TextLabel::GetProperty( BaseObject* object, Property::Index index )
126 {
127   Property::Value value;
128
129   Toolkit::TextLabel label = Toolkit::TextLabel::DownCast( Dali::BaseHandle( object ) );
130
131   if( label )
132   {
133     TextLabel& impl( GetImpl( label ) );
134     switch( index )
135     {
136       case Toolkit::TextLabel::PROPERTY_RENDERING_BACKEND:
137       {
138         value = impl.mRenderingBackend;
139         break;
140       }
141
142       case Toolkit::TextLabel::PROPERTY_TEXT:
143       {
144         DALI_LOG_WARNING( "UTF-8 text representation was discarded\n" );
145         break;
146       }
147
148       case Toolkit::TextLabel::PROPERTY_MULTI_LINE:
149       {
150         if( impl.mController )
151         {
152           value = impl.mController->GetLayoutEngine().GetLayout();
153         }
154         break;
155       }
156     }
157   }
158
159   return value;
160 }
161
162 void TextLabel::OnInitialize()
163 {
164   mController = Text::Controller::New( *this );
165 }
166
167 Vector3 TextLabel::GetNaturalSize()
168 {
169   return mController->GetNaturalSize();
170 }
171
172 float TextLabel::GetHeightForWidth( float width )
173 {
174   return mController->GetHeightForWidth( width );
175 }
176
177 void TextLabel::OnRelayout( const Vector2& size, ActorSizeContainer& container )
178 {
179   if( mController->Relayout( size ) )
180   {
181     if( !mRenderer )
182     {
183       mRenderer = Backend::Get().NewRenderer( mRenderingBackend );
184     }
185
186     if( mRenderer )
187     {
188       Actor renderableActor = mRenderer->Render( mController->GetView() );
189
190       if( renderableActor )
191       {
192         Self().Add( renderableActor );
193       }
194     }
195   }
196 }
197
198 void TextLabel::RequestTextRelayout()
199 {
200   RelayoutRequest();
201 }
202
203 TextLabel::TextLabel()
204 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_NONE ) ),
205   mRenderingBackend( DEFAULT_RENDERING_BACKEND )
206 {
207 }
208
209 TextLabel::~TextLabel()
210 {
211 }
212
213 } // namespace Internal
214
215 } // namespace Toolkit
216
217 } // namespace Dali