Merge remote-tracking branch 'trace/tizen'
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ImageView.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17 namespace Tizen.NUI.BaseComponents
18 {
19     using System;
20     using System.Runtime.InteropServices;
21
22     /// <summary>
23     /// ImageView is a class for displaying an image resource.<br>
24     /// An instance of ImageView can be created using a URL or an Image instance.<br>
25     /// </summary>
26     public class ImageView : View
27     {
28         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
29
30         internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
31         {
32             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
33         }
34
35         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
36         {
37             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
38         }
39
40
41         /// <summary>
42         /// Event arguments of resource ready.
43         /// </summary>
44         public class ResourceReadyEventArgs : EventArgs
45         {
46             private View _view;
47
48             /// <summary>
49             /// The view whose resource is ready.
50             /// </summary>
51             public View View
52             {
53                 get
54                 {
55                     return _view;
56                 }
57                 set
58                 {
59                     _view = value;
60                 }
61             }
62         }
63
64         private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
65         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
66         private delegate void ResourceReadyEventCallbackType(IntPtr data);
67         private ResourceReadyEventCallbackType _resourceReadyEventCallback;
68
69         /// <summary>
70         /// Event for ResourceReady signal which can be used to subscribe/unsubscribe the event handler.<br>
71         /// This signal is emitted after all resources required by a control are loaded and ready.<br>
72         /// Most resources are only loaded when the control is placed on stage.<br>
73         /// </summary>
74         public event EventHandler<ResourceReadyEventArgs> ResourceReady
75         {
76             add
77             {
78                 if (_resourceReadyEventHandler == null)
79                 {
80                     _resourceReadyEventCallback = OnResourceReady;
81                     ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
82                 }
83
84                 _resourceReadyEventHandler += value;
85             }
86
87             remove
88             {
89                 _resourceReadyEventHandler -= value;
90
91                 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
92                 {
93                     ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
94                 }
95             }
96         }
97
98         // Callback for View ResourceReady signal
99         private void OnResourceReady(IntPtr data)
100         {
101             ResourceReadyEventArgs e = new ResourceReadyEventArgs();
102             if(data != null)
103             {
104                 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
105             }
106
107             if (_resourceReadyEventHandler != null)
108             {
109                 _resourceReadyEventHandler(this, e);
110             }
111         }
112
113         //you can override it to clean-up your own resources.
114         protected override void Dispose(DisposeTypes type)
115         {
116             if (disposed)
117             {
118                 return;
119             }
120
121             if(type == DisposeTypes.Explicit)
122             {
123                 //Called by User
124                 //Release your own managed resources here.
125                 //You should release all of your own disposable objects here.
126                 _border?.Dispose();
127                 _border = null;
128                 _nPatchMap?.Dispose();
129                 _nPatchMap = null;
130             }
131
132             //Release your own unmanaged resources here.
133             //You should not access any managed member here except static instance.
134             //because the execution order of Finalizes is non-deterministic.
135
136             if (swigCPtr.Handle != global::System.IntPtr.Zero)
137             {
138                 if (swigCMemOwn)
139                 {
140                     swigCMemOwn = false;
141                     NDalicPINVOKE.delete_ImageView(swigCPtr);
142                 }
143                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
144             }
145
146             base.Dispose(type);
147         }
148
149         internal class Property
150         {
151             internal static readonly int RESOURCE_URL = NDalicPINVOKE.ImageView_Property_RESOURCE_URL_get();
152             internal static readonly int IMAGE = NDalicPINVOKE.ImageView_Property_IMAGE_get();
153             internal static readonly int PRE_MULTIPLIED_ALPHA = NDalicPINVOKE.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
154             internal static readonly int PIXEL_AREA = NDalicPINVOKE.ImageView_Property_PIXEL_AREA_get();
155         }
156
157         /// <summary>
158         /// Creates an initialized ImageView.
159         /// </summary>
160         public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
161         {
162             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
163
164         }
165         /// <summary>
166         /// Creates an initialized ImageView from an URL to an image resource.<br>
167         /// If the string is empty, ImageView will not display anything.<br>
168         /// </summary>
169         /// <param name="url">The url of the image resource to display</param>
170         public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
171         {
172             _url = url;
173             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174
175         }
176         internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
177         {
178             _url = url;
179             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
180
181         }
182
183         [Obsolete("Please do not use! this will be deprecated")]
184         public new static ImageView DownCast(BaseHandle handle)
185         {
186             ImageView ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
187             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
188             return ret;
189         }
190         /// <summary>
191         /// Sets this ImageView from the given URL.<br>
192         /// If the URL is empty, ImageView will not display anything.<br>
193         /// </summary>
194         /// <param name="url">The URL to the image resource to display</param>
195         public void SetImage(string url)
196         {
197             _url = url;
198             NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200         }
201         internal void SetImage(string url, Uint16Pair size)
202         {
203             _url = url;
204             NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
205             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206         }
207
208         internal ViewResourceReadySignal ResourceReadySignal(View view) {
209             ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
210             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
211             return ret;
212         }
213
214         /// <summary>
215         /// Query if all resources required by a control are loaded and ready.<br>
216         /// Most resources are only loaded when the control is placed on stage.<br>
217         /// true if the resources are loaded and ready, false otherwise.<br>
218         /// </summary>
219         public bool IsResourceReady()
220         {
221             bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
222             if (NDalicPINVOKE.SWIGPendingException.Pending)
223                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
224             return ret;
225         }
226
227         /// <summary>
228         /// ImageView ResourceUrl, type string
229         /// </summary>
230         public string ResourceUrl
231         {
232             get
233             {
234                 GetProperty(ImageView.Property.RESOURCE_URL).Get(out _url);
235                 return _url;
236             }
237             set
238             {
239                 _url = value;
240                 UpdateImage();
241             }
242         }
243
244         /// <summary>
245         /// ImageView ImageMap, type PropertyMap : string if it is a url, map otherwise
246         /// </summary>
247         public PropertyMap ImageMap
248         {
249             get
250             {
251                 if (_border == null)
252                 {
253                     PropertyMap temp = new PropertyMap();
254                     GetProperty(ImageView.Property.IMAGE).Get(temp);
255                     return temp;
256                 }
257                 else
258                 {
259                     return null;
260                 }
261             }
262             set
263             {
264                 if (_border == null)
265                 {
266                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
267                 }
268             }
269         }
270
271         /// <summary>
272         /// ImageView PreMultipliedAlpha, type Boolean.<br>
273         /// Image must be initialized.<br>
274         /// </summary>
275         public bool PreMultipliedAlpha
276         {
277             get
278             {
279                 bool temp = false;
280                 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
281                 return temp;
282             }
283             set
284             {
285                 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
286             }
287         }
288
289         /// <summary>
290         /// ImageView PixelArea, type Vector4 (Animatable property).<br>
291         /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br>
292         /// </summary>
293         public RelativeVector4 PixelArea
294         {
295             get
296             {
297                 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
298                 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
299                 return temp;
300             }
301             set
302             {
303                 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
304             }
305         }
306
307         /// <summary>
308         /// The border of the image in the order: left, right, bottom, top.<br>
309         /// If set, ImageMap will be ignored.<br>
310         /// For N-Patch images only.<br>
311         /// Optional.
312         /// </summary>
313         public Rectangle Border
314         {
315             get
316             {
317                 return _border;
318             }
319             set
320             {
321                 _border = value;
322                 UpdateImage();
323             }
324         }
325
326         /// <summary>
327         /// Get or set whether to draws the borders only(If true).<br>
328         /// If not specified, the default is false.<br>
329         /// For N-Patch images only.<br>
330         /// Optional.
331         /// </summary>
332         public bool BorderOnly
333         {
334             get
335             {
336                 return _borderOnly ?? false;
337             }
338             set
339             {
340                 _borderOnly = value;
341                 UpdateImage();
342             }
343         }
344
345         public bool SynchronosLoading
346         {
347             get
348             {
349                 return _synchronousLoading ?? false;
350             }
351             set
352             {
353                 _synchronousLoading = value;
354                 UpdateImage();
355             }
356         }
357
358         private void UpdateImage()
359         {
360             if (_url != null)
361             {
362                 if (_border != null)
363                 { // for nine-patch image
364                     _nPatchMap = new PropertyMap();
365                     _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
366                     _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
367                     _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
368                     if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
369                     if (_synchronousLoading != null) _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
370                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
371                 }
372                 else if(_synchronousLoading != null)
373                 { // for normal image, with synchronous loading property
374                     PropertyMap imageMap = new PropertyMap();
375                     imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
376                     imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
377                     imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
378                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
379                 }
380                 else
381                 { // just for normal image
382                     SetProperty(ImageView.Property.RESOURCE_URL, new PropertyValue(_url));
383                 }
384             }
385         }
386
387         private Rectangle _border = null;
388         private PropertyMap _nPatchMap = null;
389         private bool? _synchronousLoading = null;
390         private bool? _borderOnly = null;
391         private string _url = null;
392
393     }
394
395 }