a403af83ec2c344daa78f9607900f6ef440632a7
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Pagination.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.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// Pagination shows the number of pages available and the currently active page.
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class Pagination: Control
31     {
32         private VisualView container;
33
34         private List<ImageVisual> indicatorList = new List<ImageVisual>();
35
36         private int indicatorCount = 0;
37         private int selectedIndex = -1;
38
39         static Pagination() { }
40
41         /// <summary>
42         /// Creates a new instance of a Pagination.
43         /// </summary>
44         /// <since_tizen> 6 </since_tizen>
45             /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
46             [EditorBrowsable(EditorBrowsableState.Never)]
47         public Pagination() : base()
48         {
49             Initialize();
50         }
51
52         /// <summary>
53         /// Creates a new instance of a Pagination using style.
54         /// </summary>
55         /// <since_tizen> 6 </since_tizen>
56         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
57         [EditorBrowsable(EditorBrowsableState.Never)]
58         public Pagination(string style) : base(style)
59         {
60             Initialize();
61         }
62
63         /// <summary>
64         /// Creates a new instance of a Pagination using style.
65         /// </summary>
66         /// <since_tizen> 6 </since_tizen>
67         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
68         [EditorBrowsable(EditorBrowsableState.Never)]
69         public Pagination(PaginationStyle paginationStyle) : base(paginationStyle)
70         {
71             Initialize();
72         }
73
74         /// <summary>
75         /// Get style of pagination.
76         /// </summary>
77         /// This will be public opened in tizen_next after ACR done. Before ACR, need to be hidden as inhouse API.
78         [EditorBrowsable(EditorBrowsableState.Never)]
79         public new PaginationStyle Style => ViewStyle as PaginationStyle;
80
81         /// <summary>
82         /// Gets or sets the size of the indicator.
83         /// </summary>
84         /// <since_tizen> 6 </since_tizen>
85         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
86         [EditorBrowsable(EditorBrowsableState.Never)]
87         public Size IndicatorSize
88         {
89             get
90             {
91                 return Style?.IndicatorSize;
92             }
93             set
94             {
95                 if (value == null || Style == null)
96                 {
97                     return;
98                 }
99                 Style.IndicatorSize = value;
100                 UpdateVisual();
101             }
102         }
103
104         /// <summary>
105         /// Gets or sets the background resource of indicator.
106         /// </summary>
107         /// <since_tizen> 6 </since_tizen>
108         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
109         [EditorBrowsable(EditorBrowsableState.Never)]
110         public Selector<string> IndicatorImageURL
111         {
112             get
113             {
114                 return Style?.IndicatorImageURL;
115             }
116             set
117             {
118                 if (value == null || Style == null)
119                 {
120                     return;
121                 }
122                 Style.IndicatorImageURL = value;
123                 UpdateVisual();
124             }
125         }
126
127         /// <summary>
128         /// Gets or sets the space of the indicator.
129         /// </summary>
130         /// <since_tizen> 6 </since_tizen>
131         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
132         [EditorBrowsable(EditorBrowsableState.Never)]
133         public int IndicatorSpacing
134         {
135             get
136             {
137                 return (int)Style?.IndicatorSpacing;
138             }
139             set
140             {
141                 Style.IndicatorSpacing = value;
142                 UpdateVisual();
143             }
144         }
145
146
147         /// <summary>
148         /// Gets or sets the count of the pages/indicators.
149         /// </summary>
150         /// <since_tizen> 6 </since_tizen>
151         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
152         [EditorBrowsable(EditorBrowsableState.Never)]
153         public int IndicatorCount
154         {
155             get
156             {
157                 return indicatorCount;
158             }
159             set
160             {
161                 if (indicatorCount == value || indicatorCount < 0)
162                 {
163                     return;
164                 }
165                 if (indicatorCount < value)
166                 {
167                     for (int i = indicatorCount; i < value; i++)
168                     {
169                         CreateIndicator();
170                     }
171                 }
172                 else
173                 {
174                     for (int i = value; i < indicatorCount; i++)
175                     {
176                         ImageVisual indicator = indicatorList[i];
177                         container.RemoveVisual("Indicator" + i);
178                     }
179                     indicatorList.RemoveRange(value, indicatorCount - value);
180                     //if(value <= 0)
181                     //{
182                     //    container.RemoveVisual("SelectIndicator");
183                     //}
184                     //else
185                     if (selectedIndex >= value)
186                     {
187                         selectedIndex = 0;
188                         SelectIn(indicatorList[selectedIndex]);
189                     }
190                 }
191                 indicatorCount = value;
192
193                 UpdateContainer();
194             }
195         }
196
197         /// <summary>
198         /// Gets or sets the index of the select indicator.
199         /// </summary>
200         /// <since_tizen> 6 </since_tizen>
201         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
202         [EditorBrowsable(EditorBrowsableState.Never)]
203         public int SelectedIndex
204         {
205             get
206             {
207                 return selectedIndex;
208             }
209             set
210             {
211                 if (selectedIndex == value)
212                 {
213                     return;
214                 }
215                 if (selectedIndex >= 0 && selectedIndex < indicatorCount)
216                 {
217                     SelectOut(indicatorList[selectedIndex]);
218                 }
219                 selectedIndex = value;
220                 if (selectedIndex >= 0 && selectedIndex < indicatorCount)
221                 {
222                     SelectIn(indicatorList[selectedIndex]);
223                 }
224             }
225         }
226
227         /// <summary>
228         /// Retrieves the position of a indicator by index.
229         /// </summary>
230         /// <param name="index">Indicator index</param>
231         /// <since_tizen> 6 </since_tizen>
232         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
233         [EditorBrowsable(EditorBrowsableState.Never)]
234         public Position GetIndicatorPosition(int index)
235         {
236             if (index < 0 || index >= indicatorList.Count)
237             {
238                 return null;
239             }
240             return new Position(indicatorList[index].Position.X + container.PositionX, indicatorList[index].Position.Y + container.PositionY);
241         }
242
243         /// <summary>
244         /// You can override it to do your select out operation.
245         /// </summary>
246         /// <param name="selectOutIndicator">The indicator will be selected out</param>
247         /// <since_tizen> 6 </since_tizen>
248         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
249         [EditorBrowsable(EditorBrowsableState.Never)]
250         protected virtual void SelectOut(VisualMap selectOutIndicator)
251         {
252             if (!(selectOutIndicator is ImageVisual visual)) return;
253             visual.URL = Style?.IndicatorImageURL.Normal;
254             visual.Opacity = 0.5f;
255         }
256
257         /// <summary>
258         /// You can override it to do your select in operation.
259         /// </summary>
260         /// <param name="selectInIndicator">The indicator will be selected in</param>
261         /// <since_tizen> 6 </since_tizen>
262         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
263         [EditorBrowsable(EditorBrowsableState.Never)]
264         protected virtual void SelectIn(VisualMap selectInIndicator)
265         {
266             if (!(selectInIndicator is ImageVisual visual)) return;
267             visual.URL = Style?.IndicatorImageURL.Selected;
268             visual.Opacity = 1.0f;
269         }
270
271         /// <summary>
272         /// you can override it to create your own default style.
273         /// </summary>
274         /// <since_tizen> 6 </since_tizen>
275         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
276         [EditorBrowsable(EditorBrowsableState.Never)]
277         protected override ViewStyle CreateViewStyle()
278         {
279             return new PaginationStyle();
280         }
281
282         /// <summary>
283         /// you can override it to clean-up your own resources.
284         /// </summary>
285         /// <param name="type">DisposeTypes</param>
286         /// <since_tizen> 6 </since_tizen>
287         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
288         [EditorBrowsable(EditorBrowsableState.Never)]
289         protected override void Dispose(DisposeTypes type)
290         {
291             if (disposed)
292             {
293                 return;
294             }
295
296             if (type == DisposeTypes.Explicit)
297             {
298                 container.RemoveAll();
299                 indicatorList.Clear();
300
301                 this.Remove(container);
302                 container.Dispose();
303                 container = null;
304             }
305
306             base.Dispose(type);
307         }
308
309         private void Initialize()
310         {
311             container = new VisualView()
312             {
313                 Name = "Container",
314                 ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
315                 PivotPoint = Tizen.NUI.PivotPoint.CenterLeft,
316                 PositionUsesPivotPoint = true,
317             };
318             this.Add(container);
319         }
320
321         private void CreateIndicator()
322         {
323             if (Style == null)
324             {
325                 return;
326             }
327             ImageVisual indicator = new ImageVisual
328             {
329                 URL = Style.IndicatorImageURL.Normal,
330                 Size = new Size2D((int)Style.IndicatorSize.Width, (int)Style.IndicatorSize.Height),
331                 Opacity = 0.5f,
332             };
333             indicator.Position = new Position2D((int)(Style.IndicatorSize.Width + Style.IndicatorSpacing) * indicatorList.Count, 0);
334             container.AddVisual("Indicator" + indicatorList.Count, indicator);
335             indicatorList.Add(indicator);
336         }
337
338         private void UpdateContainer()
339         {
340             if (Style == null)
341             {
342                 return;
343             }
344             if (indicatorList.Count > 0)
345             {
346                 container.SizeWidth = (Style.IndicatorSize.Width + Style.IndicatorSpacing) * indicatorList.Count - Style.IndicatorSpacing;
347             }
348             else
349             {
350                 container.SizeWidth = 0;
351             }
352             container.SizeHeight = Style.IndicatorSize.Height;
353             container.PositionX = (int)((this.SizeWidth - container.SizeWidth) / 2);
354         }
355
356         private void UpdateVisual()
357         {
358             if (null == Style.IndicatorSize) return;
359             if (null == Style.IndicatorImageURL) return;
360             if (indicatorCount < 0) return;
361
362             for (int i = 0; i < indicatorList.Count; i++)
363             {
364                 ImageVisual indicator = indicatorList[i];
365                 indicator.URL = Style.IndicatorImageURL.Normal;
366                 indicator.Size = new Size2D((int)Style.IndicatorSize.Width, (int)Style.IndicatorSize.Height);
367                 indicator.Position = new Position2D((int)(Style.IndicatorSize.Width + Style.IndicatorSpacing) * i, 0);
368             }
369         }
370     }
371 }