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