2 * Copyright(c) 2023 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 object and data.
29 /// <since_tizen> 10 </since_tizen>
30 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000: Dispose objects before losing scope", Justification = "It does not have ownership.")]
31 public class DragAndDrop : BaseHandle
33 public delegate void SourceEventHandler(DragSourceEventType sourceEventType);
34 private delegate void InternalSourceEventHandler(int sourceEventType);
35 public delegate void DragAndDropEventHandler(View targetView, DragEvent dragEvent);
36 [EditorBrowsable(EditorBrowsableState.Never)]
37 public delegate void DragAndDropWindowEventHandler(Window targetWindow, DragEvent dragEvent);
38 private delegate void InternalDragAndDropEventHandler(global::System.IntPtr dragEvent);
39 private InternalSourceEventHandler sourceEventCb;
40 private Dictionary<View, InternalDragAndDropEventHandler> targetEventDictionary = new Dictionary<View, InternalDragAndDropEventHandler>();
41 private Dictionary<Window, InternalDragAndDropEventHandler> targetWindowEventDictionary = new Dictionary<Window, InternalDragAndDropEventHandler>();
42 private View mShadowView;
43 private Window mDragWindow;
44 private int shadowWidth;
45 private int shadowHeight;
46 private int dragWindowOffsetX = 0;
47 private int dragWindowOffsetY = 0;
49 private bool initDrag = false;
51 private const int MinDragWindowWidth = 100;
52 private const int MinDragWindowHeight = 100;
54 private DragAndDrop() : this(Interop.DragAndDrop.New(), true)
56 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
59 private DragAndDrop(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
64 private void ReleaseDragWindow()
70 //Application has Shadow View ownership, so DnD doesn't dispose Shadow View
71 mDragWindow.Remove(mShadowView);
75 mDragWindow.Dispose();
81 /// Gets the singleton instance of DragAndDrop.
83 /// <since_tizen> 10 </since_tizen>
84 public static DragAndDrop Instance { get; } = new DragAndDrop();
87 /// Starts drag and drop.
89 /// <param name="sourceView">The soruce view</param>
90 /// <param name="shadowView">The shadow view for drag object</param>
91 /// <param name="dragData">The data to send</param>
92 /// <param name="callback">The source event callback</param>
93 /// <since_tizen> 10 </since_tizen>
94 public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData, SourceEventHandler callback)
98 Tizen.Log.Fatal("NUI", "Start Drag And Drop Initializing...");
103 if (Window.IsSupportedMultiWindow() == false)
105 throw new NotSupportedException("This device does not support surfaceless_context. So Window cannot be created.");
108 if (null == shadowView)
110 throw new ArgumentNullException(nameof(shadowView));
115 shadowWidth = (int)shadowView.Size.Width;
116 shadowHeight = (int)shadowView.Size.Height;
118 if (shadowView.Size.Width < MinDragWindowWidth)
120 shadowWidth = MinDragWindowWidth;
123 if (shadowView.Size.Height < MinDragWindowHeight)
125 shadowHeight = MinDragWindowHeight;
128 mDragWindow = new Window("DragWindow", new Rectangle(dragWindowOffsetX, dragWindowOffsetY, shadowWidth, shadowHeight), true)
130 BackgroundColor = Color.Transparent,
135 //Set Window Orientation Available
136 List<Window.WindowOrientation> list = new List<Window.WindowOrientation>();
137 list.Add(Window.WindowOrientation.Landscape);
138 list.Add(Window.WindowOrientation.LandscapeInverse);
139 list.Add(Window.WindowOrientation.NoOrientationPreference);
140 list.Add(Window.WindowOrientation.Portrait);
141 list.Add(Window.WindowOrientation.PortraitInverse);
142 mDragWindow.SetAvailableOrientations(list);
144 //Initialize Drag Window Size based on Shadow View Size,
145 //Don't set Drag Window Posiiton, Window Server sets Position Internally
146 mDragWindow.SetWindowSize(new Size(shadowWidth, shadowHeight));
148 //Make Position 0, 0 for Moving into Drag Window
149 shadowView.Position = new Position(0, 0);
151 mShadowView = shadowView;
152 mDragWindow.Add(mShadowView);
154 sourceEventCb = (sourceEventType) =>
156 if ((DragSourceEventType)sourceEventType != DragSourceEventType.Start)
158 Tizen.Log.Fatal("NUI", "DnD Source Event is Called");
162 callback((DragSourceEventType)sourceEventType);
165 //Show Drag Window before StartDragAndDrop
168 if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
169 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
171 throw new InvalidOperationException("Fail to StartDragAndDrop");
180 /// Adds listener for drop targets
182 /// <param name="targetView">The target view</param>
183 /// <param name="callback">The callback function to get drag event when the drag source enters, moves, leaves and drops on the drop target</param>
184 /// <since_tizen> 10 </since_tizen>
185 public void AddListener(View targetView, DragAndDropEventHandler callback)
187 InternalDragAndDropEventHandler cb = (dragEvent) =>
189 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
190 DragEvent ev = new DragEvent();
191 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
192 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, true);
194 if (type == DragType.Enter)
197 ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
198 callback(targetView, ev);
200 else if (type == DragType.Leave)
203 callback(targetView, ev);
205 else if (type == DragType.Move)
208 ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
209 callback(targetView, ev);
211 else if (type == DragType.Drop)
214 ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
215 ev.Data = Interop.DragAndDrop.GetData(dragEvent);
216 callback(targetView, ev);
220 targetEventDictionary.Add(targetView, cb);
222 if (!Interop.DragAndDrop.AddListener(SwigCPtr, View.getCPtr(targetView),
223 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
225 throw new InvalidOperationException("Fail to AddListener for View");
230 /// Removes listener for drop targets
232 /// <param name="targetView">The target view</param>
233 /// <param name="callback">The callback function to remove</param>
234 /// <since_tizen> 10 </since_tizen>
235 public void RemoveListener(View targetView, DragAndDropEventHandler callback)
237 if (!targetEventDictionary.ContainsKey(targetView))
239 throw new InvalidOperationException("Fail to RemoveListener for View");
242 InternalDragAndDropEventHandler cb = targetEventDictionary[targetView];
243 targetEventDictionary.Remove(targetView);
244 if (!Interop.DragAndDrop.RemoveListener(SwigCPtr, View.getCPtr(targetView),
245 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
247 throw new InvalidOperationException("Fail to RemoveListener for View");
252 /// Adds listener for drop targets
254 /// <param name="targetWindow">The target Window</param>
255 /// <param name="callback">The callback function to get drag event when the drag source enters, moves, leaves and drops on the drop target</param>
256 [EditorBrowsable(EditorBrowsableState.Never)]
257 public void AddListener(Window targetWindow, DragAndDropWindowEventHandler callback)
259 InternalDragAndDropEventHandler cb = (dragEvent) =>
261 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
262 DragEvent ev = new DragEvent();
263 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
264 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
266 if (type == DragType.Enter)
269 callback(targetWindow, ev);
271 else if (type == DragType.Leave)
274 callback(targetWindow, ev);
276 else if (type == DragType.Move)
279 callback(targetWindow, ev);
281 else if (type == DragType.Drop)
284 ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
285 ev.Data = Interop.DragAndDrop.GetData(dragEvent);
286 callback(targetWindow, ev);
290 targetWindowEventDictionary.Add(targetWindow, cb);
292 if (!Interop.DragAndDrop.WindowAddListener(SwigCPtr, Window.getCPtr(targetWindow),
293 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
295 throw new InvalidOperationException("Fail to AddListener for Window");
300 /// Removes listener for drop targets
302 /// <param name="targetWindow">The target window</param>
303 /// <param name="callback">The callback function to remove</param>
304 [EditorBrowsable(EditorBrowsableState.Never)]
305 public void RemoveListener(Window targetWindow, DragAndDropWindowEventHandler callback)
307 if (!targetWindowEventDictionary.ContainsKey(targetWindow))
309 throw new InvalidOperationException("Fail to RemoveListener for Window");
312 InternalDragAndDropEventHandler cb = targetWindowEventDictionary[targetWindow];
313 targetWindowEventDictionary.Remove(targetWindow);
314 if (!Interop.DragAndDrop.WindowRemoveListener(SwigCPtr, Window.getCPtr(targetWindow),
315 new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
317 throw new InvalidOperationException("Fail to RemoveListener for Window");
322 /// Sets drag window offset
324 /// <param name="x">The x direction offset</param>
325 /// <param name="y">The y direction offset</param>
326 [EditorBrowsable(EditorBrowsableState.Never)]
327 public void SetDragWindowOffset(int x, int y)
329 dragWindowOffsetX = x;
330 dragWindowOffsetY = y;