Merge "[MediaContent] Fixed possible deadlock issues of async methods."
[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             /// <since_tizen> 3 </since_tizen>
52             public View View
53             {
54                 get
55                 {
56                     return _view;
57                 }
58                 set
59                 {
60                     _view = value;
61                 }
62             }
63         }
64
65         private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
66         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
67         private delegate void ResourceReadyEventCallbackType(IntPtr data);
68         private ResourceReadyEventCallbackType _resourceReadyEventCallback;
69
70         /// <summary>
71         /// An event for ResourceReady signal which can be used to subscribe or unsubscribe the event handler.<br>
72         /// This signal is emitted after all resources required by a control are loaded and ready.<br>
73         /// Most resources are only loaded when the control is placed on the stage.<br>
74         /// </summary>
75         /// <since_tizen> 3 </since_tizen>
76         public event EventHandler<ResourceReadyEventArgs> ResourceReady
77         {
78             add
79             {
80                 if (_resourceReadyEventHandler == null)
81                 {
82                     _resourceReadyEventCallback = OnResourceReady;
83                     ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
84                 }
85
86                 _resourceReadyEventHandler += value;
87             }
88
89             remove
90             {
91                 _resourceReadyEventHandler -= value;
92
93                 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
94                 {
95                     ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
96                 }
97             }
98         }
99
100         // Callback for View ResourceReady signal
101         private void OnResourceReady(IntPtr data)
102         {
103             ResourceReadyEventArgs e = new ResourceReadyEventArgs();
104             if(data != null)
105             {
106                 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
107             }
108
109             if (_resourceReadyEventHandler != null)
110             {
111                 _resourceReadyEventHandler(this, e);
112             }
113         }
114
115         //you can override it to clean-up your own resources.
116         protected override void Dispose(DisposeTypes type)
117         {
118             if (disposed)
119             {
120                 return;
121             }
122
123             if(type == DisposeTypes.Explicit)
124             {
125                 //Called by User
126                 //Release your own managed resources here.
127                 //You should release all of your own disposable objects here.
128                 _border?.Dispose();
129                 _border = null;
130                 _nPatchMap?.Dispose();
131                 _nPatchMap = null;
132             }
133
134             //Release your own unmanaged resources here.
135             //You should not access any managed member here except static instance.
136             //because the execution order of Finalizes is non-deterministic.
137
138             if (swigCPtr.Handle != global::System.IntPtr.Zero)
139             {
140                 if (swigCMemOwn)
141                 {
142                     swigCMemOwn = false;
143                     NDalicPINVOKE.delete_ImageView(swigCPtr);
144                 }
145                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
146             }
147
148             base.Dispose(type);
149         }
150
151         internal new class Property
152         {
153             internal static readonly int RESOURCE_URL = NDalicPINVOKE.ImageView_Property_RESOURCE_URL_get();
154             internal static readonly int IMAGE = NDalicPINVOKE.ImageView_Property_IMAGE_get();
155             internal static readonly int PRE_MULTIPLIED_ALPHA = NDalicPINVOKE.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
156             internal static readonly int PIXEL_AREA = NDalicPINVOKE.ImageView_Property_PIXEL_AREA_get();
157         }
158
159         /// <summary>
160         /// Creates an initialized ImageView.
161         /// </summary>
162         /// <since_tizen> 3 </since_tizen>
163         public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
164         {
165             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
166
167         }
168         /// <summary>
169         /// Creates an initialized ImageView from a URL to an image resource.<br>
170         /// If the string is empty, ImageView will not display anything.<br>
171         /// </summary>
172         /// <param name="url">The URL of the image resource to display.</param>
173         /// <since_tizen> 3 </since_tizen>
174         public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
175         {
176             _url = url;
177             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
178
179         }
180         internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
181         {
182             _url = url;
183             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
184
185         }
186
187         [Obsolete("Please do not use! this will be deprecated")]
188         public new static ImageView DownCast(BaseHandle handle)
189         {
190             ImageView ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
191             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192             return ret;
193         }
194         /// <summary>
195         /// Sets this ImageView from the given URL.<br>
196         /// If the URL is empty, ImageView will not display anything.<br>
197         /// </summary>
198         /// <param name="url">The URL to the image resource to display.</param>
199         /// <since_tizen> 3 </since_tizen>
200         public void SetImage(string url)
201         {
202             _url = url;
203             NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
204             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
205         }
206         internal void SetImage(string url, Uint16Pair size)
207         {
208             _url = url;
209             NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
210             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
211         }
212
213         internal ViewResourceReadySignal ResourceReadySignal(View view) {
214             ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
215             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
216             return ret;
217         }
218
219         /// <summary>
220         /// Queries if all resources required by a control are loaded and ready.<br>
221         /// Most resources are only loaded when the control is placed on the stage.<br>
222         /// True if the resources are loaded and ready, false otherwise.<br>
223         /// </summary>
224         /// <since_tizen> 3 </since_tizen>
225         public new  bool IsResourceReady()
226         {
227             bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
228             if (NDalicPINVOKE.SWIGPendingException.Pending)
229                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
230             return ret;
231         }
232
233         /// <summary>
234         /// ImageView ResourceUrl, type string.
235         /// </summary>
236         /// <since_tizen> 3 </since_tizen>
237         public string ResourceUrl
238         {
239             get
240             {
241                 GetProperty(ImageView.Property.RESOURCE_URL).Get(out _url);
242                 return _url;
243             }
244             set
245             {
246                 _url = value;
247                 UpdateImage();
248             }
249         }
250
251         /// <summary>
252         /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise
253         /// </summary>
254         /// <since_tizen> 3 </since_tizen>
255         public PropertyMap ImageMap
256         {
257             get
258             {
259                 if (_border == null)
260                 {
261                     PropertyMap temp = new PropertyMap();
262                     GetProperty(ImageView.Property.IMAGE).Get(temp);
263                     return temp;
264                 }
265                 else
266                 {
267                     return null;
268                 }
269             }
270             set
271             {
272                 if (_border == null)
273                 {
274                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
275                 }
276             }
277         }
278
279         /// <summary>
280         /// ImageView PreMultipliedAlpha, type Boolean.<br>
281         /// Image must be initialized.<br>
282         /// </summary>
283         /// <since_tizen> 3 </since_tizen>
284         public bool PreMultipliedAlpha
285         {
286             get
287             {
288                 bool temp = false;
289                 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
290                 return temp;
291             }
292             set
293             {
294                 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
295             }
296         }
297
298         /// <summary>
299         /// ImageView PixelArea, type Vector4 (Animatable property).<br>
300         /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br>
301         /// </summary>
302         /// <since_tizen> 3 </since_tizen>
303         public RelativeVector4 PixelArea
304         {
305             get
306             {
307                 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
308                 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
309                 return temp;
310             }
311             set
312             {
313                 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
314             }
315         }
316
317         /// <summary>
318         /// The border of the image in the order: left, right, bottom, top.<br>
319         /// If set, ImageMap will be ignored.<br>
320         /// For N-Patch images only.<br>
321         /// Optional.
322         /// </summary>
323         /// <since_tizen> 3 </since_tizen>
324         public Rectangle Border
325         {
326             get
327             {
328                 return _border;
329             }
330             set
331             {
332                 _border = value;
333                 UpdateImage();
334             }
335         }
336
337         /// <summary>
338         /// Gets or sets whether to draw the borders only (if true).<br>
339         /// If not specified, the default is false.<br>
340         /// For N-Patch images only.<br>
341         /// Optional.
342         /// </summary>
343         /// <since_tizen> 3 </since_tizen>
344         public bool BorderOnly
345         {
346             get
347             {
348                 return _borderOnly ?? false;
349             }
350             set
351             {
352                 _borderOnly = value;
353                 UpdateImage();
354             }
355         }
356
357         public bool SynchronosLoading
358         {
359             get
360             {
361                 return _synchronousLoading ?? false;
362             }
363             set
364             {
365                 _synchronousLoading = value;
366                 UpdateImage();
367             }
368         }
369
370         private void UpdateImage()
371         {
372             if (_url != null)
373             {
374                 if (_border != null)
375                 { // for nine-patch image
376                     _nPatchMap = new PropertyMap();
377                     _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
378                     _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
379                     _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
380                     if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
381                     if (_synchronousLoading != null) _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
382                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
383                 }
384                 else if(_synchronousLoading != null)
385                 { // for normal image, with synchronous loading property
386                     PropertyMap imageMap = new PropertyMap();
387                     imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
388                     imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
389                     imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
390                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
391                 }
392                 else
393                 { // just for normal image
394                     SetProperty(ImageView.Property.RESOURCE_URL, new PropertyValue(_url));
395                 }
396             }
397         }
398
399         private Rectangle _border = null;
400         private PropertyMap _nPatchMap = null;
401         private bool? _synchronousLoading = null;
402         private bool? _borderOnly = null;
403         private string _url = null;
404
405     }
406
407 }