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