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