[dali_2.3.41] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-popup-impl.cpp
1 /*
2  * Copyright (c) 2024 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/controls/text-controls/text-selection-popup-impl.h>
20
21 // EXTERNAL INCLUDES
22 #if defined(__GLIBC__)
23 #include <libintl.h>
24 #endif
25 #include <dali/integration-api/debug.h>
26 #include <dali/public-api/animation/animation.h>
27 #include <dali/public-api/math/vector2.h>
28 #include <dali/public-api/math/vector4.h>
29 #include <dali/public-api/object/type-registry-helper.h>
30 #include <string.h>
31 #include <cfloat>
32
33 // INTERNAL INCLUDES
34 #include <dali-toolkit/devel-api/controls/buttons/button-devel.h>
35 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
36 #include <dali-toolkit/devel-api/controls/control-devel.h>
37 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup-callback-interface.h>
38 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
39 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
40 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
41 #include <dali-toolkit/internal/controls/text-controls/text-selection-popup-property-handler.h>
42 #include <dali-toolkit/internal/helpers/color-conversion.h>
43 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
44 #include <dali-toolkit/public-api/visuals/color-visual-properties.h>
45 #include <dali-toolkit/public-api/visuals/text-visual-properties.h>
46 #include <dali-toolkit/public-api/visuals/visual-properties.h>
47
48 namespace Dali
49 {
50 namespace Toolkit
51 {
52 namespace Internal
53 {
54 namespace
55 {
56 #if defined(__GLIBC__)
57 #define GET_LOCALE_TEXT(string) dgettext("dali-toolkit", string)
58 #endif
59
60 const char*         TEXT_SELECTION_POPUP_BUTTON_STYLE_NAME("TextSelectionPopupButton");
61 const Dali::Vector4 DEFAULT_OPTION_PRESSED_COLOR(Dali::Vector4(0.24f, 0.72f, 0.8f, 1.0f));
62 const float         DEFAULT_OPTION_PRESSED_CORNER_RADIUS = 0.0f;
63 const Dali::Vector4 DEFAULT_LABEL_PADDING(Dali::Vector4(24.0f, 24.0f, 14.0f, 14.0f));
64
65 #if defined(DEBUG_ENABLED)
66 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
67 #endif
68
69 #ifdef DGETTEXT_ENABLED
70
71 #define POPUP_CUT_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CUT")
72 #define POPUP_COPY_STRING GET_LOCALE_TEXT("IDS_COM_BODY_COPY")
73 #define POPUP_PASTE_STRING GET_LOCALE_TEXT("IDS_COM_BODY_PASTE")
74 #define POPUP_SELECT_STRING GET_LOCALE_TEXT("IDS_COM_SK_SELECT")
75 #define POPUP_SELECT_ALL_STRING GET_LOCALE_TEXT("IDS_COM_BODY_SELECT_ALL")
76 #define POPUP_CLIPBOARD_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CLIPBOARD")
77
78 #else
79
80 #define POPUP_CUT_STRING "Cut"
81 #define POPUP_COPY_STRING "Copy"
82 #define POPUP_PASTE_STRING "Paste"
83 #define POPUP_SELECT_STRING "Select"
84 #define POPUP_SELECT_ALL_STRING "Select All"
85 #define POPUP_CLIPBOARD_STRING "Clipboard"
86
87 #endif
88
89 const char* const OPTION_SELECT_WORD = "option-select_word"; // "Select Word" popup option.
90 const char* const OPTION_SELECT_ALL("option-select_all");    // "Select All" popup option.
91 const char* const OPTION_CUT("optionCut");                   // "Cut" popup option.
92 const char* const OPTION_COPY("optionCopy");                 // "Copy" popup option.
93 const char* const OPTION_PASTE("optionPaste");               // "Paste" popup option.
94 const char* const OPTION_CLIPBOARD("optionClipboard");       // "Clipboard" popup option.
95
96 const char* IDS_LTR("IDS_LTR");
97 const char* RTL_DIRECTION("RTL");
98
99 BaseHandle Create()
100 {
101   return Toolkit::TextSelectionPopup::New(NULL);
102 }
103
104 // Setup properties, signals and actions using the type-registry.
105
106 DALI_TYPE_REGISTRATION_BEGIN(Toolkit::TextSelectionPopup, Toolkit::Control, Create);
107
108 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupMaxSize", VECTOR2, POPUP_MAX_SIZE)
109 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupMinSize", VECTOR2, POPUP_MIN_SIZE)
110 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "optionMaxSize", VECTOR2, OPTION_MAX_SIZE)
111 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "optionMinSize", VECTOR2, OPTION_MIN_SIZE)
112 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "optionDividerSize", VECTOR2, OPTION_DIVIDER_SIZE)
113 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "optionDividerPadding", VECTOR4, OPTION_DIVIDER_PADDING)
114 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupClipboardButtonImage", STRING, POPUP_CLIPBOARD_BUTTON_ICON_IMAGE)
115 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupCutButtonImage", STRING, POPUP_CUT_BUTTON_ICON_IMAGE)
116 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupCopyButtonImage", STRING, POPUP_COPY_BUTTON_ICON_IMAGE)
117 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupPasteButtonImage", STRING, POPUP_PASTE_BUTTON_ICON_IMAGE)
118 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupSelectButtonImage", STRING, POPUP_SELECT_BUTTON_ICON_IMAGE)
119 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupSelectAllButtonImage", STRING, POPUP_SELECT_ALL_BUTTON_ICON_IMAGE)
120 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupDividerColor", VECTOR4, POPUP_DIVIDER_COLOR)
121 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupIconColor", VECTOR4, POPUP_ICON_COLOR)
122 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupPressedColor", VECTOR4, POPUP_PRESSED_COLOR)
123 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupPressedCornerRadius", FLOAT, POPUP_PRESSED_CORNER_RADIUS)
124 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupPressedImage", STRING, POPUP_PRESSED_IMAGE)
125 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupFadeInDuration", FLOAT, POPUP_FADE_IN_DURATION)
126 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "popupFadeOutDuration", FLOAT, POPUP_FADE_OUT_DURATION)
127 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "backgroundBorder", MAP, BACKGROUND_BORDER)
128 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "background", MAP, BACKGROUND)
129 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "labelMinimumSize", VECTOR2, LABEL_MINIMUM_SIZE)
130 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "labelPadding", VECTOR4, LABEL_PADDING)
131 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "labelTextVisual", MAP, LABEL_TEXT_VISUAL)
132 DALI_PROPERTY_REGISTRATION(Toolkit, TextSelectionPopup, "enableScrollBar", BOOLEAN, ENABLE_SCROLL_BAR)
133
134 DALI_TYPE_REGISTRATION_END()
135
136 } // namespace
137
138 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New(TextSelectionPopupCallbackInterface* callbackInterface)
139 {
140   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextSelectionPopup::New\n");
141
142   // Create the implementation, temporarily owned by this handle on stack
143   IntrusivePtr<TextSelectionPopup> impl = new TextSelectionPopup(callbackInterface);
144
145   // Pass ownership to CustomActor handle
146   Dali::Toolkit::TextSelectionPopup handle(*impl);
147
148   // Second-phase init of the implementation
149   // This can only be done after the CustomActor connection has been made...
150   impl->Initialize();
151
152   return handle;
153 }
154
155 void TextSelectionPopup::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
156 {
157   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast(Dali::BaseHandle(object));
158
159   if(selectionPopup)
160   {
161     PropertyHandler::SetProperty(selectionPopup, index, value);
162   }
163 }
164
165 Property::Value TextSelectionPopup::GetProperty(BaseObject* object, Property::Index index)
166 {
167   Property::Value             value;
168   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast(Dali::BaseHandle(object));
169
170   if(selectionPopup)
171   {
172     value = PropertyHandler::GetProperty(selectionPopup, index);
173   }
174   return value;
175 }
176
177 void TextSelectionPopup::SetProperties(const Property::Map& properties)
178 {
179   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast(Self());
180
181   if(selectionPopup)
182   {
183     const Property::Map::SizeType count = properties.Count();
184     for(Property::Map::SizeType position = 0; position < count; ++position)
185     {
186       KeyValuePair     keyValue = properties.GetKeyValue(position);
187       Property::Key&   key      = keyValue.first;
188       Property::Value& value    = keyValue.second;
189       PropertyHandler::SetProperty(selectionPopup, key.indexKey, value);
190     }
191   }
192 }
193
194 void TextSelectionPopup::GetProperties(Property::Map& properties)
195 {
196   Property::Map map;
197
198   map.Insert(Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE, GetDimensionToCustomise(POPUP_MAXIMUM_SIZE));
199   map.Insert(Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE, GetDimensionToCustomise(OPTION_DIVIDER_SIZE));
200   map.Insert(Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_PADDING, Vector4(mOptionDividerPadding.left, mOptionDividerPadding.right, mOptionDividerPadding.top, mOptionDividerPadding.bottom));
201   map.Insert(Toolkit::TextSelectionPopup::Property::LABEL_MINIMUM_SIZE, mLabelMinimumSize);
202   map.Insert(Toolkit::TextSelectionPopup::Property::LABEL_PADDING, Vector4(mLabelPadding.left, mLabelPadding.right, mLabelPadding.top, mLabelPadding.bottom));
203   map.Insert(Toolkit::TextSelectionPopup::Property::LABEL_TEXT_VISUAL, mLabelTextVisual);
204   map.Insert(Toolkit::TextSelectionPopup::Property::ENABLE_SCROLL_BAR, mEnableScrollBar);
205   map.Insert(Toolkit::TextSelectionPopup::Property::POPUP_DIVIDER_COLOR, mDividerColor);
206   map.Insert(Toolkit::TextSelectionPopup::Property::POPUP_FADE_IN_DURATION, mFadeInDuration);
207   map.Insert(Toolkit::TextSelectionPopup::Property::POPUP_FADE_OUT_DURATION, mFadeOutDuration);
208   map.Insert(Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_COLOR, mPressedColor);
209   map.Insert(Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_CORNER_RADIUS, mPressedCornerRadius);
210
211   Property::Map         backgroundMap;
212   Toolkit::Visual::Base backgroundVisual = DevelControl::GetVisual(*this, Toolkit::Control::Property::BACKGROUND);
213   if(backgroundVisual)
214   {
215     backgroundVisual.CreatePropertyMap(backgroundMap);
216   }
217   map.Insert(Toolkit::TextSelectionPopup::Property::BACKGROUND, backgroundMap);
218
219   Property::Map         borderMap;
220   Toolkit::Visual::Base borderVisual = DevelControl::GetVisual(*this, Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER);
221   if(borderVisual)
222   {
223     borderVisual.CreatePropertyMap(borderMap);
224   }
225   map.Insert(Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER, borderMap);
226
227   properties = map;
228 }
229
230 void TextSelectionPopup::EnableButtons(Toolkit::TextSelectionPopup::Buttons buttonsToEnable)
231 {
232   mEnabledButtons = buttonsToEnable;
233   mButtonsChanged = true;
234 }
235
236 void TextSelectionPopup::RaiseAbove(Actor target)
237 {
238   if(mToolbar)
239   {
240     mToolbar.RaiseAbove(target);
241   }
242 }
243
244 void TextSelectionPopup::ShowPopup()
245 {
246   if((!mPopupShowing || mButtonsChanged) &&
247      (Toolkit::TextSelectionPopup::NONE != mEnabledButtons))
248   {
249     Actor self = Self();
250     AddPopupOptionsToToolbar(mShowIcons, mShowCaptions);
251
252     Animation animation = Animation::New(mFadeInDuration);
253     animation.AnimateTo(Property(self, Actor::Property::COLOR_ALPHA), 1.0f);
254     animation.Play();
255     mPopupShowing = true;
256   }
257 }
258
259 void TextSelectionPopup::HidePopup()
260 {
261   if(mPopupShowing)
262   {
263     mPopupShowing       = false;
264     Actor     self      = Self();
265     Animation animation = Animation::New(mFadeOutDuration);
266     animation.AnimateTo(Property(self, Actor::Property::COLOR_ALPHA), 0.0f);
267     animation.FinishedSignal().Connect(this, &TextSelectionPopup::HideAnimationFinished);
268     animation.Play();
269   }
270 }
271
272 void TextSelectionPopup::OnInitialize()
273 {
274   DALI_LOG_INFO(gLogFilter, Debug::General, "TextSelectionPopup::OnInitialize\n");
275   Actor self = Self();
276   self.SetResizePolicy(ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS);
277   self.SetProperty(Actor::Property::COLOR_ALPHA, 0.0f);
278
279   // Accessibility
280   self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_HIGHLIGHTABLE, true);
281   self.SetProperty(Toolkit::DevelControl::Property::ACCESSIBILITY_ROLE, Dali::Accessibility::Role::DIALOG);
282 }
283
284 void TextSelectionPopup::HideAnimationFinished(Animation& animation)
285 {
286   Actor self = Self();
287   if(!mPopupShowing) // During the Hide/Fade animation there could be a call to Show the Popup again, mPopupShowing will be true in this case.
288   {
289     DALI_LOG_INFO(gLogFilter, Debug::General, "TextSelectionPopup::HideAnimationFinished\n");
290     UnparentAndReset(mToolbar);
291   }
292 }
293
294 bool TextSelectionPopup::OnCutButtonPressed(Toolkit::Button button)
295 {
296   if(mCallbackInterface)
297   {
298     mCallbackInterface->TextPopupButtonTouched(Toolkit::TextSelectionPopup::CUT);
299   }
300
301   return true;
302 }
303
304 bool TextSelectionPopup::OnCopyButtonPressed(Toolkit::Button button)
305 {
306   if(mCallbackInterface)
307   {
308     mCallbackInterface->TextPopupButtonTouched(Dali::Toolkit::TextSelectionPopup::COPY);
309   }
310
311   return true;
312 }
313
314 bool TextSelectionPopup::OnPasteButtonPressed(Toolkit::Button button)
315 {
316   if(mCallbackInterface)
317   {
318     mCallbackInterface->TextPopupButtonTouched(Dali::Toolkit::TextSelectionPopup::PASTE);
319   }
320
321   return true;
322 }
323
324 bool TextSelectionPopup::OnSelectButtonPressed(Toolkit::Button button)
325 {
326   if(mCallbackInterface)
327   {
328     mCallbackInterface->TextPopupButtonTouched(Dali::Toolkit::TextSelectionPopup::SELECT);
329   }
330
331   return true;
332 }
333
334 bool TextSelectionPopup::OnSelectAllButtonPressed(Toolkit::Button button)
335 {
336   if(mCallbackInterface)
337   {
338     mCallbackInterface->TextPopupButtonTouched(Dali::Toolkit::TextSelectionPopup::SELECT_ALL);
339   }
340
341   return true;
342 }
343
344 bool TextSelectionPopup::OnClipboardButtonPressed(Toolkit::Button button)
345 {
346   if(mCallbackInterface)
347   {
348     mCallbackInterface->TextPopupButtonTouched(Dali::Toolkit::TextSelectionPopup::CLIPBOARD);
349   }
350
351   return true;
352 }
353
354 void TextSelectionPopup::SetDimensionToCustomise(const PopupCustomisations& settingToCustomise, const Size& dimension)
355 {
356   switch(settingToCustomise)
357   {
358     case POPUP_MAXIMUM_SIZE:
359     {
360       mPopupMaxSize = dimension;
361       if(mToolbar)
362       {
363         mToolbar.SetProperty(Toolkit::TextSelectionToolbar::Property::MAX_SIZE, dimension);
364       }
365       break;
366     }
367     case OPTION_MAXIMUM_SIZE:
368     {
369       mOptionMaxSize = dimension;
370       // Option max size not currently currently supported
371       break;
372     }
373     case OPTION_MINIMUM_SIZE:
374     {
375       mOptionMinSize = dimension;
376       // Option min size not currently currently supported
377       break;
378     }
379     case OPTION_DIVIDER_SIZE:
380     {
381       mOptionDividerSize = dimension;
382       if(mToolbar)
383       {
384         // Resize Dividers not currently supported
385       }
386       break;
387     }
388   } // switch
389 }
390
391 Size TextSelectionPopup::GetDimensionToCustomise(const PopupCustomisations& settingToCustomise) const
392 {
393   switch(settingToCustomise)
394   {
395     case POPUP_MAXIMUM_SIZE:
396     {
397       if(mToolbar)
398       {
399         return mToolbar.GetProperty(Toolkit::TextSelectionToolbar::Property::MAX_SIZE).Get<Vector2>();
400       }
401       else
402       {
403         return mPopupMaxSize;
404       }
405     }
406     case OPTION_MAXIMUM_SIZE:
407     {
408       return mOptionMaxSize;
409     }
410     case OPTION_MINIMUM_SIZE:
411     {
412       return mOptionMinSize;
413     }
414     case OPTION_DIVIDER_SIZE:
415     {
416       return mOptionDividerSize;
417     }
418   } // switch
419
420   return Size::ZERO;
421 }
422
423 void TextSelectionPopup::SetButtonImage(Toolkit::TextSelectionPopup::Buttons button, const std::string& image)
424 {
425   switch(button)
426   {
427     case Toolkit::TextSelectionPopup::CLIPBOARD:
428     {
429       mClipboardIconImage = image;
430       break;
431     }
432     case Toolkit::TextSelectionPopup::CUT:
433     {
434       mCutIconImage = image;
435       break;
436     }
437     case Toolkit::TextSelectionPopup::COPY:
438     {
439       mCopyIconImage = image;
440       break;
441     }
442     case Toolkit::TextSelectionPopup::PASTE:
443     {
444       mPasteIconImage = image;
445       break;
446     }
447     case Toolkit::TextSelectionPopup::SELECT:
448     {
449       mSelectIconImage = image;
450       break;
451     }
452     case Toolkit::TextSelectionPopup::SELECT_ALL:
453     {
454       mSelectAllIconImage = image;
455       break;
456     }
457     default:
458     {
459       DALI_ASSERT_DEBUG("TextSelectionPopup SetPopupImage Unknown Button");
460     }
461   } // switch
462 }
463
464 const std::string& TextSelectionPopup::GetButtonImage(Toolkit::TextSelectionPopup::Buttons button) const
465 {
466   switch(button)
467   {
468     case Toolkit::TextSelectionPopup::CLIPBOARD:
469     {
470       return mClipboardIconImage;
471       break;
472     }
473     case Toolkit::TextSelectionPopup::CUT:
474     {
475       return mCutIconImage;
476       break;
477     }
478     case Toolkit::TextSelectionPopup::COPY:
479     {
480       return mCopyIconImage;
481       break;
482     }
483     case Toolkit::TextSelectionPopup::PASTE:
484     {
485       return mPasteIconImage;
486       break;
487     }
488     case Toolkit::TextSelectionPopup::SELECT:
489     {
490       return mSelectIconImage;
491       break;
492     }
493     case Toolkit::TextSelectionPopup::SELECT_ALL:
494     {
495       return mSelectAllIconImage;
496       break;
497     }
498     case Toolkit::TextSelectionPopup::NONE:
499     {
500       break;
501     }
502   } // switch
503
504   DALI_ASSERT_DEBUG("TextSelectionPopup GetPopupImage Unknown Button");
505   static std::string empty;
506   return empty;
507 }
508
509 void TextSelectionPopup::SetPressedImage(const std::string& filename)
510 {
511   mPressedImage = filename;
512 }
513
514 std::string TextSelectionPopup::GetPressedImage() const
515 {
516   return mPressedImage;
517 }
518
519 void TextSelectionPopup::SetOptionDividerPadding(const Padding& padding)
520 {
521   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextSelectionPopup::SetOptionDividerPadding padding(%f,%f,%f,%f)\n", padding.left, padding.right, padding.top, padding.bottom);
522   mOptionDividerPadding = Padding(padding.left, padding.right, padding.top, padding.bottom);
523 }
524
525 Padding TextSelectionPopup::GetOptionDividerPadding() const
526 {
527   return mOptionDividerPadding;
528 }
529
530 void TextSelectionPopup::SetLabelPadding(const Padding& padding)
531 {
532   DALI_LOG_INFO(gLogFilter, Debug::Verbose, "TextSelectionPopup::SetLabelPadding padding(%f,%f,%f,%f)\n", padding.left, padding.right, padding.top, padding.bottom);
533   mLabelPadding = Padding(padding.left, padding.right, padding.top, padding.bottom);
534 }
535
536 Padding TextSelectionPopup::GetLabelPadding() const
537 {
538   return mLabelPadding;
539 }
540
541 void TextSelectionPopup::CreateOrderedListOfPopupOptions()
542 {
543   mOrderListOfButtons.clear();
544   mOrderListOfButtons.reserve(8u);
545
546   // Create button for each possible option using Option priority
547   mOrderListOfButtons.push_back(ButtonRequirement(Toolkit::TextSelectionPopup::CUT, mCutOptionPriority, OPTION_CUT, POPUP_CUT_STRING, 0 != (mEnabledButtons & Toolkit::TextSelectionPopup::CUT)));
548   mOrderListOfButtons.push_back(ButtonRequirement(Toolkit::TextSelectionPopup::COPY, mCopyOptionPriority, OPTION_COPY, POPUP_COPY_STRING, 0 != (mEnabledButtons & Toolkit::TextSelectionPopup::COPY)));
549   mOrderListOfButtons.push_back(ButtonRequirement(Toolkit::TextSelectionPopup::PASTE, mPasteOptionPriority, OPTION_PASTE, POPUP_PASTE_STRING, 0 != (mEnabledButtons & Toolkit::TextSelectionPopup::PASTE)));
550   mOrderListOfButtons.push_back(ButtonRequirement(Toolkit::TextSelectionPopup::SELECT, mSelectOptionPriority, OPTION_SELECT_WORD, POPUP_SELECT_STRING, 0 != (mEnabledButtons & Toolkit::TextSelectionPopup::SELECT)));
551   mOrderListOfButtons.push_back(ButtonRequirement(Toolkit::TextSelectionPopup::SELECT_ALL, mSelectAllOptionPriority, OPTION_SELECT_ALL, POPUP_SELECT_ALL_STRING, 0 != (mEnabledButtons & Toolkit::TextSelectionPopup::SELECT_ALL)));
552   mOrderListOfButtons.push_back(ButtonRequirement(Toolkit::TextSelectionPopup::CLIPBOARD, mClipboardOptionPriority, OPTION_CLIPBOARD, POPUP_CLIPBOARD_STRING, 0 != (mEnabledButtons & Toolkit::TextSelectionPopup::CLIPBOARD)));
553
554   // Sort the buttons according their priorities.
555   std::sort(mOrderListOfButtons.begin(), mOrderListOfButtons.end(), TextSelectionPopup::ButtonPriorityCompare());
556 }
557
558 void TextSelectionPopup::AddOption(const ButtonRequirement& button, bool showDivider, bool showIcons, bool showCaption)
559 {
560   // 1. Create a option.
561   DALI_LOG_INFO(gLogFilter, Debug::General, "TextSelectionPopup::AddOption\n");
562
563   Toolkit::PushButton option = Toolkit::PushButton::New();
564   option.SetProperty(Dali::Actor::Property::NAME, button.name);
565   option.SetResizePolicy(ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS);
566   option.SetProperty(Actor::Property::MINIMUM_SIZE, mLabelMinimumSize);
567
568   switch(button.id)
569   {
570     case Toolkit::TextSelectionPopup::CUT:
571     {
572       option.ClickedSignal().Connect(this, &TextSelectionPopup::OnCutButtonPressed);
573       break;
574     }
575     case Toolkit::TextSelectionPopup::COPY:
576     {
577       option.ClickedSignal().Connect(this, &TextSelectionPopup::OnCopyButtonPressed);
578       break;
579     }
580     case Toolkit::TextSelectionPopup::PASTE:
581     {
582       option.ClickedSignal().Connect(this, &TextSelectionPopup::OnPasteButtonPressed);
583       break;
584     }
585     case Toolkit::TextSelectionPopup::SELECT:
586     {
587       option.ClickedSignal().Connect(this, &TextSelectionPopup::OnSelectButtonPressed);
588       break;
589     }
590     case Toolkit::TextSelectionPopup::SELECT_ALL:
591     {
592       option.ClickedSignal().Connect(this, &TextSelectionPopup::OnSelectAllButtonPressed);
593       break;
594     }
595     case Toolkit::TextSelectionPopup::CLIPBOARD:
596     {
597       option.ClickedSignal().Connect(this, &TextSelectionPopup::OnClipboardButtonPressed);
598       break;
599     }
600     case Toolkit::TextSelectionPopup::NONE:
601     {
602       // Nothing to do:
603       break;
604     }
605   }
606
607   // 2. Set the options contents.
608   if(showCaption)
609   {
610     // PushButton layout properties.
611     const Padding padding(mLabelPadding);
612     option.SetProperty(Toolkit::PushButton::Property::LABEL_PADDING, padding);
613
614     // Label properties.
615     Property::Map buttonLabelProperties;
616     buttonLabelProperties.Insert(Toolkit::TextVisual::Property::TEXT, button.caption);
617     buttonLabelProperties.Merge(mLabelTextVisual);
618
619     option.SetProperty(Toolkit::Button::Property::LABEL, buttonLabelProperties);
620   }
621   if(showIcons)
622   {
623     option.SetProperty(Toolkit::PushButton::Property::ICON_PADDING, Vector4(10.0f, 10.0f, 10.0f, 10.0f));
624     option.SetProperty(Toolkit::DevelButton::Property::LABEL_RELATIVE_ALIGNMENT, "BOTTOM");
625
626     // TODO: This is temporarily disabled until the text-selection-popup image API is changed to strings.
627     //option.SetProperty(  Toolkit::Button::Property::SELECTED_VISUAL, button.icon );
628     //option.SetProperty(  Toolkit::Button::Property::UNSELECTED_VISUAL, button.icon );
629   }
630
631   // 3. Set the normal option image (blank / Transparent).
632   option.SetProperty(Toolkit::Button::Property::UNSELECTED_BACKGROUND_VISUAL, "");
633
634   // 4. Set the pressed option image.
635   Property::Value selectedBackgroundValue(mPressedImage);
636   if(mPressedImage.empty())
637   {
638     // The image can be blank, the color can be used in that case.
639     selectedBackgroundValue = Property::Value{{Toolkit::Visual::Property::TYPE, Toolkit::Visual::COLOR},
640                                               {Toolkit::ColorVisual::Property::MIX_COLOR, mPressedColor},
641                                               {Toolkit::DevelVisual::Property::CORNER_RADIUS, mPressedCornerRadius},
642                                               {Toolkit::DevelVisual::Property::CORNER_RADIUS_POLICY, Toolkit::Visual::Transform::Policy::RELATIVE}};
643   }
644   option.SetProperty(Toolkit::Button::Property::SELECTED_BACKGROUND_VISUAL, selectedBackgroundValue);
645   // The value set by user takes precedence over the theme value.
646   if(mLabelTextVisual.Count() == 0)
647   {
648     option.SetProperty(Toolkit::Control::Property::STYLE_NAME, TEXT_SELECTION_POPUP_BUTTON_STYLE_NAME);
649   }
650
651   // 5 Add option to tool bar
652   mToolbar.AddOption(option);
653
654   // 6. Add the divider
655   if(showDivider)
656   {
657     const Size    size(mOptionDividerSize.width, 0.0f); // Height FILL_TO_PARENT
658     const Padding padding(mOptionDividerPadding);
659
660     Toolkit::Control divider = Toolkit::Control::New();
661 #ifdef DECORATOR_DEBUG
662     divider.SetProperty(Dali::Actor::Property::NAME, "Text's popup divider");
663 #endif
664     divider.SetProperty(Actor::Property::SIZE, size);
665     divider.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT);
666     divider.SetProperty(Actor::Property::PADDING, padding);
667     divider.SetBackgroundColor(mDividerColor);
668     mToolbar.AddDivider(divider);
669   }
670 }
671
672 std::size_t TextSelectionPopup::GetNumberOfEnabledOptions() const
673 {
674   std::size_t numberOfOptions = 0u;
675   for(std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); (it != endIt); ++it)
676   {
677     const ButtonRequirement& button(*it);
678     if(button.enabled)
679     {
680       ++numberOfOptions;
681     }
682   }
683
684   return numberOfOptions;
685 }
686
687 void TextSelectionPopup::AddPopupOptionsToToolbar(bool showIcons, bool showCaptions)
688 {
689   DALI_LOG_INFO(gLogFilter, Debug::General, "TextSelectionPopup::AddPopupOptionsToToolbar\n");
690
691   CreateOrderedListOfPopupOptions();
692
693   mButtonsChanged = false;
694   UnparentAndReset(mToolbar);
695
696   if(!mToolbar)
697   {
698     Actor self = Self();
699     mToolbar   = Toolkit::TextSelectionToolbar::New();
700     if(mPopupMaxSize != Vector2::ZERO) // If PopupMaxSize property set then apply to Toolbar. Toolbar currently is not retriving this from json
701     {
702       mToolbar.SetProperty(Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mPopupMaxSize);
703     }
704     mToolbar.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
705 #ifdef DECORATOR_DEBUG
706     mToolbar.SetProperty(Dali::Actor::Property::NAME, "TextSelectionToolbar");
707 #endif
708     mToolbar.SetProperty(Toolkit::TextSelectionToolbar::Property::ENABLE_SCROLL_BAR, mEnableScrollBar);
709     self.Add(mToolbar);
710   }
711
712   // Whether to mirror the list of buttons (for right to left languages)
713   bool mirror = false;
714 #if defined(__GLIBC__)
715   char* idsLtr = GET_LOCALE_TEXT(IDS_LTR);
716   if(NULL != idsLtr)
717   {
718     mirror = (0 == strcmp(idsLtr, RTL_DIRECTION));
719
720     if(mirror)
721     {
722       std::reverse(mOrderListOfButtons.begin(), mOrderListOfButtons.end());
723     }
724   }
725 #endif
726
727   // Iterate list of buttons and add active ones to Toolbar
728   std::size_t numberOfOptionsRequired = GetNumberOfEnabledOptions();
729   std::size_t numberOfOptionsAdded    = 0u;
730   for(std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); (it != endIt); ++it)
731   {
732     const ButtonRequirement& button(*it);
733     if(button.enabled)
734     {
735       numberOfOptionsAdded++;
736       AddOption(button, (numberOfOptionsAdded < numberOfOptionsRequired), showIcons, showCaptions);
737     }
738   }
739
740   if(mirror)
741   {
742     mToolbar.ScrollTo(Vector2(mPopupMaxSize.x, 0.f));
743   }
744 }
745
746 void TextSelectionPopup::CreateBackgroundBorder(Property::Map& propertyMap)
747 {
748   // Removes previous image if necessary
749   DevelControl::UnregisterVisual(*this, Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER);
750
751   if(!propertyMap.Empty())
752   {
753     Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
754
755     if(visual)
756     {
757       DevelControl::RegisterVisual(*this, Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER, visual, DepthIndex::CONTENT);
758     }
759   }
760 }
761
762 void TextSelectionPopup::CreateBackground(Property::Map& propertyMap)
763 {
764   // Removes previous image if necessary
765   DevelControl::UnregisterVisual(*this, Toolkit::Control::Property::BACKGROUND);
766
767   if(!propertyMap.Empty())
768   {
769     Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual(propertyMap);
770
771     if(visual)
772     {
773       DevelControl::RegisterVisual(*this, Toolkit::Control::Property::BACKGROUND, visual, DepthIndex::BACKGROUND);
774     }
775   }
776 }
777
778 TextSelectionPopup::TextSelectionPopup(TextSelectionPopupCallbackInterface* callbackInterface)
779 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
780   mToolbar(),
781   mPopupMaxSize(),
782   mOptionMaxSize(),
783   mOptionMinSize(),
784   mOptionDividerSize(),
785   mOptionDividerPadding(),
786   mLabelMinimumSize(),
787   mLabelPadding(DEFAULT_LABEL_PADDING),
788   mLabelTextVisual(),
789   mEnableScrollBar(true),
790   mEnabledButtons(Toolkit::TextSelectionPopup::NONE),
791   mCallbackInterface(callbackInterface),
792   mPressedColor(DEFAULT_OPTION_PRESSED_COLOR),
793   mPressedCornerRadius(DEFAULT_OPTION_PRESSED_CORNER_RADIUS),
794   mDividerColor(Color::WHITE),
795   mIconColor(Color::WHITE),
796   mSelectOptionPriority(1),
797   mSelectAllOptionPriority(2),
798   mCutOptionPriority(4),
799   mCopyOptionPriority(3),
800   mPasteOptionPriority(5),
801   mClipboardOptionPriority(6),
802   mFadeInDuration(0.0f),
803   mFadeOutDuration(0.0f),
804   mShowIcons(false),
805   mShowCaptions(true),
806   mPopupShowing(false),
807   mButtonsChanged(false)
808 {
809 }
810
811 TextSelectionPopup::~TextSelectionPopup()
812 {
813 }
814
815 } // namespace Internal
816
817 } // namespace Toolkit
818
819 } // namespace Dali