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("ResourceUrl", typeof(string), typeof(ImageView), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
36 var imageView = (ImageView)bindable;
39 imageView._url = (newValue == null? "" : (string)newValue);
40 imageView.UpdateImage();
43 defaultValueCreator:(bindable) =>
45 var imageView = (ImageView)bindable;
46 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.IMAGE).Get(out imageView._url);
47 return imageView._url;
49 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
50 [EditorBrowsable(EditorBrowsableState.Never)]
51 public static readonly BindableProperty ImageProperty = BindableProperty.Create("Image", typeof(PropertyMap), typeof(ImageView), new PropertyMap(), propertyChanged: (bindable, oldValue, newValue) =>
53 var imageView = (ImageView)bindable;
56 if (imageView._border == null)
58 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue((PropertyMap)newValue));
62 defaultValueCreator:(bindable) =>
64 var imageView = (ImageView)bindable;
65 if (imageView._border == null)
67 PropertyMap temp = new PropertyMap();
68 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.IMAGE).Get(temp);
76 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
77 [EditorBrowsable(EditorBrowsableState.Never)]
78 public static readonly BindableProperty PreMultipliedAlphaProperty = BindableProperty.Create("PreMultipliedAlpha", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
80 var imageView = (ImageView)bindable;
83 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue((bool)newValue));
86 defaultValueCreator:(bindable) =>
88 var imageView = (ImageView)bindable;
90 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
93 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
94 [EditorBrowsable(EditorBrowsableState.Never)]
95 public static readonly BindableProperty PixelAreaProperty = BindableProperty.Create("PixelArea", typeof(RelativeVector4), typeof(ImageView), new RelativeVector4(0,0,0,0), propertyChanged: (bindable, oldValue, newValue) =>
97 var imageView = (ImageView)bindable;
100 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue((RelativeVector4)newValue));
103 defaultValueCreator:(bindable) =>
105 var imageView = (ImageView)bindable;
106 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
107 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.PIXEL_AREA).Get(temp);
108 RelativeVector4 relativeTemp = new RelativeVector4(temp.X, temp.Y, temp.Z, temp.W);
111 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
112 [EditorBrowsable(EditorBrowsableState.Never)]
113 public static readonly BindableProperty BorderProperty = BindableProperty.Create("Border", typeof(Rectangle), typeof(ImageView), new Rectangle(0,0,0,0), propertyChanged: (bindable, oldValue, newValue) =>
115 var imageView = (ImageView)bindable;
116 if (newValue != null)
118 imageView._border = (Rectangle)newValue;
119 imageView.UpdateImage();
122 defaultValueCreator:(bindable) =>
124 var imageView = (ImageView)bindable;
125 return imageView._border;
127 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
128 [EditorBrowsable(EditorBrowsableState.Never)]
129 public static readonly BindableProperty BorderOnlyProperty = BindableProperty.Create("BorderOnly", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
131 var imageView = (ImageView)bindable;
132 if (newValue != null)
134 imageView._borderOnly = (bool)newValue;
135 imageView.UpdateImage();
138 defaultValueCreator:(bindable) =>
140 var imageView = (ImageView)bindable;
141 return imageView._borderOnly ?? false;
143 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
144 [EditorBrowsable(EditorBrowsableState.Never)]
145 public static readonly BindableProperty SynchronosLoadingProperty = BindableProperty.Create("SynchronosLoading", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
147 var imageView = (ImageView)bindable;
148 if (newValue != null)
150 imageView._synchronousLoading = (bool)newValue;
151 imageView.UpdateImage();
154 defaultValueCreator:(bindable) =>
156 var imageView = (ImageView)bindable;
157 return imageView._synchronousLoading ?? false;
159 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
160 [EditorBrowsable(EditorBrowsableState.Never)]
161 public static readonly BindableProperty OrientationCorrectionProperty = BindableProperty.Create("OrientationCorrection", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
163 var imageView = (ImageView)bindable;
164 if (newValue != null)
166 imageView._orientationCorrection = (bool)newValue;
167 imageView.UpdateImage();
170 defaultValueCreator:(bindable) =>
172 var imageView = (ImageView)bindable;
173 return imageView._orientationCorrection ?? false;
176 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
178 internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
180 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
183 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
185 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
190 /// Event arguments of resource ready.
192 /// <since_tizen> 3 </since_tizen>
193 public class ResourceReadyEventArgs : EventArgs
198 /// The view whose resource is ready.
200 /// <since_tizen> 3 </since_tizen>
214 private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
215 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
216 private delegate void ResourceReadyEventCallbackType(IntPtr data);
217 private ResourceReadyEventCallbackType _resourceReadyEventCallback;
220 /// An event for ResourceReady signal which can be used to subscribe or unsubscribe the event handler.<br />
221 /// This signal is emitted after all resources required by a control are loaded and ready.<br />
222 /// Most resources are only loaded when the control is placed on the stage.<br />
224 /// <since_tizen> 3 </since_tizen>
225 public event EventHandler<ResourceReadyEventArgs> ResourceReady
229 if (_resourceReadyEventHandler == null)
231 _resourceReadyEventCallback = OnResourceReady;
232 ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
235 _resourceReadyEventHandler += value;
240 _resourceReadyEventHandler -= value;
242 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
244 ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
249 // Callback for View ResourceReady signal
250 private void OnResourceReady(IntPtr data)
252 ResourceReadyEventArgs e = new ResourceReadyEventArgs();
255 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
258 if (_resourceReadyEventHandler != null)
260 _resourceReadyEventHandler(this, e);
265 /// you can override it to clean-up your own resources.
267 /// <param name="type">DisposeTypes</param>
268 /// <since_tizen> 3 </since_tizen>
269 protected override void Dispose(DisposeTypes type)
276 if (type == DisposeTypes.Explicit)
279 //Release your own managed resources here.
280 //You should release all of your own disposable objects here.
283 _nPatchMap?.Dispose();
287 //Release your own unmanaged resources here.
288 //You should not access any managed member here except static instance.
289 //because the execution order of Finalizes is non-deterministic.
291 if (swigCPtr.Handle != global::System.IntPtr.Zero)
296 NDalicPINVOKE.delete_ImageView(swigCPtr);
298 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
304 internal new class Property
306 internal static readonly int RESOURCE_URL = NDalicPINVOKE.ImageView_Property_RESOURCE_URL_get();
307 internal static readonly int IMAGE = NDalicPINVOKE.ImageView_Property_IMAGE_get();
308 internal static readonly int PRE_MULTIPLIED_ALPHA = NDalicPINVOKE.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
309 internal static readonly int PIXEL_AREA = NDalicPINVOKE.ImageView_Property_PIXEL_AREA_get();
310 internal static readonly int ACTION_RELOAD = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_RELOAD_get();
311 internal static readonly int ACTION_PLAY = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_PLAY_get();
312 internal static readonly int ACTION_PAUSE = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_PAUSE_get();
313 internal static readonly int ACTION_STOP = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_STOP_get();
317 /// Creates an initialized ImageView.
319 /// <since_tizen> 3 </since_tizen>
320 public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
322 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
326 /// Creates an initialized ImageView from a URL to an image resource.<br />
327 /// If the string is empty, ImageView will not display anything.<br />
329 /// <param name="url">The URL of the image resource to display.</param>
330 /// <since_tizen> 3 </since_tizen>
331 public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
334 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
337 internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
340 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
345 /// Downcasts a handle to imageView handle.
347 /// Please do not use! this will be deprecated!
348 /// Instead please use as keyword.
349 /// <since_tizen> 3 </since_tizen>
350 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead! " +
352 "BaseHandle handle = new ImageView(imagePath); " +
353 "ImageView image = handle as ImageView")]
354 [EditorBrowsable(EditorBrowsableState.Never)]
355 public static ImageView DownCast(BaseHandle handle)
357 ImageView ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
358 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
363 /// Sets this ImageView from the given URL.<br />
364 /// If the URL is empty, ImageView will not display anything.<br />
366 /// <param name="url">The URL to the image resource to display.</param>
367 /// <since_tizen> 3 </since_tizen>
368 public void SetImage(string url)
371 NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
372 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
374 internal void SetImage(string url, Uint16Pair size)
377 NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
378 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
381 internal ViewResourceReadySignal ResourceReadySignal(View view)
383 ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
384 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
389 /// Queries if all resources required by a control are loaded and ready.<br />
390 /// Most resources are only loaded when the control is placed on the stage.<br />
391 /// True if the resources are loaded and ready, false otherwise.<br />
393 /// <since_tizen> 3 </since_tizen>
394 public new bool IsResourceReady()
396 bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
397 if (NDalicPINVOKE.SWIGPendingException.Pending)
398 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
403 /// Forcefully reloads the image. All the visuals using this image will reload to the latest image.
405 /// <since_tizen> 5 </since_tizen>
408 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_RELOAD, new PropertyValue(0));
412 /// Plays the animated GIF. This is also the default playback mode.
414 /// <since_tizen> 5 </since_tizen>
417 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PLAY, new PropertyValue(0));
421 /// Pauses the animated GIF.
423 /// <since_tizen> 5 </since_tizen>
426 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PAUSE, new PropertyValue(0));
430 /// Stops the animated GIF.
432 /// <since_tizen> 5 </since_tizen>
435 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_STOP, new PropertyValue(0));
439 /// ImageView ResourceUrl, type string.
441 /// <since_tizen> 3 </since_tizen>
442 public string ResourceUrl
446 return (string)GetValue(ResourceUrlProperty);
450 SetValue(ResourceUrlProperty, value);
451 NotifyPropertyChanged();
456 /// This will be deprecated, please use Image instead. <br />
457 /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
459 /// <since_tizen> 3 </since_tizen>
460 [Obsolete("Please do not use! This will be deprecated! Please use Image property instead!")]
461 [EditorBrowsable(EditorBrowsableState.Never)]
462 public PropertyMap ImageMap
468 PropertyMap temp = new PropertyMap();
469 GetProperty(ImageView.Property.IMAGE).Get(temp);
481 if (_url != null) { value.Add("url", new PropertyValue(_url)); }
482 SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
483 NotifyPropertyChanged();
489 /// ImageView Image, type PropertyMap
491 /// <since_tizen> 4 </since_tizen>
492 public PropertyMap Image
498 return (PropertyMap)GetValue(ImageProperty);
509 SetValue(ImageProperty, value);
510 NotifyPropertyChanged();
516 /// ImageView PreMultipliedAlpha, type Boolean.<br />
517 /// Image must be initialized.<br />
519 /// <since_tizen> 3 </since_tizen>
520 public bool PreMultipliedAlpha
524 return (bool)GetValue(PreMultipliedAlphaProperty);
528 SetValue(PreMultipliedAlphaProperty, value);
529 NotifyPropertyChanged();
534 /// ImageView PixelArea, type Vector4 (Animatable property).<br />
535 /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
537 /// <since_tizen> 3 </since_tizen>
538 public RelativeVector4 PixelArea
542 return (RelativeVector4)GetValue(PixelAreaProperty);
546 SetValue(PixelAreaProperty, value);
547 NotifyPropertyChanged();
552 /// The border of the image in the order: left, right, bottom, top.<br />
553 /// If set, ImageMap will be ignored.<br />
554 /// For N-Patch images only.<br />
557 /// <since_tizen> 3 </since_tizen>
558 public Rectangle Border
562 return (Rectangle)GetValue(BorderProperty);
566 SetValue(BorderProperty, value);
567 NotifyPropertyChanged();
572 /// Gets or sets whether to draw the borders only (if true).<br />
573 /// If not specified, the default is false.<br />
574 /// For N-Patch images only.<br />
577 /// <since_tizen> 3 </since_tizen>
578 public bool BorderOnly
582 return (bool)GetValue(BorderOnlyProperty);
586 SetValue(BorderOnlyProperty, value);
587 NotifyPropertyChanged();
592 /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
594 /// <since_tizen> 3 </since_tizen>
595 public bool SynchronosLoading
599 return (bool)GetValue(SynchronosLoadingProperty);
603 SetValue(SynchronosLoadingProperty, value);
604 NotifyPropertyChanged();
609 /// Gets or sets whether to automatically correct the orientation of an image.<br />
611 /// <since_tizen> 5 </since_tizen>
612 public bool OrientationCorrection
616 return (bool)GetValue(OrientationCorrectionProperty);
620 SetValue(OrientationCorrectionProperty, value);
621 NotifyPropertyChanged();
627 /// Gets the loading state of the visual resource.
629 /// <since_tizen> 5 </since_tizen>
630 public ImageView.LoadingStatusType LoadingStatus
634 return (ImageView.LoadingStatusType)NDalicManualPINVOKE.View_GetVisualResourceStatus(swigCPtr, (int)Property.IMAGE);
639 /// Enumeration for LoadingStatus of image.
641 /// <since_tizen> 5 </since_tizen>
642 public enum LoadingStatusType
645 /// Loading preparing status.
647 /// <since_tizen> 5 </since_tizen>
650 /// Loading ready status.
652 /// <since_tizen> 5 </since_tizen>
655 /// Loading failed status.
657 /// <since_tizen> 5 </since_tizen>
662 private void UpdateImage()
667 { // for nine-patch image
668 _nPatchMap = new PropertyMap();
669 _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
670 _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
671 _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
672 if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
673 if (_synchronousLoading != null) { _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
674 if (_orientationCorrection != null) { _nPatchMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
675 SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
677 else if (_synchronousLoading != null || _orientationCorrection != null)
678 { // for normal image, with synchronous loading property
679 PropertyMap imageMap = new PropertyMap();
680 imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
681 imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
682 if (_synchronousLoading != null) { imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
683 if (_orientationCorrection != null) { imageMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
684 SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
687 { // just for normal image
688 SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
693 private Rectangle _border = null;
694 private PropertyMap _nPatchMap = null;
695 private bool? _synchronousLoading = null;
696 private bool? _borderOnly = null;
697 private string _url = null;
698 private bool? _orientationCorrection = null;