Merge "DALi C# binding - Generic Delegates support for EventHandlers" into devel...
[platform/core/uifw/dali-toolkit.git] / plugins / dali-swig / SWIG / events / control-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 CONTROL_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 %define CONTROL_EVENTHANDLER_TYPEMAP_HELPER(NameSpace, ClassName)
28 %typemap(cscode) NameSpace::ClassName %{
29
30
31 /**
32   * @brief Event arguments that passed via KeyInputFocusGained signal
33   *
34   */
35 public class KeyInputFocusGainedEventArgs : EventArgs
36 {
37    private View _view;
38
39    /**
40      * @brief View - is the view that gets Key Input Focus
41      *
42      */
43    public View View
44    {
45       get
46       {
47          return _view;
48       }
49       set
50       {
51          _view = value;
52       }
53    }
54 }
55
56 /**
57   * @brief Event arguments that passed via KeyInputFocusLost signal
58   *
59   */
60 public class KeyInputFocusLostEventArgs : EventArgs
61 {
62    private View _view;
63
64    /**
65      * @brief View - is the view that loses Key Input Focus
66      *
67      */
68    public View View
69    {
70       get
71       {
72          return _view;
73       }
74       set
75       {
76          _view = value;
77       }
78    }
79 }
80
81 /**
82   * @brief Event arguments that passed via KeyEvent signal
83   *
84   */
85 public class KeyEventArgs : EventArgs
86 {
87    private View _view;
88    private KeyEvent _keyEvent;
89
90    /**
91      * @brief View - is the view that recieves the keyevent.
92      *
93      */
94    public View View
95    {
96       get
97       {
98          return _view;
99       }
100       set
101       {
102          _view = value;
103       }
104    }
105
106    /**
107      * @brief KeyEvent - is the keyevent sent to the View.
108      *
109      */
110    public KeyEvent KeyEvent
111    {
112       get
113       {
114          return _keyEvent;
115       }
116       set
117       {
118          _keyEvent = value;
119       }
120    }
121 }
122
123   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
124   private delegate void KeyInputFocusGainedCallbackDelegate(IntPtr control);
125   private DaliEventHandler<object,KeyInputFocusGainedEventArgs> _KeyInputFocusGainedEventHandler;
126   private KeyInputFocusGainedCallbackDelegate _KeyInputFocusGainedCallbackDelegate;
127
128   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
129   private delegate void KeyInputFocusLostCallbackDelegate(IntPtr control);
130   private DaliEventHandler<object,KeyInputFocusLostEventArgs> _KeyInputFocusLostEventHandler;
131   private KeyInputFocusLostCallbackDelegate _KeyInputFocusLostCallbackDelegate;
132
133   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
134   private delegate bool KeyCallbackDelegate(IntPtr control, IntPtr keyEvent);
135   private DaliEventHandlerWithReturnType<object,KeyEventArgs,bool> _KeyEventHandler;
136   private KeyCallbackDelegate _KeyCallbackDelegate;
137
138   /**
139     * @brief Event for KeyInputFocusGained signal which can be used to subscribe/unsubscribe the event handler
140     * (in the type of KeyInputFocusGainedEventHandler-DaliEventHandler<object,KeyInputFocusGainedEventArgs>) 
141     * provided by the user. KeyInputFocusGained signal is emitted when the control gets Key Input Focus.
142     */
143   public event DaliEventHandler<object,KeyInputFocusGainedEventArgs> KeyInputFocusGained
144   {
145      add
146      {
147         lock(this)
148         {
149            // Restricted to only one listener
150            if (_KeyInputFocusGainedEventHandler == null)
151            {
152               _KeyInputFocusGainedEventHandler += value;
153
154               _KeyInputFocusGainedCallbackDelegate = new KeyInputFocusGainedCallbackDelegate(OnKeyInputFocusGained);
155               this.KeyInputFocusGainedSignal().Connect(_KeyInputFocusGainedCallbackDelegate);
156            }
157         }
158      }
159
160      remove
161      {
162         lock(this)
163         {
164            if (_KeyInputFocusGainedEventHandler != null)
165            {
166               this.KeyInputFocusGainedSignal().Disconnect(_KeyInputFocusGainedCallbackDelegate);
167            }
168
169            _KeyInputFocusGainedEventHandler -= value;
170         }
171      }
172   }
173
174  private void OnKeyInputFocusGained(IntPtr view)
175   {
176    KeyInputFocusGainedEventArgs e = new KeyInputFocusGainedEventArgs();
177
178    // Populate all members of "e" (KeyInputFocusGainedEventArgs) with real data
179    e.View = Dali.View.GetViewFromPtr(view);
180
181    if (_KeyInputFocusGainedEventHandler != null)
182    {
183       //here we send all data to user event handlers
184       _KeyInputFocusGainedEventHandler(this, e);
185    }
186
187   }
188
189   /**
190     * @brief Event for KeyInputFocusLost signal which can be used to subscribe/unsubscribe the event handler
191     * (in the type of KeyInputFocusLostEventHandler-DaliEventHandler<object,KeyInputFocusLostEventArgs>) 
192     * provided by the user. KeyInputFocusLost signal is emitted when the control loses Key Input Focus.
193     */
194   public event DaliEventHandler<object,KeyInputFocusLostEventArgs> KeyInputFocusLost
195   {
196      add
197      {
198         lock(this)
199         {
200            // Restricted to only one listener
201            if (_KeyInputFocusLostEventHandler == null)
202            {
203               _KeyInputFocusLostEventHandler += value;
204
205               _KeyInputFocusLostCallbackDelegate = new KeyInputFocusLostCallbackDelegate(OnKeyInputFocusLost);
206               this.KeyInputFocusLostSignal().Connect(_KeyInputFocusLostCallbackDelegate);
207            }
208         }
209      }
210
211      remove
212      {
213         lock(this)
214         {
215            if (_KeyInputFocusLostEventHandler != null)
216            {
217               this.KeyInputFocusLostSignal().Disconnect(_KeyInputFocusLostCallbackDelegate);
218            }
219
220            _KeyInputFocusLostEventHandler -= value;
221         }
222      }
223   }
224
225  private void OnKeyInputFocusLost(IntPtr view)
226   {
227    KeyInputFocusLostEventArgs e = new KeyInputFocusLostEventArgs();
228
229    // Populate all members of "e" (KeyInputFocusLostEventArgs) with real data
230    e.View = Dali.View.GetViewFromPtr(view);
231
232    if (_KeyInputFocusLostEventHandler != null)
233    {
234       //here we send all data to user event handlers
235       _KeyInputFocusLostEventHandler(this, e);
236    }
237   }
238
239   /**
240     * @brief Event for KeyPressed signal which can be used to subscribe/unsubscribe the event handler
241     * (in the type of KeyEventEventHandler-DaliEventHandlerWithReturnType<object,KeyEventArgs,bool>) 
242     * provided by the user. KeyPressed signal is emitted when key event is received.
243     */
244   public event DaliEventHandlerWithReturnType<object,KeyEventArgs,bool> KeyPressed
245   {
246      add
247      {
248         lock(this)
249         {
250            // Restricted to only one listener
251            if (_KeyEventHandler == null)
252            {
253               _KeyEventHandler += value;
254
255               _KeyCallbackDelegate = new KeyCallbackDelegate(OnKeyEvent);
256               this.KeyEventSignal().Connect(_KeyCallbackDelegate);
257            }
258         }
259      }
260
261      remove
262      {
263         lock(this)
264         {
265            if (_KeyEventHandler != null)
266            {
267               this.KeyEventSignal().Disconnect(_KeyCallbackDelegate);
268            }
269
270            _KeyEventHandler -= value;
271         }
272      }
273   }
274
275  private bool OnKeyEvent(IntPtr view, IntPtr keyEvent)
276   {
277    KeyEventArgs e = new KeyEventArgs();
278
279    // Populate all members of "e" (KeyEventArgs) with real data
280    e.View = Dali.View.GetViewFromPtr(view);
281    e.KeyEvent = Dali.KeyEvent.GetKeyEventFromPtr(keyEvent);
282
283    if (_KeyEventHandler != null)
284    {
285       //here we send all data to user event handlers
286       return _KeyEventHandler(this, e, true);
287    }
288    return false;
289
290   }
291
292  public static View GetViewFromPtr(global::System.IntPtr cPtr) {
293     View ret = new View(cPtr, false);
294    if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
295     return ret;
296   }
297
298 %}
299
300 %enddef
301
302 %define DALI_CONTROL_EVENTHANDLER_PARAM( NameSpace, ClassName)
303
304   CONTROL_EVENTHANDLER_TYPEMAP_EVENTARG( NameSpace, ClassName);
305   CONTROL_EVENTHANDLER_TYPEMAP_HELPER( NameSpace, ClassName);
306
307 %enddef
308
309 namespace Dali
310 {
311   DALI_CONTROL_EVENTHANDLER_PARAM( Dali::Toolkit, Control);
312 }