[NUI] Add DataSelected in Clipboard
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / Clipboard / ClipboardEvent.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
18 using System;
19 using System.ComponentModel;
20 using System.Runtime.InteropServices;
21
22 namespace Tizen.NUI
23 {
24     /// <summary>
25     /// This specifies clipboard event data.
26     /// </summary>
27     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815: Override equals and operator equals on value types")]
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public struct ClipEvent
30     {
31         /// <summary>
32         /// The mime type of clipboard event data.
33         /// </summary>
34         public string MimeType { get; set; }
35         /// <summary>
36         /// The clipboard event data to receive.
37         /// </summary>
38         public string Data { get; set; }
39     }
40
41     /// <summary>
42     /// ClipboardDataSelectedEventArgs is a class to record clipboard selected event arguments which will be sent to user.<br/>
43     /// This is to catch data selection event.
44     /// </summary>
45     [EditorBrowsable(EditorBrowsableState.Never)]
46     public class ClipboardDataSelectedEventArgs : EventArgs
47     {
48         /// <summary>
49         /// The mime type of clipboard selected data.
50         /// </summary>
51         public string MimeType { get; set; }
52     }
53
54     /// <summary>
55     /// ClipboardEventArgs is a class to record clipboard event arguments which will be sent to user.<br/>
56     /// This is for internal use only.
57     /// </summary>
58     internal class ClipboardEventArgs : EventArgs
59     {
60         /// <summary>
61         /// True if data receive is successful.
62         /// </summary>
63         public bool Success { get; set; }
64
65         /// <summary>
66         /// The data request id to identify the response by request.
67         /// </summary>
68         public uint Id { get; set; }
69
70         /// <summary>
71         /// Clipboard event data.
72         /// </summary>
73         public ClipEvent ClipEvent { get; set; }
74     }
75
76     /// <summary>
77     /// Clipboard event.
78     /// </summary>
79     public partial class Clipboard
80     {
81         private EventHandler<ClipboardEventArgs> clipboardDataReceivedEventHandler;
82         private ClipboardDataReceivedCallback clipboardDataReceivedCallback;
83
84         private EventHandler<ClipboardDataSelectedEventArgs> clipboardDataSelectedEventHandler;
85         private ClipboardDataSelectedCallback clipboardDataSelectedCallback;
86
87         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
88         private delegate void ClipboardDataReceivedCallback(uint id, string mimeType, string data);
89
90         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
91         private delegate void ClipboardDataSelectedCallback(string mimeType);
92
93         /// <summary>
94         /// The DataSelected event is emitted when a copy event occurs somewhere.<br/>
95         /// In order for this event to operate normally,<br/>
96         /// the process using this event must be Secondary Selection.
97         /// </summary>
98         /// <example>
99         /// The following example demonstrates how to use the DataSelected.
100         /// <code>
101         /// kvmService.SetSecondarySelction(); // precondition
102         ///
103         /// Clipboard.Instance.DataSelected += (s, e) =>
104         /// {
105         ///     string selectedType = e.MimeType;
106         /// };
107         /// </code>
108         /// </example>
109         [EditorBrowsable(EditorBrowsableState.Never)]
110         public event EventHandler<ClipboardDataSelectedEventArgs> DataSelected
111         {
112             add
113             {
114                 if (clipboardDataSelectedEventHandler == null)
115                 {
116                     clipboardDataSelectedCallback = (OnClipboardDataSelected);
117                     ClipboardDataSelectedSignal().Connect(clipboardDataSelectedCallback);
118                 }
119                 clipboardDataSelectedEventHandler += value;
120             }
121             remove
122             {
123                 clipboardDataSelectedEventHandler -= value;
124                 if (clipboardDataSelectedEventHandler == null && ClipboardDataSelectedSignal().Empty() == false)
125                 {
126                     ClipboardDataSelectedSignal().Disconnect(clipboardDataSelectedCallback);
127                 }
128             }
129         }
130
131         private event EventHandler<ClipboardEventArgs> ClipboardDataReceived
132         {
133             add
134             {
135                 if (clipboardDataReceivedEventHandler == null)
136                 {
137                     clipboardDataReceivedCallback = (OnClipboardDataReceived);
138                     ClipboardDataReceivedSignal().Connect(clipboardDataReceivedCallback);
139                 }
140                 clipboardDataReceivedEventHandler += value;
141             }
142             remove
143             {
144                 clipboardDataReceivedEventHandler -= value;
145                 if (clipboardDataReceivedEventHandler == null && ClipboardDataReceivedSignal().Empty() == false)
146                 {
147                     ClipboardDataReceivedSignal().Disconnect(clipboardDataReceivedCallback);
148                 }
149             }
150         }
151
152         internal ClipboardSignal ClipboardDataSelectedSignal()
153         {
154             var ret = new ClipboardSignal(Interop.Clipboard.ClipboardDataSelectedSignal(SwigCPtr), false);
155             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
156             return ret;
157         }
158
159         private void OnClipboardDataSelected(string mimeType)
160         {
161             var e = new ClipboardDataSelectedEventArgs();
162             e.MimeType = mimeType;
163             clipboardDataSelectedEventHandler?.Invoke(this, e);
164         }
165
166         internal ClipboardSignal ClipboardDataReceivedSignal()
167         {
168             var ret = new ClipboardSignal(Interop.Clipboard.ClipboardDataReceivedSignal(SwigCPtr), false);
169             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
170             return ret;
171         }
172
173         private void OnClipboardDataReceived(uint id, string mimeType, string data)
174         {
175             var e = new ClipboardEventArgs();
176             var clipEvent = new ClipEvent()
177             {
178                 MimeType = mimeType,
179                 Data = data,
180             };
181             e.ClipEvent = clipEvent;
182             e.Id = id;
183             e.Success = (String.IsNullOrEmpty(e.ClipEvent.MimeType) && String.IsNullOrEmpty(e.ClipEvent.Data)) ? false : true;
184
185             clipboardDataReceivedEventHandler?.Invoke(this, e);
186         }
187     }
188 }