Add property to hide input text in textField
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / hidden-text.cpp
1 /*
2  * Copyright (c) 2017 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/devel-api/controls/text-controls/text-editor-devel.h>
23
24 using namespace Dali::Toolkit;
25
26 const uint32_t STAR = 0x2A;   // Set default substitute character as '*'
27 const int DEFAULT_SHOW_DURATION = 1000;
28
29 namespace Dali
30 {
31
32 namespace Toolkit
33 {
34
35 namespace Text
36 {
37
38 const char * const PROPERTY_MODE                      = "mode";
39 const char * const PROPERTY_SUBSTITUTE_CHARACTER      = "substituteCharacter";
40 const char * const PROPERTY_SUBSTITUTE_COUNT          = "substituteCount";
41 const char * const PROPERTY_SHOW_DURATION             = "showDuration";
42
43 HiddenText::HiddenText( Observer* observer )
44 : mObserver( observer ),
45   mHideMode( static_cast< int >( Toolkit::HiddenInput::Mode::HIDE_NONE ) ),
46   mSubstituteText( STAR ),
47   mDisplayDuration( DEFAULT_SHOW_DURATION ),
48   mSubstituteCount( 0 ),
49   mPreviousTextCount( 0 )
50 {
51   mTimer = Timer::New( mDisplayDuration );
52   mTimer.TickSignal().Connect( this, &HiddenText::OnTick );
53 }
54
55 void HiddenText::SetProperties( const Property::Map& map )
56 {
57     const Property::Map::SizeType count = map.Count();
58
59     for ( Property::Map::SizeType position = 0; position < count; ++position )
60     {
61       KeyValuePair keyValue = map.GetKeyValue( position );
62       Property::Key& key = keyValue.first;
63       Property::Value& value = keyValue.second;
64
65       if( key == Toolkit::HiddenInput::Property::MODE || key == PROPERTY_MODE )
66       {
67         value.Get( mHideMode );
68       }
69       else if( key == Toolkit::HiddenInput::Property::SUBSTITUTE_CHARACTER || key == PROPERTY_SUBSTITUTE_CHARACTER )
70       {
71         value.Get( mSubstituteText );
72       }
73       else if( key == Toolkit::HiddenInput::Property::SUBSTITUTE_COUNT || key == PROPERTY_SUBSTITUTE_COUNT )
74       {
75         value.Get( mSubstituteCount );
76       }
77       else if( key == Toolkit::HiddenInput::Property::SHOW_DURATION || key == PROPERTY_SHOW_DURATION )
78       {
79         value.Get( mDisplayDuration );
80       }
81     }
82 }
83
84 void HiddenText::GetProperties( Property::Map& map )
85 {
86   map[Toolkit::HiddenInput::Property::MODE] = mHideMode;
87   map[Toolkit::HiddenInput::Property::SUBSTITUTE_CHARACTER] = mSubstituteText;
88   map[Toolkit::HiddenInput::Property::SUBSTITUTE_COUNT] = mSubstituteCount;
89   map[Toolkit::HiddenInput::Property::SHOW_DURATION] = mDisplayDuration;
90 }
91
92 void HiddenText::Substitute( const Vector<Character>& source, Vector<Character>& destination )
93 {
94   const Length characterCount = source.Count();
95
96   destination.Resize( characterCount );
97
98   uint32_t* begin = destination.Begin();
99   uint32_t* end = begin + characterCount;
100   uint32_t* hideStart = NULL;
101   uint32_t* hideEnd = NULL;
102   uint32_t* sourcePos = source.Begin();
103
104   switch ( mHideMode )
105   {
106     case Toolkit::HiddenInput::Mode::HIDE_NONE:
107     {
108       hideStart = NULL;
109       hideEnd = NULL;
110       break;
111     }
112     case Toolkit::HiddenInput::Mode::HIDE_ALL:
113     {
114       hideStart = begin;
115       hideEnd = end;
116       break;
117     }
118     case Toolkit::HiddenInput::Mode::HIDE_COUNT:
119     {
120       hideStart = begin;
121       hideEnd = begin + mSubstituteCount;
122       break;
123     }
124     case Toolkit::HiddenInput::Mode::SHOW_COUNT:
125     {
126       hideStart = begin + mSubstituteCount;
127       hideEnd = end;
128       break;
129     }
130     case Toolkit::HiddenInput::Mode::SHOW_LAST_CHARACTER:
131     {
132       if (mPreviousTextCount < characterCount)
133       {
134         hideStart = begin;
135         hideEnd = end-1;
136         if ( mDisplayDuration > 0 )
137         {
138           mTimer.SetInterval( mDisplayDuration );
139           mTimer.Start();
140         }
141         else
142         {
143           OnTick();
144         }
145       }
146       else
147       {
148         hideStart = begin;
149         hideEnd = end;
150       }
151       break;
152     }
153   }
154   for (; begin < end; ++begin )
155   {
156     if (begin >= hideStart && begin < hideEnd)
157     {
158       *begin = static_cast<uint32_t>(mSubstituteText);
159       sourcePos++;
160     }
161     else
162     {
163       *begin = *sourcePos++;
164     }
165   }
166   mPreviousTextCount = characterCount;
167 }
168
169 bool HiddenText::OnTick()
170 {
171   if( mObserver != NULL )
172   {
173     mObserver->DisplayTimeExpired();
174   }
175
176   return false;
177 }
178
179 } // namespace Text
180
181 } // namespace Toolkit
182
183 } // namespace Dali