2 * Copyright(c) 2017 Samsung Electronics Co., Ltd.
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 namespace Tizen.NUI.UIComponents
22 using System.Runtime.InteropServices;
23 using Tizen.NUI.BaseComponents;
26 /// The Button class is a base class for different kinds of buttons.<br />
27 /// This class provides the disabled property and the clicked signal.<br />
28 /// The clicked event handler is emitted when the button is touched, and the touch point doesn't leave the boundary of the button.<br />
29 /// When the disabled property is set to true, no signal is emitted.<br />
30 /// The 'Visual' describes not just traditional images like PNG and BMP, but also refers to whatever is used to show the button. It could be a color, gradient, or some other kind of renderer.<br />
31 /// The button's appearance can be modified by setting properties for the various visuals or images.<br />
32 /// It is not mandatory to set all the visuals. A button could be defined only by setting its background visual, or by setting its background and selected visuals.<br />
33 /// The button visual is shown over the background visual.<br />
34 /// When pressed, the unselected visuals are replaced by the selected visuals.<br />
35 /// The text label is always placed on the top of all images.<br />
36 /// When the button is disabled, the background button and the selected visuals are replaced by their disabled visuals.<br />
38 public class Button : View
40 private global::System.Runtime.InteropServices.HandleRef swigCPtr;
42 internal Button(global::System.IntPtr cPtr, bool cMemoryOwn) : base(NDalicPINVOKE.Button_SWIGUpcast(cPtr), cMemoryOwn)
44 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
47 internal static global::System.Runtime.InteropServices.HandleRef getCPtr(Button obj)
49 return (obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr;
53 /// To dispose the button instance.
55 protected override void Dispose(DisposeTypes type)
62 if (type == DisposeTypes.Explicit)
65 //Release your own managed resources here.
66 //You should release all of your own disposable objects here.
70 //Release your own unmanaged resources here.
71 //You should not access any managed member here except static instance.
72 //because the execution order of Finalizes is non-deterministic.
74 DisConnectFromSignals();
76 if (swigCPtr.Handle != global::System.IntPtr.Zero)
81 NDalicPINVOKE.delete_Button(swigCPtr);
83 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
89 private void DisConnectFromSignals()
92 global::System.Runtime.InteropServices.HandleRef currentCPtr = swigCPtr;
94 // Use BaseHandle CPtr as current might have been deleted already in derived classes.
95 swigCPtr = GetBaseHandleCPtrHandleRef;
97 if (_stateChangedCallback != null)
99 StateChangedSignal().Disconnect(_stateChangedCallback);
102 if (_releasedCallback != null)
104 ReleasedSignal().Disconnect(_releasedCallback);
107 if (_pressedCallback != null)
109 this.PressedSignal().Disconnect(_pressedCallback);
112 if (_clickedCallback != null)
114 ClickedSignal().Disconnect(_clickedCallback);
117 // BaseHandle CPtr is used in Registry and there is danger of deletion if we keep using it here.
118 // Restore current CPtr.
119 swigCPtr = currentCPtr;
123 private EventHandlerWithReturnType<object, EventArgs, bool> _clickedEventHandler;
124 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
125 private delegate bool ClickedCallbackType(global::System.IntPtr data);
126 private ClickedCallbackType _clickedCallback;
129 /// The Clicked event will be triggered when the button is touched and the touch point doesn't leave the boundary of the button.
131 /// <since_tizen> 3 </since_tizen>
132 public event EventHandlerWithReturnType<object, EventArgs, bool> Clicked
136 if (_clickedEventHandler == null)
138 _clickedCallback = OnClicked;
139 ClickedSignal().Connect(_clickedCallback);
142 _clickedEventHandler += value;
147 _clickedEventHandler -= value;
149 if (_clickedEventHandler == null && ClickedSignal().Empty() == false)
151 ClickedSignal().Disconnect(_clickedCallback);
157 private bool OnClicked(IntPtr data)
159 if (_clickedEventHandler != null)
161 return _clickedEventHandler(this, null);
168 private EventHandlerWithReturnType<object, EventArgs, bool> _pressedEventHandler;
169 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
170 private delegate bool PressedCallbackType(global::System.IntPtr data);
171 private PressedCallbackType _pressedCallback;
174 /// The Pressed event will be triggered when the button is touched.
176 /// <since_tizen> 3 </since_tizen>
177 public event EventHandlerWithReturnType<object, EventArgs, bool> Pressed
181 if (_pressedEventHandler == null)
183 _pressedCallback = OnPressed;
184 PressedSignal().Connect(_pressedCallback);
187 _pressedEventHandler += value;
192 _pressedEventHandler -= value;
194 if (_pressedEventHandler == null && PressedSignal().Empty() == false)
196 this.PressedSignal().Disconnect(_pressedCallback);
201 private bool OnPressed(IntPtr data)
203 if (_pressedEventHandler != null)
205 return _pressedEventHandler(this, null);
212 private EventHandlerWithReturnType<object, EventArgs, bool> _releasedEventHandler;
213 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
214 private delegate bool ReleasedCallbackType(global::System.IntPtr data);
215 private ReleasedCallbackType _releasedCallback;
218 /// The Released event will be triggered when the button is touched and the touch point leaves the boundary of the button.
220 /// <since_tizen> 3 </since_tizen>
221 public event EventHandlerWithReturnType<object, EventArgs, bool> Released
225 if (_releasedEventHandler == null)
227 _releasedCallback = OnReleased;
228 ReleasedSignal().Connect(_releasedCallback);
230 _releasedEventHandler += value;
235 _releasedEventHandler -= value;
237 if (_releasedEventHandler == null && ReleasedSignal().Empty() == false)
239 ReleasedSignal().Disconnect(_releasedCallback);
245 private bool OnReleased(IntPtr data)
247 if (_releasedEventHandler != null)
249 return _releasedEventHandler(this, null);
255 private EventHandlerWithReturnType<object, EventArgs, bool> _stateChangedEventHandler;
256 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
257 private delegate bool StateChangedCallback(global::System.IntPtr data);
258 private StateChangedCallback _stateChangedCallback;
261 /// The StateChanged event will be triggered when the button's state is changed.
263 /// <since_tizen> 3 </since_tizen>
264 public event EventHandlerWithReturnType<object, EventArgs, bool> StateChanged
268 if (_stateChangedEventHandler == null)
270 _stateChangedCallback = OnStateChanged;
271 StateChangedSignal().Connect(_stateChangedCallback);
274 _stateChangedEventHandler += value;
279 _stateChangedEventHandler -= value;
281 if (_stateChangedEventHandler == null && StateChangedSignal().Empty() == false)
283 StateChangedSignal().Disconnect(_stateChangedCallback);
288 private bool OnStateChanged(IntPtr data)
290 if (_stateChangedEventHandler != null)
292 return _stateChangedEventHandler(this, null);
299 /// Gets or sets the unselected button foreground or icon visual.
301 /// <since_tizen> 3 </since_tizen>
302 public Tizen.NUI.PropertyMap UnselectedVisual
306 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
307 GetProperty(Button.Property.UNSELECTED_VISUAL).Get(temp);
312 SetProperty(Button.Property.UNSELECTED_VISUAL, new Tizen.NUI.PropertyValue(value));
317 /// Gets or sets the selected button foreground or icon visual.
319 /// <since_tizen> 3 </since_tizen>
320 public Tizen.NUI.PropertyMap SelectedVisual
324 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
325 GetProperty(Button.Property.SELECTED_VISUAL).Get(temp);
330 SetProperty(Button.Property.SELECTED_VISUAL, new Tizen.NUI.PropertyValue(value));
335 /// Gets or sets the disabled selected state foreground or icon button visual.
337 /// <since_tizen> 3 </since_tizen>
338 public Tizen.NUI.PropertyMap DisabledSelectedVisual
342 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
343 GetProperty(Button.Property.DISABLED_SELECTED_VISUAL).Get(temp);
348 SetProperty(Button.Property.DISABLED_SELECTED_VISUAL, new Tizen.NUI.PropertyValue(value));
353 /// Gets or sets the disabled unselected state foreground or icon visual.
355 /// <since_tizen> 3 </since_tizen>
356 public Tizen.NUI.PropertyMap DisabledUnselectedVisual
360 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
361 GetProperty(Button.Property.DISABLED_UNSELECTED_VISUAL).Get(temp);
366 SetProperty(Button.Property.DISABLED_UNSELECTED_VISUAL, new Tizen.NUI.PropertyValue(value));
371 /// Gets or sets the disabled unselected state background button visual.
373 /// <since_tizen> 3 </since_tizen>
374 public Tizen.NUI.PropertyMap UnselectedBackgroundVisual
378 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
379 GetProperty(Button.Property.UNSELECTED_BACKGROUND_VISUAL).Get(temp);
384 SetProperty(Button.Property.UNSELECTED_BACKGROUND_VISUAL, new Tizen.NUI.PropertyValue(value));
389 /// Gets or sets the selected background button visual.
391 /// <since_tizen> 3 </since_tizen>
392 public Tizen.NUI.PropertyMap SelectedBackgroundVisual
396 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
397 GetProperty(Button.Property.SELECTED_BACKGROUND_VISUAL).Get(temp);
402 SetProperty(Button.Property.SELECTED_BACKGROUND_VISUAL, new Tizen.NUI.PropertyValue(value));
407 /// Gets or sets the disabled while unselected background button visual.
409 /// <since_tizen> 3 </since_tizen>
410 public Tizen.NUI.PropertyMap DisabledUnselectedBackgroundVisual
414 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
415 GetProperty(Button.Property.DISABLED_UNSELECTED_BACKGROUND_VISUAL).Get(temp);
420 SetProperty(Button.Property.DISABLED_UNSELECTED_BACKGROUND_VISUAL, new Tizen.NUI.PropertyValue(value));
425 /// Gets or sets the disabled while selected background button visual.
427 /// <since_tizen> 3 </since_tizen>
428 public Tizen.NUI.PropertyMap DisabledSelectedBackgroundVisual
432 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
433 GetProperty(Button.Property.DISABLED_SELECTED_BACKGROUND_VISUAL).Get(temp);
438 SetProperty(Button.Property.DISABLED_SELECTED_BACKGROUND_VISUAL, new Tizen.NUI.PropertyValue(value));
443 /// Gets or sets the position of the the label in relation to the foreground or icon, if both present.
445 /// <since_tizen> 3 </since_tizen>
446 public Align LabelRelativeAlignment
451 if (GetProperty(Button.Property.LABEL_RELATIVE_ALIGNMENT).Get(out temp) == false)
453 NUILog.Error("LabelRelativeAlignment get error!");
471 string valueToString = "";
476 valueToString = "BEGIN";
481 valueToString = "END";
486 valueToString = "TOP";
491 valueToString = "BOTTOM";
496 valueToString = "END";
500 SetProperty(Button.Property.LABEL_RELATIVE_ALIGNMENT, new Tizen.NUI.PropertyValue(valueToString));
505 /// Gets or sets the padding around the text.
507 /// <since_tizen> 3 </since_tizen>
508 public Vector4 LabelPadding
512 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
513 GetProperty(Button.Property.LABEL_PADDING).Get(temp);
518 SetProperty(Button.Property.LABEL_PADDING, new Tizen.NUI.PropertyValue(value));
523 /// Gets or sets the padding around the foreground visual.
525 /// <since_tizen> 3 </since_tizen>
526 public Vector4 ForegroundVisualPadding
530 Vector4 temp = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
531 GetProperty(Button.Property.FOREGROUND_VISUAL_PADDING).Get(temp);
536 SetProperty(Button.Property.FOREGROUND_VISUAL_PADDING, new Tizen.NUI.PropertyValue(value));
540 internal new class Property
542 internal static readonly int UNSELECTED_VISUAL = NDalicManualPINVOKE.Button_Property_UNSELECTED_VISUAL_get();
543 internal static readonly int SELECTED_VISUAL = NDalicManualPINVOKE.Button_Property_SELECTED_VISUAL_get();
544 internal static readonly int DISABLED_SELECTED_VISUAL = NDalicManualPINVOKE.Button_Property_DISABLED_SELECTED_VISUAL_get();
545 internal static readonly int DISABLED_UNSELECTED_VISUAL = NDalicManualPINVOKE.Button_Property_DISABLED_UNSELECTED_VISUAL_get();
546 internal static readonly int UNSELECTED_BACKGROUND_VISUAL = NDalicManualPINVOKE.Button_Property_UNSELECTED_BACKGROUND_VISUAL_get();
547 internal static readonly int SELECTED_BACKGROUND_VISUAL = NDalicManualPINVOKE.Button_Property_SELECTED_BACKGROUND_VISUAL_get();
548 internal static readonly int DISABLED_UNSELECTED_BACKGROUND_VISUAL = NDalicManualPINVOKE.Button_Property_DISABLED_UNSELECTED_BACKGROUND_VISUAL_get();
549 internal static readonly int DISABLED_SELECTED_BACKGROUND_VISUAL = NDalicManualPINVOKE.Button_Property_DISABLED_SELECTED_BACKGROUND_VISUAL_get();
550 internal static readonly int LABEL_RELATIVE_ALIGNMENT = NDalicManualPINVOKE.Button_Property_LABEL_RELATIVE_ALIGNMENT_get();
551 internal static readonly int LABEL_PADDING = NDalicManualPINVOKE.Button_Property_LABEL_PADDING_get();
552 internal static readonly int FOREGROUND_VISUAL_PADDING = NDalicManualPINVOKE.Button_Property_VISUAL_PADDING_get();
553 internal static readonly int AUTO_REPEATING = NDalicPINVOKE.Button_Property_AUTO_REPEATING_get();
554 internal static readonly int INITIAL_AUTO_REPEATING_DELAY = NDalicPINVOKE.Button_Property_INITIAL_AUTO_REPEATING_DELAY_get();
555 internal static readonly int NEXT_AUTO_REPEATING_DELAY = NDalicPINVOKE.Button_Property_NEXT_AUTO_REPEATING_DELAY_get();
556 internal static readonly int TOGGLABLE = NDalicPINVOKE.Button_Property_TOGGLABLE_get();
557 internal static readonly int SELECTED = NDalicPINVOKE.Button_Property_SELECTED_get();
558 internal static readonly int UNSELECTED_COLOR = NDalicPINVOKE.Button_Property_UNSELECTED_COLOR_get();
559 internal static readonly int SELECTED_COLOR = NDalicPINVOKE.Button_Property_SELECTED_COLOR_get();
560 internal static readonly int LABEL = NDalicPINVOKE.Button_Property_LABEL_get();
564 /// Creates an uninitialized button.<br />
565 /// Only the derived versions can be instantiated.<br />
567 /// <since_tizen> 3 </since_tizen>
568 public Button() : this(NDalicPINVOKE.new_Button__SWIG_0(), true)
570 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
573 internal ButtonSignal PressedSignal()
575 ButtonSignal ret = new ButtonSignal(NDalicPINVOKE.Button_PressedSignal(swigCPtr), false);
576 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
580 internal ButtonSignal ReleasedSignal()
582 ButtonSignal ret = new ButtonSignal(NDalicPINVOKE.Button_ReleasedSignal(swigCPtr), false);
583 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
587 internal ButtonSignal ClickedSignal()
589 ButtonSignal ret = new ButtonSignal(NDalicPINVOKE.Button_ClickedSignal(swigCPtr), false);
590 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
594 internal ButtonSignal StateChangedSignal()
596 ButtonSignal ret = new ButtonSignal(NDalicPINVOKE.Button_StateChangedSignal(swigCPtr), false);
597 if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
602 /// If the autorepeating property is set to true, then the togglable property is set to false.
604 /// <since_tizen> 3 </since_tizen>
605 public bool AutoRepeating
610 GetProperty(Button.Property.AUTO_REPEATING).Get(out temp);
615 SetProperty(Button.Property.AUTO_REPEATING, new Tizen.NUI.PropertyValue(value));
620 /// By default, this value is set to 0.15 seconds.
622 /// <since_tizen> 3 </since_tizen>
623 public float InitialAutoRepeatingDelay
628 GetProperty(Button.Property.INITIAL_AUTO_REPEATING_DELAY).Get(out temp);
633 SetProperty(Button.Property.INITIAL_AUTO_REPEATING_DELAY, new Tizen.NUI.PropertyValue(value));
638 /// By default, this value is set to 0.05 seconds.
640 /// <since_tizen> 3 </since_tizen>
641 public float NextAutoRepeatingDelay
646 GetProperty(Button.Property.NEXT_AUTO_REPEATING_DELAY).Get(out temp);
651 SetProperty(Button.Property.NEXT_AUTO_REPEATING_DELAY, new Tizen.NUI.PropertyValue(value));
656 /// If the togglable property is set to true, then the autorepeating property is set to false.
658 /// <since_tizen> 3 </since_tizen>
659 public bool Togglable
664 GetProperty(Button.Property.TOGGLABLE).Get(out temp);
669 SetProperty(Button.Property.TOGGLABLE, new Tizen.NUI.PropertyValue(value));
674 /// Gets or sets the togglable button as either selected or unselected, togglable property must be set to true.
676 /// <since_tizen> 3 </since_tizen>
682 GetProperty(Button.Property.SELECTED).Get(out temp);
687 SetProperty(Button.Property.SELECTED, new Tizen.NUI.PropertyValue(value));
692 /// Gets or sets the unselected color.
694 /// <since_tizen> 3 </since_tizen>
695 public Color UnselectedColor
699 Color temp = new Color(0.0f, 0.0f, 0.0f, 0.0f);
700 GetProperty(Button.Property.UNSELECTED_COLOR).Get(temp);
705 SetProperty(Button.Property.UNSELECTED_COLOR, new Tizen.NUI.PropertyValue(value));
710 /// Gets or sets the selected color.
712 /// <since_tizen> 3 </since_tizen>
713 public Color SelectedColor
717 Color temp = new Color(0.0f, 0.0f, 0.0f, 0.0f);
718 GetProperty(Button.Property.SELECTED_COLOR).Get(temp);
723 SetProperty(Button.Property.SELECTED_COLOR, new Tizen.NUI.PropertyValue(value));
728 /// Gets or sets the label.
730 /// <since_tizen> 3 </since_tizen>
731 public Tizen.NUI.PropertyMap Label
735 Tizen.NUI.PropertyMap temp = new Tizen.NUI.PropertyMap();
736 GetProperty(Button.Property.LABEL).Get(temp);
741 SetProperty(Button.Property.LABEL, new Tizen.NUI.PropertyValue(value));
746 /// Gets or sets the text of the label.
748 /// <since_tizen> 3 </since_tizen>
749 public string LabelText
753 Tizen.NUI.PropertyMap map = new Tizen.NUI.PropertyMap();
754 GetProperty( Button.Property.LABEL).Get(map);
755 Tizen.NUI.PropertyValue value = map.Find( TextVisualProperty.Text, "Text");
762 SetProperty( Button.Property.LABEL, new Tizen.NUI.PropertyValue( value ) );
767 /// Enumeration for describing the position, the text label can be, in relation to the control (and foreground/icon).
769 /// <since_tizen> 3 </since_tizen>
773 /// At the start of the control before the foreground or icon.
777 /// At the end of the control after the foreground or icon.
781 /// At the top of the control above the foreground or icon.
785 /// At the bottom of the control below the foreground or icon.