[NUI] Fix Svace issue (#949)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / WidgetApplication.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.Collections.Generic;
19
20 namespace Tizen.NUI
21 {
22     internal class WidgetApplication : Application
23     {
24         private global::System.Runtime.InteropServices.HandleRef swigCPtr;
25         private static WidgetApplication _instance; //singleton
26         private Dictionary<System.Type, string> _widgetInfo;
27         private List<Widget> _widgetList = new List<Widget>();
28         private delegate System.IntPtr CreateWidgetFunctionDelegate(ref string widgetName);
29
30         internal WidgetApplication(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
31         {
32             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
33         }
34
35         internal static global::System.Runtime.InteropServices.HandleRef getCPtr(WidgetApplication obj)
36         {
37             return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
38         }
39
40         protected override void Dispose(DisposeTypes type)
41         {
42             if (disposed)
43             {
44                 return;
45             }
46
47             if (type == DisposeTypes.Explicit)
48             {
49                 //Called by User
50                 //Release your own managed resources here.
51                 //You should release all of your own disposable objects here.
52             }
53
54             //Release your own unmanaged resources here.
55             //You should not access any managed member here except static instance.
56             //because the execution order of Finalizes is non-deterministic.
57             if (swigCPtr.Handle != global::System.IntPtr.Zero)
58             {
59                 if (swigCMemOwn)
60                 {
61                     swigCMemOwn = false;
62                     Interop.WidgetApplication.delete_WidgetApplication(swigCPtr);
63                 }
64                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
65             }
66             base.Dispose(type);
67         }
68
69         public new static WidgetApplication Instance
70         {
71             get
72             {
73                 return _instance;
74             }
75         }
76
77         public static WidgetApplication NewWidgetApplication(string[] args, string stylesheet)
78         {
79             WidgetApplication ret = New(args, stylesheet);
80             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
81             _instance = ret;
82             return ret;
83         }
84
85         public static WidgetApplication New(string[] args, string stylesheet)
86         {
87             int argc = args.Length;
88             string argvStr = string.Join(" ", args);
89
90             IntPtr widgetIntPtr = Interop.WidgetApplication.WidgetApplication_New(argc, argvStr, stylesheet);
91
92             WidgetApplication ret = new WidgetApplication(widgetIntPtr, false);
93
94             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
95
96             return ret;
97         }
98
99         internal WidgetApplication(WidgetApplication widgetApplication) : this(Interop.WidgetApplication.new_WidgetApplication__SWIG_1(WidgetApplication.getCPtr(widgetApplication)), true)
100         {
101             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
102         }
103
104         internal WidgetApplication Assign(WidgetApplication widgetApplication)
105         {
106             WidgetApplication ret = new WidgetApplication(Interop.WidgetApplication.WidgetApplication_Assign(swigCPtr, WidgetApplication.getCPtr(widgetApplication)), false);
107             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
108             return ret;
109         }
110
111         public void RegisterWidgetCreatingFunction()
112         {
113             foreach (KeyValuePair<System.Type, string> widgetInfo in _widgetInfo)
114             {
115                 string widgetName = widgetInfo.Value;
116                 RegisterWidgetCreatingFunction(ref widgetName);
117             }
118         }
119
120         internal void RegisterWidgetCreatingFunction(ref string widgetName)
121         {
122             CreateWidgetFunctionDelegate newDelegate = new CreateWidgetFunctionDelegate(WidgetCreateFunction);
123
124             System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(newDelegate);
125             CreateWidgetFunction createWidgetFunction = new CreateWidgetFunction(ip, true);
126
127             Interop.WidgetApplication.WidgetApplication_RegisterWidgetCreatingFunction(swigCPtr, ref widgetName, CreateWidgetFunction.getCPtr(createWidgetFunction));
128             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
129         }
130
131         public void AddWidgetInstance( Widget widget )
132         {
133             _widgetList.Add(widget);
134         }
135
136         public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
137         {
138             _widgetInfo = widgetInfo;
139         }
140
141         public static System.IntPtr WidgetCreateFunction(ref string widgetName)
142         {
143             Dictionary<System.Type, string> widgetInfo = Instance.WidgetInfo;
144
145             foreach (System.Type widgetType in widgetInfo.Keys)
146             {
147                 if (widgetInfo[widgetType] == widgetName)
148                 {
149                     Widget widget = Activator.CreateInstance(widgetType) as Widget;
150                     if (widget)
151                     {
152                         return widget.GetIntPtr();
153                     }
154                 }
155             }
156
157             return IntPtr.Zero;
158         }
159
160         public Dictionary<System.Type, string> WidgetInfo
161         {
162             get
163             {
164                 return _widgetInfo;
165             }
166         }
167     }
168 }