/*
* Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using ElmSharp.Accessible;
namespace ElmSharp
{
///
/// Enumeration for the focus direction.
///
public enum FocusDirection
{
///
/// Previous direction
///
Previous,
///
/// Next direction
///
Next,
///
/// Up direction
///
Up,
///
/// Down direction
///
Down,
///
/// Right direction
///
Right,
///
/// Left direction
///
Left
}
///
/// The Widget is abstract class, it is the parent of other widgets.
/// Inherits from .
///
public abstract class Widget : AccessibleObject
{
Dictionary _partContents = new Dictionary();
SmartEvent _focused;
SmartEvent _unfocused;
internal Color _backgroundColor = Color.Default;
internal int _opacity = Color.Default.A;
protected Widget()
{
}
///
/// Creates and initializes a new instance of the Widget class.
///
/// The parent of new Widget instance
protected Widget(EvasObject parent) : base(parent)
{
_focused = new SmartEvent(this, "focused");
_focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);
_unfocused = new SmartEvent(this, "unfocused");
_unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
}
///
/// Update the part contents
///
/// The content which put to the part
/// The updated part
protected void UpdatePartContents(EvasObject content, string part = "__default__")
{
_partContents[part] = content;
}
///
/// Focused will be triggered when the widget is focused.
///
public event EventHandler Focused;
///
/// Unfocused will be triggered when the widget is unfocused.
///
public event EventHandler Unfocused;
///
/// Sets or gets the state of the widget, which might be enabled or disabled.
///
public bool IsEnabled
{
get
{
return !Interop.Elementary.elm_object_disabled_get(RealHandle);
}
set
{
Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
}
}
///
/// Sets or gets the style of the widget.
///
public string Style
{
get
{
return Interop.Elementary.elm_object_style_get(RealHandle);
}
set
{
Interop.Elementary.elm_object_style_set(RealHandle, value);
}
}
///
/// Gets whether this widget is focused.
///
public bool IsFocused
{
get
{
return Interop.Elementary.elm_object_focus_get(RealHandle);
}
}
///
/// Gets whether a widget is focusable or not.
///
/// Widgets which are meant to be interacted with by input events are created able to be focused, by default
public bool IsFocusAllowed
{
get
{
return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
}
}
///
/// Sets or gets the text of the widget.
///
/// It could be override by special child class
public virtual string Text
{
get
{
return Interop.Elementary.elm_object_part_text_get(RealHandle);
}
set
{
Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
}
}
///
/// Sets or gets the background color of the widget.
///
/// It could be override by special child class
public virtual Color BackgroundColor
{
get
{
if (!_backgroundColor.IsDefault)
{
_backgroundColor = GetPartColor("bg");
}
return _backgroundColor;
}
set
{
if (value.IsDefault)
{
Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
}
else
{
SetPartColor("bg", value);
_backgroundColor = value;
}
}
}
///
/// Sets or gets the opacity of the widget.
///
/// It could be override by special child class
public virtual int Opacity
{
get
{
if (_opacity != Color.Default.A)
{
_opacity = GetPartOpacity("opacity");
}
return _opacity;
}
set
{
SetPartOpacity("opacity", value);
_opacity = value;
}
}
///
/// Sets or gets whether a widget and its children are focusable or not.
///
public bool AllowTreeFocus
{
get
{
return Interop.Elementary.elm_object_tree_focus_allow_get(RealHandle);
}
set
{
Interop.Elementary.elm_object_tree_focus_allow_set(RealHandle, value);
}
}
///
/// Sets or gets the widget's mirrored mode.
///
public bool IsMirroredMode
{
get
{
return Interop.Elementary.elm_object_mirrored_get(RealHandle);
}
set
{
Interop.Elementary.elm_object_mirrored_set(RealHandle, value);
}
}
///
/// Sets or gets the widget's mirrored mode setting.
/// When widget set automatic mode(true), it follows the system mirrored mode.
///
public bool IsAutoMirroredMode
{
get
{
return Interop.Elementary.elm_object_mirrored_automatic_get(RealHandle);
}
set
{
Interop.Elementary.elm_object_mirrored_automatic_set(RealHandle, value);
}
}
///
/// Sets the widget to be focused or not.
///
/// Weather be focused
public void SetFocus(bool isFocus)
{
Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
}
///
/// Sets the ability for a widget to be focused.
///
/// True if the object can be focused, false if not(and on errors)
public void AllowFocus(bool isAllowFocus)
{
Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
}
///
/// Gives focus to next widget in widget tree.
///
/// Direction to move the focus
public void FocusNext(FocusDirection direction)
{
Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
}
///
/// Set next widget with specific focus direction.
///
/// Focus next widget
/// Focus direction
public void SetNextFocusObject(EvasObject next, FocusDirection direction)
{
Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
}
///
/// Sets content to particular part of the widget, and the preserve old content will not be unset.
///
/// The name of particular part
/// The content
///
public virtual bool SetPartContent(string part, EvasObject content)
{
return SetPartContent(part, content, false);
}
///
/// Sets content to particular part of the widget.
///
/// The name of particular part
/// The content
/// true, preserve old content will be unset. false, preserve old content will not be unset.
///
public virtual bool SetPartContent(string part, EvasObject content, bool preserveOldContent)
{
if (preserveOldContent)
{
Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
}
Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
UpdatePartContents(content, part);
return true;
}
///
/// Sets content to the widget, and the preserve old content will not be unset.
///
/// The content
///
public void SetContent(EvasObject content)
{
SetContent(content, false);
}
///
/// Sets content the widget.
///
/// The content
/// true, preserve old content will be unset. false, preserve old content will not be unset.
///
public void SetContent(EvasObject content, bool preserveOldContent)
{
if (preserveOldContent)
{
Interop.Elementary.elm_object_content_unset(RealHandle);
}
Interop.Elementary.elm_object_content_set(RealHandle, content);
UpdatePartContents(content);
}
///
/// Sets text to particular part of the widget.
///
/// The name of particular part
/// The text
public virtual bool SetPartText(string part, string text)
{
Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
return true;
}
///
/// Gets text of a particular part of the widget.
///
/// The name of particular part
/// Text of the particular part of the widget
public virtual string GetPartText(string part)
{
return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
}
///
/// Sets color of a particular part of the widget.
///
/// The name of particular part
/// The color be set to widget
/// This method is a virtual method, it could be override by special child class
public virtual void SetPartColor(string part, Color color)
{
Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
color.G * color.A / 255,
color.B * color.A / 255,
color.A);
}
///
/// Gets color of the particular part of the widget.
///
/// The name of particular part
/// The color of the particular part
/// This method is a virtual method, it could be override by special child class
public virtual Color GetPartColor(string part)
{
int r, g, b, a;
Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
}
///
/// Sets opacity of the particular part of the widget.
///
/// The name of particular part
/// The opacity of the particular part
public void SetPartOpacity(string part, int opacity)
{
Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
}
///
/// Gets opacity of the particular part of the widget.
///
/// The name of particular part
/// Opacity value of the particular part
public int GetPartOpacity(string part)
{
int r, g, b, a;
Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
return a;
}
internal IntPtr GetPartContent(string part)
{
return Interop.Elementary.elm_object_part_content_get(RealHandle, part);
}
}
}