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