[NUI] 1.Remove disposed instance from XamlResource; 2.Support BindingContext in EXaml
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Application / Application.cs
1 /*
2  * Copyright(c) 2021 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.Collections.Generic;
19 using System.Collections.ObjectModel;
20 using System.ComponentModel;
21 using System.Diagnostics;
22 using System.Runtime.InteropServices;
23 using Tizen.NUI.Binding;
24
25 namespace Tizen.NUI
26 {
27     /**
28       * @brief Event arguments that passed via NUIApplicationInit signal
29       */
30     internal class NUIApplicationInitEventArgs : EventArgs
31     {
32         private Application application;
33
34         /**
35           * @brief Application - is the application that is being initialized
36           */
37         public Application Application
38         {
39             get
40             {
41                 return application;
42             }
43             set
44             {
45                 application = value;
46             }
47         }
48     }
49
50     /**
51       * @brief Event arguments that passed via NUIApplicationTerminate signal
52       */
53     internal class NUIApplicationTerminatingEventArgs : EventArgs
54     {
55         private Application application;
56         /**
57           * @brief Application - is the application that is being Terminated
58           */
59         public Application Application
60         {
61             get
62             {
63                 return application;
64             }
65             set
66             {
67                 application = value;
68             }
69         }
70     }
71
72     /**
73       * @brief Event arguments that passed via NUIApplicationPause signal
74       */
75     internal class NUIApplicationPausedEventArgs : EventArgs
76     {
77         private Application application;
78         /**
79           * @brief Application - is the application that is being Paused
80           */
81         public Application Application
82         {
83             get
84             {
85                 return application;
86             }
87             set
88             {
89                 application = value;
90             }
91         }
92     }
93
94     /**
95       * @brief Event arguments that passed via NUIApplicationResume signal
96       */
97     internal class NUIApplicationResumedEventArgs : EventArgs
98     {
99         private Application application;
100         /**
101           * @brief Application - is the application that is being Resumed
102           */
103         public Application Application
104         {
105             get
106             {
107                 return application;
108             }
109             set
110             {
111                 application = value;
112             }
113         }
114     }
115
116     /**
117       * @brief Event arguments that passed via NUIApplicationReset signal
118       */
119     internal class NUIApplicationResetEventArgs : EventArgs
120     {
121         private Application application;
122         /**
123           * @brief Application - is the application that is being Reset
124           */
125         public Application Application
126         {
127             get
128             {
129                 return application;
130             }
131             set
132             {
133                 application = value;
134             }
135         }
136     }
137
138     /**
139       * @brief Event arguments that passed via NUIApplicationLanguageChanged signal
140       */
141     internal class NUIApplicationLanguageChangedEventArgs : EventArgs
142     {
143         private Application application;
144         /**
145           * @brief Application - is the application that is being affected with Device's language change
146           */
147         public Application Application
148         {
149             get
150             {
151                 return application;
152             }
153             set
154             {
155                 application = value;
156             }
157         }
158     }
159
160     /**
161       * @brief Event arguments that passed via NUIApplicationRegionChanged signal
162       */
163     internal class NUIApplicationRegionChangedEventArgs : EventArgs
164     {
165         private Application application;
166         /**
167           * @brief Application - is the application that is being affected with Device's region change
168           */
169         public Application Application
170         {
171             get
172             {
173                 return application;
174             }
175             set
176             {
177                 application = value;
178             }
179         }
180     }
181
182     /**
183       * @brief Event arguments that passed via NUIApplicationBatteryLow signal
184       */
185     internal class NUIApplicationBatteryLowEventArgs : EventArgs
186     {
187         private Application.BatteryStatus status;
188         /**
189           * @brief Application - is the application that is being affected when the battery level of the device is low
190           */
191         public Application.BatteryStatus BatteryStatus
192         {
193             get
194             {
195                 return status;
196             }
197             set
198             {
199                 status = value;
200             }
201         }
202     }
203
204     /**
205       * @brief Event arguments that passed via NUIApplicationMemoryLow signal
206       */
207     internal class NUIApplicationMemoryLowEventArgs : EventArgs
208     {
209         private Application.MemoryStatus status;
210         /**
211           * @brief Application - is the application that is being affected when the memory level of the device is low
212           */
213         public Application.MemoryStatus MemoryStatus
214         {
215             get
216             {
217                 return status;
218             }
219             set
220             {
221                 status = value;
222             }
223         }
224     }
225
226     /**
227       * @brief Event arguments that passed via NUIApplicationAppControl  signal
228       */
229     internal class NUIApplicationAppControlEventArgs : EventArgs
230     {
231         private Application application;
232         private IntPtr voidp;
233         /**
234           * @brief Application - is the application that is receiving the launch request from another application
235           */
236         public Application Application
237         {
238             get
239             {
240                 return application;
241             }
242             set
243             {
244                 application = value;
245             }
246         }
247         /**
248           * @brief VoidP - contains the information about why the application is launched
249           */
250         public IntPtr VoidP
251         {
252             get
253             {
254                 return voidp;
255             }
256             set
257             {
258                 voidp = value;
259             }
260         }
261     }
262
263     /// <summary>
264     /// A class to get resources in current application.
265     /// </summary>
266     public sealed class GetResourcesProvider
267     {
268         /// <summary>
269         /// Get resources in current application.
270         /// </summary>
271         static public IResourcesProvider Get()
272         {
273             return Tizen.NUI.Application.Current;
274         }
275     }
276
277     internal class Application : BaseHandle, IResourcesProvider, IElementConfiguration<Application>
278     {
279         static Application s_current;
280
281         ReadOnlyCollection<Element> logicalChildren;
282
283         [EditorBrowsable(EditorBrowsableState.Never)]
284         public static void SetCurrentApplication(Application value) => Current = value;
285
286         public static Application Current
287         {
288             get { return s_current; }
289             set
290             {
291                 if (s_current == value)
292                     return;
293                 s_current = value;
294             }
295         }
296
297         internal override ReadOnlyCollection<Element> LogicalChildrenInternal
298         {
299             get { return logicalChildren ?? (logicalChildren = new ReadOnlyCollection<Element>(InternalChildren)); }
300         }
301
302         internal IResourceDictionary SystemResources { get; }
303
304         ObservableCollection<Element> InternalChildren { get; } = new ObservableCollection<Element>();
305
306         ResourceDictionary resources;
307         public bool IsResourcesCreated => resources != null;
308
309         public delegate void resChangeCb(object sender, ResourcesChangedEventArgs e);
310
311         internal event EventHandler<ResourcesChangedEventArgs> XamlResourceChanged;
312
313         internal override void OnResourcesChanged(object sender, ResourcesChangedEventArgs e)
314         {
315             base.OnResourcesChanged(sender, e);
316             XamlResourceChanged?.Invoke(sender, e);
317         }
318
319         public ResourceDictionary XamlResources
320         {
321             get
322             {
323                 if (resources == null)
324                 {
325                     resources = new ResourceDictionary();
326                     int hashCode = resources.GetHashCode();
327                     ((IResourceDictionary)resources).ValuesChanged += OnResourcesChanged;
328                 }
329                 return resources;
330             }
331             set
332             {
333                 if (resources == value)
334                     return;
335                 OnPropertyChanging();
336
337                 if (resources != null)
338                     ((IResourceDictionary)resources).ValuesChanged -= OnResourcesChanged;
339                 resources = value;
340                 OnResourcesChanged(value);
341                 if (resources != null)
342                     ((IResourceDictionary)resources).ValuesChanged += OnResourcesChanged;
343
344                 OnPropertyChanged();
345             }
346         }
347
348         protected override void OnParentSet()
349         {
350             throw new InvalidOperationException("Setting a Parent on Application is invalid.");
351         }
352
353         [EditorBrowsable(EditorBrowsableState.Never)]
354         public static bool IsApplicationOrNull(Element element)
355         {
356             return element == null || element is Application;
357         }
358
359         internal override void OnParentResourcesChanged(IEnumerable<KeyValuePair<string, object>> values)
360         {
361             if (!((IResourcesProvider)this).IsResourcesCreated || XamlResources.Count == 0)
362             {
363                 base.OnParentResourcesChanged(values);
364                 return;
365             }
366
367             var innerKeys = new HashSet<string>();
368             var changedResources = new List<KeyValuePair<string, object>>();
369             foreach (KeyValuePair<string, object> c in XamlResources)
370                 innerKeys.Add(c.Key);
371             foreach (KeyValuePair<string, object> value in values)
372             {
373                 if (innerKeys.Add(value.Key))
374                     changedResources.Add(value);
375             }
376             OnResourcesChanged(changedResources);
377         }
378
379         internal Application(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
380         {
381             SetCurrentApplication(this);
382             s_current = this;
383         }
384
385         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Application obj)
386         {
387             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.SwigCPtr;
388         }
389
390         protected override void Dispose(DisposeTypes type)
391         {
392             if (disposed)
393             {
394                 return;
395             }
396
397             //Release your own unmanaged resources here.
398             //You should not access any managed member here except static instance.
399             //because the execution order of Finalizes is non-deterministic.
400             if (applicationInitEventCallbackDelegate != null)
401             {
402                 initSignal?.Disconnect(applicationInitEventCallbackDelegate);
403                 initSignal?.Dispose();
404                 initSignal = null;
405             }
406
407             if (applicationTerminateEventCallbackDelegate != null)
408             {
409                 terminateSignal?.Disconnect(applicationTerminateEventCallbackDelegate);
410                 terminateSignal?.Dispose();
411                 terminateSignal = null;
412             }
413
414             if (applicationPauseEventCallbackDelegate != null)
415             {
416                 pauseSignal?.Disconnect(applicationPauseEventCallbackDelegate);
417                 pauseSignal?.Dispose();
418                 pauseSignal = null;
419             }
420
421             if (applicationResumeEventCallbackDelegate != null)
422             {
423                 resumeSignal?.Disconnect(applicationResumeEventCallbackDelegate);
424                 resumeSignal?.Dispose();
425                 resumeSignal = null;
426             }
427
428             if (applicationResetEventCallbackDelegate != null)
429             {
430                 resetSignal?.Disconnect(applicationResetEventCallbackDelegate);
431                 resetSignal?.Dispose();
432                 resetSignal = null;
433             }
434
435             if (applicationLanguageChangedEventCallbackDelegate != null)
436             {
437                 languageChangedSignal?.Disconnect(applicationLanguageChangedEventCallbackDelegate);
438                 languageChangedSignal?.Dispose();
439                 languageChangedSignal = null;
440             }
441
442             if (applicationRegionChangedEventCallbackDelegate != null)
443             {
444                 regionChangedSignal?.Disconnect(applicationRegionChangedEventCallbackDelegate);
445                 regionChangedSignal?.Dispose();
446                 regionChangedSignal = null;
447             }
448
449             if (applicationBatteryLowEventCallbackDelegate != null)
450             {
451                 batteryLowSignal?.Disconnect(applicationBatteryLowEventCallbackDelegate);
452                 batteryLowSignal?.Dispose();
453                 batteryLowSignal = null;
454             }
455
456             if (applicationMemoryLowEventCallbackDelegate != null)
457             {
458                 memoryLowSignal?.Disconnect(applicationMemoryLowEventCallbackDelegate);
459                 memoryLowSignal?.Dispose();
460                 memoryLowSignal = null;
461             }
462
463             if (applicationAppControlEventCallbackDelegate != null)
464             {
465                 appControlSignal?.Disconnect(applicationAppControlEventCallbackDelegate);
466                 appControlSignal?.Dispose();
467                 appControlSignal = null;
468             }
469
470             window?.Dispose();
471             window = null;
472
473             base.Dispose(type);
474         }
475         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
476         {
477             Interop.Application.DeleteApplication(swigCPtr);
478         }
479
480         public enum BatteryStatus
481         {
482             Normal,
483             CriticallyLow,
484             PowerOff
485         };
486
487         public enum MemoryStatus
488         {
489             Normal,
490             Low,
491             CriticallyLow
492         };
493
494         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
495         private delegate void NUIApplicationInitEventCallbackDelegate(IntPtr application);
496         private DaliEventHandler<object, NUIApplicationInitEventArgs> applicationInitEventHandler;
497         private NUIApplicationInitEventCallbackDelegate applicationInitEventCallbackDelegate;
498         private ApplicationSignal initSignal;
499
500         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
501         private delegate void NUIApplicationTerminateEventCallbackDelegate(IntPtr application);
502         private DaliEventHandler<object, NUIApplicationTerminatingEventArgs> applicationTerminateEventHandler;
503         private NUIApplicationTerminateEventCallbackDelegate applicationTerminateEventCallbackDelegate;
504         private ApplicationSignal terminateSignal;
505
506         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
507         private delegate void NUIApplicationPauseEventCallbackDelegate(IntPtr application);
508         private DaliEventHandler<object, NUIApplicationPausedEventArgs> applicationPauseEventHandler;
509         private NUIApplicationPauseEventCallbackDelegate applicationPauseEventCallbackDelegate;
510         private ApplicationSignal pauseSignal;
511
512         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
513         private delegate void NUIApplicationResumeEventCallbackDelegate(IntPtr application);
514         private DaliEventHandler<object, NUIApplicationResumedEventArgs> applicationResumeEventHandler;
515         private NUIApplicationResumeEventCallbackDelegate applicationResumeEventCallbackDelegate;
516         private ApplicationSignal resumeSignal;
517
518         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
519         private delegate void NUIApplicationResetEventCallbackDelegate(IntPtr application);
520         private DaliEventHandler<object, NUIApplicationResetEventArgs> applicationResetEventHandler;
521         private NUIApplicationResetEventCallbackDelegate applicationResetEventCallbackDelegate;
522         private ApplicationSignal resetSignal;
523
524         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
525         private delegate void NUIApplicationLanguageChangedEventCallbackDelegate(IntPtr application);
526         private DaliEventHandler<object, NUIApplicationLanguageChangedEventArgs> applicationLanguageChangedEventHandler;
527         private NUIApplicationLanguageChangedEventCallbackDelegate applicationLanguageChangedEventCallbackDelegate;
528         private ApplicationSignal languageChangedSignal;
529
530
531         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
532         private delegate void NUIApplicationRegionChangedEventCallbackDelegate(IntPtr application);
533         private DaliEventHandler<object, NUIApplicationRegionChangedEventArgs> applicationRegionChangedEventHandler;
534         private NUIApplicationRegionChangedEventCallbackDelegate applicationRegionChangedEventCallbackDelegate;
535         private ApplicationSignal regionChangedSignal;
536
537         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
538         private delegate void NUIApplicationBatteryLowEventCallbackDelegate(BatteryStatus status);
539         private DaliEventHandler<object, NUIApplicationBatteryLowEventArgs> applicationBatteryLowEventHandler;
540         private NUIApplicationBatteryLowEventCallbackDelegate applicationBatteryLowEventCallbackDelegate;
541         private LowBatterySignalType batteryLowSignal;
542
543         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
544         private delegate void NUIApplicationMemoryLowEventCallbackDelegate(MemoryStatus status);
545         private DaliEventHandler<object, NUIApplicationMemoryLowEventArgs> applicationMemoryLowEventHandler;
546         private NUIApplicationMemoryLowEventCallbackDelegate applicationMemoryLowEventCallbackDelegate;
547         private LowMemorySignalType memoryLowSignal;
548
549         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
550         private delegate void NUIApplicationAppControlEventCallbackDelegate(IntPtr application, IntPtr voidp);
551         private DaliEventHandler<object, NUIApplicationAppControlEventArgs> applicationAppControlEventHandler;
552         private NUIApplicationAppControlEventCallbackDelegate applicationAppControlEventCallbackDelegate;
553         private ApplicationControlSignal appControlSignal;
554
555         private Window window;
556
557         /**
558           * @brief Event for Initialized signal which can be used to subscribe/unsubscribe the event handler
559           *  provided by the user. Initialized signal is emitted when application is initialized
560           */
561         public event DaliEventHandler<object, NUIApplicationInitEventArgs> Initialized
562         {
563             add
564             {
565                 // Restricted to only one listener
566                 if (applicationInitEventHandler == null)
567                 {
568                     applicationInitEventHandler += value;
569                     applicationInitEventCallbackDelegate = new NUIApplicationInitEventCallbackDelegate(OnApplicationInit);
570                     initSignal = this.InitSignal();
571                     initSignal?.Connect(applicationInitEventCallbackDelegate);
572                 }
573             }
574
575             remove
576             {
577                 if (applicationInitEventHandler != null)
578                 {
579                     initSignal?.Disconnect(applicationInitEventCallbackDelegate);
580                     initSignal?.Dispose();
581                     initSignal = null;
582                 }
583
584                 applicationInitEventHandler -= value;
585             }
586         }
587
588         // Callback for Application InitSignal
589         private void OnApplicationInit(IntPtr data)
590         {
591             // Initialize DisposeQueue Singleton class. This is also required to create DisposeQueue on main thread.
592             DisposeQueue.Instance.Initialize();
593             Window.Instance = GetWindow();
594
595             // Notify that the window is displayed to the app core.
596             if (NUIApplication.IsPreload)
597             {
598                 Window.Instance.Show();
599             }
600
601             if (applicationInitEventHandler != null)
602             {
603                 NUIApplicationInitEventArgs e = new NUIApplicationInitEventArgs();
604                 e.Application = this;
605                 applicationInitEventHandler.Invoke(this, e);
606             }
607
608         }
609
610         /**
611           * @brief Event for Terminated signal which can be used to subscribe/unsubscribe the event handler
612           *  provided by the user. Terminated signal is emitted when application is terminating
613           */
614         public event DaliEventHandler<object, NUIApplicationTerminatingEventArgs> Terminating
615         {
616             add
617             {
618                 // Restricted to only one listener
619                 if (applicationTerminateEventHandler == null)
620                 {
621                     applicationTerminateEventHandler += value;
622
623                     applicationTerminateEventCallbackDelegate = new NUIApplicationTerminateEventCallbackDelegate(OnNUIApplicationTerminate);
624                     terminateSignal = this.TerminateSignal();
625                     terminateSignal?.Connect(applicationTerminateEventCallbackDelegate);
626                 }
627             }
628
629             remove
630             {
631                 if (applicationTerminateEventHandler != null)
632                 {
633                     terminateSignal?.Disconnect(applicationTerminateEventCallbackDelegate);
634                     terminateSignal?.Dispose();
635                     terminateSignal = null;
636                 }
637
638                 applicationTerminateEventHandler -= value;
639             }
640         }
641
642         // Callback for Application TerminateSignal
643         private void OnNUIApplicationTerminate(IntPtr data)
644         {
645             if (applicationTerminateEventHandler != null)
646             {
647                 NUIApplicationTerminatingEventArgs e = new NUIApplicationTerminatingEventArgs();
648                 e.Application = this;
649                 applicationTerminateEventHandler.Invoke(this, e);
650             }
651
652             List<Window> windows = GetWindowList();
653             foreach (Window window in windows)
654             {
655                 window?.DisconnectNativeSignals();
656             }
657         }
658
659         /**
660           * @brief Event for Paused signal which can be used to subscribe/unsubscribe the event handler
661           * provided by the user. Paused signal is emitted when application is paused
662           */
663         public event DaliEventHandler<object, NUIApplicationPausedEventArgs> Paused
664         {
665             add
666             {
667                 // Restricted to only one listener
668                 if (applicationPauseEventHandler == null)
669                 {
670                     applicationPauseEventHandler += value;
671
672                     applicationPauseEventCallbackDelegate = new NUIApplicationPauseEventCallbackDelegate(OnNUIApplicationPause);
673                     pauseSignal = this.PauseSignal();
674                     pauseSignal?.Connect(applicationPauseEventCallbackDelegate);
675                 }
676             }
677
678             remove
679             {
680                 if (applicationPauseEventHandler != null)
681                 {
682                     pauseSignal?.Disconnect(applicationPauseEventCallbackDelegate);
683                     pauseSignal?.Dispose();
684                     pauseSignal = null;
685                 }
686
687                 applicationPauseEventHandler -= value;
688             }
689         }
690
691         // Callback for Application PauseSignal
692         private void OnNUIApplicationPause(IntPtr data)
693         {
694             if (applicationPauseEventHandler != null)
695             {
696                 NUIApplicationPausedEventArgs e = new NUIApplicationPausedEventArgs();
697                 e.Application = this;
698                 applicationPauseEventHandler.Invoke(this, e);
699             }
700         }
701
702         /**
703           * @brief Event for Resumed signal which can be used to subscribe/unsubscribe the event handler
704           *  provided by the user. Resumed signal is emitted when application is resumed
705           */
706         public event DaliEventHandler<object, NUIApplicationResumedEventArgs> Resumed
707         {
708             add
709             {
710                 // Restricted to only one listener
711                 if (applicationResumeEventHandler == null)
712                 {
713                     applicationResumeEventHandler += value;
714
715                     applicationResumeEventCallbackDelegate = new NUIApplicationResumeEventCallbackDelegate(OnNUIApplicationResume);
716                     resumeSignal = this.ResumeSignal();
717                     resumeSignal?.Connect(applicationResumeEventCallbackDelegate);
718                 }
719             }
720
721             remove
722             {
723                 if (applicationResumeEventHandler != null)
724                 {
725                     resumeSignal?.Disconnect(applicationResumeEventCallbackDelegate);
726                     resumeSignal?.Dispose();
727                     resumeSignal = null;
728                 }
729
730                 applicationResumeEventHandler -= value;
731             }
732         }
733
734         // Callback for Application ResumeSignal
735         private void OnNUIApplicationResume(IntPtr data)
736         {
737             if (applicationResumeEventHandler != null)
738             {
739                 NUIApplicationResumedEventArgs e = new NUIApplicationResumedEventArgs();
740                 e.Application = this;
741                 applicationResumeEventHandler.Invoke(this, e);
742             }
743         }
744
745         /**
746           * @brief Event for Reset signal which can be used to subscribe/unsubscribe the event handler
747           *  provided by the user. Reset signal is emitted when application is reset
748           */
749         public new event DaliEventHandler<object, NUIApplicationResetEventArgs> Reset
750         {
751             add
752             {
753                 // Restricted to only one listener
754                 if (applicationResetEventHandler == null)
755                 {
756                     applicationResetEventHandler += value;
757
758                     applicationResetEventCallbackDelegate = new NUIApplicationResetEventCallbackDelegate(OnNUIApplicationReset);
759                     resetSignal = this.ResetSignal();
760                     resetSignal?.Connect(applicationResetEventCallbackDelegate);
761                 }
762             }
763
764             remove
765             {
766                 if (applicationResetEventHandler != null)
767                 {
768                     resetSignal?.Disconnect(applicationResetEventCallbackDelegate);
769                     resetSignal?.Dispose();
770                     resetSignal = null;
771                 }
772
773                 applicationResetEventHandler -= value;
774             }
775         }
776
777         // Callback for Application ResetSignal
778         private void OnNUIApplicationReset(IntPtr data)
779         {
780             if (applicationResetEventHandler != null)
781             {
782                 NUIApplicationResetEventArgs e = new NUIApplicationResetEventArgs();
783                 e.Application = this;
784                 applicationResetEventHandler.Invoke(this, e);
785             }
786         }
787
788         /**
789           * @brief Event for LanguageChanged signal which can be used to subscribe/unsubscribe the event handler
790           *  provided by the user. LanguageChanged signal is emitted when the region of the device is changed.
791           */
792         public event DaliEventHandler<object, NUIApplicationLanguageChangedEventArgs> LanguageChanged
793         {
794             add
795             {
796                 // Restricted to only one listener
797                 if (applicationLanguageChangedEventHandler == null)
798                 {
799                     applicationLanguageChangedEventHandler += value;
800
801                     applicationLanguageChangedEventCallbackDelegate = new NUIApplicationLanguageChangedEventCallbackDelegate(OnNUIApplicationLanguageChanged);
802                     languageChangedSignal = this.LanguageChangedSignal();
803                     languageChangedSignal?.Connect(applicationLanguageChangedEventCallbackDelegate);
804                 }
805             }
806
807             remove
808             {
809                 if (applicationLanguageChangedEventHandler != null)
810                 {
811                     languageChangedSignal?.Disconnect(applicationLanguageChangedEventCallbackDelegate);
812                     languageChangedSignal?.Dispose();
813                     languageChangedSignal = null;
814                 }
815
816                 applicationLanguageChangedEventHandler -= value;
817             }
818         }
819
820         // Callback for Application LanguageChangedSignal
821         private void OnNUIApplicationLanguageChanged(IntPtr data)
822         {
823             if (applicationLanguageChangedEventHandler != null)
824             {
825                 NUIApplicationLanguageChangedEventArgs e = new NUIApplicationLanguageChangedEventArgs();
826                 e.Application = this;
827                 applicationLanguageChangedEventHandler.Invoke(this, e);
828             }
829         }
830
831         /**
832           * @brief Event for RegionChanged signal which can be used to subscribe/unsubscribe the event handler
833           *  provided by the user. RegionChanged signal is emitted when the region of the device is changed.
834           */
835         public event DaliEventHandler<object, NUIApplicationRegionChangedEventArgs> RegionChanged
836         {
837             add
838             {
839                 // Restricted to only one listener
840                 if (applicationRegionChangedEventHandler == null)
841                 {
842                     applicationRegionChangedEventHandler += value;
843
844                     applicationRegionChangedEventCallbackDelegate = new NUIApplicationRegionChangedEventCallbackDelegate(OnNUIApplicationRegionChanged);
845                     regionChangedSignal = this.RegionChangedSignal();
846                     regionChangedSignal?.Connect(applicationRegionChangedEventCallbackDelegate);
847                 }
848             }
849
850             remove
851             {
852                 if (applicationRegionChangedEventHandler != null)
853                 {
854                     regionChangedSignal?.Disconnect(applicationRegionChangedEventCallbackDelegate);
855                     regionChangedSignal?.Dispose();
856                     regionChangedSignal = null;
857                 }
858
859                 applicationRegionChangedEventHandler -= value;
860             }
861         }
862
863         // Callback for Application RegionChangedSignal
864         private void OnNUIApplicationRegionChanged(IntPtr data)
865         {
866             if (applicationRegionChangedEventHandler != null)
867             {
868                 NUIApplicationRegionChangedEventArgs e = new NUIApplicationRegionChangedEventArgs();
869                 e.Application = this;
870                 applicationRegionChangedEventHandler.Invoke(this, e);
871             }
872         }
873
874         /**
875           * @brief Event for BatteryLow signal which can be used to subscribe/unsubscribe the event handler
876           * provided by the user. BatteryLow signal is emitted when the battery level of the device is low.
877           */
878         public event DaliEventHandler<object, NUIApplicationBatteryLowEventArgs> BatteryLow
879         {
880             add
881             {
882                 // Restricted to only one listener
883                 if (applicationBatteryLowEventHandler == null)
884                 {
885                     applicationBatteryLowEventHandler += value;
886
887                     applicationBatteryLowEventCallbackDelegate = new NUIApplicationBatteryLowEventCallbackDelegate(OnNUIApplicationBatteryLow);
888                     batteryLowSignal = this.BatteryLowSignal();
889                     batteryLowSignal?.Connect(applicationBatteryLowEventCallbackDelegate);
890                 }
891             }
892
893             remove
894             {
895                 if (applicationBatteryLowEventHandler != null)
896                 {
897                     batteryLowSignal?.Disconnect(applicationBatteryLowEventCallbackDelegate);
898                     batteryLowSignal?.Dispose();
899                     batteryLowSignal = null;
900                 }
901
902                 applicationBatteryLowEventHandler -= value;
903             }
904         }
905
906         // Callback for Application BatteryLowSignal
907         private void OnNUIApplicationBatteryLow(BatteryStatus status)
908         {
909             NUIApplicationBatteryLowEventArgs e = new NUIApplicationBatteryLowEventArgs();
910
911             // Populate all members of "e" (NUIApplicationBatteryLowEventArgs) with real data
912             e.BatteryStatus = status;
913             applicationBatteryLowEventHandler?.Invoke(this, e);
914         }
915
916         /**
917           * @brief Event for MemoryLow signal which can be used to subscribe/unsubscribe the event handler
918           *  provided by the user. MemoryLow signal is emitted when the memory level of the device is low.
919           */
920         public event DaliEventHandler<object, NUIApplicationMemoryLowEventArgs> MemoryLow
921         {
922             add
923             {
924                 // Restricted to only one listener
925                 if (applicationMemoryLowEventHandler == null)
926                 {
927                     applicationMemoryLowEventHandler += value;
928
929                     applicationMemoryLowEventCallbackDelegate = new NUIApplicationMemoryLowEventCallbackDelegate(OnNUIApplicationMemoryLow);
930                     memoryLowSignal = this.MemoryLowSignal();
931                     memoryLowSignal?.Connect(applicationMemoryLowEventCallbackDelegate);
932                 }
933             }
934
935             remove
936             {
937                 if (applicationMemoryLowEventHandler != null)
938                 {
939                     memoryLowSignal?.Disconnect(applicationMemoryLowEventCallbackDelegate);
940                     memoryLowSignal?.Dispose();
941                     memoryLowSignal = null;
942                 }
943
944                 applicationMemoryLowEventHandler -= value;
945             }
946         }
947
948         // Callback for Application MemoryLowSignal
949         private void OnNUIApplicationMemoryLow(MemoryStatus status)
950         {
951             NUIApplicationMemoryLowEventArgs e = new NUIApplicationMemoryLowEventArgs();
952
953             // Populate all members of "e" (NUIApplicationMemoryLowEventArgs) with real data
954             e.MemoryStatus = status;
955             applicationMemoryLowEventHandler?.Invoke(this, e);
956         }
957
958         /**
959           * @brief Event for AppControl signal which can be used to subscribe/unsubscribe the event handler
960           *  provided by the user. AppControl signal is emitted when another application sends a launch request to the application.
961           */
962         public event DaliEventHandler<object, NUIApplicationAppControlEventArgs> AppControl
963         {
964             add
965             {
966                 // Restricted to only one listener
967                 if (applicationAppControlEventHandler == null)
968                 {
969                     applicationAppControlEventHandler += value;
970
971                     applicationAppControlEventCallbackDelegate = new NUIApplicationAppControlEventCallbackDelegate(OnNUIApplicationAppControl);
972                     appControlSignal = this.AppControlSignal();
973                     appControlSignal?.Connect(applicationAppControlEventCallbackDelegate);
974                 }
975             }
976
977             remove
978             {
979                 if (applicationAppControlEventHandler != null)
980                 {
981                     appControlSignal?.Disconnect(applicationAppControlEventCallbackDelegate);
982                     appControlSignal?.Dispose();
983                     appControlSignal = null;
984                 }
985
986                 applicationAppControlEventHandler -= value;
987             }
988         }
989
990         // Callback for Application AppControlSignal
991         private void OnNUIApplicationAppControl(IntPtr application, IntPtr voidp)
992         {
993             if (applicationAppControlEventHandler != null)
994             {
995                 NUIApplicationAppControlEventArgs e = new NUIApplicationAppControlEventArgs();
996                 e.VoidP = voidp;
997                 e.Application = this;
998                 applicationAppControlEventHandler.Invoke(this, e);
999             }
1000         }
1001
1002         protected static Application instance; // singleton
1003
1004         public static Application Instance
1005         {
1006             get
1007             {
1008                 return instance;
1009             }
1010         }
1011
1012         public static Application GetApplicationFromPtr(global::System.IntPtr cPtr)
1013         {
1014             if (cPtr == global::System.IntPtr.Zero)
1015             {
1016                 return null;
1017             }
1018
1019             Application ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Application;
1020             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1021             return ret;
1022         }
1023
1024         public static Application NewApplication()
1025         {
1026             return NewApplication("", NUIApplication.WindowMode.Opaque);
1027         }
1028
1029         public static Application NewApplication(string stylesheet)
1030         {
1031             return NewApplication(stylesheet, NUIApplication.WindowMode.Opaque);
1032         }
1033
1034         public static Application NewApplication(string stylesheet, NUIApplication.WindowMode windowMode)
1035         {
1036             // register all Views with the type registry, so that can be created / styled via JSON
1037             //ViewRegistryHelper.Initialize(); //moved to Application side.
1038             if (instance)
1039             {
1040                 return instance;
1041             }
1042
1043             Application ret = New(1, stylesheet, windowMode);
1044             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1045
1046             // set the singleton
1047             instance = ret;
1048             return ret;
1049         }
1050
1051         public static Application NewApplication(string stylesheet, NUIApplication.WindowMode windowMode, Rectangle positionSize)
1052         {
1053             if (instance)
1054             {
1055                 return instance;
1056             }
1057             Application ret = New(1, stylesheet, windowMode, positionSize);
1058             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1059
1060             // set the singleton
1061             instance = ret;
1062             return ret;
1063         }
1064
1065         public static Application NewApplication(string[] args, string stylesheet, NUIApplication.WindowMode windowMode)
1066         {
1067             if (instance)
1068             {
1069                 return instance;
1070             }
1071             Application ret = New(args, stylesheet, windowMode);
1072             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1073
1074             // set the singleton
1075             instance = ret;
1076             return instance;
1077         }
1078
1079         public static Application NewApplication(string[] args, string stylesheet, NUIApplication.WindowMode windowMode, Rectangle positionSize)
1080         {
1081             if (instance)
1082             {
1083                 return instance;
1084             }
1085             Application ret = New(args, stylesheet, windowMode, positionSize);
1086             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1087
1088             // set the singleton
1089             instance = ret;
1090             return instance;
1091         }
1092
1093         /// <summary>
1094         /// Ensures that the function passed in is called from the main loop when it is idle.
1095         /// </summary>
1096         /// <param name="func">The function to call</param>
1097         /// <returns>true if added successfully, false otherwise</returns>
1098         public bool AddIdle(System.Delegate func)
1099         {
1100             System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(func);
1101             System.IntPtr ip2 = Interop.Application.MakeCallback(new System.Runtime.InteropServices.HandleRef(this, ip));
1102
1103             bool ret = Interop.Application.AddIdle(SwigCPtr, new System.Runtime.InteropServices.HandleRef(this, ip2));
1104
1105             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1106             return ret;
1107         }
1108
1109         /**
1110         * Outer::outer_method(int)
1111         */
1112         public static Application New()
1113         {
1114             Application ret = new Application(Interop.Application.New(), true);
1115             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1116             return ret;
1117         }
1118
1119         public static Application New(int argc)
1120         {
1121             Application ret = new Application(Interop.Application.New(argc), true);
1122             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1123             return ret;
1124         }
1125
1126         public static Application New(int argc, string stylesheet)
1127         {
1128             Application ret = new Application(Interop.Application.New(argc, stylesheet), true);
1129             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1130             return ret;
1131         }
1132
1133         public static Application New(int argc, string stylesheet, NUIApplication.WindowMode windowMode)
1134         {
1135             Application ret = new Application(Interop.Application.New(argc, stylesheet, (int)windowMode), true);
1136             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1137             s_current = ret;
1138             return ret;
1139         }
1140
1141         public static Application New(string[] args, string stylesheet, NUIApplication.WindowMode windowMode)
1142         {
1143             Application ret = null;
1144             int argc = 0;
1145             string argvStr = "";
1146             try
1147             {
1148                 argc = args.Length;
1149                 argvStr = string.Join(" ", args);
1150             }
1151             catch (Exception exception)
1152             {
1153                 Tizen.Log.Fatal("NUI", "[Error] got exception during Application New(), this should not occur, message : " + exception.Message);
1154                 Tizen.Log.Fatal("NUI", "[Error] error line number : " + new StackTrace(exception, true).GetFrame(0).GetFileLineNumber());
1155                 Tizen.Log.Fatal("NUI", "[Error] Stack Trace : " + exception.StackTrace);
1156                 throw;
1157             }
1158
1159             ret = new Application(NDalicPINVOKE.ApplicationNewManual4(argc, argvStr, stylesheet, (int)windowMode), true);
1160             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1161
1162             return ret;
1163         }
1164
1165         public static Application New(int argc, string stylesheet, NUIApplication.WindowMode windowMode, Rectangle positionSize)
1166         {
1167             Application ret = new Application(Interop.Application.New(argc, stylesheet, (int)windowMode, Rectangle.getCPtr(positionSize)), true);
1168             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1169             return ret;
1170         }
1171
1172         public static Application New(string[] args, string stylesheet, NUIApplication.WindowMode windowMode, Rectangle positionSize)
1173         {
1174             Application ret = null;
1175             int argc = 0;
1176             string argvStr = "";
1177             try
1178             {
1179                 argc = args.Length;
1180                 argvStr = string.Join(" ", args);
1181             }
1182             catch (Exception exception)
1183             {
1184                 Tizen.Log.Fatal("NUI", "[Error] got exception during Application New(), this should not occur, message : " + exception.Message);
1185                 Tizen.Log.Fatal("NUI", "[Error] error line number : " + new StackTrace(exception, true).GetFrame(0).GetFileLineNumber());
1186                 Tizen.Log.Fatal("NUI", "[Error] Stack Trace : " + exception.StackTrace);
1187                 throw;
1188             }
1189
1190             ret = new Application(NDalicPINVOKE.ApplicationNewWithWindowSizePosition(argc, argvStr, stylesheet, (int)windowMode, Rectangle.getCPtr(positionSize)), true);
1191             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1192
1193             return ret;
1194         }
1195
1196         public Application() : this(Interop.Application.NewApplication(), true)
1197         {
1198             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1199         }
1200
1201         public Application(Application application) : this(Interop.Application.NewApplication(Application.getCPtr(application)), true)
1202         {
1203             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1204         }
1205
1206         public Application Assign(Application application)
1207         {
1208             Application ret = new Application(Interop.Application.Assign(SwigCPtr, Application.getCPtr(application)), false);
1209             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1210             return ret;
1211         }
1212
1213         public void MainLoop()
1214         {
1215             NDalicPINVOKE.ApplicationMainLoop(SwigCPtr);
1216             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1217         }
1218
1219         public void Lower()
1220         {
1221             Interop.Application.Lower(SwigCPtr);
1222             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1223         }
1224
1225         public void Quit()
1226         {
1227             Interop.Application.Quit(SwigCPtr);
1228             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1229         }
1230
1231         internal bool AddIdle(SWIGTYPE_p_Dali__CallbackBase callback)
1232         {
1233             bool ret = Interop.Application.AddIdle(SwigCPtr, SWIGTYPE_p_Dali__CallbackBase.getCPtr(callback));
1234             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1235             return ret;
1236         }
1237
1238         public Window GetWindow()
1239         {
1240             if (window != null)
1241             {
1242                 return window;
1243             }
1244
1245             window = (Registry.GetManagedBaseHandleFromNativePtr(Interop.Application.GetWindow(SwigCPtr)) as Window) ?? new Window(Interop.Application.GetWindow(SwigCPtr), true);
1246
1247             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1248             return window;
1249         }
1250
1251         public static string GetResourcePath()
1252         {
1253             string ret = Interop.Application.GetResourcePath();
1254             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1255             return ret;
1256         }
1257
1258         public string GetLanguage()
1259         {
1260             string ret = Interop.Application.GetLanguage(SwigCPtr);
1261             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1262             return ret;
1263         }
1264
1265         public string GetRegion()
1266         {
1267             string ret = Interop.Application.GetRegion(SwigCPtr);
1268             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1269             return ret;
1270         }
1271
1272         [EditorBrowsable(EditorBrowsableState.Never)]
1273         public static List<Window> GetWindowList()
1274         {
1275             uint ListSize = Interop.Application.GetWindowsListSize();
1276             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1277
1278             List<Window> WindowList = new List<Window>();
1279             for (uint i = 0; i < ListSize; ++i)
1280             {
1281                 Window currWin = Registry.GetManagedBaseHandleFromNativePtr(Interop.Application.GetWindowsFromList(i)) as Window;
1282                 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1283                 if (currWin)
1284                 {
1285                     WindowList.Add(currWin);
1286                 }
1287             }
1288             return WindowList;
1289         }
1290
1291         internal ApplicationSignal InitSignal()
1292         {
1293             ApplicationSignal ret = new ApplicationSignal(NDalicPINVOKE.ApplicationInitSignal(SwigCPtr), false);
1294             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1295             return ret;
1296         }
1297
1298         internal ApplicationSignal TerminateSignal()
1299         {
1300             ApplicationSignal ret = new ApplicationSignal(NDalicPINVOKE.ApplicationTerminateSignal(SwigCPtr), false);
1301             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1302             return ret;
1303         }
1304
1305         internal ApplicationSignal PauseSignal()
1306         {
1307             ApplicationSignal ret = new ApplicationSignal(NDalicPINVOKE.ApplicationPauseSignal(SwigCPtr), false);
1308             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1309             return ret;
1310         }
1311
1312         internal ApplicationSignal ResumeSignal()
1313         {
1314             ApplicationSignal ret = new ApplicationSignal(NDalicPINVOKE.ApplicationResumeSignal(SwigCPtr), false);
1315             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1316             return ret;
1317         }
1318
1319         internal ApplicationSignal ResetSignal()
1320         {
1321             ApplicationSignal ret = new ApplicationSignal(NDalicPINVOKE.ApplicationResetSignal(SwigCPtr), false);
1322             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1323             return ret;
1324         }
1325
1326         internal ApplicationControlSignal AppControlSignal()
1327         {
1328             ApplicationControlSignal ret = new ApplicationControlSignal(NDalicPINVOKE.ApplicationAppControlSignal(SwigCPtr), false);
1329             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1330             return ret;
1331         }
1332
1333         internal ApplicationSignal LanguageChangedSignal()
1334         {
1335             ApplicationSignal ret = new ApplicationSignal(NDalicPINVOKE.ApplicationLanguageChangedSignal(SwigCPtr), false);
1336             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1337             return ret;
1338         }
1339
1340         internal ApplicationSignal RegionChangedSignal()
1341         {
1342             ApplicationSignal ret = new ApplicationSignal(NDalicPINVOKE.ApplicationRegionChangedSignal(SwigCPtr), false);
1343             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1344             return ret;
1345         }
1346
1347         internal LowBatterySignalType BatteryLowSignal()
1348         {
1349             LowBatterySignalType ret = new LowBatterySignalType(NDalicPINVOKE.ApplicationLowBatterySignal(SwigCPtr), false);
1350             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1351             return ret;
1352         }
1353
1354         internal LowMemorySignalType MemoryLowSignal()
1355         {
1356             LowMemorySignalType ret = new LowMemorySignalType(NDalicPINVOKE.ApplicationLowMemorySignal(SwigCPtr), false);
1357             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
1358             return ret;
1359         }
1360     }
1361 }