1d1090484af4b2c8305cf48adb487c62db3eee06
[framework/osp/uifw.git] / src / ui / controls / FUiCtrl_TokenEditPresenter.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_TokenEditPresenter.cpp
19  * @brief               This is the implementation file for the _TokenEditPresenter class.
20  */
21 #include <FGrp_BitmapImpl.h>
22 #include <FUiAnimVisualElementAnimationGroup.h>
23 #include <FGrp_TextTextSimple.h>
24 #include <FGrp_FontImpl.h>
25 #include "FUiAnim_VisualElement.h"
26 #include "FUi_UiTouchEvent.h"
27 #include "FUiCtrl_TokenEdit.h"
28 #include "FUiCtrl_TokenEditPresenter.h"
29 #include "FUiCtrl_Scroll.h"
30 #include "FUi_Math.h"
31 #include "FUi_CoordinateSystemUtils.h"
32 #include "FUi_AccessibilityContainer.h"
33 #include "FUi_AccessibilityElement.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Runtime;
37 using namespace Tizen::Media;
38 using namespace Tizen::Ui::Animations;
39 using namespace Tizen::Graphics;
40 using namespace Tizen::Graphics::_Text;
41
42 namespace Tizen { namespace Ui { namespace Controls
43 {
44
45 float
46 SineTimingFunction::CalculateProgress(float timeProgress) const
47 {
48         const float segments[3][3] = {{0.0f, 0.01f, 0.37f}, {0.37f, 0.72f, 0.888f}, {0.888f, 0.9999f, 1.0f}};
49         float timeProgressValue = timeProgress;
50         int segmentsLength = 3; //Length of the segments array
51         int index = (int) floor(segmentsLength * timeProgressValue);
52         if (index >= segmentsLength)
53         {
54                 index = segmentsLength - 1;
55         }
56
57         float progressValue = (timeProgressValue - index * (1.0 / segmentsLength)) * segmentsLength;
58         float segmentAtIndex[3];
59         for (int i = 0; i < 3; i++)
60         {
61                 segmentAtIndex[i] = segments[index][i];
62         }
63         float ret = 0 + 1 * (segmentAtIndex[0] + progressValue * (2 * (1 - progressValue) * (segmentAtIndex[1] - segmentAtIndex[0]) + progressValue * (segmentAtIndex[2] - segmentAtIndex[0])));
64
65         return ret;
66 }
67
68 class _Token
69         : public Object
70 {
71 public:
72         _Token(void);
73         result Construct(const String& text, Font* pEditFont);
74         virtual ~_Token(void);
75         float GetTextPixelWidth(void) const;
76         wchar_t* GetText(void) const;
77         result ResetToken(const String& text);
78         result SetBounds(FloatRectangle bounds);
79         _VisualElement* GetVisualElement(void) const;
80         _Token(const _Token& src);
81         _Token& operator =(const _Token& value);
82
83 public:
84         FloatRectangle displayRect;
85         TextObject* pTextObject;
86         Font* pFont;
87         int currTokenLength;
88         bool isImplicitAnimation;
89         bool isTextCut;
90 private:
91         wchar_t* __pTextBuffer;
92         float __textPixelWidth;
93         float __textPixelHeight;
94         _VisualElement* __pVisualElement;
95 };  // _Token
96
97 _Token::_Token(void)
98         : pTextObject(null)
99         , pFont(null)
100         , currTokenLength(0)
101         , isImplicitAnimation(true)
102         , isTextCut(false)
103         , __pTextBuffer(null)
104         , __textPixelWidth(0.0f)
105         , __textPixelHeight(0.0f)
106         , __pVisualElement(null)
107 {
108 }
109
110 result
111 _Token::Construct(const String& text, Font* pEditFont)
112 {
113         result r = E_SUCCESS;
114
115         currTokenLength = text.GetLength();
116         float tokenFontSize = 0.0f;
117
118         pTextObject = new (std::nothrow) TextObject;
119         SysTryReturnResult(NID_UI_CTRL, pTextObject != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
120
121         // for default initialize.
122         r = pTextObject->Construct();
123         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
124
125         r = pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
126         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
127
128         r = pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
129         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
130
131         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, tokenFontSize);
132
133         pFont = _FontImpl::CloneN(*pEditFont);
134         r = GetLastResult();
135         SysTryCatch(NID_UI_CTRL, pFont != null, , r, "[%s] Propagating.", GetErrorMessage(r));
136
137         (_FontImpl::GetInstance(*pFont))->SetStyle(FONT_STYLE_PLAIN);
138         (_FontImpl::GetInstance(*pFont))->SetSize(tokenFontSize);
139
140         r = pTextObject->SetFont(pFont, 0, pTextObject->GetTextLength());
141         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
142
143         r = ResetToken(text);
144         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
145
146         __pVisualElement = new (std::nothrow) _VisualElement;
147         SysTryCatch(NID_UI_CTRL, __pVisualElement != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
148
149         r = __pVisualElement->Construct();
150         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct _VisualElement.", GetErrorMessage(r));
151
152         __pVisualElement->SetImplicitAnimationEnabled(false);
153         __pVisualElement->SetOpacity(0.0f);
154
155         return r;
156
157 CATCH:
158         delete pTextObject;
159         pTextObject = null;
160
161         if (__pVisualElement)
162         {
163                 __pVisualElement->Destroy();
164                 __pVisualElement = null;
165         }
166
167         delete pFont;
168         pFont = null;
169
170         return r;
171 }
172
173 result
174 _Token::ResetToken(const String& text)
175 {
176         result r = E_SUCCESS;
177         FloatDimension textSize;
178
179         if (__pTextBuffer)
180         {
181                 delete[] __pTextBuffer;
182                 __pTextBuffer = null;
183         }
184
185         int length = text.GetLength();
186         wchar_t* pTempString = const_cast< wchar_t* >(text.GetPointer());
187         SysTryReturnResult(NID_UI_CTRL, pTempString != null, E_SYSTEM, "A system error has occurred. Token text string is null.");
188
189         __pTextBuffer = new (std::nothrow) wchar_t[(length + 1) * (sizeof(wchar_t))];
190         SysTryReturnResult(NID_UI_CTRL, __pTextBuffer != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
191
192         for (int i = 0; i < length; i++)
193         {
194                 __pTextBuffer[i] = pTempString[i];
195         }
196         __pTextBuffer[length] = 0;
197
198         pTextObject->RemoveAll(true);
199
200         TextSimple* pSimpleText = new (std::nothrow) TextSimple(__pTextBuffer, length, TEXT_ELEMENT_SOURCE_TYPE_EXTERNAL, pFont);
201         SysTryCatch(NID_UI_CTRL, pSimpleText != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
202
203         r = pTextObject->AppendElement(*pSimpleText);
204         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
205
206         textSize = pTextObject->GetTextExtentF(0, length);
207         __textPixelWidth = textSize.width;
208         __textPixelHeight = textSize.height;
209
210         r = pTextObject->Compose();
211
212         currTokenLength = length;
213
214         return E_SUCCESS;
215
216 CATCH:
217         delete[] __pTextBuffer;
218         __pTextBuffer = null;
219
220         delete pSimpleText;
221         pSimpleText = null;
222
223         return E_SYSTEM;
224 }
225
226 result
227 _Token::SetBounds(FloatRectangle bounds)
228 {
229         result r = E_SUCCESS;
230         displayRect = bounds;
231         if (__pVisualElement)
232         {
233                 __pVisualElement->SetBounds(bounds);
234         }
235         return r;
236 }
237
238 _VisualElement*
239 _Token::GetVisualElement(void) const
240 {
241         return __pVisualElement;
242 }
243
244 _Token::~_Token(void)
245 {
246         delete pTextObject;
247         pTextObject = null;
248
249         delete pFont;
250         pFont = null;
251
252         delete[] __pTextBuffer;
253         __pTextBuffer = null;
254
255         if (__pVisualElement)
256         {
257                 __pVisualElement->Destroy();
258                 __pVisualElement = null;
259         }
260 }
261
262 float
263 _Token::GetTextPixelWidth(void) const
264 {
265         return __textPixelWidth;
266 }
267
268 wchar_t*
269 _Token::GetText(void) const
270 {
271         return __pTextBuffer;
272 }
273
274 _TokenEditPresenter::_TokenEditPresenter(void)
275         : __pTokenEdit(null)
276         , __pTokenList(null)
277         , __pTokenBgBitmap(null)
278         , __pTokenBgNormalEffectBitmap(null)
279         , __pTokenBgPressedEffectBitmap(null)
280         , __pTokenBgReplacementFocusBitmap(null)
281         , __pTokenBgFocusEffectBitmap(null)
282         , __pressedTokenIndex(-1)
283         , __isEditingToken(false)
284         , __editingTokenIndex(-1)
285         , __clientRect(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f))
286         , __initTextRect(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f))
287         , __isEditModeEnabled(true)
288         , __pDescriptionTextVisualElement(null)
289         , __pDescriptionTextTextObject(null)
290         , __isTokenEditingFinished(false)
291         , __prevScrollValue(0.0f)
292         , __scrollValue(0.0f)
293         , __isTokenScrolling(false)
294         , __isNeedToScroll(false)
295         , __maxScrollValue(0.0f)
296         , __autoShrink(false)
297         , __descriptionText(String())
298         , __isPopupVisible(false)
299         , __isLongPressed(false)
300         , __lineSpacing(0.0f)
301         , __animatingIndex(-1)
302         , __lastTokenIndex(-1)
303         , __pTimingFunction(null)
304         , __descriptionTextRectForScroll(FloatRectangle(0.0f, 0.0f, 0.0f, 0.0f))
305         , __previousTitleWidth(-1.0f)
306         , __isTokenEditPresenterInitialized(false)
307         , __isFocus(false)
308         , __previousCursorPosition(0)
309         , __isScrollValueChanged(false)
310         , __lineAdded(0)
311         , __isScrollValueModified(false)
312         , __isTouchMoveInProgress(false)
313         , __isTitleSliding(false)
314         , __touchPressInfo(FloatPoint(-1.0f, -1.0f))
315         , __editContentFontSize(0.0f)
316         , __trackTokenIndex(-1)
317         , __isAnimationInProgress(false)
318         , __focusOutIndex(-1)
319         , __accessibilityElements()
320         , __focusedTokenIndex(-1)
321         , __drawFocusState(false)
322 {
323 }
324
325 result
326 _TokenEditPresenter::InitializeDescriptionText(void)
327 {
328         result r = E_SUCCESS;
329         float descriptionTextSize = 0.0f;
330         Font* pFont = null;
331         float editFontSize = 0.0f;
332
333         __pDescriptionTextTextObject = new (std::nothrow) TextObject();
334         SysTryReturnResult(NID_UI_CTRL, __pDescriptionTextTextObject != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
335
336         // for default initialize.
337         r = __pDescriptionTextTextObject->Construct();
338         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
339
340         r = __pDescriptionTextTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
341         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
342
343         r = __pDescriptionTextTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
344         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
345
346         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, descriptionTextSize);
347
348         pFont = GetFont();
349         SysTryCatch(NID_UI_CTRL, pFont != null, , r, "[%s] Propagating.", GetErrorMessage(r));
350
351         editFontSize = GetTextSize();
352         (_FontImpl::GetInstance(*pFont))->SetSize(descriptionTextSize);
353
354         r = __pDescriptionTextTextObject->SetFont(pFont, 0, __pDescriptionTextTextObject->GetTextLength());
355         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
356
357         (_FontImpl::GetInstance(*pFont))->SetSize(editFontSize);
358
359         return r;
360
361 CATCH:
362
363         delete __pDescriptionTextTextObject;
364         __pDescriptionTextTextObject = null;
365
366         return r;
367 }
368
369 _TokenEditPresenter::~_TokenEditPresenter(void)
370 {
371         DisposeTokenEditPresenter();
372 }
373
374 result
375 _TokenEditPresenter::DisposeTokenEditPresenter(void)
376 {
377         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
378         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
379
380         if (pEditVisualElement && pCursorVisualElement)
381         {
382                 VisualElement* pCursorParent = pCursorVisualElement->GetParent();
383                 if (pCursorParent != pEditVisualElement)
384                 {
385                         if (pCursorParent)
386                         {
387                                 pCursorParent->DetachChild(*pCursorVisualElement);
388                                 pEditVisualElement->AttachChild(*pCursorVisualElement);
389                         }
390                 }
391         }
392
393         if (__pTokenList)
394         {
395                 __pTokenList->RemoveAll(true);
396                 delete __pTokenList;
397                 __pTokenList = null;
398         }
399
400         delete __pTokenBgBitmap;
401         __pTokenBgBitmap = null;
402
403         delete __pTokenBgNormalEffectBitmap;
404         __pTokenBgNormalEffectBitmap = null;
405
406         delete __pTokenBgPressedEffectBitmap;
407         __pTokenBgPressedEffectBitmap = null;
408
409         delete __pTokenBgReplacementFocusBitmap;
410         __pTokenBgReplacementFocusBitmap = null;
411
412         delete __pTokenBgFocusEffectBitmap;
413         __pTokenBgFocusEffectBitmap = null;
414
415         if (__pDescriptionTextVisualElement)
416         {
417                 __pDescriptionTextVisualElement->Destroy();
418                 __pDescriptionTextVisualElement = null;
419         }
420
421         delete __pDescriptionTextTextObject;
422         __pDescriptionTextTextObject = null;
423
424         if (__pTimingFunction)
425         {
426                 delete __pTimingFunction;
427                 __pTimingFunction = null;
428         }
429
430         RemoveChildAccessibilityElements();
431
432         return E_SUCCESS;
433 }
434
435 _TokenEditPresenter*
436 _TokenEditPresenter::CreateTokenEditPresenterN(void)
437 {
438         _TokenEditPresenter* pPresenter = new (std::nothrow) _TokenEditPresenter();
439         SysTryReturn(NID_UI_CTRL, pPresenter != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
440
441         return pPresenter;
442 }
443
444 result
445 _TokenEditPresenter::Initialize(const _Control& control)
446 {
447         result r = E_SUCCESS;
448
449         r = _EditPresenter::Initialize(control);
450         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
451
452         r = InitializeDescriptionText();
453         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
454
455         TextObject* pTextObject = GetTextObject();
456         if (pTextObject)
457         {
458                 pTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
459                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
460                 pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
461                 pTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_LEFT | TEXT_OBJECT_ALIGNMENT_MIDDLE);
462         }
463
464         __pTokenEdit = dynamic_cast< _TokenEdit* >(GetEditView());
465         SysTryReturnResult(NID_UI_CTRL, __pTokenEdit != null, E_SYSTEM, "A system error has occurred. The _Token instance is null.");
466
467         _TokenEditModel* pTokenEditModel = new (std::nothrow) _TokenEditModel();
468         SysTryReturnResult(NID_UI_CTRL, pTokenEditModel != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
469
470         SetModel(*pTokenEditModel);
471
472         SetKeypadEnabled(true);
473
474         float leftMargin = 0.0f;
475         float rightMargin = 0.0f;
476         float tokenTopMargin = 0.0f;
477         float tokenBottomMargin = 0.0f;
478         float tokenHeight = 0.0f;
479         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
480         Color focusTokenColor;
481         Bitmap* pTokenBgFocusBitmap = null;
482
483         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
484         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
485         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
486         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
487         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
488         GET_COLOR_CONFIG(TOKENEDIT::BG_HIGHLIGHTED, focusTokenColor);
489
490         // For drawing token in specific area
491         __clientRect.x = leftMargin;
492         __clientRect.y = tokenTopMargin;
493         __clientRect.width = __pTokenEdit->GetBoundsF().width - leftMargin - rightMargin;
494         __clientRect.height = __pTokenEdit->GetBoundsF().height - tokenTopMargin - tokenBottomMargin;
495
496         __initTextRect = GetTextBoundsF();
497
498         FloatRectangle tempDspRect(__initTextRect.x, __initTextRect.y, __clientRect.width, tokenHeight);
499         SetTextBounds(tempDspRect);
500
501         float textSize = 0.0f;
502         GET_SHAPE_CONFIG(TOKENEDIT::TEXT_SIZE, orientation, textSize);
503
504         __editContentFontSize = textSize;
505
506         _EditPresenter::SetTextSize(__editContentFontSize);
507
508         __pTokenList = new (std::nothrow) Collection::LinkedList();
509         SysTryCatch(NID_UI_CTRL, __pTokenList != null, , r = E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
510
511         __delimiter = L"\n";
512
513         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_NORMAL, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgBitmap);
514         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
515
516         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_NORMAL_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgNormalEffectBitmap);
517         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
518
519         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_PRESSED_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgPressedEffectBitmap);
520         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
521
522         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_FOCUS, BITMAP_PIXEL_FORMAT_ARGB8888, pTokenBgFocusBitmap);
523         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
524
525         __pTokenBgReplacementFocusBitmap = _BitmapImpl::GetColorReplacedBitmapN(
526                     *pTokenBgFocusBitmap,Color::GetColor(COLOR_ID_MAGENTA), focusTokenColor);
527         SysTryCatch(NID_UI_CTRL, __pTokenBgReplacementFocusBitmap != null, , r, "[%s] Propagating.", GetErrorMessage(r));
528
529         r = GET_BITMAP_CONFIG_N(TOKENEDIT::BG_FOCUS_EFFECT, BITMAP_PIXEL_FORMAT_ARGB8888, __pTokenBgFocusEffectBitmap);
530         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
531
532         delete pTokenBgFocusBitmap;
533         pTokenBgFocusBitmap = null;
534
535
536         __isTokenEditPresenterInitialized = true;
537
538         __previousCursorPosition = GetCursorPosition();
539
540         return r;
541
542 CATCH:
543
544         delete pTokenEditModel;
545         pTokenEditModel = null;
546
547         delete __pTokenBgBitmap;
548         __pTokenBgBitmap = null;
549
550         delete __pTokenBgNormalEffectBitmap;
551         __pTokenBgNormalEffectBitmap = null;
552
553         delete __pTokenBgPressedEffectBitmap;
554         __pTokenBgPressedEffectBitmap = null;
555
556         delete pTokenBgFocusBitmap;
557         pTokenBgFocusBitmap = null;
558
559         delete __pTokenBgReplacementFocusBitmap;
560         __pTokenBgReplacementFocusBitmap = null;
561
562         return r;
563 }
564
565 void
566 _TokenEditPresenter::DrawText(void)
567 {
568         bool isCustomBitmap = IS_CUSTOM_BITMAP(TOKENEDIT::BG_NORMAL);
569         //Checking IsBlocked() is additional check for handler movement in token edit mode
570         if ((__isEditingToken) && (__editingTokenIndex >= 0))
571         {
572                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
573                 if (pToken)
574                 {
575                         _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
576                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get visual element of token.");
577
578                         Canvas* pTokenCanvas = pTokenVisualElement->GetCanvasN();
579                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenCanvas != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get canvas of the token.");
580
581                         FloatRectangle tokenRect(0.0f, 0.0f, pToken->displayRect.width, pToken->displayRect.height);
582                         pTokenCanvas->SetBackgroundColor(Color(0));
583                         pTokenCanvas->Clear();
584
585                         Color selectedTokenColor = GetTokenEditColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED);
586                         Bitmap* pReplacementColorBackgroundBitmap = null;
587                         if (__pTokenBgBitmap)
588                         {
589                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), selectedTokenColor);
590
591                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pReplacementColorBackgroundBitmap))
592                                 {
593                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
594                                 }
595                                 else
596                                 {
597                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
598                                 }
599                         }
600                         else
601                         {
602                                 pTokenCanvas->FillRectangle(selectedTokenColor, tokenRect);
603                         }
604
605                         if (__pTokenBgPressedEffectBitmap && (!isCustomBitmap))
606                         {
607                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgPressedEffectBitmap))
608                                 {
609                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
610                                 }
611                                 else
612                                 {
613                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
614                                 }
615                         }
616
617                         _EditPresenter::DrawText(*pTokenCanvas);
618
619                         delete pTokenCanvas;
620                         pTokenCanvas = null;
621
622                         delete pReplacementColorBackgroundBitmap;
623                 }
624         }
625         else
626         {
627                 _VisualElement* pTextVisualElement = null;
628                 pTextVisualElement = GetTextVisualElement();
629                 if (pTextVisualElement)
630                 {
631                         _EditPresenter::DrawText();
632                 }
633                 else
634                 {
635                         Canvas* pCanvas = __pTokenEdit->GetCanvasN();
636                         SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get visual element of Control.");
637
638                         _EditPresenter::DrawText(*pCanvas);
639
640                         delete pCanvas;
641                         pCanvas = null;
642                 }
643         }
644 }
645
646 result
647 _TokenEditPresenter::Draw(Canvas& canvas)
648 {
649         if (!IsInitialized())
650         {
651                 InitializeAtFirstDrawing();
652
653                 if (IsFocused() == true)
654                 {
655                         ShowKeypad(false);
656                 }
657         }
658
659         canvas.SetBackgroundColor(Color(0, 0, 0, 0));
660         canvas.Clear();
661
662         DrawBackground(canvas);
663
664         DrawScrollBar();
665
666         if (__pDescriptionTextTextObject->GetTextLength() != 0)
667         {
668                 DrawDescriptionText();
669         }
670
671         if (GetTokenCount() != 0)
672         {
673                 DrawToken();
674         }
675
676         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
677         SysTryReturnResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
678
679         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
680         SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
681
682         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
683
684         if (pToken)
685         {
686                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
687                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "A system error has occurred. Failed to get visual element of token.");
688
689                 if (__isEditingToken)
690                 {
691                         Canvas* pTokenCanvas = pTokenVisualElement->GetCanvasN();
692                         SysTryReturnResult(NID_UI_CTRL, pTokenCanvas != null, E_SYSTEM, "A system error has occurred. Failed to get canvas of the token.");
693
694                         _EditPresenter::DrawText(*pTokenCanvas);
695
696                         InitializeCursor();
697
698                         delete pTokenCanvas;
699                         pTokenCanvas = null;
700                 }
701                 else
702                 {
703                         DrawText();
704                         InitializeCursor();
705                 }
706         }
707         else
708         {
709                 SysTryReturnResult(NID_UI_CTRL, !__isEditingToken, E_SYSTEM, "An invalid argument is given.");
710
711                 DrawText();
712                 InitializeCursor();
713         }
714
715         if (__isTokenEditingFinished)
716         {
717                 __isEditingToken = false;
718                 __editingTokenIndex = -1;
719                 _EditPresenter::SetTextSize(__editContentFontSize);
720
721                 __isTokenEditingFinished = false;
722         }
723
724         return E_SUCCESS;
725 }
726
727 bool
728 _TokenEditPresenter::DrawToken(int count)
729 {
730         int drawStartIndex = 0;
731         int tokenCount = 0;
732         float tokenTextLeftMargin = 0.0f;
733         float tokenVerticalSpacing = 0.0f;
734         bool isCustomBitmap = false;
735         bool isCustomFocusBitmap = false;
736
737         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, false, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
738
739         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
740
741         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
742         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
743
744         isCustomBitmap = IS_CUSTOM_BITMAP(TOKENEDIT::BG_NORMAL);
745         isCustomFocusBitmap = IS_CUSTOM_BITMAP(TOKENEDIT::BG_FOCUS);
746
747
748         if (__drawFocusState)
749         {
750                 __pTokenEdit->RefreshFocusUi();
751         }
752
753         if (count == -1)
754         {
755                 tokenCount = __pTokenList->GetCount();
756         }
757         else
758         {
759                 tokenCount = count;
760         }
761
762         Color normalTokenColor;
763         Color selectedTokenColor;
764
765         for (int i = drawStartIndex; i < tokenCount; i++)
766         {
767                 Bitmap* pReplacementColorBackgroundBitmap = null;
768
769                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
770                 if (pToken == null || pToken->pTextObject == null)
771                 {
772                         SysLog(NID_UI_CTRL, "[E_SYSTEM] The _Token instance is null");
773                         break;
774                 }
775
776                 normalTokenColor = GetTokenEditColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL);
777                 selectedTokenColor = GetTokenEditColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED);
778
779                 _VisualElement* pTokenElement = pToken->GetVisualElement();
780                 if (pTokenElement == null)
781                 {
782                         SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. The _VisualElement instance is null");
783                         break;
784                 }
785
786                 bool isSelected = false;
787                 Canvas* pTokenCanvas = pTokenElement->GetCanvasN();
788                 if (pTokenCanvas == null)
789                 {
790                         SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. The Canvas instance is null");
791                         break;
792                 }
793                 FloatRectangle tokenRect(0.0f, 0.0f, pToken->displayRect.width, pToken->displayRect.height);
794                 pTokenCanvas->SetBackgroundColor(Color(0));
795                 pTokenCanvas->Clear();
796
797                 if (__pressedTokenIndex == i && IsFocused())
798                 {
799                         if (__pTokenBgBitmap)
800                         {
801                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), selectedTokenColor);
802
803                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pReplacementColorBackgroundBitmap))
804                                 {
805                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
806                                 }
807                                 else
808                                 {
809                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
810                                 }
811                         }
812                         else
813                         {
814                                 pTokenCanvas->FillRectangle(selectedTokenColor, tokenRect);
815                         }
816
817
818                         if (__pTokenBgPressedEffectBitmap && (!isCustomBitmap))
819                         {
820                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgPressedEffectBitmap))
821                                 {
822                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
823                                 }
824                                 else
825                                 {
826                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgPressedEffectBitmap);
827                                 }
828                         }
829
830                         isSelected = true;
831                 }
832                 else
833                 {
834                         if (__pTokenBgBitmap)
835                         {
836                                 pReplacementColorBackgroundBitmap = _BitmapImpl::GetColorReplacedBitmapN(*__pTokenBgBitmap, Color::GetColor(COLOR_ID_MAGENTA), normalTokenColor);
837
838                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*pReplacementColorBackgroundBitmap))
839                                 {
840                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
841                                 }
842                                 else
843                                 {
844                                         pTokenCanvas->DrawBitmap(tokenRect, *pReplacementColorBackgroundBitmap);
845                                 }
846                         }
847                         else
848                         {
849                                 pTokenCanvas->FillRectangle(normalTokenColor, tokenRect);
850                         }
851
852
853                         if (__pTokenBgNormalEffectBitmap && (!isCustomBitmap))
854                         {
855                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgNormalEffectBitmap))
856                                 {
857                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgNormalEffectBitmap);
858                                 }
859                                 else
860                                 {
861                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgNormalEffectBitmap);
862                                 }
863                         }
864                 }
865
866                 if (__focusedTokenIndex == i && __drawFocusState && (!__isEditingToken))
867                 {
868                         if (__pTokenBgReplacementFocusBitmap)
869                         {
870                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgReplacementFocusBitmap))
871                                 {
872                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgReplacementFocusBitmap);
873                                 }
874                                 else
875                                 {
876                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgReplacementFocusBitmap);
877                                 }
878                         }
879
880                         if (__pTokenBgFocusEffectBitmap && (!isCustomFocusBitmap))
881                         {
882                                 if (_BitmapImpl::CheckNinePatchedBitmapStrictly(*__pTokenBgFocusEffectBitmap))
883                                 {
884                                         pTokenCanvas->DrawNinePatchedBitmap(tokenRect, *__pTokenBgFocusEffectBitmap);
885                                 }
886                                 else
887                                 {
888                                         pTokenCanvas->DrawBitmap(tokenRect, *__pTokenBgFocusEffectBitmap);
889                                 }
890                         }
891
892                 }
893
894                 pTokenElement->SetAnimationProvider(null);
895                 if (pToken->isImplicitAnimation)
896                 {
897                         VisualElementAnimation* pAnimation = CreateAnimationN(*pTokenElement, true);
898                         pTokenElement->AddAnimation(*pAnimation);
899                         delete pAnimation;
900                         pToken->isImplicitAnimation = false;
901                 }
902
903                 if (!__isEditingToken || __pressedTokenIndex != i)
904                 {
905                         Color textColor;
906                         FloatRectangle textRect(0.0f, 0.0f, pToken->displayRect.width - ((tokenTextLeftMargin * 2.0f)), pToken->displayRect.height - tokenVerticalSpacing);
907
908                         textRect.x += tokenTextLeftMargin;
909                         textRect.y += (tokenVerticalSpacing / 2.0f);
910
911                         if (isSelected)
912                         {
913                                 textColor = GetTokenEditTextColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED);
914                         }
915                         else
916                         {
917                                 textColor = GetTokenEditTextColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL);
918                         }
919
920                         pToken->pTextObject->SetForegroundColor(textColor, 0, pToken->pTextObject->GetTextLength());
921                         pToken->pTextObject->SetBounds(textRect);
922                         pToken->pTextObject->Compose();
923                         pToken->pTextObject->Draw(*_CanvasImpl::GetInstance(*pTokenCanvas));
924                 }
925
926                 pTokenElement->SetFlushNeeded();
927                 delete pTokenCanvas;
928
929                 delete pReplacementColorBackgroundBitmap;
930                 pReplacementColorBackgroundBitmap = null;
931         }
932         return true;
933 }
934
935 Color
936 _TokenEditPresenter::GetTokenEditColor(const ExpandableEditAreaTokenStatus status) const
937 {
938         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, Color(0, 0, 0, 0), E_INVALID_STATE, "[E_INVALID_STATE] _TokenEdit is in an invalid state.");
939
940         Color color;
941
942         if (status == EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL)
943         {
944                 color = __pTokenEdit->GetTokenColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL);
945         }
946         else
947         {
948                 color = __pTokenEdit->GetTokenColor(EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED);
949         }
950
951         return color;
952 }
953
954 Color
955 _TokenEditPresenter::GetTokenEditTextColor(const ExpandableEditAreaTokenStatus status) const
956 {
957         SysTryReturn(NID_UI_CTRL, __pTokenEdit != null, Color(0, 0, 0, 0), E_INVALID_STATE, "[E_INVALID_STATE] _TokenEdit is in an invalid state.");
958
959         Color color;
960
961         if (status == EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL)
962         {
963                 color = __pTokenEdit->GetTokenTextColor();
964         }
965         else
966         {
967                 color = __pTokenEdit->GetSelectedTokenTextColor();
968         }
969
970         return color;
971 }
972
973 result
974 _TokenEditPresenter::MakeToken(const String& tokenString)
975 {
976         SysTryReturnResult(NID_UI_CTRL, __pTokenEdit != null, E_INVALID_STATE, "_TokenEdit is in an invalid state.");
977         result r = E_SUCCESS;
978
979         int tokenCount = 0;
980         int index = -1;
981         int tokenLength = tokenString.GetLength();
982         SysTryReturnResult(NID_UI_CTRL, tokenLength, E_INVALID_ARG, "Invalid argument is used. Token length is (%d)", tokenLength);
983
984         String inputTokenString = tokenString;
985         String replacementString = inputTokenString;
986         bool enable = false;
987
988         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
989
990         if (enable)
991         {
992                 inputTokenString = replacementString;
993         }
994
995         if (inputTokenString.GetLength() <= 0)
996         {
997                 r = ClearText();
998                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Failed to clear text object.", GetErrorMessage(r));
999
1000                 SysLog(NID_UI_CTRL, "[E_INVALID_ARG] Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
1001                 return E_INVALID_ARG;
1002         }
1003
1004         _Token* pToken = new (std::nothrow) _Token();
1005         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1006
1007         r = pToken->Construct(inputTokenString, GetFont());
1008         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct token.", GetErrorMessage(r));
1009
1010         r = __pTokenList->Add(static_cast< Object& >(*pToken));
1011         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1012
1013         tokenCount = __pTokenList->GetCount();
1014         index = tokenCount - 1;
1015
1016         r = CalculateTokenPositionFromIndex(index);
1017         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to calculate token position.", GetErrorMessage(r));
1018
1019         r = TrimTokenAndAdjustEllipsisAt(index);
1020         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to trim token.", GetErrorMessage(r));
1021
1022         r = InitializeTokenVisibilityAt(index);
1023         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to initialize token visibility.", GetErrorMessage(r));
1024
1025         r = SetInitialBounds();
1026         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
1027
1028         r = ClearText();
1029         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to clear text object.", GetErrorMessage(r));
1030
1031         r = AdjustFlexibleHeight();
1032         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process to resize.", GetErrorMessage(r));
1033
1034         r = CheckTokenScrolling();
1035         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process scroll.", GetErrorMessage(r));
1036
1037         AppendTokenAccessibilityElement();
1038         UpdateTokenAccessibilityBounds();
1039
1040         return r;
1041
1042 CATCH:
1043         __pTokenList->Remove(*pToken);
1044
1045         delete pToken;
1046         pToken = null;
1047
1048         return r;
1049 }
1050
1051 result
1052 _TokenEditPresenter::MakeToken(void)
1053 {
1054         result r = E_SUCCESS;
1055
1056         String tempToken(GetText());
1057         r = MakeToken(tempToken);
1058
1059         return r;
1060 }
1061
1062 result
1063 _TokenEditPresenter::AppendToken(const Tizen::Base::String& token)
1064 {
1065         result r = E_SUCCESS;
1066
1067         r = MakeToken(token);
1068         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
1069
1070         if (__editingTokenIndex >= 0)
1071         {
1072                 SetEditingTokenTextBounds(__editingTokenIndex);
1073                 _EditPresenter::SetCursorPosition(__previousCursorPosition);
1074         }
1075
1076         return r;
1077 }
1078
1079 result
1080 _TokenEditPresenter::InsertTokenAt(int index, const String& token, bool isUser)
1081 {
1082         SysTryReturnResult(NID_UI_CTRL, index >= 0, E_INVALID_ARG, "Invalid argument is used. index is (%d).", index);
1083         SysTryReturnResult(NID_UI_CTRL, token.GetLength() > 0, E_INVALID_ARG, "Invalid argument is used. Token length is (%d).", token.GetLength());
1084
1085         result r = E_SUCCESS;
1086
1087         _Token* pToken = new (std::nothrow) _Token();
1088         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
1089
1090         r = pToken->Construct(token, GetFont());
1091         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to construct token.", GetErrorMessage(r));
1092
1093         r = __pTokenList->InsertAt(*pToken, index);
1094         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to insert token.", GetErrorMessage(r));
1095
1096         r = ClearText();
1097
1098         r = CalculateTokenPositionFromIndex(index);
1099         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to calculate token position.", GetErrorMessage(r));
1100
1101         r = TrimTokenAndAdjustEllipsisAt(index);
1102         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to trim token.", GetErrorMessage(r));
1103
1104         r = InitializeTokenVisibilityAt(index);
1105         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to initialize token visibility.", GetErrorMessage(r));
1106
1107         r = SetInitialBounds();
1108         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds.", GetErrorMessage(r));
1109
1110         r = AdjustFlexibleHeight();
1111         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process to resize.", GetErrorMessage(r));
1112
1113         r = CheckTokenScrolling();
1114         SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to process scroll.", GetErrorMessage(r));
1115
1116         for (int i = 0; i < GetTokenCount(); i++)
1117         {
1118                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
1119                 SysTryCatch(NID_UI_CTRL, pToken != null, , r = E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
1120
1121                 r = pToken->SetBounds(pToken->displayRect);
1122                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Failed to set bounds", GetErrorMessage(r));
1123         }
1124
1125         InsertTokenAccessibilityElementAt(index);
1126
1127         if (isUser)
1128         {
1129                 if (__editingTokenIndex >= 0)
1130                 {
1131                         if (index <= __editingTokenIndex)
1132                         {
1133                                 __editingTokenIndex++;
1134                         }
1135                         __pressedTokenIndex = __editingTokenIndex;
1136
1137                         SetEditingTokenTextBounds(__editingTokenIndex);
1138                         _EditPresenter::SetCursorPosition(__previousCursorPosition);
1139                 }
1140                 else if (__pressedTokenIndex >= index)
1141                 {
1142                         __pressedTokenIndex++;
1143                 }
1144                 else if ((__focusedTokenIndex >= index) && __drawFocusState)
1145                 {
1146                         __focusedTokenIndex++;
1147                 }
1148         }
1149
1150         return r;
1151
1152 CATCH:
1153         __pTokenList->Remove(*pToken);
1154
1155         delete pToken;
1156         pToken = null;
1157
1158         return r;
1159 }
1160
1161 String
1162 _TokenEditPresenter::GetTokenAt(int index) const
1163 {
1164         String tempString;
1165         SysTryReturn(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), tempString, E_OUT_OF_RANGE, "[E_OUT_OF_RANGE] index (%d) is out of range.", index);
1166
1167         _Token* pToken = null;
1168         pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
1169         if (pToken)
1170         {
1171                 tempString = pToken->GetText();
1172         }
1173
1174         return tempString;
1175 }
1176
1177 int
1178 _TokenEditPresenter::GetTokenCount(bool isInvokedByApp) const
1179 {
1180         if (!isInvokedByApp)
1181         {
1182                 return __pTokenList->GetCount();
1183         }
1184         else
1185         {
1186                 if (__isAnimationInProgress)
1187                 {
1188                         return __pTokenList->GetCount() - 1;
1189                 }
1190                 else
1191                 {
1192                         return __pTokenList->GetCount();
1193                 }
1194         }
1195 }
1196
1197 int
1198 _TokenEditPresenter::GetSelectedTokenIndex(void) const
1199 {
1200         return __pressedTokenIndex;
1201 }
1202
1203 bool
1204 _TokenEditPresenter::IsTokenEditModeEnabled(void) const
1205 {
1206         return __isEditModeEnabled;
1207 }
1208
1209 result
1210 _TokenEditPresenter::RemoveTokenAt(int index, bool isClearText)
1211 {
1212         result r = E_SUCCESS;
1213         SysTryReturnResult(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), E_OUT_OF_RANGE, "index (%d) is out of range.", index);
1214
1215         if (index == __editingTokenIndex && isClearText)
1216         {
1217                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
1218                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
1219
1220                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
1221                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
1222
1223                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
1224                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1225
1226                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
1227                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
1228
1229                 __editingTokenIndex = -1;
1230                 __isEditingToken = false;
1231                 __pressedTokenIndex = -1;
1232                 __isTokenEditingFinished = true;
1233
1234                 r = ClearText();
1235                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "Failed to clear text object.");
1236
1237                 _EditPresenter::SetTextSize(__editContentFontSize);
1238         }
1239
1240         r = __pTokenList->RemoveAt(index, true);
1241         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to remove token.");
1242
1243         r = CalculateTokenPositionFromIndex(index);
1244         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to calculate token position.");
1245
1246         r = SetInitialBounds();
1247         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
1248
1249         _Token* pToken = null;
1250
1251         for (int i = 0; i < GetTokenCount(); i++)
1252         {
1253                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
1254                 SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
1255
1256                 r = pToken->SetBounds(pToken->displayRect);
1257                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds");
1258         }
1259
1260         r = AdjustFlexibleHeight();
1261         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to process to resize.");
1262
1263         r = CheckTokenScrolling();
1264         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to process scroll.");
1265
1266         RemoveTokenAccessibilityElementAt(index);
1267         UpdateTokenAccessibilityBounds();
1268
1269         if (isClearText)
1270         {
1271                 if (index > __editingTokenIndex)
1272                 {
1273                         SetEditingTokenTextBounds(__editingTokenIndex);
1274                         _EditPresenter::SetCursorPosition(__previousCursorPosition);
1275                 }
1276                 if (index < __editingTokenIndex)
1277                 {
1278                         if (__editingTokenIndex > 0)
1279                         {
1280                                 __editingTokenIndex--;
1281                                 __pressedTokenIndex = __editingTokenIndex;
1282
1283                                 SetEditingTokenTextBounds(__editingTokenIndex);
1284                                 _EditPresenter::SetCursorPosition(__previousCursorPosition);
1285                         }
1286                 }
1287                 else if (index == __pressedTokenIndex)
1288                 {
1289                         __pressedTokenIndex = -1;
1290                         StopCursorTimer();
1291                         SetCursorDisabled(false);
1292                         StartCursorTimer();
1293                 }
1294                 else if (index >= 0 && index < __pressedTokenIndex)
1295                 {
1296                         __pressedTokenIndex--;
1297                 }
1298
1299                 if (__drawFocusState)
1300                 {
1301                         if (index == __focusedTokenIndex)
1302                         {
1303                                 __focusedTokenIndex = -1;
1304                                 StopCursorTimer();
1305                                 SetCursorDisabled(false);
1306                                 StartCursorTimer();
1307                         }
1308                         else if(index >= 0 && index < __focusedTokenIndex)
1309                         {
1310                                 __focusedTokenIndex--;
1311                         }
1312                 }
1313         }
1314         else if (index == __pressedTokenIndex)
1315         {
1316                 __pressedTokenIndex = -1;
1317                 StopCursorTimer();
1318                 SetCursorDisabled(false);
1319                 StartCursorTimer();
1320         }
1321         else if ((index == __focusedTokenIndex) && __drawFocusState)
1322         {
1323                 __focusedTokenIndex = -1;
1324                 StopCursorTimer();
1325                 SetCursorDisabled(false);
1326                 StartCursorTimer();
1327         }
1328
1329         return r;
1330 }
1331
1332 result
1333 _TokenEditPresenter::SetTokenSelected(int index, bool selected)
1334 {
1335         result r = E_SUCCESS;
1336
1337         SysTryReturnResult(NID_UI_CTRL, index >= 0 && index < __pTokenList->GetCount(), E_OUT_OF_RANGE, "index (%d) is out of range.");
1338
1339         if (selected == false)
1340         {
1341                 __pressedTokenIndex = -1;
1342         }
1343         else
1344         {
1345                 __pressedTokenIndex = index;
1346         }
1347
1348         return r;
1349 }
1350
1351 result
1352 _TokenEditPresenter::SetTokenEditModeEnabled(bool enable)
1353 {
1354         result r = E_SUCCESS;
1355
1356         __isEditModeEnabled = enable;
1357
1358         return r;
1359 }
1360
1361 result
1362 _TokenEditPresenter::CalculateTokenPositionFromIndex(int startIndex, bool leftWard)
1363 {
1364         result r = E_SUCCESS;
1365
1366         int tokenCount = __pTokenList->GetCount();
1367
1368         float leftMargin = 0.0f;
1369         float rightMargin = 0.0f;
1370         float tokenTopMargin = 0.0f;
1371         float tokenBottomMargin = 0.0f;
1372         float tokenHeight = 0.0f;
1373         float tokenVerticalSpacing = 0.0f;
1374         float tokenHorizontalSpacing = 0.0f;
1375         float tokenTextLeftMargin = 0.0f;
1376         float tokenTextRightMargin = 0.0f;
1377         float descriptionTextRightMargin = 0.0f;
1378         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1379
1380         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
1381         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
1382         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1383         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
1384         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1385         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1386         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HORIZONTAL_SPACING, orientation, tokenHorizontalSpacing);
1387         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1388         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1389         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_RIGHT_MARGIN, orientation, descriptionTextRightMargin);
1390
1391         SysTryReturn(NID_UI_CTRL, startIndex >= 0 && startIndex <= tokenCount, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] Invalid argument is used. startIndex = (%d)", startIndex);
1392
1393         _Token* pToken = null;
1394         _Token* pPreviousToken = null;
1395
1396         int index = startIndex;
1397
1398         FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
1399         String titleText = __pTokenEdit->GetTitleText();
1400
1401         if (!_FloatCompare(GetDescriptionTextRect().width, __previousTitleWidth))
1402         {
1403                 __descriptionTextRectForScroll = GetDescriptionTextRect();
1404                 __previousTitleWidth = GetDescriptionTextRect().width;
1405         }
1406
1407         bool findPrevTokenLoopFlag = true;
1408         for (; index < tokenCount; index++)
1409         {
1410                 pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
1411                 SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
1412
1413                 if (index == 0)
1414                 {
1415                         // TODO : description text (title style inner)
1416                         if (titleText.GetLength())
1417                         {
1418                                 pToken->displayRect.x = __descriptionTextRectForScroll.x + __descriptionTextRectForScroll.width + descriptionTextRightMargin;
1419                                 pToken->displayRect.y = __descriptionTextRectForScroll.y + __scrollValue;
1420                         }
1421                         else    // Set description text.
1422                         {
1423                                 pToken->displayRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1424                                 pToken->displayRect.y = tokenTopMargin + __scrollValue + __pTokenEdit->GetVerticalMarginF(EDIT_TEXT_TOP_MARGIN) + __lineSpacing;
1425
1426                         }
1427                         pToken->displayRect.width = tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin;
1428
1429                 }
1430                 else
1431                 {
1432                         if (findPrevTokenLoopFlag)
1433                         {
1434                                 pPreviousToken = static_cast< _Token* >(__pTokenList->GetAt(index - 1));
1435                                 findPrevTokenLoopFlag = false;
1436                         }
1437
1438                         float tempTextWidth = tokenEditBounds.width - pPreviousToken->displayRect.x - pPreviousToken->displayRect.width - tokenHorizontalSpacing - rightMargin - __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1439                         if (tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin > tempTextWidth)       // Line change
1440                         {
1441                                 pToken->displayRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1442                                 pToken->displayRect.y = pPreviousToken->displayRect.y + tokenHeight + tokenVerticalSpacing + __lineSpacing;
1443                                 __lineAdded++;
1444                         }
1445                         else
1446                         {
1447                                 pToken->displayRect.x = pPreviousToken->displayRect.x + pPreviousToken->displayRect.width +
1448                                                                                 tokenHorizontalSpacing;
1449                                 pToken->displayRect.y = pPreviousToken->displayRect.y;
1450                         }
1451
1452                         pToken->displayRect.width = tokenTextLeftMargin + pToken->GetTextPixelWidth() + tokenTextRightMargin;
1453                 }
1454
1455                 pToken->displayRect.height = tokenHeight;
1456
1457                 pPreviousToken = pToken;
1458         }
1459
1460         for (int i = 0; i < __pTokenList->GetCount(); i++)
1461         {
1462                 TrimTokenAndAdjustEllipsisAt(i);
1463                 InitializeTokenVisibilityAt(i);
1464         }
1465         return r;
1466 }
1467
1468 result
1469 _TokenEditPresenter::InitializeTokenVisibilityAt(int ndex)
1470 {
1471         result r = E_SUCCESS;
1472
1473         _Token* pToken = null;
1474         pToken = static_cast< _Token* >(__pTokenList->GetAt(ndex));
1475         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
1476
1477         r = pToken->SetBounds(pToken->displayRect);
1478         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to set bounds", GetErrorMessage(r));
1479
1480         _VisualElement* pRootElement = __pTokenEdit->GetVisualElement();
1481         SysTryReturn(NID_UI_CTRL, pRootElement, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
1482
1483         _VisualElement* pVisualElement = pToken->GetVisualElement();
1484         SysTryReturn(NID_UI_CTRL, pVisualElement, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get visual element.");
1485
1486         pVisualElement->SetShowState(true);
1487
1488         r = pRootElement->AttachChild(*pVisualElement);
1489         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, r, r, "[%s] Failed to add child", GetErrorMessage(r));
1490
1491         return r;
1492 }
1493
1494 float
1495 _TokenEditPresenter::GetMaxTextHeight(void)
1496 {
1497         Font* pFont = null;
1498         float maxHeight = __editContentFontSize;
1499
1500         pFont = GetFont();
1501         SysTryReturn(NID_UI_CTRL, pFont != null, maxHeight, E_SYSTEM, "[E_SYSTEM] Failed to get Font instance.");
1502
1503         maxHeight = pFont->GetMaxHeightF();
1504
1505         return maxHeight;
1506 }
1507
1508 void
1509 _TokenEditPresenter::ResetTextBounds(void)
1510 {
1511         if (!__isEditingToken)
1512         {
1513                 result r = SetInitialBounds();
1514                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error has occured. Failed to set margin.");
1515         }
1516         return;
1517 }
1518
1519 result
1520 _TokenEditPresenter::SetInitialBounds(void)
1521 {
1522         result r = E_SUCCESS;
1523
1524         float leftMargin = 0.0f;
1525         float rightMargin = 0.0f;
1526         float tokenTopMargin = 0.0f;
1527         float tokenHeight = 0.0f;
1528         float tokenMinWidth = 0.0f;
1529         float tokenVerticalSpacing = 0.0f;
1530         float tokenHorizontalSpacing = 0.0f;
1531         float tokenTextLeftMargin = 0.0f;
1532         float tokenTextRightMargin = 0.0f;
1533         float textBoundsAlignValue = 0.0f;
1534         float descriptionTextRightMargin = 0.0f;
1535         _ControlOrientation orientation = GetEditView()->GetOrientation();
1536
1537         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
1538         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
1539         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1540         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1541         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinWidth);
1542         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1543         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HORIZONTAL_SPACING, orientation, tokenHorizontalSpacing);
1544         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1545         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1546         GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_RIGHT_MARGIN, orientation, descriptionTextRightMargin);
1547
1548         float textObjectMaxHeight = GetMaxTextHeight();
1549         textBoundsAlignValue = (tokenHeight - textObjectMaxHeight) / 2.0f;
1550
1551         if (__pTokenList)
1552         {
1553                 int tokenCount = __pTokenList->GetCount();
1554
1555                 FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
1556                 FloatRectangle tokenTextRect = tokenEditBounds;
1557
1558                 if (tokenCount == 0)
1559                 {
1560                         if (__pTokenEdit->GetTitleText().GetLength())
1561                         {
1562                                 FloatRectangle descriptionTextRect = GetDescriptionTextRect();
1563                                 tokenTextRect.x = descriptionTextRect.x + descriptionTextRect.width + descriptionTextRightMargin;
1564                         }
1565                         else
1566                         {
1567                                 tokenTextRect.x = leftMargin + __pTokenEdit->GetHorizontalMargin(EDIT_TEXT_LEFT_MARGIN);
1568                         }
1569
1570                         tokenTextRect.y = tokenTopMargin + __pTokenEdit->GetVerticalMarginF(EDIT_TEXT_TOP_MARGIN) + textBoundsAlignValue;
1571                         tokenTextRect.width -= tokenTextRect.x + rightMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1572                         tokenTextRect.height = textObjectMaxHeight;
1573
1574                         SetTextBounds(tokenTextRect);
1575                         //set cursor bounds with tokenTextRect
1576                         __pTokenEdit->SetCursorAccessibilityBounds(tokenTextRect);
1577                         return r;
1578                 }
1579
1580                 _Token* pToken = null;
1581                 // SetTextBounds from last token
1582                 pToken = static_cast< _Token* >(__pTokenList->GetAt(tokenCount - 1));
1583                 SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
1584
1585                 float tempTextRectWidth = 0.0f;
1586                 tempTextRectWidth = tokenEditBounds.width - pToken->displayRect.x - pToken->displayRect.width - tokenHorizontalSpacing - rightMargin - __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1587                 if (tokenMinWidth > tempTextRectWidth)        // Line change
1588                 {
1589                         tokenTextRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1590                         tokenTextRect.y = pToken->displayRect.y + tokenHeight + tokenVerticalSpacing + __lineSpacing + textBoundsAlignValue;
1591                 }
1592                 else
1593                 {
1594                         tokenTextRect.x = pToken->displayRect.x + pToken->displayRect.width + tokenHorizontalSpacing;
1595                         tokenTextRect.y = pToken->displayRect.y + textBoundsAlignValue;
1596                 }
1597
1598                 tokenTextRect.width -= tokenTextRect.x + rightMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
1599                 tokenTextRect.height = textObjectMaxHeight;
1600
1601                 SetTextBounds(tokenTextRect);
1602                 if (__pressedTokenIndex < 0) // Set cursor as global focused element if no token is selected
1603                 {
1604                         __pTokenEdit->SetCursorAccessibilityBounds(tokenTextRect);
1605                 }
1606
1607         }
1608         else
1609         {
1610                 r = _EditPresenter::SetInitialBounds();
1611         }
1612
1613         return r;
1614 }
1615
1616 result
1617 _TokenEditPresenter::SetDescriptionTextRect(const FloatRectangle& rect)
1618 {
1619         result r = E_SUCCESS;
1620
1621         __descriptionTextRect = rect;
1622
1623         return r;
1624 }
1625
1626 FloatRectangle
1627 _TokenEditPresenter::GetDescriptionTextRect() const
1628 {
1629         return __descriptionTextRect;
1630 }
1631
1632 Rectangle
1633 _TokenEditPresenter::GetTextBounds(void) const
1634 {
1635         if ((__isPopupVisible == true || __isLongPressed == true) && __pressedTokenIndex >= 0)
1636         {
1637                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__pressedTokenIndex));
1638                 SysTryReturn(NID_UI_CTRL, pToken, Rectangle(), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Unable to get valid token.");
1639
1640                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1641                 float tokenTextLeftMargin = 0.0f;
1642                 float tokenTextVerticalMargin = 0.0f;
1643                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1644                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenTextVerticalMargin);
1645
1646                 FloatRectangle textBoundsF(pToken->displayRect.x + tokenTextLeftMargin, pToken->displayRect.y + (tokenTextVerticalMargin / 2.0f), pToken->displayRect.width - (tokenTextLeftMargin * 2.0f), pToken->displayRect.height - tokenTextVerticalMargin);
1647                 Rectangle textBounds = _CoordinateSystemUtils::ConvertToInteger(textBoundsF);
1648
1649                 return textBounds;
1650         }
1651         else
1652         {
1653                 return _EditPresenter::GetTextBounds();
1654         }
1655 }
1656
1657 FloatRectangle
1658 _TokenEditPresenter::GetTextBoundsF(void) const
1659 {
1660         if ((__isPopupVisible == true || __isLongPressed == true) && __pressedTokenIndex >= 0)
1661         {
1662                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__pressedTokenIndex));
1663                 SysTryReturn(NID_UI_CTRL, pToken, FloatRectangle(), E_SYSTEM, "[E_SYSTEM] A system error has occurred. Unable to get valid token.");
1664
1665                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1666                 int tokenTextLeftMargin = 0;
1667                 int tokenTextVerticalMargin = 0;
1668                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1669                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenTextVerticalMargin);
1670
1671                 FloatRectangle textBounds(pToken->displayRect.x + tokenTextLeftMargin, pToken->displayRect.y + (tokenTextVerticalMargin / 2.0f), pToken->displayRect.width - (tokenTextLeftMargin * 2.0f), pToken->displayRect.height - tokenTextVerticalMargin);
1672
1673                 return textBounds;
1674         }
1675         else
1676         {
1677                 return _EditPresenter::GetTextBoundsF();
1678         }
1679 }
1680
1681 result
1682 _TokenEditPresenter::CutText(void)
1683 {
1684         if (__isEditingToken)
1685         {
1686                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
1687                 SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
1688
1689                 pToken->isTextCut = true;
1690         }
1691         return _EditPresenter::CutText();
1692 }
1693
1694 result
1695 _TokenEditPresenter::SetLineSpacing(int linePixelGap)
1696 {
1697         __lineSpacing = linePixelGap;
1698
1699         return E_SUCCESS;
1700 }
1701
1702 result
1703 _TokenEditPresenter::SetLineSpacing(float linePixelGap)
1704 {
1705         __lineSpacing = linePixelGap;
1706
1707         return E_SUCCESS;
1708 }
1709
1710 int
1711 _TokenEditPresenter::GetLineSpacing(void) const
1712 {
1713         int lineSpacing = _CoordinateSystemUtils::ConvertToInteger(__lineSpacing);
1714         return lineSpacing;
1715 }
1716
1717 float
1718 _TokenEditPresenter::GetLineSpacingF(void) const
1719 {
1720         return __lineSpacing;
1721 }
1722
1723 VisualElementAnimation*
1724 _TokenEditPresenter::CreateAnimationN(VisualElement& source, bool create)
1725 {
1726         VisualElementAnimation* pAnimation = null;
1727         VisualElementAnimationGroup* pAnimationGroup = new (std::nothrow) VisualElementAnimationGroup();
1728         SysTryReturn(NID_UI_CTRL, pAnimationGroup, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1729
1730         pAnimationGroup->SetDuration(ANIMATION_DURATION_BOUNDS);
1731         if (__pTimingFunction == null)
1732         {
1733                 __pTimingFunction = new (std::nothrow) SineTimingFunction();
1734                 SysTryReturn(NID_UI_CTRL, __pTimingFunction, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1735         }
1736
1737         if (pAnimationGroup != null)
1738         {
1739                 VisualElementPropertyAnimation* pOpacityAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1740                 SysTryReturn(NID_UI_CTRL, pOpacityAnimation, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1741
1742                 pOpacityAnimation->SetPropertyName("opacity");
1743                 if (!create)
1744                 {
1745                         pOpacityAnimation->SetStartValue(Variant(1.0f));
1746                         pOpacityAnimation->SetEndValue(Variant(0.0f));
1747                 }
1748                 else
1749                 {
1750                         pOpacityAnimation->SetStartValue(Variant(0.0f));
1751                         pOpacityAnimation->SetEndValue(Variant(1.0f));
1752                 }
1753
1754                 pOpacityAnimation->SetDuration(ANIMATION_DURATION_OPACITY);
1755                 pOpacityAnimation->SetTimingFunction(__pTimingFunction);
1756                 pAnimationGroup->AddAnimation(*pOpacityAnimation);
1757                 delete pOpacityAnimation;
1758
1759                 VisualElementPropertyAnimation* pBoundsAnimation = new (std::nothrow) VisualElementPropertyAnimation();
1760                 SysTryReturn(NID_UI_CTRL, pBoundsAnimation, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1761
1762                 pBoundsAnimation->SetPropertyName("bounds");
1763                 FloatRectangle startValue = source.GetBounds();
1764                 startValue.x = startValue.x + startValue.width * 0.05;
1765                 startValue.y = startValue.y + startValue.height * 0.05;
1766                 startValue.width = startValue.width * 0.9;
1767                 startValue.height = startValue.height * 0.9;
1768
1769                 if (!create)
1770                 {
1771                         pBoundsAnimation->SetStartValue(Variant(source.GetBounds()));
1772                         pBoundsAnimation->SetEndValue(Variant(startValue));
1773                         pBoundsAnimation->SetVisualElementAnimationStatusEventListener(this);
1774                 }
1775                 else
1776                 {
1777                         pBoundsAnimation->SetStartValue(Variant(startValue));
1778                         pBoundsAnimation->SetEndValue(Variant(source.GetBounds()));
1779                 }
1780
1781                 pBoundsAnimation->SetDuration(ANIMATION_DURATION_BOUNDS);
1782                 pBoundsAnimation->SetTimingFunction(__pTimingFunction);
1783                 pAnimationGroup->AddAnimation(*pBoundsAnimation);
1784
1785                 delete pBoundsAnimation;
1786
1787                 pAnimation = pAnimationGroup;
1788         }
1789         return pAnimation;
1790 }
1791
1792 result
1793 _TokenEditPresenter::CalculateDescriptionTextRect(const String& descriptionText)
1794 {
1795         result r = E_SUCCESS;
1796
1797         TextSimple* pSimpleText = null;
1798         float leftMargin = 0.0f;
1799         float tokenTopMargin = 0.0f;
1800         float tokenHeight = 0.0f;
1801         float tokenVerticalSpacing = 0.0f;
1802         float tokenTextLeftMargin = 0.0f;
1803         float tokenTextRightMargin = 0.0f;
1804         float tokenTitleWidth = 0.0f;
1805         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
1806
1807         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
1808         GET_SHAPE_CONFIG(TOKENEDIT::LEFT_MARGIN, orientation, leftMargin);
1809         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
1810         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
1811         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
1812         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_RIGHT_MARGIN, orientation, tokenTextRightMargin);
1813         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TITLE_RECT_WIDTH, orientation, tokenTitleWidth);
1814
1815         int length = descriptionText.GetLength();
1816         FloatDimension textSize;
1817
1818         wchar_t* pTempString = const_cast< wchar_t* >(descriptionText.GetPointer());
1819
1820         SysAssertf(__pDescriptionTextTextObject != null, "The TextObject instance is null.");
1821
1822         __pDescriptionTextTextObject->RemoveAll(true);
1823         pSimpleText = new (std::nothrow) TextSimple(pTempString, length, TEXT_ELEMENT_SOURCE_TYPE_INTERNAL);
1824         __pDescriptionTextTextObject->AppendElement(*pSimpleText);
1825
1826         textSize = __pDescriptionTextTextObject->GetTextExtentF(0, length);
1827         r = __pDescriptionTextTextObject->SetAlignment(TEXT_OBJECT_ALIGNMENT_CENTER | TEXT_OBJECT_ALIGNMENT_MIDDLE);
1828         r = __pDescriptionTextTextObject->SetWrap(TEXT_OBJECT_WRAP_TYPE_NONE);
1829
1830         __descriptionTextRect.x = leftMargin + __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_LEFT_MARGIN);
1831         __descriptionTextRect.y = tokenTopMargin + __pTokenEdit->GetVerticalMarginF(EDIT_TEXT_TOP_MARGIN);
1832         if (textSize.width > tokenTitleWidth)
1833         {
1834                 textSize.width = tokenTitleWidth;
1835                 __pDescriptionTextTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
1836         }
1837
1838         __descriptionTextRect.width = tokenTextLeftMargin + textSize.width + tokenTextRightMargin;
1839         __descriptionTextRect.height = tokenHeight;
1840         __pDescriptionTextTextObject->SetBounds(__descriptionTextRect);
1841
1842         if (__pDescriptionTextTextObject->IsChanged())
1843         {
1844                 _EditPresenter::StopTitleSlidingTimer();
1845                 __isTitleSliding = false;
1846         }
1847
1848         r = __pDescriptionTextTextObject->Compose();
1849
1850         return r;
1851 }
1852
1853 bool
1854 _TokenEditPresenter::IsGuideTextActivated(void) const
1855 {
1856         bool isGuideTextActivated = _EditPresenter::IsGuideTextActivated();
1857
1858         if (__isTokenEditPresenterInitialized)
1859         {
1860                 if (GetTokenCount())
1861                 {
1862                         return false;
1863                 }
1864         }
1865
1866         return isGuideTextActivated;
1867 }
1868
1869 bool
1870 _TokenEditPresenter::DrawDescriptionText(void)
1871 {
1872         result r = E_SUCCESS;
1873         FloatRectangle tempDescriptionTextRect;
1874         FloatRectangle descriptionTextRect(__descriptionTextRect);
1875         Canvas* pDescriptionTextCanvas = null;
1876
1877         Font* pDescriptionFont = null;
1878         TextObjectActionType titleAction;
1879         _VisualElement* pRootElement = null;
1880
1881         if (__pDescriptionTextTextObject->GetFont(0)->GetFaceName() != GetTitleFontFaceName())
1882         {
1883                 float descriptionTextSize = 0.0f;
1884                 GET_SHAPE_CONFIG(TOKENEDIT::DESCRIPTION_TEXT_SIZE, _CONTROL_ORIENTATION_PORTRAIT, descriptionTextSize);
1885
1886                 pDescriptionFont = GetFont();
1887                 if (pDescriptionFont)
1888                 {
1889                         float editFontSize = GetTextSize();
1890                         (_FontImpl::GetInstance(*pDescriptionFont))->SetSize(descriptionTextSize);
1891
1892                         r = __pDescriptionTextTextObject->SetFont(pDescriptionFont, 0, __pDescriptionTextTextObject->GetTextLength());
1893                         SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. SetFont failed");
1894
1895                         (_FontImpl::GetInstance(*pDescriptionFont))->SetSize(editFontSize);
1896                 }
1897         }
1898
1899         pRootElement = __pTokenEdit->GetVisualElement();
1900         SysTryCatch(NID_UI_CTRL, pRootElement, , E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
1901
1902         if (!__pDescriptionTextVisualElement)
1903         {
1904                 __pDescriptionTextVisualElement = new (std::nothrow) _VisualElement();
1905                 SysTryCatch(NID_UI_CTRL, __pDescriptionTextVisualElement, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
1906                 r = __pDescriptionTextVisualElement->Construct();
1907                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[E_SYSTEM] A system error has occurred. Failed to construct _VisualElement");
1908                 __pDescriptionTextVisualElement->SetImplicitAnimationEnabled(false);
1909                 __pDescriptionTextVisualElement->SetShowState(true);
1910
1911                 r = pRootElement->AttachChild(*__pDescriptionTextVisualElement);
1912                 SysTryCatch(NID_UI_CTRL, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
1913         }
1914
1915         descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
1916         __pDescriptionTextVisualElement->SetBounds(descriptionTextRect);
1917         UpdateTitleAccessibilityBounds(descriptionTextRect); // Update title accessibility bounds to same as DescriptionTextVisualElement bounds
1918
1919         pDescriptionTextCanvas = __pDescriptionTextVisualElement->GetCanvasN();
1920         if (pDescriptionTextCanvas == null)
1921         {
1922                 SysLog(NID_UI_CTRL, "[E_SYSTEM] A system error has occurred. Failed to get canvas of an instance of _VisualElement.");
1923                 return true;
1924         }
1925
1926         tempDescriptionTextRect = __descriptionTextRect;
1927         tempDescriptionTextRect.x = 0.0f;
1928         tempDescriptionTextRect.y = 0.0f;
1929
1930         titleAction = __pDescriptionTextTextObject->GetAction();
1931
1932         pDescriptionTextCanvas->SetBackgroundColor(Color(0));
1933         pDescriptionTextCanvas->Clear();
1934         __pDescriptionTextTextObject->SetForegroundColor(__pTokenEdit->GetTitleTextColor(GetCurrentStatus()), 0, __pDescriptionTextTextObject->GetTextLength());
1935
1936         if (IsFocused() == true)
1937         {
1938                 if (__pDescriptionTextTextObject->GetTextLengthAt(0) < __pDescriptionTextTextObject->GetTextLength())
1939                 {
1940                         if (titleAction != TEXT_OBJECT_ACTION_TYPE_SLIDE_LEFT)
1941                         {
1942                                 __pDescriptionTextTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_SLIDE_LEFT);
1943                                 __pDescriptionTextTextObject->Compose();
1944                         }
1945                 }
1946                 else
1947                 {
1948                         if (titleAction != TEXT_OBJECT_ACTION_TYPE_NONE)
1949                         {
1950                                 __pDescriptionTextTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
1951                                 __pDescriptionTextTextObject->Compose();
1952                         }
1953                 }
1954
1955                 if (!__isTitleSliding)
1956                 {
1957                         _EditPresenter::StopTitleSlidingTimer();
1958                         if (__pDescriptionTextTextObject->IsActionOn() == true)
1959                         {
1960                                 _EditPresenter::StartTitleSlidingTimer();
1961                                 __isTitleSliding = true;
1962                         }
1963                 }
1964         }
1965         else
1966         {
1967                 if (titleAction != TEXT_OBJECT_ACTION_TYPE_ABBREV)
1968                 {
1969                         __pDescriptionTextTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
1970                         __pDescriptionTextTextObject->Compose();
1971                 }
1972         }
1973
1974         // Draw title text
1975         __pDescriptionTextTextObject->SetBounds(tempDescriptionTextRect);
1976         __pDescriptionTextTextObject->Draw(*_CanvasImpl::GetInstance(*pDescriptionTextCanvas));
1977
1978         delete pDescriptionTextCanvas;
1979
1980         return true;
1981
1982 CATCH:
1983         if (__pDescriptionTextVisualElement != null)
1984         {
1985                 __pDescriptionTextVisualElement->Destroy();
1986                 __pDescriptionTextVisualElement = null;
1987         }
1988
1989         return false;
1990 }
1991
1992 result
1993 _TokenEditPresenter::TrimTokenAndAdjustEllipsisAt(int index)
1994 {
1995         result r = E_SUCCESS;
1996
1997         float rightMargin = 0.0f;
1998         float tokenMinimumSize = 0.0f;
1999
2000         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2001         GET_SHAPE_CONFIG(TOKENEDIT::RIGHT_MARGIN, orientation, rightMargin);
2002         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_MIN_WIDTH, orientation, tokenMinimumSize);
2003
2004         _Token* pToken = null;
2005         pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
2006         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
2007
2008         FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
2009         FloatRectangle tokenRect = pToken->displayRect;
2010
2011         float remainWidth = tokenEditBounds.width - rightMargin - __pTokenEdit->GetHorizontalMarginF(EDIT_TEXT_RIGHT_MARGIN);
2012
2013         float dspTokenWidth = tokenRect.x + tokenRect.width;
2014         float dspTokenGap = dspTokenWidth - remainWidth;
2015
2016         if (dspTokenGap > 0.0f)
2017         {
2018                 if (pToken->displayRect.width >= tokenMinimumSize)
2019                 {
2020                         if ((pToken->displayRect.width - dspTokenGap) < tokenMinimumSize)
2021                         {
2022                                 pToken->displayRect.width = tokenMinimumSize;
2023                         }
2024                         else
2025                         {
2026                                 pToken->displayRect.width -= dspTokenGap;
2027                         }
2028                 }
2029
2030                 // Adjust ellipsis
2031                 pToken->pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
2032                 pToken->pTextObject->SetTextObjectEllipsisType(TEXT_OBJECT_ELLIPSIS_TYPE_TAIL);
2033         }
2034
2035         return r;
2036 }
2037
2038 int
2039 _TokenEditPresenter::GetTokenIndexFromCoordinate(const Point point) const
2040 {
2041         int tokenIndex = -1;
2042
2043         int tokenCount = __pTokenList->GetCount();
2044         for (int i = 0; i < tokenCount; i++)
2045         {
2046                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2047                 if (pToken)
2048                 {
2049                         FloatRectangle tokenRect = pToken->displayRect;
2050                         if (tokenRect.Contains(FloatPoint(point.x, point.y)))
2051                         {
2052                                 tokenIndex = i;
2053                                 break;
2054                         }
2055                 }
2056         }
2057         return tokenIndex;
2058 }
2059
2060 result
2061 _TokenEditPresenter::SetEditingTokenTextBounds(int index, bool isSetText)
2062 {
2063         result r = E_SUCCESS;
2064
2065         float tokenHeight = 0.0f;
2066         float tokenVerticalSpacing = 0.0f;
2067         float tokenTextLeftMargin = 0.0f;
2068         float tokenFontSize = 0.0f;
2069         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2070
2071         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
2072         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
2073         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
2074         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_SIZE, orientation, tokenFontSize);
2075
2076         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
2077         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null");
2078
2079         FloatRectangle tempTextDspRect;
2080         tempTextDspRect.x += tokenTextLeftMargin;
2081         tempTextDspRect.y += tokenVerticalSpacing / 2.0f;
2082         tempTextDspRect.width = pToken->displayRect.width - (tokenTextLeftMargin * 2.0f);
2083         tempTextDspRect.height = tokenHeight - tokenVerticalSpacing;
2084
2085         if (isSetText)
2086         {
2087                 if (!pToken->isTextCut)
2088                 {
2089                         SetText(pToken->GetText());
2090                 }
2091                 pToken->pTextObject->RemoveAll(true);
2092         }
2093
2094         _EditPresenter::SetTextSize(tokenFontSize);
2095
2096         SetTextBounds(tempTextDspRect);
2097
2098         return r;
2099 }
2100
2101 result
2102 _TokenEditPresenter::RecalculateTokenBounds(float position)
2103 {
2104         result r = E_SUCCESS;
2105
2106         __scrollValue = -position;
2107
2108         int tokenCount = GetTokenCount();
2109         CalculateTokenPositionFromIndex(0);
2110
2111         for (int i = 0; i < tokenCount; i++)
2112         {
2113                 _Token* pToken = null;
2114                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2115
2116                 if (pToken)
2117                 {
2118                         pToken->SetBounds(pToken->displayRect);
2119                 }
2120                 TrimTokenAndAdjustEllipsisAt(i);
2121                 InitializeTokenVisibilityAt(i);
2122         }
2123
2124         if (__pDescriptionTextTextObject->GetTextLength() != 0)
2125         {
2126                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2127         }
2128
2129         __pTokenEdit->Invalidate();
2130
2131         r = SetInitialBounds();
2132         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
2133
2134         return r;
2135 }
2136
2137 result
2138 _TokenEditPresenter::SetTokenBoundsByTouchInfo(const _TouchInfo& touchinfo)
2139 {
2140         result r = E_SUCCESS;
2141         int currentYPosition = _CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()).y;
2142
2143         if (_FloatCompare(__prevScrollValue, 0.0f))
2144         {
2145                 __prevScrollValue = currentYPosition;
2146         }
2147         else    // Adjust moved y position to all tokens.
2148         {
2149                 if (__isNeedToScroll)       // Need to scroll
2150                 {
2151                         float tempDefference = __prevScrollValue - currentYPosition;
2152
2153                         __prevScrollValue = currentYPosition;
2154                         __scrollValue -= tempDefference;
2155
2156                         if (__scrollValue < -__maxScrollValue)
2157                         {
2158                                 __scrollValue = -__maxScrollValue;
2159
2160                                 return E_SUCCESS;
2161                         }
2162
2163                         if (__scrollValue > 0.0f)
2164                         {
2165                                 __scrollValue = 0.0f;
2166
2167                                 return E_SUCCESS;
2168                         }
2169
2170                         int tokenCount = GetTokenCount();
2171                         CalculateTokenPositionFromIndex(0);
2172                         for (int i = 0; i < tokenCount; i++)
2173                         {
2174                                 _Token* pToken = null;
2175                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2176
2177                                 if (pToken)
2178                                 {
2179                                         pToken->SetBounds(pToken->displayRect);
2180                                 }
2181                                 TrimTokenAndAdjustEllipsisAt(i);
2182                                 InitializeTokenVisibilityAt(i);
2183                         }
2184
2185                         if (__pDescriptionTextTextObject->GetTextLength() != 0)
2186                         {
2187                                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2188                         }
2189
2190                         __pTokenEdit->Invalidate();
2191                 }
2192                 else
2193                 {
2194                         __prevScrollValue = 0.0f;
2195                         __scrollValue = 0.0f;
2196                 }
2197         }
2198
2199         r = SetInitialBounds();
2200         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Failed to set bounds.");
2201
2202         return r;
2203 }
2204
2205 result
2206 _TokenEditPresenter::ProcessTokeningByTouchEvent(const _Control& source, const _TouchInfo& touchinfo)
2207 {
2208         int tokenIndex = GetTokenIndexFromCoordinate(_CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()));
2209
2210         //Reset "longPressed" when Touch released on a different token after long gesture on a token
2211         if (__trackTokenIndex != tokenIndex)
2212         {
2213                 __isLongPressed = false;
2214         }
2215
2216         int prevPressedTokenIndex = __pressedTokenIndex;
2217         int prevEditedTokenIndex = __editingTokenIndex;
2218
2219         result r = E_SUCCESS;
2220         if (IsFocused())
2221         {
2222                 if (__isEditModeEnabled && __pressedTokenIndex != -1 && __pressedTokenIndex == tokenIndex)
2223                 {
2224                         //Comment below to Block Copy & Paste functionality in Token Edit mode
2225                         __editingTokenIndex = __pressedTokenIndex;
2226                         __isEditingToken = true;
2227                         __isTokenEditingFinished = false;
2228                         if (prevEditedTokenIndex != __editingTokenIndex)
2229                         {
2230                                 SetEditingTokenTextBounds(__editingTokenIndex);
2231                         }
2232                         SetCursorDisabled(false);
2233                 }
2234         }
2235
2236         if (tokenIndex != -1)
2237         {
2238                 if (__isEditingToken && (prevPressedTokenIndex != tokenIndex))   // Selected another token while editing.
2239                 {
2240                         __isPopupVisible = false;
2241                         __isLongPressed = false;
2242
2243                         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2244                         SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
2245
2246                         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2247                         SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
2248
2249                         _Token* pToken = null;
2250                         _VisualElement* pTokenVisualElement = null;
2251
2252                         pToken = static_cast< _Token* >(__pTokenList->GetAt(prevPressedTokenIndex));
2253
2254                         bool isParentChanged = false;
2255                         if (pToken)
2256                         {
2257                                 pTokenVisualElement = pToken->GetVisualElement();
2258                                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
2259
2260                                 if (pCursorVisualElement->GetParent() == pTokenVisualElement)
2261                                 {
2262                                         isParentChanged = true;
2263                                         result r = E_SUCCESS;
2264                                         r = pTokenVisualElement->DetachChild(*pCursorVisualElement);
2265                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2266
2267                                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2268                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2269                                 }
2270                         }
2271
2272                         String inputTokenString = GetText();
2273                         String replacementString = inputTokenString;
2274                         bool enable = false;
2275
2276                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
2277                         if (enable)
2278                         {
2279                                 inputTokenString = replacementString;
2280                         }
2281
2282                         __pressedTokenIndex = tokenIndex;
2283
2284                         r = RemoveTokenAt(prevPressedTokenIndex);
2285                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2286
2287                         if (inputTokenString.GetLength() > 0)
2288                         {
2289                                 r = InsertTokenAt(prevPressedTokenIndex, inputTokenString);
2290                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2291
2292                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(prevPressedTokenIndex));
2293                                 if (pToken)
2294                                 {
2295                                         pToken->currTokenLength = inputTokenString.GetLength();
2296                                 }
2297                         }
2298
2299                         ClearText();
2300                         //Flex height adjusted since token can move to another line
2301                         AdjustFlexibleHeight();
2302                         __editingTokenIndex = -1;
2303                         __isTokenEditingFinished = true;
2304                         __isEditingToken = false;
2305                         _EditPresenter::SetTextSize(__editContentFontSize);
2306
2307                         SetCursorDisabled(true);
2308                 }
2309                 else
2310                 {
2311                         __pressedTokenIndex = tokenIndex;
2312                         if (GetText().GetLength() > 0 && __pressedTokenIndex != prevPressedTokenIndex)
2313                         {
2314                                 r = MakeToken();
2315                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2316                         }
2317
2318                         if ((__isEditingToken == true) && (__pressedTokenIndex != -1))
2319                         {
2320                                 r = AttachCursorToToken();
2321                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2322                         }
2323
2324                         if (__isEditingToken == false)
2325                         {
2326                                 SetCursorDisabled(true);
2327                         }
2328                 }
2329         }
2330         else
2331         {
2332                 __isPopupVisible = false;
2333                 __isLongPressed = false;
2334
2335                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
2336                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
2337
2338                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
2339                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
2340
2341                 if (__isEditingToken)
2342                 {
2343                         _Token* pToken = null;
2344                         _VisualElement* pTokenVisualElement = null;
2345
2346                         pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
2347
2348                         bool isParentChanged = false;
2349                         if (pToken)
2350                         {
2351                                 pTokenVisualElement = pToken->GetVisualElement();
2352                                 SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
2353
2354                                 if (pCursorVisualElement->GetParent() == pTokenVisualElement)
2355                                 {
2356                                         isParentChanged = true;
2357                                         result r = E_SUCCESS;
2358                                         r = pTokenVisualElement->DetachChild(*pCursorVisualElement);
2359                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2360
2361                                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2362                                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2363                                 }
2364                         }
2365
2366                         String inputTokenString = GetText();
2367                         String replacementString = inputTokenString;
2368                         bool enable = false;
2369
2370                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
2371                         if (enable)
2372                         {
2373                                 inputTokenString = replacementString;
2374                         }
2375                         __pressedTokenIndex = tokenIndex;
2376
2377                         RemoveTokenAt(__editingTokenIndex);
2378
2379                         if (inputTokenString.GetLength() > 0)
2380                         {
2381                                 InsertTokenAt(__editingTokenIndex, inputTokenString);
2382
2383                                 if (isParentChanged)
2384                                 {
2385                                         pToken->currTokenLength = inputTokenString.GetLength();
2386                                 }
2387                         }
2388
2389                         __isEditingToken = false;
2390                         __editingTokenIndex = -1;
2391                         _EditPresenter::SetTextSize(__editContentFontSize);
2392                         __isTokenEditingFinished = false;
2393
2394                         ClearText();
2395
2396                         //Flex height adjusted since token can move to another line
2397                         AdjustFlexibleHeight();
2398
2399                         SysTryReturnResult(NID_UI_CTRL, (inputTokenString.GetLength() > 0), E_INVALID_ARG, "Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
2400
2401                         DrawText();
2402                 }
2403                 else
2404                 {
2405                         __pressedTokenIndex = tokenIndex;
2406                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
2407                         {
2408                                 r = (pCursorVisualElement->GetParent())->DetachChild(*pCursorVisualElement);
2409                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2410
2411                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
2412                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2413                         }
2414                 }
2415
2416                 SetCursorDisabled(false);
2417         }
2418
2419         for (int i = 0; i < __pTokenList->GetCount(); i++)
2420         {
2421                 TrimTokenAndAdjustEllipsisAt(i);
2422                 InitializeTokenVisibilityAt(i);
2423         }
2424
2425         return E_SUCCESS;
2426 }
2427
2428 result
2429 _TokenEditPresenter::CheckTokenScrolling(bool scrollToCursorPosition)
2430 {
2431         bool needToScroll = false;
2432         float tokenTopMargin = 0.0f;
2433         float tokenBottomMargin = 0.0f;
2434         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2435
2436         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
2437         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
2438
2439         int tokenCount = GetTokenCount();
2440         if (tokenCount == 0)        // There is no token to scroll
2441         {
2442                 __isNeedToScroll = false;
2443                 __maxScrollValue = 0.0f;
2444                 __isTokenScrolling = false;
2445
2446                 return E_SUCCESS;
2447         }
2448
2449         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(tokenCount - 1));
2450         SysTryReturn(NID_UI_CTRL, pToken, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2451
2452         float newScrollValue = 0.0f;
2453
2454         if (scrollToCursorPosition)
2455         {
2456                 FloatRectangle cursorBounds;
2457                 GetCursorBounds(false, cursorBounds);
2458                 newScrollValue = cursorBounds.y + cursorBounds.height - __scrollValue + tokenBottomMargin - __pTokenEdit->GetBoundsF().height;
2459                 __isScrollValueModified = true;
2460         }
2461         else
2462         {
2463                 newScrollValue = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin - __pTokenEdit->GetBoundsF().height;
2464                 __isScrollValueModified = true;
2465         }
2466
2467         needToScroll = (newScrollValue > 0.0f) ? (true) : (false);
2468
2469         __isNeedToScroll = needToScroll;
2470
2471         if (__isNeedToScroll)
2472         {
2473                 __maxScrollValue = newScrollValue;
2474                 __isTokenScrolling = true;
2475
2476                 RecalculateTokenBounds(__maxScrollValue);
2477         }
2478         else
2479         {
2480                 if (!_FloatCompare(__scrollValue, 0.0f))
2481                 {
2482                         __scrollValue = 0.0f;
2483                         __maxScrollValue = 0.0f;        // To prevent unnecessary token scrolling.
2484                         RecalculateTokenBounds(__scrollValue);
2485                         __isTokenScrolling = false;
2486                 }
2487         }
2488
2489         return E_SUCCESS;
2490 }
2491
2492 result
2493 _TokenEditPresenter::SetTokenVisualElementBounds(int index, const FloatRectangle& bounds)
2494 {
2495         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
2496         SysTryReturnResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "A system error has occurred. The _Token instance is null.");
2497
2498         return pToken->SetBounds(pToken->displayRect);
2499 }
2500
2501 result
2502 _TokenEditPresenter::AdjustFlexibleHeight(void)
2503 {
2504         result r = E_SUCCESS;
2505
2506         FloatRectangle editRect = __pTokenEdit->GetBoundsF();
2507         FloatRectangle displayScrollBounds = GetDisplayScrollBoundsF();
2508         float calcHeight = CalculateFlexibleHeightF();
2509
2510         if (!_FloatCompare(editRect.height, calcHeight))
2511         {
2512                 displayScrollBounds.height = calcHeight;
2513                 SetScrollBarBounds(displayScrollBounds);
2514
2515                 editRect.height = calcHeight;
2516                 if (!__isEditingToken)
2517                 {
2518                         FloatRectangle editRectBounds = CoordinateSystem::AlignToDevice(editRect);
2519                         r = SetFlexBounds(editRectBounds);
2520                 }
2521         }
2522
2523         return r;
2524 }
2525
2526 float
2527 _TokenEditPresenter::CalculateFlexibleHeightF(void)
2528 {
2529         float tokenHeight = 0.0f;
2530         float tokenVerticalSpacing = 0.0f;
2531         float tokenTopMargin = 0.0f;
2532         float tokenBottomMargin = 0.0f;
2533         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
2534
2535         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
2536         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
2537         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
2538         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
2539
2540         float height = 0.0f;
2541         int maxLinecount = GetMaxLineCount();
2542         float expectedEditHeight = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin;
2543         float initialHeight = GetInitialBoundsF().height;
2544         float maximumLineHeight = tokenTopMargin + maxLinecount * tokenHeight + tokenVerticalSpacing * (maxLinecount - 1) + tokenBottomMargin;
2545
2546         if (initialHeight < expectedEditHeight)
2547         {
2548                 if (expectedEditHeight > maximumLineHeight)
2549                 {
2550                         height = maximumLineHeight;
2551                 }
2552                 else
2553                 {
2554                         height = expectedEditHeight;
2555                 }
2556         }
2557         else
2558         {
2559                 height = initialHeight;
2560         }
2561
2562         return height;
2563 }
2564
2565 result
2566 _TokenEditPresenter::DrawScrollBar(void)
2567 {
2568         result r = E_SUCCESS;
2569
2570         _Scroll* pScroll = GetScrollBar();
2571         if (pScroll == null)
2572         {
2573                 return E_SYSTEM;
2574         }
2575
2576         if (IsFocused() == false)
2577         {
2578                 if (__isTouchMoveInProgress == false)
2579                 {
2580                         pScroll->SetScrollVisibility(false);
2581                         return E_SUCCESS;
2582                 }
2583         }
2584
2585         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(GetTokenCount() - 1));
2586         if (pToken == null)
2587         {
2588                 return E_SUCCESS;
2589         }
2590
2591         float tokenBottomMargin = 0.0f;
2592         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, tokenBottomMargin);
2593
2594         float totalHeight = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin;
2595         float dspHeight = __pTokenEdit->GetBoundsF().height;
2596         float firstDspY = -__scrollValue;
2597
2598         if (totalHeight <= dspHeight)
2599         {
2600                 pScroll->SetScrollVisibility(false);
2601                 pScroll->SetScrollRange(1, 1);
2602                 pScroll->SetScrollPosition(0.0f);
2603
2604                 SetScrollBarVisible(false);
2605                 SetPreviousScrollBarPosition(0.0f);
2606                 SetMaximumPreviousScrollBarPosition(0.0f);
2607         }
2608         else
2609         {
2610                 if (firstDspY > 0.0f)
2611                 {
2612                         pScroll->SetScrollRange(dspHeight, totalHeight);
2613                         pScroll->SetScrollPosition(firstDspY);
2614                         SetMaximumPreviousScrollBarPosition(totalHeight - dspHeight);
2615                 }
2616                 else
2617                 {
2618                         pScroll->SetScrollRange(dspHeight, totalHeight);
2619                         pScroll->SetScrollPosition(0.0f);
2620                         SetMaximumPreviousScrollBarPosition(0.0f);
2621                 }
2622
2623                 if (pScroll->GetScrollVisibility())
2624                 {
2625                         SetScrollBarVisible(true);
2626                 }
2627
2628                 SetPreviousScrollBarPosition(firstDspY);
2629         }
2630
2631         return r;
2632 }
2633
2634 void
2635 _TokenEditPresenter::SetAutoShrinkModeEnabled(bool enable)
2636 {
2637         __autoShrink = enable;
2638 }
2639
2640 bool
2641 _TokenEditPresenter::IsAutoShrinkModeEnabled(void) const
2642 {
2643         return __autoShrink;
2644 }
2645
2646 void
2647 _TokenEditPresenter::SetDescriptionText(String descriptionText)
2648 {
2649         __descriptionText = descriptionText;
2650 }
2651
2652 String
2653 _TokenEditPresenter::GetDescriptionText(void) const
2654 {
2655         return __descriptionText;
2656 }
2657
2658 int
2659 _TokenEditPresenter::CalculateVisibleTokenCount(void)
2660 {
2661         int visibleTokenCount = 0;
2662
2663         FloatRectangle intialBounds = GetInitialBoundsF();
2664         FloatRectangle tempInitialBounds = intialBounds;
2665         FloatRectangle hiddenTokenDisplayBounds;
2666         int tokenCount = GetTokenCount();
2667
2668         for (int i = 0; i < tokenCount; i++)
2669         {
2670                 tempInitialBounds = intialBounds;
2671                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2672                 SysTryReturn(NID_UI_CTRL, pToken != null, -1, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
2673
2674                 FloatRectangle displayBounds = pToken->displayRect;
2675                 if ((tempInitialBounds.y + displayBounds.y) > (tempInitialBounds.y + tempInitialBounds.height))
2676                 {
2677                         break;
2678                 }
2679                 visibleTokenCount++;
2680         }
2681         return visibleTokenCount;
2682 }
2683
2684 bool
2685 _TokenEditPresenter::OnFocusGained(void)
2686 {
2687         __isTitleSliding = false;
2688         if (__autoShrink)
2689         {
2690                 float tempHeight = CalculateFlexibleHeightF();
2691                 FloatRectangle tempRect = GetInitialBoundsF();
2692                 tempRect.height = tempHeight;
2693
2694                 _Token* pToken = null;
2695                 int tokenCount = GetTokenCount();
2696
2697                 for (int i = 0; i < tokenCount; i++)
2698                 {
2699                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2700
2701                         if (pToken)
2702                         {
2703                                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
2704                                 if (pTokenVisualElement)
2705                                 {
2706                                         pTokenVisualElement->SetShowState(true);
2707                                 }
2708                         }
2709                 }
2710
2711                 SetFlexBounds(tempRect);
2712
2713                 SetEditingTokenTextBounds(__editingTokenIndex, false);
2714
2715                 if (!__isFocus)
2716                 {
2717                         __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2718                         __isFocus = true;
2719                 }
2720
2721                 if (__isScrollValueChanged && !__isScrollValueModified)
2722                 {
2723                         __maxScrollValue = __maxScrollValue - (__pTokenEdit->GetBoundsF().height - GetInitialBoundsF().height);
2724                         __isScrollValueChanged = false;
2725                 }
2726         }
2727
2728         if (GetTokenCount())
2729         {
2730                 CheckTokenScrolling();
2731         }
2732
2733         TextObject* pTextObject = GetTextObject();
2734         if (pTextObject)
2735         {
2736                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_NONE);
2737                 pTextObject->Compose();
2738         }
2739
2740         if (__pressedTokenIndex < 0)
2741         {
2742                 StopCursorTimer();
2743                 SetCursorDisabled(false);
2744                 StartCursorTimer();
2745         }
2746
2747         RefreshAccessibilityElements();
2748
2749         return _EditPresenter::OnFocusGained();
2750 }
2751
2752 bool
2753 _TokenEditPresenter::OnFocusLost(void)
2754 {
2755         result r = E_SUCCESS;
2756         __isFocus = false;
2757
2758         //Remove pressed state on focus lost
2759         __pressedTokenIndex = -1;
2760
2761         _EditPresenter::StopTitleSlidingTimer();
2762         __isTitleSliding = false;
2763
2764         if (__editingTokenIndex >= 0)
2765         {
2766                 _Token* pToken = null;
2767                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
2768                 if (pToken)
2769                 {
2770                         if (GetText().GetLength() > 0)
2771                         {
2772                                 OnTextCommitted(L"\n");
2773                         }
2774                         else
2775                         {
2776                                 RemoveTokenAt(__editingTokenIndex, true);
2777                         }
2778                 }
2779         }
2780         else
2781         {
2782                 if (GetText().GetLength() > 0)
2783                 {
2784                         OnTextCommitted(L"\n");
2785                 }
2786         }
2787
2788         if (__autoShrink)
2789         {
2790                 _Scroll* pScroll = GetScrollBar();
2791                 if (pScroll)
2792                 {
2793                         pScroll->SetScrollVisibility(false);
2794                 }
2795                 __scrollValue = 0.0f;
2796
2797                 int tokenCount = GetTokenCount();
2798                 _Token* pToken = null;
2799                 for (int i = 0; i < tokenCount; i++)
2800                 {
2801                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2802                         if (pToken)
2803                         {
2804                                 pToken->SetBounds(pToken->displayRect);
2805                         }
2806                 }
2807
2808                 r = SetInitialBounds();
2809                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating", GetErrorMessage(r));
2810
2811                 UpdateTokenAccessibilityBounds();
2812
2813                 __scrollValue = 0.0f;
2814                 r = CalculateTokenPositionFromIndex(0);
2815                 SysTryReturn(NID_UI_CTRL, r == E_SUCCESS, false, r, "[%s] Propagating.", GetErrorMessage(r));
2816
2817                 int visibleTokenCount = CalculateVisibleTokenCount();
2818
2819                 for (int i = visibleTokenCount; i < tokenCount; i++)
2820                 {
2821                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
2822                         if (pToken)
2823                         {
2824                                 _VisualElement* pTokenVisualElement = pToken->GetVisualElement();
2825                                 if (pTokenVisualElement)
2826                                 {
2827                                         pTokenVisualElement->SetShowState(false);
2828                                 }
2829                         }
2830                 }
2831
2832                 float totalScrollValue = __maxScrollValue + (__pTokenEdit->GetBoundsF().height - GetInitialBoundsF().height);
2833
2834                 if (totalScrollValue > 0)
2835                 {
2836                         __isNeedToScroll = true;
2837                         __maxScrollValue = totalScrollValue;
2838                         __isTokenScrolling = true;
2839                         if (__lineAdded > 0)
2840                         {
2841                                 __isScrollValueChanged = true;
2842                                 __isScrollValueModified = false;
2843                         }
2844                 }
2845
2846                 FloatRectangle intialWindowBounds = GetInitialBoundsF();
2847                 SetFlexBounds(intialWindowBounds);
2848
2849                 SetEditingTokenTextBounds(__editingTokenIndex, false);
2850
2851                 __descriptionTextRect.y = __descriptionTextRectForScroll.y + __scrollValue;
2852         }
2853
2854         TextObject* pTextObject = GetTextObject();
2855         if (pTextObject)
2856         {
2857                 pTextObject->SetAction(TEXT_OBJECT_ACTION_TYPE_ABBREV);
2858                 pTextObject->Compose();
2859         }
2860
2861         RemoveChildAccessibilityElements();
2862         __pTokenEdit->UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
2863
2864         return _EditPresenter::OnFocusLost();
2865 }
2866
2867 result
2868 _TokenEditPresenter::SetFlexBounds(const FloatRectangle& bounds)
2869 {
2870         if (__pTokenEdit->GetBoundsF().height > bounds.height)
2871         {
2872                 if (__lineAdded > 0)
2873                 {
2874                         __lineAdded--;
2875                 }
2876         }
2877         return _EditPresenter::SetFlexBounds(bounds);
2878 }
2879
2880 result
2881 _TokenEditPresenter::SetTextSize(const int size)
2882 {
2883         result r = E_SUCCESS;
2884         __editContentFontSize = size;
2885
2886         if (!__isEditingToken)
2887         {
2888                 r = _EditPresenter::SetTextSize(size);
2889                 r = SetInitialBounds();
2890                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2891         }
2892
2893         return r;
2894 }
2895
2896 result
2897 _TokenEditPresenter::SetTextSize(const float size)
2898 {
2899         result r = E_SUCCESS;
2900         __editContentFontSize = size;
2901
2902         if (!__isEditingToken)
2903         {
2904                 r = _EditPresenter::SetTextSize(size);
2905                 r = SetInitialBounds();
2906                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
2907         }
2908
2909         return r;
2910 }
2911
2912 bool
2913 _TokenEditPresenter::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
2914 {
2915         //Remove token focus on touch press
2916         __focusedTokenIndex = -1;
2917
2918         int tokenIndex = GetTokenIndexFromCoordinate(_CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()));
2919         __trackTokenIndex = tokenIndex;
2920
2921         _TouchInfo TouchInfo(touchinfo);
2922         if (tokenIndex != -1)
2923         {
2924                 if (tokenIndex == __editingTokenIndex)
2925                 {
2926                         __touchPressInfo.x = touchinfo.GetCurrentPosition().x;
2927                         __touchPressInfo.y = touchinfo.GetCurrentPosition().y;
2928
2929                         _Token* pToken = null;
2930                         pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
2931                         if (pToken)
2932                         {
2933                                 float tokenX = pToken->displayRect.x;
2934                                 float tokenY = pToken->displayRect.y;
2935                                 FloatPoint point(__touchPressInfo.x - tokenX, __touchPressInfo.y - tokenY);
2936                                 TouchInfo.SetTouchInfo(touchinfo.GetPointId(), touchinfo.GetTouchStatus(), point, touchinfo.IsFlicked(), touchinfo.GetTimeStamp());
2937                         }
2938                 }
2939         }
2940
2941         return _EditPresenter::OnTouchPressed(source, TouchInfo);
2942 }
2943
2944 bool
2945 _TokenEditPresenter::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
2946 {
2947         __touchPressInfo = FloatPoint(-1.0f, -1.0f);
2948
2949         ProcessTokeningByTouchEvent(source, touchinfo);
2950         if (GetTokenCount())
2951         {
2952                 //Set token bounds appropriately On Fast flick of scroll bar
2953                 if (!(__isEditingToken || __editingTokenIndex >= 0))
2954                 {
2955                         SetTokenBoundsByTouchInfo(touchinfo);
2956                 }
2957         }
2958
2959         _Scroll* pScroll = GetScrollBar();
2960         if (pScroll)
2961         {
2962                 pScroll->SetScrollVisibility(false);
2963         }
2964
2965         if (__prevScrollValue)
2966         {
2967                 __prevScrollValue = 0.0f;
2968                 __isTokenScrolling = false;
2969         }
2970
2971         _TouchInfo TouchInfo(touchinfo);
2972         _Token* pToken = null;
2973         if (__editingTokenIndex >= 0)
2974         {
2975                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
2976                 if (pToken)
2977                 {
2978                         int tokenX = _CoordinateSystemUtils::ConvertToInteger(pToken->displayRect.x);
2979                         int tokenY = _CoordinateSystemUtils::ConvertToInteger(pToken->displayRect.y);
2980                         Point point(_CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()).x - tokenX, _CoordinateSystemUtils::ConvertToInteger(touchinfo.GetCurrentPosition()).y - tokenY);
2981                         TouchInfo.SetTouchInfo(touchinfo.GetPointId(), touchinfo.GetTouchStatus(), point, touchinfo.IsFlicked(), touchinfo.GetTimeStamp());
2982                 }
2983                 _EditPresenter::OnTouchReleased(source, TouchInfo);
2984                 __previousCursorPosition = GetCursorPosition();
2985         }
2986         else
2987         {
2988                 _EditPresenter::OnTouchReleased(source, touchinfo);
2989         }
2990
2991         __isTouchMoveInProgress = false;
2992         __trackTokenIndex = -1;
2993
2994         return false;
2995 }
2996
2997 void
2998 _TokenEditPresenter::OnInputConnectionTextCommitted(InputConnection& source, const String& committedText)
2999 {
3000         OnTextCommitted(committedText);
3001 }
3002
3003 void
3004 _TokenEditPresenter::OnTextCommitted(const String& commitText)
3005 {
3006         char enterText[2] = {'\n', };
3007         String enterTextComma(",");
3008         String enterTextSemiColon(";");
3009
3010         //OnTextCommitted blocked for these cases
3011         //1. Tab text not to be handled
3012         //2. Token is focused
3013         char tapText[2] = {'\t', };
3014         if (commitText == tapText)
3015         {
3016                 return;
3017         }
3018
3019         if ((commitText == enterText) || (commitText == enterTextComma) || (commitText == enterTextSemiColon))
3020         {
3021                 CoreKeypadAction keypadaction = GetKeypadAction();
3022                 __pTokenEdit->SendKeypadEvent(keypadaction, CORE_KEYPAD_EVENT_STATUS_ENTERACTION);
3023
3024                 if (__editingTokenIndex != -1)
3025                 {
3026                         ExitTokenEditingMode();
3027                 }
3028
3029                 if (GetText().GetLength() > 0)
3030                 {
3031                         MakeToken();
3032                 }
3033
3034                 for (int i = 0; i < __pTokenList->GetCount(); i++)
3035                 {
3036                         TrimTokenAndAdjustEllipsisAt(i);
3037                         InitializeTokenVisibilityAt(i);
3038                 }
3039
3040                 if (__pressedTokenIndex < 0)
3041                 {
3042                         SetCursorDisabled(true);
3043                         __pTokenEdit->Draw();
3044                         SetCursorDisabled(false);
3045                         StartCursorTimer();
3046                 }
3047
3048                 return;
3049         }
3050
3051         _EditPresenter::OnTextCommitted(commitText);
3052         __previousCursorPosition = GetCursorPosition();
3053
3054         _Token* pToken = null;
3055         if (__editingTokenIndex >= 0)
3056         {
3057                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3058                 SysTryReturnVoidResult(NID_UI_CTRL, pToken, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
3059
3060                 pToken->ResetToken(GetText());
3061                 TrimTokenAndAdjustEllipsisAt(__editingTokenIndex);
3062
3063                 float tokenHeight = 0.0f;
3064                 float tokenVerticalSpacing = 0.0f;
3065                 float tokenTextLeftMargin = 0.0f;
3066
3067                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3068                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
3069                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
3070                 GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
3071
3072                 FloatRectangle tempTextDspRect;
3073                 tempTextDspRect.x += tokenTextLeftMargin;
3074                 tempTextDspRect.y += tokenVerticalSpacing / 2.0f;
3075                 tempTextDspRect.width = pToken->displayRect.width - (tokenTextLeftMargin * 2.0f);
3076                 tempTextDspRect.height = tokenHeight - tokenVerticalSpacing;
3077
3078                 SetTextBounds(tempTextDspRect);
3079
3080                 if (GetCursorPosition() > pToken->currTokenLength)
3081                 {
3082                         _EditPresenter::SetCursorPosition(pToken->currTokenLength);
3083                 }
3084                 else
3085                 {
3086                         _EditPresenter::SetCursorPosition(GetCursorPosition());
3087                 }
3088         }
3089         else
3090         {
3091                 if (__isEditingToken == false)
3092                 {
3093                         __pressedTokenIndex = -1;
3094                         __focusedTokenIndex = -1;
3095                         SetCursorDisabled(false);
3096                 }
3097
3098                 float tokenTopMargin = 0.0f;
3099                 _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3100                 GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
3101
3102                 FloatRectangle textBounds = GetTextBoundsF();
3103                 textBounds.height = GetMaxTextHeight();
3104                 SetTextBounds(textBounds);
3105         }
3106
3107         if (__editingTokenIndex < 0)
3108         {
3109                 CheckTokenScrolling();
3110         }
3111         __pTokenEdit->Invalidate();
3112
3113         return;
3114 }
3115
3116 void
3117 _TokenEditPresenter::DeleteSurroundingText(InputConnection& source, int offset, int charCount)
3118 {
3119         OnSurroundingTextDeleted(offset, charCount);
3120 }
3121
3122 void
3123 _TokenEditPresenter::OnSurroundingTextDeleted(int offset, int charCount)
3124 {
3125         __lastTokenIndex = GetTokenCount() - 1;
3126         int cursorPosition = 0;
3127         int start = 0;
3128         int end = 0;
3129
3130         if ((offset == -1) && (charCount == 1))
3131         {
3132                 if (GetTextLength() == 0 && GetTokenCount())        // There is no candidate token.
3133                 {
3134                         if (__isEditingToken == true)
3135                         {
3136                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3137                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3138
3139                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3140                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3141
3142                                 _Token* pToken = null;
3143                                 _VisualElement* pTokenVisualElement = null;
3144
3145                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3146
3147                                 if (pToken)
3148                                 {
3149                                         pTokenVisualElement = pToken->GetVisualElement();
3150                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3151
3152                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3153                                         {
3154                                                 result r = E_SUCCESS;
3155                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3156                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3157
3158                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3159                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3160                                         }
3161                                 }
3162
3163                                 RemoveTokenAt(__editingTokenIndex);
3164
3165                                 CalculateTokenPositionFromIndex(__editingTokenIndex);
3166                                 for (int i = __editingTokenIndex; i < __lastTokenIndex + 1; i++)
3167                                 {
3168                                         _Token* pToken = null;
3169                                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
3170                                         if (pToken)
3171                                         {
3172                                                 pToken->SetBounds(pToken->displayRect);
3173                                         }
3174                                 }
3175
3176                                 __pressedTokenIndex = -1;
3177                                 __editingTokenIndex = -1;
3178                                 __isEditingToken = false;
3179                                 _EditPresenter::SetTextSize(__editContentFontSize);
3180                                 __isTokenEditingFinished = false;
3181
3182                                 AdjustFlexibleHeight();
3183                                 CheckTokenScrolling();
3184                         }
3185                         else if (__pressedTokenIndex != -1)
3186                         {
3187                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3188                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3189
3190                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3191                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3192
3193                                 _Token* pToken = null;
3194                                 _VisualElement* pTokenVisualElement = null;
3195
3196                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__pressedTokenIndex));
3197
3198                                 if (pToken)
3199                                 {
3200                                         pTokenVisualElement = pToken->GetVisualElement();
3201                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3202
3203                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3204                                         {
3205                                                 result r = E_SUCCESS;
3206                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3207                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3208
3209                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3210                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3211                                         }
3212                                 }
3213
3214                                 RemoveTokenAt(__pressedTokenIndex);
3215
3216                                 CalculateTokenPositionFromIndex(__pressedTokenIndex);
3217                                 for (int i = __pressedTokenIndex; i < __lastTokenIndex + 1; i++)
3218                                 {
3219                                         _Token* pToken = null;
3220                                         pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
3221
3222                                         if (pToken)
3223                                         {
3224                                                 pToken->SetBounds(pToken->displayRect);
3225                                         }
3226                                 }
3227
3228                         }
3229                         else if (__focusedTokenIndex != -1)
3230                         {
3231                                 RemoveTokenAt(__focusedTokenIndex);
3232                         }
3233                         else
3234                         {
3235                                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3236                                 SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3237
3238                                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3239                                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3240
3241                                 _Token* pToken = null;
3242                                 _VisualElement* pTokenVisualElement = null;
3243                                 if (__animatingIndex == (GetTokenCount() - 1))
3244                                 {
3245                                         pToken = static_cast< _Token* >(__pTokenList->GetAt(GetTokenCount() - 1));
3246                                         if (pToken)
3247                                         {
3248                                                 pTokenVisualElement = pToken->GetVisualElement();
3249                                                 SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3250                                                 pTokenVisualElement->RemoveAnimation(L"TokenAnimation");
3251                                         }
3252                                 }
3253                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(GetTokenCount() - 1));
3254                                 if (pToken)
3255                                 {
3256                                         pTokenVisualElement = pToken->GetVisualElement();
3257                                         SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3258
3259                                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3260                                         {
3261                                                 result r = E_SUCCESS;
3262                                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3263                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3264
3265                                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3266                                                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating", GetErrorMessage(r));
3267                                         }
3268                                 }
3269
3270                                 __animatingIndex = GetTokenCount() - 1;
3271
3272                                 if (pTokenVisualElement && __animatingIndex >= 0)
3273                                 {
3274                                         __isAnimationInProgress = true;
3275                                         VisualElementAnimation* pAnimation = CreateAnimationN(*pTokenVisualElement, false);
3276                                         pTokenVisualElement->AddAnimation(L"TokenAnimation", *pAnimation);
3277                                         delete pAnimation;
3278                                 }
3279                         }
3280
3281                         __pTokenEdit->UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
3282
3283                         DrawText();
3284                         __pTokenEdit->Invalidate();
3285                         return;
3286                 }
3287         }
3288
3289         if (__pressedTokenIndex >= 0 && __editingTokenIndex < 0 && !__isEditingToken)
3290         {
3291                 RemoveTokenAt(__pressedTokenIndex);
3292                 __pTokenEdit->Invalidate();
3293                 return;
3294         }
3295         else if (__focusedTokenIndex >= 0 && __editingTokenIndex < 0 && !__isEditingToken)
3296         {
3297                 RemoveTokenAt(__focusedTokenIndex);
3298                 __pTokenEdit->Invalidate();
3299                 return;
3300         }
3301
3302         //Backspace on Blocked text, delete full block
3303         if (IsBlocked() == true)
3304         {
3305                 GetBlockRange(start, end);
3306         }
3307         else
3308         {
3309                 cursorPosition = GetCursorPosition();
3310                 start = cursorPosition + offset;
3311                 if (start < 0)
3312                 {
3313                         return;
3314                 }
3315                 end = start + charCount;
3316                 if (end > GetTextLength())
3317                 {
3318                         return;
3319                 }
3320         }
3321
3322         DeleteText(start, end);
3323         __previousCursorPosition = start;
3324
3325         if (IsCopyPasteManagerExist())
3326         {
3327                 InitializeCopyPasteManager();
3328         }
3329         if (IsBlocked() == true)
3330         {
3331                 ReleaseTextBlock();
3332         }
3333         if (__isEditingToken != true)
3334         {
3335                 DrawText();
3336         }
3337
3338         __pTokenEdit->SendTextEvent(CORE_TEXT_EVENT_CHANGED);
3339
3340 #if 0 //ORIGINAL
3341         cursorPosition = GetCursorPosition();
3342         start = cursorPosition + offset;
3343         if (start < 0)
3344         {
3345                 return;
3346         }
3347         end = start + charCount;
3348         if (end > GetTextLength())
3349         {
3350                 return;
3351         }
3352
3353         Rectangle currBounds = __pTokenEdit->GetBounds();
3354         DeleteText(start, end);
3355         __previousCursorPosition = start;
3356         if (__isEditingToken != true)
3357         {
3358                 DrawText();
3359         }
3360 #endif
3361
3362         _Token* pToken = null;
3363
3364         if (__editingTokenIndex >= 0 && __isEditingToken)
3365         {
3366                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3367
3368                 if (pToken)
3369                 {
3370                         float tokenHeight = 0.0f;
3371                         float tokenVerticalSpacing = 0.0f;
3372                         float tokenTextLeftMargin = 0.0f;
3373
3374                         SetCursorPosition(__previousCursorPosition);
3375
3376                         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3377                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
3378                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_VERTICAL_SPACING, orientation, tokenVerticalSpacing);
3379                         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_TEXT_LEFT_MARGIN, orientation, tokenTextLeftMargin);
3380
3381                         FloatRectangle tempTextDspRect;
3382                         tempTextDspRect.x += tokenTextLeftMargin;
3383                         tempTextDspRect.y += tokenVerticalSpacing / 2.0f;
3384                         tempTextDspRect.width = pToken->displayRect.width - (tokenTextLeftMargin * 2.0f);
3385                         tempTextDspRect.height = tokenHeight - tokenVerticalSpacing;
3386
3387                         SetTextBounds(tempTextDspRect);
3388                         _EditPresenter::SetCursorPosition(start);
3389
3390                         pToken->ResetToken(GetText());
3391                         TrimTokenAndAdjustEllipsisAt(__editingTokenIndex);
3392                 }
3393         }
3394
3395         __pTokenEdit->Draw();
3396 }
3397
3398 bool
3399 _TokenEditPresenter::OnTapGestureDetected(void)
3400 {
3401         if (__editingTokenIndex >= 0)
3402         {
3403                 __isPopupVisible = true;
3404         }
3405
3406         //Uncomment below to Block Copy & Paste functionality in Token Edit mode
3407         if (__pressedTokenIndex != -1)
3408         {
3409                 return true;
3410         }
3411         return _EditPresenter::OnTapGestureDetected();
3412 }
3413
3414 bool
3415 _TokenEditPresenter::CheckCopyPastePopupShowStatus(void)
3416 {
3417         if (__editingTokenIndex < 0)
3418         {
3419                 float controlHeight = __pTokenEdit->GetBoundsF().height;
3420                 FloatRectangle cursorBounds;
3421                 GetCursorBounds(false, cursorBounds);
3422                 if (cursorBounds.y > controlHeight)
3423                 {
3424                         return true;
3425                 }
3426         }
3427
3428         return false;
3429 }
3430
3431 bool
3432 _TokenEditPresenter::OnLongPressGestureDetected(void)
3433 {
3434         if (CheckCopyPastePopupShowStatus())
3435         {
3436                 //TextBounds bigger than control height. Dont show Copy paste popup
3437                 return true;
3438         }
3439
3440         //Discard all long press that is detected after Touch press on a  token
3441         if (__trackTokenIndex >= 0)
3442         {
3443                 return true;
3444         }
3445
3446         __isLongPressed = true;
3447
3448         //Uncomment below to Block Copy & Paste functionality in Token Edit mode
3449         if (__pressedTokenIndex != -1)
3450         {
3451                 return true;
3452         }
3453         return _EditPresenter::OnLongPressGestureDetected();
3454 }
3455
3456 void
3457 _TokenEditPresenter::OnCursorTimerExpired(void)
3458 {
3459         if (__editingTokenIndex != -1)
3460         {
3461                 if (!IsFocused())
3462                 {
3463                         StopCursorTimer();
3464                         return;
3465                 }
3466
3467                 FloatRectangle cursorRect;
3468
3469                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3470                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null");
3471
3472                 //Set Editing token bounds for text scroll and cursor position calculation (SetTextBounds should have been done prior to this)
3473                 FloatRectangle cursorDspRect = _EditPresenter::GetTextBoundsF();
3474
3475                 if (CalculateCursorBounds(cursorDspRect, cursorRect) != E_SUCCESS)
3476                 {
3477                         StartCursorTimer();
3478                         return;
3479                 }
3480
3481                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3482                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3483
3484                 Canvas* pCursorCanvas = pCursorVisualElement->GetCanvasN();
3485                 SysTryReturnVoidResult(NID_UI_CTRL, pCursorCanvas != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get canvas of cursor visual element.");
3486
3487                 bool cursorEnable = IsCursorEnabled();
3488
3489                 pCursorVisualElement->SetBounds(cursorRect);
3490                 DrawCursor(*pCursorCanvas, cursorRect, cursorEnable);
3491
3492                 if (cursorEnable)
3493                 {
3494                         cursorEnable = false;
3495                 }
3496                 else
3497                 {
3498                         cursorEnable = true;
3499                 }
3500                 SetCursorEnabled(cursorEnable);
3501
3502                 pCursorCanvas->Show();
3503                 delete pCursorCanvas;
3504
3505                 StartCursorTimer();
3506         }
3507         else
3508         {
3509                 _EditPresenter::OnCursorTimerExpired();
3510         }
3511 }
3512
3513 bool
3514 _TokenEditPresenter::IsTextBlockedInTokenEdit(void) const
3515 {
3516         if ((IsBlocked() == true) && (__isEditingToken) && (__editingTokenIndex >= 0))
3517         {
3518                 return true;
3519         }
3520         return false;
3521 }
3522
3523 bool
3524 _TokenEditPresenter::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
3525 {
3526         __isTouchMoveInProgress = true;
3527
3528         if (GetTokenCount())
3529         {
3530                 //Scrolling is blocked when a popup is visible or when a text is blocked
3531                 if (IsBlocked() == true || __isLongPressed == true)
3532                 {
3533                         return false;
3534                 }
3535
3536                 //Allow touch move only in horizontal direction when editing token
3537                 _TouchInfo TouchInfo(touchinfo);
3538                 _Token* pToken = null;
3539                 if (__editingTokenIndex >= 0)
3540                 {
3541                         if (__touchPressInfo.y > 0.0f)
3542                         {
3543                                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__editingTokenIndex));
3544                                 if (pToken)
3545                                 {
3546                                         float tokenX = pToken->displayRect.x;
3547                                         float tokenY = pToken->displayRect.y;
3548                                         FloatPoint point(touchinfo.GetCurrentPosition().x - tokenX, __touchPressInfo.y - tokenY);
3549                                         TouchInfo.SetTouchInfo(touchinfo.GetPointId(), touchinfo.GetTouchStatus(), point, touchinfo.IsFlicked(), touchinfo.GetTimeStamp());
3550                                 }
3551
3552                                 bool retValue = _EditPresenter::OnTouchMoved(source, TouchInfo);
3553                                 __previousCursorPosition = GetCursorPosition();
3554
3555                                 return retValue;
3556                         }
3557                         else
3558                         {
3559                                 return false;
3560                         }
3561                 }
3562
3563                 SetTokenBoundsByTouchInfo(touchinfo);
3564
3565                 if (!(__pTokenEdit->GetEditStyle() & EDIT_STYLE_NOSCROLL))
3566                 {
3567                         _Scroll* pScroll = GetScrollBar();
3568                         if (pScroll)
3569                         {
3570                                 float tokenBottomMargin = 0.0f;
3571                                 GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, tokenBottomMargin);
3572
3573                                 float totalHeight = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin;
3574                                 float controlHeight = __pTokenEdit->GetBoundsF().height;
3575
3576                                 if (totalHeight > controlHeight)
3577                                 {
3578                                         pScroll->SetScrollVisibility(true);
3579                                 }
3580                                 else
3581                                 {
3582                                         pScroll->SetScrollVisibility(false);
3583                                 }
3584                         }
3585                 }
3586
3587                 return _EditPresenter::OnTouchMoved(source, touchinfo);
3588         }
3589         else
3590         {
3591                 return _EditPresenter::OnTouchMoved(source, touchinfo);
3592         }
3593
3594 }
3595
3596 void
3597 _TokenEditPresenter::OnVisualElementAnimationFinished(const Tizen::Ui::Animations::VisualElementAnimation& animation, const Tizen::Base::String& keyName, Tizen::Ui::Animations::VisualElement& target, bool completedNormally)
3598 {
3599         __isAnimationInProgress = false;
3600         RemoveTokenAt(GetTokenCount() - 1);
3601         CalculateTokenPositionFromIndex(GetTokenCount() - 1);
3602
3603         for (int i = GetTokenCount() - 1; i < GetTokenCount() - 1 + 1; i++)
3604         {
3605                 _Token* pToken = null;
3606                 pToken = static_cast< _Token* >(__pTokenList->GetAt(i));
3607
3608                 if (pToken)
3609                 {
3610                         pToken->SetBounds(pToken->displayRect);
3611                 }
3612         }
3613
3614         if (__lastTokenIndex == __pressedTokenIndex)
3615         {
3616                 __pressedTokenIndex--;
3617         }
3618
3619         if (GetTokenCount() == 0 && __animatingIndex == 0)
3620         {
3621                 DrawText();
3622         }
3623         __animatingIndex = -1;
3624         InitializeCursor();
3625
3626         DrawToken();
3627
3628         __pTokenEdit->UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
3629
3630         return;
3631 }
3632
3633 void
3634 _TokenEditPresenter::OnTimerExpired(Timer& timer)
3635 {
3636         Timer* onTimer = &timer;
3637         Canvas* pDescriptionTextCanvas = null;
3638
3639         if (onTimer == _EditPresenter::__pTitleSlidingTimer)
3640         {
3641                 if (!IsFocused())
3642                 {
3643                         _EditPresenter::StopTitleSlidingTimer();
3644                         __isTitleSliding = false;
3645                         return;
3646                 }
3647
3648                 FloatRectangle tempDescriptionTextRect = __descriptionTextRect;
3649                 tempDescriptionTextRect.x = 0.0f;
3650                 tempDescriptionTextRect.y = 0.0f;
3651
3652                 pDescriptionTextCanvas = __pDescriptionTextVisualElement->GetCanvasN();
3653                 SysTryReturnVoidResult(NID_UI_CTRL, pDescriptionTextCanvas, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] pDescriptionTextCanvas is invalid!");
3654
3655                 pDescriptionTextCanvas->SetBackgroundColor(Color(0));
3656                 pDescriptionTextCanvas->Clear();
3657                 __pDescriptionTextTextObject->SetBounds(tempDescriptionTextRect);
3658                 __pDescriptionTextTextObject->DrawWithOffset(*_CanvasImpl::GetInstance(*pDescriptionTextCanvas));
3659                 Rectangle descriptionTextRect = _CoordinateSystemUtils::ConvertToInteger(tempDescriptionTextRect);
3660                 pDescriptionTextCanvas->Show(descriptionTextRect);
3661
3662                 delete pDescriptionTextCanvas;
3663
3664                 _EditPresenter::StartTitleSlidingTimer();
3665                 __isTitleSliding = true;
3666         }
3667         else
3668         {
3669                 _EditPresenter::OnTimerExpired(timer);
3670         }
3671 }
3672
3673 result
3674 _TokenEditPresenter::ChangeInternalLayout(_ControlOrientation orientation)
3675 {
3676         result r = E_SUCCESS;
3677
3678         __scrollValue = 0.0f;
3679         __maxScrollValue = 0.0f;
3680         float tokenBottomMargin = 0.0f;
3681         float newScrollValue = 0.0f;
3682
3683         FloatRectangle windowBounds = GetInitialBoundsF();
3684
3685         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
3686
3687         r = CalculateTokenPositionFromIndex(0);
3688         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3689
3690         r = SetInitialBounds();
3691         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3692
3693         if (__isEditingToken)
3694         {
3695                 if (__editingTokenIndex >= 0 && __editingTokenIndex < GetTokenCount())
3696                 {
3697                         String inputTokenString = GetText();
3698                         String replacementString = inputTokenString;
3699                         bool enable = false;
3700
3701                         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
3702                         if (enable)
3703                         {
3704                                 inputTokenString = replacementString;
3705                         }
3706
3707                         if (inputTokenString.GetLength() > 0)
3708                         {
3709                                 int index = __editingTokenIndex;
3710                                 RemoveTokenAt(__editingTokenIndex, true);
3711                                 InsertTokenAt(index, inputTokenString);
3712                         }
3713                 }
3714         }
3715
3716         if (IsFocused())
3717         {
3718                 r = AdjustFlexibleHeight();
3719                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3720
3721                 r = CheckTokenScrolling();
3722                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3723         }
3724         else
3725         {
3726                 if (!__autoShrink)
3727                 {
3728                         r = AdjustFlexibleHeight();
3729                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3730
3731                         windowBounds = __pTokenEdit->GetBoundsF();
3732                 }
3733
3734                 newScrollValue = GetTextBoundsF().y + GetTextBoundsF().height - __scrollValue + tokenBottomMargin - windowBounds.height;
3735                 if (newScrollValue > 0.0f)
3736                 {
3737                         __maxScrollValue = newScrollValue;
3738                         __isNeedToScroll = true;
3739                 }
3740         }
3741         return r;
3742 }
3743
3744 result
3745 _TokenEditPresenter::ChangeLayout(_ControlOrientation orientation)
3746 {
3747         result r = E_SUCCESS;
3748
3749         r = _EditPresenter::ChangeLayout(orientation);
3750         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3751
3752         r = ChangeInternalLayout(orientation);
3753
3754         return r;
3755 }
3756
3757 void
3758 _TokenEditPresenter::OnBoundsChanged(void)
3759 {
3760         if (!__isTokenEditPresenterInitialized)
3761         {
3762                 return;
3763         }
3764
3765         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
3766         FloatRectangle tokenEditBounds = __pTokenEdit->GetBoundsF();
3767
3768         if (IsUpdateInitialBounds())
3769         {
3770                 SetControlInitialBounds(tokenEditBounds);
3771         }
3772         ChangeInternalLayout(orientation);
3773
3774         return;
3775 }
3776
3777 result
3778 _TokenEditPresenter::AttachCursorToToken(void)
3779 {
3780         result r = E_SUCCESS;
3781
3782         if (__pressedTokenIndex != -1)
3783         {
3784                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3785                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement, E_SYSTEM, "A system error has occurred. Failed to get root visual element.");
3786
3787                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3788                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement, E_SYSTEM, "A system error has occurred. Failed to get cursor visual element.");
3789
3790                 _Token* pToken = null;
3791                 _VisualElement* pTokenVisualElement = null;
3792
3793                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__pressedTokenIndex));
3794                 if (pToken)
3795                 {
3796                         pTokenVisualElement = pToken->GetVisualElement();
3797                         SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement, E_SYSTEM, "A system error has occurred. Failed to get token visual element.");
3798
3799                         if (pCursorVisualElement->GetParent() != pTokenVisualElement)
3800                         {
3801                                 r = (pCursorVisualElement->GetParent())->DetachChild(*pCursorVisualElement);
3802                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3803
3804                                 r = pTokenVisualElement->AttachChild(*pCursorVisualElement);
3805                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
3806                         }
3807                 }
3808         }
3809
3810         return r;
3811 }
3812
3813 result
3814 _TokenEditPresenter::DetachCursorFromToken(void)
3815 {
3816         result r = E_SUCCESS;
3817
3818         if (__pressedTokenIndex != -1)
3819         {
3820                 _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3821                 SysTryReturnResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3822
3823                 _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3824                 SysTryReturnResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3825
3826                 _Token* pToken = null;
3827                 _VisualElement* pTokenVisualElement = null;
3828
3829                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__pressedTokenIndex));
3830
3831                 if (pToken)
3832                 {
3833                         pTokenVisualElement = pToken->GetVisualElement();
3834                         SysTryReturnResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3835
3836                         if (pCursorVisualElement->GetParent() != pEditVisualElement)
3837                         {
3838                                 r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3839                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3840
3841                                 r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3842                                 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3843                         }
3844                 }
3845         }
3846         return r;
3847 }
3848
3849 void
3850 _TokenEditPresenter::ExitTokenEditingMode(void)
3851 {
3852         result r = E_SUCCESS;
3853
3854         _VisualElement* pEditVisualElement = __pTokenEdit->GetVisualElement();
3855         SysTryReturnVoidResult(NID_UI_CTRL, pEditVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get root visual element.");
3856
3857         _VisualElement* pCursorVisualElement = GetCursorVisualElement();
3858         SysTryReturnVoidResult(NID_UI_CTRL, pCursorVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get cursor visual element.");
3859
3860         _Token* pToken = null;
3861         _VisualElement* pTokenVisualElement = null;
3862
3863         pToken = static_cast <_Token*>(__pTokenList->GetAt(__editingTokenIndex));
3864
3865         if (pToken)
3866         {
3867                 pTokenVisualElement = pToken->GetVisualElement();
3868                 SysTryReturnVoidResult(NID_UI_CTRL, pTokenVisualElement != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. Failed to get token visual element.");
3869
3870                 if (pCursorVisualElement->GetParent() != pEditVisualElement)
3871                 {
3872                         r = pCursorVisualElement->GetParent()->DetachChild(*pCursorVisualElement);
3873                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3874
3875                         r = pEditVisualElement->AttachChild(*pCursorVisualElement);
3876                         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, E_SYSTEM, "[%s] Propagating.", GetErrorMessage(r));
3877                 }
3878         }
3879
3880         String inputTokenString = GetText();
3881         String replacementString = inputTokenString;
3882         bool enable = false;
3883
3884         __pTokenEdit->ProcessTokenFiltering(inputTokenString, replacementString, enable);
3885         if (enable)
3886         {
3887                 inputTokenString = replacementString;
3888         }
3889
3890         r = RemoveTokenAt(__editingTokenIndex);
3891
3892         _EditPresenter::SetTextSize(__editContentFontSize);
3893         SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
3894
3895         if (inputTokenString.GetLength() > 0)
3896         {
3897                 r = InsertTokenAt(__editingTokenIndex, inputTokenString);
3898                 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
3899
3900                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__editingTokenIndex));
3901                 SysTryReturnVoidResult(NID_UI_CTRL, pToken != null, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
3902
3903                 pToken->currTokenLength = inputTokenString.GetLength();
3904         }
3905
3906         CalculateTokenPositionFromIndex(0);
3907         int lastTokenIndex = GetTokenCount() - 1;
3908         for (int i = 0; i < lastTokenIndex + 1; i++)
3909         {
3910                 _Token* pToken = null;
3911                 pToken = static_cast <_Token*>(__pTokenList->GetAt(i));
3912
3913                 if (pToken)
3914                 {
3915                         pToken->SetBounds(pToken->displayRect);
3916                 }
3917         }
3918
3919         AdjustFlexibleHeight();
3920         __pressedTokenIndex = -1;
3921         __isTokenEditingFinished = true;
3922         __editingTokenIndex = -1;
3923         __isEditingToken = false;
3924
3925         CheckTokenScrolling();
3926         SetCursorDisabled(false);
3927
3928         if (inputTokenString.GetLength() <= 0)
3929         {
3930                 SysLog(NID_UI_CTRL, "[E_INVALID_ARG] Invalid argument is used. Token length is (%d)", inputTokenString.GetLength());
3931         }
3932
3933         return;
3934 }
3935
3936 bool
3937 _TokenEditPresenter::OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
3938 {
3939         _KeyCode keyCode = keyInfo.GetKeyCode();
3940         bool focusChanged = false;
3941         int tokenCount = GetTokenCount();
3942         int lastTokenIndex = tokenCount - 1;
3943
3944         if (IsUsbKeyboardConnected() && (keyInfo.GetKeyModifier() & _KEY_MODIFIER_CTRL))
3945         {
3946                 switch (keyCode)
3947                 {
3948                 case _KEY_A:
3949                 case _KEY_C:
3950                 case _KEY_X:
3951                 case _KEY_V:
3952                         if (__isEditingToken)
3953                         {
3954                                 return true;
3955                         }
3956                         break;
3957
3958                 default:
3959                         break;
3960                 }
3961         }
3962
3963         if ((keyCode == _KEY_NUM_LEFT) || (keyCode == _KEY_LEFT))
3964         {
3965                 if (__drawFocusState && (!__isEditingToken) && (tokenCount > 0))
3966                 {
3967                         if (__focusedTokenIndex == -1)
3968                         {
3969                                 if (GetCursorPosition() == 0)
3970                                 {
3971                                         __focusedTokenIndex = lastTokenIndex;
3972                                         if (GetTextLength() > 0)
3973                                         {
3974                                                 MakeToken();
3975                                                 StopCursorTimer();
3976                                                 SetCursorDisabled(true);
3977                                                 __pTokenEdit->Invalidate();
3978
3979                                                 StartCursorTimer();
3980
3981                                                 return _EditPresenter::OnKeyPressed(source, keyInfo);
3982                                         }
3983                                         focusChanged = true;
3984                                 }
3985                         }
3986                         else
3987                         {
3988                                 if (__focusedTokenIndex > 0)
3989                                 {
3990                                         __focusedTokenIndex--;
3991                                         focusChanged = true;
3992                                 }
3993                         }
3994                 }
3995         }
3996
3997         if ((keyCode == _KEY_NUM_RIGHT) || (keyCode == _KEY_RIGHT))
3998         {
3999                 if (__drawFocusState && (!__isEditingToken) && (tokenCount > 0))
4000                 {
4001                         if (__focusedTokenIndex != -1)
4002                         {
4003                                 if (__focusedTokenIndex == lastTokenIndex)
4004                                 {
4005                                         __focusedTokenIndex = -1;
4006                                         focusChanged = true;
4007                                 }
4008                                 else
4009                                 {
4010                                         __focusedTokenIndex++;
4011                                         focusChanged = true;
4012                                 }
4013                         }
4014                 }
4015         }
4016
4017         if (focusChanged)
4018         {
4019                 StopCursorTimer();
4020
4021                 if (__focusedTokenIndex != -1)
4022                 {
4023                         SetCursorDisabled(true);
4024                         ScrollToFocusedToken();
4025                 }
4026                 else
4027                 {
4028                         SetCursorDisabled(false);
4029                         CheckTokenScrolling();
4030                 }
4031
4032                 StartCursorTimer();
4033         }
4034
4035         return _EditPresenter::OnKeyPressed(source, keyInfo);
4036 }
4037
4038 String
4039 _TokenEditPresenter::GetTextAccessibilityElementText(void) const
4040 {
4041         String tokenText;
4042         String spaceText = " ";
4043         _Token* pToken = null;
4044         int tokenCount = GetTokenCount();
4045         static const int readTokenCount = 2;
4046         if (tokenCount > 0)
4047         {
4048                 for (int index = 0; index < readTokenCount; index++)
4049                 {
4050                         pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4051                         SysTryReturn(NID_UI_CTRL, pToken, tokenText, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4052
4053                         if (index < (readTokenCount - 1))
4054                         {
4055                                 tokenText += pToken->GetText() + spaceText;
4056                         }
4057                         else
4058                         {
4059                                 String moreTokenText;
4060                                 int moreTokenCount = tokenCount - readTokenCount;
4061                                 if (moreTokenCount > 0)
4062                                 {
4063                                         moreTokenText.Format(15, L"and %d more", moreTokenCount);
4064                                 }
4065                                 tokenText += pToken->GetText() + spaceText + moreTokenText;
4066                         }
4067                 }
4068         }
4069         return tokenText;
4070 }
4071
4072 void
4073 _TokenEditPresenter::RefreshAccessibilityElements(void)
4074 {
4075         RemoveChildAccessibilityElements();
4076         AddChildAccessibilityElements();
4077
4078         return;
4079 }
4080
4081 result
4082 _TokenEditPresenter::AddChildAccessibilityElements(void)
4083 {
4084         //Accessibility Elements added to the container upon focus gained
4085         //1.Title
4086         //2.Token(s)
4087         //3. __accessibilityElements 0 - title 1 - token 0
4088
4089         result r = E_SUCCESS;
4090         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4091
4092         __pTokenEdit->AddTitleAccessibilityElement();
4093         int tokenCount = GetTokenCount();
4094
4095         for (int index = 0; index < tokenCount; index++)
4096         {
4097                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4098                 SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4099
4100                 if (pContainer)
4101                 {
4102                         _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
4103                         SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4104
4105                         String labelText = pToken->GetText();
4106                         pAccessibilityElement->SetBounds(pToken->displayRect);
4107                         pAccessibilityElement->SetLabel(labelText);
4108                         pContainer->AddElement(*pAccessibilityElement);
4109                         __accessibilityElements.Add(pAccessibilityElement);
4110                 }
4111         }
4112
4113         __pTokenEdit->AddCursorAccessibilityElement();
4114
4115         return r;
4116 }
4117
4118 void
4119 _TokenEditPresenter::RemoveChildAccessibilityElements(void)
4120 {
4121         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4122         _AccessibilityElement* pAccessibilityElement = null;
4123
4124         __pTokenEdit->RemoveTitleAccessibilityElement();
4125
4126         while (__accessibilityElements.GetCount() > 0)
4127         {
4128                 if ((__accessibilityElements.GetAt(0, pAccessibilityElement)) == E_SUCCESS)
4129                 {
4130                         __accessibilityElements.RemoveAt(0);
4131                         pContainer->RemoveElement(*pAccessibilityElement);
4132                 }
4133         }
4134
4135         __pTokenEdit->RemoveCursorAccessibilityElement();
4136
4137         return;
4138 }
4139
4140 result
4141 _TokenEditPresenter::AppendTokenAccessibilityElement(void)
4142 {
4143         result r = E_SUCCESS;
4144         int tokenCount = GetTokenCount();
4145
4146         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4147
4148         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(tokenCount - 1));
4149         SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4150
4151         if (pContainer)
4152         {
4153                 _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
4154                 SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4155
4156                 String labelText = pToken->GetText();
4157                 pAccessibilityElement->SetBounds(pToken->displayRect);
4158                 pAccessibilityElement->SetLabel(labelText);
4159                 pAccessibilityElement->SetHint(L"double tap to edit");
4160                 pContainer->AddElement(*pAccessibilityElement);
4161                 __accessibilityElements.Add(pAccessibilityElement);
4162         }
4163
4164         return r;
4165 }
4166
4167 result
4168 _TokenEditPresenter::InsertTokenAccessibilityElementAt(int index)
4169 {
4170         result r = E_SUCCESS;
4171
4172         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4173         _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4174         SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4175
4176         if (pContainer)
4177         {
4178                 _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
4179                 SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
4180
4181                 String labelText = pToken->GetText();
4182                 pAccessibilityElement->SetBounds(pToken->displayRect);
4183                 pAccessibilityElement->SetLabel(labelText);
4184                 pContainer->AddElement(*pAccessibilityElement);
4185                 __accessibilityElements.InsertAt(pAccessibilityElement, index);
4186         }
4187
4188         return r;
4189 }
4190
4191 void
4192 _TokenEditPresenter::RemoveTokenAccessibilityElementAt(int index)
4193 {
4194         _AccessibilityContainer* pContainer = __pTokenEdit->GetAccessibilityContainer();
4195         _AccessibilityElement* pAccessibilityElement = null;
4196
4197         if (pContainer)
4198         {
4199                 if (__accessibilityElements.GetCount() > 0)
4200                 {
4201                         if ((__accessibilityElements.GetAt(index, pAccessibilityElement)) == E_SUCCESS)
4202                         {
4203                                 __accessibilityElements.RemoveAt(index);
4204                                 pContainer->RemoveElement(*pAccessibilityElement);
4205                         }
4206                 }
4207         }
4208         return;
4209 }
4210
4211 result
4212 _TokenEditPresenter::UpdateTokenAccessibilityBounds(void)
4213 {
4214         result r = E_SUCCESS;
4215         int tokenCount = GetTokenCount();
4216         _AccessibilityElement* pAccessibilityElement = null;
4217
4218         for (int index = 0; index < tokenCount; index++)
4219         {
4220                 _Token* pToken = static_cast< _Token* >(__pTokenList->GetAt(index));
4221                 SysTryReturn(NID_UI_CTRL, pToken != null, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] A system error has occurred. The _Token instance is null.");
4222
4223                 if ((__accessibilityElements.GetAt(index, pAccessibilityElement)) == E_SUCCESS)
4224                 {
4225                         pAccessibilityElement->SetBounds(pToken->displayRect);
4226                 }
4227         }
4228         return r;
4229 }
4230
4231 result
4232 _TokenEditPresenter::UpdateTitleAccessibilityBounds(const FloatRectangle& titleBounds)
4233 {
4234         _AccessibilityElement* pTitleAccessibilityElement = __pTokenEdit->GetTitleTextAccessibilityElement();
4235
4236         if (!pTitleAccessibilityElement)
4237         {
4238                 return E_SUCCESS;
4239         }
4240
4241         pTitleAccessibilityElement->SetBounds(titleBounds);
4242
4243         return E_SUCCESS;
4244 }
4245
4246 result
4247 _TokenEditPresenter::ScrollToFocusedTokenAccessibilityElement(const _AccessibilityElement& element)
4248 {
4249         result r = E_SUCCESS;
4250         int focusedTokenIndex = -1;
4251         int tokenCount = GetTokenCount();
4252         FloatRectangle focusedTokenRectangle;
4253         float newScrollValue = 0.0f;
4254         float tokenTopMargin = 0.0f;
4255         float tokenBottomMargin = 0.0f;
4256         float tokenHeight = 0.0f;
4257         float textBoundsAlignValue = 0.0f;
4258         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
4259         _AccessibilityElement* pAccessibilityElement = null;
4260
4261         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
4262         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
4263         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
4264
4265         FloatRectangle tokenEditRect = __pTokenEdit->GetBoundsF();
4266         float textObjectMaxHeight = GetMaxTextHeight();
4267         textBoundsAlignValue = (tokenHeight - textObjectMaxHeight) / 2.0f;
4268
4269         pAccessibilityElement = const_cast< _AccessibilityElement* >(&element);
4270         r = __accessibilityElements.IndexOf(pAccessibilityElement, focusedTokenIndex);
4271         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4272
4273         if ((focusedTokenIndex > -1) && (focusedTokenIndex < tokenCount))
4274         {
4275                 _Token* pToken = null;
4276                 pToken = static_cast< _Token* >(__pTokenList->GetAt(focusedTokenIndex));
4277
4278                 focusedTokenRectangle = pToken->displayRect;
4279
4280                 float focusedTokenPosition = focusedTokenRectangle.y + focusedTokenRectangle.height;
4281
4282                 if ((focusedTokenRectangle.y > 0) && (focusedTokenPosition < tokenEditRect.height))
4283                 {
4284                         DrawToken();
4285                 }
4286                 else
4287                 {
4288                         if (focusedTokenRectangle.y < 0)
4289                         {
4290                                 newScrollValue = focusedTokenRectangle.y - tokenTopMargin - __scrollValue;
4291                         }
4292                         else
4293                         {
4294                                 newScrollValue = focusedTokenPosition - textBoundsAlignValue - tokenEditRect.height + tokenBottomMargin - __scrollValue;
4295                         }
4296
4297                         r = RecalculateTokenBounds(newScrollValue);
4298                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4299                 }
4300
4301                 __accessibilityElements.GetAt(focusedTokenIndex, pAccessibilityElement);
4302                 _AccessibilityManager* pAccessibilityManager = _AccessibilityManager::GetInstance();
4303                 pAccessibilityManager->SetGlobalFocusedElement(*pAccessibilityElement);
4304                 pAccessibilityManager->RequestToDrawFocusUi();
4305         }
4306
4307         return r;
4308 }
4309
4310 bool
4311 _TokenEditPresenter::OnAccessibilityFocusIn(const _AccessibilityContainer& control, const _AccessibilityElement& element)
4312 {
4313         if (__focusOutIndex < 0) // Not a token accessibility element
4314         {
4315                 return false;
4316         }
4317
4318         _Token* pToken = null;
4319         _AccessibilityElement* pCurrentElement = const_cast< _AccessibilityElement* >(&element);
4320         _AccessibilityElement* pPreviousAccessibilityElement = null;
4321         _AccessibilityElement* pNextAccessibilityElement = null;
4322         __accessibilityElements.GetAt(__focusOutIndex, pPreviousAccessibilityElement);
4323
4324         if (pPreviousAccessibilityElement && pPreviousAccessibilityElement->GetAbsoluteBounds().y > element.GetAbsoluteBounds().y)  //Left flick
4325         {
4326                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__focusOutIndex - 1));
4327                 if (pToken)
4328                 {
4329                         if (pToken->displayRect.y < 0.0f && __focusOutIndex > 0)
4330                         {
4331                                 __accessibilityElements.GetAt(__focusOutIndex - 1, pNextAccessibilityElement);
4332                                 ScrollToFocusedTokenAccessibilityElement(*pNextAccessibilityElement);
4333                         }
4334                 }
4335         }
4336
4337         if (pPreviousAccessibilityElement && pPreviousAccessibilityElement->GetAbsoluteBounds().y < element.GetAbsoluteBounds().y)  //Right flick
4338         {
4339                 pToken = static_cast< _Token* >(__pTokenList->GetAt(__focusOutIndex + 1));
4340                 if (pToken)
4341                 {
4342                         if (pToken->displayRect.y + pToken->displayRect.height > __pTokenEdit->GetBoundsF().height)
4343                         {
4344                                 __accessibilityElements.GetAt(__focusOutIndex + 1, pNextAccessibilityElement);
4345                                 ScrollToFocusedTokenAccessibilityElement(*pNextAccessibilityElement);
4346                         }
4347                 }
4348         }
4349
4350         if (pToken == null) //Draw Cursor Accessibility Element
4351         {
4352                 _AccessibilityElement* pCursorAccessibilityElement = __pTokenEdit->GetCursorAccessibilityElement();
4353                 if (pCursorAccessibilityElement && pPreviousAccessibilityElement && pCursorAccessibilityElement != pPreviousAccessibilityElement)
4354                 {
4355                         if (GetTextBoundsF().y - pPreviousAccessibilityElement->GetBounds().y > pPreviousAccessibilityElement->GetBounds().height)  //check for different lines
4356                         {
4357                                 float height = GetTextBoundsF().height + pPreviousAccessibilityElement->GetBounds().y + pPreviousAccessibilityElement->GetBounds().height;
4358                                 if (pCurrentElement == pCursorAccessibilityElement && height > __pTokenEdit->GetBoundsF().height)
4359                                 {
4360                                         float tokenBottomMargin = 0.0f;
4361                                         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, _CONTROL_ORIENTATION_PORTRAIT, tokenBottomMargin);
4362                                         float newScrollValue = GetTextBoundsF().height + tokenBottomMargin - __scrollValue;
4363                                         RecalculateTokenBounds(newScrollValue);
4364                                         _AccessibilityManager::GetInstance()->SetGlobalFocusedElement(*(__pTokenEdit->GetCursorAccessibilityElement()));
4365                                         _AccessibilityManager::GetInstance()->RequestToDrawFocusUi();
4366                                 }
4367                         }
4368                 }
4369         }
4370
4371         __focusOutIndex = -1;
4372         UpdateTokenAccessibilityBounds();
4373
4374         return false;
4375 }
4376
4377 bool
4378 _TokenEditPresenter::OnAccessibilityFocusOut(const _AccessibilityContainer& control, const _AccessibilityElement& element)
4379 {
4380         _AccessibilityElement* pAccessibilityElement = null;
4381         pAccessibilityElement = const_cast< _AccessibilityElement* >(&element);
4382         __accessibilityElements.IndexOf(pAccessibilityElement, __focusOutIndex);
4383
4384         return false;
4385 }
4386
4387 result
4388 _TokenEditPresenter::ScrollToFocusedToken(void)
4389 {
4390         result r = E_SUCCESS;
4391         FloatRectangle focussedTokenRectangle;
4392         float newScrollValue = 0.0f;
4393         float tokenTopMargin = 0.0f;
4394         float tokenBottomMargin = 0.0f;
4395         float tokenHeight = 0.0f;
4396         float textBoundsAlignValue = 0.0f;
4397         _ControlOrientation orientation = __pTokenEdit->GetOrientation();
4398
4399
4400         GET_SHAPE_CONFIG(TOKENEDIT::TOP_MARGIN, orientation, tokenTopMargin);
4401         GET_SHAPE_CONFIG(TOKENEDIT::BOTTOM_MARGIN, orientation, tokenBottomMargin);
4402         GET_SHAPE_CONFIG(TOKENEDIT::TOKEN_HEIGHT, orientation, tokenHeight);
4403
4404         FloatRectangle tokenEditRect = __pTokenEdit->GetBoundsF();
4405         float textObjectMaxHeight = GetMaxTextHeight();
4406         textBoundsAlignValue = (tokenHeight - textObjectMaxHeight) / 2.0f;
4407
4408         if (__focusedTokenIndex == -1)
4409         {
4410                 //Focus bitmap to be reset when no token is focused.
4411                 DrawToken();
4412         }
4413         else
4414         {
4415                 _Token* pToken = null;
4416                 pToken = static_cast <_Token*>(__pTokenList->GetAt(__focusedTokenIndex));
4417
4418                 focussedTokenRectangle = pToken->displayRect;
4419
4420                 float focussedTokenPosition= focussedTokenRectangle.y + focussedTokenRectangle.height ;
4421
4422                 if ((focussedTokenRectangle.y > 0) && (focussedTokenPosition < tokenEditRect.height))
4423                 {
4424                         //Focused token is within the tokenEdit boundary
4425                         DrawToken();
4426                 }
4427                 else
4428                 {
4429                         if (focussedTokenRectangle.y < 0)
4430                         {
4431                                 //Focused token is above the upper boundary
4432                                 newScrollValue = focussedTokenRectangle.y - tokenTopMargin - __scrollValue;
4433                         }
4434                         else
4435                         {
4436                                 //Focused token is below the lower boundary
4437                                 newScrollValue = focussedTokenPosition - textBoundsAlignValue - tokenEditRect.height + tokenBottomMargin - __scrollValue;
4438                         }
4439
4440                         r = RecalculateTokenBounds(newScrollValue);
4441                         SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
4442                 }
4443         }
4444
4445         return r;
4446 }
4447
4448 void
4449 _TokenEditPresenter::SetDrawFocusState(bool focusState)
4450 {
4451         if (!focusState)
4452         {
4453                 __focusedTokenIndex = -1;
4454         }
4455         __drawFocusState = focusState;
4456
4457         return;
4458 }
4459
4460 int
4461 _TokenEditPresenter::GetFocusedTokenIndex(void) const
4462 {
4463         return __focusedTokenIndex;
4464 }
4465
4466 void
4467 _TokenEditPresenter::PrepareFocusUiMode(void)
4468 {
4469         if (__editingTokenIndex != -1)
4470         {
4471                 ExitTokenEditingMode();
4472         }
4473         else if (__pressedTokenIndex != -1)
4474         {
4475                 __pressedTokenIndex = -1;
4476                 DetachCursorFromToken();
4477                 StopCursorTimer();
4478                 SetCursorDisabled(false);
4479                 __pTokenEdit->Invalidate();
4480                 StartCursorTimer();
4481         }
4482
4483         return;
4484 }
4485
4486 }}} //Tizen::Ui::Controls