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);
298 // Temporary code due to focus issue (will be removed at Tizen 5.0)
301 if (isFocus && !Interop.Evas.evas_object_focus_get(RealHandle))
302 Interop.Evas.evas_object_focus_set(RealHandle, true);
307 /// Sets the ability for a widget to be focused.
309 /// <param name="isAllowFocus">true if the object can be focused, false if not(and on errors).</param>
310 /// <since_tizen> preview </since_tizen>
311 public void AllowFocus(bool isAllowFocus)
313 Interop.Elementary.elm_object_focus_allow_set(RealHandle, isAllowFocus);
317 /// Gives focus to the next widget in the widget tree.
319 /// <param name="direction">Direction to move the focus.</param>
320 /// <since_tizen> preview </since_tizen>
321 public void FocusNext(FocusDirection direction)
323 Interop.Elementary.elm_object_focus_next(RealHandle, (int)direction);
327 /// Sets the next widget with specific focus direction.
329 /// <param name="next">Focus next widget.</param>
330 /// <param name="direction">Focus direction.</param>
331 /// <since_tizen> preview </since_tizen>
332 public void SetNextFocusObject(EvasObject next, FocusDirection direction)
334 Interop.Elementary.elm_object_focus_next_object_set(RealHandle, next.RealHandle, (int)direction);
338 /// Sets content to the particular part of the widget, and the preserve old content will not be unset.
340 /// <param name="part">The name of the particular part.</param>
341 /// <param name="content">The content.</param>
342 /// <seealso cref="SetPartContent(string, EvasObject, bool)"/>
343 /// <since_tizen> preview </since_tizen>
344 public virtual bool SetPartContent(string part, EvasObject content)
346 return SetPartContent(part, content, false);
350 /// Sets content to the particular part of the widget.
352 /// <param name="part">The name of the particular part.</param>
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="SetPartContent(string, EvasObject)"/>
356 /// <since_tizen> preview </since_tizen>
357 public virtual bool SetPartContent(string part, EvasObject content, bool preserveOldContent)
359 if (preserveOldContent)
361 Interop.Elementary.elm_object_part_content_unset(RealHandle, part);
363 Interop.Elementary.elm_object_part_content_set(RealHandle, part, content);
364 UpdatePartContents(content, part);
369 /// Sets content to the widget, and the preserve old content will not be unset.
371 /// <param name="content">The content.</param>
372 /// <seealso cref="SetContent(EvasObject, bool)"/>
373 /// <since_tizen> preview </since_tizen>
374 public void SetContent(EvasObject content)
376 SetContent(content, false);
380 /// Sets content to the widget.
382 /// <param name="content">The content.</param>
383 /// <param name="preserveOldContent">true, preserve old content will be unset. false, preserve old content will not be unset.</param>
384 /// <seealso cref="SetContent(EvasObject)"/>
385 /// <since_tizen> preview </since_tizen>
386 public void SetContent(EvasObject content, bool preserveOldContent)
388 if (preserveOldContent)
390 Interop.Elementary.elm_object_content_unset(RealHandle);
393 Interop.Elementary.elm_object_content_set(RealHandle, content);
394 UpdatePartContents(content);
398 /// Sets text to the particular part of the widget.
400 /// <param name="part">The name of the particular part.</param>
401 /// <param name="text">The text.</param>
402 /// <since_tizen> preview </since_tizen>
403 public virtual bool SetPartText(string part, string text)
405 Interop.Elementary.elm_object_part_text_set(RealHandle, part, text);
410 /// Gets text of a particular part of the widget.
412 /// <param name="part">The name of the particular part.</param>
413 /// <returns>Text of the particular part of the widget.</returns>
414 /// <since_tizen> preview </since_tizen>
415 public virtual string GetPartText(string part)
417 return Interop.Elementary.elm_object_part_text_get(RealHandle, part);
421 /// Sets color of a particular part of the widget.
423 /// <param name="part">The name of the particular part.</param>
424 /// <param name="color">The color to be set to the widget.</param>
425 /// <remarks>This method is a virtual method, it could be overridden by special child class.</remarks>
426 /// <since_tizen> preview </since_tizen>
427 public virtual void SetPartColor(string part, Color color)
429 Interop.Elementary.elm_object_color_class_color_set(RealHandle, part, color.R * color.A / 255,
430 color.G * color.A / 255,
431 color.B * color.A / 255,
436 /// Gets color of the particular part of the widget.
438 /// <param name="part">The name of the particular part.</param>
439 /// <returns>The color of the particular part.</returns>
440 /// <remarks>This method is a virtual method, it could be overridden by special child class.</remarks>
441 /// <since_tizen> preview </since_tizen>
442 public virtual Color GetPartColor(string part)
445 Interop.Elementary.elm_object_color_class_color_get(RealHandle, part, out r, out g, out b, out a);
446 return new Color((int)(r / (a / 255.0)), (int)(g / (a / 255.0)), (int)(b / (a / 255.0)), a);
450 /// Sets opacity of the particular part of the widget.
452 /// <param name="part">The name of the particular part.</param>
453 /// <param name="opacity">The opacity of the particular part.</param>
454 /// <since_tizen> preview </since_tizen>
455 public void SetPartOpacity(string part, int opacity)
457 Interop.Elementary.elm_object_color_class_color_set(Handle, part, 255, 255, 255, opacity);
461 /// Gets opacity of the particular part of the widget.
463 /// <param name="part">The name of the particular part.</param>
464 /// <returns>Opacity value of the particular part.</returns>
465 /// <since_tizen> preview </since_tizen>
466 public int GetPartOpacity(string part)
469 Interop.Elementary.elm_object_color_class_color_get(Handle, part, out r, out g, out b, out a);
474 /// Sends a signal to the edje object of the widget.
476 /// <param name="emission">The signal's name.</param>
477 /// <param name="source">The signal's source.</param>
478 /// <since_tizen> preview </since_tizen>
479 public void SignalEmit(string emission, string source)
481 Interop.Elementary.elm_object_signal_emit(Handle, emission, source);
485 /// The callback of the Realized event.
487 /// <since_tizen> preview </since_tizen>
488 protected override void OnRealized()
491 _focused = new SmartEvent(this, "focused");
492 _focused.On += (s, e) => Focused?.Invoke(this, EventArgs.Empty);
494 _unfocused = new SmartEvent(this, "unfocused");
495 _unfocused.On += (s, e) => Unfocused?.Invoke(this, EventArgs.Empty);
498 internal IntPtr GetPartContent(string part)
500 return Interop.Elementary.elm_object_part_content_get(RealHandle, part);