[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EvasImage.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using System.IO;
21 using System.Text;
22
23 namespace ElmSharp
24 {
25     /// <summary>
26     /// This group provides the functions for image objects.
27     /// </summary>
28     /// <since_tizen> preview </since_tizen>
29     public class EvasImage : EvasObject
30     {
31         EvasObject _source = null;
32         IntPtr _handle = IntPtr.Zero;
33
34         /// <summary>
35         /// Creates and initializes a new instance of the EvasImage class.
36         /// </summary>
37         /// <param name="parent">The parent is a given container, which will be attached by EvasImage as a child. It's <see cref="EvasObject"/> type.</param>
38         /// <since_tizen> preview </since_tizen>
39         public EvasImage(EvasObject parent) : base(parent)
40         {
41         }
42
43         internal EvasImage(EvasObject parent, IntPtr handle) : base()
44         {
45             _handle = handle;
46             Realize(parent);
47         }
48
49         /// <summary>
50         /// Sets or gets the source file from where an image object must fetch the real image data.
51         /// </summary>
52         /// <since_tizen> preview </since_tizen>
53         public string File
54         {
55             get
56             {
57                 string file, key;
58                 Interop.Evas.evas_object_image_file_get(RealHandle, out file, out key);
59                 return file;
60             }
61             set
62             {
63                 Interop.Evas.evas_object_image_file_set(RealHandle, value, null);
64             }
65         }
66
67         /// <summary>
68         /// Sets or gets the source object to be visible.
69         /// </summary>
70         /// <since_tizen> preview </since_tizen>
71         public bool IsSourceVisible
72         {
73             get
74             {
75                 return Interop.Evas.evas_object_image_source_visible_get(RealHandle);
76             }
77             set
78             {
79                 Interop.Evas.evas_object_image_source_visible_set(RealHandle, value);
80             }
81         }
82
83         /// <summary>
84         /// Sets or gets whether an object is clipped by the source object's clipper.
85         /// </summary>
86         /// <since_tizen> preview </since_tizen>
87         public bool IsSourceClipped
88         {
89             get
90             {
91                 return Interop.Evas.evas_object_image_source_clip_get(RealHandle);
92             }
93             set
94             {
95                 Interop.Evas.evas_object_image_source_clip_set(RealHandle, value);
96             }
97         }
98
99         /// <summary>
100         /// Sets or gets if the center part of the given image object (not the border) should be drawn.
101         /// </summary>
102         /// <remarks>
103         /// When rendering, the image may be scaled to fit the size of the image object.
104         /// This function sets if the center part of the scaled image is to be drawn or left completely blank, or forced to be solid.
105         /// Very useful for frames and decorations.
106         /// </remarks>
107         /// <since_tizen> preview </since_tizen>
108         public ImageBorderFillMode BorderCenterFillMode
109         {
110             get
111             {
112                 return (ImageBorderFillMode)Interop.Evas.evas_object_image_border_center_fill_get(RealHandle);
113             }
114             set
115             {
116                 Interop.Evas.evas_object_image_border_center_fill_set(RealHandle, (int)value);
117             }
118         }
119
120         /// <summary>
121         /// Sets or gets whether the image object's fill property should track the object's size.
122         /// </summary>
123         /// <since_tizen> preview </since_tizen>
124         public bool IsFilled
125         {
126             get
127             {
128                 return Interop.Evas.evas_object_image_filled_get(RealHandle);
129             }
130             set
131             {
132                 Interop.Evas.evas_object_image_filled_set(RealHandle, value);
133             }
134         }
135
136         /// <summary>
137         /// Sets or gets the scaling factor (multiplier) for the borders of the image object.
138         /// </summary>
139         /// <since_tizen> preview </since_tizen>
140         public double BorderScale
141         {
142             get
143             {
144                 return Interop.Evas.evas_object_image_border_scale_get(RealHandle);
145             }
146             set
147             {
148                 Interop.Evas.evas_object_image_border_scale_set(RealHandle, value);
149             }
150         }
151
152         /// <summary>
153         /// Sets or gets the size of the given image object.
154         /// </summary>
155         /// <since_tizen> preview </since_tizen>
156         public Size Size
157         {
158             get
159             {
160                 int w, h;
161                 Interop.Evas.evas_object_image_size_get(RealHandle, out w, out h);
162                 return new Size(w, h);
163             }
164             set
165             {
166                 Interop.Evas.evas_object_image_size_set(RealHandle, value.Width, value.Height);
167             }
168         }
169
170         /// <summary>
171         /// Gets the row stride of the given image object.
172         /// </summary>
173         /// <since_tizen> preview </since_tizen>
174         public int Stride
175         {
176             get
177             {
178                 return Interop.Evas.evas_object_image_stride_get(RealHandle);
179             }
180         }
181
182         /// <summary>
183         /// Sets or gets whether the alpha channel data is being used on the given image object.
184         /// </summary>
185         /// <since_tizen> preview </since_tizen>
186         public bool IsOpaque
187         {
188             get
189             {
190                 return !Interop.Evas.evas_object_image_alpha_get(RealHandle);
191             }
192             set
193             {
194                 Interop.Evas.evas_object_image_alpha_set(RealHandle, !value);
195             }
196         }
197
198         /// <summary>
199         /// Sets or gets whether to use a high-quality image scaling algorithm on the given image object.
200         /// </summary>
201         /// <since_tizen> preview </since_tizen>
202         public bool IsSmoothScaled
203         {
204             get
205             {
206                 return Interop.Evas.evas_object_image_smooth_scale_get(RealHandle);
207             }
208             set
209             {
210                 Interop.Evas.evas_object_image_smooth_scale_set(RealHandle, value);
211             }
212         }
213
214         /// <summary>
215         /// Sets how to fill an image object's drawing rectangle given the (real) image bound to it.
216         /// </summary>
217         /// <param name="geometry">The rectangle of the given image object that the image will be drawn to.</param>
218         /// <since_tizen> preview </since_tizen>
219         public void SetFill(Rect geometry)
220         {
221             Interop.Evas.evas_object_image_fill_set(RealHandle, geometry.X, geometry.Y, geometry.Width, geometry.Height);
222         }
223
224         /// <summary>
225         /// Sets the source file from where an image object must fetch the real image data (it may be an Eet file, besides pure image ones).
226         /// </summary>
227         /// <param name="file">The image file path.</param>
228         /// <param name="key">The image key in file (if its an Eet one), otherwise set null.</param>
229         /// <since_tizen> preview </since_tizen>
230         public void SetFile(string file, string key)
231         {
232             Interop.Evas.evas_object_image_file_set(RealHandle, file, key);
233         }
234
235         /// <summary>
236         /// Sets the data for an image from the memory to be loaded.
237         /// </summary>
238         /// <param name="stream">memory stream</param>
239         /// <since_tizen> preview </since_tizen>
240         public void SetStream(Stream stream)
241         {
242             if (stream == null)
243                 throw new ArgumentNullException("stream");
244
245             MemoryStream memstream = new MemoryStream();
246             stream.CopyTo(memstream);
247             unsafe
248             {
249                 byte[] dataArr = memstream.ToArray();
250                 fixed (byte* data = &dataArr[0])
251                 {
252                     Interop.Evas.evas_object_image_memfile_set(RealHandle, data, dataArr.Length, IntPtr.Zero, IntPtr.Zero);
253                 }
254             }
255             memstream.Dispose();
256         }
257
258         /// <summary>
259         /// Sets the source object on an image object to be used as a proxy.
260         /// </summary>
261         /// <param name="source">The proxy (image) object.</param>
262         /// <returns>true if the source object is set successfully, otherwise false on error.</returns>
263         /// <since_tizen> preview </since_tizen>
264         public bool SetSource(EvasObject source)
265         {
266             bool result = false;
267             _source = source;
268             result = Interop.Evas.evas_object_image_source_set(RealHandle, IntPtr.Zero);
269             if (source != null)
270                 result = result && Interop.Evas.evas_object_image_source_set(RealHandle, source.Handle);
271             return result;
272         }
273
274         /// <summary>
275         /// Set the native surface of a given image of the canvas.
276         /// </summary>
277         /// <param name="surface">The surface.</param>
278         /// <since_tizen> preview </since_tizen>
279         [EditorBrowsable(EditorBrowsableState.Never)]
280         public void SetNativeSurface(IntPtr surface)
281         {
282             Interop.Evas.evas_object_image_native_surface_set(RealHandle, surface);
283         }
284
285         /// <summary>
286         /// Sets the dimensions for an image object's border, a region which is not scaled together with its center ever.
287         /// </summary>
288         /// <param name="left">The border's left width.</param>
289         /// <param name="right">The border's right width.</param>
290         /// <param name="top">The border's top width.</param>
291         /// <param name="bottom">The border's bottom width.</param>
292         /// <since_tizen> preview </since_tizen>
293         public void SetBorder(int left, int right, int top, int bottom)
294         {
295             Interop.Evas.evas_object_image_border_set(RealHandle, left, right, top, bottom);
296         }
297
298         /// <summary>
299         /// Save the given image object's contents to an (image) file.
300         ///
301         /// The extension suffix on file will determine which saver module Evas is to use when saving, thus the final file's format. If the file supports multiple data stored in it (Eet ones), you can specify the key to be used as the index of the image in it.
302         ///
303         /// You can specify some flags when saving the image.Currently acceptable flags are quality and compress.Eg.: "quality=100 compress=9" 
304         /// </summary>
305         /// <param name="file">The filename to be used to save the image (extension obligatory).</param>
306         /// <param name="key">The image key in the file (if an Eet one), or null, otherwise.</param>
307         /// <param name="flags">String containing the flags to be used (null for none).</param>
308         public void Save(string file, string key, string flags)
309         {
310             Interop.Evas.evas_object_image_save(RealHandle, file, key, flags);
311         }
312
313         /// <summary>
314         /// Sets the content at a part of a given container widget.
315         /// </summary>
316         /// <param name="parent">The parent is a given container, which will be attached by the image as a child. It's <see cref="EvasObject"/> type.</param>
317         /// <returns>The new object, otherwise null if it cannot be created.</returns>
318         /// <since_tizen> preview </since_tizen>
319         protected override IntPtr CreateHandle(EvasObject parent)
320         {
321             return _handle != IntPtr.Zero ? _handle : Interop.Evas.evas_object_image_add(Interop.Evas.evas_object_evas_get(parent.Handle));
322         }
323     }
324 }