[NUI] Change all CallingConvention to `Cdecl`
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Application / ComponentApplication.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
18 using System;
19 using System.Runtime.InteropServices;
20
21 namespace Tizen.NUI
22 {
23     class ComponentApplication : Application
24     {
25         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
26
27         internal ComponentApplication(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
28         {
29             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
30         }
31
32         protected override void Dispose(DisposeTypes type)
33         {
34             if (disposed)
35             {
36                 return;
37             }
38
39             //Release your own unmanaged resources here.
40             //You should not access any managed member here except static instance.
41             //because the execution order of Finalizes is non-deterministic.
42             if (swigCPtr.Handle != global::System.IntPtr.Zero)
43             {
44                 if (swigCMemOwn)
45                 {
46                     swigCMemOwn = false;
47                     Interop.ComponentApplication.DeleteComponentApplication(swigCPtr);
48                 }
49                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
50             }
51             base.Dispose(type);
52         }
53
54         public static ComponentApplication NewComponentApplication(string[] args, string stylesheet)
55         {
56             ComponentApplication ret = New(args, stylesheet);
57             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
58             instance = ret;
59             return ret;
60         }
61
62         public static ComponentApplication New(string[] args, string stylesheet)
63         {
64             int argc = args.Length;
65             string argvStr = string.Join(" ", args);
66
67             IntPtr widgetIntPtr = Interop.ComponentApplication.New(argc, argvStr, stylesheet);
68
69             ComponentApplication ret = new ComponentApplication(widgetIntPtr, false);
70
71             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
72
73             return ret;
74         }
75
76         internal ComponentApplication(ComponentApplication componentApplication) : this(Interop.ComponentApplication.NewComponentApplication(ComponentApplication.getCPtr(componentApplication)), true)
77         {
78             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
79         }
80
81         internal ComponentApplication Assign(ComponentApplication componentApplication)
82         {
83             ComponentApplication ret = new ComponentApplication(Interop.ComponentApplication.Assign(swigCPtr, ComponentApplication.getCPtr(componentApplication)), false);
84             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
85             return ret;
86         }
87
88         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
89         private delegate IntPtr NUIComponentApplicationCreatenativeEventCallbackDelegate();
90
91         public delegate IntPtr CreateNativeEventHandler();
92         private CreateNativeEventHandler applicationCreateNativeEventHandler;
93         private NUIComponentApplicationCreatenativeEventCallbackDelegate applicationCreateNativeEventCallbackDelegate;
94
95         /**
96           * @brief Event for Initialized signal which can be used to subscribe/unsubscribe the event handler
97           *  provided by the user. Initialized signal is emitted when application is initialized
98           */
99         public event CreateNativeEventHandler CreateNative
100         {
101             add
102             {
103                 // Restricted to only one listener
104                 if (applicationCreateNativeEventHandler == null)
105                 {
106                     applicationCreateNativeEventHandler += value;
107
108                     applicationCreateNativeEventCallbackDelegate = new NUIComponentApplicationCreatenativeEventCallbackDelegate(OnApplicationCreateNative);
109                     Connect(applicationCreateNativeEventCallbackDelegate);
110                 }
111             }
112
113             remove
114             {
115                 if (applicationCreateNativeEventHandler != null)
116                 {
117                     Disconnect(applicationCreateNativeEventCallbackDelegate);
118                 }
119
120                 applicationCreateNativeEventHandler -= value;
121             }
122         }
123
124
125         public void Connect(System.Delegate func)
126         {
127             System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate(func);
128             {
129                 Interop.ComponentApplication.CreateNativeSignalConnect(swigCPtr, new System.Runtime.InteropServices.HandleRef(this, ip));
130                 if (NDalicPINVOKE.SWIGPendingException.Pending)
131                 {
132                     throw NDalicPINVOKE.SWIGPendingException.Retrieve();
133                 }
134             }
135         }
136
137         public void Disconnect(System.Delegate func)
138         {
139             System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(func);
140             {
141                 Interop.ComponentApplication.CreateNativeSignalDisconnect(swigCPtr, new System.Runtime.InteropServices.HandleRef(this, ip));
142                 if (NDalicPINVOKE.SWIGPendingException.Pending)
143                 {
144                     throw NDalicPINVOKE.SWIGPendingException.Retrieve();
145                 }
146             }
147         }
148
149         internal ApplicationSignal CreateNativeSignal()
150         {
151             ApplicationSignal ret = new ApplicationSignal(Interop.ComponentApplication.CreateNativeSignal(swigCPtr), false);
152             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
153             return ret;
154         }
155
156         // Callback for Application InitSignal
157         private IntPtr OnApplicationCreateNative()
158         {
159             return applicationCreateNativeEventHandler?.Invoke() ?? IntPtr.Zero;
160         }
161     }
162 }