[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / hidden-text.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali-toolkit/internal/text/hidden-text.h>
20
21 // INTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/text-controls/text-editor.h>
23
24 using namespace Dali::Toolkit;
25
26 const int      DEFAULT_SHOW_DURATION = 1000;
27
28 namespace Dali
29 {
30 namespace Toolkit
31 {
32 namespace Text
33 {
34 const char* const PROPERTY_MODE                 = "mode";
35 const char* const PROPERTY_SUBSTITUTE_CHARACTER = "substituteCharacter";
36 const char* const PROPERTY_SUBSTITUTE_COUNT     = "substituteCount";
37 const char* const PROPERTY_SHOW_DURATION        = "showDuration";
38
39 HiddenText::HiddenText(Observer* observer)
40 : mObserver(observer),
41   mHideMode(static_cast<int>(Toolkit::HiddenInput::Mode::HIDE_NONE)),
42   mSubstituteText(STAR),
43   mDisplayDuration(DEFAULT_SHOW_DURATION),
44   mSubstituteCount(0),
45   mPreviousTextCount(0u),
46   mIsLastCharacterShow(false)
47 {
48   mTimer = Timer::New(mDisplayDuration);
49   mTimer.TickSignal().Connect(this, &HiddenText::OnTick);
50 }
51
52 void HiddenText::SetProperties(const Property::Map& map)
53 {
54   const Property::Map::SizeType count = map.Count();
55
56   for(Property::Map::SizeType position = 0; position < count; ++position)
57   {
58     KeyValuePair     keyValue = map.GetKeyValue(position);
59     Property::Key&   key      = keyValue.first;
60     Property::Value& value    = keyValue.second;
61
62     if(key == Toolkit::HiddenInput::Property::MODE || key == PROPERTY_MODE)
63     {
64       value.Get(mHideMode);
65     }
66     else if(key == Toolkit::HiddenInput::Property::SUBSTITUTE_CHARACTER || key == PROPERTY_SUBSTITUTE_CHARACTER)
67     {
68       value.Get(mSubstituteText);
69     }
70     else if(key == Toolkit::HiddenInput::Property::SUBSTITUTE_COUNT || key == PROPERTY_SUBSTITUTE_COUNT)
71     {
72       value.Get(mSubstituteCount);
73     }
74     else if(key == Toolkit::HiddenInput::Property::SHOW_LAST_CHARACTER_DURATION || key == PROPERTY_SHOW_DURATION)
75     {
76       value.Get(mDisplayDuration);
77     }
78   }
79 }
80
81 void HiddenText::GetProperties(Property::Map& map)
82 {
83   map[Toolkit::HiddenInput::Property::MODE]                         = mHideMode;
84   map[Toolkit::HiddenInput::Property::SUBSTITUTE_CHARACTER]         = mSubstituteText;
85   map[Toolkit::HiddenInput::Property::SUBSTITUTE_COUNT]             = mSubstituteCount;
86   map[Toolkit::HiddenInput::Property::SHOW_LAST_CHARACTER_DURATION] = mDisplayDuration;
87 }
88
89 void HiddenText::Substitute(const Vector<Character>& source, Vector<Character>& destination, Length cursorPos)
90 {
91   const Length characterCount = source.Count();
92
93   destination.Resize(characterCount);
94
95   uint32_t* begin     = destination.Begin();
96   uint32_t* end       = begin + characterCount;
97   uint32_t* hideStart = NULL;
98   uint32_t* hideEnd   = NULL;
99   uint32_t* sourcePos = source.Begin();
100
101   switch(mHideMode)
102   {
103     case Toolkit::HiddenInput::Mode::HIDE_NONE:
104     {
105       hideStart = NULL;
106       hideEnd   = NULL;
107       break;
108     }
109     case Toolkit::HiddenInput::Mode::HIDE_ALL:
110     {
111       hideStart = begin;
112       hideEnd   = end;
113       break;
114     }
115     case Toolkit::HiddenInput::Mode::HIDE_COUNT:
116     {
117       hideStart = begin;
118       hideEnd   = begin + mSubstituteCount;
119       break;
120     }
121     case Toolkit::HiddenInput::Mode::SHOW_COUNT:
122     {
123       hideStart = begin + mSubstituteCount;
124       hideEnd   = end;
125       break;
126     }
127     case Toolkit::HiddenInput::Mode::SHOW_LAST_CHARACTER:
128     {
129       hideStart = begin;
130       hideEnd   = end;
131       if(mPreviousTextCount < characterCount)
132       {
133         if(mDisplayDuration > 0)
134         {
135           mTimer.SetInterval(mDisplayDuration);
136           mTimer.Start();
137           mIsLastCharacterShow = true;
138         }
139         else
140         {
141           OnTick();
142         }
143       }
144       else
145       {
146         mIsLastCharacterShow = false;
147       }
148       break;
149     }
150   }
151
152
153   if(mHideMode == Toolkit::HiddenInput::Mode::SHOW_LAST_CHARACTER)
154   {
155     Length currentPos = 0u;
156     for(; begin < end; ++begin)
157     {
158       if(begin >= hideStart && begin < hideEnd && cursorPos > 0u && currentPos != cursorPos - 1u)
159       {
160         *begin = static_cast<uint32_t>(mSubstituteText);
161       }
162       else
163       {
164         *begin = mIsLastCharacterShow ? *sourcePos : static_cast<uint32_t>(mSubstituteText);
165       }
166       sourcePos++;
167       currentPos++;
168     }
169   }
170   else
171   {
172     for(; begin < end; ++begin)
173     {
174       if(begin >= hideStart && begin < hideEnd)
175       {
176         *begin = static_cast<uint32_t>(mSubstituteText);
177         sourcePos++;
178       }
179       else
180       {
181         *begin = *sourcePos++;
182       }
183     }
184   }
185   mPreviousTextCount = characterCount;
186 }
187
188 void HiddenText::InitPreviousTextCount()
189 {
190   mPreviousTextCount = 0u;
191 }
192
193 int HiddenText::GetHideMode()
194 {
195   return mHideMode;
196 }
197
198 bool HiddenText::OnTick()
199 {
200   if(mObserver != NULL)
201   {
202     mObserver->DisplayTimeExpired();
203   }
204   mIsLastCharacterShow = false;
205   return false;
206 }
207
208 } // namespace Text
209
210 } // namespace Toolkit
211
212 } // namespace Dali