Merge "[NUI] fix Image property URL update issue"
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ImageView.cs
1 /*
2  * Copyright(c) 2017 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 using System;
18 using System.Runtime.InteropServices;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.BaseComponents
22 {
23
24     /// <summary>
25     /// ImageView is a class for displaying an image resource.<br />
26     /// An instance of ImageView can be created using a URL or an image instance.<br />
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public class ImageView : View
30     {
31         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
32
33         internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
34         {
35             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
36         }
37
38         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
39         {
40             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
41         }
42
43
44         /// <summary>
45         /// Event arguments of resource ready.
46         /// </summary>
47         /// <since_tizen> 3 </since_tizen>
48         public class ResourceReadyEventArgs : EventArgs
49         {
50             private View _view;
51
52             /// <summary>
53             /// The view whose resource is ready.
54             /// </summary>
55             /// <since_tizen> 3 </since_tizen>
56             public View View
57             {
58                 get
59                 {
60                     return _view;
61                 }
62                 set
63                 {
64                     _view = value;
65                 }
66             }
67         }
68
69         private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
70         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
71         private delegate void ResourceReadyEventCallbackType(IntPtr data);
72         private ResourceReadyEventCallbackType _resourceReadyEventCallback;
73
74         /// <summary>
75         /// An event for ResourceReady signal which can be used to subscribe or unsubscribe the event handler.<br />
76         /// This signal is emitted after all resources required by a control are loaded and ready.<br />
77         /// Most resources are only loaded when the control is placed on the stage.<br />
78         /// </summary>
79         /// <since_tizen> 3 </since_tizen>
80         public event EventHandler<ResourceReadyEventArgs> ResourceReady
81         {
82             add
83             {
84                 if (_resourceReadyEventHandler == null)
85                 {
86                     _resourceReadyEventCallback = OnResourceReady;
87                     ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
88                 }
89
90                 _resourceReadyEventHandler += value;
91             }
92
93             remove
94             {
95                 _resourceReadyEventHandler -= value;
96
97                 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
98                 {
99                     ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
100                 }
101             }
102         }
103
104         // Callback for View ResourceReady signal
105         private void OnResourceReady(IntPtr data)
106         {
107             ResourceReadyEventArgs e = new ResourceReadyEventArgs();
108             if (data != null)
109             {
110                 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
111             }
112
113             if (_resourceReadyEventHandler != null)
114             {
115                 _resourceReadyEventHandler(this, e);
116             }
117         }
118
119         /// <summary>
120         /// you can override it to clean-up your own resources.
121         /// </summary>
122         /// <param name="type">DisposeTypes</param>
123         /// <since_tizen> 3 </since_tizen>
124         protected override void Dispose(DisposeTypes type)
125         {
126             if (disposed)
127             {
128                 return;
129             }
130
131             if (type == DisposeTypes.Explicit)
132             {
133                 //Called by User
134                 //Release your own managed resources here.
135                 //You should release all of your own disposable objects here.
136                 _border?.Dispose();
137                 _border = null;
138                 _nPatchMap?.Dispose();
139                 _nPatchMap = null;
140             }
141
142             //Release your own unmanaged resources here.
143             //You should not access any managed member here except static instance.
144             //because the execution order of Finalizes is non-deterministic.
145
146             if (swigCPtr.Handle != global::System.IntPtr.Zero)
147             {
148                 if (swigCMemOwn)
149                 {
150                     swigCMemOwn = false;
151                     NDalicPINVOKE.delete_ImageView(swigCPtr);
152                 }
153                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
154             }
155
156             base.Dispose(type);
157         }
158
159         internal new class Property
160         {
161             internal static readonly int RESOURCE_URL = NDalicPINVOKE.ImageView_Property_RESOURCE_URL_get();
162             internal static readonly int IMAGE = NDalicPINVOKE.ImageView_Property_IMAGE_get();
163             internal static readonly int PRE_MULTIPLIED_ALPHA = NDalicPINVOKE.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
164             internal static readonly int PIXEL_AREA = NDalicPINVOKE.ImageView_Property_PIXEL_AREA_get();
165         }
166
167         /// <summary>
168         /// Creates an initialized ImageView.
169         /// </summary>
170         /// <since_tizen> 3 </since_tizen>
171         public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
172         {
173             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
174
175         }
176         /// <summary>
177         /// Creates an initialized ImageView from a URL to an image resource.<br />
178         /// If the string is empty, ImageView will not display anything.<br />
179         /// </summary>
180         /// <param name="url">The URL of the image resource to display.</param>
181         /// <since_tizen> 3 </since_tizen>
182         public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
183         {
184             _url = url;
185             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
186
187         }
188         internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
189         {
190             _url = url;
191             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192
193         }
194
195         /// <summary>
196         /// Downcasts a handle to imageView handle.
197         /// </summary>
198         /// Please do not use! this will be deprecated!
199         /// Instead please use as keyword.
200         /// <since_tizen> 3 </since_tizen>
201         [Obsolete("Please DO NOT use! This will be deprecated, instead please USE as keyword.")]
202         [EditorBrowsable(EditorBrowsableState.Never)]
203         public new static ImageView DownCast(BaseHandle handle)
204         {
205             ImageView ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
206             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
207             return ret;
208         }
209         /// <summary>
210         /// Sets this ImageView from the given URL.<br />
211         /// If the URL is empty, ImageView will not display anything.<br />
212         /// </summary>
213         /// <param name="url">The URL to the image resource to display.</param>
214         /// <since_tizen> 3 </since_tizen>
215         public void SetImage(string url)
216         {
217             _url = url;
218             NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
219             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
220         }
221         internal void SetImage(string url, Uint16Pair size)
222         {
223             _url = url;
224             NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
225             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
226         }
227
228         internal ViewResourceReadySignal ResourceReadySignal(View view)
229         {
230             ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
231             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
232             return ret;
233         }
234
235         /// <summary>
236         /// Queries if all resources required by a control are loaded and ready.<br />
237         /// Most resources are only loaded when the control is placed on the stage.<br />
238         /// True if the resources are loaded and ready, false otherwise.<br />
239         /// </summary>
240         /// <since_tizen> 3 </since_tizen>
241         public new bool IsResourceReady()
242         {
243             bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
244             if (NDalicPINVOKE.SWIGPendingException.Pending)
245                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
246             return ret;
247         }
248
249         /// <summary>
250         /// ImageView ResourceUrl, type string.
251         /// </summary>
252         /// <since_tizen> 3 </since_tizen>
253         public string ResourceUrl
254         {
255             get
256             {
257                 GetProperty(ImageView.Property.IMAGE).Get(out _url);
258                 return _url;
259             }
260             set
261             {
262                 _url = (value == null? "" : value);
263                 UpdateImage();
264             }
265         }
266
267         /// <summary>
268         /// This will be deprecated, please use Image instead. <br />
269         /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
270         /// </summary>
271         /// <since_tizen> 3 </since_tizen>
272         [EditorBrowsable(EditorBrowsableState.Never)]
273         public PropertyMap ImageMap
274         {
275             get
276             {
277                 if (_border == null)
278                 {
279                     PropertyMap temp = new PropertyMap();
280                     GetProperty(ImageView.Property.IMAGE).Get(temp);
281                     return temp;
282                 }
283                 else
284                 {
285                     return null;
286                 }
287             }
288             set
289             {
290                 if (_border == null)
291                 {
292                     if (_url != null) { value.Add("url", new PropertyValue(_url)); }
293                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
294                 }
295             }
296         }
297
298         /// <summary>
299         /// ImageView Image, type PropertyMap
300         /// </summary>
301         /// <since_tizen> 4 </since_tizen>
302         public PropertyMap Image
303         {
304             get
305             {
306                 if (_border == null)
307                 {
308                     PropertyMap temp = new PropertyMap();
309                     GetProperty(ImageView.Property.IMAGE).Get(temp);
310                     return temp;
311                 }
312                 else
313                 {
314                     return null;
315                 }
316             }
317             set
318             {
319                 if (_border == null)
320                 {
321                     if(_url != null) { value.Add("url", new PropertyValue(_url)); }
322                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
323                 }
324             }
325         }
326
327         /// <summary>
328         /// ImageView PreMultipliedAlpha, type Boolean.<br />
329         /// Image must be initialized.<br />
330         /// </summary>
331         /// <since_tizen> 3 </since_tizen>
332         public bool PreMultipliedAlpha
333         {
334             get
335             {
336                 bool temp = false;
337                 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
338                 return temp;
339             }
340             set
341             {
342                 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
343             }
344         }
345
346         /// <summary>
347         /// ImageView PixelArea, type Vector4 (Animatable property).<br />
348         /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
349         /// </summary>
350         /// <since_tizen> 3 </since_tizen>
351         public RelativeVector4 PixelArea
352         {
353             get
354             {
355                 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
356                 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
357                 return temp;
358             }
359             set
360             {
361                 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
362             }
363         }
364
365         /// <summary>
366         /// The border of the image in the order: left, right, bottom, top.<br />
367         /// If set, ImageMap will be ignored.<br />
368         /// For N-Patch images only.<br />
369         /// Optional.
370         /// </summary>
371         /// <since_tizen> 3 </since_tizen>
372         public Rectangle Border
373         {
374             get
375             {
376                 return _border;
377             }
378             set
379             {
380                 _border = value;
381                 UpdateImage();
382             }
383         }
384
385         /// <summary>
386         /// Gets or sets whether to draw the borders only (if true).<br />
387         /// If not specified, the default is false.<br />
388         /// For N-Patch images only.<br />
389         /// Optional.
390         /// </summary>
391         /// <since_tizen> 3 </since_tizen>
392         public bool BorderOnly
393         {
394             get
395             {
396                 return _borderOnly ?? false;
397             }
398             set
399             {
400                 _borderOnly = value;
401                 UpdateImage();
402             }
403         }
404
405         /// <summary>
406         /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
407         /// </summary>
408         /// <since_tizen> 3 </since_tizen>
409         public bool SynchronosLoading
410         {
411             get
412             {
413                 return _synchronousLoading ?? false;
414             }
415             set
416             {
417                 _synchronousLoading = value;
418                 UpdateImage();
419             }
420         }
421
422         /// <summary>
423         /// Gets or sets whether to correct orientation of image automatically.<br />
424         /// </summary>
425         /// <since_tizen> 5 </since_tizen>
426         public bool OrientationCorrection
427         {
428             get
429             {
430                 return _orientationCorrection ?? false;
431             }
432             set
433             {
434                 _orientationCorrection = value;
435                 UpdateImage();
436             }
437         }
438
439
440         /// <summary>
441         /// Get the loading state of the visual resource.
442         /// </summary>
443         /// <since_tizen> 5 </since_tizen>
444         public ImageView.LoadingStatusType LoadingStatus
445         {
446             get
447             {
448                 return (ImageView.LoadingStatusType)NDalicManualPINVOKE.View_GetVisualResourceStatus(swigCPtr, (int)Property.IMAGE);
449             }
450         }
451
452         /// <summary>
453         /// Enumeration for LoadingStatus of image.
454         /// </summary>
455         /// <since_tizen> 5 </since_tizen>
456         public enum LoadingStatusType
457         {
458             /// <summary>
459             /// Loading preparing status.
460             /// </summary>
461             /// <since_tizen> 5 </since_tizen>
462             Preparing,
463             /// <summary>
464             /// Loading ready status.
465             /// </summary>
466             /// <since_tizen> 5 </since_tizen>
467             Ready,
468             /// <summary>
469             /// Loading failed status.
470             /// </summary>
471             /// <since_tizen> 5 </since_tizen>
472             Failed
473         }
474
475
476         private void UpdateImage()
477         {
478             if (_url != null)
479             {
480                 if (_border != null)
481                 { // for nine-patch image
482                     _nPatchMap = new PropertyMap();
483                     _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
484                     _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
485                     _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
486                     if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
487                     if (_synchronousLoading != null) { _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
488                     if (_orientationCorrection != null) { _nPatchMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
489                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
490                 }
491                 else if (_synchronousLoading != null || _orientationCorrection != null)
492                 { // for normal image, with synchronous loading property
493                     PropertyMap imageMap = new PropertyMap();
494                     imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
495                     imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
496                     if (_synchronousLoading != null) { imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
497                     if (_orientationCorrection != null) { imageMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
498                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
499                 }
500                 else
501                 { // just for normal image
502                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
503                 }
504             }
505         }
506
507         private Rectangle _border = null;
508         private PropertyMap _nPatchMap = null;
509         private bool? _synchronousLoading = null;
510         private bool? _borderOnly = null;
511         private string _url = null;
512         private bool? _orientationCorrection = null;
513     }
514
515 }