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