Merge "[ElmSharp] Fixed the crash issue of Calendar"
[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         /// Sets this ImageView from the given URL.<br />
197         /// If the URL is empty, ImageView will not display anything.<br />
198         /// </summary>
199         /// <param name="url">The URL to the image resource to display.</param>
200         /// <since_tizen> 3 </since_tizen>
201         public void SetImage(string url)
202         {
203             _url = url;
204             NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
205             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
206         }
207         internal void SetImage(string url, Uint16Pair size)
208         {
209             _url = url;
210             NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
211             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212         }
213
214         internal ViewResourceReadySignal ResourceReadySignal(View view)
215         {
216             ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
217             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
218             return ret;
219         }
220
221         /// <summary>
222         /// Queries if all resources required by a control are loaded and ready.<br />
223         /// Most resources are only loaded when the control is placed on the stage.<br />
224         /// True if the resources are loaded and ready, false otherwise.<br />
225         /// </summary>
226         /// <since_tizen> 3 </since_tizen>
227         public new bool IsResourceReady()
228         {
229             bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
230             if (NDalicPINVOKE.SWIGPendingException.Pending)
231                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
232             return ret;
233         }
234
235         /// <summary>
236         /// ImageView ResourceUrl, type string.
237         /// </summary>
238         /// <since_tizen> 3 </since_tizen>
239         public string ResourceUrl
240         {
241             get
242             {
243                 GetProperty(ImageView.Property.IMAGE).Get(out _url);
244                 return _url;
245             }
246             set
247             {
248                 _url = (value == null? "" : value);
249                 UpdateImage();
250             }
251         }
252
253         /// <summary>
254         /// This will be deprecated, please use Image instead. <br />
255         /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
256         /// </summary>
257         /// <since_tizen> 3 </since_tizen>
258         [Obsolete("Please do not use! This will be deprecated! Please use Image property instead!")]
259         [EditorBrowsable(EditorBrowsableState.Never)]
260         public PropertyMap ImageMap
261         {
262             get
263             {
264                 if (_border == null)
265                 {
266                     PropertyMap temp = new PropertyMap();
267                     GetProperty(ImageView.Property.IMAGE).Get(temp);
268                     return temp;
269                 }
270                 else
271                 {
272                     return null;
273                 }
274             }
275             set
276             {
277                 if (_border == null)
278                 {
279                     if (_url != null) { value.Add("url", new PropertyValue(_url)); }
280                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
281                 }
282             }
283         }
284
285         /// <summary>
286         /// ImageView Image, type PropertyMap
287         /// </summary>
288         /// <since_tizen> 4 </since_tizen>
289         public PropertyMap Image
290         {
291             get
292             {
293                 if (_border == null)
294                 {
295                     PropertyMap temp = new PropertyMap();
296                     GetProperty(ImageView.Property.IMAGE).Get(temp);
297                     return temp;
298                 }
299                 else
300                 {
301                     return null;
302                 }
303             }
304             set
305             {
306                 if (_border == null)
307                 {
308                     if(_url != null) { value.Add("url", new PropertyValue(_url)); }
309                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
310                 }
311             }
312         }
313
314         /// <summary>
315         /// ImageView PreMultipliedAlpha, type Boolean.<br />
316         /// Image must be initialized.<br />
317         /// </summary>
318         /// <since_tizen> 3 </since_tizen>
319         public bool PreMultipliedAlpha
320         {
321             get
322             {
323                 bool temp = false;
324                 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
325                 return temp;
326             }
327             set
328             {
329                 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
330             }
331         }
332
333         /// <summary>
334         /// ImageView PixelArea, type Vector4 (Animatable property).<br />
335         /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
336         /// </summary>
337         /// <since_tizen> 3 </since_tizen>
338         public RelativeVector4 PixelArea
339         {
340             get
341             {
342                 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
343                 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
344                 return temp;
345             }
346             set
347             {
348                 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
349             }
350         }
351
352         /// <summary>
353         /// The border of the image in the order: left, right, bottom, top.<br />
354         /// If set, ImageMap will be ignored.<br />
355         /// For N-Patch images only.<br />
356         /// Optional.
357         /// </summary>
358         /// <since_tizen> 3 </since_tizen>
359         public Rectangle Border
360         {
361             get
362             {
363                 return _border;
364             }
365             set
366             {
367                 _border = value;
368                 UpdateImage();
369             }
370         }
371
372         /// <summary>
373         /// Gets or sets whether to draw the borders only (if true).<br />
374         /// If not specified, the default is false.<br />
375         /// For N-Patch images only.<br />
376         /// Optional.
377         /// </summary>
378         /// <since_tizen> 3 </since_tizen>
379         public bool BorderOnly
380         {
381             get
382             {
383                 return _borderOnly ?? false;
384             }
385             set
386             {
387                 _borderOnly = value;
388                 UpdateImage();
389             }
390         }
391
392         /// <summary>
393         /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
394         /// </summary>
395         /// <since_tizen> 3 </since_tizen>
396         public bool SynchronosLoading
397         {
398             get
399             {
400                 return _synchronousLoading ?? false;
401             }
402             set
403             {
404                 _synchronousLoading = value;
405                 UpdateImage();
406             }
407         }
408
409         /// <summary>
410         /// Gets or sets whether to correct orientation of image automatically.<br />
411         /// </summary>
412         /// <since_tizen> 5 </since_tizen>
413         public bool OrientationCorrection
414         {
415             get
416             {
417                 return _orientationCorrection ?? false;
418             }
419             set
420             {
421                 _orientationCorrection = value;
422                 UpdateImage();
423             }
424         }
425
426
427         /// <summary>
428         /// Get the loading state of the visual resource.
429         /// </summary>
430         /// <since_tizen> 5 </since_tizen>
431         public ImageView.LoadingStatusType LoadingStatus
432         {
433             get
434             {
435                 return (ImageView.LoadingStatusType)NDalicManualPINVOKE.View_GetVisualResourceStatus(swigCPtr, (int)Property.IMAGE);
436             }
437         }
438
439         /// <summary>
440         /// Enumeration for LoadingStatus of image.
441         /// </summary>
442         /// <since_tizen> 5 </since_tizen>
443         public enum LoadingStatusType
444         {
445             /// <summary>
446             /// Loading preparing status.
447             /// </summary>
448             /// <since_tizen> 5 </since_tizen>
449             Preparing,
450             /// <summary>
451             /// Loading ready status.
452             /// </summary>
453             /// <since_tizen> 5 </since_tizen>
454             Ready,
455             /// <summary>
456             /// Loading failed status.
457             /// </summary>
458             /// <since_tizen> 5 </since_tizen>
459             Failed
460         }
461
462
463         private void UpdateImage()
464         {
465             if (_url != null)
466             {
467                 if (_border != null)
468                 { // for nine-patch image
469                     _nPatchMap = new PropertyMap();
470                     _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
471                     _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
472                     _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
473                     if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
474                     if (_synchronousLoading != null) { _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
475                     if (_orientationCorrection != null) { _nPatchMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
476                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
477                 }
478                 else if (_synchronousLoading != null || _orientationCorrection != null)
479                 { // for normal image, with synchronous loading property
480                     PropertyMap imageMap = new PropertyMap();
481                     imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
482                     imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
483                     if (_synchronousLoading != null) { imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
484                     if (_orientationCorrection != null) { imageMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
485                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
486                 }
487                 else
488                 { // just for normal image
489                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
490                 }
491             }
492         }
493
494         private Rectangle _border = null;
495         private PropertyMap _nPatchMap = null;
496         private bool? _synchronousLoading = null;
497         private bool? _borderOnly = null;
498         private string _url = null;
499         private bool? _orientationCorrection = null;
500     }
501
502 }