Merge "[3.0] Fixed to show textSelectionPop up normally on second long press" into...
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / controls / text-controls / text-selection-popup-impl.cpp
1 /*
2  * Copyright (c) 2016 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/control-depth-index-ranges.h>
37 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup-callback-interface.h>
38 #include <dali-toolkit/internal/helpers/color-conversion.h>
39
40 namespace Dali
41 {
42
43 namespace Toolkit
44 {
45
46 namespace Internal
47 {
48
49 namespace
50 {
51
52 #define GET_LOCALE_TEXT(string) dgettext("dali-toolkit", string)
53
54 const std::string TEXT_SELECTION_POPUP_BUTTON_STYLE_NAME( "TextSelectionPopupButton" );
55 const Dali::Vector4 DEFAULT_OPTION_PRESSED_COLOR( Dali::Vector4( 0.24f, 0.72f, 0.8f, 1.0f ) );
56
57 #if defined(DEBUG_ENABLED)
58   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
59 #endif
60
61 #ifdef DGETTEXT_ENABLED
62
63 #define POPUP_CUT_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CUT")
64 #define POPUP_COPY_STRING GET_LOCALE_TEXT("IDS_COM_BODY_COPY")
65 #define POPUP_PASTE_STRING GET_LOCALE_TEXT("IDS_COM_BODY_PASTE")
66 #define POPUP_SELECT_STRING GET_LOCALE_TEXT("IDS_COM_SK_SELECT")
67 #define POPUP_SELECT_ALL_STRING GET_LOCALE_TEXT("IDS_COM_BODY_SELECT_ALL")
68 #define POPUP_CLIPBOARD_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CLIPBOARD")
69
70 #else
71
72 #define POPUP_CUT_STRING  "Cut"
73 #define POPUP_COPY_STRING  "Copy"
74 #define POPUP_PASTE_STRING  "Paste"
75 #define POPUP_SELECT_STRING  "Select"
76 #define POPUP_SELECT_ALL_STRING  "Select All"
77 #define POPUP_CLIPBOARD_STRING  "Clipboard"
78
79 #endif
80
81 const char* const OPTION_SELECT_WORD = "option-select_word";                       // "Select Word" popup option.
82 const char* const OPTION_SELECT_ALL("option-select_all");                          // "Select All" popup option.
83 const char* const OPTION_CUT("optionCut");                                        // "Cut" popup option.
84 const char* const OPTION_COPY("optionCopy");                                      // "Copy" popup option.
85 const char* const OPTION_PASTE("optionPaste");                                    // "Paste" popup option.
86 const char* const OPTION_CLIPBOARD("optionClipboard");                            // "Clipboard" popup option.
87
88 const std::string IDS_LTR( "IDS_LTR" );
89 const std::string RTL_DIRECTION( "RTL" );
90
91 BaseHandle Create()
92 {
93   return Toolkit::TextSelectionPopup::New( NULL );
94 }
95
96 // Setup properties, signals and actions using the type-registry.
97
98 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionPopup, Toolkit::Control, Create );
99
100 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupMaxSize", VECTOR2,   POPUP_MAX_SIZE )
101 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupMinSize", VECTOR2,   POPUP_MIN_SIZE )
102 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionMaxSize", VECTOR2,   OPTION_MAX_SIZE )
103 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionMinSize", VECTOR2,   OPTION_MIN_SIZE )
104 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionDividerSize", VECTOR2,   OPTION_DIVIDER_SIZE )
105 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupClipboardButtonImage", STRING, POPUP_CLIPBOARD_BUTTON_ICON_IMAGE )
106 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupCutButtonImage", STRING, POPUP_CUT_BUTTON_ICON_IMAGE )
107 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupCopyButtonImage", STRING, POPUP_COPY_BUTTON_ICON_IMAGE )
108 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPasteButtonImage", STRING, POPUP_PASTE_BUTTON_ICON_IMAGE )
109 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupSelectButtonImage", STRING, POPUP_SELECT_BUTTON_ICON_IMAGE )
110 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupSelectAllButtonImage", STRING, POPUP_SELECT_ALL_BUTTON_ICON_IMAGE )
111 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupDividerColor", VECTOR4, POPUP_DIVIDER_COLOR )
112 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupIconColor", VECTOR4, POPUP_ICON_COLOR )
113 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPressedColor", VECTOR4, POPUP_PRESSED_COLOR )
114 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPressedImage", STRING, POPUP_PRESSED_IMAGE )
115 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupFadeInDuration", FLOAT, POPUP_FADE_IN_DURATION )
116 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupFadeOutDuration", FLOAT, POPUP_FADE_OUT_DURATION )
117 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "backgroundBorder", MAP, BACKGROUND_BORDER )
118
119 DALI_TYPE_REGISTRATION_END()
120
121 } // namespace
122
123
124 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New( TextSelectionPopupCallbackInterface* callbackInterface )
125 {
126   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextSelectionPopup::New\n" );
127
128    // Create the implementation, temporarily owned by this handle on stack
129   IntrusivePtr< TextSelectionPopup > impl = new TextSelectionPopup( callbackInterface );
130
131   // Pass ownership to CustomActor handle
132   Dali::Toolkit::TextSelectionPopup handle( *impl );
133
134   // Second-phase init of the implementation
135   // This can only be done after the CustomActor connection has been made...
136   impl->Initialize();
137
138   return handle;
139 }
140
141 void TextSelectionPopup::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
142 {
143   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
144
145   if( selectionPopup )
146   {
147     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
148
149     switch( index )
150     {
151       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
152       {
153        impl.SetDimensionToCustomise( POPUP_MAXIMUM_SIZE, value.Get< Vector2 >() );
154        break;
155       }
156       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
157       {
158         impl.SetDimensionToCustomise( OPTION_MAXIMUM_SIZE, value.Get< Vector2 >() );
159         break;
160       }
161       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
162       {
163         impl.SetDimensionToCustomise( OPTION_MINIMUM_SIZE, value.Get< Vector2>() );
164         break;
165       }
166       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
167       {
168         impl.SetDimensionToCustomise( OPTION_DIVIDER_SIZE, value.Get< Vector2>() );
169         break;
170       }
171       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
172       {
173         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
174         impl.SetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD, image );
175         break;
176       }
177       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
178       {
179         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
180         impl.SetButtonImage( Toolkit::TextSelectionPopup::CUT, image );
181         break;
182       }
183       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
184       {
185         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
186         impl.SetButtonImage( Toolkit::TextSelectionPopup::COPY, image );
187         break;
188       }
189       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
190       {
191         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
192         impl.SetButtonImage( Toolkit::TextSelectionPopup::PASTE, image );
193         break;
194       }
195       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
196       {
197         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
198         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT, image );
199         break;
200       }
201       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
202       {
203         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
204         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL, image );
205         break;
206       }
207       case Toolkit::TextSelectionPopup::Property::POPUP_DIVIDER_COLOR:
208       {
209         impl.mDividerColor = value.Get< Vector4 >();
210         break;
211       }
212       case Toolkit::TextSelectionPopup::Property::POPUP_ICON_COLOR:
213       {
214         impl.mIconColor = value.Get< Vector4 >();
215         break;
216       }
217       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_COLOR:
218       {
219         impl.mPressedColor = value.Get< Vector4 >();
220         break;
221       }
222       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_IMAGE:
223       {
224         impl.SetPressedImage( value.Get< std::string >() );
225         break;
226       }
227       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_IN_DURATION:
228       {
229         impl.mFadeInDuration = value.Get < float >();
230         break;
231       }
232       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_OUT_DURATION:
233       {
234         impl.mFadeOutDuration = value.Get < float >();
235         break;
236       }
237       case Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER:
238       {
239         Property::Map map = value.Get<Property::Map>();
240         impl.CreateBackgroundBorder( map );
241         break;
242       }
243     } // switch
244   } // TextSelectionPopup
245 }
246
247 Property::Value TextSelectionPopup::GetProperty( BaseObject* object, Property::Index index )
248 {
249   Property::Value value;
250
251   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
252
253   if( selectionPopup )
254   {
255     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
256
257     switch( index )
258     {
259       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
260       {
261         value = impl.GetDimensionToCustomise( POPUP_MAXIMUM_SIZE );
262         break;
263       }
264       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
265       {
266         value = impl.GetDimensionToCustomise( OPTION_MAXIMUM_SIZE );
267         break;
268       }
269       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
270       {
271         value = impl.GetDimensionToCustomise( OPTION_MINIMUM_SIZE );
272         break;
273       }
274       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
275       {
276         value = impl.GetDimensionToCustomise( OPTION_DIVIDER_SIZE );
277         break;
278       }
279       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
280       {
281         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD ) );
282         if( image )
283         {
284           value = image.GetUrl();
285         }
286         break;
287       }
288       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
289       {
290         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CUT ) );
291         if( image )
292         {
293           value = image.GetUrl();
294         }
295         break;
296       }
297       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
298       {
299         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::COPY ) );
300         if( image )
301         {
302           value = image.GetUrl();
303         }
304         break;
305       }
306       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
307       {
308         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::PASTE ) );
309         if( image )
310         {
311           value = image.GetUrl();
312         }
313         break;
314       }
315       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
316       {
317         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT ) );
318         if( image )
319         {
320           value = image.GetUrl();
321         }
322         break;
323       }
324       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
325       {
326         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL ) );
327         if( image )
328         {
329           value = image.GetUrl();
330         }
331         break;
332       }
333       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_IMAGE:
334       {
335         value = impl.GetPressedImage();
336         break;
337       }
338       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_IN_DURATION:
339       {
340         value = impl.mFadeInDuration;
341         break;
342       }
343       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_OUT_DURATION:
344       {
345         value = impl.mFadeOutDuration;
346         break;
347       }
348       case Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER:
349       {
350         value = impl.mBackgroundBorderProperties;
351         break;
352       }
353     } // switch
354   }
355   return value;
356 }
357
358 void TextSelectionPopup::EnableButtons( Toolkit::TextSelectionPopup::Buttons buttonsToEnable )
359 {
360   mEnabledButtons = buttonsToEnable;
361   mButtonsChanged = true;
362 }
363
364 void TextSelectionPopup::RaiseAbove( Layer target )
365 {
366   if( mToolbar )
367   {
368     mToolbar.RaiseAbove( target );
369   }
370 }
371
372 void TextSelectionPopup::ShowPopup()
373 {
374   if( ( !mPopupShowing || mButtonsChanged ) &&
375       ( Toolkit::TextSelectionPopup::NONE != mEnabledButtons ) )
376   {
377     Actor self = Self();
378     AddPopupOptionsToToolbar( mShowIcons, mShowCaptions );
379
380     Animation animation = Animation::New( mFadeInDuration );
381     animation.AnimateTo( Property(self, Actor::Property::COLOR_ALPHA), 1.0f  );
382     animation.Play();
383     mPopupShowing = true;
384   }
385 }
386
387 void TextSelectionPopup::HidePopup()
388 {
389   if ( mPopupShowing )
390   {
391     mPopupShowing = false;
392     Actor self = Self();
393     Animation animation = Animation::New( mFadeOutDuration );
394     animation.AnimateTo( Property(self, Actor::Property::COLOR_ALPHA), 0.0f  );
395     animation.FinishedSignal().Connect( this, &TextSelectionPopup::HideAnimationFinished );
396     animation.Play();
397   }
398 }
399
400 void TextSelectionPopup::OnInitialize()
401 {
402   DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::OnInitialize\n" );
403   Actor self = Self();
404   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
405   self.SetProperty( Actor::Property::COLOR_ALPHA, 0.0f );
406
407   // The Popup Control background is a nine-patch image. We clip against this so the
408   // contents are correctly clipped against the edges of the nine-patch.
409   self.SetProperty( Actor::Property::CLIPPING_MODE, ClippingMode::CLIP_CHILDREN );
410 }
411
412 void TextSelectionPopup::OnStageConnection( int depth )
413 {
414   DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::OnStageConnection\n" );
415   // Call the Control::OnStageConnection() to set the depth of the background.
416   Control::OnStageConnection( depth );
417
418   // TextSelectionToolbar::OnStageConnection() will set the depths of all the popup's components.
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.SetName( button.name );
679    option.SetAnimationTime( 0.0f );
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( "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.SetUnselectedImage( "" );
744
745    // 4. Set the pressed option image.
746    // The image can be blank, the color can be used regardless.
747    option.SetSelectedImage( mPressedImage );
748    option.SetProperty( Toolkit::Button::Property::SELECTED_COLOR, mPressedColor );
749    option.SetProperty( Toolkit::Control::Property::STYLE_NAME, TEXT_SELECTION_POPUP_BUTTON_STYLE_NAME );
750
751    // 5 Add option to tool bar
752    mToolbar.AddOption( option );
753
754    // 6. Add the divider
755    if( showDivider )
756    {
757      const Size size( mOptionDividerSize.width, 0.0f ); // Height FILL_TO_PARENT
758
759      Toolkit::Control divider = Toolkit::Control::New();
760 #ifdef DECORATOR_DEBUG
761      divider.SetName("Text's popup divider");
762 #endif
763      divider.SetSize( size );
764      divider.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::HEIGHT );
765      divider.SetBackgroundColor( mDividerColor  );
766      mToolbar.AddDivider( divider );
767    }
768  }
769
770  std::size_t TextSelectionPopup::GetNumberOfEnabledOptions()
771  {
772    std::size_t numberOfOptions = 0u;
773    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
774    {
775      const ButtonRequirement& button( *it );
776      if( button.enabled )
777      {
778        ++numberOfOptions;
779      }
780    }
781
782    return numberOfOptions;
783  }
784
785  void TextSelectionPopup::AddPopupOptionsToToolbar( bool showIcons, bool showCaptions )
786  {
787    DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::AddPopupOptionsToToolbar\n" );
788
789    CreateOrderedListOfPopupOptions();
790
791    mButtonsChanged = false;
792    UnparentAndReset( mToolbar);
793
794    if( !mToolbar )
795    {
796      Actor self = Self();
797      mToolbar = Toolkit::TextSelectionToolbar::New();
798      if ( mPopupMaxSize != Vector2::ZERO ) // If PopupMaxSize property set then apply to Toolbar. Toolbar currently is not retriving this from json
799      {
800        mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, mPopupMaxSize );
801      }
802      mToolbar.SetParentOrigin( ParentOrigin::CENTER );
803 #ifdef DECORATOR_DEBUG
804      mToolbar.SetName("TextSelectionToolbar");
805 #endif
806      self.Add( mToolbar );
807    }
808
809    // Whether to mirror the list of buttons (for right to left languages)
810    bool mirror = false;
811    char* idsLtr = GET_LOCALE_TEXT( IDS_LTR.c_str() );
812    if( NULL != idsLtr )
813    {
814      mirror = ( 0 == strcmp( idsLtr, RTL_DIRECTION.c_str() ) );
815
816      if( mirror )
817      {
818        std::reverse( mOrderListOfButtons.begin(), mOrderListOfButtons.end() );
819      }
820    }
821
822    // Iterate list of buttons and add active ones to Toolbar
823    std::size_t numberOfOptionsRequired =  GetNumberOfEnabledOptions();
824    std::size_t numberOfOptionsAdded = 0u;
825    for( std::vector<ButtonRequirement>::const_iterator it = mOrderListOfButtons.begin(), endIt = mOrderListOfButtons.end(); ( it != endIt ); ++it )
826    {
827      const ButtonRequirement& button( *it );
828      if ( button.enabled )
829      {
830        numberOfOptionsAdded++;
831        AddOption(  button, ( numberOfOptionsAdded < numberOfOptionsRequired ) , showIcons, showCaptions );
832      }
833    }
834
835    if( mirror )
836    {
837      mToolbar.ScrollTo( Vector2( mPopupMaxSize.x, 0.f ) );
838    }
839  }
840
841 void TextSelectionPopup::CreateBackgroundBorder( Property::Map& propertyMap )
842 {
843   // Removes previous image if necessary
844   UnparentAndReset( mBackgroundBorder );
845
846   mBackgroundBorderProperties = propertyMap;
847
848   if( ! propertyMap.Empty() )
849   {
850     mBackgroundBorder = Dali::Toolkit::ImageView::New();
851     mBackgroundBorder.SetProperty( Dali::Toolkit::ImageView::Property::IMAGE, propertyMap );
852     mBackgroundBorder.SetParentOrigin( ParentOrigin::CENTER );
853     mBackgroundBorder.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
854
855     Property::Value* colorValue = propertyMap.Find( "borderColor" );
856     if( colorValue )
857     {
858       Vector4 color;
859       if( ConvertPropertyToColor( *colorValue, color ) )
860       {
861         mBackgroundBorder.SetColor( color );
862       }
863     }
864
865     Self().Add( mBackgroundBorder );
866   }
867 }
868
869 TextSelectionPopup::TextSelectionPopup( TextSelectionPopupCallbackInterface* callbackInterface )
870 : Control( ControlBehaviour( CONTROL_BEHAVIOUR_DEFAULT ) ),
871   mToolbar(),
872   mPopupMaxSize(),
873   mOptionMaxSize(),
874   mOptionMinSize(),
875   mOptionDividerSize(),
876   mEnabledButtons( Toolkit::TextSelectionPopup::NONE ),
877   mCallbackInterface( callbackInterface ),
878   mPressedColor( DEFAULT_OPTION_PRESSED_COLOR ),
879   mDividerColor( Color::WHITE ),
880   mIconColor( Color::WHITE ),
881   mSelectOptionPriority( 1 ),
882   mSelectAllOptionPriority ( 2 ),
883   mCutOptionPriority ( 4 ),
884   mCopyOptionPriority ( 3 ),
885   mPasteOptionPriority ( 5 ),
886   mClipboardOptionPriority( 6 ),
887   mFadeInDuration(0.0f),
888   mFadeOutDuration(0.0f),
889   mShowIcons( false ),
890   mShowCaptions( true ),
891   mPopupShowing( false ),
892   mButtonsChanged( false )
893 {
894 }
895
896 TextSelectionPopup::~TextSelectionPopup()
897 {
898 }
899
900
901 } // namespace Internal
902
903 } // namespace Toolkit
904
905 } // namespace Dali