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