[NUI] Add DataSelected in Clipboard
[platform/core/csapi/tizenfx.git] / test / NUIClipboard / Clipboard.cs
1 using System;
2 using Tizen.NUI;
3 using Tizen.NUI.Components;
4 using Tizen.NUI.BaseComponents;
5
6 namespace NUIClipboard
7 {
8     class Program : NUIApplication
9     {
10         const string TAG = "clipboard";
11         const string MIME_TYPE_PLAIN_TEXT = "text/plain;charset=utf-8";
12         const string MIME_TYPE_TEXT_URI = "text/uri-list";
13         const string MIME_TYPE_HTML = "application/xhtml+xml";
14
15         TextField fieldCopy;
16         TextField fieldPaste;
17
18         TextLabel labelType;
19         TextLabel labelData;
20
21         protected override void OnCreate()
22         {
23             base.OnCreate();
24             Initialize();
25         }
26
27         void Initialize()
28         {
29             Window.Instance.WindowSize = new Size(900, 1080);
30             Window.Instance.BackgroundColor = Color.White;
31
32             View mainView = NewView(false);
33             Window.Instance.GetDefaultLayer().Add(mainView);
34
35             TextLabel title = NewTextLabel("Tizen.NUI.Clipboard Test", LayoutParamPolicies.MatchParent);
36             title.HorizontalAlignment = HorizontalAlignment.Center;
37             title.PointSize = 30.0f;
38             mainView.Add(title);
39
40             string descriptionText = "[Description]\n" +
41                                      "* Copy : Copy the text entered in the TextField using the Clipboard's SetData() API.\n" +
42                                      "* This sample uses the 'text/plain;charset=utf-8' MIME type to send and receive data.\n" + 
43                                      "* Paste : Paste the text using the Clipboard's GetData() API.\n" +
44                                      "* Clear : Clear Information.";
45
46             TextLabel description = NewTextLabel(descriptionText, LayoutParamPolicies.MatchParent);
47             description.PointSize = 20.0f;
48             mainView.Add(description);
49
50             View vertView1 = NewView(true);
51             mainView.Add(vertView1);
52
53             fieldCopy = NewTextField("Enter text to copy", LayoutParamPolicies.MatchParent);
54             vertView1.Add(fieldCopy);
55
56             Button buttonCopy = NewButton("Copy");
57             vertView1.Add(buttonCopy);
58             buttonCopy.Clicked += (s, e) =>
59             {
60                 string data = fieldCopy.Text;
61                 Clipboard.Instance.SetData(MIME_TYPE_PLAIN_TEXT, data);
62                 Tizen.Log.Info(TAG, $"SetData type:{MIME_TYPE_PLAIN_TEXT}, data:{data}\n");
63             };
64
65
66             View vertView2 = NewView(true);
67             mainView.Add(vertView2);
68
69             Button buttonPaste = NewButton("Paste");
70             vertView2.Add(buttonPaste);
71             buttonPaste.Clicked += (s, e) =>
72             {
73                 Clipboard.Instance.GetData(MIME_TYPE_PLAIN_TEXT, OnClipboardDataReceived);
74                 Tizen.Log.Info(TAG, $"GetData request type:{MIME_TYPE_PLAIN_TEXT}\n");
75             };
76
77             Button buttonCopyClear = NewButton("Clear");
78             vertView2.Add(buttonCopyClear);
79             buttonCopyClear.Clicked += (s, e) =>
80             {
81                 fieldCopy.Text = "";
82                 labelType.Text = " ";
83                 labelData.Text = " ";
84             };
85
86             View vertView3 = NewView(true);
87             mainView.Add(vertView3);
88
89             TextLabel pastedType = NewTextLabel("MIME type", 300);
90             vertView3.Add(pastedType);
91
92             labelType = NewTextLabel(" ", LayoutParamPolicies.MatchParent);
93             vertView3.Add(labelType);
94
95             View vertView4 = NewView(true);
96             mainView.Add(vertView4);
97
98             TextLabel pastedData = NewTextLabel("Data", 300);
99             vertView4.Add(pastedData);
100
101             labelData = NewTextLabel(" ", LayoutParamPolicies.MatchParent);
102             vertView4.Add(labelData);
103         }
104
105         public void OnClipboardDataReceived(bool success, ClipEvent clipEvent)
106         {
107             if (!success)
108             {
109                 Tizen.Log.Error(TAG, $"Data receive fail");
110                 return;
111             }
112
113             Tizen.Log.Info(TAG, $"OnClipboardDataReceived type:{clipEvent.MimeType}, data:{clipEvent.Data}\n");
114
115             // info update
116             labelType.Text = clipEvent.MimeType;
117             labelData.Text = clipEvent.Data;
118         }
119
120         public View NewView(bool horizontal)
121         {
122             var view = new View()
123             {
124                 Layout = new LinearLayout()
125                 {
126                     LinearOrientation = horizontal ? LinearLayout.Orientation.Horizontal : LinearLayout.Orientation.Vertical,
127                     LinearAlignment = LinearLayout.Alignment.Begin,
128                     CellPadding = new Size2D(10, 20),
129                 },
130                 WidthSpecification = LayoutParamPolicies.MatchParent,
131                 HeightSpecification = LayoutParamPolicies.WrapContent,
132                 BackgroundColor = Color.White,
133             };
134             return view;
135         }
136
137         public TextLabel NewTextLabel(string text, int width)
138         {
139             var label = new TextLabel
140             {
141                 Text = text,
142                 MultiLine = true,
143                 WidthSpecification = width,
144                 HeightSpecification = LayoutParamPolicies.WrapContent,
145                 PointSize = 25.0f,
146                 BackgroundColor = Color.White,
147                 BorderlineWidth = 1.0f,
148             };
149             return label;
150         }
151
152         public TextField NewTextField(string placeholderText, int width)
153         {
154             var field = new TextField
155             {
156                 PlaceholderText = placeholderText,
157                 PlaceholderTextFocused = placeholderText,
158                 WidthSpecification = width,
159                 HeightSpecification = LayoutParamPolicies.WrapContent,
160                 PointSize = 25.0f,
161                 BackgroundColor = Color.White,
162                 BorderlineWidth = 1.0f,
163             };
164             return field;
165         }
166
167         public Button NewButton(string text)
168         {
169             var button = new Button(NewButtonStyle())
170             {
171                 Text = text,
172                 PointSize = 25.0f,
173                 WidthSpecification = 200,
174                 HeightSpecification = LayoutParamPolicies.WrapContent,
175             };
176             return button;
177         }
178
179         public ButtonStyle NewButtonStyle()
180         {
181             var style = new ButtonStyle
182             {
183                 CornerRadius = 0.0f,
184                 BackgroundColor = new Selector<Color>()
185                 {
186                     Normal = new Color(0.25f, 0.75f, 1.0f, 1.0f),
187                     Pressed = new Color(0.25f, 0.75f, 1.0f, 0.3f),
188                 },
189                 Overlay = new ImageViewStyle()
190                 {
191                     BackgroundColor = new Selector<Color>()
192                     {
193                         Pressed = new Color(0, 0, 0, 0.1f),
194                         Other = new Color(1, 1, 1, 0.1f),
195                     },
196                 },
197                 Text = new TextLabelStyle()
198                 {
199                     TextColor = new Color(0.0f, 0.0f, 0.0f, 1.0f),
200                 }
201             };
202             return style;
203         }
204
205         static void Main(string[] args)
206         {
207             var app = new Program();
208             app.Run(args);
209         }
210     }
211 }