2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0/
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.
18 * @file FUiCtrl_TokenEdit.cpp
19 * @brief This is the implementation file for the _TokenEdit class.
22 #include <FBaseSysLog.h>
23 #include "FUiCtrl_TokenEdit.h"
24 #include "FUiCtrl_TokenEditPresenter.h"
26 #include "FUi_AccessibilityContainer.h"
27 #include "FUi_AccessibilityElement.h"
29 using namespace Tizen::Base;
30 using namespace Tizen::Graphics;
31 using namespace Tizen::Ui;
33 namespace Tizen { namespace Ui { namespace Controls
36 IMPLEMENT_PROPERTY(_TokenEdit);
38 _TokenEdit::_TokenEdit(void)
40 , __pTokenEditPresenter(null)
41 , __isSelectedTokenTextColorSet(false)
42 , __pTokenFilter(null)
43 , __pTitleTextAccessibilityElement(null)
44 , __pCursorAccessibilityElement(null)
46 GET_COLOR_CONFIG(TOKENEDIT::BG_NORMAL, __tokenColor[TOKEN_EDIT_STATUS_NORMAL]);
47 GET_COLOR_CONFIG(TOKENEDIT::BG_SELECTED, __tokenColor[TOKEN_EDIT_STATUS_SELECTED]);
48 GET_COLOR_CONFIG(TOKENEDIT::BG_HIGHLIGHTED, __tokenColor[TOKEN_EDIT_STATUS_HIGHLIGHTED]);
49 GET_COLOR_CONFIG(TOKENEDIT::BG_DISABLED, __tokenColor[TOKEN_EDIT_STATUS_DISABLED]);
50 __tokenColor[TOKEN_EDIT_STATUS_DISABLED].SetAlpha(TOKEN_EDIT_BG_DISABLED_OPACITY); //apply dimming
51 GET_COLOR_CONFIG(TOKENEDIT::TEXT_NORMAL, __tokenTextColor[EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL]);
52 GET_COLOR_CONFIG(TOKENEDIT::TEXT_SELECTED, __tokenTextColor[EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED]);
54 _AccessibilityContainer* pContainer = null;
55 pContainer = GetAccessibilityContainer();
58 pContainer->Activate(true);
62 _TokenEdit::~_TokenEdit(void)
68 _TokenEdit::CreateTokenEditN(void)
70 _TokenEditPresenter* pPresenter = _TokenEditPresenter::CreateTokenEditPresenterN();
71 result r = GetLastResult();
72 SysTryReturn(NID_UI_CTRL, pPresenter != null, null, r, "[%s] Propagating.", GetErrorMessage(r));
74 _TokenEdit* pTokenEdit = new (std::nothrow) _TokenEdit();
75 SysTryCatch(NID_UI_CTRL, pTokenEdit != null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
76 SysTryCatch(NID_UI_CTRL, GetLastResult() == E_SUCCESS, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
78 pTokenEdit->AcquireHandle();
80 pTokenEdit->SetPresenter(*pPresenter);
81 pTokenEdit->__pTokenEditPresenter = pPresenter;
92 _TokenEdit::OnDraw(void)
96 if (__pTokenEditPresenter == null)
101 Canvas* pCanvas = GetCanvasN();
102 SysTryReturnVoidResult(NID_UI_CTRL, pCanvas, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
104 r = __pTokenEditPresenter->Draw(*pCanvas);
105 SysTryReturnVoidResult(NID_UI_CTRL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
111 _TokenEdit::SetTokenFilter(const _ITokenFilter* pFilter)
113 result r = E_SUCCESS;
115 __pTokenFilter = const_cast< _ITokenFilter* >(pFilter);
121 _TokenEdit::GetTokenFilter(void) const
123 return __pTokenFilter;
127 _TokenEdit::AppendToken(const String& token)
129 result r = E_SUCCESS;
131 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
133 r = __pTokenEditPresenter->AppendToken(token);
134 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
140 _TokenEdit::InsertTokenAt(int index, const String& token)
142 result r = E_SUCCESS;
144 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
146 r = __pTokenEditPresenter->InsertTokenAt(index, token, true);
147 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
153 _TokenEdit::GetTokenAt(int index) const
155 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
157 return __pTokenEditPresenter->GetTokenAt(index);
161 _TokenEdit::GetTokenCount(void) const
163 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
165 return __pTokenEditPresenter->GetTokenCount(true);
169 _TokenEdit::GetSelectedTokenIndex(void) const
171 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
173 return __pTokenEditPresenter->GetSelectedTokenIndex();
177 _TokenEdit::IsTokenEditModeEnabled(void) const
179 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
181 Variant var = GetProperty("tokenEditModeEnabled");
187 _TokenEdit::RemoveTokenAt(int index)
189 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
191 return __pTokenEditPresenter->RemoveTokenAt(index, true);
195 _TokenEdit::SetTokenSelected(int index, bool selected)
197 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
199 return __pTokenEditPresenter->SetTokenSelected(index, selected);
203 _TokenEdit::SetTokenEditModeEnabled(bool enable)
205 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
209 return SetProperty("tokenEditModeEnabled", var);
213 _TokenEdit::GetTokenColor(TokenEditStatus status) const
219 case TOKEN_EDIT_STATUS_NORMAL:
220 var = GetProperty("normalTokenColor");
223 case TOKEN_EDIT_STATUS_SELECTED:
224 var = GetProperty("selectedTokenColor");
227 case TOKEN_EDIT_STATUS_HIGHLIGHTED:
228 var = GetProperty("highlightedTokenColor");
231 case TOKEN_EDIT_STATUS_DISABLED:
232 var = GetProperty("disabledTokenColor");
239 return var.ToColor();
243 _TokenEdit::GetTokenTextColor(void) const
245 Variant var = GetProperty("tokenTextColor");
247 return var.ToColor();
251 _TokenEdit::SetTokenColor(TokenEditStatus status, const Color& color)
253 result r = E_SUCCESS;
258 case TOKEN_EDIT_STATUS_NORMAL:
259 r = SetProperty("normalTokenColor", var);
262 case TOKEN_EDIT_STATUS_SELECTED:
263 r = SetProperty("selectedTokenColor", var);
266 case TOKEN_EDIT_STATUS_HIGHLIGHTED:
267 r = SetProperty("highlightedTokenColor", var);
270 case TOKEN_EDIT_STATUS_DISABLED:
271 r = SetProperty("disabledTokenColor", var);
281 _TokenEdit::SetTokenTextColor(const Color& color)
284 return SetProperty("tokenTextColor", var);
288 _TokenEdit::GetSelectedTokenTextColor(void) const
290 Variant var = GetProperty("selectedTokenTextColor");
292 return var.ToColor();
296 _TokenEdit::SetSelectedTokenTextColor(const Color& color)
300 return SetProperty("selectedTokenTextColor", var);
304 _TokenEdit::SetTitleText(const String& title)
306 result r = E_SUCCESS;
308 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
310 __pTokenEditPresenter->SetDescriptionText(title);
312 r = __pTokenEditPresenter->CalculateDescriptionTextRect(title);
313 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
315 r = __pTokenEditPresenter->CalculateTokenPositionFromIndex(0);
316 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
318 r = __pTokenEditPresenter->SetInitialBounds();
319 SysTryReturnResult(NID_UI_CTRL, r == E_SUCCESS, r, "Propagating.");
326 _TokenEdit::GetTitleText(void) const
328 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
330 return __pTokenEditPresenter->GetDescriptionText();
334 _TokenEdit::SetAutoShrinkModeEnabled(bool enable)
336 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
338 __pTokenEditPresenter->SetAutoShrinkModeEnabled(enable);
342 _TokenEdit::IsAutoShrinkModeEnabled(void) const
344 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
346 return __pTokenEditPresenter->IsAutoShrinkModeEnabled();
350 _TokenEdit::ProcessTokenFiltering(const String& inputString, String& replaceString, bool& enable)
354 enable = __pTokenFilter->ReplaceToken(inputString, replaceString);
361 _TokenEdit::OnTouchPressed(const _Control& source, const _TouchInfo& touchinfo)
368 if (__pTokenEditPresenter == null)
372 return __pTokenEditPresenter->OnTouchPressed(source, touchinfo);
376 _TokenEdit::OnTouchReleased(const _Control& source, const _TouchInfo& touchinfo)
382 if (__pTokenEditPresenter == null)
386 return __pTokenEditPresenter->OnTouchReleased(source, touchinfo);
390 _TokenEdit::OnTouchMoved(const _Control& source, const _TouchInfo& touchinfo)
396 if (__pTokenEditPresenter == null)
400 return __pTokenEditPresenter->OnTouchMoved(source, touchinfo);
404 _TokenEdit::SetPropertyNormalTokenColor(const Variant& color)
408 __tokenColor[TOKEN_EDIT_STATUS_NORMAL] = color.ToColor();
414 _TokenEdit::GetPropertyNormalTokenColor(void) const
418 return Variant(__tokenColor[TOKEN_EDIT_STATUS_NORMAL]);
422 _TokenEdit::SetPropertySelectedTokenColor(const Variant& color)
426 __tokenColor[TOKEN_EDIT_STATUS_SELECTED] = color.ToColor();
432 _TokenEdit::GetPropertySelectedTokenColor(void) const
436 return Variant(__tokenColor[TOKEN_EDIT_STATUS_SELECTED]);
440 _TokenEdit::SetPropertyHighlightedTokenColor(const Variant& color)
444 __tokenColor[TOKEN_EDIT_STATUS_HIGHLIGHTED] = color.ToColor();
450 _TokenEdit::GetPropertyHighlightedTokenColor(void) const
454 return Variant(__tokenColor[TOKEN_EDIT_STATUS_HIGHLIGHTED]);
458 _TokenEdit::SetPropertyDisabledTokenColor(const Variant& color)
462 __tokenColor[TOKEN_EDIT_STATUS_DISABLED] = color.ToColor();
468 _TokenEdit::GetPropertyDisabledTokenColor(void) const
472 return Variant(__tokenColor[TOKEN_EDIT_STATUS_DISABLED]);
476 _TokenEdit::SetPropertyTokenTextColor(const Variant& color)
480 __tokenTextColor[EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL] = color.ToColor();
481 if (!__isSelectedTokenTextColorSet)
483 __tokenTextColor[EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED] = color.ToColor();
490 _TokenEdit::GetPropertyTokenTextColor(void) const
494 return Variant(__tokenTextColor[EXPANDABLE_EDIT_AREA_TOKEN_STATUS_NORMAL]);
498 _TokenEdit::SetPropertySelectedTokenTextColor(const Variant& color)
502 __isSelectedTokenTextColorSet = true;
504 __tokenTextColor[EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED] = color.ToColor();
510 _TokenEdit::GetPropertySelectedTokenTextColor(void) const
514 return Variant(__tokenTextColor[EXPANDABLE_EDIT_AREA_TOKEN_STATUS_SELECTED]);
518 _TokenEdit::SetPropertyTokenEditModeEnabled(const Variant& enable)
520 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
524 return __pTokenEditPresenter->SetTokenEditModeEnabled(enable.ToBool());
528 _TokenEdit::GetPropertyTokenEditModeEnabled(void) const
530 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
534 bool enabled = __pTokenEditPresenter->IsTokenEditModeEnabled();
536 return Variant(enabled);
540 _TokenEdit::OnAttachedToMainTree(void)
542 _AccessibilityContainer* pContainer = null;
543 pContainer = GetAccessibilityContainer();
546 if (__pTitleTextAccessibilityElement)
550 pContainer->AddListener(*this);
551 UpdateAccessibilityElement(EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT);
558 _TokenEdit::OnBoundsChanged(void)
560 SysAssertf(__pTokenEditPresenter != null, "_TokenEditPresenter instance is null");
562 __pTokenEditPresenter->OnBoundsChanged();
568 _TokenEdit::OnAccessibilityFocusIn(const _AccessibilityContainer& control, const _AccessibilityElement& element)
570 return __pTokenEditPresenter->OnAccessibilityFocusIn(control, element);
574 _TokenEdit::OnAccessibilityFocusOut(const _AccessibilityContainer& control, const _AccessibilityElement& element)
576 return __pTokenEditPresenter->OnAccessibilityFocusOut(control, element);
580 _TokenEdit::OnAccessibilityActionPerformed(const _AccessibilityContainer& control, const _AccessibilityElement& element)
582 return __pTokenEditPresenter->OnAccessibilityActionPerformed(control, element);
586 _TokenEdit::UpdateAccessibilityElement(EditAccessibilityElementType type)
588 _AccessibilityElement* pTextAccessibilityElement = _Edit::GetTextAccessibilityElement();
589 String spaceString(L" ");
592 case EDIT_ACCESSIBILITY_ELEMENT_TYPE_TEXT:
593 if (pTextAccessibilityElement)
595 pTextAccessibilityElement->SetBounds(FloatRectangle(0.0f, 0.0f, GetBoundsF().width, GetBoundsF().height));
598 pTextAccessibilityElement->SetLabel(GetTitleText() + spaceString + __pTokenEditPresenter->GetTextAccessibilityElementText());
602 pTextAccessibilityElement->SetLabel(GetTitleText() + spaceString + GetGuideText() + GetText());
604 pTextAccessibilityElement->SetHint(L"double tap to edit");
615 _AccessibilityElement*
616 _TokenEdit::GetTitleTextAccessibilityElement(void) const
618 return __pTitleTextAccessibilityElement;
621 _AccessibilityElement*
622 _TokenEdit::GetCursorAccessibilityElement(void) const
624 return __pCursorAccessibilityElement;
628 _TokenEdit::AddTitleAccessibilityElement(void)
630 result r = E_SUCCESS;
632 if (!(GetEditStyle() & EDIT_STYLE_TITLE_LEFT))
637 if (__pTitleTextAccessibilityElement)
642 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
646 _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
647 SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
649 String labelText = GetTitleText();
650 pAccessibilityElement->SetBounds(__pTokenEditPresenter->GetDescriptionTextRect());
651 pAccessibilityElement->SetLabel(labelText);
652 pContainer->AddElement(*pAccessibilityElement);
653 __pTitleTextAccessibilityElement = pAccessibilityElement;
660 _TokenEdit::AddCursorAccessibilityElement(void)
662 result r = E_SUCCESS;
664 if (__pCursorAccessibilityElement)
669 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
673 _AccessibilityElement* pAccessibilityElement = new (std::nothrow) _AccessibilityElement(true);
674 SysTryReturnResult(NID_UI_CTRL, pAccessibilityElement != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
678 pAccessibilityElement->SetLabel(GetText());
682 pAccessibilityElement->SetTrait(L"Edit field");
685 pContainer->AddElement(*pAccessibilityElement);
687 __pCursorAccessibilityElement = pAccessibilityElement;
689 SetCursorAccessibilityBounds(__pTokenEditPresenter->GetTextBoundsF());
696 _TokenEdit::SetCursorAccessibilityBounds(const FloatRectangle& cursorBounds)
698 if (!__pCursorAccessibilityElement)
702 __pCursorAccessibilityElement->SetBounds(cursorBounds);
708 _TokenEdit::RemoveTitleAccessibilityElement(void)
710 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
713 if (__pTitleTextAccessibilityElement)
715 pContainer->RemoveElement(*__pTitleTextAccessibilityElement);
716 __pTitleTextAccessibilityElement = null;
722 _TokenEdit::RemoveCursorAccessibilityElement(void)
724 _AccessibilityContainer* pContainer = GetAccessibilityContainer();
727 if (__pCursorAccessibilityElement)
729 pContainer->RemoveElement(*__pCursorAccessibilityElement);
730 __pCursorAccessibilityElement = null;
736 _TokenEdit::OnDrawFocus(void)
738 //Exit Token editing/pressed mode
739 __pTokenEditPresenter->PrepareFocusUiMode();
741 __pTokenEditPresenter->SetDrawFocusState(true);
749 _TokenEdit::OnFocusModeStateChanged(void)
751 //Reset focus index here
752 __pTokenEditPresenter->SetDrawFocusState(false);
758 _TokenEdit::RefreshFocusUi(void)
760 int focusedTokenIndex = __pTokenEditPresenter->GetFocusedTokenIndex();
762 if (focusedTokenIndex == -1)
764 _Control::OnDrawFocus();
768 _Control::OnFocusLost(*this);
773 }}} //Tizen::Ui::Controls