d17e87e2370cf84c54272f99291d63c82d4b3692
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ImageView.cs
1 /*
2  * Copyright(c) 2018 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             internal static readonly int ACTION_RELOAD = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_RELOAD_get();
166             internal static readonly int ACTION_PLAY = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_PLAY_get();
167             internal static readonly int ACTION_PAUSE = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_PAUSE_get();
168             internal static readonly int ACTION_STOP = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_STOP_get();
169         }
170
171         /// <summary>
172         /// Creates an initialized ImageView.
173         /// </summary>
174         /// <since_tizen> 3 </since_tizen>
175         public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
176         {
177             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
178
179         }
180         /// <summary>
181         /// Creates an initialized ImageView from a URL to an image resource.<br />
182         /// If the string is empty, ImageView will not display anything.<br />
183         /// </summary>
184         /// <param name="url">The URL of the image resource to display.</param>
185         /// <since_tizen> 3 </since_tizen>
186         public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
187         {
188             _url = url;
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190
191         }
192         internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
193         {
194             _url = url;
195             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
196
197         }
198
199         /// <summary>
200         /// Downcasts a handle to imageView handle.
201         /// </summary>
202         /// Please do not use! this will be deprecated!
203         /// Instead please use as keyword.
204         /// <since_tizen> 3 </since_tizen>
205         [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead! " +
206             "Like: " +
207             "BaseHandle handle = new ImageView(imagePath); " +
208             "ImageView image = handle as ImageView")]
209         [EditorBrowsable(EditorBrowsableState.Never)]
210         public new static ImageView DownCast(BaseHandle handle)
211         {
212             ImageView ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
213             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214             return ret;
215         }
216
217         /// <summary>
218         /// Sets this ImageView from the given URL.<br />
219         /// If the URL is empty, ImageView will not display anything.<br />
220         /// </summary>
221         /// <param name="url">The URL to the image resource to display.</param>
222         /// <since_tizen> 3 </since_tizen>
223         public void SetImage(string url)
224         {
225             _url = url;
226             NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
227             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
228         }
229         internal void SetImage(string url, Uint16Pair size)
230         {
231             _url = url;
232             NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
233             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
234         }
235
236         internal ViewResourceReadySignal ResourceReadySignal(View view)
237         {
238             ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
239             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
240             return ret;
241         }
242
243         /// <summary>
244         /// Queries if all resources required by a control are loaded and ready.<br />
245         /// Most resources are only loaded when the control is placed on the stage.<br />
246         /// True if the resources are loaded and ready, false otherwise.<br />
247         /// </summary>
248         /// <since_tizen> 3 </since_tizen>
249         public new bool IsResourceReady()
250         {
251             bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
252             if (NDalicPINVOKE.SWIGPendingException.Pending)
253                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
254             return ret;
255         }
256
257         /// <summary>
258         /// Force reloading of the image, all visuals using this image will get the latest one.
259         /// </summary>
260         /// <since_tizen> 5 </since_tizen>        
261         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
262         [EditorBrowsable(EditorBrowsableState.Never)]
263         public void Reload()
264         {
265             this.DoAction(ImageView.Property.IMAGE, Property.ACTION_RELOAD, new PropertyValue(0));
266         }
267
268         /// <summary>
269         /// Play the animated GIF. This is also Default playback mode.
270         /// </summary>
271         /// <since_tizen> 5 </since_tizen>
272         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
273         [EditorBrowsable(EditorBrowsableState.Never)]
274         public void Play()
275         {
276             this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PLAY, new PropertyValue(0));
277         }
278
279         /// <summary>
280         /// Pause the animated GIF.
281         /// </summary>
282         /// <since_tizen> 5 </since_tizen>
283         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
284         [EditorBrowsable(EditorBrowsableState.Never)]
285         public void Pause()
286         {
287             this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PAUSE, new PropertyValue(0));
288         }
289
290         /// <summary>
291         /// Stop the animated GIF.
292         /// </summary>
293         /// <since_tizen> 5 </since_tizen>
294         /// This will be released at Tizen.NET API Level 5, so currently this would be used as inhouse API.
295         [EditorBrowsable(EditorBrowsableState.Never)]
296         public void Stop()
297         {
298             this.DoAction(ImageView.Property.IMAGE, Property.ACTION_STOP, new PropertyValue(0));
299         }
300
301         /// <summary>
302         /// ImageView ResourceUrl, type string.
303         /// </summary>
304         /// <since_tizen> 3 </since_tizen>
305         public string ResourceUrl
306         {
307             get
308             {
309                 GetProperty(ImageView.Property.IMAGE).Get(out _url);
310                 return _url;
311             }
312             set
313             {
314                 _url = (value == null? "" : value);
315                 UpdateImage();
316             }
317         }
318
319         /// <summary>
320         /// This will be deprecated, please use Image instead. <br />
321         /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
322         /// </summary>
323         /// <since_tizen> 3 </since_tizen>
324         [Obsolete("Please do not use! This will be deprecated! Please use Image property instead!")]
325         [EditorBrowsable(EditorBrowsableState.Never)]
326         public PropertyMap ImageMap
327         {
328             get
329             {
330                 if (_border == null)
331                 {
332                     PropertyMap temp = new PropertyMap();
333                     GetProperty(ImageView.Property.IMAGE).Get(temp);
334                     return temp;
335                 }
336                 else
337                 {
338                     return null;
339                 }
340             }
341             set
342             {
343                 if (_border == null)
344                 {
345                     if (_url != null) { value.Add("url", new PropertyValue(_url)); }
346                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
347                 }
348             }
349         }
350
351         /// <summary>
352         /// ImageView Image, type PropertyMap
353         /// </summary>
354         /// <since_tizen> 4 </since_tizen>
355         public PropertyMap Image
356         {
357             get
358             {
359                 if (_border == null)
360                 {
361                     PropertyMap temp = new PropertyMap();
362                     GetProperty(ImageView.Property.IMAGE).Get(temp);
363                     return temp;
364                 }
365                 else
366                 {
367                     return null;
368                 }
369             }
370             set
371             {
372                 if (_border == null)
373                 {
374                     if(_url != null) { value.Add("url", new PropertyValue(_url)); }
375                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
376                 }
377             }
378         }
379
380         /// <summary>
381         /// ImageView PreMultipliedAlpha, type Boolean.<br />
382         /// Image must be initialized.<br />
383         /// </summary>
384         /// <since_tizen> 3 </since_tizen>
385         public bool PreMultipliedAlpha
386         {
387             get
388             {
389                 bool temp = false;
390                 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
391                 return temp;
392             }
393             set
394             {
395                 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
396             }
397         }
398
399         /// <summary>
400         /// ImageView PixelArea, type Vector4 (Animatable property).<br />
401         /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
402         /// </summary>
403         /// <since_tizen> 3 </since_tizen>
404         public RelativeVector4 PixelArea
405         {
406             get
407             {
408                 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
409                 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
410                 return temp;
411             }
412             set
413             {
414                 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
415             }
416         }
417
418         /// <summary>
419         /// The border of the image in the order: left, right, bottom, top.<br />
420         /// If set, ImageMap will be ignored.<br />
421         /// For N-Patch images only.<br />
422         /// Optional.
423         /// </summary>
424         /// <since_tizen> 3 </since_tizen>
425         public Rectangle Border
426         {
427             get
428             {
429                 return _border;
430             }
431             set
432             {
433                 _border = value;
434                 UpdateImage();
435             }
436         }
437
438         /// <summary>
439         /// Gets or sets whether to draw the borders only (if true).<br />
440         /// If not specified, the default is false.<br />
441         /// For N-Patch images only.<br />
442         /// Optional.
443         /// </summary>
444         /// <since_tizen> 3 </since_tizen>
445         public bool BorderOnly
446         {
447             get
448             {
449                 return _borderOnly ?? false;
450             }
451             set
452             {
453                 _borderOnly = value;
454                 UpdateImage();
455             }
456         }
457
458         /// <summary>
459         /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
460         /// </summary>
461         /// <since_tizen> 3 </since_tizen>
462         public bool SynchronosLoading
463         {
464             get
465             {
466                 return _synchronousLoading ?? false;
467             }
468             set
469             {
470                 _synchronousLoading = value;
471                 UpdateImage();
472             }
473         }
474
475         /// <summary>
476         /// Gets or sets whether to correct orientation of image automatically.<br />
477         /// </summary>
478         /// <since_tizen> 5 </since_tizen>
479         public bool OrientationCorrection
480         {
481             get
482             {
483                 return _orientationCorrection ?? false;
484             }
485             set
486             {
487                 _orientationCorrection = value;
488                 UpdateImage();
489             }
490         }
491
492
493         /// <summary>
494         /// Get the loading state of the visual resource.
495         /// </summary>
496         /// <since_tizen> 5 </since_tizen>
497         public ImageView.LoadingStatusType LoadingStatus
498         {
499             get
500             {
501                 return (ImageView.LoadingStatusType)NDalicManualPINVOKE.View_GetVisualResourceStatus(swigCPtr, (int)Property.IMAGE);
502             }
503         }
504
505         /// <summary>
506         /// Enumeration for LoadingStatus of image.
507         /// </summary>
508         /// <since_tizen> 5 </since_tizen>
509         public enum LoadingStatusType
510         {
511             /// <summary>
512             /// Loading preparing status.
513             /// </summary>
514             /// <since_tizen> 5 </since_tizen>
515             Preparing,
516             /// <summary>
517             /// Loading ready status.
518             /// </summary>
519             /// <since_tizen> 5 </since_tizen>
520             Ready,
521             /// <summary>
522             /// Loading failed status.
523             /// </summary>
524             /// <since_tizen> 5 </since_tizen>
525             Failed
526         }
527
528
529         private void UpdateImage()
530         {
531             if (_url != null)
532             {
533                 if (_border != null)
534                 { // for nine-patch image
535                     _nPatchMap = new PropertyMap();
536                     _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
537                     _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
538                     _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
539                     if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
540                     if (_synchronousLoading != null) { _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
541                     if (_orientationCorrection != null) { _nPatchMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
542                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
543                 }
544                 else if (_synchronousLoading != null || _orientationCorrection != null)
545                 { // for normal image, with synchronous loading property
546                     PropertyMap imageMap = new PropertyMap();
547                     imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
548                     imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
549                     if (_synchronousLoading != null) { imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
550                     if (_orientationCorrection != null) { imageMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
551                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
552                 }
553                 else
554                 { // just for normal image
555                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
556                 }
557             }
558         }
559
560         private Rectangle _border = null;
561         private PropertyMap _nPatchMap = null;
562         private bool? _synchronousLoading = null;
563         private bool? _borderOnly = null;
564         private string _url = null;
565         private bool? _orientationCorrection = null;
566     }
567
568 }