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;
21 namespace Tizen.NUI.BaseComponents
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 />
28 /// <since_tizen> 3 </since_tizen>
29 public class ImageView : View
31 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
33 internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
35 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
38 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
40 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
45 /// Event arguments of resource ready.
47 /// <since_tizen> 3 </since_tizen>
48 public class ResourceReadyEventArgs : EventArgs
53 /// The view whose resource is ready.
55 /// <since_tizen> 3 </since_tizen>
69 private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
70 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
71 private delegate void ResourceReadyEventCallbackType(IntPtr data);
72 private ResourceReadyEventCallbackType _resourceReadyEventCallback;
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 />
79 /// <since_tizen> 3 </since_tizen>
80 public event EventHandler<ResourceReadyEventArgs> ResourceReady
84 if (_resourceReadyEventHandler == null)
86 _resourceReadyEventCallback = OnResourceReady;
87 ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
90 _resourceReadyEventHandler += value;
95 _resourceReadyEventHandler -= value;
97 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
99 ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
104 // Callback for View ResourceReady signal
105 private void OnResourceReady(IntPtr data)
107 ResourceReadyEventArgs e = new ResourceReadyEventArgs();
110 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
113 if (_resourceReadyEventHandler != null)
115 _resourceReadyEventHandler(this, e);
120 /// you can override it to clean-up your own resources.
122 /// <param name="type">DisposeTypes</param>
123 /// <since_tizen> 3 </since_tizen>
124 protected override void Dispose(DisposeTypes type)
131 if (type == DisposeTypes.Explicit)
134 //Release your own managed resources here.
135 //You should release all of your own disposable objects here.
138 _nPatchMap?.Dispose();
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.
146 if (swigCPtr.Handle != global::System.IntPtr.Zero)
151 NDalicPINVOKE.delete_ImageView(swigCPtr);
153 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
159 internal new class Property
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();
172 /// Creates an initialized ImageView.
174 /// <since_tizen> 3 </since_tizen>
175 public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
177 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
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 />
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)
189 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
192 internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
195 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200 /// Downcasts a handle to imageView handle.
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! " +
207 "BaseHandle handle = new ImageView(imagePath); " +
208 "ImageView image = handle as ImageView")]
209 [EditorBrowsable(EditorBrowsableState.Never)]
210 public new static ImageView DownCast(BaseHandle handle)
212 ImageView ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
213 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
218 /// Sets this ImageView from the given URL.<br />
219 /// If the URL is empty, ImageView will not display anything.<br />
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)
226 NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
227 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
229 internal void SetImage(string url, Uint16Pair size)
232 NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
233 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
236 internal ViewResourceReadySignal ResourceReadySignal(View view)
238 ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
239 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
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 />
248 /// <since_tizen> 3 </since_tizen>
249 public new bool IsResourceReady()
251 bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
252 if (NDalicPINVOKE.SWIGPendingException.Pending)
253 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
258 /// Force reloading of the image, all visuals using this image will get the latest one.
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)]
265 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_RELOAD, new PropertyValue(0));
269 /// Play the animated GIF. This is also Default playback mode.
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)]
276 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PLAY, new PropertyValue(0));
280 /// Pause the animated GIF.
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)]
287 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_PAUSE, new PropertyValue(0));
291 /// Stop the animated GIF.
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)]
298 this.DoAction(ImageView.Property.IMAGE, Property.ACTION_STOP, new PropertyValue(0));
302 /// ImageView ResourceUrl, type string.
304 /// <since_tizen> 3 </since_tizen>
305 public string ResourceUrl
309 GetProperty(ImageView.Property.IMAGE).Get(out _url);
314 _url = (value == null? "" : value);
320 /// This will be deprecated, please use Image instead. <br />
321 /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise.
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
332 PropertyMap temp = new PropertyMap();
333 GetProperty(ImageView.Property.IMAGE).Get(temp);
345 if (_url != null) { value.Add("url", new PropertyValue(_url)); }
346 SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
352 /// ImageView Image, type PropertyMap
354 /// <since_tizen> 4 </since_tizen>
355 public PropertyMap Image
361 PropertyMap temp = new PropertyMap();
362 GetProperty(ImageView.Property.IMAGE).Get(temp);
374 if(_url != null) { value.Add("url", new PropertyValue(_url)); }
375 SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
381 /// ImageView PreMultipliedAlpha, type Boolean.<br />
382 /// Image must be initialized.<br />
384 /// <since_tizen> 3 </since_tizen>
385 public bool PreMultipliedAlpha
390 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
395 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
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 />
403 /// <since_tizen> 3 </since_tizen>
404 public RelativeVector4 PixelArea
408 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
409 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
414 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
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 />
424 /// <since_tizen> 3 </since_tizen>
425 public Rectangle Border
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 />
444 /// <since_tizen> 3 </since_tizen>
445 public bool BorderOnly
449 return _borderOnly ?? false;
459 /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
461 /// <since_tizen> 3 </since_tizen>
462 public bool SynchronosLoading
466 return _synchronousLoading ?? false;
470 _synchronousLoading = value;
476 /// Gets or sets whether to correct orientation of image automatically.<br />
478 /// <since_tizen> 5 </since_tizen>
479 public bool OrientationCorrection
483 return _orientationCorrection ?? false;
487 _orientationCorrection = value;
494 /// Get the loading state of the visual resource.
496 /// <since_tizen> 5 </since_tizen>
497 public ImageView.LoadingStatusType LoadingStatus
501 return (ImageView.LoadingStatusType)NDalicManualPINVOKE.View_GetVisualResourceStatus(swigCPtr, (int)Property.IMAGE);
506 /// Enumeration for LoadingStatus of image.
508 /// <since_tizen> 5 </since_tizen>
509 public enum LoadingStatusType
512 /// Loading preparing status.
514 /// <since_tizen> 5 </since_tizen>
517 /// Loading ready status.
519 /// <since_tizen> 5 </since_tizen>
522 /// Loading failed status.
524 /// <since_tizen> 5 </since_tizen>
529 private void UpdateImage()
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));
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));
554 { // just for normal image
555 SetProperty(ImageView.Property.IMAGE, new PropertyValue(_url));
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;