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_ContextMenuImpl.cpp
19 * @brief This is the implementation file for the _ContextMenuImpl class.
22 #include <FBaseSysLog.h>
23 #include <FApp_AppInfo.h>
24 #include "FUi_ControlImplManager.h"
25 #include "FUi_CoordinateSystemUtils.h"
26 #include "FUiCtrl_ContextMenuImpl.h"
27 #include "FUiCtrl_FrameImpl.h"
28 #include "FUiCtrl_Form.h"
30 using namespace Tizen::App;
31 using namespace Tizen::Base;
32 using namespace Tizen::Graphics;
34 namespace Tizen { namespace Ui { namespace Controls
38 _ContextMenuImpl::GetInstance(ContextMenu& contextMenu)
40 return static_cast<_ContextMenuImpl*> (contextMenu._pControlImpl);
43 const _ContextMenuImpl*
44 _ContextMenuImpl::GetInstance(const ContextMenu& contextMenu)
46 return static_cast<const _ContextMenuImpl*> (contextMenu._pControlImpl);
49 _ContextMenuImpl::_ContextMenuImpl(ContextMenu* pPublic, _ContextMenu* pCore, ContextMenuStyle style)
50 : _WindowImpl(pPublic, pCore)
52 , __pContextMenu(pCore)
53 , __pPublicActionEvent(null)
58 _ContextMenuImpl::CreateContextMenuImplN(ContextMenu* pPublic, const FloatPoint& point, ContextMenuStyle style,
59 ContextMenuAnchorDirection direction)
65 enum ContextMenuCoreStyle coreStyle = CONTEXT_MENU_CORE_STYLE_LIST;
68 case CONTEXT_MENU_STYLE_LIST:
69 coreStyle = CONTEXT_MENU_CORE_STYLE_LIST;
72 case CONTEXT_MENU_STYLE_GRID:
73 coreStyle = CONTEXT_MENU_CORE_STYLE_GRID;
76 case CONTEXT_MENU_STYLE_ICON:
77 coreStyle = CONTEXT_MENU_CORE_STYLE_GRID;
81 SysLogException(NID_UI_CTRL, E_SYSTEM, "Unable to convert ContextMenu style.");
85 enum ContextMenuCoreAlign contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_AUTO;
88 case CONTEXT_MENU_ANCHOR_DIRECTION_LEFTWARD:
89 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_LEFT;
92 case CONTEXT_MENU_ANCHOR_DIRECTION_RIGHTWARD:
93 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_RIGHT;
96 case CONTEXT_MENU_ANCHOR_DIRECTION_UPWARD:
97 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_UP;
100 case CONTEXT_MENU_ANCHOR_DIRECTION_DOWNWARD:
101 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_DOWN;
105 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_AUTO;
109 _ContextMenu* pCore = _ContextMenu::CreateContextMenuN(point, coreStyle, contextMenuAlign);
110 SysTryReturn(NID_UI_CTRL, pCore != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
112 _ContextMenuImpl* pImpl = new (std::nothrow) _ContextMenuImpl(pPublic, pCore, style);
113 r = CheckConstruction(pCore, pImpl);
114 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
119 _ContextMenuImpl::~_ContextMenuImpl(void)
125 _ContextMenuImpl::_Dispose(void)
127 result r = E_SUCCESS;
129 if (__pPublicActionEvent)
131 delete __pPublicActionEvent;
132 __pPublicActionEvent = null;
134 __pContextMenu = null;
140 _ContextMenuImpl::AddItem(const Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap,
141 const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
145 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
147 return __pContextMenu->AddItem(text, actionId, pNormalBitmap, pPressedBitmap, pHighlightedBitmap);
151 _ContextMenuImpl::InsertItem(int index, const Tizen::Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap,
152 const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
156 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
158 return __pContextMenu->InsertItem(index, text, actionId, pNormalBitmap, pPressedBitmap, pHighlightedBitmap);
162 _ContextMenuImpl::SetItem(int index, const Tizen::Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap,
163 const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
167 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
169 return __pContextMenu->SetItem(index, text, actionId, pNormalBitmap, pPressedBitmap, pHighlightedBitmap);
173 _ContextMenuImpl::RemoveItemAt(int index)
177 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
179 if (index >= GetItemCount() || index < 0)
181 SysLogException(NID_UI_CTRL, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is not valid(index = %d).", index);
183 // E_OUT_OF_RANGE is for API version 2.0 over
184 return (_AppInfo::GetApiVersion() <= _API_VERSION_2_0 && _AppInfo::IsOspCompat())?E_SYSTEM:E_OUT_OF_RANGE;
187 return __pContextMenu->RemoveItemAt(index);
191 _ContextMenuImpl::RemoveAllItems(void)
195 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
197 return __pContextMenu->RemoveAllItems();
201 _ContextMenuImpl::SetAnchorPosition(float x, float y)
205 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
207 return __pContextMenu->SetAnchorPosition(x, y);
210 Tizen::Graphics::FloatPoint
211 _ContextMenuImpl::GetAnchorPosition(void) const
215 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
217 Tizen::Graphics::FloatPoint point;
218 point = __pContextMenu->GetAnchorPosition();
224 _ContextMenuImpl::GetItemCount(void) const
228 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
230 return __pContextMenu->GetItemCount();
234 _ContextMenuImpl::SetMaxVisibleItemsCount(int maxItemsCount)
238 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
240 return __pContextMenu->SetShowItemCount(maxItemsCount);
244 _ContextMenuImpl::GetMaxVisibleItemsCount(void) const
248 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
250 return __pContextMenu->GetShowItemCount();
254 _ContextMenuImpl::GetItemIndexFromActionId(int actionId) const
258 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
260 return __pContextMenu->GetItemIndexAt(actionId);
264 _ContextMenuImpl::GetItemActionIdAt(int index) const
268 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
270 return __pContextMenu->GetItemActionIdAt(index);
274 _ContextMenuImpl::SetColor(const Graphics::Color& color)
278 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
280 result r = E_SUCCESS;
282 r = __pContextMenu->SetColor(color);
283 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to set color.");
288 Tizen::Graphics::Color
289 _ContextMenuImpl::GetColor(void) const
293 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
295 return __pContextMenu->GetColor();
300 _ContextMenuImpl::SetItemTextColor(ContextMenuItemStatus status, const Tizen::Graphics::Color& color)
304 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
306 result r = E_SUCCESS;
310 case CONTEXT_MENU_ITEM_STATUS_NORMAL:
311 r = __pContextMenu->SetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL, color);
314 case CONTEXT_MENU_ITEM_STATUS_PRESSED:
315 r = __pContextMenu->SetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED, color);
318 case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
319 r = __pContextMenu->SetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED, color);
323 SysLogException(NID_UI_CTRL, E_INVALID_ARG, "Invalid argument at ContextMenuItemStatus.");
332 Tizen::Graphics::Color
333 _ContextMenuImpl::GetItemTextColor(ContextMenuItemStatus status) const
337 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
339 Tizen::Graphics::Color color(0, 0, 0, 0);
343 case CONTEXT_MENU_ITEM_STATUS_NORMAL:
344 color = __pContextMenu->GetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL);
347 case CONTEXT_MENU_ITEM_STATUS_PRESSED:
348 color = __pContextMenu->GetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED);
351 case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
352 color = __pContextMenu->GetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED);
356 SysLogException(NID_UI_CTRL, E_SYSTEM, "Invalid argument at ContextMenuItemStatus.");
364 _ContextMenuImpl::SetItemColor(ContextMenuItemStatus status, const Tizen::Graphics::Color& color)
368 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
370 result r = E_SUCCESS;
374 case CONTEXT_MENU_ITEM_STATUS_NORMAL:
375 r = __pContextMenu->SetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL, color);
378 case CONTEXT_MENU_ITEM_STATUS_PRESSED:
379 r = __pContextMenu->SetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED, color);
382 case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
383 r = __pContextMenu->SetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED, color);
387 SysLogException(NID_UI_CTRL, E_INVALID_ARG, "Invalid argument at ContextMenuItemStatus.");
396 Tizen::Graphics::Color
397 _ContextMenuImpl::GetItemColor(ContextMenuItemStatus status) const
401 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
403 Tizen::Graphics::Color color(0, 0, 0, 0);
407 case CONTEXT_MENU_ITEM_STATUS_NORMAL:
408 color = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL);
411 case CONTEXT_MENU_ITEM_STATUS_PRESSED:
412 color = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED);
415 case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
416 color = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED);
420 SysLogException(NID_UI_CTRL, E_SYSTEM, "Invalid argument at ContextMenuItemStatus.");
424 SetLastResult(E_SUCCESS);
430 _ContextMenuImpl::GetPublicStyle(void) const
436 _ContextMenuImpl::AddActionEventListener(Tizen::Ui::IActionEventListener& listener)
440 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
442 result r = E_SUCCESS;
444 if (__pPublicActionEvent == null)
446 __pPublicActionEvent = _PublicActionEvent::CreateInstanceN(GetPublic());
447 SysTryReturn(NID_UI_CTRL, __pPublicActionEvent != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
448 "[E_OUT_OF_MEMORY] The memory is insufficient.");
451 r = __pPublicActionEvent->AddListener(listener);
452 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to add an action event listener.");
454 r = __pContextMenu->AddActionEventListener(*this);
455 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to add an action event listener.");
461 _ContextMenuImpl::RemoveActionEventListener(Tizen::Ui::IActionEventListener& listener)
465 SysTryReturn(NID_UI_CTRL, __pPublicActionEvent != null, E_INVALID_STATE, E_INVALID_STATE,
466 "[E_INVALID_STATE] This instance isn't constructed.")
467 SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
469 result r = E_SUCCESS;
471 r = __pPublicActionEvent->RemoveListener(listener);
472 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to remove an action event listener.");
474 r = __pContextMenu->RemoveActionEventListener(*this);
475 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to remove an action event listener.");
481 _ContextMenuImpl::OnActionPerformed(const Tizen::Ui::_Control& source, int actionId)
485 if (__pPublicActionEvent != null)
487 __pPublicActionEvent->Fire(*_PublicActionEvent::CreateActionEventArgN(actionId));
492 _ContextMenuImpl::GetPublicClassName(void) const
494 return "Tizen::Ui::Controls::ContextMenu";
498 _ContextMenuImpl::GetPublic(void) const
500 return static_cast <const ContextMenu&>(_ControlImpl::GetPublic());
504 _ContextMenuImpl::GetPublic(void)
506 return static_cast <ContextMenu&>(_ControlImpl::GetPublic());
510 _ContextMenuImpl::GetCore(void) const
512 return static_cast <const _ContextMenu&>(_ControlImpl::GetCore());
516 _ContextMenuImpl::GetCore(void)
518 return static_cast <_ContextMenu&>(_ControlImpl::GetCore());
521 }}} // Tizen::Ui: Control