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