[NUI] Merge mater#1206 to API6 (#1208)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / ButtonGroup.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 using System;
18 using System.ComponentModel;
19 using System.Collections.Generic;
20 using Tizen.NUI.Binding;
21 using Tizen.NUI.BaseComponents;
22
23 namespace Tizen.NUI.Components
24 {
25     /// <summary>
26     /// ButtonGroup is a group of buttons which can be set common property<br />
27     /// </summary>
28     /// <since_tizen> 3 </since_tizen>
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class ButtonGroup : BindableObject, global::System.IDisposable
31     {
32         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         public static readonly BindableProperty ItemheightProperty = BindableProperty.Create(nameof(Itemheight), typeof(float), typeof(ButtonGroup), 0.0f, propertyChanged: (bindable, oldValue, newValue) =>
35         {
36             ButtonGroup btGroup = (ButtonGroup)bindable;
37             if (btGroup.itemGroup != null && newValue != null)
38             {
39                 foreach (Button btn in btGroup.itemGroup)
40                 {
41                     btn.Style.SizeHeight = (float)newValue;
42                 }
43                 btGroup.itemheight = (float)newValue;
44             }
45         },
46         defaultValueCreator: (bindable) =>
47         {
48             ButtonGroup btGroup = (ButtonGroup)bindable;
49             return btGroup.itemheight;
50         });
51
52         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
53         [EditorBrowsable(EditorBrowsableState.Never)]
54         public static readonly BindableProperty ItemPointSizeProperty = BindableProperty.Create(nameof(ItemPointSize), typeof(float), typeof(ButtonGroup), 0.0f, propertyChanged: (bindable, oldValue, newValue) =>
55         {
56             ButtonGroup btGroup = (ButtonGroup)bindable;
57             if (btGroup.itemGroup != null && newValue != null)
58             {
59                 foreach (Button btn in btGroup.itemGroup)
60                 {
61                     btn.Style.Text.PointSize = (float)newValue;
62                 }
63                 btGroup.itemPointSize = (float)newValue;
64             }
65         },
66         defaultValueCreator: (bindable) =>
67         {
68             ButtonGroup btGroup = (ButtonGroup)bindable;
69             return btGroup.itemPointSize;
70         });
71
72         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
73         [EditorBrowsable(EditorBrowsableState.Never)]
74         public static readonly BindableProperty ItemFontFamilyProperty = BindableProperty.Create(nameof(ItemFontFamily), typeof(string), typeof(ButtonGroup), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
75         {
76             ButtonGroup btGroup = (ButtonGroup)bindable;
77             if (btGroup.itemGroup != null && newValue != null)
78             {
79                 foreach (Button btn in btGroup.itemGroup)
80                 {
81                     btn.Style.Text.FontFamily = (string)newValue;
82                 }
83                 btGroup.itemFontFamily = (string)newValue;
84             }
85         },
86         defaultValueCreator: (bindable) =>
87         {
88             ButtonGroup btGroup = (ButtonGroup)bindable;
89             return btGroup.itemFontFamily;
90         });
91
92         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
93         [EditorBrowsable(EditorBrowsableState.Never)]
94         public static readonly BindableProperty ItemTextColorProperty = BindableProperty.Create(nameof(ItemTextColor), typeof(Color), typeof(ButtonGroup), Color.Black, propertyChanged: (bindable, oldValue, newValue) =>
95         {
96             ButtonGroup btGroup = (ButtonGroup)bindable;
97             if (btGroup.itemGroup != null && newValue != null)
98             {
99                 foreach (Button btn in btGroup.itemGroup)
100                 {
101                     btn.Style.Text.TextColor = (Color)newValue;
102                 }
103                 btGroup.itemTextColor = (Color)newValue;
104             }
105         },
106         defaultValueCreator: (bindable) =>
107         {
108             ButtonGroup btGroup = (ButtonGroup)bindable;
109             return btGroup.itemTextColor;
110         });
111
112         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
113         [EditorBrowsable(EditorBrowsableState.Never)]
114         public static readonly BindableProperty ItemTextAlignmentProperty = BindableProperty.Create(nameof(ItemTextAlignment), typeof(HorizontalAlignment), typeof(ButtonGroup), new HorizontalAlignment(), propertyChanged: (bindable, oldValue, newValue) =>
115         {
116             ButtonGroup btGroup = (ButtonGroup)bindable;
117             if (btGroup.itemGroup != null && newValue != null)
118             {
119                 foreach (Button btn in btGroup.itemGroup)
120                 {
121                     btn.Style.Text.HorizontalAlignment = (HorizontalAlignment)newValue;
122                 }
123                 btGroup.itemTextAlignment = (HorizontalAlignment)newValue;
124             }
125         },
126         defaultValueCreator: (bindable) =>
127         {
128             ButtonGroup btGroup = (ButtonGroup)bindable;
129             return btGroup.itemTextAlignment;
130         });
131
132         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
133         [EditorBrowsable(EditorBrowsableState.Never)]
134         public static readonly BindableProperty OverLayBackgroundColorSelectorProperty = BindableProperty.Create(nameof(OverLayBackgroundColorSelector), typeof(Selector<Color>), typeof(ButtonGroup), new Selector<Color>(), propertyChanged: (bindable, oldValue, newValue) =>
135         {
136             ButtonGroup btGroup = (ButtonGroup)bindable;
137             if (btGroup.itemGroup != null && newValue != null)
138             {
139                 foreach (Button btn in btGroup.itemGroup)
140                 {
141                     btn.Style.Overlay.BackgroundColor = (Selector<Color>)newValue;
142                 }
143                 btGroup.overLayBackgroundColorSelector = (Selector<Color>)newValue;
144             }
145         },
146         defaultValueCreator: (bindable) =>
147         {
148             ButtonGroup btGroup = (ButtonGroup)bindable;
149             return btGroup.overLayBackgroundColorSelector;
150         });
151
152         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
153         [EditorBrowsable(EditorBrowsableState.Never)]
154         public static readonly BindableProperty ItemBackgroundImageUrlProperty = BindableProperty.Create(nameof(ItemBackgroundImageUrl), typeof(string), typeof(ButtonGroup), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
155         {
156             ButtonGroup btGroup = (ButtonGroup)bindable;
157             if (btGroup.itemGroup != null && newValue != null)
158             {
159                 foreach (Button btn in btGroup.itemGroup)
160                 {
161                     if (btn.Style.BackgroundImage == null)
162                     {
163                         btn.Style.BackgroundImage = new Selector<string>();
164                     }              
165                     btn.Style.BackgroundImage = (string)newValue;
166                 }
167                 btGroup.itemBackgroundImageUrl = (string)newValue;
168             }
169         },
170         defaultValueCreator: (bindable) =>
171         {
172             ButtonGroup btGroup = (ButtonGroup)bindable;
173             return btGroup.itemBackgroundImageUrl;
174         });
175
176         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
177         [EditorBrowsable(EditorBrowsableState.Never)]
178         public static readonly BindableProperty ItemBackgroundBorderProperty = BindableProperty.Create(nameof(ItemBackgroundBorder), typeof(Rectangle), typeof(ButtonGroup), new Rectangle(), propertyChanged: (bindable, oldValue, newValue) =>
179         {
180             ButtonGroup btGroup = (ButtonGroup)bindable;
181             if (btGroup.itemGroup != null && newValue != null)
182             {
183                 foreach (Button btn in btGroup.itemGroup)
184                 {                 
185                     btn.BackgroundImageBorder = (Rectangle)newValue;
186                 }
187                 btGroup.itemBackgroundBorder = (Rectangle)newValue;
188             }
189         },
190         defaultValueCreator: (bindable) =>
191         {
192             ButtonGroup btGroup = (ButtonGroup)bindable;
193             return btGroup.itemBackgroundBorder;
194         });
195
196         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
197         [EditorBrowsable(EditorBrowsableState.Never)]
198         public static readonly BindableProperty ItemShadowUrlProperty = BindableProperty.Create(nameof(ItemShadowUrl), typeof(string), typeof(ButtonGroup), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
199         {
200             ButtonGroup btGroup = (ButtonGroup)bindable;
201             if (btGroup.itemGroup != null && newValue != null)
202             {
203                 foreach (Button btn in btGroup.itemGroup)
204                 {
205                     if (btn.Style.Shadow.ResourceUrl == null)
206                     {
207                         btn.Style.Shadow.ResourceUrl = new Selector<string>();
208                     }
209                     btn.Style.Shadow.ResourceUrl = (string)newValue;
210                 }
211                 btGroup.itemShadowUrl = (string)newValue;
212             }
213         },
214         defaultValueCreator: (bindable) =>
215         {
216             ButtonGroup btGroup = (ButtonGroup)bindable;
217             return btGroup.itemShadowUrl;
218         });
219
220         /// This will be public opened in tizen_5.0 after ACR done. Before ACR, need to be hidden as inhouse API.
221         [EditorBrowsable(EditorBrowsableState.Never)]
222         public static readonly BindableProperty ItemShadowBorderProperty = BindableProperty.Create(nameof(ItemShadowBorder), typeof(Rectangle), typeof(ButtonGroup), new Rectangle(), propertyChanged: (bindable, oldValue, newValue) =>
223         {
224             ButtonGroup btGroup = (ButtonGroup)bindable;
225             if (btGroup.itemGroup != null && newValue != null)
226             {
227                 foreach (Button btn in btGroup.itemGroup)
228                 {
229                     btn.Style.Shadow.Border = (Rectangle)newValue;
230                 }
231                 btGroup.itemShadowBorder = (Rectangle)newValue;
232             }
233         },
234         defaultValueCreator: (bindable) =>
235         {
236             ButtonGroup btGroup = (ButtonGroup)bindable;
237             return btGroup.itemShadowBorder;
238         });
239
240         [EditorBrowsable(EditorBrowsableState.Never)]
241         public ButtonGroup(View view) : base()
242         {
243             itemGroup = new List<Button>();
244             if ((root = view) == null)
245             {
246                 throw new Exception("Root view is null.");
247             }
248         }
249
250         [EditorBrowsable(EditorBrowsableState.Never)]
251         public int Count
252         {
253             get
254             {
255                 return itemGroup.Count;
256             }
257         }
258
259         [EditorBrowsable(EditorBrowsableState.Never)]
260         public bool Contains(Button bt)
261         {
262             return itemGroup.Contains(bt);
263         }
264
265         [EditorBrowsable(EditorBrowsableState.Never)]
266         public int GetIndex(Button bt)
267         {
268             return itemGroup.IndexOf(bt);
269         }
270
271         [EditorBrowsable(EditorBrowsableState.Never)]
272         public Button GetItem(int index)
273         {
274             if (index >= Count || index < 0)
275             {
276                 throw new Exception("button index error");
277             }
278             return itemGroup[index];
279         }
280
281         [EditorBrowsable(EditorBrowsableState.Never)]
282         public void AddItem(Button bt)
283         {
284             if (itemGroup.Contains(bt))
285             {
286                 return;
287             }
288             itemGroup.Add(bt);
289             root.Add(bt);
290         }
291
292         [EditorBrowsable(EditorBrowsableState.Never)]
293         public void RemoveItem(Button bt)
294         {
295             if (!itemGroup.Contains(bt))
296             {
297                 return;
298             }
299             itemGroup.Remove(bt);
300             root.Remove(bt);
301             bt.Dispose();
302         }
303
304         [EditorBrowsable(EditorBrowsableState.Never)]
305         public void RemoveItem(int index)
306         {
307             if (index >= Count || index < 0)
308             {
309                 throw new Exception("button index error");
310             }
311             Button bt = itemGroup[index];
312             itemGroup.Remove(bt);
313             root.Remove(bt);
314             bt.Dispose();
315         }
316
317         [EditorBrowsable(EditorBrowsableState.Never)]
318         public void RemoveAll()
319         {
320             foreach (Button bt in itemGroup)
321             {
322                 root.Remove(bt);
323                 bt.Dispose();
324             }
325             itemGroup.Clear();
326         }
327
328         [EditorBrowsable(EditorBrowsableState.Never)]
329         public void UpdateButton(ButtonStyle btStyle)
330         {
331             if (Count == 0) return;
332
333             int buttonWidth = (int)root.Size.Width / Count;
334             int buttonHeight = (int)itemheight;
335             foreach (Button btnTemp in itemGroup)
336             {
337                 btnTemp.Size = new Size(buttonWidth, buttonHeight);
338             }
339
340             int pos = 0;
341             if (root.LayoutDirection == ViewLayoutDirectionType.RTL)
342             {
343                 for (int i = Count - 1; i >= 0; i--)
344                 {
345                     itemGroup[i].Style.PositionX = pos;
346                     pos += (int)(itemGroup[i].Size.Width);
347                 }
348             }
349             else
350             {
351                 for (int i = 0; i < Count; i++)
352                 {
353                     itemGroup[i].Style.PositionX = pos;
354                     pos += (int)(itemGroup[i].Size.Width);
355                 }
356             }
357
358             if (btStyle == null || btStyle.Text == null || btStyle.Text.TextColor == null) return;
359             ItemTextColor = btStyle.Text.TextColor.All;
360         }
361
362         /// <summary>
363         /// Common height for all of the Items
364         /// </summary>
365         /// <since_tizen> 3 </since_tizen>
366         [EditorBrowsable(EditorBrowsableState.Never)]
367         public float Itemheight
368         {
369             get
370             {
371                 return (float)GetValue(ItemheightProperty);
372             }
373             set
374             {
375                 SetValue(ItemheightProperty, value);
376             }
377         }
378
379         [EditorBrowsable(EditorBrowsableState.Never)]
380         public float ItemPointSize
381         {
382             get
383             {
384                 return (float)GetValue(ItemPointSizeProperty);
385             }
386             set
387             {
388                 SetValue(ItemPointSizeProperty, value);
389             }
390         }
391
392         [EditorBrowsable(EditorBrowsableState.Never)]
393         public string ItemFontFamily
394         {
395             get
396             {
397                 return (string)GetValue(ItemFontFamilyProperty);
398             }
399             set
400             {
401                 SetValue(ItemFontFamilyProperty, value);
402             }
403         }
404
405         [EditorBrowsable(EditorBrowsableState.Never)]
406         public Color ItemTextColor
407         {
408             get
409             {
410                 return (Color)GetValue(ItemTextColorProperty);
411             }
412             set
413             {
414                 SetValue(ItemTextColorProperty, value);
415             }
416         }
417
418         [EditorBrowsable(EditorBrowsableState.Never)]
419         public HorizontalAlignment ItemTextAlignment
420         {
421             get
422             {
423                 return (HorizontalAlignment)GetValue(ItemTextAlignmentProperty);
424             }
425             set
426             {
427                 SetValue(ItemTextAlignmentProperty, value);
428             }
429         }
430
431         [EditorBrowsable(EditorBrowsableState.Never)]
432         public Selector<Color> OverLayBackgroundColorSelector
433         {
434             get
435             {
436                 return (Selector<Color>)GetValue(OverLayBackgroundColorSelectorProperty);
437             }
438             set
439             {
440                 SetValue(OverLayBackgroundColorSelectorProperty, value);
441             }
442         }
443
444         /// <summary>
445         /// The mutually exclusive with "backgroundColor" and "background" type Map.
446         /// </summary>
447         /// <since_tizen> 3 </since_tizen>
448         [EditorBrowsable(EditorBrowsableState.Never)]
449         public string ItemBackgroundImageUrl
450         {
451             get
452             {
453                 return (string)GetValue(ItemBackgroundImageUrlProperty);
454             }
455             set
456             {
457                 SetValue(ItemBackgroundImageUrlProperty, value);
458             }
459         }
460
461         [EditorBrowsable(EditorBrowsableState.Never)]
462         public Rectangle ItemBackgroundBorder
463         {
464             get
465             {
466                 return (Rectangle)GetValue(ItemBackgroundBorderProperty);
467             }
468             set
469             {
470                 SetValue(ItemBackgroundBorderProperty, value);
471             }
472         }
473
474         [EditorBrowsable(EditorBrowsableState.Never)]
475         public string ItemShadowUrl
476         {
477             get
478             {
479                 return (string)GetValue(ItemShadowUrlProperty);
480             }
481             set
482             {
483                 SetValue(ItemShadowUrlProperty, value);
484             }
485         }
486
487         [EditorBrowsable(EditorBrowsableState.Never)]
488         public Rectangle ItemShadowBorder
489         {
490             get
491             {
492                 return (Rectangle)GetValue(ItemShadowBorderProperty);
493             }
494             set
495             {
496                 SetValue(ItemShadowBorderProperty, value);
497             }
498         }
499
500         [EditorBrowsable(EditorBrowsableState.Never)]
501         public void Dispose()
502         {
503             if (disposed) return;
504             if (itemGroup != null)
505             {
506                 RemoveAll();
507                 itemGroup = null;
508             }
509             disposed = true;
510         }
511
512         private List<Button> itemGroup;
513         private View root = null;
514         private bool disposed = false;
515         private float itemheight;
516         private float itemPointSize;
517         private string itemFontFamily;
518         private Color itemTextColor = new Color();
519         private HorizontalAlignment itemTextAlignment;
520         private Selector<Color> overLayBackgroundColorSelector = new Selector<Color>();
521         private string itemBackgroundImageUrl;
522         private Rectangle itemBackgroundBorder = new Rectangle();
523         private string itemShadowUrl;
524         private Rectangle itemShadowBorder = new Rectangle();
525     }
526 }