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