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