Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ContextMenuImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
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  * @file                FUiCtrl_ContextMenuImpl.cpp
19  * @brief               This is the implementation file for the _ContextMenuImpl class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include <FApp_AppInfo.h>
24 #include "FUi_ControlImplManager.h"
25 #include "FUiCtrl_ContextMenuImpl.h"
26 #include "FUiCtrl_FrameImpl.h"
27 #include "FUiCtrl_Form.h"
28
29 using namespace Tizen::App;
30 using namespace Tizen::Base;
31 using namespace Tizen::Graphics;
32
33 namespace Tizen { namespace Ui { namespace Controls
34 {
35
36 _ContextMenuImpl*
37 _ContextMenuImpl::GetInstance(ContextMenu& contextMenu)
38 {
39         return static_cast<_ContextMenuImpl*> (contextMenu._pControlImpl);
40 }
41
42 const _ContextMenuImpl*
43 _ContextMenuImpl::GetInstance(const ContextMenu& contextMenu)
44 {
45         return static_cast<const _ContextMenuImpl*> (contextMenu._pControlImpl);
46 }
47
48 _ContextMenuImpl::_ContextMenuImpl(ContextMenu* pPublic, _ContextMenu* pCore, ContextMenuStyle style)
49         : _WindowImpl(pPublic, pCore)
50         , __style(style)
51         , __pContextMenu(pCore)
52         , __pPublicActionEvent(null)
53 {
54 }
55
56 _ContextMenuImpl*
57 _ContextMenuImpl::CreateContextMenuImplN(ContextMenu* pPublic, const Point& point, ContextMenuStyle style,
58                                                                   ContextMenuAnchorDirection direction)
59 {
60
61         ClearLastResult();
62         result r = E_SUCCESS;
63
64         enum ContextMenuCoreStyle coreStyle = CONTEXT_MENU_CORE_STYLE_LIST;
65         switch (style)
66         {
67         case CONTEXT_MENU_STYLE_LIST:
68                 coreStyle = CONTEXT_MENU_CORE_STYLE_LIST;
69                 break;
70
71         case CONTEXT_MENU_STYLE_GRID:
72                 coreStyle = CONTEXT_MENU_CORE_STYLE_GRID;
73                 break;
74
75         case CONTEXT_MENU_STYLE_ICON:
76                 coreStyle = CONTEXT_MENU_CORE_STYLE_GRID;
77                 break;
78
79         default:
80                 SysLogException(NID_UI_CTRL, E_SYSTEM, "Unable to convert ContextMenu style.");
81                 break;
82         }
83
84         enum ContextMenuCoreAlign contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_AUTO;
85         switch (direction)
86         {
87         case CONTEXT_MENU_ANCHOR_DIRECTION_LEFTWARD:
88                 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_LEFT;
89                 break;
90
91         case CONTEXT_MENU_ANCHOR_DIRECTION_RIGHTWARD:
92                 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_RIGHT;
93                 break;
94
95         case CONTEXT_MENU_ANCHOR_DIRECTION_UPWARD:
96                 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_UP;
97                 break;
98
99         case CONTEXT_MENU_ANCHOR_DIRECTION_DOWNWARD:
100                 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_DOWN;
101                 break;
102
103         default:
104                 contextMenuAlign = CONTEXT_MENU_CORE_ALIGN_AUTO;
105                 break;
106         }
107
108         _ContextMenu* pCore = _ContextMenu::CreateContextMenuN(point, coreStyle, contextMenuAlign);
109         SysTryReturn(NID_UI_CTRL, pCore != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
110
111         _ContextMenuImpl* pImpl = new (std::nothrow) _ContextMenuImpl(pPublic, pCore, style);
112         r = CheckConstruction(pCore, pImpl);
113         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, null, r, "[%s] Propagating.", GetErrorMessage(r));
114
115         return pImpl;
116 }
117
118 _ContextMenuImpl::~_ContextMenuImpl(void)
119 {
120         _Dispose();
121 }
122
123 result
124 _ContextMenuImpl::_Dispose(void)
125 {
126         result r = E_SUCCESS;
127
128         if (__pPublicActionEvent)
129         {
130                 delete __pPublicActionEvent;
131                 __pPublicActionEvent = null;
132         }
133         __pContextMenu = null;
134
135         return r;
136 }
137
138 result
139 _ContextMenuImpl::AddItem(const Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap,
140                                                   const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
141 {
142         ClearLastResult();
143
144         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
145
146         return __pContextMenu->AddItem(text, actionId, pNormalBitmap, pPressedBitmap, pHighlightedBitmap);
147 }
148
149 result
150 _ContextMenuImpl::InsertItem(int index, const Tizen::Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap,
151                                                          const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
152 {
153         ClearLastResult();
154
155         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
156
157         return __pContextMenu->InsertItem(index, text, actionId, pNormalBitmap, pPressedBitmap, pHighlightedBitmap);
158 }
159
160 result
161 _ContextMenuImpl::SetItem(int index, const Tizen::Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap,
162                                                   const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
163 {
164         ClearLastResult();
165
166         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
167
168         return __pContextMenu->SetItem(index, text, actionId, pNormalBitmap, pPressedBitmap, pHighlightedBitmap);
169 }
170
171 result
172 _ContextMenuImpl::RemoveItemAt(int index)
173 {
174         ClearLastResult();
175
176         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
177
178         if (index >= GetItemCount() || index < 0)
179         {
180                 SysLogException(NID_UI_CTRL, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] The specified index is not valid(index = %d).", index);
181
182                 return (_AppInfo::GetApiVersion() <= _API_VERSION_2_0 && _AppInfo::IsOspCompat())?E_SYSTEM:E_OUT_OF_RANGE;
183         }
184
185         return __pContextMenu->RemoveItemAt(index);
186 }
187
188 result
189 _ContextMenuImpl::RemoveAllItems(void)
190 {
191         ClearLastResult();
192
193         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
194
195         return __pContextMenu->RemoveAllItems();
196 }
197
198 result
199 _ContextMenuImpl::OnAttachedToMainTree(void)
200 {
201         result r = E_SUCCESS;
202         ClearLastResult();
203
204         if (GetOwner() == null)
205         {
206                 _FrameImpl* pFrameImpl = dynamic_cast <_FrameImpl*>(_ControlImplManager::GetInstance()->GetCurrentFrame());
207                 SysTryReturn(NID_UI_CTRL, pFrameImpl != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] This instance is not constructed.");
208                 _Form* pForm = pFrameImpl->GetCore().GetCurrentForm();
209                 _ContextMenu& control = GetCore();
210
211                 if (pForm != null)
212                 {
213                         control.SetOwner(pForm);
214                 }
215                 else
216                 {
217                         control.SetOwner(&pFrameImpl->GetCore());
218                 }
219         }
220
221         r = _WindowImpl::OnAttachedToMainTree();
222         return r;
223 }
224
225 result
226 _ContextMenuImpl::SetAnchorPosition(int x, int y)
227 {
228         ClearLastResult();
229
230         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
231
232         return __pContextMenu->SetAnchorPosition(x, y);
233 }
234
235 Tizen::Graphics::Point
236 _ContextMenuImpl::GetAnchorPosition(void) const
237 {
238         ClearLastResult();
239
240         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
241
242         Tizen::Graphics::Point point;
243
244         point = __pContextMenu->GetAnchorPosition();
245
246         return point;
247 }
248
249 int
250 _ContextMenuImpl::GetItemCount(void) const
251 {
252         ClearLastResult();
253
254         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
255
256         return __pContextMenu->GetItemCount();
257 }
258
259 result
260 _ContextMenuImpl::SetMaxVisibleItemsCount(int maxItemsCount)
261 {
262         ClearLastResult();
263
264         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
265
266         return __pContextMenu->SetShowItemCount(maxItemsCount);
267 }
268
269 int
270 _ContextMenuImpl::GetMaxVisibleItemsCount(void) const
271 {
272         ClearLastResult();
273
274         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
275
276         return __pContextMenu->GetShowItemCount();
277 }
278
279 int
280 _ContextMenuImpl::GetItemIndexFromActionId(int actionId) const
281 {
282         ClearLastResult();
283
284         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
285
286         return __pContextMenu->GetItemIndexAt(actionId);
287 }
288
289 int
290 _ContextMenuImpl::GetItemActionIdAt(int index) const
291 {
292         ClearLastResult();
293
294         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
295
296         return __pContextMenu->GetItemActionIdAt(index);
297 }
298
299 result
300 _ContextMenuImpl::SetColor(const Graphics::Color& color)
301 {
302         ClearLastResult();
303
304         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
305
306         result r = E_SUCCESS;
307
308         r = __pContextMenu->SetColor(color);
309         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to set color.");
310
311         return r;
312 }
313
314 Tizen::Graphics::Color
315 _ContextMenuImpl::GetColor(void) const
316 {
317         ClearLastResult();
318
319         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
320
321         return __pContextMenu->GetColor();
322
323 }
324
325 result
326 _ContextMenuImpl::SetItemTextColor(ContextMenuItemStatus status, const Tizen::Graphics::Color& color)
327 {
328         ClearLastResult();
329
330         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
331
332         result r = E_SUCCESS;
333
334         switch (status)
335         {
336         case CONTEXT_MENU_ITEM_STATUS_NORMAL:
337                 r = __pContextMenu->SetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL, color);
338                 break;
339
340         case CONTEXT_MENU_ITEM_STATUS_PRESSED:
341                 r = __pContextMenu->SetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED, color);
342                 break;
343
344         case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
345                 r = __pContextMenu->SetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED, color);
346                 break;
347
348         default:
349                 SysLogException(NID_UI_CTRL, E_INVALID_ARG, "Invalid argument at ContextMenuItemStatus.");
350                 r = E_INVALID_ARG;
351                 break;
352         }
353
354         return r;
355
356 }
357
358 Tizen::Graphics::Color
359 _ContextMenuImpl::GetItemTextColor(ContextMenuItemStatus status) const
360 {
361         ClearLastResult();
362
363         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
364
365         Tizen::Graphics::Color color(0, 0, 0, 0);
366
367         switch (status)
368         {
369         case CONTEXT_MENU_ITEM_STATUS_NORMAL:
370                 color = __pContextMenu->GetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL);
371                 break;
372
373         case CONTEXT_MENU_ITEM_STATUS_PRESSED:
374                 color = __pContextMenu->GetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED);
375                 break;
376
377         case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
378                 color = __pContextMenu->GetTextColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED);
379                 break;
380
381         default:
382                 SysLogException(NID_UI_CTRL, E_SYSTEM, "Invalid argument at ContextMenuItemStatus.");
383                 break;
384         }
385         return color;
386
387 }
388
389 result
390 _ContextMenuImpl::SetItemColor(ContextMenuItemStatus status, const Tizen::Graphics::Color& color)
391 {
392         ClearLastResult();
393
394         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
395
396         result r = E_SUCCESS;
397
398         switch (status)
399         {
400         case CONTEXT_MENU_ITEM_STATUS_NORMAL:
401                 r = __pContextMenu->SetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL, color);
402                 break;
403
404         case CONTEXT_MENU_ITEM_STATUS_PRESSED:
405                 r = __pContextMenu->SetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED, color);
406                 break;
407
408         case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
409                 r = __pContextMenu->SetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED, color);
410                 break;
411
412         default:
413                 SysLogException(NID_UI_CTRL, E_INVALID_ARG, "Invalid argument at ContextMenuItemStatus.");
414                 r = E_INVALID_ARG;
415                 break;
416         }
417
418         return r;
419
420 }
421
422 Tizen::Graphics::Color
423 _ContextMenuImpl::GetItemColor(ContextMenuItemStatus status) const
424 {
425         ClearLastResult();
426
427         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
428
429         Tizen::Graphics::Color color(0, 0, 0, 0);
430
431         switch (status)
432         {
433         case CONTEXT_MENU_ITEM_STATUS_NORMAL:
434                 color = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL);
435                 break;
436
437         case CONTEXT_MENU_ITEM_STATUS_PRESSED:
438                 color = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED);
439                 break;
440
441         case CONTEXT_MENU_ITEM_STATUS_HIGHLIGHTED:
442                 color = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_HIGHLIGHTED);
443                 break;
444
445         default:
446                 SysLogException(NID_UI_CTRL, E_SYSTEM, "Invalid argument at ContextMenuItemStatus.");
447                 break;
448         }
449
450         SetLastResult(E_SUCCESS);
451         return color;
452
453 }
454
455 ContextMenuStyle
456 _ContextMenuImpl::GetPublicStyle(void) const
457 {
458         return __style;
459 }
460
461 result
462 _ContextMenuImpl::AddActionEventListener(Tizen::Ui::IActionEventListener& listener)
463 {
464         ClearLastResult();
465
466         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
467
468         result r = E_SUCCESS;
469
470         if (__pPublicActionEvent == null)
471         {
472                 __pPublicActionEvent = _PublicActionEvent::CreateInstanceN(GetPublic());
473                 SysTryReturn(NID_UI_CTRL, __pPublicActionEvent != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY,
474                                         "[E_OUT_OF_MEMORY] The memory is insufficient.");
475         }
476
477         r = __pPublicActionEvent->AddListener(listener);
478         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to add an action event listener.");
479
480         r = __pContextMenu->AddActionEventListener(*this);
481         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to add an action event listener.");
482
483         return r;
484 }
485
486 result
487 _ContextMenuImpl::RemoveActionEventListener(Tizen::Ui::IActionEventListener& listener)
488 {
489         ClearLastResult();
490
491         SysTryReturn(NID_UI_CTRL, __pPublicActionEvent != null, E_INVALID_STATE, E_INVALID_STATE,
492                         "[E_INVALID_STATE] This instance isn't constructed.")
493         SysAssertf(__pContextMenu != null, "Not yet constructed. Construct() should be called before use.");
494
495         result r = E_SUCCESS;
496
497         r = __pPublicActionEvent->RemoveListener(listener);
498         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to remove an action event listener.");
499
500         r = __pContextMenu->RemoveActionEventListener(*this);
501         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to remove an action event listener.");
502
503         return r;
504 }
505
506 void
507 _ContextMenuImpl::OnActionPerformed(const Tizen::Ui::_Control& source, int actionId)
508 {
509         ClearLastResult();
510
511         if (__pPublicActionEvent != null)
512         {
513                 __pPublicActionEvent->Fire(*_PublicActionEvent::CreateActionEventArgN(actionId));
514         }
515 }
516
517 const char*
518 _ContextMenuImpl::GetPublicClassName(void) const
519 {
520         return "Tizen::Ui::Controls::ContextMenu";
521 }
522
523 const ContextMenu&
524 _ContextMenuImpl::GetPublic(void) const
525 {
526         return static_cast <const ContextMenu&>(_ControlImpl::GetPublic());
527 }
528
529 ContextMenu&
530 _ContextMenuImpl::GetPublic(void)
531 {
532         return static_cast <ContextMenu&>(_ControlImpl::GetPublic());
533 }
534
535 const _ContextMenu&
536 _ContextMenuImpl::GetCore(void) const
537 {
538         return static_cast <const _ContextMenu&>(_ControlImpl::GetCore());
539 }
540
541 _ContextMenu&
542 _ContextMenuImpl::GetCore(void)
543 {
544         return static_cast <_ContextMenu&>(_ControlImpl::GetCore());
545 }
546
547 }}} // Tizen::Ui: Control