[NUI] Change all CallingConvention to `Cdecl`
[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     /// ClipboardEventArgs is a class to record clipboard event arguments which will be sent to user.<br/>
43     /// This is for internal use only.
44     /// </summary>
45     internal class ClipboardEventArgs : EventArgs
46     {
47         /// <summary>
48         /// True if data receive is successful.
49         /// </summary>
50         public bool Success { get; set; }
51
52         /// <summary>
53         /// The data request id to identify the response by request.
54         /// </summary>
55         public uint Id { get; set; }
56
57         /// <summary>
58         /// Clipboard event data.
59         /// </summary>
60         public ClipEvent ClipEvent { get; set; }
61     }
62
63     /// <summary>
64     /// Clipboard event.
65     /// </summary>
66     public partial class Clipboard
67     {
68         private EventHandler<ClipboardEventArgs> clipboardDataReceivedEventHandler;
69         private ClipboardDataReceivedCallback clipboardDataReceivedCallback;
70
71         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
72         private delegate void ClipboardDataReceivedCallback(uint id, string mimeType, string data);
73
74         private event EventHandler<ClipboardEventArgs> ClipboardDataReceived
75         {
76             add
77             {
78                 if (clipboardDataReceivedEventHandler == null)
79                 {
80                     clipboardDataReceivedCallback = (OnClipboardDataReceived);
81                     ClipboardDataReceivedSignal().Connect(clipboardDataReceivedCallback);
82                 }
83                 clipboardDataReceivedEventHandler += value;
84             }
85             remove
86             {
87                 clipboardDataReceivedEventHandler -= value;
88                 if (clipboardDataReceivedEventHandler == null && ClipboardDataReceivedSignal().Empty() == false)
89                 {
90                     ClipboardDataReceivedSignal().Disconnect(clipboardDataReceivedCallback);
91                 }
92             }
93         }
94
95         internal ClipboardSignal ClipboardDataReceivedSignal()
96         {
97             var ret = new ClipboardSignal(Interop.Clipboard.ClipboardDataReceivedSignal(SwigCPtr), false);
98             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
99             return ret;
100         }
101
102         private void OnClipboardDataReceived(uint id, string mimeType, string data)
103         {
104             var e = new ClipboardEventArgs();
105             var clipEvent = new ClipEvent()
106             {
107                 MimeType = mimeType,
108                 Data = data,
109             };
110             e.ClipEvent = clipEvent;
111             e.Id = id;
112             e.Success = (String.IsNullOrEmpty(e.ClipEvent.MimeType) && String.IsNullOrEmpty(e.ClipEvent.Data)) ? false : true;
113
114             clipboardDataReceivedEventHandler?.Invoke(this, e);
115         }
116     }
117 }