2 * Copyright(c) 2018 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Runtime.InteropServices;
19 using System.ComponentModel;
20 using Tizen.NUI.Binding;
22 namespace Tizen.NUI.BaseComponents
26 /// ImageView is a class for displaying an image resource.<br />
27 /// An instance of ImageView can be created using a URL or an image instance.<br />
29 /// <since_tizen> 3 </since_tizen>
30 public class ImageView : View
32 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
33 [EditorBrowsable(EditorBrowsableState.Never)]
34 public static readonly BindableProperty ResourceUrlProperty = BindableProperty.Create(nameof(ImageView.ResourceUrl), typeof(string), typeof(ImageView), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
36 var imageView = (ImageView)bindable;
39 string url = (string)newValue;
41 imageView.UpdateImage();
44 defaultValueCreator: (bindable) =>
46 var imageView = (ImageView)bindable;
47 if(imageView._imageType == ImageType.Normal)
49 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.IMAGE).Get(out imageView._url);
51 return imageView._url;
54 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
55 [EditorBrowsable(EditorBrowsableState.Never)]
56 public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(ImageView.Image), typeof(PropertyMap), typeof(ImageView), null, propertyChanged: (bindable, oldValue, newValue) =>
58 var imageView = (ImageView)bindable;
61 PropertyMap map = (PropertyMap)newValue;
63 if (imageView._border == null)
65 imageView._image = map;
66 imageView.UpdateImage();
70 defaultValueCreator: (bindable) =>
72 var imageView = (ImageView)bindable;
73 if (imageView._border == null)
75 PropertyMap temp = new PropertyMap();
76 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.IMAGE).Get(temp);
85 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
86 [EditorBrowsable(EditorBrowsableState.Never)]
87 public static readonly BindableProperty PreMultipliedAlphaProperty = BindableProperty.Create("PreMultipliedAlpha", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
89 var imageView = (ImageView)bindable;
92 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue((bool)newValue));
95 defaultValueCreator: (bindable) =>
97 var imageView = (ImageView)bindable;
99 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
103 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
104 [EditorBrowsable(EditorBrowsableState.Never)]
105 public static readonly BindableProperty PixelAreaProperty = BindableProperty.Create("PixelArea", typeof(RelativeVector4), typeof(ImageView), null, propertyChanged: (bindable, oldValue, newValue) =>
107 var imageView = (ImageView)bindable;
108 if (newValue != null)
110 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue((RelativeVector4)newValue));
113 defaultValueCreator: (bindable) =>
115 var imageView = (ImageView)bindable;
116 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
117 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.PIXEL_AREA).Get(temp);
118 RelativeVector4 relativeTemp = new RelativeVector4(temp.X, temp.Y, temp.Z, temp.W);
122 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
123 [EditorBrowsable(EditorBrowsableState.Never)]
124 public static readonly BindableProperty BorderProperty = BindableProperty.Create("Border", typeof(Rectangle), typeof(ImageView), null, propertyChanged: (bindable, oldValue, newValue) =>
126 var imageView = (ImageView)bindable;
127 if (newValue != null)
129 imageView._border = (Rectangle)newValue;
130 imageView.UpdateImage();
133 defaultValueCreator: (bindable) =>
135 var imageView = (ImageView)bindable;
136 return imageView._border;
139 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
140 [EditorBrowsable(EditorBrowsableState.Never)]
141 public static readonly BindableProperty BorderOnlyProperty = BindableProperty.Create("BorderOnly", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
143 var imageView = (ImageView)bindable;
144 if (newValue != null)
146 imageView._borderOnly = (bool)newValue;
147 imageView.UpdateImage();
150 defaultValueCreator: (bindable) =>
152 var imageView = (ImageView)bindable;
153 return imageView._borderOnly ?? false;
156 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
157 [EditorBrowsable(EditorBrowsableState.Never)]
158 public static readonly BindableProperty SynchronosLoadingProperty = BindableProperty.Create("SynchronosLoading", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
160 var imageView = (ImageView)bindable;
161 if (newValue != null)
163 imageView._synchronousLoading = (bool)newValue;
164 imageView.UpdateImage();
167 defaultValueCreator: (bindable) =>
169 var imageView = (ImageView)bindable;
170 return imageView._synchronousLoading ?? false;
173 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
174 [EditorBrowsable(EditorBrowsableState.Never)]
175 public static readonly BindableProperty OrientationCorrectionProperty = BindableProperty.Create("OrientationCorrection", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
177 var imageView = (ImageView)bindable;
178 if (newValue != null)
180 imageView._orientationCorrection = (bool)newValue;
181 imageView.UpdateImage();
184 defaultValueCreator: (bindable) =>
186 var imageView = (ImageView)bindable;
187 return imageView._orientationCorrection ?? false;
190 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
191 private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
192 private ResourceReadyEventCallbackType _resourceReadyEventCallback;
193 private EventHandler<ResourceLoadedEventArgs> _resourceLoadedEventHandler;
194 private _resourceLoadedCallbackType _resourceLoadedCallback;
196 private Rectangle _border;
197 private PropertyMap _nPatchMap;
198 private bool? _synchronousLoading;
199 private bool? _borderOnly;
201 private bool? _orientationCorrection;
202 private PropertyMap _image;
203 private ImageType _imageType;
206 /// Creates an initialized ImageView.
208 /// <since_tizen> 3 </since_tizen>
209 public ImageView() : this(Interop.ImageView.ImageView_New__SWIG_0(), true)
211 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
215 /// Creates an initialized ImageView from a URL to an image resource.<br />
216 /// If the string is empty, ImageView will not display anything.<br />
218 /// <param name="url">The URL of the image resource to display.</param>
219 /// <since_tizen> 3 </since_tizen>
220 public ImageView(string url) : this(Interop.ImageView.ImageView_New__SWIG_2(url), true)
223 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
226 internal ImageView(string url, Uint16Pair size) : this(Interop.ImageView.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
229 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
232 internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.ImageView.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
234 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
237 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
238 private delegate void ResourceReadyEventCallbackType(IntPtr data);
239 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
240 private delegate void _resourceLoadedCallbackType(IntPtr view);
243 /// An event for ResourceReady signal which can be used to subscribe or unsubscribe the event handler.<br />
244 /// This signal is emitted after 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 />
247 /// <since_tizen> 3 </since_tizen>
248 public event EventHandler<ResourceReadyEventArgs> ResourceReady
252 if (_resourceReadyEventHandler == null)
254 _resourceReadyEventCallback = OnResourceReady;
255 ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
258 _resourceReadyEventHandler += value;
263 _resourceReadyEventHandler -= value;
265 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
267 ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
272 internal event EventHandler<ResourceLoadedEventArgs> ResourceLoaded
276 if (_resourceLoadedEventHandler == null)
278 _resourceLoadedCallback = OnResourceLoaded;
279 this.ResourceReadySignal(this).Connect(_resourceLoadedCallback);
282 _resourceLoadedEventHandler += value;
286 _resourceLoadedEventHandler -= value;
288 if (_resourceLoadedEventHandler == null && this.ResourceReadySignal(this).Empty() == false)
290 this.ResourceReadySignal(this).Disconnect(_resourceLoadedCallback);
296 /// Enumeration for LoadingStatus of image.
298 /// <since_tizen> 5 </since_tizen>
299 public enum LoadingStatusType
302 /// Loading preparing status.
304 /// <since_tizen> 5 </since_tizen>
307 /// Loading ready status.
309 /// <since_tizen> 5 </since_tizen>
312 /// Loading failed status.
314 /// <since_tizen> 5 </since_tizen>
319 /// ImageView ResourceUrl, type string.
320 /// This is one of mandatory property. Even if not set or null set, it sets empty string ("") internally.
321 /// When it is set as null, it gives empty string ("") to be read.
323 /// <since_tizen> 3 </since_tizen>
324 public string ResourceUrl
328 return (string)GetValue(ResourceUrlProperty);
332 _url = (value == null ? "" : value);
333 SetValue(ResourceUrlProperty, _url);
334 NotifyPropertyChanged();
339 /// This will be deprecated, please use Image instead. <br />
340 /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
342 /// <since_tizen> 3 </since_tizen>
343 [Obsolete("Please do not use! This will be deprecated! Please use Image property instead!")]
344 [EditorBrowsable(EditorBrowsableState.Never)]
345 public PropertyMap ImageMap
351 PropertyMap temp = new PropertyMap();
352 GetProperty(ImageView.Property.IMAGE).Get(temp);
364 if (_url != null) { value.Add("url", new PropertyValue(_url)); }
365 SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
366 NotifyPropertyChanged();
372 /// ImageView Image, type PropertyMap
374 /// <since_tizen> 4 </since_tizen>
375 public PropertyMap Image
381 return (PropertyMap)GetValue(ImageProperty);
392 SetValue(ImageProperty, value);
393 NotifyPropertyChanged();
399 /// ImageView PreMultipliedAlpha, type Boolean.<br />
400 /// Image must be initialized.<br />
402 /// <since_tizen> 3 </since_tizen>
403 public bool PreMultipliedAlpha
407 return (bool)GetValue(PreMultipliedAlphaProperty);
411 SetValue(PreMultipliedAlphaProperty, value);
412 NotifyPropertyChanged();
417 /// ImageView PixelArea, type Vector4 (Animatable property).<br />
418 /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
420 /// <since_tizen> 3 </since_tizen>
421 public RelativeVector4 PixelArea
425 return (RelativeVector4)GetValue(PixelAreaProperty);
429 SetValue(PixelAreaProperty, value);
430 NotifyPropertyChanged();
435 /// The border of the image in the order: left, right, bottom, top.<br />
436 /// If set, ImageMap will be ignored.<br />
437 /// For N-Patch images only.<br />
440 /// <since_tizen> 3 </since_tizen>
441 public Rectangle Border
445 return (Rectangle)GetValue(BorderProperty);
449 SetValue(BorderProperty, value);
450 NotifyPropertyChanged();
455 /// Gets or sets whether to draw the borders only (if true).<br />
456 /// If not specified, the default is false.<br />
457 /// For N-Patch images only.<br />
460 /// <since_tizen> 3 </since_tizen>
461 public bool BorderOnly
465 return (bool)GetValue(BorderOnlyProperty);
469 SetValue(BorderOnlyProperty, value);
470 NotifyPropertyChanged();
475 /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
477 /// <since_tizen> 3 </since_tizen>
478 public bool SynchronosLoading
482 return (bool)GetValue(SynchronosLoadingProperty);
486 SetValue(SynchronosLoadingProperty, value);
487 NotifyPropertyChanged();
492 /// Gets or sets whether to automatically correct the orientation of an image.<br />
494 /// <since_tizen> 5 </since_tizen>
495 public bool OrientationCorrection
499 return (bool)GetValue(OrientationCorrectionProperty);
503 SetValue(OrientationCorrectionProperty, value);
504 NotifyPropertyChanged();
509 /// Gets the loading state of the visual resource.
511 /// <since_tizen> 5 </since_tizen>
512 public ImageView.LoadingStatusType LoadingStatus
516 return (ImageView.LoadingStatusType)Interop.View.View_GetVisualResourceStatus(swigCPtr, (int)Property.IMAGE);
521 /// Downcasts a handle to imageView handle.
523 /// Please do not use! this will be deprecated!
524 /// Instead please use as keyword.
525 /// <since_tizen> 3 </since_tizen>
526 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead! " +
528 "BaseHandle handle = new ImageView(imagePath); " +
529 "ImageView image = handle as ImageView")]
530 [EditorBrowsable(EditorBrowsableState.Never)]
531 public static ImageView DownCast(BaseHandle handle)
533 ImageView ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
534 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
539 /// Sets this ImageView from the given URL.<br />
540 /// If the URL is empty, ImageView will not display anything.<br />
542 /// <param name="url">The URL to the image resource to display.</param>
543 /// <since_tizen> 3 </since_tizen>
544 public void SetImage(string url)
547 Interop.ImageView.ImageView_SetImage__SWIG_1(swigCPtr, url);
548 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
552 /// Queries if all resources required by a control are loaded and ready.<br />
553 /// Most resources are only loaded when the control is placed on the stage.<br />
554 /// True if the resources are loaded and ready, false otherwise.<br />
556 /// <since_tizen> 3 </since_tizen>
557 public new bool IsResourceReady()
559 bool ret = Interop.View.IsResourceReady(swigCPtr);
560 if (NDalicPINVOKE.SWIGPendingException.Pending)
561 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
566 /// Forcefully reloads the image. All the visuals using this image will reload to the latest image.
568 /// <since_tizen> 5 </since_tizen>
571 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_RELOAD, new PropertyValue(0));
575 /// Plays the animated GIF. This is also the default playback mode.
577 /// <since_tizen> 5 </since_tizen>
580 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PLAY, new PropertyValue(0));
584 /// Pauses the animated GIF.
586 /// <since_tizen> 5 </since_tizen>
589 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PAUSE, new PropertyValue(0));
593 /// Stops the animated GIF.
595 /// <since_tizen> 5 </since_tizen>
598 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_STOP, new PropertyValue(0));
601 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
603 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
606 internal void SetImage(string url, Uint16Pair size)
609 Interop.ImageView.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
610 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
613 internal ViewResourceReadySignal ResourceReadySignal(View view)
615 ViewResourceReadySignal ret = new ViewResourceReadySignal(Interop.View.ResourceReadySignal(View.getCPtr(view)), false);
616 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
620 internal ResourceLoadingStatusType GetResourceStatus()
622 return (ResourceLoadingStatusType)Interop.View.View_GetVisualResourceStatus(this.swigCPtr, Property.IMAGE);
626 /// you can override it to clean-up your own resources.
628 /// <param name="type">DisposeTypes</param>
629 /// <since_tizen> 3 </since_tizen>
630 protected override void Dispose(DisposeTypes type)
637 if (type == DisposeTypes.Explicit)
640 //Release your own managed resources here.
641 //You should release all of your own disposable objects here.
644 _nPatchMap?.Dispose();
650 //Release your own unmanaged resources here.
651 //You should not access any managed member here except static instance.
652 //because the execution order of Finalizes is non-deterministic.
654 if (swigCPtr.Handle != global::System.IntPtr.Zero)
659 Interop.ImageView.delete_ImageView(swigCPtr);
661 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
667 // Callback for View ResourceReady signal
668 private void OnResourceReady(IntPtr data)
670 ResourceReadyEventArgs e = new ResourceReadyEventArgs();
673 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
676 if (_resourceReadyEventHandler != null)
678 _resourceReadyEventHandler(this, e);
682 private void UpdateImage()
684 if (_url != null && _url != "")
687 { // for nine-patch image
688 _nPatchMap = new PropertyMap();
689 _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
690 _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
691 _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
692 if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
693 if (_synchronousLoading != null) { _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
694 if (_orientationCorrection != null) { _nPatchMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
695 SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
696 _imageType = ImageType.Npatch;
698 else if (_synchronousLoading != null || _orientationCorrection != null)
699 { // for normal image, with synchronous loading property
700 PropertyMap imageMap = new PropertyMap();
701 imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
702 imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
703 if (_synchronousLoading != null) { imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
704 if (_orientationCorrection != null) { imageMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
705 SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
707 _imageType = ImageType.Specific;
710 { // just for normal image
711 SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
712 _imageType = ImageType.Normal;
717 //Image property is set and used
718 PropertyMap map = new PropertyMap();
719 map.Insert(ImageVisualProperty.URL, new PropertyValue(""));
720 if (_synchronousLoading != null)
722 map.Insert(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
724 if (_orientationCorrection != null)
726 map.Insert(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection));
732 SetProperty(ImageView.Property.IMAGE, new PropertyValue(map));
733 _imageType = ImageType.Normal;
737 private void OnResourceLoaded(IntPtr view)
739 ResourceLoadedEventArgs e = new ResourceLoadedEventArgs();
740 e.Status = (ResourceLoadingStatusType)Interop.View.View_GetVisualResourceStatus(this.swigCPtr, Property.IMAGE);
742 if (_resourceLoadedEventHandler != null)
744 _resourceLoadedEventHandler(this, e);
749 /// Event arguments of resource ready.
751 /// <since_tizen> 3 </since_tizen>
752 public class ResourceReadyEventArgs : EventArgs
757 /// The view whose resource is ready.
759 /// <since_tizen> 3 </since_tizen>
773 internal class ResourceLoadedEventArgs : EventArgs
775 private ResourceLoadingStatusType status = ResourceLoadingStatusType.Invalid;
776 public ResourceLoadingStatusType Status
789 internal new class Property
791 internal static readonly int RESOURCE_URL = Interop.ImageView.ImageView_Property_RESOURCE_URL_get();
792 internal static readonly int IMAGE = Interop.ImageView.ImageView_Property_IMAGE_get();
793 internal static readonly int PRE_MULTIPLIED_ALPHA = Interop.ImageView.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
794 internal static readonly int PIXEL_AREA = Interop.ImageView.ImageView_Property_PIXEL_AREA_get();
795 internal static readonly int ACTION_RELOAD = Interop.ImageView.ImageView_IMAGE_VISUAL_ACTION_RELOAD_get();
796 internal static readonly int ACTION_PLAY = Interop.ImageView.ImageView_IMAGE_VISUAL_ACTION_PLAY_get();
797 internal static readonly int ACTION_PAUSE = Interop.ImageView.ImageView_IMAGE_VISUAL_ACTION_PAUSE_get();
798 internal static readonly int ACTION_STOP = Interop.ImageView.ImageView_IMAGE_VISUAL_ACTION_STOP_get();
801 private enum ImageType
804 /// For Normal Image.
809 /// For normal image, with synchronous loading and orientation correction property
814 /// For nine-patch image