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