48eea3e0ee10e232e09516e48cbb46fef05f88fa
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Common / 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.NewPropertyNotification(), 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.NewPropertyNotification(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.Upcast(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                 // Restricted to only one listener
70                 if (_propertyNotificationNotifyEventHandler == null)
71                 {
72                     _propertyNotificationNotifyEventHandler += value;
73
74                     _propertyNotificationNotifyEventCallbackDelegate = new NotifyEventCallbackDelegate(OnPropertyNotificationNotify);
75                     this.NotifySignal().Connect(_propertyNotificationNotifyEventCallbackDelegate);
76                 }
77             }
78
79             remove
80             {
81                 if (_propertyNotificationNotifyEventHandler != null)
82                 {
83                     this.NotifySignal().Disconnect(_propertyNotificationNotifyEventCallbackDelegate);
84                 }
85
86                 _propertyNotificationNotifyEventHandler -= value;
87             }
88         }
89
90         /// <summary>
91         /// Enumeration for description of how to check condition.
92         /// </summary>
93         /// <since_tizen> 3 </since_tizen>
94         public enum NotifyMode
95         {
96             /// <summary>
97             /// Don't notify, regardless of result of Condition
98             /// </summary>
99             /// <since_tizen> 3 </since_tizen>
100             Disabled,
101             /// <summary>
102             /// Notify whenever condition changes from false to true.
103             /// </summary>
104             /// <since_tizen> 3 </since_tizen>
105             NotifyOnTrue,
106             /// <summary>
107             /// Notify whenever condition changes from true to false.
108             /// </summary>
109             /// <since_tizen> 3 </since_tizen>
110             NotifyOnFalse,
111             /// <summary>
112             /// Notify whenever condition changes (false to true, and true to false)
113             /// </summary>
114             /// <since_tizen> 3 </since_tizen>
115             NotifyOnChanged
116         }
117
118         /// <summary>
119         /// Get property notification from Intptr.<br/>
120         /// This should be internal, please do not use.
121         /// </summary>
122         /// <param name="cPtr">An object of IntPtr type.</param>
123         /// <returns>An object of the PropertyNotification type.</returns>
124         /// <since_tizen> 4 </since_tizen>
125         [Obsolete("Deprecated in API6, Will be removed in API9, " +
126             "Please use Notified event instead!" +
127             "IntPtr(native integer pointer) is supposed to be not used in Application!")]
128         [EditorBrowsable(EditorBrowsableState.Never)]
129         public static PropertyNotification GetPropertyNotificationFromPtr(global::System.IntPtr cPtr)
130         {
131             PropertyNotification ret = new PropertyNotification(cPtr, false);
132             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
133             return ret;
134         }
135
136         /// <summary>
137         /// Downcast a PropertyNotification instance.
138         /// </summary>
139         /// <param name="handle">Handle to an object of BaseHandle type.</param>
140         /// <returns>Handle to an object of the PropertyNotification type.</returns>
141         /// <exception cref="ArgumentNullException"> Thrown when handle is null. </exception>
142         /// <since_tizen> 4 </since_tizen>
143         public static PropertyNotification DownCast(BaseHandle handle)
144         {
145             if (null == handle)
146             {
147                 throw new ArgumentNullException(nameof(handle));
148             }
149             PropertyNotification ret = Registry.GetManagedBaseHandleFromNativePtr(handle) as PropertyNotification;
150             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
151             return ret;
152         }
153
154         /// <summary>
155         /// Assign.
156         /// </summary>
157         /// <param name="rhs">A reference to the copied handle.</param>
158         /// <returns>A reference to this.</returns>
159         /// <since_tizen> 4 </since_tizen>
160         [Obsolete("Deprecated in API6, Will be removed in API9, " +
161             "Please use PropertyNotification() constructor instead!")]
162         [EditorBrowsable(EditorBrowsableState.Never)]
163         public PropertyNotification Assign(PropertyNotification rhs)
164         {
165             PropertyNotification ret = new PropertyNotification(Interop.PropertyNotification.Assign(SwigCPtr, PropertyNotification.getCPtr(rhs)), false);
166             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
167             return ret;
168         }
169
170         /// <summary>
171         /// Gets the condition of this notification.
172         /// </summary>
173         /// <returns>The condition is returned.</returns>
174         /// <since_tizen> 4 </since_tizen>
175         public PropertyCondition GetCondition()
176         {
177             PropertyCondition ret = new PropertyCondition(Interop.PropertyNotification.GetCondition(SwigCPtr), true);
178             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
179             return ret;
180         }
181
182         /// <summary>
183         /// Gets the target handle that this notification is observing.
184         /// </summary>
185         /// <since_tizen> 4 </since_tizen>
186         public Animatable GetTarget()
187         {
188             BaseHandle ret = Registry.GetManagedBaseHandleFromNativePtr(Interop.PropertyNotification.GetTarget(SwigCPtr));
189             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
190             return ret as Animatable;
191         }
192
193         /// <summary>
194         /// Gets the target handle's property index that this notification.
195         /// </summary>
196         /// <returns>The target property index.</returns>
197         /// <since_tizen> 4 </since_tizen>
198         public int GetTargetProperty()
199         {
200             int ret = Interop.PropertyNotification.GetTargetProperty(SwigCPtr);
201             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
202             return ret;
203         }
204
205         /// <summary>
206         /// Sets the Notification mode.
207         /// </summary>
208         /// <param name="mode">mode Notification mode (Default is PropertyNotification::NotifyOnTrue).</param>
209         /// <since_tizen> 4 </since_tizen>
210         public void SetNotifyMode(PropertyNotification.NotifyMode mode)
211         {
212             Interop.PropertyNotification.SetNotifyMode(SwigCPtr, (int)mode);
213             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
214         }
215
216         /// <summary>
217         /// Retrieves the current Notification mode.
218         /// </summary>
219         /// <returns>Notification mode.</returns>
220         /// <since_tizen> 4 </since_tizen>
221         public PropertyNotification.NotifyMode GetNotifyMode()
222         {
223             PropertyNotification.NotifyMode ret = (PropertyNotification.NotifyMode)Interop.PropertyNotification.GetNotifyMode(SwigCPtr);
224             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
225             return ret;
226         }
227
228         /// <summary>
229         /// Gets the result of the last condition check that caused a signal emit,
230         /// useful when using NotifyOnChanged mode and need to know what it changed to.
231         /// </summary>
232         /// <since_tizen> 4 </since_tizen>
233         public bool GetNotifyResult()
234         {
235             bool ret = Interop.PropertyNotification.GetNotifyResult(SwigCPtr);
236             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
237             return ret;
238         }
239
240         /// <summary>
241         /// Connects to this signal to be notified when the notification has occurred.
242         /// </summary>
243         /// <returns>A signal object to Connect() with</returns>
244         /// <since_tizen> 4 </since_tizen>
245         [Obsolete("Deprecated in API6, Will be removed in API9, " +
246             "Please use Notified event instead!")]
247         [EditorBrowsable(EditorBrowsableState.Never)]
248         public PropertyNotifySignal NotifySignal()
249         {
250             PropertyNotifySignal ret = new PropertyNotifySignal(Interop.PropertyNotification.NotifySignal(SwigCPtr), false);
251             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
252             return ret;
253         }
254
255         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(PropertyNotification obj)
256         {
257             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
258         }
259
260         /// This will not be public opened.
261         [EditorBrowsable(EditorBrowsableState.Never)]
262         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
263         {
264             Interop.PropertyNotification.DeletePropertyNotification(swigCPtr);
265         }
266
267         // Callback for PropertyNotification NotifySignal
268         private void OnPropertyNotificationNotify(IntPtr propertyNotification)
269         {
270             NotifyEventArgs e = new NotifyEventArgs();
271             e.PropertyNotification = GetPropertyNotificationFromPtr(propertyNotification);
272
273             if (_propertyNotificationNotifyEventHandler != null)
274             {
275                 //here we send all data to user event handlers
276                 _propertyNotificationNotifyEventHandler(this, e);
277             }
278         }
279
280         ///<summary>
281         /// Event arguments that passed via Notify signal
282         ///</summary>
283         /// <since_tizen> 3 </since_tizen>
284         public class NotifyEventArgs : EventArgs
285         {
286             private PropertyNotification _propertyNotification;
287
288             ///<summary>
289             /// PropertyNotification - is the PropertyNotification handle that has the notification properties.
290             ///</summary>
291             /// <since_tizen> 3 </since_tizen>
292             public PropertyNotification PropertyNotification
293             {
294                 get
295                 {
296                     return _propertyNotification;
297                 }
298                 set
299                 {
300                     _propertyNotification = value;
301                 }
302             }
303         }
304     }
305 }