2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.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
9 // http://www.apache.org/licenses/LICENSE-2.0/
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.
19 * @file FUiCtrl_FastScrollIndex.cpp
20 * @brief This is the implementation file for the _FastScrollIndex class.
24 #include <FBaseSysLog.h>
25 #include <FGrp_BitmapImpl.h>
26 #include "FUiCtrl_FastScrollIndex.h"
28 using namespace Tizen::Base::Collection;
32 const char* _DELIMITER = "\n";
35 namespace Tizen { namespace Ui { namespace Controls
38 _FastScrollIndex::_FastScrollIndex(void)
41 , __pParentIndex(null)
42 , __pChildIndexList(null)
43 , __pChildNextIndexList(null)
44 , __pChildPreviousIndexList(null)
45 , __pIndexObserver(null)
47 , __indexType(FAST_SCROLL_INDEX_TYPE_NORMAL)
51 _FastScrollIndex::~_FastScrollIndex(void)
55 __pIndexObserver->OnIndexDeleted(*this);
60 __pParentIndex->RemoveChildIndex(this, false);
61 __pParentIndex = null;
75 _FastScrollIndex::CreateFastScrollIndexN(void)
77 _FastScrollIndex* pIndex = new (std::nothrow) _FastScrollIndex();
78 SysTryReturn(NID_UI_CTRL, pIndex, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
80 result r = pIndex->Construct();
81 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
83 SetLastResult(E_SUCCESS);
93 _FastScrollIndex::SetIndexText(Tizen::Base::String* pText)
100 __pText = new (std::nothrow) Tizen::Base::String(*pText);
101 SysTryReturnResult(NID_UI_CTRL, __pText, E_OUT_OF_MEMORY, "Memory allocation failed.");
104 // send the action to _IFastScrollIndexObserver
105 if (__pIndexObserver)
107 __pIndexObserver->OnIndexDataUpdated(*this);
114 _FastScrollIndex::GetIndexText(void) const
120 _FastScrollIndex::SetIndexImage(Tizen::Graphics::Bitmap* pImage)
127 __pImage = Tizen::Graphics::_BitmapImpl::CloneN(*pImage);
130 // send the action to _IFastScrollIndexObserver
131 if (__pIndexObserver)
133 __pIndexObserver->OnIndexDataUpdated(*this);
139 Tizen::Graphics::Bitmap*
140 _FastScrollIndex::GetIndexImage(void) const
146 _FastScrollIndex::SetIndexObserver(_IFastScrollIndexObserver* pIndexListener)
148 __pIndexObserver = pIndexListener;
151 _IFastScrollIndexObserver*
152 _FastScrollIndex::GetIndexObserver(void) const
154 return __pIndexObserver;
158 _FastScrollIndex::SetOmissionIndex(int indexCountMax)
160 int childCount = GetChildCount();
161 _FastScrollIndex* pIndex = null;
163 int maxOmissionSlot = (indexCountMax - 1) / 2;
164 if (maxOmissionSlot < 0)
168 else if (maxOmissionSlot == 0)
173 int omissionCount = childCount - indexCountMax;
174 int omittedNum = childCount - indexCountMax;
175 int additionalOmittedNum = 0;
176 int additionalOmissionStartIndex = 0;
177 int indexCountMin = 3;
179 if (maxOmissionSlot > 0)
181 omittedNum = (omissionCount / maxOmissionSlot) + 1;
182 additionalOmittedNum = (omissionCount % maxOmissionSlot);
183 additionalOmissionStartIndex = ((maxOmissionSlot - additionalOmittedNum) / 2);
188 result r = E_SUCCESS;
189 Tizen::Base::String omissionText(L".");
191 if ((indexCountMax > indexCountMin) && (indexCountMax % 2) == 0)
193 pIndex = AddChildIndex(GetChildIndex(i), &__childOmittedIndexList);
194 SysTryReturnResult(NID_UI_CTRL, pIndex, GetLastResult(), "Failed to add child index.");
199 while (i < childCount)
201 if (j < indexCountMax)
203 pIndex = AddChildIndex(GetChildIndex(i), &__childOmittedIndexList);
204 SysTryReturnResult(NID_UI_CTRL, pIndex, GetLastResult(), "Failed to add child index.");
215 int omittedCount = omittedNum;
216 if ((additionalOmissionStartIndex < 0) && (additionalOmittedNum > 0))
219 additionalOmittedNum--;
222 if (omittedCount < 2)
226 additionalOmissionStartIndex--;
231 pIndex = AddChildIndex(&omissionText, null, &__childOmittedIndexList);
232 SysTryReturnResult(NID_UI_CTRL, pIndex, GetLastResult(), "Failed to add child index.");
234 pIndex->SetOmitted(true);
235 ArrayList* pList = new (std::nothrow) ArrayList();
236 SysTryReturnResult(NID_UI_CTRL, pList, E_OUT_OF_MEMORY, "Memory allocation failed.");
237 pIndex->SetCurrentIndexList(pList);
240 additionalOmissionStartIndex--;
242 for (int k = 0; k < omittedCount; k++)
244 pIndex->AddOmissionChildIndex(GetChildIndex(i + k));
248 SetIndexType(FAST_SCROLL_INDEX_TYPE_OMISSION);
254 _FastScrollIndex::AddChildIndex(Tizen::Base::String* pText, Tizen::Graphics::Bitmap* pImage, ArrayList* pIndexList)
256 _FastScrollIndex* pNewIndex = _FastScrollIndex::CreateFastScrollIndexN();
257 SysTryReturn(NID_UI_CTRL, pNewIndex, null, GetLastResult(),
258 "[%s] Failed to create _FastScrollIndex.", GetErrorMessage(GetLastResult()));
260 result r = pNewIndex->SetIndexText(pText);
261 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set text.", GetErrorMessage(r));
263 pNewIndex->SetIndexImage(pImage);
265 return AddChildIndex(pNewIndex, pIndexList);
274 _FastScrollIndex::AddChildIndex(_FastScrollIndex* pChildIndex, ArrayList* pIndexList)
276 return AddChildIndex(GetChildCount(pIndexList), pChildIndex, pIndexList);
280 _FastScrollIndex::AddChildIndex(int childOrder, Tizen::Base::String* pText, Tizen::Graphics::Bitmap* pImage, ArrayList* pIndexList)
282 _FastScrollIndex* pNewIndex = _FastScrollIndex::CreateFastScrollIndexN();
283 SysTryReturn(NID_UI_CTRL, pNewIndex, null, GetLastResult(),
284 "[%s] Failed to create _FastScrollIndex.", GetErrorMessage(GetLastResult()));
286 result r = pNewIndex->SetIndexText(pText);
287 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set text.", GetErrorMessage(r));
289 pNewIndex->SetIndexImage(pImage);
291 return AddChildIndex(childOrder, pNewIndex, pIndexList);
300 _FastScrollIndex::AddChildIndex(int childOrder, _FastScrollIndex* pChildIndex, ArrayList* pIndexList)
302 SysTryReturn(NID_UI_CTRL, pChildIndex, null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. pChildIndex is null.");
304 ArrayList* pList = null;
311 if (__pChildIndexList == null)
313 __pChildIndexList = new (std::nothrow) ArrayList();
314 SysTryReturn(NID_UI_CTRL, __pChildIndexList, null, E_OUT_OF_MEMORY, "Memory allocation failed.");
317 pList = __pChildIndexList;
320 if (pList->GetCount() <= childOrder)
322 childOrder = GetChildCount(pList);
325 pList->InsertAt(*pChildIndex, childOrder);
326 pChildIndex->SetParentIndex(this);
328 if (__pIndexObserver)
330 __pIndexObserver->OnChildIndexAttached(*this, childOrder, *pChildIndex);
337 _FastScrollIndex::AddOmissionChildIndex(_FastScrollIndex* pChildIndex)
339 if (GetOmitted() == false)
344 SysTryReturnVoidResult(NID_UI_CTRL, pChildIndex, E_INVALID_ARG,
345 "[E_INVALID_ARG] Invalid argument is used. pChildIndex is null.");
346 SysTryReturnVoidResult(NID_UI_CTRL, __pChildIndexList, E_INVALID_ARG,
347 "[E_INVALID_ARG] Invalid argument is used. pChildIndexList is null.");
349 __pChildIndexList->InsertAt(*pChildIndex, GetChildCount());
353 _FastScrollIndex::AddChildTextIndexArray(int childOrder, const wchar_t* pTextIndexArray, int textLenth, int indexCount)
355 result r = E_SUCCESS;
357 Tizen::Base::String* indexText = null;
359 wchar_t* pTempChar = new (std::nothrow) wchar_t[textLenth+1];
360 SysTryReturnResult(NID_UI_CTRL, pTempChar, E_OUT_OF_MEMORY, "Memory allocation failed.");
366 _FastScrollIndex* pIndex = null;
367 ArrayList* pChildIndexList = null;
368 ArrayList* pFormerList = null;
369 ArrayList* pFirstIndexList = GetCurrentIndexList();
370 if (pFirstIndexList == null)
372 pFirstIndexList = new (std::nothrow) ArrayList();
373 SysTryCatch(NID_UI_CTRL, pFirstIndexList, , E_OUT_OF_MEMORY, "Memory allocation failed.");
374 SetCurrentIndexList(pFirstIndexList);
376 pChildIndexList = pFirstIndexList;
378 for (i = 0; i < indexCount; i++)
380 for (j = 0; j < textLenth; j++)
382 pTempChar[j] = pTextIndexArray[(i*textLenth)+j];
386 indexText = new (std::nothrow) Tizen::Base::String(pTempChar);
387 SysTryCatch(NID_UI_CTRL, indexText, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
388 if (indexText->Equals(Tizen::Base::String(_DELIMITER)))
393 pFormerList = pChildIndexList;
394 _FastScrollIndex* pFormerIndex = null;
396 pChildIndexList = new (std::nothrow) ArrayList();
397 SysTryCatchLabel(NID_UI_CTRL, pChildIndexList, , CATCH1, E_OUT_OF_MEMORY, "Memory allocation failed.");
400 pFormerIndex = GetChildIndex(GetChildCount(pFormerList) - 1, pFormerList);
401 SysTryCatchLabel(NID_UI_CTRL, pFormerIndex, , CATCH1, GetLastResult(), "[%s] Failed to get the child index.", GetErrorMessage(GetLastResult()));
402 pFormerIndex->SetNextIndexList(pChildIndexList);
406 pIndex = AddChildIndex(k, indexText, null, pChildIndexList);
407 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(), "[%s] Failed to add child index.", GetErrorMessage(GetLastResult()));
409 if (k == 0 && pFormerList)
411 pIndex->SetPreviousIndexList(pFormerList);
422 pChildIndexList = pFirstIndexList;
423 int childCount = GetChildCount(pChildIndexList);
424 pIndex = GetChildIndex(childCount - 1, pChildIndexList);
425 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(),
426 "[%s] Failed to get the child index.", GetErrorMessage(GetLastResult()));
427 ArrayList* pNextIndexList = pIndex->GetNextIndexList();
429 while(pNextIndexList)
431 //next last index to previous last index
432 pIndex->SetNextIndexList(null);
433 pIndex = GetChildIndex(GetChildCount(pNextIndexList) - 1, pNextIndexList);
434 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(),
435 "[%s] Failed to get the child index.", GetErrorMessage(GetLastResult()));
436 pIndex = AddChildIndex(childCount, pIndex->GetIndexText(), null, pChildIndexList);
437 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(), "[%s] Failed to add child index.", GetErrorMessage(GetLastResult()));
438 pIndex->SetNextIndexList(pNextIndexList);
440 //next first index to previous first index
441 pIndex = GetChildIndex(0, pNextIndexList);
442 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(),
443 "[%s] Failed to get the child index.", GetErrorMessage(GetLastResult()));
444 pIndex->SetPreviousIndexList(null);
446 pIndex = GetChildIndex(i, pChildIndexList);
447 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(),
448 "[%s] Failed to get the child index.", GetErrorMessage(GetLastResult()));
449 pIndex = AddChildIndex(0, pIndex->GetIndexText(), null, pNextIndexList);
450 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(), "[%s] Failed to add child index.", GetErrorMessage(GetLastResult()));
451 pIndex->SetPreviousIndexList(pChildIndexList);
458 pChildIndexList = pNextIndexList;
459 childCount = GetChildCount(pChildIndexList);
460 pIndex = GetChildIndex(childCount - 1, pChildIndexList);
461 SysTryCatch(NID_UI_CTRL, pIndex, , GetLastResult(),
462 "[%s] Failed to get the child index.", GetErrorMessage(GetLastResult()));
463 pNextIndexList = pIndex->GetNextIndexList();
471 delete pChildIndexList;
472 pChildIndexList = null;
483 _FastScrollIndex::RemoveChildIndex(int childOrder, bool destroy, ArrayList* pIndexList)
485 _FastScrollIndex* pIndex = GetChildIndex(childOrder, pIndexList);
486 SysTryReturnResult(NID_UI_CTRL, pIndex, GetLastResult(), "Failed to get the child index.");
488 return RemoveChildIndex(pIndex, destroy, pIndexList);
492 _FastScrollIndex::RemoveChildIndex(_FastScrollIndex* pChildIndex, bool destroy, ArrayList* pIndexList)
494 int detachedOrder = 0;
495 ArrayList* pList = __pChildIndexList;
507 pList->IndexOf(*pChildIndex, detachedOrder);
511 pList->Remove(*pChildIndex, true);
515 pList->Remove(*pChildIndex, false);
516 pChildIndex->SetParentIndex(null);
519 if (__pIndexObserver)
521 __pIndexObserver->OnChildIndexDetached(*this, detachedOrder, *pChildIndex);
528 _FastScrollIndex::RemoveChildren(bool destroy, ArrayList* pIndexList)
530 RemoveOmissionChildren(destroy);
531 ArrayList* pList = __pChildIndexList;
543 int childrenCount = pList->GetCount();
544 _FastScrollIndex* pIndex = (GetChildIndex(childrenCount - 1));
547 ArrayList* nextIndexList = pIndex->GetNextIndexList();
548 if (GetOmitted() == false && nextIndexList)
550 RemoveChildren(destroy, nextIndexList);
551 pIndex->SetNextIndexList(null);
555 for (int i = childrenCount-1; i >= 0; i--)
557 pList->RemoveAt(i, destroy);
567 __pChildIndexList = null;
572 _FastScrollIndex::RemoveOmissionChildren(bool destroy)
574 int childrenCount = __childOmittedIndexList.GetCount();
576 if (childrenCount <= 0)
581 for (int i = childrenCount-1; i >= 0; i--)
583 _FastScrollIndex* pIndex = GetChildIndex(i, &__childOmittedIndexList);
584 SysTryReturnResult(NID_UI_CTRL, pIndex, GetLastResult(), "Failed to get the child index.");
586 if (pIndex->GetOmitted())
588 pIndex->RemoveChildren(false);
590 __childOmittedIndexList.RemoveAt(i, true);
594 __childOmittedIndexList.RemoveAt(i, false);
597 SetIndexType(FAST_SCROLL_INDEX_TYPE_NORMAL);
603 _FastScrollIndex::SetParentIndex(_FastScrollIndex* pParent)
605 __pParentIndex = pParent;
609 _FastScrollIndex::GetParentIndex(void) const
611 return __pParentIndex;
615 _FastScrollIndex::GetChildIndex(int childOrder, bool omitted) const
617 ArrayList* pIndexList = __pChildIndexList;
618 if (omitted && __childOmittedIndexList.GetCount() > 0)
620 pIndexList = const_cast <ArrayList*>(&__childOmittedIndexList);
623 return GetChildIndex(childOrder, pIndexList);
627 _FastScrollIndex::GetChildIndex(int childOrder, ArrayList* pIndexList) const
629 if (pIndexList == null)
634 Object* pChildIndex = const_cast <Object*>(pIndexList->GetAt(childOrder));
635 SysTryReturn(NID_UI_CTRL, pChildIndex, null, GetLastResult(), "[%s] Failed to get the child index.",
636 GetErrorMessage(GetLastResult()));
638 return dynamic_cast <_FastScrollIndex*>(pChildIndex);
642 _FastScrollIndex::GetChildOrder(const _FastScrollIndex* pChildIndex, bool omitted) const
644 ArrayList* pIndexList = __pChildIndexList;
645 if (omitted && __childOmittedIndexList.GetCount() > 0)
647 pIndexList = const_cast <ArrayList*>(&__childOmittedIndexList);
650 return GetChildOrder(pChildIndex, pIndexList);
654 _FastScrollIndex::GetChildOrder(const _FastScrollIndex* pChildIndex, ArrayList* pIndexList) const
656 if (pIndexList == null)
662 pIndexList->IndexOf(*pChildIndex, childIndex);
668 _FastScrollIndex::GetChildCount(bool omitted) const
670 ArrayList* pIndexList = __pChildIndexList;
673 pIndexList = const_cast <ArrayList*>(&__childOmittedIndexList);
676 return GetChildCount(pIndexList);
680 _FastScrollIndex::GetChildCount(ArrayList* pIndexList) const
682 if (pIndexList == null)
687 return pIndexList->GetCount();
691 _FastScrollIndex::GetIndexOrder(bool omitted) const
695 return __pParentIndex->GetChildOrder(this, omitted);
704 _FastScrollIndex::GetIndexOrder(ArrayList* pIndexList) const
708 return __pParentIndex->GetChildOrder(this, pIndexList);
717 _FastScrollIndex::SetIndexType(_FastScrollIndexType type)
723 _FastScrollIndex::GetIndexType(void) const
729 _FastScrollIndex::SetCurrentIndexList(ArrayList* pList)
731 __pChildIndexList = pList;
735 _FastScrollIndex::GetCurrentIndexList(void) const
737 return __pChildIndexList;
741 _FastScrollIndex::SetNextIndexList(ArrayList* pList)
743 __pChildNextIndexList = pList;
747 _FastScrollIndex::GetNextIndexList(void) const
749 return __pChildNextIndexList;
753 _FastScrollIndex::SetPreviousIndexList(ArrayList* pList)
755 __pChildPreviousIndexList = pList;
759 _FastScrollIndex::GetPreviousIndexList(void) const
761 return __pChildPreviousIndexList;
765 _FastScrollIndex::SetOmitted(bool omitted)
771 _FastScrollIndex::GetOmitted(void) const
777 _FastScrollIndex::Construct(void)
782 } } } // Tizen::Ui::Controls