[NUI] Add SelectedChanged event to the SelectGroup and etc. (#3221)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / RadioButtonGroup.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
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
18 using System;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Binding;
22
23 namespace Tizen.NUI.Components
24 {
25     /// <summary>
26     /// The RadioButtonGroup class is used to group together a set of RadioButton control
27     /// It enables the user to select exclusively single radio button of group.
28     /// </summary>
29     /// <code>
30     /// RadioButtonGroup radioGroup = new RadioButtonGroup();
31     /// RadioButton radio1 = new RadioButton();
32     /// RadioButton radio2 = new RadioButton();
33     /// radioGroup.Add(radio1);
34     /// radioGroup.Add(radio2);
35     /// </code>
36     /// <since_tizen> 6 </since_tizen>
37     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
38     [EditorBrowsable(EditorBrowsableState.Never)]
39     public class RadioButtonGroup : SelectGroup
40     {
41         /// <summary>
42         /// IsGroupHolderProperty
43         /// </summary>
44         [EditorBrowsable(EditorBrowsableState.Never)]
45         public static readonly BindableProperty IsGroupHolderProperty = BindableProperty.CreateAttached("IsGroupHolder", typeof(bool), typeof(View), false, propertyChanged: OnIsGroupHolderChanged);
46
47         private static readonly BindableProperty RadioButtonGroupProperty = BindableProperty.CreateAttached("RadioButtonGroup", typeof(RadioButtonGroup), typeof(View), null, propertyChanged: OnRadioButtonGroupChanged);
48
49         /// <summary>
50         /// Construct RadioButtonGroup
51         /// </summary>
52         /// <since_tizen> 6 </since_tizen>
53         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
54         [EditorBrowsable(EditorBrowsableState.Never)]
55         public RadioButtonGroup() : base()
56         {
57             EnableMultiSelection = false;
58         }
59
60         /// <summary>
61         /// Gets a RadioButtonGroup.IsGroupHolder property of a view.
62         /// </summary>
63         /// <param name="view">The group holder.</param>
64         [EditorBrowsable(EditorBrowsableState.Never)]
65         public static bool GetIsGroupHolder(View view) => (bool)view.GetValue(IsGroupHolderProperty);
66
67         /// <summary>
68         /// Sets a RadioButtonGroup.IsGroupHolder property for a view.
69         /// </summary>
70         /// <param name="view">The group holder.</param>
71         /// <param name="value">The value to set.</param>
72         [EditorBrowsable(EditorBrowsableState.Never)]
73         public static void SetIsGroupHolder(View view, bool value) => view.SetValue(IsGroupHolderProperty, value, false, true);
74
75         /// <summary>
76         /// Gets a attached RadioButtonGroup for a view.
77         /// </summary>
78         /// <param name="view">The group holder.</param>
79         [EditorBrowsable(EditorBrowsableState.Never)]
80         public static RadioButtonGroup GetRadioButtonGroup(View view) => view.GetValue(RadioButtonGroupProperty) as RadioButtonGroup;
81
82         /// <summary>
83         /// Get the RadioButton object at the specified index.
84         /// </summary>
85         /// <param name="index">item index</param>
86         /// <returns>RadioButton</returns>
87         /// <since_tizen> 6 </since_tizen>
88         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
89         [EditorBrowsable(EditorBrowsableState.Never)]
90         public RadioButton GetItem(int index)
91         {
92             return ItemGroup[index] as RadioButton;
93         }
94
95         /// <summary>
96         /// Get the RadioButton object at the currently selected. If no item selected, returns null.
97         /// </summary>
98         /// <returns>Currently selected radio button</returns>
99         [EditorBrowsable(EditorBrowsableState.Never)]
100         public RadioButton GetSelectedItem()
101         {
102             return (SelectedIndex >= 0 && SelectedIndex < ItemGroup.Count) ? ItemGroup[SelectedIndex] as RadioButton : null;
103         }
104
105         /// <summary>
106         /// Add RadioButton to the end of RadioButtonGroup.
107         /// </summary>
108         /// <param name="radio">The RadioButton to be added to the RadioButtonGroup</param>
109         /// <since_tizen> 6 </since_tizen>
110         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
111         [EditorBrowsable(EditorBrowsableState.Never)]
112         public void Add(RadioButton radio)
113         {
114             if (null == radio) return;
115             base.AddSelection(radio);
116             radio.ItemGroup = this;
117         }
118
119         /// <summary>
120         /// Remove RadioButton from the RadioButtonGroup.
121         /// </summary>
122         /// <param name="radio">The RadioButton to remove from the RadioButtonGroup</param>
123         /// <since_tizen> 6 </since_tizen>
124         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
125         [EditorBrowsable(EditorBrowsableState.Never)]
126         public void Remove(RadioButton radio)
127         {
128             if (null == radio) return;
129             base.RemoveSelection(radio);
130             radio.ItemGroup = null;
131         }
132
133         private static void OnIsGroupHolderChanged(Binding.BindableObject bindable, object oldValue, object newValue)
134         {
135             var view = bindable as View;
136
137             if (view == null) return;
138
139             if (!(bool)newValue)
140             {
141                 view.SetValue(RadioButtonGroupProperty, null, false, true);
142                 return;
143             }
144
145             if (view.GetValue(RadioButtonGroupProperty) == null)
146             {
147                 view.SetValue(RadioButtonGroupProperty, new RadioButtonGroup(), false, true);
148             }
149         }
150
151         private static void OnRadioButtonGroupChanged(Binding.BindableObject bindable, object oldValue, object newValue)
152         {
153             var view = bindable as View;
154
155             if (view == null) return;
156
157             if (oldValue is RadioButtonGroup oldGroup)
158             {
159                 view.ChildAdded -= oldGroup.OnChildChanged;
160                 view.ChildRemoved -= oldGroup.OnChildChanged;
161                 oldGroup.RemoveAll();
162             }
163
164             if (newValue is RadioButtonGroup newGroup)
165             {
166                 view.ChildAdded += newGroup.OnChildChanged;
167                 view.ChildRemoved += newGroup.OnChildChanged;
168                 newGroup.OnChildChanged(view, null);
169             }
170         }
171
172         private void OnChildChanged(object sender, EventArgs args)
173         {
174             if (sender is View view)
175             {
176                 RemoveAll();
177                 foreach (var child in view.Children)
178                     if (child is RadioButton radioButton)
179                         Add(radioButton);
180             }
181         }
182
183         private void RemoveAll()
184         {
185             var copied = ItemGroup.ToArray();
186             foreach (var button in copied)
187             {
188                 Remove(button as RadioButton);
189             }
190         }
191     }
192 }