2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using System.Collections.Generic;
19 using ElmSharp.Accessible;
24 /// Enumeration for the focus direction.
26 public enum FocusDirection
29 /// Previous direction
60 /// The Widget is abstract class, it is the parent of other widgets.
61 /// Inherits from <see cref="EvasObject"/>.
63 public abstract class Widget : AccessibleObject
65 Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
68 SmartEvent _unfocused;
70 internal Color _backgroundColor = Color.Default;
71 internal int _opacity = Color.Default.A;
78 /// Creates and initializes a new instance of the Widget class.
80 /// <param name="parent">The parent of new Widget instance</param>
81 protected Widget(EvasObject parent) : base(parent)
83 _focused = new SmartEvent(this, "focused");
84 _focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);
86 _unfocused = new SmartEvent(this, "unfocused");
87 _unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
90 protected void UpdatePartContents(EvasObject content, string part = "__default__")
92 _partContents[part] = content;
96 /// Focused will be triggered when the widget is focused.
98 public event EventHandler Focused;
101 /// Unfocused will be triggered when the widget is unfocused.
103 public event EventHandler Unfocused;
106 /// Sets or gets the state of the widget, which might be enabled or disabled.
108 public bool IsEnabled
112 return !Interop.Elementary.elm_object_disabled_get(RealHandle);
116 Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
121 /// Sets or gets the style of the widget.
127 return Interop.Elementary.elm_object_style_get(RealHandle);
131 Interop.Elementary.elm_object_style_set(RealHandle, value);
136 /// Gets whether this widget is focused.
138 public bool IsFocused
142 return Interop.Elementary.elm_object_focus_get(RealHandle);
147 /// Gets whether a widget is focusable or not.
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
154 return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
159 /// Sets or gets the text of the widget.
161 /// <remarks>It could be override by special child class</remarks>
162 public virtual string Text
166 return Interop.Elementary.elm_object_part_text_get(RealHandle);
170 Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
175 /// Sets or gets the background color of the widget.
177 /// <remarks>It could be override by special child class</remarks>
178 public virtual Color BackgroundColor
182 if (!_backgroundColor.IsDefault)
184 _backgroundColor = GetPartColor("bg");
186 return _backgroundColor;
192 Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
196 SetPartColor("bg", value);
197 _backgroundColor = value;
203 /// Sets or gets the opacity of the widget.
205 /// <remarks>It could be override by special child class</remarks>
206 public virtual int Opacity
210 if (_opacity != Color.Default.A)
212 _opacity = GetPartOpacity("opacity");
218 SetPartOpacity("opacity", value);
224 /// Sets the widget to be focused or not.
226 /// <param name="isFocus">Weather be focused</param>
227 public void SetFocus(bool isFocus)
229 Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
233 /// Sets the ability for a widget to be focused.
235 /// <param name="isAllowFocus">True if the object can be focused, false if not(and on errors)</param>
236 public void AllowFocus(bool isAllowFocus)
238 Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
242 /// Gives focus to next widget in widget tree.
244 /// <param name="direction">Direction to move the focus</param>
245 public void FocusNext(FocusDirection direction)
247 Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
251 /// Set next widget with specific focus direction.
253 /// <param name="next">Focus next widget</param>
254 /// <param name="direction">Focus direction</param>
255 public void SetNextFocusObject(EvasObject next, FocusDirection direction)
257 Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
261 /// Sets content to particular part of the widget, and the preserve old content will not be unset.
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)
268 return SetPartContent(part, content, false);
272 /// Sets content to particular part of the widget.
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)
280 if (preserveOldContent)
282 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
284 Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
285 UpdatePartContents(content, part);
290 /// Sets content to the widget, and the preserve old content will not be unset.
292 /// <param name="content">The content</param>
293 /// <seealso cref="SetContent(EvasObject, bool)"/>
294 public void SetContent(EvasObject content)
296 SetContent(content, false);
300 /// Sets content the widget.
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)
307 if (preserveOldContent)
309 Interop.Elementary.elm_object_content_unset(RealHandle);
312 Interop.Elementary.elm_object_content_set(RealHandle, content);
313 UpdatePartContents(content);
317 /// Sets text to particular part of the widget.
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)
323 Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
328 /// Gets text of a particular part of the widget.
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)
334 return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
338 /// Sets color of a particular part of the widget.
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)
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,
352 /// Gets color of the particular part of the widget.
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)
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);
365 /// Sets opacity of the particular part of the widget.
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)
371 Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
375 /// Gets opacity of the particular part of the widget.
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)
382 Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
386 internal IntPtr GetPartContent(string part)
388 return Interop.Elementary.elm_object_part_content_get(RealHandle, part);