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 [ContentProperty("ResourceUrl")]
31 public class ImageView : View
33 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
34 [EditorBrowsable(EditorBrowsableState.Never)]
35 public static readonly BindableProperty ResourceUrlProperty = BindableProperty.Create("ResourceUrl", typeof(string), typeof(ImageView), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
37 var imageView = (ImageView)bindable;
40 imageView._url = (newValue == null? "" : (string)newValue);
41 imageView.UpdateImage();
43 Console.WriteLine("ImageView ResourceUrlProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
45 defaultValueCreator:(bindable) =>
47 var imageView = (ImageView)bindable;
48 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.IMAGE).Get(out imageView._url);
49 return imageView._url;
51 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 public static readonly BindableProperty ImageProperty = BindableProperty.Create("Image", typeof(PropertyMap), typeof(ImageView), new PropertyMap(), propertyChanged: (bindable, oldValue, newValue) =>
55 var imageView = (ImageView)bindable;
58 if (imageView._border == null)
60 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue((PropertyMap)newValue));
63 Console.WriteLine("ImageView ImageProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
65 defaultValueCreator:(bindable) =>
67 var imageView = (ImageView)bindable;
68 if (imageView._border == null)
70 PropertyMap temp = new PropertyMap();
71 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.IMAGE).Get(temp);
79 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
80 [EditorBrowsable(EditorBrowsableState.Never)]
81 public static readonly BindableProperty PreMultipliedAlphaProperty = BindableProperty.Create("PreMultipliedAlpha", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
83 var imageView = (ImageView)bindable;
86 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue((bool)newValue));
88 Console.WriteLine("ImageView PreMultipliedAlphaProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
90 defaultValueCreator:(bindable) =>
92 var imageView = (ImageView)bindable;
94 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
97 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
98 [EditorBrowsable(EditorBrowsableState.Never)]
99 public static readonly BindableProperty PixelAreaProperty = BindableProperty.Create("PixelArea", typeof(RelativeVector4), typeof(ImageView), new RelativeVector4(0,0,0,0), propertyChanged: (bindable, oldValue, newValue) =>
101 var imageView = (ImageView)bindable;
102 if (newValue != null)
104 Tizen.NUI.Object.SetProperty(imageView.swigCPtr, ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue((RelativeVector4)newValue));
106 Console.WriteLine("ImageView PixelAreaProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
108 defaultValueCreator:(bindable) =>
110 var imageView = (ImageView)bindable;
111 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
112 Tizen.NUI.Object.GetProperty(imageView.swigCPtr, ImageView.Property.PIXEL_AREA).Get(temp);
113 RelativeVector4 relativeTemp = new RelativeVector4(temp.X, temp.Y, temp.Z, temp.W);
116 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
117 [EditorBrowsable(EditorBrowsableState.Never)]
118 public static readonly BindableProperty BorderProperty = BindableProperty.Create("Border", typeof(Rectangle), typeof(ImageView), new Rectangle(0,0,0,0), propertyChanged: (bindable, oldValue, newValue) =>
120 var imageView = (ImageView)bindable;
121 if (newValue != null)
123 imageView._border = (Rectangle)newValue;
124 imageView.UpdateImage();
126 Console.WriteLine("ImageView BorderProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
128 defaultValueCreator:(bindable) =>
130 var imageView = (ImageView)bindable;
131 return imageView._border;
133 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
134 [EditorBrowsable(EditorBrowsableState.Never)]
135 public static readonly BindableProperty BorderOnlyProperty = BindableProperty.Create("BorderOnly", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
137 var imageView = (ImageView)bindable;
138 if (newValue != null)
140 imageView._borderOnly = (bool)newValue;
141 imageView.UpdateImage();
143 Console.WriteLine("ImageView BorderOnlyProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
145 defaultValueCreator:(bindable) =>
147 var imageView = (ImageView)bindable;
148 return imageView._borderOnly ?? false;
150 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
151 [EditorBrowsable(EditorBrowsableState.Never)]
152 public static readonly BindableProperty SynchronosLoadingProperty = BindableProperty.Create("SynchronosLoading", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
154 var imageView = (ImageView)bindable;
155 if (newValue != null)
157 imageView._synchronousLoading = (bool)newValue;
158 imageView.UpdateImage();
160 Console.WriteLine("ImageView SynchronosLoadingProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
162 defaultValueCreator:(bindable) =>
164 var imageView = (ImageView)bindable;
165 return imageView._synchronousLoading ?? false;
167 /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
168 [EditorBrowsable(EditorBrowsableState.Never)]
169 public static readonly BindableProperty OrientationCorrectionProperty = BindableProperty.Create("OrientationCorrection", typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
171 var imageView = (ImageView)bindable;
172 if (newValue != null)
174 imageView._orientationCorrection = (bool)newValue;
175 imageView.UpdateImage();
177 Console.WriteLine("ImageView OrientationCorrectionProperty changed: oldValue: " + oldValue + ", newValue: " + newValue);
179 defaultValueCreator:(bindable) =>
181 var imageView = (ImageView)bindable;
182 return imageView._orientationCorrection ?? false;
185 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
187 internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
189 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
192 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
194 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
199 /// Event arguments of resource ready.
201 /// <since_tizen> 3 </since_tizen>
202 public class ResourceReadyEventArgs : EventArgs
207 /// The view whose resource is ready.
209 /// <since_tizen> 3 </since_tizen>
223 private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
224 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
225 private delegate void ResourceReadyEventCallbackType(IntPtr data);
226 private ResourceReadyEventCallbackType _resourceReadyEventCallback;
229 /// An event for ResourceReady signal which can be used to subscribe or unsubscribe the event handler.<br />
230 /// This signal is emitted after all resources required by a control are loaded and ready.<br />
231 /// Most resources are only loaded when the control is placed on the stage.<br />
233 /// <since_tizen> 3 </since_tizen>
234 public event EventHandler<ResourceReadyEventArgs> ResourceReady
238 if (_resourceReadyEventHandler == null)
240 _resourceReadyEventCallback = OnResourceReady;
241 ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
244 _resourceReadyEventHandler += value;
249 _resourceReadyEventHandler -= value;
251 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
253 ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
258 // Callback for View ResourceReady signal
259 private void OnResourceReady(IntPtr data)
261 ResourceReadyEventArgs e = new ResourceReadyEventArgs();
264 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
267 if (_resourceReadyEventHandler != null)
269 _resourceReadyEventHandler(this, e);
274 /// you can override it to clean-up your own resources.
276 /// <param name="type">DisposeTypes</param>
277 /// <since_tizen> 3 </since_tizen>
278 protected override void Dispose(DisposeTypes type)
285 if (type == DisposeTypes.Explicit)
288 //Release your own managed resources here.
289 //You should release all of your own disposable objects here.
292 _nPatchMap?.Dispose();
296 //Release your own unmanaged resources here.
297 //You should not access any managed member here except static instance.
298 //because the execution order of Finalizes is non-deterministic.
300 if (swigCPtr.Handle != global::System.IntPtr.Zero)
305 NDalicPINVOKE.delete_ImageView(swigCPtr);
307 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
313 internal new class Property
315 internal static readonly int RESOURCE_URL = NDalicPINVOKE.ImageView_Property_RESOURCE_URL_get();
316 internal static readonly int IMAGE = NDalicPINVOKE.ImageView_Property_IMAGE_get();
317 internal static readonly int PRE_MULTIPLIED_ALPHA = NDalicPINVOKE.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
318 internal static readonly int PIXEL_AREA = NDalicPINVOKE.ImageView_Property_PIXEL_AREA_get();
319 internal static readonly int ACTION_RELOAD = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_RELOAD_get();
320 internal static readonly int ACTION_PLAY = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_PLAY_get();
321 internal static readonly int ACTION_PAUSE = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_PAUSE_get();
322 internal static readonly int ACTION_STOP = NDalicManualPINVOKE.ImageView_IMAGE_VISUAL_ACTION_STOP_get();
326 /// Creates an initialized ImageView.
328 /// <since_tizen> 3 </since_tizen>
329 public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
331 base.InitXamlResource();
332 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
336 /// Creates an initialized ImageView from a URL to an image resource.<br />
337 /// If the string is empty, ImageView will not display anything.<br />
339 /// <param name="url">The URL of the image resource to display.</param>
340 /// <since_tizen> 3 </since_tizen>
341 public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
344 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
347 internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
350 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
355 /// Downcasts a handle to imageView handle.
357 /// Please do not use! this will be deprecated!
358 /// Instead please use as keyword.
359 /// <since_tizen> 3 </since_tizen>
360 [Obsolete("Please do not use! This will be deprecated! Please use as keyword instead! " +
362 "BaseHandle handle = new ImageView(imagePath); " +
363 "ImageView image = handle as ImageView")]
364 [EditorBrowsable(EditorBrowsableState.Never)]
365 public static ImageView DownCast(BaseHandle handle)
367 ImageView ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
368 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
373 /// Sets this ImageView from the given URL.<br />
374 /// If the URL is empty, ImageView will not display anything.<br />
376 /// <param name="url">The URL to the image resource to display.</param>
377 /// <since_tizen> 3 </since_tizen>
378 public void SetImage(string url)
381 NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
382 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
384 internal void SetImage(string url, Uint16Pair size)
387 NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
388 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
391 internal ViewResourceReadySignal ResourceReadySignal(View view)
393 ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
394 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
399 /// Queries if all resources required by a control are loaded and ready.<br />
400 /// Most resources are only loaded when the control is placed on the stage.<br />
401 /// True if the resources are loaded and ready, false otherwise.<br />
403 /// <since_tizen> 3 </since_tizen>
404 public new bool IsResourceReady()
406 bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
407 if (NDalicPINVOKE.SWIGPendingException.Pending)
408 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
413 /// Force reloading of the image, all visuals using this image will get the latest one.
415 /// <since_tizen> 5 </since_tizen>
418 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_RELOAD, new PropertyValue(0));
422 /// Play the animated GIF. This is also Default playback mode.
424 /// <since_tizen> 5 </since_tizen>
427 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PLAY, new PropertyValue(0));
431 /// Pause the animated GIF.
433 /// <since_tizen> 5 </since_tizen>
436 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PAUSE, new PropertyValue(0));
440 /// Stop the animated GIF.
442 /// <since_tizen> 5 </since_tizen>
445 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_STOP, new PropertyValue(0));
449 /// ImageView ResourceUrl, type string.
451 /// <since_tizen> 3 </since_tizen>
452 public string ResourceUrl
456 return (string)GetValue(ResourceUrlProperty);
460 SetValue(ResourceUrlProperty, value);
461 NotifyPropertyChanged();
466 /// This will be deprecated, please use Image instead. <br />
467 /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
469 /// <since_tizen> 3 </since_tizen>
470 [Obsolete("Please do not use! This will be deprecated! Please use Image property instead!")]
471 [EditorBrowsable(EditorBrowsableState.Never)]
472 public PropertyMap ImageMap
478 PropertyMap temp = new PropertyMap();
479 GetProperty(ImageView.Property.IMAGE).Get(temp);
491 if (_url != null) { value.Add("url", new PropertyValue(_url)); }
492 SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
493 NotifyPropertyChanged();
499 /// ImageView Image, type PropertyMap
501 /// <since_tizen> 4 </since_tizen>
502 public PropertyMap Image
508 return (PropertyMap)GetValue(ImageProperty);
519 SetValue(ImageProperty, value);
520 NotifyPropertyChanged();
526 /// ImageView PreMultipliedAlpha, type Boolean.<br />
527 /// Image must be initialized.<br />
529 /// <since_tizen> 3 </since_tizen>
530 public bool PreMultipliedAlpha
534 return (bool)GetValue(PreMultipliedAlphaProperty);
538 SetValue(PreMultipliedAlphaProperty, value);
539 NotifyPropertyChanged();
544 /// ImageView PixelArea, type Vector4 (Animatable property).<br />
545 /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
547 /// <since_tizen> 3 </since_tizen>
548 public RelativeVector4 PixelArea
552 return (RelativeVector4)GetValue(PixelAreaProperty);
556 SetValue(PixelAreaProperty, value);
557 NotifyPropertyChanged();
562 /// The border of the image in the order: left, right, bottom, top.<br />
563 /// If set, ImageMap will be ignored.<br />
564 /// For N-Patch images only.<br />
567 /// <since_tizen> 3 </since_tizen>
568 public Rectangle Border
572 return (Rectangle)GetValue(BorderProperty);
576 SetValue(BorderProperty, value);
577 NotifyPropertyChanged();
582 /// Gets or sets whether to draw the borders only (if true).<br />
583 /// If not specified, the default is false.<br />
584 /// For N-Patch images only.<br />
587 /// <since_tizen> 3 </since_tizen>
588 public bool BorderOnly
592 return (bool)GetValue(BorderOnlyProperty);
596 SetValue(BorderOnlyProperty, value);
597 NotifyPropertyChanged();
602 /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
604 /// <since_tizen> 3 </since_tizen>
605 public bool SynchronosLoading
609 return (bool)GetValue(SynchronosLoadingProperty);
613 SetValue(SynchronosLoadingProperty, value);
614 NotifyPropertyChanged();
619 /// Gets or sets whether to correct orientation of image automatically.<br />
621 /// <since_tizen> 5 </since_tizen>
622 public bool OrientationCorrection
626 return (bool)GetValue(OrientationCorrectionProperty);
630 SetValue(OrientationCorrectionProperty, value);
631 NotifyPropertyChanged();
637 /// Get the loading state of the visual resource.
639 /// <since_tizen> 5 </since_tizen>
640 public ImageView.LoadingStatusType LoadingStatus
644 return (ImageView.LoadingStatusType)NDalicManualPINVOKE.View_GetVisualResourceStatus(swigCPtr, (int)Property.IMAGE);
649 /// Enumeration for LoadingStatus of image.
651 /// <since_tizen> 5 </since_tizen>
652 public enum LoadingStatusType
655 /// Loading preparing status.
657 /// <since_tizen> 5 </since_tizen>
660 /// Loading ready status.
662 /// <since_tizen> 5 </since_tizen>
665 /// Loading failed status.
667 /// <since_tizen> 5 </since_tizen>
672 private void UpdateImage()
677 { // for nine-patch image
678 _nPatchMap = new PropertyMap();
679 _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
680 _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
681 _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
682 if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
683 if (_synchronousLoading != null) { _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
684 if (_orientationCorrection != null) { _nPatchMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
685 SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
687 else if (_synchronousLoading != null || _orientationCorrection != null)
688 { // for normal image, with synchronous loading property
689 PropertyMap imageMap = new PropertyMap();
690 imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
691 imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
692 if (_synchronousLoading != null) { imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading)); }
693 if (_orientationCorrection != null) { imageMap.Add(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)_orientationCorrection)); }
694 SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
697 { // just for normal image
698 SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
703 private Rectangle _border = null;
704 private PropertyMap _nPatchMap = null;
705 private bool? _synchronousLoading = null;
706 private bool? _borderOnly = null;
707 private string _url = null;
708 private bool? _orientationCorrection = null;