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.Collections.Generic;
19 using System.ComponentModel;
20 using System.Diagnostics.CodeAnalysis;
22 using System.Runtime.InteropServices;
24 using Tizen.NUI.BaseComponents;
31 [EditorBrowsable(EditorBrowsableState.Never)]
32 public partial class Clipboard : BaseHandle
34 private static readonly Clipboard instance = Clipboard.GetInternal();
37 /// User callback for clipboard event.
40 /// Receives requested data through <see cref="Tizen.NUI.ClipEvent"/>.
42 public delegate void ClipboardCallback(bool success, ClipEvent clipEvent);
44 internal bool hasClipboardDataReceived = false;
45 internal Dictionary<uint, ClipboardCallback> receivedCallbackDictionary = new Dictionary<uint, ClipboardCallback>();
47 private Clipboard(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
52 /// Gets the singleton instance of Clipboard.
54 [EditorBrowsable(EditorBrowsableState.Never)]
55 public static Clipboard Instance
63 private static Clipboard GetInternal()
65 global::System.IntPtr cPtr = Interop.Clipboard.Get();
67 if(cPtr == global::System.IntPtr.Zero)
69 NUILog.ErrorBacktrace("Clipboard.Instance called before Application created, or after Application terminated!");
70 // Do not throw exception until TCT test passed.
73 Clipboard ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Clipboard;
76 NUILog.ErrorBacktrace("Clipboard.GetInternal() Should be called only one time!");
77 object dummyObect = new object();
79 HandleRef CPtr = new HandleRef(dummyObect, cPtr);
80 Interop.BaseHandle.DeleteBaseHandle(CPtr);
81 CPtr = new HandleRef(null, global::System.IntPtr.Zero);
85 ret = new Clipboard(cPtr, true);
88 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
92 [EditorBrowsable(EditorBrowsableState.Never)]
93 protected override void Dispose(bool disposing)
97 NUILog.ErrorBacktrace("We should not manually dispose for singleton class!");
101 base.Dispose(disposing);
106 /// Request set the given data to the clipboard.
108 /// <param name="mimeType">The mime type of the data.</param>
109 /// <param name="data">The data to be set on the clipboard.</param>
110 /// <returns>True if the internal clipboard sending request is successful.</returns>
112 /// The following example demonstrates how to use the SetData.
114 /// string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
115 /// Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, "Hello Clipboard");
118 [EditorBrowsable(EditorBrowsableState.Never)]
119 public bool SetData(string mimeType, string data)
121 bool setData = Interop.Clipboard.SetData(SwigCPtr, mimeType, data);
122 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
127 /// Request get data of the specified mime type from clipboard<br/>
128 /// and invokes the given callback with the received clipboard data.
130 /// <param name="mimeType">The mime type of data to request.</param>
131 /// <param name="dataReceivedCallback">The callback method to handle the received clipboard data.</param>
133 /// GetData() method is introduced to fetch data of the specified mime type,<br/>
134 /// and it expects a callback function as a parameter.<br/>
135 /// The given callback is invoked with received clipboard data.<br/>
136 /// The callback is designed to be used only once for handling the data.
139 /// The following example demonstrates how to use the GetData and ClipboardCallback.
141 /// string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
142 /// Clipboard.Instance.GetData(MIME_TYPE_PLAIN_TEXT, OnClipboardDataReceived);
144 /// public void OnClipboardDataReceived(bool success, ClipEvent clipEvent)
146 /// if (!success) return;
147 /// string mimeType = clipEvent.MimeType;
148 /// string data = clipEvent.Data;
152 [EditorBrowsable(EditorBrowsableState.Never)]
153 public void GetData(string mimeType, ClipboardCallback dataReceivedCallback)
155 if(!hasClipboardDataReceived)
157 ClipboardDataReceived += OnClipboardDataReceived;
158 hasClipboardDataReceived = true;
161 uint id = Interop.Clipboard.GetData(SwigCPtr, mimeType);
162 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
166 // Calls the failure callback even if the request fails.
167 var clipEvent = new ClipEvent()
169 MimeType = string.Empty,
172 dataReceivedCallback(false, clipEvent);
176 receivedCallbackDictionary[id] = dataReceivedCallback;
180 private void OnClipboardDataReceived(object sender, ClipboardEventArgs e)
182 if (!receivedCallbackDictionary.Any()) return;
185 if (receivedCallbackDictionary.ContainsKey(id))
187 ClipboardCallback callback = receivedCallbackDictionary[id];
188 if (callback != null)
190 callback(e.Success, e.ClipEvent);
192 receivedCallbackDictionary.Remove(id);
199 protected override void Dispose(DisposeTypes type)
206 if (hasClipboardDataReceived)
208 ClipboardDataReceived -= OnClipboardDataReceived;
209 receivedCallbackDictionary.Clear();
214 if (clipboardDataReceivedCallback != null)
216 this.ClipboardDataReceivedSignal().Disconnect(clipboardDataReceivedCallback);
219 if (clipboardDataSelectedCallback != null)
221 this.ClipboardDataSelectedSignal().Disconnect(clipboardDataSelectedCallback);