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.
18 * @file FUiCtrl_ContextMenuItem.cpp
19 * @brief This is the implementation file for the _ContextMenuItem class.
22 #include <FBaseSysLog.h>
23 #include <FGrp_BitmapImpl.h>
24 #include "FUi_AccessibilityContainer.h"
25 #include "FUi_AccessibilityElement.h"
26 #include "FUi_CoordinateSystemUtils.h"
28 #include "FUi_ResourceManager.h"
30 #include "FUiCtrl_ScrollPanel.h"
31 #include "FUiCtrl_ContextMenuItem.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Graphics;
35 using namespace Tizen::Ui;
38 static const float TOUCH_PRESS_THRESHOLD_INSENSITIVE = 0.16f;
41 namespace Tizen { namespace Ui { namespace Controls
44 _ContextMenuItem::_ContextMenuItem(void)
45 : __type(CONTEXT_MENU_ITEM_DRAWING_TYPE_NONE)
47 , __upperDividerLine(false)
48 , __lowerDividerLine(false)
50 , __parentScrollEnable(false)
53 , __dividerHeight(0.0f)
54 , __bgPressedMargin(0.0f)
57 , __pMagentaBgBitmap(null)
58 , __size(FloatDimension(0.0f, 0.0f))
59 , __drawRect(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f))
60 , __pBitmapLabel(null)
62 , __pUpperDividerLineLabel(null)
63 , __pBackgroundLabel(null)
64 , __pLowerDividerLineLabel(null)
66 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL] = null;
67 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED] = null;
68 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED] = null;
70 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __leftMargin);
71 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __rightMargin);
72 GET_SHAPE_CONFIG(CONTEXTMENU::LIST_DIVIDER_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __dividerHeight);
73 GET_SHAPE_CONFIG(CONTEXTMENU::ITEM_BG_PRESSED_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __bgPressedMargin);
75 GET_COLOR_CONFIG(CONTEXTMENU::ITEM_BG_PRESSED, __selectedBgColor);
76 GET_COLOR_CONFIG(CONTEXTMENU::LIST_ITEM_DIVIDER_01_NORMAL, __lowerDividerLineColor); // lower divider of item n and
77 GET_COLOR_CONFIG(CONTEXTMENU::LIST_ITEM_DIVIDER_02_NORMAL, __upperDividerLineColor); // upper divider of item n+1 are bonded together.
78 GET_BITMAP_CONFIG_N(CONTEXTMENU::ITEM_BG_PRESSED, BITMAP_PIXEL_FORMAT_ARGB8888, __pMagentaBgBitmap);
80 InitializeAccessibilityElement();
83 _ContextMenuItem::~_ContextMenuItem(void)
85 if (__pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL] != null)
87 delete __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL];
88 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL] = null;
91 if (__pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED] != null)
93 delete __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED];
94 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED] = null;
97 if (__pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED] != null)
99 delete __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED];
100 __pBitmap[CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED] = null;
103 if (__pUpperDividerLineLabel != null)
105 DetachChild(*__pUpperDividerLineLabel);
106 delete __pUpperDividerLineLabel;
107 __pUpperDividerLineLabel = null;
110 if (__pBackgroundLabel != null)
112 DetachChild(*__pBackgroundLabel);
113 delete __pBackgroundLabel;
114 __pBackgroundLabel = null;
117 delete __pMagentaBgBitmap;
118 __pMagentaBgBitmap = null;
120 if (__pLowerDividerLineLabel != null)
122 DetachChild(*__pLowerDividerLineLabel);
123 delete __pLowerDividerLineLabel;
124 __pLowerDividerLineLabel = null;
127 if (__pTextLabel != null)
129 DetachChild(*__pTextLabel);
134 if (__pBitmapLabel != null)
136 DetachChild(*__pBitmapLabel);
137 delete __pBitmapLabel;
138 __pBitmapLabel = null;
141 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
144 pContainer->RemoveAllElement();
149 _ContextMenuItem::CreateContextMenuItemN(void)
153 _ContextMenuItem* pItem = null;
155 pItem = new (std::nothrow) _ContextMenuItem();
158 SetLastResult(E_OUT_OF_MEMORY);
162 pItem->AcquireHandle();
164 SetLastResult(E_SUCCESS);
169 _ContextMenuItem::InitializeAccessibilityElement(void)
171 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
174 pContainer->Activate(true);
175 _AccessibilityElement* pElement = new (std::nothrow) _AccessibilityElement(true);
176 SysTryReturnVoidResult(NID_UI_CTRL, pElement, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
178 pElement->SetTraitWithStringId("IDS_TPLATFORM_BODY_CONTEXTUAL_POP_UP_T_TTS");
179 pElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
180 pContainer->AddElement(*pElement);
187 _ContextMenuItem::SetBitmapLabel(_Label* pLabel)
189 __pBitmapLabel = pLabel;
193 _ContextMenuItem::SetTextLabel(_Label* pLabel)
195 __pTextLabel = pLabel;
200 _ContextMenuItem::SetType(ContextMenuItemDrawingType type)
205 ContextMenuItemDrawingType
206 _ContextMenuItem::GetType(void) const
212 _ContextMenuItem::SetActionId(int actionId)
214 __actionId = actionId;
218 _ContextMenuItem::GetActionId(void) const
224 _ContextMenuItem::SetUpperDivider(bool drawDivider)
226 __upperDividerLine = drawDivider;
230 _ContextMenuItem::SetLowerDivider(bool drawDivider)
232 __lowerDividerLine = drawDivider;
236 _ContextMenuItem::HasUpperDivider(void) const
238 return __upperDividerLine;
242 _ContextMenuItem::HasLowerDivider(void) const
244 return __lowerDividerLine;
248 _ContextMenuItem::SetTextSize(float size)
254 _ContextMenuItem::SetText(const Tizen::Base::String& text)
256 if (text.GetLength() <= 0)
258 return E_INVALID_ARG;
266 const Tizen::Base::String&
267 _ContextMenuItem::GetText(void) const
273 _ContextMenuItem::SetBitmap(ContextMenuItemDrawingStatus status, const Tizen::Graphics::Bitmap* pBitmap)
275 if (status < CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || status > CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED)
277 return E_INVALID_ARG;
280 SysTryReturn(NID_UI_CTRL, (status != CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || pBitmap != null), E_INVALID_ARG,
282 "[E_INVALID_ARG] The normal bitmap must not be null.");
284 Bitmap* pClonedBitmap = _BitmapImpl::CloneN(*pBitmap);
286 // If bitmap is in _ContextMenuItem, delete old one.
287 if (__pBitmap[status] != null)
289 delete __pBitmap[status];
290 __pBitmap[status] = null;
293 __pBitmap[status] = pClonedBitmap;
298 const Tizen::Graphics::Bitmap*
299 _ContextMenuItem::GetBitmap(ContextMenuItemDrawingStatus status) const
301 if (status < CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || status > CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED)
306 return __pBitmap[status];
310 _ContextMenuItem::SetSize(Tizen::Graphics::FloatDimension size)
315 Tizen::Graphics::FloatDimension
316 _ContextMenuItem::GetSize(void) const
322 _ContextMenuItem::SetDrawRect(Tizen::Graphics::FloatRectangle rect)
328 Tizen::Graphics::FloatRectangle
329 _ContextMenuItem::GetDrawRect(void) const
335 _ContextMenuItem::SetPressedDrawRect(Tizen::Graphics::FloatRectangle rect)
337 __pressedDrawRect = rect;
341 Tizen::Graphics::FloatRectangle
342 _ContextMenuItem::GetPressedDrawRect(void) const
344 return __pressedDrawRect;
348 _ContextMenuItem::Release(void)
356 _ContextMenuItem::OnBoundsChanged(void)
358 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
361 _AccessibilityElement* pElement = pContainer->GetChildElement(0);
364 pElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
370 _ContextMenuItem::OnDraw(void)
376 _ContextMenuItem::DrawItem(void)
378 if (__upperDividerLine)
380 DrawItemUpperDivider();
383 if (__lowerDividerLine)
385 DrawItemLowerDivider();
388 DrawItemBackground();
392 _ContextMenuItem::DrawItemUpperDivider(void)
394 FloatRectangle bounds(0.0f, 0.0f, 0.0f, 0.0f);
395 Color dividerLineColor = Color(0, 0, 0, 0);
397 if (__pUpperDividerLineLabel == null)
399 __pUpperDividerLineLabel = _Label::CreateLabelN();
400 SysTryReturnVoidResult(NID_UI_CTRL, __pUpperDividerLineLabel, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
402 AttachChild(*__pUpperDividerLineLabel);
404 _AccessibilityContainer* pContainer = __pUpperDividerLineLabel->GetAccessibilityContainer();
407 pContainer->Activate(false);
411 bounds = GetBoundsF();
412 if (__selected == false)
414 dividerLineColor = __upperDividerLineColor;
417 __pUpperDividerLineLabel->SetBounds(FloatRectangle(__leftMargin, 0.0f, bounds.width - __leftMargin - __rightMargin, __dividerHeight));
418 __pUpperDividerLineLabel->SetBackgroundColor(dividerLineColor);
419 __pUpperDividerLineLabel->Invalidate();
423 _ContextMenuItem::DrawItemBackground(void)
425 float topMargin = 0.0f, bottomMargin = 0.0f;
427 SetBackgroundColor(Color(0, 0, 0, 0));
429 if (__pBackgroundLabel == null)
431 __pBackgroundLabel = _Label::CreateLabelN();
432 SysTryReturnVoidResult(NID_UI_CTRL, __pBackgroundLabel, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
434 __pBackgroundLabel->SetTouchPressThreshold(TOUCH_PRESS_THRESHOLD_INSENSITIVE);
435 AttachChild(*__pBackgroundLabel);
436 MoveChildToBottom(*__pBackgroundLabel);
438 _AccessibilityContainer* pContainer = __pBackgroundLabel->GetAccessibilityContainer();
441 pContainer->Activate(false);
445 Color color= Color(0, 0, 0, 0);
446 if (__selected == true)
448 color = __selectedBgColor;
451 FloatRectangle bounds = GetBoundsF();
452 _ScrollPanel* pScrollPanel = static_cast<_ScrollPanel*>(GetParent());
453 FloatRectangle boundsScrollPanel = pScrollPanel->GetBoundsF();
455 float topOverlap = pScrollPanel->GetScrollPosition() - bounds.y;
456 if (topOverlap > 0.0f)
458 topMargin = topOverlap + __bgPressedMargin;
460 else if (!__upperDividerLine)
462 topMargin = __bgPressedMargin;
465 float bottomOverlap = bounds.y + bounds.height - (pScrollPanel->GetScrollPosition() + boundsScrollPanel.height);
466 if (bottomOverlap > 0.0f)
468 bottomMargin = bottomOverlap + __bgPressedMargin;
470 else if (!__lowerDividerLine)
472 bottomMargin = __bgPressedMargin;
475 __pBackgroundLabel->SetBounds(FloatRectangle(__bgPressedMargin, topMargin, bounds.width - __bgPressedMargin * 2.0f, bounds.height - topMargin - bottomMargin));
477 Bitmap* BgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pMagentaBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), color);
478 SysTryReturnVoidResult(NID_UI_CTRL, BgBitmap, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
480 __pBackgroundLabel->SetBackgroundBitmap(*BgBitmap);
484 __pBackgroundLabel->Invalidate();
488 _ContextMenuItem::DrawItemLowerDivider(void)
490 FloatRectangle bounds(0.0f, 0.0f, 0.0f, 0.0f);
491 Color dividerLineColor(0, 0, 0, 0);
493 if (__pLowerDividerLineLabel == null)
495 __pLowerDividerLineLabel = _Label::CreateLabelN();
496 SysTryReturnVoidResult(NID_UI_CTRL, __pLowerDividerLineLabel, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
498 AttachChild(*__pLowerDividerLineLabel);
500 _AccessibilityContainer* pContainer = __pLowerDividerLineLabel->GetAccessibilityContainer();
503 pContainer->Activate(false);
507 bounds = GetBoundsF();
508 if (__selected == false)
510 dividerLineColor = __lowerDividerLineColor;
513 __pLowerDividerLineLabel->SetBounds(FloatRectangle(__leftMargin, bounds.height - __dividerHeight, bounds.width - __leftMargin - __rightMargin, __dividerHeight));
514 __pLowerDividerLineLabel->SetBackgroundColor(dividerLineColor);
515 __pLowerDividerLineLabel->Invalidate();
518 _UiTouchEventDelivery
519 _ContextMenuItem::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
521 SetAndInvalidate(true);
523 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
527 _ContextMenuItem::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
529 SetAndInvalidate(true);
534 _UiTouchEventDelivery
535 _ContextMenuItem::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
537 TouchMoved(source, touchinfo);
539 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
543 _ContextMenuItem::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
545 TouchMoved(source, touchinfo);
550 _UiTouchEventDelivery
551 _ContextMenuItem::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
553 SetAndInvalidate(false);
555 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
559 _ContextMenuItem::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
561 SetAndInvalidate(false);
566 _UiTouchEventDelivery
567 _ContextMenuItem::OnPreviewTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
569 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
573 _ContextMenuItem::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
575 SetAndInvalidate(false);
581 _ContextMenuItem::TouchMoved(const _Control& source, const _TouchInfo& touchinfo)
583 FloatPoint touchPosition = touchinfo.GetCurrentPosition();
587 if (__parentScrollEnable)
597 int oldSelected = __selected;
599 FloatRectangle bounds(0.0f, 0.0f, __size.width, __size.height);
600 if (bounds.Contains(touchPosition))
609 if (oldSelected != __selected)
618 _ContextMenuItem::OnFontChanged(Tizen::Graphics::Font* pFont)
620 String fontName = _Control::GetFont();
622 if (__pTextLabel != null)
624 __pTextLabel->SetFont(fontName);
629 _ContextMenuItem::OnFontInfoRequested(unsigned long& style, float& size)
631 style = FONT_STYLE_PLAIN;
636 _ContextMenuItem::SetParentScrollEnable(bool enable)
638 __parentScrollEnable = enable;
642 _ContextMenuItem::GetParentScrollEnable() const
644 return __parentScrollEnable;
648 _ContextMenuItem::IsSelected() const
654 _ContextMenuItem::SetAndInvalidate(bool flag)
661 const Tizen::Graphics::Bitmap*
662 _ContextMenuItem::GetMagentaBgBitmap() const
664 return __pMagentaBgBitmap;
667 }}} // Tizen::Ui::Controls