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