Release 4.0.0-preview1-00301
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ImageView.cs
1 /*
2  * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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
18 namespace Tizen.NUI.BaseComponents
19 {
20     using System;
21     using System.Runtime.InteropServices;
22
23     /// <summary>
24     /// ImageView is a class for displaying an image resource.<br />
25     /// An instance of ImageView can be created using a URL or an image instance.<br />
26     /// </summary>
27     public class ImageView : View
28     {
29         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
30
31         internal ImageView(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.ImageView_SWIGUpcast(cPtr), cMemoryOwn)
32         {
33             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
34         }
35
36         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(ImageView obj)
37         {
38             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
39         }
40
41
42         /// <summary>
43         /// Event arguments of resource ready.
44         /// </summary>
45         public class ResourceReadyEventArgs : EventArgs
46         {
47             private View _view;
48
49             /// <summary>
50             /// The view whose resource is ready.
51             /// </summary>
52             /// <since_tizen> 3 </since_tizen>
53             public View View
54             {
55                 get
56                 {
57                     return _view;
58                 }
59                 set
60                 {
61                     _view = value;
62                 }
63             }
64         }
65
66         private EventHandler<ResourceReadyEventArgs> _resourceReadyEventHandler;
67         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
68         private delegate void ResourceReadyEventCallbackType(IntPtr data);
69         private ResourceReadyEventCallbackType _resourceReadyEventCallback;
70
71         /// <summary>
72         /// An event for ResourceReady signal which can be used to subscribe or unsubscribe the event handler.<br />
73         /// This signal is emitted after all resources required by a control are loaded and ready.<br />
74         /// Most resources are only loaded when the control is placed on the stage.<br />
75         /// </summary>
76         /// <since_tizen> 3 </since_tizen>
77         public event EventHandler<ResourceReadyEventArgs> ResourceReady
78         {
79             add
80             {
81                 if (_resourceReadyEventHandler == null)
82                 {
83                     _resourceReadyEventCallback = OnResourceReady;
84                     ResourceReadySignal(this).Connect(_resourceReadyEventCallback);
85                 }
86
87                 _resourceReadyEventHandler += value;
88             }
89
90             remove
91             {
92                 _resourceReadyEventHandler -= value;
93
94                 if (_resourceReadyEventHandler == null && ResourceReadySignal(this).Empty() == false)
95                 {
96                     ResourceReadySignal(this).Disconnect(_resourceReadyEventCallback);
97                 }
98             }
99         }
100
101         // Callback for View ResourceReady signal
102         private void OnResourceReady(IntPtr data)
103         {
104             ResourceReadyEventArgs e = new ResourceReadyEventArgs();
105             if(data != null)
106             {
107                 e.View = Registry.GetManagedBaseHandleFromNativePtr(data) as View;
108             }
109
110             if (_resourceReadyEventHandler != null)
111             {
112                 _resourceReadyEventHandler(this, e);
113             }
114         }
115
116         /// <summary>
117         /// you can override it to clean-up your own resources.
118         /// </summary>
119         /// <param name="type">DisposeTypes</param>
120         /// <since_tizen> 3 </since_tizen>
121         protected override void Dispose(DisposeTypes type)
122         {
123             if (disposed)
124             {
125                 return;
126             }
127
128             if(type == DisposeTypes.Explicit)
129             {
130                 //Called by User
131                 //Release your own managed resources here.
132                 //You should release all of your own disposable objects here.
133                 _border?.Dispose();
134                 _border = null;
135                 _nPatchMap?.Dispose();
136                 _nPatchMap = null;
137             }
138
139             //Release your own unmanaged resources here.
140             //You should not access any managed member here except static instance.
141             //because the execution order of Finalizes is non-deterministic.
142
143             if (swigCPtr.Handle != global::System.IntPtr.Zero)
144             {
145                 if (swigCMemOwn)
146                 {
147                     swigCMemOwn = false;
148                     NDalicPINVOKE.delete_ImageView(swigCPtr);
149                 }
150                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
151             }
152
153             base.Dispose(type);
154         }
155
156         internal new class Property
157         {
158             internal static readonly int RESOURCE_URL = NDalicPINVOKE.ImageView_Property_RESOURCE_URL_get();
159             internal static readonly int IMAGE = NDalicPINVOKE.ImageView_Property_IMAGE_get();
160             internal static readonly int PRE_MULTIPLIED_ALPHA = NDalicPINVOKE.ImageView_Property_PRE_MULTIPLIED_ALPHA_get();
161             internal static readonly int PIXEL_AREA = NDalicPINVOKE.ImageView_Property_PIXEL_AREA_get();
162         }
163
164         /// <summary>
165         /// Creates an initialized ImageView.
166         /// </summary>
167         /// <since_tizen> 3 </since_tizen>
168         public ImageView() : this(NDalicPINVOKE.ImageView_New__SWIG_0(), true)
169         {
170             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
171
172         }
173         /// <summary>
174         /// Creates an initialized ImageView from a URL to an image resource.<br />
175         /// If the string is empty, ImageView will not display anything.<br />
176         /// </summary>
177         /// <param name="url">The URL of the image resource to display.</param>
178         /// <since_tizen> 3 </since_tizen>
179         public ImageView(string url) : this(NDalicPINVOKE.ImageView_New__SWIG_2(url), true)
180         {
181             _url = url;
182             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
183
184         }
185         internal ImageView(string url, Uint16Pair size) : this(NDalicPINVOKE.ImageView_New__SWIG_3(url, Uint16Pair.getCPtr(size)), true)
186         {
187             _url = url;
188             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
189
190         }
191
192         /// <summary>
193         /// [Obsolete("Please do not use! this will be deprecated")]
194         /// </summary>
195         [Obsolete("Please do not use! this will be deprecated")]
196         public new static ImageView DownCast(BaseHandle handle)
197         {
198             ImageView ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as ImageView;
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200             return ret;
201         }
202         /// <summary>
203         /// Sets this ImageView from the given URL.<br />
204         /// If the URL is empty, ImageView will not display anything.<br />
205         /// </summary>
206         /// <param name="url">The URL to the image resource to display.</param>
207         /// <since_tizen> 3 </since_tizen>
208         public void SetImage(string url)
209         {
210             _url = url;
211             NDalicPINVOKE.ImageView_SetImage__SWIG_1(swigCPtr, url);
212             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
213         }
214         internal void SetImage(string url, Uint16Pair size)
215         {
216             _url = url;
217             NDalicPINVOKE.ImageView_SetImage__SWIG_2(swigCPtr, url, Uint16Pair.getCPtr(size));
218             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
219         }
220
221         internal ViewResourceReadySignal ResourceReadySignal(View view) {
222             ViewResourceReadySignal ret = new ViewResourceReadySignal(NDalicPINVOKE.ResourceReadySignal(View.getCPtr(view)), false);
223             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
224             return ret;
225         }
226
227         /// <summary>
228         /// Queries if all resources required by a control are loaded and ready.<br />
229         /// Most resources are only loaded when the control is placed on the stage.<br />
230         /// True if the resources are loaded and ready, false otherwise.<br />
231         /// </summary>
232         /// <since_tizen> 3 </since_tizen>
233         public new  bool IsResourceReady()
234         {
235             bool ret = NDalicPINVOKE.IsResourceReady(swigCPtr);
236             if (NDalicPINVOKE.SWIGPendingException.Pending)
237                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
238             return ret;
239         }
240
241         /// <summary>
242         /// ImageView ResourceUrl, type string.
243         /// </summary>
244         /// <since_tizen> 3 </since_tizen>
245         public string ResourceUrl
246         {
247             get
248             {
249                 GetProperty(ImageView.Property.RESOURCE_URL).Get(out _url);
250                 return _url;
251             }
252             set
253             {
254                 _url = value;
255                 UpdateImage();
256             }
257         }
258
259         /// <summary>
260         /// ImageView ImageMap, type PropertyMap: string if it is a URL, map otherwise
261         /// </summary>
262         /// <since_tizen> 3 </since_tizen>
263         public PropertyMap ImageMap
264         {
265             get
266             {
267                 if (_border == null)
268                 {
269                     PropertyMap temp = new PropertyMap();
270                     GetProperty(ImageView.Property.IMAGE).Get(temp);
271                     return temp;
272                 }
273                 else
274                 {
275                     return null;
276                 }
277             }
278             set
279             {
280                 if (_border == null)
281                 {
282                     SetProperty(ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(value));
283                 }
284             }
285         }
286
287         /// <summary>
288         /// ImageView PreMultipliedAlpha, type Boolean.<br />
289         /// Image must be initialized.<br />
290         /// </summary>
291         /// <since_tizen> 3 </since_tizen>
292         public bool PreMultipliedAlpha
293         {
294             get
295             {
296                 bool temp = false;
297                 GetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA).Get(out temp);
298                 return temp;
299             }
300             set
301             {
302                 SetProperty(ImageView.Property.PRE_MULTIPLIED_ALPHA, new Tizen.NUI.PropertyValue(value));
303             }
304         }
305
306         /// <summary>
307         /// ImageView PixelArea, type Vector4 (Animatable property).<br />
308         /// Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].<br />
309         /// </summary>
310         /// <since_tizen> 3 </since_tizen>
311         public RelativeVector4 PixelArea
312         {
313             get
314             {
315                 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
316                 GetProperty(ImageView.Property.PIXEL_AREA).Get(temp);
317                 return temp;
318             }
319             set
320             {
321                 SetProperty(ImageView.Property.PIXEL_AREA, new Tizen.NUI.PropertyValue(value));
322             }
323         }
324
325         /// <summary>
326         /// The border of the image in the order: left, right, bottom, top.<br />
327         /// If set, ImageMap will be ignored.<br />
328         /// For N-Patch images only.<br />
329         /// Optional.
330         /// </summary>
331         /// <since_tizen> 3 </since_tizen>
332         public Rectangle Border
333         {
334             get
335             {
336                 return _border;
337             }
338             set
339             {
340                 _border = value;
341                 UpdateImage();
342             }
343         }
344
345         /// <summary>
346         /// Gets or sets whether to draw the borders only (if true).<br />
347         /// If not specified, the default is false.<br />
348         /// For N-Patch images only.<br />
349         /// Optional.
350         /// </summary>
351         /// <since_tizen> 3 </since_tizen>
352         public bool BorderOnly
353         {
354             get
355             {
356                 return _borderOnly ?? false;
357             }
358             set
359             {
360                 _borderOnly = value;
361                 UpdateImage();
362             }
363         }
364
365         /// <summary>
366         /// Gets or sets whether to synchronos loading the resourceurl of image.<br />
367         /// </summary>
368         /// <since_tizen> 3 </since_tizen>
369         public bool SynchronosLoading
370         {
371             get
372             {
373                 return _synchronousLoading ?? false;
374             }
375             set
376             {
377                 _synchronousLoading = value;
378                 UpdateImage();
379             }
380         }
381
382         private void UpdateImage()
383         {
384             if (_url != null)
385             {
386                 if (_border != null)
387                 { // for nine-patch image
388                     _nPatchMap = new PropertyMap();
389                     _nPatchMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.NPatch));
390                     _nPatchMap.Add(NpatchImageVisualProperty.URL, new PropertyValue(_url));
391                     _nPatchMap.Add(NpatchImageVisualProperty.Border, new PropertyValue(_border));
392                     if (_borderOnly != null) { _nPatchMap.Add(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)_borderOnly)); }
393                     if (_synchronousLoading != null) _nPatchMap.Add(NpatchImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
394                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(_nPatchMap));
395                 }
396                 else if(_synchronousLoading != null)
397                 { // for normal image, with synchronous loading property
398                     PropertyMap imageMap = new PropertyMap();
399                     imageMap.Add(Visual.Property.Type, new PropertyValue((int)Visual.Type.Image));
400                     imageMap.Add(ImageVisualProperty.URL, new PropertyValue(_url));
401                     imageMap.Add(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)_synchronousLoading));
402                     SetProperty(ImageView.Property.IMAGE, new PropertyValue(imageMap));
403                 }
404                 else
405                 { // just for normal image
406                     SetProperty(ImageView.Property.RESOURCE_URL, new PropertyValue(_url));
407                 }
408             }
409         }
410
411         private Rectangle _border = null;
412         private PropertyMap _nPatchMap = null;
413         private bool? _synchronousLoading = null;
414         private bool? _borderOnly = null;
415         private string _url = null;
416
417     }
418
419 }