Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Applications.WidgetApplication / Tizen.Applications / WidgetType.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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.Applications
21 {
22     internal class WidgetType
23     {
24         internal readonly Type ClassType;
25         internal readonly string Id;
26         internal IList<WidgetBase> WidgetInstances = new List<WidgetBase>();
27
28         private Interop.Widget.WidgetiInstanceLifecycleCallbacks _callbacks;
29
30         internal WidgetType(Type ctype, string id)
31         {
32             ClassType = ctype;
33             Id = id;
34             _callbacks.OnCreate = new Interop.Widget.WidgetInstanceCreateCallback(OnCreate);
35             _callbacks.OnDestroy = new Interop.Widget.WidgetInstanceDestroyCallback(OnDestroy);
36             _callbacks.OnPause = new Interop.Widget.WidgetInstancePauseCallback(OnPause);
37             _callbacks.OnResume = new Interop.Widget.WidgetInstanceResumeCallback(OnResume);
38             _callbacks.OnResize = new Interop.Widget.WidgetInstanceResizeCallback(OnResize);
39             _callbacks.OnUpdate = new Interop.Widget.WidgetInstanceUpdateCallback(OnUpdate);
40         }
41
42         internal IntPtr Bind(IntPtr h)
43         {
44             return Interop.Widget.AddClass(h, Id, _callbacks, IntPtr.Zero);
45         }
46
47         private int OnCreate(IntPtr context, IntPtr content, int w, int h, IntPtr userData)
48         {
49             WidgetBase b = Activator.CreateInstance(ClassType) as WidgetBase;
50             Bundle bundle = null;
51
52             if (b == null)
53                 return 0;
54
55             b.Bind(context, Id);
56             WidgetInstances.Add(b);
57             if (content != IntPtr.Zero)
58                 bundle = new Bundle(new SafeBundleHandle(content, false));
59             b.OnCreate(bundle, w, h);
60
61             return 0;
62         }
63
64         private int OnDestroy(IntPtr context, Interop.Widget.WidgetAppDestroyType reason, IntPtr content, IntPtr userData)
65         {
66             foreach (WidgetBase w in WidgetInstances)
67             {
68                 if(w.Handle == context)
69                 {
70                     Bundle bundle = null;
71
72                     if (content != IntPtr.Zero)
73                         bundle = new Bundle(new SafeBundleHandle(content, false));
74
75                     w.OnDestroy(reason == Interop.Widget.WidgetAppDestroyType.Permanent ?
76                         WidgetBase.WidgetDestroyType.Permanent :
77                         WidgetBase.WidgetDestroyType.Temporary, bundle);
78                     WidgetInstances.Remove(w);
79                     break;
80                 }
81             }
82
83             return 0;
84         }
85
86         private int OnPause(IntPtr context, IntPtr userData)
87         {
88             foreach (WidgetBase w in WidgetInstances)
89             {
90                 if (w.Handle == context)
91                 {
92                     w.OnPause();
93                     break;
94                 }
95             }
96             return 0;
97         }
98
99         private int OnResume(IntPtr context, IntPtr userData)
100         {
101             foreach (WidgetBase w in WidgetInstances)
102             {
103                 if (w.Handle == context)
104                 {
105                     w.OnResume();
106                     break;
107                 }
108             }
109             return 0;
110         }
111
112         private int OnResize(IntPtr context, int w, int h, IntPtr userData)
113         {
114             foreach (WidgetBase o in WidgetInstances)
115             {
116                 if (o.Handle == context)
117                 {
118                     o.OnResize(w, h);
119                     break;
120                 }
121             }
122             return 0;
123         }
124
125         private int OnUpdate(IntPtr context, IntPtr content, int force, IntPtr userData)
126         {
127             foreach (WidgetBase o in WidgetInstances)
128             {
129                 if (o.Handle == context)
130                 {
131                     Bundle bundle = null;
132
133                     if (content != IntPtr.Zero)
134                         bundle = new Bundle(new SafeBundleHandle(content, false));
135                     o.OnUpdate(bundle, force != 0 ? true : false);
136                     break;
137                 }
138             }
139             return 0;
140         }
141     }
142 }
143