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 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
130 pContainer->RemoveAllElement();
135 _ContextMenuItem::CreateContextMenuItemN(void)
139 _ContextMenuItem* pItem = null;
141 pItem = new (std::nothrow) _ContextMenuItem();
144 SetLastResult(E_OUT_OF_MEMORY);
148 pItem->AcquireHandle();
150 SetLastResult(E_SUCCESS);
155 _ContextMenuItem::InitializeAccessibilityElement(void)
157 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
160 pContainer->Activate(true);
161 _AccessibilityElement* pElement = new (std::nothrow) _AccessibilityElement(true);
162 SysTryReturnVoidResult(NID_UI_CTRL, pElement, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
164 pElement->SetTraitWithStringId("IDS_TPLATFORM_BODY_CONTEXTUAL_POP_UP_T_TTS");
165 pElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
166 pContainer->AddElement(*pElement);
173 _ContextMenuItem::SetBitmapLabel(_Label* pLabel)
175 __pBitmapLabel = pLabel;
179 _ContextMenuItem::SetTextLabel(_Label* pLabel)
181 __pTextLabel = pLabel;
186 _ContextMenuItem::SetType(ContextMenuItemDrawingType type)
191 ContextMenuItemDrawingType
192 _ContextMenuItem::GetType(void) const
198 _ContextMenuItem::SetActionId(int actionId)
200 __actionId = actionId;
204 _ContextMenuItem::GetActionId(void) const
210 _ContextMenuItem::SetUpperDivider(bool drawDivider)
212 __upperDividerLine = drawDivider;
216 _ContextMenuItem::SetLowerDivider(bool drawDivider)
218 __lowerDividerLine = drawDivider;
222 _ContextMenuItem::HasUpperDivider(void) const
224 return __upperDividerLine;
228 _ContextMenuItem::HasLowerDivider(void) const
230 return __lowerDividerLine;
234 _ContextMenuItem::SetTextSize(float size)
240 _ContextMenuItem::SetText(const Tizen::Base::String& text)
242 if (text.GetLength() <= 0)
244 return E_INVALID_ARG;
252 const Tizen::Base::String&
253 _ContextMenuItem::GetText(void) const
259 _ContextMenuItem::SetBitmap(ContextMenuItemDrawingStatus status, const Tizen::Graphics::Bitmap* pBitmap)
261 if (status < CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || status > CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED)
263 return E_INVALID_ARG;
266 SysTryReturn(NID_UI_CTRL, (status != CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || pBitmap != null), E_INVALID_ARG,
268 "[E_INVALID_ARG] The normal bitmap must not be null.");
270 Bitmap* pClonedBitmap = _BitmapImpl::CloneN(*pBitmap);
272 // If bitmap is in _ContextMenuItem, delete old one.
273 if (__pBitmap[status] != null)
275 delete __pBitmap[status];
276 __pBitmap[status] = null;
279 __pBitmap[status] = pClonedBitmap;
284 const Tizen::Graphics::Bitmap*
285 _ContextMenuItem::GetBitmap(ContextMenuItemDrawingStatus status) const
287 if (status < CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL || status > CONTEXT_MENU_ITEM_DRAWING_STATUS_HIGHLIGHTED)
292 return __pBitmap[status];
296 _ContextMenuItem::SetSize(Tizen::Graphics::FloatDimension size)
301 Tizen::Graphics::FloatDimension
302 _ContextMenuItem::GetSize(void) const
308 _ContextMenuItem::SetDrawRect(Tizen::Graphics::FloatRectangle rect)
314 Tizen::Graphics::FloatRectangle
315 _ContextMenuItem::GetDrawRect(void) const
321 _ContextMenuItem::SetPressedDrawRect(Tizen::Graphics::FloatRectangle rect)
323 __pressedDrawRect = rect;
327 Tizen::Graphics::FloatRectangle
328 _ContextMenuItem::GetPressedDrawRect(void) const
330 return __pressedDrawRect;
334 _ContextMenuItem::Release(void)
342 _ContextMenuItem::OnBoundsChanged(void)
344 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
347 _AccessibilityElement* pElement = pContainer->GetChildElement(0);
350 pElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
356 _ContextMenuItem::OnDraw(void)
362 _ContextMenuItem::DrawItem(void)
364 if (__upperDividerLine)
366 DrawItemUpperDivider();
369 if (__lowerDividerLine)
371 DrawItemLowerDivider();
374 DrawItemBackground();
378 _ContextMenuItem::DrawItemUpperDivider(void)
380 if (__pUpperDividerLineLabel == null)
382 __pUpperDividerLineLabel = _Label::CreateLabelN();
383 SysTryReturnVoidResult(NID_UI_CTRL, __pUpperDividerLineLabel, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
385 FloatRectangle bounds = GetBoundsF();
386 __pUpperDividerLineLabel->SetBounds(FloatRectangle(__leftMargin, 0.0f, bounds.width - __leftMargin - __rightMargin, __dividerHeight));
388 AttachChild(*__pUpperDividerLineLabel);
390 _AccessibilityContainer* pContainer = __pUpperDividerLineLabel->GetAccessibilityContainer();
393 pContainer->Activate(false);
397 Color dividerLineColor = Color(0, 0, 0, 0);
398 if (__selected == false)
400 dividerLineColor = __upperDividerLineColor;
403 __pUpperDividerLineLabel->SetBackgroundColor(dividerLineColor);
404 __pUpperDividerLineLabel->Invalidate();
408 _ContextMenuItem::DrawItemBackground(void)
410 float topMargin = 0.0f, bottomMargin = 0.0f;
412 SetBackgroundColor(Color(0, 0, 0, 0));
414 if (__pBackgroundLabel == null)
416 __pBackgroundLabel = _Label::CreateLabelN();
417 SysTryReturnVoidResult(NID_UI_CTRL, __pBackgroundLabel, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
419 __pBackgroundLabel->SetTouchPressThreshold(TOUCH_PRESS_THRESHOLD_INSENSITIVE);
420 AttachChild(*__pBackgroundLabel);
421 MoveChildToBottom(*__pBackgroundLabel);
423 _AccessibilityContainer* pContainer = __pBackgroundLabel->GetAccessibilityContainer();
426 pContainer->Activate(false);
430 Color color= Color(0, 0, 0, 0);
431 if (__selected == true)
433 color = __selectedBgColor;
436 FloatRectangle bounds = GetBoundsF();
437 _ScrollPanel* pScrollPanel = static_cast<_ScrollPanel*>(GetParent());
438 FloatRectangle boundsScrollPanel = pScrollPanel->GetBoundsF();
440 float topOverlap = pScrollPanel->GetScrollPosition() - bounds.y;
441 if (topOverlap > 0.0f)
443 topMargin = topOverlap + __bgPressedMargin;
445 else if (!__upperDividerLine)
447 topMargin = __bgPressedMargin;
450 float bottomOverlap = bounds.y + bounds.height - (pScrollPanel->GetScrollPosition() + boundsScrollPanel.height);
451 if (bottomOverlap > 0.0f)
453 bottomMargin = bottomOverlap + __bgPressedMargin;
455 else if (!__lowerDividerLine)
457 bottomMargin = __bgPressedMargin;
460 __pBackgroundLabel->SetBounds(FloatRectangle(__bgPressedMargin, topMargin, bounds.width - __bgPressedMargin * 2.0f, bounds.height - topMargin - bottomMargin));
462 Bitmap* BgBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pMagentaBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), color);
463 SysTryReturnVoidResult(NID_UI_CTRL, BgBitmap, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
465 __pBackgroundLabel->SetBackgroundBitmap(*BgBitmap);
469 __pBackgroundLabel->Invalidate();
473 _ContextMenuItem::DrawItemLowerDivider(void)
475 if (__pLowerDividerLineLabel == null)
477 __pLowerDividerLineLabel = _Label::CreateLabelN();
478 SysTryReturnVoidResult(NID_UI_CTRL, __pLowerDividerLineLabel, E_OUT_OF_MEMORY, "[%s] Propagating.", GetErrorMessage(GetLastResult()));
480 FloatRectangle bounds = GetBoundsF();
481 __pLowerDividerLineLabel->SetBounds(FloatRectangle(__leftMargin, bounds.height - __dividerHeight, bounds.width - __leftMargin - __rightMargin, __dividerHeight));
483 AttachChild(*__pLowerDividerLineLabel);
485 _AccessibilityContainer* pContainer = __pLowerDividerLineLabel->GetAccessibilityContainer();
488 pContainer->Activate(false);
492 Color dividerLineColor = Color(0, 0, 0, 0);
493 if (__selected == false)
495 dividerLineColor = __lowerDividerLineColor;
498 __pLowerDividerLineLabel->SetBackgroundColor(dividerLineColor);
499 __pLowerDividerLineLabel->Invalidate();
502 _UiTouchEventDelivery
503 _ContextMenuItem::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
505 SetAndInvalidate(true);
507 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
511 _ContextMenuItem::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
513 SetAndInvalidate(true);
518 _UiTouchEventDelivery
519 _ContextMenuItem::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
521 TouchMoved(source, touchinfo);
523 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
527 _ContextMenuItem::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
529 TouchMoved(source, touchinfo);
534 _UiTouchEventDelivery
535 _ContextMenuItem::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
537 SetAndInvalidate(false);
539 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
543 _ContextMenuItem::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
545 SetAndInvalidate(false);
550 _UiTouchEventDelivery
551 _ContextMenuItem::OnPreviewTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
553 return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
557 _ContextMenuItem::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
559 SetAndInvalidate(false);
565 _ContextMenuItem::TouchMoved(const _Control& source, const _TouchInfo& touchinfo)
567 FloatPoint touchPosition = touchinfo.GetCurrentPosition();
571 if (__parentScrollEnable)
581 int oldSelected = __selected;
583 FloatRectangle bounds(0.0f, 0.0f, __size.width, __size.height);
584 if (bounds.Contains(touchPosition))
593 if (oldSelected != __selected)
602 _ContextMenuItem::OnFontChanged(Tizen::Graphics::Font* pFont)
604 String fontName = _Control::GetFont();
606 if (__pTextLabel != null)
608 __pTextLabel->SetFont(fontName);
613 _ContextMenuItem::OnFontInfoRequested(unsigned long& style, float& size)
615 style = FONT_STYLE_PLAIN;
620 _ContextMenuItem::SetParentScrollEnable(bool enable)
622 __parentScrollEnable = enable;
626 _ContextMenuItem::GetParentScrollEnable() const
628 return __parentScrollEnable;
632 _ContextMenuItem::IsSelected() const
638 _ContextMenuItem::SetAndInvalidate(bool flag)
645 const Tizen::Graphics::Bitmap*
646 _ContextMenuItem::GetMagentaBgBitmap() const
648 return __pMagentaBgBitmap;
651 }}} // Tizen::Ui::Controls