2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.ComponentModel;
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
21 using System.Diagnostics.CodeAnalysis;
22 using Tizen.NUI.BaseComponents;
27 /// DragAndDrop controls the drag objet and data.
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 public class DragAndDrop : BaseHandle
32 [EditorBrowsable(EditorBrowsableState.Never)]
33 public delegate void DragAndDropEventHandler(View targetView, DragEvent dragEvent);
34 private delegate void InternalDragAndDropEventHandler(global::System.IntPtr dragEvent);
35 private Dictionary<View, InternalDragAndDropEventHandler> targetEventDictionary = new Dictionary<View, InternalDragAndDropEventHandler>();
37 [EditorBrowsable(EditorBrowsableState.Never)]
38 private DragAndDrop() : this(Interop.DragAndDrop.New(), true)
40 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
43 private DragAndDrop(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
48 [EditorBrowsable(EditorBrowsableState.Never)]
49 public static DragAndDrop Instance { get; } = new DragAndDrop();
52 /// Starts drag and drop.
54 /// <param name="sourceView">The soruce view</param>
55 /// <param name="shadowView">The shadow view for drag object</param>
56 /// <param name="dragData">The data to send</param>
57 [EditorBrowsable(EditorBrowsableState.Never)]
58 public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData)
60 if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), View.getCPtr(shadowView), dragData.MimeType, dragData.Data))
62 throw new InvalidOperationException("Fail to StartDragAndDrop");
67 /// Adds listener for drop targets
69 /// <param name="targetView">The target view</param>
70 /// <param name="callback">The callback function to get drag event</param>
71 [EditorBrowsable(EditorBrowsableState.Never)]
72 public void AddListener(View targetView, DragAndDropEventHandler callback)
74 InternalDragAndDropEventHandler cb = (dragEvent) =>
76 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
77 DragEvent ev = new DragEvent();
78 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
79 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
81 if (type == DragType.Enter)
84 callback(targetView, ev);
86 else if (type == DragType.Leave)
89 callback(targetView, ev);
91 else if (type == DragType.Move)
94 callback(targetView, ev);
96 else if (type == DragType.Drop)
99 ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
100 ev.Data = Interop.DragAndDrop.GetData(dragEvent);
101 callback(targetView, ev);
105 targetEventDictionary.Add(targetView, cb);
107 if (!Interop.DragAndDrop.AddListener(SwigCPtr, View.getCPtr(targetView),
108 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
110 throw new InvalidOperationException("Fail to AddListener");
115 /// Removes listener for drop targets
117 /// <param name="targetView">The target view</param>
118 /// <param name="callback">The callback function to remove</param>
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 public void RemoveListener(View targetView, DragAndDropEventHandler callback)
122 if (!targetEventDictionary.ContainsKey(targetView))
124 throw new InvalidOperationException("Fail to RemoveListener");
127 InternalDragAndDropEventHandler cb = targetEventDictionary[targetView];
128 targetEventDictionary.Remove(targetView);
129 if (!Interop.DragAndDrop.RemoveListener(SwigCPtr, View.getCPtr(targetView),
130 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
132 throw new InvalidOperationException("Fail to RemoveListener");