Merge "Add C# binding for VideoView.Underlay property."
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Timer.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
19 {
20
21     using System;
22     using System.Runtime.InteropServices;
23
24     /// <summary>
25     /// Mechanism to issue simple periodic or one-shot events.<br />
26     /// Timer is provided for application developers to be able to issue
27     /// simple periodic or one-shot events. Please note that the timer
28     /// callback functions should return as soon as possible because they
29     /// block the next SignalTick. Please note that timer signals are not
30     /// in sync with DALi's render timer.<br />
31     /// This class is a handle class so it can be stack allocated and used
32     /// as a member.<br />
33     /// </summary>
34     public class Timer : BaseHandle
35     {
36         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
37
38         internal Timer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Timer_SWIGUpcast(cPtr), cMemoryOwn)
39         {
40             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
41         }
42
43         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Timer obj)
44         {
45             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
46         }
47
48         /// <summary>
49         /// Dispose.
50         /// </summary>
51         /// <since_tizen> 3 </since_tizen>
52         protected override void Dispose(DisposeTypes type)
53         {
54             if(disposed)
55             {
56                 return;
57             }
58
59             if(type == DisposeTypes.Explicit)
60             {
61                 //Called by User
62                 //Release your own managed resources here.
63                 //You should release all of your own disposable objects here.
64             }
65
66             //Release your own unmanaged resources here.
67             //You should not access any managed member here except static instance.
68             //because the execution order of Finalizes is non-deterministic.
69
70             if (_timerTickCallbackDelegate != null)
71             {
72                 TickSignal().Disconnect(_timerTickCallbackDelegate);
73             }
74
75             if (swigCPtr.Handle != global::System.IntPtr.Zero)
76             {
77                 if (swigCMemOwn)
78                 {
79                     swigCMemOwn = false;
80                     NDalicPINVOKE.delete_Timer(swigCPtr);
81                 }
82                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
83             }
84
85             base.Dispose(type);
86         }
87
88
89         /// <summary>
90         /// Event arguments that passed via the tick event.
91         /// </summary>
92         public class TickEventArgs : EventArgs
93         {
94         }
95
96         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
97         private delegate bool TickCallbackDelegate(IntPtr data);
98         private EventHandlerWithReturnType<object, TickEventArgs, bool> _timerTickEventHandler;
99         private TickCallbackDelegate _timerTickCallbackDelegate;
100
101         /// <summary>
102         /// @brief Event for the ticked signal, which can be used to subscribe or unsubscribe the event handler
103         /// provided by the user. The ticked signal is emitted after specified time interval.<br />
104         /// </summary>
105         /// <since_tizen> 3 </since_tizen>
106         public event EventHandlerWithReturnType<object, TickEventArgs, bool> Tick
107         {
108             add
109             {
110                 if (_timerTickEventHandler == null)
111                 {
112                     _timerTickCallbackDelegate = (OnTick);
113                     TickSignal().Connect(_timerTickCallbackDelegate);
114                 }
115                 _timerTickEventHandler += value;
116             }
117             remove
118             {
119                 _timerTickEventHandler -= value;
120                 if (_timerTickEventHandler == null && TickSignal().Empty() == false)
121                 {
122                     TickSignal().Disconnect(_timerTickCallbackDelegate);
123                 }
124             }
125         }
126
127         private bool OnTick(IntPtr data)
128         {
129             TickEventArgs e = new TickEventArgs();
130
131             if (_timerTickEventHandler != null)
132             {
133                 //here we send all data to user event handlers
134                 return _timerTickEventHandler(this, e);
135             }
136             return false;
137         }
138
139         /// <summary>
140         /// Creates a tick timer that emits periodic signal.
141         /// </summary>
142         /// <param name="milliSec">Interval in milliseconds.</param>
143         /// <returns>A new timer.</returns>
144         /// <since_tizen> 3 </since_tizen>
145         public Timer(uint milliSec) : this(NDalicPINVOKE.Timer_New(milliSec), true)
146         {
147             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
148
149         }
150         internal Timer(Timer timer) : this(NDalicPINVOKE.new_Timer__SWIG_1(Timer.getCPtr(timer)), true)
151         {
152             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
153         }
154
155         /// <summary>
156         /// [Obsolete("Please do not use! this will be deprecated")]
157         /// </summary>
158         /// <since_tizen> 3 </since_tizen>
159         [Obsolete("Please do not use! this will be deprecated")]
160         public static Timer DownCast(BaseHandle handle)
161         {
162             Timer ret =  Registry.GetManagedBaseHandleFromNativePtr(handle) as Timer;
163             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
164             return ret;
165         }
166
167         /// <summary>
168         /// Starts the timer.<br />
169         /// In case a timer is already running, its time is reset and the timer is restarted.<br />
170         /// </summary>
171         /// <since_tizen> 3 </since_tizen>
172         public void Start()
173         {
174             NDalicPINVOKE.Timer_Start(swigCPtr);
175             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
176         }
177
178         /// <summary>
179         /// Stops the timer.
180         /// </summary>
181         /// <since_tizen> 3 </since_tizen>
182         public void Stop()
183         {
184             NDalicPINVOKE.Timer_Stop(swigCPtr);
185             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
186         }
187
188         /// <summary>
189         /// Gets/Sets the interval of the timer.
190         /// </summary>
191         /// <since_tizen> 4 </since_tizen>
192         public uint Interval
193         {
194             get
195             {
196                 return GetInterval();
197             }
198             set
199             {
200                 SetInterval(value);
201             }
202         }
203
204         /// <summary>
205         /// Sets a new interval on the timer and starts the timer.<br />
206         /// Cancels the previous timer.<br />
207         /// </summary>
208         /// <param name="milliSec">MilliSec interval in milliseconds.</param>
209         internal void SetInterval(uint milliSec)
210         {
211             NDalicPINVOKE.Timer_SetInterval(swigCPtr, milliSec);
212             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
213         }
214
215         internal uint GetInterval()
216         {
217             uint ret = NDalicPINVOKE.Timer_GetInterval(swigCPtr);
218             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
219             return ret;
220         }
221
222         /// <summary>
223         /// Tells whether the timer is running.
224         /// </summary>
225         /// <returns>Whether the timer is started or not.</returns>
226         /// <since_tizen> 3 </since_tizen>
227         public bool IsRunning()
228         {
229             bool ret = NDalicPINVOKE.Timer_IsRunning(swigCPtr);
230             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
231             return ret;
232         }
233
234         internal TimerSignalType TickSignal()
235         {
236             TimerSignalType ret = new TimerSignalType(NDalicPINVOKE.Timer_TickSignal(swigCPtr), false);
237             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
238             return ret;
239         }
240
241     }
242
243 }