0aa9d83afc0da9b1c27b82f6ed09cddfc4d2b26e
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / TabView.cs
1 /*
2  * Copyright(c) 2020 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 Tizen.NUI.BaseComponents;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// TabView is a class which contains a TabBar and TabContent and
25     /// selects a view from the TabContent according to the selected TabButton in the TabBar.
26     /// </summary>
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public class TabView : Control
29     {
30         private TabBar tabBar = null;
31
32         private TabContent content = null;
33
34         /// <summary>
35         /// Creates a new instance of TabView.
36         /// </summary>
37         [EditorBrowsable(EditorBrowsableState.Never)]
38         public TabView()
39         {
40             Layout = new LinearLayout() { LinearOrientation = LinearLayout.Orientation.Vertical };
41             WidthSpecification = LayoutParamPolicies.MatchParent;
42             HeightSpecification = LayoutParamPolicies.MatchParent;
43
44             InitTabBar();
45             InitContent();
46         }
47
48         private void InitTabBar()
49         {
50             if (tabBar != null)
51             {
52                 return;
53             }
54
55             tabBar = new TabBar();
56             tabBar.TabButtonSelected += tabButtonSelectedHandler;
57
58             Add(tabBar);
59         }
60
61         private void InitContent()
62         {
63             if (content != null)
64             {
65                 return;
66             }
67
68             content = new TabContent();
69             content.WidthSpecification = LayoutParamPolicies.MatchParent;
70             content.HeightSpecification = LayoutParamPolicies.MatchParent;
71
72             Add(content);
73         }
74
75         private void tabButtonSelectedHandler(object sender, TabButtonSelectedEventArgs args)
76         {
77             if ((content != null) && (content.ViewCount > args.Index))
78             {
79                 content.Select(args.Index);
80             }
81         }
82
83         /// <summary>
84         /// Gets TabBar of TabView.
85         /// </summary>
86         [EditorBrowsable(EditorBrowsableState.Never)]
87         public TabBar TabBar
88         {
89             get
90             {
91                 return tabBar;
92             }
93         }
94
95         /// <summary>
96         /// Gets TabContent of TabView.
97         /// </summary>
98         [EditorBrowsable(EditorBrowsableState.Never)]
99         public TabContent Content
100         {
101             get
102             {
103                 return content;
104             }
105         }
106
107         /// <summary>
108         /// Adds a tab with tab button and content view.
109         /// </summary>
110         /// <param name="tabButton">A tab button to be added.</param>
111         /// <param name="view">A content view to be added.</param>
112         [EditorBrowsable(EditorBrowsableState.Never)]
113         public void AddTab(TabButton tabButton, View view)
114         {
115             if (TabBar != null)
116             {
117                 TabBar.AddTabButton(tabButton);
118             }
119
120             if (Content != null)
121             {
122                 Content.AddView(view);
123             }
124         }
125
126         /// <summary>
127         /// Removes a tab at the specified index of TabView.
128         /// </summary>
129         /// <param name="index">The index of TabView where a tab will be removed.</param>
130         /// <exception cref="ArgumentOutOfRangeException">Thrown when the index is less than 0, or greater than or equal to the number of tabs.</exception>
131         [EditorBrowsable(EditorBrowsableState.Never)]
132         public void RemoveTab(int index)
133         {
134             var tabButton = TabBar.GetTabButton(index);
135             if (tabButton != null)
136             {
137                 TabBar.RemoveTabButton(tabButton);
138             }
139
140             var view = Content.GetView(index);
141             if (view != null)
142             {
143                 Content.RemoveView(view);
144             }
145         }
146
147         /// <summary>
148         /// Dispose TabView and all children on it.
149         /// </summary>
150         /// <param name="type">Dispose type.</param>
151         [EditorBrowsable(EditorBrowsableState.Never)]
152         protected override void Dispose(DisposeTypes type)
153         {
154             if (disposed)
155             {
156                 return;
157             }
158
159             if (type == DisposeTypes.Explicit)
160             {
161                 if (tabBar != null)
162                 {
163                     tabBar.TabButtonSelected -= tabButtonSelectedHandler;
164                     Utility.Dispose(tabBar);
165                 }
166
167                 if (content != null)
168                 {
169                     Utility.Dispose(content);
170                 }
171             }
172
173             base.Dispose(type);
174         }
175     }
176 }