Merge "SVACE issue resolved" into devel/master
[platform/core/uifw/dali-toolkit.git] / plugins / dali-sharp / sharp / internal / FocusManager.cs
1 /*
2  * Copyright (c) 2017 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 namespace Dali {
19
20 using System;
21 using System.Runtime.InteropServices;
22
23 public class FocusManager : BaseHandle {
24   private global::System.Runtime.InteropServices.HandleRef swigCPtr;
25   private CustomAlgorithmInterfaceWrapper _customAlgorithmInterfaceWrapper;
26
27   internal FocusManager(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicManualPINVOKE.FocusManager_SWIGUpcast(cPtr), cMemoryOwn) {
28     swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
29   }
30
31   internal static global::System.Runtime.InteropServices.HandleRef getCPtr(FocusManager obj) {
32     return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
33   }
34
35   ~FocusManager() {
36     DisposeQueue.Instance.Add(this);
37   }
38
39   public override void Dispose() {
40     if (!Stage.IsInstalled()) {
41       DisposeQueue.Instance.Add(this);
42       return;
43     }
44
45     lock(this) {
46       if (swigCPtr.Handle != global::System.IntPtr.Zero) {
47         if (swigCMemOwn) {
48           swigCMemOwn = false;
49           NDalicManualPINVOKE.delete_FocusManager(swigCPtr);
50         }
51         swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
52       }
53       global::System.GC.SuppressFinalize(this);
54       base.Dispose();
55     }
56   }
57
58
59 /**
60   * @brief Event arguments that passed via FocusChanged signal
61   *
62   */
63 public class FocusChangedEventArgs : EventArgs
64 {
65    private Actor _actorCurrent;
66    private Actor _actorNext;
67
68    /**
69      * @brief Actor - is the original focused Actor
70      *
71      */
72    public Actor ActorCurrent
73    {
74       get
75       {
76          return _actorCurrent;
77       }
78       set
79       {
80          _actorCurrent = value;
81       }
82    }
83
84    /**
85      * @brief Actor - is the current focused Actor
86      *
87      */
88    public Actor ActorNext
89    {
90       get
91       {
92          return _actorNext;
93       }
94       set
95       {
96          _actorNext = value;
97       }
98    }
99 }
100
101 /**
102   * @brief Event arguments that passed via FocusGroupChanged signal
103   *
104   */
105 public class FocusGroupChangedEventArgs : EventArgs
106 {
107    private Actor _currentFocusedActor;
108    private bool _forwardDirection;
109
110    /**
111      * @brief Actor - is the current focused Actor
112      *
113      */
114    public Actor CurrentFocusedActor
115    {
116       get
117       {
118          return _currentFocusedActor;
119       }
120       set
121       {
122          _currentFocusedActor = value;
123       }
124    }
125
126    /**
127      * @brief ForwardDirection - is the direction (forward or backward) in which to move the focus next
128      *
129      */
130    public bool ForwardDirection
131    {
132       get
133       {
134          return _forwardDirection;
135       }
136       set
137       {
138          _forwardDirection = value;
139       }
140    }
141 }
142
143 /**
144   * @brief Event arguments that passed via FocusedActorEnterKey signal
145   *
146   */
147 public class FocusedActorEnterKeyEventArgs : EventArgs
148 {
149    private Actor _actor;
150
151    /**
152      * @brief Actor - is the current focused Actor which has the enter key pressed on it.
153      *
154      */
155    public Actor Actor
156    {
157       get
158       {
159          return _actor;
160       }
161       set
162       {
163          _actor = value;
164       }
165    }
166 }
167
168 /**
169   * @brief Event arguments that passed via PreFocusChange signal
170   *
171   */
172 public class PreFocusChangeEventArgs : EventArgs
173 {
174    private Actor _current;
175    private Actor _proposed;
176    private View.KeyboardFocus.Direction _direction;
177
178    /**
179     * @brief Actor - is the current focused Actor.
180     *
181     */
182    public Actor Current
183    {
184       get
185       {
186          return _current;
187       }
188       set
189       {
190           _current = value;
191       }
192    }
193
194     /**
195     * @brief Actor - is the proposed focused Actor.
196     *
197     */
198     public Actor Proposed
199     {
200         get
201         {
202             return _proposed;
203         }
204         set
205         {
206             _proposed = value;
207         }
208     }
209
210     /**
211     * @brief Direction - is the direction of Focus change.
212     *
213     */
214     public View.KeyboardFocus.Direction Direction
215     {
216         get
217         {
218             return _direction;
219         }
220         set
221         {
222             _direction = value;
223         }
224     }
225 }
226
227   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
228   public delegate void FocusChangedEventHandler(object source, FocusChangedEventArgs e);
229
230   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
231   public delegate void FocusGroupChangedEventHandler(object source, FocusGroupChangedEventArgs e);
232
233   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
234   public delegate void FocusedActorEnterKeyEventHandler(object source, FocusedActorEnterKeyEventArgs e);
235
236   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
237   public delegate Actor PreFocusChangeEventHandler(object source, PreFocusChangeEventArgs e);
238
239   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
240   public delegate IntPtr PreFocusChangeEventCallbackDelegate(IntPtr current, IntPtr proposed, View.KeyboardFocus.Direction direction);
241   private PreFocusChangeEventHandler _FocusManagerPreFocusChangeEventHandler;
242   private PreFocusChangeEventCallbackDelegate _FocusManagerPreFocusChangeEventCallbackDelegate;
243
244   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
245   private delegate void FocusChangedEventCallbackDelegate(IntPtr actorCurrent, IntPtr actorNext);
246   private FocusChangedEventHandler _FocusManagerFocusChangedEventHandler;
247   private FocusChangedEventCallbackDelegate _FocusManagerFocusChangedEventCallbackDelegate;
248
249   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
250   private delegate void FocusGroupChangedEventCallbackDelegate(IntPtr currentFocusedActor, bool forwardDirection);
251   private FocusGroupChangedEventHandler _FocusManagerFocusGroupChangedEventHandler;
252   private FocusGroupChangedEventCallbackDelegate _FocusManagerFocusGroupChangedEventCallbackDelegate;
253
254   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
255   private delegate void FocusedActorEnterKeyEventCallbackDelegate(IntPtr actor);
256   private FocusedActorEnterKeyEventHandler _FocusManagerFocusedActorEnterKeyEventHandler;
257   private FocusedActorEnterKeyEventCallbackDelegate _FocusManagerFocusedActorEnterKeyEventCallbackDelegate;
258
259   public event PreFocusChangeEventHandler PreFocusChange
260   {
261      add
262      {
263         lock(this)
264         {
265            // Restricted to only one listener
266            if (_FocusManagerPreFocusChangeEventHandler == null)
267            {
268                _FocusManagerPreFocusChangeEventHandler += value;
269
270                _FocusManagerPreFocusChangeEventCallbackDelegate = new PreFocusChangeEventCallbackDelegate(OnPreFocusChange);
271               this.PreFocusChangeSignal().Connect(_FocusManagerPreFocusChangeEventCallbackDelegate);
272            }
273         }
274      }
275
276      remove
277      {
278         lock(this)
279         {
280            if (_FocusManagerPreFocusChangeEventHandler != null)
281            {
282               this.PreFocusChangeSignal().Disconnect(_FocusManagerPreFocusChangeEventCallbackDelegate);
283            }
284
285            _FocusManagerPreFocusChangeEventHandler -= value;
286         }
287      }
288   }
289
290   // Callback for FocusManager PreFocusChangeSignal
291   private IntPtr OnPreFocusChange(IntPtr current, IntPtr proposed, View.KeyboardFocus.Direction direction)
292   {
293       Actor actor = null;
294       PreFocusChangeEventArgs e = new PreFocusChangeEventArgs();
295
296       // Populate all members of "e" (PreFocusChangeEventArgs) with real data
297       e.Current = Actor.GetActorFromPtr(current);
298       e.Proposed = Actor.GetActorFromPtr(proposed);
299       e.Direction = direction;
300
301       if (_FocusManagerPreFocusChangeEventHandler != null)
302       {
303         //here we send all data to user event handlers
304           actor = _FocusManagerPreFocusChangeEventHandler(this, e);
305       }
306
307       return actor.GetPtrfromActor();
308   }
309
310   /**
311     * @brief Event for FocusChanged signal which can be used to subscribe/unsubscribe the event handler
312     * (in the type of FocusChangedEventHandler) provided by the user.
313     * FocusChanged signal is emitted after the current focused actor has been changed.
314     */
315   public event FocusChangedEventHandler FocusChanged
316   {
317      add
318      {
319         lock(this)
320         {
321            // Restricted to only one listener
322            if (_FocusManagerFocusChangedEventHandler == null)
323            {
324               _FocusManagerFocusChangedEventHandler += value;
325
326               _FocusManagerFocusChangedEventCallbackDelegate = new FocusChangedEventCallbackDelegate(OnFocusChanged);
327               this.FocusChangedSignal().Connect(_FocusManagerFocusChangedEventCallbackDelegate);
328            }
329         }
330      }
331
332      remove
333      {
334         lock(this)
335         {
336            if (_FocusManagerFocusChangedEventHandler != null)
337            {
338               this.FocusChangedSignal().Disconnect(_FocusManagerFocusChangedEventCallbackDelegate);
339            }
340
341            _FocusManagerFocusChangedEventHandler -= value;
342         }
343      }
344   }
345
346   // Callback for FocusManager FocusChangedSignal
347   private void OnFocusChanged(IntPtr actorCurrent, IntPtr actorNext)
348   {
349      FocusChangedEventArgs e = new FocusChangedEventArgs();
350
351      // Populate all members of "e" (FocusChangedEventArgs) with real data
352      e.ActorCurrent = Actor.GetActorFromPtr(actorCurrent);
353      e.ActorNext = Actor.GetActorFromPtr(actorNext);
354
355      if (_FocusManagerFocusChangedEventHandler != null)
356      {
357         //here we send all data to user event handlers
358         _FocusManagerFocusChangedEventHandler(this, e);
359      }
360   }
361
362   /**
363     * @brief Event for FocusGroupChanged signal which can be used to subscribe/unsubscribe the event handler
364     * (in the type of FocusGroupChangedEventHandler) provided by the user.
365     * FocusGroupChanged signal is emitted when the focus group has been changed.
366     */
367   public event FocusGroupChangedEventHandler FocusGroupChanged
368   {
369      add
370      {
371         lock(this)
372         {
373            // Restricted to only one listener
374            if (_FocusManagerFocusGroupChangedEventHandler == null)
375            {
376               _FocusManagerFocusGroupChangedEventHandler += value;
377
378               _FocusManagerFocusGroupChangedEventCallbackDelegate = new FocusGroupChangedEventCallbackDelegate(OnFocusGroupChanged);
379               this.FocusGroupChangedSignal().Connect(_FocusManagerFocusGroupChangedEventCallbackDelegate);
380            }
381         }
382      }
383
384      remove
385      {
386         lock(this)
387         {
388            if (_FocusManagerFocusGroupChangedEventHandler != null)
389            {
390               this.FocusGroupChangedSignal().Disconnect(_FocusManagerFocusGroupChangedEventCallbackDelegate);
391            }
392
393            _FocusManagerFocusGroupChangedEventHandler -= value;
394         }
395      }
396   }
397
398   // Callback for FocusManager FocusGroupChangedSignal
399   private void OnFocusGroupChanged(IntPtr currentFocusedActor, bool forwardDirection)
400   {
401      FocusGroupChangedEventArgs e = new FocusGroupChangedEventArgs();
402
403      // Populate all members of "e" (FocusGroupChangedEventArgs) with real data
404      e.CurrentFocusedActor = Actor.GetActorFromPtr(currentFocusedActor);
405      e.ForwardDirection = forwardDirection;
406
407      if (_FocusManagerFocusGroupChangedEventHandler != null)
408      {
409         //here we send all data to user event handlers
410         _FocusManagerFocusGroupChangedEventHandler(this, e);
411      }
412   }
413
414   /**
415     * @brief Event for FocusedActorEnterKeyPressed signal which can be used to subscribe/unsubscribe the event handler
416     * (in the type of FocusedActorEnterKeyEventHandler) provided by the user.
417     * FocusedActorEnterKeyPressed signal is emitted when the current focused actor has the enter key pressed on it.
418     */
419   public event FocusedActorEnterKeyEventHandler FocusedActorEnterKeyPressed
420   {
421      add
422      {
423         lock(this)
424         {
425            // Restricted to only one listener
426            if (_FocusManagerFocusedActorEnterKeyEventHandler == null)
427            {
428               _FocusManagerFocusedActorEnterKeyEventHandler += value;
429
430               _FocusManagerFocusedActorEnterKeyEventCallbackDelegate = new FocusedActorEnterKeyEventCallbackDelegate(OnFocusedActorEnterKey);
431               this.FocusedActorEnterKeySignal().Connect(_FocusManagerFocusedActorEnterKeyEventCallbackDelegate);
432            }
433         }
434      }
435
436      remove
437      {
438         lock(this)
439         {
440            if (_FocusManagerFocusedActorEnterKeyEventHandler != null)
441            {
442               this.FocusedActorEnterKeySignal().Disconnect(_FocusManagerFocusedActorEnterKeyEventCallbackDelegate);
443            }
444
445            _FocusManagerFocusedActorEnterKeyEventHandler -= value;
446         }
447      }
448   }
449
450   // Callback for FocusManager FocusedActorEnterKeySignal
451   private void OnFocusedActorEnterKey(IntPtr actor)
452   {
453      FocusedActorEnterKeyEventArgs e = new FocusedActorEnterKeyEventArgs();
454
455      // Populate all members of "e" (FocusedActorEnterKeyEventArgs) with real data
456      e.Actor = Actor.GetActorFromPtr(actor);
457
458      if (_FocusManagerFocusedActorEnterKeyEventHandler != null)
459      {
460         //here we send all data to user event handlers
461         _FocusManagerFocusedActorEnterKeyEventHandler(this, e);
462      }
463   }
464
465   public FocusManager() : this(NDalicManualPINVOKE.new_FocusManager(), true) {
466     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
467   }
468
469   public static FocusManager Get() {
470     FocusManager ret = new FocusManager(NDalicManualPINVOKE.FocusManager_Get(), true);
471     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
472     return ret;
473   }
474
475   public bool SetCurrentFocusActor(Actor actor) {
476     bool ret = NDalicManualPINVOKE.FocusManager_SetCurrentFocusActor(swigCPtr, Actor.getCPtr(actor));
477     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
478     return ret;
479   }
480
481   public Actor GetCurrentFocusActor() {
482     Actor ret = new Actor(NDalicManualPINVOKE.FocusManager_GetCurrentFocusActor(swigCPtr), true);
483     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
484     return ret;
485   }
486
487   public bool MoveFocus(View.KeyboardFocus.Direction direction) {
488     bool ret = NDalicManualPINVOKE.FocusManager_MoveFocus(swigCPtr, (int)direction);
489     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
490     return ret;
491   }
492
493   public void ClearFocus() {
494     NDalicManualPINVOKE.FocusManager_ClearFocus(swigCPtr);
495     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
496   }
497
498   public void SetFocusGroupLoop(bool enabled) {
499     NDalicManualPINVOKE.FocusManager_SetFocusGroupLoop(swigCPtr, enabled);
500     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
501   }
502
503   public bool GetFocusGroupLoop() {
504     bool ret = NDalicManualPINVOKE.FocusManager_GetFocusGroupLoop(swigCPtr);
505     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
506     return ret;
507   }
508
509   public void SetAsFocusGroup(Actor actor, bool isFocusGroup) {
510     NDalicManualPINVOKE.FocusManager_SetAsFocusGroup(swigCPtr, Actor.getCPtr(actor), isFocusGroup);
511     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
512   }
513
514   public bool IsFocusGroup(Actor actor) {
515     bool ret = NDalicManualPINVOKE.FocusManager_IsFocusGroup(swigCPtr, Actor.getCPtr(actor));
516     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
517     return ret;
518   }
519
520   public Actor GetFocusGroup(Actor actor) {
521     Actor ret = new Actor(NDalicManualPINVOKE.FocusManager_GetFocusGroup(swigCPtr, Actor.getCPtr(actor)), true);
522     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
523     return ret;
524   }
525
526   public void SetFocusIndicatorActor(Actor indicator) {
527     NDalicManualPINVOKE.FocusManager_SetFocusIndicatorActor(swigCPtr, Actor.getCPtr(indicator));
528     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
529   }
530
531   public Actor GetFocusIndicatorActor() {
532     Actor ret = new Actor(NDalicManualPINVOKE.FocusManager_GetFocusIndicatorActor(swigCPtr), true);
533     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
534     return ret;
535   }
536
537   public void SetCustomAlgorithm(ICustomFocusAlgorithm arg0) {
538     _customAlgorithmInterfaceWrapper = new CustomAlgorithmInterfaceWrapper();
539     _customAlgorithmInterfaceWrapper.SetFocusAlgorithm(arg0);
540
541     NDalicPINVOKE.SetCustomAlgorithm(swigCPtr, CustomAlgorithmInterface.getCPtr(_customAlgorithmInterfaceWrapper));
542     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
543   }
544
545   public PreFocusChangeSignal PreFocusChangeSignal() {
546     PreFocusChangeSignal ret = new PreFocusChangeSignal(NDalicManualPINVOKE.FocusManager_PreFocusChangeSignal(swigCPtr), false);
547     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
548     return ret;
549   }
550
551   public FocusChangedSignal FocusChangedSignal() {
552     FocusChangedSignal ret = new FocusChangedSignal(NDalicManualPINVOKE.FocusManager_FocusChangedSignal(swigCPtr), false);
553     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
554     return ret;
555   }
556
557   public FocusGroupChangedSignal FocusGroupChangedSignal() {
558     FocusGroupChangedSignal ret = new FocusGroupChangedSignal(NDalicManualPINVOKE.FocusManager_FocusGroupChangedSignal(swigCPtr), false);
559     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
560     return ret;
561   }
562
563   public ActorSignal FocusedActorEnterKeySignal() {
564     ActorSignal ret = new ActorSignal(NDalicManualPINVOKE.FocusManager_FocusedActorEnterKeySignal(swigCPtr), false);
565     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
566     return ret;
567   }
568
569   private static readonly FocusManager instance = FocusManager.Get();
570
571   public static FocusManager Instance
572   {
573       get
574       {
575           return instance;
576       }
577   }
578
579   public interface ICustomFocusAlgorithm
580   {
581       View GetNextFocusableActor(View current, View proposed, View.KeyboardFocus.Direction direction);
582   }
583
584   private class CustomAlgorithmInterfaceWrapper : CustomAlgorithmInterface
585   {
586       private FocusManager.ICustomFocusAlgorithm _customFocusAlgorithm;
587
588       public CustomAlgorithmInterfaceWrapper()
589       {
590       }
591
592       public void SetFocusAlgorithm(FocusManager.ICustomFocusAlgorithm customFocusAlgorithm)
593       {
594           _customFocusAlgorithm = customFocusAlgorithm;
595       }
596
597       public override Actor GetNextFocusableActor(Actor current, Actor proposed, View.KeyboardFocus.Direction direction)
598       {
599           View currentView = View.DownCast<View>(current);
600           View proposedView = View.DownCast<View>(proposed);
601           return _customFocusAlgorithm.GetNextFocusableActor(currentView, proposedView, direction);
602       }
603   }
604 }
605
606 }