Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Button.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18
19 namespace ElmSharp
20 {
21     /// <summary>
22     /// The Button is a widget works as a clickable input element to trigger events.
23     /// </summary>
24     public class Button : Layout
25     {
26         private SmartEvent _clicked;
27         private SmartEvent _repeated;
28         private SmartEvent _pressed;
29         private SmartEvent _released;
30
31         /// <summary>
32         /// Creates and initializes a new instance of the Button class.
33         /// </summary>
34         /// <param name="parent">
35         /// The EvasObject to which the new Button will be attached as a child.
36         /// </param>
37         public Button(EvasObject parent) : base(parent)
38         {
39             _clicked = new SmartEvent(this, this.RealHandle, "clicked");
40             _repeated = new SmartEvent(this, this.RealHandle, "repeated");
41             _pressed = new SmartEvent(this, this.RealHandle, "pressed");
42             _released = new SmartEvent(this, this.RealHandle, "unpressed");
43
44             _clicked.On += (sender, e) =>
45             {
46                 Clicked?.Invoke(this, EventArgs.Empty);
47             };
48
49             _repeated.On += (sender, e) =>
50             {
51                 Repeated?.Invoke(this, EventArgs.Empty);
52             };
53
54             _pressed.On += (sender, e) =>
55             {
56                 Pressed?.Invoke(this, EventArgs.Empty);
57             };
58
59             _released.On += (sender, e) =>
60             {
61                 Released?.Invoke(this, EventArgs.Empty);
62             };
63         }
64
65         /// <summary>
66         /// Clicked will be triggered when Button is clicked.
67         /// </summary>
68         public event EventHandler Clicked;
69
70         /// <summary>
71         /// Repeated will be triggered when Button is pressed without releasing it.
72         /// </summary>
73         public event EventHandler Repeated;
74
75         /// <summary>
76         /// Pressed will be triggered when the Button is pressed.
77         /// </summary>
78         public event EventHandler Pressed;
79
80         /// <summary>
81         /// Released will be triggered when the Button is released after being pressed.
82         /// </summary>
83         public event EventHandler Released;
84
85         /// <summary>
86         /// Sets or gets the autorepeat feature of a given Button.
87         /// </summary>
88         /// <remarks>
89         /// Autorepeat feature means autorepeat event generated when the button is kept pressed.
90         /// When set AutoRepeat to false, no autorepeat is performed and buttons will trigger Clicked event when they are clicked.
91         /// When set to true, keeping a button pressed continuously trigger Repeated event until the button is released.
92         /// The time it takes until it starts triggering Repeated is given by AutoRepeatInitialTime,
93         /// and the time between each new emission is given by AutoRepeatGapTimeout.
94         /// </remarks>
95         public bool AutoRepeat
96         {
97             get
98             {
99                 return !Interop.Elementary.elm_button_autorepeat_get(RealHandle);
100             }
101             set
102             {
103                 Interop.Elementary.elm_button_autorepeat_set(RealHandle, value);
104             }
105         }
106
107         /// <summary>
108         /// Sets or gets the initial timeout before the Repeat event is generated.
109         /// </summary>
110         public double AutoRepeatInitialTime
111         {
112             get
113             {
114                 return Interop.Elementary.elm_button_autorepeat_initial_timeout_get(RealHandle);
115             }
116             set
117             {
118                 Interop.Elementary.elm_button_autorepeat_initial_timeout_set(RealHandle, value);
119             }
120         }
121
122         /// <summary>
123         /// Sets or gets the interval between each generated Repeat event.
124         /// </summary>
125         public double AutoRepeatGapTimeout
126         {
127             get
128             {
129                 return Interop.Elementary.elm_button_autorepeat_gap_timeout_get(RealHandle);
130             }
131             set
132             {
133                 Interop.Elementary.elm_button_autorepeat_gap_timeout_set(RealHandle, value);
134             }
135         }
136
137         [Obsolete("DeleteColorClass is obsolete, please use EdjeObject.DeleteColorClass(string)")]
138         public void DeleteColorClass(string part)
139         {
140             Interop.Elementary.edje_object_color_class_del(Handle, part);
141         }
142
143         /// <summary>
144         /// Sets or gets the BackgroundColor of a given Button in normal and pressed status.
145         /// </summary>
146         public override Color BackgroundColor
147         {
148             set
149             {
150                 if (value.IsDefault)
151                 {
152                     EdjeObject.DeleteColorClass("button/bg");
153                     EdjeObject.DeleteColorClass("button/bg_pressed");
154                     EdjeObject.DeleteColorClass("button/bg_disabled");
155                 }
156                 else
157                 {
158                     SetPartColor("bg", value);
159                     SetPartColor("bg_pressed", value);
160                     SetPartColor("bg_disabled", value);
161                 }
162                 _backgroundColor = value;
163             }
164         }
165
166         protected override IntPtr CreateHandle(EvasObject parent)
167         {
168             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
169             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
170
171             RealHandle = Interop.Elementary.elm_button_add(handle);
172             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
173
174             return handle;
175         }
176     }
177 }