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 /// <since_tizen> preview </since_tizen>
27 public enum FocusDirection
30 /// Previous direction.
61 /// The Widget is an abstract class and the parent of other widgets.
62 /// Inherits from <see cref="EvasObject"/>.
64 /// <since_tizen> preview </since_tizen>
65 public abstract class Widget : AccessibleObject
67 Dictionary<string, EvasObject> _partContents = new Dictionary<string, EvasObject>();
70 SmartEvent _unfocused;
72 internal Color _backgroundColor = Color.Default;
73 internal int _opacity = Color.Default.A;
76 /// Creates and initializes a new instance of the Widget class.
78 /// <since_tizen> preview </since_tizen>
84 /// Creates and initializes a new instance of the Widget class.
86 /// <param name="parent">The parent of the new Widget instance.</param>
87 /// <since_tizen> preview </since_tizen>
88 protected Widget(EvasObject parent) : base(parent)
93 /// Updates the part contents.
95 /// <param name="content">The content which is put into the part.</param>
96 /// <param name="part">The updated part.</param>
97 /// <since_tizen> preview </since_tizen>
98 protected void UpdatePartContents(EvasObject content, string part = "__default__")
100 _partContents[part] = content;
104 /// Focused will be triggered when the widget is focused.
106 /// <since_tizen> preview </since_tizen>
107 public event EventHandler Focused;
110 /// Unfocused will be triggered when the widget is unfocused.
112 /// <since_tizen> preview </since_tizen>
113 public event EventHandler Unfocused;
116 /// Sets or gets the state of the widget, which might be enabled or disabled.
118 /// <since_tizen> preview </since_tizen>
119 public virtual bool IsEnabled
123 return !Interop.Elementary.elm_object_disabled_get(RealHandle);
127 Interop.Elementary.elm_object_disabled_set(RealHandle, !value);
132 /// Sets or gets the style of the widget.
134 /// <since_tizen> preview </since_tizen>
139 return Interop.Elementary.elm_object_style_get(RealHandle);
143 Interop.Elementary.elm_object_style_set(RealHandle, value);
148 /// Gets whether this widget is focused.
150 /// <since_tizen> preview </since_tizen>
151 public bool IsFocused
155 return Interop.Elementary.elm_object_focus_get(RealHandle);
160 /// Gets whether a widget is focusable or not.
162 /// <remarks>Widgets which are meant to be interacted with by input events, are created able to be focused by default.</remarks>
163 /// <since_tizen> preview </since_tizen>
164 public bool IsFocusAllowed
168 return Interop.Elementary.elm_object_focus_allow_get(RealHandle);
173 /// Sets or gets the text of the widget.
175 /// <remarks>It could be overridden by special child class.</remarks>
176 /// <since_tizen> preview </since_tizen>
177 public virtual string Text
181 return Interop.Elementary.elm_object_part_text_get(RealHandle);
185 Interop.Elementary.elm_object_part_text_set(RealHandle, IntPtr.Zero, value);
190 /// Sets or gets the background color of the widget.
192 /// <remarks>It could be overridden by special child class.</remarks>
193 /// <since_tizen> preview </since_tizen>
194 public virtual Color BackgroundColor
198 if (!_backgroundColor.IsDefault)
200 _backgroundColor = GetPartColor("bg");
202 return _backgroundColor;
208 Console.WriteLine("Widget instance doesn't support to set BackgroundColor to Color.Default.");
212 SetPartColor("bg", value);
213 _backgroundColor = value;
219 /// Sets or gets the opacity of the widget.
221 /// <remarks>It could be overridden by special child class.</remarks>
222 /// <since_tizen> preview </since_tizen>
223 public virtual int Opacity
227 if (_opacity != Color.Default.A)
229 _opacity = GetPartOpacity("opacity");
235 SetPartOpacity("opacity", value);
241 /// Sets or gets whether a widget and its children are focusable or not.
243 /// <since_tizen> preview </since_tizen>
244 public bool AllowTreeFocus
248 return Interop.Elementary.elm_object_tree_focus_allow_get(RealHandle);
252 Interop.Elementary.elm_object_tree_focus_allow_set(RealHandle, value);
257 /// Sets or gets the widget's mirrored mode.
259 /// <since_tizen> preview </since_tizen>
260 public bool IsMirroredMode
264 return Interop.Elementary.elm_object_mirrored_get(RealHandle);
268 Interop.Elementary.elm_object_mirrored_set(RealHandle, value);
273 /// Sets or gets the widget's mirrored mode setting.
274 /// When widget is set to automatic mode(true), it follows the system mirrored mode.
276 /// <since_tizen> preview </since_tizen>
277 public bool IsAutoMirroredMode
281 return Interop.Elementary.elm_object_mirrored_automatic_get(RealHandle);
285 Interop.Elementary.elm_object_mirrored_automatic_set(RealHandle, value);
290 /// Sets the widget to be focused or not.
292 /// <param name="isFocus">Whether be focused.</param>
293 /// <since_tizen> preview </since_tizen>
294 public void SetFocus(bool isFocus)
296 Interop.Elementary.elm_object_focus_set(RealHandle, isFocus);
300 /// Sets the ability for a widget to be focused.
302 /// <param name="isAllowFocus">true if the object can be focused, false if not(and on errors).</param>
303 /// <since_tizen> preview </since_tizen>
304 public void AllowFocus(bool isAllowFocus)
306 Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
310 /// Gives focus to the next widget in the widget tree.
312 /// <param name="direction">Direction to move the focus.</param>
313 /// <since_tizen> preview </since_tizen>
314 public void FocusNext(FocusDirection direction)
316 Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
320 /// Sets the next widget with specific focus direction.
322 /// <param name="next">Focus next widget.</param>
323 /// <param name="direction">Focus direction.</param>
324 /// <since_tizen> preview </since_tizen>
325 public void SetNextFocusObject(EvasObject next, FocusDirection direction)
327 Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
331 /// Sets content to the particular part of the widget, and the preserve old content will not be unset.
333 /// <param name="part">The name of the particular part.</param>
334 /// <param name="content">The content.</param>
335 /// <seealso cref="SetPartContent(string, EvasObject, bool)"/>
336 /// <since_tizen> preview </since_tizen>
337 public virtual bool SetPartContent(string part, EvasObject content)
339 return SetPartContent(part, content, false);
343 /// Sets content to the particular part of the widget.
345 /// <param name="part">The name of the particular part.</param>
346 /// <param name="content">The content.</param>
347 /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
348 /// <seealso cref="SetPartContent(string, EvasObject)"/>
349 /// <since_tizen> preview </since_tizen>
350 public virtual bool SetPartContent(string part, EvasObject content, bool preserveOldContent)
352 if (preserveOldContent)
354 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
356 Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
357 UpdatePartContents(content, part);
362 /// Sets content to the widget, and the preserve old content will not be unset.
364 /// <param name="content">The content.</param>
365 /// <seealso cref="SetContent(EvasObject, bool)"/>
366 /// <since_tizen> preview </since_tizen>
367 public void SetContent(EvasObject content)
369 SetContent(content, false);
373 /// Sets content to the widget.
375 /// <param name="content">The content.</param>
376 /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
377 /// <seealso cref="SetContent(EvasObject)"/>
378 /// <since_tizen> preview </since_tizen>
379 public void SetContent(EvasObject content, bool preserveOldContent)
381 if (preserveOldContent)
383 Interop.Elementary.elm_object_content_unset(RealHandle);
386 Interop.Elementary.elm_object_content_set(RealHandle, content);
387 UpdatePartContents(content);
391 /// Sets text to the particular part of the widget.
393 /// <param name="part">The name of the particular part.</param>
394 /// <param name="text">The text.</param>
395 /// <since_tizen> preview </since_tizen>
396 public virtual bool SetPartText(string part, string text)
398 Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
403 /// Gets text of a particular part of the widget.
405 /// <param name="part">The name of the particular part.</param>
406 /// <returns>Text of the particular part of the widget.</returns>
407 /// <since_tizen> preview </since_tizen>
408 public virtual string GetPartText(string part)
410 return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
414 /// Sets color of a particular part of the widget.
416 /// <param name="part">The name of the particular part.</param>
417 /// <param name="color">The color to be set to the widget.</param>
418 /// <remarks>This method is a virtual method, it could be overridden by special child class.</remarks>
419 /// <since_tizen> preview </since_tizen>
420 public virtual void SetPartColor(string part, Color color)
422 Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
423 color.G * color.A / 255,
424 color.B * color.A / 255,
429 /// Gets color of the particular part of the widget.
431 /// <param name="part">The name of the particular part.</param>
432 /// <returns>The color of the particular part.</returns>
433 /// <remarks>This method is a virtual method, it could be overridden by special child class.</remarks>
434 /// <since_tizen> preview </since_tizen>
435 public virtual Color GetPartColor(string part)
438 Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
439 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
443 /// Sets opacity of the particular part of the widget.
445 /// <param name="part">The name of the particular part.</param>
446 /// <param name="opacity">The opacity of the particular part.</param>
447 /// <since_tizen> preview </since_tizen>
448 public void SetPartOpacity(string part, int opacity)
450 Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
454 /// Gets opacity of the particular part of the widget.
456 /// <param name="part">The name of the particular part.</param>
457 /// <returns>Opacity value of the particular part.</returns>
458 /// <since_tizen> preview </since_tizen>
459 public int GetPartOpacity(string part)
462 Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
467 /// Sends a signal to the edje object of the widget.
469 /// <param name="emission">The signal's name.</param>
470 /// <param name="source">The signal's source.</param>
471 /// <since_tizen> preview </since_tizen>
472 public void SignalEmit(string emission, string source)
474 Interop.Elementary.elm_object_signal_emit(Handle, emission, source);
478 /// The callback of the Realized event.
480 /// <since_tizen> preview </since_tizen>
481 protected override void OnRealized()
484 _focused = new SmartEvent(this, "focused");
485 _focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);
487 _unfocused = new SmartEvent(this, "unfocused");
488 _unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
491 internal IntPtr GetPartContent(string part)
493 return Interop.Elementary.elm_object_part_content_get(RealHandle, part);