[NUI] Add license, delete unnecessary code (#2679)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Widget / WidgetApplication.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
20 namespace Tizen.NUI
21 {
22     internal class WidgetApplication : Application
23     {
24         private Dictionary<System.Type, string> widgetInfo;
25         private List<Widget> widgetList = new List<Widget>();
26         private delegate System.IntPtr CreateWidgetFunctionDelegate(ref string widgetName);
27         private List<CreateWidgetFunctionDelegate> createWidgetFunctionDelegateList = new List<CreateWidgetFunctionDelegate>();
28
29         internal WidgetApplication(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
30         {
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         protected override void ReleaseSwigCPtr(System.Runtime.InteropServices.HandleRef swigCPtr)
39         {
40             Interop.WidgetApplication.DeleteWidgetApplication(swigCPtr);
41         }
42
43         public static WidgetApplication NewWidgetApplication(string[] args, string stylesheet)
44         {
45             WidgetApplication ret = New(args, stylesheet);
46             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
47             instance = ret;
48             return ret;
49         }
50
51         public static WidgetApplication New(string[] args, string stylesheet)
52         {
53             int argc = args.Length;
54             string argvStr = string.Join(" ", args);
55
56             IntPtr widgetIntPtr = Interop.WidgetApplication.New(argc, argvStr, stylesheet);
57
58             WidgetApplication ret = new WidgetApplication(widgetIntPtr, false);
59
60             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
61
62             return ret;
63         }
64
65         internal WidgetApplication(WidgetApplication widgetApplication) : this(Interop.WidgetApplication.NewWidgetApplication(WidgetApplication.getCPtr(widgetApplication)), true)
66         {
67             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
68         }
69
70         internal WidgetApplication Assign(WidgetApplication widgetApplication)
71         {
72             WidgetApplication ret = new WidgetApplication(Interop.WidgetApplication.Assign(SwigCPtr, WidgetApplication.getCPtr(widgetApplication)), false);
73             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
74             return ret;
75         }
76
77         public void RegisterWidgetCreatingFunction()
78         {
79             foreach (KeyValuePair<System.Type, string> widgetInfo in widgetInfo)
80             {
81                 string widgetName = widgetInfo.Value;
82                 RegisterWidgetCreatingFunction(ref widgetName);
83             }
84         }
85
86         internal void RegisterWidgetCreatingFunction(ref string widgetName)
87         {
88             CreateWidgetFunctionDelegate newDelegate = new CreateWidgetFunctionDelegate(WidgetCreateFunction);
89
90             // Keep this delegate until WidgetApplication is terminated
91             createWidgetFunctionDelegateList.Add(newDelegate);
92
93             System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(newDelegate);
94             CreateWidgetFunction createWidgetFunction = new CreateWidgetFunction(ip);
95
96             Interop.WidgetApplication.RegisterWidgetCreatingFunction(SwigCPtr, ref widgetName, CreateWidgetFunction.getCPtr(createWidgetFunction));
97             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
98         }
99
100         public void AddWidgetInstance(Widget widget)
101         {
102             widgetList.Add(widget);
103         }
104
105         public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
106         {
107             this.widgetInfo = widgetInfo;
108         }
109
110         public void AddWidgetInfo(Dictionary<Type, string> newWidgetInfo)
111         {
112             foreach (KeyValuePair<Type, string> widgetInfo in newWidgetInfo)
113             {
114                 if (this.widgetInfo.ContainsKey(widgetInfo.Key) == false)
115                 {
116                     this.widgetInfo.Add(widgetInfo.Key, widgetInfo.Value);
117                     string widgetName = widgetInfo.Value;
118                     RegisterWidgetCreatingFunction(ref widgetName);
119                 }
120             }
121         }
122
123         public static System.IntPtr WidgetCreateFunction(ref string widgetName)
124         {
125             if ((Instance as WidgetApplication) == null)
126             {
127                 return IntPtr.Zero;
128             }
129
130             Dictionary<System.Type, string> widgetInfo = (Instance as WidgetApplication).WidgetInfo;
131
132             foreach (System.Type widgetType in widgetInfo.Keys)
133             {
134                 if (widgetInfo[widgetType] == widgetName)
135                 {
136                     Widget widget = Activator.CreateInstance(widgetType) as Widget;
137                     if (widget != null)
138                     {
139                         return widget.GetIntPtr();
140                     }
141                 }
142             }
143
144             return IntPtr.Zero;
145         }
146
147         public Dictionary<System.Type, string> WidgetInfo
148         {
149             get
150             {
151                 return widgetInfo;
152             }
153         }
154     }
155 }