Revert " modify license, permission and remove ^M char"
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_CustomListImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19 * @file         FUiCtrl_CustomListImpl.cpp
20 * @brief        This file contains implementation of _CustomListImpl class
21 */
22
23 #include <FUiCtrlCustomListItem.h>
24 #include <FUiCtrlCustomListItemFormat.h>
25 #include <FBaseSysLog.h>
26 #include "FUi_ResourceSizeInfo.h"
27 #include "FUi_UiBuilder.h"
28 #include "FUiCtrl_CustomListItemFormatImpl.h"
29 #include "FUiCtrl_CustomListItemImpl.h"
30 #include "FUiCtrl_CustomListImpl.h"
31 #include "FUiCtrl_ListListener.h"
32 #include "FUiCtrl_ListItemBaseImpl.h"
33 #include "FUi_CoordinateSystemUtils.h"
34 #include "FUiCtrl_TableViewItemProvider.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Collection;
38 using namespace Tizen::Graphics;
39
40 namespace Tizen { namespace Ui { namespace Controls
41 {
42
43 extern const int INVALID_INDEX;
44
45 _CustomListItemDataProvider::_CustomListItemDataProvider(_CustomListImpl* pList)
46         : __pList(pList)
47 {
48 }
49
50 _CustomListItemDataProvider::~_CustomListItemDataProvider(void)
51 {
52 }
53
54 int
55 _CustomListItemDataProvider::GetItemCount(void)
56 {
57         SysTryReturn(NID_UI_CTRL, __pList, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Item provider not constructed properly.");
58         return __pList->GetItemCount();
59 }
60
61 TableViewItem*
62 _CustomListItemDataProvider::CreateItem(int index, int itemWidth)
63 {
64         SysTryReturn(NID_UI_CTRL, __pList, null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Item provider not constructed properly.");
65
66         CustomListItem* pCustomListItem = __pList->GetCustomListItemAt(-1, index);
67         SysTryReturn(NID_UI_CTRL, pCustomListItem, null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get an item at index(%d).", index);
68
69         _CustomListItemImpl* pCustomListItemImpl = null;
70         pCustomListItemImpl = pCustomListItem->__pCustomListItemImpl;
71         SysTryReturn(NID_UI_CTRL, pCustomListItemImpl, null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. CustomListItem not yet constructed.");
72
73         _TableViewItemParams tableViewItemParams;
74         tableViewItemParams.pItem = pCustomListItem;
75         tableViewItemParams.width = itemWidth;
76         tableViewItemParams.itemId = pCustomListItemImpl->itemId;
77         tableViewItemParams.groupIndex = -1;
78         tableViewItemParams.itemIndex = index;
79         tableViewItemParams.isDividerEnabled = __pList->_isDividerEnabled;
80         tableViewItemParams.pCheckBitmaps = __pList->_pCheckBitmaps;
81         tableViewItemParams.annexStyle = __pList->_annexStyle;
82
83         result r = _CustomListItemImpl::CreateTableViewItem(tableViewItemParams);
84         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
85
86         pCustomListItemImpl->__pTableViewItemData->AddTouchEventListener(*__pList);
87
88         return pCustomListItemImpl->__pTableViewItemData;
89 }
90
91 bool
92 _CustomListItemDataProvider::DeleteItem(const int itemIndex, TableViewItem* pItem)
93 {
94         delete pItem;
95
96         if (__pList->__directDelete == false)
97         {
98                 CustomListItem* pCustomListItem = __pList->GetCustomListItemAt(-1, itemIndex);
99                 pCustomListItem->__pCustomListItemImpl->__pTableViewItemData = null;
100         }
101
102         return true;
103 }
104
105 void
106 _CustomListItemDataProvider::UpdateItem(int itemIndex, TableViewItem* pItem)
107 {
108     //return true;
109 }
110
111 int
112 _CustomListItemDataProvider::GetDefaultItemHeight(void)
113 {
114         SysTryReturn(NID_UI_CTRL, __pList, 0, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Item provider not constructed properly.");
115
116         CustomListItem* pCustomListItem = __pList->GetCustomListItemAt(-1, 0);
117         SysTryReturn(NID_UI_CTRL, pCustomListItem, 0, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get first item");
118
119         _CustomListItemImpl* pCustomListItemImpl = null;
120         pCustomListItemImpl = pCustomListItem->__pCustomListItemImpl;
121         SysTryReturn(NID_UI_CTRL, pCustomListItemImpl, 0, E_SYSTEM, "[E_SYSTEM] A system error has occurred. CustomListItem not yet constructed.");
122
123         return pCustomListItemImpl->height;
124 }
125
126 _CustomListImpl*
127 _CustomListImpl::GetInstance(CustomList& customList)
128 {
129         return static_cast<_CustomListImpl*>(customList._pControlImpl);
130 }
131
132 const _CustomListImpl*
133 _CustomListImpl::GetInstance(const CustomList& customList)
134 {
135         return static_cast<const _CustomListImpl*>(customList._pControlImpl);
136 }
137
138 _CustomListImpl::_CustomListImpl(Control* pList, _TableView* pCore)
139         : _ListBaseImpl(pList, pCore)
140         , __pItemProvider(null)
141         , __directDelete(false)
142
143 {
144 }
145
146 _CustomListImpl::~_CustomListImpl(void)
147 {
148         RemoveAllFromItemsList();
149
150         __itemListenersList.RemoveAll(true);
151
152         delete __pItemProvider;
153 }
154
155 const char*
156 _CustomListImpl::GetPublicClassName(void) const
157 {
158         return "CustomList";
159 }
160
161 const CustomList&
162 _CustomListImpl::GetPublic(void) const
163 {
164         return static_cast<const CustomList&>(_ControlImpl::GetPublic());
165 }
166
167 CustomList&
168 _CustomListImpl::GetPublic(void)
169 {
170         return static_cast<CustomList&>(_ControlImpl::GetPublic());
171 }
172
173 const _TableView&
174 _CustomListImpl::GetCore(void) const
175 {
176         return static_cast<const _TableView&>(_ControlImpl::GetCore());
177 }
178
179 _TableView&
180 _CustomListImpl::GetCore(void)
181 {
182         return static_cast<_TableView&>(_ControlImpl::GetCore());
183 }
184
185 _CustomListImpl*
186 _CustomListImpl::CreateCustomListImplN(CustomList* pControl, const Rectangle& bounds, bool itemDivider)
187 {
188         result r = E_SUCCESS;
189
190         r = GET_SIZE_INFO(CustomList).CheckInitialSizeValidF(FloatDimension(bounds.width, bounds.height), _CONTROL_ORIENTATION_PORTRAIT);
191         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), null, r, "[%s] Propagating.", GetErrorMessage(r));
192
193         _TableView* pCore = null;
194         _CustomListImpl* pImpl = null;
195
196         pCore = _TableView::CreateTableViewN(TABLE_VIEW_STYLE_SIMPLE, itemDivider, TABLE_VIEW_SCROLL_BAR_STYLE_FADE_OUT);
197
198         r = GetLastResult();
199         SysTryReturn(NID_UI_CTRL, pCore, null, r, "[%s] Propagating.", GetErrorMessage(r));
200
201         pImpl = new (std::nothrow) _CustomListImpl(pControl, pCore);
202         SysTryCatch(NID_UI_CTRL, pImpl, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
203
204         ClearLastResult();
205
206         return pImpl;
207
208 CATCH:
209         delete pCore;
210         return null;
211 }
212
213 result
214 _CustomListImpl::InitializeBounds(const Rectangle& rect)
215 {
216         result r = E_SUCCESS;
217
218         r = InitializeBoundsPropertiesF(GET_SIZE_INFO(CustomList), _CoordinateSystemUtils::ConvertToFloat(rect), GetCore().GetOrientation());
219         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
220
221         return E_SUCCESS;
222 }
223
224 result
225 _CustomListImpl::AddCustomItemEventListener(const ICustomItemEventListener& listener)
226 {
227         _ListListener* pListenersList = new (std::nothrow) _ListListener();
228         SysTryReturn(NID_UI_CTRL, pListenersList, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
229
230         pListenersList->pListener = const_cast<ICustomItemEventListener*>(&listener);
231
232         __itemListenersList.Add(*pListenersList);
233
234         return E_SUCCESS;
235 }
236
237 result
238 _CustomListImpl::RemoveCustomItemEventListener(const ICustomItemEventListener& listener)
239 {
240         _ListListener* pListenersList = null;
241         result r = E_SYSTEM;
242
243         int count = __itemListenersList.GetCount();
244
245         for (int itemListenerCount = 0; itemListenerCount < count; itemListenerCount++)
246         {
247                 pListenersList = dynamic_cast<_ListListener*>(__itemListenersList.GetAt(itemListenerCount));
248
249                 if ((pListenersList != null) && (pListenersList->pListener == &listener))
250                 {
251                         r = E_SUCCESS;
252                         __itemListenersList.RemoveAt(itemListenerCount, true);
253                         break;
254                 }
255         }
256
257         return r;
258 }
259
260 result
261 _CustomListImpl::Initialize(void)
262 {
263         result r = E_SUCCESS;
264
265         __pItemProvider = new (std::nothrow) _CustomListItemDataProvider(this);
266         SysTryReturnResult(NID_UI_CTRL, __pItemProvider != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
267
268         TableViewStyle style = GetCore().GetTableViewStyle();
269         SysTryReturn(NID_UI_CTRL, style == TABLE_VIEW_STYLE_SIMPLE, E_INVALID_OPERATION, E_INVALID_OPERATION, "[E_INVALID_OPERATION] The style of TableView is not TABLE_VIEW_STYLE_SIMPLE");
270
271         _TableViewItemProvider* pItemProvider = _TableViewItemProvider::CreateTableViewItemProviderN(style);
272         SysTryReturn(NID_UI_CTRL, pItemProvider != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
273
274         r = pItemProvider->SetSimpleStyleItemProvider(__pItemProvider);
275
276         if (r != E_SUCCESS)
277         {
278                 delete pItemProvider;
279                 SysTryReturn(NID_UI_CTRL, false, r, r, "[%s] Propagating.", GetErrorMessage(r));
280         }
281
282         r = GetCore().SetItemProvider(pItemProvider);
283         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
284
285         r = GetCore().AddTableViewItemEventListener(*this);
286         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), r, r, "[%s] Propagating.", GetErrorMessage(r));
287
288         return r;
289 }
290
291 result
292 _CustomListImpl::AddItem(const CustomListItem& item, int itemId)
293 {
294         return InsertItemAt(GetItemCount(), item, itemId);
295 }
296
297 result
298 _CustomListImpl::InsertItemAt(int index, const CustomListItem& item, int itemId)
299 {
300         result r = E_SUCCESS;
301
302         SysTryReturnResult(NID_UI_CTRL, (index >= 0 && index <= GetItemCount()),
303                         E_INVALID_ARG, "The index %d is invalid.", index);
304
305         item.__pCustomListItemImpl->itemId = itemId;
306
307         r = InsertIntoItemsList(item, -1, index);
308         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to insert an item.");
309
310         if (GetCore().GetFirstDrawnFlag() == false)
311         {
312                 r = GetCore().RefreshTableView(0, index, TABLE_VIEW_REFRESH_TYPE_ITEM_ADD);
313                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , E_SYSTEM, "[%s] Propagating.",GetErrorMessage(r));
314         }
315
316         return r;
317
318 CATCH:
319         RemoveFromItemsList(-1, index);
320         return E_SYSTEM;
321 }
322
323 result
324 _CustomListImpl::SetItemAt(int index, const CustomListItem& item, int itemId)
325 {
326         SysTryReturnResult(NID_UI_CTRL, (index >= 0 && index < GetItemCount()),
327                         E_INVALID_ARG, "The index %d is invalid.", index);
328
329         SysTryReturnResult(NID_UI_CTRL, (IsItemNew(item) == true), E_SYSTEM, "A system error has occurred. The item already exists.");
330
331         result r = E_SUCCESS;
332
333         item.__pCustomListItemImpl->__pTableViewItemData = GetTableViewItemAt(-1, index);
334
335         _CheckElementModel* pOldCheckElement = GetCheckElementAt(-1, index);
336         _CheckBoxBitmapType oldCheckStatus = (pOldCheckElement) ? (pOldCheckElement->GetCheckBoxStatus()) : CHECK_BOX_UNCHECKED;
337
338         item.__pCustomListItemImpl->itemId = itemId;
339
340         _CheckElementModel* pCheckElement = item.__pCustomListItemImpl->GetCheckElement();
341
342         if (pCheckElement)
343         {
344                 pCheckElement->SetCheckBoxStatus(oldCheckStatus);
345         }
346
347         r = SetInItemsList(item, -1, index);
348         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to set an item.");
349
350         _TableViewItemUpdateParams updateParams;
351         updateParams.pItem = const_cast<CustomListItem*>(&item);
352         updateParams.isDividerEnabled = _isDividerEnabled;
353         updateParams.pCheckBitmaps = _pCheckBitmaps;
354         updateParams.annexStyle = _annexStyle;
355
356         r = _CustomListItemImpl::UpdateTableViewItem(updateParams);
357         SysTryReturn(NID_UI_CTRL, (r == E_SUCCESS), E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to refresh an item.");
358
359         if (GetCore().GetFirstDrawnFlag() == false)
360         {
361                 r = GetCore().RefreshTableView(0, index, TABLE_VIEW_REFRESH_TYPE_ITEM_MODIFY);
362                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "Propagating.");
363         }
364
365         return r;
366 }
367
368 result
369 _CustomListImpl::RemoveItemAt(int index)
370 {
371         result r = E_SUCCESS;
372         int groupIndex = 0;
373
374         SysTryReturnResult(NID_UI_CTRL, (index >= 0 && index < GetItemCount()),
375                         E_INVALID_ARG, "The index %d is invalid.", index);
376
377         r = RemoveFromItemsList(-1, index);
378         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "A system error has occurred. Failed to remove an item.");
379
380         __directDelete = true;
381
382         if (GetCore().GetFirstDrawnFlag() == false)
383         {
384                 r = GetCore().RefreshTableView(groupIndex, index, TABLE_VIEW_REFRESH_TYPE_ITEM_REMOVE);
385                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "Propagating.");
386         }
387
388         __directDelete = false;
389
390         return r;
391 }
392
393 result
394 _CustomListImpl::RemoveAllItems(void)
395 {
396         result r = E_SUCCESS;
397         result finalResult = E_SUCCESS;
398
399         for (int index = GetItemCount() - 1; index >= 0; index--)
400         {
401                 r = RemoveItemAt(index);
402
403                 if (r != E_SUCCESS)
404                 {
405                         finalResult = r;
406                         SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. Item at %d index not removed successfully.", index);
407                 }
408         }
409
410         return finalResult;
411 }
412
413 result
414 _CustomListImpl::SetItemEnabled(int index, bool enable)
415 {
416         result r = E_SUCCESS;
417
418         SysTryReturnResult(NID_UI_CTRL, (index >= 0 && index < GetItemCount()),
419                         E_SYSTEM, "[E_SYSTEM] A system error has occurred. The index %d is invalid.", index);
420
421         _CheckElementModel* pCheckElementModel = GetCheckElementAt(-1, index);
422
423         if (pCheckElementModel)
424         {
425                 if (enable)
426                 {
427                         if (pCheckElementModel->GetCheckBoxStatus() == CHECK_BOX_DISABLED)
428                         {
429                                 pCheckElementModel->SetCheckBoxStatus(CHECK_BOX_CHECKED);
430                         }
431                 }
432                 else
433                 {
434                         if (pCheckElementModel->GetCheckBoxStatus() == CHECK_BOX_CHECKED)
435                         {
436                                 if (_annexStyle == TABLE_VIEW_ANNEX_STYLE_MARK)
437                                 {
438                                         pCheckElementModel->SetCheckBoxStatus(CHECK_BOX_DISABLED);
439                                 }
440                         }
441                 }
442         }
443
444         r = GetCore().SetItemEnabled(0, index, enable);
445         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
446
447         DrawItem(-1, index);
448
449         return r;
450 }
451
452 bool
453 _CustomListImpl::IsItemEnabled(int index) const
454 {
455         return GetCore().IsItemEnabled(0, index);
456 }
457
458 result
459 _CustomListImpl::SetItemChecked(int groupIndex, int itemIndex, bool check)
460 {
461         return SetItemChecked(itemIndex, check);
462 }
463
464 result
465 _CustomListImpl::SetItemChecked(int index, bool check)
466 {
467         SysTryReturnResult(NID_UI_CTRL, (index >= 0 && index < GetItemCount()), E_SYSTEM, "[E_SYSTEM] A system error has occurred. The index %d is invalid.", index);
468
469         SysTryReturnResult(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL), E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_NORMAL");
470
471         if (IsItemChecked(index) == check)
472         {
473                 return E_SUCCESS;
474         }
475
476         SysTryReturnResult(NID_UI_CTRL, (IsItemEnabled(index)), E_SYSTEM, "[E_SYSTEM] A system error has occurred. The List item is not enabled at index %d.", index);
477
478         if (_annexStyle == TABLE_VIEW_ANNEX_STYLE_RADIO)
479         {
480                 if (check)
481                 {
482                         if (_currentRadioIndex != -1)
483                         {
484                                 _CheckElementModel* pOldCheckElementModel = GetCheckElementAt(-1, _currentRadioIndex);
485                                 SysTryReturnResult(NID_UI_CTRL, pOldCheckElementModel, E_SYSTEM, "A system error has occurred. tableViewItem is not constructed correctly.");
486
487                                 pOldCheckElementModel->SetCheckBoxStatus(CHECK_BOX_UNCHECKED);
488
489                                 _TableViewItemData* pOldRadioItem = GetTableViewItemAt(-1, _currentRadioIndex);
490                                 SysTryReturnResult(NID_UI_CTRL, pOldRadioItem, E_SYSTEM, "A system error has occurred. Failed to get item at %d index.", _currentRadioIndex);
491
492                                 pOldRadioItem->Invalidate(true);
493                         }
494                         _currentRadioIndex = index;
495                 }
496                 else
497                 {
498                         _currentRadioIndex = -1;
499                 }
500         }
501
502         _CheckElementModel* pCheckElementModel = GetCheckElementAt(-1, index);
503         SysTryReturnResult(NID_UI_CTRL, pCheckElementModel, E_SYSTEM, "A system error has occurred. TableViewItem is not constructed correctly.");
504
505         pCheckElementModel->SetCheckBoxStatus((_CheckBoxBitmapType)check);
506
507         DrawItem(-1, index);
508
509         return E_SUCCESS;
510 }
511
512 bool
513 _CustomListImpl::IsItemChecked(int groupIndex, int subIndex) const
514 {
515         return IsItemChecked(subIndex);
516 }
517
518 bool
519 _CustomListImpl::IsItemChecked(int index) const
520 {
521         SysTryReturn(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL),
522                         false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. list style should not be TABLE_VIEW_ANNEX_STYLE_NORMAL.");
523
524         SysTryReturn(NID_UI_CTRL, (index >= 0 && index < GetItemCount()),
525                         false, E_INVALID_ARG, "[E_INVALID_ARG] The index %d is invalid.", index);
526
527         const _CheckElementModel* pCheckElement = GetCheckElementAt(-1, index);
528         SysTryReturn(NID_UI_CTRL, pCheckElement, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. CheckElement is not constructed properly.");
529
530         return (bool)pCheckElement->GetCheckBoxStatus();
531 }
532
533 result
534 _CustomListImpl::SetAllItemsChecked(bool check)
535 {
536         SysTryReturnResult(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL), E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_NORMAL");
537         SysTryReturnResult(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_RADIO), E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_RADIO");
538
539         _CheckElementModel* pCheckElement = null;
540
541         for (int index = 0; index < GetItemCount(); index++)
542         {
543                 if (IsItemEnabled(index))
544                 {
545                         pCheckElement = GetCheckElementAt(-1, index);
546                         SysTryReturnResult(NID_UI_CTRL, (pCheckElement != null), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Checkbox element not created");
547
548                         pCheckElement->SetCheckBoxStatus((_CheckBoxBitmapType)check);
549                 }
550         }
551
552         GetCore().Draw();
553
554         return E_SUCCESS;
555 }
556
557 result
558 _CustomListImpl::RemoveAllCheckedItems(void)
559 {
560         result r = E_SUCCESS;
561         result finalResult = E_SUCCESS;
562
563         SysTryReturnResult(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL), E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_NORMAL");
564         SysTryReturnResult(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_RADIO), E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_RADIO");
565
566         for (int index = GetItemCount() -1; index >= 0; index--)
567         {
568                 if (IsItemChecked(index))
569                 {
570                         r = RemoveItemAt(index);
571
572                         if (r != E_SUCCESS)
573                         {
574                                 finalResult = r;
575                                 SysLog(NID_UI_CTRL, "Item at %d index not removed successfully.", index);
576                         }
577                 }
578         }
579
580         return finalResult;
581 }
582
583 int
584 _CustomListImpl::GetFirstCheckedItemIndex(void) const
585 {
586         SysTryReturn(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL), INVALID_INDEX, E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_NORMAL");
587
588         for (int index = 0; index < GetItemCount(); index++)
589         {
590                 if (IsItemChecked(index))
591                 {
592                         SetLastResult(E_SUCCESS);
593                         return index;
594                 }
595         }
596         SetLastResult(E_SUCCESS);
597         return INVALID_INDEX;
598 }
599
600 int
601 _CustomListImpl::GetLastCheckedItemIndex(void) const
602 {
603         SysTryReturn(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL), INVALID_INDEX, E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_NORMAL");
604
605         for (int index = GetItemCount() - 1; index >= 0; index--)
606         {
607                 if (IsItemChecked(index))
608                 {
609                         SetLastResult(E_SUCCESS);
610                         return index;
611                 }
612         }
613         SetLastResult(E_SUCCESS);
614         return INVALID_INDEX;
615 }
616
617 int
618 _CustomListImpl::GetNextCheckedItemIndexAfter(int index) const
619 {
620         SysTryReturn(NID_UI_CTRL, (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL), INVALID_INDEX, E_SYSTEM, "[E_SYSTEM] A system error has occurred. List style should not be TABLE_VIEW_ANNEX_STYLE_NORMAL");
621
622         SysTryReturn(NID_UI_CTRL, (index >= 0 && index < GetItemCount()),
623                                 INVALID_INDEX, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The index %d is invalid.", index);
624
625         for (int itemCount = index + 1; itemCount < GetItemCount(); itemCount++)
626         {
627                 if (IsItemChecked(itemCount))
628                 {
629                         SetLastResult(E_SUCCESS);
630                         return itemCount;
631                 }
632         }
633         SetLastResult(E_SUCCESS);
634         return INVALID_INDEX;
635 }
636
637 int
638 _CustomListImpl::GetItemIndexFromItemId(int itemId) const
639 {
640         for (int index = 0; index < GetItemCount(); index++)
641         {
642                 const CustomListItem* pCustomListItem = GetCustomListItemAt(-1, index);
643                 SysTryReturn(NID_UI_CTRL, pCustomListItem, INVALID_INDEX, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get an item at index(%d).", index);
644
645                 SysTryReturn(NID_UI_CTRL, pCustomListItem->__pCustomListItemImpl, INVALID_INDEX, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get an item at index(%d).", index);
646
647                 if (pCustomListItem->__pCustomListItemImpl->itemId == itemId)
648                 {
649                         SetLastResult(E_SUCCESS);
650                         return index;
651                 }
652         }
653         SetLastResult(E_SUCCESS);
654         return INVALID_INDEX;
655 }
656
657 void
658 _CustomListImpl::ScrollToBottom(void)
659 {
660         GetCore().SetBottomDrawnItemIndex(0 ,GetItemCount() - 1);
661         GetCore().Draw();
662         return;
663 }
664 void
665 _CustomListImpl::ScrollToTop(void)
666 {
667         GetCore().SetTopDrawnItemIndex(0,0);
668         GetCore().Draw();
669         return;
670 }
671
672 result
673 _CustomListImpl::ScrollToTop(int index)
674 {
675         SysTryReturnResult(NID_UI_CTRL, (index >= 0 && index < GetItemCount()), E_INVALID_ARG, "Invalid index(%d).", index);
676
677         result r = GetCore().SetTopDrawnItemIndex(0, index);
678         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
679
680         GetCore().Draw();
681
682         return r;
683 }
684
685 result
686 _CustomListImpl::RefreshItem(int index)
687 {
688         SysTryReturnResult(NID_UI_CTRL, (index >= 0 && index < GetItemCount()), E_INVALID_ARG, "Invalid index(%d).", index);
689
690         int topItemIndex = -1;
691         int bottomItemIndex = -1;
692         int groupIndex = -1;
693
694         GetCore().GetTopDrawnItemIndex(groupIndex, topItemIndex);
695         GetCore().GetBottomDrawnItemIndex(groupIndex, bottomItemIndex);
696
697         SysTryReturnResult(NID_UI_CTRL, (index >= topItemIndex && index <= bottomItemIndex), E_INVALID_OPERATION,
698                                           "Index should be within drawn item range %d.", index);
699
700         result r = E_SUCCESS;
701
702         _TableViewItemUpdateParams updateParams;
703         updateParams.pItem = GetCustomListItemAt(-1, index);
704         updateParams.isDividerEnabled = _isDividerEnabled;
705         updateParams.pCheckBitmaps = _pCheckBitmaps;
706         updateParams.annexStyle = _annexStyle;
707
708         r = _CustomListItemImpl::UpdateTableViewItem(updateParams);
709         SysTryReturnResult(NID_UI_CTRL, (r == E_SUCCESS), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to refresh an item.");
710
711         DrawItem(-1, index);
712
713         return E_SUCCESS;
714 }
715
716 int
717 _CustomListImpl::GetItemIdAt(int index) const
718 {
719         SysTryReturn(NID_UI_CTRL, (index >= 0 && index < GetItemCount()),
720                                 -1, E_INVALID_ARG, "[E_INVALID_ARG] The index %d is invalid.", index);
721
722         const CustomListItem* pCustomListItem = GetCustomListItemAt(-1, index);
723         SysTryReturn(NID_UI_CTRL, pCustomListItem, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get an item at index(%d).", index);
724
725         SysTryReturn(NID_UI_CTRL, pCustomListItem->__pCustomListItemImpl, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get an item at index(%d).", index);
726
727         SetLastResult(E_SUCCESS);
728         return pCustomListItem->__pCustomListItemImpl->itemId;
729 }
730
731 void
732 _CustomListImpl::OnTableViewItemStateChanged(_TableView& tableView, int itemIndex, _TableViewItem* pItem, TableViewItemStatus status)
733 {
734         ItemStatus itemStatus = ITEM_HIGHLIGHTED;
735
736         switch (status)
737         {
738         case TABLE_VIEW_ITEM_STATUS_SELECTED:
739                 itemStatus = ITEM_SELECTED;
740                 break;
741         case TABLE_VIEW_ITEM_STATUS_HIGHLIGHTED:
742                 itemStatus = ITEM_HIGHLIGHTED;
743                 break;
744         case TABLE_VIEW_ITEM_STATUS_CHECKED:
745                 itemStatus = ITEM_CHECKED;
746                 break;
747         case TABLE_VIEW_ITEM_STATUS_UNCHECKED:
748                 itemStatus = ITEM_UNCHECKED;
749                 break;
750         default:
751                 SetLastResult(E_SYSTEM);
752                 break;
753         }
754
755         if ((!_isDividerEnabled) && (_annexStyle != TABLE_VIEW_ANNEX_STYLE_NORMAL))
756         {
757                 bool isChecked = IsItemChecked(itemIndex);
758
759                 if (isChecked)
760                 {
761                         itemStatus = ITEM_UNCHECKED;
762                 }
763                 else
764                 {
765                         itemStatus = ITEM_CHECKED;
766                 }
767
768                 SetItemChecked(itemIndex, !isChecked);
769         }
770
771         ProcessItemStateChange(-1, itemIndex, itemStatus);
772         return;
773 }
774
775 void
776 _CustomListImpl::OnTableViewContextItemActivationStateChanged(_TableView& tableView, int itemIndex, _TableViewItem* pContextItem, bool activated)
777 {
778         return;
779 }
780
781 void
782 _CustomListImpl::OnTableViewItemReordered(_TableView& tableView, int itemIndexFrom, int itemIndexTo)
783 {
784         return;
785 }
786
787 void
788 _CustomListImpl::OnGroupedTableViewGroupItemStateChanged(_TableView& tableView, int groupIndex, _TableViewItem* pItem, TableViewItemStatus status)
789 {
790         return;
791 }
792
793 void
794 _CustomListImpl::OnGroupedTableViewItemStateChanged(_TableView& tableView, int groupIndex, int itemIndex, _TableViewItem* pItem, TableViewItemStatus status)
795 {
796         return;
797 }
798
799 void
800 _CustomListImpl::OnGroupedTableViewContextItemActivationStateChanged(_TableView& tableView, int groupIndex, int itemIndex, _TableViewItem* pContextItem, bool activated)
801 {
802         return;
803 }
804
805 void
806 _CustomListImpl::OnGroupedTableViewItemReordered(_TableView& tableView, int groupIndexFrom, int itemIndexFrom, int groupIndexTo, int itemIndexTo)
807 {
808         return;
809 }
810
811 void
812 _CustomListImpl::OnSectionTableViewItemStateChanged(_TableView& tableView, int sectionIndex, int itemIndex, _TableViewItem* pItem, TableViewItemStatus status)
813 {
814         return;
815 }
816
817 void
818 _CustomListImpl::OnSectionTableViewContextItemActivationStateChanged(_TableView& tableView, int sectionIndex, int itemIndex, _TableViewItem* pContextItem, bool activated)
819 {
820         return;
821 }
822
823 void
824 _CustomListImpl::OnTableViewItemSwept(_TableView& tableView, int groupIndex, int itemIndex, TableViewSweepDirection direction)
825 {
826         return;
827 }
828
829 void
830 _CustomListImpl::ProcessItemStateChange(int groupIndex, int index, int elementId, ItemStatus itemStatus)
831 {
832         int itemId = GetItemIdAt(index);
833
834         _ListListener* pListenerList = null;
835         ICustomItemEventListener* pEventListener = null;
836
837         int count = __itemListenersList.GetCount();
838         for (int listenerCount = 0; listenerCount < count; listenerCount++)
839         {
840                 pListenerList = dynamic_cast<_ListListener*>(__itemListenersList.GetAt(listenerCount));
841                 SysTryReturnVoidResult(NID_UI_CTRL, (pListenerList != null), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get Listener.");
842
843                 pEventListener = dynamic_cast<ICustomItemEventListener*>(pListenerList->pListener);
844                 SysTryReturnVoidResult(NID_UI_CTRL, (pEventListener != null), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get Listener.");
845
846                 pEventListener->OnItemStateChanged(GetPublic(), index, itemId, elementId, itemStatus);
847         }
848         SetLastResult(E_SUCCESS);
849
850         return;
851 }
852
853 void
854 _CustomListImpl::ProcessItemStateChange(int groupIndex, int index, ItemStatus itemStatus)
855 {
856         int itemId = GetItemIdAt(index);
857
858         _ListListener* pListenerList = null;
859         ICustomItemEventListener* pEventListener = null;
860
861         int count = __itemListenersList.GetCount();
862         for (int listenerCount = 0; listenerCount < count; listenerCount++)
863         {
864                 pListenerList = dynamic_cast<_ListListener*>(__itemListenersList.GetAt(listenerCount));
865                 SysTryReturnVoidResult(NID_UI_CTRL, (pListenerList != null), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get Listener.");
866
867                 pEventListener = dynamic_cast<ICustomItemEventListener*>(pListenerList->pListener);
868                 SysTryReturnVoidResult(NID_UI_CTRL, (pEventListener != null), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get Listener.");
869
870                 pEventListener->OnItemStateChanged(GetPublic(), index, itemId, itemStatus);
871         }
872         SetLastResult(E_SUCCESS);
873
874         return;
875 }
876
877 class _CustomListMaker
878         : public _UiBuilderControlMaker
879 {
880 public:
881         _CustomListMaker(_UiBuilder* uibuilder)
882                 : _UiBuilderControlMaker(uibuilder){};
883         virtual ~_CustomListMaker(){};
884         static _UiBuilderControlMaker*
885         GetInstance(_UiBuilder* uibuilder)
886         {
887                 _CustomListMaker* pCustomListMaker = new (std::nothrow) _CustomListMaker(uibuilder);
888                 return pCustomListMaker;
889         };
890 protected:
891         virtual Control*
892         Make(_UiBuilderControl* pControl)
893         {
894                 result r = E_SYSTEM;
895                 _UiBuilderControlLayout* pControlProperty = null;
896                 CustomList* pCustomList = null;
897
898                 Tizen::Base::String elementString;
899                 Color color;
900                 Rectangle rect;
901
902                 CustomListStyle style = CUSTOM_LIST_STYLE_NORMAL;
903                 bool isItemDivider = true;
904
905                 GetProperty(pControl, &pControlProperty);
906
907                 if (pControlProperty == null)
908                 {
909                         return null;
910                 }
911
912                 pCustomList = new (std::nothrow) CustomList();
913                 if (pCustomList == null)
914                 {
915                         return null;
916                 }
917
918                 rect = pControlProperty->GetRect();
919
920                 Tizen::Base::String styleString;
921                 styleString = pControlProperty->GetStyle();
922
923                 if (styleString.Equals(L"CUSTOM_LIST_STYLE_NORMAL", false))
924                 {
925                         style = CUSTOM_LIST_STYLE_NORMAL;
926                 }
927                 else if (styleString.Equals(L"CUSTOM_LIST_STYLE_RADIO", false))
928                 {
929                         style = CUSTOM_LIST_STYLE_RADIO;
930                 }
931                 else if (styleString.Equals(L"CUSTOM_LIST_STYLE_RADIO_WITH_DIVIDER", false))
932                 {
933                         style = CUSTOM_LIST_STYLE_RADIO_WITH_DIVIDER;
934                 }
935                 else if (styleString.Equals(L"CUSTOM_LIST_STYLE_MARK", false))
936                 {
937                         style = CUSTOM_LIST_STYLE_MARK;
938                 }
939                 else if (styleString.Equals(L"CUSTOM_LIST_STYLE_MARK_WITH_DIVIDER", false))
940                 {
941                         style = CUSTOM_LIST_STYLE_MARK_WITH_DIVIDER;
942                 }
943                 else if (styleString.Equals(L"CUSTOM_LIST_STYLE_ONOFF", false))
944                 {
945                         style = CUSTOM_LIST_STYLE_ONOFF;
946                 }
947                 else if (styleString.Equals(L"CUSTOM_LIST_STYLE_ONOFF_WITH_DIVIDER", false))
948                 {
949                         style = CUSTOM_LIST_STYLE_ONOFF_WITH_DIVIDER;
950                 }
951                 else
952                 {
953                         style = CUSTOM_LIST_STYLE_NORMAL;
954                 }
955
956                 //Construct
957                 if (pControl->GetElement(L"itemDivider", elementString))
958                 {
959                         if (elementString.Equals(L"true", false))
960                         {
961                                 isItemDivider = true;
962                         }
963                         else
964                         {
965                                 isItemDivider = false;
966                         }
967                 }
968
969                 r = pCustomList->Construct(rect, style, isItemDivider);
970                 if (r != E_SUCCESS)
971                 {
972                         delete pCustomList;
973                         return null;
974                 }
975
976                 if (pControl->GetElement(L"textOfEmptyList", elementString))
977                 {
978                         pCustomList->SetTextOfEmptyList(elementString);
979                 }
980
981                 if (pControl->GetElement(L"colorOfEmptyListText", elementString))
982                 {
983                         ConvertStringToColor(elementString, color);
984                         pCustomList->SetTextColorOfEmptyList(color);
985                 }
986
987                 return pCustomList;
988         }
989
990 };// _ButtonMaker
991
992 _CustomListRegister::_CustomListRegister(void)
993 {
994         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
995
996         if (pUiBuilderControlTableManager)
997         {
998                 pUiBuilderControlTableManager->RegisterControl(L"CustomList", _CustomListMaker::GetInstance);
999         }
1000 }
1001 _CustomListRegister::~_CustomListRegister(void)
1002 {
1003         _UiBuilderControlTableManager* pUiBuilderControlTableManager = _UiBuilderControlTableManager::GetInstance();
1004
1005         if (pUiBuilderControlTableManager)
1006         {
1007                 pUiBuilderControlTableManager->UnregisterControl(L"CustomList");
1008         }
1009 }
1010 static _CustomListRegister CustomListRegisterToUiBuilder;
1011 }}} //Tizen::Ui::Controls