2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali-toolkit/internal/text/text-controller-placeholder-handler.h>
22 #include <dali/integration-api/debug.h>
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>
32 #if defined(DEBUG_ENABLED)
33 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
36 const std::string EMPTY_STRING("");
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";
58 void Controller::PlaceholderHandler::SetPlaceholderTextElideEnabled(Controller& controller, bool enabled)
60 controller.mImpl->mEventData->mIsPlaceholderElideEnabled = enabled;
61 controller.mImpl->mEventData->mPlaceholderEllipsisFlag = true;
63 // Update placeholder if there is no text
64 if( controller.mImpl->IsShowingPlaceholderText() ||
65 ( 0u == controller.mImpl->mModel->mLogicalModel->mText.Count() ) )
67 controller.ShowPlaceholderText();
71 bool Controller::PlaceholderHandler::IsPlaceholderTextElideEnabled(const Controller& controller)
73 return controller.mImpl->mEventData->mIsPlaceholderElideEnabled;
76 void Controller::PlaceholderHandler::SetPlaceholderText(Controller& controller, PlaceholderType type, const std::string& text)
78 if( NULL != controller.mImpl->mEventData )
80 if( PLACEHOLDER_TYPE_INACTIVE == type )
82 controller.mImpl->mEventData->mPlaceholderTextInactive = text;
86 controller.mImpl->mEventData->mPlaceholderTextActive = text;
89 // Update placeholder if there is no text
90 if( controller.mImpl->IsShowingPlaceholderText() ||
91 ( 0u == controller.mImpl->mModel->mLogicalModel->mText.Count() ) )
93 controller.ShowPlaceholderText();
98 void Controller::PlaceholderHandler::GetPlaceholderText(const Controller& controller, PlaceholderType type, std::string& text)
100 if( NULL != controller.mImpl->mEventData )
102 if( PLACEHOLDER_TYPE_INACTIVE == type )
104 text = controller.mImpl->mEventData->mPlaceholderTextInactive;
108 text = controller.mImpl->mEventData->mPlaceholderTextActive;
113 void Controller::PlaceholderHandler::SetPlaceholderFontFamily(Controller& controller, const std::string& placeholderTextFontFamily)
115 if( NULL != controller.mImpl->mEventData )
117 if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
119 controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
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();
126 controller.mImpl->RequestRelayout();
130 const std::string& Controller::PlaceholderHandler::GetPlaceholderFontFamily(const Controller& controller)
132 if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
134 return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.family;
140 void Controller::PlaceholderHandler::SetPlaceholderTextFontWeight(Controller& controller, FontWeight weight)
142 if( NULL != controller.mImpl->mEventData )
144 if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
146 controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
149 controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.weight = weight;
150 controller.mImpl->mEventData->mPlaceholderFont->weightDefined = true;
152 controller.mImpl->RequestRelayout();
156 bool Controller::PlaceholderHandler::IsPlaceholderTextFontWeightDefined(const Controller& controller)
158 if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
160 return controller.mImpl->mEventData->mPlaceholderFont->weightDefined;
165 FontWeight Controller::PlaceholderHandler::GetPlaceholderTextFontWeight(const Controller& controller)
167 if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
169 return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.weight;
172 return TextAbstraction::FontWeight::NORMAL;
175 void Controller::PlaceholderHandler::SetPlaceholderTextFontWidth(Controller& controller, FontWidth width)
177 if( NULL != controller.mImpl->mEventData )
179 if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
181 controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
184 controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.width = width;
185 controller.mImpl->mEventData->mPlaceholderFont->widthDefined = true;
187 controller.mImpl->RequestRelayout();
191 bool Controller::PlaceholderHandler::IsPlaceholderTextFontWidthDefined(const Controller& controller)
193 if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
195 return controller.mImpl->mEventData->mPlaceholderFont->widthDefined;
200 FontWidth Controller::PlaceholderHandler::GetPlaceholderTextFontWidth(const Controller& controller)
202 if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
204 return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.width;
207 return TextAbstraction::FontWidth::NORMAL;
210 void Controller::PlaceholderHandler::SetPlaceholderTextFontSlant(Controller& controller, FontSlant slant)
212 if( NULL != controller.mImpl->mEventData )
214 if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
216 controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
219 controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.slant = slant;
220 controller.mImpl->mEventData->mPlaceholderFont->slantDefined = true;
222 controller.mImpl->RequestRelayout();
226 bool Controller::PlaceholderHandler::IsPlaceholderTextFontSlantDefined(const Controller& controller)
228 if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
230 return controller.mImpl->mEventData->mPlaceholderFont->slantDefined;
235 FontSlant Controller::PlaceholderHandler::GetPlaceholderTextFontSlant(const Controller& controller)
237 if( ( NULL != controller.mImpl->mEventData ) && ( NULL != controller.mImpl->mEventData->mPlaceholderFont ) )
239 return controller.mImpl->mEventData->mPlaceholderFont->mFontDescription.slant;
242 return TextAbstraction::FontSlant::NORMAL;
245 void Controller::PlaceholderHandler::SetPlaceholderTextFontSize(Controller& controller, float fontSize, FontSizeType type)
247 if( NULL != controller.mImpl->mEventData )
249 if( NULL == controller.mImpl->mEventData->mPlaceholderFont )
251 controller.mImpl->mEventData->mPlaceholderFont = new FontDefaults();
258 controller.mImpl->mEventData->mPlaceholderFont->mDefaultPointSize = fontSize;
259 controller.mImpl->mEventData->mPlaceholderFont->sizeDefined = true;
260 controller.mImpl->mEventData->mIsPlaceholderPixelSize = false; // Font size flag
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 );
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
278 controller.mImpl->RequestRelayout();
282 float Controller::PlaceholderHandler::GetPlaceholderTextFontSize(const Controller& controller, FontSizeType type )
285 if( NULL != controller.mImpl->mEventData )
291 if( NULL != controller.mImpl->mEventData->mPlaceholderFont )
293 value = controller.mImpl->mEventData->mPlaceholderFont->mDefaultPointSize;
297 // If the placeholder text font size is not set, then return the default font size.
298 value = controller.GetDefaultFontSize( POINT_SIZE );
304 if( NULL != controller.mImpl->mEventData->mPlaceholderFont )
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 );
312 value = controller.mImpl->mEventData->mPlaceholderFont->mDefaultPointSize * static_cast< float >( horizontalDpi ) / 72.f;
316 // If the placeholder text font size is not set, then return the default font size.
317 value = controller.GetDefaultFontSize( PIXEL_SIZE );
328 void Controller::PlaceholderHandler::SetPlaceholderTextColor(Controller& controller, const Vector4& textColor )
330 if( NULL != controller.mImpl->mEventData )
332 controller.mImpl->mEventData->mPlaceholderTextColor = textColor;
335 if( controller.mImpl->IsShowingPlaceholderText() )
337 controller.mImpl->mModel->mVisualModel->SetTextColor( textColor );
338 controller.mImpl->RequestRelayout();
342 const Vector4& Controller::PlaceholderHandler::GetPlaceholderTextColor(const Controller& controller)
344 if( NULL != controller.mImpl->mEventData )
346 return controller.mImpl->mEventData->mPlaceholderTextColor;
352 void Controller::PlaceholderHandler::SetPlaceholderProperty(Controller& controller, const Property::Map& map )
354 const Property::Map::SizeType count = map.Count();
356 for( Property::Map::SizeType position = 0; position < count; ++position )
358 KeyValuePair keyValue = map.GetKeyValue( position );
359 Property::Key& key = keyValue.first;
360 Property::Value& value = keyValue.second;
362 if( key == Toolkit::Text::PlaceHolder::Property::TEXT || key == PLACEHOLDER_TEXT )
364 std::string text = "";
366 SetPlaceholderText(controller, Controller::PLACEHOLDER_TYPE_INACTIVE, text);
368 else if( key == Toolkit::Text::PlaceHolder::Property::TEXT_FOCUSED || key == PLACEHOLDER_TEXT_FOCUSED )
370 std::string text = "";
372 SetPlaceholderText(controller, Controller::PLACEHOLDER_TYPE_ACTIVE, text);
374 else if( key == Toolkit::Text::PlaceHolder::Property::COLOR || key == PLACEHOLDER_COLOR )
377 value.Get( textColor );
378 if( GetPlaceholderTextColor(controller) != textColor )
380 SetPlaceholderTextColor(controller, textColor);
383 else if( key == Toolkit::Text::PlaceHolder::Property::FONT_FAMILY || key == PLACEHOLDER_FONT_FAMILY )
385 std::string fontFamily = "";
386 value.Get( fontFamily );
387 SetPlaceholderFontFamily(controller, fontFamily);
389 else if( key == Toolkit::Text::PlaceHolder::Property::FONT_STYLE || key == PLACEHOLDER_FONT_STYLE )
391 SetFontStyleProperty( &controller, value, Text::FontStyle::PLACEHOLDER );
393 else if( key == Toolkit::Text::PlaceHolder::Property::POINT_SIZE || key == PLACEHOLDER_POINT_SIZE )
396 value.Get( pointSize );
397 if( !Equals(GetPlaceholderTextFontSize(controller, Text::Controller::POINT_SIZE), pointSize) )
399 SetPlaceholderTextFontSize(controller, pointSize, Text::Controller::POINT_SIZE);
402 else if( key == Toolkit::Text::PlaceHolder::Property::PIXEL_SIZE || key == PLACEHOLDER_PIXEL_SIZE )
405 value.Get( pixelSize );
406 if( !Equals(GetPlaceholderTextFontSize(controller, Text::Controller::PIXEL_SIZE), pixelSize) )
408 SetPlaceholderTextFontSize(controller, pixelSize, Text::Controller::PIXEL_SIZE);
411 else if( key == Toolkit::Text::PlaceHolder::Property::ELLIPSIS || key == PLACEHOLDER_ELLIPSIS )
414 value.Get( ellipsis );
415 SetPlaceholderTextElideEnabled(controller, ellipsis);
420 void Controller::PlaceholderHandler::GetPlaceholderProperty(Controller& controller, Property::Map& map)
422 if( NULL != controller.mImpl->mEventData )
424 if( !controller.mImpl->mEventData->mPlaceholderTextActive.empty() )
426 map[ Text::PlaceHolder::Property::TEXT_FOCUSED ] = controller.mImpl->mEventData->mPlaceholderTextActive;
428 if( !controller.mImpl->mEventData->mPlaceholderTextInactive.empty() )
430 map[ Text::PlaceHolder::Property::TEXT ] = controller.mImpl->mEventData->mPlaceholderTextInactive;
433 map[ Text::PlaceHolder::Property::COLOR ] = controller.mImpl->mEventData->mPlaceholderTextColor;
434 map[ Text::PlaceHolder::Property::FONT_FAMILY ] = GetPlaceholderFontFamily(controller);
436 Property::Value fontStyleMapGet;
437 GetFontStyleProperty( &controller, fontStyleMapGet, Text::FontStyle::PLACEHOLDER );
438 map[ Text::PlaceHolder::Property::FONT_STYLE ] = fontStyleMapGet;
440 // Choose font size : POINT_SIZE or PIXEL_SIZE
441 if( !controller.mImpl->mEventData->mIsPlaceholderPixelSize )
443 map[ Text::PlaceHolder::Property::POINT_SIZE ] = GetPlaceholderTextFontSize(controller, Text::Controller::POINT_SIZE);
447 map[ Text::PlaceHolder::Property::PIXEL_SIZE ] = GetPlaceholderTextFontSize(controller, Text::Controller::PIXEL_SIZE);
450 if( controller.mImpl->mEventData->mPlaceholderEllipsisFlag )
452 map[ Text::PlaceHolder::Property::ELLIPSIS ] = IsPlaceholderTextElideEnabled(controller);
459 } // namespace Toolkit