[NUI] DragAndDrop : Add APIs for Window
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / DragAndDrop / DragAndDrop.cs
1 /*
2  * Copyright(c) 2023 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 using System;
18 using System.ComponentModel;
19 using System.Runtime.InteropServices;
20 using System.Collections.Generic;
21 using System.Diagnostics.CodeAnalysis;
22 using Tizen.NUI.BaseComponents;
23
24 namespace Tizen.NUI
25 {
26     /// <summary>
27     /// DragAndDrop controls the drag object and data.
28     /// </summary>
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
32     {
33         public delegate void SourceEventHandler(DragSourceEventType sourceEventType);
34         private delegate void InternalSourceEventHandler(int sourceEventType);
35         public delegate void DragAndDropEventHandler(View targetView, DragEvent dragEvent);
36         public delegate void DragAndDropWindowEventHandler(Window targetWindow, DragEvent dragEvent);
37         private delegate void InternalDragAndDropEventHandler(global::System.IntPtr dragEvent);
38         private InternalSourceEventHandler sourceEventCb;
39         private Dictionary<View, InternalDragAndDropEventHandler> targetEventDictionary = new Dictionary<View, InternalDragAndDropEventHandler>();
40         private Dictionary<Window, InternalDragAndDropEventHandler> targetWindowEventDictionary = new Dictionary<Window, InternalDragAndDropEventHandler>();
41         private View mShadowView;
42         private Window mDragWindow;
43         private int shadowWidth;
44         private int shadowHeight;
45
46         private bool initDrag = false;
47
48         private const int MinDragWindowWidth = 100;
49         private const int MinDragWindowHeight = 100;
50
51         private DragAndDrop() : this(Interop.DragAndDrop.New(), true)
52         {
53             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
54         }
55
56         private DragAndDrop(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
57         {
58
59         }
60
61         private void ReleaseDragWindow()
62         {
63             if (mDragWindow)
64             {                        
65                 if (mShadowView)
66                 {
67                     //Application has Shadow View ownership, so DnD doesn't dispose Shadow View
68                     mDragWindow.Remove(mShadowView);
69                     mShadowView = null;
70                 }
71             
72                 mDragWindow.Dispose();
73                 mDragWindow = null;                      
74             }         
75         }
76
77         /// <summary>
78         /// Gets the singleton instance of DragAndDrop.
79         /// </summary>
80         /// <since_tizen> 10 </since_tizen>
81         public static DragAndDrop Instance { get; } = new DragAndDrop();
82
83         /// <summary>
84         /// Starts drag and drop.
85         /// </summary>
86         /// <param name="sourceView">The soruce view</param>
87         /// <param name="shadowView">The shadow view for drag object</param>
88         /// <param name="dragData">The data to send</param>
89         /// <param name="callback">The source event callback</param>
90         /// <since_tizen> 10 </since_tizen>
91         public void StartDragAndDrop(View sourceView, View shadowView, DragData dragData, SourceEventHandler callback)
92         {            
93             if (initDrag)
94             {
95                  Tizen.Log.Fatal("NUI", "Start Drag And Drop Initializing...");
96                  return;
97             }
98             initDrag = true;
99
100             if (Window.IsSupportedMultiWindow() == false)
101             {
102                 throw new NotSupportedException("This device does not support surfaceless_context. So Window cannot be created.");
103             }
104
105             if (null == shadowView)
106             {
107                 throw new ArgumentNullException(nameof(shadowView));
108             }
109
110             ReleaseDragWindow();
111
112             shadowWidth = (int)shadowView.Size.Width;
113             shadowHeight = (int)shadowView.Size.Height;
114
115             if (shadowView.Size.Width < MinDragWindowWidth)
116             {
117                 shadowWidth = MinDragWindowWidth;
118             }
119
120             if (shadowView.Size.Height < MinDragWindowHeight)
121             {
122                 shadowHeight = MinDragWindowHeight;
123             }
124
125             mDragWindow = new Window("DragWindow", new Rectangle(-shadowWidth, -shadowHeight, shadowWidth, shadowHeight), true)
126             {
127                 BackgroundColor = Color.Transparent,
128             };
129
130             if (mDragWindow)
131             {
132                 //Set Window Orientation Available
133                 List<Window.WindowOrientation> list = new List<Window.WindowOrientation>();
134                 list.Add(Window.WindowOrientation.Landscape);
135                 list.Add(Window.WindowOrientation.LandscapeInverse);
136                 list.Add(Window.WindowOrientation.NoOrientationPreference);
137                 list.Add(Window.WindowOrientation.Portrait);
138                 list.Add(Window.WindowOrientation.PortraitInverse);
139                 mDragWindow.SetAvailableOrientations(list);
140
141                 //Initialize Drag Window Size based on Shadow View Size,
142                 //Don't set Drag Window Posiiton, Window Server sets Position Internally
143                 mDragWindow.SetWindowSize(new Size(shadowWidth, shadowHeight));
144
145                 //Make Position 0, 0 for Moving into Drag Window
146                 shadowView.Position = new Position(0, 0);
147             
148                 mShadowView = shadowView;
149                 mDragWindow.Add(mShadowView);
150            
151                 sourceEventCb = (sourceEventType) =>
152                 {   
153                     if ((DragSourceEventType)sourceEventType != DragSourceEventType.Start)
154                     {     
155                         Tizen.Log.Fatal("NUI", "DnD Source Event is Called");  
156                         ReleaseDragWindow();                
157                     }
158
159                     callback((DragSourceEventType)sourceEventType);
160                 };
161
162                 //Show Drag Window before StartDragAndDrop
163                 mDragWindow.Show();
164
165                 if (!Interop.DragAndDrop.StartDragAndDrop(SwigCPtr, View.getCPtr(sourceView), Window.getCPtr(mDragWindow), dragData.MimeType, dragData.Data,
166                                                         new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(sourceEventCb))))
167                 {
168                     throw new InvalidOperationException("Fail to StartDragAndDrop");
169                 }
170
171             }         
172             
173             initDrag = false;
174         }
175
176         /// <summary>
177         /// Adds listener for drop targets
178         /// </summary>
179         /// <param name="targetView">The target view</param>
180         /// <param name="callback">The callback function to get drag event when the drag source enters, moves, leaves and drops on the drop target</param>
181         /// <since_tizen> 10 </since_tizen>
182         public void AddListener(View targetView, DragAndDropEventHandler callback)
183         {
184             InternalDragAndDropEventHandler cb = (dragEvent) =>
185             {
186                 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
187                 DragEvent ev = new DragEvent();
188                 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
189                 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
190
191                 if (type == DragType.Enter)
192                 {
193                     ev.DragType = type;
194                     callback(targetView, ev);
195                 }
196                 else if (type == DragType.Leave)
197                 {
198                     ev.DragType = type;
199                     callback(targetView, ev);
200                 }
201                 else if (type == DragType.Move)
202                 {
203                     ev.DragType = type;
204                     callback(targetView, ev);
205                 }
206                 else if (type == DragType.Drop)
207                 {
208                     ev.DragType = type;
209                     ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
210                     ev.Data = Interop.DragAndDrop.GetData(dragEvent);
211                     callback(targetView, ev);
212                 }
213             };
214
215             targetEventDictionary.Add(targetView, cb);
216
217             if (!Interop.DragAndDrop.AddListener(SwigCPtr, View.getCPtr(targetView),
218                                                  new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
219             {
220                  throw new InvalidOperationException("Fail to AddListener for View");
221             }
222         }
223
224         /// <summary>
225         /// Removes listener for drop targets
226         /// </summary>
227         /// <param name="targetView">The target view</param>
228         /// <param name="callback">The callback function to remove</param>
229         /// <since_tizen> 10 </since_tizen>
230         public void RemoveListener(View targetView, DragAndDropEventHandler callback)
231         {
232             if (!targetEventDictionary.ContainsKey(targetView))
233             {
234                  throw new InvalidOperationException("Fail to RemoveListener for View");
235             }
236
237             InternalDragAndDropEventHandler cb = targetEventDictionary[targetView];
238             targetEventDictionary.Remove(targetView);
239             if (!Interop.DragAndDrop.RemoveListener(SwigCPtr, View.getCPtr(targetView),
240                                                     new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
241             {
242                  throw new InvalidOperationException("Fail to RemoveListener for View");
243             }
244         }
245
246         /// <summary>
247         /// Adds listener for drop targets
248         /// </summary>
249         /// <param name="targetWindow">The target Window</param>
250         /// <param name="callback">The callback function to get drag event when the drag source enters, moves, leaves and drops on the drop target</param>
251         [EditorBrowsable(EditorBrowsableState.Never)]
252         public void AddListener(Window targetWindow, DragAndDropWindowEventHandler callback)
253         {
254             InternalDragAndDropEventHandler cb = (dragEvent) =>
255             {
256                 DragType type = (DragType)Interop.DragAndDrop.GetAction(dragEvent);
257                 DragEvent ev = new DragEvent();
258                 global::System.IntPtr cPtr = Interop.DragAndDrop.GetPosition(dragEvent);
259                 ev.Position = (cPtr == global::System.IntPtr.Zero) ? null : new Position(cPtr, false);
260
261                 if (type == DragType.Enter)
262                 {
263                     ev.DragType = type;
264                     callback(targetWindow, ev);
265                 }
266                 else if (type == DragType.Leave)
267                 {
268                     ev.DragType = type;
269                     callback(targetWindow, ev);
270                 }
271                 else if (type == DragType.Move)
272                 {
273                     ev.DragType = type;
274                     callback(targetWindow, ev);
275                 }
276                 else if (type == DragType.Drop)
277                 {
278                     ev.DragType = type;
279                     ev.MimeType = Interop.DragAndDrop.GetMimeType(dragEvent);
280                     ev.Data = Interop.DragAndDrop.GetData(dragEvent);
281                     callback(targetWindow, ev);
282                 }
283             };
284
285             targetWindowEventDictionary.Add(targetWindow, cb);
286
287             if (!Interop.DragAndDrop.WindowAddListener(SwigCPtr, Window.getCPtr(targetWindow),
288                                                        new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
289             {
290                  throw new InvalidOperationException("Fail to AddListener for Window");
291             }
292         }
293
294         /// <summary>
295         /// Removes listener for drop targets
296         /// </summary>
297         /// <param name="targetWindow">The target window</param>
298         /// <param name="callback">The callback function to remove</param>
299         [EditorBrowsable(EditorBrowsableState.Never)]
300         public void RemoveListener(Window targetWindow, DragAndDropWindowEventHandler callback)
301         {
302             if (!targetWindowEventDictionary.ContainsKey(targetWindow))
303             {
304                  throw new InvalidOperationException("Fail to RemoveListener for Window");
305             }
306
307             InternalDragAndDropEventHandler cb = targetWindowEventDictionary[targetWindow];
308             targetWindowEventDictionary.Remove(targetWindow);
309             if (!Interop.DragAndDrop.WindowRemoveListener(SwigCPtr, Window.getCPtr(targetWindow),
310                                                           new global::System.Runtime.InteropServices.HandleRef(this, Marshal.GetFunctionPointerForDelegate<Delegate>(cb))))
311             {
312                  throw new InvalidOperationException("Fail to RemoveListener for Window");
313             }
314         }
315     }
316 }