[NUI] TCSACR-226 code change (#1032)
[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             //Release your own unmanaged resources here.
48             //You should not access any managed member here except static instance.
49             //because the execution order of Finalizes is non-deterministic.
50             if (swigCPtr.Handle != global::System.IntPtr.Zero)
51             {
52                 if (swigCMemOwn)
53                 {
54                     swigCMemOwn = false;
55                     Interop.WidgetApplication.delete_WidgetApplication(swigCPtr);
56                 }
57                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
58             }
59             base.Dispose(type);
60         }
61
62         public new static WidgetApplication Instance
63         {
64             get
65             {
66                 return _instance;
67             }
68         }
69
70         public static WidgetApplication NewWidgetApplication(string[] args, string stylesheet)
71         {
72             WidgetApplication ret = New(args, stylesheet);
73             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
74             _instance = ret;
75             return ret;
76         }
77
78         public static WidgetApplication New(string[] args, string stylesheet)
79         {
80             int argc = args.Length;
81             string argvStr = string.Join(" ", args);
82
83             IntPtr widgetIntPtr = Interop.WidgetApplication.WidgetApplication_New(argc, argvStr, stylesheet);
84
85             WidgetApplication ret = new WidgetApplication(widgetIntPtr, false);
86
87             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
88
89             return ret;
90         }
91
92         internal WidgetApplication(WidgetApplication widgetApplication) : this(Interop.WidgetApplication.new_WidgetApplication__SWIG_1(WidgetApplication.getCPtr(widgetApplication)), true)
93         {
94             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
95         }
96
97         internal WidgetApplication Assign(WidgetApplication widgetApplication)
98         {
99             WidgetApplication ret = new WidgetApplication(Interop.WidgetApplication.WidgetApplication_Assign(swigCPtr, WidgetApplication.getCPtr(widgetApplication)), false);
100             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
101             return ret;
102         }
103
104         public void RegisterWidgetCreatingFunction()
105         {
106             foreach (KeyValuePair<System.Type, string> widgetInfo in _widgetInfo)
107             {
108                 string widgetName = widgetInfo.Value;
109                 RegisterWidgetCreatingFunction(ref widgetName);
110             }
111         }
112
113         internal void RegisterWidgetCreatingFunction(ref string widgetName)
114         {
115             CreateWidgetFunctionDelegate newDelegate = new CreateWidgetFunctionDelegate(WidgetCreateFunction);
116
117             System.IntPtr ip = System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate<System.Delegate>(newDelegate);
118             CreateWidgetFunction createWidgetFunction = new CreateWidgetFunction(ip, true);
119
120             Interop.WidgetApplication.WidgetApplication_RegisterWidgetCreatingFunction(swigCPtr, ref widgetName, CreateWidgetFunction.getCPtr(createWidgetFunction));
121             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
122         }
123
124         public void AddWidgetInstance( Widget widget )
125         {
126             _widgetList.Add(widget);
127         }
128
129         public void RegisterWidgetInfo(Dictionary<System.Type, string> widgetInfo)
130         {
131             _widgetInfo = widgetInfo;
132         }
133
134         public static System.IntPtr WidgetCreateFunction(ref string widgetName)
135         {
136             Dictionary<System.Type, string> widgetInfo = Instance.WidgetInfo;
137
138             foreach (System.Type widgetType in widgetInfo.Keys)
139             {
140                 if (widgetInfo[widgetType] == widgetName)
141                 {
142                     Widget widget = Activator.CreateInstance(widgetType) as Widget;
143                     if (widget)
144                     {
145                         return widget.GetIntPtr();
146                     }
147                 }
148             }
149
150             return IntPtr.Zero;
151         }
152
153         public Dictionary<System.Type, string> WidgetInfo
154         {
155             get
156             {
157                 return _widgetInfo;
158             }
159         }
160     }
161 }