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