DALi C# binding - EventHandler Support
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / SWIG / events / objectregistry-event.i
1 /*
2  * Copyright (c) 2016 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
18 %define OBJECT_REGISTRY_EVENTHANDLER_TYPEMAP_EVENTARG(NameSpace, ClassName)
19 %typemap(csimports) NameSpace::ClassName %{
20 using System;
21 using System.Runtime.InteropServices;
22
23 %}
24
25 %enddef
26
27
28 %define OBJECT_REGISTRY_EVENTHANDLER_TYPEMAP_HELPER(NameSpace, ClassName)
29 %typemap(cscode) NameSpace::ClassName %{
30
31
32 public class ObjectCreatedEventArgs : EventArgs
33 {
34    private BaseHandle _baseHandle;
35
36    public BaseHandle BaseHandle
37    {
38       get
39       {
40          return _baseHandle;
41       }
42       set
43       {
44          _baseHandle = value;
45       }
46    }
47 }
48
49 public class ObjectDestroyedEventArgs : EventArgs
50 {
51    private RefObject _refObject;
52
53    public RefObject RefObject
54    {
55       get
56       {
57          return _refObject;
58       }
59       set
60       {
61          _refObject = value;
62       }
63    }
64 }
65
66   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
67   public delegate void ObjectCreatedEventHandler(object source, ObjectCreatedEventArgs e);
68
69   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
70   public delegate void ObjectDestroyedEventHandler(object source, ObjectDestroyedEventArgs e);
71
72
73   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
74   private delegate void ObjectCreatedEventCallbackDelegate(IntPtr baseHandle);
75   private ObjectCreatedEventHandler _objectRegistryObjectCreatedEventHandler;
76   private ObjectCreatedEventCallbackDelegate _objectRegistryObjectCreatedEventCallbackDelegate;
77
78   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
79   private delegate void ObjectDestroyedEventCallbackDelegate(IntPtr fefObject);
80   private ObjectDestroyedEventHandler _objectRegistryObjectDestroyedEventHandler;
81   private ObjectDestroyedEventCallbackDelegate _objectRegistryObjectDestroyedEventCallbackDelegate;
82
83   public event ObjectCreatedEventHandler ObjectCreated
84   {
85      add
86      {
87         lock(this)
88         {
89            // Restricted to only one listener
90            if (_objectRegistryObjectCreatedEventHandler == null)
91            {
92               _objectRegistryObjectCreatedEventHandler += value;
93
94               _objectRegistryObjectCreatedEventCallbackDelegate = new ObjectCreatedEventCallbackDelegate(OnObjectCreated);
95               this.ObjectCreatedSignal().Connect(_objectRegistryObjectCreatedEventCallbackDelegate);
96            }
97         }
98      }
99
100      remove
101      {
102         lock(this)
103         {
104            if (_objectRegistryObjectCreatedEventHandler != null)
105            {
106               this.ObjectCreatedSignal().Disconnect(_objectRegistryObjectCreatedEventCallbackDelegate);
107            }
108
109            _objectRegistryObjectCreatedEventHandler -= value;
110         }
111      }
112   }
113
114   // Callback for ObjectRegistry ObjectCreatedSignal
115   private void OnObjectCreated(IntPtr baseHandle)
116   {
117      ObjectCreatedEventArgs e = new ObjectCreatedEventArgs();
118
119      // Populate all members of "e" (ObjectCreatedEventArgs) with real data
120      //e.BaseHandle = BaseHandle.GetBaseHandleFromPtr(baseHandle); //GetBaseHandleFromPtr() is not present in BaseHandle.cs. Not sure what is the reason?
121
122      if (_objectRegistryObjectCreatedEventHandler != null)
123      {
124         //here we send all data to user event handlers
125         _objectRegistryObjectCreatedEventHandler(this, e);
126      }
127   }
128
129   public event ObjectDestroyedEventHandler ObjectDestroyed
130   {
131      add
132      {
133         lock(this)
134         {
135            // Restricted to only one listener
136            if (_objectRegistryObjectDestroyedEventHandler == null)
137            {
138               _objectRegistryObjectDestroyedEventHandler += value;
139
140               _objectRegistryObjectDestroyedEventCallbackDelegate = new ObjectDestroyedEventCallbackDelegate(OnObjectDestroyed);
141               this.ObjectDestroyedSignal().Connect(_objectRegistryObjectDestroyedEventCallbackDelegate);
142            }
143         }
144      }
145
146      remove
147      {
148         lock(this)
149         {
150            if (_objectRegistryObjectDestroyedEventHandler != null)
151            {
152               this.ObjectDestroyedSignal().Disconnect(_objectRegistryObjectDestroyedEventCallbackDelegate);
153            }
154
155            _objectRegistryObjectDestroyedEventHandler -= value;
156         }
157      }
158   }
159
160   // Callback for ObjectRegistry ObjectDestroyedSignal
161   private void OnObjectDestroyed(IntPtr refObject)
162   {
163      ObjectDestroyedEventArgs e = new ObjectDestroyedEventArgs();
164
165      // Populate all members of "e" (ObjectDestroyedEventArgs) with real data
166      e.RefObject = RefObject.GetRefObjectFromPtr(refObject);
167
168      if (_objectRegistryObjectDestroyedEventHandler != null)
169      {
170         //here we send all data to user event handlers
171         _objectRegistryObjectDestroyedEventHandler(this, e);
172      }
173   }
174
175 %}
176 %enddef
177
178 %typemap(cscode) Dali::BaseHandle %{
179  public static BaseHandle GetBaseHandleFromPtr(global::System.IntPtr cPtr) {
180     BaseHandle ret = new BaseHandle(cPtr, false);
181    if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
182     return ret;
183   }
184 %}
185
186
187 %define DALI_OBJECT_REGISTRY_EVENTHANDLER_PARAM( NameSpace, ClassName)
188   OBJECT_REGISTRY_EVENTHANDLER_TYPEMAP_EVENTARG( NameSpace, ClassName);
189   OBJECT_REGISTRY_EVENTHANDLER_TYPEMAP_HELPER( NameSpace, ClassName);
190 %enddef
191
192 namespace Dali
193 {
194   DALI_OBJECT_REGISTRY_EVENTHANDLER_PARAM( Dali, ObjectRegistry);
195 }
196