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