Changed button to use ImageView instead of ImageActor and enabled custom fragment...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / image-view / image-view-impl.cpp
1 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
2
3 // CLASS HEADER
4 #include "image-view-impl.h"
5
6 // EXTERNAL INCLUDES
7 #include <dali/public-api/images/resource-image.h>
8 #include <dali/public-api/object/type-registry.h>
9 #include <dali/devel-api/object/type-registry-helper.h>
10 #include <dali/devel-api/scripting/scripting.h>
11
12 // INTERNAL INCLUDES
13 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
14 #include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
15 #include <dali-toolkit/internal/controls/renderers/image/image-renderer.h>
16
17 namespace Dali
18 {
19
20 namespace Toolkit
21 {
22
23 namespace Internal
24 {
25
26 namespace
27 {
28
29 BaseHandle Create()
30 {
31   return Toolkit::ImageView::New();
32 }
33
34 // Setup properties, signals and actions using the type-registry.
35 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::ImageView, Toolkit::Control, Create );
36 DALI_PROPERTY_REGISTRATION( Toolkit, ImageView, "image", MAP, IMAGE )
37 DALI_TYPE_REGISTRATION_END()
38
39 } // anonymous namespace
40
41 using namespace Dali;
42
43 ImageView::ImageView()
44 : Control( ControlBehaviour( ACTOR_BEHAVIOUR_NONE ) )
45 {
46 }
47
48 ImageView::~ImageView()
49 {
50 }
51
52 Toolkit::ImageView ImageView::New()
53 {
54   ImageView* impl = new ImageView();
55
56   Toolkit::ImageView handle = Toolkit::ImageView( *impl );
57
58   // Second-phase init of the implementation
59   // This can only be done after the CustomActor connection has been made...
60   impl->Initialize();
61
62   return handle;
63 }
64
65 /////////////////////////////////////////////////////////////
66
67 void ImageView::SetImage( Image image )
68 {
69   if( ( mImage != image ) ||
70       ! mUrl.empty()      ||   // If we're changing from a URL type to an Image type
71       ! mPropertyMap.Empty() ) // If we're changing from a property map type to an Image type
72   {
73     mUrl.clear();
74     mPropertyMap.Clear();
75
76     mImage = image;
77
78     Actor self = Self();
79     Toolkit::RendererFactory::Get().ResetRenderer( mRenderer, self, image );
80     mImageSize = image ? ImageDimensions( image.GetWidth(), image.GetHeight() ) : ImageDimensions( 0, 0 );
81
82     RelayoutRequest();
83   }
84 }
85
86 void ImageView::SetImage( Property::Map map )
87 {
88   mUrl.clear();
89   mImage.Reset();
90   mPropertyMap = map;
91
92   Actor self = Self();
93   Toolkit::RendererFactory::Get().ResetRenderer( mRenderer, self, mPropertyMap );
94
95   Property::Value* widthValue = mPropertyMap.Find( "width" );
96   if( widthValue )
97   {
98     int width;
99     if( widthValue->Get( width ) )
100     {
101       mImageSize = ImageDimensions( width, mImageSize.GetHeight() );
102     }
103   }
104
105   Property::Value* heightValue = mPropertyMap.Find( "height" );
106   if( heightValue )
107   {
108     int height;
109     if( heightValue->Get( height ) )
110     {
111       mImageSize = ImageDimensions( mImageSize.GetWidth(), height );
112     }
113   }
114
115   RelayoutRequest();
116 }
117
118 void ImageView::SetImage( const std::string& url, ImageDimensions size )
119 {
120   if( ( mUrl != url ) ||
121       mImage          ||       // If we're changing from an Image type to a URL type
122       ! mPropertyMap.Empty() ) // If we're changing from a property map type to a URL type
123   {
124     mImage.Reset();
125     mPropertyMap.Clear();
126
127     mUrl = url;
128
129     if( size.GetWidth() == 0u && size.GetHeight() == 0u )
130     {
131       mImageSize = ResourceImage::GetImageSize( mUrl );
132     }
133     else
134     {
135       mImageSize = size;
136     }
137
138     Actor self = Self();
139     Toolkit::RendererFactory::Get().ResetRenderer( mRenderer, self, mUrl, mImageSize );
140
141     RelayoutRequest();
142   }
143 }
144
145 void ImageView::SetDepthIndex( int depthIndex )
146 {
147   mRenderer.SetDepthIndex( depthIndex );
148 }
149
150 Vector3 ImageView::GetNaturalSize()
151 {
152   Vector3 size;
153
154   size.x = mImageSize.GetWidth();
155   size.y = mImageSize.GetHeight();
156   size.z = std::min(size.x, size.y);
157
158   if( size.x > 0 && size.y > 0 )
159   {
160     return size;
161   }
162   else
163   {
164     // if no image then use Control's natural size
165     return Control::GetNaturalSize();
166   }
167 }
168
169 float ImageView::GetHeightForWidth( float width )
170 {
171   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
172   {
173     return GetHeightForWidthBase( width );
174   }
175   else
176   {
177     return Control::GetHeightForWidth( width );
178   }
179 }
180
181 float ImageView::GetWidthForHeight( float height )
182 {
183   if( mImageSize.GetWidth() > 0 && mImageSize.GetHeight() > 0 )
184   {
185     return GetWidthForHeightBase( height );
186   }
187   else
188   {
189     return Control::GetWidthForHeight( height );
190   }
191 }
192
193 ///////////////////////////////////////////////////////////
194 //
195 // Private methods
196 //
197
198 void ImageView::OnStageConnection( int depth )
199 {
200   Control::OnStageConnection( depth );
201
202   if( mRenderer )
203   {
204     CustomActor self = Self();
205     mRenderer.SetOnStage( self );
206   }
207 }
208
209 void ImageView::OnStageDisconnection()
210 {
211   if( mRenderer )
212   {
213     CustomActor self = Self();
214     mRenderer.SetOffStage( self );
215   }
216
217   Control::OnStageDisconnection();
218 }
219
220
221 ///////////////////////////////////////////////////////////
222 //
223 // Properties
224 //
225
226 void ImageView::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
227 {
228   Toolkit::ImageView imageView = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
229
230   if ( imageView )
231   {
232     switch ( index )
233     {
234       case Toolkit::ImageView::Property::IMAGE:
235       {
236         std::string imageUrl;
237         if( value.Get( imageUrl ) )
238         {
239           ImageView& impl = GetImpl( imageView );
240           impl.SetImage( imageUrl, ImageDimensions() );
241         }
242
243         // if its not a string then get a Property::Map from the property if possible.
244         Property::Map map;
245         if( value.Get( map ) )
246         {
247           ImageView& impl = GetImpl( imageView );
248           impl.SetImage( map );
249         }
250
251         break;
252       }
253     }
254   }
255 }
256
257 Property::Value ImageView::GetProperty( BaseObject* object, Property::Index propertyIndex )
258 {
259   Property::Value value;
260
261   Toolkit::ImageView imageview = Toolkit::ImageView::DownCast( Dali::BaseHandle( object ) );
262
263   if ( imageview )
264   {
265     switch ( propertyIndex )
266     {
267       case Toolkit::ImageView::Property::IMAGE:
268       {
269         ImageView& impl = GetImpl( imageview );
270         if ( !impl.mUrl.empty() )
271         {
272           value = impl.mUrl;
273         }
274         else if( impl.mImage )
275         {
276           Property::Map map;
277           Scripting::CreatePropertyMap( impl.mImage, map );
278           value = map;
279         }
280         else if( !impl.mPropertyMap.Empty() )
281         {
282           value = impl.mPropertyMap;
283         }
284         break;
285       }
286     }
287   }
288
289   return value;
290 }
291
292 } // namespace Internal
293 } // namespace Toolkit
294 } // namespace Dali