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.
19 using System.ComponentModel;
20 using System.Runtime.InteropServices;
25 /// This specifies clipboard event data.
27 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1815: Override equals and operator equals on value types")]
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public struct ClipEvent
32 /// The mime type of clipboard event data.
34 public string MimeType { get; set; }
36 /// The clipboard event data to receive.
38 public string Data { get; set; }
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.
45 [EditorBrowsable(EditorBrowsableState.Never)]
46 public class ClipboardDataSelectedEventArgs : EventArgs
49 /// The mime type of clipboard selected data.
51 public string MimeType { get; set; }
55 /// ClipboardEventArgs is a class to record clipboard event arguments which will be sent to user.<br/>
56 /// This is for internal use only.
58 internal class ClipboardEventArgs : EventArgs
61 /// True if data receive is successful.
63 public bool Success { get; set; }
66 /// The data request id to identify the response by request.
68 public uint Id { get; set; }
71 /// Clipboard event data.
73 public ClipEvent ClipEvent { get; set; }
79 public partial class Clipboard
81 private EventHandler<ClipboardEventArgs> clipboardDataReceivedEventHandler;
82 private ClipboardDataReceivedCallback clipboardDataReceivedCallback;
84 private EventHandler<ClipboardDataSelectedEventArgs> clipboardDataSelectedEventHandler;
85 private ClipboardDataSelectedCallback clipboardDataSelectedCallback;
87 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
88 private delegate void ClipboardDataReceivedCallback(uint id, string mimeType, string data);
90 [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
91 private delegate void ClipboardDataSelectedCallback(string mimeType);
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.
99 /// The following example demonstrates how to use the DataSelected.
101 /// kvmService.SetSecondarySelction(); // precondition
103 /// Clipboard.Instance.DataSelected += (s, e) =>
105 /// string selectedType = e.MimeType;
109 [EditorBrowsable(EditorBrowsableState.Never)]
110 public event EventHandler<ClipboardDataSelectedEventArgs> DataSelected
114 if (clipboardDataSelectedEventHandler == null)
116 clipboardDataSelectedCallback = (OnClipboardDataSelected);
117 ClipboardDataSelectedSignal().Connect(clipboardDataSelectedCallback);
119 clipboardDataSelectedEventHandler += value;
123 clipboardDataSelectedEventHandler -= value;
124 if (clipboardDataSelectedEventHandler == null && ClipboardDataSelectedSignal().Empty() == false)
126 ClipboardDataSelectedSignal().Disconnect(clipboardDataSelectedCallback);
131 private event EventHandler<ClipboardEventArgs> ClipboardDataReceived
135 if (clipboardDataReceivedEventHandler == null)
137 clipboardDataReceivedCallback = (OnClipboardDataReceived);
138 ClipboardDataReceivedSignal().Connect(clipboardDataReceivedCallback);
140 clipboardDataReceivedEventHandler += value;
144 clipboardDataReceivedEventHandler -= value;
145 if (clipboardDataReceivedEventHandler == null && ClipboardDataReceivedSignal().Empty() == false)
147 ClipboardDataReceivedSignal().Disconnect(clipboardDataReceivedCallback);
152 internal ClipboardSignal ClipboardDataSelectedSignal()
154 var ret = new ClipboardSignal(Interop.Clipboard.ClipboardDataSelectedSignal(SwigCPtr), false);
155 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
159 private void OnClipboardDataSelected(string mimeType)
161 var e = new ClipboardDataSelectedEventArgs();
162 e.MimeType = mimeType;
163 clipboardDataSelectedEventHandler?.Invoke(this, e);
166 internal ClipboardSignal ClipboardDataReceivedSignal()
168 var ret = new ClipboardSignal(Interop.Clipboard.ClipboardDataReceivedSignal(SwigCPtr), false);
169 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
173 private void OnClipboardDataReceived(uint id, string mimeType, string data)
175 var e = new ClipboardEventArgs();
176 var clipEvent = new ClipEvent()
181 e.ClipEvent = clipEvent;
183 e.Success = (String.IsNullOrEmpty(e.ClipEvent.MimeType) && String.IsNullOrEmpty(e.ClipEvent.Data)) ? false : true;
185 clipboardDataReceivedEventHandler?.Invoke(this, e);