[AT-SPI] Squashed implementation
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / text / text-controller-placeholder-handler.cpp
1 /*
2  * Copyright (c) 2020 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/text-controller-placeholder-handler.h>
20
21 // EXTERNAL INCLUDES
22 #include <dali/integration-api/debug.h>
23
24 // INTERNAL INCLUDES
25 #include <dali-toolkit/public-api/controls/text-controls/placeholder-properties.h>
26 #include <dali-toolkit/internal/text/text-controller-impl.h>
27 #include <dali-toolkit/internal/text/text-font-style.h>
28
29 namespace
30 {
31
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
34 #endif
35
36 const std::string EMPTY_STRING("");
37
38 const char * const PLACEHOLDER_TEXT = "text";
39 const char * const PLACEHOLDER_TEXT_FOCUSED = "textFocused";
40 const char * const PLACEHOLDER_COLOR = "color";
41 const char * const PLACEHOLDER_FONT_FAMILY = "fontFamily";
42 const char * const PLACEHOLDER_FONT_STYLE = "fontStyle";
43 const char * const PLACEHOLDER_POINT_SIZE = "pointSize";
44 const char * const PLACEHOLDER_PIXEL_SIZE = "pixelSize";
45 const char * const PLACEHOLDER_ELLIPSIS = "ellipsis";
46
47 } // namespace
48
49 namespace Dali
50 {
51
52 namespace Toolkit
53 {
54
55 namespace Text
56 {
57
58 void Controller::PlaceholderHandler::SetPlaceholderTextElideEnabled(Controller& controller, bool enabled)
59 {
60   controller.mImpl->mEventData->mIsPlaceholderElideEnabled = enabled;
61   controller.mImpl->mEventData->mPlaceholderEllipsisFlag = true;
62
63   // Update placeholder if there is no text
64   if( controller.mImpl->IsShowingPlaceholderText() ||
65       ( 0u == controller.mImpl->mModel->mLogicalModel->mText.Count() ) )
66   {
67     controller.ShowPlaceholderText();
68   }
69 }
70
71 bool Controller::PlaceholderHandler::IsPlaceholderTextElideEnabled(const Controller& controller)
72 {
73   return controller.mImpl->mEventData->mIsPlaceholderElideEnabled;
74 }
75
76 void Controller::PlaceholderHandler::SetPlaceholderText(Controller& controller, PlaceholderType type, const std::string& text)
77 {
78   if( NULL != controller.mImpl->mEventData )
79   {
80     if( PLACEHOLDER_TYPE_INACTIVE == type )
81     {
82       controller.mImpl->mEventData->mPlaceholderTextInactive = text;
83     }
84     else
85     {
86       controller.mImpl->mEventData->mPlaceholderTextActive = text;
87     }
88
89     // Update placeholder if there is no text
90     if( controller.mImpl->IsShowingPlaceholderText() ||
91         ( 0u == controller.mImpl->mModel->mLogicalModel->mText.Count() ) )
92     {
93       controller.ShowPlaceholderText();
94     }
95   }
96 }
97
98 void Controller::PlaceholderHandler::GetPlaceholderText(const Controller& controller, PlaceholderType type, std::string& text)
99 {
100   if( NULL != controller.mImpl->mEventData )
101   {
102     if( PLACEHOLDER_TYPE_INACTIVE == type )
103     {
104       text = controller.mImpl->mEventData->mPlaceholderTextInactive;
105     }
106     else
107     {
108       text = controller.mImpl->mEventData->mPlaceholderTextActive;
109     }
110   }
111 }
112
113 void Controller::PlaceholderHandler::SetPlaceholderFontFamily(Controller& controller, const std::string& placeholderTextFontFamily)
114 {
115   if( NULL != controller.mImpl->mEventData )
116   {
117     if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
118     {
119       controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
120     }
121
122     controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.family = placeholderTextFontFamily;
123     DALI_LOG_INFO( gLogFilter, Debug::General, "Controller::SetPlaceholderFontFamily %s\n", placeholderTextFontFamily.c_str());
124     controller.mImpl->mEventData->mPlaceholderFont->familyDefined = !placeholderTextFontFamily.empty();
125
126     controller.mImpl->RequestRelayout();
127   }
128 }
129
130 const std::string& Controller::PlaceholderHandler::GetPlaceholderFontFamily(const Controller& controller)
131 {
132   if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
133   {
134     return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.family;
135   }
136
137   return EMPTY_STRING;
138 }
139
140 void Controller::PlaceholderHandler::SetPlaceholderTextFontWeight(Controller& controller, FontWeight weight)
141 {
142   if( NULL != controller.mImpl->mEventData )
143   {
144     if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
145     {
146       controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
147     }
148
149     controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.weight = weight;
150     controller.mImpl->mEventData->mPlaceholderFont->weightDefined = true;
151
152     controller.mImpl->RequestRelayout();
153   }
154 }
155
156 bool Controller::PlaceholderHandler::IsPlaceholderTextFontWeightDefined(const Controller& controller)
157 {
158   if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
159   {
160     return controller.mImpl->mEventData->mPlaceholderFont->weightDefined;
161   }
162   return false;
163 }
164
165 FontWeight Controller::PlaceholderHandler::GetPlaceholderTextFontWeight(const Controller& controller)
166 {
167   if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
168   {
169     return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.weight;
170   }
171
172   return TextAbstraction::FontWeight::NORMAL;
173 }
174
175 void Controller::PlaceholderHandler::SetPlaceholderTextFontWidth(Controller& controller, FontWidth width)
176 {
177   if( NULL != controller.mImpl->mEventData )
178   {
179     if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
180     {
181       controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
182     }
183
184     controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.width = width;
185     controller.mImpl->mEventData->mPlaceholderFont->widthDefined = true;
186
187     controller.mImpl->RequestRelayout();
188   }
189 }
190
191 bool Controller::PlaceholderHandler::IsPlaceholderTextFontWidthDefined(const Controller& controller)
192 {
193   if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
194   {
195     return controller.mImpl->mEventData->mPlaceholderFont->widthDefined;
196   }
197   return false;
198 }
199
200 FontWidth Controller::PlaceholderHandler::GetPlaceholderTextFontWidth(const Controller& controller)
201 {
202   if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
203   {
204     return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.width;
205   }
206
207   return TextAbstraction::FontWidth::NORMAL;
208 }
209
210 void Controller::PlaceholderHandler::SetPlaceholderTextFontSlant(Controller& controller, FontSlant slant)
211 {
212   if( NULL != controller.mImpl->mEventData )
213   {
214     if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
215     {
216       controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
217     }
218
219     controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.slant = slant;
220     controller.mImpl->mEventData->mPlaceholderFont->slantDefined = true;
221
222     controller.mImpl->RequestRelayout();
223   }
224 }
225
226 bool Controller::PlaceholderHandler::IsPlaceholderTextFontSlantDefined(const Controller& controller)
227 {
228   if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
229   {
230     return controller.mImpl->mEventData->mPlaceholderFont->slantDefined;
231   }
232   return false;
233 }
234
235 FontSlant Controller::PlaceholderHandler::GetPlaceholderTextFontSlant(const Controller& controller)
236 {
237   if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
238   {
239     return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.slant;
240   }
241
242   return TextAbstraction::FontSlant::NORMAL;
243 }
244
245 void Controller::PlaceholderHandler::SetPlaceholderTextFontSize(Controller& controller, float fontSize, FontSizeType type)
246 {
247   if( NULL != controller.mImpl->mEventData )
248   {
249     if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
250     {
251       controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
252     }
253
254     switch( type )
255     {
256       case POINT_SIZE:
257       {
258         controller.mImpl->mEventData->mPlaceholderFont->mDefaultPointSize = fontSize;
259         controller.mImpl->mEventData->mPlaceholderFont->sizeDefined = true;
260         controller.mImpl->mEventData->mIsPlaceholderPixelSize = false; // Font size flag
261         break;
262       }
263       case PIXEL_SIZE:
264       {
265         // Point size = Pixel size * 72.f / DPI
266         unsigned int horizontalDpi = 0u;
267         unsigned int verticalDpi = 0u;
268         TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
269         fontClient.GetDpi( horizontalDpi, verticalDpi );
270
271         controller.mImpl->mEventData->mPlaceholderFont->mDefaultPointSize = ( fontSize * 72.f ) / static_cast< float >( horizontalDpi );
272         controller.mImpl->mEventData->mPlaceholderFont->sizeDefined = true;
273         controller.mImpl->mEventData->mIsPlaceholderPixelSize = true; // Font size flag
274         break;
275       }
276     }
277
278     controller.mImpl->RequestRelayout();
279   }
280 }
281
282 float Controller::PlaceholderHandler::GetPlaceholderTextFontSize(const Controller& controller, FontSizeType type )
283 {
284   float value = 0.0f;
285   if( NULL != controller.mImpl->mEventData )
286   {
287     switch( type )
288     {
289       case POINT_SIZE:
290       {
291         if( NULL != controller.mImpl->mEventData->mPlaceholderFont )
292         {
293           value = controller.mImpl->mEventData->mPlaceholderFont->mDefaultPointSize;
294         }
295         else
296         {
297           // If the placeholder text font size is not set, then return the default font size.
298           value = controller.GetDefaultFontSize( POINT_SIZE );
299         }
300         break;
301       }
302       case PIXEL_SIZE:
303       {
304         if( NULL != controller.mImpl->mEventData->mPlaceholderFont )
305         {
306           // Pixel size = Point size * DPI / 72.f
307           unsigned int horizontalDpi = 0u;
308           unsigned int verticalDpi = 0u;
309           TextAbstraction::FontClient fontClient = TextAbstraction::FontClient::Get();
310           fontClient.GetDpi( horizontalDpi, verticalDpi );
311
312           value = controller.mImpl->mEventData->mPlaceholderFont->mDefaultPointSize * static_cast< float >( horizontalDpi ) / 72.f;
313         }
314         else
315         {
316           // If the placeholder text font size is not set, then return the default font size.
317           value = controller.GetDefaultFontSize( PIXEL_SIZE );
318         }
319         break;
320       }
321     }
322     return value;
323   }
324
325   return value;
326 }
327
328 void Controller::PlaceholderHandler::SetPlaceholderTextColor(Controller& controller, const Vector4& textColor )
329 {
330   if( NULL != controller.mImpl->mEventData )
331   {
332     controller.mImpl->mEventData->mPlaceholderTextColor = textColor;
333   }
334
335   if( controller.mImpl->IsShowingPlaceholderText() )
336   {
337     controller.mImpl->mModel->mVisualModel->SetTextColor( textColor );
338     controller.mImpl->RequestRelayout();
339   }
340 }
341
342 const Vector4& Controller::PlaceholderHandler::GetPlaceholderTextColor(const Controller& controller)
343 {
344   if( NULL != controller.mImpl->mEventData )
345   {
346     return controller.mImpl->mEventData->mPlaceholderTextColor;
347   }
348
349   return Color::BLACK;
350 }
351
352 void Controller::PlaceholderHandler::SetPlaceholderProperty(Controller& controller, const Property::Map& map )
353 {
354   const Property::Map::SizeType count = map.Count();
355
356   for( Property::Map::SizeType position = 0; position < count; ++position )
357   {
358     KeyValuePair keyValue = map.GetKeyValue( position );
359     Property::Key& key = keyValue.first;
360     Property::Value& value = keyValue.second;
361
362     if( key == Toolkit::Text::PlaceHolder::Property::TEXT  || key == PLACEHOLDER_TEXT )
363     {
364       std::string text = "";
365       value.Get( text );
366       SetPlaceholderText(controller, Controller::PLACEHOLDER_TYPE_INACTIVE, text);
367     }
368     else if( key == Toolkit::Text::PlaceHolder::Property::TEXT_FOCUSED || key == PLACEHOLDER_TEXT_FOCUSED )
369     {
370       std::string text = "";
371       value.Get( text );
372       SetPlaceholderText(controller, Controller::PLACEHOLDER_TYPE_ACTIVE, text);
373     }
374     else if( key == Toolkit::Text::PlaceHolder::Property::COLOR || key == PLACEHOLDER_COLOR )
375     {
376       Vector4 textColor;
377       value.Get( textColor );
378       if( GetPlaceholderTextColor(controller) != textColor )
379       {
380         SetPlaceholderTextColor(controller, textColor);
381       }
382     }
383     else if( key == Toolkit::Text::PlaceHolder::Property::FONT_FAMILY || key == PLACEHOLDER_FONT_FAMILY )
384     {
385       std::string fontFamily = "";
386       value.Get( fontFamily );
387       SetPlaceholderFontFamily(controller, fontFamily);
388     }
389     else if( key == Toolkit::Text::PlaceHolder::Property::FONT_STYLE || key == PLACEHOLDER_FONT_STYLE )
390     {
391       SetFontStyleProperty( &controller, value, Text::FontStyle::PLACEHOLDER );
392     }
393     else if( key == Toolkit::Text::PlaceHolder::Property::POINT_SIZE || key == PLACEHOLDER_POINT_SIZE )
394     {
395       float pointSize;
396       value.Get( pointSize );
397       if( !Equals(GetPlaceholderTextFontSize(controller, Text::Controller::POINT_SIZE), pointSize) )
398       {
399         SetPlaceholderTextFontSize(controller, pointSize, Text::Controller::POINT_SIZE);
400       }
401     }
402     else if( key == Toolkit::Text::PlaceHolder::Property::PIXEL_SIZE || key == PLACEHOLDER_PIXEL_SIZE )
403     {
404       float pixelSize;
405       value.Get( pixelSize );
406       if( !Equals(GetPlaceholderTextFontSize(controller, Text::Controller::PIXEL_SIZE), pixelSize) )
407       {
408         SetPlaceholderTextFontSize(controller, pixelSize, Text::Controller::PIXEL_SIZE);
409       }
410     }
411     else if( key == Toolkit::Text::PlaceHolder::Property::ELLIPSIS || key == PLACEHOLDER_ELLIPSIS )
412     {
413       bool ellipsis;
414       value.Get( ellipsis );
415       SetPlaceholderTextElideEnabled(controller, ellipsis);
416     }
417   }
418 }
419
420 void Controller::PlaceholderHandler::GetPlaceholderProperty(Controller& controller, Property::Map& map)
421 {
422   if( NULL != controller.mImpl->mEventData )
423   {
424     if( !controller.mImpl->mEventData->mPlaceholderTextActive.empty() )
425     {
426       map[ Text::PlaceHolder::Property::TEXT_FOCUSED ] = controller.mImpl->mEventData->mPlaceholderTextActive;
427     }
428     if( !controller.mImpl->mEventData->mPlaceholderTextInactive.empty() )
429     {
430       map[ Text::PlaceHolder::Property::TEXT ] = controller.mImpl->mEventData->mPlaceholderTextInactive;
431     }
432
433     map[ Text::PlaceHolder::Property::COLOR ] = controller.mImpl->mEventData->mPlaceholderTextColor;
434     map[ Text::PlaceHolder::Property::FONT_FAMILY ] = GetPlaceholderFontFamily(controller);
435
436     Property::Value fontStyleMapGet;
437     GetFontStyleProperty( &controller, fontStyleMapGet, Text::FontStyle::PLACEHOLDER );
438     map[ Text::PlaceHolder::Property::FONT_STYLE ] = fontStyleMapGet;
439
440     // Choose font size : POINT_SIZE or PIXEL_SIZE
441     if( !controller.mImpl->mEventData->mIsPlaceholderPixelSize )
442     {
443       map[ Text::PlaceHolder::Property::POINT_SIZE ] = GetPlaceholderTextFontSize(controller, Text::Controller::POINT_SIZE);
444     }
445     else
446     {
447       map[ Text::PlaceHolder::Property::PIXEL_SIZE ] = GetPlaceholderTextFontSize(controller, Text::Controller::PIXEL_SIZE);
448     }
449
450     if( controller.mImpl->mEventData->mPlaceholderEllipsisFlag )
451     {
452       map[ Text::PlaceHolder::Property::ELLIPSIS ] = IsPlaceholderTextElideEnabled(controller);
453     }
454   }
455 }
456
457 } // namespace Text
458
459 } // namespace Toolkit
460
461 } // namespace Dali