[TtsEngine] Fix invalid marshalling (#25)
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Widget.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.Generic;
19 using ElmSharp.Accessible;
20
21 namespace ElmSharp
22 {
23     /// <summary>
24     /// Enumeration for the focus direction.
25     /// </summary>
26     /// <since_tizen> preview </since_tizen>
27     public enum FocusDirection
28     {
29         /// <summary>
30         /// Previous direction.
31         /// </summary>
32         Previous,
33
34         /// <summary>
35         /// Next direction.
36         /// </summary>
37         Next,
38
39         /// <summary>
40         /// Up direction.
41         /// </summary>
42         Up,
43
44         /// <summary>
45         /// Down direction.
46         /// </summary>
47         Down,
48
49         /// <summary>
50         /// Right direction.
51         /// </summary>
52         Right,
53
54         /// <summary>
55         /// Left direction.
56         /// </summary>
57         Left
58     }
59
60     /// <summary>
61     /// The Widget is an abstract class and the parent of other widgets.
62     /// Inherits from <see cref="EvasObject"/>.
63     /// </summary>
64     /// <since_tizen> preview </since_tizen>
65     public abstract class Widget : AccessibleObject
66     {
67         Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
68
69         SmartEvent _focused;
70         SmartEvent _unfocused;
71
72         internal Color _backgroundColor = Color.Default;
73         internal int _opacity = Color.Default.A;
74
75         /// <summary>
76         /// Creates and initializes a new instance of the Widget class.
77         /// </summary>
78         /// <since_tizen> preview </since_tizen>
79         protected Widget()
80         {
81         }
82
83         /// <summary>
84         /// Creates and initializes a new instance of the Widget class.
85         /// </summary>
86         /// <param name="parent">The parent of the new Widget instance.</param>
87         /// <since_tizen> preview </since_tizen>
88         protected Widget(EvasObject parent) : base(parent)
89         {
90         }
91
92         /// <summary>
93         /// Updates the part contents.
94         /// </summary>
95         /// <param name="content">The content which is put into the part.</param>
96         /// <param name="part">The updated part.</param>
97         /// <since_tizen> preview </since_tizen>
98         protected void UpdatePartContents(EvasObject content, string part = "__default__")
99         {
100             _partContents[part] = content;
101         }
102
103         /// <summary>
104         /// Focused will be triggered when the widget is focused.
105         /// </summary>
106         /// <since_tizen> preview </since_tizen>
107         public event EventHandler Focused;
108
109         /// <summary>
110         /// Unfocused will be triggered when the widget is unfocused.
111         /// </summary>
112         /// <since_tizen> preview </since_tizen>
113         public event EventHandler Unfocused;
114
115         /// <summary>
116         /// Sets or gets the state of the widget, which might be enabled or disabled.
117         /// </summary>
118         /// <since_tizen> preview </since_tizen>
119         public virtual bool IsEnabled
120         {
121             get
122             {
123                 return !Interop.Elementary.elm_object_disabled_get(RealHandle);
124             }
125             set
126             {
127                 Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
128             }
129         }
130
131         /// <summary>
132         /// Sets or gets the style of the widget.
133         /// </summary>
134         /// <since_tizen> preview </since_tizen>
135         public string Style
136         {
137             get
138             {
139                 return Interop.Elementary.elm_object_style_get(RealHandle);
140             }
141             set
142             {
143                 Interop.Elementary.elm_object_style_set(RealHandle, value);
144             }
145         }
146
147         /// <summary>
148         /// Gets whether this widget is focused.
149         /// </summary>
150         /// <since_tizen> preview </since_tizen>
151         public bool IsFocused
152         {
153             get
154             {
155                 return Interop.Elementary.elm_object_focus_get(RealHandle);
156             }
157         }
158
159         /// <summary>
160         /// Gets whether a widget is focusable or not.
161         /// </summary>
162         /// <remarks>Widgets which are meant to be interacted with by input events, are created able to be focused by default.</remarks>
163         /// <since_tizen> preview </since_tizen>
164         public bool IsFocusAllowed
165         {
166             get
167             {
168                 return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
169             }
170         }
171
172         /// <summary>
173         /// Sets or gets the text of the widget.
174         /// </summary>
175         /// <remarks>It could be overridden by special child class.</remarks>
176         /// <since_tizen> preview </since_tizen>
177         public virtual string Text
178         {
179             get
180             {
181                 return Interop.Elementary.elm_object_part_text_get(RealHandle);
182             }
183             set
184             {
185                 Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
186             }
187         }
188
189         /// <summary>
190         /// Sets or gets the background color of the widget.
191         /// </summary>
192         /// <remarks>It could be overridden by special child class.</remarks>
193         /// <since_tizen> preview </since_tizen>
194         public virtual Color BackgroundColor
195         {
196             get
197             {
198                 if (!_backgroundColor.IsDefault)
199                 {
200                     _backgroundColor = GetPartColor("bg");
201                 }
202                 return _backgroundColor;
203             }
204             set
205             {
206                 if (value.IsDefault)
207                 {
208                     Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
209                 }
210                 else
211                 {
212                     SetPartColor("bg", value);
213                     _backgroundColor = value;
214                 }
215             }
216         }
217
218         /// <summary>
219         /// Sets or gets the opacity of the widget.
220         /// </summary>
221         /// <remarks>It could be overridden by special child class.</remarks>
222         /// <since_tizen> preview </since_tizen>
223         public virtual int Opacity
224         {
225             get
226             {
227                 if (_opacity != Color.Default.A)
228                 {
229                     _opacity = GetPartOpacity("opacity");
230                 }
231                 return _opacity;
232             }
233             set
234             {
235                 SetPartOpacity("opacity", value);
236                 _opacity = value;
237             }
238         }
239
240         /// <summary>
241         /// Sets or gets whether a widget and its children are focusable or not.
242         /// </summary>
243         /// <since_tizen> preview </since_tizen>
244         public bool AllowTreeFocus
245         {
246             get
247             {
248                 return Interop.Elementary.elm_object_tree_focus_allow_get(RealHandle);
249             }
250             set
251             {
252                 Interop.Elementary.elm_object_tree_focus_allow_set(RealHandle, value);
253             }
254         }
255
256         /// <summary>
257         /// Sets or gets the widget's mirrored mode.
258         /// </summary>
259         /// <since_tizen> preview </since_tizen>
260         public bool IsMirroredMode
261         {
262             get
263             {
264                 return Interop.Elementary.elm_object_mirrored_get(RealHandle);
265             }
266             set
267             {
268                 Interop.Elementary.elm_object_mirrored_set(RealHandle, value);
269             }
270         }
271
272         /// <summary>
273         /// Sets or gets the widget's mirrored mode setting.
274         /// When widget is set to automatic mode(true), it follows the system mirrored mode.
275         /// </summary>
276         /// <since_tizen> preview </since_tizen>
277         public bool IsAutoMirroredMode
278         {
279             get
280             {
281                 return Interop.Elementary.elm_object_mirrored_automatic_get(RealHandle);
282             }
283             set
284             {
285                 Interop.Elementary.elm_object_mirrored_automatic_set(RealHandle, value);
286             }
287         }
288
289         /// <summary>
290         /// Sets the widget to be focused or not.
291         /// </summary>
292         /// <param name="isFocus">Whether be focused.</param>
293         /// <since_tizen> preview </since_tizen>
294         public void SetFocus(bool isFocus)
295         {
296             Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
297         }
298
299         /// <summary>
300         /// Sets the ability for a widget to be focused.
301         /// </summary>
302         /// <param name="isAllowFocus">true if the object can be focused, false if not(and on errors).</param>
303         /// <since_tizen> preview </since_tizen>
304         public void AllowFocus(bool isAllowFocus)
305         {
306             Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
307         }
308
309         /// <summary>
310         /// Gives focus to the next widget in the widget tree.
311         /// </summary>
312         /// <param name="direction">Direction to move the focus.</param>
313         /// <since_tizen> preview </since_tizen>
314         public void FocusNext(FocusDirection direction)
315         {
316             Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
317         }
318
319         /// <summary>
320         /// Sets the next widget with specific focus direction.
321         /// </summary>
322         /// <param name="next">Focus next widget.</param>
323         /// <param name="direction">Focus direction.</param>
324         /// <since_tizen> preview </since_tizen>
325         public void SetNextFocusObject(EvasObject next, FocusDirection direction)
326         {
327             Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
328         }
329
330         /// <summary>
331         /// Sets content to the particular part of the widget, and the preserve old content will not be unset.
332         /// </summary>
333         /// <param name="part">The name of the particular part.</param>
334         /// <param name="content">The content.</param>
335         /// <seealso cref="SetPartContent(string, EvasObject, bool)"/>
336         /// <since_tizen> preview </since_tizen>
337         public virtual bool SetPartContent(string part, EvasObject content)
338         {
339             return SetPartContent(part, content, false);
340         }
341
342         /// <summary>
343         /// Sets content to the particular part of the widget.
344         /// </summary>
345         /// <param name="part">The name of the particular part.</param>
346         /// <param name="content">The content.</param>
347         /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
348         /// <seealso cref="SetPartContent(string, EvasObject)"/>
349         /// <since_tizen> preview </since_tizen>
350         public virtual bool SetPartContent(string part, EvasObject content, bool preserveOldContent)
351         {
352             if (preserveOldContent)
353             {
354                 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
355             }
356             Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
357             UpdatePartContents(content, part);
358             return true;
359         }
360
361         /// <summary>
362         /// Sets content to the widget, and the preserve old content will not be unset.
363         /// </summary>
364         /// <param name="content">The content.</param>
365         /// <seealso cref="SetContent(EvasObject, bool)"/>
366         /// <since_tizen> preview </since_tizen>
367         public void SetContent(EvasObject content)
368         {
369             SetContent(content, false);
370         }
371
372         /// <summary>
373         /// Sets content to the widget.
374         /// </summary>
375         /// <param name="content">The content.</param>
376         /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
377         /// <seealso cref="SetContent(EvasObject)"/>
378         /// <since_tizen> preview </since_tizen>
379         public void SetContent(EvasObject content, bool preserveOldContent)
380         {
381             if (preserveOldContent)
382             {
383                 Interop.Elementary.elm_object_content_unset(RealHandle);
384             }
385
386             Interop.Elementary.elm_object_content_set(RealHandle, content);
387             UpdatePartContents(content);
388         }
389
390         /// <summary>
391         /// Sets text to the particular part of the widget.
392         /// </summary>
393         /// <param name="part">The name of the particular part.</param>
394         /// <param name="text">The text.</param>
395         /// <since_tizen> preview </since_tizen>
396         public virtual bool SetPartText(string part, string text)
397         {
398             Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
399             return true;
400         }
401
402         /// <summary>
403         /// Gets text of a particular part of the widget.
404         /// </summary>
405         /// <param name="part">The name of the particular part.</param>
406         /// <returns>Text of the particular part of the widget.</returns>
407         /// <since_tizen> preview </since_tizen>
408         public virtual string GetPartText(string part)
409         {
410             return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
411         }
412
413         /// <summary>
414         /// Sets color of a particular part of the widget.
415         /// </summary>
416         /// <param name="part">The name of the particular part.</param>
417         /// <param name="color">The color to be set to the widget.</param>
418         /// <remarks>This method is a virtual method, it could be overridden by special child class.</remarks>
419         /// <since_tizen> preview </since_tizen>
420         public virtual void SetPartColor(string part, Color color)
421         {
422             Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
423                                                                               color.G * color.A / 255,
424                                                                               color.B * color.A / 255,
425                                                                               color.A);
426         }
427
428         /// <summary>
429         /// Gets color of the particular part of the widget.
430         /// </summary>
431         /// <param name="part">The name of the particular part.</param>
432         /// <returns>The color of the particular part.</returns>
433         /// <remarks>This method is a virtual method, it could be overridden by special child class.</remarks>
434         /// <since_tizen> preview </since_tizen>
435         public virtual Color GetPartColor(string part)
436         {
437             int r, g, b, a;
438             Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
439             return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
440         }
441
442         /// <summary>
443         /// Sets opacity of the particular part of the widget.
444         /// </summary>
445         /// <param name="part">The name of the particular part.</param>
446         /// <param name="opacity">The opacity of the particular part.</param>
447         /// <since_tizen> preview </since_tizen>
448         public void SetPartOpacity(string part, int opacity)
449         {
450             Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
451         }
452
453         /// <summary>
454         /// Gets opacity of the particular part of the widget.
455         /// </summary>
456         /// <param name="part">The name of the particular part.</param>
457         /// <returns>Opacity value of the particular part.</returns>
458         /// <since_tizen> preview </since_tizen>
459         public int GetPartOpacity(string part)
460         {
461             int r, g, b, a;
462             Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
463             return a;
464         }
465
466         /// <summary>
467         /// Sends a signal to the edje object of the widget.
468         /// </summary>
469         /// <param name="emission">The signal's name.</param>
470         /// <param name="source">The signal's source.</param>
471         /// <since_tizen> preview </since_tizen>
472         public void SignalEmit(string emission, string source)
473         {
474             Interop.Elementary.elm_object_signal_emit(Handle, emission, source);
475         }
476
477         /// <summary>
478         /// The callback of the Realized event.
479         /// </summary>
480         /// <since_tizen> preview </since_tizen>
481         protected override void OnRealized()
482         {
483             base.OnRealized();
484             _focused = new SmartEvent(this, "focused");
485             _focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);
486
487             _unfocused = new SmartEvent(this, "unfocused");
488             _unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
489         }
490
491         internal IntPtr GetPartContent(string part)
492         {
493             return Interop.Elementary.elm_object_part_content_get(RealHandle, part);
494         }
495     }
496 }