[NUI] Add CursorPositionChanged Event (#3400)
[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         private EventHandler<AnchorClickedEventArgs> textLabelAnchorClickedEventHandler;
32         private AnchorClickedCallbackDelegate textLabelAnchorClickedCallbackDelegate;
33
34         [UnmanagedFunctionPointer(CallingConvention.StdCall)]
35         private delegate void AnchorClickedCallbackDelegate(IntPtr textLabel, IntPtr href, uint hrefLength);
36
37         /// <summary>
38         /// The AnchorClicked signal is emitted when the anchor is clicked.
39         /// </summary>
40         /// <since_tizen> 9 </since_tizen>
41         public event EventHandler<AnchorClickedEventArgs> AnchorClicked
42         {
43             add
44             {
45                 if (textLabelAnchorClickedEventHandler == null)
46                 {
47                     textLabelAnchorClickedCallbackDelegate = (OnAnchorClicked);
48                     AnchorClickedSignal().Connect(textLabelAnchorClickedCallbackDelegate);
49                 }
50                 textLabelAnchorClickedEventHandler += value;
51             }
52             remove
53             {
54                 textLabelAnchorClickedEventHandler -= value;
55                 if (textLabelAnchorClickedEventHandler == null && AnchorClickedSignal().Empty() == false)
56                 {
57                     AnchorClickedSignal().Disconnect(textLabelAnchorClickedCallbackDelegate);
58                 }
59             }
60         }
61
62         internal TextLabelSignal AnchorClickedSignal()
63         {
64             TextLabelSignal ret = new TextLabelSignal(Interop.TextLabel.AnchorClickedSignal(SwigCPtr), false);
65             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
66             return ret;
67         }
68
69         private void OnAnchorClicked(IntPtr textLabel, IntPtr href, uint hrefLength)
70         {
71             // Note: hrefLength is useful for get the length of a const char* (href) in dali-toolkit.
72             // But NUI can get the length of string (href), so hrefLength is not necessary in NUI.
73             AnchorClickedEventArgs e = new AnchorClickedEventArgs();
74
75             // Populate all members of "e" (AnchorClickedEventArgs) with real data
76             e.Href = Marshal.PtrToStringAnsi(href);
77             //here we send all data to user event handlers
78             textLabelAnchorClickedEventHandler?.Invoke(this, e);
79         }
80     }
81 }