Merge "Move Event Handlers to View class" into devel/master
[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
67   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
68   private delegate void ObjectCreatedEventCallbackDelegate(IntPtr baseHandle);
69   private DaliEventHandler<object,ObjectCreatedEventArgs> _objectRegistryObjectCreatedEventHandler;
70   private ObjectCreatedEventCallbackDelegate _objectRegistryObjectCreatedEventCallbackDelegate;
71
72   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
73   private delegate void ObjectDestroyedEventCallbackDelegate(IntPtr fefObject);
74   private DaliEventHandler<object,ObjectDestroyedEventArgs> _objectRegistryObjectDestroyedEventHandler;
75   private ObjectDestroyedEventCallbackDelegate _objectRegistryObjectDestroyedEventCallbackDelegate;
76
77   public event DaliEventHandler<object,ObjectCreatedEventArgs> ObjectCreated
78   {
79      add
80      {
81         lock(this)
82         {
83            // Restricted to only one listener
84            if (_objectRegistryObjectCreatedEventHandler == null)
85            {
86               _objectRegistryObjectCreatedEventHandler += value;
87
88               _objectRegistryObjectCreatedEventCallbackDelegate = new ObjectCreatedEventCallbackDelegate(OnObjectCreated);
89               this.ObjectCreatedSignal().Connect(_objectRegistryObjectCreatedEventCallbackDelegate);
90            }
91         }
92      }
93
94      remove
95      {
96         lock(this)
97         {
98            if (_objectRegistryObjectCreatedEventHandler != null)
99            {
100               this.ObjectCreatedSignal().Disconnect(_objectRegistryObjectCreatedEventCallbackDelegate);
101            }
102
103            _objectRegistryObjectCreatedEventHandler -= value;
104         }
105      }
106   }
107
108   // Callback for ObjectRegistry ObjectCreatedSignal
109   private void OnObjectCreated(IntPtr baseHandle)
110   {
111      ObjectCreatedEventArgs e = new ObjectCreatedEventArgs();
112
113      // Populate all members of "e" (ObjectCreatedEventArgs) with real data
114      //e.BaseHandle = BaseHandle.GetBaseHandleFromPtr(baseHandle); //GetBaseHandleFromPtr() is not present in BaseHandle.cs. Not sure what is the reason?
115
116      if (_objectRegistryObjectCreatedEventHandler != null)
117      {
118         //here we send all data to user event handlers
119         _objectRegistryObjectCreatedEventHandler(this, e);
120      }
121   }
122
123   public event DaliEventHandler<object,ObjectDestroyedEventArgs> ObjectDestroyed
124   {
125      add
126      {
127         lock(this)
128         {
129            // Restricted to only one listener
130            if (_objectRegistryObjectDestroyedEventHandler == null)
131            {
132               _objectRegistryObjectDestroyedEventHandler += value;
133
134               _objectRegistryObjectDestroyedEventCallbackDelegate = new ObjectDestroyedEventCallbackDelegate(OnObjectDestroyed);
135               this.ObjectDestroyedSignal().Connect(_objectRegistryObjectDestroyedEventCallbackDelegate);
136            }
137         }
138      }
139
140      remove
141      {
142         lock(this)
143         {
144            if (_objectRegistryObjectDestroyedEventHandler != null)
145            {
146               this.ObjectDestroyedSignal().Disconnect(_objectRegistryObjectDestroyedEventCallbackDelegate);
147            }
148
149            _objectRegistryObjectDestroyedEventHandler -= value;
150         }
151      }
152   }
153
154   // Callback for ObjectRegistry ObjectDestroyedSignal
155   private void OnObjectDestroyed(IntPtr refObject)
156   {
157      ObjectDestroyedEventArgs e = new ObjectDestroyedEventArgs();
158
159      // Populate all members of "e" (ObjectDestroyedEventArgs) with real data
160      e.RefObject = RefObject.GetRefObjectFromPtr(refObject);
161
162      if (_objectRegistryObjectDestroyedEventHandler != null)
163      {
164         //here we send all data to user event handlers
165         _objectRegistryObjectDestroyedEventHandler(this, e);
166      }
167   }
168
169 %}
170 %enddef
171
172 %typemap(cscode) Dali::BaseHandle %{
173  public static BaseHandle GetBaseHandleFromPtr(global::System.IntPtr cPtr) {
174     BaseHandle ret = new BaseHandle(cPtr, false);
175    if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
176     return ret;
177   }
178 %}
179
180
181 %define DALI_OBJECT_REGISTRY_EVENTHANDLER_PARAM( NameSpace, ClassName)
182   OBJECT_REGISTRY_EVENTHANDLER_TYPEMAP_EVENTARG( NameSpace, ClassName);
183   OBJECT_REGISTRY_EVENTHANDLER_TYPEMAP_HELPER( NameSpace, ClassName);
184 %enddef
185
186 namespace Dali
187 {
188   DALI_OBJECT_REGISTRY_EVENTHANDLER_PARAM( Dali, ObjectRegistry);
189 }
190