/*
* Copyright (c) 2018 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.
///
/// preview
public enum FocusDirection
{
///
/// Previous direction.
///
Previous,
///
/// Next direction.
///
Next,
///
/// Up direction.
///
Up,
///
/// Down direction.
///
Down,
///
/// Right direction.
///
Right,
///
/// Left direction.
///
Left
}
///
/// The Widget is an abstract class and the parent of other widgets.
/// Inherits from .
///
/// preview
public abstract class Widget : AccessibleObject
{
Dictionary _partContents = new Dictionary();
SmartEvent _focused;
SmartEvent _unfocused;
internal Color _backgroundColor = Color.Default;
internal int _opacity = Color.Default.A;
///
/// Creates and initializes a new instance of the Widget class.
///
/// preview
protected Widget()
{
}
///
/// Creates and initializes a new instance of the Widget class.
///
/// The parent of the new Widget instance.
/// preview
protected Widget(EvasObject parent) : base(parent)
{
}
///
/// Updates the part contents.
///
/// The content which is put into the part.
/// The updated part.
/// preview
protected void UpdatePartContents(EvasObject content, string part = "__default__")
{
_partContents[part] = content;
}
///
/// Focused will be triggered when the widget is focused.
///
/// preview
public event EventHandler Focused;
///
/// Unfocused will be triggered when the widget is unfocused.
///
/// preview
public event EventHandler Unfocused;
///
/// Sets or gets the state of the widget, which might be enabled or disabled.
///
/// preview
public virtual 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.
///
/// preview
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.
///
/// preview
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.
/// preview
public bool IsFocusAllowed
{
get
{
return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
}
}
///
/// Sets or gets the text of the widget.
///
/// It could be overridden by special child class.
/// preview
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 overridden by special child class.
/// preview
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 overridden by special child class.
/// preview
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.
///
/// preview
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.
///
/// preview
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 is set to automatic mode(true), it follows the system mirrored mode.
///
/// preview
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.
///
/// Whether be focused.
/// preview
public void SetFocus(bool isFocus)
{
if (isFocus)
{
Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
}
else
{
Interop.Elementary.elm_object_focused_clear(RealHandle);
}
}
///
/// Sets the ability for a widget to be focused.
///
/// true if the object can be focused, false if not(and on errors).
/// preview
public void AllowFocus(bool isAllowFocus)
{
Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
}
///
/// Gives focus to the next widget in the widget tree.
///
/// Direction to move the focus.
/// preview
public void FocusNext(FocusDirection direction)
{
Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
}
///
/// Sets the next widget with specific focus direction.
///
/// Focus next widget.
/// Focus direction.
/// preview
public void SetNextFocusObject(EvasObject next, FocusDirection direction)
{
Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next?.RealHandle ?? IntPtr.Zero, (int)direction);
}
///
/// Sets content to the particular part of the widget, and the preserve old content will be deleted.
///
/// The name of the particular part.
/// The content.
///
/// preview
public virtual bool SetPartContent(string part, EvasObject content)
{
return SetPartContent(part, content, false);
}
///
/// Sets content to the particular part of the widget.
///
/// The name of the particular part.
/// The content.
/// true, preserve old content will be unset and not be deleted. false, preserve old content will be deleted.
///
/// preview
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 be deleted.
///
/// The content.
///
/// preview
public void SetContent(EvasObject content)
{
SetContent(content, false);
}
///
/// Sets content to the widget.
///
/// The content.
/// true, preserve old content will be unset and not be deleted. false, preserve old content will be deleted.
///
/// preview
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 the particular part of the widget.
///
/// The name of the particular part.
/// The text.
/// preview
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 the particular part.
/// Text of the particular part of the widget.
/// preview
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 the particular part.
/// The color to be set to the widget.
/// This method is a virtual method, it could be overridden by special child class.
/// preview
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 the particular part.
/// The color of the particular part.
/// This method is a virtual method, it could be overridden by special child class.
/// preview
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 the particular part.
/// The opacity of the particular part.
/// preview
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 the particular part.
/// Opacity value of the particular part.
/// preview
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;
}
///
/// Sends a signal to the edje object of the widget.
///
/// The signal's name.
/// The signal's source.
/// preview
public void SignalEmit(string emission, string source)
{
Interop.Elementary.elm_object_signal_emit(Handle, emission, source);
}
///
/// The callback of the Realized event.
///
/// preview
protected override void OnRealized()
{
base.OnRealized();
_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);
}
internal IntPtr GetPartContent(string part)
{
return Interop.Elementary.elm_object_part_content_get(RealHandle, part);
}
}
}