Merge "Add PRIVATECOMMAND of ImfEvent in text-controller" 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 (!Window.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 View _viewCurrent;
66    private View _viewNext;
67
68    /**
69      * @brief ViewCurrent - is the original focused View
70      *
71      */
72    public View ViewCurrent
73    {
74       get
75       {
76          return _viewCurrent;
77       }
78       set
79       {
80         _viewCurrent = value;
81       }
82    }
83
84    /**
85      * @brief ViewNext - is the current focused View
86      *
87      */
88    public View ViewNext
89    {
90       get
91       {
92          return _viewNext;
93       }
94       set
95       {
96         _viewNext = 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 View _currentFocusedView;
108    private bool _forwardDirection;
109
110    /**
111      * @brief CurrentFocusedView - is the current focused View
112      *
113      */
114    public View CurrentFocusedView
115    {
116       get
117       {
118          return _currentFocusedView;
119       }
120       set
121       {
122          _currentFocusedView = 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 FocusedViewEnterKey signal
145   *
146   */
147 public class FocusedViewEnterKeyEventArgs : EventArgs
148 {
149    private View _view;
150
151    /**
152      * @brief View - is the current focused View which has the enter key pressed on it.
153      *
154      */
155    public View View
156    {
157       get
158       {
159          return _view;
160       }
161       set
162       {
163         _view = 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 View _current;
175    private View _proposed;
176    private View.KeyboardFocus.Direction _direction;
177
178    /**
179     * @brief Current - is the current focused View.
180     *
181     */
182    public View Current
183    {
184       get
185       {
186          return _current;
187       }
188       set
189       {
190           _current = value;
191       }
192    }
193
194     /**
195     * @brief Proposed - is the proposed focused View.
196     *
197     */
198     public View 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 FocusedViewEnterKeyEventHandler(object source, FocusedViewEnterKeyEventArgs e);
235
236   [UnmanagedFunctionPointer(CallingConvention.StdCall)]
237   public delegate View 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 FocusedViewEnterKeyEventCallbackDelegate(IntPtr actor);
256   private FocusedViewEnterKeyEventHandler _FocusManagerFocusedViewEnterKeyEventHandler;
257   private FocusedViewEnterKeyEventCallbackDelegate _FocusManagerFocusedViewEnterKeyEventCallbackDelegate;
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       View view = null;
294       PreFocusChangeEventArgs e = new PreFocusChangeEventArgs();
295
296       // Populate all members of "e" (PreFocusChangeEventArgs) with real data
297       if (current != global::System.IntPtr.Zero)
298       {
299         e.Current = View.GetViewFromPtr(current);
300       }
301
302       if (proposed != global::System.IntPtr.Zero)
303       {
304         e.Proposed = View.GetViewFromPtr(proposed);
305       }
306
307       e.Direction = direction;
308
309       if (_FocusManagerPreFocusChangeEventHandler != null)
310       {
311         //here we send all data to user event handlers
312         view = _FocusManagerPreFocusChangeEventHandler(this, e);
313       }
314
315       if (view)
316       {
317         return view.GetPtrfromView();
318       }
319       else
320       {
321         return current;
322       }
323   }
324
325   /**
326     * @brief Event for FocusChanged signal which can be used to subscribe/unsubscribe the event handler
327     * (in the type of FocusChangedEventHandler) provided by the user.
328     * FocusChanged signal is emitted after the current focused view has been changed.
329     */
330   public event FocusChangedEventHandler FocusChanged
331   {
332      add
333      {
334         lock(this)
335         {
336            // Restricted to only one listener
337            if (_FocusManagerFocusChangedEventHandler == null)
338            {
339               _FocusManagerFocusChangedEventHandler += value;
340
341               _FocusManagerFocusChangedEventCallbackDelegate = new FocusChangedEventCallbackDelegate(OnFocusChanged);
342               this.FocusChangedSignal().Connect(_FocusManagerFocusChangedEventCallbackDelegate);
343            }
344         }
345      }
346
347      remove
348      {
349         lock(this)
350         {
351            if (_FocusManagerFocusChangedEventHandler != null)
352            {
353               this.FocusChangedSignal().Disconnect(_FocusManagerFocusChangedEventCallbackDelegate);
354            }
355
356            _FocusManagerFocusChangedEventHandler -= value;
357         }
358      }
359   }
360
361   // Callback for FocusManager FocusChangedSignal
362   private void OnFocusChanged(IntPtr viewCurrent, IntPtr viewNext)
363   {
364      FocusChangedEventArgs e = new FocusChangedEventArgs();
365
366      // Populate all members of "e" (FocusChangedEventArgs) with real data
367      e.ViewCurrent = View.GetViewFromPtr(viewCurrent);
368      e.ViewNext = View.GetViewFromPtr(viewNext);
369
370      if (_FocusManagerFocusChangedEventHandler != null)
371      {
372         //here we send all data to user event handlers
373         _FocusManagerFocusChangedEventHandler(this, e);
374      }
375   }
376
377   /**
378     * @brief Event for FocusGroupChanged signal which can be used to subscribe/unsubscribe the event handler
379     * (in the type of FocusGroupChangedEventHandler) provided by the user.
380     * FocusGroupChanged signal is emitted when the focus group has been changed.
381     */
382   public event FocusGroupChangedEventHandler FocusGroupChanged
383   {
384      add
385      {
386         lock(this)
387         {
388            // Restricted to only one listener
389            if (_FocusManagerFocusGroupChangedEventHandler == null)
390            {
391               _FocusManagerFocusGroupChangedEventHandler += value;
392
393               _FocusManagerFocusGroupChangedEventCallbackDelegate = new FocusGroupChangedEventCallbackDelegate(OnFocusGroupChanged);
394               this.FocusGroupChangedSignal().Connect(_FocusManagerFocusGroupChangedEventCallbackDelegate);
395            }
396         }
397      }
398
399      remove
400      {
401         lock(this)
402         {
403            if (_FocusManagerFocusGroupChangedEventHandler != null)
404            {
405               this.FocusGroupChangedSignal().Disconnect(_FocusManagerFocusGroupChangedEventCallbackDelegate);
406            }
407
408            _FocusManagerFocusGroupChangedEventHandler -= value;
409         }
410      }
411   }
412
413   // Callback for FocusManager FocusGroupChangedSignal
414   private void OnFocusGroupChanged(IntPtr currentFocusedView, bool forwardDirection)
415   {
416      FocusGroupChangedEventArgs e = new FocusGroupChangedEventArgs();
417
418      // Populate all members of "e" (FocusGroupChangedEventArgs) with real data
419      e.CurrentFocusedView = View.GetViewFromPtr(currentFocusedView);
420      e.ForwardDirection = forwardDirection;
421
422      if (_FocusManagerFocusGroupChangedEventHandler != null)
423      {
424         //here we send all data to user event handlers
425         _FocusManagerFocusGroupChangedEventHandler(this, e);
426      }
427   }
428
429   /**
430     * @brief Event for FocusedViewEnterKeyPressed signal which can be used to subscribe/unsubscribe the event handler
431     * (in the type of FocusedViewEnterKeyEventHandler) provided by the user.
432     * FocusedViewEnterKeyPressed signal is emitted when the current focused view has the enter key pressed on it.
433     */
434   public event FocusedViewEnterKeyEventHandler FocusedViewEnterKeyPressed
435   {
436      add
437      {
438         lock(this)
439         {
440            // Restricted to only one listener
441            if (_FocusManagerFocusedViewEnterKeyEventHandler == null)
442            {
443               _FocusManagerFocusedViewEnterKeyEventHandler += value;
444
445               _FocusManagerFocusedViewEnterKeyEventCallbackDelegate = new FocusedViewEnterKeyEventCallbackDelegate(OnFocusedViewEnterKey);
446               this.FocusedViewEnterKeySignal().Connect(_FocusManagerFocusedViewEnterKeyEventCallbackDelegate);
447            }
448         }
449      }
450
451      remove
452      {
453         lock(this)
454         {
455            if (_FocusManagerFocusedViewEnterKeyEventHandler != null)
456            {
457               this.FocusedViewEnterKeySignal().Disconnect(_FocusManagerFocusedViewEnterKeyEventCallbackDelegate);
458            }
459
460            _FocusManagerFocusedViewEnterKeyEventHandler -= value;
461         }
462      }
463   }
464
465   // Callback for FocusManager FocusedViewEnterKeySignal
466   private void OnFocusedViewEnterKey(IntPtr view)
467   {
468      FocusedViewEnterKeyEventArgs e = new FocusedViewEnterKeyEventArgs();
469
470      // Populate all members of "e" (FocusedViewEnterKeyEventArgs) with real data
471      e.View = View.GetViewFromPtr(view);
472
473      if (_FocusManagerFocusedViewEnterKeyEventHandler != null)
474      {
475         //here we send all data to user event handlers
476         _FocusManagerFocusedViewEnterKeyEventHandler(this, e);
477      }
478   }
479
480   public FocusManager() : this(NDalicManualPINVOKE.new_FocusManager(), true) {
481     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
482   }
483
484   public static FocusManager Get() {
485     FocusManager ret = new FocusManager(NDalicManualPINVOKE.FocusManager_Get(), true);
486     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
487     return ret;
488   }
489
490   public bool SetCurrentFocusView(View view) {
491     bool ret = NDalicManualPINVOKE.FocusManager_SetCurrentFocusActor(swigCPtr, View.getCPtr(view));
492     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
493     return ret;
494   }
495
496   public View GetCurrentFocusView() {
497     View ret = new View(NDalicManualPINVOKE.FocusManager_GetCurrentFocusActor(swigCPtr), true);
498     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
499     return ret;
500   }
501
502   public bool MoveFocus(View.KeyboardFocus.Direction direction) {
503     bool ret = NDalicManualPINVOKE.FocusManager_MoveFocus(swigCPtr, (int)direction);
504     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
505     return ret;
506   }
507
508   public void ClearFocus() {
509     NDalicManualPINVOKE.FocusManager_ClearFocus(swigCPtr);
510     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
511   }
512
513   public void SetFocusGroupLoop(bool enabled) {
514     NDalicManualPINVOKE.FocusManager_SetFocusGroupLoop(swigCPtr, enabled);
515     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
516   }
517
518   public bool GetFocusGroupLoop() {
519     bool ret = NDalicManualPINVOKE.FocusManager_GetFocusGroupLoop(swigCPtr);
520     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
521     return ret;
522   }
523
524   public void SetAsFocusGroup(View view, bool isFocusGroup) {
525     NDalicManualPINVOKE.FocusManager_SetAsFocusGroup(swigCPtr, View.getCPtr(view), isFocusGroup);
526     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
527   }
528
529   public bool IsFocusGroup(View view) {
530     bool ret = NDalicManualPINVOKE.FocusManager_IsFocusGroup(swigCPtr, View.getCPtr(view));
531     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
532     return ret;
533   }
534
535   public View GetFocusGroup(View view) {
536     View ret = new View(NDalicManualPINVOKE.FocusManager_GetFocusGroup(swigCPtr, View.getCPtr(view)), true);
537     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
538     return ret;
539   }
540
541   public void SetFocusIndicatorView(View indicator) {
542     NDalicManualPINVOKE.FocusManager_SetFocusIndicatorActor(swigCPtr, View.getCPtr(indicator));
543     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
544   }
545
546   public View GetFocusIndicatorView() {
547     View ret = new View(NDalicManualPINVOKE.FocusManager_GetFocusIndicatorActor(swigCPtr), true);
548     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
549     return ret;
550   }
551
552   public void SetCustomAlgorithm(ICustomFocusAlgorithm arg0) {
553     _customAlgorithmInterfaceWrapper = new CustomAlgorithmInterfaceWrapper();
554     _customAlgorithmInterfaceWrapper.SetFocusAlgorithm(arg0);
555
556     NDalicPINVOKE.SetCustomAlgorithm(swigCPtr, CustomAlgorithmInterface.getCPtr(_customAlgorithmInterfaceWrapper));
557     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
558   }
559
560   public PreFocusChangeSignal PreFocusChangeSignal() {
561     PreFocusChangeSignal ret = new PreFocusChangeSignal(NDalicManualPINVOKE.FocusManager_PreFocusChangeSignal(swigCPtr), false);
562     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
563     return ret;
564   }
565
566   public FocusChangedSignal FocusChangedSignal() {
567     FocusChangedSignal ret = new FocusChangedSignal(NDalicManualPINVOKE.FocusManager_FocusChangedSignal(swigCPtr), false);
568     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
569     return ret;
570   }
571
572   public FocusGroupChangedSignal FocusGroupChangedSignal() {
573     FocusGroupChangedSignal ret = new FocusGroupChangedSignal(NDalicManualPINVOKE.FocusManager_FocusGroupChangedSignal(swigCPtr), false);
574     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
575     return ret;
576   }
577
578   public ViewSignal FocusedViewEnterKeySignal() {
579     ViewSignal ret = new ViewSignal(NDalicManualPINVOKE.FocusManager_FocusedActorEnterKeySignal(swigCPtr), false);
580     if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
581     return ret;
582   }
583
584   private static readonly FocusManager instance = FocusManager.Get();
585
586   public static FocusManager Instance
587   {
588       get
589       {
590           return instance;
591       }
592   }
593
594   public interface ICustomFocusAlgorithm
595   {
596       View GetNextFocusableView(View current, View proposed, View.KeyboardFocus.Direction direction);
597   }
598
599   private class CustomAlgorithmInterfaceWrapper : CustomAlgorithmInterface
600   {
601       private FocusManager.ICustomFocusAlgorithm _customFocusAlgorithm;
602
603       public CustomAlgorithmInterfaceWrapper()
604       {
605       }
606
607       public void SetFocusAlgorithm(FocusManager.ICustomFocusAlgorithm customFocusAlgorithm)
608       {
609           _customFocusAlgorithm = customFocusAlgorithm;
610       }
611
612       public override View GetNextFocusableView(View current, View proposed, View.KeyboardFocus.Direction direction)
613       {
614           View currentView = View.DownCast<View>(current);
615           View proposedView = View.DownCast<View>(proposed);
616           return _customFocusAlgorithm.GetNextFocusableView(currentView, proposedView, direction);
617       }
618   }
619 }
620
621 }