Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / ElmSharp / ElmSharp / Check.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 check is a widget allows for toggling a value between true and false.
23     /// </summary>
24     public class Check : Layout
25     {
26         private SmartEvent _changed;
27         private bool _currentState;
28
29         /// <summary>
30         /// Creates and initializes a new instance of the Check class.
31         /// </summary>
32         /// <param name="parent">
33         /// The EvasObject to which the new Check will be attached as a child.
34         /// </param>
35         public Check(EvasObject parent) : base(parent)
36         {
37             _changed = new SmartEvent(this, this.RealHandle, "changed");
38             _changed.On += (sender, e) =>
39             {
40                 StateChanged?.Invoke(this, new CheckStateChangedEventArgs(_currentState, IsChecked));
41             };
42         }
43
44         /// <summary>
45         /// StateChanged will be triggered when the IsChecked in the Check is changed.
46         /// </summary>
47         public event EventHandler<CheckStateChangedEventArgs> StateChanged;
48
49         /// <summary>
50         /// Sets or gets whether the given Check is checked or not.
51         /// </summary>
52         /// <remarks>
53         /// When object is checked, the value will set to true, Conversely will set to false.
54         /// </remarks>
55         public bool IsChecked
56         {
57             get
58             {
59                 _currentState = Interop.Elementary.elm_check_state_get(RealHandle);
60                 return _currentState;
61             }
62             set
63             {
64                 Interop.Elementary.elm_check_state_set(RealHandle, value);
65             }
66         }
67
68         protected override IntPtr CreateHandle(EvasObject parent)
69         {
70             IntPtr handle = Interop.Elementary.elm_layout_add(parent.Handle);
71             Interop.Elementary.elm_layout_theme_set(handle, "layout", "elm_widget", "default");
72
73             RealHandle = Interop.Elementary.elm_check_add(handle);
74             Interop.Elementary.elm_object_part_content_set(handle, "elm.swallow.content", RealHandle);
75
76             return handle;
77         }
78     }
79 }