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