Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_ContextMenuGridPresenter.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_ContextMenuGridPresenter.cpp
19  * @brief               This is the implementation file for the _ContextMenuGridPresenter class.
20  */
21
22 #include <FBaseErrorDefine.h>
23 #include <FBaseSysLog.h>
24 #include <FGrp_CanvasImpl.h>
25 #include <FGrp_TextTextSimple.h>
26 #include "FUiCtrl_ContextMenuGridPresenter.h"
27 #include "FUiCtrl_IActionEventListener.h"
28 #include "FUiCtrl_ActionEvent.h"
29 #include "FUi_ResourceManager.h"
30 #include "FUi_AccessibilityContainer.h"
31 #include "FUi_AccessibilityElement.h"
32
33 using namespace Tizen::Graphics;
34 using namespace Tizen::Ui;
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Graphics::_Text;
38
39 static const int MAX_COUNT_PER_LINE = 5;
40 static const int MIN_COUNT_PER_LINE = 3;
41 static const int MAX_LINE_COUNT = 32;
42
43 namespace Tizen { namespace Ui { namespace Controls
44 {
45
46 _ContextMenuGridPresenter::_ContextMenuGridPresenter(_ContextMenu* pContextMenu)
47         : __pContextMenu(pContextMenu)
48         , __pModel(null)
49         , __pTextObject(null)
50         , __pFont(null)
51         , __layoutSize(Dimension(0, 0))
52         , __touchOutRect(false)
53         , __selectedIndex(-1)
54         , __pressedIndex(-1)
55         , __maxWidth(0)
56         , __minWidth(0)
57         , __topMargin(0)
58         , __bottomMargin(0)
59         , __leftMargin(0)
60         , __rightMargin(0)
61         , __screenTopMargin(0)
62         , __screenBottomMargin(0)
63         , __screenLeftMargin(0)
64         , __screenRightMargin(0)
65         , __arrowMargin(0)
66         , __arrowWidth(0)
67         , __arrowHeight(0)
68         , __itemWidth(0)
69         , __itemHeight(0)
70         , __itemMaxWidth(0)
71         , __itemTextMargin(0)
72         , __itemGap(0)
73         , __itemBitmapWidth(0)
74         , __itemBitmapHeight(0)
75         , __itemFontSize(0)
76         , __dividerHeight(0)
77 {
78
79 }
80
81 _ContextMenuGridPresenter::~_ContextMenuGridPresenter(void)
82 {
83         __pContextMenu = null;
84
85         delete __pModel;
86         __pModel = null;
87
88         if (__pTextObject)
89         {
90                 __pTextObject->RemoveAll();
91                 delete __pTextObject;
92                 __pTextObject = null;
93         }
94
95         __pFont = null;
96
97 }
98
99 result
100 _ContextMenuGridPresenter::Install(void)
101 {
102         result r = E_SUCCESS;
103
104         LoadShape();
105
106         __pModel = new (std::nothrow) _ContextMenuModel;
107         SysTryCatch(NID_UI_CTRL, __pModel != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
108
109         r = __pModel->Construct();
110         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create data instance.");
111
112         __pFont = __pContextMenu->GetFallbackFont();
113         r = GetLastResult();
114         SysTryCatch(NID_UI_CTRL, (__pFont != null), , r, "[%s] Propagating.", GetErrorMessage(r));
115
116         __pTextObject = new (std::nothrow) TextObject();
117         SysTryCatch(NID_UI_CTRL, __pTextObject, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create data instance.");
118
119         __pTextObject->Construct();
120         __pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
121         __pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
122         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
123
124         return r;
125
126 CATCH:
127         delete __pModel;
128         __pModel = null;
129
130         __pFont = null;
131
132         delete __pTextObject;
133         __pTextObject = null;
134
135         return r;
136 }
137
138
139 void
140 _ContextMenuGridPresenter::LoadShape(void)
141 {
142         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __itemWidth);
143         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_MAX_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __itemMaxWidth);
144         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __itemHeight);
145         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_TEXT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __itemTextMargin);
146         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_GAP, _CONTROL_ORIENTATION_PORTRAIT, __itemGap);
147
148         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_MIN_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __minWidth);
149         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __topMargin);
150         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __bottomMargin);
151         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __leftMargin);
152         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __rightMargin);
153
154         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ITEM_FONT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, __itemFontSize);
155         GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_TOP_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenTopMargin);
156         GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenBottomMargin);
157         GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_LEFT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenLeftMargin);
158         GET_SHAPE_CONFIG(CONTEXTMENU::SCREEN_RIGHT_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __screenRightMargin);
159         GET_SHAPE_CONFIG(CONTEXTMENU::ANCHOR_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, __arrowMargin);
160         GET_SHAPE_CONFIG(CONTEXTMENU::ANCHOR_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __arrowWidth);
161         GET_SHAPE_CONFIG(CONTEXTMENU::ANCHOR_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __arrowHeight);
162         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ICON_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __itemBitmapWidth);
163         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_ICON_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __itemBitmapHeight);
164         GET_SHAPE_CONFIG(CONTEXTMENU::GRID_DIVIDER_HEIGHT, _CONTROL_ORIENTATION_PORTRAIT, __dividerHeight);
165 }
166
167 _ContextMenuItem*
168 _ContextMenuGridPresenter::CreateItem(const Base::String& text, int actionId, const Tizen::Graphics::Bitmap* pNormalBitmap, const Tizen::Graphics::Bitmap* pPressedBitmap, const Tizen::Graphics::Bitmap* pHighlightedBitmap)
169 {
170         _ContextMenuItem* pItem = _ContextMenuItem::CreateContextMenuItemN();
171         SysTryReturn(NID_UI_CTRL, pItem != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
172
173         ContextMenuItemDrawingType itemType = CONTEXT_MENU_ITEM_DRAWING_TYPE_NONE;
174         result r = E_SUCCESS;
175
176         if (text.GetLength() != 0)
177         {
178                 r = pItem->SetText(text);
179                 if (r != E_SUCCESS)
180                 {
181                         delete pItem;
182                         return null;
183                 }
184
185                 itemType = CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT;
186         }
187         else
188         {
189                 if (pNormalBitmap == null && pPressedBitmap == null)
190                 {
191                         delete pItem;
192                         return null;
193                 }
194
195                 if (pNormalBitmap != null)
196                 {
197                         r = pItem->SetBitmap(CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL, pNormalBitmap);
198                         if (r != E_SUCCESS)
199                         {
200                                 delete pItem;
201                                 return null;
202                         }
203                 }
204
205                 if (pPressedBitmap != null)
206                 {
207                         r = pItem->SetBitmap(CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED, pPressedBitmap);
208                         if (r != E_SUCCESS)
209                         {
210                                 delete pItem;
211                                 return null;
212                         }
213                 }
214
215                 itemType = CONTEXT_MENU_ITEM_DRAWING_TYPE_BITMAP;
216         }
217
218         pItem->SetType(itemType);
219         pItem->SetActionId(actionId);
220
221         // calculate item size
222         SetItemSize(pItem);
223
224         return pItem;
225 }
226
227 result
228 _ContextMenuGridPresenter::AddItem(const Base::String& text, int actionId, const Tizen::Graphics::Bitmap* normalBitmap,
229                                                                    const Tizen::Graphics::Bitmap* pPressedBitmap,
230                                                                    const Tizen::Graphics::Bitmap* pHighlightedBitmap)
231 {
232         _ContextMenuItem* pItem = CreateItem(text, actionId, normalBitmap, pPressedBitmap, pHighlightedBitmap);
233         SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
234
235         result r = __pModel->AddItem(pItem);
236
237         if (r != E_SUCCESS)
238         {
239                 delete pItem;
240                 SysTryReturn(NID_UI_CTRL, false, r, r, "Failed to add item.");
241         }
242
243
244         return r;
245 }
246
247 result
248 _ContextMenuGridPresenter::InsertItem(int index, const Tizen::Base::String& text, int actionId,
249                                                                           const Tizen::Graphics::Bitmap* normalBitmap, const Tizen::Graphics::Bitmap* pPressedBitmap,
250                                                                           const Tizen::Graphics::Bitmap* pHighlightedBitmap)
251 {
252         if (text.GetLength() == 0 && normalBitmap == null && pPressedBitmap == null)
253         {
254                 return E_INVALID_ARG;
255         }
256
257         _ContextMenuItem* pItem = null;
258
259         pItem = CreateItem(text, actionId, normalBitmap, pPressedBitmap, pHighlightedBitmap);
260         SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
261
262         result r = __pModel->InsertItem(pItem, index);
263         if (r != E_SUCCESS)
264         {
265                 delete pItem;
266                 SysTryReturn(NID_UI_CTRL, false, r, r, "Failed to add item.");
267         }
268
269         return E_SUCCESS;
270 }
271
272 result
273 _ContextMenuGridPresenter::SetItem(int index, const Tizen::Base::String& text, int actionId,
274                                                                    const Tizen::Graphics::Bitmap* normalBitmap, const Tizen::Graphics::Bitmap* pPressedBitmap,
275                                                                    const Tizen::Graphics::Bitmap* pHighlightedBitmap)
276 {
277         if (text.GetLength() == 0 && normalBitmap == null && pPressedBitmap == null)
278         {
279                 return E_INVALID_ARG;
280         }
281
282         _ContextMenuItem* pItem = null;
283         pItem = CreateItem(text, actionId, normalBitmap, pPressedBitmap, pHighlightedBitmap);
284         SysTryReturn(NID_UI_CTRL, pItem != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
285
286         result r = __pModel->SetItem(pItem, index);
287         if (r != E_SUCCESS)
288         {
289                 delete pItem;
290                 SysTryReturn(NID_UI_CTRL, false, r, r, "Failed to add item.");
291         }
292
293         return E_SUCCESS;
294 }
295
296 result
297 _ContextMenuGridPresenter::DeleteItem(int index)
298 {
299         int itemCount = __pContextMenu->GetItemCount();
300
301         if (itemCount <= 0 || index >= itemCount || index < 0)
302         {
303                 SysLogException(NID_UI_CTRL, E_INVALID_STATE, "Invalid argument.");
304                 return E_INVALID_STATE;
305         }
306
307         result r = __pModel->RemoveItem(index);
308         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to delete item.");
309
310         return r;
311 }
312
313 result
314 _ContextMenuGridPresenter::DeleteItemAll(void)
315 {
316         int itemCount = __pContextMenu->GetItemCount();
317
318         if (itemCount <= 0)
319         {
320                 SysLogException(NID_UI_CTRL, E_INVALID_STATE, "Invalid argument.");
321                 return E_INVALID_STATE;
322         }
323
324         result r =  __pModel->RemoveAllItem();
325         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to delete item all.");
326
327         return r;
328 }
329
330
331 int
332 _ContextMenuGridPresenter::CalculateShowItemCount(void)
333 {
334         return __pContextMenu->GetShowItemCount();
335 }
336
337 void
338 _ContextMenuGridPresenter::SetItemSize(_ContextMenuItem* pItem)
339 {
340         ContextMenuItemDrawingType itemType = pItem->GetType();
341
342         if (itemType == CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT)
343         {
344                 int textMargin = __itemTextMargin;
345                 int minWidth = __itemWidth;
346                 int maxWidth = __itemMaxWidth;
347
348                 String text = pItem->GetText();
349
350                 textMargin = textMargin * 2;
351                 minWidth = minWidth - textMargin;
352                 maxWidth = maxWidth - textMargin;
353
354                 Dimension textArea(0, 0);
355                 Dimension itemSize(0, 0);
356
357                 __pFont->GetTextExtent(text, text.GetLength(), textArea);
358
359                 if (textArea.width < minWidth)
360                 {
361                         textArea.width = minWidth;
362                 }
363
364                 if (textArea.width > maxWidth)
365                 {
366                         textArea.width = maxWidth;
367                 }
368
369                 itemSize.width = textArea.width + textMargin;
370                 itemSize.height = __itemHeight;
371
372                 pItem->SetSize(itemSize);
373
374         }
375         else if (itemType == CONTEXT_MENU_ITEM_DRAWING_TYPE_BITMAP)
376         {
377                 Dimension itemSize(__itemWidth, __itemHeight);
378
379                 pItem->SetSize(itemSize);
380         }
381 }
382
383 result
384 _ContextMenuGridPresenter::CalculateWindowRect(void)
385 {
386         result r = E_SUCCESS;
387
388         AdjustItemLayout();
389
390         r = CalculateRect();
391
392         AdjustItemPosition();
393
394         return r;
395 }
396
397 result
398 _ContextMenuGridPresenter::ApplyColorProperty(void)
399 {
400         return E_SUCCESS;
401 }
402
403
404 result
405 _ContextMenuGridPresenter::CalculateRect(void)
406 {
407         Tizen::Graphics::Rectangle windowRect = Tizen::Graphics::Rectangle(0, 0, 0, 0);  // Window Rect is bodyRect + arrowRect
408         Tizen::Graphics::Rectangle bodyRect = Tizen::Graphics::Rectangle(0, 0, 0, 0);    // ContextMenu rect except arrowRect
409         Tizen::Graphics::Rectangle arrowRect = Tizen::Graphics::Rectangle(0, 0, 0, 0);    // Arrow rect of the ContextMenu
410
411         int bodyTopMargin = __topMargin;
412         int bodyBottomMargin = __bottomMargin;
413         int bodyLeftMargin = __leftMargin;
414         int bodyRightMargin = __rightMargin;
415
416         int screenTopMargin = __screenTopMargin;
417         int screenBottomMargin = __screenBottomMargin;
418         int screenLeftMargin = __screenLeftMargin;
419         int screenRightMargin = __screenRightMargin;
420         int arrowMargin = __arrowMargin;
421
422         Point anchorPosition = __pContextMenu->GetAnchorPosition();
423 //      Point arrowPosition;
424
425         Dimension screen = _ControlManager::GetInstance()->GetScreenSize();
426         if (__pContextMenu->GetLayout() == _CONTROL_ORIENTATION_LANDSCAPE)
427         {
428                 screen.SetSize(screen.height, screen.width);
429         }
430
431         // calculate arrow area
432         enum ContextMenuCoreDropPosition dropPosition = __pContextMenu->GetDropPosition();
433         if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_UP || dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_DOWN) // down, up Arrow
434         {
435                 arrowRect.width = __arrowWidth;
436                 arrowRect.height = __arrowHeight;
437         }
438         else //  left, right Arrow
439         {
440                 arrowRect.width = __arrowHeight;
441                 arrowRect.height = __arrowWidth;
442         }
443
444         // calculate body rect
445         // calculate real drawing margin for contextmenu
446         int leftMargin = screenLeftMargin;
447         int rightMargin = screen.width - screenRightMargin;
448         int topMargin = screenTopMargin;
449         int bottomMargin = screen.height - screenBottomMargin;
450
451         if (__pModel->GetItemCount() <= 0)
452         {
453                 __layoutSize.width = __itemWidth;
454                 __layoutSize.height = __itemHeight;
455         }
456
457         bodyRect.width = __layoutSize.width + bodyLeftMargin + bodyRightMargin;
458         bodyRect.height = __layoutSize.height + bodyTopMargin + bodyBottomMargin;
459
460         // calculate the position of the arrow and body rect.
461         if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_UP)   // down Arrow
462         {
463                 // Check touch position
464                 //  - Check left margin
465                 if (anchorPosition.x <= leftMargin + arrowRect.width)
466                 {
467                         anchorPosition.x = leftMargin + arrowRect.width;
468                 }
469                 //  - check right margin
470                 if (anchorPosition.x >= (rightMargin - arrowRect.width))
471                 {
472                         anchorPosition.x = rightMargin - arrowRect.width;
473                 }
474                 //  - check top margin
475                 if (anchorPosition.y <= topMargin)
476                 {
477                         anchorPosition.y = topMargin + bodyRect.height;
478                 }
479                 //  - check bottom margin
480                 if (anchorPosition.y >= bottomMargin)
481                 {
482                         anchorPosition.y = bottomMargin;
483                 }
484
485                 // Set body position x
486                 bodyRect.x = anchorPosition.x - (bodyRect.width / 2);
487                 //  - Check left margin
488                 if (bodyRect.x <= leftMargin)
489                 {
490                         bodyRect.x = leftMargin;
491                 }
492                 //  - check right margin
493                 if ((bodyRect.x + bodyRect.width) >= rightMargin)
494                 {
495                         bodyRect.x = rightMargin - bodyRect.width;
496                 }
497                 // Set body position y
498                 bodyRect.y = anchorPosition.y - arrowRect.height - bodyRect.height;
499                 //  - check top margin
500                 if (bodyRect.y <= topMargin)
501                 {
502                         bodyRect.y = topMargin;
503                 }
504
505                 windowRect.x = bodyRect.x;
506                 windowRect.y = bodyRect.y;
507                 windowRect.width = bodyRect.width;
508                 windowRect.height = bodyRect.height + arrowRect.height;
509
510                 bodyRect.x = 0;
511                 bodyRect.y = arrowMargin;
512
513                 // Set arrow position
514                 arrowRect.x = anchorPosition.x - (arrowRect.width / 2) - windowRect.x;
515                 arrowRect.y = bodyRect.height;
516         }
517         else if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_DOWN)    // up Arrow
518         {
519                 // Check touch position
520                 //  - Check left margin
521                 if (anchorPosition.x <= leftMargin + arrowRect.width)
522                 {
523                         anchorPosition.x = leftMargin + arrowRect.width;
524                 }
525                 //  - check right margin
526                 if (anchorPosition.x >= (rightMargin - arrowRect.width))
527                 {
528                         anchorPosition.x = rightMargin - arrowRect.width;
529                 }
530                 //  - check top margin
531                 if (anchorPosition.y <= topMargin)
532                 {
533                         anchorPosition.y = topMargin;
534                 }
535                 //  - check bottom margin
536                 if (anchorPosition.y >= bottomMargin)
537                 {
538                         anchorPosition.y = bottomMargin;
539                 }
540
541                 // Set body position x
542                 bodyRect.x = anchorPosition.x - (bodyRect.width / 2);
543                 //  - Check left margin
544                 if (bodyRect.x <= leftMargin)
545                 {
546                         bodyRect.x = leftMargin;
547                 }
548                 //  - check right margin
549                 if ((bodyRect.x + bodyRect.width) >= rightMargin)
550                 {
551                         bodyRect.x = rightMargin - bodyRect.width;
552                 }
553                 // Set body position y
554                 bodyRect.y = anchorPosition.y + arrowRect.height;
555                 //  - Check bottom margin
556                 if ((bodyRect.y + bodyRect.height) >= bottomMargin)
557                 {
558                         bodyRect.y = bottomMargin - bodyRect.height;
559                 }
560
561                 windowRect.x = bodyRect.x;
562                 windowRect.y = bodyRect.y - arrowRect.height;
563                 windowRect.width = bodyRect.width;
564                 windowRect.height = bodyRect.height + arrowRect.height;
565
566                 bodyRect.x = 0;
567                 bodyRect.y = arrowRect.height - arrowMargin;
568
569                 // Set arrow position
570                 arrowRect.x = anchorPosition.x - (arrowRect.width / 2) - windowRect.x;
571                 arrowRect.y = 0;
572         }
573         else if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_LEFT)    // right Arrow
574         {
575                 // Check touch position
576                 //  - Check left margin
577                 if (anchorPosition.x <= leftMargin)
578                 {
579                         anchorPosition.x = leftMargin + bodyRect.width;
580                 }
581                 //  - check right margin
582                 if (anchorPosition.x >= rightMargin)
583                 {
584                         anchorPosition.x = rightMargin;
585                 }
586                 //  - Check top margin
587                 if (anchorPosition.y <= topMargin + arrowRect.height)
588                 {
589                         anchorPosition.y = topMargin + arrowRect.height;
590                 }
591                 //  - check bottom margin
592                 if (anchorPosition.y >= (bottomMargin - arrowRect.height))
593                 {
594                         anchorPosition.y = bottomMargin - arrowRect.height;
595                 }
596
597                 // Set body position x
598                 bodyRect.x = anchorPosition.x - arrowRect.width - bodyRect.width;
599                 //  - Check left margin
600                 if (bodyRect.x <= leftMargin)
601                 {
602                         bodyRect.x = leftMargin;
603                 }
604                 //  - check right margin
605                 if ((bodyRect.x + bodyRect.width + arrowRect.width) >= rightMargin)
606                 {
607                         bodyRect.x = rightMargin - bodyRect.width - arrowRect.width;
608                 }
609
610                 // Set body position y
611                 bodyRect.y = anchorPosition.y - (bodyRect.height / 2);
612                 // - check top margin
613                 if (bodyRect.y <= topMargin)
614                 {
615                         bodyRect.y = topMargin;
616                 }
617                 // - check bottom margin
618                 if ((bodyRect.y + bodyRect.height) >= bottomMargin)
619                 {
620                         bodyRect.y = bottomMargin - bodyRect.height;
621                 }
622
623                 windowRect.x = bodyRect.x;
624                 windowRect.y = bodyRect.y;
625                 windowRect.width = bodyRect.width + arrowRect.width;
626                 windowRect.height = bodyRect.height;
627
628                 bodyRect.x = arrowMargin;
629                 bodyRect.y = 0;
630
631                 // Set arrow position
632                 arrowRect.x = bodyRect.width;
633                 arrowRect.y = anchorPosition.y - (arrowRect.height / 2) - windowRect.y;
634         }
635         else    // left Arrow
636         {
637                 // Check touch position
638                 //  - Check top margin
639                 if (anchorPosition.x <= leftMargin)
640                 {
641                         anchorPosition.x = leftMargin;
642                 }
643                 //  - check right margin
644                 if (anchorPosition.x >= rightMargin)
645                 {
646                         anchorPosition.x = rightMargin - bodyRect.width;
647                 }
648                 //  - Check top margin
649                 if (anchorPosition.y <= topMargin + arrowRect.height)
650                 {
651                         anchorPosition.y = topMargin + arrowRect.height;
652                 }
653                 //  - check bottom margin
654                 if (anchorPosition.y >= (bottomMargin - arrowRect.height))
655                 {
656                         anchorPosition.y = bottomMargin - arrowRect.height;
657                 }
658
659                 // Set body position x
660                 bodyRect.x = anchorPosition.x + arrowRect.width;
661                 //  - Check left margin
662                 if (bodyRect.x <= leftMargin)
663                 {
664                         bodyRect.x = leftMargin;
665                 }
666                 //  - check right margin
667                 if ((bodyRect.x + bodyRect.width) >= rightMargin)
668                 {
669                         bodyRect.x = rightMargin - bodyRect.width;
670                 }
671                 // Set body position y
672                 bodyRect.y = anchorPosition.y - (bodyRect.height / 2);
673                 // - check top margin
674                 if (bodyRect.y <= topMargin)
675                 {
676                         bodyRect.y = topMargin;
677                 }
678                 // - check bottom margin
679                 if ((bodyRect.y + bodyRect.height) >= bottomMargin)
680                 {
681                         bodyRect.y = bottomMargin - bodyRect.height;
682                 }
683
684                 windowRect.x = bodyRect.x + arrowRect.width;
685                 windowRect.y = bodyRect.y;
686                 windowRect.width = bodyRect.width + arrowRect.width;
687                 windowRect.height = bodyRect.height;
688
689                 bodyRect.x = arrowRect.width - arrowMargin;
690                 bodyRect.y = 0;
691
692                 // Set arrow position
693                 arrowRect.x = 0;
694                 arrowRect.y = anchorPosition.y - (arrowRect.height / 2) - windowRect.y;
695         }
696
697         __pContextMenu->SetBodyRect(bodyRect);
698         __pContextMenu->SetArrowRect(arrowRect);
699         __pContextMenu->SetWindowRect(windowRect);
700         __pContextMenu->SetMovable(true);     // To do Modify a enabling
701         __pContextMenu->SetResizable(true);   // To do Modify a enabling
702         __pContextMenu->SetMinimumSize(Dimension(0, 0));    // To do Modify
703         __pContextMenu->SetMaximumSize(screen);             // To do Modify
704         __pContextMenu->SetBounds(windowRect);
705         __pContextMenu->SetMovable(false);    // To do Modify a enabling
706         __pContextMenu->SetResizable(false);  // To do Modify a enabling
707
708         return E_SUCCESS;
709 }
710
711 bool
712 _ContextMenuGridPresenter::IsLayoutBitmapOnly(void)
713 {
714         int itemCount = __pModel->GetItemCount();
715
716         if (itemCount <= 0)
717         {
718                 return false;
719         }
720
721         _ContextMenuItem* pItem = null;
722
723         for (int i = 0; i < itemCount; i++)
724         {
725                 pItem = __pModel->GetItem(i);
726                 if (pItem == null)
727                 {
728                         return false;
729                 }
730
731                 if (pItem->GetType() == CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT)
732                 {
733                         return false;
734                 }
735         }
736
737         return true;
738 }
739
740 void
741 _ContextMenuGridPresenter::AdjustItemLayout(void)
742 {
743         int itemCount = __pModel->GetItemCount();
744         Dimension layoutSize(0, 0);
745
746         if (itemCount <= 0)
747         {
748                 __layoutSize.width = __itemWidth;
749                 __layoutSize.height = __itemHeight;
750                 return;
751         }
752
753         bool bitmapOnly = IsLayoutBitmapOnly();
754
755         if (bitmapOnly == true)
756         {
757                 layoutSize = AdjustItemLayoutIconStyle();
758         }
759         else
760         {
761                 layoutSize = AdjustItemLayoutTabStyle();
762         }
763
764         Dimension screen = _ControlManager::GetInstance()->GetScreenSize();
765
766         if (__pContextMenu->GetLayout() == _CONTROL_ORIENTATION_LANDSCAPE)
767         {
768                 screen.SetSize(screen.height, screen.width);
769         }
770
771         int maxHeight = screen.height - __screenTopMargin - __screenBottomMargin - __arrowHeight;
772         if (layoutSize.height > maxHeight)
773         {
774                 layoutSize.height = maxHeight;
775         }
776
777         __layoutSize = layoutSize;
778 }
779
780 Tizen::Graphics::Dimension
781 _ContextMenuGridPresenter::AdjustItemLayoutIconStyle(void)
782 {
783         int countPerLine = 0;
784         int itemCount = __pModel->GetItemCount();
785         int itemWidth = __itemWidth + __itemGap;
786         int itemHeight = __itemHeight + __itemGap;
787
788         if (itemCount <= MAX_COUNT_PER_LINE)
789         {
790                 countPerLine = itemCount;
791         }
792         else
793         {
794                 countPerLine = GetCountPerLine(itemCount, MIN_COUNT_PER_LINE, MAX_COUNT_PER_LINE);
795         }
796
797         int lineCount = itemCount / countPerLine;
798         if (itemCount % countPerLine != 0)
799         {
800                 lineCount++;
801         }
802
803         Dimension layoutSize(0, 0);
804
805         layoutSize.width = countPerLine * __itemWidth + (countPerLine -1) * __itemGap;
806         layoutSize.height = lineCount * __itemHeight + (lineCount - 1) *__itemGap;
807
808         _ContextMenuItem* pItem = null;
809
810         for (int line = 0; line < lineCount; line++)
811         {
812                 for (int i = 0; i < countPerLine; i++)
813                 {
814                         int index = (line * countPerLine) + i;
815                         if (index >= itemCount)
816                         {
817                                 break;
818                         }
819
820                         pItem = __pModel->GetItem(index);
821
822                         if (pItem == null)
823                         {
824                                 break;
825                         }
826
827                         Rectangle drawRect(0, 0, __itemWidth, __itemHeight);
828
829                         drawRect.x = (i * itemWidth);
830                         drawRect.y = (line * itemHeight);
831
832                         bool drawDivider = true;
833                         if (i == countPerLine -1)
834                         {
835                                 drawDivider = false;
836                         }
837
838                         pItem->SetDrawRect(drawRect);
839                         pItem->SetDivider(drawDivider);
840                 }
841         }
842
843         return layoutSize;
844
845 }
846
847 Tizen::Graphics::Dimension
848 _ContextMenuGridPresenter::AdjustItemLayoutTabStyle(void)
849 {
850         int lineWidthList[MAX_LINE_COUNT] = {0, };
851         int lineItemCountList[MAX_LINE_COUNT] = {0, };
852
853         int itemCount = __pModel->GetItemCount();
854         int maxWidth = __itemWidth;
855         int itemHeight = __itemHeight + __itemGap;
856         int lineCount = 1;
857         int lineWidth = 0;
858         int lineItemCount = 0;
859         int x = 0;
860         int y = 0;
861         int remainWidth;
862         Dimension screen = _ControlManager::GetInstance()->GetScreenSize();
863         Point anchorPosition = __pContextMenu->GetAnchorPosition();
864         ContextMenuCoreDropPosition dropPosition = __pContextMenu->GetDropPosition();
865
866         if (__pContextMenu->GetLayout() == _CONTROL_ORIENTATION_LANDSCAPE)
867         {
868             GET_SHAPE_CONFIG(CONTEXTMENU::GRID_MAX_WIDTH, _CONTROL_ORIENTATION_LANDSCAPE, __maxWidth);
869                 screen.SetSize(screen.height, screen.width);
870         }
871         else
872         {
873             GET_SHAPE_CONFIG(CONTEXTMENU::GRID_MAX_WIDTH, _CONTROL_ORIENTATION_PORTRAIT, __maxWidth);
874         }
875
876         if (dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_LEFT || dropPosition == CONTEXT_MENU_CORE_DROP_POSITION_RIGHT)
877         {
878                 remainWidth = anchorPosition.x * 2 < screen.width ? screen.width - anchorPosition.x : anchorPosition.x;
879                 remainWidth -= __screenLeftMargin + __leftMargin + __rightMargin + __screenRightMargin;
880                 remainWidth -= __arrowHeight;
881                 __maxWidth = __maxWidth < remainWidth ? __maxWidth : remainWidth;
882         }
883
884         _ContextMenuItem* pItem = null;
885         for (int i = 0; i < itemCount; i++)
886         {
887                 pItem = __pModel->GetItem(i);
888                 if (pItem == null)
889                 {
890                         break;
891                 }
892
893                 Dimension itemSize = pItem->GetSize();
894                 if (lineWidth + itemSize.width > __maxWidth)
895                 {
896                         _ContextMenuItem* pPreviousItem = __pModel->GetItem(i - 1);
897                         if (pPreviousItem != null)
898                         {
899                                 pPreviousItem->SetDivider(false);
900                         }
901
902                         x = 0;
903                         y = y + itemHeight;
904
905                         lineWidth = 0;
906                         lineItemCount = 0;
907                         lineCount++;
908                 }
909
910                 Rectangle drawRect(x, y, itemSize.width, itemSize.height);
911                 pItem->SetDrawRect(drawRect);
912                 pItem->SetDivider(true);
913
914                 x = x + itemSize.width + __itemGap;
915                 lineWidth = lineWidth + itemSize.width + __itemGap;
916
917                 if (lineWidth > maxWidth)
918                 {
919                         maxWidth = lineWidth;
920                 }
921
922                 lineItemCount++;
923                 if (lineItemCount > MAX_LINE_COUNT)
924                 {
925                         lineItemCount = MAX_LINE_COUNT;
926                         break;
927                 }
928
929                 lineWidthList[lineCount - 1] = lineWidth;
930                 lineItemCountList[lineCount -1] = lineItemCount;
931         }
932
933         // last item
934         if (pItem != null)
935         {
936                 pItem->SetDivider(false);
937         }
938
939         Dimension layoutSize(__itemWidth, __itemHeight);
940         layoutSize.width = maxWidth - __itemGap;
941         layoutSize.height = lineCount * __itemHeight + (lineCount - 1) *__itemGap;
942
943         if (lineCount == 1)
944         {
945                 return layoutSize;
946         }
947
948         // divide margin
949         int itemIndex = 0;
950         for (int line = 0; line < lineCount; line++)
951         {
952                 int width = lineWidthList[line];
953                 int count = lineItemCountList[line];
954
955                 if (maxWidth == width)
956                 {
957                         itemIndex += count;
958                         continue;
959                 }
960
961                 int margin = (maxWidth - width) / count;
962
963                 for (int i = 0; i < count; i++)
964                 {
965                         int index = itemIndex + i;
966                         pItem = __pModel->GetItem(index);
967                         if (pItem == null)
968                         {
969                                 break;
970                         }
971
972                         Rectangle drawRect = pItem->GetDrawRect();
973
974                         drawRect.width = drawRect.width + margin;
975                         drawRect.x = drawRect.x + (i * margin);
976
977                         pItem->SetDrawRect(drawRect);
978                 }
979
980                 itemIndex += count;
981         }
982
983         return layoutSize;
984 }
985
986 void
987 _ContextMenuGridPresenter::AdjustItemPosition(void)
988 {
989         int itemCount = __pModel->GetItemCount();
990
991         if (itemCount <= 0)
992         {
993                 return;
994         }
995
996         int x = __pContextMenu->GetBodyRect().x + __leftMargin;
997         int y = __pContextMenu->GetBodyRect().y + __topMargin;
998
999         _ContextMenuItem* pItem = null;
1000
1001         for (int i = 0; i < itemCount; i++)
1002         {
1003                 pItem = __pModel->GetItem(i);
1004
1005                 if (pItem == null)
1006                 {
1007                         break;
1008                 }
1009
1010                 Rectangle drawRect = pItem->GetDrawRect();
1011
1012                 drawRect.x += x;
1013                 drawRect.y += y;
1014
1015                 pItem->SetDrawRect(drawRect);
1016         }
1017 }
1018
1019 int
1020 _ContextMenuGridPresenter::GetCountPerLine(int count, int min, int max) const
1021 {
1022         int minBlank = max;
1023         int countPerLine = max;
1024
1025         for (int i = max; i >= min; i--)
1026         {
1027                 int remainder = count % i;
1028
1029                 if (remainder == 0)
1030                 {
1031                         countPerLine = i;
1032                         break;
1033                 }
1034
1035                 int blank = i - remainder;
1036
1037                 if (blank < minBlank)
1038                 {
1039                         minBlank = blank;
1040                         countPerLine = i;
1041                 }
1042         }
1043
1044         return countPerLine;
1045 }
1046
1047 int
1048 _ContextMenuGridPresenter::GetItemIndexFromPosition(const Tizen::Graphics::Point& point) const
1049 {
1050         int index = -1;
1051         int itemCount = __pModel->GetItemCount();
1052
1053         if (itemCount < 0)
1054         {
1055                 return -1;
1056         }
1057
1058         _ContextMenuItem* pItem = null;
1059
1060         for (int i = 0; i < itemCount; i++)
1061         {
1062                 pItem = __pModel->GetItem(i);
1063
1064                 if (pItem == null)
1065                 {
1066                         break;
1067                 }
1068
1069                 Rectangle drawRect = pItem->GetDrawRect();
1070
1071                 if (drawRect.Contains(point) == true)
1072                 {
1073                         index = i;
1074                         break;
1075                 }
1076         }
1077
1078         return index;
1079 }
1080
1081
1082 result
1083 _ContextMenuGridPresenter::Draw(void)
1084 {
1085         result r = E_SUCCESS;
1086
1087         Canvas* pCanvas = __pContextMenu->GetCanvasN();
1088         SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "The memory is insufficient.");
1089
1090         // Clear canvas for drawing area of the ContextMenu.
1091         pCanvas->SetBackgroundColor(Color(0, 0, 0, 0));
1092         Tizen::Graphics::Rectangle bounds(__pContextMenu->GetWindowRect());
1093         pCanvas->Clear(Tizen::Graphics::Rectangle(0, 0, bounds.width, bounds.height));
1094
1095         r = DrawBackground(pCanvas);
1096         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to draw background.");
1097
1098         r = DrawArrow(pCanvas);
1099         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "Failed to draw arrow.");
1100
1101         r = DrawItem(pCanvas);
1102
1103         delete pCanvas;
1104
1105         return r;
1106 }
1107
1108 result
1109 _ContextMenuGridPresenter::DrawBackground(Canvas* pCanvas)
1110 {
1111         SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1112
1113         result r = E_SUCCESS;
1114
1115         Tizen::Graphics::Rectangle bodyRect = __pContextMenu->GetBodyRect();
1116
1117         const Bitmap* pBackgroundNormalBitmap = __pContextMenu->GetBackgroundNormalBitmap();
1118         const Bitmap* pBackgroundEffectBitmap = __pContextMenu->GetBackgroundEffectBitmap();
1119
1120         if (pBackgroundNormalBitmap == null && pBackgroundEffectBitmap == null)
1121         {
1122                 pCanvas->SetForegroundColor(__pContextMenu->GetColor());
1123                 pCanvas->DrawRectangle(bodyRect);
1124         }
1125         else
1126         {
1127                 if (pBackgroundNormalBitmap != null)
1128                 {
1129                         r = pCanvas->DrawNinePatchedBitmap(bodyRect, *pBackgroundNormalBitmap);
1130                 }
1131
1132                 if (pBackgroundEffectBitmap != null)
1133                 {
1134                         r = pCanvas->DrawNinePatchedBitmap(bodyRect, *pBackgroundEffectBitmap);
1135                 }
1136         }
1137
1138         return r;
1139 }
1140
1141
1142 result
1143 _ContextMenuGridPresenter::DrawArrow(Canvas* pCanvas)
1144 {
1145         SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1146
1147         result r = E_SUCCESS;
1148         Tizen::Graphics::Rectangle arrowRect = __pContextMenu->GetArrowRect();
1149         ContextMenuCoreDropPosition dropPosition = __pContextMenu->GetDropPosition();
1150
1151         const Bitmap* pArrowNormalBitmap = __pContextMenu->GetArrowNormalBitmap(dropPosition);
1152         const Bitmap* pEffectArrowBitmap = __pContextMenu->GetArrowEffectBitmap(dropPosition);
1153
1154         if (pArrowNormalBitmap != null)
1155         {
1156                 r = DrawBitmap(*pCanvas, arrowRect, *pArrowNormalBitmap);
1157         }
1158
1159         if (pEffectArrowBitmap != null)
1160         {
1161                 r = DrawBitmap(*pCanvas, arrowRect, *pEffectArrowBitmap);
1162         }
1163
1164         return r;
1165 }
1166
1167 result
1168 _ContextMenuGridPresenter::DrawItem(Tizen::Graphics::Canvas* pCanvas)
1169 {
1170         SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1171
1172         result r = E_SUCCESS;
1173         TextSimple* pSimpleText = null;
1174
1175         int itemCount = __pModel->GetItemCount();
1176
1177         if (itemCount <= 0)
1178         {
1179                 return E_SUCCESS;
1180         }
1181
1182         Rectangle bodyRect = __pContextMenu->GetBodyRect();
1183         Color pressedColor = __pContextMenu->GetItemColor(CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED);
1184
1185         int bitmapLeftMargin = __itemBitmapWidth >> 1;
1186         int bitmapTopMargin = __itemBitmapHeight >> 1;
1187         int linePositionX = bodyRect.x + __leftMargin + __rightMargin;
1188         int linePositionY = bodyRect.y + __topMargin;
1189
1190         _ContextMenuItem* pItem = null;
1191
1192         for (int i = 0; i < itemCount; i++)
1193         {
1194                 pItem = __pModel->GetItem(i);
1195
1196                 if (pItem == null)
1197                 {
1198                         continue;
1199                 }
1200
1201                 Rectangle rect = pItem->GetDrawRect();
1202                 ContextMenuItemDrawingType itemType = pItem->GetType();
1203
1204                 if (itemType == CONTEXT_MENU_ITEM_DRAWING_TYPE_TEXT)
1205                 {
1206                         Rectangle drawRect = rect;
1207                         drawRect.x = drawRect.x + __itemTextMargin;
1208                         drawRect.width = drawRect.width - (__itemTextMargin * 2);
1209
1210                         ContextMenuCoreItemStatus itemStatus = CONTEXT_MENU_CORE_ITEM_STATUS_NORMAL;
1211
1212                         if (__selectedIndex == i)
1213                         {
1214                                 itemStatus = CONTEXT_MENU_CORE_ITEM_STATUS_PRESSED;
1215
1216                                 //pCanvas->FillRectangle(pressedColor, rect);
1217                                 pCanvas->FillRoundRectangle(pressedColor, rect, Dimension(3, 3));
1218                         }
1219
1220                         String text = pItem->GetText();
1221
1222                         __pTextObject->RemoveAll();
1223                         pSimpleText = new (std::nothrow)TextSimple(const_cast <mchar*>(text.GetPointer()), text.GetLength());
1224                         SysTryReturn(NID_UI_CTRL, pSimpleText != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1225                         __pTextObject->AppendElement(*pSimpleText);
1226
1227                         __pTextObject->SetForegroundColor(__pContextMenu->GetTextColor(itemStatus), 0, __pTextObject->GetTextLength());
1228                         __pTextObject->SetBounds(drawRect);
1229                         __pTextObject->SetFont(__pFont, 0, __pTextObject->GetTextLength());
1230                         __pTextObject->Draw(*_CanvasImpl::GetInstance(*pCanvas));
1231
1232                 }
1233                 else
1234                 {
1235                         Rectangle drawRect = rect;
1236
1237                         drawRect.x = drawRect.x + (drawRect.width >> 1) - bitmapLeftMargin;
1238                         drawRect.y = drawRect.y + (drawRect.height >> 1) - bitmapTopMargin;
1239                         drawRect.width = __itemBitmapWidth;
1240                         drawRect.height = __itemBitmapHeight;
1241
1242                         ContextMenuItemDrawingStatus itemStatus = CONTEXT_MENU_ITEM_DRAWING_STATUS_NORMAL;
1243
1244                         if (__selectedIndex == i)
1245                         {
1246                                 itemStatus = CONTEXT_MENU_ITEM_DRAWING_STATUS_PRESSED;
1247
1248                                 //pCanvas->FillRectangle(pressedColor, rect);
1249                                 pCanvas->FillRoundRectangle(pressedColor, rect, Dimension(3, 3));
1250                         }
1251
1252                         const Bitmap* pBitmap = pItem->GetBitmap(itemStatus);
1253                         if (pBitmap != null)
1254                         {
1255                                 DrawBitmap(*pCanvas, drawRect, *pBitmap);
1256                         }
1257                 }
1258
1259                 // divder
1260                 bool drawDivider = pItem->GetDivider();
1261                 if (drawDivider == true)
1262                 {
1263                         int x = rect.x + rect.width;
1264                         int y = rect.y + __topMargin;
1265
1266                         Point point1(x, y);
1267                         Point point2(x, y + __dividerHeight);
1268
1269
1270                         DrawLine(pCanvas, point1, point2, true);
1271                 }
1272
1273                 // horizontal line
1274                 if (linePositionY != rect.y)
1275                 {
1276                         linePositionY = rect.y;
1277
1278                         int x = linePositionX + __layoutSize.width - __leftMargin - __rightMargin;
1279                         int y = linePositionY - __itemGap;
1280
1281                         Point point1(linePositionX, y);
1282                         Point point2(x, y);
1283
1284                         DrawLine(pCanvas, point1, point2, false);
1285                 }
1286         }
1287
1288         return r;
1289 }
1290
1291 result
1292 _ContextMenuGridPresenter::DrawLine(Tizen::Graphics::Canvas* pCanvas, Tizen::Graphics::Point point1, Tizen::Graphics::Point point2, bool drawVLine)
1293 {
1294         SysTryReturn(NID_UI_CTRL, pCanvas != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
1295
1296         result r = E_SUCCESS;
1297
1298         Color colorGridItemDivider01;
1299         Color colorGridItemDivider02;
1300
1301         GET_COLOR_CONFIG(CONTEXTMENU::GRID_ITEM_DIVIDER_01, colorGridItemDivider01);
1302         GET_COLOR_CONFIG(CONTEXTMENU::GRID_ITEM_DIVIDER_02, colorGridItemDivider02);
1303
1304         if (drawVLine == true)
1305         {
1306                 pCanvas->SetForegroundColor(colorGridItemDivider01);
1307                 pCanvas->DrawLine(point1, point2);
1308
1309                 point1.x++;
1310                 point2.x++;
1311
1312                 pCanvas->SetForegroundColor(colorGridItemDivider02);
1313                 pCanvas->DrawLine(point1, point2);
1314         }
1315         else
1316         {
1317                 pCanvas->SetForegroundColor(colorGridItemDivider01);
1318                 pCanvas->DrawLine(point1, point2);
1319
1320                 point1.y++;
1321                 point2.y++;
1322
1323                 pCanvas->SetForegroundColor(colorGridItemDivider02);
1324                 pCanvas->DrawLine(point1, point2);
1325         }
1326
1327         return r;
1328 }
1329
1330 _UiTouchEventDelivery
1331 _ContextMenuGridPresenter::OnPreviewTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
1332 {
1333         return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
1334 }
1335
1336 bool
1337 _ContextMenuGridPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
1338 {
1339         Rectangle bodyRect = __pContextMenu->GetBodyRect();
1340
1341         if (bodyRect.Contains(touchinfo.GetCurrentPosition()) == false)
1342         {
1343                 __selectedIndex = -1;
1344                 __pressedIndex = -1;
1345                 __touchOutRect = true;
1346
1347                 return true;
1348         }
1349
1350         __selectedIndex = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1351         __pressedIndex = __selectedIndex;
1352         __touchOutRect = false;
1353         Draw();
1354
1355         return true;
1356 }
1357
1358 _UiTouchEventDelivery
1359 _ContextMenuGridPresenter::OnPreviewTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
1360 {
1361         return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
1362 }
1363
1364 bool
1365 _ContextMenuGridPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
1366 {
1367         Rectangle bodyRect = __pContextMenu->GetBodyRect();
1368
1369         if (__touchOutRect == true)
1370         {
1371                 __pContextMenu->SetVisibleState(false);
1372         }
1373
1374         int currentSelectedIndex = GetItemIndexFromPosition(touchinfo.GetCurrentPosition());
1375         if (__selectedIndex != -1 && (__selectedIndex == currentSelectedIndex))
1376         {
1377                 _ActionEvent* pActionEvent = __pContextMenu->GetActionEvent();
1378
1379                 if (pActionEvent == null)
1380                 {
1381                         return true;
1382                 }
1383
1384                 _ContextMenuItem* pItem = null;
1385                 pItem = __pModel->GetItem(__selectedIndex);
1386
1387                 if (pItem == null)
1388                 {
1389                         __selectedIndex = -1;
1390                         __pressedIndex = -1;
1391                         return true;
1392                 }
1393
1394                 __selectedIndex = -1;
1395                 __pressedIndex = -1;
1396                 __pContextMenu->SetVisibleState(false);
1397
1398                 int actionId = pItem->GetActionId();
1399
1400                 IEventArg* pEventArg = _ActionEvent::CreateActionEventArgN(actionId);
1401
1402                 if (pEventArg == null)
1403                 {
1404                         return true;
1405                 }
1406
1407                 pActionEvent->Fire(*pEventArg);
1408         }
1409
1410         return true;
1411 }
1412
1413 bool
1414 _ContextMenuGridPresenter::OnTouchCanceled(const _Control& source, const _TouchInfo& touchinfo)
1415 {
1416         return true;
1417 }
1418
1419 _UiTouchEventDelivery
1420 _ContextMenuGridPresenter::OnPreviewTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1421 {
1422         return _UI_TOUCH_EVENT_DELIVERY_FORCED_YES;
1423 }
1424
1425 bool
1426 _ContextMenuGridPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
1427 {
1428         if (__selectedIndex != -1)
1429         {
1430                 _ContextMenuItem* pItem = __pModel->GetItem(__selectedIndex);
1431
1432                 if (pItem == null)
1433                 {
1434                         __selectedIndex = -1;
1435                         __pressedIndex = -1;
1436                         return true;
1437                 }
1438
1439                 Rectangle drawRect = pItem->GetDrawRect();
1440                 if (drawRect.Contains(touchinfo.GetCurrentPosition()) == false)
1441                 {
1442                         __selectedIndex = -1;
1443                         Draw();
1444                 }
1445         }
1446         else if (__pressedIndex != -1)
1447         {
1448                 _ContextMenuItem* pItem = __pModel->GetItem(__pressedIndex);
1449                 if(pItem != null)
1450                 {
1451                         Rectangle drawRect = pItem->GetDrawRect();
1452                         if(drawRect.Contains(touchinfo.GetCurrentPosition()))
1453                         {
1454                                 __selectedIndex = __pressedIndex;
1455                                 Draw();
1456                         }
1457                 }
1458         }
1459
1460         return true;
1461 }
1462
1463 void
1464 _ContextMenuGridPresenter::OnFontChanged(Font* pFont)
1465 {
1466         __pFont = pFont;
1467 }
1468
1469 void
1470 _ContextMenuGridPresenter::OnFontInfoRequested(unsigned long& style, int& size)
1471 {
1472         style = FONT_STYLE_PLAIN;
1473         size = __itemFontSize;
1474 }
1475
1476 void
1477 _ContextMenuGridPresenter::SetAllAccessibilityElement(void)
1478 {
1479         _AccessibilityContainer* pContainer = __pContextMenu->GetAccessibilityContainer();
1480         if (pContainer != null)
1481         {
1482                 int itemCount = __pModel->GetItemCount();
1483                 for (int i = 0; i < itemCount; i++)
1484                 {
1485                         _ContextMenuItem* pItem = __pModel->GetItem(i);
1486                         _AccessibilityElement* pElement = new (std::nothrow) _AccessibilityElement(true);
1487                         if (pItem != null && pElement != null)
1488                         {
1489                                 pElement->SetLabel(pItem->GetText());
1490 //                              pElement->SetTrait(ACCESSIBILITY_TRAITS_CONTEXTMENU);
1491                                 pElement->SetTrait(ACCESSIBILITY_TRAITS_LIST);  // not yet
1492                                 pElement->SetBounds(pItem->GetDrawRect());
1493                                 if (i == 0)
1494                                 {
1495                                         pElement->SetHint(L"The first of Contextual popup");
1496                                 }
1497                                 else if(i == itemCount - 1)
1498                                 {
1499                                         pElement->SetHint(L"The last of Contextual popup");
1500                                 }
1501                                 pContainer->AddElement(*pElement);
1502                                 __pContextMenu->AddAccessibilityElement(*pElement);
1503                         }
1504                         else
1505                         {
1506                                 delete pElement;
1507                         }
1508                 }
1509         }
1510
1511 }
1512
1513 _ContextMenuItemInfo
1514 _ContextMenuGridPresenter::GetItemFromPosition(const Point& position)
1515 {
1516         _ContextMenuItemInfo itemInfo;
1517         int index = GetItemIndexFromPosition(position);
1518         itemInfo.pContextMenuItem = __pModel->GetItem(index);
1519
1520         itemInfo.bListItem = false;
1521         itemInfo.pListItem = null;
1522         return itemInfo;
1523 }
1524
1525 _ContextMenuItemInfo
1526 _ContextMenuGridPresenter::FindItem(int index)
1527 {
1528         _ContextMenuItemInfo itemInfo;
1529         itemInfo.bListItem = false;
1530         itemInfo.pListItem = null;
1531         itemInfo.pContextMenuItem = __pModel->GetItem(index);
1532         return itemInfo;
1533 }
1534
1535 result
1536 _ContextMenuGridPresenter::SetTopDrawnItemIndex(int index)
1537 {
1538         return E_SUCCESS;
1539 }
1540
1541 result
1542 _ContextMenuGridPresenter::DrawBitmap(Tizen::Graphics::Canvas& canvas, const Tizen::Graphics::Rectangle& bounds, const Tizen::Graphics::Bitmap& bitmap)
1543 {
1544         result r = E_SUCCESS;
1545         if (bitmap.IsNinePatchedBitmap())
1546         {
1547                 r = canvas.DrawNinePatchedBitmap(bounds, bitmap);
1548                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Fail to draw ninepatched bitmap.")
1549         }
1550         else
1551         {
1552                 r = canvas.DrawBitmap(bounds, bitmap);
1553                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Fail to draw bitmap.")
1554         }
1555
1556         return r;
1557 }
1558
1559 }}} // Tizen::Ui: Control