[NUI] Reduce code duplication - refactor dispose codes (#1010)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / PropertyNotification.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 using System;
19 using System.Runtime.InteropServices;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI
23 {
24
25
26     ///<summary>
27     /// Issues a notification upon a condition of the property being met.
28     /// See PropertyCondition for available defined conditions.
29     ///</summary>
30     /// <since_tizen> 4 </since_tizen>
31     public class PropertyNotification : BaseHandle
32     {
33         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
34
35         private DaliEventHandler<object, NotifyEventArgs> _propertyNotificationNotifyEventHandler;
36         private NotifyEventCallbackDelegate _propertyNotificationNotifyEventCallbackDelegate;
37
38         /// <summary>
39         /// Create a instance of PropertyNotification.
40         /// </summary>
41         /// <since_tizen> 4 </since_tizen>
42         public PropertyNotification() : this(Interop.PropertyNotification.new_PropertyNotification__SWIG_0(), true)
43         {
44             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
45         }
46
47         /// <summary>
48         /// Create a instance of PropertyNotification.
49         /// </summary>
50         /// <since_tizen> 4 </since_tizen>
51         public PropertyNotification(PropertyNotification handle) : this(Interop.PropertyNotification.new_PropertyNotification__SWIG_1(PropertyNotification.getCPtr(handle)), true)
52         {
53             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
54         }
55
56         internal PropertyNotification(global::System.IntPtr cPtr, bool cMemoryOwn) : base(Interop.PropertyNotification.PropertyNotification_SWIGUpcast(cPtr), cMemoryOwn)
57         {
58             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
59         }
60
61         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
62         private delegate void NotifyEventCallbackDelegate(IntPtr propertyNotification);
63
64         ///<summary>
65         /// Event for Notified signal which can be used to subscribe/unsubscribe the event handler
66         /// Notified signal is emitted when the notification upon a condition of the property being met, has occurred.
67         ///</summary>
68         /// <since_tizen> 4 </since_tizen>
69         public event DaliEventHandler<object, NotifyEventArgs> Notified
70         {
71             add
72             {
73                 lock (this)
74                 {
75                     // Restricted to only one listener
76                     if (_propertyNotificationNotifyEventHandler == null)
77                     {
78                         _propertyNotificationNotifyEventHandler += value;
79
80                         _propertyNotificationNotifyEventCallbackDelegate = new NotifyEventCallbackDelegate(OnPropertyNotificationNotify);
81                         this.NotifySignal().Connect(_propertyNotificationNotifyEventCallbackDelegate);
82                     }
83                 }
84             }
85
86             remove
87             {
88                 lock (this)
89                 {
90                     if (_propertyNotificationNotifyEventHandler != null)
91                     {
92                         this.NotifySignal().Disconnect(_propertyNotificationNotifyEventCallbackDelegate);
93                     }
94
95                     _propertyNotificationNotifyEventHandler -= value;
96                 }
97             }
98         }
99
100         /// <summary>
101         /// Enumeration for description of how to check condition.
102         /// </summary>
103         /// <since_tizen> 3 </since_tizen>
104         public enum NotifyMode
105         {
106             /// <summary>
107             /// Don't notify, regardless of result of Condition
108             /// </summary>
109             /// <since_tizen> 3 </since_tizen>
110             Disabled,
111             /// <summary>
112             /// Notify whenever condition changes from false to true.
113             /// </summary>
114             /// <since_tizen> 3 </since_tizen>
115             NotifyOnTrue,
116             /// <summary>
117             /// Notify whenever condition changes from true to false.
118             /// </summary>
119             /// <since_tizen> 3 </since_tizen>
120             NotifyOnFalse,
121             /// <summary>
122             /// Notify whenever condition changes (false to true, and true to false)
123             /// </summary>
124             /// <since_tizen> 3 </since_tizen>
125             NotifyOnChanged
126         }
127
128         /// <summary>
129         /// Get property notification from Intptr.<br/>
130         /// This should be internal, please do not use.
131         /// </summary>
132         /// <param name="cPtr">An object of IntPtr type.</param>
133         /// <returns>An object of the PropertyNotification type.</returns>
134         /// <since_tizen> 4 </since_tizen>
135         public static PropertyNotification GetPropertyNotificationFromPtr(global::System.IntPtr cPtr)
136         {
137             PropertyNotification ret = new PropertyNotification(cPtr, false);
138             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
139             return ret;
140         }
141
142         /// <summary>
143         /// Downcast a PropertyNotification instance.
144         /// </summary>
145         /// <param name="handle">Handle to an object of BaseHandle type.</param>
146         /// <returns>Handle to an object of the PropertyNotification type.</returns>
147         /// <since_tizen> 4 </since_tizen>
148         public static PropertyNotification DownCast(BaseHandle handle)
149         {
150             PropertyNotification ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as PropertyNotification;
151             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
152             return ret;
153         }
154
155         /// <summary>
156         /// Assign.
157         /// </summary>
158         /// <param name="rhs">A reference to the copied handle.</param>
159         /// <returns>A reference to this.</returns>
160         /// <since_tizen> 4 </since_tizen>
161         public PropertyNotification Assign(PropertyNotification rhs)
162         {
163             PropertyNotification ret = new PropertyNotification(Interop.PropertyNotification.PropertyNotification_Assign(swigCPtr, PropertyNotification.getCPtr(rhs)), false);
164             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
165             return ret;
166         }
167
168         /// <summary>
169         /// Gets the condition of this notification.
170         /// </summary>
171         /// <returns>The condition is returned.</returns>
172         /// <since_tizen> 4 </since_tizen>
173         public PropertyCondition GetCondition()
174         {
175             PropertyCondition ret = new PropertyCondition(Interop.PropertyNotification.PropertyNotification_GetCondition__SWIG_0(swigCPtr), true);
176             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
177             return ret;
178         }
179
180         /// <summary>
181         /// Gets the target handle that this notification is observing.
182         /// </summary>
183         /// <since_tizen> 4 </since_tizen>
184         public Animatable GetTarget()
185         {
186             Animatable ret = new Animatable(Interop.PropertyNotification.PropertyNotification_GetTarget(swigCPtr), true);
187             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
188             return ret;
189         }
190
191         /// <summary>
192         /// Gets the target handle's property index that this notification.
193         /// </summary>
194         /// <returns>The target property index.</returns>
195         /// <since_tizen> 4 </since_tizen>
196         public int GetTargetProperty()
197         {
198             int ret = Interop.PropertyNotification.PropertyNotification_GetTargetProperty(swigCPtr);
199             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
200             return ret;
201         }
202
203         /// <summary>
204         /// Sets the Notification mode.
205         /// </summary>
206         /// <param name="mode">mode Notification mode (Default is PropertyNotification::NotifyOnTrue).</param>
207         /// <since_tizen> 4 </since_tizen>
208         public void SetNotifyMode(PropertyNotification.NotifyMode mode)
209         {
210             Interop.PropertyNotification.PropertyNotification_SetNotifyMode(swigCPtr, (int)mode);
211             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
212         }
213
214         /// <summary>
215         /// Retrieves the current Notification mode.
216         /// </summary>
217         /// <returns>Notification mode.</returns>
218         /// <since_tizen> 4 </since_tizen>
219         public PropertyNotification.NotifyMode GetNotifyMode()
220         {
221             PropertyNotification.NotifyMode ret = (PropertyNotification.NotifyMode)Interop.PropertyNotification.PropertyNotification_GetNotifyMode(swigCPtr);
222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
223             return ret;
224         }
225
226         /// <summary>
227         /// Gets the result of the last condition check that caused a signal emit,
228         /// useful when using NotifyOnChanged mode and need to know what it changed to.
229         /// </summary>
230         /// <since_tizen> 4 </since_tizen>
231         public bool GetNotifyResult()
232         {
233             bool ret = Interop.PropertyNotification.PropertyNotification_GetNotifyResult(swigCPtr);
234             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
235             return ret;
236         }
237
238         /// <summary>
239         /// Connects to this signal to be notified when the notification has occurred.
240         /// </summary>
241         /// <returns>A signal object to Connect() with</returns>
242         /// <since_tizen> 4 </since_tizen>
243         public PropertyNotifySignal NotifySignal()
244         {
245             PropertyNotifySignal ret = new PropertyNotifySignal(Interop.PropertyNotification.PropertyNotification_NotifySignal(swigCPtr), false);
246             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
247             return ret;
248         }
249
250         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(PropertyNotification obj)
251         {
252             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
253         }
254
255         /// <summary>
256         /// Dispose.
257         /// </summary>
258         /// <since_tizen> 4 </since_tizen>
259         protected override void Dispose(DisposeTypes type)
260         {
261             if (disposed)
262             {
263                 return;
264             }
265
266             //Release your own unmanaged resources here.
267             //You should not access any managed member here except static instance.
268             //because the execution order of Finalizes is non-deterministic.
269
270             if (swigCPtr.Handle != global::System.IntPtr.Zero)
271             {
272                 if (swigCMemOwn)
273                 {
274                     swigCMemOwn = false;
275                     Interop.PropertyNotification.delete_PropertyNotification(swigCPtr);
276                 }
277                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
278             }
279
280             base.Dispose(type);
281         }
282
283         // Callback for PropertyNotification NotifySignal
284         private void OnPropertyNotificationNotify(IntPtr propertyNotification)
285         {
286             NotifyEventArgs e = new NotifyEventArgs();
287             e.PropertyNotification = GetPropertyNotificationFromPtr(propertyNotification);
288
289             if (_propertyNotificationNotifyEventHandler != null)
290             {
291                 //here we send all data to user event handlers
292                 _propertyNotificationNotifyEventHandler(this, e);
293             }
294         }
295
296         ///<summary>
297         /// Event arguments that passed via Notify signal
298         ///</summary>
299         /// <since_tizen> 3 </since_tizen>
300         public class NotifyEventArgs : EventArgs
301         {
302             private PropertyNotification _propertyNotification;
303
304             ///<summary>
305             /// PropertyNotification - is the PropertyNotification handle that has the notification properties.
306             ///</summary>
307             /// <since_tizen> 3 </since_tizen>
308             public PropertyNotification PropertyNotification
309             {
310                 get
311                 {
312                     return _propertyNotification;
313                 }
314                 set
315                 {
316                     _propertyNotification = value;
317                 }
318             }
319         }
320     }
321 }