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