2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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.
19 using System.Threading;
20 using System.Threading.Tasks;
25 /// The Image is a widget that allows one to load and display an image file on it,
26 /// be it from a disk file or from a memory region.
29 public class Image : Widget
31 bool _canScaleUp = true;
32 bool _canScaleDown = true;
34 Color _color = Color.Default;
36 EvasImage _imageObject = null;
39 /// Creates and initializes a new instance of Image class.
41 /// <param name="parent">The parent is a given container which will be attached by Image as a child. It's <see cref="EvasObject"/> type.</param>
42 public Image(EvasObject parent) : base(parent)
44 _clicked = new SmartEvent(this, "clicked");
45 _clicked.On += (s, e) => Clicked?.Invoke(this, EventArgs.Empty);
49 /// Clicked will be triggered when the image is clicked.
51 public event EventHandler Clicked;
54 /// LoadingCompleted will be triggered when the image is loaded completely.
56 public event EventHandler LoadingCompleted;
59 /// Clicked will be triggered when the image is fail to load.
61 public event EventHandler LoadingFailed;
64 /// Gets the file that is used as an image.
70 return Interop.Elementary.elm_image_file_get(RealHandle);
75 /// Sets or gets the smooth effect for an image.
81 return Interop.Elementary.elm_image_smooth_get(RealHandle);
85 Interop.Elementary.elm_image_smooth_set(RealHandle, value);
90 /// Sets or gets whether scaling is disabled on the object.
96 return !Interop.Elementary.elm_image_no_scale_get(RealHandle);
100 Interop.Elementary.elm_image_no_scale_set(RealHandle, !value);
105 /// Sets or gets whether the object is down resizeable.
107 public bool CanScaleDown
111 return _canScaleDown;
115 _canScaleDown = value;
116 Interop.Elementary.elm_image_resizable_set(RealHandle, _canScaleUp, _canScaleDown);
121 /// Sets or gets whether the object is up resizeable.
123 public bool CanScaleUp
132 Interop.Elementary.elm_image_resizable_set(RealHandle, _canScaleUp, _canScaleDown);
137 /// Sets or gets whether the image fills the entire object area, when keeping the aspect ratio.
139 public bool CanFillOutside
143 return Interop.Elementary.elm_image_fill_outside_get(RealHandle);
147 Interop.Elementary.elm_image_fill_outside_set(RealHandle, value);
152 /// Sets or gets the prescale size for the image.
154 public int PrescaleSize
158 return Interop.Elementary.elm_image_prescale_get(RealHandle);
162 Interop.Elementary.elm_image_prescale_set(RealHandle, value);
167 /// Sets or gets whether the original aspect ratio of the image should be kept on resize.
169 public bool IsFixedAspect
173 return Interop.Elementary.elm_image_aspect_fixed_get(RealHandle);
177 Interop.Elementary.elm_image_aspect_fixed_set(RealHandle, value);
182 /// Sets or gets whether an image object (which supports animation) is to animate itself.
184 public bool IsAnimated
188 return Interop.Elementary.elm_image_animated_get(RealHandle);
192 Interop.Elementary.elm_image_animated_set(RealHandle, value);
197 /// Gets whether an image object supports animation.
199 public bool IsAnimatedAvailable
203 return Interop.Elementary.elm_image_animated_available_get(RealHandle);
208 /// Sets or gets whether an image object is under animation.
211 /// An image object, even if it supports animation, will be displayed by default without animation.
212 /// To actually start playing any image object's animation, <see cref="IsAnimated"/> should be TRUE before setting this property true.
214 public bool IsAnimationPlaying
218 return Interop.Elementary.elm_image_animated_play_get(RealHandle);
222 Interop.Elementary.elm_image_animated_play_set(RealHandle, value);
227 /// Sets or gets whether the image is 'editable'.
229 public bool IsEditable
233 return Interop.Elementary.elm_image_editable_get(RealHandle);
237 Interop.Elementary.elm_image_editable_set(RealHandle, value);
242 /// Gets the current size of the image.
244 public Size ObjectSize
248 Interop.Elementary.elm_image_object_size_get(RealHandle, out int w, out int h);
249 return new Size(w, h);
254 /// Sets or gets whether alpha channel data is being used on the given image object.
260 if (ImageObject != null)
262 return ImageObject.IsOpaque;
268 if (ImageObject != null)
270 ImageObject.IsOpaque = value;
276 /// Sets or gets the image orientation.
278 public ImageOrientation Orientation
282 return (ImageOrientation)Interop.Elementary.elm_image_orient_get(RealHandle);
286 Interop.Elementary.elm_image_orient_set(RealHandle, (int)value);
291 /// Sets or gets the image color
293 public override Color Color
301 if (ImageObject != null)
305 ImageObject.Color = Color.FromRgba(255, 255, 255, 255);
309 ImageObject.Color = value;
317 /// Sets the background color
319 public override Color BackgroundColor
325 SetPartColor("bg", Color.Transparent);
329 SetPartColor("bg", value);
331 _backgroundColor = value;
335 public EvasImage ImageObject
339 if (_imageObject == null)
341 IntPtr evasObj = Interop.Elementary.elm_image_object_get(RealHandle);
342 if (evasObj != IntPtr.Zero)
343 _imageObject = new EvasImage(this, evasObj);
350 /// Sets the dimensions for an image object's border, a region which is not scaled together with its center ever.
352 /// <param name="left">The border's left width</param>
353 /// <param name="right">The border's right width</param>
354 /// <param name="top">The border's top width</param>
355 /// <param name="bottom">The border's bottom width</param>
356 public void SetBorder(int left, int right, int top, int bottom)
358 ImageObject?.SetBorder(left, right, top, bottom);
362 /// Sets or gets if the center part of the given image object (not the border) should be drawn.
365 /// When rendering, the image may be scaled to fit the size of the image object.
366 /// This function sets if the center part of the scaled image is to be drawn or left completely blank, or forced to be solid.
367 /// Very useful for frames and decorations.
369 public ImageBorderFillMode BorderCenterFillMode
373 if (ImageObject != null)
375 return ImageObject.BorderCenterFillMode;
379 return default(ImageBorderFillMode);
385 if (ImageObject != null)
387 ImageObject.BorderCenterFillMode = value;
393 /// Sets the file that is used as the image's source.
395 /// <param name="file">The path to the file that is used as an image source</param>
396 /// <returns>(true = success, false = error)</returns>
397 public bool Load(string file)
400 throw new ArgumentNullException("file");
402 Interop.Elementary.elm_image_async_open_set(RealHandle, false);
403 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, true);
404 return Interop.Elementary.elm_image_file_set(RealHandle, file, null);
408 /// Sets the uri that is used as the image's source.
410 /// <param name="uri">The uri to the file that is used as an image source</param>
411 /// <returns>(true = success, false = error)</returns>
412 public bool Load(Uri uri)
415 throw new ArgumentNullException("uri");
417 return Load(uri.IsFile ? uri.LocalPath : uri.AbsoluteUri);
421 /// Sets a location in the memory to be used as an image object's source bitmap.
424 /// This function is handy when the contents of an image file are mapped into the memory, for example.
425 /// The format string should be something like "png", "jpg", "tga", "tiff", "bmp" etc, when provided (null, on the contrary).
426 /// This improves the loader performance as it tries the "correct" loader first, before trying a range of other possible loaders until one succeeds.
428 /// <param name="img">The binary data that is used as an image source</param>
429 /// <param name="size">The size of the binary data blob img</param>
430 /// <returns>(true = success, false = error)</returns>
431 [Obsolete("This method will be removed. Use Load(Stream stream) instead.")]
432 public unsafe bool Load(byte* img, long size)
435 throw new ArgumentNullException("img");
437 Interop.Elementary.elm_image_async_open_set(RealHandle, false);
438 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, true);
439 return Interop.Elementary.elm_image_memfile_set(RealHandle, img, size, IntPtr.Zero, IntPtr.Zero);
443 /// Sets the stream that is used as the image's source.
445 /// <param name="stream">The stream that is used as an image source</param>
446 /// <returns>(true = success, false = error)</returns>
447 public bool Load(Stream stream)
450 throw new ArgumentNullException("stream");
452 Interop.Elementary.elm_image_async_open_set(RealHandle, false);
453 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, true);
454 MemoryStream memstream = new MemoryStream();
455 stream.CopyTo(memstream);
458 byte[] dataArr = memstream.ToArray();
459 fixed (byte* data = &dataArr[0])
461 return Interop.Elementary.elm_image_memfile_set(RealHandle, data, dataArr.Length, IntPtr.Zero, IntPtr.Zero);
467 /// Sets the file that is used as the image's source with async.
469 /// <param name="file">The path to the file that is used as an image source</param>
470 /// <param name="cancellationToken">cancellation token</param>
471 /// <returns>(true = success, false = error)</returns>
472 public Task<bool> LoadAsync(string file, CancellationToken cancellationToken = default(CancellationToken))
475 throw new ArgumentNullException("file");
477 Interop.Elementary.elm_image_async_open_set(RealHandle, true);
478 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, false);
480 var tcs = new TaskCompletionSource<bool>();
482 cancellationToken.Register(() =>
484 if (tcs != null && !tcs.Task.IsCompleted)
490 SmartEvent loadReady = new SmartEvent(this, RealHandle, "load,ready");
491 loadReady.On += (s, e) =>
494 LoadingCompleted?.Invoke(this, EventArgs.Empty);
495 if (tcs != null && !tcs.Task.IsCompleted)
501 SmartEvent loadError = new SmartEvent(this, RealHandle, "load,error");
502 loadError.On += (s, e) =>
505 LoadingFailed?.Invoke(this, EventArgs.Empty);
506 if (tcs != null && !tcs.Task.IsCompleted)
508 tcs.SetResult(false);
512 bool ret = Interop.Elementary.elm_image_file_set(RealHandle, file, null);
515 throw new InvalidOperationException("Failed to set file to Image");
522 /// Sets the uri that is used as the image's source with async.
524 /// <param name="uri">The uri to the file that is used as an image source</param>
525 /// <param name="cancellationToken">cancellation token</param>
526 /// <returns>(true = success, false = error)</returns>
527 public Task<bool> LoadAsync(Uri uri, CancellationToken cancellationToken = default(CancellationToken))
530 throw new ArgumentNullException("uri");
532 return LoadAsync(uri.IsFile ? uri.LocalPath : uri.AbsoluteUri, cancellationToken);
536 /// Sets the stream that is used as the image's source with async.
538 /// <param name="stream">The stream that is used as an image source</param>
539 /// <param name="cancellationToken">cancellation token</param>
540 /// <returns>(true = success, false = error)</returns>
541 public async Task<bool> LoadAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
544 throw new ArgumentNullException("stream");
546 Interop.Elementary.elm_image_async_open_set(RealHandle, true);
547 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, false);
549 var tcs = new TaskCompletionSource<bool>();
551 cancellationToken.Register(() =>
553 if (tcs != null && !tcs.Task.IsCompleted)
559 SmartEvent loadReady = new SmartEvent(this, RealHandle, "load,ready");
560 loadReady.On += (s, e) =>
563 LoadingCompleted?.Invoke(this, EventArgs.Empty);
564 if (tcs != null && !tcs.Task.IsCompleted)
570 SmartEvent loadError = new SmartEvent(this, RealHandle, "load,error");
571 loadError.On += (s, e) =>
574 LoadingFailed?.Invoke(this, EventArgs.Empty);
575 if (tcs != null && !tcs.Task.IsCompleted)
577 tcs.SetResult(false);
581 MemoryStream memstream = new MemoryStream();
582 await stream.CopyToAsync(memstream);
586 byte[] dataArr = memstream.ToArray();
587 fixed (byte* data = &dataArr[0])
589 bool ret = Interop.Elementary.elm_image_memfile_set(RealHandle, data, dataArr.Length, IntPtr.Zero, IntPtr.Zero);
597 return await tcs.Task;
601 /// Sets the color of color class for a given widget.
603 /// <param name="part">The name of color class.</param>
604 /// <param name="color">The struct of color</param>
605 public override void SetPartColor(string part, Color color)
607 Interop.Elementary.elm_object_color_class_color_set(Handle, part, color.R * color.A / 255,
608 color.G * color.A / 255,
609 color.B * color.A / 255,
614 /// Gets the color of color class for a given widget.
616 /// <param name="part">The name of color class.</param>
617 /// <returns>color object</returns>
618 public override Color GetPartColor(string part)
620 Interop.Elementary.elm_object_color_class_color_get(Handle, part, out int r, out int g, out int b, out int a);
621 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
625 /// Sets the content at a part of a given container widget.
627 /// <param name="parent">The parent is a given container which will be attached by Image as a child. It's <see cref="EvasObject"/> type.</param>
628 /// <returns>The new object, otherwise null if it cannot be created</returns>
629 protected override IntPtr CreateHandle(EvasObject parent)
631 IntPtr handle = Interop.Elementary.elm_layout_add(parent);
632 Interop.Elementary.elm_layout_theme_set(handle, "layout", "background", "default");
634 RealHandle = Interop.Elementary.elm_image_add(handle);
635 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
642 /// Enumeration for the fill mode of image border
644 public enum ImageBorderFillMode
647 /// None mode of image border
652 /// Default mode of image border
657 /// Solid mode of image border
663 /// Enumeration for the possible orientation options
665 public enum ImageOrientation : int
668 /// No orientation change
673 /// Rotate 90 degrees clockwise
678 /// Rotate 180 degrees clockwise
683 /// Rotate 90 degrees counter-clockwise (i.e. 270 degrees clockwise)
688 /// Flip image horizontally
693 /// Flip image vertically
698 /// Flip the image along the y = (width - x) line (bottom-left to top-right)
703 /// Flip the image along the y = x line (top-left to bottom-right)