[NUI] Add constructor with style instance
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / TabContent.cs
1 /*
2  * Copyright(c) 2021 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.Collections.Generic;
19 using Tizen.NUI.BaseComponents;
20 using System.ComponentModel;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// TabContent is a class which contains a set of Views and has one of them selected.
26     /// </summary>
27     /// <since_tizen> 9 </since_tizen>
28     public class TabContent : Control
29     {
30         private IList<View> views;
31
32         private void Initialize()
33         {
34             SelectedIndex = -1;
35             views = new List<View>();
36         }
37
38         /// <summary>
39         /// Creates a new instance of TabContent.
40         /// </summary>
41         /// <since_tizen> 9 </since_tizen>
42         public TabContent()
43         {
44             Initialize();
45         }
46
47         /// <summary>
48         /// Creates a new instance of a TabContent with style.
49         /// </summary>
50         /// <param name="style">A style applied to the newly created TabContent.</param>
51         [EditorBrowsable(EditorBrowsableState.Never)]
52         public TabContent(ControlStyle style) : base(style)
53         {
54             Initialize();
55         }
56
57         /// <summary>
58         /// The index of the selected view.
59         /// The indices of views in TabContent are basically the order of adding to TabContent by <see cref="TabView.AddTab"/>.
60         /// So a view's index in TabContent can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
61         /// </summary>
62         [EditorBrowsable(EditorBrowsableState.Never)]
63         protected int SelectedIndex { get; set; }
64
65         /// <summary>
66         /// Gets the count of views.
67         /// </summary>
68         /// <since_tizen> 9 </since_tizen>
69         public int ViewCount => views.Count;
70
71         /// <inheritdoc/>
72         [EditorBrowsable(EditorBrowsableState.Never)]
73         public override void OnInitialize()
74         {
75             base.OnInitialize();
76
77             AccessibilityRole = Role.PageTabList;
78         }
79
80         /// <summary>
81         /// Adds a view to TabContent.
82         /// </summary>
83         /// <param name="view">A view to be added to TabContent.</param>
84         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
85         [EditorBrowsable(EditorBrowsableState.Never)]
86         protected internal void AddView(View view)
87         {
88             if (view == null)
89             {
90                 throw new ArgumentNullException(nameof(view), "view should not be null.");
91             }
92
93             views.Add(view);
94
95             if (SelectedIndex == -1)
96             {
97                 Select(0);
98             }
99         }
100
101         /// <summary>
102         /// Removes a view from TabContent.
103         /// </summary>
104         /// <param name="view">A view to be removed from TabContent.</param>
105         /// <exception cref="ArgumentNullException">Thrown when the argument view is null.</exception>
106         /// <exception cref="ArgumentException">Thrown when the argument view does not exist in TabContent.</exception>
107         [EditorBrowsable(EditorBrowsableState.Never)]
108         protected internal void RemoveView(View view)
109         {
110             if (view == null)
111             {
112                 throw new ArgumentNullException(nameof(view), "view should not be null.");
113             }
114
115             if (views.Contains(view) == false)
116             {
117                 throw new ArgumentException("view does not exist in TabContent.", nameof(view));
118             }
119
120             int index = views.IndexOf(view);
121
122             views.Remove(view);
123
124             if (index == SelectedIndex)
125             {
126                 if (views.Count == 0)
127                 {
128                     Select(-1);
129                 }
130                 else if (SelectedIndex == views.Count)
131                 {
132                     Select(SelectedIndex - 1);
133                 }
134             }
135         }
136
137         /// <summary>
138         /// Selects a view at the specified index of TabContent.
139         /// The indices of views in TabContent are basically the order of adding to TabContent by <see cref="TabView.AddTab"/>.
140         /// So a view's index in TabContent can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
141         /// </summary>
142         /// <param name="index">The index of a view in TabContent where the view will be selected.</param>
143         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than -1, or greater than or equal to the number of views.</exception>
144         [EditorBrowsable(EditorBrowsableState.Never)]
145         protected internal void Select(int index)
146         {
147             if ((index < -1) || (index >= views.Count))
148             {
149                 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to -1, and less than the number of views.");
150             }
151
152             if (SelectedIndex != -1)
153             {
154                 Remove(views[SelectedIndex]);
155             }
156
157             if (index != -1)
158             {
159                 Add(views[index]);
160             }
161
162             SelectedIndex = index;
163         }
164
165         /// <summary>
166         /// Gets the view at the specified index of TabContent.
167         /// The indices of views in TabContent are basically the order of adding to TabContent by <see cref="TabView.AddTab"/>.
168         /// So a view's index in TabContent can be changed whenever <see cref="TabView.AddTab"/> or <see cref="TabView.RemoveTab"/> is called.
169         /// </summary>
170         /// <param name="index">The index of a view in TabContent where the specified view exists.</param>
171         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of views.</exception>
172         /// <since_tizen> 9 </since_tizen>
173         public View GetView(int index)
174         {
175             if ((index < 0) || (index >= views.Count))
176             {
177                 throw new ArgumentOutOfRangeException(nameof(index), "index should not be greater than or equal to 0, and less than the number of views.");
178             }
179
180             return views[index];
181         }
182
183         /// <inheritdoc/>
184         [EditorBrowsable(EditorBrowsableState.Never)]
185         protected override void Dispose(DisposeTypes type)
186         {
187             if (disposed)
188             {
189                 return;
190             }
191
192             if (type == DisposeTypes.Explicit)
193             {
194                 if (views != null)
195                 {
196                     foreach (View view in views)
197                     {
198                         Utility.Dispose(view);
199                     }
200
201                     views = null;
202                 }
203             }
204
205             base.Dispose(type);
206         }
207     }
208 }