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);
91 /// Update the part contents
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__")
97 _partContents[part] = content;
101 /// Focused will be triggered when the widget is focused.
103 public event EventHandler Focused;
106 /// Unfocused will be triggered when the widget is unfocused.
108 public event EventHandler Unfocused;
111 /// Sets or gets the state of the widget, which might be enabled or disabled.
113 public bool IsEnabled
117 return !Interop.Elementary.elm_object_disabled_get(RealHandle);
121 Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
126 /// Sets or gets the style of the widget.
132 return Interop.Elementary.elm_object_style_get(RealHandle);
136 Interop.Elementary.elm_object_style_set(RealHandle, value);
141 /// Gets whether this widget is focused.
143 public bool IsFocused
147 return Interop.Elementary.elm_object_focus_get(RealHandle);
152 /// Gets whether a widget is focusable or not.
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
159 return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
164 /// Sets or gets the text of the widget.
166 /// <remarks>It could be override by special child class</remarks>
167 public virtual string Text
171 return Interop.Elementary.elm_object_part_text_get(RealHandle);
175 Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
180 /// Sets or gets the background color of the widget.
182 /// <remarks>It could be override by special child class</remarks>
183 public virtual Color BackgroundColor
187 if (!_backgroundColor.IsDefault)
189 _backgroundColor = GetPartColor("bg");
191 return _backgroundColor;
197 Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
201 SetPartColor("bg", value);
202 _backgroundColor = value;
208 /// Sets or gets the opacity of the widget.
210 /// <remarks>It could be override by special child class</remarks>
211 public virtual int Opacity
215 if (_opacity != Color.Default.A)
217 _opacity = GetPartOpacity("opacity");
223 SetPartOpacity("opacity", value);
229 /// Sets or gets whether a widget and its children are focusable or not.
231 public bool AllowTreeFocus
235 return Interop.Elementary.elm_object_tree_focus_allow_get(RealHandle);
239 Interop.Elementary.elm_object_tree_focus_allow_set(RealHandle, value);
244 /// Sets or gets the widget's mirrored mode.
246 public bool IsMirroredMode
250 return Interop.Elementary.elm_object_mirrored_get(RealHandle);
254 Interop.Elementary.elm_object_mirrored_set(RealHandle, value);
259 /// Sets or gets the widget's mirrored mode setting.
260 /// When widget set automatic mode(true), it follows the system mirrored mode.
262 public bool IsAutoMirroredMode
266 return Interop.Elementary.elm_object_mirrored_automatic_get(RealHandle);
270 Interop.Elementary.elm_object_mirrored_automatic_set(RealHandle, value);
275 /// Sets the widget to be focused or not.
277 /// <param name="isFocus">Weather be focused</param>
278 public void SetFocus(bool isFocus)
280 Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
284 /// Sets the ability for a widget to be focused.
286 /// <param name="isAllowFocus">True if the object can be focused, false if not(and on errors)</param>
287 public void AllowFocus(bool isAllowFocus)
289 Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
293 /// Gives focus to next widget in widget tree.
295 /// <param name="direction">Direction to move the focus</param>
296 public void FocusNext(FocusDirection direction)
298 Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
302 /// Set next widget with specific focus direction.
304 /// <param name="next">Focus next widget</param>
305 /// <param name="direction">Focus direction</param>
306 public void SetNextFocusObject(EvasObject next, FocusDirection direction)
308 Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
312 /// Sets content to particular part of the widget, and the preserve old content will not be unset.
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)
319 return SetPartContent(part, content, false);
323 /// Sets content to particular part of the widget.
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)
331 if (preserveOldContent)
333 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
335 Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
336 UpdatePartContents(content, part);
341 /// Sets content to the widget, and the preserve old content will not be unset.
343 /// <param name="content">The content</param>
344 /// <seealso cref="SetContent(EvasObject, bool)"/>
345 public void SetContent(EvasObject content)
347 SetContent(content, false);
351 /// Sets content the widget.
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)
358 if (preserveOldContent)
360 Interop.Elementary.elm_object_content_unset(RealHandle);
363 Interop.Elementary.elm_object_content_set(RealHandle, content);
364 UpdatePartContents(content);
368 /// Sets text to particular part of the widget.
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)
374 Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
379 /// Gets text of a particular part of the widget.
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)
385 return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
389 /// Sets color of a particular part of the widget.
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)
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,
403 /// Gets color of the particular part of the widget.
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)
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);
416 /// Sets opacity of the particular part of the widget.
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)
422 Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
426 /// Gets opacity of the particular part of the widget.
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)
433 Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
437 internal IntPtr GetPartContent(string part)
439 return Interop.Elementary.elm_object_part_content_get(RealHandle, part);