[NUI] TCSACR-226 code change (#1032)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / EdjeObject.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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 using System;
18 using System.Collections;
19 using System.Collections.Generic;
20
21 namespace ElmSharp
22 {
23     /// <summary>
24     /// The EdjeObject is a class that the evas object exists in.
25     /// </summary>
26     /// <since_tizen> preview </since_tizen>
27     public class EdjeObject
28     {
29         IntPtr _edjeHandle;
30         Dictionary<SignalData, Interop.Elementary.Edje_Signal_Cb> _signalDatas = new Dictionary<SignalData, Interop.Elementary.Edje_Signal_Cb>();
31
32         internal EdjeObject(IntPtr handle)
33         {
34             _edjeHandle = handle;
35         }
36
37         /// <summary>
38         /// Checks whether an edje part exists in a given edje object's group definition.
39         /// This function returns if a given part exists in the edje group bound to object obj
40         /// </summary>
41         /// <remarks>This call is useful, for example, when one could expect a given GUI element, depending on the theme applied to object.</remarks>
42         /// <param name="part">The part's name to check for existence in object's group.</param>
43         /// <returns>TRUE, if the edje part exists in the object's group, otherwise FALSE.</returns>
44         /// <since_tizen> preview </since_tizen>
45         public EdjeTextPartObject this[string part]
46         {
47             get
48             {
49                 if (Interop.Elementary.edje_object_part_exists(_edjeHandle, part))
50                 {
51                     return new EdjeTextPartObject(_edjeHandle, part);
52                 }
53                 return null;
54             }
55         }
56
57         /// <summary>
58         /// Sends or emits an edje signal to a given edje object.
59         /// </summary>
60         /// <param name="emission">The signal's "emission" string</param>
61         /// <param name="source">The signal's "source" string</param>
62         /// <since_tizen> preview </since_tizen>
63         public void EmitSignal(string emission, string source)
64         {
65             Interop.Elementary.edje_object_signal_emit(_edjeHandle, emission, source);
66         }
67
68         /// <summary>
69         /// Deletes the object Color class.
70         /// This function deletes any values at the object level for the specified object and Color class.
71         /// </summary>
72         /// <remarks>Deleting the Color class defined in the theme file.</remarks>
73         /// <param name="part">The Color class to be deleted.</param>
74         /// <since_tizen> preview </since_tizen>
75         public void DeleteColorClass(string part)
76         {
77             Interop.Elementary.edje_object_color_class_del(_edjeHandle, part);
78         }
79
80         /// <summary>
81         /// Sets the object Color class.
82         /// </summary>
83         /// <param name="colorClass">The Color class name.</param>
84         /// <param name="red">The object red value.</param>
85         /// <param name="green">The object green value.</param>
86         /// <param name="blue">The object blue value.</param>
87         /// <param name="alpha">The object alpha value.</param>
88         /// <param name="outlineRed">The outline red value.</param>
89         /// <param name="outlineGreen">The outline green value.</param>
90         /// <param name="outlineBlue">The outline blue value.</param>
91         /// <param name="outlineAlpha">The outline alpha value.</param>
92         /// <param name="shadowRed">The shadow red value.</param>
93         /// <param name="shadowGreen">The shadow green value.</param>
94         /// <param name="shadowBlue">The shadow blue value.</param>
95         /// <param name="shadowAlpha">The shadow alpha value.</param>
96         /// <returns>True if succeeds, otherwise False.</returns>
97         /// <since_tizen> preview </since_tizen>
98         public bool SetColorClass(string colorClass, int red, int green, int blue, int alpha, int outlineRed, int outlineGreen, int outlineBlue, int outlineAlpha,
99             int shadowRed, int shadowGreen, int shadowBlue, int shadowAlpha)
100         {
101             return Interop.Elementary.edje_object_color_class_set(_edjeHandle, colorClass, red, green, blue, alpha, outlineRed, outlineGreen, outlineBlue, outlineAlpha,
102                 shadowRed, shadowGreen, shadowBlue, shadowAlpha);
103         }
104
105         /// <summary>
106         /// Gets the object Color class.
107         /// </summary>
108         /// <param name="colorClass">The Color class name.</param>
109         /// <param name="red">The object red value.</param>
110         /// <param name="green">The object green value.</param>
111         /// <param name="blue">The object blue value.</param>
112         /// <param name="alpha">The object alpha value.</param>
113         /// <param name="outlineRed">The outline red value.</param>
114         /// <param name="outlineGreen">The outline green value.</param>
115         /// <param name="outlineBlue">The outline blue value.</param>
116         /// <param name="outlineAlpha">The outline alpha value.</param>
117         /// <param name="shadowRed">The shadow red value.</param>
118         /// <param name="shadowGreen">The shadow green value.</param>
119         /// <param name="shadowBlue">The shadow blue value.</param>
120         /// <param name="shadowAlpha">The shadow alpha value.</param>
121         /// <returns>True if succeeds, otherwise False.</returns>
122         /// <since_tizen> preview </since_tizen>
123         public bool GetColorClass(string colorClass, out int red, out int green, out int blue, out int alpha, out int outlineRed, out int outlineGreen, out int outlineBlue, out int outlineAlpha,
124             out int shadowRed, out int shadowGreen, out int shadowBlue, out int shadowAlpha)
125         {
126             return Interop.Elementary.edje_object_color_class_get(_edjeHandle, colorClass, out red, out green, out blue, out alpha, out outlineRed, out outlineGreen, out outlineBlue, out outlineAlpha,
127                 out shadowRed, out shadowGreen, out shadowBlue, out shadowAlpha);
128         }
129
130         /// <summary>
131         /// Sets the Edje text class.
132         /// </summary>
133         /// <param name="textClass">The text class name.</param>
134         /// <param name="font">Font name.</param>
135         /// <param name="fontSize">Font size.</param>
136         /// <returns>True if succeeds, otherwise False.</returns>
137         /// <since_tizen> preview </since_tizen>
138         public bool SetTextClass(string textClass, string font, int fontSize)
139         {
140             return Interop.Elementary.edje_object_text_class_set(_edjeHandle, textClass, font, fontSize);
141         }
142
143         /// <summary>
144         /// Gets the Edje text class.
145         /// </summary>
146         /// <param name="textClass">The text class name.</param>
147         /// <param name="font">Font name.</param>
148         /// <param name="fontSize">Font size.</param>
149         /// <returns>True if succeeds, otherwise False.</returns>
150         /// <since_tizen> preview </since_tizen>
151         public bool GetTextClass(string textClass, out string font, out int fontSize)
152         {
153             return Interop.Elementary.edje_object_text_class_get(_edjeHandle, textClass, out font, out fontSize);
154         }
155
156         /// <summary>
157         /// Adds Action for an arriving edje signal, emitted by a given Ejde object.
158         /// </summary>
159         /// <param name="emission">The signal's "emission" string.</param>
160         /// <param name="source">The signal's "source" string.</param>
161         /// <param name="action">The action to be executed when the signal is emitted.</param>
162         /// <since_tizen> preview </since_tizen>
163         public void AddSignalAction(string emission, string source, Action<string, string> action)
164         {
165             if (emission != null && source != null && action != null)
166             {
167                 var signalData = new SignalData(emission, source, action);
168                 if (!_signalDatas.ContainsKey(signalData))
169                 {
170                     var signalCallback = new Interop.Elementary.Edje_Signal_Cb((d, o, e, s) =>
171                     {
172                         action(e, s);
173                     });
174
175                     Interop.Elementary.edje_object_signal_callback_add(_edjeHandle, emission, source, signalCallback, IntPtr.Zero);
176                     _signalDatas.Add(signalData, signalCallback);
177                 }
178             }
179         }
180
181         /// <summary>
182         /// Deletes a signal-triggered action from an object.
183         /// </summary>
184         /// <param name="emission">The signal's "emission" string.</param>
185         /// <param name="source">The signal's "source" string.</param>
186         /// <param name="action">The action to be executed when the signal is emitted.</param>
187         /// <since_tizen> preview </since_tizen>
188         public void DeleteSignalAction(string emission, string source, Action<string, string> action)
189         {
190             if (emission != null && source != null && action != null)
191             {
192                 var signalData = new SignalData(emission, source, action);
193
194                 Interop.Elementary.Edje_Signal_Cb signalCallback = null;
195                 _signalDatas.TryGetValue(signalData, out signalCallback);
196
197                 if (signalCallback != null)
198                 {
199                     Interop.Elementary.edje_object_signal_callback_del(_edjeHandle, emission, source, signalCallback);
200                     _signalDatas.Remove(signalData);
201                 }
202             }
203         }
204
205         class SignalData : IEquatable<SignalData>
206         {
207             public string Emission { get; set; }
208             public string Source { get; set; }
209
210             public Action<string, string> Action { get; set; }
211
212             public SignalData(string emission, string source, Action<string, string> action)
213             {
214                 Emission = emission;
215                 Source = source;
216                 Action = action;
217             }
218
219             public override bool Equals(object other)
220             {
221                 return Equals(other as SignalData);
222             }
223
224             public bool Equals(SignalData other) {
225                 if (other == null)
226                 {
227                     return false;
228                 }
229                 return (Emission == other.Emission) && (Source == other.Source) && (Action == other.Action);
230             }
231
232             public override int GetHashCode()
233             {
234                 int hashCode = Emission.GetHashCode();
235                 hashCode ^= Source.GetHashCode();
236                 hashCode ^= Action.GetHashCode();
237                 return hashCode;
238             }
239         }
240     }
241
242     /// <summary>
243     /// The EdjeTextPartObject is a class dealing with parts of type text.
244     /// </summary>
245     /// <since_tizen> preview </since_tizen>
246     public class EdjeTextPartObject
247     {
248         string _part;
249         IntPtr _edjeHandle;
250
251         internal EdjeTextPartObject(IntPtr edjeHandle, string part)
252         {
253             _edjeHandle = edjeHandle;
254             _part = part;
255         }
256
257         /// <summary>
258         /// Gets the name of the EdjeTextPartObject.
259         /// </summary>
260         /// <since_tizen> preview </since_tizen>
261         public string Name { get { return _part; } }
262
263         /// <summary>
264         /// Gets or sets the text for an object part.
265         /// </summary>
266         /// <since_tizen> preview </since_tizen>
267         public string Text
268         {
269             get
270             {
271                 return Interop.Elementary.edje_object_part_text_get(_edjeHandle, _part);
272             }
273             set
274             {
275                 Interop.Elementary.edje_object_part_text_set(_edjeHandle, _part, value);
276             }
277         }
278
279         /// <summary>
280         /// Sets or gets the style of the object part.
281         /// </summary>
282         /// <since_tizen> preview </since_tizen>
283         public string TextStyle
284         {
285             get
286             {
287                 return Interop.Elementary.edje_object_part_text_style_user_peek(_edjeHandle, _part);
288             }
289             set
290             {
291                 if (value == null)
292                 {
293                     Interop.Elementary.edje_object_part_text_style_user_pop(_edjeHandle, _part);
294                 }
295                 else
296                 {
297                     Interop.Elementary.edje_object_part_text_style_user_push(_edjeHandle, _part, value);
298                 }
299             }
300         }
301
302         /// <summary>
303         /// Gets the geometry of a given edje part, in a given edje object's group definition, relative to the object's area.
304         /// </summary>
305         /// <since_tizen> preview </since_tizen>
306         public Rect Geometry
307         {
308             get
309             {
310                 int x, y, w, h;
311                 Interop.Elementary.edje_object_part_geometry_get(_edjeHandle, _part, out x, out y, out w, out h);
312                 return new Rect(x, y, w, h);
313             }
314         }
315
316         /// <summary>
317         /// Gets the native width and height.
318         /// </summary>
319         /// <since_tizen> preview </since_tizen>
320         public Size TextBlockNativeSize
321         {
322             get
323             {
324                 int w;
325                 int h;
326                 IntPtr part = Interop.Elementary.edje_object_part_object_get(_edjeHandle, _part);
327                 Interop.Evas.evas_object_textblock_size_native_get(part, out w, out h);
328                 return new Size(w, h);
329             }
330         }
331
332         /// <summary>
333         /// Gets the formatted width and height.
334         /// </summary>
335         /// <since_tizen> preview </since_tizen>
336         public Size TextBlockFormattedSize
337         {
338             get
339             {
340                 int w;
341                 int h;
342                 IntPtr part = Interop.Elementary.edje_object_part_object_get(_edjeHandle, _part);
343                 Interop.Evas.evas_object_textblock_size_formatted_get(part, out w, out h);
344                 return new Size(w, h);
345             }
346         }
347     }
348 }