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