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