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