Fixed Coding Idioms Violations
[platform/framework/native/uifw.git] / src / ui / controls / FUiCtrl_IconListItem.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
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_IconListItem.cpp
20  * @brief               This is the implementation file for the _IconListItem class.
21  */
22
23 //Includes
24 #include <FBaseSysLog.h>
25 #include <FGrpCanvas.h>
26 #include <FUiAnimVisualElementPropertyAnimation.h>
27 #include <FUiCtrlIconListViewItem.h>
28 #include <FGrpCoordinateSystem.h>
29 #include <FGrp_BitmapImpl.h>
30 #include <FGrp_CanvasImpl.h>
31 #include <FGrp_TextTextObject.h>
32 #include <FGrp_TextTextSimple.h>
33
34 #include "FUi_CoordinateSystemUtils.h"
35 #include "FUi_Math.h"
36 #include "FUiAnim_VisualElement.h"
37 #include "FUiCtrl_IconListItem.h"
38 #include "FUiCtrl_IconListItemDrawingProperty.h"
39 #include "FUiCtrl_IconListUtils.h"
40
41 using namespace Tizen::Base;
42 using namespace Tizen::Graphics;
43 using namespace Tizen::Graphics::_Text;
44 using namespace Tizen::Ui::Animations;
45
46 namespace Tizen { namespace Ui { namespace Controls
47 {
48
49 _IconListItem::_IconListItem(_VisualElement* pBase)
50         : _ListItemCommon()
51         , __itemIndex(0)
52         , __text(L"")
53         , __pBase(pBase)
54         , __pTextVE(null)
55         , __pCheckVE(null)
56         , __pCheckBgVE(null)
57         , __pTouchAniVE(null)
58         , __pFocusVE(null)
59         , __pProperty(null)
60         , __foreDrawnState(LIST_ITEM_STATE_NORMAL)
61         , __isDrawn(false)
62         , __isAlradyDestory(false)
63         , __pTextObject(null)
64         , __pAccessibilityElement(null)
65 {
66         __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL] = null;
67         __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_PRESSED] = null;
68         __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_HIGHLIGHTED] = null;
69 }
70
71 _IconListItem::~_IconListItem(void)
72 {
73         delete __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL];
74         __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL] = null;
75
76         delete __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_PRESSED];
77         __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_PRESSED] = null;
78
79         delete __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_HIGHLIGHTED];
80         __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_HIGHLIGHTED] = null;
81
82         __pProperty = null;
83
84         delete __pTextObject;
85         __pTextObject = null;
86
87         if (__pBase != null)
88         {
89                 __pBase->RemoveAllAnimations();
90
91                 for (int i = 0, n = __overlayBitmaps.size() ; i < n ; i++)
92                 {
93                         _OverlayBitmap* pOverlayBitmap = __overlayBitmaps.at(i);
94                         delete pOverlayBitmap->pBitmap;
95                         pOverlayBitmap->pBitmap = null;
96
97                         if (pOverlayBitmap->pVE != null)
98                         {
99                                 __pBase->DetachChild(*(pOverlayBitmap->pVE));
100                                 pOverlayBitmap->pVE->Destroy();
101                                 pOverlayBitmap->pVE = null;
102                         }
103                         delete pOverlayBitmap;
104                 }
105
106                 if (__pTextVE != null)
107                 {
108                         __pBase->DetachChild(*__pTextVE);
109                         __pTextVE->Destroy();
110                         __pTextVE = null;
111                 }
112
113                 if (__pCheckVE != null)
114                 {
115                         __pBase->DetachChild(*__pCheckVE);
116                         __pCheckVE->Destroy();
117                         __pCheckVE = null;
118                 }
119
120                 if (__pCheckBgVE != null)
121                 {
122                         __pBase->DetachChild(*__pCheckBgVE);
123                         __pCheckBgVE->Destroy();
124                         __pCheckBgVE = null;
125                 }
126
127                 if (__pTouchAniVE!= null)
128                 {
129                         __pBase->DetachChild(*__pTouchAniVE);
130                         __pTouchAniVE->Destroy();
131                         __pTouchAniVE = null;
132                 }
133
134                 if (__pFocusVE!= null)
135                 {
136                         __pBase->DetachChild(*__pFocusVE);
137                         __pFocusVE->Destroy();
138                         __pFocusVE = null;
139                 }
140
141                 VisualElement* pParent = __pBase->GetParent();
142                 if (pParent != null)
143                 {
144                         pParent->DetachChild(*__pBase);
145                 }
146                 __pBase->Destroy();
147                 __pBase = null;
148         }
149
150         __isAlradyDestory = true;
151         __overlayBitmaps.clear();
152
153         if (__pAccessibilityElement != null)
154         {
155                 if (__pAccessibilityElement->GetParent() != null)
156                 {
157                         __pAccessibilityElement->GetParent()->RemoveElement(*__pAccessibilityElement);
158                 }
159                 __pAccessibilityElement = null;
160         }
161 }
162
163 _IconListItem*
164 _IconListItem::CreateIconListItemN(IconListViewItem* pIconListViewItem)
165 {
166         result r = E_SUCCESS;
167         _IconListItem* pItem = null;
168         _VisualElement* pBase = new (std::nothrow) _VisualElement;
169         SysTryReturn(NID_UI_CTRL, (pBase != null), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
170
171         r = pBase->Construct();
172         SysTryCatch(NID_UI_CTRL, (r == E_SUCCESS), , r, "[%s] Propagating.", GetErrorMessage(r));
173
174         ClearLastResult();
175
176         pBase->SetSurfaceOpaque(false);
177         pBase->SetImplicitAnimationEnabled(false);
178
179         pItem = new (std::nothrow) _IconListItem(pBase);
180         SysTryCatch(NID_UI_CTRL, pItem != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
181
182         pItem->SetAppInfo(pIconListViewItem);
183
184         return pItem;
185 CATCH:
186         pBase->Destroy();
187
188         return null;
189 }
190
191 bool
192 _IconListItem::DrawItem(FloatRectangle& rect, ListItemState isFocused, int itemIndex)
193 {
194         SysAssertf(__pProperty != null, "Not yet initialized. SetDrawingProperty() should be called before use.");
195
196         ClearLastResult();
197
198         if (!__pBase->GetShowState())
199         {
200                 __pBase->SetShowState(true);
201         }
202
203         __pBase->SetBounds(rect);
204
205         // Support Accessibility
206         if (__pProperty->IsAccessibilityEnabled())
207         {
208                 SetItemAccessibilityElement(rect, itemIndex);
209         }
210
211         if (!__pProperty->IsChanged() && __isDrawn && __foreDrawnState == isFocused)
212         {
213                 return true;
214         }
215
216         Canvas* pCanvas = __pBase->GetCanvasN(FloatRectangle(0.0f, 0.0f, rect.width, rect.height));
217         if (pCanvas == null)
218         {
219                 return false;
220         }
221
222         // Draw Background Color
223         IconListViewItemDrawingStatus status = _IconListUtils::EnumConverter::ConvertListItemStateToIconListViewItemDrawingStatus(isFocused);
224         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
225         pCanvas->Clear();
226
227         float margin = __pProperty->GetSelectBackgroundMargin();
228         pCanvas->FillRectangle(__pProperty->GetBackgroundColor(status), FloatRectangle(margin, margin, rect.width - margin, rect.height - margin));
229
230         // Draw Bitmap
231         Bitmap* pBitmap = (__pBitmap[isFocused] != null) ? __pBitmap[isFocused] : __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL];
232         if (pBitmap != null)
233         {
234                 DrawBitmap(*pCanvas, *pBitmap);
235         }
236
237         // Draw Text
238         DrawText(__pProperty->GetTextColor(status));
239
240         // Draw Check
241         if (__pProperty->IsItemCheckedStyle())
242         {
243                 DrawCheck(*pCanvas);
244         }
245
246         // Draw Overlay Bitmap
247         if (__overlayBitmaps.size() > 0)
248         {
249                 DrawOverlayBitmaps();
250         }
251
252         DrawBorder(*pCanvas, (status == ICON_LIST_VIEW_ITEM_DRAWING_STATUS_HIGHLIGHTED));
253
254         __pBase->SetFlushNeeded();
255         __isDrawn = true;
256         __foreDrawnState = isFocused;
257         delete pCanvas;
258
259         return true;
260 }
261
262 result
263 _IconListItem::DrawItemTouchAnimation(FloatRectangle& rect, float ratio)
264 {
265         SysAssertf(__pProperty != null, "Not yet initialized. SetDrawingProperty() should be called before use.");
266
267         __pBase->SetBounds(rect);
268
269         if (!__pBase->GetShowState())
270         {
271                 __pBase->SetShowState(true);
272         }
273
274         Canvas* pCanvas = __pBase->GetCanvasN(FloatRectangle(0.0f, 0.0f, rect.width, rect.height));
275         if (pCanvas == null)
276         {
277                 result r = GetLastResult();
278                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
279                 return r;
280         }
281
282         pCanvas->SetBackgroundColor(__pProperty->GetBackgroundColor(ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL));
283         pCanvas->Clear();
284
285         // Draw Bitmap
286         Bitmap* pBitmap = __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL];
287         if (pBitmap != null)
288         {
289                 DrawBitmap(*pCanvas, *pBitmap, ratio);
290         }
291
292         // Draw Text
293         DrawText(__pProperty->GetTextColor(ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL));
294
295         // Draw Check
296         if (__pProperty->IsItemCheckedStyle())
297         {
298                 DrawCheck(*pCanvas);
299         }
300
301         if (__overlayBitmaps.size() > 0)
302         {
303                 DrawOverlayBitmaps();
304         }
305
306         __isDrawn = false;
307         delete pCanvas;
308
309         return E_SUCCESS;
310 }
311
312 result
313 _IconListItem::DrawItemCheckAnimation(FloatRectangle& rect, float ratio)
314 {
315         SysAssertf(__pProperty != null, "Not yet initialized. SetDrawingProperty() should be called before use.");
316
317         __pBase->SetBounds(rect);
318
319         if (!__pBase->GetShowState())
320         {
321                 __pBase->SetShowState(true);
322         }
323
324         Canvas* pCanvas = __pBase->GetCanvasN(FloatRectangle(0.0f, 0.0f, rect.width, rect.height));
325         if (pCanvas == null)
326         {
327                 result r = GetLastResult();
328                 SysLogException(NID_UI_CTRL, r, "[%s] Propagating.", GetErrorMessage(r));
329                 return r;
330         }
331
332         pCanvas->SetBackgroundColor(__pProperty->GetBackgroundColor(ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL));
333         pCanvas->Clear();
334
335         // Draw Bitmap
336         Bitmap* pBitmap = __pBitmap[ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL];
337         if (pBitmap != null)
338         {
339                 DrawBitmap(*pCanvas, *pBitmap);
340         }
341
342         // Draw Text
343         DrawText(__pProperty->GetTextColor(ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL));
344
345         // Draw Check
346         if (__pProperty->IsItemCheckedStyle())
347         {
348                 DrawCheck(*pCanvas, ratio);
349         }
350
351         if (__overlayBitmaps.size() > 0)
352         {
353                 DrawOverlayBitmaps();
354         }
355
356         __isDrawn = false;
357         delete pCanvas;
358
359         return E_SUCCESS;
360 }
361
362 result
363 _IconListItem::DrawBitmap(Canvas& canvas, Bitmap& bitmap, float ratio)
364 {
365         FloatRectangle rect = __pProperty->GetItemBitmapBounds();
366         float borderWidth = __pProperty->GetDropShadowBorderWidth();
367         if (__pProperty->GetBorderStyle() == ICON_LIST_VIEW_ITEM_BORDER_STYLE_SHADOW || __pProperty->GetBorderStyle() == ICON_LIST_VIEW_ITEM_BORDER_STYLE_OUTLINE)
368         {
369                 rect.x += CoordinateSystem::ConvertToLogicalX(1.0f);
370                 rect.y += CoordinateSystem::ConvertToLogicalY(1.0f);
371                 rect.width -= CoordinateSystem::ConvertToLogicalX(borderWidth);
372                 rect.height -= CoordinateSystem::ConvertToLogicalY(borderWidth);
373         }
374
375         float width = rect.width;
376         float height = rect.height;
377
378         // Adjust Bitmap Ratio
379         if (__pProperty->IsItemBitmapAsAspectRatio())
380         {
381                 width = bitmap.GetWidthF() * rect.height / bitmap.GetHeightF();
382                 height = bitmap.GetHeightF() * rect.width / bitmap.GetWidthF();
383                 if (!_FloatCompareLE(width, rect.width))
384                 {
385                         width = rect.width;
386                 }
387
388                 if (!_FloatCompareLE(height, rect.height))
389                 {
390                         height = rect.height;
391                 }
392         }
393
394         // Resize & Set Alpha by Ratio(ratio < 1.0f)
395         if (!_FloatCompareGE(ratio, REFERENCE_FACTOR))
396         {
397                 width = width * ratio;
398                 height = height * ratio;
399                 bitmap.SetAlphaConstant(OPAQUE_ALPHA * ratio);
400         }
401
402         // Set bitmap bounds
403         rect.x += (rect.width - width) / 2.0f;
404         rect.y += (rect.height - height) / 2.0f;
405         rect.SetSize(width, height);
406
407         if (__pTouchAniVE == null)
408         {
409                 __pTouchAniVE = new (std::nothrow) _VisualElement();
410                 SysTryReturn(NID_UI_CTRL, __pTouchAniVE != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
411
412                 result r = __pTouchAniVE->Construct();
413                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
414
415                 __pTouchAniVE->SetRedrawOnResizeEnabled(false);
416                 __pTouchAniVE->SetImplicitAnimationEnabled(false);
417                 __pBase->AttachChild(*__pTouchAniVE);
418         }
419         __pTouchAniVE->SetBounds(FloatRectangle(rect.x, rect.y, width, height));
420         __pTouchAniVE->SetShowState(true);
421
422         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(bitmap))
423         {
424                 Canvas* pCanvas = __pTouchAniVE->GetCanvasN();
425                 SysTryReturn(NID_UI_CTRL, pCanvas != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
426                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
427                 pCanvas->Clear();
428                 pCanvas->DrawNinePatchedBitmap(FloatRectangle(0.0f, 0.0f, bitmap.GetWidthF(), bitmap.GetHeightF()), bitmap);
429                 delete pCanvas;
430         }
431         else
432         {
433                 String  imagePath = _BitmapImpl::GetInstance(bitmap)->GetFileName();
434
435                 if (!imagePath.IsEmpty())
436                 {
437                         __pTouchAniVE->SetImageSource(imagePath);
438                 }
439                 else
440                 {
441                         __pTouchAniVE->SetImageSource(L"");
442                         Canvas* pCanvas = __pTouchAniVE->GetCanvasN();
443                         SysTryReturn(NID_UI_CTRL, pCanvas != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
444                         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
445                         pCanvas->Clear();
446                         pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, width, height), bitmap);
447                         delete pCanvas;
448                 }
449         }
450
451         if (!_FloatCompareGE(ratio, REFERENCE_FACTOR))
452         {
453                 bitmap.SetAlphaConstant(OPAQUE_ALPHA);
454         }
455
456         return E_SUCCESS;
457 }
458
459 result
460 _IconListItem::DrawText(const Color& color)
461 {
462         if (__text.IsEmpty())
463         {
464                 return E_SUCCESS;
465         }
466
467         if (__pTextObject == null)
468         {
469                 __pTextObject = new (std::nothrow) TextObject;
470                 wchar_t* pStr = const_cast <wchar_t*>(__text.GetPointer());
471                 __pTextObject->Construct();
472                 TextSimple* pSimpleText = new (std::nothrow)TextSimple(pStr, __text.GetLength(), TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL);
473                 __pTextObject->AppendElement(*pSimpleText);
474
475                 __pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
476         }
477
478
479         switch (__pProperty->GetTextHorizontalAlignment())
480         {
481         case ALIGNMENT_LEFT:
482                 __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT);
483                 break;
484
485         case ALIGNMENT_CENTER:
486                 __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER);
487                 break;
488
489         case ALIGNMENT_RIGHT:
490                 __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_RIGHT);
491                 break;
492         }
493
494         __pTextObject->SetForegroundColor(color, 0, __pTextObject->GetTextLength());
495
496         Font* pFont = __pProperty->GetFont();
497         SysTryReturn(NID_UI_CTRL, pFont != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has been occurred.");
498
499         float textVerticalMargin = __pProperty->GetTextVerticalMargin();
500         float width = __pProperty->GetTextAreaWidth();
501
502         __pTextObject->SetFont(pFont, 0, __pTextObject->GetTextLength());
503         __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_WORD);
504         __pTextObject->SetBounds(FloatRectangle(0.0f, 0.0f, width, 1.0f));
505         __pTextObject->Compose();
506
507         float height = __pTextObject->GetTotalHeightF();
508         float leading = _FontImpl::GetInstance((*pFont))->GetLeadingF();
509         float textYPos = __pProperty->GetTextYPosition();
510         if (_FloatCompareGE(height, leading * 2))
511         {
512                 textYPos = __pProperty->GetTwoLineTextYPosition();
513                 height = (leading + textVerticalMargin) * 2;
514         }
515         else
516         {
517                 height += (textVerticalMargin * 2);
518         }
519
520         if (__pProperty->GetTextVerticalAlignment() == ICON_LIST_VIEW_ITEM_TEXT_VERTICAL_ALIGNMENT_INSIDE_BOTTOM)
521         {
522                 FloatDimension itemSize = __pProperty->GetItemSize();
523                 textYPos = itemSize.height - height - 1;
524         }
525
526         result r = GetLastResult();
527         FloatRectangle bounds(0.0f, 0.0f, width, height);
528         Canvas* pCanvas = null;
529
530         if (__pTextVE == null)
531         {
532                 __pTextVE = new (std::nothrow) _VisualElement();
533                 SysTryReturn(NID_UI_CTRL, __pTextVE != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
534
535                 result r = __pTextVE->Construct();
536                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS,  , r, "[%s] Propagating.", GetErrorMessage(r));
537
538                 __pTextVE->SetRedrawOnResizeEnabled(false);
539                 __pTextVE->SetImplicitAnimationEnabled(false);
540
541                 __pBase->AttachChild(*__pTextVE);
542         }
543
544         __pTextVE->SetBounds(FloatRectangle(__pProperty->GetTextXPosition(), textYPos, width, height));
545         __pTextVE->SetShowState(true);
546
547         pCanvas = __pTextVE->GetCanvasN();
548         SysTryReturn(NID_UI_CTRL, pCanvas != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
549
550         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
551         pCanvas->Clear();
552         pCanvas->FillRectangle(__pProperty->GetTextBackgroundColor(), bounds);
553
554         __pTextObject->SetBounds(bounds);
555         __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
556         delete pCanvas;
557
558         return E_SUCCESS;
559
560 CATCH:
561         __pTextVE->Destroy();
562         __pTextVE = null;
563
564         return r;
565 }
566
567 result
568 _IconListItem::DrawCheck(Canvas& canvas, float ratio)
569 {
570         const Bitmap* pBackgroundBitmap = null;
571         const Bitmap* pBitmap = null;
572         pBitmap = IsChecked() ? __pProperty->GetCheckBitmap() : null;
573         pBackgroundBitmap = IsItemEnabled() ? (IsChecked() ? __pProperty->GetCheckedBackgroundBitmap() : __pProperty->GetUnCheckedBackgroundBitmap()) : __pProperty->GetDisabledCheckBitmap();
574
575         if (pBackgroundBitmap == null)
576         {
577                 return E_SYSTEM;
578         }
579
580         float width = pBackgroundBitmap->GetWidthF();
581         float height = pBackgroundBitmap->GetHeightF();
582         float checkMargin = __pProperty->GetCheckMargin();
583
584         FloatRectangle bitmapBounds = __pProperty->GetItemBitmapBounds();
585         FloatPoint checkPosition(0.0f, 0.0f);
586         switch (__pProperty->GetCheckBoxPosition())
587         {
588         case ICON_LIST_VIEW_CHECK_BOX_POSITION_TOP_LEFT:
589                 checkPosition.x += checkMargin;
590                 checkPosition.y += checkMargin;
591                 break;
592
593         case ICON_LIST_VIEW_CHECK_BOX_POSITION_TOP_RIGHT:
594                 checkPosition.x += bitmapBounds.width - checkMargin - width;
595                 checkPosition.y += checkMargin;
596                 break;
597
598         case ICON_LIST_VIEW_CHECK_BOX_POSITION_BOTTOM_LEFT:
599                 checkPosition.x += checkMargin;
600                 checkPosition.y += bitmapBounds.height - checkMargin - height;
601                 break;
602
603         case ICON_LIST_VIEW_CHECK_BOX_POSITION_BOTTOM_RIGHT:
604                 checkPosition.x += bitmapBounds.width - checkMargin - width;
605                 checkPosition.y += bitmapBounds.height - checkMargin - height;
606                 break;
607         }
608
609         if (__pCheckBgVE == null)
610         {
611                 __pCheckBgVE = new (std::nothrow) _VisualElement();
612                 SysTryReturn(NID_UI_CTRL, __pCheckBgVE != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
613
614                 result r = __pCheckBgVE->Construct();
615                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
616
617                 __pCheckBgVE->SetRedrawOnResizeEnabled(false);
618                 __pCheckBgVE->SetImplicitAnimationEnabled(false);
619                 __pBase->AttachChild(*__pCheckBgVE);
620                 __pCheckBgVE->SetBounds(FloatRectangle(checkPosition.x, checkPosition.y, width, height));
621                 __pCheckBgVE->SetShowState(true);
622         }
623
624         Canvas* pCheckBgCanvas = __pCheckBgVE->GetCanvasN();
625         SysTryReturn(NID_UI_CTRL, pCheckBgCanvas != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
626
627         pCheckBgCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
628         pCheckBgCanvas->Clear();
629
630         // draw background of check box
631         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBackgroundBitmap))
632         {
633                 pCheckBgCanvas->DrawNinePatchedBitmap(FloatRectangle(0.0f, 0.0f, width, height), *pBackgroundBitmap);
634         }
635         else
636         {
637                 pCheckBgCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, width, height), *pBackgroundBitmap);
638         }
639
640         // draw check image
641         if (__pCheckVE == null)
642         {
643                 __pCheckVE = new (std::nothrow) _VisualElement();
644                 SysTryReturn(NID_UI_CTRL, __pCheckVE != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
645
646                 result r = __pCheckVE->Construct();
647                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
648
649                 __pCheckVE->SetRedrawOnResizeEnabled(false);
650                 __pCheckVE->SetImplicitAnimationEnabled(false);
651                 __pBase->AttachChild(*__pCheckVE);
652
653                 __pCheckVE->SetBounds(FloatRectangle(checkPosition.x, checkPosition.y, width, height));
654                 __pCheckVE->SetShowState(true);
655         }
656
657         Canvas* pCanvas = __pCheckVE->GetCanvasN();
658         SysTryReturn(NID_UI_CTRL, pCanvas != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
659
660         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
661         pCanvas->Clear();
662
663         if (pBitmap != null)
664         {
665                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBitmap))
666                 {
667                         pCanvas->DrawNinePatchedBitmap(FloatRectangle(0.0f, 0.0f, pBitmap->GetWidthF(), pBitmap->GetHeightF()), *pBitmap);
668                 }
669                 else
670                 {
671                         pCanvas->DrawBitmap(FloatRectangle(0.0f, 0.0f, pBitmap->GetWidthF() * ratio, pBitmap->GetHeightF()), *pBitmap, FloatRectangle(0.0f, 0.0f, pBitmap->GetWidthF() * ratio, pBitmap->GetHeightF()));
672                 }
673         }
674
675         delete pCheckBgCanvas;
676         delete pCanvas;
677
678         return E_SUCCESS;
679 }
680
681 result
682 _IconListItem::DrawOverlayBitmaps(void)
683 {
684         result r = E_SUCCESS;
685         FloatRectangle bitmapBounds = __pProperty->GetItemBitmapBounds();
686         Canvas* pCanvas = null;
687         for (int i = 0, n = __overlayBitmaps.size(); i < n; i++)
688         {
689                 _OverlayBitmap* pOverlayBitmap = __overlayBitmaps.at(i);
690                 float overlayMargin = __pProperty->GetOverlayMargin();
691                 if (pOverlayBitmap->pBitmap != null)
692                 {
693                         FloatRectangle overlayBitmapBounds = GetOverlayBitmapBounds(pOverlayBitmap, bitmapBounds, overlayMargin);
694
695                         _VisualElement* pVE = null;
696                         if (pOverlayBitmap->pVE == null)
697                         {
698                                 pVE = new (std::nothrow) _VisualElement();
699                                 SysTryReturn(NID_UI_CTRL, pVE != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
700
701                                 r = pVE->Construct();
702                                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
703
704                                 pVE->SetRedrawOnResizeEnabled(false);
705                                 pVE->SetImplicitAnimationEnabled(false);
706
707                                 __pBase->AttachChild(*pVE);
708                                 pOverlayBitmap->pVE = pVE;
709                         }
710                         else
711                         {
712                                 pVE = pOverlayBitmap->pVE;
713                         }
714
715                         pVE->SetBounds(overlayBitmapBounds);
716                         pVE->SetShowState(true);
717
718                         pCanvas = pVE->GetCanvasN();
719                         SysTryReturn(NID_UI_CTRL, pCanvas != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
720
721                         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
722                         pCanvas->Clear();
723
724                         overlayBitmapBounds.SetPosition(0.0f, 0.0f);
725                         Bitmap* pBitmap = pOverlayBitmap->pBitmap;
726                         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pBitmap))
727                         {
728                                 pCanvas->DrawNinePatchedBitmap(overlayBitmapBounds, *pBitmap);
729                         }
730                         else
731                         {
732                                 pCanvas->DrawBitmap(overlayBitmapBounds, *pBitmap);
733                         }
734                         delete pCanvas;
735                 }
736         }
737         return r;
738 }
739
740 result
741 _IconListItem::DrawBorder(Tizen::Graphics::Canvas& canvas, bool isFocused)
742 {
743         if (__pFocusVE == null)
744         {
745                 __pFocusVE = new (std::nothrow) _VisualElement();
746                 SysTryReturn(NID_UI_CTRL, __pFocusVE != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
747
748                 result r = __pFocusVE->Construct();
749                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
750
751                 __pFocusVE->SetRedrawOnResizeEnabled(false);
752                 __pFocusVE->SetImplicitAnimationEnabled(false);
753                 __pBase->AttachChild(*__pFocusVE);
754
755                 FloatRectangle focusUiBounds = __pProperty->GetItemBitmapBounds();
756                 __pFocusVE->SetBounds(FloatRectangle(focusUiBounds.x, focusUiBounds.y, focusUiBounds.width, focusUiBounds.height));
757         }
758
759         if (isFocused)
760         {
761                 Canvas* pCanvas = __pFocusVE->GetCanvasN();
762                 SysTryReturn(NID_UI_CTRL, pCanvas != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
763
764                 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
765                 pCanvas->Clear();
766
767                 const Bitmap* pShadowBitmap = __pProperty->GetFocusBitmap();
768
769                 if (pShadowBitmap != null)
770                 {
771                         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pShadowBitmap))
772                         {
773                                 pCanvas->DrawNinePatchedBitmap(__pProperty->GetDropShadowBitmapBounds(), *pShadowBitmap);
774                         }
775                         else
776                         {
777                                 pCanvas->DrawBitmap(__pProperty->GetDropShadowBitmapBounds(), *pShadowBitmap);
778                         }
779                         __pFocusVE->SetShowState(true);
780                 }
781                 delete pCanvas;
782         }
783         else
784         {
785                 switch (__pProperty->GetBorderStyle())
786                 {
787                         case ICON_LIST_VIEW_ITEM_BORDER_STYLE_NONE:
788                                 break;
789
790                         case ICON_LIST_VIEW_ITEM_BORDER_STYLE_OUTLINE:
791                         {
792                                 // Draw Outline
793                                 canvas.SetForegroundColor(__pProperty->GetBorderColor(isFocused));
794                                 canvas.SetLineWidth(1.0f);
795                                 FloatDimension dim = __pProperty->GetItemSize();
796                                 canvas.DrawRectangle(FloatRectangle(0.0f, 0.0f, dim.width, dim.height));
797                                 break;
798                         }
799
800                         case ICON_LIST_VIEW_ITEM_BORDER_STYLE_SHADOW:
801                         {
802                                 // Draw Drop-Shadow Effect
803                                 if (__pProperty->GetDropShadowBitmap() != null)
804                                 {
805                                         const Bitmap* pShadowBitmap = __pProperty->GetDropShadowBitmap();
806                                         if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pShadowBitmap))
807                                         {
808                                                 canvas.DrawNinePatchedBitmap(__pProperty->GetDropShadowBitmapBounds(), *pShadowBitmap);
809                                         }
810                                         else
811                                         {
812                                                 canvas.DrawBitmap(__pProperty->GetDropShadowBitmapBounds(), *pShadowBitmap);
813                                         }
814                                 }
815                                 break;
816                         }
817                 }
818                 __pFocusVE->SetShowState(false);
819         }
820         return E_SUCCESS;
821 }
822
823 result
824 _IconListItem::SetText(const String* pText)
825 {
826         if (pText == null)
827         {
828                 __text = String(L"");
829                 return E_SUCCESS;
830         }
831
832         __text = *pText;
833         __isDrawn = false;
834
835         return E_SUCCESS;
836 }
837
838 const String&
839 _IconListItem::GetText(void) const
840 {
841         return __text;
842 }
843
844 result
845 _IconListItem::SetBitmap(IconListViewItemDrawingStatus status, const Bitmap* pBitmap)
846 {
847         if (ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL > status || ICON_LIST_VIEW_ITEM_DRAWING_STATUS_HIGHLIGHTED < status)
848         {
849                 SysLogException(NID_UI_CTRL, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used.");
850                 return E_INVALID_ARG;
851         }
852
853         SysTryReturn(NID_UI_CTRL, (status != ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL || pBitmap != null), E_INVALID_ARG,
854                         E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. The normal bitmap must not be null.");
855
856         result r = E_SUCCESS;
857         Bitmap* pCopiedBitmap = null;
858         if (pBitmap != null)
859         {
860                 pCopiedBitmap = _BitmapImpl::CloneN(*pBitmap);
861                 r = GetLastResult();
862         }
863
864         if (r == E_SUCCESS)
865         {
866                 delete __pBitmap[status];
867                 __pBitmap[status] = pCopiedBitmap;
868         }
869         else
870         {
871                 delete pCopiedBitmap;
872         }
873
874         __isDrawn = false;
875
876         return r;
877 }
878
879 const Bitmap*
880 _IconListItem::GetBitmap(IconListViewItemDrawingStatus status) const
881 {
882         SysTryReturn(NID_UI_CTRL,
883                         (status > ICON_LIST_VIEW_ITEM_DRAWING_STATUS_NORMAL && status < ICON_LIST_VIEW_ITEM_DRAWING_STATUS_HIGHLIGHTED),
884                         null, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used.");
885
886         SetLastResult(E_SUCCESS);
887         return __pBitmap[status];
888 }
889
890 _VisualElement*
891 _IconListItem::GetVisualElement(void) const
892 {
893         return __pBase;
894 }
895
896 void
897 _IconListItem::ClearVisualElement(void)
898 {
899         __isDrawn = false;
900 }
901
902 result
903 _IconListItem::SetDrawingProperty(_IconListItemDrawingProperty& property)
904 {
905         __pProperty = &property;
906
907         return E_SUCCESS;
908 }
909
910 void
911 _IconListItem::SetChecked(bool checked)
912 {
913         if (IsChecked() == checked)
914         {
915                 return;
916         }
917
918         _ListItemCommon::SetChecked(checked);
919         __isDrawn = false;
920 }
921
922 void
923 _IconListItem::SetItemIndex(int groupIndex, int itemIndex)
924 {
925         __itemIndex = itemIndex;
926 }
927
928 void
929 _IconListItem::GetItemIndex(int& groupIndex, int& itemIndex) const
930 {
931         groupIndex = DEFAULT_GROUP_INDEX;
932         itemIndex = __itemIndex;
933 }
934
935 result
936 _IconListItem::SetOverlayBitmap(int overlayBitmapId, const Bitmap* pOverlayBitmap, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment)
937 {
938         result r = E_SUCCESS;
939
940         SysTryReturn(NID_UI_CTRL, horizontalAlignment >= ALIGNMENT_LEFT && horizontalAlignment <= ALIGNMENT_RIGHT, E_INVALID_ARG,
941                         E_INVALID_ARG, "[E_INVALID_ARG] The horizontal alignment is inappropriate.");
942         SysTryReturn(NID_UI_CTRL, verticalAlignment >= ALIGNMENT_TOP && verticalAlignment <= ALIGNMENT_BOTTOM, E_INVALID_ARG,
943                         E_INVALID_ARG, "[E_INVALID_ARG] The vertical alignment is inappropriate.");
944
945         bool found = false;
946         Bitmap* pCopiedBitmap = null;
947         for (int i = 0, n = __overlayBitmaps.size() ; i < n; i++)
948         {
949                 _OverlayBitmap* pCurrentOverlayBitmap = __overlayBitmaps.at(i);
950                 if (pCurrentOverlayBitmap->horizontalAlignment == horizontalAlignment && pCurrentOverlayBitmap->verticalAlignment == verticalAlignment)
951                 {
952                         found = true;
953                         if (pOverlayBitmap == null)
954                         {
955                                 // delete overlay bitmap
956                                 delete pCurrentOverlayBitmap->pBitmap;
957                                 pCurrentOverlayBitmap->pBitmap = null;
958
959                                 if (pCurrentOverlayBitmap->pVE != null)
960                                 {
961                                         pCurrentOverlayBitmap->pVE->Destroy();
962                                         pCurrentOverlayBitmap->pVE = null;
963                                 }
964                                 __overlayBitmaps.erase(__overlayBitmaps.begin() + i);
965                                 delete pCurrentOverlayBitmap;
966                         }
967                         else
968                         {
969                                 // replace overlay bitmap
970                                 pCopiedBitmap = null;
971                                 pCopiedBitmap = _BitmapImpl::CloneN(*pOverlayBitmap);
972                                 r = GetLastResult();
973
974                                 if (r == E_SUCCESS)
975                                 {
976                                         delete pCurrentOverlayBitmap->pBitmap;
977                                         pCurrentOverlayBitmap->pBitmap = pCopiedBitmap;
978                                         pCurrentOverlayBitmap->id = overlayBitmapId;
979                                 }
980                         }
981                         break;
982                 }
983         }
984
985         if (!found)
986         {
987                 if (pOverlayBitmap != null)
988                 {
989                         // add overlay bitmap
990                         pCopiedBitmap = null;
991                         pCopiedBitmap = _BitmapImpl::CloneN(*pOverlayBitmap);
992                         r = GetLastResult();
993                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
994
995                         _OverlayBitmap* pNewOverlayBitmap = new (std::nothrow) _OverlayBitmap();
996                         pNewOverlayBitmap->id = overlayBitmapId;
997                         pNewOverlayBitmap->pBitmap = pCopiedBitmap;
998                         pNewOverlayBitmap->horizontalAlignment = horizontalAlignment;
999                         pNewOverlayBitmap->verticalAlignment = verticalAlignment;
1000                         pNewOverlayBitmap->pVE = null;
1001
1002                         __overlayBitmaps.push_back(pNewOverlayBitmap);
1003                 }
1004         }
1005
1006         SetLastResult(r);
1007         return r;
1008 }
1009
1010 int
1011 _IconListItem::GetOverlayBitmapIdFromPosition(FloatPoint& position) const
1012 {
1013         if (__overlayBitmaps.size() <= 0)
1014         {
1015                 return -1;
1016         }
1017
1018         FloatRectangle bitmapBounds = __pProperty->GetItemBitmapBounds();
1019         for (int i = __overlayBitmaps.size() - 1; i >= 0; i--)
1020         {
1021                 _OverlayBitmap* pOverlayBitmap = __overlayBitmaps.at(i);
1022                 if (pOverlayBitmap != null)
1023                 {
1024                         float overlayMargin = __pProperty->GetOverlayMargin();
1025                         if (pOverlayBitmap->pBitmap != null)
1026                         {
1027                                 FloatRectangle rect = GetOverlayBitmapBounds(pOverlayBitmap, bitmapBounds, overlayMargin);
1028                                 if (rect.Contains(position))
1029                                 {
1030                                         return pOverlayBitmap->id;
1031                                 }
1032                         }
1033                 }
1034         }
1035         return -1;
1036 }
1037
1038 _VisualElement*
1039 _IconListItem::GetItemVisualElement(void) const
1040 {
1041         return __pBase;
1042 }
1043
1044 _AccessibilityElement*
1045 _IconListItem::SetItemAccessibilityElement(FloatRectangle& rect, int itemIndex, const _AccessibilityElement& pElement,  bool swapElement)
1046 {
1047         if (__pAccessibilityElement == null)
1048         {
1049                 __pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
1050                 SysTryReturn(NID_UI_CTRL, __pAccessibilityElement != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1051
1052                 __pAccessibilityElement->SetTraitWithStringId("IDS_TPLATFORM_BODY_IMAGE");
1053         }
1054
1055         if (itemIndex >= 0)
1056         {
1057                 if (swapElement)
1058                 {
1059                         __pAccessibilityElement = const_cast<_AccessibilityElement*>(&pElement);
1060                 }
1061
1062                 __pAccessibilityElement->Activate(true);
1063                 if (IsChecked())
1064                 {
1065                         __pAccessibilityElement->SetValueWithStringId("IDS_TPLATFORM_BODY_SELECTED_T_TTS");
1066                 }
1067                 __pAccessibilityElement->SetName(L"IconListViewItem" + Integer::ToString(itemIndex + 1));
1068                 __pAccessibilityElement->SetLabel(GetText());
1069
1070                 if (__pAccessibilityElement->GetBounds() != rect)
1071                 {
1072                         __pAccessibilityElement->SetBounds(rect);
1073                 }
1074         }
1075
1076         return __pAccessibilityElement;
1077 }
1078
1079 _AccessibilityElement*
1080 _IconListItem::GetItemAccessibilityElement(void)
1081 {
1082         return __pAccessibilityElement;
1083 }
1084
1085 FloatRectangle
1086 _IconListItem::GetOverlayBitmapBounds(_OverlayBitmap* pOverlayBitmap, const FloatRectangle& parentRect, float overlayMargin) const
1087 {
1088         float x = parentRect.x;
1089         float y = parentRect.y;
1090         float width = pOverlayBitmap->pBitmap->GetWidthF();
1091         float height = pOverlayBitmap->pBitmap->GetHeightF();
1092
1093         switch (pOverlayBitmap->horizontalAlignment)
1094         {
1095         case ALIGNMENT_LEFT:
1096                 x += overlayMargin;
1097                 break;
1098         case ALIGNMENT_CENTER:
1099                 x += (parentRect.width - width) / 2;
1100                 break;
1101         case ALIGNMENT_RIGHT:
1102                 x += (parentRect.width - width - overlayMargin);
1103                 break;
1104         }
1105
1106         switch (pOverlayBitmap->verticalAlignment)
1107         {
1108         case ALIGNMENT_TOP:
1109                 y += overlayMargin;
1110                 break;
1111         case ALIGNMENT_MIDDLE:
1112                 y += (parentRect.height - height) / 2;
1113                 break;
1114         case ALIGNMENT_BOTTOM:
1115                 y += (parentRect.height - height - overlayMargin);
1116                 break;
1117         }
1118
1119         return FloatRectangle(x, y, width, height);
1120 }
1121
1122 bool
1123 _IconListItem::IsDrawn(void) const
1124 {
1125         return __isDrawn;
1126 }
1127
1128 Tizen::Ui::Animations::_VisualElement*
1129 _IconListItem::GetSelectAniVisualElement(void) const
1130 {
1131         return __pTouchAniVE;
1132 }
1133
1134 Tizen::Ui::Animations::_VisualElement*
1135 _IconListItem::GetCheckAniVisualElement(void) const
1136 {
1137         return __pCheckVE;
1138 }
1139
1140 Tizen::Ui::Animations::_VisualElement*
1141 _IconListItem::GetCheckBGVisualElement(void) const
1142 {
1143         return __pCheckBgVE;
1144 }
1145 }}} // Tizen::Ui::Controls