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