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