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