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