2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://floralicense.org/license/
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.
18 * @file FUiCtrl_ContextMenuGridPresenter.cpp
19 * @brief This is the implementation file for the _ContextMenuGridPresenter class.
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include <FGrp_CanvasImpl.h>
25 #include <FGrp_TextTextSimple.h>
26 #include "FUiCtrl_ContextMenuGridPresenter.h"
27 #include "FUiCtrl_IActionEventListener.h"
28 #include "FUiCtrl_ActionEvent.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUi_AccessibilityContainer.h"
31 #include "FUi_AccessibilityElement.h"
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Graphics::_Text;
39 static const int MAX_COUNT_PER_LINE = 5;
40 static const int MIN_COUNT_PER_LINE = 3;
41 static const int MAX_LINE_COUNT = 32;
43 namespace Tizen { namespace Ui { namespace Controls
46 _ContextMenuGridPresenter::_ContextMenuGridPresenter(_ContextMenu* pContextMenu)
47 : __pContextMenu(pContextMenu)
51 , __layoutSize(Dimension(0, 0))
52 , __touchOutRect(false)
61 , __screenTopMargin(0)
62 , __screenBottomMargin(0)
63 , __screenLeftMargin(0)
64 , __screenRightMargin(0)
73 , __itemBitmapWidth(0)
74 , __itemBitmapHeight(0)
81 _ContextMenuGridPresenter::~_ContextMenuGridPresenter(void)
83 __pContextMenu = null;
90 __pTextObject->RemoveAll();
100 _ContextMenuGridPresenter::Install(void)
102 result r = E_SUCCESS;
106 __pModel = new (std::nothrow) _ContextMenuModel;
107 SysTryCatch(NID_UI_CTRL, __pModel != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
109 r = __pModel->Construct();
110 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create data instance.");
112 __pFont = __pContextMenu->GetFallbackFont();
114 SysTryCatch(NID_UI_CTRL, (__pFont != null), , r, "[%s] Propagating.", GetErrorMessage(r));
116 __pTextObject = new (std::nothrow) TextObject();
117 SysTryCatch(NID_UI_CTRL, __pTextObject, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create data instance.");
119 __pTextObject->Construct();
120 __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
121 __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
122 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
132 delete __pTextObject;
133 __pTextObject = null;
140 _ContextMenuGridPresenter::LoadShape(void)
142 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __itemWidth);
143 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_MAX_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __itemMaxWidth);
144 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __itemHeight);
145 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_TEXT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __itemTextMargin);
146 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_GAP, _CONTROL_ORIENTATION_PORTRAIT, __itemGap);
148 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_MIN_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __minWidth);
149 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __topMargin);
150 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __bottomMargin);
151 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __leftMargin);
152 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __rightMargin);
154 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, __itemFontSize);
155 GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenTopMargin);
156 GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenBottomMargin);
157 GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenLeftMargin);
158 GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenRightMargin);
159 GET_SHAPE_CONFIG(CONTEXTMENU::ANCHOR_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __arrowMargin);
160 GET_SHAPE_CONFIG(CONTEXTMENU::ANCHOR_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __arrowWidth);
161 GET_SHAPE_CONFIG(CONTEXTMENU::ANCHOR_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __arrowHeight);
162 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ICON_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __itemBitmapWidth);
163 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ICON_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __itemBitmapHeight);
164 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_DIVIDER_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __dividerHeight);
168 _ContextMenuGridPresenter::CreateItem(const Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap, const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
170 _ContextMenuItem* pItem = _ContextMenuItem::CreateContextMenuItemN();
171 SysTryReturn(NID_UI_CTRL, pItem != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
173 ContextMenuItemDrawingType itemType = CONTEXT_MENU_ITEM_DRAWING_TYPE_NONE;
174 result r = E_SUCCESS;
176 if (text.GetLength() != 0)
178 r = pItem->SetText(text);
185 itemType = CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT;
189 if (pNormalBitmap == null && pPressedBitmap == null)
195 if (pNormalBitmap != null)
197 r = pItem->SetBitmap(CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL, pNormalBitmap);
205 if (pPressedBitmap != null)
207 r = pItem->SetBitmap(CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED, pPressedBitmap);
215 itemType = CONTEXT_MENU_ITEM_DRAWING_TYPE_BITMAP;
218 pItem->SetType(itemType);
219 pItem->SetActionId(actionId);
221 // calculate item size
228 _ContextMenuGridPresenter::AddItem(const Base::String& text, int actionId, const Tizen::Graphics::Bitmap* normalBitmap,
229 const Tizen::Graphics::Bitmap* pPressedBitmap,
230 const Tizen::Graphics::Bitmap* pHighlightedBitmap)
232 _ContextMenuItem* pItem = CreateItem(text, actionId, normalBitmap, pPressedBitmap, pHighlightedBitmap);
233 SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
235 result r = __pModel->AddItem(pItem);
240 SysTryReturn(NID_UI_CTRL, false, r, r, "Failed to add item.");
248 _ContextMenuGridPresenter::InsertItem(int index, const Tizen::Base::String& text, int actionId,
249 const Tizen::Graphics::Bitmap* normalBitmap, const Tizen::Graphics::Bitmap* pPressedBitmap,
250 const Tizen::Graphics::Bitmap* pHighlightedBitmap)
252 if (text.GetLength() == 0 && normalBitmap == null && pPressedBitmap == null)
254 return E_INVALID_ARG;
257 _ContextMenuItem* pItem = null;
259 pItem = CreateItem(text, actionId, normalBitmap, pPressedBitmap, pHighlightedBitmap);
260 SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
262 result r = __pModel->InsertItem(pItem, index);
266 SysTryReturn(NID_UI_CTRL, false, r, r, "Failed to add item.");
273 _ContextMenuGridPresenter::SetItem(int index, const Tizen::Base::String& text, int actionId,
274 const Tizen::Graphics::Bitmap* normalBitmap, const Tizen::Graphics::Bitmap* pPressedBitmap,
275 const Tizen::Graphics::Bitmap* pHighlightedBitmap)
277 if (text.GetLength() == 0 && normalBitmap == null && pPressedBitmap == null)
279 return E_INVALID_ARG;
282 _ContextMenuItem* pItem = null;
283 pItem = CreateItem(text, actionId, normalBitmap, pPressedBitmap, pHighlightedBitmap);
284 SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
286 result r = __pModel->SetItem(pItem, index);
290 SysTryReturn(NID_UI_CTRL, false, r, r, "Failed to add item.");
297 _ContextMenuGridPresenter::DeleteItem(int index)
299 int itemCount = __pContextMenu->GetItemCount();
301 if (itemCount <= 0 || index >= itemCount || index < 0)
303 SysLogException(NID_UI_CTRL, E_INVALID_STATE, "Invalid argument.");
304 return E_INVALID_STATE;
307 result r = __pModel->RemoveItem(index);
308 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to delete item.");
314 _ContextMenuGridPresenter::DeleteItemAll(void)
316 int itemCount = __pContextMenu->GetItemCount();
320 SysLogException(NID_UI_CTRL, E_INVALID_STATE, "Invalid argument.");
321 return E_INVALID_STATE;
324 result r = __pModel->RemoveAllItem();
325 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to delete item all.");
332 _ContextMenuGridPresenter::CalculateShowItemCount(void)
334 return __pContextMenu->GetShowItemCount();
338 _ContextMenuGridPresenter::SetItemSize(_ContextMenuItem* pItem)
340 ContextMenuItemDrawingType itemType = pItem->GetType();
342 if (itemType == CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT)
344 int textMargin = __itemTextMargin;
345 int minWidth = __itemWidth;
346 int maxWidth = __itemMaxWidth;
348 String text = pItem->GetText();
350 textMargin = textMargin * 2;
351 minWidth = minWidth - textMargin;
352 maxWidth = maxWidth - textMargin;
354 Dimension textArea(0, 0);
355 Dimension itemSize(0, 0);
357 __pFont->GetTextExtent(text, text.GetLength(), textArea);
359 if (textArea.width < minWidth)
361 textArea.width = minWidth;
364 if (textArea.width > maxWidth)
366 textArea.width = maxWidth;
369 itemSize.width = textArea.width + textMargin;
370 itemSize.height = __itemHeight;
372 pItem->SetSize(itemSize);
375 else if (itemType == CONTEXT_MENU_ITEM_DRAWING_TYPE_BITMAP)
377 Dimension itemSize(__itemWidth, __itemHeight);
379 pItem->SetSize(itemSize);
384 _ContextMenuGridPresenter::CalculateWindowRect(void)
386 result r = E_SUCCESS;
392 AdjustItemPosition();
398 _ContextMenuGridPresenter::ApplyColorProperty(void)
405 _ContextMenuGridPresenter::CalculateRect(void)
407 Tizen::Graphics::Rectangle windowRect = Tizen::Graphics::Rectangle(0, 0, 0, 0); // Window Rect is bodyRect + arrowRect
408 Tizen::Graphics::Rectangle bodyRect = Tizen::Graphics::Rectangle(0, 0, 0, 0); // ContextMenu rect except arrowRect
409 Tizen::Graphics::Rectangle arrowRect = Tizen::Graphics::Rectangle(0, 0, 0, 0); // Arrow rect of the ContextMenu
411 int bodyTopMargin = __topMargin;
412 int bodyBottomMargin = __bottomMargin;
413 int bodyLeftMargin = __leftMargin;
414 int bodyRightMargin = __rightMargin;
416 int screenTopMargin = __screenTopMargin;
417 int screenBottomMargin = __screenBottomMargin;
418 int screenLeftMargin = __screenLeftMargin;
419 int screenRightMargin = __screenRightMargin;
420 int arrowMargin = __arrowMargin;
422 Point anchorPosition = __pContextMenu->GetAnchorPosition();
423 // Point arrowPosition;
425 Dimension screen = _ControlManager::GetInstance()->GetScreenSize();
426 if (__pContextMenu->GetLayout() == _CONTROL_ORIENTATION_LANDSCAPE)
428 screen.SetSize(screen.height, screen.width);
431 // calculate arrow area
432 enum ContextMenuCoreDropPosition dropPosition = __pContextMenu->GetDropPosition();
433 if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_UP || dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_DOWN) // down, up Arrow
435 arrowRect.width = __arrowWidth;
436 arrowRect.height = __arrowHeight;
438 else // left, right Arrow
440 arrowRect.width = __arrowHeight;
441 arrowRect.height = __arrowWidth;
444 // calculate body rect
445 // calculate real drawing margin for contextmenu
446 int leftMargin = screenLeftMargin;
447 int rightMargin = screen.width - screenRightMargin;
448 int topMargin = screenTopMargin;
449 int bottomMargin = screen.height - screenBottomMargin;
451 if (__pModel->GetItemCount() <= 0)
453 __layoutSize.width = __itemWidth;
454 __layoutSize.height = __itemHeight;
457 bodyRect.width = __layoutSize.width + bodyLeftMargin + bodyRightMargin;
458 bodyRect.height = __layoutSize.height + bodyTopMargin + bodyBottomMargin;
460 // calculate the position of the arrow and body rect.
461 if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_UP) // down Arrow
463 // Check touch position
464 // - Check left margin
465 if (anchorPosition.x <= leftMargin + arrowRect.width)
467 anchorPosition.x = leftMargin + arrowRect.width;
469 // - check right margin
470 if (anchorPosition.x >= (rightMargin - arrowRect.width))
472 anchorPosition.x = rightMargin - arrowRect.width;
474 // - check top margin
475 if (anchorPosition.y <= topMargin)
477 anchorPosition.y = topMargin + bodyRect.height;
479 // - check bottom margin
480 if (anchorPosition.y >= bottomMargin)
482 anchorPosition.y = bottomMargin;
485 // Set body position x
486 bodyRect.x = anchorPosition.x - (bodyRect.width / 2);
487 // - Check left margin
488 if (bodyRect.x <= leftMargin)
490 bodyRect.x = leftMargin;
492 // - check right margin
493 if ((bodyRect.x + bodyRect.width) >= rightMargin)
495 bodyRect.x = rightMargin - bodyRect.width;
497 // Set body position y
498 bodyRect.y = anchorPosition.y - arrowRect.height - bodyRect.height;
499 // - check top margin
500 if (bodyRect.y <= topMargin)
502 bodyRect.y = topMargin;
505 windowRect.x = bodyRect.x;
506 windowRect.y = bodyRect.y;
507 windowRect.width = bodyRect.width;
508 windowRect.height = bodyRect.height + arrowRect.height;
511 bodyRect.y = arrowMargin;
513 // Set arrow position
514 arrowRect.x = anchorPosition.x - (arrowRect.width / 2) - windowRect.x;
515 arrowRect.y = bodyRect.height;
517 else if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_DOWN) // up Arrow
519 // Check touch position
520 // - Check left margin
521 if (anchorPosition.x <= leftMargin + arrowRect.width)
523 anchorPosition.x = leftMargin + arrowRect.width;
525 // - check right margin
526 if (anchorPosition.x >= (rightMargin - arrowRect.width))
528 anchorPosition.x = rightMargin - arrowRect.width;
530 // - check top margin
531 if (anchorPosition.y <= topMargin)
533 anchorPosition.y = topMargin;
535 // - check bottom margin
536 if (anchorPosition.y >= bottomMargin)
538 anchorPosition.y = bottomMargin;
541 // Set body position x
542 bodyRect.x = anchorPosition.x - (bodyRect.width / 2);
543 // - Check left margin
544 if (bodyRect.x <= leftMargin)
546 bodyRect.x = leftMargin;
548 // - check right margin
549 if ((bodyRect.x + bodyRect.width) >= rightMargin)
551 bodyRect.x = rightMargin - bodyRect.width;
553 // Set body position y
554 bodyRect.y = anchorPosition.y + arrowRect.height;
555 // - Check bottom margin
556 if ((bodyRect.y + bodyRect.height) >= bottomMargin)
558 bodyRect.y = bottomMargin - bodyRect.height;
561 windowRect.x = bodyRect.x;
562 windowRect.y = bodyRect.y - arrowRect.height;
563 windowRect.width = bodyRect.width;
564 windowRect.height = bodyRect.height + arrowRect.height;
567 bodyRect.y = arrowRect.height - arrowMargin;
569 // Set arrow position
570 arrowRect.x = anchorPosition.x - (arrowRect.width / 2) - windowRect.x;
573 else if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_LEFT) // right Arrow
575 // Check touch position
576 // - Check left margin
577 if (anchorPosition.x <= leftMargin)
579 anchorPosition.x = leftMargin + bodyRect.width;
581 // - check right margin
582 if (anchorPosition.x >= rightMargin)
584 anchorPosition.x = rightMargin;
586 // - Check top margin
587 if (anchorPosition.y <= topMargin + arrowRect.height)
589 anchorPosition.y = topMargin + arrowRect.height;
591 // - check bottom margin
592 if (anchorPosition.y >= (bottomMargin - arrowRect.height))
594 anchorPosition.y = bottomMargin - arrowRect.height;
597 // Set body position x
598 bodyRect.x = anchorPosition.x - arrowRect.width - bodyRect.width;
599 // - Check left margin
600 if (bodyRect.x <= leftMargin)
602 bodyRect.x = leftMargin;
604 // - check right margin
605 if ((bodyRect.x + bodyRect.width + arrowRect.width) >= rightMargin)
607 bodyRect.x = rightMargin - bodyRect.width - arrowRect.width;
610 // Set body position y
611 bodyRect.y = anchorPosition.y - (bodyRect.height / 2);
612 // - check top margin
613 if (bodyRect.y <= topMargin)
615 bodyRect.y = topMargin;
617 // - check bottom margin
618 if ((bodyRect.y + bodyRect.height) >= bottomMargin)
620 bodyRect.y = bottomMargin - bodyRect.height;
623 windowRect.x = bodyRect.x;
624 windowRect.y = bodyRect.y;
625 windowRect.width = bodyRect.width + arrowRect.width;
626 windowRect.height = bodyRect.height;
628 bodyRect.x = arrowMargin;
631 // Set arrow position
632 arrowRect.x = bodyRect.width;
633 arrowRect.y = anchorPosition.y - (arrowRect.height / 2) - windowRect.y;
637 // Check touch position
638 // - Check top margin
639 if (anchorPosition.x <= leftMargin)
641 anchorPosition.x = leftMargin;
643 // - check right margin
644 if (anchorPosition.x >= rightMargin)
646 anchorPosition.x = rightMargin - bodyRect.width;
648 // - Check top margin
649 if (anchorPosition.y <= topMargin + arrowRect.height)
651 anchorPosition.y = topMargin + arrowRect.height;
653 // - check bottom margin
654 if (anchorPosition.y >= (bottomMargin - arrowRect.height))
656 anchorPosition.y = bottomMargin - arrowRect.height;
659 // Set body position x
660 bodyRect.x = anchorPosition.x + arrowRect.width;
661 // - Check left margin
662 if (bodyRect.x <= leftMargin)
664 bodyRect.x = leftMargin;
666 // - check right margin
667 if ((bodyRect.x + bodyRect.width) >= rightMargin)
669 bodyRect.x = rightMargin - bodyRect.width;
671 // Set body position y
672 bodyRect.y = anchorPosition.y - (bodyRect.height / 2);
673 // - check top margin
674 if (bodyRect.y <= topMargin)
676 bodyRect.y = topMargin;
678 // - check bottom margin
679 if ((bodyRect.y + bodyRect.height) >= bottomMargin)
681 bodyRect.y = bottomMargin - bodyRect.height;
684 windowRect.x = bodyRect.x + arrowRect.width;
685 windowRect.y = bodyRect.y;
686 windowRect.width = bodyRect.width + arrowRect.width;
687 windowRect.height = bodyRect.height;
689 bodyRect.x = arrowRect.width - arrowMargin;
692 // Set arrow position
694 arrowRect.y = anchorPosition.y - (arrowRect.height / 2) - windowRect.y;
697 __pContextMenu->SetBodyRect(bodyRect);
698 __pContextMenu->SetArrowRect(arrowRect);
699 __pContextMenu->SetWindowRect(windowRect);
700 __pContextMenu->SetMovable(true); // To do Modify a enabling
701 __pContextMenu->SetResizable(true); // To do Modify a enabling
702 __pContextMenu->SetMinimumSize(Dimension(0, 0)); // To do Modify
703 __pContextMenu->SetMaximumSize(screen); // To do Modify
704 __pContextMenu->SetBounds(windowRect);
705 __pContextMenu->SetMovable(false); // To do Modify a enabling
706 __pContextMenu->SetResizable(false); // To do Modify a enabling
712 _ContextMenuGridPresenter::IsLayoutBitmapOnly(void)
714 int itemCount = __pModel->GetItemCount();
721 _ContextMenuItem* pItem = null;
723 for (int i = 0; i < itemCount; i++)
725 pItem = __pModel->GetItem(i);
731 if (pItem->GetType() == CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT)
741 _ContextMenuGridPresenter::AdjustItemLayout(void)
743 int itemCount = __pModel->GetItemCount();
744 Dimension layoutSize(0, 0);
748 __layoutSize.width = __itemWidth;
749 __layoutSize.height = __itemHeight;
753 bool bitmapOnly = IsLayoutBitmapOnly();
755 if (bitmapOnly == true)
757 layoutSize = AdjustItemLayoutIconStyle();
761 layoutSize = AdjustItemLayoutTabStyle();
764 Dimension screen = _ControlManager::GetInstance()->GetScreenSize();
766 if (__pContextMenu->GetLayout() == _CONTROL_ORIENTATION_LANDSCAPE)
768 screen.SetSize(screen.height, screen.width);
771 int maxHeight = screen.height - __screenTopMargin - __screenBottomMargin - __arrowHeight;
772 if (layoutSize.height > maxHeight)
774 layoutSize.height = maxHeight;
777 __layoutSize = layoutSize;
780 Tizen::Graphics::Dimension
781 _ContextMenuGridPresenter::AdjustItemLayoutIconStyle(void)
783 int countPerLine = 0;
784 int itemCount = __pModel->GetItemCount();
785 int itemWidth = __itemWidth + __itemGap;
786 int itemHeight = __itemHeight + __itemGap;
788 if (itemCount <= MAX_COUNT_PER_LINE)
790 countPerLine = itemCount;
794 countPerLine = GetCountPerLine(itemCount, MIN_COUNT_PER_LINE, MAX_COUNT_PER_LINE);
797 int lineCount = itemCount / countPerLine;
798 if (itemCount % countPerLine != 0)
803 Dimension layoutSize(0, 0);
805 layoutSize.width = countPerLine * __itemWidth + (countPerLine -1) * __itemGap;
806 layoutSize.height = lineCount * __itemHeight + (lineCount - 1) *__itemGap;
808 _ContextMenuItem* pItem = null;
810 for (int line = 0; line < lineCount; line++)
812 for (int i = 0; i < countPerLine; i++)
814 int index = (line * countPerLine) + i;
815 if (index >= itemCount)
820 pItem = __pModel->GetItem(index);
827 Rectangle drawRect(0, 0, __itemWidth, __itemHeight);
829 drawRect.x = (i * itemWidth);
830 drawRect.y = (line * itemHeight);
832 bool drawDivider = true;
833 if (i == countPerLine -1)
838 pItem->SetDrawRect(drawRect);
839 pItem->SetDivider(drawDivider);
847 Tizen::Graphics::Dimension
848 _ContextMenuGridPresenter::AdjustItemLayoutTabStyle(void)
850 int lineWidthList[MAX_LINE_COUNT] = {0, };
851 int lineItemCountList[MAX_LINE_COUNT] = {0, };
853 int itemCount = __pModel->GetItemCount();
854 int maxWidth = __itemWidth;
855 int itemHeight = __itemHeight + __itemGap;
858 int lineItemCount = 0;
862 Dimension screen = _ControlManager::GetInstance()->GetScreenSize();
863 Point anchorPosition = __pContextMenu->GetAnchorPosition();
864 ContextMenuCoreDropPosition dropPosition = __pContextMenu->GetDropPosition();
866 if (__pContextMenu->GetLayout() == _CONTROL_ORIENTATION_LANDSCAPE)
868 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_MAX_WIDTH, _CONTROL_ORIENTATION_LANDSCAPE, __maxWidth);
869 screen.SetSize(screen.height, screen.width);
873 GET_SHAPE_CONFIG(CONTEXTMENU::GRID_MAX_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __maxWidth);
876 if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_LEFT || dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_RIGHT)
878 remainWidth = anchorPosition.x * 2 < screen.width ? screen.width - anchorPosition.x : anchorPosition.x;
879 remainWidth -= __screenLeftMargin + __leftMargin + __rightMargin + __screenRightMargin;
880 remainWidth -= __arrowHeight;
881 __maxWidth = __maxWidth < remainWidth ? __maxWidth : remainWidth;
884 _ContextMenuItem* pItem = null;
885 for (int i = 0; i < itemCount; i++)
887 pItem = __pModel->GetItem(i);
893 Dimension itemSize = pItem->GetSize();
894 if (lineWidth + itemSize.width > __maxWidth)
896 _ContextMenuItem* pPreviousItem = __pModel->GetItem(i - 1);
897 if (pPreviousItem != null)
899 pPreviousItem->SetDivider(false);
910 Rectangle drawRect(x, y, itemSize.width, itemSize.height);
911 pItem->SetDrawRect(drawRect);
912 pItem->SetDivider(true);
914 x = x + itemSize.width + __itemGap;
915 lineWidth = lineWidth + itemSize.width + __itemGap;
917 if (lineWidth > maxWidth)
919 maxWidth = lineWidth;
923 if (lineItemCount > MAX_LINE_COUNT)
925 lineItemCount = MAX_LINE_COUNT;
929 lineWidthList[lineCount - 1] = lineWidth;
930 lineItemCountList[lineCount -1] = lineItemCount;
936 pItem->SetDivider(false);
939 Dimension layoutSize(__itemWidth, __itemHeight);
940 layoutSize.width = maxWidth - __itemGap;
941 layoutSize.height = lineCount * __itemHeight + (lineCount - 1) *__itemGap;
950 for (int line = 0; line < lineCount; line++)
952 int width = lineWidthList[line];
953 int count = lineItemCountList[line];
955 if (maxWidth == width)
961 int margin = (maxWidth - width) / count;
963 for (int i = 0; i < count; i++)
965 int index = itemIndex + i;
966 pItem = __pModel->GetItem(index);
972 Rectangle drawRect = pItem->GetDrawRect();
974 drawRect.width = drawRect.width + margin;
975 drawRect.x = drawRect.x + (i * margin);
977 pItem->SetDrawRect(drawRect);
987 _ContextMenuGridPresenter::AdjustItemPosition(void)
989 int itemCount = __pModel->GetItemCount();
996 int x = __pContextMenu->GetBodyRect().x + __leftMargin;
997 int y = __pContextMenu->GetBodyRect().y + __topMargin;
999 _ContextMenuItem* pItem = null;
1001 for (int i = 0; i < itemCount; i++)
1003 pItem = __pModel->GetItem(i);
1010 Rectangle drawRect = pItem->GetDrawRect();
1015 pItem->SetDrawRect(drawRect);
1020 _ContextMenuGridPresenter::GetCountPerLine(int count, int min, int max) const
1023 int countPerLine = max;
1025 for (int i = max; i >= min; i--)
1027 int remainder = count % i;
1035 int blank = i - remainder;
1037 if (blank < minBlank)
1044 return countPerLine;
1048 _ContextMenuGridPresenter::GetItemIndexFromPosition(const Tizen::Graphics::Point& point) const
1051 int itemCount = __pModel->GetItemCount();
1058 _ContextMenuItem* pItem = null;
1060 for (int i = 0; i < itemCount; i++)
1062 pItem = __pModel->GetItem(i);
1069 Rectangle drawRect = pItem->GetDrawRect();
1071 if (drawRect.Contains(point) == true)
1083 _ContextMenuGridPresenter::Draw(void)
1085 result r = E_SUCCESS;
1087 Canvas* pCanvas = __pContextMenu->GetCanvasN();
1088 SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
1090 // Clear canvas for drawing area of the ContextMenu.
1091 pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1092 Tizen::Graphics::Rectangle bounds(__pContextMenu->GetWindowRect());
1093 pCanvas->Clear(Tizen::Graphics::Rectangle(0, 0, bounds.width, bounds.height));
1095 r = DrawBackground(pCanvas);
1096 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to draw background.");
1098 r = DrawArrow(pCanvas);
1099 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to draw arrow.");
1101 r = DrawItem(pCanvas);
1109 _ContextMenuGridPresenter::DrawBackground(Canvas* pCanvas)
1111 SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1113 result r = E_SUCCESS;
1115 Tizen::Graphics::Rectangle bodyRect = __pContextMenu->GetBodyRect();
1117 const Bitmap* pBackgroundNormalBitmap = __pContextMenu->GetBackgroundNormalBitmap();
1118 const Bitmap* pBackgroundEffectBitmap = __pContextMenu->GetBackgroundEffectBitmap();
1120 if (pBackgroundNormalBitmap == null && pBackgroundEffectBitmap == null)
1122 pCanvas->SetForegroundColor(__pContextMenu->GetColor());
1123 pCanvas->DrawRectangle(bodyRect);
1127 if (pBackgroundNormalBitmap != null)
1129 r = pCanvas->DrawNinePatchedBitmap(bodyRect, *pBackgroundNormalBitmap);
1132 if (pBackgroundEffectBitmap != null)
1134 r = pCanvas->DrawNinePatchedBitmap(bodyRect, *pBackgroundEffectBitmap);
1143 _ContextMenuGridPresenter::DrawArrow(Canvas* pCanvas)
1145 SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1147 result r = E_SUCCESS;
1148 Tizen::Graphics::Rectangle arrowRect = __pContextMenu->GetArrowRect();
1149 ContextMenuCoreDropPosition dropPosition = __pContextMenu->GetDropPosition();
1151 const Bitmap* pArrowNormalBitmap = __pContextMenu->GetArrowNormalBitmap(dropPosition);
1152 const Bitmap* pEffectArrowBitmap = __pContextMenu->GetArrowEffectBitmap(dropPosition);
1154 if (pArrowNormalBitmap != null)
1156 r = DrawBitmap(*pCanvas, arrowRect, *pArrowNormalBitmap);
1159 if (pEffectArrowBitmap != null)
1161 r = DrawBitmap(*pCanvas, arrowRect, *pEffectArrowBitmap);
1168 _ContextMenuGridPresenter::DrawItem(Tizen::Graphics::Canvas* pCanvas)
1170 SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1172 result r = E_SUCCESS;
1173 TextSimple* pSimpleText = null;
1175 int itemCount = __pModel->GetItemCount();
1182 Rectangle bodyRect = __pContextMenu->GetBodyRect();
1183 Color pressedColor = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED);
1185 int bitmapLeftMargin = __itemBitmapWidth >> 1;
1186 int bitmapTopMargin = __itemBitmapHeight >> 1;
1187 int linePositionX = bodyRect.x + __leftMargin + __rightMargin;
1188 int linePositionY = bodyRect.y + __topMargin;
1190 _ContextMenuItem* pItem = null;
1192 for (int i = 0; i < itemCount; i++)
1194 pItem = __pModel->GetItem(i);
1201 Rectangle rect = pItem->GetDrawRect();
1202 ContextMenuItemDrawingType itemType = pItem->GetType();
1204 if (itemType == CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT)
1206 Rectangle drawRect = rect;
1207 drawRect.x = drawRect.x + __itemTextMargin;
1208 drawRect.width = drawRect.width - (__itemTextMargin * 2);
1210 ContextMenuCoreItemStatus itemStatus = CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL;
1212 if (__selectedIndex == i)
1214 itemStatus = CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED;
1216 //pCanvas->FillRectangle(pressedColor, rect);
1217 pCanvas->FillRoundRectangle(pressedColor, rect, Dimension(3, 3));
1220 String text = pItem->GetText();
1222 __pTextObject->RemoveAll();
1223 pSimpleText = new (std::nothrow)TextSimple(const_cast <mchar*>(text.GetPointer()), text.GetLength());
1224 SysTryReturn(NID_UI_CTRL, pSimpleText != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1225 __pTextObject->AppendElement(*pSimpleText);
1227 __pTextObject->SetForegroundColor(__pContextMenu->GetTextColor(itemStatus), 0, __pTextObject->GetTextLength());
1228 __pTextObject->SetBounds(drawRect);
1229 __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
1230 __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
1235 Rectangle drawRect = rect;
1237 drawRect.x = drawRect.x + (drawRect.width >> 1) - bitmapLeftMargin;
1238 drawRect.y = drawRect.y + (drawRect.height >> 1) - bitmapTopMargin;
1239 drawRect.width = __itemBitmapWidth;
1240 drawRect.height = __itemBitmapHeight;
1242 ContextMenuItemDrawingStatus itemStatus = CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL;
1244 if (__selectedIndex == i)
1246 itemStatus = CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED;
1248 //pCanvas->FillRectangle(pressedColor, rect);
1249 pCanvas->FillRoundRectangle(pressedColor, rect, Dimension(3, 3));
1252 const Bitmap* pBitmap = pItem->GetBitmap(itemStatus);
1253 if (pBitmap != null)
1255 DrawBitmap(*pCanvas, drawRect, *pBitmap);
1260 bool drawDivider = pItem->GetDivider();
1261 if (drawDivider == true)
1263 int x = rect.x + rect.width;
1264 int y = rect.y + __topMargin;
1267 Point point2(x, y + __dividerHeight);
1270 DrawLine(pCanvas, point1, point2, true);
1274 if (linePositionY != rect.y)
1276 linePositionY = rect.y;
1278 int x = linePositionX + __layoutSize.width - __leftMargin - __rightMargin;
1279 int y = linePositionY - __itemGap;
1281 Point point1(linePositionX, y);
1284 DrawLine(pCanvas, point1, point2, false);
1292 _ContextMenuGridPresenter::DrawLine(Tizen::Graphics::Canvas* pCanvas, Tizen::Graphics::Point point1, Tizen::Graphics::Point point2, bool drawVLine)
1294 SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1296 result r = E_SUCCESS;
1298 Color colorGridItemDivider01;
1299 Color colorGridItemDivider02;
1301 GET_COLOR_CONFIG(CONTEXTMENU::GRID_ITEM_DIVIDER_01, colorGridItemDivider01);
1302 GET_COLOR_CONFIG(CONTEXTMENU::GRID_ITEM_DIVIDER_02, colorGridItemDivider02);
1304 if (drawVLine == true)
1306 pCanvas->SetForegroundColor(colorGridItemDivider01);
1307 pCanvas->DrawLine(point1, point2);
1312 pCanvas->SetForegroundColor(colorGridItemDivider02);
1313 pCanvas->DrawLine(point1, point2);
1317 pCanvas->SetForegroundColor(colorGridItemDivider01);
1318 pCanvas->DrawLine(point1, point2);
1323 pCanvas->SetForegroundColor(colorGridItemDivider02);
1324 pCanvas->DrawLine(point1, point2);
1330 _UiTouchEventDelivery
1331 _ContextMenuGridPresenter::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
1333 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
1337 _ContextMenuGridPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
1339 Rectangle bodyRect = __pContextMenu->GetBodyRect();
1341 if (bodyRect.Contains(touchinfo.GetCurrentPosition()) == false)
1343 __selectedIndex = -1;
1344 __pressedIndex = -1;
1345 __touchOutRect = true;
1350 __selectedIndex = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1351 __pressedIndex = __selectedIndex;
1352 __touchOutRect = false;
1358 _UiTouchEventDelivery
1359 _ContextMenuGridPresenter::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
1361 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
1365 _ContextMenuGridPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
1367 Rectangle bodyRect = __pContextMenu->GetBodyRect();
1369 if (__touchOutRect == true)
1371 __pContextMenu->SetVisibleState(false);
1374 int currentSelectedIndex = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1375 if (__selectedIndex != -1 && (__selectedIndex == currentSelectedIndex))
1377 _ActionEvent* pActionEvent = __pContextMenu->GetActionEvent();
1379 if (pActionEvent == null)
1384 _ContextMenuItem* pItem = null;
1385 pItem = __pModel->GetItem(__selectedIndex);
1389 __selectedIndex = -1;
1390 __pressedIndex = -1;
1394 __selectedIndex = -1;
1395 __pressedIndex = -1;
1396 __pContextMenu->SetVisibleState(false);
1398 int actionId = pItem->GetActionId();
1400 IEventArg* pEventArg = _ActionEvent::CreateActionEventArgN(actionId);
1402 if (pEventArg == null)
1407 pActionEvent->Fire(*pEventArg);
1414 _ContextMenuGridPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
1419 _UiTouchEventDelivery
1420 _ContextMenuGridPresenter::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1422 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
1426 _ContextMenuGridPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1428 if (__selectedIndex != -1)
1430 _ContextMenuItem* pItem = __pModel->GetItem(__selectedIndex);
1434 __selectedIndex = -1;
1435 __pressedIndex = -1;
1439 Rectangle drawRect = pItem->GetDrawRect();
1440 if (drawRect.Contains(touchinfo.GetCurrentPosition()) == false)
1442 __selectedIndex = -1;
1446 else if (__pressedIndex != -1)
1448 _ContextMenuItem* pItem = __pModel->GetItem(__pressedIndex);
1451 Rectangle drawRect = pItem->GetDrawRect();
1452 if(drawRect.Contains(touchinfo.GetCurrentPosition()))
1454 __selectedIndex = __pressedIndex;
1464 _ContextMenuGridPresenter::OnFontChanged(Font* pFont)
1470 _ContextMenuGridPresenter::OnFontInfoRequested(unsigned long& style, int& size)
1472 style = FONT_STYLE_PLAIN;
1473 size = __itemFontSize;
1477 _ContextMenuGridPresenter::SetAllAccessibilityElement(void)
1479 _AccessibilityContainer* pContainer = __pContextMenu->GetAccessibilityContainer();
1480 if (pContainer != null)
1482 int itemCount = __pModel->GetItemCount();
1483 for (int i = 0; i < itemCount; i++)
1485 _ContextMenuItem* pItem = __pModel->GetItem(i);
1486 _AccessibilityElement* pElement = new (std::nothrow) _AccessibilityElement(true);
1487 if (pItem != null && pElement != null)
1489 pElement->SetLabel(pItem->GetText());
1490 // pElement->SetTrait(ACCESSIBILITY_TRAITS_CONTEXTMENU);
1491 pElement->SetTrait(ACCESSIBILITY_TRAITS_LIST); // not yet
1492 pElement->SetBounds(pItem->GetDrawRect());
1495 pElement->SetHint(L"The first of Contextual popup");
1497 else if(i == itemCount - 1)
1499 pElement->SetHint(L"The last of Contextual popup");
1501 pContainer->AddElement(*pElement);
1502 __pContextMenu->AddAccessibilityElement(*pElement);
1513 _ContextMenuItemInfo
1514 _ContextMenuGridPresenter::GetItemFromPosition(const Point& position)
1516 _ContextMenuItemInfo itemInfo;
1517 int index = GetItemIndexFromPosition(position);
1518 itemInfo.pContextMenuItem = __pModel->GetItem(index);
1520 itemInfo.bListItem = false;
1521 itemInfo.pListItem = null;
1525 _ContextMenuItemInfo
1526 _ContextMenuGridPresenter::FindItem(int index)
1528 _ContextMenuItemInfo itemInfo;
1529 itemInfo.bListItem = false;
1530 itemInfo.pListItem = null;
1531 itemInfo.pContextMenuItem = __pModel->GetItem(index);
1536 _ContextMenuGridPresenter::SetTopDrawnItemIndex(int index)
1542 _ContextMenuGridPresenter::DrawBitmap(Tizen::Graphics::Canvas& canvas, const Tizen::Graphics::Rectangle& bounds, const Tizen::Graphics::Bitmap& bitmap)
1544 result r = E_SUCCESS;
1545 if (bitmap.IsNinePatchedBitmap())
1547 r = canvas.DrawNinePatchedBitmap(bounds, bitmap);
1548 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Fail to draw ninepatched bitmap.")
1552 r = canvas.DrawBitmap(bounds, bitmap);
1553 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Fail to draw bitmap.")
1559 }}} // Tizen::Ui: Control