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;
336 /// Gets the inlined image object of the image widget.
337 /// This property allows one to get the underlying EvasObject of type Image from this elementary widget. It can be useful to do things like save the image to a file, etc.
339 /// <remarks>Be careful to not manipulate it, as it is under the control of widget.</remarks>
340 public EvasImage ImageObject
344 if (_imageObject == null)
346 IntPtr evasObj = Interop.Elementary.elm_image_object_get(RealHandle);
347 if (evasObj != IntPtr.Zero)
348 _imageObject = new EvasImage(this, evasObj);
355 /// Sets the dimensions for an image object's border, a region which is not scaled together with its center ever.
357 /// <param name="left">The border's left width</param>
358 /// <param name="right">The border's right width</param>
359 /// <param name="top">The border's top width</param>
360 /// <param name="bottom">The border's bottom width</param>
361 public void SetBorder(int left, int right, int top, int bottom)
363 ImageObject?.SetBorder(left, right, top, bottom);
367 /// Sets or gets if the center part of the given image object (not the border) should be drawn.
370 /// When rendering, the image may be scaled to fit the size of the image object.
371 /// This function sets if the center part of the scaled image is to be drawn or left completely blank, or forced to be solid.
372 /// Very useful for frames and decorations.
374 public ImageBorderFillMode BorderCenterFillMode
378 if (ImageObject != null)
380 return ImageObject.BorderCenterFillMode;
384 return default(ImageBorderFillMode);
390 if (ImageObject != null)
392 ImageObject.BorderCenterFillMode = value;
398 /// Sets the file that is used as the image's source.
400 /// <param name="file">The path to the file that is used as an image source</param>
401 /// <returns>(true = success, false = error)</returns>
402 public bool Load(string file)
405 throw new ArgumentNullException("file");
407 Interop.Elementary.elm_image_async_open_set(RealHandle, false);
408 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, true);
409 return Interop.Elementary.elm_image_file_set(RealHandle, file, null);
413 /// Sets the uri that is used as the image's source.
415 /// <param name="uri">The uri to the file that is used as an image source</param>
416 /// <returns>(true = success, false = error)</returns>
417 public bool Load(Uri uri)
420 throw new ArgumentNullException("uri");
422 return Load(uri.IsFile ? uri.LocalPath : uri.AbsoluteUri);
426 /// Sets a location in the memory to be used as an image object's source bitmap.
429 /// This function is handy when the contents of an image file are mapped into the memory, for example.
430 /// The format string should be something like "png", "jpg", "tga", "tiff", "bmp" etc, when provided (null, on the contrary).
431 /// This improves the loader performance as it tries the "correct" loader first, before trying a range of other possible loaders until one succeeds.
433 /// <param name="img">The binary data that is used as an image source</param>
434 /// <param name="size">The size of the binary data blob img</param>
435 /// <returns>(true = success, false = error)</returns>
436 [Obsolete("This method will be removed. Use Load(Stream stream) instead.")]
437 public unsafe bool Load(byte* img, long size)
440 throw new ArgumentNullException("img");
442 Interop.Elementary.elm_image_async_open_set(RealHandle, false);
443 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, true);
444 return Interop.Elementary.elm_image_memfile_set(RealHandle, img, size, IntPtr.Zero, IntPtr.Zero);
448 /// Sets the stream that is used as the image's source.
450 /// <param name="stream">The stream that is used as an image source</param>
451 /// <returns>(true = success, false = error)</returns>
452 public bool Load(Stream stream)
455 throw new ArgumentNullException("stream");
457 Interop.Elementary.elm_image_async_open_set(RealHandle, false);
458 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, true);
459 MemoryStream memstream = new MemoryStream();
460 stream.CopyTo(memstream);
463 byte[] dataArr = memstream.ToArray();
464 fixed (byte* data = &dataArr[0])
466 return Interop.Elementary.elm_image_memfile_set(RealHandle, data, dataArr.Length, IntPtr.Zero, IntPtr.Zero);
472 /// Sets the file that is used as the image's source with async.
474 /// <param name="file">The path to the file that is used as an image source</param>
475 /// <param name="cancellationToken">cancellation token</param>
476 /// <returns>(true = success, false = error)</returns>
477 public Task<bool> LoadAsync(string file, CancellationToken cancellationToken = default(CancellationToken))
480 throw new ArgumentNullException("file");
482 Interop.Elementary.elm_image_async_open_set(RealHandle, true);
483 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, false);
485 var tcs = new TaskCompletionSource<bool>();
487 cancellationToken.Register(() =>
489 if (tcs != null && !tcs.Task.IsCompleted)
495 SmartEvent loadReady = new SmartEvent(this, RealHandle, "load,ready");
496 loadReady.On += (s, e) =>
499 LoadingCompleted?.Invoke(this, EventArgs.Empty);
500 if (tcs != null && !tcs.Task.IsCompleted)
506 SmartEvent loadError = new SmartEvent(this, RealHandle, "load,error");
507 loadError.On += (s, e) =>
510 LoadingFailed?.Invoke(this, EventArgs.Empty);
511 if (tcs != null && !tcs.Task.IsCompleted)
513 tcs.SetResult(false);
517 bool ret = Interop.Elementary.elm_image_file_set(RealHandle, file, null);
520 throw new InvalidOperationException("Failed to set file to Image");
527 /// Sets the uri that is used as the image's source with async.
529 /// <param name="uri">The uri to the file that is used as an image source</param>
530 /// <param name="cancellationToken">cancellation token</param>
531 /// <returns>(true = success, false = error)</returns>
532 public Task<bool> LoadAsync(Uri uri, CancellationToken cancellationToken = default(CancellationToken))
535 throw new ArgumentNullException("uri");
537 return LoadAsync(uri.IsFile ? uri.LocalPath : uri.AbsoluteUri, cancellationToken);
541 /// Sets the stream that is used as the image's source with async.
543 /// <param name="stream">The stream that is used as an image source</param>
544 /// <param name="cancellationToken">cancellation token</param>
545 /// <returns>(true = success, false = error)</returns>
546 public async Task<bool> LoadAsync(Stream stream, CancellationToken cancellationToken = default(CancellationToken))
549 throw new ArgumentNullException("stream");
551 Interop.Elementary.elm_image_async_open_set(RealHandle, true);
552 Interop.Elementary.elm_image_preload_disabled_set(RealHandle, false);
554 var tcs = new TaskCompletionSource<bool>();
556 cancellationToken.Register(() =>
558 if (tcs != null && !tcs.Task.IsCompleted)
564 SmartEvent loadReady = new SmartEvent(this, RealHandle, "load,ready");
565 loadReady.On += (s, e) =>
568 LoadingCompleted?.Invoke(this, EventArgs.Empty);
569 if (tcs != null && !tcs.Task.IsCompleted)
575 SmartEvent loadError = new SmartEvent(this, RealHandle, "load,error");
576 loadError.On += (s, e) =>
579 LoadingFailed?.Invoke(this, EventArgs.Empty);
580 if (tcs != null && !tcs.Task.IsCompleted)
582 tcs.SetResult(false);
586 MemoryStream memstream = new MemoryStream();
587 await stream.CopyToAsync(memstream);
591 byte[] dataArr = memstream.ToArray();
592 fixed (byte* data = &dataArr[0])
594 bool ret = Interop.Elementary.elm_image_memfile_set(RealHandle, data, dataArr.Length, IntPtr.Zero, IntPtr.Zero);
604 return await tcs.Task;
608 /// Sets the color of color class for a given widget.
610 /// <param name="part">The name of color class.</param>
611 /// <param name="color">The struct of color</param>
612 public override void SetPartColor(string part, Color color)
614 Interop.Elementary.elm_object_color_class_color_set(Handle, part, color.R * color.A / 255,
615 color.G * color.A / 255,
616 color.B * color.A / 255,
621 /// Gets the color of color class for a given widget.
623 /// <param name="part">The name of color class.</param>
624 /// <returns>color object</returns>
625 public override Color GetPartColor(string part)
627 Interop.Elementary.elm_object_color_class_color_get(Handle, part, out int r, out int g, out int b, out int a);
628 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
632 /// Sets the content at a part of a given container widget.
634 /// <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>
635 /// <returns>The new object, otherwise null if it cannot be created</returns>
636 protected override IntPtr CreateHandle(EvasObject parent)
638 IntPtr handle = Interop.Elementary.elm_layout_add(parent);
639 Interop.Elementary.elm_layout_theme_set(handle, "layout", "background", "default");
641 RealHandle = Interop.Elementary.elm_image_add(handle);
642 Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
649 /// Enumeration for the fill mode of image border
651 public enum ImageBorderFillMode
654 /// None mode of image border
659 /// Default mode of image border
664 /// Solid mode of image border
670 /// Enumeration for the possible orientation options
672 public enum ImageOrientation : int
675 /// No orientation change
680 /// Rotate 90 degrees clockwise
685 /// Rotate 180 degrees clockwise
690 /// Rotate 90 degrees counter-clockwise (i.e. 270 degrees clockwise)
695 /// Flip image horizontally
700 /// Flip image vertically
705 /// Flip the image along the y = (width - x) line (bottom-left to top-right)
710 /// Flip the image along the y = x line (top-left to bottom-right)