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