Merge "Enhance Entry widget" into tizen
[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 the widget to be focused or not.
225         /// </summary>
226         /// <param name="isFocus">Weather be focused</param>
227         public void SetFocus(bool isFocus)
228         {
229             Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
230         }
231
232         /// <summary>
233         /// Sets the ability for a widget to be focused.
234         /// </summary>
235         /// <param name="isAllowFocus">True if the object can be focused, false if not(and on errors)</param>
236         public void AllowFocus(bool isAllowFocus)
237         {
238             Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
239         }
240
241         /// <summary>
242         /// Gives focus to next widget in widget tree.
243         /// </summary>
244         /// <param name="direction">Direction to move the focus</param>
245         public void FocusNext(FocusDirection direction)
246         {
247             Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
248         }
249
250         /// <summary>
251         /// Set next widget with specific focus direction.
252         /// </summary>
253         /// <param name="next">Focus next widget</param>
254         /// <param name="direction">Focus direction</param>
255         public void SetNextFocusObject(EvasObject next, FocusDirection direction)
256         {
257             Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
258         }
259
260         /// <summary>
261         /// Sets content to particular part of the widget, and the preserve old content will not be unset.
262         /// </summary>
263         /// <param name="part">The name of particular part</param>
264         /// <param name="content">The content</param>
265         /// <seealso cref="SetPartContent(string, EvasObject, bool)"/>
266         public virtual bool SetPartContent(string part, EvasObject content)
267         {
268             return SetPartContent(part, content, false);
269         }
270
271         /// <summary>
272         /// Sets content to particular part of the widget.
273         /// </summary>
274         /// <param name="part">The name of particular part</param>
275         /// <param name="content">The content</param>
276         /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
277         /// <seealso cref="SetPartContent(string, EvasObject)"/>
278         public virtual bool SetPartContent(string part, EvasObject content, bool preserveOldContent)
279         {
280             if (preserveOldContent)
281             {
282                 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
283             }
284             Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
285             UpdatePartContents(content, part);
286             return true;
287         }
288
289         /// <summary>
290         /// Sets content to the widget, and the preserve old content will not be unset.
291         /// </summary>
292         /// <param name="content">The content</param>
293         /// <seealso cref="SetContent(EvasObject, bool)"/>
294         public void SetContent(EvasObject content)
295         {
296             SetContent(content, false);
297         }
298
299         /// <summary>
300         /// Sets content the widget.
301         /// </summary>
302         /// <param name="content">The content</param>
303         /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
304         /// <seealso cref="SetContent(EvasObject)"/>
305         public void SetContent(EvasObject content, bool preserveOldContent)
306         {
307             if (preserveOldContent)
308             {
309                 Interop.Elementary.elm_object_content_unset(RealHandle);
310             }
311
312             Interop.Elementary.elm_object_content_set(RealHandle, content);
313             UpdatePartContents(content);
314         }
315
316         /// <summary>
317         /// Sets text to particular part of the widget.
318         /// </summary>
319         /// <param name="part">The name of particular part</param>
320         /// <param name="text">The text</param>
321         public virtual bool SetPartText(string part, string text)
322         {
323             Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
324             return true;
325         }
326
327         /// <summary>
328         /// Gets text of a particular part of the widget.
329         /// </summary>
330         /// <param name="part">The name of particular part</param>
331         /// <returns>Text of the particular part of the widget</returns>
332         public virtual string GetPartText(string part)
333         {
334             return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
335         }
336
337         /// <summary>
338         /// Sets color of a particular part of the widget.
339         /// </summary>
340         /// <param name="part">The name of particular part</param>
341         /// <param name="color">The color be set to widget</param>
342         /// <remarks>This method is a virtual method, it could be override by special child class</remarks>
343         public virtual void SetPartColor(string part, Color color)
344         {
345             Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
346                                                                               color.G * color.A / 255,
347                                                                               color.B * color.A / 255,
348                                                                               color.A);
349         }
350
351         /// <summary>
352         /// Gets color of the particular part of the widget.
353         /// </summary>
354         /// <param name="part">The name of particular part</param>
355         /// <returns>The color of the particular part</returns>
356         /// <remarks>This method is a virtual method, it could be override by special child class</remarks>
357         public virtual Color GetPartColor(string part)
358         {
359             int r, g, b, a;
360             Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
361             return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
362         }
363
364         /// <summary>
365         /// Sets opacity of the particular part of the widget.
366         /// </summary>
367         /// <param name="part">The name of particular part</param>
368         /// <param name="opacity">The opacity of the particular part</param>
369         public void SetPartOpacity(string part, int opacity)
370         {
371             Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
372         }
373
374         /// <summary>
375         /// Gets opacity of the particular part of the widget.
376         /// </summary>
377         /// <param name="part">The name of particular part</param>
378         /// <returns>Opacity value of the particular part</returns>
379         public int GetPartOpacity(string part)
380         {
381             int r, g, b, a;
382             Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
383             return a;
384         }
385
386         internal IntPtr GetPartContent(string part)
387         {
388             return Interop.Elementary.elm_object_part_content_get(RealHandle, part);
389         }
390     }
391 }