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;
74 /// Creates and initializes a new instance of Widget class.
81 /// Creates and initializes a new instance of the Widget class.
83 /// <param name="parent">The parent of new Widget instance</param>
84 protected Widget(EvasObject parent) : base(parent)
86 _focused = new SmartEvent(this, "focused");
87 _focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);
89 _unfocused = new SmartEvent(this, "unfocused");
90 _unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
94 /// Update the part contents
96 /// <param name="content">The content which put to the part</param>
97 /// <param name="part">The updated part</param>
98 protected void UpdatePartContents(EvasObject content, string part = "__default__")
100 _partContents[part] = content;
104 /// Focused will be triggered when the widget is focused.
106 public event EventHandler Focused;
109 /// Unfocused will be triggered when the widget is unfocused.
111 public event EventHandler Unfocused;
114 /// Sets or gets the state of the widget, which might be enabled or disabled.
116 public bool IsEnabled
120 return !Interop.Elementary.elm_object_disabled_get(RealHandle);
124 Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
129 /// Sets or gets the style of the widget.
135 return Interop.Elementary.elm_object_style_get(RealHandle);
139 Interop.Elementary.elm_object_style_set(RealHandle, value);
144 /// Gets whether this widget is focused.
146 public bool IsFocused
150 return Interop.Elementary.elm_object_focus_get(RealHandle);
155 /// Gets whether a widget is focusable or not.
157 /// <remarks>Widgets which are meant to be interacted with by input events are created able to be focused, by default</remarks>
158 public bool IsFocusAllowed
162 return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
167 /// Sets or gets the text of the widget.
169 /// <remarks>It could be override by special child class</remarks>
170 public virtual string Text
174 return Interop.Elementary.elm_object_part_text_get(RealHandle);
178 Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
183 /// Sets or gets the background color of the widget.
185 /// <remarks>It could be override by special child class</remarks>
186 public virtual Color BackgroundColor
190 if (!_backgroundColor.IsDefault)
192 _backgroundColor = GetPartColor("bg");
194 return _backgroundColor;
200 Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
204 SetPartColor("bg", value);
205 _backgroundColor = value;
211 /// Sets or gets the opacity of the widget.
213 /// <remarks>It could be override by special child class</remarks>
214 public virtual int Opacity
218 if (_opacity != Color.Default.A)
220 _opacity = GetPartOpacity("opacity");
226 SetPartOpacity("opacity", value);
232 /// Sets or gets whether a widget and its children are focusable or not.
234 public bool AllowTreeFocus
238 return Interop.Elementary.elm_object_tree_focus_allow_get(RealHandle);
242 Interop.Elementary.elm_object_tree_focus_allow_set(RealHandle, value);
247 /// Sets or gets the widget's mirrored mode.
249 public bool IsMirroredMode
253 return Interop.Elementary.elm_object_mirrored_get(RealHandle);
257 Interop.Elementary.elm_object_mirrored_set(RealHandle, value);
262 /// Sets or gets the widget's mirrored mode setting.
263 /// When widget set automatic mode(true), it follows the system mirrored mode.
265 public bool IsAutoMirroredMode
269 return Interop.Elementary.elm_object_mirrored_automatic_get(RealHandle);
273 Interop.Elementary.elm_object_mirrored_automatic_set(RealHandle, value);
278 /// Sets the widget to be focused or not.
280 /// <param name="isFocus">Weather be focused</param>
281 public void SetFocus(bool isFocus)
283 Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
287 /// Sets the ability for a widget to be focused.
289 /// <param name="isAllowFocus">True if the object can be focused, false if not(and on errors)</param>
290 public void AllowFocus(bool isAllowFocus)
292 Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
296 /// Gives focus to next widget in widget tree.
298 /// <param name="direction">Direction to move the focus</param>
299 public void FocusNext(FocusDirection direction)
301 Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
305 /// Set next widget with specific focus direction.
307 /// <param name="next">Focus next widget</param>
308 /// <param name="direction">Focus direction</param>
309 public void SetNextFocusObject(EvasObject next, FocusDirection direction)
311 Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
315 /// Sets content to particular part of the widget, and the preserve old content will not be unset.
317 /// <param name="part">The name of particular part</param>
318 /// <param name="content">The content</param>
319 /// <seealso cref="SetPartContent(string, EvasObject, bool)"/>
320 public virtual bool SetPartContent(string part, EvasObject content)
322 return SetPartContent(part, content, false);
326 /// Sets content to particular part of the widget.
328 /// <param name="part">The name of particular part</param>
329 /// <param name="content">The content</param>
330 /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
331 /// <seealso cref="SetPartContent(string, EvasObject)"/>
332 public virtual bool SetPartContent(string part, EvasObject content, bool preserveOldContent)
334 if (preserveOldContent)
336 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
338 Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
339 UpdatePartContents(content, part);
344 /// Sets content to the widget, and the preserve old content will not be unset.
346 /// <param name="content">The content</param>
347 /// <seealso cref="SetContent(EvasObject, bool)"/>
348 public void SetContent(EvasObject content)
350 SetContent(content, false);
354 /// Sets content the widget.
356 /// <param name="content">The content</param>
357 /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
358 /// <seealso cref="SetContent(EvasObject)"/>
359 public void SetContent(EvasObject content, bool preserveOldContent)
361 if (preserveOldContent)
363 Interop.Elementary.elm_object_content_unset(RealHandle);
366 Interop.Elementary.elm_object_content_set(RealHandle, content);
367 UpdatePartContents(content);
371 /// Sets text to particular part of the widget.
373 /// <param name="part">The name of particular part</param>
374 /// <param name="text">The text</param>
375 public virtual bool SetPartText(string part, string text)
377 Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
382 /// Gets text of a particular part of the widget.
384 /// <param name="part">The name of particular part</param>
385 /// <returns>Text of the particular part of the widget</returns>
386 public virtual string GetPartText(string part)
388 return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
392 /// Sets color of a particular part of the widget.
394 /// <param name="part">The name of particular part</param>
395 /// <param name="color">The color be set to widget</param>
396 /// <remarks>This method is a virtual method, it could be override by special child class</remarks>
397 public virtual void SetPartColor(string part, Color color)
399 Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
400 color.G * color.A / 255,
401 color.B * color.A / 255,
406 /// Gets color of the particular part of the widget.
408 /// <param name="part">The name of particular part</param>
409 /// <returns>The color of the particular part</returns>
410 /// <remarks>This method is a virtual method, it could be override by special child class</remarks>
411 public virtual Color GetPartColor(string part)
414 Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
415 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
419 /// Sets opacity of the particular part of the widget.
421 /// <param name="part">The name of particular part</param>
422 /// <param name="opacity">The opacity of the particular part</param>
423 public void SetPartOpacity(string part, int opacity)
425 Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
429 /// Gets opacity of the particular part of the widget.
431 /// <param name="part">The name of particular part</param>
432 /// <returns>Opacity value of the particular part</returns>
433 public int GetPartOpacity(string part)
436 Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
440 internal IntPtr GetPartContent(string part)
442 return Interop.Elementary.elm_object_part_content_get(RealHandle, part);