[NUI] Change all CallingConvention to `Cdecl`
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / TextLabelEvent.cs
1 /*
2  * Copyright(c) 2021 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.BaseComponents
23 {
24     /// <summary>
25     /// A control which renders a short text string.<br />
26     /// Text labels are lightweight, non-editable, and do not respond to the user input.<br />
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     public partial class TextLabel
30     {
31
32         private EventHandler<AnchorClickedEventArgs> textLabelAnchorClickedEventHandler;
33         private AnchorClickedCallbackDelegate textLabelAnchorClickedCallbackDelegate;
34
35         private EventHandler textLabelTextFitChangedEventHandler;
36         private TextFitChangedCallbackDelegate textLabelTextFitChangedCallbackDelegate;
37
38         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
39         private delegate void AnchorClickedCallbackDelegate(IntPtr textLabel, IntPtr href, uint hrefLength);
40
41         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
42         private delegate void TextFitChangedCallbackDelegate(IntPtr textLabel);
43
44         /// <summary>
45         /// The AnchorClicked signal is emitted when the anchor is clicked.
46         /// </summary>
47         /// <since_tizen> 9 </since_tizen>
48         public event EventHandler<AnchorClickedEventArgs> AnchorClicked
49         {
50             add
51             {
52                 if (textLabelAnchorClickedEventHandler == null)
53                 {
54                     textLabelAnchorClickedCallbackDelegate = (OnAnchorClicked);
55                     AnchorClickedSignal().Connect(textLabelAnchorClickedCallbackDelegate);
56                 }
57                 textLabelAnchorClickedEventHandler += value;
58             }
59             remove
60             {
61                 textLabelAnchorClickedEventHandler -= value;
62                 if (textLabelAnchorClickedEventHandler == null && AnchorClickedSignal().Empty() == false)
63                 {
64                     AnchorClickedSignal().Disconnect(textLabelAnchorClickedCallbackDelegate);
65                 }
66             }
67         }
68
69         internal TextLabelSignal AnchorClickedSignal()
70         {
71             TextLabelSignal ret = new TextLabelSignal(Interop.TextLabel.AnchorClickedSignal(SwigCPtr), false);
72             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
73             return ret;
74         }
75
76         private void OnAnchorClicked(IntPtr textLabel, IntPtr href, uint hrefLength)
77         {
78             // Note: hrefLength is useful for get the length of a const char* (href) in dali-toolkit.
79             // But NUI can get the length of string (href), so hrefLength is not necessary in NUI.
80             AnchorClickedEventArgs e = new AnchorClickedEventArgs();
81
82             // Populate all members of "e" (AnchorClickedEventArgs) with real data
83             e.Href = Marshal.PtrToStringAnsi(href);
84             //here we send all data to user event handlers
85             textLabelAnchorClickedEventHandler?.Invoke(this, e);
86         }
87
88         /// <summary>
89         /// An event for the TextFitChanged signal which can be used to subscribe or unsubscribe the event handler
90         /// provided by the user. The TextFitChanged signal is emitted when the text fit properties changes.<br />
91         /// </summary>
92         [EditorBrowsable(EditorBrowsableState.Never)]
93         public event EventHandler TextFitChanged
94         {
95             add
96             {
97                 if (textLabelTextFitChangedEventHandler == null)
98                 {
99                     textLabelTextFitChangedCallbackDelegate = (OnTextFitChanged);
100                     TextFitChangedSignal().Connect(textLabelTextFitChangedCallbackDelegate);
101                 }
102                 textLabelTextFitChangedEventHandler += value;
103             }
104             remove
105             {
106                 textLabelTextFitChangedEventHandler -= value;
107                 if (textLabelTextFitChangedEventHandler == null && TextFitChangedSignal().Empty() == false)
108                 {
109                     TextFitChangedSignal().Disconnect(textLabelTextFitChangedCallbackDelegate);
110                 }
111             }
112         }
113
114         internal TextLabelSignal TextFitChangedSignal()
115         {
116             TextLabelSignal ret = new TextLabelSignal(Interop.TextLabel.TextFitChangedSignal(SwigCPtr), false);
117             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
118             return ret;
119         }
120
121         private void OnTextFitChanged(IntPtr textLabel)
122         {
123             // no data to be sent to the user, as in NUI there is no event provide old values.
124             textLabelTextFitChangedEventHandler?.Invoke(this, EventArgs.Empty);
125         }
126     }
127 }