[NUI.Components] Fix build warnings (#1233) (#1237)
[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         /// <summary>
241         /// Construct an button group.
242         /// </summary>
243         /// <param name="view">root view</param>
244         [EditorBrowsable(EditorBrowsableState.Never)]
245         public ButtonGroup(View view) : base()
246         {
247             itemGroup = new List<Button>();
248             if ((root = view) == null)
249             {
250                 throw new Exception("Root view is null.");
251             }
252         }
253
254         /// <summary>
255         /// Get group item number.
256         /// </summary>
257         [EditorBrowsable(EditorBrowsableState.Never)]
258         public int Count
259         {
260             get
261             {
262                 return itemGroup.Count;
263             }
264         }
265
266         /// <summary>
267         /// Check if the button exists.
268         /// </summary>
269         /// <param name="bt">button item</param>
270         /// <returns></returns>
271         [EditorBrowsable(EditorBrowsableState.Never)]
272         public bool Contains(Button bt)
273         {
274             return itemGroup.Contains(bt);
275         }
276
277         /// <summary>
278         /// Get button item index.
279         /// </summary>
280         /// <param name="bt">button item</param>
281         /// <returns></returns>
282         [EditorBrowsable(EditorBrowsableState.Never)]
283         public int GetIndex(Button bt)
284         {
285             return itemGroup.IndexOf(bt);
286         }
287
288         /// <summary>
289         /// Get item by index.
290         /// </summary>
291         /// <param name="index">item index.</param>
292         /// <returns></returns>
293         [EditorBrowsable(EditorBrowsableState.Never)]
294         public Button GetItem(int index)
295         {
296             if (index >= Count || index < 0)
297             {
298                 throw new Exception("button index error");
299             }
300             return itemGroup[index];
301         }
302
303         /// <summary>
304         /// Add item into group.
305         /// </summary>
306         /// <param name="bt">button item.</param>
307         [EditorBrowsable(EditorBrowsableState.Never)]
308         public void AddItem(Button bt)
309         {
310             if (itemGroup.Contains(bt))
311             {
312                 return;
313             }
314             itemGroup.Add(bt);
315             root.Add(bt);
316         }
317
318         /// <summary>
319         /// The item to removed.
320         /// </summary>
321         /// <param name="bt">button item.</param>
322         [EditorBrowsable(EditorBrowsableState.Never)]
323         public void RemoveItem(Button bt)
324         {
325             if (null == bt) return;
326             if (!itemGroup.Contains(bt))
327             {
328                 return;
329             }
330             itemGroup.Remove(bt);
331             root.Remove(bt);
332             bt.Dispose();
333         }
334
335         /// <summary>
336         /// Remove item by index.
337         /// </summary>
338         /// <param name="index">item index.</param>
339         [EditorBrowsable(EditorBrowsableState.Never)]
340         public void RemoveItem(int index)
341         {
342             if (index >= Count || index < 0)
343             {
344                 throw new Exception("button index error");
345             }
346             Button bt = itemGroup[index];
347             itemGroup.Remove(bt);
348             root.Remove(bt);
349             bt.Dispose();
350         }
351
352         /// <summary>
353         /// Remove all items.
354         /// </summary>
355         [EditorBrowsable(EditorBrowsableState.Never)]
356         public void RemoveAll()
357         {
358             foreach (Button bt in itemGroup)
359             {
360                 root.Remove(bt);
361                 bt.Dispose();
362             }
363             itemGroup.Clear();
364         }
365
366         /// <summary>
367         /// Update button by style.
368         /// </summary>
369         /// <param name="btStyle">button style.</param>
370         [EditorBrowsable(EditorBrowsableState.Never)]
371         public void UpdateButton(ButtonStyle btStyle)
372         {
373             if (Count == 0) return;
374
375             int buttonWidth = (int)root.Size.Width / Count;
376             int buttonHeight = (int)itemheight;
377             foreach (Button btnTemp in itemGroup)
378             {
379                 btnTemp.Size = new Size(buttonWidth, buttonHeight);
380             }
381
382             int pos = 0;
383             if (root.LayoutDirection == ViewLayoutDirectionType.RTL)
384             {
385                 for (int i = Count - 1; i >= 0; i--)
386                 {
387                     itemGroup[i].Style.PositionX = pos;
388                     pos += (int)(itemGroup[i].Size.Width);
389                 }
390             }
391             else
392             {
393                 for (int i = 0; i < Count; i++)
394                 {
395                     itemGroup[i].Style.PositionX = pos;
396                     pos += (int)(itemGroup[i].Size.Width);
397                 }
398             }
399
400             if (btStyle == null || btStyle.Text == null || btStyle.Text.TextColor == null) return;
401             ItemTextColor = btStyle.Text.TextColor.All;
402         }
403
404         /// <summary>
405         /// Common height for all of the Items
406         /// </summary>
407         /// <since_tizen> 3 </since_tizen>
408         [EditorBrowsable(EditorBrowsableState.Never)]
409         public float Itemheight
410         {
411             get
412             {
413                 return (float)GetValue(ItemheightProperty);
414             }
415             set
416             {
417                 SetValue(ItemheightProperty, value);
418             }
419         }
420
421         /// <summary>
422         /// Get or set point size of item.
423         /// </summary>
424         [EditorBrowsable(EditorBrowsableState.Never)]
425         public float ItemPointSize
426         {
427             get
428             {
429                 return (float)GetValue(ItemPointSizeProperty);
430             }
431             set
432             {
433                 SetValue(ItemPointSizeProperty, value);
434             }
435         }
436
437         /// <summary>
438         /// Get or set font family of item.
439         /// </summary>
440         [EditorBrowsable(EditorBrowsableState.Never)]
441         public string ItemFontFamily
442         {
443             get
444             {
445                 return (string)GetValue(ItemFontFamilyProperty);
446             }
447             set
448             {
449                 SetValue(ItemFontFamilyProperty, value);
450             }
451         }
452
453         /// <summary>
454         /// Get or set text color of item.
455         /// </summary>
456         [EditorBrowsable(EditorBrowsableState.Never)]
457         public Color ItemTextColor
458         {
459             get
460             {
461                 return (Color)GetValue(ItemTextColorProperty);
462             }
463             set
464             {
465                 SetValue(ItemTextColorProperty, value);
466             }
467         }
468
469         /// <summary>
470         /// Get or set text alignment of item.
471         /// </summary>
472         [EditorBrowsable(EditorBrowsableState.Never)]
473         public HorizontalAlignment ItemTextAlignment
474         {
475             get
476             {
477                 return (HorizontalAlignment)GetValue(ItemTextAlignmentProperty);
478             }
479             set
480             {
481                 SetValue(ItemTextAlignmentProperty, value);
482             }
483         }
484
485         /// <summary>
486         /// Get or set background color of item.
487         /// </summary>
488         [EditorBrowsable(EditorBrowsableState.Never)]
489         public Selector<Color> OverLayBackgroundColorSelector
490         {
491             get
492             {
493                 return (Selector<Color>)GetValue(OverLayBackgroundColorSelectorProperty);
494             }
495             set
496             {
497                 SetValue(OverLayBackgroundColorSelectorProperty, value);
498             }
499         }
500
501         /// <summary>
502         /// The mutually exclusive with "backgroundColor" and "background" type Map.
503         /// </summary>
504         /// <since_tizen> 3 </since_tizen>
505         [EditorBrowsable(EditorBrowsableState.Never)]
506         public string ItemBackgroundImageUrl
507         {
508             get
509             {
510                 return (string)GetValue(ItemBackgroundImageUrlProperty);
511             }
512             set
513             {
514                 SetValue(ItemBackgroundImageUrlProperty, value);
515             }
516         }
517
518         /// <summary>
519         /// Get or set background border of item.
520         /// </summary>
521         [EditorBrowsable(EditorBrowsableState.Never)]
522         public Rectangle ItemBackgroundBorder
523         {
524             get
525             {
526                 return (Rectangle)GetValue(ItemBackgroundBorderProperty);
527             }
528             set
529             {
530                 SetValue(ItemBackgroundBorderProperty, value);
531             }
532         }
533
534         /// <summary>
535         /// Get or set shadow resource of item.
536         /// </summary>
537         [EditorBrowsable(EditorBrowsableState.Never)]
538         public string ItemShadowUrl
539         {
540             get
541             {
542                 return (string)GetValue(ItemShadowUrlProperty);
543             }
544             set
545             {
546                 SetValue(ItemShadowUrlProperty, value);
547             }
548         }
549
550         /// <summary>
551         /// Get or set shadow border of item.
552         /// </summary>
553         [EditorBrowsable(EditorBrowsableState.Never)]
554         public Rectangle ItemShadowBorder
555         {
556             get
557             {
558                 return (Rectangle)GetValue(ItemShadowBorderProperty);
559             }
560             set
561             {
562                 SetValue(ItemShadowBorderProperty, value);
563             }
564         }
565
566         /// <summary>
567         /// Dispose.
568         /// </summary>
569         [EditorBrowsable(EditorBrowsableState.Never)]
570         public void Dispose()
571         {
572             if (disposed) return;
573             if (itemGroup != null)
574             {
575                 RemoveAll();
576                 itemGroup = null;
577             }
578             disposed = true;
579         }
580
581         private List<Button> itemGroup;
582         private View root = null;
583         private bool disposed = false;
584         private float itemheight;
585         private float itemPointSize;
586         private string itemFontFamily;
587         private Color itemTextColor = new Color();
588         private HorizontalAlignment itemTextAlignment;
589         private Selector<Color> overLayBackgroundColorSelector = new Selector<Color>();
590         private string itemBackgroundImageUrl;
591         private Rectangle itemBackgroundBorder = new Rectangle();
592         private string itemShadowUrl;
593         private Rectangle itemShadowBorder = new Rectangle();
594     }
595 }