18e152151de479cd849d44fef2f92db2df5e6ec1
[platform/core/csapi/widget-application.git] / Tizen.Applications.WidgetApplication / Tizen.Applications / WidgetType.cs
1 // Copyright 2016 by Samsung Electronics, Inc.,
2 //
3 // This software is the confidential and proprietary information
4 // of Samsung Electronics, Inc. ("Confidential Information"). You
5 // shall not disclose such Confidential Information and shall use
6 // it only in accordance with the terms of the license agreement
7 // you entered into with Samsung.
8
9 using System;
10 using System.Collections.Generic;
11
12 namespace Tizen.Applications
13 {
14     internal class WidgetType
15     {
16         internal readonly Type ClassType;
17         internal readonly string Id;
18         internal IList<WidgetBase> WidgetInstances = new List<WidgetBase>();
19
20         private Interop.Widget.WidgetiInstanceLifecycleCallbacks _callbacks;
21
22         internal WidgetType(Type ctype, string id)
23         {
24             ClassType = ctype;
25             Id = id;
26             _callbacks.OnCreate = new Interop.Widget.WidgetInstanceCreateCallback(OnCreate);
27             _callbacks.OnDestroy = new Interop.Widget.WidgetInstanceDestroyCallback(OnDestroy);
28             _callbacks.OnPause = new Interop.Widget.WidgetInstancePauseCallback(OnPause);
29             _callbacks.OnResume = new Interop.Widget.WidgetInstanceResumeCallback(OnResume);
30             _callbacks.OnResize = new Interop.Widget.WidgetInstanceResizeCallback(OnResize);
31             _callbacks.OnUpdate = new Interop.Widget.WidgetInstanceUpdateCallback(OnUpdate);
32         }
33
34         internal IntPtr Bind(IntPtr h)
35         {
36             return Interop.Widget.AddClass(h, Id, _callbacks, IntPtr.Zero);
37         }
38
39         private int OnCreate(IntPtr context, IntPtr content, int w, int h, IntPtr userData)
40         {
41             WidgetBase b = Activator.CreateInstance(ClassType) as WidgetBase;
42             Bundle bundle = null;
43
44             if (b == null)
45                 return 0;
46
47             b.Bind(context, Id);
48             WidgetInstances.Add(b);
49             if (content != IntPtr.Zero)
50                 bundle = new Bundle(new SafeBundleHandle(content, false));
51             b.OnCreate(bundle, w, h);
52
53             return 0;
54         }
55
56         private int OnDestroy(IntPtr context, Interop.Widget.WidgetAppDestroyType reason, IntPtr content, IntPtr userData)
57         {
58             foreach (WidgetBase w in WidgetInstances)
59             {
60                 if(w.Handle == context)
61                 {
62                     Bundle bundle = null;
63
64                     if (content != IntPtr.Zero)
65                         bundle = new Bundle(new SafeBundleHandle(content, false));
66
67                     w.OnDestroy(reason == Interop.Widget.WidgetAppDestroyType.Permanent ?
68                         WidgetBase.WidgetDestroyType.Permanent :
69                         WidgetBase.WidgetDestroyType.Temporary, bundle);
70                     WidgetInstances.Remove(w);
71                     break;
72                 }
73             }
74
75             return 0;
76         }
77
78         private int OnPause(IntPtr context, IntPtr userData)
79         {
80             foreach (WidgetBase w in WidgetInstances)
81             {
82                 if (w.Handle == context)
83                 {
84                     w.OnPause();
85                     break;
86                 }
87             }
88             return 0;
89         }
90
91         private int OnResume(IntPtr context, IntPtr userData)
92         {
93             foreach (WidgetBase w in WidgetInstances)
94             {
95                 if (w.Handle == context)
96                 {
97                     w.OnResume();
98                     break;
99                 }
100             }
101             return 0;
102         }
103
104         private int OnResize(IntPtr context, int w, int h, IntPtr userData)
105         {
106             foreach (WidgetBase o in WidgetInstances)
107             {
108                 if (o.Handle == context)
109                 {
110                     o.OnResize(w, h);
111                     break;
112                 }
113             }
114             return 0;
115         }
116
117         private int OnUpdate(IntPtr context, IntPtr content, int force, IntPtr userData)
118         {
119             foreach (WidgetBase o in WidgetInstances)
120             {
121                 if (o.Handle == context)
122                 {
123                     Bundle bundle = null;
124
125                     if (content != IntPtr.Zero)
126                         bundle = new Bundle(new SafeBundleHandle(content, false));
127                     o.OnUpdate(bundle, force != 0 ? true : false);
128                     break;
129                 }
130             }
131             return 0;
132         }
133     }
134 }
135