6ea83c0f086d7286b70effd7ba2d83d5297aa2dc
[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/buttons/button-devel.h>
36 #include <dali-toolkit/devel-api/controls/control-depth-index-ranges.h>
37 #include <dali-toolkit/devel-api/controls/text-controls/text-selection-popup-callback-interface.h>
38 #include <dali-toolkit/devel-api/visuals/text-visual-properties.h>
39 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
40 #include <dali-toolkit/internal/helpers/color-conversion.h>
41 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
42
43 namespace Dali
44 {
45
46 namespace Toolkit
47 {
48
49 namespace Internal
50 {
51
52 namespace
53 {
54
55 #define GET_LOCALE_TEXT(string) dgettext("dali-toolkit", string)
56
57 const std::string TEXT_SELECTION_POPUP_BUTTON_STYLE_NAME( "TextSelectionPopupButton" );
58 const Dali::Vector4 DEFAULT_OPTION_PRESSED_COLOR( Dali::Vector4( 0.24f, 0.72f, 0.8f, 1.0f ) );
59
60 #if defined(DEBUG_ENABLED)
61   Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, true, "LOG_TEXT_CONTROLS");
62 #endif
63
64 #ifdef DGETTEXT_ENABLED
65
66 #define POPUP_CUT_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CUT")
67 #define POPUP_COPY_STRING GET_LOCALE_TEXT("IDS_COM_BODY_COPY")
68 #define POPUP_PASTE_STRING GET_LOCALE_TEXT("IDS_COM_BODY_PASTE")
69 #define POPUP_SELECT_STRING GET_LOCALE_TEXT("IDS_COM_SK_SELECT")
70 #define POPUP_SELECT_ALL_STRING GET_LOCALE_TEXT("IDS_COM_BODY_SELECT_ALL")
71 #define POPUP_CLIPBOARD_STRING GET_LOCALE_TEXT("IDS_COM_BODY_CLIPBOARD")
72
73 #else
74
75 #define POPUP_CUT_STRING  "Cut"
76 #define POPUP_COPY_STRING  "Copy"
77 #define POPUP_PASTE_STRING  "Paste"
78 #define POPUP_SELECT_STRING  "Select"
79 #define POPUP_SELECT_ALL_STRING  "Select All"
80 #define POPUP_CLIPBOARD_STRING  "Clipboard"
81
82 #endif
83
84 const char* const OPTION_SELECT_WORD = "option-select_word";                       // "Select Word" popup option.
85 const char* const OPTION_SELECT_ALL("option-select_all");                          // "Select All" popup option.
86 const char* const OPTION_CUT("optionCut");                                        // "Cut" popup option.
87 const char* const OPTION_COPY("optionCopy");                                      // "Copy" popup option.
88 const char* const OPTION_PASTE("optionPaste");                                    // "Paste" popup option.
89 const char* const OPTION_CLIPBOARD("optionClipboard");                            // "Clipboard" popup option.
90
91 BaseHandle Create()
92 {
93   return Toolkit::TextSelectionPopup::New( NULL );
94 }
95
96 // Setup properties, signals and actions using the type-registry.
97
98 DALI_TYPE_REGISTRATION_BEGIN( Toolkit::TextSelectionPopup, Toolkit::Control, Create );
99
100 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupMaxSize", VECTOR2,   POPUP_MAX_SIZE )
101 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupMinSize", VECTOR2,   POPUP_MIN_SIZE )
102 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionMaxSize", VECTOR2,   OPTION_MAX_SIZE )
103 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionMinSize", VECTOR2,   OPTION_MIN_SIZE )
104 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "optionDividerSize", VECTOR2,   OPTION_DIVIDER_SIZE )
105 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupClipboardButtonImage", STRING, POPUP_CLIPBOARD_BUTTON_ICON_IMAGE )
106 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupCutButtonImage", STRING, POPUP_CUT_BUTTON_ICON_IMAGE )
107 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupCopyButtonImage", STRING, POPUP_COPY_BUTTON_ICON_IMAGE )
108 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPasteButtonImage", STRING, POPUP_PASTE_BUTTON_ICON_IMAGE )
109 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupSelectButtonImage", STRING, POPUP_SELECT_BUTTON_ICON_IMAGE )
110 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupSelectAllButtonImage", STRING, POPUP_SELECT_ALL_BUTTON_ICON_IMAGE )
111 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupDividerColor", VECTOR4, POPUP_DIVIDER_COLOR )
112 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupIconColor", VECTOR4, POPUP_ICON_COLOR )
113 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPressedColor", VECTOR4, POPUP_PRESSED_COLOR )
114 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupPressedImage", STRING, POPUP_PRESSED_IMAGE )
115 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupFadeInDuration", FLOAT, POPUP_FADE_IN_DURATION )
116 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "popupFadeOutDuration", FLOAT, POPUP_FADE_OUT_DURATION )
117 DALI_PROPERTY_REGISTRATION( Toolkit, TextSelectionPopup, "backgroundBorder", MAP, BACKGROUND_BORDER )
118
119 DALI_TYPE_REGISTRATION_END()
120
121 } // namespace
122
123
124 Dali::Toolkit::TextSelectionPopup TextSelectionPopup::New( TextSelectionPopupCallbackInterface* callbackInterface )
125 {
126   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "TextSelectionPopup::New\n" );
127
128    // Create the implementation, temporarily owned by this handle on stack
129   IntrusivePtr< TextSelectionPopup > impl = new TextSelectionPopup( callbackInterface );
130
131   // Pass ownership to CustomActor handle
132   Dali::Toolkit::TextSelectionPopup handle( *impl );
133
134   // Second-phase init of the implementation
135   // This can only be done after the CustomActor connection has been made...
136   impl->Initialize();
137
138   return handle;
139 }
140
141 void TextSelectionPopup::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
142 {
143   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
144
145   if( selectionPopup )
146   {
147     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
148
149     switch( index )
150     {
151       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
152       {
153        impl.SetDimensionToCustomise( POPUP_MAXIMUM_SIZE, value.Get< Vector2 >() );
154        break;
155       }
156       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
157       {
158         impl.SetDimensionToCustomise( OPTION_MAXIMUM_SIZE, value.Get< Vector2 >() );
159         break;
160       }
161       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
162       {
163         impl.SetDimensionToCustomise( OPTION_MINIMUM_SIZE, value.Get< Vector2>() );
164         break;
165       }
166       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
167       {
168         impl.SetDimensionToCustomise( OPTION_DIVIDER_SIZE, value.Get< Vector2>() );
169         break;
170       }
171       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
172       {
173         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
174         impl.SetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD, image );
175         break;
176       }
177       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
178       {
179         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
180         impl.SetButtonImage( Toolkit::TextSelectionPopup::CUT, image );
181         break;
182       }
183       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
184       {
185         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
186         impl.SetButtonImage( Toolkit::TextSelectionPopup::COPY, image );
187         break;
188       }
189       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
190       {
191         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
192         impl.SetButtonImage( Toolkit::TextSelectionPopup::PASTE, image );
193         break;
194       }
195       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
196       {
197         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
198         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT, image );
199         break;
200       }
201       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
202       {
203         ResourceImage image = ResourceImage::New( value.Get< std::string >() );
204         impl.SetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL, image );
205         break;
206       }
207       case Toolkit::TextSelectionPopup::Property::POPUP_DIVIDER_COLOR:
208       {
209         impl.mDividerColor = value.Get< Vector4 >();
210         break;
211       }
212       case Toolkit::TextSelectionPopup::Property::POPUP_ICON_COLOR:
213       {
214         impl.mIconColor = value.Get< Vector4 >();
215         break;
216       }
217       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_COLOR:
218       {
219         impl.mPressedColor = value.Get< Vector4 >();
220         break;
221       }
222       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_IMAGE:
223       {
224         impl.SetPressedImage( value.Get< std::string >() );
225         break;
226       }
227       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_IN_DURATION:
228       {
229         impl.mFadeInDuration = value.Get < float >();
230         break;
231       }
232       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_OUT_DURATION:
233       {
234         impl.mFadeOutDuration = value.Get < float >();
235         break;
236       }
237       case Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER:
238       {
239         Property::Map map = value.Get<Property::Map>();
240         impl.CreateBackgroundBorder( map );
241         break;
242       }
243     } // switch
244   } // TextSelectionPopup
245 }
246
247 Property::Value TextSelectionPopup::GetProperty( BaseObject* object, Property::Index index )
248 {
249   Property::Value value;
250
251   Toolkit::TextSelectionPopup selectionPopup = Toolkit::TextSelectionPopup::DownCast( Dali::BaseHandle( object ) );
252
253   if( selectionPopup )
254   {
255     TextSelectionPopup& impl( GetImpl( selectionPopup ) );
256
257     switch( index )
258     {
259       case Toolkit::TextSelectionPopup::Property::POPUP_MAX_SIZE:
260       {
261         value = impl.GetDimensionToCustomise( POPUP_MAXIMUM_SIZE );
262         break;
263       }
264       case Toolkit::TextSelectionPopup::Property::OPTION_MAX_SIZE:
265       {
266         value = impl.GetDimensionToCustomise( OPTION_MAXIMUM_SIZE );
267         break;
268       }
269       case Toolkit::TextSelectionPopup::Property::OPTION_MIN_SIZE:
270       {
271         value = impl.GetDimensionToCustomise( OPTION_MINIMUM_SIZE );
272         break;
273       }
274       case Toolkit::TextSelectionPopup::Property::OPTION_DIVIDER_SIZE:
275       {
276         value = impl.GetDimensionToCustomise( OPTION_DIVIDER_SIZE );
277         break;
278       }
279       case Toolkit::TextSelectionPopup::Property::POPUP_CLIPBOARD_BUTTON_ICON_IMAGE:
280       {
281         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CLIPBOARD ) );
282         if( image )
283         {
284           value = image.GetUrl();
285         }
286         break;
287       }
288       case Toolkit::TextSelectionPopup::Property::POPUP_CUT_BUTTON_ICON_IMAGE:
289       {
290         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::CUT ) );
291         if( image )
292         {
293           value = image.GetUrl();
294         }
295         break;
296       }
297       case Toolkit::TextSelectionPopup::Property::POPUP_COPY_BUTTON_ICON_IMAGE:
298       {
299         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::COPY ) );
300         if( image )
301         {
302           value = image.GetUrl();
303         }
304         break;
305       }
306       case Toolkit::TextSelectionPopup::Property::POPUP_PASTE_BUTTON_ICON_IMAGE:
307       {
308         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::PASTE ) );
309         if( image )
310         {
311           value = image.GetUrl();
312         }
313         break;
314       }
315       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_BUTTON_ICON_IMAGE:
316       {
317         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT ) );
318         if( image )
319         {
320           value = image.GetUrl();
321         }
322         break;
323       }
324       case Toolkit::TextSelectionPopup::Property::POPUP_SELECT_ALL_BUTTON_ICON_IMAGE:
325       {
326         ResourceImage image = ResourceImage::DownCast( impl.GetButtonImage( Toolkit::TextSelectionPopup::SELECT_ALL ) );
327         if( image )
328         {
329           value = image.GetUrl();
330         }
331         break;
332       }
333       case Toolkit::TextSelectionPopup::Property::POPUP_PRESSED_IMAGE:
334       {
335         value = impl.GetPressedImage();
336         break;
337       }
338       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_IN_DURATION:
339       {
340         value = impl.mFadeInDuration;
341         break;
342       }
343       case Toolkit::TextSelectionPopup::Property::POPUP_FADE_OUT_DURATION:
344       {
345         value = impl.mFadeOutDuration;
346         break;
347       }
348       case Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER:
349       {
350         Property::Map map;
351         Toolkit::Visual::Base visual = impl.GetVisual( Toolkit::TextSelectionPopup::Property::BACKGROUND_BORDER );
352         if( visual )
353         {
354           visual.CreatePropertyMap( map );
355         }
356         value = map;
357         break;
358       }
359     } // switch
360   }
361   return value;
362 }
363
364 void TextSelectionPopup::EnableButtons( Toolkit::TextSelectionPopup::Buttons buttonsToEnable )
365 {
366   mEnabledButtons = buttonsToEnable;
367   mButtonsChanged = true;
368 }
369
370 void TextSelectionPopup::RaiseAbove( Layer target )
371 {
372   if( mToolbar )
373   {
374     mToolbar.RaiseAbove( target );
375   }
376 }
377
378 void TextSelectionPopup::ShowPopup()
379 {
380   if( ( !mPopupShowing || mButtonsChanged ) &&
381       ( Toolkit::TextSelectionPopup::NONE != mEnabledButtons ) )
382   {
383     Actor self = Self();
384     AddPopupOptionsToToolbar( mShowIcons, mShowCaptions );
385
386     Animation animation = Animation::New( mFadeInDuration );
387     animation.AnimateTo( Property(self, Actor::Property::COLOR_ALPHA), 1.0f  );
388     animation.Play();
389     mPopupShowing = true;
390   }
391 }
392
393 void TextSelectionPopup::HidePopup()
394 {
395   if ( mPopupShowing )
396   {
397     mPopupShowing = false;
398     Actor self = Self();
399     Animation animation = Animation::New( mFadeOutDuration );
400     animation.AnimateTo( Property(self, Actor::Property::COLOR_ALPHA), 0.0f  );
401     animation.FinishedSignal().Connect( this, &TextSelectionPopup::HideAnimationFinished );
402     animation.Play();
403   }
404 }
405
406 void TextSelectionPopup::OnInitialize()
407 {
408   DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::OnInitialize\n" );
409   Actor self = Self();
410   self.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
411   self.SetProperty( Actor::Property::COLOR_ALPHA, 0.0f );
412
413   // The Popup Control background is a nine-patch image. We clip against this so the
414   // contents are correctly clipped against the edges of the nine-patch.
415   self.SetProperty( Actor::Property::CLIPPING_MODE, ClippingMode::CLIP_CHILDREN );
416 }
417
418 void TextSelectionPopup::HideAnimationFinished( Animation& animation )
419 {
420   Actor self = Self();
421   if ( !mPopupShowing ) // During the Hide/Fade animation there could be a call to Show the Popup again, mPopupShowing will be true in this case.
422   {
423     DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::HideAnimationFinished\n" );
424     UnparentAndReset( mToolbar );
425   }
426 }
427
428 bool TextSelectionPopup::OnCutButtonPressed( Toolkit::Button button )
429 {
430   if( mCallbackInterface )
431   {
432     mCallbackInterface->TextPopupButtonTouched( Toolkit::TextSelectionPopup::CUT );
433   }
434
435   return true;
436 }
437
438 bool TextSelectionPopup::OnCopyButtonPressed( Toolkit::Button button )
439 {
440   if( mCallbackInterface )
441   {
442     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::COPY );
443   }
444
445   return true;
446 }
447
448 bool TextSelectionPopup::OnPasteButtonPressed( Toolkit::Button button )
449 {
450   if( mCallbackInterface )
451   {
452     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::PASTE );
453   }
454
455   return true;
456 }
457
458 bool TextSelectionPopup::OnSelectButtonPressed( Toolkit::Button button )
459 {
460   if( mCallbackInterface )
461   {
462     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::SELECT );
463   }
464
465   return true;
466 }
467
468 bool TextSelectionPopup::OnSelectAllButtonPressed( Toolkit::Button button )
469 {
470   if( mCallbackInterface )
471   {
472     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::SELECT_ALL );
473   }
474
475   return true;
476 }
477
478 bool TextSelectionPopup::OnClipboardButtonPressed( Toolkit::Button button )
479 {
480   if( mCallbackInterface )
481   {
482     mCallbackInterface->TextPopupButtonTouched( Dali::Toolkit::TextSelectionPopup::CLIPBOARD );
483   }
484
485   return true;
486 }
487
488 void TextSelectionPopup::SetDimensionToCustomise( const PopupCustomisations& settingToCustomise, const Size& dimension )
489 {
490   switch( settingToCustomise )
491   {
492     case POPUP_MAXIMUM_SIZE :
493     {
494       mPopupMaxSize = dimension;
495       if ( mToolbar )
496       {
497         mToolbar.SetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE, dimension );
498       }
499       break;
500     }
501     case OPTION_MAXIMUM_SIZE :
502     {
503       mOptionMaxSize = dimension;
504       // Option max size not currently currently supported
505       break;
506     }
507     case OPTION_MINIMUM_SIZE :
508     {
509       mOptionMinSize = dimension;
510       // Option min size not currently currently supported
511       break;
512     }
513     case OPTION_DIVIDER_SIZE :
514     {
515       mOptionDividerSize = dimension;
516       if ( mToolbar )
517       {
518         // Resize Dividers not currently supported
519       }
520       break;
521     }
522   } // switch
523 }
524
525 Size TextSelectionPopup::GetDimensionToCustomise( const PopupCustomisations& settingToCustomise )
526 {
527   switch( settingToCustomise )
528   {
529     case POPUP_MAXIMUM_SIZE :
530     {
531       if ( mToolbar )
532       {
533         return mToolbar.GetProperty( Toolkit::TextSelectionToolbar::Property::MAX_SIZE ).Get< Vector2 >();
534       }
535       else
536       {
537         return mPopupMaxSize;
538       }
539     }
540     case OPTION_MAXIMUM_SIZE :
541     {
542       return mOptionMaxSize;
543     }
544     case OPTION_MINIMUM_SIZE :
545     {
546       return mOptionMinSize;
547     }
548     case OPTION_DIVIDER_SIZE :
549     {
550       return mOptionDividerSize;
551     }
552   } // switch
553
554   return Size::ZERO;
555 }
556
557 void TextSelectionPopup::SetButtonImage( Toolkit::TextSelectionPopup::Buttons button, Dali::Image image )
558 {
559    switch ( button )
560    {
561    break;
562    case Toolkit::TextSelectionPopup::CLIPBOARD:
563    {
564      mClipboardIconImage  = image;
565    }
566    break;
567    case Toolkit::TextSelectionPopup::CUT :
568    {
569      mCutIconImage = image;
570    }
571    break;
572    case Toolkit::TextSelectionPopup::COPY :
573    {
574      mCopyIconImage = image;
575    }
576    break;
577    case Toolkit::TextSelectionPopup::PASTE :
578    {
579      mPasteIconImage = image;
580    }
581    break;
582    case Toolkit::TextSelectionPopup::SELECT :
583    {
584      mSelectIconImage = image;
585    }
586    break;
587    case Toolkit::TextSelectionPopup::SELECT_ALL :
588    {
589      mSelectAllIconImage = image;
590    }
591    break;
592    default :
593    {
594      DALI_ASSERT_DEBUG( "TextSelectionPopup SetPopupImage Unknown Button" );
595    }
596    } // switch
597 }
598
599 Dali::Image TextSelectionPopup::GetButtonImage( Toolkit::TextSelectionPopup::Buttons button )
600 {
601   switch ( button )
602   {
603   case Toolkit::TextSelectionPopup::CLIPBOARD :
604   {
605     return mClipboardIconImage;
606   }
607   break;
608   case Toolkit::TextSelectionPopup::CUT :
609   {
610     return mCutIconImage;
611   }
612   break;
613   case Toolkit::TextSelectionPopup::COPY :
614   {
615     return mCopyIconImage;
616   }
617   break;
618   case Toolkit::TextSelectionPopup::PASTE :
619   {
620     return mPasteIconImage;
621   }
622   break;
623   case Toolkit::TextSelectionPopup::SELECT :
624   {
625     return mSelectIconImage;
626   }
627   break;
628   case Toolkit::TextSelectionPopup::SELECT_ALL :
629   {
630     return mSelectAllIconImage;
631   }
632   break;
633   default :
634   {
635     DALI_ASSERT_DEBUG( "TextSelectionPopup GetPopupImage Unknown Button" );
636   }
637   } // switch
638
639   return Dali::Image();
640 }
641
642 void TextSelectionPopup::SetPressedImage( const std::string& filename )
643 {
644   mPressedImage = filename;
645 }
646
647 std::string TextSelectionPopup::GetPressedImage() const
648 {
649   return mPressedImage;
650 }
651
652  void TextSelectionPopup::CreateOrderedListOfPopupOptions()
653  {
654    mOrderListOfButtons.clear();
655    mOrderListOfButtons.reserve( 8u );
656
657    // Create button for each possible option using Option priority
658    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CUT, mCutOptionPriority, OPTION_CUT, POPUP_CUT_STRING , mCutIconImage, 0 != ( mEnabledButtons & Toolkit::TextSelectionPopup::CUT)  ) );
659    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::COPY, mCopyOptionPriority, OPTION_COPY, POPUP_COPY_STRING, mCopyIconImage, 0 != ( mEnabledButtons & Toolkit::TextSelectionPopup::COPY)  ) );
660    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::PASTE, mPasteOptionPriority, OPTION_PASTE, POPUP_PASTE_STRING, mPasteIconImage, 0 != ( mEnabledButtons & Toolkit::TextSelectionPopup::PASTE)  ) );
661    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT, mSelectOptionPriority, OPTION_SELECT_WORD, POPUP_SELECT_STRING, mSelectIconImage, 0 != ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT)  ) );
662    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::SELECT_ALL, mSelectAllOptionPriority, OPTION_SELECT_ALL, POPUP_SELECT_ALL_STRING, mSelectAllIconImage, 0 != ( mEnabledButtons & Toolkit::TextSelectionPopup::SELECT_ALL)  ) );
663    mOrderListOfButtons.push_back( ButtonRequirement( Toolkit::TextSelectionPopup::CLIPBOARD, mClipboardOptionPriority, OPTION_CLIPBOARD, POPUP_CLIPBOARD_STRING, mClipboardIconImage, 0 != ( mEnabledButtons & Toolkit::TextSelectionPopup::CLIPBOARD)  ) );
664
665    // Sort the buttons according their priorities.
666    std::sort( mOrderListOfButtons.begin(), mOrderListOfButtons.end(), TextSelectionPopup::ButtonPriorityCompare() );
667  }
668
669  void TextSelectionPopup::AddOption( const ButtonRequirement& button, bool showDivider, bool showIcons, bool showCaption  )
670  {
671    // 1. Create a option.
672    DALI_LOG_INFO( gLogFilter, Debug::General, "TextSelectionPopup::AddOption\n" );
673
674    Toolkit::PushButton option = Toolkit::PushButton::New();
675    option.SetName( button.name );
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.SetProperty( Toolkit::DevelButton::Property::UNSELECTED_BACKGROUND_VISUAL, "" );
740
741    // 4. Set the pressed option image.
742    // The image can be blank, the color can be used regardless.
743    option.SetProperty( Toolkit::DevelButton::Property::SELECTED_BACKGROUND_VISUAL, 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