Release 4.0.0-preview1-00230
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / WidgetApplication.cs
1 /** Copyright (c) 2017 Samsung Electronics Co., Ltd.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16 using System;
17 using System.Runtime.InteropServices;
18
19 namespace Tizen.NUI
20 {
21     /// <summary>
22     /// Widget application
23     /// </summary>
24     internal class WidgetApplication : BaseHandle
25     {
26         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
27
28         internal WidgetApplication(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicManualPINVOKE.WidgetApplication_SWIGUpcast(cPtr), cMemoryOwn)
29         {
30             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
31         }
32
33         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(WidgetApplication obj)
34         {
35             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
36         }
37
38         /// <summary>
39         /// To make Window instance be disposed.
40         /// </summary>
41         protected override void Dispose(DisposeTypes type)
42         {
43             if(disposed)
44             {
45                 return;
46             }
47
48             if(type == DisposeTypes.Explicit)
49             {
50                 //Called by User
51                 //Release your own managed resources here.
52                 //You should release all of your own disposable objects here.
53             }
54
55             //Release your own unmanaged resources here.
56             //You should not access any managed member here except static instance.
57             //because the execution order of Finalizes is non-deterministic.
58
59             if (_initCallback != null)
60             {
61                 this.InitSignal().Disconnect(_initCallback);
62             }
63             if (_terminateCallback != null)
64             {
65                 this.TerminateSignal().Disconnect(_terminateCallback);
66             }
67             if (_languageChangedCallback != null)
68             {
69                 this.LanguageChangedSignal().Disconnect(_languageChangedCallback);;
70             }
71             if (_regionChangedCallback != null)
72             {
73                 this.RegionChangedSignal().Disconnect(_regionChangedCallback);
74             }
75             if (_batteryLowCallback != null)
76             {
77                 this.BatteryLowSignal().Disconnect(_batteryLowCallback);
78             }
79             if (_memoryLowCallback != null)
80             {
81                 this.MemoryLowSignal().Disconnect(_memoryLowCallback);
82             }
83             
84             if (swigCPtr.Handle != global::System.IntPtr.Zero)
85             {
86                 if (swigCMemOwn)
87                 {
88                     swigCMemOwn = false;
89                     NDalicManualPINVOKE.delete_WidgetApplication(swigCPtr);
90                 }
91                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
92             }
93
94             base.Dispose(type);
95         }
96
97         internal static WidgetApplication GetWidgetApplicationFromPtr(global::System.IntPtr cPtr)
98         {
99             WidgetApplication ret = new WidgetApplication(cPtr, false);
100             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101             return ret;
102         }
103
104         public static WidgetApplication instance
105         {
106             get
107             {
108                 return _instance;
109             }
110         }
111
112         internal void SetupDelegates()
113         {
114             InitDelegateInternal initializeCallback = new InitDelegateInternal(Initialization);
115
116             NUILog.Debug("InitSignal connection count");
117             this.InitSignal().Connect(initializeCallback);
118  
119             NUILog.Debug("InitSignal connection count = " + InitSignal().GetConnectionCount());
120         }
121
122         public static WidgetApplication NewWidgetApplication(string[] args, string stylesheet)
123         {
124             WidgetApplication ret = New(args, stylesheet);
125             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
126
127             // we've got an application now connect the signals
128             ret.SetupDelegates();
129             _instance = ret;
130             return ret;
131         }
132
133         public static WidgetApplication New(string[] args, string stylesheet)
134         {
135             int argc = args.Length;
136             string argvStr = string.Join(" ", args);
137
138             WidgetApplication ret = new WidgetApplication(NDalicManualPINVOKE.WidgetApplication_New(argc, argvStr, stylesheet), true);
139             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
140             return ret;
141         }
142  
143         internal WidgetApplication(WidgetApplication widgetApplication) : this(NDalicManualPINVOKE.new_WidgetApplication__SWIG_1(WidgetApplication.getCPtr(widgetApplication)), true)
144         {
145             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
146         }
147
148         internal WidgetApplication Assign(WidgetApplication widgetApplication)
149         {
150             WidgetApplication ret = new WidgetApplication(NDalicManualPINVOKE.WidgetApplication_Assign(swigCPtr, WidgetApplication.getCPtr(widgetApplication)), false);
151             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
152             return ret;
153         }
154
155         /// <summary>
156         /// This starts the application.
157         /// </summary>
158         public void MainLoop()
159         {
160             NDalicManualPINVOKE.WidgetApplication_MainLoop(swigCPtr);
161             if (NDalicPINVOKE.SWIGPendingException.Pending)
162             {
163                 throw NDalicPINVOKE.SWIGPendingException.Retrieve();
164             }
165         }
166
167         /// <summary>
168         /// This quits the application.
169         /// Tizen applications should use Lower to improve re-start performance unless they need to Quit completely.
170         /// </summary>
171         public void Quit()
172         {
173             NDalicManualPINVOKE.WidgetApplication_Quit(swigCPtr);
174             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
175         }
176
177         /// <summary>
178         /// Get window.
179         /// </summary>
180         /// <returns> The window for widget instance.</returns>
181         public Window GetWindow()
182         {
183             Window ret = new Window(NDalicManualPINVOKE.WidgetApplication_GetWindow(swigCPtr), true);
184             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
185             return ret;
186         }
187
188         /// <summary>
189         /// Get path application resources are stored at.
190         /// </summary>
191         /// <returns>The full path of the resources</returns>
192         public static string GetResourcePath()
193         {
194             string ret = NDalicManualPINVOKE.WidgetApplication_GetResourcePath();
195             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
196             return ret;
197         }
198
199         /// <summary>
200         /// Event arguments that passed via widget app event signal.
201         /// </summary>
202         public class WidgetApplicationEventArgs : EventArgs
203         {
204             /// <summary>
205             /// Widget application.
206             /// </summary>
207             public WidgetApplication WidgetApplication
208             {
209                 get;
210                 set;
211             }
212         }
213
214         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
215         private delegate void InitCallbackType(IntPtr widgetApplication);
216         private InitCallbackType _initCallback;
217         private DaliEventHandler<object,WidgetApplicationEventArgs> _initEventHandler;
218
219         /// <summary>
220         /// Init event.
221         /// </summary>
222         public event DaliEventHandler<object, WidgetApplicationEventArgs> Init
223         {
224             add
225             {
226                 if (_initEventHandler == null)
227                 {
228                     _initCallback = new InitCallbackType( OnInit);
229                     InitSignal().Connect(_initCallback);
230                 }
231
232                 _initEventHandler += value;
233             }
234
235             remove
236             {
237                 _initEventHandler -= value;
238
239                 if (_initEventHandler == null && InitSignal().Empty() == false)
240                 {
241                    InitSignal().Disconnect(_initCallback);
242                 }
243             }
244         }
245
246         private void OnInit(IntPtr widgetApplication)
247         {
248             WidgetApplicationEventArgs e = new WidgetApplicationEventArgs();
249             if (widgetApplication != null)
250             {
251                 e.WidgetApplication = WidgetApplication.GetWidgetApplicationFromPtr(widgetApplication);
252             }
253
254             if (_initEventHandler != null)
255             {
256                 _initEventHandler(this, e);
257             }
258         }
259
260         internal AppSignalType InitSignal()
261         {
262             AppSignalType ret = new AppSignalType(NDalicManualPINVOKE.WidgetApplication_InitSignal(swigCPtr), false);
263             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
264             return ret;
265         }
266
267         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
268         private delegate void TerminateCallbackType(IntPtr widgetApplication);
269         private TerminateCallbackType _terminateCallback;
270         private DaliEventHandler<object, WidgetApplicationEventArgs> _terminateEventHandler;
271
272         /// <summary>
273         /// Terminate event.
274         /// </summary>
275         public event DaliEventHandler<object, WidgetApplicationEventArgs> Terminate
276         {
277             add
278             {
279                 if (_terminateEventHandler == null)
280                 {
281                     _terminateCallback = new TerminateCallbackType( OnTerminate);
282                     TerminateSignal().Connect(_terminateCallback);
283                 }
284
285                 _terminateEventHandler += value;
286             }
287
288             remove
289             {
290                 _terminateEventHandler -= value;
291
292                 if (_terminateEventHandler == null && TerminateSignal().Empty() == false)
293                 {
294                    TerminateSignal().Disconnect(_terminateCallback);
295                 }
296             }
297         }
298
299         private void OnTerminate(IntPtr widgetApplication)
300         {
301             WidgetApplicationEventArgs e = new WidgetApplicationEventArgs();
302
303             if (widgetApplication != null)
304             {
305                 e.WidgetApplication = WidgetApplication.GetWidgetApplicationFromPtr(widgetApplication);
306             }
307
308             if (_terminateEventHandler != null)
309             {
310                 _terminateEventHandler(this, e);
311             }
312         }
313
314         internal AppSignalType TerminateSignal()
315         {
316             AppSignalType ret = new AppSignalType(NDalicManualPINVOKE.WidgetApplication_TerminateSignal(swigCPtr), false);
317             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
318             return ret;
319         }
320
321         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
322         private delegate void LanguageChangedCallbackType(IntPtr widgetApplication);
323         private LanguageChangedCallbackType _languageChangedCallback;
324         private DaliEventHandler<object, WidgetApplicationEventArgs> _languageChangedEventHandler;
325
326         /// <summary>
327         /// LanguageChanged event.
328         /// </summary>
329         public event DaliEventHandler<object, WidgetApplicationEventArgs> LanguageChanged
330         {
331             add
332             {
333                 if (_languageChangedEventHandler == null)
334                 {
335                     _languageChangedCallback = new LanguageChangedCallbackType( OnLanguageChanged);
336                     LanguageChangedSignal().Connect(_languageChangedCallback);
337                 }
338
339                 _languageChangedEventHandler += value;
340             }
341
342             remove
343             {
344                 _languageChangedEventHandler -= value;
345
346                 if (_languageChangedEventHandler == null && LanguageChangedSignal().Empty() == false)
347                 {
348                    LanguageChangedSignal().Disconnect(_languageChangedCallback);
349                 }
350             }
351         }
352
353         private void OnLanguageChanged(IntPtr widgetApplication)
354         {
355             WidgetApplicationEventArgs e = new WidgetApplicationEventArgs();
356
357             if (widgetApplication != null)
358             {
359                 e.WidgetApplication = WidgetApplication.GetWidgetApplicationFromPtr(widgetApplication);
360             }
361
362             if (_languageChangedEventHandler != null)
363             {
364                 _languageChangedEventHandler(this, e);
365             }
366         }
367
368         internal AppSignalType LanguageChangedSignal()
369         {
370             AppSignalType ret = new AppSignalType(NDalicManualPINVOKE.WidgetApplication_LanguageChangedSignal(swigCPtr), false);
371             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
372             return ret;
373         }
374
375         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
376         private delegate void RegionChangedCallbackType(IntPtr widgetApplication);
377         private RegionChangedCallbackType _regionChangedCallback;
378         private DaliEventHandler<object, WidgetApplicationEventArgs> _regionChangedEventHandler;
379
380         /// <summary>
381         /// RegionChanged event.
382         /// </summary>
383         public event DaliEventHandler<object, WidgetApplicationEventArgs> RegionChanged
384         {
385             add
386             {
387                 if (_regionChangedEventHandler == null)
388                 {
389                     _regionChangedCallback = new RegionChangedCallbackType( OnRegionChanged );
390                     RegionChangedSignal().Connect(_regionChangedCallback);
391                 }
392
393                 _regionChangedEventHandler += value;
394             }
395
396             remove
397             {
398                 _regionChangedEventHandler -= value;
399
400                 if (_regionChangedEventHandler == null && RegionChangedSignal().Empty() == false)
401                 {
402                    RegionChangedSignal().Disconnect(_regionChangedCallback);
403                 }
404             }
405         }
406
407         private void OnRegionChanged(IntPtr widgetApplication)
408         {
409             WidgetApplicationEventArgs e = new WidgetApplicationEventArgs();
410
411             if (widgetApplication != null)
412             {
413                 e.WidgetApplication = WidgetApplication.GetWidgetApplicationFromPtr(widgetApplication);
414             }
415
416             if (_regionChangedEventHandler != null)
417             {
418                 _regionChangedEventHandler(this, e);
419             }
420         }
421
422         internal AppSignalType RegionChangedSignal()
423         {
424             AppSignalType ret = new AppSignalType(NDalicManualPINVOKE.WidgetApplication_RegionChangedSignal(swigCPtr), false);
425             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
426             return ret;
427         }
428
429         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
430         private delegate void BatteryLowCallbackType(IntPtr widgetApplication);
431         private BatteryLowCallbackType _batteryLowCallback;
432         private DaliEventHandler<object, WidgetApplicationEventArgs> _batteryLowEventHandler;
433
434         /// <summary>
435         /// BatteryLow event.
436         /// </summary>
437         public event DaliEventHandler<object, WidgetApplicationEventArgs> BatteryLow
438         {
439             add
440             {
441                 if (_batteryLowEventHandler == null)
442                 {
443                     _batteryLowCallback = new BatteryLowCallbackType( OnBatteryLow );
444                     BatteryLowSignal().Connect(_batteryLowCallback);
445                 }
446
447                 _batteryLowEventHandler += value;
448             }
449
450             remove
451             {
452                 _batteryLowEventHandler -= value;
453
454                 if (_batteryLowEventHandler == null && BatteryLowSignal().Empty() == false)
455                 {
456                    BatteryLowSignal().Disconnect(_batteryLowCallback);
457                 }
458             }
459         }
460
461         private void OnBatteryLow(IntPtr widgetApplication)
462         {
463             WidgetApplicationEventArgs e = new WidgetApplicationEventArgs();
464
465             if (widgetApplication != null)
466             {
467                 e.WidgetApplication = WidgetApplication.GetWidgetApplicationFromPtr(widgetApplication);
468             }
469
470             if (_batteryLowEventHandler != null)
471             {
472                 _batteryLowEventHandler(this, e);
473             }
474         }
475
476         internal AppSignalType BatteryLowSignal()
477         {
478             AppSignalType ret = new AppSignalType(NDalicManualPINVOKE.WidgetApplication_BatteryLowSignal(swigCPtr), false);
479             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
480             return ret;
481         }
482
483         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
484         private delegate void MemoryLowCallbackType(IntPtr widgetApplication);
485         private MemoryLowCallbackType _memoryLowCallback;
486         private DaliEventHandler<object, WidgetApplicationEventArgs> _memoryLowEventHandler;
487
488         /// <summary>
489         /// MemoryLow event.
490         /// </summary>
491         public event DaliEventHandler<object, WidgetApplicationEventArgs> MemoryLow
492         {
493             add
494             {
495                 if (_memoryLowEventHandler == null)
496                 {
497                     _memoryLowCallback = new MemoryLowCallbackType (OnMemoryLow);
498                     MemoryLowSignal().Connect(_memoryLowCallback);
499                 }
500
501                 _memoryLowEventHandler += value;
502             }
503
504             remove
505             {
506                 _memoryLowEventHandler -= value;
507
508                 if (_memoryLowEventHandler == null && MemoryLowSignal().Empty() == false)
509                 {
510                    MemoryLowSignal().Disconnect(_memoryLowCallback);
511                 }
512             }
513         }
514
515         private void OnMemoryLow(IntPtr widgetApplication)
516         {
517             WidgetApplicationEventArgs e = new WidgetApplicationEventArgs();
518
519             if (widgetApplication != null)
520             {
521                 e.WidgetApplication = WidgetApplication.GetWidgetApplicationFromPtr(widgetApplication);
522             }
523
524             if (_memoryLowEventHandler != null)
525             {
526                 _memoryLowEventHandler(this, e);
527             }
528         }
529
530         internal AppSignalType MemoryLowSignal()
531         {
532             AppSignalType ret = new AppSignalType(NDalicManualPINVOKE.WidgetApplication_MemoryLowSignal(swigCPtr), false);
533             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
534             return ret;
535         }
536
537         public delegate void InitDelegate();
538
539         public delegate void TerminateDelegate();
540
541         public delegate void LanguageChangedDelegate();
542
543         public delegate void RegionChangedDelegate();
544
545         public delegate void BatteryLowDelegate();
546
547         public delegate void MemoryLowDelegate();
548
549         [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)]
550         internal delegate void InitDelegateInternal();
551
552         [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)]
553         internal delegate void TerminateDelegateInternal();
554
555         [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)]
556         internal delegate void LanguageChangedDelegateInternal();
557
558         [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)]
559         internal delegate void RegionChangedDelegateInternal();
560
561         [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)]
562         internal delegate void BatteryLowDelegateInternal();
563
564         [System.Runtime.InteropServices.UnmanagedFunctionPointer(System.Runtime.InteropServices.CallingConvention.StdCall)]
565         internal delegate void MemoryLowDelegateInternal();
566
567         static void Initialization()
568         {
569             //  instance.InitDelegate();
570         }
571
572         private static WidgetApplication _instance; //singleton
573
574     }
575
576 }